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