cc-defs.el 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  1. ;;; cc-defs.el --- compile time definitions for CC Mode
  2. ;; Copyright (C) 1985, 1987, 1992-2012 Free Software Foundation, Inc.
  3. ;; Authors: 2003- Alan Mackenzie
  4. ;; 1998- Martin Stjernholm
  5. ;; 1992-1999 Barry A. Warsaw
  6. ;; 1987 Dave Detlefs
  7. ;; 1987 Stewart Clamen
  8. ;; 1985 Richard M. Stallman
  9. ;; Maintainer: bug-cc-mode@gnu.org
  10. ;; Created: 22-Apr-1997 (split from cc-mode.el)
  11. ;; Keywords: c languages
  12. ;; Package: cc-mode
  13. ;; This file is part of GNU Emacs.
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;; Commentary:
  25. ;; This file contains macros, defsubsts, and various other things that
  26. ;; must be loaded early both during compilation and at runtime.
  27. ;;; Code:
  28. (eval-when-compile
  29. (let ((load-path
  30. (if (and (boundp 'byte-compile-dest-file)
  31. (stringp byte-compile-dest-file))
  32. (cons (file-name-directory byte-compile-dest-file) load-path)
  33. load-path)))
  34. (load "cc-bytecomp" nil t)))
  35. (eval-when-compile (require 'cl)) ; was (cc-external-require 'cl). ACM 2005/11/29.
  36. (cc-external-require 'regexp-opt)
  37. ;; Silence the compiler.
  38. (cc-bytecomp-defvar c-enable-xemacs-performance-kludge-p) ; In cc-vars.el
  39. (cc-bytecomp-defun buffer-syntactic-context-depth) ; XEmacs
  40. (cc-bytecomp-defun region-active-p) ; XEmacs
  41. (cc-bytecomp-defvar zmacs-region-stays) ; XEmacs
  42. (cc-bytecomp-defvar zmacs-regions) ; XEmacs
  43. (cc-bytecomp-defvar mark-active) ; Emacs
  44. (cc-bytecomp-defvar deactivate-mark) ; Emacs
  45. (cc-bytecomp-defvar inhibit-point-motion-hooks) ; Emacs
  46. (cc-bytecomp-defvar parse-sexp-lookup-properties) ; Emacs
  47. (cc-bytecomp-defvar text-property-default-nonsticky) ; Emacs 21
  48. (cc-bytecomp-defvar lookup-syntax-properties) ; XEmacs
  49. (cc-bytecomp-defun string-to-syntax) ; Emacs 21
  50. ;; cc-fix.el contains compatibility macros that should be used if
  51. ;; needed.
  52. (eval-and-compile
  53. (if (or (/= (regexp-opt-depth "\\(\\(\\)\\)") 2)
  54. (not (fboundp 'push)))
  55. (cc-load "cc-fix")))
  56. ; (eval-after-load "font-lock" ; 2006-07-09. font-lock is now preloaded
  57. ; '
  58. (if (and (featurep 'xemacs) ; There is now (2005/12) code in GNU Emacs CVS
  59. ; to make the call to f-l-c-k throw an error.
  60. (not (featurep 'cc-fix)) ; only load the file once.
  61. (let (font-lock-keywords)
  62. (font-lock-compile-keywords '("\\<\\>"))
  63. font-lock-keywords)) ; did the previous call foul this up?
  64. (load "cc-fix")) ;)
  65. ;; The above takes care of the delayed loading, but this is necessary
  66. ;; to ensure correct byte compilation.
  67. (eval-when-compile
  68. (if (and (featurep 'xemacs)
  69. (not (featurep 'cc-fix))
  70. (progn
  71. (require 'font-lock)
  72. (let (font-lock-keywords)
  73. (font-lock-compile-keywords '("\\<\\>"))
  74. font-lock-keywords)))
  75. (cc-load "cc-fix")))
  76. ;;; Variables also used at compile time.
  77. (defconst c-version "5.32.3"
  78. "CC Mode version number.")
  79. (defconst c-version-sym (intern c-version))
  80. ;; A little more compact and faster in comparisons.
  81. (defvar c-buffer-is-cc-mode nil
  82. "Non-nil for all buffers with a major mode derived from CC Mode.
  83. Otherwise, this variable is nil. I.e. this variable is non-nil for
  84. `c-mode', `c++-mode', `objc-mode', `java-mode', `idl-mode',
  85. `pike-mode', `awk-mode', and any other non-CC Mode mode that calls
  86. `c-initialize-cc-mode'. The value is the mode symbol itself
  87. \(i.e. `c-mode' etc) of the original CC Mode mode, or just t if it's
  88. not known.")
  89. (make-variable-buffer-local 'c-buffer-is-cc-mode)
  90. ;; Have to make `c-buffer-is-cc-mode' permanently local so that it
  91. ;; survives the initialization of the derived mode.
  92. (put 'c-buffer-is-cc-mode 'permanent-local t)
  93. ;; The following is used below during compilation.
  94. (eval-and-compile
  95. (defvar c-inside-eval-when-compile nil)
  96. (defmacro cc-eval-when-compile (&rest body)
  97. "Like `progn', but evaluates the body at compile time.
  98. The result of the body appears to the compiler as a quoted constant.
  99. This variant works around bugs in `eval-when-compile' in various
  100. \(X)Emacs versions. See cc-defs.el for details."
  101. (if c-inside-eval-when-compile
  102. ;; XEmacs 21.4.6 has a bug in `eval-when-compile' in that it
  103. ;; evaluates its body at macro expansion time if it's nested
  104. ;; inside another `eval-when-compile'. So we use a dynamically
  105. ;; bound variable to avoid nesting them.
  106. `(progn ,@body)
  107. `(eval-when-compile
  108. ;; In all (X)Emacsen so far, `eval-when-compile' byte compiles
  109. ;; its contents before evaluating it. That can cause forms to
  110. ;; be compiled in situations they aren't intended to be
  111. ;; compiled.
  112. ;;
  113. ;; Example: It's not possible to defsubst a primitive, e.g. the
  114. ;; following will produce an error (in any emacs flavor), since
  115. ;; `nthcdr' is a primitive function that's handled specially by
  116. ;; the byte compiler and thus can't be redefined:
  117. ;;
  118. ;; (defsubst nthcdr (val) val)
  119. ;;
  120. ;; `defsubst', like `defmacro', needs to be evaluated at
  121. ;; compile time, so this will produce an error during byte
  122. ;; compilation.
  123. ;;
  124. ;; CC Mode occasionally needs to do things like this for
  125. ;; cross-emacs compatibility. It therefore uses the following
  126. ;; to conditionally do a `defsubst':
  127. ;;
  128. ;; (eval-when-compile
  129. ;; (if (not (fboundp 'foo))
  130. ;; (defsubst foo ...)))
  131. ;;
  132. ;; But `eval-when-compile' byte compiles its contents and
  133. ;; _then_ evaluates it (in all current emacs versions, up to
  134. ;; and including Emacs 20.6 and XEmacs 21.1 as of this
  135. ;; writing). So this will still produce an error, since the
  136. ;; byte compiler will get to the defsubst anyway. That's
  137. ;; arguably a bug because the point with `eval-when-compile' is
  138. ;; that it should evaluate rather than compile its contents.
  139. ;;
  140. ;; We get around it by expanding the body to a quoted
  141. ;; constant that we eval. That otoh introduce a problem in
  142. ;; that a returned lambda expression doesn't get byte
  143. ;; compiled (even if `function' is used).
  144. (eval '(let ((c-inside-eval-when-compile t)) ,@body)))))
  145. (put 'cc-eval-when-compile 'lisp-indent-hook 0))
  146. ;;; Macros.
  147. (defmacro c-point (position &optional point)
  148. "Return the value of certain commonly referenced POSITIONs relative to POINT.
  149. The current point is used if POINT isn't specified. POSITION can be
  150. one of the following symbols:
  151. `bol' -- beginning of line
  152. `eol' -- end of line
  153. `bod' -- beginning of defun
  154. `eod' -- end of defun
  155. `boi' -- beginning of indentation
  156. `ionl' -- indentation of next line
  157. `iopl' -- indentation of previous line
  158. `bonl' -- beginning of next line
  159. `eonl' -- end of next line
  160. `bopl' -- beginning of previous line
  161. `eopl' -- end of previous line
  162. `bosws' -- beginning of syntactic whitespace
  163. `eosws' -- end of syntactic whitespace
  164. If the referenced position doesn't exist, the closest accessible point
  165. to it is returned. This function does not modify the point or the mark."
  166. (if (eq (car-safe position) 'quote)
  167. (let ((position (eval position)))
  168. (cond
  169. ((eq position 'bol)
  170. (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
  171. `(line-beginning-position)
  172. `(save-excursion
  173. ,@(if point `((goto-char ,point)))
  174. (beginning-of-line)
  175. (point))))
  176. ((eq position 'eol)
  177. (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
  178. `(line-end-position)
  179. `(save-excursion
  180. ,@(if point `((goto-char ,point)))
  181. (end-of-line)
  182. (point))))
  183. ((eq position 'boi)
  184. `(save-excursion
  185. ,@(if point `((goto-char ,point)))
  186. (back-to-indentation)
  187. (point)))
  188. ((eq position 'bod)
  189. `(save-excursion
  190. ,@(if point `((goto-char ,point)))
  191. (c-beginning-of-defun-1)
  192. (point)))
  193. ((eq position 'eod)
  194. `(save-excursion
  195. ,@(if point `((goto-char ,point)))
  196. (c-end-of-defun-1)
  197. (point)))
  198. ((eq position 'bopl)
  199. (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
  200. `(line-beginning-position 0)
  201. `(save-excursion
  202. ,@(if point `((goto-char ,point)))
  203. (forward-line -1)
  204. (point))))
  205. ((eq position 'bonl)
  206. (if (and (cc-bytecomp-fboundp 'line-beginning-position) (not point))
  207. `(line-beginning-position 2)
  208. `(save-excursion
  209. ,@(if point `((goto-char ,point)))
  210. (forward-line 1)
  211. (point))))
  212. ((eq position 'eopl)
  213. (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
  214. `(line-end-position 0)
  215. `(save-excursion
  216. ,@(if point `((goto-char ,point)))
  217. (beginning-of-line)
  218. (or (bobp) (backward-char))
  219. (point))))
  220. ((eq position 'eonl)
  221. (if (and (cc-bytecomp-fboundp 'line-end-position) (not point))
  222. `(line-end-position 2)
  223. `(save-excursion
  224. ,@(if point `((goto-char ,point)))
  225. (forward-line 1)
  226. (end-of-line)
  227. (point))))
  228. ((eq position 'iopl)
  229. `(save-excursion
  230. ,@(if point `((goto-char ,point)))
  231. (forward-line -1)
  232. (back-to-indentation)
  233. (point)))
  234. ((eq position 'ionl)
  235. `(save-excursion
  236. ,@(if point `((goto-char ,point)))
  237. (forward-line 1)
  238. (back-to-indentation)
  239. (point)))
  240. ((eq position 'bosws)
  241. `(save-excursion
  242. ,@(if point `((goto-char ,point)))
  243. (c-backward-syntactic-ws)
  244. (point)))
  245. ((eq position 'eosws)
  246. `(save-excursion
  247. ,@(if point `((goto-char ,point)))
  248. (c-forward-syntactic-ws)
  249. (point)))
  250. (t (error "Unknown buffer position requested: %s" position))))
  251. ;; The bulk of this should perhaps be in a function to avoid large
  252. ;; expansions, but this case is not used anywhere in CC Mode (and
  253. ;; probably not anywhere else either) so we only have it to be on
  254. ;; the safe side.
  255. (message "Warning: c-point long expansion")
  256. `(save-excursion
  257. ,@(if point `((goto-char ,point)))
  258. (let ((position ,position))
  259. (cond
  260. ((eq position 'bol) (beginning-of-line))
  261. ((eq position 'eol) (end-of-line))
  262. ((eq position 'boi) (back-to-indentation))
  263. ((eq position 'bod) (c-beginning-of-defun-1))
  264. ((eq position 'eod) (c-end-of-defun-1))
  265. ((eq position 'bopl) (forward-line -1))
  266. ((eq position 'bonl) (forward-line 1))
  267. ((eq position 'eopl) (progn
  268. (beginning-of-line)
  269. (or (bobp) (backward-char))))
  270. ((eq position 'eonl) (progn
  271. (forward-line 1)
  272. (end-of-line)))
  273. ((eq position 'iopl) (progn
  274. (forward-line -1)
  275. (back-to-indentation)))
  276. ((eq position 'ionl) (progn
  277. (forward-line 1)
  278. (back-to-indentation)))
  279. ((eq position 'bosws) (c-backward-syntactic-ws))
  280. ((eq position 'eosws) (c-forward-syntactic-ws))
  281. (t (error "Unknown buffer position requested: %s" position))))
  282. (point))))
  283. (defmacro c-region-is-active-p ()
  284. ;; Return t when the region is active. The determination of region
  285. ;; activeness is different in both Emacs and XEmacs.
  286. (if (cc-bytecomp-boundp 'mark-active)
  287. ;; Emacs.
  288. 'mark-active
  289. ;; XEmacs.
  290. '(region-active-p)))
  291. (defmacro c-set-region-active (activate)
  292. ;; Activate the region if ACTIVE is non-nil, deactivate it
  293. ;; otherwise. Covers the differences between Emacs and XEmacs.
  294. (if (cc-bytecomp-fboundp 'zmacs-activate-region)
  295. ;; XEmacs.
  296. `(if ,activate
  297. (zmacs-activate-region)
  298. (zmacs-deactivate-region))
  299. ;; Emacs.
  300. `(setq mark-active ,activate)))
  301. (defmacro c-delete-and-extract-region (start end)
  302. "Delete the text between START and END and return it."
  303. (if (cc-bytecomp-fboundp 'delete-and-extract-region)
  304. ;; Emacs 21.1 and later
  305. `(delete-and-extract-region ,start ,end)
  306. ;; XEmacs and Emacs 20.x
  307. `(prog1
  308. (buffer-substring ,start ,end)
  309. (delete-region ,start ,end))))
  310. (defmacro c-safe (&rest body)
  311. ;; safely execute BODY, return nil if an error occurred
  312. `(condition-case nil
  313. (progn ,@body)
  314. (error nil)))
  315. (put 'c-safe 'lisp-indent-function 0)
  316. (defmacro c-int-to-char (integer)
  317. ;; In Emacs, a character is an integer. In XEmacs, a character is a
  318. ;; type distinct from an integer. Sometimes we need to convert integers to
  319. ;; characters. `c-int-to-char' makes this conversion, if necessary.
  320. (if (fboundp 'int-to-char)
  321. `(int-to-char ,integer)
  322. integer))
  323. (defmacro c-sentence-end ()
  324. ;; Get the regular expression `sentence-end'.
  325. (if (cc-bytecomp-fboundp 'sentence-end)
  326. ;; Emacs 22:
  327. `(sentence-end)
  328. ;; Emacs <22 + XEmacs
  329. `sentence-end))
  330. (defmacro c-default-value-sentence-end ()
  331. ;; Get the default value of the variable sentence end.
  332. (if (cc-bytecomp-fboundp 'sentence-end)
  333. ;; Emacs 22:
  334. `(let (sentence-end) (sentence-end))
  335. ;; Emacs <22 + XEmacs
  336. `(default-value 'sentence-end)))
  337. ;; The following is essentially `save-buffer-state' from lazy-lock.el.
  338. ;; It ought to be a standard macro.
  339. (defmacro c-save-buffer-state (varlist &rest body)
  340. "Bind variables according to VARLIST (in `let*' style) and eval BODY,
  341. then restore the buffer state under the assumption that no significant
  342. modification has been made in BODY. A change is considered
  343. significant if it affects the buffer text in any way that isn't
  344. completely restored again. Changes in text properties like `face' or
  345. `syntax-table' are considered insignificant. This macro allows text
  346. properties to be changed, even in a read-only buffer.
  347. This macro should be placed around all calculations which set
  348. \"insignificant\" text properties in a buffer, even when the buffer is
  349. known to be writable. That way, these text properties remain set
  350. even if the user undoes the command which set them.
  351. This macro should ALWAYS be placed around \"temporary\" internal buffer
  352. changes \(like adding a newline to calculate a text-property then
  353. deleting it again\), so that the user never sees them on his
  354. `buffer-undo-list'. See also `c-tentative-buffer-changes'.
  355. However, any user-visible changes to the buffer \(like auto-newlines\)
  356. must not be within a `c-save-buffer-state', since the user then
  357. wouldn't be able to undo them.
  358. The return value is the value of the last form in BODY."
  359. `(let* ((modified (buffer-modified-p)) (buffer-undo-list t)
  360. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  361. before-change-functions after-change-functions
  362. deactivate-mark
  363. buffer-file-name buffer-file-truename ; Prevent primitives checking
  364. ; for file modification
  365. ,@varlist)
  366. (unwind-protect
  367. (progn ,@body)
  368. (and (not modified)
  369. (buffer-modified-p)
  370. (set-buffer-modified-p nil)))))
  371. (put 'c-save-buffer-state 'lisp-indent-function 1)
  372. (defmacro c-tentative-buffer-changes (&rest body)
  373. "Eval BODY and optionally restore the buffer contents to the state it
  374. was in before BODY. Any changes are kept if the last form in BODY
  375. returns non-nil. Otherwise it's undone using the undo facility, and
  376. various other buffer state that might be affected by the changes is
  377. restored. That includes the current buffer, point, mark, mark
  378. activation \(similar to `save-excursion'), and the modified state.
  379. The state is also restored if BODY exits nonlocally.
  380. If BODY makes a change that unconditionally is undone then wrap this
  381. macro inside `c-save-buffer-state'. That way the change can be done
  382. even when the buffer is read-only, and without interference from
  383. various buffer change hooks."
  384. `(let (-tnt-chng-keep
  385. -tnt-chng-state)
  386. (unwind-protect
  387. ;; Insert an undo boundary for use with `undo-more'. We
  388. ;; don't use `undo-boundary' since it doesn't insert one
  389. ;; unconditionally.
  390. (setq buffer-undo-list (cons nil buffer-undo-list)
  391. -tnt-chng-state (c-tnt-chng-record-state)
  392. -tnt-chng-keep (progn ,@body))
  393. (c-tnt-chng-cleanup -tnt-chng-keep -tnt-chng-state))))
  394. (put 'c-tentative-buffer-changes 'lisp-indent-function 0)
  395. (defun c-tnt-chng-record-state ()
  396. ;; Used internally in `c-tentative-buffer-changes'.
  397. (vector buffer-undo-list ; 0
  398. (current-buffer) ; 1
  399. ;; No need to use markers for the point and mark; if the
  400. ;; undo got out of synch we're hosed anyway.
  401. (point) ; 2
  402. (mark t) ; 3
  403. (c-region-is-active-p) ; 4
  404. (buffer-modified-p))) ; 5
  405. (defun c-tnt-chng-cleanup (keep saved-state)
  406. ;; Used internally in `c-tentative-buffer-changes'.
  407. (let ((saved-undo-list (elt saved-state 0)))
  408. (if (eq buffer-undo-list saved-undo-list)
  409. ;; No change was done after all.
  410. (setq buffer-undo-list (cdr saved-undo-list))
  411. (if keep
  412. ;; Find and remove the undo boundary.
  413. (let ((p buffer-undo-list))
  414. (while (not (eq (cdr p) saved-undo-list))
  415. (setq p (cdr p)))
  416. (setcdr p (cdr saved-undo-list)))
  417. ;; `primitive-undo' will remove the boundary.
  418. (setq saved-undo-list (cdr saved-undo-list))
  419. (let ((undo-in-progress t))
  420. (while (not (eq (setq buffer-undo-list
  421. (primitive-undo 1 buffer-undo-list))
  422. saved-undo-list))))
  423. (when (buffer-live-p (elt saved-state 1))
  424. (set-buffer (elt saved-state 1))
  425. (goto-char (elt saved-state 2))
  426. (set-mark (elt saved-state 3))
  427. (c-set-region-active (elt saved-state 4))
  428. (and (not (elt saved-state 5))
  429. (buffer-modified-p)
  430. (set-buffer-modified-p nil)))))))
  431. (defmacro c-forward-syntactic-ws (&optional limit)
  432. "Forward skip over syntactic whitespace.
  433. Syntactic whitespace is defined as whitespace characters, comments,
  434. and preprocessor directives. However if point starts inside a comment
  435. or preprocessor directive, the content of it is not treated as
  436. whitespace.
  437. LIMIT sets an upper limit of the forward movement, if specified. If
  438. LIMIT or the end of the buffer is reached inside a comment or
  439. preprocessor directive, the point will be left there.
  440. Note that this function might do hidden buffer changes. See the
  441. comment at the start of cc-engine.el for more info."
  442. (if limit
  443. `(save-restriction
  444. (narrow-to-region (point-min) (or ,limit (point-max)))
  445. (c-forward-sws))
  446. '(c-forward-sws)))
  447. (defmacro c-backward-syntactic-ws (&optional limit)
  448. "Backward skip over syntactic whitespace.
  449. Syntactic whitespace is defined as whitespace characters, comments,
  450. and preprocessor directives. However if point starts inside a comment
  451. or preprocessor directive, the content of it is not treated as
  452. whitespace.
  453. LIMIT sets a lower limit of the backward movement, if specified. If
  454. LIMIT is reached inside a line comment or preprocessor directive then
  455. the point is moved into it past the whitespace at the end.
  456. Note that this function might do hidden buffer changes. See the
  457. comment at the start of cc-engine.el for more info."
  458. (if limit
  459. `(save-restriction
  460. (narrow-to-region (or ,limit (point-min)) (point-max))
  461. (c-backward-sws))
  462. '(c-backward-sws)))
  463. (defmacro c-forward-sexp (&optional count)
  464. "Move forward across COUNT balanced expressions.
  465. A negative COUNT means move backward. Signal an error if the move
  466. fails for any reason.
  467. This is like `forward-sexp' except that it isn't interactive and does
  468. not do any user friendly adjustments of the point and that it isn't
  469. susceptible to user configurations such as disabling of signals in
  470. certain situations."
  471. (or count (setq count 1))
  472. `(goto-char (scan-sexps (point) ,count)))
  473. (defmacro c-backward-sexp (&optional count)
  474. "See `c-forward-sexp' and reverse directions."
  475. (or count (setq count 1))
  476. `(c-forward-sexp ,(if (numberp count) (- count) `(- ,count))))
  477. (defmacro c-safe-scan-lists (from count depth &optional limit)
  478. "Like `scan-lists' but returns nil instead of signaling errors
  479. for unbalanced parens.
  480. A limit for the search may be given. FROM is assumed to be on the
  481. right side of it."
  482. (let ((res (if (featurep 'xemacs)
  483. `(scan-lists ,from ,count ,depth nil t)
  484. `(c-safe (scan-lists ,from ,count ,depth)))))
  485. (if limit
  486. `(save-restriction
  487. ,(if (numberp count)
  488. (if (< count 0)
  489. `(narrow-to-region ,limit (point-max))
  490. `(narrow-to-region (point-min) ,limit))
  491. `(if (< ,count 0)
  492. (narrow-to-region ,limit (point-max))
  493. (narrow-to-region (point-min) ,limit)))
  494. ,res)
  495. res)))
  496. ;; Wrappers for common scan-lists cases, mainly because it's almost
  497. ;; impossible to get a feel for how that function works.
  498. (defmacro c-go-list-forward ()
  499. "Move backward across one balanced group of parentheses.
  500. Return POINT when we succeed, NIL when we fail. In the latter case, leave
  501. point unmoved."
  502. `(c-safe (let ((endpos (scan-lists (point) 1 0)))
  503. (goto-char endpos)
  504. endpos)))
  505. (defmacro c-go-list-backward ()
  506. "Move backward across one balanced group of parentheses.
  507. Return POINT when we succeed, NIL when we fail. In the latter case, leave
  508. point unmoved."
  509. `(c-safe (let ((endpos (scan-lists (point) -1 0)))
  510. (goto-char endpos)
  511. endpos)))
  512. (defmacro c-up-list-forward (&optional pos limit)
  513. "Return the first position after the list sexp containing POS,
  514. or nil if no such position exists. The point is used if POS is left out.
  515. A limit for the search may be given. The start position is assumed to
  516. be before it."
  517. `(c-safe-scan-lists ,(or pos `(point)) 1 1 ,limit))
  518. (defmacro c-up-list-backward (&optional pos limit)
  519. "Return the position of the start of the list sexp containing POS,
  520. or nil if no such position exists. The point is used if POS is left out.
  521. A limit for the search may be given. The start position is assumed to
  522. be after it."
  523. `(c-safe-scan-lists ,(or pos `(point)) -1 1 ,limit))
  524. (defmacro c-down-list-forward (&optional pos limit)
  525. "Return the first position inside the first list sexp after POS,
  526. or nil if no such position exists. The point is used if POS is left out.
  527. A limit for the search may be given. The start position is assumed to
  528. be before it."
  529. `(c-safe-scan-lists ,(or pos `(point)) 1 -1 ,limit))
  530. (defmacro c-down-list-backward (&optional pos limit)
  531. "Return the last position inside the last list sexp before POS,
  532. or nil if no such position exists. The point is used if POS is left out.
  533. A limit for the search may be given. The start position is assumed to
  534. be after it."
  535. `(c-safe-scan-lists ,(or pos `(point)) -1 -1 ,limit))
  536. (defmacro c-go-up-list-forward (&optional pos limit)
  537. "Move the point to the first position after the list sexp containing POS,
  538. or containing the point if POS is left out. Return t if such a
  539. position exists, otherwise nil is returned and the point isn't moved.
  540. A limit for the search may be given. The start position is assumed to
  541. be before it."
  542. (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 1)) t)))
  543. (if limit
  544. `(save-restriction
  545. (narrow-to-region (point-min) ,limit)
  546. ,res)
  547. res)))
  548. (defmacro c-go-up-list-backward (&optional pos limit)
  549. "Move the point to the position of the start of the list sexp containing POS,
  550. or containing the point if POS is left out. Return t if such a
  551. position exists, otherwise nil is returned and the point isn't moved.
  552. A limit for the search may be given. The start position is assumed to
  553. be after it."
  554. (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 1)) t)))
  555. (if limit
  556. `(save-restriction
  557. (narrow-to-region ,limit (point-max))
  558. ,res)
  559. res)))
  560. (defmacro c-go-down-list-forward (&optional pos limit)
  561. "Move the point to the first position inside the first list sexp after POS,
  562. or before the point if POS is left out. Return t if such a position
  563. exists, otherwise nil is returned and the point isn't moved.
  564. A limit for the search may be given. The start position is assumed to
  565. be before it."
  566. (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) 1 -1)) t)))
  567. (if limit
  568. `(save-restriction
  569. (narrow-to-region (point-min) ,limit)
  570. ,res)
  571. res)))
  572. (defmacro c-go-down-list-backward (&optional pos limit)
  573. "Move the point to the last position inside the last list sexp before POS,
  574. or before the point if POS is left out. Return t if such a position
  575. exists, otherwise nil is returned and the point isn't moved.
  576. A limit for the search may be given. The start position is assumed to
  577. be after it."
  578. (let ((res `(c-safe (goto-char (scan-lists ,(or pos `(point)) -1 -1)) t)))
  579. (if limit
  580. `(save-restriction
  581. (narrow-to-region ,limit (point-max))
  582. ,res)
  583. res)))
  584. (defmacro c-beginning-of-defun-1 ()
  585. ;; Wrapper around beginning-of-defun.
  586. ;;
  587. ;; NOTE: This function should contain the only explicit use of
  588. ;; beginning-of-defun in CC Mode. Eventually something better than
  589. ;; b-o-d will be available and this should be the only place the
  590. ;; code needs to change. Everything else should use
  591. ;; (c-beginning-of-defun-1)
  592. ;;
  593. ;; This is really a bit too large to be a macro but that isn't a
  594. ;; problem as long as it only is used in one place in
  595. ;; `c-parse-state'.
  596. `(progn
  597. (if (and ,(cc-bytecomp-fboundp 'buffer-syntactic-context-depth)
  598. c-enable-xemacs-performance-kludge-p)
  599. ,(when (cc-bytecomp-fboundp 'buffer-syntactic-context-depth)
  600. ;; XEmacs only. This can improve the performance of
  601. ;; c-parse-state to between 3 and 60 times faster when
  602. ;; braces are hung. It can also degrade performance by
  603. ;; about as much when braces are not hung.
  604. '(let (beginning-of-defun-function end-of-defun-function
  605. pos)
  606. (while (not pos)
  607. (save-restriction
  608. (widen)
  609. (setq pos (c-safe-scan-lists
  610. (point) -1 (buffer-syntactic-context-depth))))
  611. (cond
  612. ((bobp) (setq pos (point-min)))
  613. ((not pos)
  614. (let ((distance (skip-chars-backward "^{")))
  615. ;; unbalanced parenthesis, while invalid C code,
  616. ;; shouldn't cause an infloop! See unbal.c
  617. (when (zerop distance)
  618. ;; Punt!
  619. (beginning-of-defun)
  620. (setq pos (point)))))
  621. ((= pos 0))
  622. ((not (eq (char-after pos) ?{))
  623. (goto-char pos)
  624. (setq pos nil))
  625. ))
  626. (goto-char pos)))
  627. ;; Emacs, which doesn't have buffer-syntactic-context-depth
  628. (let (beginning-of-defun-function end-of-defun-function)
  629. (beginning-of-defun)))
  630. ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at the
  631. ;; open brace.
  632. (and defun-prompt-regexp
  633. (looking-at defun-prompt-regexp)
  634. (goto-char (match-end 0)))))
  635. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  636. ;; V i r t u a l S e m i c o l o n s
  637. ;;
  638. ;; In most CC Mode languages, statements are terminated explicitly by
  639. ;; semicolons or closing braces. In some of the CC modes (currently AWK Mode
  640. ;; and certain user-specified #define macros in C, C++, etc. (November 2008)),
  641. ;; statements are (or can be) terminated by EOLs. Such a statement is said to
  642. ;; be terminated by a "virtual semicolon" (VS). A statement terminated by an
  643. ;; actual semicolon or brace is never considered to have a VS.
  644. ;;
  645. ;; The indentation engine (or whatever) tests for a VS at a specific position
  646. ;; by invoking the macro `c-at-vsemi-p', which in its turn calls the mode
  647. ;; specific function (if any) which is the value of the language variable
  648. ;; `c-at-vsemi-p-fn'. This function should only use "low-level" features of
  649. ;; CC Mode, i.e. features which won't trigger infinite recursion. ;-) The
  650. ;; actual details of what constitutes a VS in a language are thus encapsulated
  651. ;; in code specific to that language (e.g. cc-awk.el). `c-at-vsemi-p' returns
  652. ;; non-nil if point (or the optional parameter POS) is at a VS, nil otherwise.
  653. ;;
  654. ;; The language specific function might well do extensive analysis of the
  655. ;; source text, and may use a caching scheme to speed up repeated calls.
  656. ;;
  657. ;; The "virtual semicolon" lies just after the last non-ws token on the line.
  658. ;; Like POINT, it is considered to lie between two characters. For example,
  659. ;; at the place shown in the following AWK source line:
  660. ;;
  661. ;; kbyte = 1024 # 1000 if you're not picky
  662. ;; ^
  663. ;; |
  664. ;; Virtual Semicolon
  665. ;;
  666. ;; In addition to `c-at-vsemi-p-fn', a mode may need to supply a function for
  667. ;; `c-vsemi-status-unknown-p-fn'. The macro `c-vsemi-status-unknown-p' is a
  668. ;; rather recondite kludge. It exists because the function
  669. ;; `c-beginning-of-statement-1' sometimes tests for VSs as an optimization,
  670. ;; but `c-at-vsemi-p' might well need to call `c-beginning-of-statement-1' in
  671. ;; its calculations, thus potentially leading to infinite recursion.
  672. ;;
  673. ;; The macro `c-vsemi-status-unknown-p' resolves this problem; it may return
  674. ;; non-nil at any time; returning nil is a guarantee that an immediate
  675. ;; invocation of `c-at-vsemi-p' at point will NOT call
  676. ;; `c-beginning-of-statement-1'. `c-vsemi-status-unknown-p' may not itself
  677. ;; call `c-beginning-of-statement-1'.
  678. ;;
  679. ;; The macro `c-vsemi-status-unknown-p' will typically check the caching
  680. ;; scheme used by the `c-at-vsemi-p-fn', hence the name - the status is
  681. ;; "unknown" if there is no cache entry current for the line.
  682. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  683. (defmacro c-at-vsemi-p (&optional pos)
  684. ;; Is there a virtual semicolon (not a real one or a }) at POS (defaults to
  685. ;; point)? Always returns nil for languages which don't have Virtual
  686. ;; semicolons.
  687. ;; This macro might do hidden buffer changes.
  688. `(if c-at-vsemi-p-fn
  689. (funcall c-at-vsemi-p-fn ,@(if pos `(,pos)))))
  690. (defmacro c-vsemi-status-unknown-p ()
  691. ;; Return NIL only if it can be guaranteed that an immediate
  692. ;; (c-at-vsemi-p) will NOT call c-beginning-of-statement-1. Otherwise,
  693. ;; return non-nil. (See comments above). The function invoked by this
  694. ;; macro MUST NOT UNDER ANY CIRCUMSTANCES itself call
  695. ;; c-beginning-of-statement-1.
  696. ;; Languages which don't have EOL terminated statements always return NIL
  697. ;; (they _know_ there's no vsemi ;-).
  698. `(if c-vsemi-status-unknown-p-fn (funcall c-vsemi-status-unknown-p-fn)))
  699. (defmacro c-benign-error (format &rest args)
  700. ;; Formats an error message for the echo area and dings, i.e. like
  701. ;; `error' but doesn't abort.
  702. `(progn
  703. (message ,format ,@args)
  704. (ding)))
  705. (defmacro c-with-syntax-table (table &rest code)
  706. ;; Temporarily switches to the specified syntax table in a failsafe
  707. ;; way to execute code.
  708. `(let ((c-with-syntax-table-orig-table (syntax-table)))
  709. (unwind-protect
  710. (progn
  711. (set-syntax-table ,table)
  712. ,@code)
  713. (set-syntax-table c-with-syntax-table-orig-table))))
  714. (put 'c-with-syntax-table 'lisp-indent-function 1)
  715. (defmacro c-skip-ws-forward (&optional limit)
  716. "Skip over any whitespace following point.
  717. This function skips over horizontal and vertical whitespace and line
  718. continuations."
  719. (if limit
  720. `(let ((limit (or ,limit (point-max))))
  721. (while (progn
  722. ;; skip-syntax-* doesn't count \n as whitespace..
  723. (skip-chars-forward " \t\n\r\f\v" limit)
  724. (when (and (eq (char-after) ?\\)
  725. (< (point) limit))
  726. (forward-char)
  727. (or (eolp)
  728. (progn (backward-char) nil))))))
  729. '(while (progn
  730. (skip-chars-forward " \t\n\r\f\v")
  731. (when (eq (char-after) ?\\)
  732. (forward-char)
  733. (or (eolp)
  734. (progn (backward-char) nil)))))))
  735. (defmacro c-skip-ws-backward (&optional limit)
  736. "Skip over any whitespace preceding point.
  737. This function skips over horizontal and vertical whitespace and line
  738. continuations."
  739. (if limit
  740. `(let ((limit (or ,limit (point-min))))
  741. (while (progn
  742. ;; skip-syntax-* doesn't count \n as whitespace..
  743. (skip-chars-backward " \t\n\r\f\v" limit)
  744. (and (eolp)
  745. (eq (char-before) ?\\)
  746. (> (point) limit)))
  747. (backward-char)))
  748. '(while (progn
  749. (skip-chars-backward " \t\n\r\f\v")
  750. (and (eolp)
  751. (eq (char-before) ?\\)))
  752. (backward-char))))
  753. (eval-and-compile
  754. (defvar c-langs-are-parametric nil))
  755. (defmacro c-major-mode-is (mode)
  756. "Return non-nil if the current CC Mode major mode is MODE.
  757. MODE is either a mode symbol or a list of mode symbols."
  758. (if c-langs-are-parametric
  759. ;; Inside a `c-lang-defconst'.
  760. `(c-lang-major-mode-is ,mode)
  761. (if (eq (car-safe mode) 'quote)
  762. (let ((mode (eval mode)))
  763. (if (listp mode)
  764. `(memq c-buffer-is-cc-mode ',mode)
  765. `(eq c-buffer-is-cc-mode ',mode)))
  766. `(let ((mode ,mode))
  767. (if (listp mode)
  768. (memq c-buffer-is-cc-mode mode)
  769. (eq c-buffer-is-cc-mode mode))))))
  770. ;; Macros/functions to handle so-called "char properties", which are
  771. ;; properties set on a single character and that never spread to any
  772. ;; other characters.
  773. (eval-and-compile
  774. ;; Constant used at compile time to decide whether or not to use
  775. ;; XEmacs extents. Check all the extent functions we'll use since
  776. ;; some packages might add compatibility aliases for some of them in
  777. ;; Emacs.
  778. (defconst c-use-extents (and (cc-bytecomp-fboundp 'extent-at)
  779. (cc-bytecomp-fboundp 'set-extent-property)
  780. (cc-bytecomp-fboundp 'set-extent-properties)
  781. (cc-bytecomp-fboundp 'make-extent)
  782. (cc-bytecomp-fboundp 'extent-property)
  783. (cc-bytecomp-fboundp 'delete-extent)
  784. (cc-bytecomp-fboundp 'map-extents))))
  785. ;; `c-put-char-property' is complex enough in XEmacs and Emacs < 21 to
  786. ;; make it a function.
  787. (defalias 'c-put-char-property-fun
  788. (cc-eval-when-compile
  789. (cond (c-use-extents
  790. ;; XEmacs.
  791. (byte-compile
  792. (lambda (pos property value)
  793. (let ((ext (extent-at pos nil property)))
  794. (if ext
  795. (set-extent-property ext property value)
  796. (set-extent-properties (make-extent pos (1+ pos))
  797. (cons property
  798. (cons value
  799. '(start-open t
  800. end-open t)))))))))
  801. ((not (cc-bytecomp-boundp 'text-property-default-nonsticky))
  802. ;; In Emacs < 21 we have to mess with the `rear-nonsticky' property.
  803. (byte-compile
  804. (lambda (pos property value)
  805. (put-text-property pos (1+ pos) property value)
  806. (let ((prop (get-text-property pos 'rear-nonsticky)))
  807. (or (memq property prop)
  808. (put-text-property pos (1+ pos)
  809. 'rear-nonsticky
  810. (cons property prop)))))))
  811. ;; This won't be used for anything.
  812. (t 'ignore))))
  813. (cc-bytecomp-defun c-put-char-property-fun) ; Make it known below.
  814. (defmacro c-put-char-property (pos property value)
  815. ;; Put the given property with the given value on the character at
  816. ;; POS and make it front and rear nonsticky, or start and end open
  817. ;; in XEmacs vocabulary. If the character already has the given
  818. ;; property then the value is replaced, and the behavior is
  819. ;; undefined if that property has been put by some other function.
  820. ;; PROPERTY is assumed to be constant.
  821. ;;
  822. ;; If there's a `text-property-default-nonsticky' variable (Emacs
  823. ;; 21) then it's assumed that the property is present on it.
  824. ;;
  825. ;; This macro does a hidden buffer change.
  826. (setq property (eval property))
  827. (if (or c-use-extents
  828. (not (cc-bytecomp-boundp 'text-property-default-nonsticky)))
  829. ;; XEmacs and Emacs < 21.
  830. `(c-put-char-property-fun ,pos ',property ,value)
  831. ;; In Emacs 21 we got the `rear-nonsticky' property covered
  832. ;; by `text-property-default-nonsticky'.
  833. `(let ((-pos- ,pos))
  834. (put-text-property -pos- (1+ -pos-) ',property ,value))))
  835. (defmacro c-get-char-property (pos property)
  836. ;; Get the value of the given property on the character at POS if
  837. ;; it's been put there by `c-put-char-property'. PROPERTY is
  838. ;; assumed to be constant.
  839. (setq property (eval property))
  840. (if c-use-extents
  841. ;; XEmacs.
  842. `(let ((ext (extent-at ,pos nil ',property)))
  843. (if ext (extent-property ext ',property)))
  844. ;; Emacs.
  845. `(get-text-property ,pos ',property)))
  846. ;; `c-clear-char-property' is complex enough in Emacs < 21 to make it
  847. ;; a function, since we have to mess with the `rear-nonsticky' property.
  848. (defalias 'c-clear-char-property-fun
  849. (cc-eval-when-compile
  850. (unless (or c-use-extents
  851. (cc-bytecomp-boundp 'text-property-default-nonsticky))
  852. (byte-compile
  853. (lambda (pos property)
  854. (when (get-text-property pos property)
  855. (remove-text-properties pos (1+ pos) (list property nil))
  856. (put-text-property pos (1+ pos)
  857. 'rear-nonsticky
  858. (delq property (get-text-property
  859. pos 'rear-nonsticky)))))))))
  860. (cc-bytecomp-defun c-clear-char-property-fun) ; Make it known below.
  861. (defmacro c-clear-char-property (pos property)
  862. ;; Remove the given property on the character at POS if it's been put
  863. ;; there by `c-put-char-property'. PROPERTY is assumed to be
  864. ;; constant.
  865. ;;
  866. ;; This macro does a hidden buffer change.
  867. (setq property (eval property))
  868. (cond (c-use-extents
  869. ;; XEmacs.
  870. `(let ((ext (extent-at ,pos nil ',property)))
  871. (if ext (delete-extent ext))))
  872. ((cc-bytecomp-boundp 'text-property-default-nonsticky)
  873. ;; In Emacs 21 we got the `rear-nonsticky' property covered
  874. ;; by `text-property-default-nonsticky'.
  875. `(let ((pos ,pos))
  876. (remove-text-properties pos (1+ pos)
  877. '(,property nil))))
  878. (t
  879. ;; Emacs < 21.
  880. `(c-clear-char-property-fun ,pos ',property))))
  881. (defmacro c-clear-char-properties (from to property)
  882. ;; Remove all the occurrences of the given property in the given
  883. ;; region that has been put with `c-put-char-property'. PROPERTY is
  884. ;; assumed to be constant.
  885. ;;
  886. ;; Note that this function does not clean up the property from the
  887. ;; lists of the `rear-nonsticky' properties in the region, if such
  888. ;; are used. Thus it should not be used for common properties like
  889. ;; `syntax-table'.
  890. ;;
  891. ;; This macro does hidden buffer changes.
  892. (setq property (eval property))
  893. (if c-use-extents
  894. ;; XEmacs.
  895. `(map-extents (lambda (ext ignored)
  896. (delete-extent ext))
  897. nil ,from ,to nil nil ',property)
  898. ;; Emacs.
  899. `(remove-text-properties ,from ,to '(,property nil))))
  900. (defmacro c-search-forward-char-property (property value &optional limit)
  901. "Search forward for a text-property PROPERTY having value VALUE.
  902. LIMIT bounds the search. The comparison is done with `equal'.
  903. Leave point just after the character, and set the match data on
  904. this character, and return point. If VALUE isn't found, Return
  905. nil; point is then left undefined."
  906. `(let ((place (point)))
  907. (while
  908. (and
  909. (< place ,(or limit '(point-max)))
  910. (not (equal (get-text-property place ,property) ,value)))
  911. (setq place (next-single-property-change
  912. place ,property nil ,(or limit '(point-max)))))
  913. (when (< place ,(or limit '(point-max)))
  914. (goto-char place)
  915. (search-forward-regexp ".") ; to set the match-data.
  916. (point))))
  917. (defmacro c-search-backward-char-property (property value &optional limit)
  918. "Search backward for a text-property PROPERTY having value VALUE.
  919. LIMIT bounds the search. The comparison is done with `equal'.
  920. Leave point just before the character, set the match data on this
  921. character, and return point. If VALUE isn't found, Return nil;
  922. point is then left undefined."
  923. `(let ((place (point)))
  924. (while
  925. (and
  926. (> place ,(or limit '(point-min)))
  927. (not (equal (get-text-property (1- place) ,property) ,value)))
  928. (setq place (previous-single-property-change
  929. place ,property nil ,(or limit '(point-min)))))
  930. (when (> place ,(or limit '(point-max)))
  931. (goto-char place)
  932. (search-backward-regexp ".") ; to set the match-data.
  933. (point))))
  934. (defun c-clear-char-property-with-value-function (from to property value)
  935. "Remove all text-properties PROPERTY from the region (FROM, TO)
  936. which have the value VALUE, as tested by `equal'. These
  937. properties are assumed to be over individual characters, having
  938. been put there by c-put-char-property. POINT remains unchanged."
  939. (let ((place from) end-place)
  940. (while ; loop round occurrences of (PROPERTY VALUE)
  941. (progn
  942. (while ; loop round changes in PROPERTY till we find VALUE
  943. (and
  944. (< place to)
  945. (not (equal (get-text-property place property) value)))
  946. (setq place (next-single-property-change place property nil to)))
  947. (< place to))
  948. (setq end-place (next-single-property-change place property nil to))
  949. (remove-text-properties place end-place (cons property nil))
  950. ;; Do we have to do anything with stickiness here?
  951. (setq place end-place))))
  952. (defmacro c-clear-char-property-with-value (from to property value)
  953. "Remove all text-properties PROPERTY from the region [FROM, TO)
  954. which have the value VALUE, as tested by `equal'. These
  955. properties are assumed to be over individual characters, having
  956. been put there by c-put-char-property. POINT remains unchanged."
  957. (if c-use-extents
  958. ;; XEmacs
  959. `(let ((-property- ,property))
  960. (map-extents (lambda (ext val)
  961. (if (equal (extent-property ext -property-) val)
  962. (delete-extent ext)))
  963. nil ,from ,to ,value nil -property-))
  964. ;; Gnu Emacs
  965. `(c-clear-char-property-with-value-function ,from ,to ,property ,value)))
  966. ;; Macros to put overlays (Emacs) or extents (XEmacs) on buffer text.
  967. ;; For our purposes, these are characterized by being possible to
  968. ;; remove again without affecting the other text properties in the
  969. ;; buffer that got overridden when they were put.
  970. (defmacro c-put-overlay (from to property value)
  971. ;; Put an overlay/extent covering the given range in the current
  972. ;; buffer. It's currently undefined whether it's front/end sticky
  973. ;; or not. The overlay/extent object is returned.
  974. (if (cc-bytecomp-fboundp 'make-overlay)
  975. ;; Emacs.
  976. `(let ((ol (make-overlay ,from ,to)))
  977. (overlay-put ol ,property ,value)
  978. ol)
  979. ;; XEmacs.
  980. `(let ((ext (make-extent ,from ,to)))
  981. (set-extent-property ext ,property ,value)
  982. ext)))
  983. (defmacro c-delete-overlay (overlay)
  984. ;; Deletes an overlay/extent object previously retrieved using
  985. ;; `c-put-overlay'.
  986. (if (cc-bytecomp-fboundp 'make-overlay)
  987. ;; Emacs.
  988. `(delete-overlay ,overlay)
  989. ;; XEmacs.
  990. `(delete-extent ,overlay)))
  991. ;; Make edebug understand the macros.
  992. ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
  993. ; '(progn
  994. (def-edebug-spec cc-eval-when-compile t)
  995. (def-edebug-spec c-point t)
  996. (def-edebug-spec c-set-region-active t)
  997. (def-edebug-spec c-safe t)
  998. (def-edebug-spec c-save-buffer-state let*)
  999. (def-edebug-spec c-tentative-buffer-changes t)
  1000. (def-edebug-spec c-forward-syntactic-ws t)
  1001. (def-edebug-spec c-backward-syntactic-ws t)
  1002. (def-edebug-spec c-forward-sexp t)
  1003. (def-edebug-spec c-backward-sexp t)
  1004. (def-edebug-spec c-up-list-forward t)
  1005. (def-edebug-spec c-up-list-backward t)
  1006. (def-edebug-spec c-down-list-forward t)
  1007. (def-edebug-spec c-down-list-backward t)
  1008. (def-edebug-spec c-add-syntax t)
  1009. (def-edebug-spec c-add-class-syntax t)
  1010. (def-edebug-spec c-benign-error t)
  1011. (def-edebug-spec c-with-syntax-table t)
  1012. (def-edebug-spec c-skip-ws-forward t)
  1013. (def-edebug-spec c-skip-ws-backward t)
  1014. (def-edebug-spec c-major-mode-is t)
  1015. (def-edebug-spec c-put-char-property t)
  1016. (def-edebug-spec c-get-char-property t)
  1017. (def-edebug-spec c-clear-char-property t)
  1018. (def-edebug-spec c-clear-char-properties t)
  1019. (def-edebug-spec c-put-overlay t)
  1020. (def-edebug-spec c-delete-overlay t) ;))
  1021. ;;; Functions.
  1022. ;; Note: All these after the macros, to be on safe side in avoiding
  1023. ;; bugs where macros are defined too late. These bugs often only show
  1024. ;; when the files are compiled in a certain order within the same
  1025. ;; session.
  1026. (defsubst c-end-of-defun-1 ()
  1027. ;; Replacement for end-of-defun that use c-beginning-of-defun-1.
  1028. (let ((start (point)))
  1029. ;; Skip forward into the next defun block. Don't bother to avoid
  1030. ;; comments, literals etc, since beginning-of-defun doesn't do that
  1031. ;; anyway.
  1032. (skip-chars-forward "^}")
  1033. (c-beginning-of-defun-1)
  1034. (if (eq (char-after) ?{)
  1035. (c-forward-sexp))
  1036. (if (< (point) start)
  1037. (goto-char (point-max)))))
  1038. (defconst c-<-as-paren-syntax '(4 . ?>))
  1039. (put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
  1040. (defsubst c-mark-<-as-paren (pos)
  1041. ;; Mark the "<" character at POS as a template opener using the
  1042. ;; `syntax-table' property via the `category' property.
  1043. ;;
  1044. ;; This function does a hidden buffer change. Note that we use
  1045. ;; indirection through the `category' text property. This allows us to
  1046. ;; toggle the property in all template brackets simultaneously and
  1047. ;; cheaply. We use this, for instance, in `c-parse-state'.
  1048. (c-put-char-property pos 'category 'c-<-as-paren-syntax))
  1049. (defconst c->-as-paren-syntax '(5 . ?<))
  1050. (put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax)
  1051. (defsubst c-mark->-as-paren (pos)
  1052. ;; Mark the ">" character at POS as an sexp list closer using the
  1053. ;; syntax-table property.
  1054. ;;
  1055. ;; This function does a hidden buffer change. Note that we use
  1056. ;; indirection through the `category' text property. This allows us to
  1057. ;; toggle the property in all template brackets simultaneously and
  1058. ;; cheaply. We use this, for instance, in `c-parse-state'.
  1059. (c-put-char-property pos 'category 'c->-as-paren-syntax))
  1060. (defsubst c-unmark-<->-as-paren (pos)
  1061. ;; Unmark the "<" or "<" character at POS as an sexp list opener using
  1062. ;; the syntax-table property indirectly through the `category' text
  1063. ;; property.
  1064. ;;
  1065. ;; This function does a hidden buffer change. Note that we use
  1066. ;; indirection through the `category' text property. This allows us to
  1067. ;; toggle the property in all template brackets simultaneously and
  1068. ;; cheaply. We use this, for instance, in `c-parse-state'.
  1069. (c-clear-char-property pos 'category))
  1070. (defsubst c-suppress-<->-as-parens ()
  1071. ;; Suppress the syntactic effect of all marked < and > as parens. Note
  1072. ;; that this effect is NOT buffer local. You should probably not use
  1073. ;; this directly, but only through the macro
  1074. ;; `c-with-<->-as-parens-suppressed'
  1075. (put 'c-<-as-paren-syntax 'syntax-table nil)
  1076. (put 'c->-as-paren-syntax 'syntax-table nil))
  1077. (defsubst c-restore-<->-as-parens ()
  1078. ;; Restore the syntactic effect of all marked <s and >s as parens. This
  1079. ;; has no effect on unmarked <s and >s
  1080. (put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
  1081. (put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax))
  1082. (defmacro c-with-<->-as-parens-suppressed (&rest forms)
  1083. ;; Like progn, except that the paren property is suppressed on all
  1084. ;; template brackets whilst they are running. This macro does a hidden
  1085. ;; buffer change.
  1086. `(unwind-protect
  1087. (progn
  1088. (c-suppress-<->-as-parens)
  1089. ,@forms)
  1090. (c-restore-<->-as-parens)))
  1091. ;;;;;;;;;;;;;;;
  1092. (defconst c-cpp-delimiter '(14)) ; generic comment syntax
  1093. ;; This is the value of the `category' text property placed on every #
  1094. ;; which introduces a CPP construct and every EOL (or EOB, or character
  1095. ;; preceding //, etc.) which terminates it. We can instantly "comment
  1096. ;; out" all CPP constructs by giving `c-cpp-delimiter' a syntax-table
  1097. ;; property '(14) (generic comment delimiter).
  1098. (defmacro c-set-cpp-delimiters (beg end)
  1099. ;; This macro does a hidden buffer change.
  1100. `(progn
  1101. (c-put-char-property ,beg 'category 'c-cpp-delimiter)
  1102. (if (< ,end (point-max))
  1103. (c-put-char-property ,end 'category 'c-cpp-delimiter))))
  1104. (defmacro c-clear-cpp-delimiters (beg end)
  1105. ;; This macro does a hidden buffer change.
  1106. `(progn
  1107. (c-clear-char-property ,beg 'category)
  1108. (if (< ,end (point-max))
  1109. (c-clear-char-property ,end 'category))))
  1110. (defsubst c-comment-out-cpps ()
  1111. ;; Render all preprocessor constructs syntactically commented out.
  1112. (put 'c-cpp-delimiter 'syntax-table c-cpp-delimiter))
  1113. (defsubst c-uncomment-out-cpps ()
  1114. ;; Restore the syntactic visibility of preprocessor constructs.
  1115. (put 'c-cpp-delimiter 'syntax-table nil))
  1116. (defmacro c-with-cpps-commented-out (&rest forms)
  1117. ;; Execute FORMS... whilst the syntactic effect of all characters in
  1118. ;; all CPP regions is suppressed. In particular, this is to suppress
  1119. ;; the syntactic significance of parens/braces/brackets to functions
  1120. ;; such as `scan-lists' and `parse-partial-sexp'.
  1121. `(unwind-protect
  1122. (c-save-buffer-state ()
  1123. (c-comment-out-cpps)
  1124. ,@forms)
  1125. (c-save-buffer-state ()
  1126. (c-uncomment-out-cpps))))
  1127. (defmacro c-with-all-but-one-cpps-commented-out (beg end &rest forms)
  1128. ;; Execute FORMS... whilst the syntactic effect of all characters in
  1129. ;; every CPP region APART FROM THE ONE BETWEEN BEG and END is
  1130. ;; suppressed.
  1131. `(unwind-protect
  1132. (c-save-buffer-state ()
  1133. (c-clear-cpp-delimiters ,beg ,end)
  1134. ,`(c-with-cpps-commented-out ,@forms))
  1135. (c-save-buffer-state ()
  1136. (c-set-cpp-delimiters ,beg ,end))))
  1137. (defsubst c-intersect-lists (list alist)
  1138. ;; return the element of ALIST that matches the first element found
  1139. ;; in LIST. Uses assq.
  1140. (let (match)
  1141. (while (and list
  1142. (not (setq match (assq (car list) alist))))
  1143. (setq list (cdr list)))
  1144. match))
  1145. (defsubst c-lookup-lists (list alist1 alist2)
  1146. ;; first, find the first entry from LIST that is present in ALIST1,
  1147. ;; then find the entry in ALIST2 for that entry.
  1148. (assq (car (c-intersect-lists list alist1)) alist2))
  1149. (defsubst c-langelem-sym (langelem)
  1150. "Return the syntactic symbol in LANGELEM.
  1151. LANGELEM is either a cons cell on the \"old\" form given as the first
  1152. argument to lineup functions or a syntactic element on the \"new\"
  1153. form as used in `c-syntactic-element'."
  1154. (car langelem))
  1155. (defsubst c-langelem-pos (langelem)
  1156. "Return the anchor position in LANGELEM, or nil if there is none.
  1157. LANGELEM is either a cons cell on the \"old\" form given as the first
  1158. argument to lineup functions or a syntactic element on the \"new\"
  1159. form as used in `c-syntactic-element'."
  1160. (if (consp (cdr langelem))
  1161. (car-safe (cdr langelem))
  1162. (cdr langelem)))
  1163. (defun c-langelem-col (langelem &optional preserve-point)
  1164. "Return the column of the anchor position in LANGELEM.
  1165. Also move the point to that position unless PRESERVE-POINT is non-nil.
  1166. LANGELEM is either a cons cell on the \"old\" form given as the first
  1167. argument to lineup functions or a syntactic element on the \"new\"
  1168. form as used in `c-syntactic-element'."
  1169. (let ((pos (c-langelem-pos langelem))
  1170. (here (point)))
  1171. (if pos
  1172. (progn
  1173. (goto-char pos)
  1174. (prog1 (current-column)
  1175. (if preserve-point
  1176. (goto-char here))))
  1177. 0)))
  1178. (defsubst c-langelem-2nd-pos (langelem)
  1179. "Return the secondary position in LANGELEM, or nil if there is none.
  1180. LANGELEM is typically a syntactic element on the \"new\" form as used
  1181. in `c-syntactic-element'. It may also be a cons cell as passed in the
  1182. first argument to lineup functions, but then the returned value always
  1183. will be nil."
  1184. (car-safe (cdr-safe (cdr-safe langelem))))
  1185. (defsubst c-keep-region-active ()
  1186. ;; Do whatever is necessary to keep the region active in XEmacs.
  1187. ;; This is not needed for Emacs.
  1188. (and (boundp 'zmacs-region-stays)
  1189. (setq zmacs-region-stays t)))
  1190. (put 'c-mode 'c-mode-prefix "c-")
  1191. (put 'c++-mode 'c-mode-prefix "c++-")
  1192. (put 'objc-mode 'c-mode-prefix "objc-")
  1193. (put 'java-mode 'c-mode-prefix "java-")
  1194. (put 'idl-mode 'c-mode-prefix "idl-")
  1195. (put 'pike-mode 'c-mode-prefix "pike-")
  1196. (put 'awk-mode 'c-mode-prefix "awk-")
  1197. (defsubst c-mode-symbol (suffix)
  1198. "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
  1199. the corresponding symbol."
  1200. (or c-buffer-is-cc-mode
  1201. (error "Not inside a CC Mode based mode"))
  1202. (let ((mode-prefix (get c-buffer-is-cc-mode 'c-mode-prefix)))
  1203. (or mode-prefix
  1204. (error "%S has no mode prefix known to `c-mode-symbol'"
  1205. c-buffer-is-cc-mode))
  1206. (intern (concat mode-prefix suffix))))
  1207. (defsubst c-mode-var (suffix)
  1208. "Prefix the current mode prefix (e.g. \"c-\") to SUFFIX and return
  1209. the value of the variable with that name."
  1210. (symbol-value (c-mode-symbol suffix)))
  1211. (defsubst c-got-face-at (pos faces)
  1212. "Return non-nil if position POS in the current buffer has any of the
  1213. faces in the list FACES."
  1214. (let ((pos-faces (get-text-property pos 'face)))
  1215. (if (consp pos-faces)
  1216. (progn
  1217. (while (and pos-faces
  1218. (not (memq (car pos-faces) faces)))
  1219. (setq pos-faces (cdr pos-faces)))
  1220. pos-faces)
  1221. (memq pos-faces faces))))
  1222. (defsubst c-face-name-p (facename)
  1223. ;; Return t if FACENAME is the name of a face. This method is
  1224. ;; necessary since facep in XEmacs only returns t for the actual
  1225. ;; face objects (while it's only their names that are used just
  1226. ;; about anywhere else) without providing a predicate that tests
  1227. ;; face names.
  1228. (memq facename (face-list)))
  1229. (defun c-concat-separated (list separator)
  1230. "Like `concat' on LIST, but separate each element with SEPARATOR.
  1231. Notably, null elements in LIST are ignored."
  1232. (mapconcat 'identity (delete nil (append list nil)) separator))
  1233. (defun c-make-keywords-re (adorn list &optional mode)
  1234. "Make a regexp that matches all the strings the list.
  1235. Duplicates and nil elements in the list are removed. The resulting
  1236. regexp may contain zero or more submatch expressions.
  1237. If ADORN is t there will be at least one submatch and the first
  1238. surrounds the matched alternative, and the regexp will also not match
  1239. a prefix of any identifier. Adorned regexps cannot be appended. The
  1240. language variable `c-nonsymbol-key' is used to make the adornment.
  1241. A value 'appendable for ADORN is like above, but all alternatives in
  1242. the list that end with a word constituent char will have \\> appended
  1243. instead, so that the regexp remains appendable. Note that this
  1244. variant doesn't always guarantee that an identifier prefix isn't
  1245. matched since the symbol constituent '_' is normally considered a
  1246. nonword token by \\>.
  1247. The optional MODE specifies the language to get `c-nonsymbol-key' from
  1248. when it's needed. The default is the current language taken from
  1249. `c-buffer-is-cc-mode'."
  1250. (let (unique)
  1251. (dolist (elt list)
  1252. (unless (member elt unique)
  1253. (push elt unique)))
  1254. (setq list (delete nil unique)))
  1255. (if list
  1256. (let (re)
  1257. (if (eq adorn 'appendable)
  1258. ;; This is kludgy but it works: Search for a string that
  1259. ;; doesn't occur in any word in LIST. Append it to all
  1260. ;; the alternatives where we want to add \>. Run through
  1261. ;; `regexp-opt' and then replace it with \>.
  1262. (let ((unique "") pos)
  1263. (while (let (found)
  1264. (setq unique (concat unique "@")
  1265. pos list)
  1266. (while (and pos
  1267. (if (string-match unique (car pos))
  1268. (progn (setq found t)
  1269. nil)
  1270. t))
  1271. (setq pos (cdr pos)))
  1272. found))
  1273. (setq pos list)
  1274. (while pos
  1275. (if (string-match "\\w\\'" (car pos))
  1276. (setcar pos (concat (car pos) unique)))
  1277. (setq pos (cdr pos)))
  1278. (setq re (regexp-opt list))
  1279. (setq pos 0)
  1280. (while (string-match unique re pos)
  1281. (setq pos (+ (match-beginning 0) 2)
  1282. re (replace-match "\\>" t t re))))
  1283. (setq re (regexp-opt list)))
  1284. ;; Emacs 20 and XEmacs (all versions so far) has a buggy
  1285. ;; regexp-opt that doesn't always cope with strings containing
  1286. ;; newlines. This kludge doesn't handle shy parens correctly
  1287. ;; so we can't advice regexp-opt directly with it.
  1288. (let (fail-list)
  1289. (while list
  1290. (and (string-match "\n" (car list)) ; To speed it up a little.
  1291. (not (string-match (concat "\\`\\(" re "\\)\\'")
  1292. (car list)))
  1293. (setq fail-list (cons (car list) fail-list)))
  1294. (setq list (cdr list)))
  1295. (when fail-list
  1296. (setq re (concat re
  1297. "\\|"
  1298. (mapconcat
  1299. (if (eq adorn 'appendable)
  1300. (lambda (str)
  1301. (if (string-match "\\w\\'" str)
  1302. (concat (regexp-quote str)
  1303. "\\>")
  1304. (regexp-quote str)))
  1305. 'regexp-quote)
  1306. (sort fail-list
  1307. (lambda (a b)
  1308. (> (length a) (length b))))
  1309. "\\|")))))
  1310. ;; Add our own grouping parenthesis around re instead of
  1311. ;; passing adorn to `regexp-opt', since in XEmacs it makes the
  1312. ;; top level grouping "shy".
  1313. (cond ((eq adorn 'appendable)
  1314. (concat "\\(" re "\\)"))
  1315. (adorn
  1316. (concat "\\(" re "\\)"
  1317. "\\("
  1318. (c-get-lang-constant 'c-nonsymbol-key nil mode)
  1319. "\\|$\\)"))
  1320. (t
  1321. re)))
  1322. ;; Produce a regexp that matches nothing.
  1323. (if adorn
  1324. "\\(\\<\\>\\)"
  1325. "\\<\\>")))
  1326. (put 'c-make-keywords-re 'lisp-indent-function 1)
  1327. (defun c-make-bare-char-alt (chars &optional inverted)
  1328. "Make a character alternative string from the list of characters CHARS.
  1329. The returned string is of the type that can be used with
  1330. `skip-chars-forward' and `skip-chars-backward'. If INVERTED is
  1331. non-nil, a caret is prepended to invert the set."
  1332. ;; This function ought to be in the elisp core somewhere.
  1333. (let ((str (if inverted "^" "")) char char2)
  1334. (setq chars (sort (append chars nil) `<))
  1335. (while chars
  1336. (setq char (pop chars))
  1337. (if (memq char '(?\\ ?^ ?-))
  1338. ;; Quoting necessary (this method only works in the skip
  1339. ;; functions).
  1340. (setq str (format "%s\\%c" str char))
  1341. (setq str (format "%s%c" str char)))
  1342. ;; Check for range.
  1343. (setq char2 char)
  1344. (while (and chars (>= (1+ char2) (car chars)))
  1345. (setq char2 (pop chars)))
  1346. (unless (= char char2)
  1347. (if (< (1+ char) char2)
  1348. (setq str (format "%s-%c" str char2))
  1349. (push char2 chars))))
  1350. str))
  1351. ;; Leftovers from (X)Emacs 19 compatibility.
  1352. (defalias 'c-regexp-opt 'regexp-opt)
  1353. (defalias 'c-regexp-opt-depth 'regexp-opt-depth)
  1354. ;; Figure out what features this Emacs has
  1355. (cc-bytecomp-defvar open-paren-in-column-0-is-defun-start)
  1356. (defconst c-emacs-features
  1357. (let (list)
  1358. (if (boundp 'infodock-version)
  1359. ;; I've no idea what this actually is, but it's legacy. /mast
  1360. (setq list (cons 'infodock list)))
  1361. ;; XEmacs uses 8-bit modify-syntax-entry flags.
  1362. ;; Emacs uses a 1-bit flag. We will have to set up our
  1363. ;; syntax tables differently to handle this.
  1364. (let ((table (copy-syntax-table))
  1365. entry)
  1366. (modify-syntax-entry ?a ". 12345678" table)
  1367. (cond
  1368. ;; Emacs
  1369. ((arrayp table)
  1370. (setq entry (aref table ?a))
  1371. ;; In Emacs, table entries are cons cells
  1372. (if (consp entry) (setq entry (car entry))))
  1373. ;; XEmacs
  1374. ((fboundp 'get-char-table)
  1375. (setq entry (get-char-table ?a table)))
  1376. ;; incompatible
  1377. (t (error "CC Mode is incompatible with this version of Emacs")))
  1378. (setq list (cons (if (= (logand (lsh entry -16) 255) 255)
  1379. '8-bit
  1380. '1-bit)
  1381. list)))
  1382. ;; Check whether beginning/end-of-defun call
  1383. ;; beginning/end-of-defun-function nicely, passing through the
  1384. ;; argument and respecting the return code.
  1385. (let* (mark-ring
  1386. (bod-param 'foo) (eod-param 'foo)
  1387. (beginning-of-defun-function
  1388. (lambda (&optional arg)
  1389. (or (eq bod-param 'foo) (setq bod-param 'bar))
  1390. (and (eq bod-param 'foo)
  1391. (setq bod-param arg)
  1392. (eq arg 3))))
  1393. (end-of-defun-function
  1394. (lambda (&optional arg)
  1395. (and (eq eod-param 'foo)
  1396. (setq eod-param arg)
  1397. (eq arg 3)))))
  1398. (if (save-excursion (and (beginning-of-defun 3) (eq bod-param 3)
  1399. (not (beginning-of-defun))
  1400. (end-of-defun 3) (eq eod-param 3)
  1401. (not (end-of-defun))))
  1402. (setq list (cons 'argumentative-bod-function list))))
  1403. (let ((buf (generate-new-buffer " test"))
  1404. parse-sexp-lookup-properties
  1405. parse-sexp-ignore-comments
  1406. lookup-syntax-properties)
  1407. (with-current-buffer buf
  1408. (set-syntax-table (make-syntax-table))
  1409. ;; For some reason we have to set some of these after the
  1410. ;; buffer has been made current. (Specifically,
  1411. ;; `parse-sexp-ignore-comments' in Emacs 21.)
  1412. (setq parse-sexp-lookup-properties t
  1413. parse-sexp-ignore-comments t
  1414. lookup-syntax-properties t)
  1415. ;; Find out if the `syntax-table' text property works.
  1416. (modify-syntax-entry ?< ".")
  1417. (modify-syntax-entry ?> ".")
  1418. (insert "<()>")
  1419. (c-mark-<-as-paren (point-min))
  1420. (c-mark->-as-paren (+ 3 (point-min)))
  1421. (goto-char (point-min))
  1422. (c-forward-sexp)
  1423. (if (= (point) (+ 4 (point-min)))
  1424. (setq list (cons 'syntax-properties list))
  1425. (error (concat
  1426. "CC Mode is incompatible with this version of Emacs - "
  1427. "support for the `syntax-table' text property "
  1428. "is required.")))
  1429. ;; Find out if generic comment delimiters work.
  1430. (c-safe
  1431. (modify-syntax-entry ?x "!")
  1432. (if (string-match "\\s!" "x")
  1433. (setq list (cons 'gen-comment-delim list))))
  1434. ;; Find out if generic string delimiters work.
  1435. (c-safe
  1436. (modify-syntax-entry ?x "|")
  1437. (if (string-match "\\s|" "x")
  1438. (setq list (cons 'gen-string-delim list))))
  1439. ;; See if POSIX char classes work.
  1440. (when (and (string-match "[[:alpha:]]" "a")
  1441. ;; All versions of Emacs 21 so far haven't fixed
  1442. ;; char classes in `skip-chars-forward' and
  1443. ;; `skip-chars-backward'.
  1444. (progn
  1445. (delete-region (point-min) (point-max))
  1446. (insert "foo123")
  1447. (skip-chars-backward "[:alnum:]")
  1448. (bobp))
  1449. (= (skip-chars-forward "[:alpha:]") 3))
  1450. (setq list (cons 'posix-char-classes list)))
  1451. ;; See if `open-paren-in-column-0-is-defun-start' exists and
  1452. ;; isn't buggy (Emacs >= 21.4).
  1453. (when (boundp 'open-paren-in-column-0-is-defun-start)
  1454. (let ((open-paren-in-column-0-is-defun-start nil)
  1455. (parse-sexp-ignore-comments t))
  1456. (delete-region (point-min) (point-max))
  1457. (set-syntax-table (make-syntax-table))
  1458. (modify-syntax-entry ?\' "\"")
  1459. (cond
  1460. ;; XEmacs. Afaik this is currently an Emacs-only
  1461. ;; feature, but it's good to be prepared.
  1462. ((memq '8-bit list)
  1463. (modify-syntax-entry ?/ ". 1456")
  1464. (modify-syntax-entry ?* ". 23"))
  1465. ;; Emacs
  1466. ((memq '1-bit list)
  1467. (modify-syntax-entry ?/ ". 124b")
  1468. (modify-syntax-entry ?* ". 23")))
  1469. (modify-syntax-entry ?\n "> b")
  1470. (insert "/* '\n () */")
  1471. (backward-sexp)
  1472. (if (bobp)
  1473. (setq list (cons 'col-0-paren list)))))
  1474. (set-buffer-modified-p nil))
  1475. (kill-buffer buf))
  1476. ;; See if `parse-partial-sexp' returns the eighth element.
  1477. (if (c-safe (>= (length (save-excursion (parse-partial-sexp (point) (point))))
  1478. 10))
  1479. (setq list (cons 'pps-extended-state list))
  1480. (error (concat
  1481. "CC Mode is incompatible with this version of Emacs - "
  1482. "`parse-partial-sexp' has to return at least 10 elements.")))
  1483. ;;(message "c-emacs-features: %S" list)
  1484. list)
  1485. "A list of certain features in the (X)Emacs you are using.
  1486. There are many flavors of Emacs out there, each with different
  1487. features supporting those needed by CC Mode. The following values
  1488. might be present:
  1489. '8-bit 8 bit syntax entry flags (XEmacs style).
  1490. '1-bit 1 bit syntax entry flags (Emacs style).
  1491. 'argumentative-bod-function beginning-of-defun passes ARG through
  1492. to a non-null beginning-of-defun-function. It is assumed
  1493. the end-of-defun does the same thing.
  1494. 'syntax-properties It works to override the syntax for specific characters
  1495. in the buffer with the 'syntax-table property. It's
  1496. always set - CC Mode no longer works in emacsen without
  1497. this feature.
  1498. 'gen-comment-delim Generic comment delimiters work
  1499. (i.e. the syntax class `!').
  1500. 'gen-string-delim Generic string delimiters work
  1501. (i.e. the syntax class `|').
  1502. 'pps-extended-state `parse-partial-sexp' returns a list with at least 10
  1503. elements, i.e. it contains the position of the start of
  1504. the last comment or string. It's always set - CC Mode
  1505. no longer works in emacsen without this feature.
  1506. 'posix-char-classes The regexp engine understands POSIX character classes.
  1507. 'col-0-paren It's possible to turn off the ad-hoc rule that a paren
  1508. in column zero is the start of a defun.
  1509. 'infodock This is Infodock (based on XEmacs).
  1510. '8-bit and '1-bit are mutually exclusive.")
  1511. ;;; Some helper constants.
  1512. ;; If the regexp engine supports POSIX char classes then we can use
  1513. ;; them to handle extended charsets correctly.
  1514. (if (memq 'posix-char-classes c-emacs-features)
  1515. (progn
  1516. (defconst c-alpha "[:alpha:]")
  1517. (defconst c-alnum "[:alnum:]")
  1518. (defconst c-digit "[:digit:]")
  1519. (defconst c-upper "[:upper:]")
  1520. (defconst c-lower "[:lower:]"))
  1521. (defconst c-alpha "a-zA-Z")
  1522. (defconst c-alnum "a-zA-Z0-9")
  1523. (defconst c-digit "0-9")
  1524. (defconst c-upper "A-Z")
  1525. (defconst c-lower "a-z"))
  1526. ;;; System for handling language dependent constants.
  1527. ;; This is used to set various language dependent data in a flexible
  1528. ;; way: Language constants can be built from the values of other
  1529. ;; language constants, also those for other languages. They can also
  1530. ;; process the values of other language constants uniformly across all
  1531. ;; the languages. E.g. one language constant can list all the type
  1532. ;; keywords in each language, and another can build a regexp for each
  1533. ;; language from those lists without code duplication.
  1534. ;;
  1535. ;; Language constants are defined with `c-lang-defconst', and their
  1536. ;; value forms (referred to as source definitions) are evaluated only
  1537. ;; on demand when requested for a particular language with
  1538. ;; `c-lang-const'. It's therefore possible to refer to the values of
  1539. ;; constants defined later in the file, or in another file, just as
  1540. ;; long as all the relevant `c-lang-defconst' have been loaded when
  1541. ;; `c-lang-const' is actually evaluated from somewhere else.
  1542. ;;
  1543. ;; `c-lang-const' forms are also evaluated at compile time and
  1544. ;; replaced with the values they produce. Thus there's no overhead
  1545. ;; for this system when compiled code is used - only the values
  1546. ;; actually used in the code are present, and the file(s) containing
  1547. ;; the `c-lang-defconst' forms don't need to be loaded at all then.
  1548. ;; There are however safeguards to make sure that they can be loaded
  1549. ;; to get the source definitions for the values if there's a mismatch
  1550. ;; in compiled versions, or if `c-lang-const' is used uncompiled.
  1551. ;;
  1552. ;; Note that the source definitions in a `c-lang-defconst' form are
  1553. ;; compiled into the .elc file where it stands; there's no need to
  1554. ;; load the source file to get it.
  1555. ;;
  1556. ;; See cc-langs.el for more details about how this system is deployed
  1557. ;; in CC Mode, and how the associated language variable system
  1558. ;; (`c-lang-defvar') works. That file also contains a lot of
  1559. ;; examples.
  1560. (defun c-add-language (mode base-mode)
  1561. "Declare a new language in the language dependent variable system.
  1562. This is intended to be used by modes that inherit CC Mode to add new
  1563. languages. It should be used at the top level before any calls to
  1564. `c-lang-defconst'. MODE is the mode name symbol for the new language,
  1565. and BASE-MODE is the mode name symbol for the language in CC Mode that
  1566. is to be the template for the new mode.
  1567. The exact effect of BASE-MODE is to make all language constants that
  1568. haven't got a setting in the new language fall back to their values in
  1569. BASE-MODE. It does not have any effect outside the language constant
  1570. system."
  1571. (unless (string-match "\\`\\(.*-\\)mode\\'" (symbol-name mode))
  1572. (error "The mode name symbol `%s' must end with \"-mode\"" mode))
  1573. (put mode 'c-mode-prefix (match-string 1 (symbol-name mode)))
  1574. (unless (get base-mode 'c-mode-prefix)
  1575. (error "Unknown base mode `%s'" base-mode))
  1576. (put mode 'c-fallback-mode base-mode))
  1577. (defvar c-lang-constants (make-vector 151 0))
  1578. ;; This obarray is a cache to keep track of the language constants
  1579. ;; defined by `c-lang-defconst' and the evaluated values returned by
  1580. ;; `c-lang-const'. It's mostly used at compile time but it's not
  1581. ;; stored in compiled files.
  1582. ;;
  1583. ;; The obarray contains all the language constants as symbols. The
  1584. ;; value cells hold the evaluated values as alists where each car is
  1585. ;; the mode name symbol and the corresponding cdr is the evaluated
  1586. ;; value in that mode. The property lists hold the source definitions
  1587. ;; and other miscellaneous data. The obarray might also contain
  1588. ;; various other symbols, but those don't have any variable bindings.
  1589. (defvar c-lang-const-expansion nil)
  1590. (defsubst c-get-current-file ()
  1591. ;; Return the base name of the current file.
  1592. (let ((file (cond
  1593. (load-in-progress
  1594. ;; Being loaded.
  1595. load-file-name)
  1596. ((and (boundp 'byte-compile-dest-file)
  1597. (stringp byte-compile-dest-file))
  1598. ;; Being compiled.
  1599. byte-compile-dest-file)
  1600. (t
  1601. ;; Being evaluated interactively.
  1602. (buffer-file-name)))))
  1603. (and file
  1604. (file-name-sans-extension
  1605. (file-name-nondirectory file)))))
  1606. (defmacro c-lang-defconst-eval-immediately (form)
  1607. "Can be used inside a VAL in `c-lang-defconst' to evaluate FORM
  1608. immediately, i.e. at the same time as the `c-lang-defconst' form
  1609. itself is evaluated."
  1610. ;; Evaluate at macro expansion time, i.e. in the
  1611. ;; `cl-macroexpand-all' inside `c-lang-defconst'.
  1612. (eval form))
  1613. ;; Only used at compile time - suppress "might not be defined at runtime".
  1614. (declare-function cl-macroexpand-all "cl-extra" (form &optional env))
  1615. (defmacro c-lang-defconst (name &rest args)
  1616. "Set the language specific values of the language constant NAME.
  1617. The second argument can optionally be a docstring. The rest of the
  1618. arguments are one or more repetitions of LANG VAL where LANG specifies
  1619. the language(s) that VAL applies to. LANG is the name of the
  1620. language, i.e. the mode name without the \"-mode\" suffix, or a list
  1621. of such language names, or `t' for all languages. VAL is a form to
  1622. evaluate to get the value.
  1623. If LANG isn't `t' or one of the core languages in CC Mode, it must
  1624. have been declared with `c-add-language'.
  1625. Neither NAME, LANG nor VAL are evaluated directly - they should not be
  1626. quoted. `c-lang-defconst-eval-immediately' can however be used inside
  1627. VAL to evaluate parts of it directly.
  1628. When VAL is evaluated for some language, that language is temporarily
  1629. made current so that `c-lang-const' without an explicit language can
  1630. be used inside VAL to refer to the value of a language constant in the
  1631. same language. That is particularly useful if LANG is `t'.
  1632. VAL is not evaluated right away but rather when the value is requested
  1633. with `c-lang-const'. Thus it's possible to use `c-lang-const' inside
  1634. VAL to refer to language constants that haven't been defined yet.
  1635. However, if the definition of a language constant is in another file
  1636. then that file must be loaded \(at compile time) before it's safe to
  1637. reference the constant.
  1638. The assignments in ARGS are processed in sequence like `setq', so
  1639. \(c-lang-const NAME) may be used inside a VAL to refer to the last
  1640. assigned value to this language constant, or a value that it has
  1641. gotten in another earlier loaded file.
  1642. To work well with repeated loads and interactive reevaluation, only
  1643. one `c-lang-defconst' for each NAME is permitted per file. If there
  1644. already is one it will be completely replaced; the value in the
  1645. earlier definition will not affect `c-lang-const' on the same
  1646. constant. A file is identified by its base name."
  1647. (let* ((sym (intern (symbol-name name) c-lang-constants))
  1648. ;; Make `c-lang-const' expand to a straightforward call to
  1649. ;; `c-get-lang-constant' in `cl-macroexpand-all' below.
  1650. ;;
  1651. ;; (The default behavior, i.e. to expand to a call inside
  1652. ;; `eval-when-compile' should be equivalent, since that macro
  1653. ;; should only expand to its content if it's used inside a
  1654. ;; form that's already evaluated at compile time. It's
  1655. ;; however necessary to use our cover macro
  1656. ;; `cc-eval-when-compile' due to bugs in `eval-when-compile',
  1657. ;; and it expands to a bulkier form that in this case only is
  1658. ;; unnecessary garbage that we don't want to store in the
  1659. ;; language constant source definitions.)
  1660. (c-lang-const-expansion 'call)
  1661. (c-langs-are-parametric t)
  1662. bindings
  1663. pre-files)
  1664. (or (symbolp name)
  1665. (error "Not a symbol: %s" name))
  1666. (when (stringp (car-safe args))
  1667. ;; The docstring is hardly used anywhere since there's no normal
  1668. ;; symbol to attach it to. It's primarily for getting the right
  1669. ;; format in the source.
  1670. (put sym 'variable-documentation (car args))
  1671. (setq args (cdr args)))
  1672. (or args
  1673. (error "No assignments in `c-lang-defconst' for %s" name))
  1674. ;; Rework ARGS to an association list to make it easier to handle.
  1675. ;; It's reversed at the same time to make it easier to implement
  1676. ;; the demand-driven (i.e. reversed) evaluation in `c-lang-const'.
  1677. (while args
  1678. (let ((assigned-mode
  1679. (cond ((eq (car args) t) t)
  1680. ((symbolp (car args))
  1681. (list (intern (concat (symbol-name (car args))
  1682. "-mode"))))
  1683. ((listp (car args))
  1684. (mapcar (lambda (lang)
  1685. (or (symbolp lang)
  1686. (error "Not a list of symbols: %s"
  1687. (car args)))
  1688. (intern (concat (symbol-name lang)
  1689. "-mode")))
  1690. (car args)))
  1691. (t (error "Not a symbol or a list of symbols: %s"
  1692. (car args)))))
  1693. val)
  1694. (or (cdr args)
  1695. (error "No value for %s" (car args)))
  1696. (setq args (cdr args)
  1697. val (car args))
  1698. ;; Emacs has a weird bug where it seems to fail to read
  1699. ;; backquote lists from byte compiled files correctly (,@
  1700. ;; forms, to be specific), so make sure the bindings in the
  1701. ;; expansion below don't contain any backquote stuff.
  1702. ;; (XEmacs handles it correctly and doesn't need this for that
  1703. ;; reason, but we also use this expansion handle
  1704. ;; `c-lang-defconst-eval-immediately' and to register
  1705. ;; dependencies on the `c-lang-const's in VAL.)
  1706. (setq val (cl-macroexpand-all val))
  1707. (setq bindings (cons (cons assigned-mode val) bindings)
  1708. args (cdr args))))
  1709. ;; Compile in the other files that have provided source
  1710. ;; definitions for this symbol, to make sure the order in the
  1711. ;; `source' property is correct even when files are loaded out of
  1712. ;; order.
  1713. (setq pre-files (nreverse
  1714. ;; Reverse to get the right load order.
  1715. (mapcar 'car (get sym 'source))))
  1716. `(eval-and-compile
  1717. (c-define-lang-constant ',name ',bindings
  1718. ,@(and pre-files `(',pre-files))))))
  1719. (put 'c-lang-defconst 'lisp-indent-function 1)
  1720. ;(eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
  1721. ; '
  1722. (def-edebug-spec c-lang-defconst
  1723. (&define name [&optional stringp] [&rest sexp def-form]))
  1724. (defun c-define-lang-constant (name bindings &optional pre-files)
  1725. ;; Used by `c-lang-defconst'.
  1726. (let* ((sym (intern (symbol-name name) c-lang-constants))
  1727. (source (get sym 'source))
  1728. (file (intern
  1729. (or (c-get-current-file)
  1730. (error "`c-lang-defconst' must be used in a file"))))
  1731. (elem (assq file source)))
  1732. ;;(when (cdr-safe elem)
  1733. ;; (message "Language constant %s redefined in %S" name file))
  1734. ;; Note that the order in the source alist is relevant. Like how
  1735. ;; `c-lang-defconst' reverses the bindings, this reverses the
  1736. ;; order between files so that the last to evaluate comes first.
  1737. (unless elem
  1738. (while pre-files
  1739. (unless (assq (car pre-files) source)
  1740. (setq source (cons (list (car pre-files)) source)))
  1741. (setq pre-files (cdr pre-files)))
  1742. (put sym 'source (cons (setq elem (list file)) source)))
  1743. (setcdr elem bindings)
  1744. ;; Bind the symbol as a variable, or clear any earlier evaluated
  1745. ;; value it has.
  1746. (set sym nil)
  1747. ;; Clear the evaluated values that depend on this source.
  1748. (let ((agenda (get sym 'dependents))
  1749. (visited (make-vector 101 0))
  1750. ptr)
  1751. (while agenda
  1752. (setq sym (car agenda)
  1753. agenda (cdr agenda))
  1754. (intern (symbol-name sym) visited)
  1755. (set sym nil)
  1756. (setq ptr (get sym 'dependents))
  1757. (while ptr
  1758. (setq sym (car ptr)
  1759. ptr (cdr ptr))
  1760. (unless (intern-soft (symbol-name sym) visited)
  1761. (setq agenda (cons sym agenda))))))
  1762. name))
  1763. (defmacro c-lang-const (name &optional lang)
  1764. "Get the mode specific value of the language constant NAME in language LANG.
  1765. LANG is the name of the language, i.e. the mode name without the
  1766. \"-mode\" suffix. If used inside `c-lang-defconst' or
  1767. `c-lang-defvar', LANG may be left out to refer to the current
  1768. language. NAME and LANG are not evaluated so they should not be
  1769. quoted."
  1770. (or (symbolp name)
  1771. (error "Not a symbol: %s" name))
  1772. (or (symbolp lang)
  1773. (error "Not a symbol: %s" lang))
  1774. (let ((sym (intern (symbol-name name) c-lang-constants))
  1775. mode source-files args)
  1776. (when lang
  1777. (setq mode (intern (concat (symbol-name lang) "-mode")))
  1778. (unless (get mode 'c-mode-prefix)
  1779. (error
  1780. "Unknown language %S since it got no `c-mode-prefix' property"
  1781. (symbol-name lang))))
  1782. (if (eq c-lang-const-expansion 'immediate)
  1783. ;; No need to find out the source file(s) when we evaluate
  1784. ;; immediately since all the info is already there in the
  1785. ;; `source' property.
  1786. `',(c-get-lang-constant name nil mode)
  1787. (let ((file (c-get-current-file)))
  1788. (if file (setq file (intern file)))
  1789. ;; Get the source file(s) that must be loaded to get the value
  1790. ;; of the constant. If the symbol isn't defined yet we assume
  1791. ;; that its definition will come later in this file, and thus
  1792. ;; are no file dependencies needed.
  1793. (setq source-files (nreverse
  1794. ;; Reverse to get the right load order.
  1795. (apply 'nconc
  1796. (mapcar (lambda (elem)
  1797. (if (eq file (car elem))
  1798. nil ; Exclude our own file.
  1799. (list (car elem))))
  1800. (get sym 'source))))))
  1801. ;; Make some effort to do a compact call to
  1802. ;; `c-get-lang-constant' since it will be compiled in.
  1803. (setq args (and mode `(',mode)))
  1804. (if (or source-files args)
  1805. (setq args (cons (and source-files `',source-files)
  1806. args)))
  1807. (if (or (eq c-lang-const-expansion 'call)
  1808. (and (not c-lang-const-expansion)
  1809. (not mode))
  1810. load-in-progress
  1811. (not (boundp 'byte-compile-dest-file))
  1812. (not (stringp byte-compile-dest-file)))
  1813. ;; Either a straight call is requested in the context, or
  1814. ;; we're in an "uncontrolled" context and got no language,
  1815. ;; or we're not being byte compiled so the compile time
  1816. ;; stuff below is unnecessary.
  1817. `(c-get-lang-constant ',name ,@args)
  1818. ;; Being compiled. If the loading and compiling version is
  1819. ;; the same we use a value that is evaluated at compile time,
  1820. ;; otherwise it's evaluated at runtime.
  1821. `(if (eq c-version-sym ',c-version-sym)
  1822. (cc-eval-when-compile
  1823. (c-get-lang-constant ',name ,@args))
  1824. (c-get-lang-constant ',name ,@args))))))
  1825. (defvar c-lang-constants-under-evaluation nil)
  1826. (defun c-get-lang-constant (name &optional source-files mode)
  1827. ;; Used by `c-lang-const'.
  1828. (or mode
  1829. (setq mode c-buffer-is-cc-mode)
  1830. (error "No current language"))
  1831. (let* ((sym (intern (symbol-name name) c-lang-constants))
  1832. (source (get sym 'source))
  1833. elem
  1834. (eval-in-sym (and c-lang-constants-under-evaluation
  1835. (caar c-lang-constants-under-evaluation))))
  1836. ;; Record the dependencies between this symbol and the one we're
  1837. ;; being evaluated in.
  1838. (when eval-in-sym
  1839. (or (memq eval-in-sym (get sym 'dependents))
  1840. (put sym 'dependents (cons eval-in-sym (get sym 'dependents)))))
  1841. ;; Make sure the source files have entries on the `source'
  1842. ;; property so that loading will take place when necessary.
  1843. (while source-files
  1844. (unless (assq (car source-files) source)
  1845. (put sym 'source
  1846. (setq source (cons (list (car source-files)) source)))
  1847. ;; Might pull in more definitions which affect the value. The
  1848. ;; clearing of dependent values etc is done when the
  1849. ;; definition is encountered during the load; this is just to
  1850. ;; jump past the check for a cached value below.
  1851. (set sym nil))
  1852. (setq source-files (cdr source-files)))
  1853. (if (and (boundp sym)
  1854. (setq elem (assq mode (symbol-value sym))))
  1855. (cdr elem)
  1856. ;; Check if an evaluation of this symbol is already underway.
  1857. ;; In that case we just continue with the "assignment" before
  1858. ;; the one currently being evaluated, thereby creating the
  1859. ;; illusion if a `setq'-like sequence of assignments.
  1860. (let* ((c-buffer-is-cc-mode mode)
  1861. (source-pos
  1862. (or (assq sym c-lang-constants-under-evaluation)
  1863. (cons sym (vector source nil))))
  1864. ;; Append `c-lang-constants-under-evaluation' even if an
  1865. ;; earlier entry is found. It's only necessary to get
  1866. ;; the recording of dependencies above correct.
  1867. (c-lang-constants-under-evaluation
  1868. (cons source-pos c-lang-constants-under-evaluation))
  1869. (fallback (get mode 'c-fallback-mode))
  1870. value
  1871. ;; Make sure the recursion limits aren't very low
  1872. ;; since the `c-lang-const' dependencies can go deep.
  1873. (max-specpdl-size (max max-specpdl-size 3000))
  1874. (max-lisp-eval-depth (max max-lisp-eval-depth 1000)))
  1875. (if (if fallback
  1876. (let ((backup-source-pos (copy-sequence (cdr source-pos))))
  1877. (and
  1878. ;; First try the original mode but don't accept an
  1879. ;; entry matching all languages since the fallback
  1880. ;; mode might have an explicit entry before that.
  1881. (eq (setq value (c-find-assignment-for-mode
  1882. (cdr source-pos) mode nil name))
  1883. c-lang-constants)
  1884. ;; Try again with the fallback mode from the
  1885. ;; original position. Note that
  1886. ;; `c-buffer-is-cc-mode' still is the real mode if
  1887. ;; language parameterization takes place.
  1888. (eq (setq value (c-find-assignment-for-mode
  1889. (setcdr source-pos backup-source-pos)
  1890. fallback t name))
  1891. c-lang-constants)))
  1892. ;; A simple lookup with no fallback mode.
  1893. (eq (setq value (c-find-assignment-for-mode
  1894. (cdr source-pos) mode t name))
  1895. c-lang-constants))
  1896. (error
  1897. "`%s' got no (prior) value in %s (might be a cyclic reference)"
  1898. name mode))
  1899. (condition-case err
  1900. (setq value (eval value))
  1901. (error
  1902. ;; Print a message to aid in locating the error. We don't
  1903. ;; print the error itself since that will be done later by
  1904. ;; some caller higher up.
  1905. (message "Eval error in the `c-lang-defconst' for `%s' in %s:"
  1906. sym mode)
  1907. (makunbound sym)
  1908. (signal (car err) (cdr err))))
  1909. (set sym (cons (cons mode value) (symbol-value sym)))
  1910. value))))
  1911. (defun c-find-assignment-for-mode (source-pos mode match-any-lang name)
  1912. ;; Find the first assignment entry that applies to MODE at or after
  1913. ;; SOURCE-POS. If MATCH-ANY-LANG is non-nil, entries with `t' as
  1914. ;; the language list are considered to match, otherwise they don't.
  1915. ;; On return SOURCE-POS is updated to point to the next assignment
  1916. ;; after the returned one. If no assignment is found,
  1917. ;; `c-lang-constants' is returned as a magic value.
  1918. ;;
  1919. ;; SOURCE-POS is a vector that points out a specific assignment in
  1920. ;; the double alist that's used in the `source' property. The first
  1921. ;; element is the position in the top alist which is indexed with
  1922. ;; the source files, and the second element is the position in the
  1923. ;; nested bindings alist.
  1924. ;;
  1925. ;; NAME is only used for error messages.
  1926. (catch 'found
  1927. (let ((file-entry (elt source-pos 0))
  1928. (assignment-entry (elt source-pos 1))
  1929. assignment)
  1930. (while (if assignment-entry
  1931. t
  1932. ;; Handled the last assignment from one file, begin on the
  1933. ;; next. Due to the check in `c-lang-defconst', we know
  1934. ;; there's at least one.
  1935. (when file-entry
  1936. (unless (aset source-pos 1
  1937. (setq assignment-entry (cdar file-entry)))
  1938. ;; The file containing the source definitions has not
  1939. ;; been loaded.
  1940. (let ((file (symbol-name (caar file-entry)))
  1941. (c-lang-constants-under-evaluation nil))
  1942. ;;(message (concat "Loading %s to get the source "
  1943. ;; "value for language constant %s")
  1944. ;; file name)
  1945. (load file))
  1946. (unless (setq assignment-entry (cdar file-entry))
  1947. ;; The load didn't fill in the source for the
  1948. ;; constant as expected. The situation is
  1949. ;; probably that a derived mode was written for
  1950. ;; and compiled with another version of CC Mode,
  1951. ;; and the requested constant isn't in the
  1952. ;; currently loaded one. Put in a dummy
  1953. ;; assignment that matches no language.
  1954. (setcdr (car file-entry)
  1955. (setq assignment-entry (list (list nil))))))
  1956. (aset source-pos 0 (setq file-entry (cdr file-entry)))
  1957. t))
  1958. (setq assignment (car assignment-entry))
  1959. (aset source-pos 1
  1960. (setq assignment-entry (cdr assignment-entry)))
  1961. (when (if (listp (car assignment))
  1962. (memq mode (car assignment))
  1963. match-any-lang)
  1964. (throw 'found (cdr assignment))))
  1965. c-lang-constants)))
  1966. (defun c-lang-major-mode-is (mode)
  1967. ;; `c-major-mode-is' expands to a call to this function inside
  1968. ;; `c-lang-defconst'. Here we also match the mode(s) against any
  1969. ;; fallback modes for the one in `c-buffer-is-cc-mode', so that
  1970. ;; e.g. (c-major-mode-is 'c++-mode) is true in a derived language
  1971. ;; that has c++-mode as base mode.
  1972. (unless (listp mode)
  1973. (setq mode (list mode)))
  1974. (let (match (buf-mode c-buffer-is-cc-mode))
  1975. (while (if (memq buf-mode mode)
  1976. (progn
  1977. (setq match t)
  1978. nil)
  1979. (setq buf-mode (get buf-mode 'c-fallback-mode))))
  1980. match))
  1981. (cc-provide 'cc-defs)
  1982. ;;; cc-defs.el ends here