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