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