1 ;;; pascal.el --- major mode for editing pascal source in Emacs
3 ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Espen Skoglund (espensk@stud.cs.uit.no)
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; 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.
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>
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):
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"))
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.
63 (defvar pascal-mode-abbrev-table nil
64 "Abbrev table in use in Pascal-mode buffers.")
65 (define-abbrev-table 'pascal-mode-abbrev-table
())
67 (defvar pascal-mode-map
()
68 "Keymap used in Pascal mode.")
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)
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'.")
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"
112 ;;; Regular expressions used to calculate indent, etc.
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\\)\\>")
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 -----/\\-----}")
129 (defvar pascal-mode-syntax-table nil
130 "Syntax table in use in Pascal-mode buffers.")
132 (if pascal-mode-syntax-table
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
))
152 (defvar pascal-font-lock-keywords
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")
166 "and\\|begin\\|case\\|do\\|e\\(lse\\|nd\\)\\|for\\|i[fn]\\|"
167 "not\\|o[fr]\\|repeat\\|t\\(hen\\|o\\)\\|until\\|w\\(hile\\|ith\\)"
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.")
173 (defvar pascal-indent-level
3
174 "*Indentation of Pascal statements with respect to containing block.")
176 (defvar pascal-case-indent
2
177 "*Indentation for case statements.")
179 (defvar pascal-auto-newline nil
180 "*Non-nil means automatically newline after semicolons and the punctuation
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.")
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.")
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.")
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.")
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.")
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.")
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.")
230 (defsubst pascal-get-beg-of-line
(&optional arg
)
232 (beginning-of-line arg
)
235 (defsubst pascal-get-end-of-line
(&optional arg
)
240 (defun pascal-declaration-end ()
242 (while (and (> nest
0)
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))))))
251 (defun pascal-declaration-beg ()
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
)))))
261 (defsubst pascal-within-string
()
263 (nth 3 (parse-partial-sexp (pascal-get-beg-of-line) (point)))))
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.
271 \\[pascal-complete-word] completes the word around current point with respect \
273 \\[pascal-show-completions] shows all possible completions at this point.
275 Other useful functions are:
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).
288 Variables controlling indentation/edit style:
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
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.
306 See also the user variables pascal-type-keywords, pascal-start-keywords and
307 pascal-separator-keywords.
309 Turning on Pascal mode calls the value of the variable pascal-mode-hook with
310 no args, if that value is non-nil."
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
"}")
333 (make-local-variable 'font-lock-defaults
)
334 (setq font-lock-defaults
'(pascal-font-lock-keywords nil t
))
336 (make-local-variable 'imenu-generic-expression
)
337 (setq imenu-generic-expression pascal-imenu-generic-expression
)
338 (run-hooks 'pascal-mode-hook
))
343 ;;; Electric functions
345 (defun electric-pascal-terminate-line ()
346 "Terminate line and indent next line."
348 ;; First, check if current line should be indented
351 (skip-chars-forward " \t")
352 (if (looking-at pascal-autoindent-lines-re
)
353 (pascal-indent-line)))
354 (delete-horizontal-space) ; Removes trailing whitespaces
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
365 (skip-chars-forward " \t")
366 (cond ((looking-at "\\*[ \t]+)")
367 ;; Delete region between `*' and `)' if there is only whitespaces.
369 (delete-horizontal-space))
370 ((and (looking-at "(\\*\\|\\*[^)]")
372 (search-forward "*)" (pascal-get-end-of-line) t
))))
374 ;; If last line was a star comment line then this one shall be too.
380 (defun electric-pascal-semi-or-dot ()
381 "Insert `;' or `.' character and reindent the line."
383 (insert last-command-char
)
386 (pascal-indent-line))
387 (if pascal-auto-newline
388 (electric-pascal-terminate-line)))
390 (defun electric-pascal-colon ()
391 "Insert `:' and do all indentions except line indent on this line."
393 (insert last-command-char
)
394 ;; Do nothing if within string.
395 (if (pascal-within-string)
399 (pascal-indent-line))
400 (let ((pascal-tab-always-indent nil
))
401 (pascal-indent-command))))
403 (defun electric-pascal-equal ()
404 "Insert `=', and do indention if within type declaration."
406 (insert last-command-char
)
407 (if (eq (car (pascal-calculate-indent)) 'declaration
)
408 (let ((pascal-tab-always-indent nil
))
409 (pascal-indent-command))))
411 (defun electric-pascal-hash ()
412 "Insert `#', and indent to column 0 if this is a CPP directive."
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))))
419 (defun electric-pascal-tab ()
420 "Function called when TAB is pressed in Pascal mode."
422 ;; Do nothing if within a string or in a CPP directive.
423 (if (or (pascal-within-string)
425 (save-excursion (beginning-of-line) (eq (following-char) ?
#))))
427 ;; If pascal-tab-always-indent, indent the beginning of the line.
428 (if pascal-tab-always-indent
431 (pascal-indent-line))
433 (skip-chars-backward " \t")
437 (pascal-indent-command)))
442 ;;; Interactive functions
444 (defun pascal-insert-block ()
445 "Insert Pascal begin ... end; block in the code with right indentation."
449 (electric-pascal-terminate-line)
451 (electric-pascal-terminate-line)
454 (pascal-indent-line)))
456 (defun pascal-star-comment ()
457 "Insert Pascal star comment at point."
461 (electric-pascal-terminate-line)
463 (electric-pascal-terminate-line)
464 (delete-horizontal-space)
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."
473 (pascal-end-of-defun)
475 (pascal-beg-of-defun)
476 (if (fboundp 'zmacs-activate-region
)
477 (zmacs-activate-region)))
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.
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."
491 ;; Insert start and endcomments
493 (if (and (save-excursion (skip-chars-forward " \t") (eolp))
494 (not (save-excursion (skip-chars-backward " \t") (bolp))))
497 (insert pascal-exclude-str-end
)
502 (insert pascal-exclude-str-start
)
504 ;; Replace end-comments within commented area
507 (while (re-search-backward "\\*)" start t
)
508 (replace-match "!(*" t t
)))
510 (while (re-search-backward "}" start t
)
511 (replace-match "!{" t t
)))))
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'."
519 (let ((start (point))
521 ;; Find the boundaries of the comment
523 (setq start
(progn (search-backward pascal-exclude-str-start nil t
)
525 (setq end
(progn (search-forward pascal-exclude-str-end nil t
)
527 ;; Check if we're really inside a comment
528 (if (or (equal start
(point)) (<= end
(point)))
529 (message "Not standing within commented area.")
536 (delete-region pos
(1+ (point))))
537 ;; Change comments back to normal
539 (while (re-search-backward "!{" start t
)
540 (replace-match "}" t t
)))
542 (while (re-search-backward "!(\\*" start t
)
543 (replace-match "*)" t t
)))
544 ;; Remove startcomment
549 (delete-region pos
(1+ (point)))))))))
551 (defun pascal-beg-of-defun ()
552 "Move backward to the beginning of the current function or procedure."
555 (if (not (looking-at (concat "\\s \\|\\s)\\|" pascal-defun-re
)))
557 (let ((nest 0) (max -
1) (func 0)
558 (reg (concat pascal-beg-block-re
"\\|"
559 pascal-end-block-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
566 ((match-end 1) ; begin|case|record|repeat
567 (if (and (looking-at "\\<record\\>") (>= max
0))
568 (setq func
(1- func
)))
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
578 (setq func
(1- func
)))))))
581 (defun pascal-end-of-defun ()
582 "Move forward to the end of the current function or procedure."
584 (if (looking-at "\\s ")
586 (if (not (looking-at pascal-defun-re
))
587 (pascal-beg-of-defun))
589 (let ((nest 0) (func 1)
590 (reg (concat pascal-beg-block-re
"\\|"
591 pascal-end-block-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
600 (setq nest
(1+ nest
))
602 (goto-char (match-beginning 0))
603 (looking-at "\\<record\\>"))
604 (setq func
(1+ func
))))
606 (setq nest
(1- nest
))
608 (setq func
(1- func
))))
610 (setq func
(1+ func
))))))
613 (defun pascal-end-of-statement ()
614 "Move forward to end of current statement."
616 (let ((parse-sexp-ignore-comments t
)
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
626 (cond ((looking-at "[ \t]*;")
627 (skip-chars-forward "^;")
629 (throw 'found
(point)))
632 (looking-at pascal-beg-block-re
))
633 (goto-char (match-beginning 0))
636 (throw 'found
(point))))))))
638 ;; Skip a whole block
641 (re-search-forward regexp nil
'move
)
642 (setq nest
(if (match-end 1)
646 (throw 'found
(point)))
648 (throw 'found
(pascal-end-of-statement))))))
651 (defun pascal-downcase-keywords ()
652 "Downcase all Pascal keywords in the buffer."
654 (pascal-change-keywords 'downcase-word
))
656 (defun pascal-upcase-keywords ()
657 "Upcase all Pascal keywords in the buffer."
659 (pascal-change-keywords 'upcase-word
))
661 (defun pascal-capitalize-keywords ()
662 "Capitalize all Pascal keywords in the buffer."
664 (pascal-change-keywords 'capitalize-word
))
666 ;; Change the keywords according to argument.
667 (defun pascal-change-keywords (change-word)
669 (let ((keyword-re (concat "\\<\\("
670 (mapconcat 'identity pascal-keywords
"\\|")
672 (goto-char (point-min))
673 (while (re-search-forward keyword-re nil t
)
674 (funcall change-word -
1)))))
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."
688 (skip-chars-forward " \t")
689 (if (and (looking-at "\\<end;")
692 (search-backward "{" (pascal-get-beg-of-line) t
))))
693 (let ((type (car (pascal-calculate-indent))))
694 (if (eq type
'declaration
)
697 ;; This is a case block
700 (delete-horizontal-space)
701 (insert " { case }"))
703 ;; Check if this is the end of a function
705 (while (not (or (looking-at pascal-defun-re
) (bobp)))
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
)))))
716 (delete-horizontal-space)
720 (setq b
(progn (pascal-beg-of-defun)
721 (skip-chars-forward "^ \t")
722 (skip-chars-forward " \t")
724 e
(progn (skip-chars-forward "a-zA-Z0-9_")
726 (insert-buffer-substring (current-buffer) b e
))
727 (insert " }"))))))))))
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)))
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"))))
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
))
776 (looking-at pascal-defun-re
))
778 ;; Other things should have no extra indent
779 (if (looking-at pascal-noindent-re
)
781 ;; But most lines are treated this way:
782 (indent-to (eval (cdr (assoc type pascal-indent-alist
))))
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)."
789 (let* ((parse-sexp-ignore-comments t
)
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
))
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
807 (cond (;--Escape from case statements
808 (and (looking-at "[A-Za-z0-9]+[ \t]*:[^=]")
810 (save-excursion (skip-chars-backward " \t")
813 (end-of-line) (backward-sexp) (point))
815 (> (save-excursion (goto-char oldpos
)
819 (throw 'nesting
'caseblock
))
820 (;--Nest block outwards
821 (looking-at pascal-beg-block-re
)
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
))
836 (;--Defun (or parameter list)
837 (looking-at pascal-defun-re
)
839 (throw 'nesting
'defun
)
842 (while (re-search-forward
843 "\\(\\<record\\>\\)\\|\\<end\\>"
846 (setq n
(1+ n
)) (setq n
(1- n
))))
848 (throw 'nesting
'declaration
)
849 (throw 'nesting
'paramlist
)))))
851 (looking-at pascal-declaration-re
)
855 (looking-at "^[ \t]*$"))
856 (throw 'nesting
'unknown
)
857 (throw 'nesting
'declaration
)))
858 (;--If, else or while statement
860 (looking-at pascal-sub-block-re
))
861 (throw 'nesting
'block
))
862 (;--Found complete statement
863 (save-excursion (forward-sexp 1)
864 (= (following-char) ?\
;))
866 (;--No known statements
868 (throw 'nesting
'unknown
))
871 ;; Return type of block and indent level.
872 (if (> par
0) ; Unclosed Parenthesis
874 (list type
(pascal-indent-level))))))
876 (defun pascal-indent-level ()
877 "Return the indent-level the current statement has.
878 Do not count labels, case-statements or records."
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")
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)))))
896 (delete-horizontal-space)
899 (defun pascal-indent-case ()
900 "Indent within case statements."
901 (let ((savepos (point-marker))
905 (re-search-backward "\\<case\\>" nil t
)))
909 (while (< (point) (marker-position end
))
910 (if (re-search-forward
911 "^[ \t]*[^ \t,:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
912 (marker-position end
) 'move
)
914 (if (< (point) (marker-position end
))
916 (delete-horizontal-space)
917 (if (> (current-column) ind
)
918 (setq ind
(current-column)))
919 (pascal-end-of-statement))))
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
)
929 (if (/= (following-char) ?
:)
932 (delete-horizontal-space)
934 (setq oldpos
(point))
935 (pascal-end-of-statement))
936 (goto-char savepos
)))
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."
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
)
949 (usevar (re-search-backward "\\<var\\>" stpos t
)))
951 ;; If arg, just return indent
954 (if (or (not usevar
) (looking-at "[ \t]*var\\>"))
958 (delete-horizontal-space)
959 (if (and usevar
(not (looking-at "var\\>")))
960 (indent-to (+ 4 stcol
)))
961 (pascal-indent-declaration nil stpos edpos
)))))
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)))
968 (let ((lineup (if (or (looking-at "\\<var\\>\\|\\<record\\>") arg start
)
970 (stpos (if start start
971 (forward-word 2) (backward-word 1) (point)))
972 (edpos (set-marker (make-marker)
974 (max (progn (pascal-declaration-end)
980 ;; Indent lines in record block
982 (while (<= (point) (marker-position edpos
))
984 (delete-horizontal-space)
985 (if (looking-at "end\\>")
987 (indent-to (+ arg pascal-indent-level
)))
991 (setq ind
(pascal-get-lineup-indent stpos edpos lineup
))
993 (while (and (<= (point) (marker-position edpos
))
995 (if (search-forward lineup
(pascal-get-end-of-line) 'move
)
997 (delete-horizontal-space)
999 (if (not (looking-at lineup
))
1000 (forward-line 1) ; No more indent if there is no : or =
1002 (delete-horizontal-space)
1004 ;; Indent record block
1005 (if (looking-at "record\\>")
1006 (pascal-indent-declaration (current-column)))
1007 (forward-line 1)))))
1009 ;; If arg - move point
1010 (if arg
(forward-line -
1)
1011 (goto-char (marker-position pos
)))))
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
)
1018 (reg (concat str
"\\|\\(\\<record\\>\\)"))
1021 ;; Get rightmost position
1022 (while (< (point) e
)
1024 (if (re-search-forward reg
(min e
(pascal-get-end-of-line 2)) 'move
)
1026 ;; Skip record blocks
1027 (if (match-beginning 1)
1028 (pascal-declaration-end)
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
1038 ;; No lineup-string found
1041 (skip-chars-backward " \t")
1042 (1+ (current-column))))))
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
)
1055 (defun pascal-string-diff (str1 str2
)
1056 "Return index of first letter where STR1 and STR2 differs."
1060 (if (or (> (1+ diff
) (length str1
))
1061 (> (1+ diff
) (length str2
)))
1063 (or (equal (aref str1 diff
) (aref str2 diff
))
1065 (setq diff
(1+ diff
))))))
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.
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_.]*\\)\\>"))
1082 (if (not (looking-at "\\<\\(function\\|procedure\\)\\>"))
1083 (re-search-backward "\\<\\(function\\|procedure\\)\\>" nil t
))
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)
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)))))
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)
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\\)\\>\\)\\|"
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)))))
1120 (defun pascal-type-completion ()
1121 "Calculate all possible completions for types."
1122 (let ((start (point))
1124 ;; Search for all reachable type declarations
1125 (while (or (pascal-beg-of-defun)
1126 (setq goon
(not goon
)))
1128 (if (and (< start
(prog1 (save-excursion (pascal-end-of-defun)
1132 "\\<type\\>\\|\\<\\(begin\\|function\\|procedure\\)\\>"
1134 (not (match-end 1)))
1135 ;; Check current type declaration
1136 (pascal-get-completion-decl))))))
1138 (defun pascal-var-completion ()
1139 "Calculate all possible completions for variables (or constants)."
1140 (let ((start (point))
1142 ;; Search for all reachable var declarations
1143 (while (or (pascal-beg-of-defun)
1144 (setq goon
(not goon
)))
1146 (if (> start
(prog1 (save-excursion (pascal-end-of-defun)
1148 () ; Declarations not reachable
1149 (if (search-forward "(" (pascal-get-end-of-line) t
)
1150 ;; Check parameterlist
1151 (pascal-get-completion-decl))
1153 (while (>= (setq twice
(1- twice
)) 0)
1154 (cond ((and (re-search-forward
1155 (concat "\\<\\(var\\|const\\)\\>\\|"
1156 "\\<\\(begin\\|function\\|procedure\\)\\>")
1158 (not (match-end 2)))
1159 ;; Check var/const declarations
1160 (pascal-get-completion-decl))
1162 (setq twice
0)))))))))
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
)))))
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
1183 (defun pascal-completion (pascal-str pascal-pred pascal-flag
)
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
)
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
)
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
))
1205 (skip-chars-backward "a-zA-Z0-9_.")
1207 (or (looking-at pascal-nosemi-re
)
1210 (looking-at "\\s *\\(;\\|:[^=]\\)")))))
1211 (save-excursion (pascal-var-completion))
1212 (pascal-func-completion 'procedure
)
1213 (pascal-keyword-completion pascal-start-keywords
))
1215 (save-excursion (pascal-var-completion))
1216 (pascal-func-completion 'function
)
1217 (pascal-keyword-completion pascal-separator-keywords
))))
1219 ;; Now we have built a list of all matches. Give response to caller
1220 (pascal-completion-response))))
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
1228 ;; Get longest string common in the labels
1229 (let* ((elm (cdr pascal-all
))
1230 (match (car pascal-all
))
1231 (min (length match
))
1233 (if (string= match pascal-str
)
1234 ;; Return t if first match was an exact match
1236 (while (not (null elm
))
1237 ;; Find longest common string
1238 (if (< (setq tmp
(pascal-string-diff match
(car elm
))) min
)
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
)
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
)))
1252 ;; If flag is t, this was called by all-completions. Return
1253 ;; list of all possible completions
1257 (defvar pascal-last-word-numb
0)
1258 (defvar pascal-last-word-shown nil
)
1259 (defvar pascal-last-completions nil
)
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'.)"
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
1277 pascal-str
(mapcar '(lambda (elm)
1278 (cons elm
0)) allcomp
)))))
1279 ;; Delete old string
1282 ;; Toggle-completions inserts whole labels
1283 (if pascal-toggle-completions
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
)))
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.
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)"))
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
)
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*")))
1320 (defun pascal-show-completions ()
1321 "Show all possible completions at current point."
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*")))))
1340 (defun pascal-get-default-symbol ()
1341 "Return symbol around current point as a string."
1343 (buffer-substring (progn
1344 (skip-chars-backward " \t")
1345 (skip-chars-backward "a-zA-Z0-9_")
1348 (skip-chars-forward "a-zA-Z0-9_")
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."
1355 (concat "^\\(function\\|procedure\\)[ \t]+\\(" str
"\\)\\>")
1356 (concat "^\\(function\\|procedure\\)[ \t]+\\(" str
"[a-zA-Z0-9_]*\\)\\>")))
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.
1367 (defun pascal-comp-defun (pascal-str pascal-pred pascal-flag
)
1369 (let ((pascal-all nil
)
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
)
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))
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
)))))
1390 ;; Now we have built a list of all matches. Give response to caller
1391 (pascal-completion-response))))
1393 (defun pascal-goto-defun ()
1394 "Move to specified Pascal function/procedure.
1395 The default is a name found in the buffer around point."
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
)
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
"")
1415 (goto-char (point-min))
1416 (re-search-forward (pascal-build-defun-re label t
))
1417 (beginning-of-line)))))
1422 ;;; Pascal-outline-mode
1424 (defvar pascal-outline-map nil
"Keymap used in Pascal Outline mode.")
1426 (if pascal-outline-map
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
))
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")))))
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>
1452 Pascal Outline mode provides some additional commands.
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."
1466 (setq pascal-outline-mode
1467 (if (null arg
) (not pascal-outline-mode
) t
))
1468 (if (boundp 'redraw-mode-line
)
1470 (if pascal-outline-mode
1472 (setq selective-display t
)
1473 (use-local-map pascal-outline-map
))
1475 (setq selective-display nil
)
1477 (use-local-map pascal-mode-map
))))
1479 (defun pascal-outline-change (b e pascal-flag
)
1480 (let ((modp (buffer-modified-p)))
1482 (subst-char-in-region b e
(if (= pascal-flag ?
\n)
1483 ?\^M ?
\n) pascal-flag
)
1484 (set-buffer-modified-p modp
))))
1486 (defun pascal-show-all ()
1487 "Show all of the text in the buffer."
1489 (pascal-outline-change (point-min) (point-max) ?
\n))
1491 (defun pascal-hide-other-defuns ()
1492 "Show only the current defun."
1495 (let ((beg (progn (if (not (looking-at "\\(function\\|procedure\\)\\>"))
1496 (pascal-beg-of-defun))
1498 (end (progn (pascal-end-of-defun)
1500 (search-forward "\n\\|\^M" nil t
)
1502 (opoint (point-min)))
1503 (goto-char (point-min))
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
)))
1513 (pascal-outline-change opoint
(1- beg
) ?\^M
))
1515 ;; Show current function
1516 (pascal-outline-change beg end ?
\n)
1517 ;; Hide nested functions
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
))
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
)
1534 ;; Hide main program
1535 (if (< (progn (forward-line -
1) (point)) end
)
1538 (pascal-end-of-defun)
1540 (pascal-outline-change (point) (point-max) ?\^M
))))))
1542 (defun pascal-outline-next-defun ()
1543 "Move to next function/procedure, hiding all others."
1545 (pascal-end-of-defun)
1546 (pascal-hide-other-defuns))
1548 (defun pascal-outline-prev-defun ()
1549 "Move to previous function/procedure, hiding all others."
1551 (pascal-beg-of-defun)
1552 (pascal-hide-other-defuns))
1554 (defun pascal-outline-goto-defun ()
1555 "Move to specified function/procedure, hiding all others."
1558 (pascal-hide-other-defuns))
1560 ;;; pascal.el ends here