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