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