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