Commit | Line | Data |
---|---|---|
d9e94c22 MS |
1 | ;;; cc-awk.el --- AWK specific code within cc-mode. |
2 | ||
3 | ;; Copyright (C) 1988,94,96,2000,01,02,03 Free Software Foundation, Inc. | |
4 | ||
3efc2cd7 | 5 | ;; Author: Alan Mackenzie <acm@muc.de> (originally based on awk-mode.el) |
d9e94c22 MS |
6 | ;; Maintainer: FSF |
7 | ;; Keywords: AWK, cc-mode, unix, languages | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
25 | ||
26 | ;;; Commentary: | |
27 | ||
28 | ;; This file contains (most of) the adaptations to cc-mode required for the | |
29 | ;; integration of AWK Mode. | |
30 | ;; It is organised thusly: | |
31 | ;; 1. The AWK Mode syntax table. | |
32 | ;; 2. Indentation calculation stuff ("c-awk-NL-prop text-property"). | |
33 | ;; 3. Syntax-table property/font-locking stuff, but not including the | |
34 | ;; font-lock-keywords setting. | |
35 | ;; 4. The AWK Mode before/after-change-functions. | |
36 | ;; 5. AWK Mode specific versions of commands like beginning-of-defun. | |
37 | ;; The AWK Mode keymap, abbreviation table, and the mode function itself are | |
38 | ;; in cc-mode.el. | |
39 | ||
40 | ;;; Code: | |
41 | ||
42 | (eval-when-compile | |
43 | (let ((load-path | |
44 | (if (and (boundp 'byte-compile-dest-file) | |
45 | (stringp byte-compile-dest-file)) | |
46 | (cons (file-name-directory byte-compile-dest-file) load-path) | |
47 | load-path))) | |
48 | (load "cc-bytecomp" nil t))) | |
49 | ||
50 | (cc-require 'cc-defs) | |
51 | ||
52 | ;; Silence the byte compiler. | |
53 | (cc-bytecomp-defvar font-lock-mode) ; Checked with boundp before use. | |
54 | ||
55 | ;; Some functions in cc-engine that are used below. There's a cyclic | |
56 | ;; dependency so it can't be required here. (Perhaps some functions | |
57 | ;; could be moved to cc-engine to avoid it.) | |
58 | (cc-bytecomp-defun c-backward-token-1) | |
59 | (cc-bytecomp-defun c-beginning-of-statement-1) | |
60 | (cc-bytecomp-defun c-backward-sws) | |
61 | ||
62 | (defvar awk-mode-syntax-table | |
63 | (let ((st (make-syntax-table))) | |
64 | (modify-syntax-entry ?\\ "\\" st) | |
65 | (modify-syntax-entry ?\n "> " st) | |
66 | (modify-syntax-entry ?\r "> " st) | |
67 | (modify-syntax-entry ?\f "> " st) | |
68 | (modify-syntax-entry ?\# "< " st) | |
69 | ;; / can delimit regexes or be a division operator. By default we assume | |
70 | ;; that it is a division sign, and fix the regexp operator cases with | |
71 | ;; `font-lock-syntactic-keywords'. | |
72 | (modify-syntax-entry ?/ "." st) ; ACM 2002/4/27. | |
73 | (modify-syntax-entry ?* "." st) | |
74 | (modify-syntax-entry ?+ "." st) | |
75 | (modify-syntax-entry ?- "." st) | |
76 | (modify-syntax-entry ?= "." st) | |
77 | (modify-syntax-entry ?% "." st) | |
78 | (modify-syntax-entry ?< "." st) | |
79 | (modify-syntax-entry ?> "." st) | |
80 | (modify-syntax-entry ?& "." st) | |
81 | (modify-syntax-entry ?| "." st) | |
82 | (modify-syntax-entry ?_ "_" st) | |
83 | (modify-syntax-entry ?\' "." st) | |
84 | st) | |
85 | "Syntax table in use in AWK Mode buffers.") | |
86 | ||
87 | ;; ACM, 2002/5/29: | |
88 | ;; | |
89 | ;; The next section of code is about determining whether or not an AWK | |
90 | ;; statement is complete or not. We use this to indent the following line. | |
91 | ;; The determination is pretty straightforward in C, where a statement ends | |
92 | ;; with either a ; or a }. Only "while" really gives any trouble there, since | |
93 | ;; it might be the end of a do-while. In AWK, on the other hand, semicolons | |
94 | ;; are rarely used, and EOLs _usually_ act as "virtual semicolons". In | |
95 | ;; addition, we have the complexity of escaped EOLs. The core of this | |
96 | ;; analysis is in the middle of the function | |
97 | ;; c-awk-calculate-NL-prop-prev-line, about 130 lines lower down. | |
98 | ;; | |
99 | ;; To avoid continually repeating this expensive analysis, we "cache" its | |
100 | ;; result in a text-property, c-awk-NL-prop, whose value for a line is set on | |
101 | ;; the EOL (if any) which terminates that line. Should the property be | |
102 | ;; required for the very last line (which has no EOL), it is calculated as | |
103 | ;; required but not cached. The c-awk-NL-prop property should be thought of | |
104 | ;; as only really valid immediately after a buffer change, not a permanently | |
105 | ;; set property. (By contrast, the syntax-table text properties (set by an | |
106 | ;; after-change function) must be constantly updated for the mode to work | |
107 | ;; properly). | |
108 | ;; | |
109 | ;; The valid values for c-awk-NL-prop are: | |
110 | ;; | |
111 | ;; nil The property is not currently set for this line. | |
112 | ;; '#' There is NO statement on this line (at most a comment), and no open | |
113 | ;; statement from a previous line which could have been completed on this | |
114 | ;; line. | |
115 | ;; '{' There is an unfinished statement on this (or a previous) line which | |
116 | ;; doesn't require \s to continue onto another line, e.g. the line ends | |
117 | ;; with {, or the && operator, or "if (condition)". Note that even if the | |
118 | ;; newline is redundantly escaped, it remains a '{' line. | |
119 | ;; '\' There is an escaped newline at the end of this line and this '\' is | |
120 | ;; essential to the syntax of the program. (i.e. if it had been a | |
121 | ;; frivolous \, it would have been ignored and the line been given one of | |
122 | ;; the other property values.) | |
123 | ;; ';' A statement is completed as the last thing (aside from ws) on the line - | |
124 | ;; i.e. there is (at least part of) a statement on this line, and the last | |
125 | ;; statement on the line is complete, OR (2002/10/25) the line is | |
126 | ;; content-free but terminates a statement from the preceding (continued) | |
127 | ;; line (which has property \). | |
128 | ;; | |
129 | ;; This set of values has been chosen so that the property's value on a line | |
130 | ;; is completely determined by the contents of the line and the property on | |
131 | ;; the previous line, EXCEPT for where a "while" might be the closing | |
132 | ;; statement of a do-while. | |
133 | ||
134 | (defun c-awk-after-if-for-while-condition-p (&optional do-lim) | |
135 | ;; Are we just after the ) in "if/for/while (<condition>)"? | |
136 | ;; | |
137 | ;; Note that the end of the ) in a do .... while (<condition>) doesn't | |
138 | ;; count, since the purpose of this routine is essentially to decide | |
139 | ;; whether to indent the next line. | |
140 | ;; | |
141 | ;; DO-LIM sets a limit on how far back we search for the "do" of a possible | |
142 | ;; do-while. | |
143 | (and | |
144 | (eq (char-before) ?\)) | |
145 | (save-excursion | |
146 | (let ((par-pos (c-safe (scan-lists (point) -1 0)))) | |
147 | (when par-pos | |
148 | (goto-char par-pos) ; back over "(...)" | |
149 | (c-backward-token-1) ; BOB isn't a problem. | |
150 | (or (looking-at "\\(if\\|for\\)\\>\\([^_]\\|$\\)") | |
151 | (and (looking-at "while\\>\\([^_]\\|$\\)") ; Ensure this isn't a do-while. | |
152 | (not (eq (c-beginning-of-statement-1 do-lim) | |
153 | 'beginning))))))))) | |
154 | ||
155 | (defun c-awk-after-function-decl-param-list () | |
156 | ;; Are we just after the ) in "function foo (bar)" ? | |
157 | (and (eq (char-before) ?\)) | |
158 | (save-excursion | |
159 | (let ((par-pos (c-safe (scan-lists (point) -1 0)))) | |
160 | (when par-pos | |
161 | (goto-char par-pos) ; back over "(...)" | |
162 | (c-backward-token-1) ; BOB isn't a problem | |
163 | (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*\\>") | |
164 | (progn (c-backward-token-1) | |
165 | (looking-at "func\\(tion\\)?\\>")))))))) | |
166 | ||
167 | ;; 2002/11/8: FIXME! Check c-backward-token-1/2 for success (0 return code). | |
168 | (defun c-awk-after-continue-token () | |
169 | ;; Are we just after a token which can be continued onto the next line without | |
170 | ;; a backslash? | |
171 | (save-excursion | |
172 | (c-backward-token-1) ; FIXME 2002/10/27. What if this fails? | |
173 | (if (and (looking-at "[&|]") (not (bobp))) | |
174 | (backward-char)) ; c-backward-token-1 doesn't do this :-( | |
175 | (looking-at "[,{?:]\\|&&\\|||\\|do\\>\\|else\\>"))) | |
176 | ||
177 | (defun c-awk-after-rbrace-or-statement-semicolon () | |
178 | ;; Are we just after a } or a ; which closes a statement? | |
179 | ;; Be careful about ;s in for loop control bits. They don't count! | |
180 | (or (eq (char-before) ?\}) | |
181 | (and | |
182 | (eq (char-before) ?\;) | |
183 | (save-excursion | |
184 | (let ((par-pos (c-safe (scan-lists (point) -1 1)))) | |
185 | (when par-pos | |
186 | (goto-char par-pos) ; go back to containing ( | |
187 | (not (and (looking-at "(") | |
188 | (c-backward-token-1) ; BOB isn't a problem | |
189 | (looking-at "for\\>"))))))))) | |
190 | ||
191 | (defun c-awk-back-to-contentful-text-or-NL-prop () | |
192 | ;; Move back to just after the first found of either (i) an EOL which has | |
193 | ;; the c-awk-NL-prop text-property set; or (ii) non-ws text; or (iii) BOB. | |
194 | ;; We return either the value of c-awk-NL-prop (in case (i)) or nil. | |
195 | ;; Calling function can best distinguish cases (ii) and (iii) with (bolp). | |
196 | ;; | |
197 | ;; Note that an escaped eol counts as whitespace here. | |
198 | ;; | |
199 | ;; Kludge: If c-backward-syntactic-ws gets stuck at a BOL, it is likely | |
200 | ;; that the previous line contains an unterminated string (without \). In | |
201 | ;; this case, assume that the previous line's c-awk-NL-prop is a ;. | |
202 | ;; | |
203 | ;; POINT MUST BE AT THE START OF A LINE when calling this function. This | |
204 | ;; is to ensure that the various backward-comment functions will work | |
205 | ;; properly. | |
206 | (let ((nl-prop nil) | |
207 | bol-pos bsws-pos) ; starting pos for a backward-syntactic-ws call. | |
208 | (while ;; We are at a BOL here. Go back one line each iteration. | |
209 | (and | |
210 | (not (bobp)) | |
211 | (not (setq nl-prop (c-get-char-property (1- (point)) 'c-awk-NL-prop))) | |
212 | (progn (setq bol-pos (c-point 'bopl)) | |
213 | (setq bsws-pos (point)) | |
214 | ;; N.B. the following function will not go back past an EOL if | |
215 | ;; there is an open string (without \) on the previous line. | |
216 | (c-backward-syntactic-ws bol-pos) | |
217 | (or (/= (point) bsws-pos) | |
218 | (progn (setq nl-prop ?\;) | |
219 | nil))) | |
220 | ;; If we had a backslash at EOL, c-backward-syntactic-ws will | |
221 | ;; have gone backwards over it. Check the backslash was "real". | |
222 | (progn | |
223 | (if (looking-at "[ \t]*\\\\+$") | |
224 | (if (progn | |
225 | (end-of-line) | |
226 | (search-backward-regexp | |
227 | "\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\$" ; ODD number of \s at EOL :-) | |
228 | bol-pos t)) | |
229 | (progn (end-of-line) ; escaped EOL. | |
230 | (backward-char) | |
231 | (c-backward-syntactic-ws bol-pos)) | |
232 | (end-of-line))) ; The \ at eol is a fake. | |
233 | (bolp)))) | |
234 | nl-prop)) | |
235 | ||
236 | (defun c-awk-calculate-NL-prop-prev-line (&optional do-lim) | |
237 | ;; Calculate and set the value of the c-awk-NL-prop on the immediately | |
238 | ;; preceding EOL. This may also involve doing the same for several | |
239 | ;; preceding EOLs. | |
240 | ;; | |
241 | ;; NOTE that if the property was already set, we return it without | |
242 | ;; recalculation. (This is by accident rather than design.) | |
243 | ;; | |
244 | ;; Return the property which got set (or was already set) on the previous | |
245 | ;; line. Return nil if we hit BOB. | |
246 | ;; | |
247 | ;; See c-awk-after-if-for-while-condition-p for a description of DO-LIM. | |
248 | (save-excursion | |
249 | (save-match-data | |
250 | (beginning-of-line) | |
251 | (let* ((pos (point)) | |
252 | (nl-prop (c-awk-back-to-contentful-text-or-NL-prop))) | |
253 | ;; We are either (1) at a BOL (with nl-prop containing the previous | |
254 | ;; line's c-awk-NL-prop) or (2) after contentful text on a line. At | |
255 | ;; the BOB counts as case (1), so we test next for bolp rather than | |
256 | ;; non-nil nl-prop. | |
257 | (when (not (bolp)) | |
258 | (setq nl-prop | |
259 | (cond | |
260 | ;; Incomplete statement which doesn't require escaped EOL? | |
261 | ((or (c-awk-after-if-for-while-condition-p do-lim) | |
262 | (c-awk-after-function-decl-param-list) | |
263 | (c-awk-after-continue-token)) | |
264 | ?\{) | |
265 | ;; Escaped EOL (where there's also something to continue)? | |
266 | ((and (looking-at "[ \t]*\\\\$") | |
267 | (not (c-awk-after-rbrace-or-statement-semicolon))) | |
268 | ?\\) | |
269 | (t ?\;))) ; A statement was completed on this line | |
270 | (end-of-line) | |
271 | (c-put-char-property (point) 'c-awk-NL-prop nl-prop) | |
272 | (forward-line)) | |
273 | ||
274 | ;; We are now at a (possibly empty) sequence of content-free lines. | |
275 | ;; Set c-awk-NL-prop on each of these lines's EOL. | |
276 | (while (< (point) pos) ; one content-free line each iteration. | |
277 | (cond ; recalculate nl-prop from previous line's value. | |
278 | ((memq nl-prop '(?\; nil)) (setq nl-prop ?\#)) | |
279 | ((eq nl-prop ?\\) | |
280 | (if (not (looking-at "[ \t]*\\\\$")) (setq nl-prop ?\;))) ; was ?\# 2002/10/25 | |
281 | ;; ?\# (empty line) and ?\{ (open stmt) don't change. | |
282 | ) | |
283 | (forward-line) | |
284 | (c-put-char-property (1- (point)) 'c-awk-NL-prop nl-prop)) | |
285 | nl-prop)))) | |
286 | ||
287 | (defun c-awk-get-NL-prop-prev-line (&optional do-lim) | |
288 | ;; Get the c-awk-NL-prop text-property from the previous line, calculating | |
289 | ;; it if necessary. Return nil iff we're already at BOB. | |
290 | ;; See c-awk-after-if-for-while-condition-p for a description of DO-LIM. | |
291 | (if (bobp) | |
292 | nil | |
293 | (or (c-get-char-property (c-point 'eopl) 'c-awk-NL-prop) | |
294 | (c-awk-calculate-NL-prop-prev-line do-lim)))) | |
295 | ||
296 | (defun c-awk-get-NL-prop-cur-line (&optional do-lim) | |
297 | ;; Get the c-awk-NL-prop text-property from the current line, calculating it | |
298 | ;; if necessary. (As a special case, the property doesn't get set on an | |
299 | ;; empty line at EOB (there's no position to set the property on), but the | |
300 | ;; function returns the property value an EOL would have got.) | |
301 | ;; | |
302 | ;; See c-awk-after-if-for-while-condition-p for a description of DO-LIM. | |
303 | (save-excursion | |
304 | (let ((extra-nl nil)) | |
305 | (end-of-line) ; Necessary for the following test to work. | |
306 | (when (= (forward-line) 1) ; if we were on the last line.... | |
307 | (insert-char ?\n 1) ; ...artificial eol is needed for comment detection. | |
308 | (setq extra-nl t)) | |
309 | (prog1 (c-awk-get-NL-prop-prev-line do-lim) | |
310 | (if extra-nl (delete-backward-char 1)))))) | |
311 | ||
312 | (defun c-awk-prev-line-incomplete-p (&optional do-lim) | |
313 | ;; Is there an incomplete statement at the end of the previous line? | |
314 | ;; See c-awk-after-if-for-while-condition-p for a description of DO-LIM. | |
315 | (memq (c-awk-get-NL-prop-prev-line do-lim) '(?\\ ?\{))) | |
316 | ||
317 | (defun c-awk-cur-line-incomplete-p (&optional do-lim) | |
318 | ;; Is there an incomplete statement at the end of the current line? | |
319 | ;; See c-awk-after-if-for-while-condition-p for a description of DO-LIM. | |
320 | (memq (c-awk-get-NL-prop-cur-line do-lim) '(?\\ ?\{))) | |
321 | ||
322 | (defun c-awk-completed-stmt-ws-ends-prev-line-p (&optional do-lim) | |
323 | ;; Is there a termination of a statement as the last thing (apart from an | |
324 | ;; optional comment) on the previous line? | |
325 | ;; See c-awk-after-if-for-while-condition-p for a description of DO-LIM. | |
326 | (eq (c-awk-get-NL-prop-prev-line do-lim) ?\;)) | |
327 | ||
328 | (defun c-awk-completed-stmt-ws-ends-line-p (&optional pos do-lim) | |
329 | ;; Same as previous function, but for the line containing position POS (or | |
330 | ;; the current line if POS is omitted). | |
331 | ;; See c-awk-after-if-for-while-condition-p for a description of DO-LIM. | |
332 | (save-excursion | |
333 | (if pos (goto-char pos)) | |
334 | (eq (c-awk-get-NL-prop-cur-line do-lim) ?\;))) | |
335 | ||
336 | (defun c-awk-after-logical-semicolon (&optional do-lim) | |
337 | ;; Are we at BOL, the preceding EOL being a "logical semicolon"? | |
338 | ;; See c-awk-after-if-for-while-condition-p for a description of DO-LIM. | |
339 | (and (bolp) | |
340 | (eq (c-awk-get-NL-prop-prev-line do-lim) ?\;))) | |
341 | ||
342 | (defun c-awk-backward-syntactic-ws (&optional lim) | |
343 | ;; Skip backwards over awk-syntactic whitespace. This is whitespace | |
344 | ;; characters, comments, and NEWLINES WHICH AREN'T "VIRTUAL SEMICOLONS". For | |
345 | ;; this function, a newline isn't a "virtual semicolon" if that line ends with | |
346 | ;; a real semicolon (or closing brace). | |
347 | ;; However if point starts inside a comment or preprocessor directive, the | |
348 | ;; content of it is not treated as whitespace. LIM (optional) sets a limit on | |
349 | ;; the backward movement. | |
350 | (let ((lim (or lim (point-min))) | |
351 | after-real-br) | |
352 | (c-backward-syntactic-ws (max lim (c-point 'bol))) | |
353 | (while ; go back one WS line each time round this loop. | |
354 | (and (bolp) | |
355 | (> (point) lim) | |
356 | (/= (c-awk-get-NL-prop-prev-line) ?\;) | |
357 | (/= (point) | |
358 | ;; The following function requires point at BONL [not EOL] to | |
359 | ;; recognise a preceding comment,. | |
360 | (progn (c-backward-syntactic-ws (max lim (c-point 'bopl))) | |
361 | (point))))) | |
362 | ;; Does the previous line end with a real ; or }? If so, go back to it. | |
363 | (if (and (bolp) | |
364 | (eq (c-awk-get-NL-prop-prev-line) ?\;) | |
365 | (save-excursion | |
366 | (c-backward-syntactic-ws (max lim (c-point 'bopl))) | |
367 | (setq after-real-br (point)) | |
368 | (c-awk-after-rbrace-or-statement-semicolon))) | |
369 | (goto-char after-real-br)))) | |
370 | ||
371 | (defun c-awk-NL-prop-not-set () | |
372 | ;; Is the NL-prop on the current line either nil or unset? | |
373 | (not (c-get-char-property (c-point 'eol) 'c-awk-NL-prop))) | |
374 | ||
375 | (defun c-awk-clear-NL-props (beg end) | |
376 | ;; This function is run from before-change-hooks. It clears the | |
377 | ;; c-awk-NL-prop text property from beg to the end of the buffer (The END | |
378 | ;; parameter is ignored). This ensures that the indentation engine will | |
379 | ;; never use stale values for this property. | |
380 | (save-restriction | |
381 | (widen) | |
382 | (c-clear-char-properties beg (point-max) 'c-awk-NL-prop))) | |
383 | ||
384 | (defun c-awk-unstick-NL-prop () | |
385 | ;; Ensure that the text property c-awk-NL-prop is "non-sticky". Without | |
386 | ;; this, a new newline inserted after an old newline (e.g. by C-j) would | |
387 | ;; inherit any c-awk-NL-prop from the old newline. This would be a Bad | |
388 | ;; Thing. This function's action is required by c-put-char-property. | |
389 | (if (and (boundp 'text-property-default-nonsticky) ; doesn't exist in Xemacs | |
390 | (not (assoc 'c-awk-NL-prop text-property-default-nonsticky))) | |
391 | (setq text-property-default-nonsticky | |
392 | (cons '(c-awk-NL-prop . t) text-property-default-nonsticky)))) | |
393 | ||
394 | ;; The following is purely a diagnostic command, to be commented out of the | |
395 | ;; final release. ACM, 2002/6/1 | |
396 | ;; (defun NL-props () | |
397 | ;; (interactive) | |
398 | ;; (let (pl-prop cl-prop) | |
399 | ;; (message "Prev-line: %s Cur-line: %s" | |
400 | ;; (if (setq pl-prop (c-get-char-property (c-point 'eopl) 'c-awk-NL-prop)) | |
401 | ;; (char-to-string pl-prop) | |
402 | ;; "nil") | |
403 | ;; (if (setq cl-prop (c-get-char-property (c-point 'eol) 'c-awk-NL-prop)) | |
404 | ;; (char-to-string cl-prop) | |
405 | ;; "nil")))) | |
406 | ;(define-key awk-mode-map [?\C-c ?\r] 'NL-props) ; commented out, 2002/8/31 | |
407 | ;for now. In the byte compiled version, this causes things to crash because | |
408 | ;awk-mode-map isn't yet defined. :-( | |
409 | ||
410 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
411 | ||
412 | ;; The following section of the code is to do with font-locking. The biggest | |
413 | ;; problem for font-locking is deciding whether a / is a regular expression | |
414 | ;; delimiter or a division sign - determining precisely where strings and | |
415 | ;; regular expressions start and stop is also troublesome. This is the | |
416 | ;; purpose of the function c-awk-set-syntax-table-properties and the myriad | |
417 | ;; elisp regular expressions it uses. | |
418 | ;; | |
419 | ;; Because AWK is a line oriented language, I felt the normal cc-mode strategy | |
420 | ;; for font-locking unterminated strings (i.e. font-locking the buffer up to | |
421 | ;; the next string delimiter as a string) was inappropriate. Instead, | |
422 | ;; unbalanced string/regexp delimiters are given the warning font, being | |
423 | ;; refonted with the string font as soon as the matching delimiter is entered. | |
424 | ;; | |
425 | ;; This requires the region processed by the current font-lock after-change | |
426 | ;; function to have access to the start of the string/regexp, which may be | |
427 | ;; several lines back. The elisp "advice" feature is used on these functions | |
428 | ;; to allow this. | |
429 | ||
430 | (defun c-awk-beginning-of-logical-line (&optional pos) | |
431 | ;; Go back to the start of the (apparent) current line (or the start of the | |
432 | ;; line containing POS), returning the buffer position of that point. I.e., | |
433 | ;; go back to the last line which doesn't have an escaped EOL before it. | |
434 | ;; | |
435 | ;; This is guaranteed to be "safe" for syntactic analysis, i.e. outwith any | |
436 | ;; comment, string or regexp. IT MAY WELL BE that this function should not be | |
437 | ;; executed on a narrowed buffer. | |
438 | (if pos (goto-char pos)) | |
439 | (forward-line 0) | |
440 | (while (and (> (point) (point-min)) | |
441 | (eq (char-before (1- (point))) ?\\)) | |
442 | (forward-line -1)) | |
443 | (point)) | |
444 | ||
445 | (defun c-awk-end-of-logical-line (&optional pos) | |
446 | ;; Go forward to the end of the (apparent) current logical line (or the end of | |
447 | ;; the line containing POS), returning the buffer position of that point. I.e., | |
448 | ;; go to the end of the next line which doesn't have an escaped EOL. | |
449 | ;; | |
450 | ;; This is guaranteed to be "safe" for syntactic analysis, i.e. outwith any | |
451 | ;; comment, string or regexp. IT MAY WELL BE that this function should not be | |
452 | ;; executed on a narrowed buffer. | |
453 | (if pos (goto-char pos)) | |
454 | (end-of-line) | |
455 | (while (and (< (point) (point-max)) | |
456 | (eq (char-before) ?\\)) | |
457 | (end-of-line 2)) | |
458 | (point)) | |
459 | ||
460 | ;; N.B. In the following regexps, an EOL is either \n OR \r. This is because | |
461 | ;; Emacs has in the past used \r to mark hidden lines in some fashion (and | |
462 | ;; maybe still does). | |
463 | ||
464 | (defconst c-awk-esc-pair-re "\\\\\\(.\\|\n\\|\r\\|\\'\\)") | |
465 | ;; Matches any escaped (with \) character-pair, including an escaped newline. | |
466 | (defconst c-awk-comment-without-nl "#.*") | |
467 | ;; Matches an AWK comment, not including the terminating NL (if any). Note | |
468 | ;; that the "enclosing" (elisp) regexp must ensure the # is real. | |
469 | (defconst c-awk-nl-or-eob "\\(\n\\|\r\\|\\'\\)") | |
470 | ;; Matches a newline, or the end of buffer. | |
471 | ||
472 | ;; "Space" regular expressions. | |
473 | (defconst c-awk-escaped-nl "\\\\[\n\r]") | |
474 | ;; Matches an escaped newline. | |
475 | (defconst c-awk-escaped-nls* (concat "\\(" c-awk-escaped-nl "\\)*")) | |
476 | ;; Matches a possibly empty sequence of escaped newlines. Used in | |
477 | ;; awk-font-lock-keywords. | |
478 | ;; (defconst c-awk-escaped-nls*-with-space* | |
479 | ;; (concat "\\(" c-awk-escaped-nls* "\\|" "[ \t]+" "\\)*")) | |
480 | ;; The above RE was very slow. It's runtime was doubling with each additional | |
481 | ;; space :-( Reformulate it as below: | |
482 | (defconst c-awk-escaped-nls*-with-space* | |
483 | (concat "\\(" c-awk-escaped-nl "\\|" "[ \t]" "\\)*")) | |
484 | ;; Matches a possibly empty sequence of escaped newlines with optional | |
485 | ;; interspersed spaces and tabs. Used in awk-font-lock-keywords. | |
486 | ||
487 | ;; REGEXPS FOR "HARMLESS" STRINGS/LINES. | |
488 | (defconst c-awk-harmless-char-re "[^_#/\"\\\\\n\r]") | |
489 | ;; Matches any character but a _, #, /, ", \, or newline. N.B. _" starts a | |
490 | ;; localisation string in gawk 3.1 | |
491 | (defconst c-awk-harmless-_ "_\\([^\"]\\|\\'\\)") | |
492 | ;; Matches an underline NOT followed by ". | |
493 | (defconst c-awk-harmless-string*-re | |
494 | (concat "\\(" c-awk-harmless-char-re "\\|" c-awk-esc-pair-re "\\|" c-awk-harmless-_ "\\)*")) | |
495 | ;; Matches a (possibly empty) sequence of chars without unescaped /, ", \, | |
496 | ;; #, or newlines. | |
497 | (defconst c-awk-harmless-string*-here-re | |
498 | (concat "\\=" c-awk-harmless-string*-re)) | |
499 | ;; Matches the (possibly empty) sequence of chars without unescaped /, ", \, | |
500 | ;; at point. | |
501 | (defconst c-awk-harmless-line-re | |
502 | (concat c-awk-harmless-string*-re | |
503 | "\\(" c-awk-comment-without-nl "\\)?" c-awk-nl-or-eob)) | |
504 | ;; Matches (the tail of) an AWK \"logical\" line not containing an unescaped | |
505 | ;; " or /. "logical" means "possibly containing escaped newlines". A comment | |
506 | ;; is matched as part of the line even if it contains a " or a /. The End of | |
507 | ;; buffer is also an end of line. | |
508 | (defconst c-awk-harmless-lines+-here-re | |
509 | (concat "\\=\\(" c-awk-harmless-line-re "\\)+")) | |
510 | ;; Matches a sequence of (at least one) \"harmless-line\" at point. | |
511 | ||
512 | ||
513 | ;; REGEXPS FOR AWK STRINGS. | |
514 | (defconst c-awk-string-ch-re "[^\"\\\n\r]") | |
515 | ;; Matches any character which can appear unescaped in a string. | |
516 | (defconst c-awk-string-innards-re | |
517 | (concat "\\(" c-awk-string-ch-re "\\|" c-awk-esc-pair-re "\\)*")) | |
518 | ;; Matches the inside of an AWK string (i.e. without the enclosing quotes). | |
519 | (defconst c-awk-string-without-end-here-re | |
520 | (concat "\\=_?\"" c-awk-string-innards-re)) | |
521 | ;; Matches an AWK string at point up to, but not including, any terminator. | |
522 | ;; A gawk 3.1+ string may look like _"localisable string". | |
523 | ||
524 | ;; REGEXPS FOR AWK REGEXPS. | |
525 | (defconst c-awk-regexp-normal-re "[^[/\\\n\r]") | |
526 | ;; Matches any AWK regexp character which doesn't require special analysis. | |
527 | (defconst c-awk-escaped-newlines*-re "\\(\\\\[\n\r]\\)*") | |
528 | ;; Matches a (possibly empty) sequence of escaped newlines. | |
3efc2cd7 MS |
529 | |
530 | ;; NOTE: In what follows, "[asdf]" in a regexp will be called a "character | |
531 | ;; list", and "[:alpha:]" inside a character list will be known as a | |
532 | ;; "character class". These terms for these things vary between regexp | |
533 | ;; descriptions . | |
d9e94c22 | 534 | (defconst c-awk-regexp-char-class-re |
3efc2cd7 MS |
535 | "\\[:[a-z]+:\\]") |
536 | ;; Matches a character class spec (e.g. [:alpha:]). | |
537 | (defconst c-awk-regexp-char-list-re | |
d9e94c22 | 538 | (concat "\\[" c-awk-escaped-newlines*-re "^?" c-awk-escaped-newlines*-re "]?" |
3efc2cd7 MS |
539 | "\\(" c-awk-esc-pair-re "\\|" c-awk-regexp-char-class-re |
540 | "\\|" "[^]\n\r]" "\\)*" "\\(]\\|$\\)")) | |
541 | ;; Matches a regexp char list, up to (but not including) EOL if the ] is | |
d9e94c22 MS |
542 | ;; missing. |
543 | (defconst c-awk-regexp-innards-re | |
3efc2cd7 | 544 | (concat "\\(" c-awk-esc-pair-re "\\|" c-awk-regexp-char-list-re |
d9e94c22 MS |
545 | "\\|" c-awk-regexp-normal-re "\\)*")) |
546 | ;; Matches the inside of an AWK regexp (i.e. without the enclosing /s) | |
547 | (defconst c-awk-regexp-without-end-re | |
548 | (concat "/" c-awk-regexp-innards-re)) | |
549 | ;; Matches an AWK regexp up to, but not including, any terminating /. | |
550 | ||
551 | ;; REGEXPS used for scanning an AWK buffer in order to decide IF A '/' IS A | |
552 | ;; REGEXP OPENER OR A DIVISION SIGN. By "state" in the following is meant | |
553 | ;; whether a '/' at the current position would by a regexp opener or a | |
554 | ;; division sign. | |
555 | (defconst c-awk-neutral-re | |
556 | ; "\\([{}@` \t]\\|\\+\\+\\|--\\|\\\\.\\)+") ; changed, 2003/6/7 | |
557 | "\\([{}@` \t]\\|\\+\\+\\|--\\|\\\\.\\)") | |
558 | ;; A "neutral" char(pair). Doesn't change the "state" of a subsequent /. | |
559 | ;; This is space/tab, braces, an auto-increment/decrement operator or an | |
560 | ;; escaped character. Or one of the (illegal) characters @ or `. But NOT an | |
3efc2cd7 | 561 | ;; end of line (even if escaped). |
d9e94c22 MS |
562 | (defconst c-awk-neutrals*-re |
563 | (concat "\\(" c-awk-neutral-re "\\)*")) | |
564 | ;; A (possibly empty) string of neutral characters (or character pairs). | |
565 | (defconst c-awk-var-num-ket-re "[]\)0-9a-zA-Z_$.\x80-\xff]+") | |
566 | ;; Matches a char which is a constituent of a variable or number, or a ket | |
567 | ;; (i.e. closing bracKET), round or square. Assume that all characters \x80 to | |
568 | ;; \xff are "letters". | |
569 | (defconst c-awk-div-sign-re | |
570 | (concat c-awk-var-num-ket-re c-awk-neutrals*-re "/")) | |
571 | ;; Will match a piece of AWK buffer ending in / which is a division sign, in | |
572 | ;; a context where an immediate / would be a regexp bracket. It follows a | |
573 | ;; variable or number (with optional intervening "neutral" characters). This | |
574 | ;; will only work when there won't be a preceding " or / before the sought / | |
575 | ;; to foul things up. | |
576 | (defconst c-awk-non-arith-op-bra-re | |
577 | "[[\(&=:!><,?;'~|]") | |
578 | ;; Matches an openeing BRAcket ,round or square, or any operator character | |
579 | ;; apart from +,-,/,*,%. For the purpose at hand (detecting a / which is a | |
580 | ;; regexp bracket) these arith ops are unnecessary and a pain, because of "++" | |
581 | ;; and "--". | |
582 | (defconst c-awk-regexp-sign-re | |
583 | (concat c-awk-non-arith-op-bra-re c-awk-neutrals*-re "/")) | |
584 | ;; Will match a piece of AWK buffer ending in / which is an opening regexp | |
585 | ;; bracket, in a context where an immediate / would be a division sign. This | |
586 | ;; will only work when there won't be a preceding " or / before the sought / | |
587 | ;; to foul things up. | |
588 | ||
589 | ;; ACM, 2002/02/15: The idea of the next function is to put the "Error font" | |
590 | ;; on strings/regexps which are missing their closing delimiter. | |
591 | ;; 2002/4/28. The default syntax for / has been changed from "string" to | |
592 | ;; "punctuation", to reduce hassle when this character appears within a string | |
593 | ;; or comment. | |
594 | ||
595 | (defun c-awk-set-string-regexp-syntax-table-properties (beg end) | |
596 | ;; BEG and END bracket a (possibly unterminated) string or regexp. The | |
597 | ;; opening delimiter is after BEG, and the closing delimiter, IF ANY, is AFTER | |
598 | ;; END. Set the appropriate syntax-table properties on the delimiters and | |
599 | ;; contents of this string/regex. | |
600 | ;; | |
601 | ;; "String" here can also mean a gawk 3.1 "localizable" string which starts | |
602 | ;; with _". In this case, we step over the _ and ignore it; It will get it's | |
603 | ;; font from an entry in awk-font-lock-keywords. | |
604 | ;; | |
605 | ;; If the closing delimiter is missing (i.e., there is an EOL there) set the | |
606 | ;; STRING-FENCE property on the opening " or / and closing EOL. | |
607 | (if (eq (char-after beg) ?_) (setq beg (1+ beg))) | |
608 | ||
609 | ;; First put the properties on the delimiters. | |
610 | (cond ((eq end (point-max)) ; string/regexp terminated by EOB | |
611 | (put-text-property beg (1+ beg) 'syntax-table '(15))) ; (15) = "string fence" | |
612 | ((/= (char-after beg) (char-after end)) ; missing end delimiter | |
613 | (put-text-property beg (1+ beg) 'syntax-table '(15)) | |
614 | (put-text-property end (1+ end) 'syntax-table '(15))) | |
615 | ((eq (char-after beg) ?/) ; Properly bracketed regexp | |
616 | (put-text-property beg (1+ beg) 'syntax-table '(7)) ; (7) = "string" | |
617 | (put-text-property end (1+ end) 'syntax-table '(7))) | |
618 | (t)) ; Properly bracketed string: Nothing to do. | |
619 | ;; Now change the properties of any escaped "s in the string to punctuation. | |
620 | (save-excursion | |
621 | (goto-char (1+ beg)) | |
622 | (or (eobp) | |
623 | (while (search-forward "\"" end t) | |
624 | (put-text-property (1- (point)) (point) 'syntax-table '(1)))))) | |
625 | ||
626 | (defun c-awk-syntax-tablify-string () | |
627 | ;; Point is at the opening " or _" of a string. Set the syntax-table | |
628 | ;; properties on this string, leaving point just after the string. | |
629 | ;; | |
630 | ;; The result is nil if a / immediately after the string would be a regexp | |
631 | ;; opener, t if it would be a division sign. | |
632 | (search-forward-regexp c-awk-string-without-end-here-re nil t) ; a (possibly unterminated) string | |
633 | (c-awk-set-string-regexp-syntax-table-properties | |
634 | (match-beginning 0) (match-end 0)) | |
635 | (cond ((looking-at "\"") | |
636 | (forward-char) | |
637 | t) ; In AWK, ("15" / 5) gives 3 ;-) | |
638 | ((looking-at "[\n\r]") ; Unterminated string with EOL. | |
639 | (forward-char) | |
640 | nil) ; / on next line would start a regexp | |
641 | (t nil))) ; Unterminated string at EOB | |
642 | ||
643 | (defun c-awk-syntax-tablify-/ (anchor anchor-state-/div) | |
644 | ;; Point is at a /. Determine whether this is a division sign or a regexp | |
645 | ;; opener, and if the latter, apply syntax-table properties to the entire | |
646 | ;; regexp. Point is left immediately after the division sign or regexp, as | |
647 | ;; the case may be. | |
648 | ;; | |
649 | ;; ANCHOR-STATE-/DIV identifies whether a / at ANCHOR would have been a | |
650 | ;; division sign (value t) or a regexp opener (value nil). The idea is that | |
651 | ;; we analyse the line from ANCHOR up till point to determine what the / at | |
652 | ;; point is. | |
653 | ;; | |
654 | ;; The result is what ANCHOR-STATE-/DIV (see above) is where point is left. | |
655 | (let ((/point (point))) | |
656 | (goto-char anchor) | |
657 | ;; Analyse the line to find out what the / is. | |
658 | (if (if anchor-state-/div | |
659 | (not (search-forward-regexp c-awk-regexp-sign-re (1+ /point) t)) | |
660 | (search-forward-regexp c-awk-div-sign-re (1+ /point) t)) | |
661 | ;; A division sign. | |
662 | (progn (goto-char (1+ /point)) nil) | |
663 | ;; A regexp opener | |
664 | ;; Jump over the regexp innards, setting the match data. | |
665 | (goto-char /point) | |
666 | (search-forward-regexp c-awk-regexp-without-end-re) | |
667 | (c-awk-set-string-regexp-syntax-table-properties | |
668 | (match-beginning 0) (match-end 0)) | |
669 | (cond ((looking-at "/") ; Terminating / | |
670 | (forward-char) | |
671 | t) | |
672 | ((looking-at "[\n\r]") ; Incomplete regexp terminated by EOL | |
673 | (forward-char) | |
674 | nil) ; / on next line would start another regexp | |
675 | (t nil))))) ; Unterminated regexp at EOB | |
676 | ||
677 | (defun c-awk-set-syntax-table-properties (lim) | |
678 | ;; Scan the buffer text between point and LIM, setting (and clearing) the | |
679 | ;; syntax-table property where necessary. | |
680 | ;; | |
681 | ;; This function is designed to be called as the FUNCTION in a MATCHER in | |
682 | ;; font-lock-syntactic-keywords, and it always returns NIL (to inhibit | |
683 | ;; repeated calls from font-lock: See elisp info page "Search-based | |
684 | ;; Fontification"). It also gets called, with a bit of glue, from | |
685 | ;; after-change-functions when font-lock isn't active. Point is left | |
686 | ;; "undefined" after this function exits. THE BUFFER SHOULD HAVE BEEN | |
687 | ;; WIDENED, AND ANY PRECIOUS MATCH-DATA SAVED BEFORE CALLING THIS ROUTINE. | |
688 | ;; | |
689 | ;; We need to set/clear the syntax-table property on: | |
690 | ;; (i) / - It is set to "string" on a / which is the opening or closing | |
691 | ;; delimiter of the properly terminated regexp (and left unset on a | |
692 | ;; division sign). | |
693 | ;; (ii) the opener of an unterminated string/regexp, we set the property | |
694 | ;; "generic string delimiter" on both the opening " or / and the end of the | |
695 | ;; line where the closing delimiter is missing. | |
696 | ;; (iii) "s inside strings/regexps (these will all be escaped "s). They are | |
697 | ;; given the property "punctuation". This will later allow other routines | |
698 | ;; to use the regexp "\\S\"*" to skip over the string innards. | |
699 | ;; (iv) Inside a comment, all syntax-table properties are cleared. | |
700 | (let (anchor | |
701 | (anchor-state-/div nil)) ; t means a following / would be a div sign. | |
702 | (c-awk-beginning-of-logical-line) ; ACM 2002/7/21. This is probably redundant. | |
703 | (put-text-property (point) lim 'syntax-table nil) | |
704 | (search-forward-regexp c-awk-harmless-lines+-here-re nil t) ; skip harmless lines. | |
705 | ||
706 | ;; Once round the next loop for each string, regexp, or div sign | |
707 | (while (< (point) lim) | |
708 | (setq anchor (point)) | |
709 | (search-forward-regexp c-awk-harmless-string*-here-re nil t) | |
710 | ;; We are now looking at either a " or a /. | |
711 | ;; Do our thing on the string, regexp or divsion sign. | |
712 | (setq anchor-state-/div | |
713 | (if (looking-at "_?\"") | |
714 | (c-awk-syntax-tablify-string) | |
715 | (c-awk-syntax-tablify-/ anchor anchor-state-/div))) | |
716 | ||
717 | ;; Skip any further "harmless" lines before the next tricky one. | |
718 | (if (search-forward-regexp c-awk-harmless-lines+-here-re nil t) | |
719 | (setq anchor-state-/div nil))) | |
720 | nil)) | |
721 | ||
722 | ||
723 | ;; ACM, 2002/07/21: Thoughts: We need an AWK Mode after-change function to set | |
724 | ;; the syntax-table properties even when font-lock isn't enabled, for the | |
725 | ;; subsequent use of movement functions, etc. However, it seems that if font | |
726 | ;; lock _is_ enabled, we can always leave it to do the job. | |
727 | (defvar c-awk-old-EOLL 0) | |
728 | (make-variable-buffer-local 'c-awk-old-EOLL) | |
729 | ;; End of logical line following the region which is about to be changed. Set | |
730 | ;; in c-awk-before-change and used in c-awk-after-change. | |
731 | ||
732 | (defun c-awk-before-change (beg end) | |
733 | ;; This function is called exclusively from the before-change-functions hook. | |
734 | ;; It does two things: Finds the end of the (logical) line on which END lies, | |
735 | ;; and clears c-awk-NL-prop text properties from this point onwards. | |
736 | (save-restriction | |
737 | (save-excursion | |
738 | (setq c-awk-old-EOLL (c-awk-end-of-logical-line end)) | |
739 | (c-save-buffer-state nil | |
740 | (c-awk-clear-NL-props end (point-max)))))) | |
741 | ||
742 | (defun c-awk-end-of-change-region (beg end old-len) | |
743 | ;; Find the end of the region which needs to be font-locked after a change. | |
744 | ;; This is the end of the logical line on which the change happened, either | |
745 | ;; as it was before the change, or as it is now, which ever is later. | |
746 | ;; N.B. point is left undefined. | |
747 | (max (+ (- c-awk-old-EOLL old-len) (- end beg)) | |
748 | (c-awk-end-of-logical-line end))) | |
749 | ||
750 | (defun c-awk-after-change (beg end old-len) | |
751 | ;; This function is called exclusively as an after-change function in | |
752 | ;; AWK Mode. It ensures that the syntax-table properties get set in the | |
753 | ;; changed region. However, if font-lock is enabled, this function does | |
754 | ;; nothing, since an enabled font-lock after-change function will always do | |
755 | ;; this. | |
756 | (unless (and (boundp 'font-lock-mode) font-lock-mode) | |
757 | (save-restriction | |
758 | (save-excursion | |
759 | (setq end (c-awk-end-of-change-region beg end old-len)) | |
760 | (c-awk-beginning-of-logical-line beg) | |
761 | (c-save-buffer-state nil ; So that read-only status isn't affected. | |
762 | ; (e.g. when first loading the buffer) | |
763 | (c-awk-set-syntax-table-properties end)))))) | |
764 | ||
765 | ;; ACM 2002/5/25. When font-locking is invoked by a buffer change, the region | |
766 | ;; specified by the font-lock after-change function must be expanded to | |
767 | ;; include ALL of any string or regexp within the region. The simplest way to | |
768 | ;; do this in practice is to use the beginning/end-of-logical-line functions. | |
769 | ;; Don't overlook the possibility of the buffer change being the "recapturing" | |
770 | ;; of a previously escaped newline. | |
771 | (defmacro c-awk-advise-fl-for-awk-region (function) | |
772 | `(defadvice ,function (before get-awk-region activate) | |
773 | ;; When font-locking an AWK Mode buffer, make sure that any string/regexp is | |
774 | ;; completely font-locked. | |
775 | (when (eq major-mode 'awk-mode) | |
776 | (save-excursion | |
777 | (ad-set-arg 1 (c-awk-end-of-change-region | |
778 | (ad-get-arg 0) ; beg | |
779 | (ad-get-arg 1) ; end | |
780 | (ad-get-arg 2))) ; old-len | |
781 | (ad-set-arg 0 (c-awk-beginning-of-logical-line (ad-get-arg 0))))))) | |
782 | ||
783 | (c-awk-advise-fl-for-awk-region font-lock-after-change-function) | |
784 | (c-awk-advise-fl-for-awk-region jit-lock-after-change) | |
785 | (c-awk-advise-fl-for-awk-region lazy-lock-defer-rest-after-change) | |
786 | (c-awk-advise-fl-for-awk-region lazy-lock-defer-line-after-change) | |
787 | ||
788 | ;; ACM 2002/9/29. Functions for C-M-a and C-M-e | |
789 | ||
790 | (defconst c-awk-terminated-regexp-or-string-here-re "\\=\\s\"\\S\"*\\s\"") | |
791 | ;; Matches a terminated string/regexp (utilising syntax-table properties). | |
792 | ||
793 | (defconst c-awk-unterminated-regexp-or-string-here-re "\\=\\s|\\S|*$") | |
794 | ;; Matches an unterminated string/regexp, NOT including the eol at the end. | |
795 | ||
796 | (defconst c-awk-harmless-pattern-characters* | |
797 | (concat "\\([^{;#/\"\\\\\n\r]\\|" c-awk-esc-pair-re "\\)*")) | |
798 | ;; Matches any "harmless" character in a pattern or an escaped character pair. | |
799 | ||
800 | (defun c-awk-beginning-of-defun (&optional arg) | |
801 | "Move backward to the beginning of an AWK \"defun\". With ARG, do it that | |
802 | many times. Negative arg -N means move forward to Nth following beginning of | |
803 | defun. Returns t unless search stops due to beginning or end of buffer. | |
804 | ||
805 | By a \"defun\" is meant either a pattern-action pair or a function. The start | |
806 | of a defun is recognised as code starting at column zero which is neither a | |
807 | closing brace nor a comment nor a continuation of the previous line. Unlike | |
808 | in some other modes, having an opening brace at column 0 is neither necessary | |
809 | nor helpful." | |
810 | (interactive "p") | |
811 | (save-match-data | |
812 | (c-save-buffer-state ; ensures the buffer is writable. | |
813 | nil | |
814 | (let ((found t)) ; Has the most recent regexp search found b-of-defun? | |
815 | (if (>= arg 0) | |
816 | ;; Go back one defun each time round the following loop. (For +ve arg) | |
817 | (while (and found (> arg 0) (not (eq (point) (point-min)))) | |
818 | ;; Go back one "candidate" each time round the next loop until one | |
819 | ;; is genuinely a beginning-of-defun. | |
820 | (while (and (setq found (search-backward-regexp | |
821 | "^[^#} \t\n\r]" (point-min) 'stop-at-limit)) | |
822 | (not (memq (c-awk-get-NL-prop-prev-line) '(?\; ?\#))))) | |
823 | (setq arg (1- arg))) | |
824 | ;; The same for a -ve arg. | |
825 | (if (not (eq (point) (point-max))) (forward-char 1)) | |
826 | (while (and found (< arg 0) (not (eq (point) (point-max)))) ; The same for -ve arg. | |
827 | (while (and (setq found (search-forward-regexp | |
828 | "^[^#} \t\n\r]" (point-max) 'stop-at-limit)) | |
829 | (not (memq (c-awk-get-NL-prop-prev-line) '(?\; ?\#))))) | |
830 | (setq arg (1+ arg))) | |
831 | (if found (goto-char (match-beginning 0)))) | |
832 | (eq arg 0))))) | |
833 | ||
834 | (defun c-awk-forward-awk-pattern () | |
835 | ;; Point is at the start of an AWK pattern (which may be null) or function | |
836 | ;; declaration. Move to the pattern's end, and past any trailing space or | |
837 | ;; comment. Typically, we stop at the { which denotes the corresponding AWK | |
838 | ;; action/function body. Otherwise we stop at the EOL (or ;) marking the | |
839 | ;; absence of an explicit action. | |
840 | (while | |
841 | (progn | |
842 | (search-forward-regexp c-awk-harmless-pattern-characters*) | |
843 | (if (looking-at "#") (end-of-line)) | |
844 | (cond | |
845 | ((eobp) nil) | |
846 | ((looking-at "[{;]") nil) ; We've finished! | |
847 | ((eolp) | |
848 | (if (c-awk-cur-line-incomplete-p) | |
849 | (forward-line) ; returns non-nil | |
850 | nil)) | |
851 | ((search-forward-regexp c-awk-terminated-regexp-or-string-here-re nil t)) | |
852 | ((search-forward-regexp c-awk-unterminated-regexp-or-string-here-re nil t)) | |
853 | ((looking-at "/") (forward-char) t))))) ; division sign. | |
854 | ||
855 | (defun c-awk-end-of-defun1 () | |
856 | ;; point is at the start of a "defun". Move to its end. Return end position. | |
857 | (c-awk-forward-awk-pattern) | |
858 | (cond | |
859 | ((looking-at "{") (goto-char (scan-sexps (point) 1))) | |
860 | ((looking-at ";") (forward-char)) | |
861 | ((eolp)) | |
862 | (t (error "c-awk-end-of-defun1: Failure of c-awk-forward-awk-pattern"))) | |
863 | (point)) | |
864 | ||
865 | (defun c-awk-beginning-of-defun-p () | |
866 | ;; Are we already at the beginning of a defun? (i.e. at code in column 0 | |
867 | ;; which isn't a }, and isn't a continuation line of any sort. | |
868 | (and (looking-at "^[^#} \t\n\r]") | |
869 | (not (c-awk-prev-line-incomplete-p)))) | |
870 | ||
871 | (defun c-awk-end-of-defun (&optional arg) | |
872 | "Move forward to next end of defun. With argument, do it that many times. | |
873 | Negative argument -N means move back to Nth preceding end of defun. | |
874 | ||
875 | An end of a defun occurs right after the closing brace that matches the | |
876 | opening brace at its start, or immediately after the AWK pattern when there is | |
877 | no explicit action; see function `c-awk-beginning-of-defun'." | |
878 | (interactive "p") | |
879 | (or arg (setq arg 1)) | |
880 | (save-match-data | |
881 | (c-save-buffer-state | |
882 | nil | |
883 | (let ((start-point (point)) end-point) | |
884 | ;; Strategy: (For +ve ARG): If we're not already at a beginning-of-defun, | |
885 | ;; move backwards to one. | |
886 | ;; Repeat [(i) move forward to end-of-current-defun (see below); | |
887 | ;; (ii) If this isn't it, move forward to beginning-of-defun]. | |
888 | ;; We start counting ARG only when step (i) has passed the original point. | |
889 | (when (> arg 0) | |
890 | ;; Try to move back to a beginning-of-defun, if not already at one. | |
891 | (if (not (c-awk-beginning-of-defun-p)) | |
892 | (when (not (c-awk-beginning-of-defun 1)) ; No bo-defun before point. | |
893 | (goto-char start-point) | |
894 | (c-awk-beginning-of-defun -1))) ; if this fails, we're at EOB, tough! | |
895 | ;; Now count forward, one defun at a time | |
896 | (while (and (not (eobp)) | |
897 | (c-awk-end-of-defun1) | |
898 | (if (> (point) start-point) (setq arg (1- arg)) t) | |
899 | (> arg 0) | |
900 | (c-awk-beginning-of-defun -1)))) | |
901 | ||
902 | (when (< arg 0) | |
903 | (setq end-point start-point) | |
904 | (while (and (not (bobp)) | |
905 | (c-awk-beginning-of-defun 1) | |
906 | (if (< (setq end-point (if (bobp) (point) | |
907 | (save-excursion (c-awk-end-of-defun1)))) | |
908 | start-point) | |
909 | (setq arg (1+ arg)) t) | |
910 | (< arg 0))) | |
911 | (goto-char (min start-point end-point))))))) | |
912 | ||
913 | (cc-provide 'cc-awk) ; Changed from 'awk-mode, ACM 2002/5/21 | |
ab5796a9 MB |
914 | |
915 | ;;; arch-tag: c4836289-3aa4-4a59-9934-9ccc2bacccf3 | |
d9e94c22 | 916 | ;;; awk-mode.el ends here |