pascal.el 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  1. ;;; pascal.el --- major mode for editing pascal source in Emacs -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. ;; Author: Espen Skoglund <esk@gnu.org>
  4. ;; Keywords: languages
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; USAGE
  18. ;; =====
  19. ;; Emacs should enter Pascal mode when you find a Pascal source file.
  20. ;; When you have entered Pascal mode, you can get more info by pressing
  21. ;; C-h m. You can also get help describing various functions by:
  22. ;; C-h f <Name of function you want described>
  23. ;; If you want to customize Pascal mode to fit you better, you may add
  24. ;; these lines (the values of the variables presented here are the defaults):
  25. ;;
  26. ;; ;; User customization for Pascal mode
  27. ;; (setq pascal-indent-level 3
  28. ;; pascal-case-indent 2
  29. ;; pascal-auto-newline nil
  30. ;; pascal-tab-always-indent t
  31. ;; pascal-auto-endcomments t
  32. ;; pascal-auto-lineup '(all)
  33. ;; pascal-type-keywords '("array" "file" "packed" "char"
  34. ;; "integer" "real" "string" "record")
  35. ;; pascal-start-keywords '("begin" "end" "function" "procedure"
  36. ;; "repeat" "until" "while" "read" "readln"
  37. ;; "reset" "rewrite" "write" "writeln")
  38. ;; pascal-separator-keywords '("downto" "else" "mod" "div" "then"))
  39. ;; KNOWN BUGS / BUGREPORTS
  40. ;; =======================
  41. ;; As far as I know, there are no bugs in the current version of this
  42. ;; package. This may not be true however, since I never use this mode
  43. ;; myself and therefore would never notice them anyway. If you do
  44. ;; find any bugs, you may submit them to: esk@gnu.org as well as to
  45. ;; bug-gnu-emacs@gnu.org.
  46. ;;; Code:
  47. (defgroup pascal nil
  48. "Major mode for editing Pascal source in Emacs."
  49. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  50. :group 'languages)
  51. (defvar pascal-mode-abbrev-table nil
  52. "Abbrev table in use in Pascal mode buffers.")
  53. (define-abbrev-table 'pascal-mode-abbrev-table ())
  54. (defvar pascal-mode-map
  55. (let ((map (make-sparse-keymap)))
  56. (define-key map ";" 'electric-pascal-semi-or-dot)
  57. (define-key map "." 'electric-pascal-semi-or-dot)
  58. (define-key map ":" 'electric-pascal-colon)
  59. (define-key map "=" 'electric-pascal-equal)
  60. (define-key map "#" 'electric-pascal-hash)
  61. ;; These are user preferences, so not to set by default.
  62. ;;(define-key map "\r" 'electric-pascal-terminate-line)
  63. ;;(define-key map "\t" 'electric-pascal-tab)
  64. (define-key map "\M-\t" 'completion-at-point)
  65. (define-key map "\M-?" 'completion-help-at-point)
  66. (define-key map "\177" 'backward-delete-char-untabify)
  67. (define-key map "\M-\C-h" 'pascal-mark-defun)
  68. (define-key map "\C-c\C-b" 'pascal-insert-block)
  69. (define-key map "\M-*" 'pascal-star-comment)
  70. (define-key map "\C-c\C-c" 'pascal-comment-area)
  71. (define-key map "\C-c\C-u" 'pascal-uncomment-area)
  72. (define-key map "\M-\C-a" 'pascal-beg-of-defun)
  73. (define-key map "\M-\C-e" 'pascal-end-of-defun)
  74. (define-key map "\C-c\C-d" 'pascal-goto-defun)
  75. (define-key map "\C-c\C-o" 'pascal-outline-mode)
  76. ;; A command to change the whole buffer won't be used terribly
  77. ;; often, so no need for a key binding.
  78. ;; (define-key map "\C-cd" 'pascal-downcase-keywords)
  79. ;; (define-key map "\C-cu" 'pascal-upcase-keywords)
  80. ;; (define-key map "\C-cc" 'pascal-capitalize-keywords)
  81. map)
  82. "Keymap used in Pascal mode.")
  83. (defvar pascal-imenu-generic-expression
  84. '((nil "^[ \t]*\\(function\\|procedure\\)[ \t\n]+\\([a-zA-Z0-9_.:]+\\)" 2))
  85. "Imenu expression for Pascal mode. See `imenu-generic-expression'.")
  86. (defvar pascal-keywords
  87. '("and" "array" "begin" "case" "const" "div" "do" "downto" "else" "end"
  88. "file" "for" "function" "goto" "if" "in" "label" "mod" "nil" "not" "of"
  89. "or" "packed" "procedure" "program" "record" "repeat" "set" "then" "to"
  90. "type" "until" "var" "while" "with"
  91. ;; The following are not standard in pascal, but widely used.
  92. "get" "put" "input" "output" "read" "readln" "reset" "rewrite" "write"
  93. "writeln"))
  94. ;;;
  95. ;;; Regular expressions used to calculate indent, etc.
  96. ;;;
  97. (defconst pascal-symbol-re "\\<[a-zA-Z_][a-zA-Z_0-9.]*\\>")
  98. (defconst pascal-beg-block-re "\\<\\(begin\\|case\\|record\\|repeat\\)\\>")
  99. (defconst pascal-end-block-re "\\<\\(end\\|until\\)\\>")
  100. (defconst pascal-declaration-re "\\<\\(const\\|label\\|type\\|var\\)\\>")
  101. (defconst pascal-progbeg-re "\\<\\program\\>")
  102. (defconst pascal-defun-re "\\<\\(function\\|procedure\\|program\\)\\>")
  103. (defconst pascal-sub-block-re "\\<\\(if\\|else\\|for\\|while\\|with\\)\\>")
  104. (defconst pascal-noindent-re "\\<\\(begin\\|end\\|until\\|else\\)\\>")
  105. (defconst pascal-nosemi-re "\\<\\(begin\\|repeat\\|then\\|do\\|else\\)\\>")
  106. (defconst pascal-autoindent-lines-re
  107. "\\<\\(label\\|var\\|type\\|const\\|until\\|end\\|begin\\|repeat\\|else\\)\\>")
  108. ;;; Strings used to mark beginning and end of excluded text
  109. (defconst pascal-exclude-str-start "{-----\\/----- EXCLUDED -----\\/-----"
  110. "String used to mark beginning of excluded text.")
  111. (defconst pascal-exclude-str-end " -----/\\----- EXCLUDED -----/\\-----}"
  112. "String used to mark end of excluded text.")
  113. (defvar pascal-mode-syntax-table
  114. (let ((st (make-syntax-table)))
  115. (modify-syntax-entry ?\\ "." st)
  116. (modify-syntax-entry ?\( "()1" st)
  117. (modify-syntax-entry ?\) ")(4" st)
  118. ;; This used to use comment-syntax `b'. But the only document I could
  119. ;; find about the syntax of Pascal's comments said that (* ... } is
  120. ;; a valid comment, just as { ... *) or (* ... *) or { ... }.
  121. (modify-syntax-entry ?* ". 23" st)
  122. ;; Allow //...\n comments as accepted by Free Pascal (bug#13585).
  123. (modify-syntax-entry ?/ ". 12c" st)
  124. (modify-syntax-entry ?\n "> c" st)
  125. (modify-syntax-entry ?{ "<" st)
  126. (modify-syntax-entry ?} ">" st)
  127. (modify-syntax-entry ?+ "." st)
  128. (modify-syntax-entry ?- "." st)
  129. (modify-syntax-entry ?= "." st)
  130. (modify-syntax-entry ?% "." st)
  131. (modify-syntax-entry ?< "." st)
  132. (modify-syntax-entry ?> "." st)
  133. (modify-syntax-entry ?& "." st)
  134. (modify-syntax-entry ?| "." st)
  135. (modify-syntax-entry ?_ "_" st)
  136. (modify-syntax-entry ?\' "\"" st)
  137. st)
  138. "Syntax table in use in Pascal-mode buffers.")
  139. (defconst pascal-font-lock-keywords
  140. `(("\\_<\\(function\\|pro\\(cedure\\|gram\\)\\)[ \t]+\\([[:alpha:]][[:alnum:]_]*\\)"
  141. (1 font-lock-keyword-face)
  142. (3 font-lock-function-name-face))
  143. ;; ("type" "const" "real" "integer" "char" "boolean" "var"
  144. ;; "record" "array" "file")
  145. (,(concat "\\_<\\(array\\|boolean\\|c\\(har\\|onst\\)\\|file\\|"
  146. "integer\\|re\\(al\\|cord\\)\\|type\\|var\\)\\_>")
  147. . font-lock-type-face)
  148. ("\\_<\\(label\\|external\\|forward\\)\\_>" . font-lock-constant-face)
  149. ("\\_<\\([0-9]+\\)[ \t]*:" 1 font-lock-function-name-face)
  150. ;; ("of" "to" "for" "if" "then" "else" "case" "while"
  151. ;; "do" "until" "and" "or" "not" "in" "with" "repeat" "begin" "end")
  152. ,(concat "\\_<\\("
  153. "and\\|begin\\|case\\|do\\|e\\(lse\\|nd\\)\\|for\\|i[fn]\\|"
  154. "not\\|o[fr]\\|repeat\\|t\\(hen\\|o\\)\\|until\\|w\\(hile\\|ith\\)"
  155. "\\)\\_>")
  156. ("\\_<\\(goto\\)\\_>[ \t]*\\([0-9]+\\)?"
  157. (1 font-lock-keyword-face) (2 font-lock-keyword-face t)))
  158. "Additional expressions to highlight in Pascal mode.")
  159. (defconst pascal--syntax-propertize
  160. (syntax-propertize-rules
  161. ;; The syntax-table settings are too coarse and end up treating /* and (/
  162. ;; as comment starters. Fix it here by removing the "2" from the syntax
  163. ;; of the second char of such sequences.
  164. ("/\\(\\*\\)" (1 ". 3b"))
  165. ("(\\(\\/\\)" (1 (prog1 ". 1c" (forward-char -1) nil)))
  166. ;; Pascal uses '' and "" rather than \' and \" to escape quotes.
  167. ("''\\|\"\"" (0 (if (save-excursion
  168. (nth 3 (syntax-ppss (match-beginning 0))))
  169. (string-to-syntax ".")
  170. ;; In case of 3 or more quotes in a row, only advance
  171. ;; one quote at a time.
  172. (forward-char -1)
  173. nil)))))
  174. (defcustom pascal-indent-level 3
  175. "Indentation of Pascal statements with respect to containing block."
  176. :type 'integer
  177. :group 'pascal)
  178. (defcustom pascal-case-indent 2
  179. "Indentation for case statements."
  180. :type 'integer
  181. :group 'pascal)
  182. (defcustom pascal-auto-newline nil
  183. "Non-nil means automatically insert newlines in certain cases.
  184. These include after semicolons and after the punctuation mark after an `end'."
  185. :type 'boolean
  186. :group 'pascal)
  187. (defcustom pascal-indent-nested-functions t
  188. "Non-nil means nested functions are indented."
  189. :type 'boolean
  190. :group 'pascal)
  191. (defcustom pascal-tab-always-indent t
  192. "Non-nil means TAB in Pascal mode should always reindent the current line.
  193. If this is nil, TAB inserts a tab if it is at the end of the line
  194. and follows non-whitespace text."
  195. :type 'boolean
  196. :group 'pascal)
  197. (defcustom pascal-auto-endcomments t
  198. "Non-nil means automatically insert comments after certain `end's.
  199. Specifically, this is done after the ends of case statements and functions.
  200. The name of the function or case is included between the braces."
  201. :type 'boolean
  202. :group 'pascal)
  203. (defcustom pascal-auto-lineup '(all)
  204. "List of contexts where auto lineup of :'s or ='s should be done.
  205. Elements can be of type: `paramlist', `declaration' or `case', which will
  206. do auto lineup in parameterlist, declarations or case-statements
  207. respectively. The word `all' will do all lineups. '(case paramlist) for
  208. instance will do lineup in case-statements and parameterlist, while '(all)
  209. will do all lineups."
  210. :type '(set :extra-offset 8
  211. (const :tag "Everything" all)
  212. (const :tag "Parameter lists" paramlist)
  213. (const :tag "Declarations" declaration)
  214. (const :tag "Case statements" case))
  215. :group 'pascal)
  216. (defvar pascal-toggle-completions nil
  217. "If non-nil, `pascal-complete-word' tries all possible completions.
  218. Repeated use of \\[pascal-complete-word] then shows all
  219. completions in turn, instead of displaying a list of all possible
  220. completions.")
  221. (make-obsolete-variable 'pascal-toggle-completions
  222. 'completion-cycle-threshold "24.1")
  223. (defcustom pascal-type-keywords
  224. '("array" "file" "packed" "char" "integer" "real" "string" "record")
  225. "Keywords for types used when completing a word in a declaration or parmlist.
  226. These include integer, real, char, etc.
  227. The types defined within the Pascal program
  228. are handled in another way, and should not be added to this list."
  229. :type '(repeat (string :tag "Keyword"))
  230. :group 'pascal)
  231. (defcustom pascal-start-keywords
  232. '("begin" "end" "function" "procedure" "repeat" "until" "while"
  233. "read" "readln" "reset" "rewrite" "write" "writeln")
  234. "Keywords to complete when standing at the first word of a statement.
  235. These are keywords such as begin, repeat, until, readln.
  236. The procedures and variables defined within the Pascal program
  237. are handled in another way, and should not be added to this list."
  238. :type '(repeat (string :tag "Keyword"))
  239. :group 'pascal)
  240. (defcustom pascal-separator-keywords
  241. '("downto" "else" "mod" "div" "then")
  242. "Keywords to complete when NOT standing at the first word of a statement.
  243. These are keywords such as downto, else, mod, then.
  244. Variables and function names defined within the Pascal program
  245. are handled in another way, and should not be added to this list."
  246. :type '(repeat (string :tag "Keyword"))
  247. :group 'pascal)
  248. ;;;
  249. ;;; Macros
  250. ;;;
  251. (defun pascal-declaration-end ()
  252. (let ((nest 1))
  253. (while (and (> nest 0)
  254. (re-search-forward
  255. "[:=]\\|\\(\\<record\\>\\)\\|\\(\\<end\\>\\)"
  256. (point-at-eol 2) t))
  257. (cond ((match-beginning 1) (setq nest (1+ nest)))
  258. ((match-beginning 2) (setq nest (1- nest)))
  259. ((looking-at "[^(\n]+)") (setq nest 0))))))
  260. (defun pascal-declaration-beg ()
  261. (let ((nest 1))
  262. (while (and (> nest 0)
  263. (re-search-backward "[:=]\\|\\<\\(type\\|var\\|label\\|const\\)\\>\\|\\(\\<record\\>\\)\\|\\(\\<end\\>\\)" (point-at-bol 0) t))
  264. (cond ((match-beginning 1) (setq nest 0))
  265. ((match-beginning 2) (setq nest (1- nest)))
  266. ((match-beginning 3) (setq nest (1+ nest)))))
  267. (= nest 0)))
  268. (defsubst pascal-within-string ()
  269. (nth 3 (parse-partial-sexp (point-at-bol) (point))))
  270. ;;;###autoload
  271. (define-derived-mode pascal-mode prog-mode "Pascal"
  272. "Major mode for editing Pascal code.\\<pascal-mode-map>
  273. TAB indents for Pascal code. Delete converts tabs to spaces as it moves back.
  274. \\[completion-at-point] completes the word around current point with respect \
  275. to position in code
  276. \\[completion-help-at-point] shows all possible completions at this point.
  277. Other useful functions are:
  278. \\[pascal-mark-defun]\t- Mark function.
  279. \\[pascal-insert-block]\t- insert begin ... end;
  280. \\[pascal-star-comment]\t- insert (* ... *)
  281. \\[pascal-comment-area]\t- Put marked area in a comment, fixing nested comments.
  282. \\[pascal-uncomment-area]\t- Uncomment an area commented with \
  283. \\[pascal-comment-area].
  284. \\[pascal-beg-of-defun]\t- Move to beginning of current function.
  285. \\[pascal-end-of-defun]\t- Move to end of current function.
  286. \\[pascal-goto-defun]\t- Goto function prompted for in the minibuffer.
  287. \\[pascal-outline-mode]\t- Enter `pascal-outline-mode'.
  288. Variables controlling indentation/edit style:
  289. `pascal-indent-level' (default 3)
  290. Indentation of Pascal statements with respect to containing block.
  291. `pascal-case-indent' (default 2)
  292. Indentation for case statements.
  293. `pascal-auto-newline' (default nil)
  294. Non-nil means automatically newline after semicolons and the punctuation
  295. mark after an end.
  296. `pascal-indent-nested-functions' (default t)
  297. Non-nil means nested functions are indented.
  298. `pascal-tab-always-indent' (default t)
  299. Non-nil means TAB in Pascal mode should always reindent the current line,
  300. regardless of where in the line point is when the TAB command is used.
  301. `pascal-auto-endcomments' (default t)
  302. Non-nil means a comment { ... } is set after the ends which ends cases and
  303. functions. The name of the function or case will be set between the braces.
  304. `pascal-auto-lineup' (default t)
  305. List of contexts where auto lineup of :'s or ='s should be done.
  306. See also the user variables `pascal-type-keywords', `pascal-start-keywords' and
  307. `pascal-separator-keywords'."
  308. (setq-local local-abbrev-table pascal-mode-abbrev-table)
  309. (setq-local indent-line-function 'pascal-indent-line)
  310. (setq-local comment-indent-function 'pascal-indent-comment)
  311. (setq-local parse-sexp-ignore-comments nil)
  312. (setq-local blink-matching-paren-dont-ignore-comments t)
  313. (setq-local case-fold-search t)
  314. (setq-local comment-start "{")
  315. (setq-local comment-start-skip "(\\*+ *\\|{ *")
  316. (setq-local comment-end "}")
  317. (add-hook 'completion-at-point-functions 'pascal-completions-at-point nil t)
  318. ;; Font lock support
  319. (setq-local font-lock-defaults '(pascal-font-lock-keywords nil t))
  320. (setq-local syntax-propertize-function pascal--syntax-propertize)
  321. ;; Imenu support
  322. (setq-local imenu-generic-expression pascal-imenu-generic-expression)
  323. (setq-local imenu-case-fold-search t)
  324. ;; Pascal-mode's own hide/show support.
  325. (add-to-invisibility-spec '(pascal . t)))
  326. ;;;
  327. ;;; Electric functions
  328. ;;;
  329. (defun electric-pascal-terminate-line ()
  330. "Terminate line and indent next line."
  331. (interactive)
  332. ;; First, check if current line should be indented
  333. (save-excursion
  334. (beginning-of-line)
  335. (skip-chars-forward " \t")
  336. (if (looking-at pascal-autoindent-lines-re)
  337. (pascal-indent-line)))
  338. (delete-horizontal-space) ; Removes trailing whitespaces
  339. (newline)
  340. ;; Indent next line
  341. (pascal-indent-line)
  342. ;; Maybe we should set some endcomments
  343. (if pascal-auto-endcomments
  344. (pascal-set-auto-comments))
  345. ;; Check if we shall indent inside comment
  346. (let ((setstar nil))
  347. (save-excursion
  348. (forward-line -1)
  349. (skip-chars-forward " \t")
  350. (cond ((looking-at "\\*[ \t]+)")
  351. ;; Delete region between `*' and `)' if there is only whitespaces.
  352. (forward-char 1)
  353. (delete-horizontal-space))
  354. ((and (looking-at "(\\*\\|\\*[^)]")
  355. (not (save-excursion (search-forward "*)" (point-at-eol) t))))
  356. (setq setstar t))))
  357. ;; If last line was a star comment line then this one shall be too.
  358. (if (null setstar)
  359. (pascal-indent-line)
  360. (insert "* "))))
  361. (defun electric-pascal-semi-or-dot ()
  362. "Insert `;' or `.' character and reindent the line."
  363. (interactive)
  364. (insert last-command-event)
  365. (save-excursion
  366. (beginning-of-line)
  367. (pascal-indent-line))
  368. (if pascal-auto-newline
  369. (electric-pascal-terminate-line)))
  370. (defun electric-pascal-colon ()
  371. "Insert `:' and do all indentations except line indent on this line."
  372. (interactive)
  373. (insert last-command-event)
  374. ;; Do nothing if within string.
  375. (if (pascal-within-string)
  376. ()
  377. (save-excursion
  378. (beginning-of-line)
  379. (pascal-indent-line))
  380. (let ((pascal-tab-always-indent nil))
  381. (pascal-indent-command))))
  382. (defun electric-pascal-equal ()
  383. "Insert `=', and do indentation if within type declaration."
  384. (interactive)
  385. (insert last-command-event)
  386. (if (eq (car (pascal-calculate-indent)) 'declaration)
  387. (let ((pascal-tab-always-indent nil))
  388. (pascal-indent-command))))
  389. (defun electric-pascal-hash ()
  390. "Insert `#', and indent to column 0 if this is a CPP directive."
  391. (interactive)
  392. (insert last-command-event)
  393. (if (save-excursion (beginning-of-line) (looking-at "^[ \t]*#"))
  394. (save-excursion (beginning-of-line)
  395. (delete-horizontal-space))))
  396. (defun electric-pascal-tab ()
  397. "Function called when TAB is pressed in Pascal mode."
  398. (interactive)
  399. ;; Do nothing if within a string or in a CPP directive.
  400. (if (or (pascal-within-string)
  401. (and (not (bolp))
  402. (save-excursion (beginning-of-line) (eq (following-char) ?#))))
  403. (insert "\t")
  404. ;; If pascal-tab-always-indent, indent the beginning of the line.
  405. (if pascal-tab-always-indent
  406. (save-excursion
  407. (beginning-of-line)
  408. (pascal-indent-line))
  409. (if (save-excursion
  410. (skip-chars-backward " \t")
  411. (bolp))
  412. (pascal-indent-line)
  413. (insert "\t")))
  414. (pascal-indent-command)))
  415. ;;;
  416. ;;; Interactive functions
  417. ;;;
  418. (defvar pascal--extra-indent 0)
  419. (defun pascal-insert-block ()
  420. "Insert Pascal begin ... end; block in the code with right indentation."
  421. (interactive)
  422. (insert "begin")
  423. (electric-pascal-terminate-line)
  424. (save-excursion
  425. (newline)
  426. (insert "end;")
  427. (beginning-of-line)
  428. (pascal-indent-line)))
  429. (defun pascal-star-comment ()
  430. "Insert Pascal star comment at point."
  431. (interactive)
  432. (pascal-indent-line)
  433. (insert "(*")
  434. (electric-pascal-terminate-line)
  435. (save-excursion
  436. (electric-pascal-terminate-line)
  437. (delete-horizontal-space)
  438. (insert ")"))
  439. (insert " "))
  440. (defun pascal-mark-defun ()
  441. "Mark the current Pascal function (or procedure).
  442. This puts the mark at the end, and point at the beginning."
  443. (interactive)
  444. (push-mark (point))
  445. (pascal-end-of-defun)
  446. (push-mark (point))
  447. (pascal-beg-of-defun)
  448. (when (featurep 'xemacs)
  449. (zmacs-activate-region)))
  450. (defun pascal-comment-area (start end)
  451. "Put the region into a Pascal comment.\\<pascal-mode-map>
  452. The comments that are in this area are \"deformed\":
  453. `*)' becomes `!(*' and `}' becomes `!{'.
  454. These deformed comments are returned to normal if you use
  455. \\[pascal-uncomment-area] to undo the commenting.
  456. The commented area starts with `pascal-exclude-str-start', and ends
  457. with `pascal-exclude-str-end'. But if you change these variables,
  458. \\[pascal-uncomment-area] won't recognize the comments."
  459. (interactive "r")
  460. (save-excursion
  461. ;; Insert start and endcomments
  462. (goto-char end)
  463. (if (and (save-excursion (skip-chars-forward " \t") (eolp))
  464. (not (save-excursion (skip-chars-backward " \t") (bolp))))
  465. (forward-line 1)
  466. (beginning-of-line))
  467. (insert pascal-exclude-str-end)
  468. (setq end (point))
  469. (newline)
  470. (goto-char start)
  471. (beginning-of-line)
  472. (insert pascal-exclude-str-start)
  473. (newline)
  474. ;; Replace end-comments within commented area
  475. (goto-char end)
  476. (save-excursion
  477. (while (re-search-backward "\\*)" start t)
  478. (replace-match "!(*" t t)))
  479. (save-excursion
  480. (while (re-search-backward "}" start t)
  481. (replace-match "!{" t t)))))
  482. (defun pascal-uncomment-area ()
  483. "Uncomment a commented area; change deformed comments back to normal.
  484. This command does nothing if the pointer is not in a commented area.
  485. See also `pascal-comment-area'."
  486. (interactive)
  487. (save-excursion
  488. (let ((start (point))
  489. (end (point)))
  490. ;; Find the boundaries of the comment
  491. (save-excursion
  492. (setq start (progn (search-backward pascal-exclude-str-start nil t)
  493. (point)))
  494. (setq end (progn (search-forward pascal-exclude-str-end nil t)
  495. (point))))
  496. ;; Check if we're really inside a comment
  497. (if (or (equal start (point)) (<= end (point)))
  498. (message "Not standing within commented area.")
  499. (progn
  500. ;; Remove endcomment
  501. (goto-char end)
  502. (beginning-of-line)
  503. (let ((pos (point)))
  504. (end-of-line)
  505. (delete-region pos (1+ (point))))
  506. ;; Change comments back to normal
  507. (save-excursion
  508. (while (re-search-backward "!{" start t)
  509. (replace-match "}" t t)))
  510. (save-excursion
  511. (while (re-search-backward "!(\\*" start t)
  512. (replace-match "*)" t t)))
  513. ;; Remove startcomment
  514. (goto-char start)
  515. (beginning-of-line)
  516. (let ((pos (point)))
  517. (end-of-line)
  518. (delete-region pos (1+ (point)))))))))
  519. (defun pascal-beg-of-defun ()
  520. "Move backward to the beginning of the current function or procedure."
  521. (interactive)
  522. (catch 'found
  523. (if (not (looking-at (concat "\\s \\|\\s)\\|" pascal-defun-re)))
  524. (forward-sexp 1))
  525. (let ((nest 0) (max -1) (func 0)
  526. (reg (concat pascal-beg-block-re "\\|"
  527. pascal-end-block-re "\\|"
  528. pascal-defun-re)))
  529. (while (re-search-backward reg nil 'move)
  530. (cond ((let ((state (save-excursion
  531. (parse-partial-sexp (point-min) (point)))))
  532. (or (nth 3 state) (nth 4 state))) ; Inside string or comment
  533. ())
  534. ((match-end 1) ; begin|case|record|repeat
  535. (if (and (looking-at "\\<record\\>") (>= max 0))
  536. (setq func (1- func)))
  537. (setq nest (1+ nest)
  538. max (max nest max)))
  539. ((match-end 2) ; end|until
  540. (if (and (= nest max) (>= max 0))
  541. (setq func (1+ func)))
  542. (setq nest (1- nest)))
  543. ((match-end 3) ; function|procedure
  544. (if (= 0 func)
  545. (throw 'found t)
  546. (setq func (1- func)))))))
  547. nil))
  548. (defun pascal-end-of-defun ()
  549. "Move forward to the end of the current function or procedure."
  550. (interactive)
  551. (if (looking-at "\\s ")
  552. (forward-sexp 1))
  553. (if (not (looking-at pascal-defun-re))
  554. (pascal-beg-of-defun))
  555. (forward-char 1)
  556. (let ((nest 0) (func 1)
  557. (reg (concat pascal-beg-block-re "\\|"
  558. pascal-end-block-re "\\|"
  559. pascal-defun-re)))
  560. (while (and (/= func 0)
  561. (re-search-forward reg nil 'move))
  562. (cond ((let ((state (save-excursion
  563. (parse-partial-sexp (point-min) (point)))))
  564. (or (nth 3 state) (nth 4 state))) ; Inside string or comment
  565. ())
  566. ((match-end 1)
  567. (setq nest (1+ nest))
  568. (if (save-excursion
  569. (goto-char (match-beginning 0))
  570. (looking-at "\\<record\\>"))
  571. (setq func (1+ func))))
  572. ((match-end 2)
  573. (setq nest (1- nest))
  574. (if (= nest 0)
  575. (setq func (1- func))))
  576. ((match-end 3)
  577. (setq func (1+ func))))))
  578. (forward-line 1))
  579. (defun pascal-end-of-statement ()
  580. "Move forward to end of current statement."
  581. (interactive)
  582. (let ((parse-sexp-ignore-comments t)
  583. (nest 0) pos
  584. (regexp (concat "\\(" pascal-beg-block-re "\\)\\|\\("
  585. pascal-end-block-re "\\)")))
  586. (if (not (looking-at "[ \t\n]")) (forward-sexp -1))
  587. (or (looking-at pascal-beg-block-re)
  588. ;; Skip to end of statement
  589. (setq pos (catch 'found
  590. (while t
  591. (forward-sexp 1)
  592. (cond ((looking-at "[ \t]*;")
  593. (skip-chars-forward "^;")
  594. (forward-char 1)
  595. (throw 'found (point)))
  596. ((save-excursion
  597. (forward-sexp -1)
  598. (looking-at pascal-beg-block-re))
  599. (goto-char (match-beginning 0))
  600. (throw 'found nil))
  601. ((eobp)
  602. (throw 'found (point))))))))
  603. (if (not pos)
  604. ;; Skip a whole block
  605. (catch 'found
  606. (while t
  607. (re-search-forward regexp nil 'move)
  608. (setq nest (if (match-end 1)
  609. (1+ nest)
  610. (1- nest)))
  611. (cond ((eobp)
  612. (throw 'found (point)))
  613. ((= 0 nest)
  614. (throw 'found (pascal-end-of-statement))))))
  615. pos)))
  616. (defun pascal-downcase-keywords ()
  617. "Downcase all Pascal keywords in the buffer."
  618. (interactive)
  619. (pascal-change-keywords 'downcase-word))
  620. (defun pascal-upcase-keywords ()
  621. "Upcase all Pascal keywords in the buffer."
  622. (interactive)
  623. (pascal-change-keywords 'upcase-word))
  624. (defun pascal-capitalize-keywords ()
  625. "Capitalize all Pascal keywords in the buffer."
  626. (interactive)
  627. (pascal-change-keywords 'capitalize-word))
  628. ;; Change the keywords according to argument.
  629. (defun pascal-change-keywords (change-word)
  630. (save-excursion
  631. (let ((keyword-re (concat "\\<\\("
  632. (mapconcat 'identity pascal-keywords "\\|")
  633. "\\)\\>")))
  634. (goto-char (point-min))
  635. (while (re-search-forward keyword-re nil t)
  636. (funcall change-word -1)))))
  637. ;;;
  638. ;;; Other functions
  639. ;;;
  640. (defun pascal-set-auto-comments ()
  641. "Insert `{ case }' or `{ NAME }' on this line if appropriate.
  642. Insert `{ case }' if there is an `end' on the line which
  643. ends a case block. Insert `{ NAME }' if there is an `end'
  644. on the line which ends a function or procedure named NAME."
  645. (save-excursion
  646. (forward-line -1)
  647. (skip-chars-forward " \t")
  648. (if (and (looking-at "\\<end;")
  649. (not (save-excursion
  650. (end-of-line)
  651. (search-backward "{" (point-at-bol) t))))
  652. (let ((type (car (pascal-calculate-indent))))
  653. (if (eq type 'declaration)
  654. ()
  655. (if (eq type 'case)
  656. ;; This is a case block
  657. (progn
  658. (end-of-line)
  659. (delete-horizontal-space)
  660. (insert " { case }"))
  661. (let ((nest 1))
  662. ;; Check if this is the end of a function
  663. (save-excursion
  664. (while (not (or (looking-at pascal-defun-re) (bobp)))
  665. (backward-sexp 1)
  666. (cond ((looking-at pascal-beg-block-re)
  667. (setq nest (1- nest)))
  668. ((looking-at pascal-end-block-re)
  669. (setq nest (1+ nest)))))
  670. (if (bobp)
  671. (setq nest 1)))
  672. (if (zerop nest)
  673. (progn
  674. (end-of-line)
  675. (delete-horizontal-space)
  676. (insert " { ")
  677. (let (b e)
  678. (save-excursion
  679. (setq b (progn (pascal-beg-of-defun)
  680. (skip-chars-forward "^ \t")
  681. (skip-chars-forward " \t")
  682. (point))
  683. e (progn (skip-chars-forward "a-zA-Z0-9_")
  684. (point))))
  685. (insert-buffer-substring (current-buffer) b e))
  686. (insert " }"))))))))))
  687. ;;;
  688. ;;; Indentation
  689. ;;;
  690. (defconst pascal-indent-alist
  691. '((block . (+ pascal--extra-indent pascal-indent-level))
  692. (case . (+ pascal--extra-indent pascal-case-indent))
  693. (caseblock . pascal--extra-indent) (cpp . 0)
  694. (declaration . (+ pascal--extra-indent pascal-indent-level))
  695. (paramlist . (pascal-indent-paramlist t))
  696. (comment . (pascal-indent-comment))
  697. (defun . pascal--extra-indent) (contexp . pascal--extra-indent)
  698. (unknown . pascal--extra-indent) (string . 0) (progbeg . 0)))
  699. (defun pascal-indent-command ()
  700. "Indent for special part of code."
  701. (let* ((indent-str (pascal-calculate-indent))
  702. (type (car indent-str)))
  703. (cond ((and (eq type 'paramlist)
  704. (or (memq 'all pascal-auto-lineup)
  705. (memq 'paramlist pascal-auto-lineup)))
  706. (pascal-indent-paramlist)
  707. (pascal-indent-paramlist))
  708. ((and (eq type 'declaration)
  709. (or (memq 'all pascal-auto-lineup)
  710. (memq 'declaration pascal-auto-lineup)))
  711. (pascal-indent-declaration))
  712. ((and (eq type 'case) (not (looking-at "^[ \t]*$"))
  713. (or (memq 'all pascal-auto-lineup)
  714. (memq 'case pascal-auto-lineup)))
  715. (pascal-indent-case)))
  716. (if (looking-at "[ \t]+$")
  717. (skip-chars-forward " \t"))))
  718. (defun pascal-indent-line ()
  719. "Indent current line as a Pascal statement."
  720. (let* ((indent-str (pascal-calculate-indent))
  721. (type (car indent-str))
  722. (pascal--extra-indent (car (cdr indent-str))))
  723. ;; Labels should not be indented.
  724. (if (and (looking-at "^[0-9a-zA-Z]+[ \t]*:[^=]")
  725. (not (eq type 'declaration)))
  726. (search-forward ":" nil t))
  727. (delete-horizontal-space)
  728. (cond (; Some things should not be indented
  729. (or (and (eq type 'declaration) (looking-at pascal-declaration-re))
  730. (eq type 'cpp))
  731. ())
  732. (; Other things should have no extra indent
  733. (looking-at pascal-noindent-re)
  734. (indent-to pascal--extra-indent))
  735. (; Nested functions should be indented
  736. (looking-at pascal-defun-re)
  737. (if (and pascal-indent-nested-functions
  738. (eq type 'defun))
  739. (indent-to (+ pascal--extra-indent pascal-indent-level))
  740. (indent-to pascal--extra-indent)))
  741. (; But most lines are treated this way
  742. (indent-to (eval (cdr (assoc type pascal-indent-alist))))
  743. ))))
  744. (defun pascal-calculate-indent ()
  745. "Calculate the indent of the current Pascal line.
  746. Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
  747. (save-excursion
  748. (let* ((parse-sexp-ignore-comments t)
  749. (oldpos (point))
  750. (state (save-excursion (parse-partial-sexp (point-min) (point))))
  751. (nest 0) (par 0) (complete (looking-at "[ \t]*end\\>"))
  752. (elsed (looking-at "[ \t]*else\\>")) (funccnt 0)
  753. (did-func (looking-at "[ \t]*\\(procedure\\|function\\)\\>"))
  754. (type (catch 'nesting
  755. ;; Check if inside a string, comment or parenthesis
  756. (cond ((nth 3 state) (throw 'nesting 'string))
  757. ((nth 4 state) (throw 'nesting 'comment))
  758. ((> (car state) 0)
  759. (goto-char (scan-lists (point) -1 (car state)))
  760. (setq par (1+ (current-column))))
  761. ((save-excursion (beginning-of-line)
  762. (eq (following-char) ?#))
  763. (throw 'nesting 'cpp)))
  764. ;; Loop until correct indent is found
  765. (while t
  766. (backward-sexp 1)
  767. (cond (;--Escape from case statements
  768. (and (looking-at "[A-Za-z0-9]+[ \t]*:[^=]")
  769. (not complete)
  770. (save-excursion (skip-chars-backward " \t")
  771. (bolp))
  772. (= (save-excursion
  773. (end-of-line) (backward-sexp) (point))
  774. (point))
  775. (> (save-excursion (goto-char oldpos)
  776. (beginning-of-line)
  777. (point))
  778. (point)))
  779. (throw 'nesting 'caseblock))
  780. (;--Beginning of program
  781. (looking-at pascal-progbeg-re)
  782. (throw 'nesting 'progbeg))
  783. (;--No known statements
  784. (bobp)
  785. (throw 'nesting 'progbeg))
  786. (;--Nest block outwards
  787. (looking-at pascal-beg-block-re)
  788. (if (= nest 0)
  789. (cond ((looking-at "case\\>")
  790. (throw 'nesting 'case))
  791. ((looking-at "record\\>")
  792. (throw 'nesting 'declaration))
  793. (t (throw 'nesting 'block)))
  794. (if (and (looking-at "record\\>") (= nest 1))
  795. (setq funccnt (1- funccnt)))
  796. (setq nest (1- nest))))
  797. (;--Nest block inwards
  798. (looking-at pascal-end-block-re)
  799. (if (and (looking-at "end\\s ")
  800. elsed (not complete))
  801. (throw 'nesting 'block))
  802. (if (= nest 0)
  803. (setq funccnt (1+ funccnt)))
  804. (setq complete t
  805. nest (1+ nest)))
  806. (;--Defun (or parameter list)
  807. (and (looking-at pascal-defun-re)
  808. (progn (setq funccnt (1- funccnt)
  809. did-func t)
  810. (or (bolp) (< funccnt 0))))
  811. ;; Prevent searching whole buffer
  812. (if (and (bolp) (>= funccnt 0))
  813. (throw 'nesting 'progbeg))
  814. (if (= 0 par)
  815. (throw 'nesting 'defun)
  816. (setq par 0)
  817. (let ((n 0))
  818. (while (re-search-forward
  819. "\\(\\<record\\>\\)\\|\\<end\\>"
  820. oldpos t)
  821. (if (match-end 1)
  822. (setq n (1+ n)) (setq n (1- n))))
  823. (if (> n 0)
  824. (throw 'nesting 'declaration)
  825. (throw 'nesting 'paramlist)))))
  826. (;--Declaration part
  827. (and (looking-at pascal-declaration-re)
  828. (not did-func)
  829. (= funccnt 0))
  830. (if (save-excursion
  831. (goto-char oldpos)
  832. (forward-line -1)
  833. (looking-at "^[ \t]*$"))
  834. (throw 'nesting 'unknown)
  835. (throw 'nesting 'declaration)))
  836. (;--If, else or while statement
  837. (and (not complete)
  838. (looking-at pascal-sub-block-re))
  839. (throw 'nesting 'block))
  840. (;--Found complete statement
  841. (save-excursion (forward-sexp 1)
  842. (= (following-char) ?\;))
  843. (setq complete t))
  844. )))))
  845. ;; Return type of block and indent level.
  846. (if (> par 0) ; Unclosed Parenthesis
  847. (list 'contexp par)
  848. (list type (pascal-indent-level))))))
  849. (defun pascal-indent-level ()
  850. "Return the indent-level the current statement has.
  851. Do not count labels, case statements or records."
  852. (save-excursion
  853. (beginning-of-line)
  854. (if (looking-at "[ \t]*[0-9a-zA-Z]+[ \t]*:[^=]")
  855. (search-forward ":" nil t)
  856. (if (looking-at ".*=[ \t]*record\\>")
  857. (search-forward "=" nil t)))
  858. (skip-chars-forward " \t")
  859. (current-column)))
  860. (defun pascal-indent-comment ()
  861. "Return indent for current comment."
  862. (save-excursion
  863. (re-search-backward "\\((\\*\\)\\|{" nil t)
  864. (if (match-beginning 1)
  865. (1+ (current-column))
  866. (current-column))))
  867. (defun pascal-indent-case ()
  868. "Indent within case statements."
  869. (let ((savepos (point-marker))
  870. (end (prog2
  871. (end-of-line)
  872. (point-marker)
  873. (re-search-backward "\\<case\\>" nil t)))
  874. (beg (point))
  875. (pascal--extra-indent 0))
  876. ;; Get right indent
  877. (while (< (point) end)
  878. (if (re-search-forward
  879. "^[ \t]*[^ \t,:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
  880. (marker-position end) 'move)
  881. (forward-char -1))
  882. (if (< (point) end)
  883. (progn
  884. (delete-horizontal-space)
  885. (if (> (current-column) pascal--extra-indent)
  886. (setq pascal--extra-indent (current-column)))
  887. (pascal-end-of-statement))))
  888. (goto-char beg)
  889. ;; Indent all case statements
  890. (while (< (point) end)
  891. (if (re-search-forward
  892. "^[ \t]*[^][ \t,\\.:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
  893. (marker-position end) 'move)
  894. (forward-char -1))
  895. (indent-to (1+ pascal--extra-indent))
  896. (if (/= (following-char) ?:)
  897. ()
  898. (forward-char 1)
  899. (delete-horizontal-space)
  900. (insert " "))
  901. (pascal-end-of-statement))
  902. (goto-char savepos)))
  903. (defun pascal-indent-paramlist (&optional arg)
  904. "Indent current line in parameterlist.
  905. If optional ARG is non-nil, just return the
  906. indent of the current line in parameterlist."
  907. (save-excursion
  908. (let* ((oldpos (point))
  909. (stpos (progn (goto-char (scan-lists (point) -1 1)) (point)))
  910. (stcol (1+ (current-column)))
  911. (edpos (progn (pascal-declaration-end)
  912. (search-backward ")" (point-at-bol) t)
  913. (point)))
  914. (usevar (re-search-backward "\\<var\\>" stpos t)))
  915. (if arg (progn
  916. ;; If arg, just return indent
  917. (goto-char oldpos)
  918. (beginning-of-line)
  919. (if (or (not usevar) (looking-at "[ \t]*var\\>"))
  920. stcol (+ 4 stcol)))
  921. (goto-char stpos)
  922. (forward-char 1)
  923. (delete-horizontal-space)
  924. (if (and usevar (not (looking-at "var\\>")))
  925. (indent-to (+ 4 stcol)))
  926. (pascal-indent-declaration nil stpos edpos)))))
  927. (defun pascal-indent-declaration (&optional arg start end)
  928. "Indent current lines as declaration, lining up the `:'s or `='s."
  929. (let ((pos (point-marker)))
  930. (if (and (not (or arg start)) (not (pascal-declaration-beg)))
  931. ()
  932. (let ((lineup (if (or (looking-at "\\<var\\>\\|\\<record\\>") arg start)
  933. ":" "="))
  934. (stpos (if start start
  935. (forward-word 2) (backward-word 1) (point)))
  936. (edpos (set-marker (make-marker)
  937. (if end end
  938. (max (progn (pascal-declaration-end)
  939. (point))
  940. pos))))
  941. pascal--extra-indent)
  942. (goto-char stpos)
  943. ;; Indent lines in record block
  944. (if arg
  945. (while (<= (point) edpos)
  946. (beginning-of-line)
  947. (delete-horizontal-space)
  948. (if (looking-at "end\\>")
  949. (indent-to arg)
  950. (indent-to (+ arg pascal-indent-level)))
  951. (forward-line 1)))
  952. ;; Do lineup
  953. (setq pascal--extra-indent (pascal-get-lineup-indent stpos edpos lineup))
  954. (goto-char stpos)
  955. (while (and (<= (point) edpos) (not (eobp)))
  956. (if (search-forward lineup (point-at-eol) 'move)
  957. (forward-char -1))
  958. (delete-horizontal-space)
  959. (indent-to pascal--extra-indent)
  960. (if (not (looking-at lineup))
  961. (forward-line 1) ; No more indent if there is no : or =
  962. (forward-char 1)
  963. (delete-horizontal-space)
  964. (insert " ")
  965. ;; Indent record block
  966. (if (looking-at "record\\>")
  967. (pascal-indent-declaration (current-column)))
  968. (forward-line 1)))))
  969. ;; If arg - move point
  970. (if arg (forward-line -1)
  971. (goto-char pos))))
  972. ; "Return the indent level that will line up several lines within the region
  973. ;from b to e nicely. The lineup string is str."
  974. (defun pascal-get-lineup-indent (b e str)
  975. (save-excursion
  976. (let ((pascal--extra-indent 0)
  977. (reg (concat str "\\|\\(\\<record\\>\\)\\|" pascal-defun-re)))
  978. (goto-char b)
  979. ;; Get rightmost position
  980. (while (< (point) e)
  981. (and (re-search-forward reg (min e (point-at-eol 2)) 'move)
  982. (cond ((match-beginning 1)
  983. ;; Skip record blocks
  984. (pascal-declaration-end))
  985. ((match-beginning 2)
  986. ;; We have entered a new procedure. Exit.
  987. (goto-char e))
  988. (t
  989. (goto-char (match-beginning 0))
  990. (skip-chars-backward " \t")
  991. (if (> (current-column) pascal--extra-indent)
  992. (setq pascal--extra-indent (current-column)))
  993. (goto-char (match-end 0))
  994. (end-of-line)
  995. ))))
  996. ;; In case no lineup was found
  997. (if (> pascal--extra-indent 0)
  998. (1+ pascal--extra-indent)
  999. ;; No lineup-string found
  1000. (goto-char b)
  1001. (end-of-line)
  1002. (skip-chars-backward " \t")
  1003. (1+ (current-column))))))
  1004. ;;;
  1005. ;;; Completion
  1006. (defun pascal-string-diff (str1 str2)
  1007. "Return index of first letter where STR1 and STR2 differs."
  1008. (catch 'done
  1009. (let ((diff 0))
  1010. (while t
  1011. (if (or (> (1+ diff) (length str1))
  1012. (> (1+ diff) (length str2)))
  1013. (throw 'done diff))
  1014. (or (equal (aref str1 diff) (aref str2 diff))
  1015. (throw 'done diff))
  1016. (setq diff (1+ diff))))))
  1017. ;; Calculate all possible completions for functions if argument is `function',
  1018. ;; completions for procedures if argument is `procedure' or both functions and
  1019. ;; procedures otherwise.
  1020. (defun pascal-func-completion (type pascal-str)
  1021. ;; Build regular expression for function/procedure names
  1022. (save-excursion
  1023. (if (string= pascal-str "")
  1024. (setq pascal-str "[a-zA-Z_]"))
  1025. (let ((pascal-str (concat (cond
  1026. ((eq type 'procedure) "\\<\\(procedure\\)\\s +")
  1027. ((eq type 'function) "\\<\\(function\\)\\s +")
  1028. (t "\\<\\(function\\|procedure\\)\\s +"))
  1029. "\\<\\(" pascal-str "[a-zA-Z0-9_.]*\\)\\>"))
  1030. (pascal-all ())
  1031. match)
  1032. (if (not (looking-at "\\<\\(function\\|procedure\\)\\>"))
  1033. (re-search-backward "\\<\\(function\\|procedure\\)\\>" nil t))
  1034. (forward-char 1)
  1035. ;; Search through all reachable functions
  1036. (while (pascal-beg-of-defun)
  1037. (if (re-search-forward pascal-str (point-at-eol) t)
  1038. (progn (setq match (buffer-substring (match-beginning 2)
  1039. (match-end 2)))
  1040. (push match pascal-all)))
  1041. (goto-char (match-beginning 0)))
  1042. pascal-all)))
  1043. (defun pascal-get-completion-decl (pascal-str)
  1044. ;; Macro for searching through current declaration (var, type or const)
  1045. ;; for matches of `str' and adding the occurrence to `all'
  1046. (let ((end (save-excursion (pascal-declaration-end)
  1047. (point)))
  1048. (pascal-all ())
  1049. match)
  1050. ;; Traverse lines
  1051. (while (< (point) end)
  1052. (if (re-search-forward "[:=]" (point-at-eol) t)
  1053. ;; Traverse current line
  1054. (while (and (re-search-backward
  1055. (concat "\\((\\|\\<\\(var\\|type\\|const\\)\\>\\)\\|"
  1056. pascal-symbol-re)
  1057. (point-at-bol) t)
  1058. (not (match-end 1)))
  1059. (setq match (buffer-substring (match-beginning 0) (match-end 0)))
  1060. (if (string-match (concat "\\<" pascal-str) match)
  1061. (push match pascal-all))))
  1062. (if (re-search-forward "\\<record\\>" (point-at-eol) t)
  1063. (pascal-declaration-end)
  1064. (forward-line 1)))
  1065. pascal-all))
  1066. (defun pascal-type-completion (pascal-str)
  1067. "Calculate all possible completions for types."
  1068. (let ((start (point))
  1069. (pascal-all ())
  1070. goon)
  1071. ;; Search for all reachable type declarations
  1072. (while (or (pascal-beg-of-defun)
  1073. (setq goon (not goon)))
  1074. (save-excursion
  1075. (if (and (< start (prog1 (save-excursion (pascal-end-of-defun)
  1076. (point))
  1077. (forward-char 1)))
  1078. (re-search-forward
  1079. "\\<type\\>\\|\\<\\(begin\\|function\\|procedure\\)\\>"
  1080. start t)
  1081. (not (match-end 1)))
  1082. ;; Check current type declaration
  1083. (setq pascal-all
  1084. (nconc (pascal-get-completion-decl pascal-str)
  1085. pascal-all)))))
  1086. pascal-all))
  1087. (defun pascal-var-completion (prefix)
  1088. "Calculate all possible completions for variables (or constants)."
  1089. (save-excursion
  1090. (let ((start (point))
  1091. (pascal-all ())
  1092. goon twice)
  1093. ;; Search for all reachable var declarations
  1094. (while (or (pascal-beg-of-defun)
  1095. (setq goon (not goon)))
  1096. (save-excursion
  1097. (if (> start (prog1 (save-excursion (pascal-end-of-defun)
  1098. (point))))
  1099. () ; Declarations not reachable
  1100. (if (search-forward "(" (point-at-eol) t)
  1101. ;; Check parameterlist
  1102. ;; FIXME: pascal-get-completion-decl doesn't understand
  1103. ;; the var declarations in parameter lists :-(
  1104. (setq pascal-all
  1105. (nconc (pascal-get-completion-decl prefix)
  1106. pascal-all)))
  1107. (setq twice 2)
  1108. (while (>= (setq twice (1- twice)) 0)
  1109. (cond
  1110. ((and (re-search-forward
  1111. (concat "\\<\\(var\\|const\\)\\>\\|"
  1112. "\\<\\(begin\\|function\\|procedure\\)\\>")
  1113. start t)
  1114. (not (match-end 2)))
  1115. ;; Check var/const declarations
  1116. (setq pascal-all
  1117. (nconc (pascal-get-completion-decl prefix)
  1118. pascal-all)))
  1119. ((match-end 2)
  1120. (setq twice 0)))))))
  1121. pascal-all)))
  1122. (defun pascal-keyword-completion (keyword-list pascal-str)
  1123. "Give list of all possible completions of keywords in KEYWORD-LIST."
  1124. (let ((pascal-all ()))
  1125. (dolist (s keyword-list)
  1126. (if (string-match (concat "\\<" pascal-str) s)
  1127. (push s pascal-all)))
  1128. pascal-all))
  1129. ;; Function passed to completing-read, try-completion or
  1130. ;; all-completions to get completion on STR. If predicate is non-nil,
  1131. ;; it must be a function to be called for every match to check if this
  1132. ;; should really be a match. If flag is t, the function returns a list
  1133. ;; of all possible completions. If it is nil it returns a string, the
  1134. ;; longest possible completion, or t if STR is an exact match. If flag
  1135. ;; is 'lambda, the function returns t if STR is an exact match, nil
  1136. ;; otherwise.
  1137. (defvar pascal-completion-cache nil)
  1138. (defun pascal-completion (pascal-str pascal-pred pascal-flag)
  1139. (let ((all (car pascal-completion-cache)))
  1140. ;; Check the cache's freshness.
  1141. (unless (and pascal-completion-cache
  1142. (string-prefix-p (nth 1 pascal-completion-cache) pascal-str)
  1143. (eq (current-buffer) (nth 2 pascal-completion-cache))
  1144. (eq (field-beginning) (nth 3 pascal-completion-cache)))
  1145. (let ((state (car (pascal-calculate-indent))))
  1146. (setq all
  1147. ;; Determine what should be completed
  1148. (cond
  1149. ( ;--Within a declaration or parameterlist
  1150. (or (eq state 'declaration) (eq state 'paramlist)
  1151. (and (eq state 'defun)
  1152. (save-excursion
  1153. (re-search-backward ")[ \t]*:" (point-at-bol) t))))
  1154. (if (or (eq state 'paramlist) (eq state 'defun))
  1155. (pascal-beg-of-defun))
  1156. (nconc
  1157. (pascal-type-completion pascal-str)
  1158. (pascal-keyword-completion pascal-type-keywords pascal-str)))
  1159. ( ;--Starting a new statement
  1160. (and (not (eq state 'contexp))
  1161. (save-excursion
  1162. (skip-chars-backward "a-zA-Z0-9_.")
  1163. (backward-sexp 1)
  1164. (or (looking-at pascal-nosemi-re)
  1165. (progn
  1166. (forward-sexp 1)
  1167. (looking-at "\\s *\\(;\\|:[^=]\\)")))))
  1168. (nconc
  1169. (pascal-var-completion pascal-str)
  1170. (pascal-func-completion 'procedure pascal-str)
  1171. (pascal-keyword-completion pascal-start-keywords pascal-str)))
  1172. (t ;--Anywhere else
  1173. (nconc
  1174. (pascal-var-completion pascal-str)
  1175. (pascal-func-completion 'function pascal-str)
  1176. (pascal-keyword-completion pascal-separator-keywords
  1177. pascal-str)))))
  1178. (setq pascal-completion-cache
  1179. (list all pascal-str (current-buffer) (field-beginning)))))
  1180. ;; Now we have built a list of all matches. Give response to caller
  1181. (complete-with-action pascal-flag all pascal-str pascal-pred)))
  1182. (defvar pascal-last-word-numb 0)
  1183. (defvar pascal-last-word-shown nil)
  1184. (defvar pascal-last-completions nil)
  1185. (defun pascal-completions-at-point ()
  1186. (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
  1187. (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point))))
  1188. (when (> e b)
  1189. (list b e #'pascal-completion))))
  1190. (define-obsolete-function-alias 'pascal-complete-word
  1191. 'completion-at-point "24.1")
  1192. (define-obsolete-function-alias 'pascal-show-completions
  1193. 'completion-help-at-point "24.1")
  1194. (defun pascal-get-default-symbol ()
  1195. "Return symbol around current point as a string."
  1196. (save-excursion
  1197. (buffer-substring (progn
  1198. (skip-chars-backward " \t")
  1199. (skip-chars-backward "a-zA-Z0-9_")
  1200. (point))
  1201. (progn
  1202. (skip-chars-forward "a-zA-Z0-9_")
  1203. (point)))))
  1204. (defun pascal-build-defun-re (str &optional arg)
  1205. "Return function/procedure starting with STR as regular expression.
  1206. With optional second arg non-nil, STR is the complete name of the instruction."
  1207. (if arg
  1208. (concat "^\\(function\\|procedure\\)[ \t]+\\(" str "\\)\\>")
  1209. (concat "^\\(function\\|procedure\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))
  1210. ;; Function passed to completing-read, try-completion or
  1211. ;; all-completions to get completion on any function name. If
  1212. ;; predicate is non-nil, it must be a function to be called for every
  1213. ;; match to check if this should really be a match. If flag is t, the
  1214. ;; function returns a list of all possible completions. If it is nil
  1215. ;; it returns a string, the longest possible completion, or t if STR
  1216. ;; is an exact match. If flag is 'lambda, the function returns t if
  1217. ;; STR is an exact match, nil otherwise.
  1218. (defun pascal-comp-defun (pascal-str pascal-pred pascal-flag)
  1219. (save-excursion
  1220. (let ((pascal-all nil))
  1221. ;; Build regular expression for functions
  1222. (let ((pascal-str (pascal-build-defun-re (if (string= pascal-str "")
  1223. "[a-zA-Z_]"
  1224. pascal-str))))
  1225. (goto-char (point-min))
  1226. ;; Build a list of all possible completions
  1227. (while (re-search-forward pascal-str nil t)
  1228. (push (match-string 2) pascal-all)))
  1229. ;; Now we have built a list of all matches. Give response to caller
  1230. (complete-with-action pascal-flag pascal-all pascal-str pascal-pred))))
  1231. (defun pascal-goto-defun ()
  1232. "Move to specified Pascal function/procedure.
  1233. The default is a name found in the buffer around point."
  1234. (interactive)
  1235. (let* ((default (pascal-get-default-symbol))
  1236. (default (if (pascal-comp-defun default nil 'lambda)
  1237. default ""))
  1238. (label
  1239. ;; Do completion with default.
  1240. (completing-read (if (not (string= default ""))
  1241. (concat "Label (default " default "): ")
  1242. "Label: ")
  1243. ;; Complete with the defuns found in the
  1244. ;; current-buffer.
  1245. (let ((buf (current-buffer)))
  1246. (lambda (s p a)
  1247. (with-current-buffer buf
  1248. (pascal-comp-defun s p a))))
  1249. nil t "")))
  1250. ;; If there was no response on prompt, use default value.
  1251. (if (string= label "")
  1252. (setq label default))
  1253. ;; Goto right place in buffer if label is not an empty string.
  1254. (or (string= label "")
  1255. (progn
  1256. (goto-char (point-min))
  1257. (re-search-forward (pascal-build-defun-re label t))
  1258. (beginning-of-line)))))
  1259. ;;;
  1260. ;;; Pascal-outline-mode
  1261. ;;;
  1262. (defvar pascal-outline-map
  1263. (let ((map (make-sparse-keymap)))
  1264. (if (fboundp 'set-keymap-name)
  1265. (set-keymap-name pascal-outline-map 'pascal-outline-map))
  1266. (define-key map "\M-\C-a" 'pascal-outline-prev-defun)
  1267. (define-key map "\M-\C-e" 'pascal-outline-next-defun)
  1268. (define-key map "\C-c\C-d" 'pascal-outline-goto-defun)
  1269. (define-key map "\C-c\C-s" 'pascal-show-all)
  1270. (define-key map "\C-c\C-h" 'pascal-hide-other-defuns)
  1271. map)
  1272. "Keymap used in Pascal Outline mode.")
  1273. (define-obsolete-function-alias 'pascal-outline 'pascal-outline-mode "22.1")
  1274. (define-minor-mode pascal-outline-mode
  1275. "Outline-line minor mode for Pascal mode.
  1276. With a prefix argument ARG, enable the mode if ARG is positive,
  1277. and disable it otherwise. If called from Lisp, enable the mode
  1278. if ARG is omitted or nil.
  1279. When enabled, portions of the text being edited may be made
  1280. invisible.\\<pascal-outline-map>
  1281. Pascal Outline mode provides some additional commands.
  1282. \\[pascal-outline-prev-defun]\
  1283. \t- Move to previous function/procedure, hiding everything else.
  1284. \\[pascal-outline-next-defun]\
  1285. \t- Move to next function/procedure, hiding everything else.
  1286. \\[pascal-outline-goto-defun]\
  1287. \t- Goto function/procedure prompted for in minibuffer,
  1288. \t hide all other functions.
  1289. \\[pascal-show-all]\t- Show the whole buffer.
  1290. \\[pascal-hide-other-defuns]\
  1291. \t- Hide everything but the current function (function under the cursor).
  1292. \\[pascal-outline]\t- Leave Pascal Outline mode."
  1293. :init-value nil :lighter " Outl" :keymap pascal-outline-map
  1294. (add-to-invisibility-spec '(pascal . t))
  1295. (unless pascal-outline-mode
  1296. (pascal-show-all)))
  1297. (defun pascal-outline-change (b e hide)
  1298. (when (> e b)
  1299. ;; We could try and optimize this in the case where the region is
  1300. ;; already hidden. But I'm not sure it's worth the trouble.
  1301. (remove-overlays b e 'invisible 'pascal)
  1302. (when hide
  1303. (let ((ol (make-overlay b e nil t nil)))
  1304. (overlay-put ol 'invisible 'pascal)
  1305. (overlay-put ol 'evaporate t)))))
  1306. (defun pascal-show-all ()
  1307. "Show all of the text in the buffer."
  1308. (interactive)
  1309. (pascal-outline-change (point-min) (point-max) nil))
  1310. (defun pascal-hide-other-defuns ()
  1311. "Show only the current defun."
  1312. (interactive)
  1313. (save-excursion
  1314. (let ((beg (progn (if (not (looking-at "\\(function\\|procedure\\)\\>"))
  1315. (pascal-beg-of-defun))
  1316. (line-beginning-position)))
  1317. (end (progn (pascal-end-of-defun)
  1318. (backward-sexp 1)
  1319. (line-beginning-position 2)))
  1320. (opoint (point-min)))
  1321. ;; BEG at BOL.
  1322. ;; OPOINT at EOL.
  1323. ;; END at BOL.
  1324. (goto-char (point-min))
  1325. ;; Hide all functions before current function
  1326. (while (re-search-forward "^[ \t]*\\(function\\|procedure\\)\\>"
  1327. beg 'move)
  1328. (pascal-outline-change opoint (line-end-position 0) t)
  1329. (setq opoint (line-end-position))
  1330. ;; Functions may be nested
  1331. (if (> (progn (pascal-end-of-defun) (point)) beg)
  1332. (goto-char opoint)))
  1333. (if (> beg opoint)
  1334. (pascal-outline-change opoint (1- beg) t))
  1335. ;; Show current function
  1336. (pascal-outline-change (1- beg) end nil)
  1337. ;; Hide nested functions
  1338. (forward-char 1)
  1339. (while (re-search-forward "^\\(function\\|procedure\\)\\>" end 'move)
  1340. (setq opoint (line-end-position))
  1341. (pascal-end-of-defun)
  1342. (pascal-outline-change opoint (line-end-position) t))
  1343. (goto-char end)
  1344. (setq opoint end)
  1345. ;; Hide all function after current function
  1346. (while (re-search-forward "^\\(function\\|procedure\\)\\>" nil 'move)
  1347. (pascal-outline-change opoint (line-end-position 0) t)
  1348. (setq opoint (line-end-position))
  1349. (pascal-end-of-defun))
  1350. (pascal-outline-change opoint (point-max) t)
  1351. ;; Hide main program
  1352. (if (< (progn (forward-line -1) (point)) end)
  1353. (progn
  1354. (goto-char beg)
  1355. (pascal-end-of-defun)
  1356. (backward-sexp 1)
  1357. (pascal-outline-change (line-end-position) (point-max) t))))))
  1358. (defun pascal-outline-next-defun ()
  1359. "Move to next function/procedure, hiding all others."
  1360. (interactive)
  1361. (pascal-end-of-defun)
  1362. (pascal-hide-other-defuns))
  1363. (defun pascal-outline-prev-defun ()
  1364. "Move to previous function/procedure, hiding all others."
  1365. (interactive)
  1366. (pascal-beg-of-defun)
  1367. (pascal-hide-other-defuns))
  1368. (defun pascal-outline-goto-defun ()
  1369. "Move to specified function/procedure, hiding all others."
  1370. (interactive)
  1371. (pascal-goto-defun)
  1372. (pascal-hide-other-defuns))
  1373. (provide 'pascal)
  1374. ;;; pascal.el ends here