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