1 ;;; pascal.el --- major mode for editing pascal source in Emacs -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993-2012 Free Software Foundation, Inc.
5 ;; Author: Espen Skoglund <esk@gnu.org>
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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
28 ;; Emacs should enter Pascal mode when you find a Pascal source file.
29 ;; When you have entered Pascal mode, you may get more info by pressing
30 ;; C-h m. You may also get online help describing various functions by:
31 ;; C-h f <Name of function you want described>
33 ;; If you want to customize Pascal mode to fit you better, you may add
34 ;; these lines (the values of the variables presented here are the defaults):
36 ;; ;; User customization for Pascal mode
37 ;; (setq pascal-indent-level 3
38 ;; pascal-case-indent 2
39 ;; pascal-auto-newline nil
40 ;; pascal-tab-always-indent t
41 ;; pascal-auto-endcomments t
42 ;; pascal-auto-lineup '(all)
43 ;; pascal-type-keywords '("array" "file" "packed" "char"
44 ;; "integer" "real" "string" "record")
45 ;; pascal-start-keywords '("begin" "end" "function" "procedure"
46 ;; "repeat" "until" "while" "read" "readln"
47 ;; "reset" "rewrite" "write" "writeln")
48 ;; pascal-separator-keywords '("downto" "else" "mod" "div" "then"))
50 ;; KNOWN BUGS / BUGREPORTS
51 ;; =======================
52 ;; As far as I know, there are no bugs in the current version of this
53 ;; package. This may not be true however, since I never use this mode
54 ;; myself and therefore would never notice them anyway. If you do
55 ;; find any bugs, you may submit them to: esk@gnu.org as well as to
56 ;; bug-gnu-emacs@gnu.org.
60 (eval-when-compile (require 'cl
))
63 "Major mode for editing Pascal source in Emacs."
64 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
67 (defvar pascal-mode-abbrev-table nil
68 "Abbrev table in use in Pascal-mode buffers.")
69 (define-abbrev-table 'pascal-mode-abbrev-table
())
71 (defvar pascal-mode-map
72 (let ((map (make-sparse-keymap)))
73 (define-key map
";" 'electric-pascal-semi-or-dot
)
74 (define-key map
"." 'electric-pascal-semi-or-dot
)
75 (define-key map
":" 'electric-pascal-colon
)
76 (define-key map
"=" 'electric-pascal-equal
)
77 (define-key map
"#" 'electric-pascal-hash
)
78 ;; These are user preferences, so not to set by default.
79 ;;(define-key map "\r" 'electric-pascal-terminate-line)
80 ;;(define-key map "\t" 'electric-pascal-tab)
81 (define-key map
"\M-\t" 'completion-at-point
)
82 (define-key map
"\M-?" 'completion-help-at-point
)
83 (define-key map
"\177" 'backward-delete-char-untabify
)
84 (define-key map
"\M-\C-h" 'pascal-mark-defun
)
85 (define-key map
"\C-c\C-b" 'pascal-insert-block
)
86 (define-key map
"\M-*" 'pascal-star-comment
)
87 (define-key map
"\C-c\C-c" 'pascal-comment-area
)
88 (define-key map
"\C-c\C-u" 'pascal-uncomment-area
)
89 (define-key map
"\M-\C-a" 'pascal-beg-of-defun
)
90 (define-key map
"\M-\C-e" 'pascal-end-of-defun
)
91 (define-key map
"\C-c\C-d" 'pascal-goto-defun
)
92 (define-key map
"\C-c\C-o" 'pascal-outline-mode
)
93 ;; A command to change the whole buffer won't be used terribly
94 ;; often, so no need for a key binding.
95 ;; (define-key map "\C-cd" 'pascal-downcase-keywords)
96 ;; (define-key map "\C-cu" 'pascal-upcase-keywords)
97 ;; (define-key map "\C-cc" 'pascal-capitalize-keywords)
99 "Keymap used in Pascal mode.")
101 (defvar pascal-imenu-generic-expression
102 '((nil "^[ \t]*\\(function\\|procedure\\)[ \t\n]+\\([a-zA-Z0-9_.:]+\\)" 2))
103 "Imenu expression for Pascal-mode. See `imenu-generic-expression'.")
105 (defvar pascal-keywords
106 '("and" "array" "begin" "case" "const" "div" "do" "downto" "else" "end"
107 "file" "for" "function" "goto" "if" "in" "label" "mod" "nil" "not" "of"
108 "or" "packed" "procedure" "program" "record" "repeat" "set" "then" "to"
109 "type" "until" "var" "while" "with"
110 ;; The following are not standard in pascal, but widely used.
111 "get" "put" "input" "output" "read" "readln" "reset" "rewrite" "write"
115 ;;; Regular expressions used to calculate indent, etc.
117 (defconst pascal-symbol-re
"\\<[a-zA-Z_][a-zA-Z_0-9.]*\\>")
118 (defconst pascal-beg-block-re
"\\<\\(begin\\|case\\|record\\|repeat\\)\\>")
119 (defconst pascal-end-block-re
"\\<\\(end\\|until\\)\\>")
120 (defconst pascal-declaration-re
"\\<\\(const\\|label\\|type\\|var\\)\\>")
121 (defconst pascal-progbeg-re
"\\<\\program\\>")
122 (defconst pascal-defun-re
"\\<\\(function\\|procedure\\|program\\)\\>")
123 (defconst pascal-sub-block-re
"\\<\\(if\\|else\\|for\\|while\\|with\\)\\>")
124 (defconst pascal-noindent-re
"\\<\\(begin\\|end\\|until\\|else\\)\\>")
125 (defconst pascal-nosemi-re
"\\<\\(begin\\|repeat\\|then\\|do\\|else\\)\\>")
126 (defconst pascal-autoindent-lines-re
127 "\\<\\(label\\|var\\|type\\|const\\|until\\|end\\|begin\\|repeat\\|else\\)\\>")
129 ;;; Strings used to mark beginning and end of excluded text
130 (defconst pascal-exclude-str-start
"{-----\\/----- EXCLUDED -----\\/-----")
131 (defconst pascal-exclude-str-end
" -----/\\----- EXCLUDED -----/\\-----}")
133 (defvar pascal-mode-syntax-table
134 (let ((st (make-syntax-table)))
135 (modify-syntax-entry ?
\\ "." st
)
136 (modify-syntax-entry ?\
( "()1" st
)
137 (modify-syntax-entry ?\
) ")(4" st
)
138 ;; This used to use comment-syntax `b'. But the only document I could
139 ;; find about the syntax of Pascal's comments said that (* ... } is
140 ;; a valid comment, just as { ... *) or (* ... *) or { ... }.
141 (modify-syntax-entry ?
* ". 23" st
)
142 (modify-syntax-entry ?
{ "<" st
)
143 (modify-syntax-entry ?
} ">" st
)
144 (modify-syntax-entry ?
+ "." st
)
145 (modify-syntax-entry ?-
"." st
)
146 (modify-syntax-entry ?
= "." st
)
147 (modify-syntax-entry ?%
"." st
)
148 (modify-syntax-entry ?
< "." st
)
149 (modify-syntax-entry ?
> "." st
)
150 (modify-syntax-entry ?
& "." st
)
151 (modify-syntax-entry ?|
"." st
)
152 (modify-syntax-entry ?_
"_" st
)
153 (modify-syntax-entry ?
\' "\"" st
)
155 "Syntax table in use in Pascal-mode buffers.")
159 (defconst pascal-font-lock-keywords
(purecopy
161 '("^[ \t]*\\(function\\|pro\\(cedure\\|gram\\)\\)\\>[ \t]*\\([a-z]\\)"
162 1 font-lock-keyword-face
)
163 '("^[ \t]*\\(function\\|pro\\(cedure\\|gram\\)\\)\\>[ \t]*\\([a-z][a-z0-9_]*\\)"
164 3 font-lock-function-name-face t
)
165 ; ("type" "const" "real" "integer" "char" "boolean" "var"
166 ; "record" "array" "file")
167 (cons (concat "\\<\\(array\\|boolean\\|c\\(har\\|onst\\)\\|file\\|"
168 "integer\\|re\\(al\\|cord\\)\\|type\\|var\\)\\>")
169 'font-lock-type-face
)
170 '("\\<\\(label\\|external\\|forward\\)\\>" . font-lock-constant-face
)
171 '("\\<\\([0-9]+\\)[ \t]*:" 1 font-lock-function-name-face
)
172 ; ("of" "to" "for" "if" "then" "else" "case" "while"
173 ; "do" "until" "and" "or" "not" "in" "with" "repeat" "begin" "end")
175 "and\\|begin\\|case\\|do\\|e\\(lse\\|nd\\)\\|for\\|i[fn]\\|"
176 "not\\|o[fr]\\|repeat\\|t\\(hen\\|o\\)\\|until\\|w\\(hile\\|ith\\)"
178 '("\\<\\(goto\\)\\>[ \t]*\\([0-9]+\\)?"
179 1 font-lock-keyword-face
)
180 '("\\<\\(goto\\)\\>[ \t]*\\([0-9]+\\)?"
181 2 font-lock-keyword-face t
)))
182 "Additional expressions to highlight in Pascal mode.")
183 (put 'pascal-mode
'font-lock-defaults
'(pascal-font-lock-keywords nil t
))
185 (defcustom pascal-indent-level
3
186 "Indentation of Pascal statements with respect to containing block."
190 (defcustom pascal-case-indent
2
191 "Indentation for case statements."
195 (defcustom pascal-auto-newline nil
196 "Non-nil means automatically insert newlines in certain cases.
197 These include after semicolons and after the punctuation mark after an `end'."
201 (defcustom pascal-indent-nested-functions t
202 "Non-nil means nested functions are indented."
206 (defcustom pascal-tab-always-indent t
207 "Non-nil means TAB in Pascal mode should always reindent the current line.
208 If this is nil, TAB inserts a tab if it is at the end of the line
209 and follows non-whitespace text."
213 (defcustom pascal-auto-endcomments t
214 "Non-nil means automatically insert comments after certain `end's.
215 Specifically, this is done after the ends of cases statements and functions.
216 The name of the function or case is included between the braces."
220 (defcustom pascal-auto-lineup
'(all)
221 "List of contexts where auto lineup of :'s or ='s should be done.
222 Elements can be of type: 'paramlist', 'declaration' or 'case', which will
223 do auto lineup in parameterlist, declarations or case-statements
224 respectively. The word 'all' will do all lineups. '(case paramlist) for
225 instance will do lineup in case-statements and parameterlist, while '(all)
226 will do all lineups."
227 :type
'(set :extra-offset
8
228 (const :tag
"Everything" all
)
229 (const :tag
"Parameter lists" paramlist
)
230 (const :tag
"Declarations" declaration
)
231 (const :tag
"Case statements" case
))
234 (defvar pascal-toggle-completions nil
235 "Non-nil meant \\<pascal-mode-map>\\[pascal-complete-word] would try all possible completions one by one.
236 Repeated use of \\[pascal-complete-word] would show you all of them.
237 Normally, when there is more than one possible completion,
238 it displays a list of all possible completions.")
239 (make-obsolete-variable 'pascal-toggle-completions
240 'completion-cycle-threshold
"24.1")
242 (defcustom pascal-type-keywords
243 '("array" "file" "packed" "char" "integer" "real" "string" "record")
244 "Keywords for types used when completing a word in a declaration or parmlist.
245 These include integer, real, char, etc.
246 The types defined within the Pascal program
247 are handled in another way, and should not be added to this list."
248 :type
'(repeat (string :tag
"Keyword"))
251 (defcustom pascal-start-keywords
252 '("begin" "end" "function" "procedure" "repeat" "until" "while"
253 "read" "readln" "reset" "rewrite" "write" "writeln")
254 "Keywords to complete when standing at the first word of a statement.
255 These are keywords such as begin, repeat, until, readln.
256 The procedures and variables defined within the Pascal program
257 are handled in another way, and should not be added to this list."
258 :type
'(repeat (string :tag
"Keyword"))
261 (defcustom pascal-separator-keywords
262 '("downto" "else" "mod" "div" "then")
263 "Keywords to complete when NOT standing at the first word of a statement.
264 These are keywords such as downto, else, mod, then.
265 Variables and function names defined within the Pascal program
266 are handled in another way, and should not be added to this list."
267 :type
'(repeat (string :tag
"Keyword"))
275 (defun pascal-declaration-end ()
277 (while (and (> nest
0)
279 "[:=]\\|\\(\\<record\\>\\)\\|\\(\\<end\\>\\)"
281 (cond ((match-beginning 1) (setq nest
(1+ nest
)))
282 ((match-beginning 2) (setq nest
(1- nest
)))
283 ((looking-at "[^(\n]+)") (setq nest
0))))))
286 (defun pascal-declaration-beg ()
288 (while (and (> nest
0)
289 (re-search-backward "[:=]\\|\\<\\(type\\|var\\|label\\|const\\)\\>\\|\\(\\<record\\>\\)\\|\\(\\<end\\>\\)" (point-at-bol 0) t
))
290 (cond ((match-beginning 1) (setq nest
0))
291 ((match-beginning 2) (setq nest
(1- nest
)))
292 ((match-beginning 3) (setq nest
(1+ nest
)))))
296 (defsubst pascal-within-string
()
297 (nth 3 (parse-partial-sexp (point-at-bol) (point))))
301 (define-derived-mode pascal-mode prog-mode
"Pascal"
302 "Major mode for editing Pascal code. \\<pascal-mode-map>
303 TAB indents for Pascal code. Delete converts tabs to spaces as it moves back.
305 \\[completion-at-point] completes the word around current point with respect \
307 \\[completion-help-at-point] shows all possible completions at this point.
309 Other useful functions are:
311 \\[pascal-mark-defun]\t- Mark function.
312 \\[pascal-insert-block]\t- insert begin ... end;
313 \\[pascal-star-comment]\t- insert (* ... *)
314 \\[pascal-comment-area]\t- Put marked area in a comment, fixing nested comments.
315 \\[pascal-uncomment-area]\t- Uncomment an area commented with \
316 \\[pascal-comment-area].
317 \\[pascal-beg-of-defun]\t- Move to beginning of current function.
318 \\[pascal-end-of-defun]\t- Move to end of current function.
319 \\[pascal-goto-defun]\t- Goto function prompted for in the minibuffer.
320 \\[pascal-outline-mode]\t- Enter `pascal-outline-mode'.
322 Variables controlling indentation/edit style:
324 `pascal-indent-level' (default 3)
325 Indentation of Pascal statements with respect to containing block.
326 `pascal-case-indent' (default 2)
327 Indentation for case statements.
328 `pascal-auto-newline' (default nil)
329 Non-nil means automatically newline after semicolons and the punctuation
331 `pascal-indent-nested-functions' (default t)
332 Non-nil means nested functions are indented.
333 `pascal-tab-always-indent' (default t)
334 Non-nil means TAB in Pascal mode should always reindent the current line,
335 regardless of where in the line point is when the TAB command is used.
336 `pascal-auto-endcomments' (default t)
337 Non-nil means a comment { ... } is set after the ends which ends cases and
338 functions. The name of the function or case will be set between the braces.
339 `pascal-auto-lineup' (default t)
340 List of contexts where auto lineup of :'s or ='s should be done.
342 See also the user variables `pascal-type-keywords', `pascal-start-keywords' and
343 `pascal-separator-keywords'.
345 Turning on Pascal mode calls the value of the variable pascal-mode-hook with
346 no args, if that value is non-nil."
347 (set (make-local-variable 'local-abbrev-table
) pascal-mode-abbrev-table
)
348 (set (make-local-variable 'indent-line-function
) 'pascal-indent-line
)
349 (set (make-local-variable 'comment-indent-function
) 'pascal-indent-comment
)
350 (set (make-local-variable 'parse-sexp-ignore-comments
) nil
)
351 (set (make-local-variable 'blink-matching-paren-dont-ignore-comments
) t
)
352 (set (make-local-variable 'case-fold-search
) t
)
353 (set (make-local-variable 'comment-start
) "{")
354 (set (make-local-variable 'comment-start-skip
) "(\\*+ *\\|{ *")
355 (set (make-local-variable 'comment-end
) "}")
356 (add-hook 'completion-at-point-functions
'pascal-completions-at-point nil t
)
358 (set (make-local-variable 'font-lock-defaults
)
359 '(pascal-font-lock-keywords nil t
))
361 (set (make-local-variable 'imenu-generic-expression
)
362 pascal-imenu-generic-expression
)
363 (set (make-local-variable 'imenu-case-fold-search
) t
)
364 ;; Pascal-mode's own hide/show support.
365 (add-to-invisibility-spec '(pascal . t
)))
370 ;;; Electric functions
372 (defun electric-pascal-terminate-line ()
373 "Terminate line and indent next line."
375 ;; First, check if current line should be indented
378 (skip-chars-forward " \t")
379 (if (looking-at pascal-autoindent-lines-re
)
380 (pascal-indent-line)))
381 (delete-horizontal-space) ; Removes trailing whitespaces
385 ;; Maybe we should set some endcomments
386 (if pascal-auto-endcomments
387 (pascal-set-auto-comments))
388 ;; Check if we shall indent inside comment
392 (skip-chars-forward " \t")
393 (cond ((looking-at "\\*[ \t]+)")
394 ;; Delete region between `*' and `)' if there is only whitespaces.
396 (delete-horizontal-space))
397 ((and (looking-at "(\\*\\|\\*[^)]")
398 (not (save-excursion (search-forward "*)" (point-at-eol) t
))))
400 ;; If last line was a star comment line then this one shall be too.
406 (defun electric-pascal-semi-or-dot ()
407 "Insert `;' or `.' character and reindent the line."
409 (insert last-command-event
)
412 (pascal-indent-line))
413 (if pascal-auto-newline
414 (electric-pascal-terminate-line)))
416 (defun electric-pascal-colon ()
417 "Insert `:' and do all indentations except line indent on this line."
419 (insert last-command-event
)
420 ;; Do nothing if within string.
421 (if (pascal-within-string)
425 (pascal-indent-line))
426 (let ((pascal-tab-always-indent nil
))
427 (pascal-indent-command))))
429 (defun electric-pascal-equal ()
430 "Insert `=', and do indentation if within type declaration."
432 (insert last-command-event
)
433 (if (eq (car (pascal-calculate-indent)) 'declaration
)
434 (let ((pascal-tab-always-indent nil
))
435 (pascal-indent-command))))
437 (defun electric-pascal-hash ()
438 "Insert `#', and indent to column 0 if this is a CPP directive."
440 (insert last-command-event
)
441 (if (save-excursion (beginning-of-line) (looking-at "^[ \t]*#"))
442 (save-excursion (beginning-of-line)
443 (delete-horizontal-space))))
445 (defun electric-pascal-tab ()
446 "Function called when TAB is pressed in Pascal mode."
448 ;; Do nothing if within a string or in a CPP directive.
449 (if (or (pascal-within-string)
451 (save-excursion (beginning-of-line) (eq (following-char) ?
#))))
453 ;; If pascal-tab-always-indent, indent the beginning of the line.
454 (if pascal-tab-always-indent
457 (pascal-indent-line))
459 (skip-chars-backward " \t")
463 (pascal-indent-command)))
468 ;;; Interactive functions
470 (defvar pascal--extra-indent
0)
472 (defun pascal-insert-block ()
473 "Insert Pascal begin ... end; block in the code with right indentation."
476 (electric-pascal-terminate-line)
481 (pascal-indent-line)))
483 (defun pascal-star-comment ()
484 "Insert Pascal star comment at point."
488 (electric-pascal-terminate-line)
490 (electric-pascal-terminate-line)
491 (delete-horizontal-space)
495 (defun pascal-mark-defun ()
496 "Mark the current pascal function (or procedure).
497 This puts the mark at the end, and point at the beginning."
500 (pascal-end-of-defun)
502 (pascal-beg-of-defun)
503 (when (featurep 'xemacs
)
504 (zmacs-activate-region)))
506 (defun pascal-comment-area (start end
)
507 "Put the region into a Pascal comment.
508 The comments that are in this area are \"deformed\":
509 `*)' becomes `!(*' and `}' becomes `!{'.
510 These deformed comments are returned to normal if you use
511 \\[pascal-uncomment-area] to undo the commenting.
513 The commented area starts with `pascal-exclude-str-start', and ends with
514 `pascal-include-str-end'. But if you change these variables,
515 \\[pascal-uncomment-area] won't recognize the comments."
518 ;; Insert start and endcomments
520 (if (and (save-excursion (skip-chars-forward " \t") (eolp))
521 (not (save-excursion (skip-chars-backward " \t") (bolp))))
524 (insert pascal-exclude-str-end
)
529 (insert pascal-exclude-str-start
)
531 ;; Replace end-comments within commented area
534 (while (re-search-backward "\\*)" start t
)
535 (replace-match "!(*" t t
)))
537 (while (re-search-backward "}" start t
)
538 (replace-match "!{" t t
)))))
540 (defun pascal-uncomment-area ()
541 "Uncomment a commented area; change deformed comments back to normal.
542 This command does nothing if the pointer is not in a commented
543 area. See also `pascal-comment-area'."
546 (let ((start (point))
548 ;; Find the boundaries of the comment
550 (setq start
(progn (search-backward pascal-exclude-str-start nil t
)
552 (setq end
(progn (search-forward pascal-exclude-str-end nil t
)
554 ;; Check if we're really inside a comment
555 (if (or (equal start
(point)) (<= end
(point)))
556 (message "Not standing within commented area.")
563 (delete-region pos
(1+ (point))))
564 ;; Change comments back to normal
566 (while (re-search-backward "!{" start t
)
567 (replace-match "}" t t
)))
569 (while (re-search-backward "!(\\*" start t
)
570 (replace-match "*)" t t
)))
571 ;; Remove startcomment
576 (delete-region pos
(1+ (point)))))))))
578 (defun pascal-beg-of-defun ()
579 "Move backward to the beginning of the current function or procedure."
582 (if (not (looking-at (concat "\\s \\|\\s)\\|" pascal-defun-re
)))
584 (let ((nest 0) (max -
1) (func 0)
585 (reg (concat pascal-beg-block-re
"\\|"
586 pascal-end-block-re
"\\|"
588 (while (re-search-backward reg nil
'move
)
589 (cond ((let ((state (save-excursion
590 (parse-partial-sexp (point-min) (point)))))
591 (or (nth 3 state
) (nth 4 state
))) ; Inside string or comment
593 ((match-end 1) ; begin|case|record|repeat
594 (if (and (looking-at "\\<record\\>") (>= max
0))
595 (setq func
(1- func
)))
598 ((match-end 2) ; end|until
599 (if (and (= nest max
) (>= max
0))
600 (setq func
(1+ func
)))
601 (setq nest
(1- nest
)))
602 ((match-end 3) ; function|procedure
605 (setq func
(1- func
)))))))
608 (defun pascal-end-of-defun ()
609 "Move forward to the end of the current function or procedure."
611 (if (looking-at "\\s ")
613 (if (not (looking-at pascal-defun-re
))
614 (pascal-beg-of-defun))
616 (let ((nest 0) (func 1)
617 (reg (concat pascal-beg-block-re
"\\|"
618 pascal-end-block-re
"\\|"
620 (while (and (/= func
0)
621 (re-search-forward reg nil
'move
))
622 (cond ((let ((state (save-excursion
623 (parse-partial-sexp (point-min) (point)))))
624 (or (nth 3 state
) (nth 4 state
))) ; Inside string or comment
627 (setq nest
(1+ nest
))
629 (goto-char (match-beginning 0))
630 (looking-at "\\<record\\>"))
631 (setq func
(1+ func
))))
633 (setq nest
(1- nest
))
635 (setq func
(1- func
))))
637 (setq func
(1+ func
))))))
640 (defun pascal-end-of-statement ()
641 "Move forward to end of current statement."
643 (let ((parse-sexp-ignore-comments t
)
645 (regexp (concat "\\(" pascal-beg-block-re
"\\)\\|\\("
646 pascal-end-block-re
"\\)")))
647 (if (not (looking-at "[ \t\n]")) (forward-sexp -
1))
648 (or (looking-at pascal-beg-block-re
)
649 ;; Skip to end of statement
650 (setq pos
(catch 'found
653 (cond ((looking-at "[ \t]*;")
654 (skip-chars-forward "^;")
656 (throw 'found
(point)))
659 (looking-at pascal-beg-block-re
))
660 (goto-char (match-beginning 0))
663 (throw 'found
(point))))))))
665 ;; Skip a whole block
668 (re-search-forward regexp nil
'move
)
669 (setq nest
(if (match-end 1)
673 (throw 'found
(point)))
675 (throw 'found
(pascal-end-of-statement))))))
678 (defun pascal-downcase-keywords ()
679 "Downcase all Pascal keywords in the buffer."
681 (pascal-change-keywords 'downcase-word
))
683 (defun pascal-upcase-keywords ()
684 "Upcase all Pascal keywords in the buffer."
686 (pascal-change-keywords 'upcase-word
))
688 (defun pascal-capitalize-keywords ()
689 "Capitalize all Pascal keywords in the buffer."
691 (pascal-change-keywords 'capitalize-word
))
693 ;; Change the keywords according to argument.
694 (defun pascal-change-keywords (change-word)
696 (let ((keyword-re (concat "\\<\\("
697 (mapconcat 'identity pascal-keywords
"\\|")
699 (goto-char (point-min))
700 (while (re-search-forward keyword-re nil t
)
701 (funcall change-word -
1)))))
708 (defun pascal-set-auto-comments ()
709 "Insert `{ case }' or `{ NAME }' on this line if appropriate.
710 Insert `{ case }' if there is an `end' on the line which
711 ends a case block. Insert `{ NAME }' if there is an `end'
712 on the line which ends a function or procedure named NAME."
715 (skip-chars-forward " \t")
716 (if (and (looking-at "\\<end;")
719 (search-backward "{" (point-at-bol) t
))))
720 (let ((type (car (pascal-calculate-indent))))
721 (if (eq type
'declaration
)
724 ;; This is a case block
727 (delete-horizontal-space)
728 (insert " { case }"))
730 ;; Check if this is the end of a function
732 (while (not (or (looking-at pascal-defun-re
) (bobp)))
734 (cond ((looking-at pascal-beg-block-re
)
735 (setq nest
(1- nest
)))
736 ((looking-at pascal-end-block-re
)
737 (setq nest
(1+ nest
)))))
743 (delete-horizontal-space)
747 (setq b
(progn (pascal-beg-of-defun)
748 (skip-chars-forward "^ \t")
749 (skip-chars-forward " \t")
751 e
(progn (skip-chars-forward "a-zA-Z0-9_")
753 (insert-buffer-substring (current-buffer) b e
))
754 (insert " }"))))))))))
761 (defconst pascal-indent-alist
762 '((block .
(+ pascal--extra-indent pascal-indent-level
))
763 (case .
(+ pascal--extra-indent pascal-case-indent
))
764 (caseblock . pascal--extra-indent
) (cpp .
0)
765 (declaration .
(+ pascal--extra-indent pascal-indent-level
))
766 (paramlist .
(pascal-indent-paramlist t
))
767 (comment .
(pascal-indent-comment))
768 (defun . pascal--extra-indent
) (contexp . pascal--extra-indent
)
769 (unknown . pascal--extra-indent
) (string .
0) (progbeg .
0)))
771 (defun pascal-indent-command ()
772 "Indent for special part of code."
773 (let* ((indent-str (pascal-calculate-indent))
774 (type (car indent-str
)))
775 (cond ((and (eq type
'paramlist
)
776 (or (memq 'all pascal-auto-lineup
)
777 (memq 'paramlist pascal-auto-lineup
)))
778 (pascal-indent-paramlist)
779 (pascal-indent-paramlist))
780 ((and (eq type
'declaration
)
781 (or (memq 'all pascal-auto-lineup
)
782 (memq 'declaration pascal-auto-lineup
)))
783 (pascal-indent-declaration))
784 ((and (eq type
'case
) (not (looking-at "^[ \t]*$"))
785 (or (memq 'all pascal-auto-lineup
)
786 (memq 'case pascal-auto-lineup
)))
787 (pascal-indent-case)))
788 (if (looking-at "[ \t]+$")
789 (skip-chars-forward " \t"))))
791 (defun pascal-indent-line ()
792 "Indent current line as a Pascal statement."
793 (let* ((indent-str (pascal-calculate-indent))
794 (type (car indent-str
))
795 (pascal--extra-indent (car (cdr indent-str
))))
796 ;; Labels should not be indented.
797 (if (and (looking-at "^[0-9a-zA-Z]+[ \t]*:[^=]")
798 (not (eq type
'declaration
)))
799 (search-forward ":" nil t
))
800 (delete-horizontal-space)
801 (cond (; Some things should not be indented
802 (or (and (eq type
'declaration
) (looking-at pascal-declaration-re
))
805 (; Other things should have no extra indent
806 (looking-at pascal-noindent-re
)
807 (indent-to pascal--extra-indent
))
808 (; Nested functions should be indented
809 (looking-at pascal-defun-re
)
810 (if (and pascal-indent-nested-functions
812 (indent-to (+ pascal--extra-indent pascal-indent-level
))
813 (indent-to pascal--extra-indent
)))
814 (; But most lines are treated this way
815 (indent-to (eval (cdr (assoc type pascal-indent-alist
))))
818 (defun pascal-calculate-indent ()
819 "Calculate the indent of the current Pascal line.
820 Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
822 (let* ((parse-sexp-ignore-comments t
)
824 (state (save-excursion (parse-partial-sexp (point-min) (point))))
825 (nest 0) (par 0) (complete (looking-at "[ \t]*end\\>"))
826 (elsed (looking-at "[ \t]*else\\>")) (funccnt 0)
827 (did-func (looking-at "[ \t]*\\(procedure\\|function\\)\\>"))
828 (type (catch 'nesting
829 ;; Check if inside a string, comment or parenthesis
830 (cond ((nth 3 state
) (throw 'nesting
'string
))
831 ((nth 4 state
) (throw 'nesting
'comment
))
833 (goto-char (scan-lists (point) -
1 (car state
)))
834 (setq par
(1+ (current-column))))
835 ((save-excursion (beginning-of-line)
836 (eq (following-char) ?
#))
837 (throw 'nesting
'cpp
)))
838 ;; Loop until correct indent is found
841 (cond (;--Escape from case statements
842 (and (looking-at "[A-Za-z0-9]+[ \t]*:[^=]")
844 (save-excursion (skip-chars-backward " \t")
847 (end-of-line) (backward-sexp) (point))
849 (> (save-excursion (goto-char oldpos
)
853 (throw 'nesting
'caseblock
))
854 (;--Beginning of program
855 (looking-at pascal-progbeg-re
)
856 (throw 'nesting
'progbeg
))
857 (;--No known statements
859 (throw 'nesting
'progbeg
))
860 (;--Nest block outwards
861 (looking-at pascal-beg-block-re
)
863 (cond ((looking-at "case\\>")
864 (throw 'nesting
'case
))
865 ((looking-at "record\\>")
866 (throw 'nesting
'declaration
))
867 (t (throw 'nesting
'block
)))
868 (if (and (looking-at "record\\>") (= nest
1))
869 (setq funccnt
(1- funccnt
)))
870 (setq nest
(1- nest
))))
871 (;--Nest block inwards
872 (looking-at pascal-end-block-re
)
873 (if (and (looking-at "end\\s ")
874 elsed
(not complete
))
875 (throw 'nesting
'block
))
877 (setq funccnt
(1+ funccnt
)))
880 (;--Defun (or parameter list)
881 (and (looking-at pascal-defun-re
)
882 (progn (setq funccnt
(1- funccnt
)
884 (or (bolp) (< funccnt
0))))
885 ;; Prevent searching whole buffer
886 (if (and (bolp) (>= funccnt
0))
887 (throw 'nesting
'progbeg
))
889 (throw 'nesting
'defun
)
892 (while (re-search-forward
893 "\\(\\<record\\>\\)\\|\\<end\\>"
896 (setq n
(1+ n
)) (setq n
(1- n
))))
898 (throw 'nesting
'declaration
)
899 (throw 'nesting
'paramlist
)))))
901 (and (looking-at pascal-declaration-re
)
907 (looking-at "^[ \t]*$"))
908 (throw 'nesting
'unknown
)
909 (throw 'nesting
'declaration
)))
910 (;--If, else or while statement
912 (looking-at pascal-sub-block-re
))
913 (throw 'nesting
'block
))
914 (;--Found complete statement
915 (save-excursion (forward-sexp 1)
916 (= (following-char) ?\
;))
920 ;; Return type of block and indent level.
921 (if (> par
0) ; Unclosed Parenthesis
923 (list type
(pascal-indent-level))))))
925 (defun pascal-indent-level ()
926 "Return the indent-level the current statement has.
927 Do not count labels, case-statements or records."
930 (if (looking-at "[ \t]*[0-9a-zA-Z]+[ \t]*:[^=]")
931 (search-forward ":" nil t
)
932 (if (looking-at ".*=[ \t]*record\\>")
933 (search-forward "=" nil t
)))
934 (skip-chars-forward " \t")
937 (defun pascal-indent-comment ()
938 "Return indent for current comment."
940 (re-search-backward "\\((\\*\\)\\|{" nil t
)
941 (if (match-beginning 1)
942 (1+ (current-column))
945 (defun pascal-indent-case ()
946 "Indent within case statements."
947 (let ((savepos (point-marker))
951 (re-search-backward "\\<case\\>" nil t
)))
953 (pascal--extra-indent 0))
955 (while (< (point) end
)
956 (if (re-search-forward
957 "^[ \t]*[^ \t,:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
958 (marker-position end
) 'move
)
962 (delete-horizontal-space)
963 (if (> (current-column) pascal--extra-indent
)
964 (setq pascal--extra-indent
(current-column)))
965 (pascal-end-of-statement))))
967 ;; Indent all case statements
968 (while (< (point) end
)
969 (if (re-search-forward
970 "^[ \t]*[^][ \t,\\.:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
971 (marker-position end
) 'move
)
973 (indent-to (1+ pascal--extra-indent
))
974 (if (/= (following-char) ?
:)
977 (delete-horizontal-space)
979 (pascal-end-of-statement))
980 (goto-char savepos
)))
982 (defun pascal-indent-paramlist (&optional arg
)
983 "Indent current line in parameterlist.
984 If optional arg is non-nil, just return the
985 indent of the current line in parameterlist."
987 (let* ((oldpos (point))
988 (stpos (progn (goto-char (scan-lists (point) -
1 1)) (point)))
989 (stcol (1+ (current-column)))
990 (edpos (progn (pascal-declaration-end)
991 (search-backward ")" (point-at-bol) t
)
993 (usevar (re-search-backward "\\<var\\>" stpos t
)))
995 ;; If arg, just return indent
998 (if (or (not usevar
) (looking-at "[ \t]*var\\>"))
1002 (delete-horizontal-space)
1003 (if (and usevar
(not (looking-at "var\\>")))
1004 (indent-to (+ 4 stcol
)))
1005 (pascal-indent-declaration nil stpos edpos
)))))
1007 (defun pascal-indent-declaration (&optional arg start end
)
1008 "Indent current lines as declaration, lining up the `:'s or `='s."
1009 (let ((pos (point-marker)))
1010 (if (and (not (or arg start
)) (not (pascal-declaration-beg)))
1012 (let ((lineup (if (or (looking-at "\\<var\\>\\|\\<record\\>") arg start
)
1014 (stpos (if start start
1015 (forward-word 2) (backward-word 1) (point)))
1016 (edpos (set-marker (make-marker)
1018 (max (progn (pascal-declaration-end)
1021 pascal--extra-indent
)
1024 ;; Indent lines in record block
1026 (while (<= (point) edpos
)
1028 (delete-horizontal-space)
1029 (if (looking-at "end\\>")
1031 (indent-to (+ arg pascal-indent-level
)))
1035 (setq pascal--extra-indent
(pascal-get-lineup-indent stpos edpos lineup
))
1037 (while (and (<= (point) edpos
) (not (eobp)))
1038 (if (search-forward lineup
(point-at-eol) 'move
)
1040 (delete-horizontal-space)
1041 (indent-to pascal--extra-indent
)
1042 (if (not (looking-at lineup
))
1043 (forward-line 1) ; No more indent if there is no : or =
1045 (delete-horizontal-space)
1047 ;; Indent record block
1048 (if (looking-at "record\\>")
1049 (pascal-indent-declaration (current-column)))
1050 (forward-line 1)))))
1052 ;; If arg - move point
1053 (if arg
(forward-line -
1)
1056 ; "Return the indent level that will line up several lines within the region
1057 ;from b to e nicely. The lineup string is str."
1058 (defun pascal-get-lineup-indent (b e str
)
1060 (let ((pascal--extra-indent 0)
1061 (reg (concat str
"\\|\\(\\<record\\>\\)\\|" pascal-defun-re
)))
1063 ;; Get rightmost position
1064 (while (< (point) e
)
1065 (and (re-search-forward reg
(min e
(point-at-eol 2)) 'move
)
1066 (cond ((match-beginning 1)
1067 ;; Skip record blocks
1068 (pascal-declaration-end))
1069 ((match-beginning 2)
1070 ;; We have entered a new procedure. Exit.
1073 (goto-char (match-beginning 0))
1074 (skip-chars-backward " \t")
1075 (if (> (current-column) pascal--extra-indent
)
1076 (setq pascal--extra-indent
(current-column)))
1077 (goto-char (match-end 0))
1080 ;; In case no lineup was found
1081 (if (> pascal--extra-indent
0)
1082 (1+ pascal--extra-indent
)
1083 ;; No lineup-string found
1086 (skip-chars-backward " \t")
1087 (1+ (current-column))))))
1094 (defun pascal-string-diff (str1 str2
)
1095 "Return index of first letter where STR1 and STR2 differs."
1099 (if (or (> (1+ diff
) (length str1
))
1100 (> (1+ diff
) (length str2
)))
1102 (or (equal (aref str1 diff
) (aref str2 diff
))
1104 (setq diff
(1+ diff
))))))
1106 ;; Calculate all possible completions for functions if argument is `function',
1107 ;; completions for procedures if argument is `procedure' or both functions and
1108 ;; procedures otherwise.
1110 (defun pascal-func-completion (type pascal-str
)
1111 ;; Build regular expression for function/procedure names
1113 (if (string= pascal-str
"")
1114 (setq pascal-str
"[a-zA-Z_]"))
1115 (let ((pascal-str (concat (cond
1116 ((eq type
'procedure
) "\\<\\(procedure\\)\\s +")
1117 ((eq type
'function
) "\\<\\(function\\)\\s +")
1118 (t "\\<\\(function\\|procedure\\)\\s +"))
1119 "\\<\\(" pascal-str
"[a-zA-Z0-9_.]*\\)\\>"))
1123 (if (not (looking-at "\\<\\(function\\|procedure\\)\\>"))
1124 (re-search-backward "\\<\\(function\\|procedure\\)\\>" nil t
))
1127 ;; Search through all reachable functions
1128 (while (pascal-beg-of-defun)
1129 (if (re-search-forward pascal-str
(point-at-eol) t
)
1130 (progn (setq match
(buffer-substring (match-beginning 2)
1132 (push match pascal-all
)))
1133 (goto-char (match-beginning 0)))
1137 (defun pascal-get-completion-decl (pascal-str)
1138 ;; Macro for searching through current declaration (var, type or const)
1139 ;; for matches of `str' and adding the occurrence to `all'
1140 (let ((end (save-excursion (pascal-declaration-end)
1145 (while (< (point) end
)
1146 (if (re-search-forward "[:=]" (point-at-eol) t
)
1147 ;; Traverse current line
1148 (while (and (re-search-backward
1149 (concat "\\((\\|\\<\\(var\\|type\\|const\\)\\>\\)\\|"
1152 (not (match-end 1)))
1153 (setq match
(buffer-substring (match-beginning 0) (match-end 0)))
1154 (if (string-match (concat "\\<" pascal-str
) match
)
1155 (push match pascal-all
))))
1156 (if (re-search-forward "\\<record\\>" (point-at-eol) t
)
1157 (pascal-declaration-end)
1162 (defun pascal-type-completion (pascal-str)
1163 "Calculate all possible completions for types."
1164 (let ((start (point))
1167 ;; Search for all reachable type declarations
1168 (while (or (pascal-beg-of-defun)
1169 (setq goon
(not goon
)))
1171 (if (and (< start
(prog1 (save-excursion (pascal-end-of-defun)
1175 "\\<type\\>\\|\\<\\(begin\\|function\\|procedure\\)\\>"
1177 (not (match-end 1)))
1178 ;; Check current type declaration
1180 (nconc (pascal-get-completion-decl pascal-str
)
1185 (defun pascal-var-completion (prefix)
1186 "Calculate all possible completions for variables (or constants)."
1188 (let ((start (point))
1191 ;; Search for all reachable var declarations
1192 (while (or (pascal-beg-of-defun)
1193 (setq goon
(not goon
)))
1195 (if (> start
(prog1 (save-excursion (pascal-end-of-defun)
1197 () ; Declarations not reachable
1198 (if (search-forward "(" (point-at-eol) t
)
1199 ;; Check parameterlist
1200 ;; FIXME: pascal-get-completion-decl doesn't understand
1201 ;; the var declarations in parameter lists :-(
1203 (nconc (pascal-get-completion-decl prefix
)
1206 (while (>= (setq twice
(1- twice
)) 0)
1208 ((and (re-search-forward
1209 (concat "\\<\\(var\\|const\\)\\>\\|"
1210 "\\<\\(begin\\|function\\|procedure\\)\\>")
1212 (not (match-end 2)))
1213 ;; Check var/const declarations
1215 (nconc (pascal-get-completion-decl prefix
)
1218 (setq twice
0)))))))
1222 (defun pascal-keyword-completion (keyword-list pascal-str
)
1223 "Give list of all possible completions of keywords in KEYWORD-LIST."
1224 (let ((pascal-all ()))
1225 (dolist (s keyword-list
)
1226 (if (string-match (concat "\\<" pascal-str
) s
)
1227 (push s pascal-all
)))
1230 ;; Function passed to completing-read, try-completion or
1231 ;; all-completions to get completion on STR. If predicate is non-nil,
1232 ;; it must be a function to be called for every match to check if this
1233 ;; should really be a match. If flag is t, the function returns a list
1234 ;; of all possible completions. If it is nil it returns a string, the
1235 ;; longest possible completion, or t if STR is an exact match. If flag
1236 ;; is 'lambda, the function returns t if STR is an exact match, nil
1239 (defvar pascal-completion-cache nil
)
1241 (defun pascal-completion (pascal-str pascal-pred pascal-flag
)
1242 (let ((all (car pascal-completion-cache
)))
1243 ;; Check the cache's freshness.
1244 (unless (and pascal-completion-cache
1245 (string-prefix-p (nth 1 pascal-completion-cache
) pascal-str
)
1246 (eq (current-buffer) (nth 2 pascal-completion-cache
))
1247 (eq (field-beginning) (nth 3 pascal-completion-cache
)))
1248 (let ((state (car (pascal-calculate-indent))))
1250 ;; Determine what should be completed
1252 ( ;--Within a declaration or parameterlist
1253 (or (eq state
'declaration
) (eq state
'paramlist
)
1254 (and (eq state
'defun
)
1256 (re-search-backward ")[ \t]*:" (point-at-bol) t
))))
1257 (if (or (eq state
'paramlist
) (eq state
'defun
))
1258 (pascal-beg-of-defun))
1260 (pascal-type-completion pascal-str
)
1261 (pascal-keyword-completion pascal-type-keywords pascal-str
)))
1262 ( ;--Starting a new statement
1263 (and (not (eq state
'contexp
))
1265 (skip-chars-backward "a-zA-Z0-9_.")
1267 (or (looking-at pascal-nosemi-re
)
1270 (looking-at "\\s *\\(;\\|:[^=]\\)")))))
1272 (pascal-var-completion pascal-str
)
1273 (pascal-func-completion 'procedure pascal-str
)
1274 (pascal-keyword-completion pascal-start-keywords pascal-str
)))
1277 (pascal-var-completion pascal-str
)
1278 (pascal-func-completion 'function pascal-str
)
1279 (pascal-keyword-completion pascal-separator-keywords
1282 (setq pascal-completion-cache
1283 (list all pascal-str
(current-buffer) (field-beginning)))))
1285 ;; Now we have built a list of all matches. Give response to caller
1286 (complete-with-action pascal-flag all pascal-str pascal-pred
)))
1288 (defvar pascal-last-word-numb
0)
1289 (defvar pascal-last-word-shown nil
)
1290 (defvar pascal-last-completions nil
)
1292 (defun pascal-completions-at-point ()
1293 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
1294 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point))))
1296 (list b e
#'pascal-completion
))))
1298 (define-obsolete-function-alias 'pascal-complete-word
1299 'completion-at-point
"24.1")
1301 (define-obsolete-function-alias 'pascal-show-completions
1302 'completion-help-at-point
"24.1")
1305 (defun pascal-get-default-symbol ()
1306 "Return symbol around current point as a string."
1308 (buffer-substring (progn
1309 (skip-chars-backward " \t")
1310 (skip-chars-backward "a-zA-Z0-9_")
1313 (skip-chars-forward "a-zA-Z0-9_")
1316 (defun pascal-build-defun-re (str &optional arg
)
1317 "Return function/procedure starting with STR as regular expression.
1318 With optional second arg non-nil, STR is the complete name of the instruction."
1320 (concat "^\\(function\\|procedure\\)[ \t]+\\(" str
"\\)\\>")
1321 (concat "^\\(function\\|procedure\\)[ \t]+\\(" str
"[a-zA-Z0-9_]*\\)\\>")))
1323 ;; Function passed to completing-read, try-completion or
1324 ;; all-completions to get completion on any function name. If
1325 ;; predicate is non-nil, it must be a function to be called for every
1326 ;; match to check if this should really be a match. If flag is t, the
1327 ;; function returns a list of all possible completions. If it is nil
1328 ;; it returns a string, the longest possible completion, or t if STR
1329 ;; is an exact match. If flag is 'lambda, the function returns t if
1330 ;; STR is an exact match, nil otherwise.
1332 (defun pascal-comp-defun (pascal-str pascal-pred pascal-flag
)
1334 (let ((pascal-all nil
))
1336 ;; Build regular expression for functions
1337 (let ((pascal-str (pascal-build-defun-re (if (string= pascal-str
"")
1340 (goto-char (point-min))
1342 ;; Build a list of all possible completions
1343 (while (re-search-forward pascal-str nil t
)
1344 (push (match-string 2) pascal-all
)))
1346 ;; Now we have built a list of all matches. Give response to caller
1347 (complete-with-action pascal-flag pascal-all pascal-str pascal-pred
))))
1349 (defun pascal-goto-defun ()
1350 "Move to specified Pascal function/procedure.
1351 The default is a name found in the buffer around point."
1353 (let* ((default (pascal-get-default-symbol))
1354 (default (if (pascal-comp-defun default nil
'lambda
)
1357 ;; Do completion with default.
1358 (completing-read (if (not (string= default
""))
1359 (concat "Label (default " default
"): ")
1361 ;; Complete with the defuns found in the
1363 (let ((buf (current-buffer)))
1365 (with-current-buffer buf
1366 (pascal-comp-defun s p a
))))
1368 ;; If there was no response on prompt, use default value.
1369 (if (string= label
"")
1370 (setq label default
))
1371 ;; Goto right place in buffer if label is not an empty string.
1372 (or (string= label
"")
1374 (goto-char (point-min))
1375 (re-search-forward (pascal-build-defun-re label t
))
1376 (beginning-of-line)))))
1381 ;;; Pascal-outline-mode
1383 (defvar pascal-outline-map
1384 (let ((map (make-sparse-keymap)))
1385 (if (fboundp 'set-keymap-name
)
1386 (set-keymap-name pascal-outline-map
'pascal-outline-map
))
1387 (define-key map
"\M-\C-a" 'pascal-outline-prev-defun
)
1388 (define-key map
"\M-\C-e" 'pascal-outline-next-defun
)
1389 (define-key map
"\C-c\C-d" 'pascal-outline-goto-defun
)
1390 (define-key map
"\C-c\C-s" 'pascal-show-all
)
1391 (define-key map
"\C-c\C-h" 'pascal-hide-other-defuns
)
1393 "Keymap used in Pascal Outline mode.")
1395 (define-obsolete-function-alias 'pascal-outline
'pascal-outline-mode
"22.1")
1396 (define-minor-mode pascal-outline-mode
1397 "Outline-line minor mode for Pascal mode.
1398 With a prefix argument ARG, enable the mode if ARG is positive,
1399 and disable it otherwise. If called from Lisp, enable the mode
1400 if ARG is omitted or nil.
1402 When enabled, portions of the text being edited may be made
1403 invisible. \\<pascal-outline-map>
1405 Pascal Outline mode provides some additional commands.
1407 \\[pascal-outline-prev-defun]\
1408 \t- Move to previous function/procedure, hiding everything else.
1409 \\[pascal-outline-next-defun]\
1410 \t- Move to next function/procedure, hiding everything else.
1411 \\[pascal-outline-goto-defun]\
1412 \t- Goto function/procedure prompted for in minibuffer,
1413 \t hide all other functions.
1414 \\[pascal-show-all]\t- Show the whole buffer.
1415 \\[pascal-hide-other-defuns]\
1416 \t- Hide everything but the current function (function under the cursor).
1417 \\[pascal-outline]\t- Leave pascal-outline-mode."
1418 :init-value nil
:lighter
" Outl" :keymap pascal-outline-map
1419 (add-to-invisibility-spec '(pascal . t
))
1420 (unless pascal-outline-mode
1423 (defun pascal-outline-change (b e hide
)
1425 ;; We could try and optimize this in the case where the region is
1426 ;; already hidden. But I'm not sure it's worth the trouble.
1427 (remove-overlays b e
'invisible
'pascal
)
1429 (let ((ol (make-overlay b e nil t nil
)))
1430 (overlay-put ol
'invisible
'pascal
)
1431 (overlay-put ol
'evaporate t
)))))
1433 (defun pascal-show-all ()
1434 "Show all of the text in the buffer."
1436 (pascal-outline-change (point-min) (point-max) nil
))
1438 (defun pascal-hide-other-defuns ()
1439 "Show only the current defun."
1442 (let ((beg (progn (if (not (looking-at "\\(function\\|procedure\\)\\>"))
1443 (pascal-beg-of-defun))
1444 (line-beginning-position)))
1445 (end (progn (pascal-end-of-defun)
1447 (line-beginning-position 2)))
1448 (opoint (point-min)))
1452 (goto-char (point-min))
1454 ;; Hide all functions before current function
1455 (while (re-search-forward "^[ \t]*\\(function\\|procedure\\)\\>"
1457 (pascal-outline-change opoint
(line-end-position 0) t
)
1458 (setq opoint
(line-end-position))
1459 ;; Functions may be nested
1460 (if (> (progn (pascal-end-of-defun) (point)) beg
)
1461 (goto-char opoint
)))
1463 (pascal-outline-change opoint
(1- beg
) t
))
1465 ;; Show current function
1466 (pascal-outline-change (1- beg
) end nil
)
1467 ;; Hide nested functions
1469 (while (re-search-forward "^\\(function\\|procedure\\)\\>" end
'move
)
1470 (setq opoint
(line-end-position))
1471 (pascal-end-of-defun)
1472 (pascal-outline-change opoint
(line-end-position) t
))
1477 ;; Hide all function after current function
1478 (while (re-search-forward "^\\(function\\|procedure\\)\\>" nil
'move
)
1479 (pascal-outline-change opoint
(line-end-position 0) t
)
1480 (setq opoint
(line-end-position))
1481 (pascal-end-of-defun))
1482 (pascal-outline-change opoint
(point-max) t
)
1484 ;; Hide main program
1485 (if (< (progn (forward-line -
1) (point)) end
)
1488 (pascal-end-of-defun)
1490 (pascal-outline-change (line-end-position) (point-max) t
))))))
1492 (defun pascal-outline-next-defun ()
1493 "Move to next function/procedure, hiding all others."
1495 (pascal-end-of-defun)
1496 (pascal-hide-other-defuns))
1498 (defun pascal-outline-prev-defun ()
1499 "Move to previous function/procedure, hiding all others."
1501 (pascal-beg-of-defun)
1502 (pascal-hide-other-defuns))
1504 (defun pascal-outline-goto-defun ()
1505 "Move to specified function/procedure, hiding all others."
1508 (pascal-hide-other-defuns))
1512 ;;; pascal.el ends here