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