Commit | Line | Data |
---|---|---|
d9e94c22 MS |
1 | ;;; cc-awk.el --- AWK specific code within cc-mode. |
2 | ||
ab422c4d PE |
3 | ;; Copyright (C) 1988, 1994, 1996, 2000-2013 Free Software Foundation, |
4 | ;; Inc. | |
d9e94c22 | 5 | |
3efc2cd7 | 6 | ;; Author: Alan Mackenzie <acm@muc.de> (originally based on awk-mode.el) |
d9e94c22 MS |
7 | ;; Maintainer: FSF |
8 | ;; Keywords: AWK, cc-mode, unix, languages | |
bd78fa1d | 9 | ;; Package: cc-mode |
d9e94c22 MS |
10 | |
11 | ;; This file is part of GNU Emacs. | |
12 | ||
b1fc2b50 | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
d9e94c22 | 14 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
d9e94c22 MS |
17 | |
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
b1fc2b50 | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
d9e94c22 MS |
25 | |
26 | ;;; Commentary: | |
27 | ||
28 | ;; This file contains (most of) the adaptations to cc-mode required for the | |
29 | ;; integration of AWK Mode. | |
6772c8e1 | 30 | ;; It is organized thusly, the sections being separated by page breaks: |
d9e94c22 | 31 | ;; 1. The AWK Mode syntax table. |
6772c8e1 | 32 | ;; 2. Regular expressions for analyzing AWK code. |
0386b551 | 33 | ;; 3. Indentation calculation stuff ("c-awk-NL-prop text-property"). |
3c0ab532 | 34 | ;; 4. Syntax-table property/font-locking stuff, including the |
d9e94c22 | 35 | ;; font-lock-keywords setting. |
0386b551 AM |
36 | ;; 5. The AWK Mode before/after-change-functions. |
37 | ;; 6. AWK Mode specific versions of commands like beginning-of-defun. | |
d9e94c22 MS |
38 | ;; The AWK Mode keymap, abbreviation table, and the mode function itself are |
39 | ;; in cc-mode.el. | |
40 | ||
41 | ;;; Code: | |
42 | ||
43 | (eval-when-compile | |
44 | (let ((load-path | |
45 | (if (and (boundp 'byte-compile-dest-file) | |
46 | (stringp byte-compile-dest-file)) | |
47 | (cons (file-name-directory byte-compile-dest-file) load-path) | |
48 | load-path))) | |
49 | (load "cc-bytecomp" nil t))) | |
50 | ||
51 | (cc-require 'cc-defs) | |
52 | ||
53 | ;; Silence the byte compiler. | |
54 | (cc-bytecomp-defvar font-lock-mode) ; Checked with boundp before use. | |
88a46e21 AM |
55 | (cc-bytecomp-defvar c-new-BEG) |
56 | (cc-bytecomp-defvar c-new-END) | |
d9e94c22 MS |
57 | |
58 | ;; Some functions in cc-engine that are used below. There's a cyclic | |
59 | ;; dependency so it can't be required here. (Perhaps some functions | |
60 | ;; could be moved to cc-engine to avoid it.) | |
61 | (cc-bytecomp-defun c-backward-token-1) | |
62 | (cc-bytecomp-defun c-beginning-of-statement-1) | |
63 | (cc-bytecomp-defun c-backward-sws) | |
64 | ||
65 | (defvar awk-mode-syntax-table | |
66 | (let ((st (make-syntax-table))) | |
67 | (modify-syntax-entry ?\\ "\\" st) | |
68 | (modify-syntax-entry ?\n "> " st) | |
69 | (modify-syntax-entry ?\r "> " st) | |
70 | (modify-syntax-entry ?\f "> " st) | |
71 | (modify-syntax-entry ?\# "< " st) | |
72 | ;; / can delimit regexes or be a division operator. By default we assume | |
73 | ;; that it is a division sign, and fix the regexp operator cases with | |
74 | ;; `font-lock-syntactic-keywords'. | |
0d26e0b6 | 75 | (modify-syntax-entry ?/ "." st) ; ACM 2002/4/27. |
d9e94c22 MS |
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 | (modify-syntax-entry ?| "." st) | |
85 | (modify-syntax-entry ?_ "_" st) | |
86 | (modify-syntax-entry ?\' "." st) | |
87 | st) | |
88 | "Syntax table in use in AWK Mode buffers.") | |
89 | ||
0386b551 AM |
90 | \f |
91 | ;; This section defines regular expressions used in the analysis of AWK code. | |
92 | ||
93 | ;; N.B. In the following regexps, an EOL is either \n OR \r. This is because | |
94 | ;; Emacs has in the past used \r to mark hidden lines in some fashion (and | |
95 | ;; maybe still does). | |
96 | ||
97 | (defconst c-awk-esc-pair-re "\\\\\\(.\\|\n\\|\r\\|\\'\\)") | |
98 | ;; Matches any escaped (with \) character-pair, including an escaped newline. | |
99 | (defconst c-awk-non-eol-esc-pair-re "\\\\\\(.\\|\\'\\)") | |
100 | ;; Matches any escaped (with \) character-pair, apart from an escaped newline. | |
101 | (defconst c-awk-comment-without-nl "#.*") | |
102 | ;; Matches an AWK comment, not including the terminating NL (if any). Note | |
103 | ;; that the "enclosing" (elisp) regexp must ensure the # is real. | |
104 | (defconst c-awk-nl-or-eob "\\(\n\\|\r\\|\\'\\)") | |
105 | ;; Matches a newline, or the end of buffer. | |
106 | ||
107 | ;; "Space" regular expressions. | |
108 | (eval-and-compile | |
109 | (defconst c-awk-escaped-nl "\\\\[\n\r]")) | |
110 | ;; Matches an escaped newline. | |
ca331935 CY |
111 | (eval-and-compile |
112 | (defconst c-awk-escaped-nls* (concat "\\(" c-awk-escaped-nl "\\)*"))) | |
0386b551 AM |
113 | ;; Matches a possibly empty sequence of escaped newlines. Used in |
114 | ;; awk-font-lock-keywords. | |
115 | ;; (defconst c-awk-escaped-nls*-with-space* | |
116 | ;; (concat "\\(" c-awk-escaped-nls* "\\|" "[ \t]+" "\\)*")) | |
117 | ;; The above RE was very slow. It's runtime was doubling with each additional | |
118 | ;; space :-( Reformulate it as below: | |
119 | (eval-and-compile | |
120 | (defconst c-awk-escaped-nls*-with-space* | |
121 | (concat "\\(" c-awk-escaped-nl "\\|" "[ \t]" "\\)*"))) | |
122 | ;; Matches a possibly empty sequence of escaped newlines with optional | |
123 | ;; interspersed spaces and tabs. Used in awk-font-lock-keywords. | |
124 | (defconst c-awk-blank-or-comment-line-re | |
125 | (concat "[ \t]*\\(#\\|\\\\?$\\)")) | |
126 | ;; Matche (the tail of) a line containing at most either a comment or an | |
127 | ;; escaped EOL. | |
128 | ||
129 | ;; REGEXPS FOR "HARMLESS" STRINGS/LINES. | |
0386b551 AM |
130 | (defconst c-awk-harmless-_ "_\\([^\"]\\|\\'\\)") |
131 | ;; Matches an underline NOT followed by ". | |
953e5b8c AM |
132 | (defconst c-awk-harmless-char-re "[^_#/\"{}();\\\\\n\r]") |
133 | ;; Matches any character not significant in the state machine applying | |
134 | ;; syntax-table properties to "s and /s. | |
0386b551 AM |
135 | (defconst c-awk-harmless-string*-re |
136 | (concat "\\(" c-awk-harmless-char-re "\\|" c-awk-esc-pair-re "\\|" c-awk-harmless-_ "\\)*")) | |
953e5b8c AM |
137 | ;; Matches a (possibly empty) sequence of characters insignificant in the |
138 | ;; state machine applying syntax-table properties to "s and /s. | |
0386b551 AM |
139 | (defconst c-awk-harmless-string*-here-re |
140 | (concat "\\=" c-awk-harmless-string*-re)) | |
953e5b8c AM |
141 | ;; Matches the (possibly empty) sequence of "insignificant" chars at point. |
142 | ||
143 | (defconst c-awk-harmless-line-char-re "[^_#/\"\\\\\n\r]") | |
144 | ;; Matches any character but a _, #, /, ", \, or newline. N.B. _" starts a | |
d9c287e5 | 145 | ;; localization string in gawk 3.1 |
953e5b8c AM |
146 | (defconst c-awk-harmless-line-string*-re |
147 | (concat "\\(" c-awk-harmless-line-char-re "\\|" c-awk-esc-pair-re "\\|" c-awk-harmless-_ "\\)*")) | |
148 | ;; Matches a (possibly empty) sequence of chars without unescaped /, ", \, | |
149 | ;; #, or newlines. | |
0386b551 | 150 | (defconst c-awk-harmless-line-re |
953e5b8c AM |
151 | (concat c-awk-harmless-line-string*-re |
152 | "\\(" c-awk-comment-without-nl "\\)?" c-awk-nl-or-eob)) | |
0386b551 AM |
153 | ;; Matches (the tail of) an AWK \"logical\" line not containing an unescaped |
154 | ;; " or /. "logical" means "possibly containing escaped newlines". A comment | |
155 | ;; is matched as part of the line even if it contains a " or a /. The End of | |
156 | ;; buffer is also an end of line. | |
157 | (defconst c-awk-harmless-lines+-here-re | |
158 | (concat "\\=\\(" c-awk-harmless-line-re "\\)+")) | |
159 | ;; Matches a sequence of (at least one) \"harmless-line\" at point. | |
160 | ||
161 | ||
162 | ;; REGEXPS FOR AWK STRINGS. | |
163 | (defconst c-awk-string-ch-re "[^\"\\\n\r]") | |
164 | ;; Matches any character which can appear unescaped in a string. | |
165 | (defconst c-awk-string-innards-re | |
166 | (concat "\\(" c-awk-string-ch-re "\\|" c-awk-esc-pair-re "\\)*")) | |
167 | ;; Matches the inside of an AWK string (i.e. without the enclosing quotes). | |
168 | (defconst c-awk-string-without-end-here-re | |
169 | (concat "\\=_?\"" c-awk-string-innards-re)) | |
170 | ;; Matches an AWK string at point up to, but not including, any terminator. | |
53964682 | 171 | ;; A gawk 3.1+ string may look like _"localizable string". |
0386b551 AM |
172 | (defconst c-awk-one-line-possibly-open-string-re |
173 | (concat "\"\\(" c-awk-string-ch-re "\\|" c-awk-non-eol-esc-pair-re "\\)*" | |
174 | "\\(\"\\|\\\\?$\\|\\'\\)")) | |
175 | ||
176 | ;; REGEXPS FOR AWK REGEXPS. | |
177 | (defconst c-awk-regexp-normal-re "[^[/\\\n\r]") | |
178 | ;; Matches any AWK regexp character which doesn't require special analysis. | |
179 | (defconst c-awk-escaped-newlines*-re "\\(\\\\[\n\r]\\)*") | |
180 | ;; Matches a (possibly empty) sequence of escaped newlines. | |
181 | ||
182 | ;; NOTE: In what follows, "[asdf]" in a regexp will be called a "character | |
183 | ;; list", and "[:alpha:]" inside a character list will be known as a | |
184 | ;; "character class". These terms for these things vary between regexp | |
185 | ;; descriptions . | |
186 | (defconst c-awk-regexp-char-class-re | |
187 | "\\[:[a-z]+:\\]") | |
188 | ;; Matches a character class spec (e.g. [:alpha:]). | |
189 | (defconst c-awk-regexp-char-list-re | |
190 | (concat "\\[" c-awk-escaped-newlines*-re "^?" c-awk-escaped-newlines*-re "]?" | |
191 | "\\(" c-awk-esc-pair-re "\\|" c-awk-regexp-char-class-re | |
192 | "\\|" "[^]\n\r]" "\\)*" "\\(]\\|$\\)")) | |
193 | ;; Matches a regexp char list, up to (but not including) EOL if the ] is | |
194 | ;; missing. | |
195 | (defconst c-awk-regexp-one-line-possibly-open-char-list-re | |
196 | (concat "\\[\\]?\\(" c-awk-non-eol-esc-pair-re "\\|" "[^]\n\r]" "\\)*" | |
197 | "\\(]\\|\\\\?$\\|\\'\\)")) | |
198 | ;; Matches the head (or all) of a regexp char class, up to (but not | |
199 | ;; including) the first EOL. | |
200 | (defconst c-awk-regexp-innards-re | |
201 | (concat "\\(" c-awk-esc-pair-re "\\|" c-awk-regexp-char-list-re | |
202 | "\\|" c-awk-regexp-normal-re "\\)*")) | |
203 | ;; Matches the inside of an AWK regexp (i.e. without the enclosing /s) | |
204 | (defconst c-awk-regexp-without-end-re | |
205 | (concat "/" c-awk-regexp-innards-re)) | |
0d26e0b6 | 206 | ;; Matches an AWK regexp up to, but not including, any terminating /. |
0386b551 AM |
207 | (defconst c-awk-one-line-possibly-open-regexp-re |
208 | (concat "/\\(" c-awk-non-eol-esc-pair-re | |
209 | "\\|" c-awk-regexp-one-line-possibly-open-char-list-re | |
210 | "\\|" c-awk-regexp-normal-re "\\)*" | |
211 | "\\(/\\|\\\\?$\\|\\'\\)")) | |
212 | ;; Matches as much of the head of an AWK regexp which fits on one line, | |
213 | ;; possibly all of it. | |
214 | ||
215 | ;; REGEXPS used for scanning an AWK buffer in order to decide IF A '/' IS A | |
216 | ;; REGEXP OPENER OR A DIVISION SIGN. By "state" in the following is meant | |
217 | ;; whether a '/' at the current position would by a regexp opener or a | |
218 | ;; division sign. | |
219 | (defconst c-awk-neutral-re | |
220 | ; "\\([{}@` \t]\\|\\+\\+\\|--\\|\\\\.\\)+") ; changed, 2003/6/7 | |
953e5b8c | 221 | "\\([}@` \t]\\|\\+\\+\\|--\\|\\\\\\(.\\|[\n\r]\\)\\)") |
0386b551 | 222 | ;; A "neutral" char(pair). Doesn't change the "state" of a subsequent /. |
953e5b8c | 223 | ;; This is space/tab, close brace, an auto-increment/decrement operator or an |
cb5bf6ba | 224 | ;; escaped character. Or one of the (invalid) characters @ or `. But NOT an |
953e5b8c | 225 | ;; end of line (unless escaped). |
0386b551 AM |
226 | (defconst c-awk-neutrals*-re |
227 | (concat "\\(" c-awk-neutral-re "\\)*")) | |
228 | ;; A (possibly empty) string of neutral characters (or character pairs). | |
229 | (defconst c-awk-var-num-ket-re "[]\)0-9a-zA-Z_$.\x80-\xff]+") | |
230 | ;; Matches a char which is a constituent of a variable or number, or a ket | |
231 | ;; (i.e. closing bracKET), round or square. Assume that all characters \x80 to | |
232 | ;; \xff are "letters". | |
233 | (defconst c-awk-div-sign-re | |
234 | (concat c-awk-var-num-ket-re c-awk-neutrals*-re "/")) | |
235 | ;; Will match a piece of AWK buffer ending in / which is a division sign, in | |
236 | ;; a context where an immediate / would be a regexp bracket. It follows a | |
237 | ;; variable or number (with optional intervening "neutral" characters). This | |
238 | ;; will only work when there won't be a preceding " or / before the sought / | |
239 | ;; to foul things up. | |
240 | (defconst c-awk-non-arith-op-bra-re | |
953e5b8c AM |
241 | "[[\({&=:!><,?;'~|]") |
242 | ;; Matches an opening BRAcket (of any sort), or any operator character | |
0386b551 AM |
243 | ;; apart from +,-,/,*,%. For the purpose at hand (detecting a / which is a |
244 | ;; regexp bracket) these arith ops are unnecessary and a pain, because of "++" | |
245 | ;; and "--". | |
246 | (defconst c-awk-regexp-sign-re | |
247 | (concat c-awk-non-arith-op-bra-re c-awk-neutrals*-re "/")) | |
248 | ;; Will match a piece of AWK buffer ending in / which is an opening regexp | |
249 | ;; bracket, in a context where an immediate / would be a division sign. This | |
250 | ;; will only work when there won't be a preceding " or / before the sought / | |
251 | ;; to foul things up. | |
953e5b8c AM |
252 | (defconst c-awk-pre-exp-alphanum-kwd-re |
253 | (concat "\\(^\\|\\=\\|[^_\n\r]\\)\\<" | |
254 | (regexp-opt '("print" "return" "case") t) | |
255 | "\\>\\([^_\n\r]\\|$\\)")) | |
256 | ;; Matches all AWK keywords which can precede expressions (including | |
257 | ;; /regexp/). | |
258 | (defconst c-awk-kwd-regexp-sign-re | |
259 | (concat c-awk-pre-exp-alphanum-kwd-re c-awk-escaped-nls*-with-space* "/")) | |
260 | ;; Matches a piece of AWK buffer ending in <kwd> /, where <kwd> is a keyword | |
261 | ;; which can precede an expression. | |
0386b551 AM |
262 | |
263 | ;; REGEXPS USED FOR FINDING THE POSITION OF A "virtual semicolon" | |
264 | (defconst c-awk-_-harmless-nonws-char-re "[^#/\"\\\\\n\r \t]") | |
d355a0b7 | 265 | ;; NEW VERSION! (which will be restricted to the current line) |
0386b551 AM |
266 | (defconst c-awk-one-line-non-syn-ws*-re |
267 | (concat "\\([ \t]*" | |
268 | "\\(" c-awk-_-harmless-nonws-char-re "\\|" | |
269 | c-awk-non-eol-esc-pair-re "\\|" | |
270 | c-awk-one-line-possibly-open-string-re "\\|" | |
271 | c-awk-one-line-possibly-open-regexp-re | |
272 | "\\)" | |
273 | "\\)*")) | |
274 | ||
275 | \f | |
d9e94c22 | 276 | ;; ACM, 2002/5/29: |
0d26e0b6 | 277 | ;; |
d9e94c22 MS |
278 | ;; The next section of code is about determining whether or not an AWK |
279 | ;; statement is complete or not. We use this to indent the following line. | |
280 | ;; The determination is pretty straightforward in C, where a statement ends | |
281 | ;; with either a ; or a }. Only "while" really gives any trouble there, since | |
282 | ;; it might be the end of a do-while. In AWK, on the other hand, semicolons | |
283 | ;; are rarely used, and EOLs _usually_ act as "virtual semicolons". In | |
284 | ;; addition, we have the complexity of escaped EOLs. The core of this | |
285 | ;; analysis is in the middle of the function | |
286 | ;; c-awk-calculate-NL-prop-prev-line, about 130 lines lower down. | |
287 | ;; | |
288 | ;; To avoid continually repeating this expensive analysis, we "cache" its | |
289 | ;; result in a text-property, c-awk-NL-prop, whose value for a line is set on | |
290 | ;; the EOL (if any) which terminates that line. Should the property be | |
291 | ;; required for the very last line (which has no EOL), it is calculated as | |
292 | ;; required but not cached. The c-awk-NL-prop property should be thought of | |
293 | ;; as only really valid immediately after a buffer change, not a permanently | |
294 | ;; set property. (By contrast, the syntax-table text properties (set by an | |
295 | ;; after-change function) must be constantly updated for the mode to work | |
296 | ;; properly). | |
297 | ;; | |
0386b551 AM |
298 | ;; This text property is also used for "syntactic whitespace" movement, this |
299 | ;; being where the distinction between the values '$' and '}' is significant. | |
300 | ;; | |
d9e94c22 MS |
301 | ;; The valid values for c-awk-NL-prop are: |
302 | ;; | |
303 | ;; nil The property is not currently set for this line. | |
304 | ;; '#' There is NO statement on this line (at most a comment), and no open | |
305 | ;; statement from a previous line which could have been completed on this | |
306 | ;; line. | |
307 | ;; '{' There is an unfinished statement on this (or a previous) line which | |
308 | ;; doesn't require \s to continue onto another line, e.g. the line ends | |
309 | ;; with {, or the && operator, or "if (condition)". Note that even if the | |
310 | ;; newline is redundantly escaped, it remains a '{' line. | |
311 | ;; '\' There is an escaped newline at the end of this line and this '\' is | |
312 | ;; essential to the syntax of the program. (i.e. if it had been a | |
313 | ;; frivolous \, it would have been ignored and the line been given one of | |
314 | ;; the other property values.) | |
0386b551 AM |
315 | ;; '$' A non-empty statement is terminated on the line by an EOL (a "virtual |
316 | ;; semicolon"). This might be a content-free line terminating a statement | |
317 | ;; from the preceding (continued) line (which has property \). | |
318 | ;; '}' A statement, being the last thing (aside from ws/comments) is | |
319 | ;; explicitly terminated on this line by a closing brace (or sometimes a | |
320 | ;; semicolon). | |
d9e94c22 MS |
321 | ;; |
322 | ;; This set of values has been chosen so that the property's value on a line | |
323 | ;; is completely determined by the contents of the line and the property on | |
324 | ;; the previous line, EXCEPT for where a "while" might be the closing | |
325 | ;; statement of a do-while. | |
326 | ||
327 | (defun c-awk-after-if-for-while-condition-p (&optional do-lim) | |
328 | ;; Are we just after the ) in "if/for/while (<condition>)"? | |
329 | ;; | |
330 | ;; Note that the end of the ) in a do .... while (<condition>) doesn't | |
331 | ;; count, since the purpose of this routine is essentially to decide | |
332 | ;; whether to indent the next line. | |
333 | ;; | |
334 | ;; DO-LIM sets a limit on how far back we search for the "do" of a possible | |
335 | ;; do-while. | |
0386b551 AM |
336 | ;; |
337 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
338 | (and |
339 | (eq (char-before) ?\)) | |
340 | (save-excursion | |
341 | (let ((par-pos (c-safe (scan-lists (point) -1 0)))) | |
342 | (when par-pos | |
343 | (goto-char par-pos) ; back over "(...)" | |
344 | (c-backward-token-1) ; BOB isn't a problem. | |
345 | (or (looking-at "\\(if\\|for\\)\\>\\([^_]\\|$\\)") | |
346 | (and (looking-at "while\\>\\([^_]\\|$\\)") ; Ensure this isn't a do-while. | |
347 | (not (eq (c-beginning-of-statement-1 do-lim) | |
348 | 'beginning))))))))) | |
349 | ||
350 | (defun c-awk-after-function-decl-param-list () | |
351 | ;; Are we just after the ) in "function foo (bar)" ? | |
0386b551 AM |
352 | ;; |
353 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
354 | (and (eq (char-before) ?\)) |
355 | (save-excursion | |
356 | (let ((par-pos (c-safe (scan-lists (point) -1 0)))) | |
357 | (when par-pos | |
358 | (goto-char par-pos) ; back over "(...)" | |
359 | (c-backward-token-1) ; BOB isn't a problem | |
360 | (and (looking-at "[_a-zA-Z][_a-zA-Z0-9]*\\>") | |
361 | (progn (c-backward-token-1) | |
362 | (looking-at "func\\(tion\\)?\\>")))))))) | |
363 | ||
364 | ;; 2002/11/8: FIXME! Check c-backward-token-1/2 for success (0 return code). | |
365 | (defun c-awk-after-continue-token () | |
366 | ;; Are we just after a token which can be continued onto the next line without | |
367 | ;; a backslash? | |
0386b551 AM |
368 | ;; |
369 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
370 | (save-excursion |
371 | (c-backward-token-1) ; FIXME 2002/10/27. What if this fails? | |
372 | (if (and (looking-at "[&|]") (not (bobp))) | |
373 | (backward-char)) ; c-backward-token-1 doesn't do this :-( | |
374 | (looking-at "[,{?:]\\|&&\\|||\\|do\\>\\|else\\>"))) | |
375 | ||
376 | (defun c-awk-after-rbrace-or-statement-semicolon () | |
377 | ;; Are we just after a } or a ; which closes a statement? | |
378 | ;; Be careful about ;s in for loop control bits. They don't count! | |
0386b551 AM |
379 | ;; |
380 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
381 | (or (eq (char-before) ?\}) |
382 | (and | |
383 | (eq (char-before) ?\;) | |
384 | (save-excursion | |
385 | (let ((par-pos (c-safe (scan-lists (point) -1 1)))) | |
386 | (when par-pos | |
387 | (goto-char par-pos) ; go back to containing ( | |
388 | (not (and (looking-at "(") | |
389 | (c-backward-token-1) ; BOB isn't a problem | |
390 | (looking-at "for\\>"))))))))) | |
391 | ||
392 | (defun c-awk-back-to-contentful-text-or-NL-prop () | |
393 | ;; Move back to just after the first found of either (i) an EOL which has | |
394 | ;; the c-awk-NL-prop text-property set; or (ii) non-ws text; or (iii) BOB. | |
395 | ;; We return either the value of c-awk-NL-prop (in case (i)) or nil. | |
0386b551 | 396 | ;; Calling functions can best distinguish cases (ii) and (iii) with (bolp). |
d9e94c22 MS |
397 | ;; |
398 | ;; Note that an escaped eol counts as whitespace here. | |
399 | ;; | |
400 | ;; Kludge: If c-backward-syntactic-ws gets stuck at a BOL, it is likely | |
401 | ;; that the previous line contains an unterminated string (without \). In | |
0386b551 | 402 | ;; this case, assume that the previous line's c-awk-NL-prop is a $. |
0d26e0b6 | 403 | ;; |
d9e94c22 MS |
404 | ;; POINT MUST BE AT THE START OF A LINE when calling this function. This |
405 | ;; is to ensure that the various backward-comment functions will work | |
406 | ;; properly. | |
0386b551 AM |
407 | ;; |
408 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
409 | (let ((nl-prop nil) |
410 | bol-pos bsws-pos) ; starting pos for a backward-syntactic-ws call. | |
411 | (while ;; We are at a BOL here. Go back one line each iteration. | |
412 | (and | |
413 | (not (bobp)) | |
414 | (not (setq nl-prop (c-get-char-property (1- (point)) 'c-awk-NL-prop))) | |
415 | (progn (setq bol-pos (c-point 'bopl)) | |
416 | (setq bsws-pos (point)) | |
417 | ;; N.B. the following function will not go back past an EOL if | |
418 | ;; there is an open string (without \) on the previous line. | |
0386b551 AM |
419 | ;; If we find such, set the c-awk-NL-prop on it, too |
420 | ;; (2004/3/29). | |
d9e94c22 MS |
421 | (c-backward-syntactic-ws bol-pos) |
422 | (or (/= (point) bsws-pos) | |
0386b551 AM |
423 | (progn (setq nl-prop ?\$) |
424 | (c-put-char-property (1- (point)) 'c-awk-NL-prop nl-prop) | |
d9e94c22 MS |
425 | nil))) |
426 | ;; If we had a backslash at EOL, c-backward-syntactic-ws will | |
427 | ;; have gone backwards over it. Check the backslash was "real". | |
428 | (progn | |
429 | (if (looking-at "[ \t]*\\\\+$") | |
430 | (if (progn | |
431 | (end-of-line) | |
432 | (search-backward-regexp | |
433 | "\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\$" ; ODD number of \s at EOL :-) | |
434 | bol-pos t)) | |
435 | (progn (end-of-line) ; escaped EOL. | |
436 | (backward-char) | |
437 | (c-backward-syntactic-ws bol-pos)) | |
438 | (end-of-line))) ; The \ at eol is a fake. | |
439 | (bolp)))) | |
440 | nl-prop)) | |
441 | ||
442 | (defun c-awk-calculate-NL-prop-prev-line (&optional do-lim) | |
443 | ;; Calculate and set the value of the c-awk-NL-prop on the immediately | |
444 | ;; preceding EOL. This may also involve doing the same for several | |
445 | ;; preceding EOLs. | |
0d26e0b6 | 446 | ;; |
d9e94c22 MS |
447 | ;; NOTE that if the property was already set, we return it without |
448 | ;; recalculation. (This is by accident rather than design.) | |
0d26e0b6 | 449 | ;; |
d9e94c22 MS |
450 | ;; Return the property which got set (or was already set) on the previous |
451 | ;; line. Return nil if we hit BOB. | |
0d26e0b6 | 452 | ;; |
d9e94c22 | 453 | ;; See c-awk-after-if-for-while-condition-p for a description of DO-LIM. |
0386b551 AM |
454 | ;; |
455 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
456 | (save-excursion |
457 | (save-match-data | |
458 | (beginning-of-line) | |
459 | (let* ((pos (point)) | |
460 | (nl-prop (c-awk-back-to-contentful-text-or-NL-prop))) | |
461 | ;; We are either (1) at a BOL (with nl-prop containing the previous | |
462 | ;; line's c-awk-NL-prop) or (2) after contentful text on a line. At | |
463 | ;; the BOB counts as case (1), so we test next for bolp rather than | |
464 | ;; non-nil nl-prop. | |
465 | (when (not (bolp)) | |
466 | (setq nl-prop | |
467 | (cond | |
468 | ;; Incomplete statement which doesn't require escaped EOL? | |
469 | ((or (c-awk-after-if-for-while-condition-p do-lim) | |
470 | (c-awk-after-function-decl-param-list) | |
471 | (c-awk-after-continue-token)) | |
472 | ?\{) | |
473 | ;; Escaped EOL (where there's also something to continue)? | |
474 | ((and (looking-at "[ \t]*\\\\$") | |
475 | (not (c-awk-after-rbrace-or-statement-semicolon))) | |
476 | ?\\) | |
0386b551 AM |
477 | ;; A statement was completed on this line. How? |
478 | ((memq (char-before) '(?\; ?\})) ?\}) ; Real ; or } | |
479 | (t ?\$))) ; A virtual semicolon. | |
d9e94c22 MS |
480 | (end-of-line) |
481 | (c-put-char-property (point) 'c-awk-NL-prop nl-prop) | |
482 | (forward-line)) | |
483 | ||
484 | ;; We are now at a (possibly empty) sequence of content-free lines. | |
485 | ;; Set c-awk-NL-prop on each of these lines's EOL. | |
486 | (while (< (point) pos) ; one content-free line each iteration. | |
487 | (cond ; recalculate nl-prop from previous line's value. | |
0386b551 | 488 | ((memq nl-prop '(?\} ?\$ nil)) (setq nl-prop ?\#)) |
d9e94c22 | 489 | ((eq nl-prop ?\\) |
0386b551 | 490 | (if (not (looking-at "[ \t]*\\\\$")) (setq nl-prop ?\$))) |
d9e94c22 MS |
491 | ;; ?\# (empty line) and ?\{ (open stmt) don't change. |
492 | ) | |
493 | (forward-line) | |
494 | (c-put-char-property (1- (point)) 'c-awk-NL-prop nl-prop)) | |
495 | nl-prop)))) | |
496 | ||
497 | (defun c-awk-get-NL-prop-prev-line (&optional do-lim) | |
498 | ;; Get the c-awk-NL-prop text-property from the previous line, calculating | |
f6558e2d | 499 | ;; it if necessary. Return nil if we're already at BOB. |
d9e94c22 | 500 | ;; See c-awk-after-if-for-while-condition-p for a description of DO-LIM. |
0386b551 AM |
501 | ;; |
502 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
503 | (if (bobp) |
504 | nil | |
505 | (or (c-get-char-property (c-point 'eopl) 'c-awk-NL-prop) | |
506 | (c-awk-calculate-NL-prop-prev-line do-lim)))) | |
507 | ||
508 | (defun c-awk-get-NL-prop-cur-line (&optional do-lim) | |
509 | ;; Get the c-awk-NL-prop text-property from the current line, calculating it | |
510 | ;; if necessary. (As a special case, the property doesn't get set on an | |
511 | ;; empty line at EOB (there's no position to set the property on), but the | |
512 | ;; function returns the property value an EOL would have got.) | |
0d26e0b6 | 513 | ;; |
d9e94c22 | 514 | ;; See c-awk-after-if-for-while-condition-p for a description of DO-LIM. |
0386b551 AM |
515 | ;; |
516 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
517 | (save-excursion |
518 | (let ((extra-nl nil)) | |
519 | (end-of-line) ; Necessary for the following test to work. | |
520 | (when (= (forward-line) 1) ; if we were on the last line.... | |
521 | (insert-char ?\n 1) ; ...artificial eol is needed for comment detection. | |
522 | (setq extra-nl t)) | |
523 | (prog1 (c-awk-get-NL-prop-prev-line do-lim) | |
d355a0b7 | 524 | (if extra-nl (delete-char -1)))))) |
d9e94c22 | 525 | |
0386b551 | 526 | (defsubst c-awk-prev-line-incomplete-p (&optional do-lim) |
d9e94c22 MS |
527 | ;; Is there an incomplete statement at the end of the previous line? |
528 | ;; See c-awk-after-if-for-while-condition-p for a description of DO-LIM. | |
0386b551 AM |
529 | ;; |
530 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
531 | (memq (c-awk-get-NL-prop-prev-line do-lim) '(?\\ ?\{))) |
532 | ||
0386b551 | 533 | (defsubst c-awk-cur-line-incomplete-p (&optional do-lim) |
d9e94c22 MS |
534 | ;; Is there an incomplete statement at the end of the current line? |
535 | ;; See c-awk-after-if-for-while-condition-p for a description of DO-LIM. | |
0386b551 AM |
536 | ;; |
537 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
538 | (memq (c-awk-get-NL-prop-cur-line do-lim) '(?\\ ?\{))) |
539 | ||
d355a0b7 SM |
540 | ;; NOTES ON "VIRTUAL SEMICOLONS" |
541 | ;; | |
542 | ;; A "virtual semicolon" is what terminates a statement when there is no ; | |
543 | ;; or } to do the job. Like point, it is considered to lie _between_ two | |
544 | ;; characters. As from mid-March 2004, it is considered to lie just after | |
545 | ;; the last non-syntactic-whitespace character on the line; (previously, it | |
546 | ;; was considered an attribute of the EOL on the line). A real semicolon | |
547 | ;; never counts as a virtual one. | |
0386b551 AM |
548 | |
549 | (defun c-awk-at-vsemi-p (&optional pos) | |
550 | ;; Is there a virtual semicolon at POS (or POINT)? | |
d9e94c22 | 551 | (save-excursion |
0386b551 AM |
552 | (let (nl-prop |
553 | (pos-or-point (progn (if pos (goto-char pos)) (point)))) | |
554 | (forward-line 0) | |
555 | (search-forward-regexp c-awk-one-line-non-syn-ws*-re) | |
556 | (and (eq (point) pos-or-point) | |
557 | (progn | |
558 | (while (and (eq (setq nl-prop (c-awk-get-NL-prop-cur-line)) ?\\) | |
559 | (eq (forward-line) 0) | |
560 | (looking-at c-awk-blank-or-comment-line-re))) | |
561 | (eq nl-prop ?\$)))))) | |
562 | ||
563 | (defun c-awk-vsemi-status-unknown-p () | |
564 | ;; Are we unsure whether there is a virtual semicolon on the current line? | |
565 | ;; DO NOT under any circumstances attempt to calculate this; that would | |
333f9019 | 566 | ;; defeat the (admittedly kludgy) purpose of this function, which is to |
0386b551 AM |
567 | ;; prevent an infinite recursion in c-beginning-of-statement-1 when point |
568 | ;; starts at a `while' token. | |
d9e94c22 MS |
569 | (not (c-get-char-property (c-point 'eol) 'c-awk-NL-prop))) |
570 | ||
571 | (defun c-awk-clear-NL-props (beg end) | |
572 | ;; This function is run from before-change-hooks. It clears the | |
573 | ;; c-awk-NL-prop text property from beg to the end of the buffer (The END | |
574 | ;; parameter is ignored). This ensures that the indentation engine will | |
575 | ;; never use stale values for this property. | |
0386b551 AM |
576 | ;; |
577 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
578 | (save-restriction |
579 | (widen) | |
580 | (c-clear-char-properties beg (point-max) 'c-awk-NL-prop))) | |
581 | ||
582 | (defun c-awk-unstick-NL-prop () | |
583 | ;; Ensure that the text property c-awk-NL-prop is "non-sticky". Without | |
584 | ;; this, a new newline inserted after an old newline (e.g. by C-j) would | |
585 | ;; inherit any c-awk-NL-prop from the old newline. This would be a Bad | |
586 | ;; Thing. This function's action is required by c-put-char-property. | |
0d26e0b6 | 587 | (if (and (boundp 'text-property-default-nonsticky) ; doesn't exist in XEmacs |
d9e94c22 MS |
588 | (not (assoc 'c-awk-NL-prop text-property-default-nonsticky))) |
589 | (setq text-property-default-nonsticky | |
590 | (cons '(c-awk-NL-prop . t) text-property-default-nonsticky)))) | |
591 | ||
592 | ;; The following is purely a diagnostic command, to be commented out of the | |
593 | ;; final release. ACM, 2002/6/1 | |
594 | ;; (defun NL-props () | |
595 | ;; (interactive) | |
596 | ;; (let (pl-prop cl-prop) | |
597 | ;; (message "Prev-line: %s Cur-line: %s" | |
598 | ;; (if (setq pl-prop (c-get-char-property (c-point 'eopl) 'c-awk-NL-prop)) | |
599 | ;; (char-to-string pl-prop) | |
600 | ;; "nil") | |
601 | ;; (if (setq cl-prop (c-get-char-property (c-point 'eol) 'c-awk-NL-prop)) | |
602 | ;; (char-to-string cl-prop) | |
603 | ;; "nil")))) | |
604 | ;(define-key awk-mode-map [?\C-c ?\r] 'NL-props) ; commented out, 2002/8/31 | |
605 | ;for now. In the byte compiled version, this causes things to crash because | |
606 | ;awk-mode-map isn't yet defined. :-( | |
607 | ||
608 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
0386b551 | 609 | \f |
d9e94c22 MS |
610 | ;; The following section of the code is to do with font-locking. The biggest |
611 | ;; problem for font-locking is deciding whether a / is a regular expression | |
612 | ;; delimiter or a division sign - determining precisely where strings and | |
613 | ;; regular expressions start and stop is also troublesome. This is the | |
614 | ;; purpose of the function c-awk-set-syntax-table-properties and the myriad | |
615 | ;; elisp regular expressions it uses. | |
616 | ;; | |
617 | ;; Because AWK is a line oriented language, I felt the normal cc-mode strategy | |
618 | ;; for font-locking unterminated strings (i.e. font-locking the buffer up to | |
619 | ;; the next string delimiter as a string) was inappropriate. Instead, | |
620 | ;; unbalanced string/regexp delimiters are given the warning font, being | |
621 | ;; refonted with the string font as soon as the matching delimiter is entered. | |
622 | ;; | |
623 | ;; This requires the region processed by the current font-lock after-change | |
624 | ;; function to have access to the start of the string/regexp, which may be | |
625 | ;; several lines back. The elisp "advice" feature is used on these functions | |
626 | ;; to allow this. | |
627 | ||
628 | (defun c-awk-beginning-of-logical-line (&optional pos) | |
629 | ;; Go back to the start of the (apparent) current line (or the start of the | |
630 | ;; line containing POS), returning the buffer position of that point. I.e., | |
631 | ;; go back to the last line which doesn't have an escaped EOL before it. | |
0d26e0b6 | 632 | ;; |
d9e94c22 MS |
633 | ;; This is guaranteed to be "safe" for syntactic analysis, i.e. outwith any |
634 | ;; comment, string or regexp. IT MAY WELL BE that this function should not be | |
635 | ;; executed on a narrowed buffer. | |
0386b551 AM |
636 | ;; |
637 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
638 | (if pos (goto-char pos)) |
639 | (forward-line 0) | |
640 | (while (and (> (point) (point-min)) | |
641 | (eq (char-before (1- (point))) ?\\)) | |
642 | (forward-line -1)) | |
643 | (point)) | |
644 | ||
88a46e21 AM |
645 | (defun c-awk-beyond-logical-line (&optional pos) |
646 | ;; Return the position just beyond the (apparent) current logical line, or the | |
647 | ;; one containing POS. This is usually the beginning of the next line which | |
648 | ;; doesn't follow an escaped EOL. At EOB, this will be EOB. | |
649 | ;; | |
650 | ;; Point is unchanged. | |
d9e94c22 MS |
651 | ;; |
652 | ;; This is guaranteed to be "safe" for syntactic analysis, i.e. outwith any | |
653 | ;; comment, string or regexp. IT MAY WELL BE that this function should not be | |
654 | ;; executed on a narrowed buffer. | |
88a46e21 AM |
655 | (save-excursion |
656 | (if pos (goto-char pos)) | |
657 | (end-of-line) | |
658 | (while (and (< (point) (point-max)) | |
659 | (eq (char-before) ?\\)) | |
660 | (end-of-line 2)) | |
661 | (if (< (point) (point-max)) | |
662 | (1+ (point)) | |
663 | (point)))) | |
d9e94c22 | 664 | |
d9e94c22 MS |
665 | ;; ACM, 2002/02/15: The idea of the next function is to put the "Error font" |
666 | ;; on strings/regexps which are missing their closing delimiter. | |
667 | ;; 2002/4/28. The default syntax for / has been changed from "string" to | |
668 | ;; "punctuation", to reduce hassle when this character appears within a string | |
669 | ;; or comment. | |
670 | ||
671 | (defun c-awk-set-string-regexp-syntax-table-properties (beg end) | |
672 | ;; BEG and END bracket a (possibly unterminated) string or regexp. The | |
673 | ;; opening delimiter is after BEG, and the closing delimiter, IF ANY, is AFTER | |
674 | ;; END. Set the appropriate syntax-table properties on the delimiters and | |
675 | ;; contents of this string/regex. | |
676 | ;; | |
677 | ;; "String" here can also mean a gawk 3.1 "localizable" string which starts | |
678 | ;; with _". In this case, we step over the _ and ignore it; It will get it's | |
679 | ;; font from an entry in awk-font-lock-keywords. | |
680 | ;; | |
681 | ;; If the closing delimiter is missing (i.e., there is an EOL there) set the | |
682 | ;; STRING-FENCE property on the opening " or / and closing EOL. | |
0386b551 AM |
683 | ;; |
684 | ;; This function does hidden buffer changes. | |
d9e94c22 MS |
685 | (if (eq (char-after beg) ?_) (setq beg (1+ beg))) |
686 | ||
687 | ;; First put the properties on the delimiters. | |
688 | (cond ((eq end (point-max)) ; string/regexp terminated by EOB | |
0386b551 | 689 | (c-put-char-property beg 'syntax-table '(15))) ; (15) = "string fence" |
d9e94c22 | 690 | ((/= (char-after beg) (char-after end)) ; missing end delimiter |
0386b551 AM |
691 | (c-put-char-property beg 'syntax-table '(15)) |
692 | (c-put-char-property end 'syntax-table '(15))) | |
d9e94c22 | 693 | ((eq (char-after beg) ?/) ; Properly bracketed regexp |
0386b551 AM |
694 | (c-put-char-property beg 'syntax-table '(7)) ; (7) = "string" |
695 | (c-put-char-property end 'syntax-table '(7))) | |
d9e94c22 MS |
696 | (t)) ; Properly bracketed string: Nothing to do. |
697 | ;; Now change the properties of any escaped "s in the string to punctuation. | |
698 | (save-excursion | |
699 | (goto-char (1+ beg)) | |
700 | (or (eobp) | |
701 | (while (search-forward "\"" end t) | |
0386b551 | 702 | (c-put-char-property (1- (point)) 'syntax-table '(1)))))) |
d9e94c22 MS |
703 | |
704 | (defun c-awk-syntax-tablify-string () | |
705 | ;; Point is at the opening " or _" of a string. Set the syntax-table | |
706 | ;; properties on this string, leaving point just after the string. | |
707 | ;; | |
708 | ;; The result is nil if a / immediately after the string would be a regexp | |
709 | ;; opener, t if it would be a division sign. | |
0386b551 AM |
710 | ;; |
711 | ;; This function does hidden buffer changes. | |
d9e94c22 MS |
712 | (search-forward-regexp c-awk-string-without-end-here-re nil t) ; a (possibly unterminated) string |
713 | (c-awk-set-string-regexp-syntax-table-properties | |
714 | (match-beginning 0) (match-end 0)) | |
715 | (cond ((looking-at "\"") | |
716 | (forward-char) | |
717 | t) ; In AWK, ("15" / 5) gives 3 ;-) | |
718 | ((looking-at "[\n\r]") ; Unterminated string with EOL. | |
719 | (forward-char) | |
720 | nil) ; / on next line would start a regexp | |
721 | (t nil))) ; Unterminated string at EOB | |
722 | ||
723 | (defun c-awk-syntax-tablify-/ (anchor anchor-state-/div) | |
724 | ;; Point is at a /. Determine whether this is a division sign or a regexp | |
725 | ;; opener, and if the latter, apply syntax-table properties to the entire | |
726 | ;; regexp. Point is left immediately after the division sign or regexp, as | |
727 | ;; the case may be. | |
728 | ;; | |
729 | ;; ANCHOR-STATE-/DIV identifies whether a / at ANCHOR would have been a | |
730 | ;; division sign (value t) or a regexp opener (value nil). The idea is that | |
6772c8e1 | 731 | ;; we analyze the line from ANCHOR up till point to determine what the / at |
d9e94c22 MS |
732 | ;; point is. |
733 | ;; | |
734 | ;; The result is what ANCHOR-STATE-/DIV (see above) is where point is left. | |
0386b551 | 735 | ;; |
88a46e21 | 736 | ;; This function does hidden buffer changes. |
d9e94c22 MS |
737 | (let ((/point (point))) |
738 | (goto-char anchor) | |
c7015153 | 739 | ;; Analyze the line to find out what the / is. |
d9e94c22 | 740 | (if (if anchor-state-/div |
953e5b8c AM |
741 | (not (search-forward-regexp c-awk-regexp-sign-re (1+ /point) t)) |
742 | (and (not (search-forward-regexp c-awk-kwd-regexp-sign-re (1+ /point) t)) | |
743 | (search-forward-regexp c-awk-div-sign-re (1+ /point) t))) | |
744 | ;; A division sign. | |
51c9af45 | 745 | (progn (goto-char (1+ /point)) nil) |
d9e94c22 MS |
746 | ;; A regexp opener |
747 | ;; Jump over the regexp innards, setting the match data. | |
748 | (goto-char /point) | |
749 | (search-forward-regexp c-awk-regexp-without-end-re) | |
750 | (c-awk-set-string-regexp-syntax-table-properties | |
751 | (match-beginning 0) (match-end 0)) | |
752 | (cond ((looking-at "/") ; Terminating / | |
753 | (forward-char) | |
754 | t) | |
755 | ((looking-at "[\n\r]") ; Incomplete regexp terminated by EOL | |
756 | (forward-char) | |
757 | nil) ; / on next line would start another regexp | |
758 | (t nil))))) ; Unterminated regexp at EOB | |
759 | ||
760 | (defun c-awk-set-syntax-table-properties (lim) | |
761 | ;; Scan the buffer text between point and LIM, setting (and clearing) the | |
762 | ;; syntax-table property where necessary. | |
763 | ;; | |
764 | ;; This function is designed to be called as the FUNCTION in a MATCHER in | |
765 | ;; font-lock-syntactic-keywords, and it always returns NIL (to inhibit | |
766 | ;; repeated calls from font-lock: See elisp info page "Search-based | |
767 | ;; Fontification"). It also gets called, with a bit of glue, from | |
768 | ;; after-change-functions when font-lock isn't active. Point is left | |
769 | ;; "undefined" after this function exits. THE BUFFER SHOULD HAVE BEEN | |
770 | ;; WIDENED, AND ANY PRECIOUS MATCH-DATA SAVED BEFORE CALLING THIS ROUTINE. | |
771 | ;; | |
772 | ;; We need to set/clear the syntax-table property on: | |
773 | ;; (i) / - It is set to "string" on a / which is the opening or closing | |
774 | ;; delimiter of the properly terminated regexp (and left unset on a | |
775 | ;; division sign). | |
776 | ;; (ii) the opener of an unterminated string/regexp, we set the property | |
777 | ;; "generic string delimiter" on both the opening " or / and the end of the | |
778 | ;; line where the closing delimiter is missing. | |
779 | ;; (iii) "s inside strings/regexps (these will all be escaped "s). They are | |
780 | ;; given the property "punctuation". This will later allow other routines | |
781 | ;; to use the regexp "\\S\"*" to skip over the string innards. | |
782 | ;; (iv) Inside a comment, all syntax-table properties are cleared. | |
0386b551 AM |
783 | ;; |
784 | ;; This function does hidden buffer changes. | |
d9e94c22 MS |
785 | (let (anchor |
786 | (anchor-state-/div nil)) ; t means a following / would be a div sign. | |
787 | (c-awk-beginning-of-logical-line) ; ACM 2002/7/21. This is probably redundant. | |
0386b551 | 788 | (c-clear-char-properties (point) lim 'syntax-table) |
d9e94c22 | 789 | ;; Once round the next loop for each string, regexp, or div sign |
0386b551 AM |
790 | (while (progn |
791 | ;; Skip any "harmless" lines before the next tricky one. | |
792 | (if (search-forward-regexp c-awk-harmless-lines+-here-re nil t) | |
793 | (setq anchor-state-/div nil)) | |
794 | (< (point) lim)) | |
d9e94c22 MS |
795 | (setq anchor (point)) |
796 | (search-forward-regexp c-awk-harmless-string*-here-re nil t) | |
953e5b8c | 797 | ;; We are now looking at either a " or a / or a brace/paren/semicolon. |
d9c287e5 PE |
798 | ;; Do our thing on the string, regexp or division sign or update |
799 | ;; our state. | |
d9e94c22 | 800 | (setq anchor-state-/div |
953e5b8c AM |
801 | (cond |
802 | ((looking-at "_?\"") | |
803 | (c-awk-syntax-tablify-string)) | |
804 | ((eq (char-after) ?/) | |
805 | (c-awk-syntax-tablify-/ anchor anchor-state-/div)) | |
806 | ((memq (char-after) '(?{ ?} ?\( ?\;)) | |
807 | (forward-char) | |
808 | nil) | |
809 | (t ; ?\) | |
810 | (forward-char) | |
811 | t)))) | |
d9e94c22 MS |
812 | nil)) |
813 | ||
d9e94c22 MS |
814 | ;; ACM, 2002/07/21: Thoughts: We need an AWK Mode after-change function to set |
815 | ;; the syntax-table properties even when font-lock isn't enabled, for the | |
816 | ;; subsequent use of movement functions, etc. However, it seems that if font | |
817 | ;; lock _is_ enabled, we can always leave it to do the job. | |
88a46e21 AM |
818 | (defvar c-awk-old-ByLL 0) |
819 | (make-variable-buffer-local 'c-awk-old-Byll) | |
820 | ;; Just beyond logical line following the region which is about to be changed. | |
821 | ;; Set in c-awk-record-region-clear-NL and used in c-awk-after-change. | |
d9e94c22 | 822 | |
88a46e21 | 823 | (defun c-awk-record-region-clear-NL (beg end) |
d9e94c22 MS |
824 | ;; This function is called exclusively from the before-change-functions hook. |
825 | ;; It does two things: Finds the end of the (logical) line on which END lies, | |
88a46e21 AM |
826 | ;; and clears c-awk-NL-prop text properties from this point onwards. BEG is |
827 | ;; ignored. | |
0386b551 | 828 | ;; |
88a46e21 AM |
829 | ;; On entry, the buffer will have been widened and match-data will have been |
830 | ;; saved; point is undefined on both entry and exit; the return value is | |
831 | ;; ignored. | |
832 | ;; | |
833 | ;; This function does hidden buffer changes. | |
834 | (c-save-buffer-state () | |
835 | (setq c-awk-old-ByLL (c-awk-beyond-logical-line end)) | |
836 | (c-save-buffer-state nil | |
837 | (c-awk-clear-NL-props end (point-max))))) | |
d9e94c22 MS |
838 | |
839 | (defun c-awk-end-of-change-region (beg end old-len) | |
840 | ;; Find the end of the region which needs to be font-locked after a change. | |
841 | ;; This is the end of the logical line on which the change happened, either | |
cb694ab7 | 842 | ;; as it was before the change, or as it is now, whichever is later. |
d9e94c22 | 843 | ;; N.B. point is left undefined. |
88a46e21 AM |
844 | (max (+ (- c-awk-old-ByLL old-len) (- end beg)) |
845 | (c-awk-beyond-logical-line end))) | |
d9e94c22 MS |
846 | |
847 | ;; ACM 2002/5/25. When font-locking is invoked by a buffer change, the region | |
848 | ;; specified by the font-lock after-change function must be expanded to | |
849 | ;; include ALL of any string or regexp within the region. The simplest way to | |
850 | ;; do this in practice is to use the beginning/end-of-logical-line functions. | |
851 | ;; Don't overlook the possibility of the buffer change being the "recapturing" | |
852 | ;; of a previously escaped newline. | |
88a46e21 | 853 | |
0d26e0b6 | 854 | ;; ACM 2008-02-05: |
88a46e21 AM |
855 | (defun c-awk-extend-and-syntax-tablify-region (beg end old-len) |
856 | ;; Expand the region (BEG END) as needed to (c-new-BEG c-new-END) then put | |
857 | ;; `syntax-table' properties on this region. | |
858 | ;; | |
859 | ;; This function is called from an after-change function, BEG END and | |
860 | ;; OLD-LEN being the standard parameters. | |
0d26e0b6 | 861 | ;; |
88a46e21 AM |
862 | ;; Point is undefined both before and after this function call, the buffer |
863 | ;; has been widened, and match-data saved. The return value is ignored. | |
864 | ;; | |
865 | ;; It prepares the buffer for font | |
866 | ;; locking, hence must get called before `font-lock-after-change-function'. | |
867 | ;; | |
868 | ;; This function is the AWK value of `c-before-font-lock-function'. | |
869 | ;; It does hidden buffer changes. | |
870 | (c-save-buffer-state () | |
871 | (setq c-new-END (c-awk-end-of-change-region beg end old-len)) | |
872 | (setq c-new-BEG (c-awk-beginning-of-logical-line beg)) | |
873 | (goto-char c-new-BEG) | |
874 | (c-awk-set-syntax-table-properties c-new-END))) | |
d9e94c22 | 875 | |
3c0ab532 AM |
876 | ;; Awk regexps written with help from Peter Galbraith |
877 | ;; <galbraith@mixing.qc.dfo.ca>. | |
c7015153 | 878 | ;; Take GNU Emacs's 'words out of the following regexp-opts. They don't work |
0d26e0b6 | 879 | ;; in XEmacs 21.4.4. acm 2002/9/19. |
3c0ab532 AM |
880 | (defconst awk-font-lock-keywords |
881 | (eval-when-compile | |
882 | (list | |
883 | ;; Function names. | |
884 | '("^\\s *\\(func\\(tion\\)?\\)\\>\\s *\\(\\sw+\\)?" | |
885 | (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t)) | |
886 | ;; | |
887 | ;; Variable names. | |
888 | (cons | |
889 | (concat "\\<" | |
890 | (regexp-opt | |
891 | '("ARGC" "ARGIND" "ARGV" "BINMODE" "CONVFMT" "ENVIRON" | |
892 | "ERRNO" "FIELDWIDTHS" "FILENAME" "FNR" "FS" "IGNORECASE" | |
893 | "LINT" "NF" "NR" "OFMT" "OFS" "ORS" "PROCINFO" "RLENGTH" | |
894 | "RS" "RSTART" "RT" "SUBSEP" "TEXTDOMAIN") t) "\\>") | |
895 | 'font-lock-variable-name-face) | |
896 | ||
897 | ;; Special file names. (acm, 2002/7/22) | |
898 | ;; The following regexp was created by first evaluating this in GNU Emacs 21.1: | |
899 | ;; (regexp-opt '("/dev/stdin" "/dev/stdout" "/dev/stderr" "/dev/fd/n" "/dev/pid" | |
900 | ;; "/dev/ppid" "/dev/pgrpid" "/dev/user") 'words) | |
901 | ;; , removing the "?:" from each "\\(?:" (for backward compatibility with older Emacsen) | |
902 | ;; , replacing the "n" in "dev/fd/n" with "[0-9]+" | |
903 | ;; , removing the unwanted \\< at the beginning, and finally filling out the | |
904 | ;; regexp so that a " must come before, and either a " or heuristic stuff after. | |
905 | ;; The surrounding quotes are fontified along with the filename, since, semantically, | |
906 | ;; they are an indivisible unit. | |
907 | '("\\(\"/dev/\\(fd/[0-9]+\\|p\\(\\(\\(gr\\)?p\\)?id\\)\\|\ | |
908 | std\\(err\\|in\\|out\\)\\|user\\)\\)\\>\ | |
909 | \\(\\(\"\\)\\|\\([^\"/\n\r][^\"\n\r]*\\)?$\\)" | |
910 | (1 font-lock-variable-name-face t) | |
911 | (8 font-lock-variable-name-face t t)) | |
912 | ;; Do the same (almost) with | |
913 | ;; (regexp-opt '("/inet/tcp/lport/rhost/rport" "/inet/udp/lport/rhost/rport" | |
914 | ;; "/inet/raw/lport/rhost/rport") 'words) | |
915 | ;; This cannot be combined with the above pattern, because the match number | |
916 | ;; for the (optional) closing \" would then exceed 9. | |
917 | '("\\(\"/inet/\\(\\(raw\\|\\(tc\\|ud\\)p\\)/lport/rhost/rport\\)\\)\\>\ | |
918 | \\(\\(\"\\)\\|\\([^\"/\n\r][^\"\n\r]*\\)?$\\)" | |
919 | (1 font-lock-variable-name-face t) | |
920 | (6 font-lock-variable-name-face t t)) | |
921 | ||
922 | ;; Keywords. | |
923 | (concat "\\<" | |
924 | (regexp-opt | |
898169a2 AM |
925 | '("BEGIN" "END" "break" "case" "continue" "default" "delete" |
926 | "do" "else" "exit" "for" "getline" "if" "in" "next" | |
927 | "nextfile" "return" "switch" "while") | |
3c0ab532 AM |
928 | t) "\\>") |
929 | ||
930 | ;; Builtins. | |
931 | `(eval . (list | |
932 | ,(concat | |
933 | "\\<" | |
934 | (regexp-opt | |
935 | '("adump" "and" "asort" "atan2" "bindtextdomain" "close" | |
936 | "compl" "cos" "dcgettext" "exp" "extension" "fflush" | |
937 | "gensub" "gsub" "index" "int" "length" "log" "lshift" | |
938 | "match" "mktime" "or" "print" "printf" "rand" "rshift" | |
939 | "sin" "split" "sprintf" "sqrt" "srand" "stopme" | |
940 | "strftime" "strtonum" "sub" "substr" "system" | |
941 | "systime" "tolower" "toupper" "xor") t) | |
942 | "\\>") | |
943 | 0 c-preprocessor-face-name)) | |
944 | ||
945 | ;; gawk debugging keywords. (acm, 2002/7/21) | |
946 | ;; (Removed, 2003/6/6. These functions are now fontified as built-ins) | |
947 | ;; (list (concat "\\<" (regexp-opt '("adump" "stopme") t) "\\>") | |
948 | ;; 0 'font-lock-warning-face) | |
949 | ||
950 | ;; User defined functions with an apparent spurious space before the | |
951 | ;; opening parenthesis. acm, 2002/5/30. | |
952 | `(,(concat "\\(\\w\\|_\\)" c-awk-escaped-nls* "\\s " | |
953 | c-awk-escaped-nls*-with-space* "(") | |
954 | (0 'font-lock-warning-face)) | |
955 | ||
956 | ;; Space after \ in what looks like an escaped newline. 2002/5/31 | |
957 | '("\\\\\\s +$" 0 font-lock-warning-face t) | |
958 | ||
959 | ;; Unbalanced string (") or regexp (/) delimiters. 2002/02/16. | |
960 | '("\\s|" 0 font-lock-warning-face t nil) | |
961 | ;; gawk 3.1 localizable strings ( _"translate me!"). 2002/5/21 | |
962 | '("\\(_\\)\\s|" 1 font-lock-warning-face) | |
963 | '("\\(_\\)\\s\"" 1 font-lock-string-face) ; FIXME! not for XEmacs. 2002/10/6 | |
964 | )) | |
965 | "Default expressions to highlight in AWK mode.") | |
0386b551 AM |
966 | \f |
967 | ;; ACM 2002/9/29. Movement functions, e.g. for C-M-a and C-M-e | |
d9e94c22 | 968 | |
0386b551 AM |
969 | ;; The following three regexps differ from those earlier on in cc-awk.el in |
970 | ;; that they assume the syntax-table properties have been set. They are thus | |
971 | ;; not useful for code which sets these properties. | |
d9e94c22 | 972 | (defconst c-awk-terminated-regexp-or-string-here-re "\\=\\s\"\\S\"*\\s\"") |
0386b551 | 973 | ;; Matches a terminated string/regexp. |
d9e94c22 MS |
974 | |
975 | (defconst c-awk-unterminated-regexp-or-string-here-re "\\=\\s|\\S|*$") | |
976 | ;; Matches an unterminated string/regexp, NOT including the eol at the end. | |
977 | ||
978 | (defconst c-awk-harmless-pattern-characters* | |
979 | (concat "\\([^{;#/\"\\\\\n\r]\\|" c-awk-esc-pair-re "\\)*")) | |
980 | ;; Matches any "harmless" character in a pattern or an escaped character pair. | |
981 | ||
0386b551 AM |
982 | (defun c-awk-at-statement-end-p () |
983 | ;; Point is not inside a comment or string. Is it AT the end of a | |
984 | ;; statement? This means immediately after the last non-ws character of the | |
985 | ;; statement. The caller is responsible for widening the buffer, if | |
986 | ;; appropriate. | |
987 | (and (not (bobp)) | |
988 | (save-excursion | |
989 | (backward-char) | |
990 | (or (looking-at "[};]") | |
991 | (and (memq (c-awk-get-NL-prop-cur-line) '(?\$ ?\\)) | |
992 | (looking-at | |
993 | (eval-when-compile | |
994 | (concat "[^ \t\n\r\\]" c-awk-escaped-nls*-with-space* | |
995 | "[#\n\r]")))))))) | |
996 | ||
d9e94c22 MS |
997 | (defun c-awk-beginning-of-defun (&optional arg) |
998 | "Move backward to the beginning of an AWK \"defun\". With ARG, do it that | |
999 | many times. Negative arg -N means move forward to Nth following beginning of | |
1000 | defun. Returns t unless search stops due to beginning or end of buffer. | |
1001 | ||
1002 | By a \"defun\" is meant either a pattern-action pair or a function. The start | |
fd35a256 | 1003 | of a defun is recognized as code starting at column zero which is neither a |
d9e94c22 MS |
1004 | closing brace nor a comment nor a continuation of the previous line. Unlike |
1005 | in some other modes, having an opening brace at column 0 is neither necessary | |
0386b551 AM |
1006 | nor helpful. |
1007 | ||
1008 | Note that this function might do hidden buffer changes. See the | |
1009 | comment at the start of cc-engine.el for more info." | |
d9e94c22 | 1010 | (interactive "p") |
28abe5e2 | 1011 | (or arg (setq arg 1)) |
d9e94c22 MS |
1012 | (save-match-data |
1013 | (c-save-buffer-state ; ensures the buffer is writable. | |
1014 | nil | |
1015 | (let ((found t)) ; Has the most recent regexp search found b-of-defun? | |
1016 | (if (>= arg 0) | |
1017 | ;; Go back one defun each time round the following loop. (For +ve arg) | |
1018 | (while (and found (> arg 0) (not (eq (point) (point-min)))) | |
1019 | ;; Go back one "candidate" each time round the next loop until one | |
1020 | ;; is genuinely a beginning-of-defun. | |
1021 | (while (and (setq found (search-backward-regexp | |
1022 | "^[^#} \t\n\r]" (point-min) 'stop-at-limit)) | |
0386b551 | 1023 | (not (memq (c-awk-get-NL-prop-prev-line) '(?\$ ?\} ?\#))))) |
d9e94c22 MS |
1024 | (setq arg (1- arg))) |
1025 | ;; The same for a -ve arg. | |
1026 | (if (not (eq (point) (point-max))) (forward-char 1)) | |
1027 | (while (and found (< arg 0) (not (eq (point) (point-max)))) ; The same for -ve arg. | |
1028 | (while (and (setq found (search-forward-regexp | |
1029 | "^[^#} \t\n\r]" (point-max) 'stop-at-limit)) | |
0386b551 | 1030 | (not (memq (c-awk-get-NL-prop-prev-line) '(?\$ ?\} ?\#))))) |
d9e94c22 MS |
1031 | (setq arg (1+ arg))) |
1032 | (if found (goto-char (match-beginning 0)))) | |
1033 | (eq arg 0))))) | |
1034 | ||
1035 | (defun c-awk-forward-awk-pattern () | |
1036 | ;; Point is at the start of an AWK pattern (which may be null) or function | |
1037 | ;; declaration. Move to the pattern's end, and past any trailing space or | |
1038 | ;; comment. Typically, we stop at the { which denotes the corresponding AWK | |
1039 | ;; action/function body. Otherwise we stop at the EOL (or ;) marking the | |
1040 | ;; absence of an explicit action. | |
0386b551 AM |
1041 | ;; |
1042 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
1043 | (while |
1044 | (progn | |
1045 | (search-forward-regexp c-awk-harmless-pattern-characters*) | |
1046 | (if (looking-at "#") (end-of-line)) | |
1047 | (cond | |
1048 | ((eobp) nil) | |
1049 | ((looking-at "[{;]") nil) ; We've finished! | |
1050 | ((eolp) | |
1051 | (if (c-awk-cur-line-incomplete-p) | |
1052 | (forward-line) ; returns non-nil | |
1053 | nil)) | |
1054 | ((search-forward-regexp c-awk-terminated-regexp-or-string-here-re nil t)) | |
1055 | ((search-forward-regexp c-awk-unterminated-regexp-or-string-here-re nil t)) | |
1056 | ((looking-at "/") (forward-char) t))))) ; division sign. | |
1057 | ||
1058 | (defun c-awk-end-of-defun1 () | |
1059 | ;; point is at the start of a "defun". Move to its end. Return end position. | |
0386b551 AM |
1060 | ;; |
1061 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
1062 | (c-awk-forward-awk-pattern) |
1063 | (cond | |
1064 | ((looking-at "{") (goto-char (scan-sexps (point) 1))) | |
1065 | ((looking-at ";") (forward-char)) | |
1066 | ((eolp)) | |
1067 | (t (error "c-awk-end-of-defun1: Failure of c-awk-forward-awk-pattern"))) | |
1068 | (point)) | |
1069 | ||
1070 | (defun c-awk-beginning-of-defun-p () | |
1071 | ;; Are we already at the beginning of a defun? (i.e. at code in column 0 | |
1072 | ;; which isn't a }, and isn't a continuation line of any sort. | |
0386b551 AM |
1073 | ;; |
1074 | ;; This function might do hidden buffer changes. | |
d9e94c22 MS |
1075 | (and (looking-at "^[^#} \t\n\r]") |
1076 | (not (c-awk-prev-line-incomplete-p)))) | |
1077 | ||
1078 | (defun c-awk-end-of-defun (&optional arg) | |
1079 | "Move forward to next end of defun. With argument, do it that many times. | |
1080 | Negative argument -N means move back to Nth preceding end of defun. | |
1081 | ||
1082 | An end of a defun occurs right after the closing brace that matches the | |
1083 | opening brace at its start, or immediately after the AWK pattern when there is | |
0386b551 AM |
1084 | no explicit action; see function `c-awk-beginning-of-defun'. |
1085 | ||
1086 | Note that this function might do hidden buffer changes. See the | |
1087 | comment at the start of cc-engine.el for more info." | |
d9e94c22 MS |
1088 | (interactive "p") |
1089 | (or arg (setq arg 1)) | |
1090 | (save-match-data | |
1091 | (c-save-buffer-state | |
1092 | nil | |
1093 | (let ((start-point (point)) end-point) | |
1094 | ;; Strategy: (For +ve ARG): If we're not already at a beginning-of-defun, | |
1095 | ;; move backwards to one. | |
1096 | ;; Repeat [(i) move forward to end-of-current-defun (see below); | |
1097 | ;; (ii) If this isn't it, move forward to beginning-of-defun]. | |
1098 | ;; We start counting ARG only when step (i) has passed the original point. | |
1099 | (when (> arg 0) | |
1100 | ;; Try to move back to a beginning-of-defun, if not already at one. | |
1101 | (if (not (c-awk-beginning-of-defun-p)) | |
1102 | (when (not (c-awk-beginning-of-defun 1)) ; No bo-defun before point. | |
1103 | (goto-char start-point) | |
1104 | (c-awk-beginning-of-defun -1))) ; if this fails, we're at EOB, tough! | |
1105 | ;; Now count forward, one defun at a time | |
1106 | (while (and (not (eobp)) | |
1107 | (c-awk-end-of-defun1) | |
1108 | (if (> (point) start-point) (setq arg (1- arg)) t) | |
1109 | (> arg 0) | |
1110 | (c-awk-beginning-of-defun -1)))) | |
1111 | ||
1112 | (when (< arg 0) | |
1113 | (setq end-point start-point) | |
1114 | (while (and (not (bobp)) | |
1115 | (c-awk-beginning-of-defun 1) | |
1116 | (if (< (setq end-point (if (bobp) (point) | |
1117 | (save-excursion (c-awk-end-of-defun1)))) | |
1118 | start-point) | |
1119 | (setq arg (1+ arg)) t) | |
1120 | (< arg 0))) | |
1121 | (goto-char (min start-point end-point))))))) | |
1122 | ||
0386b551 | 1123 | \f |
d9e94c22 | 1124 | (cc-provide 'cc-awk) ; Changed from 'awk-mode, ACM 2002/5/21 |
ab5796a9 | 1125 | |
d9e94c22 | 1126 | ;;; awk-mode.el ends here |