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