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