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