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