Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / progmodes / pascal.el
blobed79eacecc488503b78582d4d892cd5ba158b873
1 ;;; pascal.el --- major mode for editing pascal source in Emacs -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993-2014 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
162 `(("\\_<\\(function\\|pro\\(cedure\\|gram\\)\\)[ \t]+\\([[:alpha:]][[:alnum:]_]*\\)"
163 (1 font-lock-keyword-face)
164 (3 font-lock-function-name-face))
165 ;; ("type" "const" "real" "integer" "char" "boolean" "var"
166 ;; "record" "array" "file")
167 (,(concat "\\<\\(array\\|boolean\\|c\\(har\\|onst\\)\\|file\\|"
168 "integer\\|re\\(al\\|cord\\)\\|type\\|var\\)\\>")
169 font-lock-type-face)
170 ("\\<\\(label\\|external\\|forward\\)\\>" . font-lock-constant-face)
171 ("\\<\\([0-9]+\\)[ \t]*:" 1 font-lock-function-name-face)
172 ;; ("of" "to" "for" "if" "then" "else" "case" "while"
173 ;; "do" "until" "and" "or" "not" "in" "with" "repeat" "begin" "end")
174 ,(concat "\\<\\("
175 "and\\|begin\\|case\\|do\\|e\\(lse\\|nd\\)\\|for\\|i[fn]\\|"
176 "not\\|o[fr]\\|repeat\\|t\\(hen\\|o\\)\\|until\\|w\\(hile\\|ith\\)"
177 "\\)\\>")
178 ("\\<\\(goto\\)\\>[ \t]*\\([0-9]+\\)?"
179 1 font-lock-keyword-face)
180 ("\\<\\(goto\\)\\>[ \t]*\\([0-9]+\\)?"
181 2 font-lock-keyword-face t))
182 "Additional expressions to highlight in Pascal mode.")
184 (defconst pascal--syntax-propertize
185 (syntax-propertize-rules
186 ;; The syntax-table settings are too coarse and end up treating /* and (/
187 ;; as comment starters. Fix it here by removing the "2" from the syntax
188 ;; of the second char of such sequences.
189 ("/\\(\\*\\)" (1 ". 3b"))
190 ("(\\(\\/\\)" (1 (prog1 ". 1c" (forward-char -1) nil)))
191 ;; Pascal uses '' and "" rather than \' and \" to escape quotes.
192 ("''\\|\"\"" (0 (if (save-excursion
193 (nth 3 (syntax-ppss (match-beginning 0))))
194 (string-to-syntax ".")
195 ;; In case of 3 or more quotes in a row, only advance
196 ;; one quote at a time.
197 (forward-char -1)
198 nil)))))
200 (defcustom pascal-indent-level 3
201 "Indentation of Pascal statements with respect to containing block."
202 :type 'integer
203 :group 'pascal)
205 (defcustom pascal-case-indent 2
206 "Indentation for case statements."
207 :type 'integer
208 :group 'pascal)
210 (defcustom pascal-auto-newline nil
211 "Non-nil means automatically insert newlines in certain cases.
212 These include after semicolons and after the punctuation mark after an `end'."
213 :type 'boolean
214 :group 'pascal)
216 (defcustom pascal-indent-nested-functions t
217 "Non-nil means nested functions are indented."
218 :type 'boolean
219 :group 'pascal)
221 (defcustom pascal-tab-always-indent t
222 "Non-nil means TAB in Pascal mode should always reindent the current line.
223 If this is nil, TAB inserts a tab if it is at the end of the line
224 and follows non-whitespace text."
225 :type 'boolean
226 :group 'pascal)
228 (defcustom pascal-auto-endcomments t
229 "Non-nil means automatically insert comments after certain `end's.
230 Specifically, this is done after the ends of cases statements and functions.
231 The name of the function or case is included between the braces."
232 :type 'boolean
233 :group 'pascal)
235 (defcustom pascal-auto-lineup '(all)
236 "List of contexts where auto lineup of :'s or ='s should be done.
237 Elements can be of type: 'paramlist', 'declaration' or 'case', which will
238 do auto lineup in parameterlist, declarations or case-statements
239 respectively. The word 'all' will do all lineups. '(case paramlist) for
240 instance will do lineup in case-statements and parameterlist, while '(all)
241 will do all lineups."
242 :type '(set :extra-offset 8
243 (const :tag "Everything" all)
244 (const :tag "Parameter lists" paramlist)
245 (const :tag "Declarations" declaration)
246 (const :tag "Case statements" case))
247 :group 'pascal)
249 (defvar pascal-toggle-completions nil
250 "If non-nil, `pascal-complete-word' tries all possible completions.
251 Repeated use of \\[pascal-complete-word] then shows all
252 completions in turn, instead of displaying a list of all possible
253 completions.")
254 (make-obsolete-variable 'pascal-toggle-completions
255 'completion-cycle-threshold "24.1")
257 (defcustom pascal-type-keywords
258 '("array" "file" "packed" "char" "integer" "real" "string" "record")
259 "Keywords for types used when completing a word in a declaration or parmlist.
260 These include integer, real, char, etc.
261 The types defined within the Pascal program
262 are handled in another way, and should not be added to this list."
263 :type '(repeat (string :tag "Keyword"))
264 :group 'pascal)
266 (defcustom pascal-start-keywords
267 '("begin" "end" "function" "procedure" "repeat" "until" "while"
268 "read" "readln" "reset" "rewrite" "write" "writeln")
269 "Keywords to complete when standing at the first word of a statement.
270 These are keywords such as begin, repeat, until, readln.
271 The procedures and variables defined within the Pascal program
272 are handled in another way, and should not be added to this list."
273 :type '(repeat (string :tag "Keyword"))
274 :group 'pascal)
276 (defcustom pascal-separator-keywords
277 '("downto" "else" "mod" "div" "then")
278 "Keywords to complete when NOT standing at the first word of a statement.
279 These are keywords such as downto, else, mod, then.
280 Variables and function names defined within the Pascal program
281 are handled in another way, and should not be added to this list."
282 :type '(repeat (string :tag "Keyword"))
283 :group 'pascal)
287 ;;; Macros
290 (defun pascal-declaration-end ()
291 (let ((nest 1))
292 (while (and (> nest 0)
293 (re-search-forward
294 "[:=]\\|\\(\\<record\\>\\)\\|\\(\\<end\\>\\)"
295 (point-at-eol 2) t))
296 (cond ((match-beginning 1) (setq nest (1+ nest)))
297 ((match-beginning 2) (setq nest (1- nest)))
298 ((looking-at "[^(\n]+)") (setq nest 0))))))
301 (defun pascal-declaration-beg ()
302 (let ((nest 1))
303 (while (and (> nest 0)
304 (re-search-backward "[:=]\\|\\<\\(type\\|var\\|label\\|const\\)\\>\\|\\(\\<record\\>\\)\\|\\(\\<end\\>\\)" (point-at-bol 0) t))
305 (cond ((match-beginning 1) (setq nest 0))
306 ((match-beginning 2) (setq nest (1- nest)))
307 ((match-beginning 3) (setq nest (1+ nest)))))
308 (= nest 0)))
311 (defsubst pascal-within-string ()
312 (nth 3 (parse-partial-sexp (point-at-bol) (point))))
315 ;;;###autoload
316 (define-derived-mode pascal-mode prog-mode "Pascal"
317 "Major mode for editing Pascal code. \\<pascal-mode-map>
318 TAB indents for Pascal code. Delete converts tabs to spaces as it moves back.
320 \\[completion-at-point] completes the word around current point with respect \
321 to position in code
322 \\[completion-help-at-point] shows all possible completions at this point.
324 Other useful functions are:
326 \\[pascal-mark-defun]\t- Mark function.
327 \\[pascal-insert-block]\t- insert begin ... end;
328 \\[pascal-star-comment]\t- insert (* ... *)
329 \\[pascal-comment-area]\t- Put marked area in a comment, fixing nested comments.
330 \\[pascal-uncomment-area]\t- Uncomment an area commented with \
331 \\[pascal-comment-area].
332 \\[pascal-beg-of-defun]\t- Move to beginning of current function.
333 \\[pascal-end-of-defun]\t- Move to end of current function.
334 \\[pascal-goto-defun]\t- Goto function prompted for in the minibuffer.
335 \\[pascal-outline-mode]\t- Enter `pascal-outline-mode'.
337 Variables controlling indentation/edit style:
339 `pascal-indent-level' (default 3)
340 Indentation of Pascal statements with respect to containing block.
341 `pascal-case-indent' (default 2)
342 Indentation for case statements.
343 `pascal-auto-newline' (default nil)
344 Non-nil means automatically newline after semicolons and the punctuation
345 mark after an end.
346 `pascal-indent-nested-functions' (default t)
347 Non-nil means nested functions are indented.
348 `pascal-tab-always-indent' (default t)
349 Non-nil means TAB in Pascal mode should always reindent the current line,
350 regardless of where in the line point is when the TAB command is used.
351 `pascal-auto-endcomments' (default t)
352 Non-nil means a comment { ... } is set after the ends which ends cases and
353 functions. The name of the function or case will be set between the braces.
354 `pascal-auto-lineup' (default t)
355 List of contexts where auto lineup of :'s or ='s should be done.
357 See also the user variables `pascal-type-keywords', `pascal-start-keywords' and
358 `pascal-separator-keywords'.
360 Turning on Pascal mode calls the value of the variable pascal-mode-hook with
361 no args, if that value is non-nil."
362 (setq-local local-abbrev-table pascal-mode-abbrev-table)
363 (setq-local indent-line-function 'pascal-indent-line)
364 (setq-local comment-indent-function 'pascal-indent-comment)
365 (setq-local parse-sexp-ignore-comments nil)
366 (setq-local blink-matching-paren-dont-ignore-comments t)
367 (setq-local case-fold-search t)
368 (setq-local comment-start "{")
369 (setq-local comment-start-skip "(\\*+ *\\|{ *")
370 (setq-local comment-end "}")
371 (add-hook 'completion-at-point-functions 'pascal-completions-at-point nil t)
372 ;; Font lock support
373 (setq-local font-lock-defaults '(pascal-font-lock-keywords nil t))
374 (setq-local syntax-propertize-function pascal--syntax-propertize)
375 ;; Imenu support
376 (setq-local imenu-generic-expression pascal-imenu-generic-expression)
377 (setq-local imenu-case-fold-search t)
378 ;; Pascal-mode's own hide/show support.
379 (add-to-invisibility-spec '(pascal . t)))
384 ;;; Electric functions
386 (defun electric-pascal-terminate-line ()
387 "Terminate line and indent next line."
388 (interactive)
389 ;; First, check if current line should be indented
390 (save-excursion
391 (beginning-of-line)
392 (skip-chars-forward " \t")
393 (if (looking-at pascal-autoindent-lines-re)
394 (pascal-indent-line)))
395 (delete-horizontal-space) ; Removes trailing whitespaces
396 (newline)
397 ;; Indent next line
398 (pascal-indent-line)
399 ;; Maybe we should set some endcomments
400 (if pascal-auto-endcomments
401 (pascal-set-auto-comments))
402 ;; Check if we shall indent inside comment
403 (let ((setstar nil))
404 (save-excursion
405 (forward-line -1)
406 (skip-chars-forward " \t")
407 (cond ((looking-at "\\*[ \t]+)")
408 ;; Delete region between `*' and `)' if there is only whitespaces.
409 (forward-char 1)
410 (delete-horizontal-space))
411 ((and (looking-at "(\\*\\|\\*[^)]")
412 (not (save-excursion (search-forward "*)" (point-at-eol) t))))
413 (setq setstar t))))
414 ;; If last line was a star comment line then this one shall be too.
415 (if (null setstar)
416 (pascal-indent-line)
417 (insert "* "))))
420 (defun electric-pascal-semi-or-dot ()
421 "Insert `;' or `.' character and reindent the line."
422 (interactive)
423 (insert last-command-event)
424 (save-excursion
425 (beginning-of-line)
426 (pascal-indent-line))
427 (if pascal-auto-newline
428 (electric-pascal-terminate-line)))
430 (defun electric-pascal-colon ()
431 "Insert `:' and do all indentations except line indent on this line."
432 (interactive)
433 (insert last-command-event)
434 ;; Do nothing if within string.
435 (if (pascal-within-string)
437 (save-excursion
438 (beginning-of-line)
439 (pascal-indent-line))
440 (let ((pascal-tab-always-indent nil))
441 (pascal-indent-command))))
443 (defun electric-pascal-equal ()
444 "Insert `=', and do indentation if within type declaration."
445 (interactive)
446 (insert last-command-event)
447 (if (eq (car (pascal-calculate-indent)) 'declaration)
448 (let ((pascal-tab-always-indent nil))
449 (pascal-indent-command))))
451 (defun electric-pascal-hash ()
452 "Insert `#', and indent to column 0 if this is a CPP directive."
453 (interactive)
454 (insert last-command-event)
455 (if (save-excursion (beginning-of-line) (looking-at "^[ \t]*#"))
456 (save-excursion (beginning-of-line)
457 (delete-horizontal-space))))
459 (defun electric-pascal-tab ()
460 "Function called when TAB is pressed in Pascal mode."
461 (interactive)
462 ;; Do nothing if within a string or in a CPP directive.
463 (if (or (pascal-within-string)
464 (and (not (bolp))
465 (save-excursion (beginning-of-line) (eq (following-char) ?#))))
466 (insert "\t")
467 ;; If pascal-tab-always-indent, indent the beginning of the line.
468 (if pascal-tab-always-indent
469 (save-excursion
470 (beginning-of-line)
471 (pascal-indent-line))
472 (if (save-excursion
473 (skip-chars-backward " \t")
474 (bolp))
475 (pascal-indent-line)
476 (insert "\t")))
477 (pascal-indent-command)))
482 ;;; Interactive functions
484 (defvar pascal--extra-indent 0)
486 (defun pascal-insert-block ()
487 "Insert Pascal begin ... end; block in the code with right indentation."
488 (interactive)
489 (insert "begin")
490 (electric-pascal-terminate-line)
491 (save-excursion
492 (newline)
493 (insert "end;")
494 (beginning-of-line)
495 (pascal-indent-line)))
497 (defun pascal-star-comment ()
498 "Insert Pascal star comment at point."
499 (interactive)
500 (pascal-indent-line)
501 (insert "(*")
502 (electric-pascal-terminate-line)
503 (save-excursion
504 (electric-pascal-terminate-line)
505 (delete-horizontal-space)
506 (insert ")"))
507 (insert " "))
509 (defun pascal-mark-defun ()
510 "Mark the current pascal function (or procedure).
511 This puts the mark at the end, and point at the beginning."
512 (interactive)
513 (push-mark (point))
514 (pascal-end-of-defun)
515 (push-mark (point))
516 (pascal-beg-of-defun)
517 (when (featurep 'xemacs)
518 (zmacs-activate-region)))
520 (defun pascal-comment-area (start end)
521 "Put the region into a Pascal comment.
522 The comments that are in this area are \"deformed\":
523 `*)' becomes `!(*' and `}' becomes `!{'.
524 These deformed comments are returned to normal if you use
525 \\[pascal-uncomment-area] to undo the commenting.
527 The commented area starts with `pascal-exclude-str-start', and ends with
528 `pascal-include-str-end'. But if you change these variables,
529 \\[pascal-uncomment-area] won't recognize the comments."
530 (interactive "r")
531 (save-excursion
532 ;; Insert start and endcomments
533 (goto-char end)
534 (if (and (save-excursion (skip-chars-forward " \t") (eolp))
535 (not (save-excursion (skip-chars-backward " \t") (bolp))))
536 (forward-line 1)
537 (beginning-of-line))
538 (insert pascal-exclude-str-end)
539 (setq end (point))
540 (newline)
541 (goto-char start)
542 (beginning-of-line)
543 (insert pascal-exclude-str-start)
544 (newline)
545 ;; Replace end-comments within commented area
546 (goto-char end)
547 (save-excursion
548 (while (re-search-backward "\\*)" start t)
549 (replace-match "!(*" t t)))
550 (save-excursion
551 (while (re-search-backward "}" start t)
552 (replace-match "!{" t t)))))
554 (defun pascal-uncomment-area ()
555 "Uncomment a commented area; change deformed comments back to normal.
556 This command does nothing if the pointer is not in a commented
557 area. See also `pascal-comment-area'."
558 (interactive)
559 (save-excursion
560 (let ((start (point))
561 (end (point)))
562 ;; Find the boundaries of the comment
563 (save-excursion
564 (setq start (progn (search-backward pascal-exclude-str-start nil t)
565 (point)))
566 (setq end (progn (search-forward pascal-exclude-str-end nil t)
567 (point))))
568 ;; Check if we're really inside a comment
569 (if (or (equal start (point)) (<= end (point)))
570 (message "Not standing within commented area.")
571 (progn
572 ;; Remove endcomment
573 (goto-char end)
574 (beginning-of-line)
575 (let ((pos (point)))
576 (end-of-line)
577 (delete-region pos (1+ (point))))
578 ;; Change comments back to normal
579 (save-excursion
580 (while (re-search-backward "!{" start t)
581 (replace-match "}" t t)))
582 (save-excursion
583 (while (re-search-backward "!(\\*" start t)
584 (replace-match "*)" t t)))
585 ;; Remove startcomment
586 (goto-char start)
587 (beginning-of-line)
588 (let ((pos (point)))
589 (end-of-line)
590 (delete-region pos (1+ (point)))))))))
592 (defun pascal-beg-of-defun ()
593 "Move backward to the beginning of the current function or procedure."
594 (interactive)
595 (catch 'found
596 (if (not (looking-at (concat "\\s \\|\\s)\\|" pascal-defun-re)))
597 (forward-sexp 1))
598 (let ((nest 0) (max -1) (func 0)
599 (reg (concat pascal-beg-block-re "\\|"
600 pascal-end-block-re "\\|"
601 pascal-defun-re)))
602 (while (re-search-backward reg nil 'move)
603 (cond ((let ((state (save-excursion
604 (parse-partial-sexp (point-min) (point)))))
605 (or (nth 3 state) (nth 4 state))) ; Inside string or comment
607 ((match-end 1) ; begin|case|record|repeat
608 (if (and (looking-at "\\<record\\>") (>= max 0))
609 (setq func (1- func)))
610 (setq nest (1+ nest)
611 max (max nest max)))
612 ((match-end 2) ; end|until
613 (if (and (= nest max) (>= max 0))
614 (setq func (1+ func)))
615 (setq nest (1- nest)))
616 ((match-end 3) ; function|procedure
617 (if (= 0 func)
618 (throw 'found t)
619 (setq func (1- func)))))))
620 nil))
622 (defun pascal-end-of-defun ()
623 "Move forward to the end of the current function or procedure."
624 (interactive)
625 (if (looking-at "\\s ")
626 (forward-sexp 1))
627 (if (not (looking-at pascal-defun-re))
628 (pascal-beg-of-defun))
629 (forward-char 1)
630 (let ((nest 0) (func 1)
631 (reg (concat pascal-beg-block-re "\\|"
632 pascal-end-block-re "\\|"
633 pascal-defun-re)))
634 (while (and (/= func 0)
635 (re-search-forward reg nil 'move))
636 (cond ((let ((state (save-excursion
637 (parse-partial-sexp (point-min) (point)))))
638 (or (nth 3 state) (nth 4 state))) ; Inside string or comment
640 ((match-end 1)
641 (setq nest (1+ nest))
642 (if (save-excursion
643 (goto-char (match-beginning 0))
644 (looking-at "\\<record\\>"))
645 (setq func (1+ func))))
646 ((match-end 2)
647 (setq nest (1- nest))
648 (if (= nest 0)
649 (setq func (1- func))))
650 ((match-end 3)
651 (setq func (1+ func))))))
652 (forward-line 1))
654 (defun pascal-end-of-statement ()
655 "Move forward to end of current statement."
656 (interactive)
657 (let ((parse-sexp-ignore-comments t)
658 (nest 0) pos
659 (regexp (concat "\\(" pascal-beg-block-re "\\)\\|\\("
660 pascal-end-block-re "\\)")))
661 (if (not (looking-at "[ \t\n]")) (forward-sexp -1))
662 (or (looking-at pascal-beg-block-re)
663 ;; Skip to end of statement
664 (setq pos (catch 'found
665 (while t
666 (forward-sexp 1)
667 (cond ((looking-at "[ \t]*;")
668 (skip-chars-forward "^;")
669 (forward-char 1)
670 (throw 'found (point)))
671 ((save-excursion
672 (forward-sexp -1)
673 (looking-at pascal-beg-block-re))
674 (goto-char (match-beginning 0))
675 (throw 'found nil))
676 ((eobp)
677 (throw 'found (point))))))))
678 (if (not pos)
679 ;; Skip a whole block
680 (catch 'found
681 (while t
682 (re-search-forward regexp nil 'move)
683 (setq nest (if (match-end 1)
684 (1+ nest)
685 (1- nest)))
686 (cond ((eobp)
687 (throw 'found (point)))
688 ((= 0 nest)
689 (throw 'found (pascal-end-of-statement))))))
690 pos)))
692 (defun pascal-downcase-keywords ()
693 "Downcase all Pascal keywords in the buffer."
694 (interactive)
695 (pascal-change-keywords 'downcase-word))
697 (defun pascal-upcase-keywords ()
698 "Upcase all Pascal keywords in the buffer."
699 (interactive)
700 (pascal-change-keywords 'upcase-word))
702 (defun pascal-capitalize-keywords ()
703 "Capitalize all Pascal keywords in the buffer."
704 (interactive)
705 (pascal-change-keywords 'capitalize-word))
707 ;; Change the keywords according to argument.
708 (defun pascal-change-keywords (change-word)
709 (save-excursion
710 (let ((keyword-re (concat "\\<\\("
711 (mapconcat 'identity pascal-keywords "\\|")
712 "\\)\\>")))
713 (goto-char (point-min))
714 (while (re-search-forward keyword-re nil t)
715 (funcall change-word -1)))))
720 ;;; Other functions
722 (defun pascal-set-auto-comments ()
723 "Insert `{ case }' or `{ NAME }' on this line if appropriate.
724 Insert `{ case }' if there is an `end' on the line which
725 ends a case block. Insert `{ NAME }' if there is an `end'
726 on the line which ends a function or procedure named NAME."
727 (save-excursion
728 (forward-line -1)
729 (skip-chars-forward " \t")
730 (if (and (looking-at "\\<end;")
731 (not (save-excursion
732 (end-of-line)
733 (search-backward "{" (point-at-bol) t))))
734 (let ((type (car (pascal-calculate-indent))))
735 (if (eq type 'declaration)
737 (if (eq type 'case)
738 ;; This is a case block
739 (progn
740 (end-of-line)
741 (delete-horizontal-space)
742 (insert " { case }"))
743 (let ((nest 1))
744 ;; Check if this is the end of a function
745 (save-excursion
746 (while (not (or (looking-at pascal-defun-re) (bobp)))
747 (backward-sexp 1)
748 (cond ((looking-at pascal-beg-block-re)
749 (setq nest (1- nest)))
750 ((looking-at pascal-end-block-re)
751 (setq nest (1+ nest)))))
752 (if (bobp)
753 (setq nest 1)))
754 (if (zerop nest)
755 (progn
756 (end-of-line)
757 (delete-horizontal-space)
758 (insert " { ")
759 (let (b e)
760 (save-excursion
761 (setq b (progn (pascal-beg-of-defun)
762 (skip-chars-forward "^ \t")
763 (skip-chars-forward " \t")
764 (point))
765 e (progn (skip-chars-forward "a-zA-Z0-9_")
766 (point))))
767 (insert-buffer-substring (current-buffer) b e))
768 (insert " }"))))))))))
773 ;;; Indentation
775 (defconst pascal-indent-alist
776 '((block . (+ pascal--extra-indent pascal-indent-level))
777 (case . (+ pascal--extra-indent pascal-case-indent))
778 (caseblock . pascal--extra-indent) (cpp . 0)
779 (declaration . (+ pascal--extra-indent pascal-indent-level))
780 (paramlist . (pascal-indent-paramlist t))
781 (comment . (pascal-indent-comment))
782 (defun . pascal--extra-indent) (contexp . pascal--extra-indent)
783 (unknown . pascal--extra-indent) (string . 0) (progbeg . 0)))
785 (defun pascal-indent-command ()
786 "Indent for special part of code."
787 (let* ((indent-str (pascal-calculate-indent))
788 (type (car indent-str)))
789 (cond ((and (eq type 'paramlist)
790 (or (memq 'all pascal-auto-lineup)
791 (memq 'paramlist pascal-auto-lineup)))
792 (pascal-indent-paramlist)
793 (pascal-indent-paramlist))
794 ((and (eq type 'declaration)
795 (or (memq 'all pascal-auto-lineup)
796 (memq 'declaration pascal-auto-lineup)))
797 (pascal-indent-declaration))
798 ((and (eq type 'case) (not (looking-at "^[ \t]*$"))
799 (or (memq 'all pascal-auto-lineup)
800 (memq 'case pascal-auto-lineup)))
801 (pascal-indent-case)))
802 (if (looking-at "[ \t]+$")
803 (skip-chars-forward " \t"))))
805 (defun pascal-indent-line ()
806 "Indent current line as a Pascal statement."
807 (let* ((indent-str (pascal-calculate-indent))
808 (type (car indent-str))
809 (pascal--extra-indent (car (cdr indent-str))))
810 ;; Labels should not be indented.
811 (if (and (looking-at "^[0-9a-zA-Z]+[ \t]*:[^=]")
812 (not (eq type 'declaration)))
813 (search-forward ":" nil t))
814 (delete-horizontal-space)
815 (cond (; Some things should not be indented
816 (or (and (eq type 'declaration) (looking-at pascal-declaration-re))
817 (eq type 'cpp))
819 (; Other things should have no extra indent
820 (looking-at pascal-noindent-re)
821 (indent-to pascal--extra-indent))
822 (; Nested functions should be indented
823 (looking-at pascal-defun-re)
824 (if (and pascal-indent-nested-functions
825 (eq type 'defun))
826 (indent-to (+ pascal--extra-indent pascal-indent-level))
827 (indent-to pascal--extra-indent)))
828 (; But most lines are treated this way
829 (indent-to (eval (cdr (assoc type pascal-indent-alist))))
830 ))))
832 (defun pascal-calculate-indent ()
833 "Calculate the indent of the current Pascal line.
834 Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
835 (save-excursion
836 (let* ((parse-sexp-ignore-comments t)
837 (oldpos (point))
838 (state (save-excursion (parse-partial-sexp (point-min) (point))))
839 (nest 0) (par 0) (complete (looking-at "[ \t]*end\\>"))
840 (elsed (looking-at "[ \t]*else\\>")) (funccnt 0)
841 (did-func (looking-at "[ \t]*\\(procedure\\|function\\)\\>"))
842 (type (catch 'nesting
843 ;; Check if inside a string, comment or parenthesis
844 (cond ((nth 3 state) (throw 'nesting 'string))
845 ((nth 4 state) (throw 'nesting 'comment))
846 ((> (car state) 0)
847 (goto-char (scan-lists (point) -1 (car state)))
848 (setq par (1+ (current-column))))
849 ((save-excursion (beginning-of-line)
850 (eq (following-char) ?#))
851 (throw 'nesting 'cpp)))
852 ;; Loop until correct indent is found
853 (while t
854 (backward-sexp 1)
855 (cond (;--Escape from case statements
856 (and (looking-at "[A-Za-z0-9]+[ \t]*:[^=]")
857 (not complete)
858 (save-excursion (skip-chars-backward " \t")
859 (bolp))
860 (= (save-excursion
861 (end-of-line) (backward-sexp) (point))
862 (point))
863 (> (save-excursion (goto-char oldpos)
864 (beginning-of-line)
865 (point))
866 (point)))
867 (throw 'nesting 'caseblock))
868 (;--Beginning of program
869 (looking-at pascal-progbeg-re)
870 (throw 'nesting 'progbeg))
871 (;--No known statements
872 (bobp)
873 (throw 'nesting 'progbeg))
874 (;--Nest block outwards
875 (looking-at pascal-beg-block-re)
876 (if (= nest 0)
877 (cond ((looking-at "case\\>")
878 (throw 'nesting 'case))
879 ((looking-at "record\\>")
880 (throw 'nesting 'declaration))
881 (t (throw 'nesting 'block)))
882 (if (and (looking-at "record\\>") (= nest 1))
883 (setq funccnt (1- funccnt)))
884 (setq nest (1- nest))))
885 (;--Nest block inwards
886 (looking-at pascal-end-block-re)
887 (if (and (looking-at "end\\s ")
888 elsed (not complete))
889 (throw 'nesting 'block))
890 (if (= nest 0)
891 (setq funccnt (1+ funccnt)))
892 (setq complete t
893 nest (1+ nest)))
894 (;--Defun (or parameter list)
895 (and (looking-at pascal-defun-re)
896 (progn (setq funccnt (1- funccnt)
897 did-func t)
898 (or (bolp) (< funccnt 0))))
899 ;; Prevent searching whole buffer
900 (if (and (bolp) (>= funccnt 0))
901 (throw 'nesting 'progbeg))
902 (if (= 0 par)
903 (throw 'nesting 'defun)
904 (setq par 0)
905 (let ((n 0))
906 (while (re-search-forward
907 "\\(\\<record\\>\\)\\|\\<end\\>"
908 oldpos t)
909 (if (match-end 1)
910 (setq n (1+ n)) (setq n (1- n))))
911 (if (> n 0)
912 (throw 'nesting 'declaration)
913 (throw 'nesting 'paramlist)))))
914 (;--Declaration part
915 (and (looking-at pascal-declaration-re)
916 (not did-func)
917 (= funccnt 0))
918 (if (save-excursion
919 (goto-char oldpos)
920 (forward-line -1)
921 (looking-at "^[ \t]*$"))
922 (throw 'nesting 'unknown)
923 (throw 'nesting 'declaration)))
924 (;--If, else or while statement
925 (and (not complete)
926 (looking-at pascal-sub-block-re))
927 (throw 'nesting 'block))
928 (;--Found complete statement
929 (save-excursion (forward-sexp 1)
930 (= (following-char) ?\;))
931 (setq complete t))
932 )))))
934 ;; Return type of block and indent level.
935 (if (> par 0) ; Unclosed Parenthesis
936 (list 'contexp par)
937 (list type (pascal-indent-level))))))
939 (defun pascal-indent-level ()
940 "Return the indent-level the current statement has.
941 Do not count labels, case-statements or records."
942 (save-excursion
943 (beginning-of-line)
944 (if (looking-at "[ \t]*[0-9a-zA-Z]+[ \t]*:[^=]")
945 (search-forward ":" nil t)
946 (if (looking-at ".*=[ \t]*record\\>")
947 (search-forward "=" nil t)))
948 (skip-chars-forward " \t")
949 (current-column)))
951 (defun pascal-indent-comment ()
952 "Return indent for current comment."
953 (save-excursion
954 (re-search-backward "\\((\\*\\)\\|{" nil t)
955 (if (match-beginning 1)
956 (1+ (current-column))
957 (current-column))))
959 (defun pascal-indent-case ()
960 "Indent within case statements."
961 (let ((savepos (point-marker))
962 (end (prog2
963 (end-of-line)
964 (point-marker)
965 (re-search-backward "\\<case\\>" nil t)))
966 (beg (point))
967 (pascal--extra-indent 0))
968 ;; Get right indent
969 (while (< (point) end)
970 (if (re-search-forward
971 "^[ \t]*[^ \t,:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
972 (marker-position end) 'move)
973 (forward-char -1))
974 (if (< (point) end)
975 (progn
976 (delete-horizontal-space)
977 (if (> (current-column) pascal--extra-indent)
978 (setq pascal--extra-indent (current-column)))
979 (pascal-end-of-statement))))
980 (goto-char beg)
981 ;; Indent all case statements
982 (while (< (point) end)
983 (if (re-search-forward
984 "^[ \t]*[^][ \t,\\.:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
985 (marker-position end) 'move)
986 (forward-char -1))
987 (indent-to (1+ pascal--extra-indent))
988 (if (/= (following-char) ?:)
990 (forward-char 1)
991 (delete-horizontal-space)
992 (insert " "))
993 (pascal-end-of-statement))
994 (goto-char savepos)))
996 (defun pascal-indent-paramlist (&optional arg)
997 "Indent current line in parameterlist.
998 If optional arg is non-nil, just return the
999 indent of the current line in parameterlist."
1000 (save-excursion
1001 (let* ((oldpos (point))
1002 (stpos (progn (goto-char (scan-lists (point) -1 1)) (point)))
1003 (stcol (1+ (current-column)))
1004 (edpos (progn (pascal-declaration-end)
1005 (search-backward ")" (point-at-bol) t)
1006 (point)))
1007 (usevar (re-search-backward "\\<var\\>" stpos t)))
1008 (if arg (progn
1009 ;; If arg, just return indent
1010 (goto-char oldpos)
1011 (beginning-of-line)
1012 (if (or (not usevar) (looking-at "[ \t]*var\\>"))
1013 stcol (+ 4 stcol)))
1014 (goto-char stpos)
1015 (forward-char 1)
1016 (delete-horizontal-space)
1017 (if (and usevar (not (looking-at "var\\>")))
1018 (indent-to (+ 4 stcol)))
1019 (pascal-indent-declaration nil stpos edpos)))))
1021 (defun pascal-indent-declaration (&optional arg start end)
1022 "Indent current lines as declaration, lining up the `:'s or `='s."
1023 (let ((pos (point-marker)))
1024 (if (and (not (or arg start)) (not (pascal-declaration-beg)))
1026 (let ((lineup (if (or (looking-at "\\<var\\>\\|\\<record\\>") arg start)
1027 ":" "="))
1028 (stpos (if start start
1029 (forward-word 2) (backward-word 1) (point)))
1030 (edpos (set-marker (make-marker)
1031 (if end end
1032 (max (progn (pascal-declaration-end)
1033 (point))
1034 pos))))
1035 pascal--extra-indent)
1037 (goto-char stpos)
1038 ;; Indent lines in record block
1039 (if arg
1040 (while (<= (point) edpos)
1041 (beginning-of-line)
1042 (delete-horizontal-space)
1043 (if (looking-at "end\\>")
1044 (indent-to arg)
1045 (indent-to (+ arg pascal-indent-level)))
1046 (forward-line 1)))
1048 ;; Do lineup
1049 (setq pascal--extra-indent (pascal-get-lineup-indent stpos edpos lineup))
1050 (goto-char stpos)
1051 (while (and (<= (point) edpos) (not (eobp)))
1052 (if (search-forward lineup (point-at-eol) 'move)
1053 (forward-char -1))
1054 (delete-horizontal-space)
1055 (indent-to pascal--extra-indent)
1056 (if (not (looking-at lineup))
1057 (forward-line 1) ; No more indent if there is no : or =
1058 (forward-char 1)
1059 (delete-horizontal-space)
1060 (insert " ")
1061 ;; Indent record block
1062 (if (looking-at "record\\>")
1063 (pascal-indent-declaration (current-column)))
1064 (forward-line 1)))))
1066 ;; If arg - move point
1067 (if arg (forward-line -1)
1068 (goto-char pos))))
1070 ; "Return the indent level that will line up several lines within the region
1071 ;from b to e nicely. The lineup string is str."
1072 (defun pascal-get-lineup-indent (b e str)
1073 (save-excursion
1074 (let ((pascal--extra-indent 0)
1075 (reg (concat str "\\|\\(\\<record\\>\\)\\|" pascal-defun-re)))
1076 (goto-char b)
1077 ;; Get rightmost position
1078 (while (< (point) e)
1079 (and (re-search-forward reg (min e (point-at-eol 2)) 'move)
1080 (cond ((match-beginning 1)
1081 ;; Skip record blocks
1082 (pascal-declaration-end))
1083 ((match-beginning 2)
1084 ;; We have entered a new procedure. Exit.
1085 (goto-char e))
1087 (goto-char (match-beginning 0))
1088 (skip-chars-backward " \t")
1089 (if (> (current-column) pascal--extra-indent)
1090 (setq pascal--extra-indent (current-column)))
1091 (goto-char (match-end 0))
1092 (end-of-line)
1093 ))))
1094 ;; In case no lineup was found
1095 (if (> pascal--extra-indent 0)
1096 (1+ pascal--extra-indent)
1097 ;; No lineup-string found
1098 (goto-char b)
1099 (end-of-line)
1100 (skip-chars-backward " \t")
1101 (1+ (current-column))))))
1106 ;;; Completion
1108 (defun pascal-string-diff (str1 str2)
1109 "Return index of first letter where STR1 and STR2 differs."
1110 (catch 'done
1111 (let ((diff 0))
1112 (while t
1113 (if (or (> (1+ diff) (length str1))
1114 (> (1+ diff) (length str2)))
1115 (throw 'done diff))
1116 (or (equal (aref str1 diff) (aref str2 diff))
1117 (throw 'done diff))
1118 (setq diff (1+ diff))))))
1120 ;; Calculate all possible completions for functions if argument is `function',
1121 ;; completions for procedures if argument is `procedure' or both functions and
1122 ;; procedures otherwise.
1124 (defun pascal-func-completion (type pascal-str)
1125 ;; Build regular expression for function/procedure names
1126 (save-excursion
1127 (if (string= pascal-str "")
1128 (setq pascal-str "[a-zA-Z_]"))
1129 (let ((pascal-str (concat (cond
1130 ((eq type 'procedure) "\\<\\(procedure\\)\\s +")
1131 ((eq type 'function) "\\<\\(function\\)\\s +")
1132 (t "\\<\\(function\\|procedure\\)\\s +"))
1133 "\\<\\(" pascal-str "[a-zA-Z0-9_.]*\\)\\>"))
1134 (pascal-all ())
1135 match)
1137 (if (not (looking-at "\\<\\(function\\|procedure\\)\\>"))
1138 (re-search-backward "\\<\\(function\\|procedure\\)\\>" nil t))
1139 (forward-char 1)
1141 ;; Search through all reachable functions
1142 (while (pascal-beg-of-defun)
1143 (if (re-search-forward pascal-str (point-at-eol) t)
1144 (progn (setq match (buffer-substring (match-beginning 2)
1145 (match-end 2)))
1146 (push match pascal-all)))
1147 (goto-char (match-beginning 0)))
1149 pascal-all)))
1151 (defun pascal-get-completion-decl (pascal-str)
1152 ;; Macro for searching through current declaration (var, type or const)
1153 ;; for matches of `str' and adding the occurrence to `all'
1154 (let ((end (save-excursion (pascal-declaration-end)
1155 (point)))
1156 (pascal-all ())
1157 match)
1158 ;; Traverse lines
1159 (while (< (point) end)
1160 (if (re-search-forward "[:=]" (point-at-eol) t)
1161 ;; Traverse current line
1162 (while (and (re-search-backward
1163 (concat "\\((\\|\\<\\(var\\|type\\|const\\)\\>\\)\\|"
1164 pascal-symbol-re)
1165 (point-at-bol) t)
1166 (not (match-end 1)))
1167 (setq match (buffer-substring (match-beginning 0) (match-end 0)))
1168 (if (string-match (concat "\\<" pascal-str) match)
1169 (push match pascal-all))))
1170 (if (re-search-forward "\\<record\\>" (point-at-eol) t)
1171 (pascal-declaration-end)
1172 (forward-line 1)))
1174 pascal-all))
1176 (defun pascal-type-completion (pascal-str)
1177 "Calculate all possible completions for types."
1178 (let ((start (point))
1179 (pascal-all ())
1180 goon)
1181 ;; Search for all reachable type declarations
1182 (while (or (pascal-beg-of-defun)
1183 (setq goon (not goon)))
1184 (save-excursion
1185 (if (and (< start (prog1 (save-excursion (pascal-end-of-defun)
1186 (point))
1187 (forward-char 1)))
1188 (re-search-forward
1189 "\\<type\\>\\|\\<\\(begin\\|function\\|procedure\\)\\>"
1190 start t)
1191 (not (match-end 1)))
1192 ;; Check current type declaration
1193 (setq pascal-all
1194 (nconc (pascal-get-completion-decl pascal-str)
1195 pascal-all)))))
1197 pascal-all))
1199 (defun pascal-var-completion (prefix)
1200 "Calculate all possible completions for variables (or constants)."
1201 (save-excursion
1202 (let ((start (point))
1203 (pascal-all ())
1204 goon twice)
1205 ;; Search for all reachable var declarations
1206 (while (or (pascal-beg-of-defun)
1207 (setq goon (not goon)))
1208 (save-excursion
1209 (if (> start (prog1 (save-excursion (pascal-end-of-defun)
1210 (point))))
1211 () ; Declarations not reachable
1212 (if (search-forward "(" (point-at-eol) t)
1213 ;; Check parameterlist
1214 ;; FIXME: pascal-get-completion-decl doesn't understand
1215 ;; the var declarations in parameter lists :-(
1216 (setq pascal-all
1217 (nconc (pascal-get-completion-decl prefix)
1218 pascal-all)))
1219 (setq twice 2)
1220 (while (>= (setq twice (1- twice)) 0)
1221 (cond
1222 ((and (re-search-forward
1223 (concat "\\<\\(var\\|const\\)\\>\\|"
1224 "\\<\\(begin\\|function\\|procedure\\)\\>")
1225 start t)
1226 (not (match-end 2)))
1227 ;; Check var/const declarations
1228 (setq pascal-all
1229 (nconc (pascal-get-completion-decl prefix)
1230 pascal-all)))
1231 ((match-end 2)
1232 (setq twice 0)))))))
1233 pascal-all)))
1236 (defun pascal-keyword-completion (keyword-list pascal-str)
1237 "Give list of all possible completions of keywords in KEYWORD-LIST."
1238 (let ((pascal-all ()))
1239 (dolist (s keyword-list)
1240 (if (string-match (concat "\\<" pascal-str) s)
1241 (push s pascal-all)))
1242 pascal-all))
1244 ;; Function passed to completing-read, try-completion or
1245 ;; all-completions to get completion on STR. If predicate is non-nil,
1246 ;; it must be a function to be called for every match to check if this
1247 ;; should really be a match. If flag is t, the function returns a list
1248 ;; of all possible completions. If it is nil it returns a string, the
1249 ;; longest possible completion, or t if STR is an exact match. If flag
1250 ;; is 'lambda, the function returns t if STR is an exact match, nil
1251 ;; otherwise.
1253 (defvar pascal-completion-cache nil)
1255 (defun pascal-completion (pascal-str pascal-pred pascal-flag)
1256 (let ((all (car pascal-completion-cache)))
1257 ;; Check the cache's freshness.
1258 (unless (and pascal-completion-cache
1259 (string-prefix-p (nth 1 pascal-completion-cache) pascal-str)
1260 (eq (current-buffer) (nth 2 pascal-completion-cache))
1261 (eq (field-beginning) (nth 3 pascal-completion-cache)))
1262 (let ((state (car (pascal-calculate-indent))))
1263 (setq all
1264 ;; Determine what should be completed
1265 (cond
1266 ( ;--Within a declaration or parameterlist
1267 (or (eq state 'declaration) (eq state 'paramlist)
1268 (and (eq state 'defun)
1269 (save-excursion
1270 (re-search-backward ")[ \t]*:" (point-at-bol) t))))
1271 (if (or (eq state 'paramlist) (eq state 'defun))
1272 (pascal-beg-of-defun))
1273 (nconc
1274 (pascal-type-completion pascal-str)
1275 (pascal-keyword-completion pascal-type-keywords pascal-str)))
1276 ( ;--Starting a new statement
1277 (and (not (eq state 'contexp))
1278 (save-excursion
1279 (skip-chars-backward "a-zA-Z0-9_.")
1280 (backward-sexp 1)
1281 (or (looking-at pascal-nosemi-re)
1282 (progn
1283 (forward-sexp 1)
1284 (looking-at "\\s *\\(;\\|:[^=]\\)")))))
1285 (nconc
1286 (pascal-var-completion pascal-str)
1287 (pascal-func-completion 'procedure pascal-str)
1288 (pascal-keyword-completion pascal-start-keywords pascal-str)))
1289 (t ;--Anywhere else
1290 (nconc
1291 (pascal-var-completion pascal-str)
1292 (pascal-func-completion 'function pascal-str)
1293 (pascal-keyword-completion pascal-separator-keywords
1294 pascal-str)))))
1296 (setq pascal-completion-cache
1297 (list all pascal-str (current-buffer) (field-beginning)))))
1299 ;; Now we have built a list of all matches. Give response to caller
1300 (complete-with-action pascal-flag all pascal-str pascal-pred)))
1302 (defvar pascal-last-word-numb 0)
1303 (defvar pascal-last-word-shown nil)
1304 (defvar pascal-last-completions nil)
1306 (defun pascal-completions-at-point ()
1307 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
1308 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point))))
1309 (when (> e b)
1310 (list b e #'pascal-completion))))
1312 (define-obsolete-function-alias 'pascal-complete-word
1313 'completion-at-point "24.1")
1315 (define-obsolete-function-alias 'pascal-show-completions
1316 'completion-help-at-point "24.1")
1319 (defun pascal-get-default-symbol ()
1320 "Return symbol around current point as a string."
1321 (save-excursion
1322 (buffer-substring (progn
1323 (skip-chars-backward " \t")
1324 (skip-chars-backward "a-zA-Z0-9_")
1325 (point))
1326 (progn
1327 (skip-chars-forward "a-zA-Z0-9_")
1328 (point)))))
1330 (defun pascal-build-defun-re (str &optional arg)
1331 "Return function/procedure starting with STR as regular expression.
1332 With optional second arg non-nil, STR is the complete name of the instruction."
1333 (if arg
1334 (concat "^\\(function\\|procedure\\)[ \t]+\\(" str "\\)\\>")
1335 (concat "^\\(function\\|procedure\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))
1337 ;; Function passed to completing-read, try-completion or
1338 ;; all-completions to get completion on any function name. If
1339 ;; predicate is non-nil, it must be a function to be called for every
1340 ;; match to check if this should really be a match. If flag is t, the
1341 ;; function returns a list of all possible completions. If it is nil
1342 ;; it returns a string, the longest possible completion, or t if STR
1343 ;; is an exact match. If flag is 'lambda, the function returns t if
1344 ;; STR is an exact match, nil otherwise.
1346 (defun pascal-comp-defun (pascal-str pascal-pred pascal-flag)
1347 (save-excursion
1348 (let ((pascal-all nil))
1350 ;; Build regular expression for functions
1351 (let ((pascal-str (pascal-build-defun-re (if (string= pascal-str "")
1352 "[a-zA-Z_]"
1353 pascal-str))))
1354 (goto-char (point-min))
1356 ;; Build a list of all possible completions
1357 (while (re-search-forward pascal-str nil t)
1358 (push (match-string 2) pascal-all)))
1360 ;; Now we have built a list of all matches. Give response to caller
1361 (complete-with-action pascal-flag pascal-all pascal-str pascal-pred))))
1363 (defun pascal-goto-defun ()
1364 "Move to specified Pascal function/procedure.
1365 The default is a name found in the buffer around point."
1366 (interactive)
1367 (let* ((default (pascal-get-default-symbol))
1368 (default (if (pascal-comp-defun default nil 'lambda)
1369 default ""))
1370 (label
1371 ;; Do completion with default.
1372 (completing-read (if (not (string= default ""))
1373 (concat "Label (default " default "): ")
1374 "Label: ")
1375 ;; Complete with the defuns found in the
1376 ;; current-buffer.
1377 (let ((buf (current-buffer)))
1378 (lambda (s p a)
1379 (with-current-buffer buf
1380 (pascal-comp-defun s p a))))
1381 nil t "")))
1382 ;; If there was no response on prompt, use default value.
1383 (if (string= label "")
1384 (setq label default))
1385 ;; Goto right place in buffer if label is not an empty string.
1386 (or (string= label "")
1387 (progn
1388 (goto-char (point-min))
1389 (re-search-forward (pascal-build-defun-re label t))
1390 (beginning-of-line)))))
1395 ;;; Pascal-outline-mode
1397 (defvar pascal-outline-map
1398 (let ((map (make-sparse-keymap)))
1399 (if (fboundp 'set-keymap-name)
1400 (set-keymap-name pascal-outline-map 'pascal-outline-map))
1401 (define-key map "\M-\C-a" 'pascal-outline-prev-defun)
1402 (define-key map "\M-\C-e" 'pascal-outline-next-defun)
1403 (define-key map "\C-c\C-d" 'pascal-outline-goto-defun)
1404 (define-key map "\C-c\C-s" 'pascal-show-all)
1405 (define-key map "\C-c\C-h" 'pascal-hide-other-defuns)
1406 map)
1407 "Keymap used in Pascal Outline mode.")
1409 (define-obsolete-function-alias 'pascal-outline 'pascal-outline-mode "22.1")
1410 (define-minor-mode pascal-outline-mode
1411 "Outline-line minor mode for Pascal mode.
1412 With a prefix argument ARG, enable the mode if ARG is positive,
1413 and disable it otherwise. If called from Lisp, enable the mode
1414 if ARG is omitted or nil.
1416 When enabled, portions of the text being edited may be made
1417 invisible. \\<pascal-outline-map>
1419 Pascal Outline mode provides some additional commands.
1421 \\[pascal-outline-prev-defun]\
1422 \t- Move to previous function/procedure, hiding everything else.
1423 \\[pascal-outline-next-defun]\
1424 \t- Move to next function/procedure, hiding everything else.
1425 \\[pascal-outline-goto-defun]\
1426 \t- Goto function/procedure prompted for in minibuffer,
1427 \t hide all other functions.
1428 \\[pascal-show-all]\t- Show the whole buffer.
1429 \\[pascal-hide-other-defuns]\
1430 \t- Hide everything but the current function (function under the cursor).
1431 \\[pascal-outline]\t- Leave pascal-outline-mode."
1432 :init-value nil :lighter " Outl" :keymap pascal-outline-map
1433 (add-to-invisibility-spec '(pascal . t))
1434 (unless pascal-outline-mode
1435 (pascal-show-all)))
1437 (defun pascal-outline-change (b e hide)
1438 (when (> e b)
1439 ;; We could try and optimize this in the case where the region is
1440 ;; already hidden. But I'm not sure it's worth the trouble.
1441 (remove-overlays b e 'invisible 'pascal)
1442 (when hide
1443 (let ((ol (make-overlay b e nil t nil)))
1444 (overlay-put ol 'invisible 'pascal)
1445 (overlay-put ol 'evaporate t)))))
1447 (defun pascal-show-all ()
1448 "Show all of the text in the buffer."
1449 (interactive)
1450 (pascal-outline-change (point-min) (point-max) nil))
1452 (defun pascal-hide-other-defuns ()
1453 "Show only the current defun."
1454 (interactive)
1455 (save-excursion
1456 (let ((beg (progn (if (not (looking-at "\\(function\\|procedure\\)\\>"))
1457 (pascal-beg-of-defun))
1458 (line-beginning-position)))
1459 (end (progn (pascal-end-of-defun)
1460 (backward-sexp 1)
1461 (line-beginning-position 2)))
1462 (opoint (point-min)))
1463 ;; BEG at BOL.
1464 ;; OPOINT at EOL.
1465 ;; END at BOL.
1466 (goto-char (point-min))
1468 ;; Hide all functions before current function
1469 (while (re-search-forward "^[ \t]*\\(function\\|procedure\\)\\>"
1470 beg 'move)
1471 (pascal-outline-change opoint (line-end-position 0) t)
1472 (setq opoint (line-end-position))
1473 ;; Functions may be nested
1474 (if (> (progn (pascal-end-of-defun) (point)) beg)
1475 (goto-char opoint)))
1476 (if (> beg opoint)
1477 (pascal-outline-change opoint (1- beg) t))
1479 ;; Show current function
1480 (pascal-outline-change (1- beg) end nil)
1481 ;; Hide nested functions
1482 (forward-char 1)
1483 (while (re-search-forward "^\\(function\\|procedure\\)\\>" end 'move)
1484 (setq opoint (line-end-position))
1485 (pascal-end-of-defun)
1486 (pascal-outline-change opoint (line-end-position) t))
1488 (goto-char end)
1489 (setq opoint end)
1491 ;; Hide all function after current function
1492 (while (re-search-forward "^\\(function\\|procedure\\)\\>" nil 'move)
1493 (pascal-outline-change opoint (line-end-position 0) t)
1494 (setq opoint (line-end-position))
1495 (pascal-end-of-defun))
1496 (pascal-outline-change opoint (point-max) t)
1498 ;; Hide main program
1499 (if (< (progn (forward-line -1) (point)) end)
1500 (progn
1501 (goto-char beg)
1502 (pascal-end-of-defun)
1503 (backward-sexp 1)
1504 (pascal-outline-change (line-end-position) (point-max) t))))))
1506 (defun pascal-outline-next-defun ()
1507 "Move to next function/procedure, hiding all others."
1508 (interactive)
1509 (pascal-end-of-defun)
1510 (pascal-hide-other-defuns))
1512 (defun pascal-outline-prev-defun ()
1513 "Move to previous function/procedure, hiding all others."
1514 (interactive)
1515 (pascal-beg-of-defun)
1516 (pascal-hide-other-defuns))
1518 (defun pascal-outline-goto-defun ()
1519 "Move to specified function/procedure, hiding all others."
1520 (interactive)
1521 (pascal-goto-defun)
1522 (pascal-hide-other-defuns))
1524 (provide 'pascal)
1526 ;;; pascal.el ends here