pascal.el 53 KB

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