]> code.delx.au - gnu-emacs/blob - lisp/progmodes/pascal.el
Rewrite Font Lock support.
[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 (make-local-variable 'comment-indent-function)
321 (setq comment-indent-function 'pascal-indent-comment)
322 (make-local-variable 'parse-sexp-ignore-comments)
323 (setq parse-sexp-ignore-comments nil)
324 (make-local-variable 'case-fold-search)
325 (setq case-fold-search t)
326 (make-local-variable 'comment-start)
327 (setq comment-start "{")
328 (make-local-variable 'comment-start-skip)
329 (setq comment-start-skip "(\\*+ *\\|{ *")
330 (make-local-variable 'comment-end)
331 (setq comment-end "}")
332 ;; Font lock support
333 (make-local-variable 'font-lock-defaults)
334 (setq font-lock-defaults '(pascal-font-lock-keywords nil t))
335 ;; Imenu support
336 (make-local-variable 'imenu-generic-expression)
337 (setq imenu-generic-expression pascal-imenu-generic-expression)
338 (run-hooks 'pascal-mode-hook))
339
340 \f
341
342 ;;;
343 ;;; Electric functions
344 ;;;
345 (defun electric-pascal-terminate-line ()
346 "Terminate line and indent next line."
347 (interactive)
348 ;; First, check if current line should be indented
349 (save-excursion
350 (beginning-of-line)
351 (skip-chars-forward " \t")
352 (if (looking-at pascal-autoindent-lines-re)
353 (pascal-indent-line)))
354 (delete-horizontal-space) ; Removes trailing whitespaces
355 (newline)
356 ;; Indent next line
357 (pascal-indent-line)
358 ;; Maybe we should set some endcomments
359 (if pascal-auto-endcomments
360 (pascal-set-auto-comments))
361 ;; Check if we shall indent inside comment
362 (let ((setstar nil))
363 (save-excursion
364 (forward-line -1)
365 (skip-chars-forward " \t")
366 (cond ((looking-at "\\*[ \t]+)")
367 ;; Delete region between `*' and `)' if there is only whitespaces.
368 (forward-char 1)
369 (delete-horizontal-space))
370 ((and (looking-at "(\\*\\|\\*[^)]")
371 (not (save-excursion
372 (search-forward "*)" (pascal-get-end-of-line) t))))
373 (setq setstar t))))
374 ;; If last line was a star comment line then this one shall be too.
375 (if (null setstar)
376 (pascal-indent-line)
377 (insert "* "))))
378
379
380 (defun electric-pascal-semi-or-dot ()
381 "Insert `;' or `.' character and reindent the line."
382 (interactive)
383 (insert last-command-char)
384 (save-excursion
385 (beginning-of-line)
386 (pascal-indent-line))
387 (if pascal-auto-newline
388 (electric-pascal-terminate-line)))
389
390 (defun electric-pascal-colon ()
391 "Insert `:' and do all indentions except line indent on this line."
392 (interactive)
393 (insert last-command-char)
394 ;; Do nothing if within string.
395 (if (pascal-within-string)
396 ()
397 (save-excursion
398 (beginning-of-line)
399 (pascal-indent-line))
400 (let ((pascal-tab-always-indent nil))
401 (pascal-indent-command))))
402
403 (defun electric-pascal-equal ()
404 "Insert `=', and do indention if within type declaration."
405 (interactive)
406 (insert last-command-char)
407 (if (eq (car (pascal-calculate-indent)) 'declaration)
408 (let ((pascal-tab-always-indent nil))
409 (pascal-indent-command))))
410
411 (defun electric-pascal-hash ()
412 "Insert `#', and indent to column 0 if this is a CPP directive."
413 (interactive)
414 (insert last-command-char)
415 (if (save-excursion (beginning-of-line) (looking-at "^[ \t]*#"))
416 (save-excursion (beginning-of-line)
417 (delete-horizontal-space))))
418
419 (defun electric-pascal-tab ()
420 "Function called when TAB is pressed in Pascal mode."
421 (interactive)
422 ;; Do nothing if within a string or in a CPP directive.
423 (if (or (pascal-within-string)
424 (and (not (bolp))
425 (save-excursion (beginning-of-line) (eq (following-char) ?#))))
426 (insert "\t")
427 ;; If pascal-tab-always-indent, indent the beginning of the line.
428 (if pascal-tab-always-indent
429 (save-excursion
430 (beginning-of-line)
431 (pascal-indent-line))
432 (if (save-excursion
433 (skip-chars-backward " \t")
434 (bolp))
435 (pascal-indent-line)
436 (insert "\t")))
437 (pascal-indent-command)))
438
439 \f
440
441 ;;;
442 ;;; Interactive functions
443 ;;;
444 (defun pascal-insert-block ()
445 "Insert Pascal begin ... end; block in the code with right indentation."
446 (interactive)
447 (pascal-indent-line)
448 (insert "begin")
449 (electric-pascal-terminate-line)
450 (save-excursion
451 (electric-pascal-terminate-line)
452 (insert "end;")
453 (beginning-of-line)
454 (pascal-indent-line)))
455
456 (defun pascal-star-comment ()
457 "Insert Pascal star comment at point."
458 (interactive)
459 (pascal-indent-line)
460 (insert "(*")
461 (electric-pascal-terminate-line)
462 (save-excursion
463 (electric-pascal-terminate-line)
464 (delete-horizontal-space)
465 (insert ")"))
466 (insert " "))
467
468 (defun pascal-mark-defun ()
469 "Mark the current pascal function (or procedure).
470 This puts the mark at the end, and point at the beginning."
471 (interactive)
472 (push-mark (point))
473 (pascal-end-of-defun)
474 (push-mark (point))
475 (pascal-beg-of-defun)
476 (if (fboundp 'zmacs-activate-region)
477 (zmacs-activate-region)))
478
479 (defun pascal-comment-area (start end)
480 "Put the region into a Pascal comment.
481 The comments that are in this area are \"deformed\":
482 `*)' becomes `!(*' and `}' becomes `!{'.
483 These deformed comments are returned to normal if you use
484 \\[pascal-uncomment-area] to undo the commenting.
485
486 The commented area starts with `pascal-exclude-str-start', and ends with
487 `pascal-include-str-end'. But if you change these variables,
488 \\[pascal-uncomment-area] won't recognize the comments."
489 (interactive "r")
490 (save-excursion
491 ;; Insert start and endcomments
492 (goto-char end)
493 (if (and (save-excursion (skip-chars-forward " \t") (eolp))
494 (not (save-excursion (skip-chars-backward " \t") (bolp))))
495 (forward-line 1)
496 (beginning-of-line))
497 (insert pascal-exclude-str-end)
498 (setq end (point))
499 (newline)
500 (goto-char start)
501 (beginning-of-line)
502 (insert pascal-exclude-str-start)
503 (newline)
504 ;; Replace end-comments within commented area
505 (goto-char end)
506 (save-excursion
507 (while (re-search-backward "\\*)" start t)
508 (replace-match "!(*" t t)))
509 (save-excursion
510 (while (re-search-backward "}" start t)
511 (replace-match "!{" t t)))))
512
513 (defun pascal-uncomment-area ()
514 "Uncomment a commented area; change deformed comments back to normal.
515 This command does nothing if the pointer is not in a commented
516 area. See also `pascal-comment-area'."
517 (interactive)
518 (save-excursion
519 (let ((start (point))
520 (end (point)))
521 ;; Find the boundaries of the comment
522 (save-excursion
523 (setq start (progn (search-backward pascal-exclude-str-start nil t)
524 (point)))
525 (setq end (progn (search-forward pascal-exclude-str-end nil t)
526 (point))))
527 ;; Check if we're really inside a comment
528 (if (or (equal start (point)) (<= end (point)))
529 (message "Not standing within commented area.")
530 (progn
531 ;; Remove endcomment
532 (goto-char end)
533 (beginning-of-line)
534 (let ((pos (point)))
535 (end-of-line)
536 (delete-region pos (1+ (point))))
537 ;; Change comments back to normal
538 (save-excursion
539 (while (re-search-backward "!{" start t)
540 (replace-match "}" t t)))
541 (save-excursion
542 (while (re-search-backward "!(\\*" start t)
543 (replace-match "*)" t t)))
544 ;; Remove startcomment
545 (goto-char start)
546 (beginning-of-line)
547 (let ((pos (point)))
548 (end-of-line)
549 (delete-region pos (1+ (point)))))))))
550
551 (defun pascal-beg-of-defun ()
552 "Move backward to the beginning of the current function or procedure."
553 (interactive)
554 (catch 'found
555 (if (not (looking-at (concat "\\s \\|\\s)\\|" pascal-defun-re)))
556 (forward-sexp 1))
557 (let ((nest 0) (max -1) (func 0)
558 (reg (concat pascal-beg-block-re "\\|"
559 pascal-end-block-re "\\|"
560 pascal-defun-re)))
561 (while (re-search-backward reg nil 'move)
562 (cond ((let ((state (save-excursion
563 (parse-partial-sexp (point-min) (point)))))
564 (or (nth 3 state) (nth 4 state))) ; Inside string or comment
565 ())
566 ((match-end 1) ; begin|case|record|repeat
567 (if (and (looking-at "\\<record\\>") (>= max 0))
568 (setq func (1- func)))
569 (setq nest (1+ nest)
570 max (max nest max)))
571 ((match-end 2) ; end|until
572 (if (and (= nest max) (>= max 0))
573 (setq func (1+ func)))
574 (setq nest (1- nest)))
575 ((match-end 3) ; function|procedure
576 (if (= 0 func)
577 (throw 'found t)
578 (setq func (1- func)))))))
579 nil))
580
581 (defun pascal-end-of-defun ()
582 "Move forward to the end of the current function or procedure."
583 (interactive)
584 (if (looking-at "\\s ")
585 (forward-sexp 1))
586 (if (not (looking-at pascal-defun-re))
587 (pascal-beg-of-defun))
588 (forward-char 1)
589 (let ((nest 0) (func 1)
590 (reg (concat pascal-beg-block-re "\\|"
591 pascal-end-block-re "\\|"
592 pascal-defun-re)))
593 (while (and (/= func 0)
594 (re-search-forward reg nil 'move))
595 (cond ((let ((state (save-excursion
596 (parse-partial-sexp (point-min) (point)))))
597 (or (nth 3 state) (nth 4 state))) ; Inside string or comment
598 ())
599 ((match-end 1)
600 (setq nest (1+ nest))
601 (if (save-excursion
602 (goto-char (match-beginning 0))
603 (looking-at "\\<record\\>"))
604 (setq func (1+ func))))
605 ((match-end 2)
606 (setq nest (1- nest))
607 (if (= nest 0)
608 (setq func (1- func))))
609 ((match-end 3)
610 (setq func (1+ func))))))
611 (forward-line 1))
612
613 (defun pascal-end-of-statement ()
614 "Move forward to end of current statement."
615 (interactive)
616 (let ((parse-sexp-ignore-comments t)
617 (nest 0) pos
618 (regexp (concat "\\(" pascal-beg-block-re "\\)\\|\\("
619 pascal-end-block-re "\\)")))
620 (if (not (looking-at "[ \t\n]")) (forward-sexp -1))
621 (or (looking-at pascal-beg-block-re)
622 ;; Skip to end of statement
623 (setq pos (catch 'found
624 (while t
625 (forward-sexp 1)
626 (cond ((looking-at "[ \t]*;")
627 (skip-chars-forward "^;")
628 (forward-char 1)
629 (throw 'found (point)))
630 ((save-excursion
631 (forward-sexp -1)
632 (looking-at pascal-beg-block-re))
633 (goto-char (match-beginning 0))
634 (throw 'found nil))
635 ((eobp)
636 (throw 'found (point))))))))
637 (if (not pos)
638 ;; Skip a whole block
639 (catch 'found
640 (while t
641 (re-search-forward regexp nil 'move)
642 (setq nest (if (match-end 1)
643 (1+ nest)
644 (1- nest)))
645 (cond ((eobp)
646 (throw 'found (point)))
647 ((= 0 nest)
648 (throw 'found (pascal-end-of-statement))))))
649 pos)))
650
651 (defun pascal-downcase-keywords ()
652 "Downcase all Pascal keywords in the buffer."
653 (interactive)
654 (pascal-change-keywords 'downcase-word))
655
656 (defun pascal-upcase-keywords ()
657 "Upcase all Pascal keywords in the buffer."
658 (interactive)
659 (pascal-change-keywords 'upcase-word))
660
661 (defun pascal-capitalize-keywords ()
662 "Capitalize all Pascal keywords in the buffer."
663 (interactive)
664 (pascal-change-keywords 'capitalize-word))
665
666 ;; Change the keywords according to argument.
667 (defun pascal-change-keywords (change-word)
668 (save-excursion
669 (let ((keyword-re (concat "\\<\\("
670 (mapconcat 'identity pascal-keywords "\\|")
671 "\\)\\>")))
672 (goto-char (point-min))
673 (while (re-search-forward keyword-re nil t)
674 (funcall change-word -1)))))
675
676 \f
677
678 ;;;
679 ;;; Other functions
680 ;;;
681 (defun pascal-set-auto-comments ()
682 "Insert `{ case }' or `{ NAME }' on this line if appropriate.
683 Insert `{ case }' if there is an `end' on the line which
684 ends a case block. Insert `{ NAME }' if there is an `end'
685 on the line which ends a function or procedure named NAME."
686 (save-excursion
687 (forward-line -1)
688 (skip-chars-forward " \t")
689 (if (and (looking-at "\\<end;")
690 (not (save-excursion
691 (end-of-line)
692 (search-backward "{" (pascal-get-beg-of-line) t))))
693 (let ((type (car (pascal-calculate-indent))))
694 (if (eq type 'declaration)
695 ()
696 (if (eq type 'case)
697 ;; This is a case block
698 (progn
699 (end-of-line)
700 (delete-horizontal-space)
701 (insert " { case }"))
702 (let ((nest 1))
703 ;; Check if this is the end of a function
704 (save-excursion
705 (while (not (or (looking-at pascal-defun-re) (bobp)))
706 (backward-sexp 1)
707 (cond ((looking-at pascal-beg-block-re)
708 (setq nest (1- nest)))
709 ((looking-at pascal-end-block-re)
710 (setq nest (1+ nest)))))
711 (if (bobp)
712 (setq nest 1)))
713 (if (zerop nest)
714 (progn
715 (end-of-line)
716 (delete-horizontal-space)
717 (insert " { ")
718 (let (b e)
719 (save-excursion
720 (setq b (progn (pascal-beg-of-defun)
721 (skip-chars-forward "^ \t")
722 (skip-chars-forward " \t")
723 (point))
724 e (progn (skip-chars-forward "a-zA-Z0-9_")
725 (point))))
726 (insert-buffer-substring (current-buffer) b e))
727 (insert " }"))))))))))
728
729 \f
730
731 ;;;
732 ;;; Indentation
733 ;;;
734 (defconst pascal-indent-alist
735 '((block . (+ ind pascal-indent-level))
736 (case . (+ ind pascal-case-indent))
737 (caseblock . ind) (cpp . 0)
738 (declaration . (+ ind pascal-indent-level))
739 (paramlist . (pascal-indent-paramlist t))
740 (comment . (pascal-indent-comment t))
741 (defun . ind) (contexp . ind)
742 (unknown . 0) (string . 0)))
743
744 (defun pascal-indent-command ()
745 "Indent for special part of code."
746 (let* ((indent-str (pascal-calculate-indent))
747 (type (car indent-str))
748 (ind (car (cdr indent-str))))
749 (cond ((and (eq type 'paramlist)
750 (or (memq 'all pascal-auto-lineup)
751 (memq 'paramlist pascal-auto-lineup)))
752 (pascal-indent-paramlist)
753 (pascal-indent-paramlist))
754 ((and (eq type 'declaration)
755 (or (memq 'all pascal-auto-lineup)
756 (memq 'declaration pascal-auto-lineup)))
757 (pascal-indent-declaration))
758 ((and (eq type 'case) (not (looking-at "^[ \t]*$"))
759 (or (memq 'all pascal-auto-lineup)
760 (memq 'case pascal-auto-lineup)))
761 (pascal-indent-case)))
762 (if (looking-at "[ \t]+$")
763 (skip-chars-forward " \t"))))
764
765 (defun pascal-indent-line ()
766 "Indent current line as a Pascal statement."
767 (let* ((indent-str (pascal-calculate-indent))
768 (type (car indent-str))
769 (ind (car (cdr indent-str))))
770 (if (looking-at "^[0-9a-zA-Z]+[ \t]*:[^=]")
771 (search-forward ":" nil t))
772 (delete-horizontal-space)
773 ;; Some things should not be indented
774 (if (or (and (eq type 'declaration) (looking-at pascal-declaration-re))
775 (eq type 'cpp)
776 (looking-at pascal-defun-re))
777 ()
778 ;; Other things should have no extra indent
779 (if (looking-at pascal-noindent-re)
780 (indent-to ind)
781 ;; But most lines are treated this way:
782 (indent-to (eval (cdr (assoc type pascal-indent-alist))))
783 ))))
784
785 (defun pascal-calculate-indent ()
786 "Calculate the indent of the current Pascal line.
787 Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
788 (save-excursion
789 (let* ((parse-sexp-ignore-comments t)
790 (oldpos (point))
791 (state (save-excursion (parse-partial-sexp (point-min) (point))))
792 (nest 0) (par 0) (complete (looking-at "[ \t]*end\\>"))
793 (elsed (looking-at "[ \t]*else\\>"))
794 (type (catch 'nesting
795 ;; Check if inside a string, comment or parenthesis
796 (cond ((nth 3 state) (throw 'nesting 'string))
797 ((nth 4 state) (throw 'nesting 'comment))
798 ((> (car state) 0)
799 (goto-char (scan-lists (point) -1 (car state)))
800 (setq par (1+ (current-column))))
801 ((save-excursion (beginning-of-line)
802 (eq (following-char) ?#))
803 (throw 'nesting 'cpp)))
804 ;; Loop until correct indent is found
805 (while t
806 (backward-sexp 1)
807 (cond (;--Escape from case statements
808 (and (looking-at "[A-Za-z0-9]+[ \t]*:[^=]")
809 (not complete)
810 (save-excursion (skip-chars-backward " \t")
811 (bolp))
812 (= (save-excursion
813 (end-of-line) (backward-sexp) (point))
814 (point))
815 (> (save-excursion (goto-char oldpos)
816 (beginning-of-line)
817 (point))
818 (point)))
819 (throw 'nesting 'caseblock))
820 (;--Nest block outwards
821 (looking-at pascal-beg-block-re)
822 (if (= nest 0)
823 (cond ((looking-at "case\\>")
824 (throw 'nesting 'case))
825 ((looking-at "record\\>")
826 (throw 'nesting 'declaration))
827 (t (throw 'nesting 'block)))
828 (setq nest (1- nest))))
829 (;--Nest block inwards
830 (looking-at pascal-end-block-re)
831 (if (and (looking-at "end\\s ")
832 elsed (not complete))
833 (throw 'nesting 'block))
834 (setq complete t
835 nest (1+ nest)))
836 (;--Defun (or parameter list)
837 (looking-at pascal-defun-re)
838 (if (= 0 par)
839 (throw 'nesting 'defun)
840 (setq par 0)
841 (let ((n 0))
842 (while (re-search-forward
843 "\\(\\<record\\>\\)\\|\\<end\\>"
844 oldpos t)
845 (if (match-end 1)
846 (setq n (1+ n)) (setq n (1- n))))
847 (if (> n 0)
848 (throw 'nesting 'declaration)
849 (throw 'nesting 'paramlist)))))
850 (;--Declaration part
851 (looking-at pascal-declaration-re)
852 (if (save-excursion
853 (goto-char oldpos)
854 (forward-line -1)
855 (looking-at "^[ \t]*$"))
856 (throw 'nesting 'unknown)
857 (throw 'nesting 'declaration)))
858 (;--If, else or while statement
859 (and (not complete)
860 (looking-at pascal-sub-block-re))
861 (throw 'nesting 'block))
862 (;--Found complete statement
863 (save-excursion (forward-sexp 1)
864 (= (following-char) ?\;))
865 (setq complete t))
866 (;--No known statements
867 (bobp)
868 (throw 'nesting 'unknown))
869 )))))
870
871 ;; Return type of block and indent level.
872 (if (> par 0) ; Unclosed Parenthesis
873 (list 'contexp par)
874 (list type (pascal-indent-level))))))
875
876 (defun pascal-indent-level ()
877 "Return the indent-level the current statement has.
878 Do not count labels, case-statements or records."
879 (save-excursion
880 (beginning-of-line)
881 (if (looking-at "[ \t]*[0-9a-zA-Z]+[ \t]*:[^=]")
882 (search-forward ":" nil t)
883 (if (looking-at ".*=[ \t]*record\\>")
884 (search-forward "=" nil t)))
885 (skip-chars-forward " \t")
886 (current-column)))
887
888 (defun pascal-indent-comment (&optional arg)
889 "Indent current line as comment.
890 If optional arg is non-nil, just return the
891 column number the line should be indented to."
892 (let* ((stcol (save-excursion
893 (re-search-backward "(\\*\\|{" nil t)
894 (1+ (current-column)))))
895 (if arg stcol
896 (delete-horizontal-space)
897 (indent-to stcol))))
898
899 (defun pascal-indent-case ()
900 "Indent within case statements."
901 (let ((savepos (point-marker))
902 (end (prog2
903 (end-of-line)
904 (point-marker)
905 (re-search-backward "\\<case\\>" nil t)))
906 (beg (point)) oldpos
907 (ind 0))
908 ;; Get right indent
909 (while (< (point) (marker-position end))
910 (if (re-search-forward
911 "^[ \t]*[^ \t,:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
912 (marker-position end) 'move)
913 (forward-char -1))
914 (if (< (point) (marker-position end))
915 (progn
916 (delete-horizontal-space)
917 (if (> (current-column) ind)
918 (setq ind (current-column)))
919 (pascal-end-of-statement))))
920 (goto-char beg)
921 (setq oldpos (marker-position end))
922 ;; Indent all case statements
923 (while (< (point) (marker-position end))
924 (if (re-search-forward
925 "^[ \t]*[^][ \t,\\.:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
926 (marker-position end) 'move)
927 (forward-char -1))
928 (indent-to (1+ ind))
929 (if (/= (following-char) ?:)
930 ()
931 (forward-char 1)
932 (delete-horizontal-space)
933 (insert " "))
934 (setq oldpos (point))
935 (pascal-end-of-statement))
936 (goto-char savepos)))
937
938 (defun pascal-indent-paramlist (&optional arg)
939 "Indent current line in parameterlist.
940 If optional arg is non-nil, just return the
941 indent of the current line in parameterlist."
942 (save-excursion
943 (let* ((oldpos (point))
944 (stpos (progn (goto-char (scan-lists (point) -1 1)) (point)))
945 (stcol (1+ (current-column)))
946 (edpos (progn (pascal-declaration-end)
947 (search-backward ")" (pascal-get-beg-of-line) t)
948 (point)))
949 (usevar (re-search-backward "\\<var\\>" stpos t)))
950 (if arg (progn
951 ;; If arg, just return indent
952 (goto-char oldpos)
953 (beginning-of-line)
954 (if (or (not usevar) (looking-at "[ \t]*var\\>"))
955 stcol (+ 4 stcol)))
956 (goto-char stpos)
957 (forward-char 1)
958 (delete-horizontal-space)
959 (if (and usevar (not (looking-at "var\\>")))
960 (indent-to (+ 4 stcol)))
961 (pascal-indent-declaration nil stpos edpos)))))
962
963 (defun pascal-indent-declaration (&optional arg start end)
964 "Indent current lines as declaration, lining up the `:'s or `='s."
965 (let ((pos (point-marker)))
966 (if (and (not (or arg start)) (not (pascal-declaration-beg)))
967 ()
968 (let ((lineup (if (or (looking-at "\\<var\\>\\|\\<record\\>") arg start)
969 ":" "="))
970 (stpos (if start start
971 (forward-word 2) (backward-word 1) (point)))
972 (edpos (set-marker (make-marker)
973 (if end end
974 (max (progn (pascal-declaration-end)
975 (point))
976 pos))))
977 ind)
978
979 (goto-char stpos)
980 ;; Indent lines in record block
981 (if arg
982 (while (<= (point) (marker-position edpos))
983 (beginning-of-line)
984 (delete-horizontal-space)
985 (if (looking-at "end\\>")
986 (indent-to arg)
987 (indent-to (+ arg pascal-indent-level)))
988 (forward-line 1)))
989
990 ;; Do lineup
991 (setq ind (pascal-get-lineup-indent stpos edpos lineup))
992 (goto-char stpos)
993 (while (and (<= (point) (marker-position edpos))
994 (not (eobp)))
995 (if (search-forward lineup (pascal-get-end-of-line) 'move)
996 (forward-char -1))
997 (delete-horizontal-space)
998 (indent-to ind)
999 (if (not (looking-at lineup))
1000 (forward-line 1) ; No more indent if there is no : or =
1001 (forward-char 1)
1002 (delete-horizontal-space)
1003 (insert " ")
1004 ;; Indent record block
1005 (if (looking-at "record\\>")
1006 (pascal-indent-declaration (current-column)))
1007 (forward-line 1)))))
1008
1009 ;; If arg - move point
1010 (if arg (forward-line -1)
1011 (goto-char (marker-position pos)))))
1012
1013 ; "Return the indent level that will line up several lines within the region
1014 ;from b to e nicely. The lineup string is str."
1015 (defun pascal-get-lineup-indent (b e str)
1016 (save-excursion
1017 (let ((ind 0)
1018 (reg (concat str "\\|\\(\\<record\\>\\)"))
1019 nest)
1020 (goto-char b)
1021 ;; Get rightmost position
1022 (while (< (point) e)
1023 (setq nest 1)
1024 (if (re-search-forward reg (min e (pascal-get-end-of-line 2)) 'move)
1025 (progn
1026 ;; Skip record blocks
1027 (if (match-beginning 1)
1028 (pascal-declaration-end)
1029 (progn
1030 (goto-char (match-beginning 0))
1031 (skip-chars-backward " \t")
1032 (if (> (current-column) ind)
1033 (setq ind (current-column)))
1034 (goto-char (match-end 0)))))))
1035 ;; In case no lineup was found
1036 (if (> ind 0)
1037 (1+ ind)
1038 ;; No lineup-string found
1039 (goto-char b)
1040 (end-of-line)
1041 (skip-chars-backward " \t")
1042 (1+ (current-column))))))
1043
1044 \f
1045
1046 ;;;
1047 ;;; Completion
1048 ;;;
1049 (defvar pascal-str nil)
1050 (defvar pascal-all nil)
1051 (defvar pascal-pred nil)
1052 (defvar pascal-buffer-to-use nil)
1053 (defvar pascal-flag nil)
1054
1055 (defun pascal-string-diff (str1 str2)
1056 "Return index of first letter where STR1 and STR2 differs."
1057 (catch 'done
1058 (let ((diff 0))
1059 (while t
1060 (if (or (> (1+ diff) (length str1))
1061 (> (1+ diff) (length str2)))
1062 (throw 'done diff))
1063 (or (equal (aref str1 diff) (aref str2 diff))
1064 (throw 'done diff))
1065 (setq diff (1+ diff))))))
1066
1067 ;; Calculate all possible completions for functions if argument is `function',
1068 ;; completions for procedures if argument is `procedure' or both functions and
1069 ;; procedures otherwise.
1070
1071 (defun pascal-func-completion (type)
1072 ;; Build regular expression for function/procedure names
1073 (if (string= pascal-str "")
1074 (setq pascal-str "[a-zA-Z_]"))
1075 (let ((pascal-str (concat (cond
1076 ((eq type 'procedure) "\\<\\(procedure\\)\\s +")
1077 ((eq type 'function) "\\<\\(function\\)\\s +")
1078 (t "\\<\\(function\\|procedure\\)\\s +"))
1079 "\\<\\(" pascal-str "[a-zA-Z0-9_.]*\\)\\>"))
1080 match)
1081
1082 (if (not (looking-at "\\<\\(function\\|procedure\\)\\>"))
1083 (re-search-backward "\\<\\(function\\|procedure\\)\\>" nil t))
1084 (forward-char 1)
1085
1086 ;; Search through all reachable functions
1087 (while (pascal-beg-of-defun)
1088 (if (re-search-forward pascal-str (pascal-get-end-of-line) t)
1089 (progn (setq match (buffer-substring (match-beginning 2)
1090 (match-end 2)))
1091 (if (or (null pascal-pred)
1092 (funcall pascal-pred match))
1093 (setq pascal-all (cons match pascal-all)))))
1094 (goto-char (match-beginning 0)))))
1095
1096 (defun pascal-get-completion-decl ()
1097 ;; Macro for searching through current declaration (var, type or const)
1098 ;; for matches of `str' and adding the occurrence tp `all'
1099 (let ((end (save-excursion (pascal-declaration-end)
1100 (point)))
1101 match)
1102 ;; Traverse lines
1103 (while (< (point) end)
1104 (if (re-search-forward "[:=]" (pascal-get-end-of-line) t)
1105 ;; Traverse current line
1106 (while (and (re-search-backward
1107 (concat "\\((\\|\\<\\(var\\|type\\|const\\)\\>\\)\\|"
1108 pascal-symbol-re)
1109 (pascal-get-beg-of-line) t)
1110 (not (match-end 1)))
1111 (setq match (buffer-substring (match-beginning 0) (match-end 0)))
1112 (if (string-match (concat "\\<" pascal-str) match)
1113 (if (or (null pascal-pred)
1114 (funcall pascal-pred match))
1115 (setq pascal-all (cons match pascal-all))))))
1116 (if (re-search-forward "\\<record\\>" (pascal-get-end-of-line) t)
1117 (pascal-declaration-end)
1118 (forward-line 1)))))
1119
1120 (defun pascal-type-completion ()
1121 "Calculate all possible completions for types."
1122 (let ((start (point))
1123 goon)
1124 ;; Search for all reachable type declarations
1125 (while (or (pascal-beg-of-defun)
1126 (setq goon (not goon)))
1127 (save-excursion
1128 (if (and (< start (prog1 (save-excursion (pascal-end-of-defun)
1129 (point))
1130 (forward-char 1)))
1131 (re-search-forward
1132 "\\<type\\>\\|\\<\\(begin\\|function\\|procedure\\)\\>"
1133 start t)
1134 (not (match-end 1)))
1135 ;; Check current type declaration
1136 (pascal-get-completion-decl))))))
1137
1138 (defun pascal-var-completion ()
1139 "Calculate all possible completions for variables (or constants)."
1140 (let ((start (point))
1141 goon twice)
1142 ;; Search for all reachable var declarations
1143 (while (or (pascal-beg-of-defun)
1144 (setq goon (not goon)))
1145 (save-excursion
1146 (if (> start (prog1 (save-excursion (pascal-end-of-defun)
1147 (point))))
1148 () ; Declarations not reachable
1149 (if (search-forward "(" (pascal-get-end-of-line) t)
1150 ;; Check parameterlist
1151 (pascal-get-completion-decl))
1152 (setq twice 2)
1153 (while (>= (setq twice (1- twice)) 0)
1154 (cond ((and (re-search-forward
1155 (concat "\\<\\(var\\|const\\)\\>\\|"
1156 "\\<\\(begin\\|function\\|procedure\\)\\>")
1157 start t)
1158 (not (match-end 2)))
1159 ;; Check var/const declarations
1160 (pascal-get-completion-decl))
1161 ((match-end 2)
1162 (setq twice 0)))))))))
1163
1164
1165 (defun pascal-keyword-completion (keyword-list)
1166 "Give list of all possible completions of keywords in KEYWORD-LIST."
1167 (mapcar '(lambda (s)
1168 (if (string-match (concat "\\<" pascal-str) s)
1169 (if (or (null pascal-pred)
1170 (funcall pascal-pred s))
1171 (setq pascal-all (cons s pascal-all)))))
1172 keyword-list))
1173
1174 ;; Function passed to completing-read, try-completion or
1175 ;; all-completions to get completion on STR. If predicate is non-nil,
1176 ;; it must be a function to be called for every match to check if this
1177 ;; should really be a match. If flag is t, the function returns a list
1178 ;; of all possible completions. If it is nil it returns a string, the
1179 ;; longest possible completion, or t if STR is an exact match. If flag
1180 ;; is 'lambda, the function returns t if STR is an exact match, nil
1181 ;; otherwise.
1182
1183 (defun pascal-completion (pascal-str pascal-pred pascal-flag)
1184 (save-excursion
1185 (let ((pascal-all nil))
1186 ;; Set buffer to use for searching labels. This should be set
1187 ;; within functions which use pascal-completions
1188 (set-buffer pascal-buffer-to-use)
1189
1190 ;; Determine what should be completed
1191 (let ((state (car (pascal-calculate-indent))))
1192 (cond (;--Within a declaration or parameterlist
1193 (or (eq state 'declaration) (eq state 'paramlist)
1194 (and (eq state 'defun)
1195 (save-excursion
1196 (re-search-backward ")[ \t]*:"
1197 (pascal-get-beg-of-line) t))))
1198 (if (or (eq state 'paramlist) (eq state 'defun))
1199 (pascal-beg-of-defun))
1200 (pascal-type-completion)
1201 (pascal-keyword-completion pascal-type-keywords))
1202 (;--Starting a new statement
1203 (and (not (eq state 'contexp))
1204 (save-excursion
1205 (skip-chars-backward "a-zA-Z0-9_.")
1206 (backward-sexp 1)
1207 (or (looking-at pascal-nosemi-re)
1208 (progn
1209 (forward-sexp 1)
1210 (looking-at "\\s *\\(;\\|:[^=]\\)")))))
1211 (save-excursion (pascal-var-completion))
1212 (pascal-func-completion 'procedure)
1213 (pascal-keyword-completion pascal-start-keywords))
1214 (t;--Anywhere else
1215 (save-excursion (pascal-var-completion))
1216 (pascal-func-completion 'function)
1217 (pascal-keyword-completion pascal-separator-keywords))))
1218
1219 ;; Now we have built a list of all matches. Give response to caller
1220 (pascal-completion-response))))
1221
1222 (defun pascal-completion-response ()
1223 (cond ((or (equal pascal-flag 'lambda) (null pascal-flag))
1224 ;; This was not called by all-completions
1225 (if (null pascal-all)
1226 ;; Return nil if there was no matching label
1227 nil
1228 ;; Get longest string common in the labels
1229 (let* ((elm (cdr pascal-all))
1230 (match (car pascal-all))
1231 (min (length match))
1232 exact tmp)
1233 (if (string= match pascal-str)
1234 ;; Return t if first match was an exact match
1235 (setq match t)
1236 (while (not (null elm))
1237 ;; Find longest common string
1238 (if (< (setq tmp (pascal-string-diff match (car elm))) min)
1239 (progn
1240 (setq min tmp)
1241 (setq match (substring match 0 min))))
1242 ;; Terminate with match=t if this is an exact match
1243 (if (string= (car elm) pascal-str)
1244 (progn
1245 (setq match t)
1246 (setq elm nil))
1247 (setq elm (cdr elm)))))
1248 ;; If this is a test just for exact match, return nil ot t
1249 (if (and (equal pascal-flag 'lambda) (not (equal match 't)))
1250 nil
1251 match))))
1252 ;; If flag is t, this was called by all-completions. Return
1253 ;; list of all possible completions
1254 (pascal-flag
1255 pascal-all)))
1256
1257 (defvar pascal-last-word-numb 0)
1258 (defvar pascal-last-word-shown nil)
1259 (defvar pascal-last-completions nil)
1260
1261 (defun pascal-complete-word ()
1262 "Complete word at current point.
1263 \(See also `pascal-toggle-completions', `pascal-type-keywords',
1264 `pascal-start-keywords' and `pascal-separator-keywords'.)"
1265 (interactive)
1266 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
1267 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
1268 (pascal-str (buffer-substring b e))
1269 ;; The following variable is used in pascal-completion
1270 (pascal-buffer-to-use (current-buffer))
1271 (allcomp (if (and pascal-toggle-completions
1272 (string= pascal-last-word-shown pascal-str))
1273 pascal-last-completions
1274 (all-completions pascal-str 'pascal-completion)))
1275 (match (if pascal-toggle-completions
1276 "" (try-completion
1277 pascal-str (mapcar '(lambda (elm)
1278 (cons elm 0)) allcomp)))))
1279 ;; Delete old string
1280 (delete-region b e)
1281
1282 ;; Toggle-completions inserts whole labels
1283 (if pascal-toggle-completions
1284 (progn
1285 ;; Update entry number in list
1286 (setq pascal-last-completions allcomp
1287 pascal-last-word-numb
1288 (if (>= pascal-last-word-numb (1- (length allcomp)))
1289 0
1290 (1+ pascal-last-word-numb)))
1291 (setq pascal-last-word-shown (elt allcomp pascal-last-word-numb))
1292 ;; Display next match or same string if no match was found
1293 (if (not (null allcomp))
1294 (insert "" pascal-last-word-shown)
1295 (insert "" pascal-str)
1296 (message "(No match)")))
1297 ;; The other form of completion does not necessarily do that.
1298
1299 ;; Insert match if found, or the original string if no match
1300 (if (or (null match) (equal match 't))
1301 (progn (insert "" pascal-str)
1302 (message "(No match)"))
1303 (insert "" match))
1304 ;; Give message about current status of completion
1305 (cond ((equal match 't)
1306 (if (not (null (cdr allcomp)))
1307 (message "(Complete but not unique)")
1308 (message "(Sole completion)")))
1309 ;; Display buffer if the current completion didn't help
1310 ;; on completing the label.
1311 ((and (not (null (cdr allcomp))) (= (length pascal-str)
1312 (length match)))
1313 (with-output-to-temp-buffer "*Completions*"
1314 (display-completion-list allcomp))
1315 ;; Wait for a keypress. Then delete *Completion* window
1316 (momentary-string-display "" (point))
1317 (delete-window (get-buffer-window (get-buffer "*Completions*")))
1318 )))))
1319
1320 (defun pascal-show-completions ()
1321 "Show all possible completions at current point."
1322 (interactive)
1323 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
1324 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
1325 (pascal-str (buffer-substring b e))
1326 ;; The following variable is used in pascal-completion
1327 (pascal-buffer-to-use (current-buffer))
1328 (allcomp (if (and pascal-toggle-completions
1329 (string= pascal-last-word-shown pascal-str))
1330 pascal-last-completions
1331 (all-completions pascal-str 'pascal-completion))))
1332 ;; Show possible completions in a temporary buffer.
1333 (with-output-to-temp-buffer "*Completions*"
1334 (display-completion-list allcomp))
1335 ;; Wait for a keypress. Then delete *Completion* window
1336 (momentary-string-display "" (point))
1337 (delete-window (get-buffer-window (get-buffer "*Completions*")))))
1338
1339
1340 (defun pascal-get-default-symbol ()
1341 "Return symbol around current point as a string."
1342 (save-excursion
1343 (buffer-substring (progn
1344 (skip-chars-backward " \t")
1345 (skip-chars-backward "a-zA-Z0-9_")
1346 (point))
1347 (progn
1348 (skip-chars-forward "a-zA-Z0-9_")
1349 (point)))))
1350
1351 (defun pascal-build-defun-re (str &optional arg)
1352 "Return function/procedure starting with STR as regular expression.
1353 With optional second arg non-nil, STR is the complete name of the instruction."
1354 (if arg
1355 (concat "^\\(function\\|procedure\\)[ \t]+\\(" str "\\)\\>")
1356 (concat "^\\(function\\|procedure\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))
1357
1358 ;; Function passed to completing-read, try-completion or
1359 ;; all-completions to get completion on any function name. If
1360 ;; predicate is non-nil, it must be a function to be called for every
1361 ;; match to check if this should really be a match. If flag is t, the
1362 ;; function returns a list of all possible completions. If it is nil
1363 ;; it returns a string, the longest possible completion, or t if STR
1364 ;; is an exact match. If flag is 'lambda, the function returns t if
1365 ;; STR is an exact match, nil otherwise.
1366
1367 (defun pascal-comp-defun (pascal-str pascal-pred pascal-flag)
1368 (save-excursion
1369 (let ((pascal-all nil)
1370 match)
1371
1372 ;; Set buffer to use for searching labels. This should be set
1373 ;; within functions which use pascal-completions
1374 (set-buffer pascal-buffer-to-use)
1375
1376 (let ((pascal-str pascal-str))
1377 ;; Build regular expression for functions
1378 (if (string= pascal-str "")
1379 (setq pascal-str (pascal-build-defun-re "[a-zA-Z_]"))
1380 (setq pascal-str (pascal-build-defun-re pascal-str)))
1381 (goto-char (point-min))
1382
1383 ;; Build a list of all possible completions
1384 (while (re-search-forward pascal-str nil t)
1385 (setq match (buffer-substring (match-beginning 2) (match-end 2)))
1386 (if (or (null pascal-pred)
1387 (funcall pascal-pred match))
1388 (setq pascal-all (cons match pascal-all)))))
1389
1390 ;; Now we have built a list of all matches. Give response to caller
1391 (pascal-completion-response))))
1392
1393 (defun pascal-goto-defun ()
1394 "Move to specified Pascal function/procedure.
1395 The default is a name found in the buffer around point."
1396 (interactive)
1397 (let* ((default (pascal-get-default-symbol))
1398 ;; The following variable is used in pascal-comp-function
1399 (pascal-buffer-to-use (current-buffer))
1400 (default (if (pascal-comp-defun default nil 'lambda)
1401 default ""))
1402 (label (if (not (string= default ""))
1403 ;; Do completion with default
1404 (completing-read (concat "Label: (default " default ") ")
1405 'pascal-comp-defun nil t "")
1406 ;; There is no default value. Complete without it
1407 (completing-read "Label: "
1408 'pascal-comp-defun nil t ""))))
1409 ;; If there was no response on prompt, use default value
1410 (if (string= label "")
1411 (setq label default))
1412 ;; Goto right place in buffer if label is not an empty string
1413 (or (string= label "")
1414 (progn
1415 (goto-char (point-min))
1416 (re-search-forward (pascal-build-defun-re label t))
1417 (beginning-of-line)))))
1418
1419 \f
1420
1421 ;;;
1422 ;;; Pascal-outline-mode
1423 ;;;
1424 (defvar pascal-outline-map nil "Keymap used in Pascal Outline mode.")
1425
1426 (if pascal-outline-map
1427 nil
1428 (if (boundp 'set-keymap-name)
1429 (set-keymap-name pascal-outline-map 'pascal-outline-map))
1430 (if (not (boundp 'set-keymap-parent))
1431 (setq pascal-outline-map (copy-keymap pascal-mode-map))
1432 (setq pascal-outline-map (make-sparse-keymap))
1433 (set-keymap-parent pascal-outline-map pascal-mode-map))
1434 (define-key pascal-outline-map "\M-\C-a" 'pascal-outline-prev-defun)
1435 (define-key pascal-outline-map "\M-\C-e" 'pascal-outline-next-defun)
1436 (define-key pascal-outline-map "\C-c\C-d" 'pascal-outline-goto-defun)
1437 (define-key pascal-outline-map "\C-c\C-s" 'pascal-show-all)
1438 (define-key pascal-outline-map "\C-c\C-h" 'pascal-hide-other-defuns))
1439
1440 (defvar pascal-outline-mode nil "Non-nil while using Pascal Outline mode.")
1441 (make-variable-buffer-local 'pascal-outline-mode)
1442 (set-default 'pascal-outline-mode nil)
1443 (if (not (assoc 'pascal-outline-mode minor-mode-alist))
1444 (setq minor-mode-alist (append minor-mode-alist
1445 (list '(pascal-outline-mode " Outl")))))
1446
1447 (defun pascal-outline (&optional arg)
1448 "Outline-line minor mode for Pascal mode.
1449 When in Pascal Outline mode, portions
1450 of the text being edited may be made invisible. \\<pascal-outline-map>
1451
1452 Pascal Outline mode provides some additional commands.
1453
1454 \\[pascal-outline-prev-defun]\
1455 \t- Move to previous function/procedure, hiding everything else.
1456 \\[pascal-outline-next-defun]\
1457 \t- Move to next function/procedure, hiding everything else.
1458 \\[pascal-outline-goto-defun]\
1459 \t- Goto function/procedure prompted for in minibuffer,
1460 \t hide all other functions.
1461 \\[pascal-show-all]\t- Show the whole buffer.
1462 \\[pascal-hide-other-defuns]\
1463 \t- Hide everything but the current function (function under the cursor).
1464 \\[pascal-outline]\t- Leave pascal-outline-mode."
1465 (interactive "P")
1466 (setq pascal-outline-mode
1467 (if (null arg) (not pascal-outline-mode) t))
1468 (if (boundp 'redraw-mode-line)
1469 (redraw-mode-line))
1470 (if pascal-outline-mode
1471 (progn
1472 (setq selective-display t)
1473 (use-local-map pascal-outline-map))
1474 (progn
1475 (setq selective-display nil)
1476 (pascal-show-all)
1477 (use-local-map pascal-mode-map))))
1478
1479 (defun pascal-outline-change (b e pascal-flag)
1480 (let ((modp (buffer-modified-p)))
1481 (unwind-protect
1482 (subst-char-in-region b e (if (= pascal-flag ?\n)
1483 ?\^M ?\n) pascal-flag)
1484 (set-buffer-modified-p modp))))
1485
1486 (defun pascal-show-all ()
1487 "Show all of the text in the buffer."
1488 (interactive)
1489 (pascal-outline-change (point-min) (point-max) ?\n))
1490
1491 (defun pascal-hide-other-defuns ()
1492 "Show only the current defun."
1493 (interactive)
1494 (save-excursion
1495 (let ((beg (progn (if (not (looking-at "\\(function\\|procedure\\)\\>"))
1496 (pascal-beg-of-defun))
1497 (point)))
1498 (end (progn (pascal-end-of-defun)
1499 (backward-sexp 1)
1500 (search-forward "\n\\|\^M" nil t)
1501 (point)))
1502 (opoint (point-min)))
1503 (goto-char (point-min))
1504
1505 ;; Hide all functions before current function
1506 (while (re-search-forward "^\\(function\\|procedure\\)\\>" beg 'move)
1507 (pascal-outline-change opoint (1- (match-beginning 0)) ?\^M)
1508 (setq opoint (point))
1509 ;; Functions may be nested
1510 (if (> (progn (pascal-end-of-defun) (point)) beg)
1511 (goto-char opoint)))
1512 (if (> beg opoint)
1513 (pascal-outline-change opoint (1- beg) ?\^M))
1514
1515 ;; Show current function
1516 (pascal-outline-change beg end ?\n)
1517 ;; Hide nested functions
1518 (forward-char 1)
1519 (while (re-search-forward "^\\(function\\|procedure\\)\\>" end 'move)
1520 (setq opoint (point))
1521 (pascal-end-of-defun)
1522 (pascal-outline-change opoint (point) ?\^M))
1523
1524 (goto-char end)
1525 (setq opoint end)
1526
1527 ;; Hide all function after current function
1528 (while (re-search-forward "^\\(function\\|procedure\\)\\>" nil 'move)
1529 (pascal-outline-change opoint (1- (match-beginning 0)) ?\^M)
1530 (setq opoint (point))
1531 (pascal-end-of-defun))
1532 (pascal-outline-change opoint (point-max) ?\^M)
1533
1534 ;; Hide main program
1535 (if (< (progn (forward-line -1) (point)) end)
1536 (progn
1537 (goto-char beg)
1538 (pascal-end-of-defun)
1539 (backward-sexp 1)
1540 (pascal-outline-change (point) (point-max) ?\^M))))))
1541
1542 (defun pascal-outline-next-defun ()
1543 "Move to next function/procedure, hiding all others."
1544 (interactive)
1545 (pascal-end-of-defun)
1546 (pascal-hide-other-defuns))
1547
1548 (defun pascal-outline-prev-defun ()
1549 "Move to previous function/procedure, hiding all others."
1550 (interactive)
1551 (pascal-beg-of-defun)
1552 (pascal-hide-other-defuns))
1553
1554 (defun pascal-outline-goto-defun ()
1555 "Move to specified function/procedure, hiding all others."
1556 (interactive)
1557 (pascal-goto-defun)
1558 (pascal-hide-other-defuns))
1559
1560 ;;; pascal.el ends here