cc-defs.el 93 KB

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