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