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