cc-awk.el 50 KB

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