| 1 | ;;; cc-engine.el --- core syntax guessing engine for CC mode |
| 2 | |
| 3 | ;; Copyright (C) 1985, 1987, 1992-2012 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Authors: 2001- Alan Mackenzie |
| 6 | ;; 1998- Martin Stjernholm |
| 7 | ;; 1992-1999 Barry A. Warsaw |
| 8 | ;; 1987 Dave Detlefs |
| 9 | ;; 1987 Stewart Clamen |
| 10 | ;; 1985 Richard M. Stallman |
| 11 | ;; Maintainer: bug-cc-mode@gnu.org |
| 12 | ;; Created: 22-Apr-1997 (split from cc-mode.el) |
| 13 | ;; Keywords: c languages |
| 14 | ;; Package: cc-mode |
| 15 | |
| 16 | ;; This file is part of GNU Emacs. |
| 17 | |
| 18 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 19 | ;; it under the terms of the GNU General Public License as published by |
| 20 | ;; the Free Software Foundation, either version 3 of the License, or |
| 21 | ;; (at your option) any later version. |
| 22 | |
| 23 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 24 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 | ;; GNU General Public License for more details. |
| 27 | |
| 28 | ;; You should have received a copy of the GNU General Public License |
| 29 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 30 | |
| 31 | ;;; Commentary: |
| 32 | |
| 33 | ;; The functions which have docstring documentation can be considered |
| 34 | ;; part of an API which other packages can use in CC Mode buffers. |
| 35 | ;; Otoh, undocumented functions and functions with the documentation |
| 36 | ;; in comments are considered purely internal and can change semantics |
| 37 | ;; or even disappear in the future. |
| 38 | ;; |
| 39 | ;; (This policy applies to CC Mode as a whole, not just this file. It |
| 40 | ;; probably also applies to many other Emacs packages, but here it's |
| 41 | ;; clearly spelled out.) |
| 42 | |
| 43 | ;; Hidden buffer changes |
| 44 | ;; |
| 45 | ;; Various functions in CC Mode use text properties for caching and |
| 46 | ;; syntactic markup purposes, and those of them that might modify such |
| 47 | ;; properties but still don't modify the buffer in a visible way are |
| 48 | ;; said to do "hidden buffer changes". They should be used within |
| 49 | ;; `c-save-buffer-state' or a similar function that saves and restores |
| 50 | ;; buffer modifiedness, disables buffer change hooks, etc. |
| 51 | ;; |
| 52 | ;; Interactive functions are assumed to not do hidden buffer changes, |
| 53 | ;; except in the specific parts of them that do real changes. |
| 54 | ;; |
| 55 | ;; Lineup functions are assumed to do hidden buffer changes. They |
| 56 | ;; must not do real changes, though. |
| 57 | ;; |
| 58 | ;; All other functions that do hidden buffer changes have that noted |
| 59 | ;; in their doc string or comment. |
| 60 | ;; |
| 61 | ;; The intention with this system is to avoid wrapping every leaf |
| 62 | ;; function that do hidden buffer changes inside |
| 63 | ;; `c-save-buffer-state'. It should be used as near the top of the |
| 64 | ;; interactive functions as possible. |
| 65 | ;; |
| 66 | ;; Functions called during font locking are allowed to do hidden |
| 67 | ;; buffer changes since the font-lock package run them in a context |
| 68 | ;; similar to `c-save-buffer-state' (in fact, that function is heavily |
| 69 | ;; inspired by `save-buffer-state' in the font-lock package). |
| 70 | |
| 71 | ;; Use of text properties |
| 72 | ;; |
| 73 | ;; CC Mode uses several text properties internally to mark up various |
| 74 | ;; positions, e.g. to improve speed and to eliminate glitches in |
| 75 | ;; interactive refontification. |
| 76 | ;; |
| 77 | ;; Note: This doc is for internal use only. Other packages should not |
| 78 | ;; assume that these text properties are used as described here. |
| 79 | ;; |
| 80 | ;; 'category |
| 81 | ;; Used for "indirection". With its help, some other property can |
| 82 | ;; be cheaply and easily switched on or off everywhere it occurs. |
| 83 | ;; |
| 84 | ;; 'syntax-table |
| 85 | ;; Used to modify the syntax of some characters. It is used to |
| 86 | ;; mark the "<" and ">" of angle bracket parens with paren syntax, and |
| 87 | ;; to "hide" obtrusive characters in preprocessor lines. |
| 88 | ;; |
| 89 | ;; This property is used on single characters and is therefore |
| 90 | ;; always treated as front and rear nonsticky (or start and end open |
| 91 | ;; in XEmacs vocabulary). It's therefore installed on |
| 92 | ;; `text-property-default-nonsticky' if that variable exists (Emacs |
| 93 | ;; >= 21). |
| 94 | ;; |
| 95 | ;; 'c-is-sws and 'c-in-sws |
| 96 | ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to |
| 97 | ;; speed them up. See the comment blurb before `c-put-is-sws' |
| 98 | ;; below for further details. |
| 99 | ;; |
| 100 | ;; 'c-type |
| 101 | ;; This property is used on single characters to mark positions with |
| 102 | ;; special syntactic relevance of various sorts. Its primary use is |
| 103 | ;; to avoid glitches when multiline constructs are refontified |
| 104 | ;; interactively (on font lock decoration level 3). It's cleared in |
| 105 | ;; a region before it's fontified and is then put on relevant chars |
| 106 | ;; in that region as they are encountered during the fontification. |
| 107 | ;; The value specifies the kind of position: |
| 108 | ;; |
| 109 | ;; 'c-decl-arg-start |
| 110 | ;; Put on the last char of the token preceding each declaration |
| 111 | ;; inside a declaration style arglist (typically in a function |
| 112 | ;; prototype). |
| 113 | ;; |
| 114 | ;; 'c-decl-end |
| 115 | ;; Put on the last char of the token preceding a declaration. |
| 116 | ;; This is used in cases where declaration boundaries can't be |
| 117 | ;; recognized simply by looking for a token like ";" or "}". |
| 118 | ;; `c-type-decl-end-used' must be set if this is used (see also |
| 119 | ;; `c-find-decl-spots'). |
| 120 | ;; |
| 121 | ;; 'c-<>-arg-sep |
| 122 | ;; Put on the commas that separate arguments in angle bracket |
| 123 | ;; arglists like C++ template arglists. |
| 124 | ;; |
| 125 | ;; 'c-decl-id-start and 'c-decl-type-start |
| 126 | ;; Put on the last char of the token preceding each declarator |
| 127 | ;; in the declarator list of a declaration. They are also used |
| 128 | ;; between the identifiers cases like enum declarations. |
| 129 | ;; 'c-decl-type-start is used when the declarators are types, |
| 130 | ;; 'c-decl-id-start otherwise. |
| 131 | ;; |
| 132 | ;; 'c-awk-NL-prop |
| 133 | ;; Used in AWK mode to mark the various kinds of newlines. See |
| 134 | ;; cc-awk.el. |
| 135 | |
| 136 | ;;; Code: |
| 137 | |
| 138 | (eval-when-compile |
| 139 | (let ((load-path |
| 140 | (if (and (boundp 'byte-compile-dest-file) |
| 141 | (stringp byte-compile-dest-file)) |
| 142 | (cons (file-name-directory byte-compile-dest-file) load-path) |
| 143 | load-path))) |
| 144 | (load "cc-bytecomp" nil t))) |
| 145 | |
| 146 | (cc-require 'cc-defs) |
| 147 | (cc-require-when-compile 'cc-langs) |
| 148 | (cc-require 'cc-vars) |
| 149 | |
| 150 | ;; Silence the compiler. |
| 151 | (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs |
| 152 | |
| 153 | \f |
| 154 | ;; Make declarations for all the `c-lang-defvar' variables in cc-langs. |
| 155 | |
| 156 | (defmacro c-declare-lang-variables () |
| 157 | `(progn |
| 158 | ,@(apply 'nconc |
| 159 | (mapcar (lambda (init) |
| 160 | `(,(if (elt init 2) |
| 161 | `(defvar ,(car init) nil ,(elt init 2)) |
| 162 | `(defvar ,(car init) nil)) |
| 163 | (make-variable-buffer-local ',(car init)))) |
| 164 | (cdr c-lang-variable-inits))))) |
| 165 | (c-declare-lang-variables) |
| 166 | |
| 167 | \f |
| 168 | ;;; Internal state variables. |
| 169 | |
| 170 | ;; Internal state of hungry delete key feature |
| 171 | (defvar c-hungry-delete-key nil) |
| 172 | (make-variable-buffer-local 'c-hungry-delete-key) |
| 173 | |
| 174 | ;; The electric flag (toggled by `c-toggle-electric-state'). |
| 175 | ;; If t, electric actions (like automatic reindentation, and (if |
| 176 | ;; c-auto-newline is also set) auto newlining) will happen when an electric |
| 177 | ;; key like `{' is pressed (or an electric keyword like `else'). |
| 178 | (defvar c-electric-flag t) |
| 179 | (make-variable-buffer-local 'c-electric-flag) |
| 180 | |
| 181 | ;; Internal state of auto newline feature. |
| 182 | (defvar c-auto-newline nil) |
| 183 | (make-variable-buffer-local 'c-auto-newline) |
| 184 | |
| 185 | ;; Included in the mode line to indicate the active submodes. |
| 186 | ;; (defvar c-submode-indicators nil) |
| 187 | ;; (make-variable-buffer-local 'c-submode-indicators) |
| 188 | |
| 189 | (defun c-calculate-state (arg prevstate) |
| 190 | ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If |
| 191 | ;; arg is nil or zero, toggle the state. If arg is negative, turn |
| 192 | ;; the state off, and if arg is positive, turn the state on |
| 193 | (if (or (not arg) |
| 194 | (zerop (setq arg (prefix-numeric-value arg)))) |
| 195 | (not prevstate) |
| 196 | (> arg 0))) |
| 197 | |
| 198 | \f |
| 199 | ;; Basic handling of preprocessor directives. |
| 200 | |
| 201 | ;; This is a dynamically bound cache used together with |
| 202 | ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only |
| 203 | ;; works as long as point doesn't cross a macro boundary. |
| 204 | (defvar c-macro-start 'unknown) |
| 205 | |
| 206 | (defsubst c-query-and-set-macro-start () |
| 207 | (if (symbolp c-macro-start) |
| 208 | (setq c-macro-start (save-excursion |
| 209 | (c-save-buffer-state () |
| 210 | (and (c-beginning-of-macro) |
| 211 | (point))))) |
| 212 | c-macro-start)) |
| 213 | |
| 214 | (defsubst c-query-macro-start () |
| 215 | (if (symbolp c-macro-start) |
| 216 | (save-excursion |
| 217 | (c-save-buffer-state () |
| 218 | (and (c-beginning-of-macro) |
| 219 | (point)))) |
| 220 | c-macro-start)) |
| 221 | |
| 222 | (defun c-beginning-of-macro (&optional lim) |
| 223 | "Go to the beginning of a preprocessor directive. |
| 224 | Leave point at the beginning of the directive and return t if in one, |
| 225 | otherwise return nil and leave point unchanged. |
| 226 | |
| 227 | Note that this function might do hidden buffer changes. See the |
| 228 | comment at the start of cc-engine.el for more info." |
| 229 | (when c-opt-cpp-prefix |
| 230 | (let ((here (point))) |
| 231 | (save-restriction |
| 232 | (if lim (narrow-to-region lim (point-max))) |
| 233 | (beginning-of-line) |
| 234 | (while (eq (char-before (1- (point))) ?\\) |
| 235 | (forward-line -1)) |
| 236 | (back-to-indentation) |
| 237 | (if (and (<= (point) here) |
| 238 | (looking-at c-opt-cpp-start)) |
| 239 | t |
| 240 | (goto-char here) |
| 241 | nil))))) |
| 242 | |
| 243 | (defun c-end-of-macro () |
| 244 | "Go to the end of a preprocessor directive. |
| 245 | More accurately, move the point to the end of the closest following |
| 246 | line that doesn't end with a line continuation backslash - no check is |
| 247 | done that the point is inside a cpp directive to begin with. |
| 248 | |
| 249 | Note that this function might do hidden buffer changes. See the |
| 250 | comment at the start of cc-engine.el for more info." |
| 251 | (while (progn |
| 252 | (end-of-line) |
| 253 | (when (and (eq (char-before) ?\\) |
| 254 | (not (eobp))) |
| 255 | (forward-char) |
| 256 | t)))) |
| 257 | |
| 258 | (defun c-syntactic-end-of-macro () |
| 259 | ;; Go to the end of a CPP directive, or a "safe" pos just before. |
| 260 | ;; |
| 261 | ;; This is normally the end of the next non-escaped line. A "safe" |
| 262 | ;; position is one not within a string or comment. (The EOL on a line |
| 263 | ;; comment is NOT "safe"). |
| 264 | ;; |
| 265 | ;; This function must only be called from the beginning of a CPP construct. |
| 266 | ;; |
| 267 | ;; Note that this function might do hidden buffer changes. See the comment |
| 268 | ;; at the start of cc-engine.el for more info. |
| 269 | (let* ((here (point)) |
| 270 | (there (progn (c-end-of-macro) (point))) |
| 271 | (s (parse-partial-sexp here there))) |
| 272 | (while (and (or (nth 3 s) ; in a string |
| 273 | (nth 4 s)) ; in a comment (maybe at end of line comment) |
| 274 | (> there here)) ; No infinite loops, please. |
| 275 | (setq there (1- (nth 8 s))) |
| 276 | (setq s (parse-partial-sexp here there))) |
| 277 | (point))) |
| 278 | |
| 279 | (defun c-forward-over-cpp-define-id () |
| 280 | ;; Assuming point is at the "#" that introduces a preprocessor |
| 281 | ;; directive, it's moved forward to the end of the identifier which is |
| 282 | ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil |
| 283 | ;; is returned in this case, in all other cases nil is returned and |
| 284 | ;; point isn't moved. |
| 285 | ;; |
| 286 | ;; This function might do hidden buffer changes. |
| 287 | (when (and c-opt-cpp-macro-define-id |
| 288 | (looking-at c-opt-cpp-macro-define-id)) |
| 289 | (goto-char (match-end 0)))) |
| 290 | |
| 291 | (defun c-forward-to-cpp-define-body () |
| 292 | ;; Assuming point is at the "#" that introduces a preprocessor |
| 293 | ;; directive, it's moved forward to the start of the definition body |
| 294 | ;; if it's a "#define" (or whatever c-opt-cpp-macro-define |
| 295 | ;; specifies). Non-nil is returned in this case, in all other cases |
| 296 | ;; nil is returned and point isn't moved. |
| 297 | ;; |
| 298 | ;; This function might do hidden buffer changes. |
| 299 | (when (and c-opt-cpp-macro-define-start |
| 300 | (looking-at c-opt-cpp-macro-define-start) |
| 301 | (not (= (match-end 0) (c-point 'eol)))) |
| 302 | (goto-char (match-end 0)))) |
| 303 | |
| 304 | \f |
| 305 | ;;; Basic utility functions. |
| 306 | |
| 307 | (defun c-syntactic-content (from to paren-level) |
| 308 | ;; Return the given region as a string where all syntactic |
| 309 | ;; whitespace is removed or, where necessary, replaced with a single |
| 310 | ;; space. If PAREN-LEVEL is given then all parens in the region are |
| 311 | ;; collapsed to "()", "[]" etc. |
| 312 | ;; |
| 313 | ;; This function might do hidden buffer changes. |
| 314 | |
| 315 | (save-excursion |
| 316 | (save-restriction |
| 317 | (narrow-to-region from to) |
| 318 | (goto-char from) |
| 319 | (let* ((parts (list nil)) (tail parts) pos in-paren) |
| 320 | |
| 321 | (while (re-search-forward c-syntactic-ws-start to t) |
| 322 | (goto-char (setq pos (match-beginning 0))) |
| 323 | (c-forward-syntactic-ws) |
| 324 | (if (= (point) pos) |
| 325 | (forward-char) |
| 326 | |
| 327 | (when paren-level |
| 328 | (save-excursion |
| 329 | (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1) |
| 330 | pos (point)))) |
| 331 | |
| 332 | (if (and (> pos from) |
| 333 | (< (point) to) |
| 334 | (looking-at "\\w\\|\\s_") |
| 335 | (save-excursion |
| 336 | (goto-char (1- pos)) |
| 337 | (looking-at "\\w\\|\\s_"))) |
| 338 | (progn |
| 339 | (setcdr tail (list (buffer-substring-no-properties from pos) |
| 340 | " ")) |
| 341 | (setq tail (cddr tail))) |
| 342 | (setcdr tail (list (buffer-substring-no-properties from pos))) |
| 343 | (setq tail (cdr tail))) |
| 344 | |
| 345 | (when in-paren |
| 346 | (when (= (car (parse-partial-sexp pos to -1)) -1) |
| 347 | (setcdr tail (list (buffer-substring-no-properties |
| 348 | (1- (point)) (point)))) |
| 349 | (setq tail (cdr tail)))) |
| 350 | |
| 351 | (setq from (point)))) |
| 352 | |
| 353 | (setcdr tail (list (buffer-substring-no-properties from to))) |
| 354 | (apply 'concat (cdr parts)))))) |
| 355 | |
| 356 | (defun c-shift-line-indentation (shift-amt) |
| 357 | ;; Shift the indentation of the current line with the specified |
| 358 | ;; amount (positive inwards). The buffer is modified only if |
| 359 | ;; SHIFT-AMT isn't equal to zero. |
| 360 | (let ((pos (- (point-max) (point))) |
| 361 | (c-macro-start c-macro-start) |
| 362 | tmp-char-inserted) |
| 363 | (if (zerop shift-amt) |
| 364 | nil |
| 365 | ;; If we're on an empty line inside a macro, we take the point |
| 366 | ;; to be at the current indentation and shift it to the |
| 367 | ;; appropriate column. This way we don't treat the extra |
| 368 | ;; whitespace out to the line continuation as indentation. |
| 369 | (when (and (c-query-and-set-macro-start) |
| 370 | (looking-at "[ \t]*\\\\$") |
| 371 | (save-excursion |
| 372 | (skip-chars-backward " \t") |
| 373 | (bolp))) |
| 374 | (insert ?x) |
| 375 | (backward-char) |
| 376 | (setq tmp-char-inserted t)) |
| 377 | (unwind-protect |
| 378 | (let ((col (current-indentation))) |
| 379 | (delete-region (c-point 'bol) (c-point 'boi)) |
| 380 | (beginning-of-line) |
| 381 | (indent-to (+ col shift-amt))) |
| 382 | (when tmp-char-inserted |
| 383 | (delete-char 1)))) |
| 384 | ;; If initial point was within line's indentation and we're not on |
| 385 | ;; a line with a line continuation in a macro, position after the |
| 386 | ;; indentation. Else stay at same point in text. |
| 387 | (if (and (< (point) (c-point 'boi)) |
| 388 | (not tmp-char-inserted)) |
| 389 | (back-to-indentation) |
| 390 | (if (> (- (point-max) pos) (point)) |
| 391 | (goto-char (- (point-max) pos)))))) |
| 392 | |
| 393 | (defsubst c-keyword-sym (keyword) |
| 394 | ;; Return non-nil if the string KEYWORD is a known keyword. More |
| 395 | ;; precisely, the value is the symbol for the keyword in |
| 396 | ;; `c-keywords-obarray'. |
| 397 | (intern-soft keyword c-keywords-obarray)) |
| 398 | |
| 399 | (defsubst c-keyword-member (keyword-sym lang-constant) |
| 400 | ;; Return non-nil if the symbol KEYWORD-SYM, as returned by |
| 401 | ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name |
| 402 | ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is |
| 403 | ;; nil then the result is nil. |
| 404 | (get keyword-sym lang-constant)) |
| 405 | |
| 406 | ;; String syntax chars, suitable for skip-syntax-(forward|backward). |
| 407 | (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features) |
| 408 | "\"|" |
| 409 | "\"")) |
| 410 | |
| 411 | ;; Regexp matching string limit syntax. |
| 412 | (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features) |
| 413 | "\\s\"\\|\\s|" |
| 414 | "\\s\"")) |
| 415 | |
| 416 | ;; Regexp matching WS followed by string limit syntax. |
| 417 | (defconst c-ws*-string-limit-regexp |
| 418 | (concat "[ \t]*\\(" c-string-limit-regexp "\\)")) |
| 419 | |
| 420 | ;; Holds formatted error strings for the few cases where parse errors |
| 421 | ;; are reported. |
| 422 | (defvar c-parsing-error nil) |
| 423 | (make-variable-buffer-local 'c-parsing-error) |
| 424 | |
| 425 | (defun c-echo-parsing-error (&optional quiet) |
| 426 | (when (and c-report-syntactic-errors c-parsing-error (not quiet)) |
| 427 | (c-benign-error "%s" c-parsing-error)) |
| 428 | c-parsing-error) |
| 429 | |
| 430 | ;; Faces given to comments and string literals. This is used in some |
| 431 | ;; situations to speed up recognition; it isn't mandatory that font |
| 432 | ;; locking is in use. This variable is extended with the face in |
| 433 | ;; `c-doc-face-name' when fontification is activated in cc-fonts.el. |
| 434 | (defvar c-literal-faces |
| 435 | (append '(font-lock-comment-face font-lock-string-face) |
| 436 | (when (facep 'font-lock-comment-delimiter-face) |
| 437 | ;; New in Emacs 22. |
| 438 | '(font-lock-comment-delimiter-face)))) |
| 439 | |
| 440 | (defsubst c-put-c-type-property (pos value) |
| 441 | ;; Put a c-type property with the given value at POS. |
| 442 | (c-put-char-property pos 'c-type value)) |
| 443 | |
| 444 | (defun c-clear-c-type-property (from to value) |
| 445 | ;; Remove all occurrences of the c-type property that has the given |
| 446 | ;; value in the region between FROM and TO. VALUE is assumed to not |
| 447 | ;; be nil. |
| 448 | ;; |
| 449 | ;; Note: This assumes that c-type is put on single chars only; it's |
| 450 | ;; very inefficient if matching properties cover large regions. |
| 451 | (save-excursion |
| 452 | (goto-char from) |
| 453 | (while (progn |
| 454 | (when (eq (get-text-property (point) 'c-type) value) |
| 455 | (c-clear-char-property (point) 'c-type)) |
| 456 | (goto-char (next-single-property-change (point) 'c-type nil to)) |
| 457 | (< (point) to))))) |
| 458 | |
| 459 | \f |
| 460 | ;; Some debug tools to visualize various special positions. This |
| 461 | ;; debug code isn't as portable as the rest of CC Mode. |
| 462 | |
| 463 | (cc-bytecomp-defun overlays-in) |
| 464 | (cc-bytecomp-defun overlay-get) |
| 465 | (cc-bytecomp-defun overlay-start) |
| 466 | (cc-bytecomp-defun overlay-end) |
| 467 | (cc-bytecomp-defun delete-overlay) |
| 468 | (cc-bytecomp-defun overlay-put) |
| 469 | (cc-bytecomp-defun make-overlay) |
| 470 | |
| 471 | (defun c-debug-add-face (beg end face) |
| 472 | (c-save-buffer-state ((overlays (overlays-in beg end)) overlay) |
| 473 | (while overlays |
| 474 | (setq overlay (car overlays) |
| 475 | overlays (cdr overlays)) |
| 476 | (when (eq (overlay-get overlay 'face) face) |
| 477 | (setq beg (min beg (overlay-start overlay)) |
| 478 | end (max end (overlay-end overlay))) |
| 479 | (delete-overlay overlay))) |
| 480 | (overlay-put (make-overlay beg end) 'face face))) |
| 481 | |
| 482 | (defun c-debug-remove-face (beg end face) |
| 483 | (c-save-buffer-state ((overlays (overlays-in beg end)) overlay |
| 484 | (ol-beg beg) (ol-end end)) |
| 485 | (while overlays |
| 486 | (setq overlay (car overlays) |
| 487 | overlays (cdr overlays)) |
| 488 | (when (eq (overlay-get overlay 'face) face) |
| 489 | (setq ol-beg (min ol-beg (overlay-start overlay)) |
| 490 | ol-end (max ol-end (overlay-end overlay))) |
| 491 | (delete-overlay overlay))) |
| 492 | (when (< ol-beg beg) |
| 493 | (overlay-put (make-overlay ol-beg beg) 'face face)) |
| 494 | (when (> ol-end end) |
| 495 | (overlay-put (make-overlay end ol-end) 'face face)))) |
| 496 | |
| 497 | \f |
| 498 | ;; `c-beginning-of-statement-1' and accompanying stuff. |
| 499 | |
| 500 | ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between |
| 501 | ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A |
| 502 | ;; better way should be implemented, but this will at least shut up |
| 503 | ;; the byte compiler. |
| 504 | (defvar c-maybe-labelp) |
| 505 | |
| 506 | ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22 |
| 507 | |
| 508 | ;; Macros used internally in c-beginning-of-statement-1 for the |
| 509 | ;; automaton actions. |
| 510 | (defmacro c-bos-push-state () |
| 511 | '(setq stack (cons (cons state saved-pos) |
| 512 | stack))) |
| 513 | (defmacro c-bos-pop-state (&optional do-if-done) |
| 514 | `(if (setq state (car (car stack)) |
| 515 | saved-pos (cdr (car stack)) |
| 516 | stack (cdr stack)) |
| 517 | t |
| 518 | ,do-if-done |
| 519 | (throw 'loop nil))) |
| 520 | (defmacro c-bos-pop-state-and-retry () |
| 521 | '(throw 'loop (setq state (car (car stack)) |
| 522 | saved-pos (cdr (car stack)) |
| 523 | ;; Throw nil if stack is empty, else throw non-nil. |
| 524 | stack (cdr stack)))) |
| 525 | (defmacro c-bos-save-pos () |
| 526 | '(setq saved-pos (vector pos tok ptok pptok))) |
| 527 | (defmacro c-bos-restore-pos () |
| 528 | '(unless (eq (elt saved-pos 0) start) |
| 529 | (setq pos (elt saved-pos 0) |
| 530 | tok (elt saved-pos 1) |
| 531 | ptok (elt saved-pos 2) |
| 532 | pptok (elt saved-pos 3)) |
| 533 | (goto-char pos) |
| 534 | (setq sym nil))) |
| 535 | (defmacro c-bos-save-error-info (missing got) |
| 536 | `(setq saved-pos (vector pos ,missing ,got))) |
| 537 | (defmacro c-bos-report-error () |
| 538 | '(unless noerror |
| 539 | (setq c-parsing-error |
| 540 | (format "No matching `%s' found for `%s' on line %d" |
| 541 | (elt saved-pos 1) |
| 542 | (elt saved-pos 2) |
| 543 | (1+ (count-lines (point-min) |
| 544 | (c-point 'bol (elt saved-pos 0)))))))) |
| 545 | |
| 546 | (defun c-beginning-of-statement-1 (&optional lim ignore-labels |
| 547 | noerror comma-delim) |
| 548 | "Move to the start of the current statement or declaration, or to |
| 549 | the previous one if already at the beginning of one. Only |
| 550 | statements/declarations on the same level are considered, i.e. don't |
| 551 | move into or out of sexps (not even normal expression parentheses). |
| 552 | |
| 553 | If point is already at the earliest statement within braces or parens, |
| 554 | this function doesn't move back into any whitespace preceding it; it |
| 555 | returns 'same in this case. |
| 556 | |
| 557 | Stop at statement continuation tokens like \"else\", \"catch\", |
| 558 | \"finally\" and the \"while\" in \"do ... while\" if the start point |
| 559 | is within the continuation. If starting at such a token, move to the |
| 560 | corresponding statement start. If at the beginning of a statement, |
| 561 | move to the closest containing statement if there is any. This might |
| 562 | also stop at a continuation clause. |
| 563 | |
| 564 | Labels are treated as part of the following statements if |
| 565 | IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known |
| 566 | statement start keyword.) Otherwise, each label is treated as a |
| 567 | separate statement. |
| 568 | |
| 569 | Macros are ignored \(i.e. skipped over) unless point is within one, in |
| 570 | which case the content of the macro is treated as normal code. Aside |
| 571 | from any normal statement starts found in it, stop at the first token |
| 572 | of the content in the macro, i.e. the expression of an \"#if\" or the |
| 573 | start of the definition in a \"#define\". Also stop at start of |
| 574 | macros before leaving them. |
| 575 | |
| 576 | Return: |
| 577 | 'label if stopped at a label or \"case...:\" or \"default:\"; |
| 578 | 'same if stopped at the beginning of the current statement; |
| 579 | 'up if stepped to a containing statement; |
| 580 | 'previous if stepped to a preceding statement; |
| 581 | 'beginning if stepped from a statement continuation clause to |
| 582 | its start clause; or |
| 583 | 'macro if stepped to a macro start. |
| 584 | Note that 'same and not 'label is returned if stopped at the same |
| 585 | label without crossing the colon character. |
| 586 | |
| 587 | LIM may be given to limit the search. If the search hits the limit, |
| 588 | point will be left at the closest following token, or at the start |
| 589 | position if that is less ('same is returned in this case). |
| 590 | |
| 591 | NOERROR turns off error logging to `c-parsing-error'. |
| 592 | |
| 593 | Normally only ';' and virtual semicolons are considered to delimit |
| 594 | statements, but if COMMA-DELIM is non-nil then ',' is treated |
| 595 | as a delimiter too. |
| 596 | |
| 597 | Note that this function might do hidden buffer changes. See the |
| 598 | comment at the start of cc-engine.el for more info." |
| 599 | |
| 600 | ;; The bulk of this function is a pushdown automaton that looks at statement |
| 601 | ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its |
| 602 | ;; purpose is to keep track of nested statements, ensuring that such |
| 603 | ;; statements are skipped over in their entirety (somewhat akin to what C-M-p |
| 604 | ;; does with nested braces/brackets/parentheses). |
| 605 | ;; |
| 606 | ;; Note: The position of a boundary is the following token. |
| 607 | ;; |
| 608 | ;; Beginning with the current token (the one following point), move back one |
| 609 | ;; sexp at a time (where a sexp is, more or less, either a token or the |
| 610 | ;; entire contents of a brace/bracket/paren pair). Each time a statement |
| 611 | ;; boundary is crossed or a "while"-like token is found, update the state of |
| 612 | ;; the PDA. Stop at the beginning of a statement when the stack (holding |
| 613 | ;; nested statement info) is empty and the position has been moved. |
| 614 | ;; |
| 615 | ;; The following variables constitute the PDA: |
| 616 | ;; |
| 617 | ;; sym: This is either the "while"-like token (e.g. 'for) we've just |
| 618 | ;; scanned back over, 'boundary if we've just gone back over a |
| 619 | ;; statement boundary, or nil otherwise. |
| 620 | ;; state: takes one of the values (nil else else-boundary while |
| 621 | ;; while-boundary catch catch-boundary). |
| 622 | ;; nil means "no "while"-like token yet scanned". |
| 623 | ;; 'else, for example, means "just gone back over an else". |
| 624 | ;; 'else-boundary means "just gone back over a statement boundary |
| 625 | ;; immediately after having gone back over an else". |
| 626 | ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or |
| 627 | ;; of error reporting information. |
| 628 | ;; stack: The stack onto which the PDA pushes its state. Each entry |
| 629 | ;; consists of a saved value of state and saved-pos. An entry is |
| 630 | ;; pushed when we move back over a "continuation" token (e.g. else) |
| 631 | ;; and popped when we encounter the corresponding opening token |
| 632 | ;; (e.g. if). |
| 633 | ;; |
| 634 | ;; |
| 635 | ;; The following diagram briefly outlines the PDA. |
| 636 | ;; |
| 637 | ;; Common state: |
| 638 | ;; "else": Push state, goto state `else'. |
| 639 | ;; "while": Push state, goto state `while'. |
| 640 | ;; "catch" or "finally": Push state, goto state `catch'. |
| 641 | ;; boundary: Pop state. |
| 642 | ;; other: Do nothing special. |
| 643 | ;; |
| 644 | ;; State `else': |
| 645 | ;; boundary: Goto state `else-boundary'. |
| 646 | ;; other: Error, pop state, retry token. |
| 647 | ;; |
| 648 | ;; State `else-boundary': |
| 649 | ;; "if": Pop state. |
| 650 | ;; boundary: Error, pop state. |
| 651 | ;; other: See common state. |
| 652 | ;; |
| 653 | ;; State `while': |
| 654 | ;; boundary: Save position, goto state `while-boundary'. |
| 655 | ;; other: Pop state, retry token. |
| 656 | ;; |
| 657 | ;; State `while-boundary': |
| 658 | ;; "do": Pop state. |
| 659 | ;; boundary: Restore position if it's not at start, pop state. [*see below] |
| 660 | ;; other: See common state. |
| 661 | ;; |
| 662 | ;; State `catch': |
| 663 | ;; boundary: Goto state `catch-boundary'. |
| 664 | ;; other: Error, pop state, retry token. |
| 665 | ;; |
| 666 | ;; State `catch-boundary': |
| 667 | ;; "try": Pop state. |
| 668 | ;; "catch": Goto state `catch'. |
| 669 | ;; boundary: Error, pop state. |
| 670 | ;; other: See common state. |
| 671 | ;; |
| 672 | ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were |
| 673 | ;; searching for a "do" which would have opened a do-while. If we didn't |
| 674 | ;; find it, we discard the analysis done since the "while", go back to this |
| 675 | ;; token in the buffer and restart the scanning there, this time WITHOUT |
| 676 | ;; pushing the 'while state onto the stack. |
| 677 | ;; |
| 678 | ;; In addition to the above there is some special handling of labels |
| 679 | ;; and macros. |
| 680 | |
| 681 | (let ((case-fold-search nil) |
| 682 | (start (point)) |
| 683 | macro-start |
| 684 | (delims (if comma-delim '(?\; ?,) '(?\;))) |
| 685 | (c-stmt-delim-chars (if comma-delim |
| 686 | c-stmt-delim-chars-with-comma |
| 687 | c-stmt-delim-chars)) |
| 688 | c-in-literal-cache c-maybe-labelp after-case:-pos saved |
| 689 | ;; Current position. |
| 690 | pos |
| 691 | ;; Position of last stmt boundary character (e.g. ;). |
| 692 | boundary-pos |
| 693 | ;; The position of the last sexp or bound that follows the |
| 694 | ;; first found colon, i.e. the start of the nonlabel part of |
| 695 | ;; the statement. It's `start' if a colon is found just after |
| 696 | ;; the start. |
| 697 | after-labels-pos |
| 698 | ;; Like `after-labels-pos', but the first such position inside |
| 699 | ;; a label, i.e. the start of the last label before the start |
| 700 | ;; of the nonlabel part of the statement. |
| 701 | last-label-pos |
| 702 | ;; The last position where a label is possible provided the |
| 703 | ;; statement started there. It's nil as long as no invalid |
| 704 | ;; label content has been found (according to |
| 705 | ;; `c-nonlabel-token-key'). It's `start' if no valid label |
| 706 | ;; content was found in the label. Note that we might still |
| 707 | ;; regard it a label if it starts with `c-label-kwds'. |
| 708 | label-good-pos |
| 709 | ;; Putative positions of the components of a bitfield declaration, |
| 710 | ;; e.g. "int foo : NUM_FOO_BITS ;" |
| 711 | bitfield-type-pos bitfield-id-pos bitfield-size-pos |
| 712 | ;; Symbol just scanned back over (e.g. 'while or 'boundary). |
| 713 | ;; See above. |
| 714 | sym |
| 715 | ;; Current state in the automaton. See above. |
| 716 | state |
| 717 | ;; Current saved positions. See above. |
| 718 | saved-pos |
| 719 | ;; Stack of conses (state . saved-pos). |
| 720 | stack |
| 721 | ;; Regexp which matches "for", "if", etc. |
| 722 | (cond-key (or c-opt-block-stmt-key |
| 723 | "\\<\\>")) ; Matches nothing. |
| 724 | ;; Return value. |
| 725 | (ret 'same) |
| 726 | ;; Positions of the last three sexps or bounds we've stopped at. |
| 727 | tok ptok pptok) |
| 728 | |
| 729 | (save-restriction |
| 730 | (if lim (narrow-to-region lim (point-max))) |
| 731 | |
| 732 | (if (save-excursion |
| 733 | (and (c-beginning-of-macro) |
| 734 | (/= (point) start))) |
| 735 | (setq macro-start (point))) |
| 736 | |
| 737 | ;; Try to skip back over unary operator characters, to register |
| 738 | ;; that we've moved. |
| 739 | (while (progn |
| 740 | (setq pos (point)) |
| 741 | (c-backward-syntactic-ws) |
| 742 | ;; Protect post-++/-- operators just before a virtual semicolon. |
| 743 | (and (not (c-at-vsemi-p)) |
| 744 | (/= (skip-chars-backward "-+!*&~@`#") 0)))) |
| 745 | |
| 746 | ;; Skip back over any semicolon here. If it was a bare semicolon, we're |
| 747 | ;; done. Later on we ignore the boundaries for statements that don't |
| 748 | ;; contain any sexp. The only thing that is affected is that the error |
| 749 | ;; checking is a little less strict, and we really don't bother. |
| 750 | (if (and (memq (char-before) delims) |
| 751 | (progn (forward-char -1) |
| 752 | (setq saved (point)) |
| 753 | (c-backward-syntactic-ws) |
| 754 | (or (memq (char-before) delims) |
| 755 | (memq (char-before) '(?: nil)) |
| 756 | (eq (char-syntax (char-before)) ?\() |
| 757 | (c-at-vsemi-p)))) |
| 758 | (setq ret 'previous |
| 759 | pos saved) |
| 760 | |
| 761 | ;; Begin at start and not pos to detect macros if we stand |
| 762 | ;; directly after the #. |
| 763 | (goto-char start) |
| 764 | (if (looking-at "\\<\\|\\W") |
| 765 | ;; Record this as the first token if not starting inside it. |
| 766 | (setq tok start)) |
| 767 | |
| 768 | |
| 769 | ;; The following while loop goes back one sexp (balanced parens, |
| 770 | ;; etc. with contents, or symbol or suchlike) each iteration. This |
| 771 | ;; movement is accomplished with a call to c-backward-sexp approx 170 |
| 772 | ;; lines below. |
| 773 | ;; |
| 774 | ;; The loop is exited only by throwing nil to the (catch 'loop ...): |
| 775 | ;; 1. On reaching the start of a macro; |
| 776 | ;; 2. On having passed a stmt boundary with the PDA stack empty; |
| 777 | ;; 3. On reaching the start of an Objective C method def; |
| 778 | ;; 4. From macro `c-bos-pop-state'; when the stack is empty; |
| 779 | ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty. |
| 780 | (while |
| 781 | (catch 'loop ;; Throw nil to break, non-nil to continue. |
| 782 | (cond |
| 783 | ;; Are we in a macro, just after the opening #? |
| 784 | ((save-excursion |
| 785 | (and macro-start ; Always NIL for AWK. |
| 786 | (progn (skip-chars-backward " \t") |
| 787 | (eq (char-before) ?#)) |
| 788 | (progn (setq saved (1- (point))) |
| 789 | (beginning-of-line) |
| 790 | (not (eq (char-before (1- (point))) ?\\))) |
| 791 | (looking-at c-opt-cpp-start) |
| 792 | (progn (skip-chars-forward " \t") |
| 793 | (eq (point) saved)))) |
| 794 | (goto-char saved) |
| 795 | (if (and (c-forward-to-cpp-define-body) |
| 796 | (progn (c-forward-syntactic-ws start) |
| 797 | (< (point) start))) |
| 798 | ;; Stop at the first token in the content of the macro. |
| 799 | (setq pos (point) |
| 800 | ignore-labels t) ; Avoid the label check on exit. |
| 801 | (setq pos saved |
| 802 | ret 'macro |
| 803 | ignore-labels t)) |
| 804 | (throw 'loop nil)) ; 1. Start of macro. |
| 805 | |
| 806 | ;; Do a round through the automaton if we've just passed a |
| 807 | ;; statement boundary or passed a "while"-like token. |
| 808 | ((or sym |
| 809 | (and (looking-at cond-key) |
| 810 | (setq sym (intern (match-string 1))))) |
| 811 | |
| 812 | (when (and (< pos start) (null stack)) |
| 813 | (throw 'loop nil)) ; 2. Statement boundary. |
| 814 | |
| 815 | ;; The PDA state handling. |
| 816 | ;; |
| 817 | ;; Refer to the description of the PDA in the opening |
| 818 | ;; comments. In the following OR form, the first leaf |
| 819 | ;; attempts to handles one of the specific actions detailed |
| 820 | ;; (e.g., finding token "if" whilst in state `else-boundary'). |
| 821 | ;; We drop through to the second leaf (which handles common |
| 822 | ;; state) if no specific handler is found in the first cond. |
| 823 | ;; If a parsing error is detected (e.g. an "else" with no |
| 824 | ;; preceding "if"), we throw to the enclosing catch. |
| 825 | ;; |
| 826 | ;; Note that the (eq state 'else) means |
| 827 | ;; "we've just passed an else", NOT "we're looking for an |
| 828 | ;; else". |
| 829 | (or (cond |
| 830 | ((eq state 'else) |
| 831 | (if (eq sym 'boundary) |
| 832 | (setq state 'else-boundary) |
| 833 | (c-bos-report-error) |
| 834 | (c-bos-pop-state-and-retry))) |
| 835 | |
| 836 | ((eq state 'else-boundary) |
| 837 | (cond ((eq sym 'if) |
| 838 | (c-bos-pop-state (setq ret 'beginning))) |
| 839 | ((eq sym 'boundary) |
| 840 | (c-bos-report-error) |
| 841 | (c-bos-pop-state)))) |
| 842 | |
| 843 | ((eq state 'while) |
| 844 | (if (and (eq sym 'boundary) |
| 845 | ;; Since this can cause backtracking we do a |
| 846 | ;; little more careful analysis to avoid it: |
| 847 | ;; If there's a label in front of the while |
| 848 | ;; it can't be part of a do-while. |
| 849 | (not after-labels-pos)) |
| 850 | (progn (c-bos-save-pos) |
| 851 | (setq state 'while-boundary)) |
| 852 | (c-bos-pop-state-and-retry))) ; Can't be a do-while |
| 853 | |
| 854 | ((eq state 'while-boundary) |
| 855 | (cond ((eq sym 'do) |
| 856 | (c-bos-pop-state (setq ret 'beginning))) |
| 857 | ((eq sym 'boundary) ; isn't a do-while |
| 858 | (c-bos-restore-pos) ; the position of the while |
| 859 | (c-bos-pop-state)))) ; no longer searching for do. |
| 860 | |
| 861 | ((eq state 'catch) |
| 862 | (if (eq sym 'boundary) |
| 863 | (setq state 'catch-boundary) |
| 864 | (c-bos-report-error) |
| 865 | (c-bos-pop-state-and-retry))) |
| 866 | |
| 867 | ((eq state 'catch-boundary) |
| 868 | (cond |
| 869 | ((eq sym 'try) |
| 870 | (c-bos-pop-state (setq ret 'beginning))) |
| 871 | ((eq sym 'catch) |
| 872 | (setq state 'catch)) |
| 873 | ((eq sym 'boundary) |
| 874 | (c-bos-report-error) |
| 875 | (c-bos-pop-state))))) |
| 876 | |
| 877 | ;; This is state common. We get here when the previous |
| 878 | ;; cond statement found no particular state handler. |
| 879 | (cond ((eq sym 'boundary) |
| 880 | ;; If we have a boundary at the start |
| 881 | ;; position we push a frame to go to the |
| 882 | ;; previous statement. |
| 883 | (if (>= pos start) |
| 884 | (c-bos-push-state) |
| 885 | (c-bos-pop-state))) |
| 886 | ((eq sym 'else) |
| 887 | (c-bos-push-state) |
| 888 | (c-bos-save-error-info 'if 'else) |
| 889 | (setq state 'else)) |
| 890 | ((eq sym 'while) |
| 891 | ;; Is this a real while, or a do-while? |
| 892 | ;; The next `when' triggers unless we are SURE that |
| 893 | ;; the `while' is not the tail end of a `do-while'. |
| 894 | (when (or (not pptok) |
| 895 | (memq (char-after pptok) delims) |
| 896 | ;; The following kludge is to prevent |
| 897 | ;; infinite recursion when called from |
| 898 | ;; c-awk-after-if-for-while-condition-p, |
| 899 | ;; or the like. |
| 900 | (and (eq (point) start) |
| 901 | (c-vsemi-status-unknown-p)) |
| 902 | (c-at-vsemi-p pptok)) |
| 903 | ;; Since this can cause backtracking we do a |
| 904 | ;; little more careful analysis to avoid it: If |
| 905 | ;; the while isn't followed by a (possibly |
| 906 | ;; virtual) semicolon it can't be a do-while. |
| 907 | (c-bos-push-state) |
| 908 | (setq state 'while))) |
| 909 | ((memq sym '(catch finally)) |
| 910 | (c-bos-push-state) |
| 911 | (c-bos-save-error-info 'try sym) |
| 912 | (setq state 'catch)))) |
| 913 | |
| 914 | (when c-maybe-labelp |
| 915 | ;; We're either past a statement boundary or at the |
| 916 | ;; start of a statement, so throw away any label data |
| 917 | ;; for the previous one. |
| 918 | (setq after-labels-pos nil |
| 919 | last-label-pos nil |
| 920 | c-maybe-labelp nil)))) |
| 921 | |
| 922 | ;; Step to the previous sexp, but not if we crossed a |
| 923 | ;; boundary, since that doesn't consume an sexp. |
| 924 | (if (eq sym 'boundary) |
| 925 | (setq ret 'previous) |
| 926 | |
| 927 | ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE |
| 928 | ;; BACKWARDS THROUGH THE SOURCE. |
| 929 | |
| 930 | (c-backward-syntactic-ws) |
| 931 | (let ((before-sws-pos (point)) |
| 932 | ;; The end position of the area to search for statement |
| 933 | ;; barriers in this round. |
| 934 | (maybe-after-boundary-pos pos)) |
| 935 | |
| 936 | ;; Go back over exactly one logical sexp, taking proper |
| 937 | ;; account of macros and escaped EOLs. |
| 938 | (while |
| 939 | (progn |
| 940 | (unless (c-safe (c-backward-sexp) t) |
| 941 | ;; Give up if we hit an unbalanced block. Since the |
| 942 | ;; stack won't be empty the code below will report a |
| 943 | ;; suitable error. |
| 944 | (throw 'loop nil)) |
| 945 | (cond |
| 946 | ;; Have we moved into a macro? |
| 947 | ((and (not macro-start) |
| 948 | (c-beginning-of-macro)) |
| 949 | ;; Have we crossed a statement boundary? If not, |
| 950 | ;; keep going back until we find one or a "real" sexp. |
| 951 | (and |
| 952 | (save-excursion |
| 953 | (c-end-of-macro) |
| 954 | (not (c-crosses-statement-barrier-p |
| 955 | (point) maybe-after-boundary-pos))) |
| 956 | (setq maybe-after-boundary-pos (point)))) |
| 957 | ;; Have we just gone back over an escaped NL? This |
| 958 | ;; doesn't count as a sexp. |
| 959 | ((looking-at "\\\\$"))))) |
| 960 | |
| 961 | ;; Have we crossed a statement boundary? |
| 962 | (setq boundary-pos |
| 963 | (cond |
| 964 | ;; Are we at a macro beginning? |
| 965 | ((and (not macro-start) |
| 966 | c-opt-cpp-prefix |
| 967 | (looking-at c-opt-cpp-prefix)) |
| 968 | (save-excursion |
| 969 | (c-end-of-macro) |
| 970 | (c-crosses-statement-barrier-p |
| 971 | (point) maybe-after-boundary-pos))) |
| 972 | ;; Just gone back over a brace block? |
| 973 | ((and |
| 974 | (eq (char-after) ?{) |
| 975 | (not (c-looking-at-inexpr-block lim nil t))) |
| 976 | (save-excursion |
| 977 | (c-forward-sexp) (point))) |
| 978 | ;; Just gone back over some paren block? |
| 979 | ((looking-at "\\s\(") |
| 980 | (save-excursion |
| 981 | (goto-char (1+ (c-down-list-backward |
| 982 | before-sws-pos))) |
| 983 | (c-crosses-statement-barrier-p |
| 984 | (point) maybe-after-boundary-pos))) |
| 985 | ;; Just gone back over an ordinary symbol of some sort? |
| 986 | (t (c-crosses-statement-barrier-p |
| 987 | (point) maybe-after-boundary-pos)))) |
| 988 | |
| 989 | (when boundary-pos |
| 990 | (setq pptok ptok |
| 991 | ptok tok |
| 992 | tok boundary-pos |
| 993 | sym 'boundary) |
| 994 | ;; Like a C "continue". Analyze the next sexp. |
| 995 | (throw 'loop t)))) |
| 996 | |
| 997 | ;; ObjC method def? |
| 998 | (when (and c-opt-method-key |
| 999 | (setq saved (c-in-method-def-p))) |
| 1000 | (setq pos saved |
| 1001 | ignore-labels t) ; Avoid the label check on exit. |
| 1002 | (throw 'loop nil)) ; 3. ObjC method def. |
| 1003 | |
| 1004 | ;; Might we have a bitfield declaration, "<type> <id> : <size>"? |
| 1005 | (if c-has-bitfields |
| 1006 | (cond |
| 1007 | ;; The : <size> and <id> fields? |
| 1008 | ((and (numberp c-maybe-labelp) |
| 1009 | (not bitfield-size-pos) |
| 1010 | (save-excursion |
| 1011 | (goto-char (or tok start)) |
| 1012 | (not (looking-at c-keywords-regexp))) |
| 1013 | (not (looking-at c-keywords-regexp)) |
| 1014 | (not (c-punctuation-in (point) c-maybe-labelp))) |
| 1015 | (setq bitfield-size-pos (or tok start) |
| 1016 | bitfield-id-pos (point))) |
| 1017 | ;; The <type> field? |
| 1018 | ((and bitfield-id-pos |
| 1019 | (not bitfield-type-pos)) |
| 1020 | (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-) |
| 1021 | (not (looking-at c-not-primitive-type-keywords-regexp)) |
| 1022 | (not (c-punctuation-in (point) tok))) |
| 1023 | (setq bitfield-type-pos (point)) |
| 1024 | (setq bitfield-size-pos nil |
| 1025 | bitfield-id-pos nil))))) |
| 1026 | |
| 1027 | ;; Handle labels. |
| 1028 | (unless (eq ignore-labels t) |
| 1029 | (when (numberp c-maybe-labelp) |
| 1030 | ;; `c-crosses-statement-barrier-p' has found a colon, so we |
| 1031 | ;; might be in a label now. Have we got a real label |
| 1032 | ;; (including a case label) or something like C++'s "public:"? |
| 1033 | ;; A case label might use an expression rather than a token. |
| 1034 | (setq after-case:-pos (or tok start)) |
| 1035 | (if (or (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'" |
| 1036 | ;; Catch C++'s inheritance construct "class foo : bar". |
| 1037 | (save-excursion |
| 1038 | (and |
| 1039 | (c-safe (c-backward-sexp) t) |
| 1040 | (looking-at c-nonlabel-token-2-key)))) |
| 1041 | (setq c-maybe-labelp nil) |
| 1042 | (if after-labels-pos ; Have we already encountered a label? |
| 1043 | (if (not last-label-pos) |
| 1044 | (setq last-label-pos (or tok start))) |
| 1045 | (setq after-labels-pos (or tok start))) |
| 1046 | (setq c-maybe-labelp t |
| 1047 | label-good-pos nil))) ; bogus "label" |
| 1048 | |
| 1049 | (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet |
| 1050 | ; been found. |
| 1051 | (looking-at c-nonlabel-token-key)) ; e.g. "while :" |
| 1052 | ;; We're in a potential label and it's the first |
| 1053 | ;; time we've found something that isn't allowed in |
| 1054 | ;; one. |
| 1055 | (setq label-good-pos (or tok start)))) |
| 1056 | |
| 1057 | ;; We've moved back by a sexp, so update the token positions. |
| 1058 | (setq sym nil |
| 1059 | pptok ptok |
| 1060 | ptok tok |
| 1061 | tok (point) |
| 1062 | pos tok) ; always non-nil |
| 1063 | ) ; end of (catch loop ....) |
| 1064 | ) ; end of sexp-at-a-time (while ....) |
| 1065 | |
| 1066 | ;; If the stack isn't empty there might be errors to report. |
| 1067 | (while stack |
| 1068 | (if (and (vectorp saved-pos) (eq (length saved-pos) 3)) |
| 1069 | (c-bos-report-error)) |
| 1070 | (setq saved-pos (cdr (car stack)) |
| 1071 | stack (cdr stack))) |
| 1072 | |
| 1073 | (when (and (eq ret 'same) |
| 1074 | (not (memq sym '(boundary ignore nil)))) |
| 1075 | ;; Need to investigate closer whether we've crossed |
| 1076 | ;; between a substatement and its containing statement. |
| 1077 | (if (setq saved (if (looking-at c-block-stmt-1-key) |
| 1078 | ptok |
| 1079 | pptok)) |
| 1080 | (cond ((> start saved) (setq pos saved)) |
| 1081 | ((= start saved) (setq ret 'up))))) |
| 1082 | |
| 1083 | (when (and (not ignore-labels) |
| 1084 | (eq c-maybe-labelp t) |
| 1085 | (not (eq ret 'beginning)) |
| 1086 | after-labels-pos |
| 1087 | (not bitfield-type-pos) ; Bitfields take precedence over labels. |
| 1088 | (or (not label-good-pos) |
| 1089 | (<= label-good-pos pos) |
| 1090 | (progn |
| 1091 | (goto-char (if (and last-label-pos |
| 1092 | (< last-label-pos start)) |
| 1093 | last-label-pos |
| 1094 | pos)) |
| 1095 | (looking-at c-label-kwds-regexp)))) |
| 1096 | ;; We're in a label. Maybe we should step to the statement |
| 1097 | ;; after it. |
| 1098 | (if (< after-labels-pos start) |
| 1099 | (setq pos after-labels-pos) |
| 1100 | (setq ret 'label) |
| 1101 | (if (and last-label-pos (< last-label-pos start)) |
| 1102 | ;; Might have jumped over several labels. Go to the last one. |
| 1103 | (setq pos last-label-pos))))) |
| 1104 | |
| 1105 | ;; Have we got "case <expression>:"? |
| 1106 | (goto-char pos) |
| 1107 | (when (and after-case:-pos |
| 1108 | (not (eq ret 'beginning)) |
| 1109 | (looking-at c-case-kwds-regexp)) |
| 1110 | (if (< after-case:-pos start) |
| 1111 | (setq pos after-case:-pos)) |
| 1112 | (if (eq ret 'same) |
| 1113 | (setq ret 'label))) |
| 1114 | |
| 1115 | ;; Skip over the unary operators that can start the statement. |
| 1116 | (while (progn |
| 1117 | (c-backward-syntactic-ws) |
| 1118 | ;; protect AWK post-inc/decrement operators, etc. |
| 1119 | (and (not (c-at-vsemi-p (point))) |
| 1120 | (/= (skip-chars-backward "-+!*&~@`#") 0))) |
| 1121 | (setq pos (point))) |
| 1122 | (goto-char pos) |
| 1123 | ret))) |
| 1124 | |
| 1125 | (defun c-punctuation-in (from to) |
| 1126 | "Return non-nil if there is a non-comment non-macro punctuation character |
| 1127 | between FROM and TO. FROM must not be in a string or comment. The returned |
| 1128 | value is the position of the first such character." |
| 1129 | (save-excursion |
| 1130 | (goto-char from) |
| 1131 | (let ((pos (point))) |
| 1132 | (while (progn (skip-chars-forward c-symbol-chars to) |
| 1133 | (c-forward-syntactic-ws to) |
| 1134 | (> (point) pos)) |
| 1135 | (setq pos (point)))) |
| 1136 | (and (< (point) to) (point)))) |
| 1137 | |
| 1138 | (defun c-crosses-statement-barrier-p (from to) |
| 1139 | "Return non-nil if buffer positions FROM to TO cross one or more |
| 1140 | statement or declaration boundaries. The returned value is actually |
| 1141 | the position of the earliest boundary char. FROM must not be within |
| 1142 | a string or comment. |
| 1143 | |
| 1144 | The variable `c-maybe-labelp' is set to the position of the first `:' that |
| 1145 | might start a label (i.e. not part of `::' and not preceded by `?'). If a |
| 1146 | single `?' is found, then `c-maybe-labelp' is cleared. |
| 1147 | |
| 1148 | For AWK, a statement which is terminated by an EOL (not a \; or a }) is |
| 1149 | regarded as having a \"virtual semicolon\" immediately after the last token on |
| 1150 | the line. If this virtual semicolon is _at_ from, the function recognizes it. |
| 1151 | |
| 1152 | Note that this function might do hidden buffer changes. See the |
| 1153 | comment at the start of cc-engine.el for more info." |
| 1154 | (let* ((skip-chars |
| 1155 | ;; If the current language has CPP macros, insert # into skip-chars. |
| 1156 | (if c-opt-cpp-symbol |
| 1157 | (concat (substring c-stmt-delim-chars 0 1) ; "^" |
| 1158 | c-opt-cpp-symbol ; usually "#" |
| 1159 | (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:" |
| 1160 | c-stmt-delim-chars)) |
| 1161 | (non-skip-list |
| 1162 | (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:) |
| 1163 | lit-range vsemi-pos) |
| 1164 | (save-restriction |
| 1165 | (widen) |
| 1166 | (save-excursion |
| 1167 | (catch 'done |
| 1168 | (goto-char from) |
| 1169 | (while (progn (skip-chars-forward |
| 1170 | skip-chars |
| 1171 | (min to (c-point 'bonl))) |
| 1172 | (< (point) to)) |
| 1173 | (cond |
| 1174 | ;; Virtual semicolon? |
| 1175 | ((and (bolp) |
| 1176 | (save-excursion |
| 1177 | (progn |
| 1178 | (if (setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment? |
| 1179 | (goto-char (car lit-range))) |
| 1180 | (c-backward-syntactic-ws) ; ? put a limit here, maybe? |
| 1181 | (setq vsemi-pos (point)) |
| 1182 | (c-at-vsemi-p)))) |
| 1183 | (throw 'done vsemi-pos)) |
| 1184 | ;; In a string/comment? |
| 1185 | ((setq lit-range (c-literal-limits)) |
| 1186 | (goto-char (cdr lit-range))) |
| 1187 | ((eq (char-after) ?:) |
| 1188 | (forward-char) |
| 1189 | (if (and (eq (char-after) ?:) |
| 1190 | (< (point) to)) |
| 1191 | ;; Ignore scope operators. |
| 1192 | (forward-char) |
| 1193 | (setq c-maybe-labelp (1- (point))))) |
| 1194 | ((eq (char-after) ??) |
| 1195 | ;; A question mark. Can't be a label, so stop |
| 1196 | ;; looking for more : and ?. |
| 1197 | (setq c-maybe-labelp nil |
| 1198 | skip-chars (substring c-stmt-delim-chars 0 -2))) |
| 1199 | ;; At a CPP construct? |
| 1200 | ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol) |
| 1201 | (save-excursion |
| 1202 | (forward-line 0) |
| 1203 | (looking-at c-opt-cpp-prefix))) |
| 1204 | (c-end-of-macro)) |
| 1205 | ((memq (char-after) non-skip-list) |
| 1206 | (throw 'done (point))))) |
| 1207 | ;; In trailing space after an as yet undetected virtual semicolon? |
| 1208 | (c-backward-syntactic-ws from) |
| 1209 | (if (and (< (point) to) |
| 1210 | (c-at-vsemi-p)) |
| 1211 | (point) |
| 1212 | nil)))))) |
| 1213 | |
| 1214 | (defun c-at-statement-start-p () |
| 1215 | "Return non-nil if the point is at the first token in a statement |
| 1216 | or somewhere in the syntactic whitespace before it. |
| 1217 | |
| 1218 | A \"statement\" here is not restricted to those inside code blocks. |
| 1219 | Any kind of declaration-like construct that occur outside function |
| 1220 | bodies is also considered a \"statement\". |
| 1221 | |
| 1222 | Note that this function might do hidden buffer changes. See the |
| 1223 | comment at the start of cc-engine.el for more info." |
| 1224 | |
| 1225 | (save-excursion |
| 1226 | (let ((end (point)) |
| 1227 | c-maybe-labelp) |
| 1228 | (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t) |
| 1229 | (or (bobp) |
| 1230 | (eq (char-before) ?}) |
| 1231 | (and (eq (char-before) ?{) |
| 1232 | (not (and c-special-brace-lists |
| 1233 | (progn (backward-char) |
| 1234 | (c-looking-at-special-brace-list))))) |
| 1235 | (c-crosses-statement-barrier-p (point) end))))) |
| 1236 | |
| 1237 | (defun c-at-expression-start-p () |
| 1238 | "Return non-nil if the point is at the first token in an expression or |
| 1239 | statement, or somewhere in the syntactic whitespace before it. |
| 1240 | |
| 1241 | An \"expression\" here is a bit different from the normal language |
| 1242 | grammar sense: It's any sequence of expression tokens except commas, |
| 1243 | unless they are enclosed inside parentheses of some kind. Also, an |
| 1244 | expression never continues past an enclosing parenthesis, but it might |
| 1245 | contain parenthesis pairs of any sort except braces. |
| 1246 | |
| 1247 | Since expressions never cross statement boundaries, this function also |
| 1248 | recognizes statement beginnings, just like `c-at-statement-start-p'. |
| 1249 | |
| 1250 | Note that this function might do hidden buffer changes. See the |
| 1251 | comment at the start of cc-engine.el for more info." |
| 1252 | |
| 1253 | (save-excursion |
| 1254 | (let ((end (point)) |
| 1255 | (c-stmt-delim-chars c-stmt-delim-chars-with-comma) |
| 1256 | c-maybe-labelp) |
| 1257 | (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t) |
| 1258 | (or (bobp) |
| 1259 | (memq (char-before) '(?{ ?})) |
| 1260 | (save-excursion (backward-char) |
| 1261 | (looking-at "\\s(")) |
| 1262 | (c-crosses-statement-barrier-p (point) end))))) |
| 1263 | |
| 1264 | \f |
| 1265 | ;; A set of functions that covers various idiosyncrasies in |
| 1266 | ;; implementations of `forward-comment'. |
| 1267 | |
| 1268 | ;; Note: Some emacsen considers incorrectly that any line comment |
| 1269 | ;; ending with a backslash continues to the next line. I can't think |
| 1270 | ;; of any way to work around that in a reliable way without changing |
| 1271 | ;; the buffer, though. Suggestions welcome. ;) (No, temporarily |
| 1272 | ;; changing the syntax for backslash doesn't work since we must treat |
| 1273 | ;; escapes in string literals correctly.) |
| 1274 | |
| 1275 | (defun c-forward-single-comment () |
| 1276 | "Move forward past whitespace and the closest following comment, if any. |
| 1277 | Return t if a comment was found, nil otherwise. In either case, the |
| 1278 | point is moved past the following whitespace. Line continuations, |
| 1279 | i.e. a backslashes followed by line breaks, are treated as whitespace. |
| 1280 | The line breaks that end line comments are considered to be the |
| 1281 | comment enders, so the point will be put on the beginning of the next |
| 1282 | line if it moved past a line comment. |
| 1283 | |
| 1284 | This function does not do any hidden buffer changes." |
| 1285 | |
| 1286 | (let ((start (point))) |
| 1287 | (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+") |
| 1288 | (goto-char (match-end 0))) |
| 1289 | |
| 1290 | (when (forward-comment 1) |
| 1291 | (if (eobp) |
| 1292 | ;; Some emacsen (e.g. XEmacs 21) return t when moving |
| 1293 | ;; forwards at eob. |
| 1294 | nil |
| 1295 | |
| 1296 | ;; Emacs includes the ending newline in a b-style (c++) |
| 1297 | ;; comment, but XEmacs doesn't. We depend on the Emacs |
| 1298 | ;; behavior (which also is symmetric). |
| 1299 | (if (and (eolp) (elt (parse-partial-sexp start (point)) 7)) |
| 1300 | (condition-case nil (forward-char 1))) |
| 1301 | |
| 1302 | t)))) |
| 1303 | |
| 1304 | (defsubst c-forward-comments () |
| 1305 | "Move forward past all following whitespace and comments. |
| 1306 | Line continuations, i.e. a backslashes followed by line breaks, are |
| 1307 | treated as whitespace. |
| 1308 | |
| 1309 | Note that this function might do hidden buffer changes. See the |
| 1310 | comment at the start of cc-engine.el for more info." |
| 1311 | |
| 1312 | (while (or |
| 1313 | ;; If forward-comment in at least XEmacs 21 is given a large |
| 1314 | ;; positive value, it'll loop all the way through if it hits |
| 1315 | ;; eob. |
| 1316 | (and (forward-comment 5) |
| 1317 | ;; Some emacsen (e.g. XEmacs 21) return t when moving |
| 1318 | ;; forwards at eob. |
| 1319 | (not (eobp))) |
| 1320 | |
| 1321 | (when (looking-at "\\\\[\n\r]") |
| 1322 | (forward-char 2) |
| 1323 | t)))) |
| 1324 | |
| 1325 | (defun c-backward-single-comment () |
| 1326 | "Move backward past whitespace and the closest preceding comment, if any. |
| 1327 | Return t if a comment was found, nil otherwise. In either case, the |
| 1328 | point is moved past the preceding whitespace. Line continuations, |
| 1329 | i.e. a backslashes followed by line breaks, are treated as whitespace. |
| 1330 | The line breaks that end line comments are considered to be the |
| 1331 | comment enders, so the point cannot be at the end of the same line to |
| 1332 | move over a line comment. |
| 1333 | |
| 1334 | This function does not do any hidden buffer changes." |
| 1335 | |
| 1336 | (let ((start (point))) |
| 1337 | ;; When we got newline terminated comments, forward-comment in all |
| 1338 | ;; supported emacsen so far will stop at eol of each line not |
| 1339 | ;; ending with a comment when moving backwards. This corrects for |
| 1340 | ;; that, and at the same time handles line continuations. |
| 1341 | (while (progn |
| 1342 | (skip-chars-backward " \t\n\r\f\v") |
| 1343 | (and (looking-at "[\n\r]") |
| 1344 | (eq (char-before) ?\\))) |
| 1345 | (backward-char)) |
| 1346 | |
| 1347 | (if (bobp) |
| 1348 | ;; Some emacsen (e.g. Emacs 19.34) return t when moving |
| 1349 | ;; backwards at bob. |
| 1350 | nil |
| 1351 | |
| 1352 | ;; Leave point after the closest following newline if we've |
| 1353 | ;; backed up over any above, since forward-comment won't move |
| 1354 | ;; backward over a line comment if point is at the end of the |
| 1355 | ;; same line. |
| 1356 | (re-search-forward "\\=\\s *[\n\r]" start t) |
| 1357 | |
| 1358 | (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -1)) |
| 1359 | (if (eolp) |
| 1360 | ;; If forward-comment above succeeded and we're at eol |
| 1361 | ;; then the newline we moved over above didn't end a |
| 1362 | ;; line comment, so we give it another go. |
| 1363 | (let (open-paren-in-column-0-is-defun-start) |
| 1364 | (forward-comment -1)) |
| 1365 | t)) |
| 1366 | |
| 1367 | ;; Emacs <= 20 and XEmacs move back over the closer of a |
| 1368 | ;; block comment that lacks an opener. |
| 1369 | (if (looking-at "\\*/") |
| 1370 | (progn (forward-char 2) nil) |
| 1371 | t))))) |
| 1372 | |
| 1373 | (defsubst c-backward-comments () |
| 1374 | "Move backward past all preceding whitespace and comments. |
| 1375 | Line continuations, i.e. a backslashes followed by line breaks, are |
| 1376 | treated as whitespace. The line breaks that end line comments are |
| 1377 | considered to be the comment enders, so the point cannot be at the end |
| 1378 | of the same line to move over a line comment. Unlike |
| 1379 | c-backward-syntactic-ws, this function doesn't move back over |
| 1380 | preprocessor directives. |
| 1381 | |
| 1382 | Note that this function might do hidden buffer changes. See the |
| 1383 | comment at the start of cc-engine.el for more info." |
| 1384 | |
| 1385 | (let ((start (point))) |
| 1386 | (while (and |
| 1387 | ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4) |
| 1388 | ;; return t when moving backwards at bob. |
| 1389 | (not (bobp)) |
| 1390 | |
| 1391 | (if (let (open-paren-in-column-0-is-defun-start) |
| 1392 | (forward-comment -1)) |
| 1393 | (if (looking-at "\\*/") |
| 1394 | ;; Emacs <= 20 and XEmacs move back over the |
| 1395 | ;; closer of a block comment that lacks an opener. |
| 1396 | (progn (forward-char 2) nil) |
| 1397 | t) |
| 1398 | |
| 1399 | ;; XEmacs treats line continuations as whitespace but |
| 1400 | ;; only in the backward direction, which seems a bit |
| 1401 | ;; odd. Anyway, this is necessary for Emacs. |
| 1402 | (when (and (looking-at "[\n\r]") |
| 1403 | (eq (char-before) ?\\) |
| 1404 | (< (point) start)) |
| 1405 | (backward-char) |
| 1406 | t)))))) |
| 1407 | |
| 1408 | \f |
| 1409 | ;; Tools for skipping over syntactic whitespace. |
| 1410 | |
| 1411 | ;; The following functions use text properties to cache searches over |
| 1412 | ;; large regions of syntactic whitespace. It works as follows: |
| 1413 | ;; |
| 1414 | ;; o If a syntactic whitespace region contains anything but simple |
| 1415 | ;; whitespace (i.e. space, tab and line breaks), the text property |
| 1416 | ;; `c-in-sws' is put over it. At places where we have stopped |
| 1417 | ;; within that region there's also a `c-is-sws' text property. |
| 1418 | ;; That since there typically are nested whitespace inside that |
| 1419 | ;; must be handled separately, e.g. whitespace inside a comment or |
| 1420 | ;; cpp directive. Thus, from one point with `c-is-sws' it's safe |
| 1421 | ;; to jump to another point with that property within the same |
| 1422 | ;; `c-in-sws' region. It can be likened to a ladder where |
| 1423 | ;; `c-in-sws' marks the bars and `c-is-sws' the rungs. |
| 1424 | ;; |
| 1425 | ;; o The `c-is-sws' property is put on the simple whitespace chars at |
| 1426 | ;; a "rung position" and also maybe on the first following char. |
| 1427 | ;; As many characters as can be conveniently found in this range |
| 1428 | ;; are marked, but no assumption can be made that the whole range |
| 1429 | ;; is marked (it could be clobbered by later changes, for |
| 1430 | ;; instance). |
| 1431 | ;; |
| 1432 | ;; Note that some part of the beginning of a sequence of simple |
| 1433 | ;; whitespace might be part of the end of a preceding line comment |
| 1434 | ;; or cpp directive and must not be considered part of the "rung". |
| 1435 | ;; Such whitespace is some amount of horizontal whitespace followed |
| 1436 | ;; by a newline. In the case of cpp directives it could also be |
| 1437 | ;; two newlines with horizontal whitespace between them. |
| 1438 | ;; |
| 1439 | ;; The reason to include the first following char is to cope with |
| 1440 | ;; "rung positions" that doesn't have any ordinary whitespace. If |
| 1441 | ;; `c-is-sws' is put on a token character it does not have |
| 1442 | ;; `c-in-sws' set simultaneously. That's the only case when that |
| 1443 | ;; can occur, and the reason for not extending the `c-in-sws' |
| 1444 | ;; region to cover it is that the `c-in-sws' region could then be |
| 1445 | ;; accidentally merged with a following one if the token is only |
| 1446 | ;; one character long. |
| 1447 | ;; |
| 1448 | ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are |
| 1449 | ;; removed in the changed region. If the change was inside |
| 1450 | ;; syntactic whitespace that means that the "ladder" is broken, but |
| 1451 | ;; a later call to `c-forward-sws' or `c-backward-sws' will use the |
| 1452 | ;; parts on either side and use an ordinary search only to "repair" |
| 1453 | ;; the gap. |
| 1454 | ;; |
| 1455 | ;; Special care needs to be taken if a region is removed: If there |
| 1456 | ;; are `c-in-sws' on both sides of it which do not connect inside |
| 1457 | ;; the region then they can't be joined. If e.g. a marked macro is |
| 1458 | ;; broken, syntactic whitespace inside the new text might be |
| 1459 | ;; marked. If those marks would become connected with the old |
| 1460 | ;; `c-in-sws' range around the macro then we could get a ladder |
| 1461 | ;; with one end outside the macro and the other at some whitespace |
| 1462 | ;; within it. |
| 1463 | ;; |
| 1464 | ;; The main motivation for this system is to increase the speed in |
| 1465 | ;; skipping over the large whitespace regions that can occur at the |
| 1466 | ;; top level in e.g. header files that contain a lot of comments and |
| 1467 | ;; cpp directives. For small comments inside code it's probably |
| 1468 | ;; slower than using `forward-comment' straightforwardly, but speed is |
| 1469 | ;; not a significant factor there anyway. |
| 1470 | |
| 1471 | ; (defface c-debug-is-sws-face |
| 1472 | ; '((t (:background "GreenYellow"))) |
| 1473 | ; "Debug face to mark the `c-is-sws' property.") |
| 1474 | ; (defface c-debug-in-sws-face |
| 1475 | ; '((t (:underline t))) |
| 1476 | ; "Debug face to mark the `c-in-sws' property.") |
| 1477 | |
| 1478 | ; (defun c-debug-put-sws-faces () |
| 1479 | ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws' |
| 1480 | ; ;; properties in the buffer. |
| 1481 | ; (interactive) |
| 1482 | ; (save-excursion |
| 1483 | ; (c-save-buffer-state (in-face) |
| 1484 | ; (goto-char (point-min)) |
| 1485 | ; (setq in-face (if (get-text-property (point) 'c-is-sws) |
| 1486 | ; (point))) |
| 1487 | ; (while (progn |
| 1488 | ; (goto-char (next-single-property-change |
| 1489 | ; (point) 'c-is-sws nil (point-max))) |
| 1490 | ; (if in-face |
| 1491 | ; (progn |
| 1492 | ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face) |
| 1493 | ; (setq in-face nil)) |
| 1494 | ; (setq in-face (point))) |
| 1495 | ; (not (eobp)))) |
| 1496 | ; (goto-char (point-min)) |
| 1497 | ; (setq in-face (if (get-text-property (point) 'c-in-sws) |
| 1498 | ; (point))) |
| 1499 | ; (while (progn |
| 1500 | ; (goto-char (next-single-property-change |
| 1501 | ; (point) 'c-in-sws nil (point-max))) |
| 1502 | ; (if in-face |
| 1503 | ; (progn |
| 1504 | ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face) |
| 1505 | ; (setq in-face nil)) |
| 1506 | ; (setq in-face (point))) |
| 1507 | ; (not (eobp))))))) |
| 1508 | |
| 1509 | (defmacro c-debug-sws-msg (&rest args) |
| 1510 | ;;`(message ,@args) |
| 1511 | ) |
| 1512 | |
| 1513 | (defmacro c-put-is-sws (beg end) |
| 1514 | ;; This macro does a hidden buffer change. |
| 1515 | `(let ((beg ,beg) (end ,end)) |
| 1516 | (put-text-property beg end 'c-is-sws t) |
| 1517 | ,@(when (facep 'c-debug-is-sws-face) |
| 1518 | `((c-debug-add-face beg end 'c-debug-is-sws-face))))) |
| 1519 | |
| 1520 | (defmacro c-put-in-sws (beg end) |
| 1521 | ;; This macro does a hidden buffer change. |
| 1522 | `(let ((beg ,beg) (end ,end)) |
| 1523 | (put-text-property beg end 'c-in-sws t) |
| 1524 | ,@(when (facep 'c-debug-is-sws-face) |
| 1525 | `((c-debug-add-face beg end 'c-debug-in-sws-face))))) |
| 1526 | |
| 1527 | (defmacro c-remove-is-sws (beg end) |
| 1528 | ;; This macro does a hidden buffer change. |
| 1529 | `(let ((beg ,beg) (end ,end)) |
| 1530 | (remove-text-properties beg end '(c-is-sws nil)) |
| 1531 | ,@(when (facep 'c-debug-is-sws-face) |
| 1532 | `((c-debug-remove-face beg end 'c-debug-is-sws-face))))) |
| 1533 | |
| 1534 | (defmacro c-remove-in-sws (beg end) |
| 1535 | ;; This macro does a hidden buffer change. |
| 1536 | `(let ((beg ,beg) (end ,end)) |
| 1537 | (remove-text-properties beg end '(c-in-sws nil)) |
| 1538 | ,@(when (facep 'c-debug-is-sws-face) |
| 1539 | `((c-debug-remove-face beg end 'c-debug-in-sws-face))))) |
| 1540 | |
| 1541 | (defmacro c-remove-is-and-in-sws (beg end) |
| 1542 | ;; This macro does a hidden buffer change. |
| 1543 | `(let ((beg ,beg) (end ,end)) |
| 1544 | (remove-text-properties beg end '(c-is-sws nil c-in-sws nil)) |
| 1545 | ,@(when (facep 'c-debug-is-sws-face) |
| 1546 | `((c-debug-remove-face beg end 'c-debug-is-sws-face) |
| 1547 | (c-debug-remove-face beg end 'c-debug-in-sws-face))))) |
| 1548 | |
| 1549 | (defsubst c-invalidate-sws-region-after (beg end) |
| 1550 | ;; Called from `after-change-functions'. Note that if |
| 1551 | ;; `c-forward-sws' or `c-backward-sws' are used outside |
| 1552 | ;; `c-save-buffer-state' or similar then this will remove the cache |
| 1553 | ;; properties right after they're added. |
| 1554 | ;; |
| 1555 | ;; This function does hidden buffer changes. |
| 1556 | |
| 1557 | (save-excursion |
| 1558 | ;; Adjust the end to remove the properties in any following simple |
| 1559 | ;; ws up to and including the next line break, if there is any |
| 1560 | ;; after the changed region. This is necessary e.g. when a rung |
| 1561 | ;; marked empty line is converted to a line comment by inserting |
| 1562 | ;; "//" before the line break. In that case the line break would |
| 1563 | ;; keep the rung mark which could make a later `c-backward-sws' |
| 1564 | ;; move into the line comment instead of over it. |
| 1565 | (goto-char end) |
| 1566 | (skip-chars-forward " \t\f\v") |
| 1567 | (when (and (eolp) (not (eobp))) |
| 1568 | (setq end (1+ (point))))) |
| 1569 | |
| 1570 | (when (and (= beg end) |
| 1571 | (get-text-property beg 'c-in-sws) |
| 1572 | (> beg (point-min)) |
| 1573 | (get-text-property (1- beg) 'c-in-sws)) |
| 1574 | ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't |
| 1575 | ;; safe to keep a range that was continuous before the change. E.g: |
| 1576 | ;; |
| 1577 | ;; #define foo |
| 1578 | ;; \ |
| 1579 | ;; bar |
| 1580 | ;; |
| 1581 | ;; There can be a "ladder" between "#" and "b". Now, if the newline |
| 1582 | ;; after "foo" is removed then "bar" will become part of the cpp |
| 1583 | ;; directive instead of a syntactically relevant token. In that |
| 1584 | ;; case there's no longer syntactic ws from "#" to "b". |
| 1585 | (setq beg (1- beg))) |
| 1586 | |
| 1587 | (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end) |
| 1588 | (c-remove-is-and-in-sws beg end)) |
| 1589 | |
| 1590 | (defun c-forward-sws () |
| 1591 | ;; Used by `c-forward-syntactic-ws' to implement the unbounded search. |
| 1592 | ;; |
| 1593 | ;; This function might do hidden buffer changes. |
| 1594 | |
| 1595 | (let (;; `rung-pos' is set to a position as early as possible in the |
| 1596 | ;; unmarked part of the simple ws region. |
| 1597 | (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos |
| 1598 | rung-is-marked next-rung-is-marked simple-ws-end |
| 1599 | ;; `safe-start' is set when it's safe to cache the start position. |
| 1600 | ;; It's not set if we've initially skipped over comments and line |
| 1601 | ;; continuations since we might have gone out through the end of a |
| 1602 | ;; macro then. This provision makes `c-forward-sws' not populate the |
| 1603 | ;; cache in the majority of cases, but otoh is `c-backward-sws' by far |
| 1604 | ;; more common. |
| 1605 | safe-start) |
| 1606 | |
| 1607 | ;; Skip simple ws and do a quick check on the following character to see |
| 1608 | ;; if it's anything that can't start syntactic ws, so we can bail out |
| 1609 | ;; early in the majority of cases when there just are a few ws chars. |
| 1610 | (skip-chars-forward " \t\n\r\f\v") |
| 1611 | (when (looking-at c-syntactic-ws-start) |
| 1612 | |
| 1613 | (setq rung-end-pos (min (1+ (point)) (point-max))) |
| 1614 | (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos |
| 1615 | 'c-is-sws t)) |
| 1616 | ;; Find the last rung position to avoid setting properties in all |
| 1617 | ;; the cases when the marked rung is complete. |
| 1618 | ;; (`next-single-property-change' is certain to move at least one |
| 1619 | ;; step forward.) |
| 1620 | (setq rung-pos (1- (next-single-property-change |
| 1621 | rung-is-marked 'c-is-sws nil rung-end-pos))) |
| 1622 | ;; Got no marked rung here. Since the simple ws might have started |
| 1623 | ;; inside a line comment or cpp directive we must set `rung-pos' as |
| 1624 | ;; high as possible. |
| 1625 | (setq rung-pos (point))) |
| 1626 | |
| 1627 | (while |
| 1628 | (progn |
| 1629 | (while |
| 1630 | (when (and rung-is-marked |
| 1631 | (get-text-property (point) 'c-in-sws)) |
| 1632 | |
| 1633 | ;; The following search is the main reason that `c-in-sws' |
| 1634 | ;; and `c-is-sws' aren't combined to one property. |
| 1635 | (goto-char (next-single-property-change |
| 1636 | (point) 'c-in-sws nil (point-max))) |
| 1637 | (unless (get-text-property (point) 'c-is-sws) |
| 1638 | ;; If the `c-in-sws' region extended past the last |
| 1639 | ;; `c-is-sws' char we have to go back a bit. |
| 1640 | (or (get-text-property (1- (point)) 'c-is-sws) |
| 1641 | (goto-char (previous-single-property-change |
| 1642 | (point) 'c-is-sws))) |
| 1643 | (backward-char)) |
| 1644 | |
| 1645 | (c-debug-sws-msg |
| 1646 | "c-forward-sws cached move %s -> %s (max %s)" |
| 1647 | rung-pos (point) (point-max)) |
| 1648 | |
| 1649 | (setq rung-pos (point)) |
| 1650 | (and (> (skip-chars-forward " \t\n\r\f\v") 0) |
| 1651 | (not (eobp)))) |
| 1652 | |
| 1653 | ;; We'll loop here if there is simple ws after the last rung. |
| 1654 | ;; That means that there's been some change in it and it's |
| 1655 | ;; possible that we've stepped into another ladder, so extend |
| 1656 | ;; the previous one to join with it if there is one, and try to |
| 1657 | ;; use the cache again. |
| 1658 | (c-debug-sws-msg |
| 1659 | "c-forward-sws extending rung with [%s..%s] (max %s)" |
| 1660 | (1+ rung-pos) (1+ (point)) (point-max)) |
| 1661 | (unless (get-text-property (point) 'c-is-sws) |
| 1662 | ;; Remove any `c-in-sws' property from the last char of |
| 1663 | ;; the rung before we mark it with `c-is-sws', so that we |
| 1664 | ;; won't connect with the remains of a broken "ladder". |
| 1665 | (c-remove-in-sws (point) (1+ (point)))) |
| 1666 | (c-put-is-sws (1+ rung-pos) |
| 1667 | (1+ (point))) |
| 1668 | (c-put-in-sws rung-pos |
| 1669 | (setq rung-pos (point) |
| 1670 | last-put-in-sws-pos rung-pos))) |
| 1671 | |
| 1672 | (setq simple-ws-end (point)) |
| 1673 | (c-forward-comments) |
| 1674 | |
| 1675 | (cond |
| 1676 | ((/= (point) simple-ws-end) |
| 1677 | ;; Skipped over comments. Don't cache at eob in case the buffer |
| 1678 | ;; is narrowed. |
| 1679 | (not (eobp))) |
| 1680 | |
| 1681 | ((save-excursion |
| 1682 | (and c-opt-cpp-prefix |
| 1683 | (looking-at c-opt-cpp-start) |
| 1684 | (progn (skip-chars-backward " \t") |
| 1685 | (bolp)) |
| 1686 | (or (bobp) |
| 1687 | (progn (backward-char) |
| 1688 | (not (eq (char-before) ?\\)))))) |
| 1689 | ;; Skip a preprocessor directive. |
| 1690 | (end-of-line) |
| 1691 | (while (and (eq (char-before) ?\\) |
| 1692 | (= (forward-line 1) 0)) |
| 1693 | (end-of-line)) |
| 1694 | (forward-line 1) |
| 1695 | (setq safe-start t) |
| 1696 | ;; Don't cache at eob in case the buffer is narrowed. |
| 1697 | (not (eobp))))) |
| 1698 | |
| 1699 | ;; We've searched over a piece of non-white syntactic ws. See if this |
| 1700 | ;; can be cached. |
| 1701 | (setq next-rung-pos (point)) |
| 1702 | (skip-chars-forward " \t\n\r\f\v") |
| 1703 | (setq rung-end-pos (min (1+ (point)) (point-max))) |
| 1704 | |
| 1705 | (if (or |
| 1706 | ;; Cache if we haven't skipped comments only, and if we started |
| 1707 | ;; either from a marked rung or from a completely uncached |
| 1708 | ;; position. |
| 1709 | (and safe-start |
| 1710 | (or rung-is-marked |
| 1711 | (not (get-text-property simple-ws-end 'c-in-sws)))) |
| 1712 | |
| 1713 | ;; See if there's a marked rung in the encountered simple ws. If |
| 1714 | ;; so then we can cache, unless `safe-start' is nil. Even then |
| 1715 | ;; we need to do this to check if the cache can be used for the |
| 1716 | ;; next step. |
| 1717 | (and (setq next-rung-is-marked |
| 1718 | (text-property-any next-rung-pos rung-end-pos |
| 1719 | 'c-is-sws t)) |
| 1720 | safe-start)) |
| 1721 | |
| 1722 | (progn |
| 1723 | (c-debug-sws-msg |
| 1724 | "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)" |
| 1725 | rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos |
| 1726 | (point-max)) |
| 1727 | |
| 1728 | ;; Remove the properties for any nested ws that might be cached. |
| 1729 | ;; Only necessary for `c-is-sws' since `c-in-sws' will be set |
| 1730 | ;; anyway. |
| 1731 | (c-remove-is-sws (1+ simple-ws-end) next-rung-pos) |
| 1732 | (unless (and rung-is-marked (= rung-pos simple-ws-end)) |
| 1733 | (c-put-is-sws rung-pos |
| 1734 | (1+ simple-ws-end)) |
| 1735 | (setq rung-is-marked t)) |
| 1736 | (c-put-in-sws rung-pos |
| 1737 | (setq rung-pos (point) |
| 1738 | last-put-in-sws-pos rung-pos)) |
| 1739 | (unless (get-text-property (1- rung-end-pos) 'c-is-sws) |
| 1740 | ;; Remove any `c-in-sws' property from the last char of |
| 1741 | ;; the rung before we mark it with `c-is-sws', so that we |
| 1742 | ;; won't connect with the remains of a broken "ladder". |
| 1743 | (c-remove-in-sws (1- rung-end-pos) rung-end-pos)) |
| 1744 | (c-put-is-sws next-rung-pos |
| 1745 | rung-end-pos)) |
| 1746 | |
| 1747 | (c-debug-sws-msg |
| 1748 | "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)" |
| 1749 | rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos |
| 1750 | (point-max)) |
| 1751 | |
| 1752 | ;; Set `rung-pos' for the next rung. It's the same thing here as |
| 1753 | ;; initially, except that the rung position is set as early as |
| 1754 | ;; possible since we can't be in the ending ws of a line comment or |
| 1755 | ;; cpp directive now. |
| 1756 | (if (setq rung-is-marked next-rung-is-marked) |
| 1757 | (setq rung-pos (1- (next-single-property-change |
| 1758 | rung-is-marked 'c-is-sws nil rung-end-pos))) |
| 1759 | (setq rung-pos next-rung-pos)) |
| 1760 | (setq safe-start t))) |
| 1761 | |
| 1762 | ;; Make sure that the newly marked `c-in-sws' region doesn't connect to |
| 1763 | ;; another one after the point (which might occur when editing inside a |
| 1764 | ;; comment or macro). |
| 1765 | (when (eq last-put-in-sws-pos (point)) |
| 1766 | (cond ((< last-put-in-sws-pos (point-max)) |
| 1767 | (c-debug-sws-msg |
| 1768 | "c-forward-sws clearing at %s for cache separation" |
| 1769 | last-put-in-sws-pos) |
| 1770 | (c-remove-in-sws last-put-in-sws-pos |
| 1771 | (1+ last-put-in-sws-pos))) |
| 1772 | (t |
| 1773 | ;; If at eob we have to clear the last character before the end |
| 1774 | ;; instead since the buffer might be narrowed and there might |
| 1775 | ;; be a `c-in-sws' after (point-max). In this case it's |
| 1776 | ;; necessary to clear both properties. |
| 1777 | (c-debug-sws-msg |
| 1778 | "c-forward-sws clearing thoroughly at %s for cache separation" |
| 1779 | (1- last-put-in-sws-pos)) |
| 1780 | (c-remove-is-and-in-sws (1- last-put-in-sws-pos) |
| 1781 | last-put-in-sws-pos)))) |
| 1782 | ))) |
| 1783 | |
| 1784 | (defun c-backward-sws () |
| 1785 | ;; Used by `c-backward-syntactic-ws' to implement the unbounded search. |
| 1786 | ;; |
| 1787 | ;; This function might do hidden buffer changes. |
| 1788 | |
| 1789 | (let (;; `rung-pos' is set to a position as late as possible in the unmarked |
| 1790 | ;; part of the simple ws region. |
| 1791 | (rung-pos (point)) next-rung-pos last-put-in-sws-pos |
| 1792 | rung-is-marked simple-ws-beg cmt-skip-pos) |
| 1793 | |
| 1794 | ;; Skip simple horizontal ws and do a quick check on the preceding |
| 1795 | ;; character to see if it's anything that can't end syntactic ws, so we can |
| 1796 | ;; bail out early in the majority of cases when there just are a few ws |
| 1797 | ;; chars. Newlines are complicated in the backward direction, so we can't |
| 1798 | ;; skip over them. |
| 1799 | (skip-chars-backward " \t\f") |
| 1800 | (when (and (not (bobp)) |
| 1801 | (save-excursion |
| 1802 | (backward-char) |
| 1803 | (looking-at c-syntactic-ws-end))) |
| 1804 | |
| 1805 | ;; Try to find a rung position in the simple ws preceding point, so that |
| 1806 | ;; we can get a cache hit even if the last bit of the simple ws has |
| 1807 | ;; changed recently. |
| 1808 | (setq simple-ws-beg (point)) |
| 1809 | (skip-chars-backward " \t\n\r\f\v") |
| 1810 | (if (setq rung-is-marked (text-property-any |
| 1811 | (point) (min (1+ rung-pos) (point-max)) |
| 1812 | 'c-is-sws t)) |
| 1813 | ;; `rung-pos' will be the earliest marked position, which means that |
| 1814 | ;; there might be later unmarked parts in the simple ws region. |
| 1815 | ;; It's not worth the effort to fix that; the last part of the |
| 1816 | ;; simple ws is also typically edited often, so it could be wasted. |
| 1817 | (goto-char (setq rung-pos rung-is-marked)) |
| 1818 | (goto-char simple-ws-beg)) |
| 1819 | |
| 1820 | (while |
| 1821 | (progn |
| 1822 | (while |
| 1823 | (when (and rung-is-marked |
| 1824 | (not (bobp)) |
| 1825 | (get-text-property (1- (point)) 'c-in-sws)) |
| 1826 | |
| 1827 | ;; The following search is the main reason that `c-in-sws' |
| 1828 | ;; and `c-is-sws' aren't combined to one property. |
| 1829 | (goto-char (previous-single-property-change |
| 1830 | (point) 'c-in-sws nil (point-min))) |
| 1831 | (unless (get-text-property (point) 'c-is-sws) |
| 1832 | ;; If the `c-in-sws' region extended past the first |
| 1833 | ;; `c-is-sws' char we have to go forward a bit. |
| 1834 | (goto-char (next-single-property-change |
| 1835 | (point) 'c-is-sws))) |
| 1836 | |
| 1837 | (c-debug-sws-msg |
| 1838 | "c-backward-sws cached move %s <- %s (min %s)" |
| 1839 | (point) rung-pos (point-min)) |
| 1840 | |
| 1841 | (setq rung-pos (point)) |
| 1842 | (if (and (< (min (skip-chars-backward " \t\f\v") |
| 1843 | (progn |
| 1844 | (setq simple-ws-beg (point)) |
| 1845 | (skip-chars-backward " \t\n\r\f\v"))) |
| 1846 | 0) |
| 1847 | (setq rung-is-marked |
| 1848 | (text-property-any (point) rung-pos |
| 1849 | 'c-is-sws t))) |
| 1850 | t |
| 1851 | (goto-char simple-ws-beg) |
| 1852 | nil)) |
| 1853 | |
| 1854 | ;; We'll loop here if there is simple ws before the first rung. |
| 1855 | ;; That means that there's been some change in it and it's |
| 1856 | ;; possible that we've stepped into another ladder, so extend |
| 1857 | ;; the previous one to join with it if there is one, and try to |
| 1858 | ;; use the cache again. |
| 1859 | (c-debug-sws-msg |
| 1860 | "c-backward-sws extending rung with [%s..%s] (min %s)" |
| 1861 | rung-is-marked rung-pos (point-min)) |
| 1862 | (unless (get-text-property (1- rung-pos) 'c-is-sws) |
| 1863 | ;; Remove any `c-in-sws' property from the last char of |
| 1864 | ;; the rung before we mark it with `c-is-sws', so that we |
| 1865 | ;; won't connect with the remains of a broken "ladder". |
| 1866 | (c-remove-in-sws (1- rung-pos) rung-pos)) |
| 1867 | (c-put-is-sws rung-is-marked |
| 1868 | rung-pos) |
| 1869 | (c-put-in-sws rung-is-marked |
| 1870 | (1- rung-pos)) |
| 1871 | (setq rung-pos rung-is-marked |
| 1872 | last-put-in-sws-pos rung-pos)) |
| 1873 | |
| 1874 | (c-backward-comments) |
| 1875 | (setq cmt-skip-pos (point)) |
| 1876 | |
| 1877 | (cond |
| 1878 | ((and c-opt-cpp-prefix |
| 1879 | (/= cmt-skip-pos simple-ws-beg) |
| 1880 | (c-beginning-of-macro)) |
| 1881 | ;; Inside a cpp directive. See if it should be skipped over. |
| 1882 | (let ((cpp-beg (point))) |
| 1883 | |
| 1884 | ;; Move back over all line continuations in the region skipped |
| 1885 | ;; over by `c-backward-comments'. If we go past it then we |
| 1886 | ;; started inside the cpp directive. |
| 1887 | (goto-char simple-ws-beg) |
| 1888 | (beginning-of-line) |
| 1889 | (while (and (> (point) cmt-skip-pos) |
| 1890 | (progn (backward-char) |
| 1891 | (eq (char-before) ?\\))) |
| 1892 | (beginning-of-line)) |
| 1893 | |
| 1894 | (if (< (point) cmt-skip-pos) |
| 1895 | ;; Don't move past the cpp directive if we began inside |
| 1896 | ;; it. Note that the position at the end of the last line |
| 1897 | ;; of the macro is also considered to be within it. |
| 1898 | (progn (goto-char cmt-skip-pos) |
| 1899 | nil) |
| 1900 | |
| 1901 | ;; It's worthwhile to spend a little bit of effort on finding |
| 1902 | ;; the end of the macro, to get a good `simple-ws-beg' |
| 1903 | ;; position for the cache. Note that `c-backward-comments' |
| 1904 | ;; could have stepped over some comments before going into |
| 1905 | ;; the macro, and then `simple-ws-beg' must be kept on the |
| 1906 | ;; same side of those comments. |
| 1907 | (goto-char simple-ws-beg) |
| 1908 | (skip-chars-backward " \t\n\r\f\v") |
| 1909 | (if (eq (char-before) ?\\) |
| 1910 | (forward-char)) |
| 1911 | (forward-line 1) |
| 1912 | (if (< (point) simple-ws-beg) |
| 1913 | ;; Might happen if comments after the macro were skipped |
| 1914 | ;; over. |
| 1915 | (setq simple-ws-beg (point))) |
| 1916 | |
| 1917 | (goto-char cpp-beg) |
| 1918 | t))) |
| 1919 | |
| 1920 | ((/= (save-excursion |
| 1921 | (skip-chars-forward " \t\n\r\f\v" simple-ws-beg) |
| 1922 | (setq next-rung-pos (point))) |
| 1923 | simple-ws-beg) |
| 1924 | ;; Skipped over comments. Must put point at the end of |
| 1925 | ;; the simple ws at point since we might be after a line |
| 1926 | ;; comment or cpp directive that's been partially |
| 1927 | ;; narrowed out, and we can't risk marking the simple ws |
| 1928 | ;; at the end of it. |
| 1929 | (goto-char next-rung-pos) |
| 1930 | t))) |
| 1931 | |
| 1932 | ;; We've searched over a piece of non-white syntactic ws. See if this |
| 1933 | ;; can be cached. |
| 1934 | (setq next-rung-pos (point)) |
| 1935 | (skip-chars-backward " \t\f\v") |
| 1936 | |
| 1937 | (if (or |
| 1938 | ;; Cache if we started either from a marked rung or from a |
| 1939 | ;; completely uncached position. |
| 1940 | rung-is-marked |
| 1941 | (not (get-text-property (1- simple-ws-beg) 'c-in-sws)) |
| 1942 | |
| 1943 | ;; Cache if there's a marked rung in the encountered simple ws. |
| 1944 | (save-excursion |
| 1945 | (skip-chars-backward " \t\n\r\f\v") |
| 1946 | (text-property-any (point) (min (1+ next-rung-pos) (point-max)) |
| 1947 | 'c-is-sws t))) |
| 1948 | |
| 1949 | (progn |
| 1950 | (c-debug-sws-msg |
| 1951 | "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)" |
| 1952 | (point) (1+ next-rung-pos) |
| 1953 | simple-ws-beg (min (1+ rung-pos) (point-max)) |
| 1954 | (point-min)) |
| 1955 | |
| 1956 | ;; Remove the properties for any nested ws that might be cached. |
| 1957 | ;; Only necessary for `c-is-sws' since `c-in-sws' will be set |
| 1958 | ;; anyway. |
| 1959 | (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg) |
| 1960 | (unless (and rung-is-marked (= simple-ws-beg rung-pos)) |
| 1961 | (let ((rung-end-pos (min (1+ rung-pos) (point-max)))) |
| 1962 | (unless (get-text-property (1- rung-end-pos) 'c-is-sws) |
| 1963 | ;; Remove any `c-in-sws' property from the last char of |
| 1964 | ;; the rung before we mark it with `c-is-sws', so that we |
| 1965 | ;; won't connect with the remains of a broken "ladder". |
| 1966 | (c-remove-in-sws (1- rung-end-pos) rung-end-pos)) |
| 1967 | (c-put-is-sws simple-ws-beg |
| 1968 | rung-end-pos) |
| 1969 | (setq rung-is-marked t))) |
| 1970 | (c-put-in-sws (setq simple-ws-beg (point) |
| 1971 | last-put-in-sws-pos simple-ws-beg) |
| 1972 | rung-pos) |
| 1973 | (c-put-is-sws (setq rung-pos simple-ws-beg) |
| 1974 | (1+ next-rung-pos))) |
| 1975 | |
| 1976 | (c-debug-sws-msg |
| 1977 | "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)" |
| 1978 | (point) (1+ next-rung-pos) |
| 1979 | simple-ws-beg (min (1+ rung-pos) (point-max)) |
| 1980 | (point-min)) |
| 1981 | (setq rung-pos next-rung-pos |
| 1982 | simple-ws-beg (point)) |
| 1983 | )) |
| 1984 | |
| 1985 | ;; Make sure that the newly marked `c-in-sws' region doesn't connect to |
| 1986 | ;; another one before the point (which might occur when editing inside a |
| 1987 | ;; comment or macro). |
| 1988 | (when (eq last-put-in-sws-pos (point)) |
| 1989 | (cond ((< (point-min) last-put-in-sws-pos) |
| 1990 | (c-debug-sws-msg |
| 1991 | "c-backward-sws clearing at %s for cache separation" |
| 1992 | (1- last-put-in-sws-pos)) |
| 1993 | (c-remove-in-sws (1- last-put-in-sws-pos) |
| 1994 | last-put-in-sws-pos)) |
| 1995 | ((> (point-min) 1) |
| 1996 | ;; If at bob and the buffer is narrowed, we have to clear the |
| 1997 | ;; character we're standing on instead since there might be a |
| 1998 | ;; `c-in-sws' before (point-min). In this case it's necessary |
| 1999 | ;; to clear both properties. |
| 2000 | (c-debug-sws-msg |
| 2001 | "c-backward-sws clearing thoroughly at %s for cache separation" |
| 2002 | last-put-in-sws-pos) |
| 2003 | (c-remove-is-and-in-sws last-put-in-sws-pos |
| 2004 | (1+ last-put-in-sws-pos))))) |
| 2005 | ))) |
| 2006 | |
| 2007 | \f |
| 2008 | ;; Other whitespace tools |
| 2009 | (defun c-partial-ws-p (beg end) |
| 2010 | ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the |
| 2011 | ;; region? This is a "heuristic" function. ..... |
| 2012 | ;; |
| 2013 | ;; The motivation for the second bit is to check whether removing this |
| 2014 | ;; region would coalesce two symbols. |
| 2015 | ;; |
| 2016 | ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be |
| 2017 | ;; careful about using this function for, e.g. AWK. (2007/3/7) |
| 2018 | (save-excursion |
| 2019 | (let ((end+1 (min (1+ end) (point-max)))) |
| 2020 | (or (progn (goto-char (max (point-min) (1- beg))) |
| 2021 | (c-skip-ws-forward end) |
| 2022 | (eq (point) end)) |
| 2023 | (progn (goto-char beg) |
| 2024 | (c-skip-ws-forward end+1) |
| 2025 | (eq (point) end+1)))))) |
| 2026 | \f |
| 2027 | ;; A system for finding noteworthy parens before the point. |
| 2028 | |
| 2029 | (defconst c-state-cache-too-far 5000) |
| 2030 | ;; A maximum comfortable scanning distance, e.g. between |
| 2031 | ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When |
| 2032 | ;; this distance is exceeded, we take "emergency measures", e.g. by clearing |
| 2033 | ;; the cache and starting again from point-min or a beginning of defun. This |
| 2034 | ;; value can be tuned for efficiency or set to a lower value for testing. |
| 2035 | |
| 2036 | (defvar c-state-cache nil) |
| 2037 | (make-variable-buffer-local 'c-state-cache) |
| 2038 | ;; The state cache used by `c-parse-state' to cut down the amount of |
| 2039 | ;; searching. It's the result from some earlier `c-parse-state' call. See |
| 2040 | ;; `c-parse-state''s doc string for details of its structure. |
| 2041 | ;; |
| 2042 | ;; The use of the cached info is more effective if the next |
| 2043 | ;; `c-parse-state' call is on a line close by the one the cached state |
| 2044 | ;; was made at; the cache can actually slow down a little if the |
| 2045 | ;; cached state was made very far back in the buffer. The cache is |
| 2046 | ;; most effective if `c-parse-state' is used on each line while moving |
| 2047 | ;; forward. |
| 2048 | |
| 2049 | (defvar c-state-cache-good-pos 1) |
| 2050 | (make-variable-buffer-local 'c-state-cache-good-pos) |
| 2051 | ;; This is a position where `c-state-cache' is known to be correct, or |
| 2052 | ;; nil (see below). It's a position inside one of the recorded unclosed |
| 2053 | ;; parens or the top level, but not further nested inside any literal or |
| 2054 | ;; subparen that is closed before the last recorded position. |
| 2055 | ;; |
| 2056 | ;; The exact position is chosen to try to be close to yet earlier than |
| 2057 | ;; the position where `c-state-cache' will be called next. Right now |
| 2058 | ;; the heuristic is to set it to the position after the last found |
| 2059 | ;; closing paren (of any type) before the line on which |
| 2060 | ;; `c-parse-state' was called. That is chosen primarily to work well |
| 2061 | ;; with refontification of the current line. |
| 2062 | ;; |
| 2063 | ;; 2009-07-28: When `c-state-point-min' and the last position where |
| 2064 | ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are |
| 2065 | ;; both in the same literal, there is no such "good position", and |
| 2066 | ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which |
| 2067 | ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil. |
| 2068 | ;; |
| 2069 | ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in |
| 2070 | ;; the middle of the desert, as long as it is not within a brace pair |
| 2071 | ;; recorded in `c-state-cache' or a paren/bracket pair. |
| 2072 | |
| 2073 | |
| 2074 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2075 | ;; We maintain a simple cache of positions which aren't in a literal, so as to |
| 2076 | ;; speed up testing for non-literality. |
| 2077 | (defconst c-state-nonlit-pos-interval 10000) |
| 2078 | ;; The approximate interval between entries in `c-state-nonlit-pos-cache'. |
| 2079 | |
| 2080 | (defvar c-state-nonlit-pos-cache nil) |
| 2081 | (make-variable-buffer-local 'c-state-nonlit-pos-cache) |
| 2082 | ;; A list of buffer positions which are known not to be in a literal or a cpp |
| 2083 | ;; construct. This is ordered with higher positions at the front of the list. |
| 2084 | ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid. |
| 2085 | |
| 2086 | (defvar c-state-nonlit-pos-cache-limit 1) |
| 2087 | (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit) |
| 2088 | ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is |
| 2089 | ;; reduced by buffer changes, and increased by invocations of |
| 2090 | ;; `c-state-literal-at'. |
| 2091 | |
| 2092 | (defsubst c-state-pp-to-literal (from to) |
| 2093 | ;; Do a parse-partial-sexp from FROM to TO, returning either |
| 2094 | ;; (STATE TYPE (BEG . END)) if TO is in a literal; or |
| 2095 | ;; (STATE) otherwise, |
| 2096 | ;; where STATE is the parsing state at TO, TYPE is the type of the literal |
| 2097 | ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal. |
| 2098 | ;; |
| 2099 | ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote), |
| 2100 | ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of |
| 2101 | ;; STATE are valid. |
| 2102 | (save-excursion |
| 2103 | (let ((s (parse-partial-sexp from to)) |
| 2104 | ty) |
| 2105 | (when (or (nth 3 s) (nth 4 s)) ; in a string or comment |
| 2106 | (setq ty (cond |
| 2107 | ((nth 3 s) 'string) |
| 2108 | ((eq (nth 7 s) t) 'c++) |
| 2109 | (t 'c))) |
| 2110 | (parse-partial-sexp (point) (point-max) |
| 2111 | nil ; TARGETDEPTH |
| 2112 | nil ; STOPBEFORE |
| 2113 | s ; OLDSTATE |
| 2114 | 'syntax-table)) ; stop at end of literal |
| 2115 | (if ty |
| 2116 | `(,s ,ty (,(nth 8 s) . ,(point))) |
| 2117 | `(,s))))) |
| 2118 | |
| 2119 | (defun c-state-safe-place (here) |
| 2120 | ;; Return a buffer position before HERE which is "safe", i.e. outside any |
| 2121 | ;; string, comment, or macro. |
| 2122 | ;; |
| 2123 | ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache |
| 2124 | ;; MAY NOT contain any positions within macros, since macros are frequently |
| 2125 | ;; turned into comments by use of the `c-cpp-delimiter' category properties. |
| 2126 | ;; We cannot rely on this mechanism whilst determining a cache pos since |
| 2127 | ;; this function is also called from outwith `c-parse-state'. |
| 2128 | (save-restriction |
| 2129 | (widen) |
| 2130 | (save-excursion |
| 2131 | (let ((c c-state-nonlit-pos-cache) |
| 2132 | pos npos lit) |
| 2133 | ;; Trim the cache to take account of buffer changes. |
| 2134 | (while (and c (> (car c) c-state-nonlit-pos-cache-limit)) |
| 2135 | (setq c (cdr c))) |
| 2136 | (setq c-state-nonlit-pos-cache c) |
| 2137 | |
| 2138 | (while (and c (> (car c) here)) |
| 2139 | (setq c (cdr c))) |
| 2140 | (setq pos (or (car c) (point-min))) |
| 2141 | |
| 2142 | (while (<= (setq npos (+ pos c-state-nonlit-pos-interval)) |
| 2143 | here) |
| 2144 | (setq lit (car (cddr (c-state-pp-to-literal pos npos)))) |
| 2145 | (setq pos (or (cdr lit) npos)) ; end of literal containing npos. |
| 2146 | (goto-char pos) |
| 2147 | (when (and (c-beginning-of-macro) (/= (point) pos)) |
| 2148 | (c-syntactic-end-of-macro) |
| 2149 | (or (eobp) (forward-char)) |
| 2150 | (setq pos (point))) |
| 2151 | (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache))) |
| 2152 | |
| 2153 | (if (> pos c-state-nonlit-pos-cache-limit) |
| 2154 | (setq c-state-nonlit-pos-cache-limit pos)) |
| 2155 | pos)))) |
| 2156 | |
| 2157 | (defun c-state-literal-at (here) |
| 2158 | ;; If position HERE is inside a literal, return (START . END), the |
| 2159 | ;; boundaries of the literal (which may be outside the accessible bit of the |
| 2160 | ;; buffer). Otherwise, return nil. |
| 2161 | ;; |
| 2162 | ;; This function is almost the same as `c-literal-limits'. Previously, it |
| 2163 | ;; differed in that it was a lower level function, and that it rigorously |
| 2164 | ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12) |
| 2165 | ;; virtually identical to this function. |
| 2166 | (save-restriction |
| 2167 | (widen) |
| 2168 | (save-excursion |
| 2169 | (let ((pos (c-state-safe-place here))) |
| 2170 | (car (cddr (c-state-pp-to-literal pos here))))))) |
| 2171 | |
| 2172 | (defsubst c-state-lit-beg (pos) |
| 2173 | ;; Return the start of the literal containing POS, or POS itself. |
| 2174 | (or (car (c-state-literal-at pos)) |
| 2175 | pos)) |
| 2176 | |
| 2177 | (defsubst c-state-cache-non-literal-place (pos state) |
| 2178 | ;; Return a position outside of a string/comment/macro at or before POS. |
| 2179 | ;; STATE is the parse-partial-sexp state at POS. |
| 2180 | (let ((res (if (or (nth 3 state) ; in a string? |
| 2181 | (nth 4 state)) ; in a comment? |
| 2182 | (nth 8 state) |
| 2183 | pos))) |
| 2184 | (save-excursion |
| 2185 | (goto-char res) |
| 2186 | (if (c-beginning-of-macro) |
| 2187 | (point) |
| 2188 | res)))) |
| 2189 | |
| 2190 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2191 | ;; Stuff to do with point-min, and coping with any literal there. |
| 2192 | (defvar c-state-point-min 1) |
| 2193 | (make-variable-buffer-local 'c-state-point-min) |
| 2194 | ;; This is (point-min) when `c-state-cache' was last calculated. A change of |
| 2195 | ;; narrowing is likely to affect the parens that are visible before the point. |
| 2196 | |
| 2197 | (defvar c-state-point-min-lit-type nil) |
| 2198 | (make-variable-buffer-local 'c-state-point-min-lit-type) |
| 2199 | (defvar c-state-point-min-lit-start nil) |
| 2200 | (make-variable-buffer-local 'c-state-point-min-lit-start) |
| 2201 | ;; These two variables define the literal, if any, containing point-min. |
| 2202 | ;; Their values are, respectively, 'string, c, or c++, and the start of the |
| 2203 | ;; literal. If there's no literal there, they're both nil. |
| 2204 | |
| 2205 | (defvar c-state-min-scan-pos 1) |
| 2206 | (make-variable-buffer-local 'c-state-min-scan-pos) |
| 2207 | ;; This is the earliest buffer-pos from which scanning can be done. It is |
| 2208 | ;; either the end of the literal containing point-min, or point-min itself. |
| 2209 | ;; It becomes nil if the buffer is changed earlier than this point. |
| 2210 | (defun c-state-get-min-scan-pos () |
| 2211 | ;; Return the lowest valid scanning pos. This will be the end of the |
| 2212 | ;; literal enclosing point-min, or point-min itself. |
| 2213 | (or c-state-min-scan-pos |
| 2214 | (save-restriction |
| 2215 | (save-excursion |
| 2216 | (widen) |
| 2217 | (goto-char c-state-point-min-lit-start) |
| 2218 | (if (eq c-state-point-min-lit-type 'string) |
| 2219 | (forward-sexp) |
| 2220 | (forward-comment 1)) |
| 2221 | (setq c-state-min-scan-pos (point)))))) |
| 2222 | |
| 2223 | (defun c-state-mark-point-min-literal () |
| 2224 | ;; Determine the properties of any literal containing POINT-MIN, setting the |
| 2225 | ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start', |
| 2226 | ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless. |
| 2227 | (let ((p-min (point-min)) |
| 2228 | lit) |
| 2229 | (save-restriction |
| 2230 | (widen) |
| 2231 | (setq lit (c-state-literal-at p-min)) |
| 2232 | (if lit |
| 2233 | (setq c-state-point-min-lit-type |
| 2234 | (save-excursion |
| 2235 | (goto-char (car lit)) |
| 2236 | (cond |
| 2237 | ((looking-at c-block-comment-start-regexp) 'c) |
| 2238 | ((looking-at c-line-comment-starter) 'c++) |
| 2239 | (t 'string))) |
| 2240 | c-state-point-min-lit-start (car lit) |
| 2241 | c-state-min-scan-pos (cdr lit)) |
| 2242 | (setq c-state-point-min-lit-type nil |
| 2243 | c-state-point-min-lit-start nil |
| 2244 | c-state-min-scan-pos p-min))))) |
| 2245 | |
| 2246 | |
| 2247 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2248 | ;; A variable which signals a brace dessert - helpful for reducing the number |
| 2249 | ;; of fruitless backward scans. |
| 2250 | (defvar c-state-brace-pair-desert nil) |
| 2251 | (make-variable-buffer-local 'c-state-brace-pair-desert) |
| 2252 | ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when |
| 2253 | ;; that defun has searched backwards for a brace pair and not found one. Its |
| 2254 | ;; value is either nil or a cons (PA . FROM), where PA is the position of the |
| 2255 | ;; enclosing opening paren/brace/bracket which bounds the backwards search (or |
| 2256 | ;; nil when at top level) and FROM is where the backward search started. It |
| 2257 | ;; is reset to nil in `c-invalidate-state-cache'. |
| 2258 | |
| 2259 | |
| 2260 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2261 | ;; Lowish level functions/macros which work directly on `c-state-cache', or a |
| 2262 | ;; list of like structure. |
| 2263 | (defmacro c-state-cache-top-lparen (&optional cache) |
| 2264 | ;; Return the address of the top left brace/bracket/paren recorded in CACHE |
| 2265 | ;; (default `c-state-cache') (or nil). |
| 2266 | (let ((cash (or cache 'c-state-cache))) |
| 2267 | `(if (consp (car ,cash)) |
| 2268 | (caar ,cash) |
| 2269 | (car ,cash)))) |
| 2270 | |
| 2271 | (defmacro c-state-cache-top-paren (&optional cache) |
| 2272 | ;; Return the address of the latest brace/bracket/paren (whether left or |
| 2273 | ;; right) recorded in CACHE (default `c-state-cache') or nil. |
| 2274 | (let ((cash (or cache 'c-state-cache))) |
| 2275 | `(if (consp (car ,cash)) |
| 2276 | (cdar ,cash) |
| 2277 | (car ,cash)))) |
| 2278 | |
| 2279 | (defmacro c-state-cache-after-top-paren (&optional cache) |
| 2280 | ;; Return the position just after the latest brace/bracket/paren (whether |
| 2281 | ;; left or right) recorded in CACHE (default `c-state-cache') or nil. |
| 2282 | (let ((cash (or cache 'c-state-cache))) |
| 2283 | `(if (consp (car ,cash)) |
| 2284 | (cdar ,cash) |
| 2285 | (and (car ,cash) |
| 2286 | (1+ (car ,cash)))))) |
| 2287 | |
| 2288 | (defun c-get-cache-scan-pos (here) |
| 2289 | ;; From the state-cache, determine the buffer position from which we might |
| 2290 | ;; scan forward to HERE to update this cache. This position will be just |
| 2291 | ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise |
| 2292 | ;; return the earliest position in the accessible region which isn't within |
| 2293 | ;; a literal. If the visible portion of the buffer is entirely within a |
| 2294 | ;; literal, return NIL. |
| 2295 | (let ((c c-state-cache) elt) |
| 2296 | ;(while (>= (or (c-state-cache-top-lparen c) 1) here) |
| 2297 | (while (and c |
| 2298 | (>= (c-state-cache-top-lparen c) here)) |
| 2299 | (setq c (cdr c))) |
| 2300 | |
| 2301 | (setq elt (car c)) |
| 2302 | (cond |
| 2303 | ((consp elt) |
| 2304 | (if (> (cdr elt) here) |
| 2305 | (1+ (car elt)) |
| 2306 | (cdr elt))) |
| 2307 | (elt (1+ elt)) |
| 2308 | ((<= (c-state-get-min-scan-pos) here) |
| 2309 | (c-state-get-min-scan-pos)) |
| 2310 | (t nil)))) |
| 2311 | |
| 2312 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2313 | ;; Variables which keep track of preprocessor constructs. |
| 2314 | (defvar c-state-old-cpp-beg nil) |
| 2315 | (make-variable-buffer-local 'c-state-old-cpp-beg) |
| 2316 | (defvar c-state-old-cpp-end nil) |
| 2317 | (make-variable-buffer-local 'c-state-old-cpp-end) |
| 2318 | ;; These are the limits of the macro containing point at the previous call of |
| 2319 | ;; `c-parse-state', or nil. |
| 2320 | |
| 2321 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2322 | ;; Defuns which analyze the buffer, yet don't change `c-state-cache'. |
| 2323 | (defun c-get-fallback-scan-pos (here) |
| 2324 | ;; Return a start position for building `c-state-cache' from |
| 2325 | ;; scratch. This will be at the top level, 2 defuns back. |
| 2326 | (save-excursion |
| 2327 | ;; Go back 2 bods, but ignore any bogus positions returned by |
| 2328 | ;; beginning-of-defun (i.e. open paren in column zero). |
| 2329 | (goto-char here) |
| 2330 | (let ((cnt 2)) |
| 2331 | (while (not (or (bobp) (zerop cnt))) |
| 2332 | (c-beginning-of-defun-1) ; Pure elisp BOD. |
| 2333 | (if (eq (char-after) ?\{) |
| 2334 | (setq cnt (1- cnt))))) |
| 2335 | (point))) |
| 2336 | |
| 2337 | (defun c-state-balance-parens-backwards (here- here+ top) |
| 2338 | ;; Return the position of the opening paren/brace/bracket before HERE- which |
| 2339 | ;; matches the outermost close p/b/b between HERE+ and TOP. Except when |
| 2340 | ;; there's a macro, HERE- and HERE+ are the same. Like this: |
| 2341 | ;; |
| 2342 | ;; ............................................ |
| 2343 | ;; | | |
| 2344 | ;; ( [ ( .........#macro.. ) ( ) ] ) |
| 2345 | ;; ^ ^ ^ ^ |
| 2346 | ;; | | | | |
| 2347 | ;; return HERE- HERE+ TOP |
| 2348 | ;; |
| 2349 | ;; If there aren't enough opening paren/brace/brackets, return the position |
| 2350 | ;; of the outermost one found, or HERE- if there are none. If there are no |
| 2351 | ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP |
| 2352 | ;; must not be inside literals. Only the accessible portion of the buffer |
| 2353 | ;; will be scanned. |
| 2354 | |
| 2355 | ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose |
| 2356 | ;; `here'. Go round the next loop each time we pass over such a ")". These |
| 2357 | ;; probably match "("s before `here-'. |
| 2358 | (let (pos pa ren+1 lonely-rens) |
| 2359 | (save-excursion |
| 2360 | (save-restriction |
| 2361 | (narrow-to-region (point-min) top) ; This can move point, sometimes. |
| 2362 | (setq pos here+) |
| 2363 | (c-safe |
| 2364 | (while |
| 2365 | (setq ren+1 (scan-lists pos 1 1)) ; might signal |
| 2366 | (setq lonely-rens (cons ren+1 lonely-rens) |
| 2367 | pos ren+1))))) |
| 2368 | |
| 2369 | ;; PART 2: Scan back before `here-' searching for the "("s |
| 2370 | ;; matching/mismatching the ")"s found above. We only need to direct the |
| 2371 | ;; caller to scan when we've encountered unmatched right parens. |
| 2372 | (setq pos here-) |
| 2373 | (when lonely-rens |
| 2374 | (c-safe |
| 2375 | (while |
| 2376 | (and lonely-rens ; actual values aren't used. |
| 2377 | (setq pa (scan-lists pos -1 1))) |
| 2378 | (setq pos pa) |
| 2379 | (setq lonely-rens (cdr lonely-rens))))) |
| 2380 | pos)) |
| 2381 | |
| 2382 | (defun c-parse-state-get-strategy (here good-pos) |
| 2383 | ;; Determine the scanning strategy for adjusting `c-parse-state', attempting |
| 2384 | ;; to minimize the amount of scanning. HERE is the pertinent position in |
| 2385 | ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with |
| 2386 | ;; its head trimmed) is known to be good, or nil if there is no such |
| 2387 | ;; position. |
| 2388 | ;; |
| 2389 | ;; The return value is a list, one of the following: |
| 2390 | ;; |
| 2391 | ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT, |
| 2392 | ;; which is not less than CACHE-POS. |
| 2393 | ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE). |
| 2394 | ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the |
| 2395 | ;; top level. |
| 2396 | ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min. |
| 2397 | ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at |
| 2398 | ;; or below HERE. |
| 2399 | (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1) |
| 2400 | BOD-pos ; position of 2nd BOD before HERE. |
| 2401 | strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT. |
| 2402 | start-point |
| 2403 | how-far) ; putative scanning distance. |
| 2404 | (setq good-pos (or good-pos (c-state-get-min-scan-pos))) |
| 2405 | (cond |
| 2406 | ((< here (c-state-get-min-scan-pos)) |
| 2407 | (setq strategy 'IN-LIT |
| 2408 | start-point nil |
| 2409 | cache-pos nil |
| 2410 | how-far 0)) |
| 2411 | ((<= good-pos here) |
| 2412 | (setq strategy 'forward |
| 2413 | start-point (max good-pos cache-pos) |
| 2414 | how-far (- here start-point))) |
| 2415 | ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting. |
| 2416 | (setq strategy 'backward |
| 2417 | how-far (- good-pos here))) |
| 2418 | (t |
| 2419 | (setq strategy 'forward |
| 2420 | how-far (- here cache-pos) |
| 2421 | start-point cache-pos))) |
| 2422 | |
| 2423 | ;; Might we be better off starting from the top level, two defuns back, |
| 2424 | ;; instead? |
| 2425 | (when (> how-far c-state-cache-too-far) |
| 2426 | (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!! |
| 2427 | (if (< (- here BOD-pos) how-far) |
| 2428 | (setq strategy 'BOD |
| 2429 | start-point BOD-pos))) |
| 2430 | |
| 2431 | (list |
| 2432 | strategy |
| 2433 | (and (memq strategy '(forward backward)) cache-pos) |
| 2434 | (and (memq strategy '(forward BOD)) start-point)))) |
| 2435 | |
| 2436 | |
| 2437 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2438 | ;; Routines which change `c-state-cache' and associated values. |
| 2439 | (defun c-renarrow-state-cache () |
| 2440 | ;; The region (more precisely, point-min) has changed since we |
| 2441 | ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly. |
| 2442 | (if (< (point-min) c-state-point-min) |
| 2443 | ;; If point-min has MOVED BACKWARDS then we drop the state completely. |
| 2444 | ;; It would be possible to do a better job here and recalculate the top |
| 2445 | ;; only. |
| 2446 | (progn |
| 2447 | (c-state-mark-point-min-literal) |
| 2448 | (setq c-state-cache nil |
| 2449 | c-state-cache-good-pos c-state-min-scan-pos |
| 2450 | c-state-brace-pair-desert nil)) |
| 2451 | |
| 2452 | ;; point-min has MOVED FORWARD. |
| 2453 | |
| 2454 | ;; Is the new point-min inside a (different) literal? |
| 2455 | (unless (and c-state-point-min-lit-start ; at prev. point-min |
| 2456 | (< (point-min) (c-state-get-min-scan-pos))) |
| 2457 | (c-state-mark-point-min-literal)) |
| 2458 | |
| 2459 | ;; Cut off a bit of the tail from `c-state-cache'. |
| 2460 | (let ((ptr (cons nil c-state-cache)) |
| 2461 | pa) |
| 2462 | (while (and (setq pa (c-state-cache-top-lparen (cdr ptr))) |
| 2463 | (>= pa (point-min))) |
| 2464 | (setq ptr (cdr ptr))) |
| 2465 | |
| 2466 | (when (consp ptr) |
| 2467 | (if (eq (cdr ptr) c-state-cache) |
| 2468 | (setq c-state-cache nil |
| 2469 | c-state-cache-good-pos c-state-min-scan-pos) |
| 2470 | (setcdr ptr nil) |
| 2471 | (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen)))) |
| 2472 | ))) |
| 2473 | |
| 2474 | (setq c-state-point-min (point-min))) |
| 2475 | |
| 2476 | (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim) |
| 2477 | ;; If there is a brace pair preceding FROM in the buffer (not necessarily |
| 2478 | ;; immediately preceding), push a cons onto `c-state-cache' to represent it. |
| 2479 | ;; FROM must not be inside a literal. If UPPER-LIM is non-nil, we append |
| 2480 | ;; the highest brace pair whose "}" is below UPPER-LIM. |
| 2481 | ;; |
| 2482 | ;; Return non-nil when this has been done. |
| 2483 | ;; |
| 2484 | ;; This routine should be fast. Since it can get called a LOT, we maintain |
| 2485 | ;; `c-state-brace-pair-desert', a small cache of "failures", such that we |
| 2486 | ;; reduce the time wasted in repeated fruitless searches in brace deserts. |
| 2487 | (save-excursion |
| 2488 | (save-restriction |
| 2489 | (let ((bra from) ce ; Positions of "{" and "}". |
| 2490 | new-cons |
| 2491 | (cache-pos (c-state-cache-top-lparen)) ; might be nil. |
| 2492 | (macro-start-or-from |
| 2493 | (progn (goto-char from) |
| 2494 | (c-beginning-of-macro) |
| 2495 | (point)))) |
| 2496 | (or upper-lim (setq upper-lim from)) |
| 2497 | |
| 2498 | ;; If we're essentially repeating a fruitless search, just give up. |
| 2499 | (unless (and c-state-brace-pair-desert |
| 2500 | (eq cache-pos (car c-state-brace-pair-desert)) |
| 2501 | (<= from (cdr c-state-brace-pair-desert))) |
| 2502 | ;; Only search what we absolutely need to: |
| 2503 | (if (and c-state-brace-pair-desert |
| 2504 | (eq cache-pos (car c-state-brace-pair-desert))) |
| 2505 | (narrow-to-region (cdr c-state-brace-pair-desert) (point-max))) |
| 2506 | |
| 2507 | ;; In the next pair of nested loops, the inner one moves back past a |
| 2508 | ;; pair of (mis-)matching parens or brackets; the outer one moves |
| 2509 | ;; back over a sequence of unmatched close brace/paren/bracket each |
| 2510 | ;; time round. |
| 2511 | (while |
| 2512 | (progn |
| 2513 | (c-safe |
| 2514 | (while |
| 2515 | (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal |
| 2516 | (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal |
| 2517 | (or (> ce upper-lim) |
| 2518 | (not (eq (char-after bra) ?\{)) |
| 2519 | (and (goto-char bra) |
| 2520 | (c-beginning-of-macro) |
| 2521 | (< (point) macro-start-or-from)))))) |
| 2522 | (and ce (< ce bra))) |
| 2523 | (setq bra ce)) ; If we just backed over an unbalanced closing |
| 2524 | ; brace, ignore it. |
| 2525 | |
| 2526 | (if (and ce (< bra ce) (eq (char-after bra) ?\{)) |
| 2527 | ;; We've found the desired brace-pair. |
| 2528 | (progn |
| 2529 | (setq new-cons (cons bra (1+ ce))) |
| 2530 | (cond |
| 2531 | ((consp (car c-state-cache)) |
| 2532 | (setcar c-state-cache new-cons)) |
| 2533 | ((and (numberp (car c-state-cache)) ; probably never happens |
| 2534 | (< ce (car c-state-cache))) |
| 2535 | (setcdr c-state-cache |
| 2536 | (cons new-cons (cdr c-state-cache)))) |
| 2537 | (t (setq c-state-cache (cons new-cons c-state-cache))))) |
| 2538 | |
| 2539 | ;; We haven't found a brace pair. Record this. |
| 2540 | (setq c-state-brace-pair-desert (cons cache-pos from)))))))) |
| 2541 | |
| 2542 | (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here) |
| 2543 | ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position |
| 2544 | ;; following a {, and that brace has a (mis-)matching } (or ]), and we |
| 2545 | ;; "push" "a" brace pair onto `c-state-cache'. |
| 2546 | ;; |
| 2547 | ;; Here "push" means overwrite the top element if it's itself a brace-pair, |
| 2548 | ;; otherwise push it normally. |
| 2549 | ;; |
| 2550 | ;; The brace pair we push is normally the one surrounding BRA+1, but if the |
| 2551 | ;; latter is inside a macro, not being a macro containing |
| 2552 | ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro |
| 2553 | ;; base pair. This latter case is assumed to be rare. |
| 2554 | ;; |
| 2555 | ;; Note: POINT is not preserved in this routine. |
| 2556 | (if bra+1 |
| 2557 | (if (or (> bra+1 macro-start-or-here) |
| 2558 | (progn (goto-char bra+1) |
| 2559 | (not (c-beginning-of-macro)))) |
| 2560 | (setq c-state-cache |
| 2561 | (cons (cons (1- bra+1) |
| 2562 | (scan-lists bra+1 1 1)) |
| 2563 | (if (consp (car c-state-cache)) |
| 2564 | (cdr c-state-cache) |
| 2565 | c-state-cache))) |
| 2566 | ;; N.B. This defsubst codes one method for the simple, normal case, |
| 2567 | ;; and a more sophisticated, slower way for the general case. Don't |
| 2568 | ;; eliminate this defsubst - it's a speed optimization. |
| 2569 | (c-append-lower-brace-pair-to-state-cache (1- bra+1))))) |
| 2570 | |
| 2571 | (defun c-append-to-state-cache (from) |
| 2572 | ;; Scan the buffer from FROM to (point-max), adding elements into |
| 2573 | ;; `c-state-cache' for braces etc. Return a candidate for |
| 2574 | ;; `c-state-cache-good-pos'. |
| 2575 | ;; |
| 2576 | ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if |
| 2577 | ;; any. Typically, it is immediately after it. It must not be inside a |
| 2578 | ;; literal. |
| 2579 | (let ((here-bol (c-point 'bol (point-max))) |
| 2580 | (macro-start-or-here |
| 2581 | (save-excursion (goto-char (point-max)) |
| 2582 | (if (c-beginning-of-macro) |
| 2583 | (point) |
| 2584 | (point-max)))) |
| 2585 | pa+1 ; pos just after an opening PAren (or brace). |
| 2586 | (ren+1 from) ; usually a pos just after an closing paREN etc. |
| 2587 | ; Is actually the pos. to scan for a (/{/[ from, |
| 2588 | ; which sometimes is after a silly )/}/]. |
| 2589 | paren+1 ; Pos after some opening or closing paren. |
| 2590 | paren+1s ; A list of `paren+1's; used to determine a |
| 2591 | ; good-pos. |
| 2592 | bra+1 ce+1 ; just after L/R bra-ces. |
| 2593 | bra+1s ; list of OLD values of bra+1. |
| 2594 | mstart) ; start of a macro. |
| 2595 | |
| 2596 | (save-excursion |
| 2597 | ;; Each time round the following loop, we enter a successively deeper |
| 2598 | ;; level of brace/paren nesting. (Except sometimes we "continue at |
| 2599 | ;; the existing level".) `pa+1' is a pos inside an opening |
| 2600 | ;; brace/paren/bracket, usually just after it. |
| 2601 | (while |
| 2602 | (progn |
| 2603 | ;; Each time round the next loop moves forward over an opening then |
| 2604 | ;; a closing brace/bracket/paren. This loop is white hot, so it |
| 2605 | ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS |
| 2606 | ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a |
| 2607 | ;; call of `scan-lists' signals an error, which happens when there |
| 2608 | ;; are no more b/b/p's to scan. |
| 2609 | (c-safe |
| 2610 | (while t |
| 2611 | (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal |
| 2612 | paren+1s (cons pa+1 paren+1s)) |
| 2613 | (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal |
| 2614 | (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later. |
| 2615 | (setq bra+1 pa+1)) |
| 2616 | (setcar paren+1s ren+1))) |
| 2617 | |
| 2618 | (if (and pa+1 (> pa+1 ren+1)) |
| 2619 | ;; We've just entered a deeper nesting level. |
| 2620 | (progn |
| 2621 | ;; Insert the brace pair (if present) and the single open |
| 2622 | ;; paren/brace/bracket into `c-state-cache' It cannot be |
| 2623 | ;; inside a macro, except one around point, because of what |
| 2624 | ;; `c-neutralize-syntax-in-CPP' has done. |
| 2625 | (c-state-push-any-brace-pair bra+1 macro-start-or-here) |
| 2626 | ;; Insert the opening brace/bracket/paren position. |
| 2627 | (setq c-state-cache (cons (1- pa+1) c-state-cache)) |
| 2628 | ;; Clear admin stuff for the next more nested part of the scan. |
| 2629 | (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil) |
| 2630 | t) ; Carry on the loop |
| 2631 | |
| 2632 | ;; All open p/b/b's at this nesting level, if any, have probably |
| 2633 | ;; been closed by matching/mismatching ones. We're probably |
| 2634 | ;; finished - we just need to check for having found an |
| 2635 | ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a |
| 2636 | ;; macro, due the action of `c-neutralize-syntax-in-CPP'. |
| 2637 | (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control. |
| 2638 | |
| 2639 | ;; Record the final, innermost, brace-pair if there is one. |
| 2640 | (c-state-push-any-brace-pair bra+1 macro-start-or-here) |
| 2641 | |
| 2642 | ;; Determine a good pos |
| 2643 | (while (and (setq paren+1 (car paren+1s)) |
| 2644 | (> (if (> paren+1 macro-start-or-here) |
| 2645 | paren+1 |
| 2646 | (goto-char paren+1) |
| 2647 | (setq mstart (and (c-beginning-of-macro) |
| 2648 | (point))) |
| 2649 | (or mstart paren+1)) |
| 2650 | here-bol)) |
| 2651 | (setq paren+1s (cdr paren+1s))) |
| 2652 | (cond |
| 2653 | ((and paren+1 mstart) |
| 2654 | (min paren+1 mstart)) |
| 2655 | (paren+1) |
| 2656 | (t from))))) |
| 2657 | |
| 2658 | (defun c-remove-stale-state-cache (good-pos pps-point) |
| 2659 | ;; Remove stale entries from the `c-cache-state', i.e. those which will |
| 2660 | ;; not be in it when it is amended for position (point-max). |
| 2661 | ;; Additionally, the "outermost" open-brace entry before (point-max) |
| 2662 | ;; will be converted to a cons if the matching close-brace is scanned. |
| 2663 | ;; |
| 2664 | ;; GOOD-POS is a "maximal" "safe position" - there must be no open |
| 2665 | ;; parens/braces/brackets between GOOD-POS and (point-max). |
| 2666 | ;; |
| 2667 | ;; As a second thing, calculate the result of parse-partial-sexp at |
| 2668 | ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that |
| 2669 | ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to |
| 2670 | ;; adjust it to get outside a string/comment. (Sorry about this! The code |
| 2671 | ;; needs to be FAST). |
| 2672 | ;; |
| 2673 | ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where |
| 2674 | ;; o - GOOD-POS is a position where the new value `c-state-cache' is known |
| 2675 | ;; to be good (we aim for this to be as high as possible); |
| 2676 | ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair |
| 2677 | ;; preceding POS which needs to be recorded in `c-state-cache'. It is a |
| 2678 | ;; position to scan backwards from. |
| 2679 | ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT. |
| 2680 | (save-restriction |
| 2681 | (narrow-to-region 1 (point-max)) |
| 2682 | (save-excursion |
| 2683 | (let* ((in-macro-start ; start of macro containing (point-max) or nil. |
| 2684 | (save-excursion |
| 2685 | (goto-char (point-max)) |
| 2686 | (and (c-beginning-of-macro) |
| 2687 | (point)))) |
| 2688 | (good-pos-actual-macro-start ; Start of macro containing good-pos |
| 2689 | ; or nil |
| 2690 | (and (< good-pos (point-max)) |
| 2691 | (save-excursion |
| 2692 | (goto-char good-pos) |
| 2693 | (and (c-beginning-of-macro) |
| 2694 | (point))))) |
| 2695 | (good-pos-actual-macro-end ; End of this macro, (maybe |
| 2696 | ; (point-max)), or nil. |
| 2697 | (and good-pos-actual-macro-start |
| 2698 | (save-excursion |
| 2699 | (goto-char good-pos-actual-macro-start) |
| 2700 | (c-end-of-macro) |
| 2701 | (point)))) |
| 2702 | pps-state ; Will be 9 or 10 elements long. |
| 2703 | pos |
| 2704 | upper-lim ; ,beyond which `c-state-cache' entries are removed |
| 2705 | scan-back-pos |
| 2706 | pair-beg pps-point-state target-depth) |
| 2707 | |
| 2708 | ;; Remove entries beyond (point-max). Also remove any entries inside |
| 2709 | ;; a macro, unless (point-max) is in the same macro. |
| 2710 | (setq upper-lim |
| 2711 | (if (or (null c-state-old-cpp-beg) |
| 2712 | (and (> (point-max) c-state-old-cpp-beg) |
| 2713 | (< (point-max) c-state-old-cpp-end))) |
| 2714 | (point-max) |
| 2715 | (min (point-max) c-state-old-cpp-beg))) |
| 2716 | (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim)) |
| 2717 | (setq c-state-cache (cdr c-state-cache))) |
| 2718 | ;; If `upper-lim' is inside the last recorded brace pair, remove its |
| 2719 | ;; RBrace and indicate we'll need to search backwards for a previous |
| 2720 | ;; brace pair. |
| 2721 | (when (and c-state-cache |
| 2722 | (consp (car c-state-cache)) |
| 2723 | (> (cdar c-state-cache) upper-lim)) |
| 2724 | (setcar c-state-cache (caar c-state-cache)) |
| 2725 | (setq scan-back-pos (car c-state-cache))) |
| 2726 | |
| 2727 | ;; The next loop jumps forward out of a nested level of parens each |
| 2728 | ;; time round; the corresponding elements in `c-state-cache' are |
| 2729 | ;; removed. `pos' is just after the brace-pair or the open paren at |
| 2730 | ;; (car c-state-cache). There can be no open parens/braces/brackets |
| 2731 | ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max), |
| 2732 | ;; due to the interface spec to this function. |
| 2733 | (setq pos (if (and good-pos-actual-macro-end |
| 2734 | (not (eq good-pos-actual-macro-start |
| 2735 | in-macro-start))) |
| 2736 | (1+ good-pos-actual-macro-end) ; get outside the macro as |
| 2737 | ; marked by a `category' text property. |
| 2738 | good-pos)) |
| 2739 | (goto-char pos) |
| 2740 | (while (and c-state-cache |
| 2741 | (< (point) (point-max))) |
| 2742 | (cond |
| 2743 | ((null pps-state) ; first time through |
| 2744 | (setq target-depth -1)) |
| 2745 | ((eq (car pps-state) target-depth) ; found closing ),},] |
| 2746 | (setq target-depth (1- (car pps-state)))) |
| 2747 | ;; Do nothing when we've merely reached pps-point. |
| 2748 | ) |
| 2749 | |
| 2750 | ;; Scan! |
| 2751 | (setq pps-state |
| 2752 | (parse-partial-sexp |
| 2753 | (point) (if (< (point) pps-point) pps-point (point-max)) |
| 2754 | target-depth |
| 2755 | nil pps-state)) |
| 2756 | |
| 2757 | (if (= (point) pps-point) |
| 2758 | (setq pps-point-state pps-state)) |
| 2759 | |
| 2760 | (when (eq (car pps-state) target-depth) |
| 2761 | (setq pos (point)) ; POS is now just after an R-paren/brace. |
| 2762 | (cond |
| 2763 | ((and (consp (car c-state-cache)) |
| 2764 | (eq (point) (cdar c-state-cache))) |
| 2765 | ;; We've just moved out of the paren pair containing the brace-pair |
| 2766 | ;; at (car c-state-cache). `pair-beg' is where the open paren is, |
| 2767 | ;; and is potentially where the open brace of a cons in |
| 2768 | ;; c-state-cache will be. |
| 2769 | (setq pair-beg (car-safe (cdr c-state-cache)) |
| 2770 | c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen. |
| 2771 | ((numberp (car c-state-cache)) |
| 2772 | (setq pair-beg (car c-state-cache) |
| 2773 | c-state-cache (cdr c-state-cache))) ; remove this |
| 2774 | ; containing Lparen |
| 2775 | ((numberp (cadr c-state-cache)) |
| 2776 | (setq pair-beg (cadr c-state-cache) |
| 2777 | c-state-cache (cddr c-state-cache))) ; Remove a paren pair |
| 2778 | ; together with enclosed brace pair. |
| 2779 | ;; (t nil) ; Ignore an unmated Rparen. |
| 2780 | ))) |
| 2781 | |
| 2782 | (if (< (point) pps-point) |
| 2783 | (setq pps-state (parse-partial-sexp (point) pps-point |
| 2784 | nil nil ; TARGETDEPTH, STOPBEFORE |
| 2785 | pps-state))) |
| 2786 | |
| 2787 | ;; If the last paren pair we moved out of was actually a brace pair, |
| 2788 | ;; insert it into `c-state-cache'. |
| 2789 | (when (and pair-beg (eq (char-after pair-beg) ?{)) |
| 2790 | (if (consp (car-safe c-state-cache)) |
| 2791 | (setq c-state-cache (cdr c-state-cache))) |
| 2792 | (setq c-state-cache (cons (cons pair-beg pos) |
| 2793 | c-state-cache))) |
| 2794 | |
| 2795 | (list pos scan-back-pos pps-state))))) |
| 2796 | |
| 2797 | (defun c-remove-stale-state-cache-backwards (here cache-pos) |
| 2798 | ;; Strip stale elements of `c-state-cache' by moving backwards through the |
| 2799 | ;; buffer, and inform the caller of the scenario detected. |
| 2800 | ;; |
| 2801 | ;; HERE is the position we're setting `c-state-cache' for. |
| 2802 | ;; CACHE-POS is just after the latest recorded position in `c-state-cache' |
| 2803 | ;; before HERE, or a position at or near point-min which isn't in a |
| 2804 | ;; literal. |
| 2805 | ;; |
| 2806 | ;; This function must only be called only when (> `c-state-cache-good-pos' |
| 2807 | ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus |
| 2808 | ;; optimized to eliminate (or minimize) scanning between these two |
| 2809 | ;; positions. |
| 2810 | ;; |
| 2811 | ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where: |
| 2812 | ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or |
| 2813 | ;; could become so after missing elements are inserted into |
| 2814 | ;; `c-state-cache'. This is JUST AFTER an opening or closing |
| 2815 | ;; brace/paren/bracket which is already in `c-state-cache' or just before |
| 2816 | ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL |
| 2817 | ;; before `here''s line, or the start of the literal containing it. |
| 2818 | ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair |
| 2819 | ;; preceding POS which isn't recorded in `c-state-cache'. It is a position |
| 2820 | ;; to scan backwards from. |
| 2821 | ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between |
| 2822 | ;; POS and HERE which aren't recorded in `c-state-cache'. |
| 2823 | ;; |
| 2824 | ;; The comments in this defun use "paren" to mean parenthesis or square |
| 2825 | ;; bracket (as contrasted with a brace), and "(" and ")" likewise. |
| 2826 | ;; |
| 2827 | ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..) |
| 2828 | ;; | | | | | | |
| 2829 | ;; CP E here D C good |
| 2830 | (let ((pos c-state-cache-good-pos) |
| 2831 | pa ren ; positions of "(" and ")" |
| 2832 | dropped-cons ; whether the last element dropped from `c-state-cache' |
| 2833 | ; was a cons (representing a brace-pair) |
| 2834 | good-pos ; see above. |
| 2835 | lit ; (START . END) of a literal containing some point. |
| 2836 | here-lit-start here-lit-end ; bounds of literal containing `here' |
| 2837 | ; or `here' itself. |
| 2838 | here- here+ ; start/end of macro around HERE, or HERE |
| 2839 | (here-bol (c-point 'bol here)) |
| 2840 | (too-far-back (max (- here c-state-cache-too-far) (point-min)))) |
| 2841 | |
| 2842 | ;; Remove completely irrelevant entries from `c-state-cache'. |
| 2843 | (while (and c-state-cache |
| 2844 | (>= (setq pa (c-state-cache-top-lparen)) here)) |
| 2845 | (setq dropped-cons (consp (car c-state-cache))) |
| 2846 | (setq c-state-cache (cdr c-state-cache)) |
| 2847 | (setq pos pa)) |
| 2848 | ;; At this stage, (> pos here); |
| 2849 | ;; (< (c-state-cache-top-lparen) here) (or is nil). |
| 2850 | |
| 2851 | (cond |
| 2852 | ((and (consp (car c-state-cache)) |
| 2853 | (> (cdar c-state-cache) here)) |
| 2854 | ;; CASE 1: The top of the cache is a brace pair which now encloses |
| 2855 | ;; `here'. As good-pos, return the address. of the "{". Since we've no |
| 2856 | ;; knowledge of what's inside these braces, we have no alternative but |
| 2857 | ;; to direct the caller to scan the buffer from the opening brace. |
| 2858 | (setq pos (caar c-state-cache)) |
| 2859 | (setcar c-state-cache pos) |
| 2860 | (list (1+ pos) pos t)) ; return value. We've just converted a brace pair |
| 2861 | ; entry into a { entry, so the caller needs to |
| 2862 | ; search for a brace pair before the {. |
| 2863 | |
| 2864 | ;; `here' might be inside a literal. Check for this. |
| 2865 | ((progn |
| 2866 | (setq lit (c-state-literal-at here) |
| 2867 | here-lit-start (or (car lit) here) |
| 2868 | here-lit-end (or (cdr lit) here)) |
| 2869 | ;; Has `here' just "newly entered" a macro? |
| 2870 | (save-excursion |
| 2871 | (goto-char here-lit-start) |
| 2872 | (if (and (c-beginning-of-macro) |
| 2873 | (or (null c-state-old-cpp-beg) |
| 2874 | (not (= (point) c-state-old-cpp-beg)))) |
| 2875 | (progn |
| 2876 | (setq here- (point)) |
| 2877 | (c-end-of-macro) |
| 2878 | (setq here+ (point))) |
| 2879 | (setq here- here-lit-start |
| 2880 | here+ here-lit-end))) |
| 2881 | |
| 2882 | ;; `here' might be nested inside any depth of parens (or brackets but |
| 2883 | ;; not braces). Scan backwards to find the outermost such opening |
| 2884 | ;; paren, if there is one. This will be the scan position to return. |
| 2885 | (save-restriction |
| 2886 | (narrow-to-region cache-pos (point-max)) |
| 2887 | (setq pos (c-state-balance-parens-backwards here- here+ pos))) |
| 2888 | nil)) ; for the cond |
| 2889 | |
| 2890 | ((< pos here-lit-start) |
| 2891 | ;; CASE 2: Address of outermost ( or [ which now encloses `here', but |
| 2892 | ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is |
| 2893 | ;; a brace pair preceding this, it will already be in `c-state-cache', |
| 2894 | ;; unless there was a brace pair after it, i.e. there'll only be one to |
| 2895 | ;; scan for if we've just deleted one. |
| 2896 | (list pos (and dropped-cons pos) t)) ; Return value. |
| 2897 | |
| 2898 | ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren. |
| 2899 | ;; Further forward scanning isn't needed, but we still need to find a |
| 2900 | ;; GOOD-POS. Step out of all enclosing "("s on HERE's line. |
| 2901 | ((progn |
| 2902 | (save-restriction |
| 2903 | (narrow-to-region here-bol (point-max)) |
| 2904 | (setq pos here-lit-start) |
| 2905 | (c-safe (while (setq pa (scan-lists pos -1 1)) |
| 2906 | (setq pos pa)))) ; might signal |
| 2907 | nil)) ; for the cond |
| 2908 | |
| 2909 | ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back)) |
| 2910 | ;; CASE 3: After a }/)/] before `here''s BOL. |
| 2911 | (list (1+ ren) (and dropped-cons pos) nil)) ; Return value |
| 2912 | |
| 2913 | (t |
| 2914 | ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of |
| 2915 | ;; literal containing it. |
| 2916 | (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol))) |
| 2917 | (list good-pos (and dropped-cons good-pos) nil))))) |
| 2918 | |
| 2919 | |
| 2920 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2921 | ;; Externally visible routines. |
| 2922 | |
| 2923 | (defun c-state-cache-init () |
| 2924 | (setq c-state-cache nil |
| 2925 | c-state-cache-good-pos 1 |
| 2926 | c-state-nonlit-pos-cache nil |
| 2927 | c-state-nonlit-pos-cache-limit 1 |
| 2928 | c-state-brace-pair-desert nil |
| 2929 | c-state-point-min 1 |
| 2930 | c-state-point-min-lit-type nil |
| 2931 | c-state-point-min-lit-start nil |
| 2932 | c-state-min-scan-pos 1 |
| 2933 | c-state-old-cpp-beg nil |
| 2934 | c-state-old-cpp-end nil) |
| 2935 | (c-state-mark-point-min-literal)) |
| 2936 | |
| 2937 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2938 | ;; Debugging routines to dump `c-state-cache' in a "replayable" form. |
| 2939 | ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element" |
| 2940 | ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt)) |
| 2941 | ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element" |
| 2942 | ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt)) |
| 2943 | ;; (defun c-state-dump () |
| 2944 | ;; ;; For debugging. |
| 2945 | ;; ;(message |
| 2946 | ;; (concat |
| 2947 | ;; (c-sc-qde c-state-cache) |
| 2948 | ;; (c-sc-de c-state-cache-good-pos) |
| 2949 | ;; (c-sc-qde c-state-nonlit-pos-cache) |
| 2950 | ;; (c-sc-de c-state-nonlit-pos-cache-limit) |
| 2951 | ;; (c-sc-qde c-state-brace-pair-desert) |
| 2952 | ;; (c-sc-de c-state-point-min) |
| 2953 | ;; (c-sc-de c-state-point-min-lit-type) |
| 2954 | ;; (c-sc-de c-state-point-min-lit-start) |
| 2955 | ;; (c-sc-de c-state-min-scan-pos) |
| 2956 | ;; (c-sc-de c-state-old-cpp-beg) |
| 2957 | ;; (c-sc-de c-state-old-cpp-end))) |
| 2958 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2959 | |
| 2960 | (defun c-invalidate-state-cache-1 (here) |
| 2961 | ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE |
| 2962 | ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is |
| 2963 | ;; left in a consistent state. |
| 2964 | ;; |
| 2965 | ;; This is much like `c-whack-state-after', but it never changes a paren |
| 2966 | ;; pair element into an open paren element. Doing that would mean that the |
| 2967 | ;; new open paren wouldn't have the required preceding paren pair element. |
| 2968 | ;; |
| 2969 | ;; This function is called from c-after-change. |
| 2970 | |
| 2971 | ;; The cache of non-literals: |
| 2972 | (if (< here c-state-nonlit-pos-cache-limit) |
| 2973 | (setq c-state-nonlit-pos-cache-limit here)) |
| 2974 | |
| 2975 | ;; `c-state-cache': |
| 2976 | ;; Case 1: if `here' is in a literal containing point-min, everything |
| 2977 | ;; becomes (or is already) nil. |
| 2978 | (if (or (null c-state-cache-good-pos) |
| 2979 | (< here (c-state-get-min-scan-pos))) |
| 2980 | (setq c-state-cache nil |
| 2981 | c-state-cache-good-pos nil |
| 2982 | c-state-min-scan-pos nil) |
| 2983 | |
| 2984 | ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value |
| 2985 | ;; below `here'. To maintain its consistency, we may need to insert a new |
| 2986 | ;; brace pair. |
| 2987 | (let ((here-bol (c-point 'bol here)) |
| 2988 | too-high-pa ; recorded {/(/[ next above here, or nil. |
| 2989 | dropped-cons ; was the last removed element a brace pair? |
| 2990 | pa) |
| 2991 | ;; The easy bit - knock over-the-top bits off `c-state-cache'. |
| 2992 | (while (and c-state-cache |
| 2993 | (>= (setq pa (c-state-cache-top-paren)) here)) |
| 2994 | (setq dropped-cons (consp (car c-state-cache)) |
| 2995 | too-high-pa (c-state-cache-top-lparen) |
| 2996 | c-state-cache (cdr c-state-cache))) |
| 2997 | |
| 2998 | ;; Do we need to add in an earlier brace pair, having lopped one off? |
| 2999 | (if (and dropped-cons |
| 3000 | (< too-high-pa (+ here c-state-cache-too-far))) |
| 3001 | (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol)) |
| 3002 | (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren) |
| 3003 | (c-state-get-min-scan-pos))))) |
| 3004 | |
| 3005 | ;; The brace-pair desert marker: |
| 3006 | (when (car c-state-brace-pair-desert) |
| 3007 | (if (< here (car c-state-brace-pair-desert)) |
| 3008 | (setq c-state-brace-pair-desert nil) |
| 3009 | (if (< here (cdr c-state-brace-pair-desert)) |
| 3010 | (setcdr c-state-brace-pair-desert here))))) |
| 3011 | |
| 3012 | (defun c-parse-state-1 () |
| 3013 | ;; Find and record all noteworthy parens between some good point earlier in |
| 3014 | ;; the file and point. That good point is at least the beginning of the |
| 3015 | ;; top-level construct we are in, or the beginning of the preceding |
| 3016 | ;; top-level construct if we aren't in one. |
| 3017 | ;; |
| 3018 | ;; The returned value is a list of the noteworthy parens with the last one |
| 3019 | ;; first. If an element in the list is an integer, it's the position of an |
| 3020 | ;; open paren (of any type) which has not been closed before the point. If |
| 3021 | ;; an element is a cons, it gives the position of a closed BRACE paren |
| 3022 | ;; pair[*]; the car is the start brace position and the cdr is the position |
| 3023 | ;; following the closing brace. Only the last closed brace paren pair |
| 3024 | ;; before each open paren and before the point is recorded, and thus the |
| 3025 | ;; state never contains two cons elements in succession. When a close brace |
| 3026 | ;; has no matching open brace (e.g., the matching brace is outside the |
| 3027 | ;; visible region), it is not represented in the returned value. |
| 3028 | ;; |
| 3029 | ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren. |
| 3030 | ;; This defun explicitly treats mismatching parens/braces/brackets as |
| 3031 | ;; matching. It is the open brace which makes it a "brace" pair. |
| 3032 | ;; |
| 3033 | ;; If POINT is within a macro, open parens and brace pairs within |
| 3034 | ;; THIS macro MIGHT be recorded. This depends on whether their |
| 3035 | ;; syntactic properties have been suppressed by |
| 3036 | ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11). |
| 3037 | ;; |
| 3038 | ;; Currently no characters which are given paren syntax with the |
| 3039 | ;; syntax-table property are recorded, i.e. angle bracket arglist |
| 3040 | ;; parens are never present here. Note that this might change. |
| 3041 | ;; |
| 3042 | ;; BUG: This function doesn't cope entirely well with unbalanced |
| 3043 | ;; parens in macros. (2008-12-11: this has probably been resolved |
| 3044 | ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the |
| 3045 | ;; following case the brace before the macro isn't balanced with the |
| 3046 | ;; one after it: |
| 3047 | ;; |
| 3048 | ;; { |
| 3049 | ;; #define X { |
| 3050 | ;; } |
| 3051 | ;; |
| 3052 | ;; Note to maintainers: this function DOES get called with point |
| 3053 | ;; within comments and strings, so don't assume it doesn't! |
| 3054 | ;; |
| 3055 | ;; This function might do hidden buffer changes. |
| 3056 | (let* ((here (point)) |
| 3057 | (here-bopl (c-point 'bopl)) |
| 3058 | strategy ; 'forward, 'backward etc.. |
| 3059 | ;; Candidate positions to start scanning from: |
| 3060 | cache-pos ; highest position below HERE already existing in |
| 3061 | ; cache (or 1). |
| 3062 | good-pos |
| 3063 | start-point |
| 3064 | bopl-state |
| 3065 | res |
| 3066 | scan-backward-pos scan-forward-p) ; used for 'backward. |
| 3067 | ;; If POINT-MIN has changed, adjust the cache |
| 3068 | (unless (= (point-min) c-state-point-min) |
| 3069 | (c-renarrow-state-cache)) |
| 3070 | |
| 3071 | ;; Strategy? |
| 3072 | (setq res (c-parse-state-get-strategy here c-state-cache-good-pos) |
| 3073 | strategy (car res) |
| 3074 | cache-pos (cadr res) |
| 3075 | start-point (nth 2 res)) |
| 3076 | |
| 3077 | (when (eq strategy 'BOD) |
| 3078 | (setq c-state-cache nil |
| 3079 | c-state-cache-good-pos start-point)) |
| 3080 | |
| 3081 | ;; SCAN! |
| 3082 | (save-restriction |
| 3083 | (cond |
| 3084 | ((memq strategy '(forward BOD)) |
| 3085 | (narrow-to-region (point-min) here) |
| 3086 | (setq res (c-remove-stale-state-cache start-point here-bopl)) |
| 3087 | (setq cache-pos (car res) |
| 3088 | scan-backward-pos (cadr res) |
| 3089 | bopl-state (car (cddr res))) ; will be nil if (< here-bopl |
| 3090 | ; start-point) |
| 3091 | (if scan-backward-pos |
| 3092 | (c-append-lower-brace-pair-to-state-cache scan-backward-pos)) |
| 3093 | (setq good-pos |
| 3094 | (c-append-to-state-cache cache-pos)) |
| 3095 | (setq c-state-cache-good-pos |
| 3096 | (if (and bopl-state |
| 3097 | (< good-pos (- here c-state-cache-too-far))) |
| 3098 | (c-state-cache-non-literal-place here-bopl bopl-state) |
| 3099 | good-pos))) |
| 3100 | |
| 3101 | ((eq strategy 'backward) |
| 3102 | (setq res (c-remove-stale-state-cache-backwards here cache-pos) |
| 3103 | good-pos (car res) |
| 3104 | scan-backward-pos (cadr res) |
| 3105 | scan-forward-p (car (cddr res))) |
| 3106 | (if scan-backward-pos |
| 3107 | (c-append-lower-brace-pair-to-state-cache |
| 3108 | scan-backward-pos)) |
| 3109 | (setq c-state-cache-good-pos |
| 3110 | (if scan-forward-p |
| 3111 | (progn (narrow-to-region (point-min) here) |
| 3112 | (c-append-to-state-cache good-pos)) |
| 3113 | |
| 3114 | (c-get-cache-scan-pos good-pos)))) |
| 3115 | |
| 3116 | (t ; (eq strategy 'IN-LIT) |
| 3117 | (setq c-state-cache nil |
| 3118 | c-state-cache-good-pos nil))))) |
| 3119 | |
| 3120 | c-state-cache) |
| 3121 | |
| 3122 | (defun c-invalidate-state-cache (here) |
| 3123 | ;; This is a wrapper over `c-invalidate-state-cache-1'. |
| 3124 | ;; |
| 3125 | ;; It suppresses the syntactic effect of the < and > (template) brackets and |
| 3126 | ;; of all parens in preprocessor constructs, except for any such construct |
| 3127 | ;; containing point. We can then call `c-invalidate-state-cache-1' without |
| 3128 | ;; worrying further about macros and template delimiters. |
| 3129 | (c-with-<->-as-parens-suppressed |
| 3130 | (if (and c-state-old-cpp-beg |
| 3131 | (< c-state-old-cpp-beg here)) |
| 3132 | (c-with-all-but-one-cpps-commented-out |
| 3133 | c-state-old-cpp-beg |
| 3134 | (min c-state-old-cpp-end here) |
| 3135 | (c-invalidate-state-cache-1 here)) |
| 3136 | (c-with-cpps-commented-out |
| 3137 | (c-invalidate-state-cache-1 here))))) |
| 3138 | |
| 3139 | (defun c-parse-state () |
| 3140 | ;; This is a wrapper over `c-parse-state-1'. See that function for a |
| 3141 | ;; description of the functionality and return value. |
| 3142 | ;; |
| 3143 | ;; It suppresses the syntactic effect of the < and > (template) brackets and |
| 3144 | ;; of all parens in preprocessor constructs, except for any such construct |
| 3145 | ;; containing point. We can then call `c-parse-state-1' without worrying |
| 3146 | ;; further about macros and template delimiters. |
| 3147 | (let (here-cpp-beg here-cpp-end) |
| 3148 | (save-excursion |
| 3149 | (when (c-beginning-of-macro) |
| 3150 | (setq here-cpp-beg (point)) |
| 3151 | (unless |
| 3152 | (> (setq here-cpp-end (c-syntactic-end-of-macro)) |
| 3153 | here-cpp-beg) |
| 3154 | (setq here-cpp-beg nil here-cpp-end nil)))) |
| 3155 | ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the |
| 3156 | ;; subsystem. |
| 3157 | (prog1 |
| 3158 | (c-with-<->-as-parens-suppressed |
| 3159 | (if (and here-cpp-beg (> here-cpp-end here-cpp-beg)) |
| 3160 | (c-with-all-but-one-cpps-commented-out |
| 3161 | here-cpp-beg here-cpp-end |
| 3162 | (c-parse-state-1)) |
| 3163 | (c-with-cpps-commented-out |
| 3164 | (c-parse-state-1)))) |
| 3165 | (setq c-state-old-cpp-beg (and here-cpp-beg (copy-marker here-cpp-beg t)) |
| 3166 | c-state-old-cpp-end (and here-cpp-end (copy-marker here-cpp-end t))) |
| 3167 | ))) |
| 3168 | |
| 3169 | ;; Debug tool to catch cache inconsistencies. This is called from |
| 3170 | ;; 000tests.el. |
| 3171 | (defvar c-debug-parse-state nil) |
| 3172 | (unless (fboundp 'c-real-parse-state) |
| 3173 | (fset 'c-real-parse-state (symbol-function 'c-parse-state))) |
| 3174 | (cc-bytecomp-defun c-real-parse-state) |
| 3175 | |
| 3176 | (defvar c-parse-state-state nil) |
| 3177 | (defun c-record-parse-state-state () |
| 3178 | (setq c-parse-state-state |
| 3179 | (mapcar |
| 3180 | (lambda (arg) |
| 3181 | (cons arg (symbol-value arg))) |
| 3182 | '(c-state-cache |
| 3183 | c-state-cache-good-pos |
| 3184 | c-state-nonlit-pos-cache |
| 3185 | c-state-nonlit-pos-cache-limit |
| 3186 | c-state-brace-pair-desert |
| 3187 | c-state-point-min |
| 3188 | c-state-point-min-lit-type |
| 3189 | c-state-point-min-lit-start |
| 3190 | c-state-min-scan-pos |
| 3191 | c-state-old-cpp-beg |
| 3192 | c-state-old-cpp-end)))) |
| 3193 | (defun c-replay-parse-state-state () |
| 3194 | (message |
| 3195 | (concat "(setq " |
| 3196 | (mapconcat |
| 3197 | (lambda (arg) |
| 3198 | (format "%s %s%s" (car arg) (if (atom (cdr arg)) "" "'") (cdr arg))) |
| 3199 | c-parse-state-state " ") |
| 3200 | ")"))) |
| 3201 | |
| 3202 | (defun c-debug-parse-state () |
| 3203 | (let ((here (point)) (res1 (c-real-parse-state)) res2) |
| 3204 | (let ((c-state-cache nil) |
| 3205 | (c-state-cache-good-pos 1) |
| 3206 | (c-state-nonlit-pos-cache nil) |
| 3207 | (c-state-nonlit-pos-cache-limit 1) |
| 3208 | (c-state-brace-pair-desert nil) |
| 3209 | (c-state-point-min 1) |
| 3210 | (c-state-point-min-lit-type nil) |
| 3211 | (c-state-point-min-lit-start nil) |
| 3212 | (c-state-min-scan-pos 1) |
| 3213 | (c-state-old-cpp-beg nil) |
| 3214 | (c-state-old-cpp-end nil)) |
| 3215 | (setq res2 (c-real-parse-state))) |
| 3216 | (unless (equal res1 res2) |
| 3217 | ;; The cache can actually go further back due to the ad-hoc way |
| 3218 | ;; the first paren is found, so try to whack off a bit of its |
| 3219 | ;; start before complaining. |
| 3220 | ;; (save-excursion |
| 3221 | ;; (goto-char (or (c-least-enclosing-brace res2) (point))) |
| 3222 | ;; (c-beginning-of-defun-1) |
| 3223 | ;; (while (not (or (bobp) (eq (char-after) ?{))) |
| 3224 | ;; (c-beginning-of-defun-1)) |
| 3225 | ;; (unless (equal (c-whack-state-before (point) res1) res2) |
| 3226 | ;; (message (concat "c-parse-state inconsistency at %s: " |
| 3227 | ;; "using cache: %s, from scratch: %s") |
| 3228 | ;; here res1 res2))) |
| 3229 | (message (concat "c-parse-state inconsistency at %s: " |
| 3230 | "using cache: %s, from scratch: %s") |
| 3231 | here res1 res2) |
| 3232 | (message "Old state:") |
| 3233 | (c-replay-parse-state-state)) |
| 3234 | (c-record-parse-state-state) |
| 3235 | res1)) |
| 3236 | |
| 3237 | (defun c-toggle-parse-state-debug (&optional arg) |
| 3238 | (interactive "P") |
| 3239 | (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state)) |
| 3240 | (fset 'c-parse-state (symbol-function (if c-debug-parse-state |
| 3241 | 'c-debug-parse-state |
| 3242 | 'c-real-parse-state))) |
| 3243 | (c-keep-region-active)) |
| 3244 | (when c-debug-parse-state |
| 3245 | (c-toggle-parse-state-debug 1)) |
| 3246 | |
| 3247 | \f |
| 3248 | (defun c-whack-state-before (bufpos paren-state) |
| 3249 | ;; Whack off any state information from PAREN-STATE which lies |
| 3250 | ;; before BUFPOS. Not destructive on PAREN-STATE. |
| 3251 | (let* ((newstate (list nil)) |
| 3252 | (ptr newstate) |
| 3253 | car) |
| 3254 | (while paren-state |
| 3255 | (setq car (car paren-state) |
| 3256 | paren-state (cdr paren-state)) |
| 3257 | (if (< (if (consp car) (car car) car) bufpos) |
| 3258 | (setq paren-state nil) |
| 3259 | (setcdr ptr (list car)) |
| 3260 | (setq ptr (cdr ptr)))) |
| 3261 | (cdr newstate))) |
| 3262 | |
| 3263 | (defun c-whack-state-after (bufpos paren-state) |
| 3264 | ;; Whack off any state information from PAREN-STATE which lies at or |
| 3265 | ;; after BUFPOS. Not destructive on PAREN-STATE. |
| 3266 | (catch 'done |
| 3267 | (while paren-state |
| 3268 | (let ((car (car paren-state))) |
| 3269 | (if (consp car) |
| 3270 | ;; just check the car, because in a balanced brace |
| 3271 | ;; expression, it must be impossible for the corresponding |
| 3272 | ;; close brace to be before point, but the open brace to |
| 3273 | ;; be after. |
| 3274 | (if (<= bufpos (car car)) |
| 3275 | nil ; whack it off |
| 3276 | (if (< bufpos (cdr car)) |
| 3277 | ;; its possible that the open brace is before |
| 3278 | ;; bufpos, but the close brace is after. In that |
| 3279 | ;; case, convert this to a non-cons element. The |
| 3280 | ;; rest of the state is before bufpos, so we're |
| 3281 | ;; done. |
| 3282 | (throw 'done (cons (car car) (cdr paren-state))) |
| 3283 | ;; we know that both the open and close braces are |
| 3284 | ;; before bufpos, so we also know that everything else |
| 3285 | ;; on state is before bufpos. |
| 3286 | (throw 'done paren-state))) |
| 3287 | (if (<= bufpos car) |
| 3288 | nil ; whack it off |
| 3289 | ;; it's before bufpos, so everything else should too. |
| 3290 | (throw 'done paren-state))) |
| 3291 | (setq paren-state (cdr paren-state))) |
| 3292 | nil))) |
| 3293 | |
| 3294 | (defun c-most-enclosing-brace (paren-state &optional bufpos) |
| 3295 | ;; Return the bufpos of the innermost enclosing open paren before |
| 3296 | ;; bufpos, or nil if none was found. |
| 3297 | (let (enclosingp) |
| 3298 | (or bufpos (setq bufpos 134217727)) |
| 3299 | (while paren-state |
| 3300 | (setq enclosingp (car paren-state) |
| 3301 | paren-state (cdr paren-state)) |
| 3302 | (if (or (consp enclosingp) |
| 3303 | (>= enclosingp bufpos)) |
| 3304 | (setq enclosingp nil) |
| 3305 | (setq paren-state nil))) |
| 3306 | enclosingp)) |
| 3307 | |
| 3308 | (defun c-least-enclosing-brace (paren-state) |
| 3309 | ;; Return the bufpos of the outermost enclosing open paren, or nil |
| 3310 | ;; if none was found. |
| 3311 | (let (pos elem) |
| 3312 | (while paren-state |
| 3313 | (setq elem (car paren-state) |
| 3314 | paren-state (cdr paren-state)) |
| 3315 | (if (integerp elem) |
| 3316 | (setq pos elem))) |
| 3317 | pos)) |
| 3318 | |
| 3319 | (defun c-safe-position (bufpos paren-state) |
| 3320 | ;; Return the closest "safe" position recorded on PAREN-STATE that |
| 3321 | ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't |
| 3322 | ;; contain any. Return nil if BUFPOS is nil, which is useful to |
| 3323 | ;; find the closest limit before a given limit that might be nil. |
| 3324 | ;; |
| 3325 | ;; A "safe" position is a position at or after a recorded open |
| 3326 | ;; paren, or after a recorded close paren. The returned position is |
| 3327 | ;; thus either the first position after a close brace, or the first |
| 3328 | ;; position after an enclosing paren, or at the enclosing paren in |
| 3329 | ;; case BUFPOS is immediately after it. |
| 3330 | (when bufpos |
| 3331 | (let (elem) |
| 3332 | (catch 'done |
| 3333 | (while paren-state |
| 3334 | (setq elem (car paren-state)) |
| 3335 | (if (consp elem) |
| 3336 | (cond ((< (cdr elem) bufpos) |
| 3337 | (throw 'done (cdr elem))) |
| 3338 | ((< (car elem) bufpos) |
| 3339 | ;; See below. |
| 3340 | (throw 'done (min (1+ (car elem)) bufpos)))) |
| 3341 | (if (< elem bufpos) |
| 3342 | ;; elem is the position at and not after the opening paren, so |
| 3343 | ;; we can go forward one more step unless it's equal to |
| 3344 | ;; bufpos. This is useful in some cases avoid an extra paren |
| 3345 | ;; level between the safe position and bufpos. |
| 3346 | (throw 'done (min (1+ elem) bufpos)))) |
| 3347 | (setq paren-state (cdr paren-state))))))) |
| 3348 | |
| 3349 | (defun c-beginning-of-syntax () |
| 3350 | ;; This is used for `font-lock-beginning-of-syntax-function'. It |
| 3351 | ;; goes to the closest previous point that is known to be outside |
| 3352 | ;; any string literal or comment. `c-state-cache' is used if it has |
| 3353 | ;; a position in the vicinity. |
| 3354 | (let* ((paren-state c-state-cache) |
| 3355 | elem |
| 3356 | |
| 3357 | (pos (catch 'done |
| 3358 | ;; Note: Similar code in `c-safe-position'. The |
| 3359 | ;; difference is that we accept a safe position at |
| 3360 | ;; the point and don't bother to go forward past open |
| 3361 | ;; parens. |
| 3362 | (while paren-state |
| 3363 | (setq elem (car paren-state)) |
| 3364 | (if (consp elem) |
| 3365 | (cond ((<= (cdr elem) (point)) |
| 3366 | (throw 'done (cdr elem))) |
| 3367 | ((<= (car elem) (point)) |
| 3368 | (throw 'done (car elem)))) |
| 3369 | (if (<= elem (point)) |
| 3370 | (throw 'done elem))) |
| 3371 | (setq paren-state (cdr paren-state))) |
| 3372 | (point-min)))) |
| 3373 | |
| 3374 | (if (> pos (- (point) 4000)) |
| 3375 | (goto-char pos) |
| 3376 | ;; The position is far back. Try `c-beginning-of-defun-1' |
| 3377 | ;; (although we can't be entirely sure it will go to a position |
| 3378 | ;; outside a comment or string in current emacsen). FIXME: |
| 3379 | ;; Consult `syntax-ppss' here. |
| 3380 | (c-beginning-of-defun-1) |
| 3381 | (if (< (point) pos) |
| 3382 | (goto-char pos))))) |
| 3383 | |
| 3384 | \f |
| 3385 | ;; Tools for scanning identifiers and other tokens. |
| 3386 | |
| 3387 | (defun c-on-identifier () |
| 3388 | "Return non-nil if the point is on or directly after an identifier. |
| 3389 | Keywords are recognized and not considered identifiers. If an |
| 3390 | identifier is detected, the returned value is its starting position. |
| 3391 | If an identifier ends at the point and another begins at it \(can only |
| 3392 | happen in Pike) then the point for the preceding one is returned. |
| 3393 | |
| 3394 | Note that this function might do hidden buffer changes. See the |
| 3395 | comment at the start of cc-engine.el for more info." |
| 3396 | |
| 3397 | ;; FIXME: Shouldn't this function handle "operator" in C++? |
| 3398 | |
| 3399 | (save-excursion |
| 3400 | (skip-syntax-backward "w_") |
| 3401 | |
| 3402 | (or |
| 3403 | |
| 3404 | ;; Check for a normal (non-keyword) identifier. |
| 3405 | (and (looking-at c-symbol-start) |
| 3406 | (not (looking-at c-keywords-regexp)) |
| 3407 | (point)) |
| 3408 | |
| 3409 | (when (c-major-mode-is 'pike-mode) |
| 3410 | ;; Handle the `<operator> syntax in Pike. |
| 3411 | (let ((pos (point))) |
| 3412 | (skip-chars-backward "-!%&*+/<=>^|~[]()") |
| 3413 | (and (if (< (skip-chars-backward "`") 0) |
| 3414 | t |
| 3415 | (goto-char pos) |
| 3416 | (eq (char-after) ?\`)) |
| 3417 | (looking-at c-symbol-key) |
| 3418 | (>= (match-end 0) pos) |
| 3419 | (point)))) |
| 3420 | |
| 3421 | ;; Handle the "operator +" syntax in C++. |
| 3422 | (when (and c-overloadable-operators-regexp |
| 3423 | (= (c-backward-token-2 0) 0)) |
| 3424 | |
| 3425 | (cond ((and (looking-at c-overloadable-operators-regexp) |
| 3426 | (or (not c-opt-op-identifier-prefix) |
| 3427 | (and (= (c-backward-token-2 1) 0) |
| 3428 | (looking-at c-opt-op-identifier-prefix)))) |
| 3429 | (point)) |
| 3430 | |
| 3431 | ((save-excursion |
| 3432 | (and c-opt-op-identifier-prefix |
| 3433 | (looking-at c-opt-op-identifier-prefix) |
| 3434 | (= (c-forward-token-2 1) 0) |
| 3435 | (looking-at c-overloadable-operators-regexp))) |
| 3436 | (point)))) |
| 3437 | |
| 3438 | ))) |
| 3439 | |
| 3440 | (defsubst c-simple-skip-symbol-backward () |
| 3441 | ;; If the point is at the end of a symbol then skip backward to the |
| 3442 | ;; beginning of it. Don't move otherwise. Return non-nil if point |
| 3443 | ;; moved. |
| 3444 | ;; |
| 3445 | ;; This function might do hidden buffer changes. |
| 3446 | (or (< (skip-syntax-backward "w_") 0) |
| 3447 | (and (c-major-mode-is 'pike-mode) |
| 3448 | ;; Handle the `<operator> syntax in Pike. |
| 3449 | (let ((pos (point))) |
| 3450 | (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0) |
| 3451 | (< (skip-chars-backward "`") 0) |
| 3452 | (looking-at c-symbol-key) |
| 3453 | (>= (match-end 0) pos)) |
| 3454 | t |
| 3455 | (goto-char pos) |
| 3456 | nil))))) |
| 3457 | |
| 3458 | (defun c-beginning-of-current-token (&optional back-limit) |
| 3459 | ;; Move to the beginning of the current token. Do not move if not |
| 3460 | ;; in the middle of one. BACK-LIMIT may be used to bound the |
| 3461 | ;; backward search; if given it's assumed to be at the boundary |
| 3462 | ;; between two tokens. Return non-nil if the point is moved, nil |
| 3463 | ;; otherwise. |
| 3464 | ;; |
| 3465 | ;; This function might do hidden buffer changes. |
| 3466 | (let ((start (point))) |
| 3467 | (if (looking-at "\\w\\|\\s_") |
| 3468 | (skip-syntax-backward "w_" back-limit) |
| 3469 | (when (< (skip-syntax-backward ".()" back-limit) 0) |
| 3470 | (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp) |
| 3471 | (match-end 0)) |
| 3472 | ;; `c-nonsymbol-token-regexp' should always match |
| 3473 | ;; since we've skipped backward over punctuator |
| 3474 | ;; or paren syntax, but consume one char in case |
| 3475 | ;; it doesn't so that we don't leave point before |
| 3476 | ;; some earlier incorrect token. |
| 3477 | (1+ (point))))) |
| 3478 | (if (<= pos start) |
| 3479 | (goto-char pos)))))) |
| 3480 | (< (point) start))) |
| 3481 | |
| 3482 | (defun c-end-of-current-token (&optional back-limit) |
| 3483 | ;; Move to the end of the current token. Do not move if not in the |
| 3484 | ;; middle of one. BACK-LIMIT may be used to bound the backward |
| 3485 | ;; search; if given it's assumed to be at the boundary between two |
| 3486 | ;; tokens. Return non-nil if the point is moved, nil otherwise. |
| 3487 | ;; |
| 3488 | ;; This function might do hidden buffer changes. |
| 3489 | (let ((start (point))) |
| 3490 | (cond ((< (skip-syntax-backward "w_" (1- start)) 0) |
| 3491 | (skip-syntax-forward "w_")) |
| 3492 | ((< (skip-syntax-backward ".()" back-limit) 0) |
| 3493 | (while (progn |
| 3494 | (if (looking-at c-nonsymbol-token-regexp) |
| 3495 | (goto-char (match-end 0)) |
| 3496 | ;; `c-nonsymbol-token-regexp' should always match since |
| 3497 | ;; we've skipped backward over punctuator or paren |
| 3498 | ;; syntax, but move forward in case it doesn't so that |
| 3499 | ;; we don't leave point earlier than we started with. |
| 3500 | (forward-char)) |
| 3501 | (< (point) start))))) |
| 3502 | (> (point) start))) |
| 3503 | |
| 3504 | (defconst c-jump-syntax-balanced |
| 3505 | (if (memq 'gen-string-delim c-emacs-features) |
| 3506 | "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|" |
| 3507 | "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"")) |
| 3508 | |
| 3509 | (defconst c-jump-syntax-unbalanced |
| 3510 | (if (memq 'gen-string-delim c-emacs-features) |
| 3511 | "\\w\\|\\s_\\|\\s\"\\|\\s|" |
| 3512 | "\\w\\|\\s_\\|\\s\"")) |
| 3513 | |
| 3514 | (defun c-forward-token-2 (&optional count balanced limit) |
| 3515 | "Move forward by tokens. |
| 3516 | A token is defined as all symbols and identifiers which aren't |
| 3517 | syntactic whitespace \(note that multicharacter tokens like \"==\" are |
| 3518 | treated properly). Point is always either left at the beginning of a |
| 3519 | token or not moved at all. COUNT specifies the number of tokens to |
| 3520 | move; a negative COUNT moves in the opposite direction. A COUNT of 0 |
| 3521 | moves to the next token beginning only if not already at one. If |
| 3522 | BALANCED is true, move over balanced parens, otherwise move into them. |
| 3523 | Also, if BALANCED is true, never move out of an enclosing paren. |
| 3524 | |
| 3525 | LIMIT sets the limit for the movement and defaults to the point limit. |
| 3526 | The case when LIMIT is set in the middle of a token, comment or macro |
| 3527 | is handled correctly, i.e. the point won't be left there. |
| 3528 | |
| 3529 | Return the number of tokens left to move \(positive or negative). If |
| 3530 | BALANCED is true, a move over a balanced paren counts as one. Note |
| 3531 | that if COUNT is 0 and no appropriate token beginning is found, 1 will |
| 3532 | be returned. Thus, a return value of 0 guarantees that point is at |
| 3533 | the requested position and a return value less \(without signs) than |
| 3534 | COUNT guarantees that point is at the beginning of some token. |
| 3535 | |
| 3536 | Note that this function might do hidden buffer changes. See the |
| 3537 | comment at the start of cc-engine.el for more info." |
| 3538 | |
| 3539 | (or count (setq count 1)) |
| 3540 | (if (< count 0) |
| 3541 | (- (c-backward-token-2 (- count) balanced limit)) |
| 3542 | |
| 3543 | (let ((jump-syntax (if balanced |
| 3544 | c-jump-syntax-balanced |
| 3545 | c-jump-syntax-unbalanced)) |
| 3546 | (last (point)) |
| 3547 | (prev (point))) |
| 3548 | |
| 3549 | (if (zerop count) |
| 3550 | ;; If count is zero we should jump if in the middle of a token. |
| 3551 | (c-end-of-current-token)) |
| 3552 | |
| 3553 | (save-restriction |
| 3554 | (if limit (narrow-to-region (point-min) limit)) |
| 3555 | (if (/= (point) |
| 3556 | (progn (c-forward-syntactic-ws) (point))) |
| 3557 | ;; Skip whitespace. Count this as a move if we did in |
| 3558 | ;; fact move. |
| 3559 | (setq count (max (1- count) 0))) |
| 3560 | |
| 3561 | (if (eobp) |
| 3562 | ;; Moved out of bounds. Make sure the returned count isn't zero. |
| 3563 | (progn |
| 3564 | (if (zerop count) (setq count 1)) |
| 3565 | (goto-char last)) |
| 3566 | |
| 3567 | ;; Use `condition-case' to avoid having the limit tests |
| 3568 | ;; inside the loop. |
| 3569 | (condition-case nil |
| 3570 | (while (and |
| 3571 | (> count 0) |
| 3572 | (progn |
| 3573 | (setq last (point)) |
| 3574 | (cond ((looking-at jump-syntax) |
| 3575 | (goto-char (scan-sexps (point) 1)) |
| 3576 | t) |
| 3577 | ((looking-at c-nonsymbol-token-regexp) |
| 3578 | (goto-char (match-end 0)) |
| 3579 | t) |
| 3580 | ;; `c-nonsymbol-token-regexp' above should always |
| 3581 | ;; match if there are correct tokens. Try to |
| 3582 | ;; widen to see if the limit was set in the |
| 3583 | ;; middle of one, else fall back to treating |
| 3584 | ;; the offending thing as a one character token. |
| 3585 | ((and limit |
| 3586 | (save-restriction |
| 3587 | (widen) |
| 3588 | (looking-at c-nonsymbol-token-regexp))) |
| 3589 | nil) |
| 3590 | (t |
| 3591 | (forward-char) |
| 3592 | t)))) |
| 3593 | (c-forward-syntactic-ws) |
| 3594 | (setq prev last |
| 3595 | count (1- count))) |
| 3596 | (error (goto-char last))) |
| 3597 | |
| 3598 | (when (eobp) |
| 3599 | (goto-char prev) |
| 3600 | (setq count (1+ count))))) |
| 3601 | |
| 3602 | count))) |
| 3603 | |
| 3604 | (defun c-backward-token-2 (&optional count balanced limit) |
| 3605 | "Move backward by tokens. |
| 3606 | See `c-forward-token-2' for details." |
| 3607 | |
| 3608 | (or count (setq count 1)) |
| 3609 | (if (< count 0) |
| 3610 | (- (c-forward-token-2 (- count) balanced limit)) |
| 3611 | |
| 3612 | (or limit (setq limit (point-min))) |
| 3613 | (let ((jump-syntax (if balanced |
| 3614 | c-jump-syntax-balanced |
| 3615 | c-jump-syntax-unbalanced)) |
| 3616 | (last (point))) |
| 3617 | |
| 3618 | (if (zerop count) |
| 3619 | ;; The count is zero so try to skip to the beginning of the |
| 3620 | ;; current token. |
| 3621 | (if (> (point) |
| 3622 | (progn (c-beginning-of-current-token) (point))) |
| 3623 | (if (< (point) limit) |
| 3624 | ;; The limit is inside the same token, so return 1. |
| 3625 | (setq count 1)) |
| 3626 | |
| 3627 | ;; We're not in the middle of a token. If there's |
| 3628 | ;; whitespace after the point then we must move backward, |
| 3629 | ;; so set count to 1 in that case. |
| 3630 | (and (looking-at c-syntactic-ws-start) |
| 3631 | ;; If we're looking at a '#' that might start a cpp |
| 3632 | ;; directive then we have to do a more elaborate check. |
| 3633 | (or (/= (char-after) ?#) |
| 3634 | (not c-opt-cpp-prefix) |
| 3635 | (save-excursion |
| 3636 | (and (= (point) |
| 3637 | (progn (beginning-of-line) |
| 3638 | (looking-at "[ \t]*") |
| 3639 | (match-end 0))) |
| 3640 | (or (bobp) |
| 3641 | (progn (backward-char) |
| 3642 | (not (eq (char-before) ?\\))))))) |
| 3643 | (setq count 1)))) |
| 3644 | |
| 3645 | ;; Use `condition-case' to avoid having to check for buffer |
| 3646 | ;; limits in `backward-char', `scan-sexps' and `goto-char' below. |
| 3647 | (condition-case nil |
| 3648 | (while (and |
| 3649 | (> count 0) |
| 3650 | (progn |
| 3651 | (c-backward-syntactic-ws) |
| 3652 | (backward-char) |
| 3653 | (if (looking-at jump-syntax) |
| 3654 | (goto-char (scan-sexps (1+ (point)) -1)) |
| 3655 | ;; This can be very inefficient if there's a long |
| 3656 | ;; sequence of operator tokens without any separation. |
| 3657 | ;; That doesn't happen in practice, anyway. |
| 3658 | (c-beginning-of-current-token)) |
| 3659 | (>= (point) limit))) |
| 3660 | (setq last (point) |
| 3661 | count (1- count))) |
| 3662 | (error (goto-char last))) |
| 3663 | |
| 3664 | (if (< (point) limit) |
| 3665 | (goto-char last)) |
| 3666 | |
| 3667 | count))) |
| 3668 | |
| 3669 | (defun c-forward-token-1 (&optional count balanced limit) |
| 3670 | "Like `c-forward-token-2' but doesn't treat multicharacter operator |
| 3671 | tokens like \"==\" as single tokens, i.e. all sequences of symbol |
| 3672 | characters are jumped over character by character. This function is |
| 3673 | for compatibility only; it's only a wrapper over `c-forward-token-2'." |
| 3674 | (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)")) |
| 3675 | (c-forward-token-2 count balanced limit))) |
| 3676 | |
| 3677 | (defun c-backward-token-1 (&optional count balanced limit) |
| 3678 | "Like `c-backward-token-2' but doesn't treat multicharacter operator |
| 3679 | tokens like \"==\" as single tokens, i.e. all sequences of symbol |
| 3680 | characters are jumped over character by character. This function is |
| 3681 | for compatibility only; it's only a wrapper over `c-backward-token-2'." |
| 3682 | (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)")) |
| 3683 | (c-backward-token-2 count balanced limit))) |
| 3684 | |
| 3685 | \f |
| 3686 | ;; Tools for doing searches restricted to syntactically relevant text. |
| 3687 | |
| 3688 | (defun c-syntactic-re-search-forward (regexp &optional bound noerror |
| 3689 | paren-level not-inside-token |
| 3690 | lookbehind-submatch) |
| 3691 | "Like `re-search-forward', but only report matches that are found |
| 3692 | in syntactically significant text. I.e. matches in comments, macros |
| 3693 | or string literals are ignored. The start point is assumed to be |
| 3694 | outside any comment, macro or string literal, or else the content of |
| 3695 | that region is taken as syntactically significant text. |
| 3696 | |
| 3697 | If PAREN-LEVEL is non-nil, an additional restriction is added to |
| 3698 | ignore matches in nested paren sexps. The search will also not go |
| 3699 | outside the current list sexp, which has the effect that if the point |
| 3700 | should be moved to BOUND when no match is found \(i.e. NOERROR is |
| 3701 | neither nil nor t), then it will be at the closing paren if the end of |
| 3702 | the current list sexp is encountered first. |
| 3703 | |
| 3704 | If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are |
| 3705 | ignored. Things like multicharacter operators and special symbols |
| 3706 | \(e.g. \"`()\" in Pike) are handled but currently not floating point |
| 3707 | constants. |
| 3708 | |
| 3709 | If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a |
| 3710 | subexpression in REGEXP. The end of that submatch is used as the |
| 3711 | position to check for syntactic significance. If LOOKBEHIND-SUBMATCH |
| 3712 | isn't used or if that subexpression didn't match then the start |
| 3713 | position of the whole match is used instead. The \"look behind\" |
| 3714 | subexpression is never tested before the starting position, so it |
| 3715 | might be a good idea to include \\=\\= as a match alternative in it. |
| 3716 | |
| 3717 | Optimization note: Matches might be missed if the \"look behind\" |
| 3718 | subexpression can match the end of nonwhite syntactic whitespace, |
| 3719 | i.e. the end of comments or cpp directives. This since the function |
| 3720 | skips over such things before resuming the search. It's on the other |
| 3721 | hand not safe to assume that the \"look behind\" subexpression never |
| 3722 | matches syntactic whitespace. |
| 3723 | |
| 3724 | Bug: Unbalanced parens inside cpp directives are currently not handled |
| 3725 | correctly \(i.e. they don't get ignored as they should) when |
| 3726 | PAREN-LEVEL is set. |
| 3727 | |
| 3728 | Note that this function might do hidden buffer changes. See the |
| 3729 | comment at the start of cc-engine.el for more info." |
| 3730 | |
| 3731 | (or bound (setq bound (point-max))) |
| 3732 | (if paren-level (setq paren-level -1)) |
| 3733 | |
| 3734 | ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp) |
| 3735 | |
| 3736 | (let ((start (point)) |
| 3737 | tmp |
| 3738 | ;; Start position for the last search. |
| 3739 | search-pos |
| 3740 | ;; The `parse-partial-sexp' state between the start position |
| 3741 | ;; and the point. |
| 3742 | state |
| 3743 | ;; The current position after the last state update. The next |
| 3744 | ;; `parse-partial-sexp' continues from here. |
| 3745 | (state-pos (point)) |
| 3746 | ;; The position at which to check the state and the state |
| 3747 | ;; there. This is separate from `state-pos' since we might |
| 3748 | ;; need to back up before doing the next search round. |
| 3749 | check-pos check-state |
| 3750 | ;; Last position known to end a token. |
| 3751 | (last-token-end-pos (point-min)) |
| 3752 | ;; Set when a valid match is found. |
| 3753 | found) |
| 3754 | |
| 3755 | (condition-case err |
| 3756 | (while |
| 3757 | (and |
| 3758 | (progn |
| 3759 | (setq search-pos (point)) |
| 3760 | (re-search-forward regexp bound noerror)) |
| 3761 | |
| 3762 | (progn |
| 3763 | (setq state (parse-partial-sexp |
| 3764 | state-pos (match-beginning 0) paren-level nil state) |
| 3765 | state-pos (point)) |
| 3766 | (if (setq check-pos (and lookbehind-submatch |
| 3767 | (or (not paren-level) |
| 3768 | (>= (car state) 0)) |
| 3769 | (match-end lookbehind-submatch))) |
| 3770 | (setq check-state (parse-partial-sexp |
| 3771 | state-pos check-pos paren-level nil state)) |
| 3772 | (setq check-pos state-pos |
| 3773 | check-state state)) |
| 3774 | |
| 3775 | ;; NOTE: If we got a look behind subexpression and get |
| 3776 | ;; an insignificant match in something that isn't |
| 3777 | ;; syntactic whitespace (i.e. strings or in nested |
| 3778 | ;; parentheses), then we can never skip more than a |
| 3779 | ;; single character from the match start position |
| 3780 | ;; (i.e. `state-pos' here) before continuing the |
| 3781 | ;; search. That since the look behind subexpression |
| 3782 | ;; might match the end of the insignificant region in |
| 3783 | ;; the next search. |
| 3784 | |
| 3785 | (cond |
| 3786 | ((elt check-state 7) |
| 3787 | ;; Match inside a line comment. Skip to eol. Use |
| 3788 | ;; `re-search-forward' instead of `skip-chars-forward' to get |
| 3789 | ;; the right bound behavior. |
| 3790 | (re-search-forward "[\n\r]" bound noerror)) |
| 3791 | |
| 3792 | ((elt check-state 4) |
| 3793 | ;; Match inside a block comment. Skip to the '*/'. |
| 3794 | (search-forward "*/" bound noerror)) |
| 3795 | |
| 3796 | ((and (not (elt check-state 5)) |
| 3797 | (eq (char-before check-pos) ?/) |
| 3798 | (not (c-get-char-property (1- check-pos) 'syntax-table)) |
| 3799 | (memq (char-after check-pos) '(?/ ?*))) |
| 3800 | ;; Match in the middle of the opener of a block or line |
| 3801 | ;; comment. |
| 3802 | (if (= (char-after check-pos) ?/) |
| 3803 | (re-search-forward "[\n\r]" bound noerror) |
| 3804 | (search-forward "*/" bound noerror))) |
| 3805 | |
| 3806 | ;; The last `parse-partial-sexp' above might have |
| 3807 | ;; stopped short of the real check position if the end |
| 3808 | ;; of the current sexp was encountered in paren-level |
| 3809 | ;; mode. The checks above are always false in that |
| 3810 | ;; case, and since they can do better skipping in |
| 3811 | ;; lookbehind-submatch mode, we do them before |
| 3812 | ;; checking the paren level. |
| 3813 | |
| 3814 | ((and paren-level |
| 3815 | (/= (setq tmp (car check-state)) 0)) |
| 3816 | ;; Check the paren level first since we're short of the |
| 3817 | ;; syntactic checking position if the end of the |
| 3818 | ;; current sexp was encountered by `parse-partial-sexp'. |
| 3819 | (if (> tmp 0) |
| 3820 | |
| 3821 | ;; Inside a nested paren sexp. |
| 3822 | (if lookbehind-submatch |
| 3823 | ;; See the NOTE above. |
| 3824 | (progn (goto-char state-pos) t) |
| 3825 | ;; Skip out of the paren quickly. |
| 3826 | (setq state (parse-partial-sexp state-pos bound 0 nil state) |
| 3827 | state-pos (point))) |
| 3828 | |
| 3829 | ;; Have exited the current paren sexp. |
| 3830 | (if noerror |
| 3831 | (progn |
| 3832 | ;; The last `parse-partial-sexp' call above |
| 3833 | ;; has left us just after the closing paren |
| 3834 | ;; in this case, so we can modify the bound |
| 3835 | ;; to leave the point at the right position |
| 3836 | ;; upon return. |
| 3837 | (setq bound (1- (point))) |
| 3838 | nil) |
| 3839 | (signal 'search-failed (list regexp))))) |
| 3840 | |
| 3841 | ((setq tmp (elt check-state 3)) |
| 3842 | ;; Match inside a string. |
| 3843 | (if (or lookbehind-submatch |
| 3844 | (not (integerp tmp))) |
| 3845 | ;; See the NOTE above. |
| 3846 | (progn (goto-char state-pos) t) |
| 3847 | ;; Skip to the end of the string before continuing. |
| 3848 | (let ((ender (make-string 1 tmp)) (continue t)) |
| 3849 | (while (if (search-forward ender bound noerror) |
| 3850 | (progn |
| 3851 | (setq state (parse-partial-sexp |
| 3852 | state-pos (point) nil nil state) |
| 3853 | state-pos (point)) |
| 3854 | (elt state 3)) |
| 3855 | (setq continue nil))) |
| 3856 | continue))) |
| 3857 | |
| 3858 | ((save-excursion |
| 3859 | (save-match-data |
| 3860 | (c-beginning-of-macro start))) |
| 3861 | ;; Match inside a macro. Skip to the end of it. |
| 3862 | (c-end-of-macro) |
| 3863 | (cond ((<= (point) bound) t) |
| 3864 | (noerror nil) |
| 3865 | (t (signal 'search-failed (list regexp))))) |
| 3866 | |
| 3867 | ((and not-inside-token |
| 3868 | (or (< check-pos last-token-end-pos) |
| 3869 | (< check-pos |
| 3870 | (save-excursion |
| 3871 | (goto-char check-pos) |
| 3872 | (save-match-data |
| 3873 | (c-end-of-current-token last-token-end-pos)) |
| 3874 | (setq last-token-end-pos (point)))))) |
| 3875 | ;; Inside a token. |
| 3876 | (if lookbehind-submatch |
| 3877 | ;; See the NOTE above. |
| 3878 | (goto-char state-pos) |
| 3879 | (goto-char (min last-token-end-pos bound)))) |
| 3880 | |
| 3881 | (t |
| 3882 | ;; A real match. |
| 3883 | (setq found t) |
| 3884 | nil))) |
| 3885 | |
| 3886 | ;; Should loop to search again, but take care to avoid |
| 3887 | ;; looping on the same spot. |
| 3888 | (or (/= search-pos (point)) |
| 3889 | (if (= (point) bound) |
| 3890 | (if noerror |
| 3891 | nil |
| 3892 | (signal 'search-failed (list regexp))) |
| 3893 | (forward-char) |
| 3894 | t)))) |
| 3895 | |
| 3896 | (error |
| 3897 | (goto-char start) |
| 3898 | (signal (car err) (cdr err)))) |
| 3899 | |
| 3900 | ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point))) |
| 3901 | |
| 3902 | (if found |
| 3903 | (progn |
| 3904 | (goto-char (match-end 0)) |
| 3905 | (match-end 0)) |
| 3906 | |
| 3907 | ;; Search failed. Set point as appropriate. |
| 3908 | (if (eq noerror t) |
| 3909 | (goto-char start) |
| 3910 | (goto-char bound)) |
| 3911 | nil))) |
| 3912 | |
| 3913 | (defvar safe-pos-list) ; bound in c-syntactic-skip-backward |
| 3914 | |
| 3915 | (defsubst c-ssb-lit-begin () |
| 3916 | ;; Return the start of the literal point is in, or nil. |
| 3917 | ;; We read and write the variables `safe-pos', `safe-pos-list', `state' |
| 3918 | ;; bound in the caller. |
| 3919 | |
| 3920 | ;; Use `parse-partial-sexp' from a safe position down to the point to check |
| 3921 | ;; if it's outside comments and strings. |
| 3922 | (save-excursion |
| 3923 | (let ((pos (point)) safe-pos state pps-end-pos) |
| 3924 | ;; Pick a safe position as close to the point as possible. |
| 3925 | ;; |
| 3926 | ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good |
| 3927 | ;; position. |
| 3928 | |
| 3929 | (while (and safe-pos-list |
| 3930 | (> (car safe-pos-list) (point))) |
| 3931 | (setq safe-pos-list (cdr safe-pos-list))) |
| 3932 | (unless (setq safe-pos (car-safe safe-pos-list)) |
| 3933 | (setq safe-pos (max (or (c-safe-position |
| 3934 | (point) (or c-state-cache |
| 3935 | (c-parse-state))) |
| 3936 | 0) |
| 3937 | (point-min)) |
| 3938 | safe-pos-list (list safe-pos))) |
| 3939 | |
| 3940 | ;; Cache positions along the way to use if we have to back up more. We |
| 3941 | ;; cache every closing paren on the same level. If the paren cache is |
| 3942 | ;; relevant in this region then we're typically already on the same |
| 3943 | ;; level as the target position. Note that we might cache positions |
| 3944 | ;; after opening parens in case safe-pos is in a nested list. That's |
| 3945 | ;; both uncommon and harmless. |
| 3946 | (while (progn |
| 3947 | (setq state (parse-partial-sexp |
| 3948 | safe-pos pos 0)) |
| 3949 | (< (point) pos)) |
| 3950 | (setq safe-pos (point) |
| 3951 | safe-pos-list (cons safe-pos safe-pos-list))) |
| 3952 | |
| 3953 | ;; If the state contains the start of the containing sexp we cache that |
| 3954 | ;; position too, so that parse-partial-sexp in the next run has a bigger |
| 3955 | ;; chance of starting at the same level as the target position and thus |
| 3956 | ;; will get more good safe positions into the list. |
| 3957 | (if (elt state 1) |
| 3958 | (setq safe-pos (1+ (elt state 1)) |
| 3959 | safe-pos-list (cons safe-pos safe-pos-list))) |
| 3960 | |
| 3961 | (if (or (elt state 3) (elt state 4)) |
| 3962 | ;; Inside string or comment. Continue search at the |
| 3963 | ;; beginning of it. |
| 3964 | (elt state 8))))) |
| 3965 | |
| 3966 | (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level) |
| 3967 | "Like `skip-chars-backward' but only look at syntactically relevant chars, |
| 3968 | i.e. don't stop at positions inside syntactic whitespace or string |
| 3969 | literals. Preprocessor directives are also ignored, with the exception |
| 3970 | of the one that the point starts within, if any. If LIMIT is given, |
| 3971 | it's assumed to be at a syntactically relevant position. |
| 3972 | |
| 3973 | If PAREN-LEVEL is non-nil, the function won't stop in nested paren |
| 3974 | sexps, and the search will also not go outside the current paren sexp. |
| 3975 | However, if LIMIT or the buffer limit is reached inside a nested paren |
| 3976 | then the point will be left at the limit. |
| 3977 | |
| 3978 | Non-nil is returned if the point moved, nil otherwise. |
| 3979 | |
| 3980 | Note that this function might do hidden buffer changes. See the |
| 3981 | comment at the start of cc-engine.el for more info." |
| 3982 | |
| 3983 | (let ((start (point)) |
| 3984 | state-2 |
| 3985 | ;; A list of syntactically relevant positions in descending |
| 3986 | ;; order. It's used to avoid scanning repeatedly over |
| 3987 | ;; potentially large regions with `parse-partial-sexp' to verify |
| 3988 | ;; each position. Used in `c-ssb-lit-begin' |
| 3989 | safe-pos-list |
| 3990 | ;; The result from `c-beginning-of-macro' at the start position or the |
| 3991 | ;; start position itself if it isn't within a macro. Evaluated on |
| 3992 | ;; demand. |
| 3993 | start-macro-beg |
| 3994 | ;; The earliest position after the current one with the same paren |
| 3995 | ;; level. Used only when `paren-level' is set. |
| 3996 | lit-beg |
| 3997 | (paren-level-pos (point))) |
| 3998 | |
| 3999 | (while |
| 4000 | (progn |
| 4001 | ;; The next loop "tries" to find the end point each time round, |
| 4002 | ;; loops when it hasn't succeeded. |
| 4003 | (while |
| 4004 | (and |
| 4005 | (< (skip-chars-backward skip-chars limit) 0) |
| 4006 | |
| 4007 | (let ((pos (point)) state-2 pps-end-pos) |
| 4008 | |
| 4009 | (cond |
| 4010 | ;; Don't stop inside a literal |
| 4011 | ((setq lit-beg (c-ssb-lit-begin)) |
| 4012 | (goto-char lit-beg) |
| 4013 | t) |
| 4014 | |
| 4015 | ((and paren-level |
| 4016 | (save-excursion |
| 4017 | (setq state-2 (parse-partial-sexp |
| 4018 | pos paren-level-pos -1) |
| 4019 | pps-end-pos (point)) |
| 4020 | (/= (car state-2) 0))) |
| 4021 | ;; Not at the right level. |
| 4022 | |
| 4023 | (if (and (< (car state-2) 0) |
| 4024 | ;; We stop above if we go out of a paren. |
| 4025 | ;; Now check whether it precedes or is |
| 4026 | ;; nested in the starting sexp. |
| 4027 | (save-excursion |
| 4028 | (setq state-2 |
| 4029 | (parse-partial-sexp |
| 4030 | pps-end-pos paren-level-pos |
| 4031 | nil nil state-2)) |
| 4032 | (< (car state-2) 0))) |
| 4033 | |
| 4034 | ;; We've stopped short of the starting position |
| 4035 | ;; so the hit was inside a nested list. Go up |
| 4036 | ;; until we are at the right level. |
| 4037 | (condition-case nil |
| 4038 | (progn |
| 4039 | (goto-char (scan-lists pos -1 |
| 4040 | (- (car state-2)))) |
| 4041 | (setq paren-level-pos (point)) |
| 4042 | (if (and limit (>= limit paren-level-pos)) |
| 4043 | (progn |
| 4044 | (goto-char limit) |
| 4045 | nil) |
| 4046 | t)) |
| 4047 | (error |
| 4048 | (goto-char (or limit (point-min))) |
| 4049 | nil)) |
| 4050 | |
| 4051 | ;; The hit was outside the list at the start |
| 4052 | ;; position. Go to the start of the list and exit. |
| 4053 | (goto-char (1+ (elt state-2 1))) |
| 4054 | nil)) |
| 4055 | |
| 4056 | ((c-beginning-of-macro limit) |
| 4057 | ;; Inside a macro. |
| 4058 | (if (< (point) |
| 4059 | (or start-macro-beg |
| 4060 | (setq start-macro-beg |
| 4061 | (save-excursion |
| 4062 | (goto-char start) |
| 4063 | (c-beginning-of-macro limit) |
| 4064 | (point))))) |
| 4065 | t |
| 4066 | |
| 4067 | ;; It's inside the same macro we started in so it's |
| 4068 | ;; a relevant match. |
| 4069 | (goto-char pos) |
| 4070 | nil)))))) |
| 4071 | |
| 4072 | (> (point) |
| 4073 | (progn |
| 4074 | ;; Skip syntactic ws afterwards so that we don't stop at the |
| 4075 | ;; end of a comment if `skip-chars' is something like "^/". |
| 4076 | (c-backward-syntactic-ws) |
| 4077 | (point))))) |
| 4078 | |
| 4079 | ;; We might want to extend this with more useful return values in |
| 4080 | ;; the future. |
| 4081 | (/= (point) start))) |
| 4082 | |
| 4083 | ;; The following is an alternative implementation of |
| 4084 | ;; `c-syntactic-skip-backward' that uses backward movement to keep |
| 4085 | ;; track of the syntactic context. It turned out to be generally |
| 4086 | ;; slower than the one above which uses forward checks from earlier |
| 4087 | ;; safe positions. |
| 4088 | ;; |
| 4089 | ;;(defconst c-ssb-stop-re |
| 4090 | ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to |
| 4091 | ;; ;; stop at to avoid going into comments and literals. |
| 4092 | ;; (concat |
| 4093 | ;; ;; Match comment end syntax and string literal syntax. Also match |
| 4094 | ;; ;; '/' for block comment endings (not covered by comment end |
| 4095 | ;; ;; syntax). |
| 4096 | ;; "\\s>\\|/\\|\\s\"" |
| 4097 | ;; (if (memq 'gen-string-delim c-emacs-features) |
| 4098 | ;; "\\|\\s|" |
| 4099 | ;; "") |
| 4100 | ;; (if (memq 'gen-comment-delim c-emacs-features) |
| 4101 | ;; "\\|\\s!" |
| 4102 | ;; ""))) |
| 4103 | ;; |
| 4104 | ;;(defconst c-ssb-stop-paren-re |
| 4105 | ;; ;; Like `c-ssb-stop-re' but also stops at paren chars. |
| 4106 | ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)")) |
| 4107 | ;; |
| 4108 | ;;(defconst c-ssb-sexp-end-re |
| 4109 | ;; ;; Regexp matching the ending syntax of a complex sexp. |
| 4110 | ;; (concat c-string-limit-regexp "\\|\\s)")) |
| 4111 | ;; |
| 4112 | ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level) |
| 4113 | ;; "Like `skip-chars-backward' but only look at syntactically relevant chars, |
| 4114 | ;;i.e. don't stop at positions inside syntactic whitespace or string |
| 4115 | ;;literals. Preprocessor directives are also ignored. However, if the |
| 4116 | ;;point is within a comment, string literal or preprocessor directory to |
| 4117 | ;;begin with, its contents is treated as syntactically relevant chars. |
| 4118 | ;;If LIMIT is given, it limits the backward search and the point will be |
| 4119 | ;;left there if no earlier position is found. |
| 4120 | ;; |
| 4121 | ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren |
| 4122 | ;;sexps, and the search will also not go outside the current paren sexp. |
| 4123 | ;;However, if LIMIT or the buffer limit is reached inside a nested paren |
| 4124 | ;;then the point will be left at the limit. |
| 4125 | ;; |
| 4126 | ;;Non-nil is returned if the point moved, nil otherwise. |
| 4127 | ;; |
| 4128 | ;;Note that this function might do hidden buffer changes. See the |
| 4129 | ;;comment at the start of cc-engine.el for more info." |
| 4130 | ;; |
| 4131 | ;; (save-restriction |
| 4132 | ;; (when limit |
| 4133 | ;; (narrow-to-region limit (point-max))) |
| 4134 | ;; |
| 4135 | ;; (let ((start (point))) |
| 4136 | ;; (catch 'done |
| 4137 | ;; (while (let ((last-pos (point)) |
| 4138 | ;; (stop-pos (progn |
| 4139 | ;; (skip-chars-backward skip-chars) |
| 4140 | ;; (point)))) |
| 4141 | ;; |
| 4142 | ;; ;; Skip back over the same region as |
| 4143 | ;; ;; `skip-chars-backward' above, but keep to |
| 4144 | ;; ;; syntactically relevant positions. |
| 4145 | ;; (goto-char last-pos) |
| 4146 | ;; (while (and |
| 4147 | ;; ;; `re-search-backward' with a single char regexp |
| 4148 | ;; ;; should be fast. |
| 4149 | ;; (re-search-backward |
| 4150 | ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re) |
| 4151 | ;; stop-pos 'move) |
| 4152 | ;; |
| 4153 | ;; (progn |
| 4154 | ;; (cond |
| 4155 | ;; ((looking-at "\\s(") |
| 4156 | ;; ;; `paren-level' is set and we've found the |
| 4157 | ;; ;; start of the containing paren. |
| 4158 | ;; (forward-char) |
| 4159 | ;; (throw 'done t)) |
| 4160 | ;; |
| 4161 | ;; ((looking-at c-ssb-sexp-end-re) |
| 4162 | ;; ;; We're at the end of a string literal or paren |
| 4163 | ;; ;; sexp (if `paren-level' is set). |
| 4164 | ;; (forward-char) |
| 4165 | ;; (condition-case nil |
| 4166 | ;; (c-backward-sexp) |
| 4167 | ;; (error |
| 4168 | ;; (goto-char limit) |
| 4169 | ;; (throw 'done t)))) |
| 4170 | ;; |
| 4171 | ;; (t |
| 4172 | ;; (forward-char) |
| 4173 | ;; ;; At the end of some syntactic ws or possibly |
| 4174 | ;; ;; after a plain '/' operator. |
| 4175 | ;; (let ((pos (point))) |
| 4176 | ;; (c-backward-syntactic-ws) |
| 4177 | ;; (if (= pos (point)) |
| 4178 | ;; ;; Was a plain '/' operator. Go past it. |
| 4179 | ;; (backward-char))))) |
| 4180 | ;; |
| 4181 | ;; (> (point) stop-pos)))) |
| 4182 | ;; |
| 4183 | ;; ;; Now the point is either at `stop-pos' or at some |
| 4184 | ;; ;; position further back if `stop-pos' was at a |
| 4185 | ;; ;; syntactically irrelevant place. |
| 4186 | ;; |
| 4187 | ;; ;; Skip additional syntactic ws so that we don't stop |
| 4188 | ;; ;; at the end of a comment if `skip-chars' is |
| 4189 | ;; ;; something like "^/". |
| 4190 | ;; (c-backward-syntactic-ws) |
| 4191 | ;; |
| 4192 | ;; (< (point) stop-pos)))) |
| 4193 | ;; |
| 4194 | ;; ;; We might want to extend this with more useful return values |
| 4195 | ;; ;; in the future. |
| 4196 | ;; (/= (point) start)))) |
| 4197 | |
| 4198 | \f |
| 4199 | ;; Tools for handling comments and string literals. |
| 4200 | |
| 4201 | (defun c-in-literal (&optional lim detect-cpp) |
| 4202 | "Return the type of literal point is in, if any. |
| 4203 | The return value is `c' if in a C-style comment, `c++' if in a C++ |
| 4204 | style comment, `string' if in a string literal, `pound' if DETECT-CPP |
| 4205 | is non-nil and in a preprocessor line, or nil if somewhere else. |
| 4206 | Optional LIM is used as the backward limit of the search. If omitted, |
| 4207 | or nil, `c-beginning-of-defun' is used. |
| 4208 | |
| 4209 | The last point calculated is cached if the cache is enabled, i.e. if |
| 4210 | `c-in-literal-cache' is bound to a two element vector. |
| 4211 | |
| 4212 | Note that this function might do hidden buffer changes. See the |
| 4213 | comment at the start of cc-engine.el for more info." |
| 4214 | (let* ((safe-place (c-state-safe-place (point))) |
| 4215 | (lit (c-state-pp-to-literal safe-place (point)))) |
| 4216 | (or (cadr lit) |
| 4217 | (and detect-cpp |
| 4218 | (save-excursion (c-beginning-of-macro)) |
| 4219 | 'pound)))) |
| 4220 | |
| 4221 | (defun c-literal-limits (&optional lim near not-in-delimiter) |
| 4222 | "Return a cons of the beginning and end positions of the comment or |
| 4223 | string surrounding point (including both delimiters), or nil if point |
| 4224 | isn't in one. If LIM is non-nil, it's used as the \"safe\" position |
| 4225 | to start parsing from. If NEAR is non-nil, then the limits of any |
| 4226 | literal next to point is returned. \"Next to\" means there's only |
| 4227 | spaces and tabs between point and the literal. The search for such a |
| 4228 | literal is done first in forward direction. If NOT-IN-DELIMITER is |
| 4229 | non-nil, the case when point is inside a starting delimiter won't be |
| 4230 | recognized. This only has effect for comments which have starting |
| 4231 | delimiters with more than one character. |
| 4232 | |
| 4233 | Note that this function might do hidden buffer changes. See the |
| 4234 | comment at the start of cc-engine.el for more info." |
| 4235 | |
| 4236 | (save-excursion |
| 4237 | (let* ((pos (point)) |
| 4238 | (lim (or lim (c-state-safe-place pos))) |
| 4239 | (pp-to-lit (c-state-pp-to-literal lim pos)) |
| 4240 | (state (car pp-to-lit)) |
| 4241 | (lit-type (cadr pp-to-lit)) |
| 4242 | (lit-limits (car (cddr pp-to-lit)))) |
| 4243 | |
| 4244 | (cond |
| 4245 | (lit-limits) |
| 4246 | ((and (not not-in-delimiter) |
| 4247 | (not (elt state 5)) |
| 4248 | (eq (char-before) ?/) |
| 4249 | (looking-at "[/*]")) ; FIXME!!! use c-line/block-comment-starter. 2008-09-28. |
| 4250 | ;; We're standing in a comment starter. |
| 4251 | (backward-char 1) |
| 4252 | (cons (point) (progn (c-forward-single-comment) (point)))) |
| 4253 | |
| 4254 | (near |
| 4255 | (goto-char pos) |
| 4256 | ;; Search forward for a literal. |
| 4257 | (skip-chars-forward " \t") |
| 4258 | (cond |
| 4259 | ((looking-at c-string-limit-regexp) ; String. |
| 4260 | (cons (point) (or (c-safe (c-forward-sexp 1) (point)) |
| 4261 | (point-max)))) |
| 4262 | |
| 4263 | ((looking-at c-comment-start-regexp) ; Line or block comment. |
| 4264 | (cons (point) (progn (c-forward-single-comment) (point)))) |
| 4265 | |
| 4266 | (t |
| 4267 | ;; Search backward. |
| 4268 | (skip-chars-backward " \t") |
| 4269 | |
| 4270 | (let ((end (point)) beg) |
| 4271 | (cond |
| 4272 | ((save-excursion |
| 4273 | (< (skip-syntax-backward c-string-syntax) 0)) ; String. |
| 4274 | (setq beg (c-safe (c-backward-sexp 1) (point)))) |
| 4275 | |
| 4276 | ((and (c-safe (forward-char -2) t) |
| 4277 | (looking-at "*/")) |
| 4278 | ;; Block comment. Due to the nature of line |
| 4279 | ;; comments, they will always be covered by the |
| 4280 | ;; normal case above. |
| 4281 | (goto-char end) |
| 4282 | (c-backward-single-comment) |
| 4283 | ;; If LIM is bogus, beg will be bogus. |
| 4284 | (setq beg (point)))) |
| 4285 | |
| 4286 | (if beg (cons beg end)))))) |
| 4287 | )))) |
| 4288 | |
| 4289 | ;; In case external callers use this; it did have a docstring. |
| 4290 | (defalias 'c-literal-limits-fast 'c-literal-limits) |
| 4291 | |
| 4292 | (defun c-collect-line-comments (range) |
| 4293 | "If the argument is a cons of two buffer positions (such as returned by |
| 4294 | `c-literal-limits'), and that range contains a C++ style line comment, |
| 4295 | then an extended range is returned that contains all adjacent line |
| 4296 | comments (i.e. all comments that starts in the same column with no |
| 4297 | empty lines or non-whitespace characters between them). Otherwise the |
| 4298 | argument is returned. |
| 4299 | |
| 4300 | Note that this function might do hidden buffer changes. See the |
| 4301 | comment at the start of cc-engine.el for more info." |
| 4302 | |
| 4303 | (save-excursion |
| 4304 | (condition-case nil |
| 4305 | (if (and (consp range) (progn |
| 4306 | (goto-char (car range)) |
| 4307 | (looking-at c-line-comment-starter))) |
| 4308 | (let ((col (current-column)) |
| 4309 | (beg (point)) |
| 4310 | (bopl (c-point 'bopl)) |
| 4311 | (end (cdr range))) |
| 4312 | ;; Got to take care in the backward direction to handle |
| 4313 | ;; comments which are preceded by code. |
| 4314 | (while (and (c-backward-single-comment) |
| 4315 | (>= (point) bopl) |
| 4316 | (looking-at c-line-comment-starter) |
| 4317 | (= col (current-column))) |
| 4318 | (setq beg (point) |
| 4319 | bopl (c-point 'bopl))) |
| 4320 | (goto-char end) |
| 4321 | (while (and (progn (skip-chars-forward " \t") |
| 4322 | (looking-at c-line-comment-starter)) |
| 4323 | (= col (current-column)) |
| 4324 | (prog1 (zerop (forward-line 1)) |
| 4325 | (setq end (point))))) |
| 4326 | (cons beg end)) |
| 4327 | range) |
| 4328 | (error range)))) |
| 4329 | |
| 4330 | (defun c-literal-type (range) |
| 4331 | "Convenience function that given the result of `c-literal-limits', |
| 4332 | returns nil or the type of literal that the range surrounds, one |
| 4333 | of the symbols 'c, 'c++ or 'string. It's much faster than using |
| 4334 | `c-in-literal' and is intended to be used when you need both the |
| 4335 | type of a literal and its limits. |
| 4336 | |
| 4337 | Note that this function might do hidden buffer changes. See the |
| 4338 | comment at the start of cc-engine.el for more info." |
| 4339 | |
| 4340 | (if (consp range) |
| 4341 | (save-excursion |
| 4342 | (goto-char (car range)) |
| 4343 | (cond ((looking-at c-string-limit-regexp) 'string) |
| 4344 | ((or (looking-at "//") ; c++ line comment |
| 4345 | (and (looking-at "\\s<") ; comment starter |
| 4346 | (looking-at "#"))) ; awk comment. |
| 4347 | 'c++) |
| 4348 | (t 'c))) ; Assuming the range is valid. |
| 4349 | range)) |
| 4350 | |
| 4351 | \f |
| 4352 | ;; `c-find-decl-spots' and accompanying stuff. |
| 4353 | |
| 4354 | ;; Variables used in `c-find-decl-spots' to cache the search done for |
| 4355 | ;; the first declaration in the last call. When that function starts, |
| 4356 | ;; it needs to back up over syntactic whitespace to look at the last |
| 4357 | ;; token before the region being searched. That can sometimes cause |
| 4358 | ;; moves back and forth over a quite large region of comments and |
| 4359 | ;; macros, which would be repeated for each changed character when |
| 4360 | ;; we're called during fontification, since font-lock refontifies the |
| 4361 | ;; current line for each change. Thus it's worthwhile to cache the |
| 4362 | ;; first match. |
| 4363 | ;; |
| 4364 | ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in |
| 4365 | ;; the syntactic whitespace less or equal to some start position. |
| 4366 | ;; There's no cached value if it's nil. |
| 4367 | ;; |
| 4368 | ;; `c-find-decl-match-pos' is the match position if |
| 4369 | ;; `c-find-decl-prefix-search' matched before the syntactic whitespace |
| 4370 | ;; at `c-find-decl-syntactic-pos', or nil if there's no such match. |
| 4371 | (defvar c-find-decl-syntactic-pos nil) |
| 4372 | (make-variable-buffer-local 'c-find-decl-syntactic-pos) |
| 4373 | (defvar c-find-decl-match-pos nil) |
| 4374 | (make-variable-buffer-local 'c-find-decl-match-pos) |
| 4375 | |
| 4376 | (defsubst c-invalidate-find-decl-cache (change-min-pos) |
| 4377 | (and c-find-decl-syntactic-pos |
| 4378 | (< change-min-pos c-find-decl-syntactic-pos) |
| 4379 | (setq c-find-decl-syntactic-pos nil))) |
| 4380 | |
| 4381 | ; (defface c-debug-decl-spot-face |
| 4382 | ; '((t (:background "Turquoise"))) |
| 4383 | ; "Debug face to mark the spots where `c-find-decl-spots' stopped.") |
| 4384 | ; (defface c-debug-decl-sws-face |
| 4385 | ; '((t (:background "Khaki"))) |
| 4386 | ; "Debug face to mark the syntactic whitespace between the declaration |
| 4387 | ; spots and the preceding token end.") |
| 4388 | |
| 4389 | (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos) |
| 4390 | (when (facep 'c-debug-decl-spot-face) |
| 4391 | `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos)) |
| 4392 | (c-debug-add-face (max match-pos (point-min)) decl-pos |
| 4393 | 'c-debug-decl-sws-face) |
| 4394 | (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max)) |
| 4395 | 'c-debug-decl-spot-face)))) |
| 4396 | (defmacro c-debug-remove-decl-spot-faces (beg end) |
| 4397 | (when (facep 'c-debug-decl-spot-face) |
| 4398 | `(c-save-buffer-state () |
| 4399 | (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face) |
| 4400 | (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face)))) |
| 4401 | |
| 4402 | (defmacro c-find-decl-prefix-search () |
| 4403 | ;; Macro used inside `c-find-decl-spots'. It ought to be a defun, |
| 4404 | ;; but it contains lots of free variables that refer to things |
| 4405 | ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos' |
| 4406 | ;; if there is a match, otherwise at `cfd-limit'. |
| 4407 | ;; |
| 4408 | ;; This macro might do hidden buffer changes. |
| 4409 | |
| 4410 | '(progn |
| 4411 | ;; Find the next property match position if we haven't got one already. |
| 4412 | (unless cfd-prop-match |
| 4413 | (save-excursion |
| 4414 | (while (progn |
| 4415 | (goto-char (next-single-property-change |
| 4416 | (point) 'c-type nil cfd-limit)) |
| 4417 | (and (< (point) cfd-limit) |
| 4418 | (not (eq (c-get-char-property (1- (point)) 'c-type) |
| 4419 | 'c-decl-end))))) |
| 4420 | (setq cfd-prop-match (point)))) |
| 4421 | |
| 4422 | ;; Find the next `c-decl-prefix-or-start-re' match if we haven't |
| 4423 | ;; got one already. |
| 4424 | (unless cfd-re-match |
| 4425 | |
| 4426 | (if (> cfd-re-match-end (point)) |
| 4427 | (goto-char cfd-re-match-end)) |
| 4428 | |
| 4429 | (while (if (setq cfd-re-match-end |
| 4430 | (re-search-forward c-decl-prefix-or-start-re |
| 4431 | cfd-limit 'move)) |
| 4432 | |
| 4433 | ;; Match. Check if it's inside a comment or string literal. |
| 4434 | (c-got-face-at |
| 4435 | (if (setq cfd-re-match (match-end 1)) |
| 4436 | ;; Matched the end of a token preceding a decl spot. |
| 4437 | (progn |
| 4438 | (goto-char cfd-re-match) |
| 4439 | (1- cfd-re-match)) |
| 4440 | ;; Matched a token that start a decl spot. |
| 4441 | (goto-char (match-beginning 0)) |
| 4442 | (point)) |
| 4443 | c-literal-faces) |
| 4444 | |
| 4445 | ;; No match. Finish up and exit the loop. |
| 4446 | (setq cfd-re-match cfd-limit) |
| 4447 | nil) |
| 4448 | |
| 4449 | ;; Skip out of comments and string literals. |
| 4450 | (while (progn |
| 4451 | (goto-char (next-single-property-change |
| 4452 | (point) 'face nil cfd-limit)) |
| 4453 | (and (< (point) cfd-limit) |
| 4454 | (c-got-face-at (point) c-literal-faces))))) |
| 4455 | |
| 4456 | ;; If we matched at the decl start, we have to back up over the |
| 4457 | ;; preceding syntactic ws to set `cfd-match-pos' and to catch |
| 4458 | ;; any decl spots in the syntactic ws. |
| 4459 | (unless cfd-re-match |
| 4460 | (c-backward-syntactic-ws) |
| 4461 | (setq cfd-re-match (point)))) |
| 4462 | |
| 4463 | ;; Choose whichever match is closer to the start. |
| 4464 | (if (< cfd-re-match cfd-prop-match) |
| 4465 | (setq cfd-match-pos cfd-re-match |
| 4466 | cfd-re-match nil) |
| 4467 | (setq cfd-match-pos cfd-prop-match |
| 4468 | cfd-prop-match nil)) |
| 4469 | |
| 4470 | (goto-char cfd-match-pos) |
| 4471 | |
| 4472 | (when (< cfd-match-pos cfd-limit) |
| 4473 | ;; Skip forward past comments only so we don't skip macros. |
| 4474 | (c-forward-comments) |
| 4475 | ;; Set the position to continue at. We can avoid going over |
| 4476 | ;; the comments skipped above a second time, but it's possible |
| 4477 | ;; that the comment skipping has taken us past `cfd-prop-match' |
| 4478 | ;; since the property might be used inside comments. |
| 4479 | (setq cfd-continue-pos (if cfd-prop-match |
| 4480 | (min cfd-prop-match (point)) |
| 4481 | (point)))))) |
| 4482 | |
| 4483 | (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun) |
| 4484 | ;; Call CFD-FUN for each possible spot for a declaration, cast or |
| 4485 | ;; label from the point to CFD-LIMIT. |
| 4486 | ;; |
| 4487 | ;; CFD-FUN is called with point at the start of the spot. It's |
| 4488 | ;; passed two arguments: The first is the end position of the token |
| 4489 | ;; preceding the spot, or 0 for the implicit match at bob. The |
| 4490 | ;; second is a flag that is t when the match is inside a macro. If |
| 4491 | ;; CFD-FUN adds `c-decl-end' properties somewhere below the current |
| 4492 | ;; spot, it should return non-nil to ensure that the next search |
| 4493 | ;; will find them. |
| 4494 | ;; |
| 4495 | ;; Such a spot is: |
| 4496 | ;; o The first token after bob. |
| 4497 | ;; o The first token after the end of submatch 1 in |
| 4498 | ;; `c-decl-prefix-or-start-re' when that submatch matches. |
| 4499 | ;; o The start of each `c-decl-prefix-or-start-re' match when |
| 4500 | ;; submatch 1 doesn't match. |
| 4501 | ;; o The first token after the end of each occurrence of the |
| 4502 | ;; `c-type' text property with the value `c-decl-end', provided |
| 4503 | ;; `c-type-decl-end-used' is set. |
| 4504 | ;; |
| 4505 | ;; Only a spot that match CFD-DECL-RE and whose face is in the |
| 4506 | ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face |
| 4507 | ;; check is disabled if CFD-FACE-CHECKLIST is nil. |
| 4508 | ;; |
| 4509 | ;; If the match is inside a macro then the buffer is narrowed to the |
| 4510 | ;; end of it, so that CFD-FUN can investigate the following tokens |
| 4511 | ;; without matching something that begins inside a macro and ends |
| 4512 | ;; outside it. It's to avoid this work that the CFD-DECL-RE and |
| 4513 | ;; CFD-FACE-CHECKLIST checks exist. |
| 4514 | ;; |
| 4515 | ;; The spots are visited approximately in order from top to bottom. |
| 4516 | ;; It's however the positions where `c-decl-prefix-or-start-re' |
| 4517 | ;; matches and where `c-decl-end' properties are found that are in |
| 4518 | ;; order. Since the spots often are at the following token, they |
| 4519 | ;; might be visited out of order insofar as more spots are reported |
| 4520 | ;; later on within the syntactic whitespace between the match |
| 4521 | ;; positions and their spots. |
| 4522 | ;; |
| 4523 | ;; It's assumed that comments and strings are fontified in the |
| 4524 | ;; searched range. |
| 4525 | ;; |
| 4526 | ;; This is mainly used in fontification, and so has an elaborate |
| 4527 | ;; cache to handle repeated calls from the same start position; see |
| 4528 | ;; the variables above. |
| 4529 | ;; |
| 4530 | ;; All variables in this function begin with `cfd-' to avoid name |
| 4531 | ;; collision with the (dynamically bound) variables used in CFD-FUN. |
| 4532 | ;; |
| 4533 | ;; This function might do hidden buffer changes. |
| 4534 | |
| 4535 | (let ((cfd-start-pos (point)) |
| 4536 | (cfd-buffer-end (point-max)) |
| 4537 | ;; The end of the token preceding the decl spot last found |
| 4538 | ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's |
| 4539 | ;; no match. |
| 4540 | cfd-re-match |
| 4541 | ;; The end position of the last `c-decl-prefix-or-start-re' |
| 4542 | ;; match. If this is greater than `cfd-continue-pos', the |
| 4543 | ;; next regexp search is started here instead. |
| 4544 | (cfd-re-match-end (point-min)) |
| 4545 | ;; The end of the last `c-decl-end' found by |
| 4546 | ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no |
| 4547 | ;; match. If searching for the property isn't needed then we |
| 4548 | ;; disable it by setting it to `cfd-limit' directly. |
| 4549 | (cfd-prop-match (unless c-type-decl-end-used cfd-limit)) |
| 4550 | ;; The end of the token preceding the decl spot last found by |
| 4551 | ;; `c-find-decl-prefix-search'. 0 for the implicit match at |
| 4552 | ;; bob. `cfd-limit' if there's no match. In other words, |
| 4553 | ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'. |
| 4554 | (cfd-match-pos cfd-limit) |
| 4555 | ;; The position to continue searching at. |
| 4556 | cfd-continue-pos |
| 4557 | ;; The position of the last "real" token we've stopped at. |
| 4558 | ;; This can be greater than `cfd-continue-pos' when we get |
| 4559 | ;; hits inside macros or at `c-decl-end' positions inside |
| 4560 | ;; comments. |
| 4561 | (cfd-token-pos 0) |
| 4562 | ;; The end position of the last entered macro. |
| 4563 | (cfd-macro-end 0)) |
| 4564 | |
| 4565 | ;; Initialize by finding a syntactically relevant start position |
| 4566 | ;; before the point, and do the first `c-decl-prefix-or-start-re' |
| 4567 | ;; search unless we're at bob. |
| 4568 | |
| 4569 | (let (start-in-literal start-in-macro syntactic-pos) |
| 4570 | ;; Must back up a bit since we look for the end of the previous |
| 4571 | ;; statement or declaration, which is earlier than the first |
| 4572 | ;; returned match. |
| 4573 | |
| 4574 | (cond |
| 4575 | ;; First we need to move to a syntactically relevant position. |
| 4576 | ;; Begin by backing out of comment or string literals. |
| 4577 | ((and |
| 4578 | (when (c-got-face-at (point) c-literal-faces) |
| 4579 | ;; Try to use the faces to back up to the start of the |
| 4580 | ;; literal. FIXME: What if the point is on a declaration |
| 4581 | ;; inside a comment? |
| 4582 | (while (and (not (bobp)) |
| 4583 | (c-got-face-at (1- (point)) c-literal-faces)) |
| 4584 | (goto-char (previous-single-property-change |
| 4585 | (point) 'face nil (point-min)))) |
| 4586 | |
| 4587 | ;; XEmacs doesn't fontify the quotes surrounding string |
| 4588 | ;; literals. |
| 4589 | (and (featurep 'xemacs) |
| 4590 | (eq (get-text-property (point) 'face) |
| 4591 | 'font-lock-string-face) |
| 4592 | (not (bobp)) |
| 4593 | (progn (backward-char) |
| 4594 | (not (looking-at c-string-limit-regexp))) |
| 4595 | (forward-char)) |
| 4596 | |
| 4597 | ;; Don't trust the literal to contain only literal faces |
| 4598 | ;; (the font lock package might not have fontified the |
| 4599 | ;; start of it at all, for instance) so check that we have |
| 4600 | ;; arrived at something that looks like a start or else |
| 4601 | ;; resort to `c-literal-limits'. |
| 4602 | (unless (looking-at c-literal-start-regexp) |
| 4603 | (let ((range (c-literal-limits))) |
| 4604 | (if range (goto-char (car range))))) |
| 4605 | |
| 4606 | (setq start-in-literal (point))) |
| 4607 | |
| 4608 | ;; The start is in a literal. If the limit is in the same |
| 4609 | ;; one we don't have to find a syntactic position etc. We |
| 4610 | ;; only check that if the limit is at or before bonl to save |
| 4611 | ;; time; it covers the by far most common case when font-lock |
| 4612 | ;; refontifies the current line only. |
| 4613 | (<= cfd-limit (c-point 'bonl cfd-start-pos)) |
| 4614 | (save-excursion |
| 4615 | (goto-char cfd-start-pos) |
| 4616 | (while (progn |
| 4617 | (goto-char (next-single-property-change |
| 4618 | (point) 'face nil cfd-limit)) |
| 4619 | (and (< (point) cfd-limit) |
| 4620 | (c-got-face-at (point) c-literal-faces)))) |
| 4621 | (= (point) cfd-limit))) |
| 4622 | |
| 4623 | ;; Completely inside a literal. Set up variables to trig the |
| 4624 | ;; (< cfd-continue-pos cfd-start-pos) case below and it'll |
| 4625 | ;; find a suitable start position. |
| 4626 | (setq cfd-continue-pos start-in-literal)) |
| 4627 | |
| 4628 | ;; Check if the region might be completely inside a macro, to |
| 4629 | ;; optimize that like the completely-inside-literal above. |
| 4630 | ((save-excursion |
| 4631 | (and (= (forward-line 1) 0) |
| 4632 | (bolp) ; forward-line has funny behavior at eob. |
| 4633 | (>= (point) cfd-limit) |
| 4634 | (progn (backward-char) |
| 4635 | (eq (char-before) ?\\)))) |
| 4636 | ;; (Maybe) completely inside a macro. Only need to trig the |
| 4637 | ;; (< cfd-continue-pos cfd-start-pos) case below to make it |
| 4638 | ;; set things up. |
| 4639 | (setq cfd-continue-pos (1- cfd-start-pos) |
| 4640 | start-in-macro t)) |
| 4641 | |
| 4642 | (t |
| 4643 | ;; Back out of any macro so we don't miss any declaration |
| 4644 | ;; that could follow after it. |
| 4645 | (when (c-beginning-of-macro) |
| 4646 | (setq start-in-macro t)) |
| 4647 | |
| 4648 | ;; Now we're at a proper syntactically relevant position so we |
| 4649 | ;; can use the cache. But first clear it if it applied |
| 4650 | ;; further down. |
| 4651 | (c-invalidate-find-decl-cache cfd-start-pos) |
| 4652 | |
| 4653 | (setq syntactic-pos (point)) |
| 4654 | (unless (eq syntactic-pos c-find-decl-syntactic-pos) |
| 4655 | ;; Don't have to do this if the cache is relevant here, |
| 4656 | ;; typically if the same line is refontified again. If |
| 4657 | ;; we're just some syntactic whitespace further down we can |
| 4658 | ;; still use the cache to limit the skipping. |
| 4659 | (c-backward-syntactic-ws c-find-decl-syntactic-pos)) |
| 4660 | |
| 4661 | ;; If we hit `c-find-decl-syntactic-pos' and |
| 4662 | ;; `c-find-decl-match-pos' is set then we install the cached |
| 4663 | ;; values. If we hit `c-find-decl-syntactic-pos' and |
| 4664 | ;; `c-find-decl-match-pos' is nil then we know there's no decl |
| 4665 | ;; prefix in the whitespace before `c-find-decl-syntactic-pos' |
| 4666 | ;; and so we can continue the search from this point. If we |
| 4667 | ;; didn't hit `c-find-decl-syntactic-pos' then we're now in |
| 4668 | ;; the right spot to begin searching anyway. |
| 4669 | (if (and (eq (point) c-find-decl-syntactic-pos) |
| 4670 | c-find-decl-match-pos) |
| 4671 | (setq cfd-match-pos c-find-decl-match-pos |
| 4672 | cfd-continue-pos syntactic-pos) |
| 4673 | |
| 4674 | (setq c-find-decl-syntactic-pos syntactic-pos) |
| 4675 | |
| 4676 | (when (if (bobp) |
| 4677 | ;; Always consider bob a match to get the first |
| 4678 | ;; declaration in the file. Do this separately instead of |
| 4679 | ;; letting `c-decl-prefix-or-start-re' match bob, so that |
| 4680 | ;; regexp always can consume at least one character to |
| 4681 | ;; ensure that we won't get stuck in an infinite loop. |
| 4682 | (setq cfd-re-match 0) |
| 4683 | (backward-char) |
| 4684 | (c-beginning-of-current-token) |
| 4685 | (< (point) cfd-limit)) |
| 4686 | ;; Do an initial search now. In the bob case above it's |
| 4687 | ;; only done to search for a `c-decl-end' spot. |
| 4688 | (c-find-decl-prefix-search)) |
| 4689 | |
| 4690 | (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos) |
| 4691 | cfd-match-pos))))) |
| 4692 | |
| 4693 | ;; Advance `cfd-continue-pos' if it's before the start position. |
| 4694 | ;; The closest continue position that might have effect at or |
| 4695 | ;; after the start depends on what we started in. This also |
| 4696 | ;; finds a suitable start position in the special cases when the |
| 4697 | ;; region is completely within a literal or macro. |
| 4698 | (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos)) |
| 4699 | |
| 4700 | (cond |
| 4701 | (start-in-macro |
| 4702 | ;; If we're in a macro then it's the closest preceding token |
| 4703 | ;; in the macro. Check this before `start-in-literal', |
| 4704 | ;; since if we're inside a literal in a macro, the preceding |
| 4705 | ;; token is earlier than any `c-decl-end' spot inside the |
| 4706 | ;; literal (comment). |
| 4707 | (goto-char (or start-in-literal cfd-start-pos)) |
| 4708 | ;; The only syntactic ws in macros are comments. |
| 4709 | (c-backward-comments) |
| 4710 | (backward-char) |
| 4711 | (c-beginning-of-current-token)) |
| 4712 | |
| 4713 | (start-in-literal |
| 4714 | ;; If we're in a comment it can only be the closest |
| 4715 | ;; preceding `c-decl-end' position within that comment, if |
| 4716 | ;; any. Go back to the beginning of such a property so that |
| 4717 | ;; `c-find-decl-prefix-search' will find the end of it. |
| 4718 | ;; (Can't stop at the end and install it directly on |
| 4719 | ;; `cfd-prop-match' since that variable might be cleared |
| 4720 | ;; after `cfd-fun' below.) |
| 4721 | ;; |
| 4722 | ;; Note that if the literal is a string then the property |
| 4723 | ;; search will simply skip to the beginning of it right |
| 4724 | ;; away. |
| 4725 | (if (not c-type-decl-end-used) |
| 4726 | (goto-char start-in-literal) |
| 4727 | (goto-char cfd-start-pos) |
| 4728 | (while (progn |
| 4729 | (goto-char (previous-single-property-change |
| 4730 | (point) 'c-type nil start-in-literal)) |
| 4731 | (and (> (point) start-in-literal) |
| 4732 | (not (eq (c-get-char-property (point) 'c-type) |
| 4733 | 'c-decl-end)))))) |
| 4734 | |
| 4735 | (when (= (point) start-in-literal) |
| 4736 | ;; Didn't find any property inside the comment, so we can |
| 4737 | ;; skip it entirely. (This won't skip past a string, but |
| 4738 | ;; that'll be handled quickly by the next |
| 4739 | ;; `c-find-decl-prefix-search' anyway.) |
| 4740 | (c-forward-single-comment) |
| 4741 | (if (> (point) cfd-limit) |
| 4742 | (goto-char cfd-limit)))) |
| 4743 | |
| 4744 | (t |
| 4745 | ;; If we started in normal code, the only match that might |
| 4746 | ;; apply before the start is what we already got in |
| 4747 | ;; `cfd-match-pos' so we can continue at the start position. |
| 4748 | ;; (Note that we don't get here if the first match is below |
| 4749 | ;; it.) |
| 4750 | (goto-char cfd-start-pos))) |
| 4751 | |
| 4752 | ;; Delete found matches if they are before our new continue |
| 4753 | ;; position, so that `c-find-decl-prefix-search' won't back up |
| 4754 | ;; to them later on. |
| 4755 | (setq cfd-continue-pos (point)) |
| 4756 | (when (and cfd-re-match (< cfd-re-match cfd-continue-pos)) |
| 4757 | (setq cfd-re-match nil)) |
| 4758 | (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos)) |
| 4759 | (setq cfd-prop-match nil))) |
| 4760 | |
| 4761 | (if syntactic-pos |
| 4762 | ;; This is the normal case and we got a proper syntactic |
| 4763 | ;; position. If there's a match then it's always outside |
| 4764 | ;; macros and comments, so advance to the next token and set |
| 4765 | ;; `cfd-token-pos'. The loop below will later go back using |
| 4766 | ;; `cfd-continue-pos' to fix declarations inside the |
| 4767 | ;; syntactic ws. |
| 4768 | (when (and cfd-match-pos (< cfd-match-pos syntactic-pos)) |
| 4769 | (goto-char syntactic-pos) |
| 4770 | (c-forward-syntactic-ws) |
| 4771 | (and cfd-continue-pos |
| 4772 | (< cfd-continue-pos (point)) |
| 4773 | (setq cfd-token-pos (point)))) |
| 4774 | |
| 4775 | ;; Have one of the special cases when the region is completely |
| 4776 | ;; within a literal or macro. `cfd-continue-pos' is set to a |
| 4777 | ;; good start position for the search, so do it. |
| 4778 | (c-find-decl-prefix-search))) |
| 4779 | |
| 4780 | ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match. |
| 4781 | |
| 4782 | (while (progn |
| 4783 | (while (and |
| 4784 | (< cfd-match-pos cfd-limit) |
| 4785 | |
| 4786 | (or |
| 4787 | ;; Kludge to filter out matches on the "<" that |
| 4788 | ;; aren't open parens, for the sake of languages |
| 4789 | ;; that got `c-recognize-<>-arglists' set. |
| 4790 | (and (eq (char-before cfd-match-pos) ?<) |
| 4791 | (not (c-get-char-property (1- cfd-match-pos) |
| 4792 | 'syntax-table))) |
| 4793 | |
| 4794 | ;; If `cfd-continue-pos' is less or equal to |
| 4795 | ;; `cfd-token-pos', we've got a hit inside a macro |
| 4796 | ;; that's in the syntactic whitespace before the last |
| 4797 | ;; "real" declaration we've checked. If they're equal |
| 4798 | ;; we've arrived at the declaration a second time, so |
| 4799 | ;; there's nothing to do. |
| 4800 | (= cfd-continue-pos cfd-token-pos) |
| 4801 | |
| 4802 | (progn |
| 4803 | ;; If `cfd-continue-pos' is less than `cfd-token-pos' |
| 4804 | ;; we're still searching for declarations embedded in |
| 4805 | ;; the syntactic whitespace. In that case we need |
| 4806 | ;; only to skip comments and not macros, since they |
| 4807 | ;; can't be nested, and that's already been done in |
| 4808 | ;; `c-find-decl-prefix-search'. |
| 4809 | (when (> cfd-continue-pos cfd-token-pos) |
| 4810 | (c-forward-syntactic-ws) |
| 4811 | (setq cfd-token-pos (point))) |
| 4812 | |
| 4813 | ;; Continue if the following token fails the |
| 4814 | ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks. |
| 4815 | (when (or (>= (point) cfd-limit) |
| 4816 | (not (looking-at cfd-decl-re)) |
| 4817 | (and cfd-face-checklist |
| 4818 | (not (c-got-face-at |
| 4819 | (point) cfd-face-checklist)))) |
| 4820 | (goto-char cfd-continue-pos) |
| 4821 | t))) |
| 4822 | |
| 4823 | (< (point) cfd-limit)) |
| 4824 | (c-find-decl-prefix-search)) |
| 4825 | |
| 4826 | (< (point) cfd-limit)) |
| 4827 | |
| 4828 | (when (and |
| 4829 | (>= (point) cfd-start-pos) |
| 4830 | |
| 4831 | (progn |
| 4832 | ;; Narrow to the end of the macro if we got a hit inside |
| 4833 | ;; one, to avoid recognizing things that start inside the |
| 4834 | ;; macro and end outside it. |
| 4835 | (when (> cfd-match-pos cfd-macro-end) |
| 4836 | ;; Not in the same macro as in the previous round. |
| 4837 | (save-excursion |
| 4838 | (goto-char cfd-match-pos) |
| 4839 | (setq cfd-macro-end |
| 4840 | (if (save-excursion (and (c-beginning-of-macro) |
| 4841 | (< (point) cfd-match-pos))) |
| 4842 | (progn (c-end-of-macro) |
| 4843 | (point)) |
| 4844 | 0)))) |
| 4845 | |
| 4846 | (if (zerop cfd-macro-end) |
| 4847 | t |
| 4848 | (if (> cfd-macro-end (point)) |
| 4849 | (progn (narrow-to-region (point-min) cfd-macro-end) |
| 4850 | t) |
| 4851 | ;; The matched token was the last thing in the macro, |
| 4852 | ;; so the whole match is bogus. |
| 4853 | (setq cfd-macro-end 0) |
| 4854 | nil)))) |
| 4855 | |
| 4856 | (c-debug-put-decl-spot-faces cfd-match-pos (point)) |
| 4857 | (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0)) |
| 4858 | (setq cfd-prop-match nil)) |
| 4859 | |
| 4860 | (when (/= cfd-macro-end 0) |
| 4861 | ;; Restore limits if we did macro narrowing above. |
| 4862 | (narrow-to-region (point-min) cfd-buffer-end))) |
| 4863 | |
| 4864 | (goto-char cfd-continue-pos) |
| 4865 | (if (= cfd-continue-pos cfd-limit) |
| 4866 | (setq cfd-match-pos cfd-limit) |
| 4867 | (c-find-decl-prefix-search))))) |
| 4868 | |
| 4869 | \f |
| 4870 | ;; A cache for found types. |
| 4871 | |
| 4872 | ;; Buffer local variable that contains an obarray with the types we've |
| 4873 | ;; found. If a declaration is recognized somewhere we record the |
| 4874 | ;; fully qualified identifier in it to recognize it as a type |
| 4875 | ;; elsewhere in the file too. This is not accurate since we do not |
| 4876 | ;; bother with the scoping rules of the languages, but in practice the |
| 4877 | ;; same name is seldom used as both a type and something else in a |
| 4878 | ;; file, and we only use this as a last resort in ambiguous cases (see |
| 4879 | ;; `c-forward-decl-or-cast-1'). |
| 4880 | ;; |
| 4881 | ;; Not every type need be in this cache. However, things which have |
| 4882 | ;; ceased to be types must be removed from it. |
| 4883 | ;; |
| 4884 | ;; Template types in C++ are added here too but with the template |
| 4885 | ;; arglist replaced with "<>" in references or "<" for the one in the |
| 4886 | ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as |
| 4887 | ;; "Foo<>::Bar<". This avoids storing very long strings (since C++ |
| 4888 | ;; template specs can be fairly sized programs in themselves) and |
| 4889 | ;; improves the hit ratio (it's a type regardless of the template |
| 4890 | ;; args; it's just not the same type, but we're only interested in |
| 4891 | ;; recognizing types, not telling distinct types apart). Note that |
| 4892 | ;; template types in references are added here too; from the example |
| 4893 | ;; above there will also be an entry "Foo<". |
| 4894 | (defvar c-found-types nil) |
| 4895 | (make-variable-buffer-local 'c-found-types) |
| 4896 | |
| 4897 | (defsubst c-clear-found-types () |
| 4898 | ;; Clears `c-found-types'. |
| 4899 | (setq c-found-types (make-vector 53 0))) |
| 4900 | |
| 4901 | (defun c-add-type (from to) |
| 4902 | ;; Add the given region as a type in `c-found-types'. If the region |
| 4903 | ;; doesn't match an existing type but there is a type which is equal |
| 4904 | ;; to the given one except that the last character is missing, then |
| 4905 | ;; the shorter type is removed. That's done to avoid adding all |
| 4906 | ;; prefixes of a type as it's being entered and font locked. This |
| 4907 | ;; doesn't cover cases like when characters are removed from a type |
| 4908 | ;; or added in the middle. We'd need the position of point when the |
| 4909 | ;; font locking is invoked to solve this well. |
| 4910 | ;; |
| 4911 | ;; This function might do hidden buffer changes. |
| 4912 | (let ((type (c-syntactic-content from to c-recognize-<>-arglists))) |
| 4913 | (unless (intern-soft type c-found-types) |
| 4914 | (unintern (substring type 0 -1) c-found-types) |
| 4915 | (intern type c-found-types)))) |
| 4916 | |
| 4917 | (defun c-unfind-type (name) |
| 4918 | ;; Remove the "NAME" from c-found-types, if present. |
| 4919 | (unintern name c-found-types)) |
| 4920 | |
| 4921 | (defsubst c-check-type (from to) |
| 4922 | ;; Return non-nil if the given region contains a type in |
| 4923 | ;; `c-found-types'. |
| 4924 | ;; |
| 4925 | ;; This function might do hidden buffer changes. |
| 4926 | (intern-soft (c-syntactic-content from to c-recognize-<>-arglists) |
| 4927 | c-found-types)) |
| 4928 | |
| 4929 | (defun c-list-found-types () |
| 4930 | ;; Return all the types in `c-found-types' as a sorted list of |
| 4931 | ;; strings. |
| 4932 | (let (type-list) |
| 4933 | (mapatoms (lambda (type) |
| 4934 | (setq type-list (cons (symbol-name type) |
| 4935 | type-list))) |
| 4936 | c-found-types) |
| 4937 | (sort type-list 'string-lessp))) |
| 4938 | |
| 4939 | ;; Shut up the byte compiler. |
| 4940 | (defvar c-maybe-stale-found-type) |
| 4941 | |
| 4942 | (defun c-trim-found-types (beg end old-len) |
| 4943 | ;; An after change function which, in conjunction with the info in |
| 4944 | ;; c-maybe-stale-found-type (set in c-before-change), removes a type |
| 4945 | ;; from `c-found-types', should this type have become stale. For |
| 4946 | ;; example, this happens to "foo" when "foo \n bar();" becomes |
| 4947 | ;; "foo(); \n bar();". Such stale types, if not removed, foul up |
| 4948 | ;; the fontification. |
| 4949 | ;; |
| 4950 | ;; Have we, perhaps, added non-ws characters to the front/back of a found |
| 4951 | ;; type? |
| 4952 | (when (> end beg) |
| 4953 | (save-excursion |
| 4954 | (when (< end (point-max)) |
| 4955 | (goto-char end) |
| 4956 | (if (and (c-beginning-of-current-token) ; only moves when we started in the middle |
| 4957 | (progn (goto-char end) |
| 4958 | (c-end-of-current-token))) |
| 4959 | (c-unfind-type (buffer-substring-no-properties |
| 4960 | end (point))))) |
| 4961 | (when (> beg (point-min)) |
| 4962 | (goto-char beg) |
| 4963 | (if (and (c-end-of-current-token) ; only moves when we started in the middle |
| 4964 | (progn (goto-char beg) |
| 4965 | (c-beginning-of-current-token))) |
| 4966 | (c-unfind-type (buffer-substring-no-properties |
| 4967 | (point) beg)))))) |
| 4968 | |
| 4969 | (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o") |
| 4970 | (cond |
| 4971 | ;; Changing the amount of (already existing) whitespace - don't do anything. |
| 4972 | ((and (c-partial-ws-p beg end) |
| 4973 | (or (= beg end) ; removal of WS |
| 4974 | (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type))))) |
| 4975 | |
| 4976 | ;; The syntactic relationship which defined a "found type" has been |
| 4977 | ;; destroyed. |
| 4978 | ((eq (car c-maybe-stale-found-type) 'c-decl-id-start) |
| 4979 | (c-unfind-type (cadr c-maybe-stale-found-type))) |
| 4980 | ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!! |
| 4981 | ))) |
| 4982 | |
| 4983 | \f |
| 4984 | ;; Setting and removing syntax properties on < and > in languages (C++ |
| 4985 | ;; and Java) where they can be template/generic delimiters as well as |
| 4986 | ;; their normal meaning of "less/greater than". |
| 4987 | |
| 4988 | ;; Normally, < and > have syntax 'punctuation'. When they are found to |
| 4989 | ;; be delimiters, they are marked as such with the category properties |
| 4990 | ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively. |
| 4991 | |
| 4992 | ;; STRATEGY: |
| 4993 | ;; |
| 4994 | ;; It is impossible to determine with certainty whether a <..> pair in |
| 4995 | ;; C++ is two comparison operators or is template delimiters, unless |
| 4996 | ;; one duplicates a lot of a C++ compiler. For example, the following |
| 4997 | ;; code fragment: |
| 4998 | ;; |
| 4999 | ;; foo (a < b, c > d) ; |
| 5000 | ;; |
| 5001 | ;; could be a function call with two integer parameters (each a |
| 5002 | ;; relational expression), or it could be a constructor for class foo |
| 5003 | ;; taking one parameter d of templated type "a < b, c >". They are |
| 5004 | ;; somewhat easier to distinguish in Java. |
| 5005 | ;; |
| 5006 | ;; The strategy now (2010-01) adopted is to mark and unmark < and |
| 5007 | ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked |
| 5008 | ;; individually when their context so indicated. This gave rise to |
| 5009 | ;; intractable problems when one of a matching pair was deleted, or |
| 5010 | ;; pulled into a literal.] |
| 5011 | ;; |
| 5012 | ;; At each buffer change, the syntax-table properties are removed in a |
| 5013 | ;; before-change function and reapplied, when needed, in an |
| 5014 | ;; after-change function. It is far more important that the |
| 5015 | ;; properties get removed when they they are spurious than that they |
| 5016 | ;; be present when wanted. |
| 5017 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 5018 | (defun c-clear-<-pair-props (&optional pos) |
| 5019 | ;; POS (default point) is at a < character. If it is marked with |
| 5020 | ;; open paren syntax-table text property, remove the property, |
| 5021 | ;; together with the close paren property on the matching > (if |
| 5022 | ;; any). |
| 5023 | (save-excursion |
| 5024 | (if pos |
| 5025 | (goto-char pos) |
| 5026 | (setq pos (point))) |
| 5027 | (when (equal (c-get-char-property (point) 'syntax-table) |
| 5028 | c-<-as-paren-syntax) |
| 5029 | (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,.. |
| 5030 | (c-go-list-forward)) |
| 5031 | (when (equal (c-get-char-property (1- (point)) 'syntax-table) |
| 5032 | c->-as-paren-syntax) ; should always be true. |
| 5033 | (c-clear-char-property (1- (point)) 'category)) |
| 5034 | (c-clear-char-property pos 'category)))) |
| 5035 | |
| 5036 | (defun c-clear->-pair-props (&optional pos) |
| 5037 | ;; POS (default point) is at a > character. If it is marked with |
| 5038 | ;; close paren syntax-table property, remove the property, together |
| 5039 | ;; with the open paren property on the matching < (if any). |
| 5040 | (save-excursion |
| 5041 | (if pos |
| 5042 | (goto-char pos) |
| 5043 | (setq pos (point))) |
| 5044 | (when (equal (c-get-char-property (point) 'syntax-table) |
| 5045 | c->-as-paren-syntax) |
| 5046 | (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,.. |
| 5047 | (c-go-up-list-backward)) |
| 5048 | (when (equal (c-get-char-property (point) 'syntax-table) |
| 5049 | c-<-as-paren-syntax) ; should always be true. |
| 5050 | (c-clear-char-property (point) 'category)) |
| 5051 | (c-clear-char-property pos 'category)))) |
| 5052 | |
| 5053 | (defun c-clear-<>-pair-props (&optional pos) |
| 5054 | ;; POS (default point) is at a < or > character. If it has an |
| 5055 | ;; open/close paren syntax-table property, remove this property both |
| 5056 | ;; from the current character and its partner (which will also be |
| 5057 | ;; thusly marked). |
| 5058 | (cond |
| 5059 | ((eq (char-after) ?\<) |
| 5060 | (c-clear-<-pair-props pos)) |
| 5061 | ((eq (char-after) ?\>) |
| 5062 | (c-clear->-pair-props pos)) |
| 5063 | (t (c-benign-error |
| 5064 | "c-clear-<>-pair-props called from wrong position")))) |
| 5065 | |
| 5066 | (defun c-clear-<-pair-props-if-match-after (lim &optional pos) |
| 5067 | ;; POS (default point) is at a < character. If it is both marked |
| 5068 | ;; with open/close paren syntax-table property, and has a matching > |
| 5069 | ;; (also marked) which is after LIM, remove the property both from |
| 5070 | ;; the current > and its partner. Return t when this happens, nil |
| 5071 | ;; when it doesn't. |
| 5072 | (save-excursion |
| 5073 | (if pos |
| 5074 | (goto-char pos) |
| 5075 | (setq pos (point))) |
| 5076 | (when (equal (c-get-char-property (point) 'syntax-table) |
| 5077 | c-<-as-paren-syntax) |
| 5078 | (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,.. |
| 5079 | (c-go-list-forward)) |
| 5080 | (when (and (>= (point) lim) |
| 5081 | (equal (c-get-char-property (1- (point)) 'syntax-table) |
| 5082 | c->-as-paren-syntax)) ; should always be true. |
| 5083 | (c-unmark-<->-as-paren (1- (point))) |
| 5084 | (c-unmark-<->-as-paren pos)) |
| 5085 | t))) |
| 5086 | |
| 5087 | (defun c-clear->-pair-props-if-match-before (lim &optional pos) |
| 5088 | ;; POS (default point) is at a > character. If it is both marked |
| 5089 | ;; with open/close paren syntax-table property, and has a matching < |
| 5090 | ;; (also marked) which is before LIM, remove the property both from |
| 5091 | ;; the current < and its partner. Return t when this happens, nil |
| 5092 | ;; when it doesn't. |
| 5093 | (save-excursion |
| 5094 | (if pos |
| 5095 | (goto-char pos) |
| 5096 | (setq pos (point))) |
| 5097 | (when (equal (c-get-char-property (point) 'syntax-table) |
| 5098 | c->-as-paren-syntax) |
| 5099 | (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,.. |
| 5100 | (c-go-up-list-backward)) |
| 5101 | (when (and (<= (point) lim) |
| 5102 | (equal (c-get-char-property (point) 'syntax-table) |
| 5103 | c-<-as-paren-syntax)) ; should always be true. |
| 5104 | (c-unmark-<->-as-paren (point)) |
| 5105 | (c-unmark-<->-as-paren pos)) |
| 5106 | t))) |
| 5107 | |
| 5108 | ;; Set by c-common-init in cc-mode.el. |
| 5109 | (defvar c-new-BEG) |
| 5110 | (defvar c-new-END) |
| 5111 | |
| 5112 | (defun c-before-change-check-<>-operators (beg end) |
| 5113 | ;; Unmark certain pairs of "< .... >" which are currently marked as |
| 5114 | ;; template/generic delimiters. (This marking is via syntax-table |
| 5115 | ;; text properties). |
| 5116 | ;; |
| 5117 | ;; These pairs are those which are in the current "statement" (i.e., |
| 5118 | ;; the region between the {, }, or ; before BEG and the one after |
| 5119 | ;; END), and which enclose any part of the interval (BEG END). |
| 5120 | ;; |
| 5121 | ;; Note that in C++ (?and Java), template/generic parens cannot |
| 5122 | ;; enclose a brace or semicolon, so we use these as bounds on the |
| 5123 | ;; region we must work on. |
| 5124 | ;; |
| 5125 | ;; This function is called from before-change-functions (via |
| 5126 | ;; c-get-state-before-change-functions). Thus the buffer is widened, |
| 5127 | ;; and point is undefined, both at entry and exit. |
| 5128 | ;; |
| 5129 | ;; FIXME!!! This routine ignores the possibility of macros entirely. |
| 5130 | ;; 2010-01-29. |
| 5131 | (save-excursion |
| 5132 | (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits))) |
| 5133 | (end-lit-limits (progn (goto-char end) (c-literal-limits))) |
| 5134 | new-beg new-end need-new-beg need-new-end) |
| 5135 | ;; Locate the barrier before the changed region |
| 5136 | (goto-char (if beg-lit-limits (car beg-lit-limits) beg)) |
| 5137 | (c-syntactic-skip-backward "^;{}" (max (- beg 2048) (point-min))) |
| 5138 | (setq new-beg (point)) |
| 5139 | |
| 5140 | ;; Remove the syntax-table properties from each pertinent <...> pair. |
| 5141 | ;; Firsly, the ones with the < before beg and > after beg. |
| 5142 | (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg) |
| 5143 | (if (c-clear-<-pair-props-if-match-after beg (1- (point))) |
| 5144 | (setq need-new-beg t))) |
| 5145 | |
| 5146 | ;; Locate the barrier after END. |
| 5147 | (goto-char (if end-lit-limits (cdr end-lit-limits) end)) |
| 5148 | (c-syntactic-re-search-forward "[;{}]" |
| 5149 | (min (+ end 2048) (point-max)) 'end) |
| 5150 | (setq new-end (point)) |
| 5151 | |
| 5152 | ;; Remove syntax-table properties from the remaining pertinent <...> |
| 5153 | ;; pairs, those with a > after end and < before end. |
| 5154 | (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end) |
| 5155 | (if (c-clear->-pair-props-if-match-before end) |
| 5156 | (setq need-new-end t))) |
| 5157 | |
| 5158 | ;; Extend the fontification region, if needed. |
| 5159 | (when need-new-beg |
| 5160 | (goto-char new-beg) |
| 5161 | (c-forward-syntactic-ws) |
| 5162 | (and (< (point) c-new-BEG) (setq c-new-BEG (point)))) |
| 5163 | |
| 5164 | (when need-new-end |
| 5165 | (and (> new-end c-new-END) (setq c-new-END new-end)))))) |
| 5166 | |
| 5167 | |
| 5168 | |
| 5169 | (defun c-after-change-check-<>-operators (beg end) |
| 5170 | ;; This is called from `after-change-functions' when |
| 5171 | ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">" |
| 5172 | ;; chars with paren syntax become part of another operator like "<<" |
| 5173 | ;; or ">=". |
| 5174 | ;; |
| 5175 | ;; This function might do hidden buffer changes. |
| 5176 | |
| 5177 | (save-excursion |
| 5178 | (goto-char beg) |
| 5179 | (when (or (looking-at "[<>]") |
| 5180 | (< (skip-chars-backward "<>") 0)) |
| 5181 | |
| 5182 | (goto-char beg) |
| 5183 | (c-beginning-of-current-token) |
| 5184 | (when (and (< (point) beg) |
| 5185 | (looking-at c-<>-multichar-token-regexp) |
| 5186 | (< beg (setq beg (match-end 0)))) |
| 5187 | (while (progn (skip-chars-forward "^<>" beg) |
| 5188 | (< (point) beg)) |
| 5189 | (c-clear-<>-pair-props) |
| 5190 | (forward-char)))) |
| 5191 | |
| 5192 | (when (< beg end) |
| 5193 | (goto-char end) |
| 5194 | (when (or (looking-at "[<>]") |
| 5195 | (< (skip-chars-backward "<>") 0)) |
| 5196 | |
| 5197 | (goto-char end) |
| 5198 | (c-beginning-of-current-token) |
| 5199 | (when (and (< (point) end) |
| 5200 | (looking-at c-<>-multichar-token-regexp) |
| 5201 | (< end (setq end (match-end 0)))) |
| 5202 | (while (progn (skip-chars-forward "^<>" end) |
| 5203 | (< (point) end)) |
| 5204 | (c-clear-<>-pair-props) |
| 5205 | (forward-char))))))) |
| 5206 | |
| 5207 | |
| 5208 | \f |
| 5209 | ;; Handling of small scale constructs like types and names. |
| 5210 | |
| 5211 | ;; Dynamically bound variable that instructs `c-forward-type' to also |
| 5212 | ;; treat possible types (i.e. those that it normally returns 'maybe or |
| 5213 | ;; 'found for) as actual types (and always return 'found for them). |
| 5214 | ;; This means that it records them in `c-record-type-identifiers' if |
| 5215 | ;; that is set, and that it adds them to `c-found-types'. |
| 5216 | (defvar c-promote-possible-types nil) |
| 5217 | |
| 5218 | ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to |
| 5219 | ;; mark up successfully parsed arglists with paren syntax properties on |
| 5220 | ;; the surrounding angle brackets and with `c-<>-arg-sep' in the |
| 5221 | ;; `c-type' property of each argument separating comma. |
| 5222 | ;; |
| 5223 | ;; Setting this variable also makes `c-forward-<>-arglist' recurse into |
| 5224 | ;; all arglists for side effects (i.e. recording types), otherwise it |
| 5225 | ;; exploits any existing paren syntax properties to quickly jump to the |
| 5226 | ;; end of already parsed arglists. |
| 5227 | ;; |
| 5228 | ;; Marking up the arglists is not the default since doing that correctly |
| 5229 | ;; depends on a proper value for `c-restricted-<>-arglists'. |
| 5230 | (defvar c-parse-and-markup-<>-arglists nil) |
| 5231 | |
| 5232 | ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to |
| 5233 | ;; not accept arglists that contain binary operators. |
| 5234 | ;; |
| 5235 | ;; This is primarily used to handle C++ template arglists. C++ |
| 5236 | ;; disambiguates them by checking whether the preceding name is a |
| 5237 | ;; template or not. We can't do that, so we assume it is a template |
| 5238 | ;; if it can be parsed as one. That usually works well since |
| 5239 | ;; comparison expressions on the forms "a < b > c" or "a < b, c > d" |
| 5240 | ;; in almost all cases would be pointless. |
| 5241 | ;; |
| 5242 | ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we |
| 5243 | ;; should let the comma separate the function arguments instead. And |
| 5244 | ;; in a context where the value of the expression is taken, e.g. in |
| 5245 | ;; "if (a < b || c > d)", it's probably not a template. |
| 5246 | (defvar c-restricted-<>-arglists nil) |
| 5247 | |
| 5248 | ;; Dynamically bound variables that instructs |
| 5249 | ;; `c-forward-keyword-clause', `c-forward-<>-arglist', |
| 5250 | ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and |
| 5251 | ;; `c-forward-label' to record the ranges of all the type and |
| 5252 | ;; reference identifiers they encounter. They will build lists on |
| 5253 | ;; these variables where each element is a cons of the buffer |
| 5254 | ;; positions surrounding each identifier. This recording is only |
| 5255 | ;; activated when `c-record-type-identifiers' is non-nil. |
| 5256 | ;; |
| 5257 | ;; All known types that can't be identifiers are recorded, and also |
| 5258 | ;; other possible types if `c-promote-possible-types' is set. |
| 5259 | ;; Recording is however disabled inside angle bracket arglists that |
| 5260 | ;; are encountered inside names and other angle bracket arglists. |
| 5261 | ;; Such occurrences are taken care of by `c-font-lock-<>-arglists' |
| 5262 | ;; instead. |
| 5263 | ;; |
| 5264 | ;; Only the names in C++ template style references (e.g. "tmpl" in |
| 5265 | ;; "tmpl<a,b>::foo") are recorded as references, other references |
| 5266 | ;; aren't handled here. |
| 5267 | ;; |
| 5268 | ;; `c-forward-label' records the label identifier(s) on |
| 5269 | ;; `c-record-ref-identifiers'. |
| 5270 | (defvar c-record-type-identifiers nil) |
| 5271 | (defvar c-record-ref-identifiers nil) |
| 5272 | |
| 5273 | ;; This variable will receive a cons cell of the range of the last |
| 5274 | ;; single identifier symbol stepped over by `c-forward-name' if it's |
| 5275 | ;; successful. This is the range that should be put on one of the |
| 5276 | ;; record lists above by the caller. It's assigned nil if there's no |
| 5277 | ;; such symbol in the name. |
| 5278 | (defvar c-last-identifier-range nil) |
| 5279 | |
| 5280 | (defmacro c-record-type-id (range) |
| 5281 | (if (eq (car-safe range) 'cons) |
| 5282 | ;; Always true. |
| 5283 | `(setq c-record-type-identifiers |
| 5284 | (cons ,range c-record-type-identifiers)) |
| 5285 | `(let ((range ,range)) |
| 5286 | (if range |
| 5287 | (setq c-record-type-identifiers |
| 5288 | (cons range c-record-type-identifiers)))))) |
| 5289 | |
| 5290 | (defmacro c-record-ref-id (range) |
| 5291 | (if (eq (car-safe range) 'cons) |
| 5292 | ;; Always true. |
| 5293 | `(setq c-record-ref-identifiers |
| 5294 | (cons ,range c-record-ref-identifiers)) |
| 5295 | `(let ((range ,range)) |
| 5296 | (if range |
| 5297 | (setq c-record-ref-identifiers |
| 5298 | (cons range c-record-ref-identifiers)))))) |
| 5299 | |
| 5300 | ;; Dynamically bound variable that instructs `c-forward-type' to |
| 5301 | ;; record the ranges of types that only are found. Behaves otherwise |
| 5302 | ;; like `c-record-type-identifiers'. |
| 5303 | (defvar c-record-found-types nil) |
| 5304 | |
| 5305 | (defmacro c-forward-keyword-prefixed-id (type) |
| 5306 | ;; Used internally in `c-forward-keyword-clause' to move forward |
| 5307 | ;; over a type (if TYPE is 'type) or a name (otherwise) which |
| 5308 | ;; possibly is prefixed by keywords and their associated clauses. |
| 5309 | ;; Try with a type/name first to not trip up on those that begin |
| 5310 | ;; with a keyword. Return t if a known or found type is moved |
| 5311 | ;; over. The point is clobbered if nil is returned. If range |
| 5312 | ;; recording is enabled, the identifier is recorded on as a type |
| 5313 | ;; if TYPE is 'type or as a reference if TYPE is 'ref. |
| 5314 | ;; |
| 5315 | ;; This macro might do hidden buffer changes. |
| 5316 | `(let (res) |
| 5317 | (while (if (setq res ,(if (eq type 'type) |
| 5318 | `(c-forward-type) |
| 5319 | `(c-forward-name))) |
| 5320 | nil |
| 5321 | (and (looking-at c-keywords-regexp) |
| 5322 | (c-forward-keyword-clause 1)))) |
| 5323 | (when (memq res '(t known found prefix)) |
| 5324 | ,(when (eq type 'ref) |
| 5325 | `(when c-record-type-identifiers |
| 5326 | (c-record-ref-id c-last-identifier-range))) |
| 5327 | t))) |
| 5328 | |
| 5329 | (defmacro c-forward-id-comma-list (type update-safe-pos) |
| 5330 | ;; Used internally in `c-forward-keyword-clause' to move forward |
| 5331 | ;; over a comma separated list of types or names using |
| 5332 | ;; `c-forward-keyword-prefixed-id'. |
| 5333 | ;; |
| 5334 | ;; This macro might do hidden buffer changes. |
| 5335 | `(while (and (progn |
| 5336 | ,(when update-safe-pos |
| 5337 | `(setq safe-pos (point))) |
| 5338 | (eq (char-after) ?,)) |
| 5339 | (progn |
| 5340 | (forward-char) |
| 5341 | (c-forward-syntactic-ws) |
| 5342 | (c-forward-keyword-prefixed-id ,type))))) |
| 5343 | |
| 5344 | (defun c-forward-keyword-clause (match) |
| 5345 | ;; Submatch MATCH in the current match data is assumed to surround a |
| 5346 | ;; token. If it's a keyword, move over it and any immediately |
| 5347 | ;; following clauses associated with it, stopping at the start of |
| 5348 | ;; the next token. t is returned in that case, otherwise the point |
| 5349 | ;; stays and nil is returned. The kind of clauses that are |
| 5350 | ;; recognized are those specified by `c-type-list-kwds', |
| 5351 | ;; `c-ref-list-kwds', `c-colon-type-list-kwds', |
| 5352 | ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds', |
| 5353 | ;; and `c-<>-arglist-kwds'. |
| 5354 | ;; |
| 5355 | ;; This function records identifier ranges on |
| 5356 | ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if |
| 5357 | ;; `c-record-type-identifiers' is non-nil. |
| 5358 | ;; |
| 5359 | ;; Note that for `c-colon-type-list-kwds', which doesn't necessary |
| 5360 | ;; apply directly after the keyword, the type list is moved over |
| 5361 | ;; only when there is no unaccounted token before it (i.e. a token |
| 5362 | ;; that isn't moved over due to some other keyword list). The |
| 5363 | ;; identifier ranges in the list are still recorded if that should |
| 5364 | ;; be done, though. |
| 5365 | ;; |
| 5366 | ;; This function might do hidden buffer changes. |
| 5367 | |
| 5368 | (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos |
| 5369 | ;; The call to `c-forward-<>-arglist' below is made after |
| 5370 | ;; `c-<>-sexp-kwds' keywords, so we're certain they actually |
| 5371 | ;; are angle bracket arglists and `c-restricted-<>-arglists' |
| 5372 | ;; should therefore be nil. |
| 5373 | (c-parse-and-markup-<>-arglists t) |
| 5374 | c-restricted-<>-arglists) |
| 5375 | |
| 5376 | (when kwd-sym |
| 5377 | (goto-char (match-end match)) |
| 5378 | (c-forward-syntactic-ws) |
| 5379 | (setq safe-pos (point)) |
| 5380 | |
| 5381 | (cond |
| 5382 | ((and (c-keyword-member kwd-sym 'c-type-list-kwds) |
| 5383 | (c-forward-keyword-prefixed-id type)) |
| 5384 | ;; There's a type directly after a keyword in `c-type-list-kwds'. |
| 5385 | (c-forward-id-comma-list type t)) |
| 5386 | |
| 5387 | ((and (c-keyword-member kwd-sym 'c-ref-list-kwds) |
| 5388 | (c-forward-keyword-prefixed-id ref)) |
| 5389 | ;; There's a name directly after a keyword in `c-ref-list-kwds'. |
| 5390 | (c-forward-id-comma-list ref t)) |
| 5391 | |
| 5392 | ((and (c-keyword-member kwd-sym 'c-paren-any-kwds) |
| 5393 | (eq (char-after) ?\()) |
| 5394 | ;; There's an open paren after a keyword in `c-paren-any-kwds'. |
| 5395 | |
| 5396 | (forward-char) |
| 5397 | (when (and (setq pos (c-up-list-forward)) |
| 5398 | (eq (char-before pos) ?\))) |
| 5399 | (when (and c-record-type-identifiers |
| 5400 | (c-keyword-member kwd-sym 'c-paren-type-kwds)) |
| 5401 | ;; Use `c-forward-type' on every identifier we can find |
| 5402 | ;; inside the paren, to record the types. |
| 5403 | (while (c-syntactic-re-search-forward c-symbol-start pos t) |
| 5404 | (goto-char (match-beginning 0)) |
| 5405 | (unless (c-forward-type) |
| 5406 | (looking-at c-symbol-key) ; Always matches. |
| 5407 | (goto-char (match-end 0))))) |
| 5408 | |
| 5409 | (goto-char pos) |
| 5410 | (c-forward-syntactic-ws) |
| 5411 | (setq safe-pos (point)))) |
| 5412 | |
| 5413 | ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds) |
| 5414 | (eq (char-after) ?<) |
| 5415 | (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds))) |
| 5416 | (c-forward-syntactic-ws) |
| 5417 | (setq safe-pos (point))) |
| 5418 | |
| 5419 | ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds) |
| 5420 | (not (looking-at c-symbol-start)) |
| 5421 | (c-safe (c-forward-sexp) t)) |
| 5422 | (c-forward-syntactic-ws) |
| 5423 | (setq safe-pos (point)))) |
| 5424 | |
| 5425 | (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds) |
| 5426 | (if (eq (char-after) ?:) |
| 5427 | ;; If we are at the colon already, we move over the type |
| 5428 | ;; list after it. |
| 5429 | (progn |
| 5430 | (forward-char) |
| 5431 | (c-forward-syntactic-ws) |
| 5432 | (when (c-forward-keyword-prefixed-id type) |
| 5433 | (c-forward-id-comma-list type t))) |
| 5434 | ;; Not at the colon, so stop here. But the identifier |
| 5435 | ;; ranges in the type list later on should still be |
| 5436 | ;; recorded. |
| 5437 | (and c-record-type-identifiers |
| 5438 | (progn |
| 5439 | ;; If a keyword matched both one of the types above and |
| 5440 | ;; this one, we match `c-colon-type-list-re' after the |
| 5441 | ;; clause matched above. |
| 5442 | (goto-char safe-pos) |
| 5443 | (looking-at c-colon-type-list-re)) |
| 5444 | (progn |
| 5445 | (goto-char (match-end 0)) |
| 5446 | (c-forward-syntactic-ws) |
| 5447 | (c-forward-keyword-prefixed-id type)) |
| 5448 | ;; There's a type after the `c-colon-type-list-re' match |
| 5449 | ;; after a keyword in `c-colon-type-list-kwds'. |
| 5450 | (c-forward-id-comma-list type nil)))) |
| 5451 | |
| 5452 | (goto-char safe-pos) |
| 5453 | t))) |
| 5454 | |
| 5455 | ;; cc-mode requires cc-fonts. |
| 5456 | (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ()) |
| 5457 | |
| 5458 | (defun c-forward-<>-arglist (all-types) |
| 5459 | ;; The point is assumed to be at a "<". Try to treat it as the open |
| 5460 | ;; paren of an angle bracket arglist and move forward to the |
| 5461 | ;; corresponding ">". If successful, the point is left after the |
| 5462 | ;; ">" and t is returned, otherwise the point isn't moved and nil is |
| 5463 | ;; returned. If ALL-TYPES is t then all encountered arguments in |
| 5464 | ;; the arglist that might be types are treated as found types. |
| 5465 | ;; |
| 5466 | ;; The variable `c-parse-and-markup-<>-arglists' controls how this |
| 5467 | ;; function handles text properties on the angle brackets and argument |
| 5468 | ;; separating commas. |
| 5469 | ;; |
| 5470 | ;; `c-restricted-<>-arglists' controls how lenient the template |
| 5471 | ;; arglist recognition should be. |
| 5472 | ;; |
| 5473 | ;; This function records identifier ranges on |
| 5474 | ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if |
| 5475 | ;; `c-record-type-identifiers' is non-nil. |
| 5476 | ;; |
| 5477 | ;; This function might do hidden buffer changes. |
| 5478 | |
| 5479 | (let ((start (point)) |
| 5480 | ;; If `c-record-type-identifiers' is set then activate |
| 5481 | ;; recording of any found types that constitute an argument in |
| 5482 | ;; the arglist. |
| 5483 | (c-record-found-types (if c-record-type-identifiers t))) |
| 5484 | (if (catch 'angle-bracket-arglist-escape |
| 5485 | (setq c-record-found-types |
| 5486 | (c-forward-<>-arglist-recur all-types))) |
| 5487 | (progn |
| 5488 | (when (consp c-record-found-types) |
| 5489 | (setq c-record-type-identifiers |
| 5490 | ;; `nconc' doesn't mind that the tail of |
| 5491 | ;; `c-record-found-types' is t. |
| 5492 | (nconc c-record-found-types c-record-type-identifiers))) |
| 5493 | (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs)) |
| 5494 | t) |
| 5495 | |
| 5496 | (goto-char start) |
| 5497 | nil))) |
| 5498 | |
| 5499 | (defun c-forward-<>-arglist-recur (all-types) |
| 5500 | ;; Recursive part of `c-forward-<>-arglist'. |
| 5501 | ;; |
| 5502 | ;; This function might do hidden buffer changes. |
| 5503 | |
| 5504 | (let ((start (point)) res pos tmp |
| 5505 | ;; Cover this so that any recorded found type ranges are |
| 5506 | ;; automatically lost if it turns out to not be an angle |
| 5507 | ;; bracket arglist. It's propagated through the return value |
| 5508 | ;; on successful completion. |
| 5509 | (c-record-found-types c-record-found-types) |
| 5510 | ;; List that collects the positions after the argument |
| 5511 | ;; separating ',' in the arglist. |
| 5512 | arg-start-pos) |
| 5513 | ;; If the '<' has paren open syntax then we've marked it as an angle |
| 5514 | ;; bracket arglist before, so skip to the end. |
| 5515 | (if (and (not c-parse-and-markup-<>-arglists) |
| 5516 | (c-get-char-property (point) 'syntax-table)) |
| 5517 | |
| 5518 | (progn |
| 5519 | (forward-char) |
| 5520 | (if (and (c-go-up-list-forward) |
| 5521 | (eq (char-before) ?>)) |
| 5522 | t |
| 5523 | ;; Got unmatched paren angle brackets. We don't clear the paren |
| 5524 | ;; syntax properties and retry, on the basis that it's very |
| 5525 | ;; unlikely that paren angle brackets become operators by code |
| 5526 | ;; manipulation. It's far more likely that it doesn't match due |
| 5527 | ;; to narrowing or some temporary change. |
| 5528 | (goto-char start) |
| 5529 | nil)) |
| 5530 | |
| 5531 | (forward-char) ; Forward over the opening '<'. |
| 5532 | |
| 5533 | (unless (looking-at c-<-op-cont-regexp) |
| 5534 | ;; go forward one non-alphanumeric character (group) per iteration of |
| 5535 | ;; this loop. |
| 5536 | (while (and |
| 5537 | (progn |
| 5538 | (c-forward-syntactic-ws) |
| 5539 | (let ((orig-record-found-types c-record-found-types)) |
| 5540 | (when (or (and c-record-type-identifiers all-types) |
| 5541 | (c-major-mode-is 'java-mode)) |
| 5542 | ;; All encountered identifiers are types, so set the |
| 5543 | ;; promote flag and parse the type. |
| 5544 | (progn |
| 5545 | (c-forward-syntactic-ws) |
| 5546 | (if (looking-at "\\?") |
| 5547 | (forward-char) |
| 5548 | (when (looking-at c-identifier-start) |
| 5549 | (let ((c-promote-possible-types t) |
| 5550 | (c-record-found-types t)) |
| 5551 | (c-forward-type)))) |
| 5552 | |
| 5553 | (c-forward-syntactic-ws) |
| 5554 | |
| 5555 | (when (or (looking-at "extends") |
| 5556 | (looking-at "super")) |
| 5557 | (forward-word) |
| 5558 | (c-forward-syntactic-ws) |
| 5559 | (let ((c-promote-possible-types t) |
| 5560 | (c-record-found-types t)) |
| 5561 | (c-forward-type) |
| 5562 | (c-forward-syntactic-ws)))))) |
| 5563 | |
| 5564 | (setq pos (point)) ; e.g. first token inside the '<' |
| 5565 | |
| 5566 | ;; Note: These regexps exploit the match order in \| so |
| 5567 | ;; that "<>" is matched by "<" rather than "[^>:-]>". |
| 5568 | (c-syntactic-re-search-forward |
| 5569 | ;; Stop on ',', '|', '&', '+' and '-' to catch |
| 5570 | ;; common binary operators that could be between |
| 5571 | ;; two comparison expressions "a<b" and "c>d". |
| 5572 | "[<;{},|+&-]\\|[>)]" |
| 5573 | nil t t)) |
| 5574 | |
| 5575 | (cond |
| 5576 | ((eq (char-before) ?>) |
| 5577 | ;; Either an operator starting with '>' or the end of |
| 5578 | ;; the angle bracket arglist. |
| 5579 | |
| 5580 | (if (looking-at c->-op-cont-regexp) |
| 5581 | (progn |
| 5582 | (goto-char (match-end 0)) |
| 5583 | t) ; Continue the loop. |
| 5584 | |
| 5585 | ;; The angle bracket arglist is finished. |
| 5586 | (when c-parse-and-markup-<>-arglists |
| 5587 | (while arg-start-pos |
| 5588 | (c-put-c-type-property (1- (car arg-start-pos)) |
| 5589 | 'c-<>-arg-sep) |
| 5590 | (setq arg-start-pos (cdr arg-start-pos))) |
| 5591 | (c-mark-<-as-paren start) |
| 5592 | (c-mark->-as-paren (1- (point)))) |
| 5593 | (setq res t) |
| 5594 | nil)) ; Exit the loop. |
| 5595 | |
| 5596 | ((eq (char-before) ?<) |
| 5597 | ;; Either an operator starting with '<' or a nested arglist. |
| 5598 | (setq pos (point)) |
| 5599 | (let (id-start id-end subres keyword-match) |
| 5600 | (cond |
| 5601 | ;; The '<' begins a multi-char operator. |
| 5602 | ((looking-at c-<-op-cont-regexp) |
| 5603 | (setq tmp (match-end 0)) |
| 5604 | (goto-char (match-end 0))) |
| 5605 | ;; We're at a nested <.....> |
| 5606 | ((progn |
| 5607 | (setq tmp pos) |
| 5608 | (backward-char) ; to the '<' |
| 5609 | (and |
| 5610 | (save-excursion |
| 5611 | ;; There's always an identifier before an angle |
| 5612 | ;; bracket arglist, or a keyword in `c-<>-type-kwds' |
| 5613 | ;; or `c-<>-arglist-kwds'. |
| 5614 | (c-backward-syntactic-ws) |
| 5615 | (setq id-end (point)) |
| 5616 | (c-simple-skip-symbol-backward) |
| 5617 | (when (or (setq keyword-match |
| 5618 | (looking-at c-opt-<>-sexp-key)) |
| 5619 | (not (looking-at c-keywords-regexp))) |
| 5620 | (setq id-start (point)))) |
| 5621 | (setq subres |
| 5622 | (let ((c-promote-possible-types t) |
| 5623 | (c-record-found-types t)) |
| 5624 | (c-forward-<>-arglist-recur |
| 5625 | (and keyword-match |
| 5626 | (c-keyword-member |
| 5627 | (c-keyword-sym (match-string 1)) |
| 5628 | 'c-<>-type-kwds))))))) |
| 5629 | |
| 5630 | ;; It was an angle bracket arglist. |
| 5631 | (setq c-record-found-types subres) |
| 5632 | |
| 5633 | ;; Record the identifier before the template as a type |
| 5634 | ;; or reference depending on whether the arglist is last |
| 5635 | ;; in a qualified identifier. |
| 5636 | (when (and c-record-type-identifiers |
| 5637 | (not keyword-match)) |
| 5638 | (if (and c-opt-identifier-concat-key |
| 5639 | (progn |
| 5640 | (c-forward-syntactic-ws) |
| 5641 | (looking-at c-opt-identifier-concat-key))) |
| 5642 | (c-record-ref-id (cons id-start id-end)) |
| 5643 | (c-record-type-id (cons id-start id-end))))) |
| 5644 | |
| 5645 | ;; At a "less than" operator. |
| 5646 | (t |
| 5647 | (forward-char) |
| 5648 | ))) |
| 5649 | t) ; carry on looping. |
| 5650 | |
| 5651 | ((and (not c-restricted-<>-arglists) |
| 5652 | (or (and (eq (char-before) ?&) |
| 5653 | (not (eq (char-after) ?&))) |
| 5654 | (eq (char-before) ?,))) |
| 5655 | ;; Just another argument. Record the position. The |
| 5656 | ;; type check stuff that made us stop at it is at |
| 5657 | ;; the top of the loop. |
| 5658 | (setq arg-start-pos (cons (point) arg-start-pos))) |
| 5659 | |
| 5660 | (t |
| 5661 | ;; Got a character that can't be in an angle bracket |
| 5662 | ;; arglist argument. Abort using `throw', since |
| 5663 | ;; it's useless to try to find a surrounding arglist |
| 5664 | ;; if we're nested. |
| 5665 | (throw 'angle-bracket-arglist-escape nil)))))) |
| 5666 | (if res |
| 5667 | (or c-record-found-types t))))) |
| 5668 | |
| 5669 | (defun c-backward-<>-arglist (all-types &optional limit) |
| 5670 | ;; The point is assumed to be directly after a ">". Try to treat it |
| 5671 | ;; as the close paren of an angle bracket arglist and move back to |
| 5672 | ;; the corresponding "<". If successful, the point is left at |
| 5673 | ;; the "<" and t is returned, otherwise the point isn't moved and |
| 5674 | ;; nil is returned. ALL-TYPES is passed on to |
| 5675 | ;; `c-forward-<>-arglist'. |
| 5676 | ;; |
| 5677 | ;; If the optional LIMIT is given, it bounds the backward search. |
| 5678 | ;; It's then assumed to be at a syntactically relevant position. |
| 5679 | ;; |
| 5680 | ;; This is a wrapper around `c-forward-<>-arglist'. See that |
| 5681 | ;; function for more details. |
| 5682 | |
| 5683 | (let ((start (point))) |
| 5684 | (backward-char) |
| 5685 | (if (and (not c-parse-and-markup-<>-arglists) |
| 5686 | (c-get-char-property (point) 'syntax-table)) |
| 5687 | |
| 5688 | (if (and (c-go-up-list-backward) |
| 5689 | (eq (char-after) ?<)) |
| 5690 | t |
| 5691 | ;; See corresponding note in `c-forward-<>-arglist'. |
| 5692 | (goto-char start) |
| 5693 | nil) |
| 5694 | |
| 5695 | (while (progn |
| 5696 | (c-syntactic-skip-backward "^<;{}" limit t) |
| 5697 | |
| 5698 | (and |
| 5699 | (if (eq (char-before) ?<) |
| 5700 | t |
| 5701 | ;; Stopped at bob or a char that isn't allowed in an |
| 5702 | ;; arglist, so we've failed. |
| 5703 | (goto-char start) |
| 5704 | nil) |
| 5705 | |
| 5706 | (if (> (point) |
| 5707 | (progn (c-beginning-of-current-token) |
| 5708 | (point))) |
| 5709 | ;; If we moved then the "<" was part of some |
| 5710 | ;; multicharacter token. |
| 5711 | t |
| 5712 | |
| 5713 | (backward-char) |
| 5714 | (let ((beg-pos (point))) |
| 5715 | (if (c-forward-<>-arglist all-types) |
| 5716 | (cond ((= (point) start) |
| 5717 | ;; Matched the arglist. Break the while. |
| 5718 | (goto-char beg-pos) |
| 5719 | nil) |
| 5720 | ((> (point) start) |
| 5721 | ;; We started from a non-paren ">" inside an |
| 5722 | ;; arglist. |
| 5723 | (goto-char start) |
| 5724 | nil) |
| 5725 | (t |
| 5726 | ;; Matched a shorter arglist. Can be a nested |
| 5727 | ;; one so continue looking. |
| 5728 | (goto-char beg-pos) |
| 5729 | t)) |
| 5730 | t)))))) |
| 5731 | |
| 5732 | (/= (point) start)))) |
| 5733 | |
| 5734 | (defun c-forward-name () |
| 5735 | ;; Move forward over a complete name if at the beginning of one, |
| 5736 | ;; stopping at the next following token. A keyword, as such, |
| 5737 | ;; doesn't count as a name. If the point is not at something that |
| 5738 | ;; is recognized as a name then it stays put. |
| 5739 | ;; |
| 5740 | ;; A name could be something as simple as "foo" in C or something as |
| 5741 | ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> :: |
| 5742 | ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short |
| 5743 | ;; int>::*volatile const" in C++ (this function is actually little |
| 5744 | ;; more than a `looking-at' call in all modes except those that, |
| 5745 | ;; like C++, have `c-recognize-<>-arglists' set). |
| 5746 | ;; |
| 5747 | ;; Return |
| 5748 | ;; o - nil if no name is found; |
| 5749 | ;; o - 'template if it's an identifier ending with an angle bracket |
| 5750 | ;; arglist; |
| 5751 | ;; o - 'operator of it's an operator identifier; |
| 5752 | ;; o - t if it's some other kind of name. |
| 5753 | ;; |
| 5754 | ;; This function records identifier ranges on |
| 5755 | ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if |
| 5756 | ;; `c-record-type-identifiers' is non-nil. |
| 5757 | ;; |
| 5758 | ;; This function might do hidden buffer changes. |
| 5759 | |
| 5760 | (let ((pos (point)) (start (point)) res id-start id-end |
| 5761 | ;; Turn off `c-promote-possible-types' here since we might |
| 5762 | ;; call `c-forward-<>-arglist' and we don't want it to promote |
| 5763 | ;; every suspect thing in the arglist to a type. We're |
| 5764 | ;; typically called from `c-forward-type' in this case, and |
| 5765 | ;; the caller only wants the top level type that it finds to |
| 5766 | ;; be promoted. |
| 5767 | c-promote-possible-types) |
| 5768 | (while |
| 5769 | (and |
| 5770 | (looking-at c-identifier-key) |
| 5771 | |
| 5772 | (progn |
| 5773 | ;; Check for keyword. We go to the last symbol in |
| 5774 | ;; `c-identifier-key' first. |
| 5775 | (goto-char (setq id-end (match-end 0))) |
| 5776 | (c-simple-skip-symbol-backward) |
| 5777 | (setq id-start (point)) |
| 5778 | |
| 5779 | (if (looking-at c-keywords-regexp) |
| 5780 | (when (and (c-major-mode-is 'c++-mode) |
| 5781 | (looking-at |
| 5782 | (cc-eval-when-compile |
| 5783 | (concat "\\(operator\\|\\(template\\)\\)" |
| 5784 | "\\(" (c-lang-const c-nonsymbol-key c++) |
| 5785 | "\\|$\\)"))) |
| 5786 | (if (match-beginning 2) |
| 5787 | ;; "template" is only valid inside an |
| 5788 | ;; identifier if preceded by "::". |
| 5789 | (save-excursion |
| 5790 | (c-backward-syntactic-ws) |
| 5791 | (and (c-safe (backward-char 2) t) |
| 5792 | (looking-at "::"))) |
| 5793 | t)) |
| 5794 | |
| 5795 | ;; Handle a C++ operator or template identifier. |
| 5796 | (goto-char id-end) |
| 5797 | (c-forward-syntactic-ws) |
| 5798 | (cond ((eq (char-before id-end) ?e) |
| 5799 | ;; Got "... ::template". |
| 5800 | (let ((subres (c-forward-name))) |
| 5801 | (when subres |
| 5802 | (setq pos (point) |
| 5803 | res subres)))) |
| 5804 | |
| 5805 | ((looking-at c-identifier-start) |
| 5806 | ;; Got a cast operator. |
| 5807 | (when (c-forward-type) |
| 5808 | (setq pos (point) |
| 5809 | res 'operator) |
| 5810 | ;; Now we should match a sequence of either |
| 5811 | ;; '*', '&' or a name followed by ":: *", |
| 5812 | ;; where each can be followed by a sequence |
| 5813 | ;; of `c-opt-type-modifier-key'. |
| 5814 | (while (cond ((looking-at "[*&]") |
| 5815 | (goto-char (match-end 0)) |
| 5816 | t) |
| 5817 | ((looking-at c-identifier-start) |
| 5818 | (and (c-forward-name) |
| 5819 | (looking-at "::") |
| 5820 | (progn |
| 5821 | (goto-char (match-end 0)) |
| 5822 | (c-forward-syntactic-ws) |
| 5823 | (eq (char-after) ?*)) |
| 5824 | (progn |
| 5825 | (forward-char) |
| 5826 | t)))) |
| 5827 | (while (progn |
| 5828 | (c-forward-syntactic-ws) |
| 5829 | (setq pos (point)) |
| 5830 | (looking-at c-opt-type-modifier-key)) |
| 5831 | (goto-char (match-end 1)))))) |
| 5832 | |
| 5833 | ((looking-at c-overloadable-operators-regexp) |
| 5834 | ;; Got some other operator. |
| 5835 | (setq c-last-identifier-range |
| 5836 | (cons (point) (match-end 0))) |
| 5837 | (goto-char (match-end 0)) |
| 5838 | (c-forward-syntactic-ws) |
| 5839 | (setq pos (point) |
| 5840 | res 'operator))) |
| 5841 | |
| 5842 | nil) |
| 5843 | |
| 5844 | ;; `id-start' is equal to `id-end' if we've jumped over |
| 5845 | ;; an identifier that doesn't end with a symbol token. |
| 5846 | ;; That can occur e.g. for Java import directives on the |
| 5847 | ;; form "foo.bar.*". |
| 5848 | (when (and id-start (/= id-start id-end)) |
| 5849 | (setq c-last-identifier-range |
| 5850 | (cons id-start id-end))) |
| 5851 | (goto-char id-end) |
| 5852 | (c-forward-syntactic-ws) |
| 5853 | (setq pos (point) |
| 5854 | res t))) |
| 5855 | |
| 5856 | (progn |
| 5857 | (goto-char pos) |
| 5858 | (when (or c-opt-identifier-concat-key |
| 5859 | c-recognize-<>-arglists) |
| 5860 | |
| 5861 | (cond |
| 5862 | ((and c-opt-identifier-concat-key |
| 5863 | (looking-at c-opt-identifier-concat-key)) |
| 5864 | ;; Got a concatenated identifier. This handles the |
| 5865 | ;; cases with tricky syntactic whitespace that aren't |
| 5866 | ;; covered in `c-identifier-key'. |
| 5867 | (goto-char (match-end 0)) |
| 5868 | (c-forward-syntactic-ws) |
| 5869 | t) |
| 5870 | |
| 5871 | ((and c-recognize-<>-arglists |
| 5872 | (eq (char-after) ?<)) |
| 5873 | ;; Maybe an angle bracket arglist. |
| 5874 | (when (let ((c-record-type-identifiers t) |
| 5875 | (c-record-found-types t)) |
| 5876 | (c-forward-<>-arglist nil)) |
| 5877 | |
| 5878 | (c-add-type start (1+ pos)) |
| 5879 | (c-forward-syntactic-ws) |
| 5880 | (setq pos (point) |
| 5881 | c-last-identifier-range nil) |
| 5882 | |
| 5883 | (if (and c-opt-identifier-concat-key |
| 5884 | (looking-at c-opt-identifier-concat-key)) |
| 5885 | |
| 5886 | ;; Continue if there's an identifier concatenation |
| 5887 | ;; operator after the template argument. |
| 5888 | (progn |
| 5889 | (when (and c-record-type-identifiers id-start) |
| 5890 | (c-record-ref-id (cons id-start id-end))) |
| 5891 | (forward-char 2) |
| 5892 | (c-forward-syntactic-ws) |
| 5893 | t) |
| 5894 | |
| 5895 | (when (and c-record-type-identifiers id-start) |
| 5896 | (c-record-type-id (cons id-start id-end))) |
| 5897 | (setq res 'template) |
| 5898 | nil))) |
| 5899 | ))))) |
| 5900 | |
| 5901 | (goto-char pos) |
| 5902 | res)) |
| 5903 | |
| 5904 | (defun c-forward-type (&optional brace-block-too) |
| 5905 | ;; Move forward over a type spec if at the beginning of one, |
| 5906 | ;; stopping at the next following token. The keyword "typedef" |
| 5907 | ;; isn't part of a type spec here. |
| 5908 | ;; |
| 5909 | ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in |
| 5910 | ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;". |
| 5911 | ;; The current (2009-03-10) intention is to convert all uses of |
| 5912 | ;; `c-forward-type' to call with this parameter set, then to |
| 5913 | ;; eliminate it. |
| 5914 | ;; |
| 5915 | ;; Return |
| 5916 | ;; o - t if it's a known type that can't be a name or other |
| 5917 | ;; expression; |
| 5918 | ;; o - 'known if it's an otherwise known type (according to |
| 5919 | ;; `*-font-lock-extra-types'); |
| 5920 | ;; o - 'prefix if it's a known prefix of a type; |
| 5921 | ;; o - 'found if it's a type that matches one in `c-found-types'; |
| 5922 | ;; o - 'maybe if it's an identifier that might be a type; or |
| 5923 | ;; o - nil if it can't be a type (the point isn't moved then). |
| 5924 | ;; |
| 5925 | ;; The point is assumed to be at the beginning of a token. |
| 5926 | ;; |
| 5927 | ;; Note that this function doesn't skip past the brace definition |
| 5928 | ;; that might be considered part of the type, e.g. |
| 5929 | ;; "enum {a, b, c} foo". |
| 5930 | ;; |
| 5931 | ;; This function records identifier ranges on |
| 5932 | ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if |
| 5933 | ;; `c-record-type-identifiers' is non-nil. |
| 5934 | ;; |
| 5935 | ;; This function might do hidden buffer changes. |
| 5936 | (when (and c-recognize-<>-arglists |
| 5937 | (looking-at "<")) |
| 5938 | (c-forward-<>-arglist t) |
| 5939 | (c-forward-syntactic-ws)) |
| 5940 | |
| 5941 | (let ((start (point)) pos res name-res id-start id-end id-range) |
| 5942 | |
| 5943 | ;; Skip leading type modifiers. If any are found we know it's a |
| 5944 | ;; prefix of a type. |
| 5945 | (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef" |
| 5946 | (while (looking-at c-opt-type-modifier-key) |
| 5947 | (goto-char (match-end 1)) |
| 5948 | (c-forward-syntactic-ws) |
| 5949 | (setq res 'prefix))) |
| 5950 | |
| 5951 | (cond |
| 5952 | ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT |
| 5953 | ; "typedef". |
| 5954 | (goto-char (match-end 1)) |
| 5955 | (c-forward-syntactic-ws) |
| 5956 | (setq pos (point)) |
| 5957 | |
| 5958 | (setq name-res (c-forward-name)) |
| 5959 | (setq res (not (null name-res))) |
| 5960 | (when (eq name-res t) |
| 5961 | ;; In many languages the name can be used without the |
| 5962 | ;; prefix, so we add it to `c-found-types'. |
| 5963 | (c-add-type pos (point)) |
| 5964 | (when (and c-record-type-identifiers |
| 5965 | c-last-identifier-range) |
| 5966 | (c-record-type-id c-last-identifier-range))) |
| 5967 | (when (and brace-block-too |
| 5968 | (memq res '(t nil)) |
| 5969 | (eq (char-after) ?\{) |
| 5970 | (save-excursion |
| 5971 | (c-safe |
| 5972 | (progn (c-forward-sexp) |
| 5973 | (c-forward-syntactic-ws) |
| 5974 | (setq pos (point)))))) |
| 5975 | (goto-char pos) |
| 5976 | (setq res t)) |
| 5977 | (unless res (goto-char start))) ; invalid syntax |
| 5978 | |
| 5979 | ((progn |
| 5980 | (setq pos nil) |
| 5981 | (if (looking-at c-identifier-start) |
| 5982 | (save-excursion |
| 5983 | (setq id-start (point) |
| 5984 | name-res (c-forward-name)) |
| 5985 | (when name-res |
| 5986 | (setq id-end (point) |
| 5987 | id-range c-last-identifier-range)))) |
| 5988 | (and (cond ((looking-at c-primitive-type-key) |
| 5989 | (setq res t)) |
| 5990 | ((c-with-syntax-table c-identifier-syntax-table |
| 5991 | (looking-at c-known-type-key)) |
| 5992 | (setq res 'known))) |
| 5993 | (or (not id-end) |
| 5994 | (>= (save-excursion |
| 5995 | (save-match-data |
| 5996 | (goto-char (match-end 1)) |
| 5997 | (c-forward-syntactic-ws) |
| 5998 | (setq pos (point)))) |
| 5999 | id-end) |
| 6000 | (setq res nil)))) |
| 6001 | ;; Looking at a primitive or known type identifier. We've |
| 6002 | ;; checked for a name first so that we don't go here if the |
| 6003 | ;; known type match only is a prefix of another name. |
| 6004 | |
| 6005 | (setq id-end (match-end 1)) |
| 6006 | |
| 6007 | (when (and c-record-type-identifiers |
| 6008 | (or c-promote-possible-types (eq res t))) |
| 6009 | (c-record-type-id (cons (match-beginning 1) (match-end 1)))) |
| 6010 | |
| 6011 | (if (and c-opt-type-component-key |
| 6012 | (save-match-data |
| 6013 | (looking-at c-opt-type-component-key))) |
| 6014 | ;; There might be more keywords for the type. |
| 6015 | (let (safe-pos) |
| 6016 | (c-forward-keyword-clause 1) |
| 6017 | (while (progn |
| 6018 | (setq safe-pos (point)) |
| 6019 | (looking-at c-opt-type-component-key)) |
| 6020 | (when (and c-record-type-identifiers |
| 6021 | (looking-at c-primitive-type-key)) |
| 6022 | (c-record-type-id (cons (match-beginning 1) |
| 6023 | (match-end 1)))) |
| 6024 | (c-forward-keyword-clause 1)) |
| 6025 | (if (looking-at c-primitive-type-key) |
| 6026 | (progn |
| 6027 | (when c-record-type-identifiers |
| 6028 | (c-record-type-id (cons (match-beginning 1) |
| 6029 | (match-end 1)))) |
| 6030 | (c-forward-keyword-clause 1) |
| 6031 | (setq res t)) |
| 6032 | (goto-char safe-pos) |
| 6033 | (setq res 'prefix))) |
| 6034 | (unless (save-match-data (c-forward-keyword-clause 1)) |
| 6035 | (if pos |
| 6036 | (goto-char pos) |
| 6037 | (goto-char (match-end 1)) |
| 6038 | (c-forward-syntactic-ws))))) |
| 6039 | |
| 6040 | (name-res |
| 6041 | (cond ((eq name-res t) |
| 6042 | ;; A normal identifier. |
| 6043 | (goto-char id-end) |
| 6044 | (if (or res c-promote-possible-types) |
| 6045 | (progn |
| 6046 | (c-add-type id-start id-end) |
| 6047 | (when (and c-record-type-identifiers id-range) |
| 6048 | (c-record-type-id id-range)) |
| 6049 | (unless res |
| 6050 | (setq res 'found))) |
| 6051 | (setq res (if (c-check-type id-start id-end) |
| 6052 | ;; It's an identifier that has been used as |
| 6053 | ;; a type somewhere else. |
| 6054 | 'found |
| 6055 | ;; It's an identifier that might be a type. |
| 6056 | 'maybe)))) |
| 6057 | ((eq name-res 'template) |
| 6058 | ;; A template is a type. |
| 6059 | (goto-char id-end) |
| 6060 | (setq res t)) |
| 6061 | (t |
| 6062 | ;; Otherwise it's an operator identifier, which is not a type. |
| 6063 | (goto-char start) |
| 6064 | (setq res nil))))) |
| 6065 | |
| 6066 | (when res |
| 6067 | ;; Skip trailing type modifiers. If any are found we know it's |
| 6068 | ;; a type. |
| 6069 | (when c-opt-type-modifier-key |
| 6070 | (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile" |
| 6071 | (goto-char (match-end 1)) |
| 6072 | (c-forward-syntactic-ws) |
| 6073 | (setq res t))) |
| 6074 | ;; Step over any type suffix operator. Do not let the existence |
| 6075 | ;; of these alter the classification of the found type, since |
| 6076 | ;; these operators typically are allowed in normal expressions |
| 6077 | ;; too. |
| 6078 | (when c-opt-type-suffix-key |
| 6079 | (while (looking-at c-opt-type-suffix-key) |
| 6080 | (goto-char (match-end 1)) |
| 6081 | (c-forward-syntactic-ws))) |
| 6082 | |
| 6083 | (when c-opt-type-concat-key ; Only/mainly for pike. |
| 6084 | ;; Look for a trailing operator that concatenates the type |
| 6085 | ;; with a following one, and if so step past that one through |
| 6086 | ;; a recursive call. Note that we don't record concatenated |
| 6087 | ;; types in `c-found-types' - it's the component types that |
| 6088 | ;; are recorded when appropriate. |
| 6089 | (setq pos (point)) |
| 6090 | (let* ((c-promote-possible-types (or (memq res '(t known)) |
| 6091 | c-promote-possible-types)) |
| 6092 | ;; If we can't promote then set `c-record-found-types' so that |
| 6093 | ;; we can merge in the types from the second part afterwards if |
| 6094 | ;; it turns out to be a known type there. |
| 6095 | (c-record-found-types (and c-record-type-identifiers |
| 6096 | (not c-promote-possible-types))) |
| 6097 | subres) |
| 6098 | (if (and (looking-at c-opt-type-concat-key) |
| 6099 | |
| 6100 | (progn |
| 6101 | (goto-char (match-end 1)) |
| 6102 | (c-forward-syntactic-ws) |
| 6103 | (setq subres (c-forward-type)))) |
| 6104 | |
| 6105 | (progn |
| 6106 | ;; If either operand certainly is a type then both are, but we |
| 6107 | ;; don't let the existence of the operator itself promote two |
| 6108 | ;; uncertain types to a certain one. |
| 6109 | (cond ((eq res t)) |
| 6110 | ((eq subres t) |
| 6111 | (unless (eq name-res 'template) |
| 6112 | (c-add-type id-start id-end)) |
| 6113 | (when (and c-record-type-identifiers id-range) |
| 6114 | (c-record-type-id id-range)) |
| 6115 | (setq res t)) |
| 6116 | ((eq res 'known)) |
| 6117 | ((eq subres 'known) |
| 6118 | (setq res 'known)) |
| 6119 | ((eq res 'found)) |
| 6120 | ((eq subres 'found) |
| 6121 | (setq res 'found)) |
| 6122 | (t |
| 6123 | (setq res 'maybe))) |
| 6124 | |
| 6125 | (when (and (eq res t) |
| 6126 | (consp c-record-found-types)) |
| 6127 | ;; Merge in the ranges of any types found by the second |
| 6128 | ;; `c-forward-type'. |
| 6129 | (setq c-record-type-identifiers |
| 6130 | ;; `nconc' doesn't mind that the tail of |
| 6131 | ;; `c-record-found-types' is t. |
| 6132 | (nconc c-record-found-types |
| 6133 | c-record-type-identifiers)))) |
| 6134 | |
| 6135 | (goto-char pos)))) |
| 6136 | |
| 6137 | (when (and c-record-found-types (memq res '(known found)) id-range) |
| 6138 | (setq c-record-found-types |
| 6139 | (cons id-range c-record-found-types)))) |
| 6140 | |
| 6141 | ;;(message "c-forward-type %s -> %s: %s" start (point) res) |
| 6142 | |
| 6143 | res)) |
| 6144 | |
| 6145 | (defun c-forward-annotation () |
| 6146 | ;; Used for Java code only at the moment. Assumes point is on the |
| 6147 | ;; @, moves forward an annotation. returns nil if there is no |
| 6148 | ;; annotation at point. |
| 6149 | (and (looking-at "@") |
| 6150 | (progn (forward-char) t) |
| 6151 | (c-forward-type) |
| 6152 | (progn (c-forward-syntactic-ws) t) |
| 6153 | (if (looking-at "(") |
| 6154 | (c-go-list-forward) |
| 6155 | t))) |
| 6156 | |
| 6157 | \f |
| 6158 | ;; Handling of large scale constructs like statements and declarations. |
| 6159 | |
| 6160 | ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a |
| 6161 | ;; defsubst or perhaps even a defun, but it contains lots of free |
| 6162 | ;; variables that refer to things inside `c-forward-decl-or-cast-1'. |
| 6163 | (defmacro c-fdoc-shift-type-backward (&optional short) |
| 6164 | ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list |
| 6165 | ;; of types when parsing a declaration, which means that it |
| 6166 | ;; sometimes consumes the identifier in the declaration as a type. |
| 6167 | ;; This is used to "backtrack" and make the last type be treated as |
| 6168 | ;; an identifier instead. |
| 6169 | `(progn |
| 6170 | ,(unless short |
| 6171 | ;; These identifiers are bound only in the inner let. |
| 6172 | '(setq identifier-type at-type |
| 6173 | identifier-start type-start |
| 6174 | got-parens nil |
| 6175 | got-identifier t |
| 6176 | got-suffix t |
| 6177 | got-suffix-after-parens id-start |
| 6178 | paren-depth 0)) |
| 6179 | |
| 6180 | (if (setq at-type (if (eq backup-at-type 'prefix) |
| 6181 | t |
| 6182 | backup-at-type)) |
| 6183 | (setq type-start backup-type-start |
| 6184 | id-start backup-id-start) |
| 6185 | (setq type-start start-pos |
| 6186 | id-start start-pos)) |
| 6187 | |
| 6188 | ;; When these flags already are set we've found specifiers that |
| 6189 | ;; unconditionally signal these attributes - backtracking doesn't |
| 6190 | ;; change that. So keep them set in that case. |
| 6191 | (or at-type-decl |
| 6192 | (setq at-type-decl backup-at-type-decl)) |
| 6193 | (or maybe-typeless |
| 6194 | (setq maybe-typeless backup-maybe-typeless)) |
| 6195 | |
| 6196 | ,(unless short |
| 6197 | ;; This identifier is bound only in the inner let. |
| 6198 | '(setq start id-start)))) |
| 6199 | |
| 6200 | (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end) |
| 6201 | ;; Move forward over a declaration or a cast if at the start of one. |
| 6202 | ;; The point is assumed to be at the start of some token. Nil is |
| 6203 | ;; returned if no declaration or cast is recognized, and the point |
| 6204 | ;; is clobbered in that case. |
| 6205 | ;; |
| 6206 | ;; If a declaration is parsed: |
| 6207 | ;; |
| 6208 | ;; The point is left at the first token after the first complete |
| 6209 | ;; declarator, if there is one. The return value is a cons where |
| 6210 | ;; the car is the position of the first token in the declarator. (See |
| 6211 | ;; below for the cdr.) |
| 6212 | ;; Some examples: |
| 6213 | ;; |
| 6214 | ;; void foo (int a, char *b) stuff ... |
| 6215 | ;; car ^ ^ point |
| 6216 | ;; float (*a)[], b; |
| 6217 | ;; car ^ ^ point |
| 6218 | ;; unsigned int a = c_style_initializer, b; |
| 6219 | ;; car ^ ^ point |
| 6220 | ;; unsigned int a (cplusplus_style_initializer), b; |
| 6221 | ;; car ^ ^ point (might change) |
| 6222 | ;; class Foo : public Bar {} |
| 6223 | ;; car ^ ^ point |
| 6224 | ;; class PikeClass (int a, string b) stuff ... |
| 6225 | ;; car ^ ^ point |
| 6226 | ;; enum bool; |
| 6227 | ;; car ^ ^ point |
| 6228 | ;; enum bool flag; |
| 6229 | ;; car ^ ^ point |
| 6230 | ;; void cplusplus_function (int x) throw (Bad); |
| 6231 | ;; car ^ ^ point |
| 6232 | ;; Foo::Foo (int b) : Base (b) {} |
| 6233 | ;; car ^ ^ point |
| 6234 | ;; |
| 6235 | ;; The cdr of the return value is non-nil when a |
| 6236 | ;; `c-typedef-decl-kwds' specifier is found in the declaration. |
| 6237 | ;; Specifically it is a dotted pair (A . B) where B is t when a |
| 6238 | ;; `c-typedef-kwds' ("typedef") is present, and A is t when some |
| 6239 | ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum) |
| 6240 | ;; specifier is present. I.e., (some of) the declared |
| 6241 | ;; identifier(s) are types. |
| 6242 | ;; |
| 6243 | ;; If a cast is parsed: |
| 6244 | ;; |
| 6245 | ;; The point is left at the first token after the closing paren of |
| 6246 | ;; the cast. The return value is `cast'. Note that the start |
| 6247 | ;; position must be at the first token inside the cast parenthesis |
| 6248 | ;; to recognize it. |
| 6249 | ;; |
| 6250 | ;; PRECEDING-TOKEN-END is the first position after the preceding |
| 6251 | ;; token, i.e. on the other side of the syntactic ws from the point. |
| 6252 | ;; Use a value less than or equal to (point-min) if the point is at |
| 6253 | ;; the first token in (the visible part of) the buffer. |
| 6254 | ;; |
| 6255 | ;; CONTEXT is a symbol that describes the context at the point: |
| 6256 | ;; 'decl In a comma-separated declaration context (typically |
| 6257 | ;; inside a function declaration arglist). |
| 6258 | ;; '<> In an angle bracket arglist. |
| 6259 | ;; 'arglist Some other type of arglist. |
| 6260 | ;; nil Some other context or unknown context. Includes |
| 6261 | ;; within the parens of an if, for, ... construct. |
| 6262 | ;; |
| 6263 | ;; LAST-CAST-END is the first token after the closing paren of a |
| 6264 | ;; preceding cast, or nil if none is known. If |
| 6265 | ;; `c-forward-decl-or-cast-1' is used in succession, it should be |
| 6266 | ;; the position after the closest preceding call where a cast was |
| 6267 | ;; matched. In that case it's used to discover chains of casts like |
| 6268 | ;; "(a) (b) c". |
| 6269 | ;; |
| 6270 | ;; This function records identifier ranges on |
| 6271 | ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if |
| 6272 | ;; `c-record-type-identifiers' is non-nil. |
| 6273 | ;; |
| 6274 | ;; This function might do hidden buffer changes. |
| 6275 | |
| 6276 | (let (;; `start-pos' is used below to point to the start of the |
| 6277 | ;; first type, i.e. after any leading specifiers. It might |
| 6278 | ;; also point at the beginning of the preceding syntactic |
| 6279 | ;; whitespace. |
| 6280 | (start-pos (point)) |
| 6281 | ;; Set to the result of `c-forward-type'. |
| 6282 | at-type |
| 6283 | ;; The position of the first token in what we currently |
| 6284 | ;; believe is the type in the declaration or cast, after any |
| 6285 | ;; specifiers and their associated clauses. |
| 6286 | type-start |
| 6287 | ;; The position of the first token in what we currently |
| 6288 | ;; believe is the declarator for the first identifier. Set |
| 6289 | ;; when the type is found, and moved forward over any |
| 6290 | ;; `c-decl-hangon-kwds' and their associated clauses that |
| 6291 | ;; occurs after the type. |
| 6292 | id-start |
| 6293 | ;; These store `at-type', `type-start' and `id-start' of the |
| 6294 | ;; identifier before the one in those variables. The previous |
| 6295 | ;; identifier might turn out to be the real type in a |
| 6296 | ;; declaration if the last one has to be the declarator in it. |
| 6297 | ;; If `backup-at-type' is nil then the other variables have |
| 6298 | ;; undefined values. |
| 6299 | backup-at-type backup-type-start backup-id-start |
| 6300 | ;; Set if we've found a specifier (apart from "typedef") that makes |
| 6301 | ;; the defined identifier(s) types. |
| 6302 | at-type-decl |
| 6303 | ;; Set if we've a "typedef" keyword. |
| 6304 | at-typedef |
| 6305 | ;; Set if we've found a specifier that can start a declaration |
| 6306 | ;; where there's no type. |
| 6307 | maybe-typeless |
| 6308 | ;; If a specifier is found that also can be a type prefix, |
| 6309 | ;; these flags are set instead of those above. If we need to |
| 6310 | ;; back up an identifier, they are copied to the real flag |
| 6311 | ;; variables. Thus they only take effect if we fail to |
| 6312 | ;; interpret it as a type. |
| 6313 | backup-at-type-decl backup-maybe-typeless |
| 6314 | ;; Whether we've found a declaration or a cast. We might know |
| 6315 | ;; this before we've found the type in it. It's 'ids if we've |
| 6316 | ;; found two consecutive identifiers (usually a sure sign, but |
| 6317 | ;; we should allow that in labels too), and t if we've found a |
| 6318 | ;; specifier keyword (a 100% sure sign). |
| 6319 | at-decl-or-cast |
| 6320 | ;; Set when we need to back up to parse this as a declaration |
| 6321 | ;; but not as a cast. |
| 6322 | backup-if-not-cast |
| 6323 | ;; For casts, the return position. |
| 6324 | cast-end |
| 6325 | ;; Save `c-record-type-identifiers' and |
| 6326 | ;; `c-record-ref-identifiers' since ranges are recorded |
| 6327 | ;; speculatively and should be thrown away if it turns out |
| 6328 | ;; that it isn't a declaration or cast. |
| 6329 | (save-rec-type-ids c-record-type-identifiers) |
| 6330 | (save-rec-ref-ids c-record-ref-identifiers)) |
| 6331 | |
| 6332 | (while (c-forward-annotation) |
| 6333 | (c-forward-syntactic-ws)) |
| 6334 | |
| 6335 | ;; Check for a type. Unknown symbols are treated as possible |
| 6336 | ;; types, but they could also be specifiers disguised through |
| 6337 | ;; macros like __INLINE__, so we recognize both types and known |
| 6338 | ;; specifiers after them too. |
| 6339 | (while |
| 6340 | (let* ((start (point)) kwd-sym kwd-clause-end found-type) |
| 6341 | |
| 6342 | ;; Look for a specifier keyword clause. |
| 6343 | (when (or (looking-at c-prefix-spec-kwds-re) |
| 6344 | (and (c-major-mode-is 'java-mode) |
| 6345 | (looking-at "@[A-Za-z0-9]+"))) |
| 6346 | (if (looking-at c-typedef-key) |
| 6347 | (setq at-typedef t)) |
| 6348 | (setq kwd-sym (c-keyword-sym (match-string 1))) |
| 6349 | (save-excursion |
| 6350 | (c-forward-keyword-clause 1) |
| 6351 | (setq kwd-clause-end (point)))) |
| 6352 | |
| 6353 | (when (setq found-type (c-forward-type t)) ; brace-block-too |
| 6354 | ;; Found a known or possible type or a prefix of a known type. |
| 6355 | |
| 6356 | (when at-type |
| 6357 | ;; Got two identifiers with nothing but whitespace |
| 6358 | ;; between them. That can only happen in declarations. |
| 6359 | (setq at-decl-or-cast 'ids) |
| 6360 | |
| 6361 | (when (eq at-type 'found) |
| 6362 | ;; If the previous identifier is a found type we |
| 6363 | ;; record it as a real one; it might be some sort of |
| 6364 | ;; alias for a prefix like "unsigned". |
| 6365 | (save-excursion |
| 6366 | (goto-char type-start) |
| 6367 | (let ((c-promote-possible-types t)) |
| 6368 | (c-forward-type))))) |
| 6369 | |
| 6370 | (setq backup-at-type at-type |
| 6371 | backup-type-start type-start |
| 6372 | backup-id-start id-start |
| 6373 | at-type found-type |
| 6374 | type-start start |
| 6375 | id-start (point) |
| 6376 | ;; The previous ambiguous specifier/type turned out |
| 6377 | ;; to be a type since we've parsed another one after |
| 6378 | ;; it, so clear these backup flags. |
| 6379 | backup-at-type-decl nil |
| 6380 | backup-maybe-typeless nil)) |
| 6381 | |
| 6382 | (if kwd-sym |
| 6383 | (progn |
| 6384 | ;; Handle known specifier keywords and |
| 6385 | ;; `c-decl-hangon-kwds' which can occur after known |
| 6386 | ;; types. |
| 6387 | |
| 6388 | (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds) |
| 6389 | ;; It's a hang-on keyword that can occur anywhere. |
| 6390 | (progn |
| 6391 | (setq at-decl-or-cast t) |
| 6392 | (if at-type |
| 6393 | ;; Move the identifier start position if |
| 6394 | ;; we've passed a type. |
| 6395 | (setq id-start kwd-clause-end) |
| 6396 | ;; Otherwise treat this as a specifier and |
| 6397 | ;; move the fallback position. |
| 6398 | (setq start-pos kwd-clause-end)) |
| 6399 | (goto-char kwd-clause-end)) |
| 6400 | |
| 6401 | ;; It's an ordinary specifier so we know that |
| 6402 | ;; anything before this can't be the type. |
| 6403 | (setq backup-at-type nil |
| 6404 | start-pos kwd-clause-end) |
| 6405 | |
| 6406 | (if found-type |
| 6407 | ;; It's ambiguous whether this keyword is a |
| 6408 | ;; specifier or a type prefix, so set the backup |
| 6409 | ;; flags. (It's assumed that `c-forward-type' |
| 6410 | ;; moved further than `c-forward-keyword-clause'.) |
| 6411 | (progn |
| 6412 | (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds) |
| 6413 | (setq backup-at-type-decl t)) |
| 6414 | (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds) |
| 6415 | (setq backup-maybe-typeless t))) |
| 6416 | |
| 6417 | (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds) |
| 6418 | ;; This test only happens after we've scanned a type. |
| 6419 | ;; So, with valid syntax, kwd-sym can't be 'typedef. |
| 6420 | (setq at-type-decl t)) |
| 6421 | (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds) |
| 6422 | (setq maybe-typeless t)) |
| 6423 | |
| 6424 | ;; Haven't matched a type so it's an unambiguous |
| 6425 | ;; specifier keyword and we know we're in a |
| 6426 | ;; declaration. |
| 6427 | (setq at-decl-or-cast t) |
| 6428 | |
| 6429 | (goto-char kwd-clause-end)))) |
| 6430 | |
| 6431 | ;; If the type isn't known we continue so that we'll jump |
| 6432 | ;; over all specifiers and type identifiers. The reason |
| 6433 | ;; to do this for a known type prefix is to make things |
| 6434 | ;; like "unsigned INT16" work. |
| 6435 | (and found-type (not (eq found-type t)))))) |
| 6436 | |
| 6437 | (cond |
| 6438 | ((eq at-type t) |
| 6439 | ;; If a known type was found, we still need to skip over any |
| 6440 | ;; hangon keyword clauses after it. Otherwise it has already |
| 6441 | ;; been done in the loop above. |
| 6442 | (while (looking-at c-decl-hangon-key) |
| 6443 | (c-forward-keyword-clause 1)) |
| 6444 | (setq id-start (point))) |
| 6445 | |
| 6446 | ((eq at-type 'prefix) |
| 6447 | ;; A prefix type is itself a primitive type when it's not |
| 6448 | ;; followed by another type. |
| 6449 | (setq at-type t)) |
| 6450 | |
| 6451 | ((not at-type) |
| 6452 | ;; Got no type but set things up to continue anyway to handle |
| 6453 | ;; the various cases when a declaration doesn't start with a |
| 6454 | ;; type. |
| 6455 | (setq id-start start-pos)) |
| 6456 | |
| 6457 | ((and (eq at-type 'maybe) |
| 6458 | (c-major-mode-is 'c++-mode)) |
| 6459 | ;; If it's C++ then check if the last "type" ends on the form |
| 6460 | ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a |
| 6461 | ;; (con|de)structor. |
| 6462 | (save-excursion |
| 6463 | (let (name end-2 end-1) |
| 6464 | (goto-char id-start) |
| 6465 | (c-backward-syntactic-ws) |
| 6466 | (setq end-2 (point)) |
| 6467 | (when (and |
| 6468 | (c-simple-skip-symbol-backward) |
| 6469 | (progn |
| 6470 | (setq name |
| 6471 | (buffer-substring-no-properties (point) end-2)) |
| 6472 | ;; Cheating in the handling of syntactic ws below. |
| 6473 | (< (skip-chars-backward ":~ \t\n\r\v\f") 0)) |
| 6474 | (progn |
| 6475 | (setq end-1 (point)) |
| 6476 | (c-simple-skip-symbol-backward)) |
| 6477 | (>= (point) type-start) |
| 6478 | (equal (buffer-substring-no-properties (point) end-1) |
| 6479 | name)) |
| 6480 | ;; It is a (con|de)structor name. In that case the |
| 6481 | ;; declaration is typeless so zap out any preceding |
| 6482 | ;; identifier(s) that we might have taken as types. |
| 6483 | (goto-char type-start) |
| 6484 | (setq at-type nil |
| 6485 | backup-at-type nil |
| 6486 | id-start type-start)))))) |
| 6487 | |
| 6488 | ;; Check for and step over a type decl expression after the thing |
| 6489 | ;; that is or might be a type. This can't be skipped since we |
| 6490 | ;; need the correct end position of the declarator for |
| 6491 | ;; `max-type-decl-end-*'. |
| 6492 | (let ((start (point)) (paren-depth 0) pos |
| 6493 | ;; True if there's a non-open-paren match of |
| 6494 | ;; `c-type-decl-prefix-key'. |
| 6495 | got-prefix |
| 6496 | ;; True if the declarator is surrounded by a parenthesis pair. |
| 6497 | got-parens |
| 6498 | ;; True if there is an identifier in the declarator. |
| 6499 | got-identifier |
| 6500 | ;; True if there's a non-close-paren match of |
| 6501 | ;; `c-type-decl-suffix-key'. |
| 6502 | got-suffix |
| 6503 | ;; True if there's a prefix match outside the outermost |
| 6504 | ;; paren pair that surrounds the declarator. |
| 6505 | got-prefix-before-parens |
| 6506 | ;; True if there's a suffix match outside the outermost |
| 6507 | ;; paren pair that surrounds the declarator. The value is |
| 6508 | ;; the position of the first suffix match. |
| 6509 | got-suffix-after-parens |
| 6510 | ;; True if we've parsed the type decl to a token that is |
| 6511 | ;; known to end declarations in this context. |
| 6512 | at-decl-end |
| 6513 | ;; The earlier values of `at-type' and `type-start' if we've |
| 6514 | ;; shifted the type backwards. |
| 6515 | identifier-type identifier-start |
| 6516 | ;; If `c-parse-and-markup-<>-arglists' is set we need to |
| 6517 | ;; turn it off during the name skipping below to avoid |
| 6518 | ;; getting `c-type' properties that might be bogus. That |
| 6519 | ;; can happen since we don't know if |
| 6520 | ;; `c-restricted-<>-arglists' will be correct inside the |
| 6521 | ;; arglist paren that gets entered. |
| 6522 | c-parse-and-markup-<>-arglists) |
| 6523 | |
| 6524 | (goto-char id-start) |
| 6525 | |
| 6526 | ;; Skip over type decl prefix operators. (Note similar code in |
| 6527 | ;; `c-font-lock-declarators'.) |
| 6528 | (while (and (looking-at c-type-decl-prefix-key) |
| 6529 | (if (and (c-major-mode-is 'c++-mode) |
| 6530 | (match-beginning 3)) |
| 6531 | ;; If the second submatch matches in C++ then |
| 6532 | ;; we're looking at an identifier that's a |
| 6533 | ;; prefix only if it specifies a member pointer. |
| 6534 | (when (setq got-identifier (c-forward-name)) |
| 6535 | (if (looking-at "\\(::\\)") |
| 6536 | ;; We only check for a trailing "::" and |
| 6537 | ;; let the "*" that should follow be |
| 6538 | ;; matched in the next round. |
| 6539 | (progn (setq got-identifier nil) t) |
| 6540 | ;; It turned out to be the real identifier, |
| 6541 | ;; so stop. |
| 6542 | nil)) |
| 6543 | t)) |
| 6544 | |
| 6545 | (if (eq (char-after) ?\() |
| 6546 | (progn |
| 6547 | (setq paren-depth (1+ paren-depth)) |
| 6548 | (forward-char)) |
| 6549 | (unless got-prefix-before-parens |
| 6550 | (setq got-prefix-before-parens (= paren-depth 0))) |
| 6551 | (setq got-prefix t) |
| 6552 | (goto-char (match-end 1))) |
| 6553 | (c-forward-syntactic-ws)) |
| 6554 | |
| 6555 | (setq got-parens (> paren-depth 0)) |
| 6556 | |
| 6557 | ;; Skip over an identifier. |
| 6558 | (or got-identifier |
| 6559 | (and (looking-at c-identifier-start) |
| 6560 | (setq got-identifier (c-forward-name)))) |
| 6561 | |
| 6562 | ;; Skip over type decl suffix operators. |
| 6563 | (while (if (looking-at c-type-decl-suffix-key) |
| 6564 | |
| 6565 | (if (eq (char-after) ?\)) |
| 6566 | (when (> paren-depth 0) |
| 6567 | (setq paren-depth (1- paren-depth)) |
| 6568 | (forward-char) |
| 6569 | t) |
| 6570 | (when (if (save-match-data (looking-at "\\s\(")) |
| 6571 | (c-safe (c-forward-sexp 1) t) |
| 6572 | (goto-char (match-end 1)) |
| 6573 | t) |
| 6574 | (when (and (not got-suffix-after-parens) |
| 6575 | (= paren-depth 0)) |
| 6576 | (setq got-suffix-after-parens (match-beginning 0))) |
| 6577 | (setq got-suffix t))) |
| 6578 | |
| 6579 | ;; No suffix matched. We might have matched the |
| 6580 | ;; identifier as a type and the open paren of a |
| 6581 | ;; function arglist as a type decl prefix. In that |
| 6582 | ;; case we should "backtrack": Reinterpret the last |
| 6583 | ;; type as the identifier, move out of the arglist and |
| 6584 | ;; continue searching for suffix operators. |
| 6585 | ;; |
| 6586 | ;; Do this even if there's no preceding type, to cope |
| 6587 | ;; with old style function declarations in K&R C, |
| 6588 | ;; (con|de)structors in C++ and `c-typeless-decl-kwds' |
| 6589 | ;; style declarations. That isn't applicable in an |
| 6590 | ;; arglist context, though. |
| 6591 | (when (and (= paren-depth 1) |
| 6592 | (not got-prefix-before-parens) |
| 6593 | (not (eq at-type t)) |
| 6594 | (or backup-at-type |
| 6595 | maybe-typeless |
| 6596 | backup-maybe-typeless |
| 6597 | (when c-recognize-typeless-decls |
| 6598 | (not context))) |
| 6599 | (setq pos (c-up-list-forward (point))) |
| 6600 | (eq (char-before pos) ?\))) |
| 6601 | (c-fdoc-shift-type-backward) |
| 6602 | (goto-char pos) |
| 6603 | t)) |
| 6604 | |
| 6605 | (c-forward-syntactic-ws)) |
| 6606 | |
| 6607 | (when (and (or maybe-typeless backup-maybe-typeless) |
| 6608 | (not got-identifier) |
| 6609 | (not got-prefix) |
| 6610 | at-type) |
| 6611 | ;; Have found no identifier but `c-typeless-decl-kwds' has |
| 6612 | ;; matched so we know we're inside a declaration. The |
| 6613 | ;; preceding type must be the identifier instead. |
| 6614 | (c-fdoc-shift-type-backward)) |
| 6615 | |
| 6616 | (setq |
| 6617 | at-decl-or-cast |
| 6618 | (catch 'at-decl-or-cast |
| 6619 | |
| 6620 | ;; CASE 1 |
| 6621 | (when (> paren-depth 0) |
| 6622 | ;; Encountered something inside parens that isn't matched by |
| 6623 | ;; the `c-type-decl-*' regexps, so it's not a type decl |
| 6624 | ;; expression. Try to skip out to the same paren depth to |
| 6625 | ;; not confuse the cast check below. |
| 6626 | (c-safe (goto-char (scan-lists (point) 1 paren-depth))) |
| 6627 | ;; If we've found a specifier keyword then it's a |
| 6628 | ;; declaration regardless. |
| 6629 | (throw 'at-decl-or-cast (eq at-decl-or-cast t))) |
| 6630 | |
| 6631 | (setq at-decl-end |
| 6632 | (looking-at (cond ((eq context '<>) "[,>]") |
| 6633 | (context "[,\)]") |
| 6634 | (t "[,;]")))) |
| 6635 | |
| 6636 | ;; Now we've collected info about various characteristics of |
| 6637 | ;; the construct we're looking at. Below follows a decision |
| 6638 | ;; tree based on that. It's ordered to check more certain |
| 6639 | ;; signs before less certain ones. |
| 6640 | |
| 6641 | (if got-identifier |
| 6642 | (progn |
| 6643 | |
| 6644 | ;; CASE 2 |
| 6645 | (when (and (or at-type maybe-typeless) |
| 6646 | (not (or got-prefix got-parens))) |
| 6647 | ;; Got another identifier directly after the type, so it's a |
| 6648 | ;; declaration. |
| 6649 | (throw 'at-decl-or-cast t)) |
| 6650 | |
| 6651 | (when (and got-parens |
| 6652 | (not got-prefix) |
| 6653 | (not got-suffix-after-parens) |
| 6654 | (or backup-at-type |
| 6655 | maybe-typeless |
| 6656 | backup-maybe-typeless)) |
| 6657 | ;; Got a declaration of the form "foo bar (gnu);" where we've |
| 6658 | ;; recognized "bar" as the type and "gnu" as the declarator. |
| 6659 | ;; In this case it's however more likely that "bar" is the |
| 6660 | ;; declarator and "gnu" a function argument or initializer (if |
| 6661 | ;; `c-recognize-paren-inits' is set), since the parens around |
| 6662 | ;; "gnu" would be superfluous if it's a declarator. Shift the |
| 6663 | ;; type one step backward. |
| 6664 | (c-fdoc-shift-type-backward))) |
| 6665 | |
| 6666 | ;; Found no identifier. |
| 6667 | |
| 6668 | (if backup-at-type |
| 6669 | (progn |
| 6670 | |
| 6671 | |
| 6672 | ;; CASE 3 |
| 6673 | (when (= (point) start) |
| 6674 | ;; Got a plain list of identifiers. If a colon follows it's |
| 6675 | ;; a valid label, or maybe a bitfield. Otherwise the last |
| 6676 | ;; one probably is the declared identifier and we should |
| 6677 | ;; back up to the previous type, providing it isn't a cast. |
| 6678 | (if (and (eq (char-after) ?:) |
| 6679 | (not (c-major-mode-is 'java-mode))) |
| 6680 | (cond |
| 6681 | ;; If we've found a specifier keyword then it's a |
| 6682 | ;; declaration regardless. |
| 6683 | ((eq at-decl-or-cast t) |
| 6684 | (throw 'at-decl-or-cast t)) |
| 6685 | ((and c-has-bitfields |
| 6686 | (eq at-decl-or-cast 'ids)) ; bitfield. |
| 6687 | (setq backup-if-not-cast t) |
| 6688 | (throw 'at-decl-or-cast t))) |
| 6689 | |
| 6690 | (setq backup-if-not-cast t) |
| 6691 | (throw 'at-decl-or-cast t))) |
| 6692 | |
| 6693 | ;; CASE 4 |
| 6694 | (when (and got-suffix |
| 6695 | (not got-prefix) |
| 6696 | (not got-parens)) |
| 6697 | ;; Got a plain list of identifiers followed by some suffix. |
| 6698 | ;; If this isn't a cast then the last identifier probably is |
| 6699 | ;; the declared one and we should back up to the previous |
| 6700 | ;; type. |
| 6701 | (setq backup-if-not-cast t) |
| 6702 | (throw 'at-decl-or-cast t))) |
| 6703 | |
| 6704 | ;; CASE 5 |
| 6705 | (when (eq at-type t) |
| 6706 | ;; If the type is known we know that there can't be any |
| 6707 | ;; identifier somewhere else, and it's only in declarations in |
| 6708 | ;; e.g. function prototypes and in casts that the identifier may |
| 6709 | ;; be left out. |
| 6710 | (throw 'at-decl-or-cast t)) |
| 6711 | |
| 6712 | (when (= (point) start) |
| 6713 | ;; Only got a single identifier (parsed as a type so far). |
| 6714 | ;; CASE 6 |
| 6715 | (if (and |
| 6716 | ;; Check that the identifier isn't at the start of an |
| 6717 | ;; expression. |
| 6718 | at-decl-end |
| 6719 | (cond |
| 6720 | ((eq context 'decl) |
| 6721 | ;; Inside an arglist that contains declarations. If K&R |
| 6722 | ;; style declarations and parenthesis style initializers |
| 6723 | ;; aren't allowed then the single identifier must be a |
| 6724 | ;; type, else we require that it's known or found |
| 6725 | ;; (primitive types are handled above). |
| 6726 | (or (and (not c-recognize-knr-p) |
| 6727 | (not c-recognize-paren-inits)) |
| 6728 | (memq at-type '(known found)))) |
| 6729 | ((eq context '<>) |
| 6730 | ;; Inside a template arglist. Accept known and found |
| 6731 | ;; types; other identifiers could just as well be |
| 6732 | ;; constants in C++. |
| 6733 | (memq at-type '(known found))))) |
| 6734 | (throw 'at-decl-or-cast t) |
| 6735 | ;; CASE 7 |
| 6736 | ;; Can't be a valid declaration or cast, but if we've found a |
| 6737 | ;; specifier it can't be anything else either, so treat it as |
| 6738 | ;; an invalid/unfinished declaration or cast. |
| 6739 | (throw 'at-decl-or-cast at-decl-or-cast)))) |
| 6740 | |
| 6741 | (if (and got-parens |
| 6742 | (not got-prefix) |
| 6743 | (not context) |
| 6744 | (not (eq at-type t)) |
| 6745 | (or backup-at-type |
| 6746 | maybe-typeless |
| 6747 | backup-maybe-typeless |
| 6748 | (when c-recognize-typeless-decls |
| 6749 | (or (not got-suffix) |
| 6750 | (not (looking-at |
| 6751 | c-after-suffixed-type-maybe-decl-key)))))) |
| 6752 | ;; Got an empty paren pair and a preceding type that probably |
| 6753 | ;; really is the identifier. Shift the type backwards to make |
| 6754 | ;; the last one the identifier. This is analogous to the |
| 6755 | ;; "backtracking" done inside the `c-type-decl-suffix-key' loop |
| 6756 | ;; above. |
| 6757 | ;; |
| 6758 | ;; Exception: In addition to the conditions in that |
| 6759 | ;; "backtracking" code, do not shift backward if we're not |
| 6760 | ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]". |
| 6761 | ;; Since there's no preceding type, the shift would mean that |
| 6762 | ;; the declaration is typeless. But if the regexp doesn't match |
| 6763 | ;; then we will simply fall through in the tests below and not |
| 6764 | ;; recognize it at all, so it's better to try it as an abstract |
| 6765 | ;; declarator instead. |
| 6766 | (c-fdoc-shift-type-backward) |
| 6767 | |
| 6768 | ;; Still no identifier. |
| 6769 | ;; CASE 8 |
| 6770 | (when (and got-prefix (or got-parens got-suffix)) |
| 6771 | ;; Require `got-prefix' together with either `got-parens' or |
| 6772 | ;; `got-suffix' to recognize it as an abstract declarator: |
| 6773 | ;; `got-parens' only is probably an empty function call. |
| 6774 | ;; `got-suffix' only can build an ordinary expression together |
| 6775 | ;; with the preceding identifier which we've taken as a type. |
| 6776 | ;; We could actually accept on `got-prefix' only, but that can |
| 6777 | ;; easily occur temporarily while writing an expression so we |
| 6778 | ;; avoid that case anyway. We could do a better job if we knew |
| 6779 | ;; the point when the fontification was invoked. |
| 6780 | (throw 'at-decl-or-cast t)) |
| 6781 | |
| 6782 | ;; CASE 9 |
| 6783 | (when (and at-type |
| 6784 | (not got-prefix) |
| 6785 | (not got-parens) |
| 6786 | got-suffix-after-parens |
| 6787 | (eq (char-after got-suffix-after-parens) ?\()) |
| 6788 | ;; Got a type, no declarator but a paren suffix. I.e. it's a |
| 6789 | ;; normal function call after all (or perhaps a C++ style object |
| 6790 | ;; instantiation expression). |
| 6791 | (throw 'at-decl-or-cast nil)))) |
| 6792 | |
| 6793 | ;; CASE 10 |
| 6794 | (when at-decl-or-cast |
| 6795 | ;; By now we've located the type in the declaration that we know |
| 6796 | ;; we're in. |
| 6797 | (throw 'at-decl-or-cast t)) |
| 6798 | |
| 6799 | ;; CASE 11 |
| 6800 | (when (and got-identifier |
| 6801 | (not context) |
| 6802 | (looking-at c-after-suffixed-type-decl-key) |
| 6803 | (if (and got-parens |
| 6804 | (not got-prefix) |
| 6805 | (not got-suffix) |
| 6806 | (not (eq at-type t))) |
| 6807 | ;; Shift the type backward in the case that there's a |
| 6808 | ;; single identifier inside parens. That can only |
| 6809 | ;; occur in K&R style function declarations so it's |
| 6810 | ;; more likely that it really is a function call. |
| 6811 | ;; Therefore we only do this after |
| 6812 | ;; `c-after-suffixed-type-decl-key' has matched. |
| 6813 | (progn (c-fdoc-shift-type-backward) t) |
| 6814 | got-suffix-after-parens)) |
| 6815 | ;; A declaration according to `c-after-suffixed-type-decl-key'. |
| 6816 | (throw 'at-decl-or-cast t)) |
| 6817 | |
| 6818 | ;; CASE 12 |
| 6819 | (when (and (or got-prefix (not got-parens)) |
| 6820 | (memq at-type '(t known))) |
| 6821 | ;; It's a declaration if a known type precedes it and it can't be a |
| 6822 | ;; function call. |
| 6823 | (throw 'at-decl-or-cast t)) |
| 6824 | |
| 6825 | ;; If we get here we can't tell if this is a type decl or a normal |
| 6826 | ;; expression by looking at it alone. (That's under the assumption |
| 6827 | ;; that normal expressions always can look like type decl expressions, |
| 6828 | ;; which isn't really true but the cases where it doesn't hold are so |
| 6829 | ;; uncommon (e.g. some placements of "const" in C++) it's not worth |
| 6830 | ;; the effort to look for them.) |
| 6831 | |
| 6832 | (unless (or at-decl-end (looking-at "=[^=]")) |
| 6833 | ;; If this is a declaration it should end here or its initializer(*) |
| 6834 | ;; should start here, so check for allowed separation tokens. Note |
| 6835 | ;; that this rule doesn't work e.g. with a K&R arglist after a |
| 6836 | ;; function header. |
| 6837 | ;; |
| 6838 | ;; *) Don't check for C++ style initializers using parens |
| 6839 | ;; since those already have been matched as suffixes. |
| 6840 | ;; |
| 6841 | ;; If `at-decl-or-cast' is then we've found some other sign that |
| 6842 | ;; it's a declaration or cast, so then it's probably an |
| 6843 | ;; invalid/unfinished one. |
| 6844 | (throw 'at-decl-or-cast at-decl-or-cast)) |
| 6845 | |
| 6846 | ;; Below are tests that only should be applied when we're certain to |
| 6847 | ;; not have parsed halfway through an expression. |
| 6848 | |
| 6849 | ;; CASE 14 |
| 6850 | (when (memq at-type '(t known)) |
| 6851 | ;; The expression starts with a known type so treat it as a |
| 6852 | ;; declaration. |
| 6853 | (throw 'at-decl-or-cast t)) |
| 6854 | |
| 6855 | ;; CASE 15 |
| 6856 | (when (and (c-major-mode-is 'c++-mode) |
| 6857 | ;; In C++ we check if the identifier is a known type, since |
| 6858 | ;; (con|de)structors use the class name as identifier. |
| 6859 | ;; We've always shifted over the identifier as a type and |
| 6860 | ;; then backed up again in this case. |
| 6861 | identifier-type |
| 6862 | (or (memq identifier-type '(found known)) |
| 6863 | (and (eq (char-after identifier-start) ?~) |
| 6864 | ;; `at-type' probably won't be 'found for |
| 6865 | ;; destructors since the "~" is then part of the |
| 6866 | ;; type name being checked against the list of |
| 6867 | ;; known types, so do a check without that |
| 6868 | ;; operator. |
| 6869 | (or (save-excursion |
| 6870 | (goto-char (1+ identifier-start)) |
| 6871 | (c-forward-syntactic-ws) |
| 6872 | (c-with-syntax-table |
| 6873 | c-identifier-syntax-table |
| 6874 | (looking-at c-known-type-key))) |
| 6875 | (save-excursion |
| 6876 | (goto-char (1+ identifier-start)) |
| 6877 | ;; We have already parsed the type earlier, |
| 6878 | ;; so it'd be possible to cache the end |
| 6879 | ;; position instead of redoing it here, but |
| 6880 | ;; then we'd need to keep track of another |
| 6881 | ;; position everywhere. |
| 6882 | (c-check-type (point) |
| 6883 | (progn (c-forward-type) |
| 6884 | (point)))))))) |
| 6885 | (throw 'at-decl-or-cast t)) |
| 6886 | |
| 6887 | (if got-identifier |
| 6888 | (progn |
| 6889 | ;; CASE 16 |
| 6890 | (when (and got-prefix-before-parens |
| 6891 | at-type |
| 6892 | (or at-decl-end (looking-at "=[^=]")) |
| 6893 | (not context) |
| 6894 | (not got-suffix)) |
| 6895 | ;; Got something like "foo * bar;". Since we're not inside an |
| 6896 | ;; arglist it would be a meaningless expression because the |
| 6897 | ;; result isn't used. We therefore choose to recognize it as |
| 6898 | ;; a declaration. Do not allow a suffix since it could then |
| 6899 | ;; be a function call. |
| 6900 | (throw 'at-decl-or-cast t)) |
| 6901 | |
| 6902 | ;; CASE 17 |
| 6903 | (when (and (or got-suffix-after-parens |
| 6904 | (looking-at "=[^=]")) |
| 6905 | (eq at-type 'found) |
| 6906 | (not (eq context 'arglist))) |
| 6907 | ;; Got something like "a (*b) (c);" or "a (b) = c;". It could |
| 6908 | ;; be an odd expression or it could be a declaration. Treat |
| 6909 | ;; it as a declaration if "a" has been used as a type |
| 6910 | ;; somewhere else (if it's a known type we won't get here). |
| 6911 | (throw 'at-decl-or-cast t))) |
| 6912 | |
| 6913 | ;; CASE 18 |
| 6914 | (when (and context |
| 6915 | (or got-prefix |
| 6916 | (and (eq context 'decl) |
| 6917 | (not c-recognize-paren-inits) |
| 6918 | (or got-parens got-suffix)))) |
| 6919 | ;; Got a type followed by an abstract declarator. If `got-prefix' |
| 6920 | ;; is set it's something like "a *" without anything after it. If |
| 6921 | ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]", |
| 6922 | ;; or similar, which we accept only if the context rules out |
| 6923 | ;; expressions. |
| 6924 | (throw 'at-decl-or-cast t))) |
| 6925 | |
| 6926 | ;; If we had a complete symbol table here (which rules out |
| 6927 | ;; `c-found-types') we should return t due to the disambiguation rule |
| 6928 | ;; (in at least C++) that anything that can be parsed as a declaration |
| 6929 | ;; is a declaration. Now we're being more defensive and prefer to |
| 6930 | ;; highlight things like "foo (bar);" as a declaration only if we're |
| 6931 | ;; inside an arglist that contains declarations. |
| 6932 | (eq context 'decl)))) |
| 6933 | |
| 6934 | ;; The point is now after the type decl expression. |
| 6935 | |
| 6936 | (cond |
| 6937 | ;; Check for a cast. |
| 6938 | ((save-excursion |
| 6939 | (and |
| 6940 | c-cast-parens |
| 6941 | |
| 6942 | ;; Should be the first type/identifier in a cast paren. |
| 6943 | (> preceding-token-end (point-min)) |
| 6944 | (memq (char-before preceding-token-end) c-cast-parens) |
| 6945 | |
| 6946 | ;; The closing paren should follow. |
| 6947 | (progn |
| 6948 | (c-forward-syntactic-ws) |
| 6949 | (looking-at "\\s\)")) |
| 6950 | |
| 6951 | ;; There should be a primary expression after it. |
| 6952 | (let (pos) |
| 6953 | (forward-char) |
| 6954 | (c-forward-syntactic-ws) |
| 6955 | (setq cast-end (point)) |
| 6956 | (and (looking-at c-primary-expr-regexp) |
| 6957 | (progn |
| 6958 | (setq pos (match-end 0)) |
| 6959 | (or |
| 6960 | ;; Check if the expression begins with a prefix keyword. |
| 6961 | (match-beginning 2) |
| 6962 | (if (match-beginning 1) |
| 6963 | ;; Expression begins with an ambiguous operator. Treat |
| 6964 | ;; it as a cast if it's a type decl or if we've |
| 6965 | ;; recognized the type somewhere else. |
| 6966 | (or at-decl-or-cast |
| 6967 | (memq at-type '(t known found))) |
| 6968 | ;; Unless it's a keyword, it's the beginning of a primary |
| 6969 | ;; expression. |
| 6970 | (not (looking-at c-keywords-regexp))))) |
| 6971 | ;; If `c-primary-expr-regexp' matched a nonsymbol token, check |
| 6972 | ;; that it matched a whole one so that we don't e.g. confuse |
| 6973 | ;; the operator '-' with '->'. It's ok if it matches further, |
| 6974 | ;; though, since it e.g. can match the float '.5' while the |
| 6975 | ;; operator regexp only matches '.'. |
| 6976 | (or (not (looking-at c-nonsymbol-token-regexp)) |
| 6977 | (<= (match-end 0) pos)))) |
| 6978 | |
| 6979 | ;; There should either be a cast before it or something that isn't an |
| 6980 | ;; identifier or close paren. |
| 6981 | (> preceding-token-end (point-min)) |
| 6982 | (progn |
| 6983 | (goto-char (1- preceding-token-end)) |
| 6984 | (or (eq (point) last-cast-end) |
| 6985 | (progn |
| 6986 | (c-backward-syntactic-ws) |
| 6987 | (if (< (skip-syntax-backward "w_") 0) |
| 6988 | ;; It's a symbol. Accept it only if it's one of the |
| 6989 | ;; keywords that can precede an expression (without |
| 6990 | ;; surrounding parens). |
| 6991 | (looking-at c-simple-stmt-key) |
| 6992 | (and |
| 6993 | ;; Check that it isn't a close paren (block close is ok, |
| 6994 | ;; though). |
| 6995 | (not (memq (char-before) '(?\) ?\]))) |
| 6996 | ;; Check that it isn't a nonsymbol identifier. |
| 6997 | (not (c-on-identifier))))))))) |
| 6998 | |
| 6999 | ;; Handle the cast. |
| 7000 | (when (and c-record-type-identifiers at-type (not (eq at-type t))) |
| 7001 | (let ((c-promote-possible-types t)) |
| 7002 | (goto-char type-start) |
| 7003 | (c-forward-type))) |
| 7004 | |
| 7005 | (goto-char cast-end) |
| 7006 | 'cast) |
| 7007 | |
| 7008 | (at-decl-or-cast |
| 7009 | ;; We're at a declaration. Highlight the type and the following |
| 7010 | ;; declarators. |
| 7011 | |
| 7012 | (when backup-if-not-cast |
| 7013 | (c-fdoc-shift-type-backward t)) |
| 7014 | |
| 7015 | (when (and (eq context 'decl) (looking-at ",")) |
| 7016 | ;; Make sure to propagate the `c-decl-arg-start' property to |
| 7017 | ;; the next argument if it's set in this one, to cope with |
| 7018 | ;; interactive refontification. |
| 7019 | (c-put-c-type-property (point) 'c-decl-arg-start)) |
| 7020 | |
| 7021 | (when (and c-record-type-identifiers at-type (not (eq at-type t))) |
| 7022 | (let ((c-promote-possible-types t)) |
| 7023 | (save-excursion |
| 7024 | (goto-char type-start) |
| 7025 | (c-forward-type)))) |
| 7026 | |
| 7027 | (cons id-start |
| 7028 | (and (or at-type-decl at-typedef) |
| 7029 | (cons at-type-decl at-typedef)))) |
| 7030 | |
| 7031 | (t |
| 7032 | ;; False alarm. Restore the recorded ranges. |
| 7033 | (setq c-record-type-identifiers save-rec-type-ids |
| 7034 | c-record-ref-identifiers save-rec-ref-ids) |
| 7035 | nil)))) |
| 7036 | |
| 7037 | (defun c-forward-label (&optional assume-markup preceding-token-end limit) |
| 7038 | ;; Assuming that point is at the beginning of a token, check if it starts a |
| 7039 | ;; label and if so move over it and return non-nil (t in default situations, |
| 7040 | ;; specific symbols (see below) for interesting situations), otherwise don't |
| 7041 | ;; move and return nil. "Label" here means "most things with a colon". |
| 7042 | ;; |
| 7043 | ;; More precisely, a "label" is regarded as one of: |
| 7044 | ;; (i) a goto target like "foo:" - returns the symbol `goto-target'; |
| 7045 | ;; (ii) A case label - either the entire construct "case FOO:", or just the |
| 7046 | ;; bare "case", should the colon be missing. We return t; |
| 7047 | ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We |
| 7048 | ;; return t; |
| 7049 | ;; (iv) One of QT's "extended" C++ variants of |
| 7050 | ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:". |
| 7051 | ;; Returns the symbol `qt-2kwds-colon'. |
| 7052 | ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'. |
| 7053 | ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any |
| 7054 | ;; colon). Currently (2006-03), this applies only to Objective C's |
| 7055 | ;; keywords "@private", "@protected", and "@public". Returns t. |
| 7056 | ;; |
| 7057 | ;; One of the things which will NOT be recognized as a label is a bit-field |
| 7058 | ;; element of a struct, something like "int foo:5". |
| 7059 | ;; |
| 7060 | ;; The end of the label is taken to be just after the colon, or the end of |
| 7061 | ;; the first submatch in `c-opt-extra-label-key'. The point is directly |
| 7062 | ;; after the end on return. The terminating char gets marked with |
| 7063 | ;; `c-decl-end' to improve recognition of the following declaration or |
| 7064 | ;; statement. |
| 7065 | ;; |
| 7066 | ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding |
| 7067 | ;; label, if any, has already been marked up like that. |
| 7068 | ;; |
| 7069 | ;; If PRECEDING-TOKEN-END is given, it should be the first position |
| 7070 | ;; after the preceding token, i.e. on the other side of the |
| 7071 | ;; syntactic ws from the point. Use a value less than or equal to |
| 7072 | ;; (point-min) if the point is at the first token in (the visible |
| 7073 | ;; part of) the buffer. |
| 7074 | ;; |
| 7075 | ;; The optional LIMIT limits the forward scan for the colon. |
| 7076 | ;; |
| 7077 | ;; This function records the ranges of the label symbols on |
| 7078 | ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is |
| 7079 | ;; non-nil. |
| 7080 | ;; |
| 7081 | ;; This function might do hidden buffer changes. |
| 7082 | |
| 7083 | (let ((start (point)) |
| 7084 | label-end |
| 7085 | qt-symbol-idx |
| 7086 | macro-start ; if we're in one. |
| 7087 | label-type |
| 7088 | kwd) |
| 7089 | (cond |
| 7090 | ;; "case" or "default" (Doesn't apply to AWK). |
| 7091 | ((looking-at c-label-kwds-regexp) |
| 7092 | (let ((kwd-end (match-end 1))) |
| 7093 | ;; Record only the keyword itself for fontification, since in |
| 7094 | ;; case labels the following is a constant expression and not |
| 7095 | ;; a label. |
| 7096 | (when c-record-type-identifiers |
| 7097 | (c-record-ref-id (cons (match-beginning 1) kwd-end))) |
| 7098 | |
| 7099 | ;; Find the label end. |
| 7100 | (goto-char kwd-end) |
| 7101 | (setq label-type |
| 7102 | (if (and (c-syntactic-re-search-forward |
| 7103 | ;; Stop on chars that aren't allowed in expressions, |
| 7104 | ;; and on operator chars that would be meaningless |
| 7105 | ;; there. FIXME: This doesn't cope with ?: operators. |
| 7106 | "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)" |
| 7107 | limit t t nil 1) |
| 7108 | (match-beginning 2)) |
| 7109 | |
| 7110 | (progn ; there's a proper : |
| 7111 | (goto-char (match-beginning 2)) ; just after the : |
| 7112 | (c-put-c-type-property (1- (point)) 'c-decl-end) |
| 7113 | t) |
| 7114 | |
| 7115 | ;; It's an unfinished label. We consider the keyword enough |
| 7116 | ;; to recognize it as a label, so that it gets fontified. |
| 7117 | ;; Leave the point at the end of it, but don't put any |
| 7118 | ;; `c-decl-end' marker. |
| 7119 | (goto-char kwd-end) |
| 7120 | t)))) |
| 7121 | |
| 7122 | ;; @private, @protected, @public, in Objective C, or similar. |
| 7123 | ((and c-opt-extra-label-key |
| 7124 | (looking-at c-opt-extra-label-key)) |
| 7125 | ;; For a `c-opt-extra-label-key' match, we record the whole |
| 7126 | ;; thing for fontification. That's to get the leading '@' in |
| 7127 | ;; Objective-C protection labels fontified. |
| 7128 | (goto-char (match-end 1)) |
| 7129 | (when c-record-type-identifiers |
| 7130 | (c-record-ref-id (cons (match-beginning 1) (point)))) |
| 7131 | (c-put-c-type-property (1- (point)) 'c-decl-end) |
| 7132 | (setq label-type t)) |
| 7133 | |
| 7134 | ;; All other cases of labels. |
| 7135 | ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t. |
| 7136 | |
| 7137 | ;; A colon label must have something before the colon. |
| 7138 | (not (eq (char-after) ?:)) |
| 7139 | |
| 7140 | ;; Check that we're not after a token that can't precede a label. |
| 7141 | (or |
| 7142 | ;; Trivially succeeds when there's no preceding token. |
| 7143 | ;; Succeeds when we're at a virtual semicolon. |
| 7144 | (if preceding-token-end |
| 7145 | (<= preceding-token-end (point-min)) |
| 7146 | (save-excursion |
| 7147 | (c-backward-syntactic-ws) |
| 7148 | (setq preceding-token-end (point)) |
| 7149 | (or (bobp) |
| 7150 | (c-at-vsemi-p)))) |
| 7151 | |
| 7152 | ;; Check if we're after a label, if we're after a closing |
| 7153 | ;; paren that belong to statement, and with |
| 7154 | ;; `c-label-prefix-re'. It's done in different order |
| 7155 | ;; depending on `assume-markup' since the checks have |
| 7156 | ;; different expensiveness. |
| 7157 | (if assume-markup |
| 7158 | (or |
| 7159 | (eq (c-get-char-property (1- preceding-token-end) 'c-type) |
| 7160 | 'c-decl-end) |
| 7161 | |
| 7162 | (save-excursion |
| 7163 | (goto-char (1- preceding-token-end)) |
| 7164 | (c-beginning-of-current-token) |
| 7165 | (or (looking-at c-label-prefix-re) |
| 7166 | (looking-at c-block-stmt-1-key))) |
| 7167 | |
| 7168 | (and (eq (char-before preceding-token-end) ?\)) |
| 7169 | (c-after-conditional))) |
| 7170 | |
| 7171 | (or |
| 7172 | (save-excursion |
| 7173 | (goto-char (1- preceding-token-end)) |
| 7174 | (c-beginning-of-current-token) |
| 7175 | (or (looking-at c-label-prefix-re) |
| 7176 | (looking-at c-block-stmt-1-key))) |
| 7177 | |
| 7178 | (cond |
| 7179 | ((eq (char-before preceding-token-end) ?\)) |
| 7180 | (c-after-conditional)) |
| 7181 | |
| 7182 | ((eq (char-before preceding-token-end) ?:) |
| 7183 | ;; Might be after another label, so check it recursively. |
| 7184 | (save-restriction |
| 7185 | (save-excursion |
| 7186 | (goto-char (1- preceding-token-end)) |
| 7187 | ;; Essentially the same as the |
| 7188 | ;; `c-syntactic-re-search-forward' regexp below. |
| 7189 | (setq macro-start |
| 7190 | (save-excursion (and (c-beginning-of-macro) |
| 7191 | (point)))) |
| 7192 | (if macro-start (narrow-to-region macro-start (point-max))) |
| 7193 | (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t) |
| 7194 | ;; Note: the following should work instead of the |
| 7195 | ;; narrow-to-region above. Investigate why not, |
| 7196 | ;; sometime. ACM, 2006-03-31. |
| 7197 | ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" |
| 7198 | ;; macro-start t) |
| 7199 | (let ((pte (point)) |
| 7200 | ;; If the caller turned on recording for us, |
| 7201 | ;; it shouldn't apply when we check the |
| 7202 | ;; preceding label. |
| 7203 | c-record-type-identifiers) |
| 7204 | ;; A label can't start at a cpp directive. Check for |
| 7205 | ;; this, since c-forward-syntactic-ws would foul up on it. |
| 7206 | (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix)) |
| 7207 | (c-forward-syntactic-ws) |
| 7208 | (c-forward-label nil pte start)))))))))) |
| 7209 | |
| 7210 | ;; Point is still at the beginning of the possible label construct. |
| 7211 | ;; |
| 7212 | ;; Check that the next nonsymbol token is ":", or that we're in one |
| 7213 | ;; of QT's "slots" declarations. Allow '(' for the sake of macro |
| 7214 | ;; arguments. FIXME: Should build this regexp from the language |
| 7215 | ;; constants. |
| 7216 | (cond |
| 7217 | ;; public: protected: private: |
| 7218 | ((and |
| 7219 | (c-major-mode-is 'c++-mode) |
| 7220 | (search-forward-regexp |
| 7221 | "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t) |
| 7222 | (progn (backward-char) |
| 7223 | (c-forward-syntactic-ws limit) |
| 7224 | (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon. |
| 7225 | (forward-char) |
| 7226 | (setq label-type t)) |
| 7227 | ;; QT double keyword like "protected slots:" or goto target. |
| 7228 | ((progn (goto-char start) nil)) |
| 7229 | ((when (c-syntactic-re-search-forward |
| 7230 | "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB |
| 7231 | (backward-char) |
| 7232 | (setq label-end (point)) |
| 7233 | (setq qt-symbol-idx |
| 7234 | (and (c-major-mode-is 'c++-mode) |
| 7235 | (string-match |
| 7236 | "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>" |
| 7237 | (buffer-substring start (point))))) |
| 7238 | (c-forward-syntactic-ws limit) |
| 7239 | (cond |
| 7240 | ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon. |
| 7241 | (forward-char) |
| 7242 | (setq label-type |
| 7243 | (if (or (string= "signals" ; Special QT macro |
| 7244 | (setq kwd (buffer-substring-no-properties start label-end))) |
| 7245 | (string= "Q_SIGNALS" kwd)) |
| 7246 | 'qt-1kwd-colon |
| 7247 | 'goto-target))) |
| 7248 | ((and qt-symbol-idx |
| 7249 | (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t) |
| 7250 | (progn (c-forward-syntactic-ws limit) |
| 7251 | (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon |
| 7252 | (forward-char) |
| 7253 | (setq label-type 'qt-2kwds-colon))))))) |
| 7254 | |
| 7255 | (save-restriction |
| 7256 | (narrow-to-region start (point)) |
| 7257 | |
| 7258 | ;; Check that `c-nonlabel-token-key' doesn't match anywhere. |
| 7259 | (catch 'check-label |
| 7260 | (goto-char start) |
| 7261 | (while (progn |
| 7262 | (when (looking-at c-nonlabel-token-key) |
| 7263 | (goto-char start) |
| 7264 | (setq label-type nil) |
| 7265 | (throw 'check-label nil)) |
| 7266 | (and (c-safe (c-forward-sexp) |
| 7267 | (c-forward-syntactic-ws) |
| 7268 | t) |
| 7269 | (not (eobp))))) |
| 7270 | |
| 7271 | ;; Record the identifiers in the label for fontification, unless |
| 7272 | ;; it begins with `c-label-kwds' in which case the following |
| 7273 | ;; identifiers are part of a (constant) expression that |
| 7274 | ;; shouldn't be fontified. |
| 7275 | (when (and c-record-type-identifiers |
| 7276 | (progn (goto-char start) |
| 7277 | (not (looking-at c-label-kwds-regexp)))) |
| 7278 | (while (c-syntactic-re-search-forward c-symbol-key nil t) |
| 7279 | (c-record-ref-id (cons (match-beginning 0) |
| 7280 | (match-end 0))))) |
| 7281 | |
| 7282 | (c-put-c-type-property (1- (point-max)) 'c-decl-end) |
| 7283 | (goto-char (point-max))))) |
| 7284 | |
| 7285 | (t |
| 7286 | ;; Not a label. |
| 7287 | (goto-char start))) |
| 7288 | label-type)) |
| 7289 | |
| 7290 | (defun c-forward-objc-directive () |
| 7291 | ;; Assuming the point is at the beginning of a token, try to move |
| 7292 | ;; forward to the end of the Objective-C directive that starts |
| 7293 | ;; there. Return t if a directive was fully recognized, otherwise |
| 7294 | ;; the point is moved as far as one could be successfully parsed and |
| 7295 | ;; nil is returned. |
| 7296 | ;; |
| 7297 | ;; This function records identifier ranges on |
| 7298 | ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if |
| 7299 | ;; `c-record-type-identifiers' is non-nil. |
| 7300 | ;; |
| 7301 | ;; This function might do hidden buffer changes. |
| 7302 | |
| 7303 | (let ((start (point)) |
| 7304 | start-char |
| 7305 | (c-promote-possible-types t) |
| 7306 | ;; Turn off recognition of angle bracket arglists while parsing |
| 7307 | ;; types here since the protocol reference list might then be |
| 7308 | ;; considered part of the preceding name or superclass-name. |
| 7309 | c-recognize-<>-arglists) |
| 7310 | |
| 7311 | (if (or |
| 7312 | (when (looking-at |
| 7313 | (eval-when-compile |
| 7314 | (c-make-keywords-re t |
| 7315 | (append (c-lang-const c-protection-kwds objc) |
| 7316 | '("@end")) |
| 7317 | 'objc-mode))) |
| 7318 | (goto-char (match-end 1)) |
| 7319 | t) |
| 7320 | |
| 7321 | (and |
| 7322 | (looking-at |
| 7323 | (eval-when-compile |
| 7324 | (c-make-keywords-re t |
| 7325 | '("@interface" "@implementation" "@protocol") |
| 7326 | 'objc-mode))) |
| 7327 | |
| 7328 | ;; Handle the name of the class itself. |
| 7329 | (progn |
| 7330 | ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's |
| 7331 | ; at EOB. |
| 7332 | (goto-char (match-end 0)) |
| 7333 | (c-skip-ws-forward) |
| 7334 | (c-forward-type)) |
| 7335 | |
| 7336 | (catch 'break |
| 7337 | ;; Look for ": superclass-name" or "( category-name )". |
| 7338 | (when (looking-at "[:\(]") |
| 7339 | (setq start-char (char-after)) |
| 7340 | (forward-char) |
| 7341 | (c-forward-syntactic-ws) |
| 7342 | (unless (c-forward-type) (throw 'break nil)) |
| 7343 | (when (eq start-char ?\() |
| 7344 | (unless (eq (char-after) ?\)) (throw 'break nil)) |
| 7345 | (forward-char) |
| 7346 | (c-forward-syntactic-ws))) |
| 7347 | |
| 7348 | ;; Look for a protocol reference list. |
| 7349 | (if (eq (char-after) ?<) |
| 7350 | (let ((c-recognize-<>-arglists t) |
| 7351 | (c-parse-and-markup-<>-arglists t) |
| 7352 | c-restricted-<>-arglists) |
| 7353 | (c-forward-<>-arglist t)) |
| 7354 | t)))) |
| 7355 | |
| 7356 | (progn |
| 7357 | (c-backward-syntactic-ws) |
| 7358 | (c-clear-c-type-property start (1- (point)) 'c-decl-end) |
| 7359 | (c-put-c-type-property (1- (point)) 'c-decl-end) |
| 7360 | t) |
| 7361 | |
| 7362 | (c-clear-c-type-property start (point) 'c-decl-end) |
| 7363 | nil))) |
| 7364 | |
| 7365 | (defun c-beginning-of-inheritance-list (&optional lim) |
| 7366 | ;; Go to the first non-whitespace after the colon that starts a |
| 7367 | ;; multiple inheritance introduction. Optional LIM is the farthest |
| 7368 | ;; back we should search. |
| 7369 | ;; |
| 7370 | ;; This function might do hidden buffer changes. |
| 7371 | (c-with-syntax-table c++-template-syntax-table |
| 7372 | (c-backward-token-2 0 t lim) |
| 7373 | (while (and (or (looking-at c-symbol-start) |
| 7374 | (looking-at "[<,]\\|::")) |
| 7375 | (zerop (c-backward-token-2 1 t lim)))))) |
| 7376 | |
| 7377 | (defun c-in-method-def-p () |
| 7378 | ;; Return nil if we aren't in a method definition, otherwise the |
| 7379 | ;; position of the initial [+-]. |
| 7380 | ;; |
| 7381 | ;; This function might do hidden buffer changes. |
| 7382 | (save-excursion |
| 7383 | (beginning-of-line) |
| 7384 | (and c-opt-method-key |
| 7385 | (looking-at c-opt-method-key) |
| 7386 | (point)) |
| 7387 | )) |
| 7388 | |
| 7389 | ;; Contributed by Kevin Ryde <user42@zip.com.au>. |
| 7390 | (defun c-in-gcc-asm-p () |
| 7391 | ;; Return non-nil if point is within a gcc \"asm\" block. |
| 7392 | ;; |
| 7393 | ;; This should be called with point inside an argument list. |
| 7394 | ;; |
| 7395 | ;; Only one level of enclosing parentheses is considered, so for |
| 7396 | ;; instance `nil' is returned when in a function call within an asm |
| 7397 | ;; operand. |
| 7398 | ;; |
| 7399 | ;; This function might do hidden buffer changes. |
| 7400 | |
| 7401 | (and c-opt-asm-stmt-key |
| 7402 | (save-excursion |
| 7403 | (beginning-of-line) |
| 7404 | (backward-up-list 1) |
| 7405 | (c-beginning-of-statement-1 (point-min) nil t) |
| 7406 | (looking-at c-opt-asm-stmt-key)))) |
| 7407 | |
| 7408 | (defun c-at-toplevel-p () |
| 7409 | "Return a determination as to whether point is \"at the top level\". |
| 7410 | Informally, \"at the top level\" is anywhere where you can write |
| 7411 | a function. |
| 7412 | |
| 7413 | More precisely, being at the top-level means that point is either |
| 7414 | outside any enclosing block (such as a function definition), or |
| 7415 | directly inside a class, namespace or other block that contains |
| 7416 | another declaration level. |
| 7417 | |
| 7418 | If point is not at the top-level (e.g. it is inside a method |
| 7419 | definition), then nil is returned. Otherwise, if point is at a |
| 7420 | top-level not enclosed within a class definition, t is returned. |
| 7421 | Otherwise, a 2-vector is returned where the zeroth element is the |
| 7422 | buffer position of the start of the class declaration, and the first |
| 7423 | element is the buffer position of the enclosing class's opening |
| 7424 | brace. |
| 7425 | |
| 7426 | Note that this function might do hidden buffer changes. See the |
| 7427 | comment at the start of cc-engine.el for more info." |
| 7428 | (let ((paren-state (c-parse-state))) |
| 7429 | (or (not (c-most-enclosing-brace paren-state)) |
| 7430 | (c-search-uplist-for-classkey paren-state)))) |
| 7431 | |
| 7432 | (defun c-just-after-func-arglist-p (&optional lim) |
| 7433 | ;; Return non-nil if the point is in the region after the argument |
| 7434 | ;; list of a function and its opening brace (or semicolon in case it |
| 7435 | ;; got no body). If there are K&R style argument declarations in |
| 7436 | ;; that region, the point has to be inside the first one for this |
| 7437 | ;; function to recognize it. |
| 7438 | ;; |
| 7439 | ;; If successful, the point is moved to the first token after the |
| 7440 | ;; function header (see `c-forward-decl-or-cast-1' for details) and |
| 7441 | ;; the position of the opening paren of the function arglist is |
| 7442 | ;; returned. |
| 7443 | ;; |
| 7444 | ;; The point is clobbered if not successful. |
| 7445 | ;; |
| 7446 | ;; LIM is used as bound for backward buffer searches. |
| 7447 | ;; |
| 7448 | ;; This function might do hidden buffer changes. |
| 7449 | |
| 7450 | (let ((beg (point)) end id-start) |
| 7451 | (and |
| 7452 | (eq (c-beginning-of-statement-1 lim) 'same) |
| 7453 | |
| 7454 | (not (or (c-major-mode-is 'objc-mode) |
| 7455 | (c-forward-objc-directive))) |
| 7456 | |
| 7457 | (setq id-start |
| 7458 | (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))) |
| 7459 | (< id-start beg) |
| 7460 | |
| 7461 | ;; There should not be a '=' or ',' between beg and the |
| 7462 | ;; start of the declaration since that means we were in the |
| 7463 | ;; "expression part" of the declaration. |
| 7464 | (or (> (point) beg) |
| 7465 | (not (looking-at "[=,]"))) |
| 7466 | |
| 7467 | (save-excursion |
| 7468 | ;; Check that there's an arglist paren in the |
| 7469 | ;; declaration. |
| 7470 | (goto-char id-start) |
| 7471 | (cond ((eq (char-after) ?\() |
| 7472 | ;; The declarator is a paren expression, so skip past it |
| 7473 | ;; so that we don't get stuck on that instead of the |
| 7474 | ;; function arglist. |
| 7475 | (c-forward-sexp)) |
| 7476 | ((and c-opt-op-identifier-prefix |
| 7477 | (looking-at c-opt-op-identifier-prefix)) |
| 7478 | ;; Don't trip up on "operator ()". |
| 7479 | (c-forward-token-2 2 t))) |
| 7480 | (and (< (point) beg) |
| 7481 | (c-syntactic-re-search-forward "(" beg t t) |
| 7482 | (1- (point))))))) |
| 7483 | |
| 7484 | (defun c-in-knr-argdecl (&optional lim) |
| 7485 | ;; Return the position of the first argument declaration if point is |
| 7486 | ;; inside a K&R style argument declaration list, nil otherwise. |
| 7487 | ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a |
| 7488 | ;; position that bounds the backward search for the argument list. |
| 7489 | ;; |
| 7490 | ;; Point must be within a possible K&R region, e.g. just before a top-level |
| 7491 | ;; "{". It must be outside of parens and brackets. The test can return |
| 7492 | ;; false positives otherwise. |
| 7493 | ;; |
| 7494 | ;; This function might do hidden buffer changes. |
| 7495 | |
| 7496 | (save-excursion |
| 7497 | (save-restriction |
| 7498 | ;; If we're in a macro, our search range is restricted to it. Narrow to |
| 7499 | ;; the searchable range. |
| 7500 | (let* ((macro-start (c-query-macro-start)) |
| 7501 | (lim (max (or lim (point-min)) (or macro-start (point-min)))) |
| 7502 | before-lparen after-rparen |
| 7503 | (pp-count-out 20)) ; Max number of paren/brace constructs before we give up |
| 7504 | (narrow-to-region lim (c-point 'eol)) |
| 7505 | |
| 7506 | ;; Search backwards for the defun's argument list. We give up if we |
| 7507 | ;; encounter a "}" (end of a previous defun) or BOB. |
| 7508 | ;; |
| 7509 | ;; The criterion for a paren structure being the arg list is: |
| 7510 | ;; o - there is non-WS stuff after it but before any "{"; AND |
| 7511 | ;; o - the token after it isn't a ";" AND |
| 7512 | ;; o - it is preceded by either an identifier (the function name) or |
| 7513 | ;; a macro expansion like "DEFUN (...)"; AND |
| 7514 | ;; o - its content is a non-empty comma-separated list of identifiers |
| 7515 | ;; (an empty arg list won't have a knr region). |
| 7516 | ;; |
| 7517 | ;; The following snippet illustrates these rules: |
| 7518 | ;; int foo (bar, baz, yuk) |
| 7519 | ;; int bar [] ; |
| 7520 | ;; int (*baz) (my_type) ; |
| 7521 | ;; int (*) (void) (*yuk) (void) ; |
| 7522 | ;; { |
| 7523 | |
| 7524 | (catch 'knr |
| 7525 | (while (> pp-count-out 0) ; go back one paren/bracket pair each time. |
| 7526 | (setq pp-count-out (1- pp-count-out)) |
| 7527 | (c-syntactic-skip-backward "^)]}") |
| 7528 | (cond ((eq (char-before) ?\)) |
| 7529 | (setq after-rparen (point))) |
| 7530 | ((eq (char-before) ?\]) |
| 7531 | (setq after-rparen nil)) |
| 7532 | (t ; either } (hit previous defun) or no more parens/brackets |
| 7533 | (throw 'knr nil))) |
| 7534 | |
| 7535 | (if after-rparen |
| 7536 | ;; We're inside a paren. Could it be our argument list....? |
| 7537 | (if |
| 7538 | (and |
| 7539 | (progn |
| 7540 | (goto-char after-rparen) |
| 7541 | (unless (c-go-list-backward) (throw 'knr nil)) ; |
| 7542 | ;; FIXME!!! What about macros between the parens? 2007/01/20 |
| 7543 | (setq before-lparen (point))) |
| 7544 | |
| 7545 | ;; It can't be the arg list if next token is ; or { |
| 7546 | (progn (goto-char after-rparen) |
| 7547 | (c-forward-syntactic-ws) |
| 7548 | (not (memq (char-after) '(?\; ?\{)))) |
| 7549 | |
| 7550 | ;; Is the thing preceding the list an identifier (the |
| 7551 | ;; function name), or a macro expansion? |
| 7552 | (progn |
| 7553 | (goto-char before-lparen) |
| 7554 | (eq (c-backward-token-2) 0) |
| 7555 | (or (c-on-identifier) |
| 7556 | (and (eq (char-after) ?\)) |
| 7557 | (c-go-up-list-backward) |
| 7558 | (eq (c-backward-token-2) 0) |
| 7559 | (c-on-identifier)))) |
| 7560 | |
| 7561 | ;; Have we got a non-empty list of comma-separated |
| 7562 | ;; identifiers? |
| 7563 | (progn |
| 7564 | (goto-char before-lparen) |
| 7565 | (c-forward-token-2) ; to first token inside parens |
| 7566 | (and |
| 7567 | (c-on-identifier) |
| 7568 | (c-forward-token-2) |
| 7569 | (catch 'id-list |
| 7570 | (while (eq (char-after) ?\,) |
| 7571 | (c-forward-token-2) |
| 7572 | (unless (c-on-identifier) (throw 'id-list nil)) |
| 7573 | (c-forward-token-2)) |
| 7574 | (eq (char-after) ?\)))))) |
| 7575 | |
| 7576 | ;; ...Yes. We've identified the function's argument list. |
| 7577 | (throw 'knr |
| 7578 | (progn (goto-char after-rparen) |
| 7579 | (c-forward-syntactic-ws) |
| 7580 | (point))) |
| 7581 | |
| 7582 | ;; ...No. The current parens aren't the function's arg list. |
| 7583 | (goto-char before-lparen)) |
| 7584 | |
| 7585 | (or (c-go-list-backward) ; backwards over [ .... ] |
| 7586 | (throw 'knr nil))))))))) |
| 7587 | |
| 7588 | (defun c-skip-conditional () |
| 7589 | ;; skip forward over conditional at point, including any predicate |
| 7590 | ;; statements in parentheses. No error checking is performed. |
| 7591 | ;; |
| 7592 | ;; This function might do hidden buffer changes. |
| 7593 | (c-forward-sexp (cond |
| 7594 | ;; else if() |
| 7595 | ((looking-at (concat "\\<else" |
| 7596 | "\\([ \t\n]\\|\\\\\n\\)+" |
| 7597 | "if\\>\\([^_]\\|$\\)")) |
| 7598 | 3) |
| 7599 | ;; do, else, try, finally |
| 7600 | ((looking-at (concat "\\<\\(" |
| 7601 | "do\\|else\\|try\\|finally" |
| 7602 | "\\)\\>\\([^_]\\|$\\)")) |
| 7603 | 1) |
| 7604 | ;; for, if, while, switch, catch, synchronized, foreach |
| 7605 | (t 2)))) |
| 7606 | |
| 7607 | (defun c-after-conditional (&optional lim) |
| 7608 | ;; If looking at the token after a conditional then return the |
| 7609 | ;; position of its start, otherwise return nil. |
| 7610 | ;; |
| 7611 | ;; This function might do hidden buffer changes. |
| 7612 | (save-excursion |
| 7613 | (and (zerop (c-backward-token-2 1 t lim)) |
| 7614 | (or (looking-at c-block-stmt-1-key) |
| 7615 | (and (eq (char-after) ?\() |
| 7616 | (zerop (c-backward-token-2 1 t lim)) |
| 7617 | (looking-at c-block-stmt-2-key))) |
| 7618 | (point)))) |
| 7619 | |
| 7620 | (defun c-after-special-operator-id (&optional lim) |
| 7621 | ;; If the point is after an operator identifier that isn't handled |
| 7622 | ;; like an ordinary symbol (i.e. like "operator =" in C++) then the |
| 7623 | ;; position of the start of that identifier is returned. nil is |
| 7624 | ;; returned otherwise. The point may be anywhere in the syntactic |
| 7625 | ;; whitespace after the last token of the operator identifier. |
| 7626 | ;; |
| 7627 | ;; This function might do hidden buffer changes. |
| 7628 | (save-excursion |
| 7629 | (and c-overloadable-operators-regexp |
| 7630 | (zerop (c-backward-token-2 1 nil lim)) |
| 7631 | (looking-at c-overloadable-operators-regexp) |
| 7632 | (or (not c-opt-op-identifier-prefix) |
| 7633 | (and |
| 7634 | (zerop (c-backward-token-2 1 nil lim)) |
| 7635 | (looking-at c-opt-op-identifier-prefix))) |
| 7636 | (point)))) |
| 7637 | |
| 7638 | (defsubst c-backward-to-block-anchor (&optional lim) |
| 7639 | ;; Assuming point is at a brace that opens a statement block of some |
| 7640 | ;; kind, move to the proper anchor point for that block. It might |
| 7641 | ;; need to be adjusted further by c-add-stmt-syntax, but the |
| 7642 | ;; position at return is suitable as start position for that |
| 7643 | ;; function. |
| 7644 | ;; |
| 7645 | ;; This function might do hidden buffer changes. |
| 7646 | (unless (= (point) (c-point 'boi)) |
| 7647 | (let ((start (c-after-conditional lim))) |
| 7648 | (if start |
| 7649 | (goto-char start))))) |
| 7650 | |
| 7651 | (defsubst c-backward-to-decl-anchor (&optional lim) |
| 7652 | ;; Assuming point is at a brace that opens the block of a top level |
| 7653 | ;; declaration of some kind, move to the proper anchor point for |
| 7654 | ;; that block. |
| 7655 | ;; |
| 7656 | ;; This function might do hidden buffer changes. |
| 7657 | (unless (= (point) (c-point 'boi)) |
| 7658 | (c-beginning-of-statement-1 lim))) |
| 7659 | |
| 7660 | (defun c-search-decl-header-end () |
| 7661 | ;; Search forward for the end of the "header" of the current |
| 7662 | ;; declaration. That's the position where the definition body |
| 7663 | ;; starts, or the first variable initializer, or the ending |
| 7664 | ;; semicolon. I.e. search forward for the closest following |
| 7665 | ;; (syntactically relevant) '{', '=' or ';' token. Point is left |
| 7666 | ;; _after_ the first found token, or at point-max if none is found. |
| 7667 | ;; |
| 7668 | ;; This function might do hidden buffer changes. |
| 7669 | |
| 7670 | (let ((base (point))) |
| 7671 | (if (c-major-mode-is 'c++-mode) |
| 7672 | |
| 7673 | ;; In C++ we need to take special care to handle operator |
| 7674 | ;; tokens and those pesky template brackets. |
| 7675 | (while (and |
| 7676 | (c-syntactic-re-search-forward "[;{<=]" nil 'move t t) |
| 7677 | (or |
| 7678 | (c-end-of-current-token base) |
| 7679 | ;; Handle operator identifiers, i.e. ignore any |
| 7680 | ;; operator token preceded by "operator". |
| 7681 | (save-excursion |
| 7682 | (and (c-safe (c-backward-sexp) t) |
| 7683 | (looking-at c-opt-op-identifier-prefix))) |
| 7684 | (and (eq (char-before) ?<) |
| 7685 | (c-with-syntax-table c++-template-syntax-table |
| 7686 | (if (c-safe (goto-char (c-up-list-forward (point)))) |
| 7687 | t |
| 7688 | (goto-char (point-max)) |
| 7689 | nil))))) |
| 7690 | (setq base (point))) |
| 7691 | |
| 7692 | (while (and |
| 7693 | (c-syntactic-re-search-forward "[;{=]" nil 'move t t) |
| 7694 | (c-end-of-current-token base)) |
| 7695 | (setq base (point)))))) |
| 7696 | |
| 7697 | (defun c-beginning-of-decl-1 (&optional lim) |
| 7698 | ;; Go to the beginning of the current declaration, or the beginning |
| 7699 | ;; of the previous one if already at the start of it. Point won't |
| 7700 | ;; be moved out of any surrounding paren. Return a cons cell of the |
| 7701 | ;; form (MOVE . KNR-POS). MOVE is like the return value from |
| 7702 | ;; `c-beginning-of-statement-1'. If point skipped over some K&R |
| 7703 | ;; style argument declarations (and they are to be recognized) then |
| 7704 | ;; KNR-POS is set to the start of the first such argument |
| 7705 | ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a |
| 7706 | ;; position that bounds the backward search. |
| 7707 | ;; |
| 7708 | ;; NB: Cases where the declaration continues after the block, as in |
| 7709 | ;; "struct foo { ... } bar;", are currently recognized as two |
| 7710 | ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case. |
| 7711 | ;; |
| 7712 | ;; This function might do hidden buffer changes. |
| 7713 | (catch 'return |
| 7714 | (let* ((start (point)) |
| 7715 | (last-stmt-start (point)) |
| 7716 | (move (c-beginning-of-statement-1 lim nil t))) |
| 7717 | |
| 7718 | ;; `c-beginning-of-statement-1' stops at a block start, but we |
| 7719 | ;; want to continue if the block doesn't begin a top level |
| 7720 | ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob, |
| 7721 | ;; or an open paren. |
| 7722 | (let ((beg (point)) tentative-move) |
| 7723 | ;; Go back one "statement" each time round the loop until we're just |
| 7724 | ;; after a ;, }, or :, or at BOB or the start of a macro or start of |
| 7725 | ;; an ObjC method. This will move over a multiple declaration whose |
| 7726 | ;; components are comma separated. |
| 7727 | (while (and |
| 7728 | ;; Must check with c-opt-method-key in ObjC mode. |
| 7729 | (not (and c-opt-method-key |
| 7730 | (looking-at c-opt-method-key))) |
| 7731 | (/= last-stmt-start (point)) |
| 7732 | (progn |
| 7733 | (c-backward-syntactic-ws lim) |
| 7734 | (not (memq (char-before) '(?\; ?} ?: nil)))) |
| 7735 | (save-excursion |
| 7736 | (backward-char) |
| 7737 | (not (looking-at "\\s("))) |
| 7738 | ;; Check that we don't move from the first thing in a |
| 7739 | ;; macro to its header. |
| 7740 | (not (eq (setq tentative-move |
| 7741 | (c-beginning-of-statement-1 lim nil t)) |
| 7742 | 'macro))) |
| 7743 | (setq last-stmt-start beg |
| 7744 | beg (point) |
| 7745 | move tentative-move)) |
| 7746 | (goto-char beg)) |
| 7747 | |
| 7748 | (when c-recognize-knr-p |
| 7749 | (let ((fallback-pos (point)) knr-argdecl-start) |
| 7750 | ;; Handle K&R argdecls. Back up after the "statement" jumped |
| 7751 | ;; over by `c-beginning-of-statement-1', unless it was the |
| 7752 | ;; function body, in which case we're sitting on the opening |
| 7753 | ;; brace now. Then test if we're in a K&R argdecl region and |
| 7754 | ;; that we started at the other side of the first argdecl in |
| 7755 | ;; it. |
| 7756 | (unless (eq (char-after) ?{) |
| 7757 | (goto-char last-stmt-start)) |
| 7758 | (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim)) |
| 7759 | (< knr-argdecl-start start) |
| 7760 | (progn |
| 7761 | (goto-char knr-argdecl-start) |
| 7762 | (not (eq (c-beginning-of-statement-1 lim nil t) 'macro)))) |
| 7763 | (throw 'return |
| 7764 | (cons (if (eq (char-after fallback-pos) ?{) |
| 7765 | 'previous |
| 7766 | 'same) |
| 7767 | knr-argdecl-start)) |
| 7768 | (goto-char fallback-pos)))) |
| 7769 | |
| 7770 | ;; `c-beginning-of-statement-1' counts each brace block as a separate |
| 7771 | ;; statement, so the result will be 'previous if we've moved over any. |
| 7772 | ;; So change our result back to 'same if necessary. |
| 7773 | ;; |
| 7774 | ;; If they were brace list initializers we might not have moved over a |
| 7775 | ;; declaration boundary though, so change it to 'same if we've moved |
| 7776 | ;; past a '=' before '{', but not ';'. (This ought to be integrated |
| 7777 | ;; into `c-beginning-of-statement-1', so we avoid this extra pass which |
| 7778 | ;; potentially can search over a large amount of text.). Take special |
| 7779 | ;; pains not to get mislead by C++'s "operator=", and the like. |
| 7780 | (if (and (eq move 'previous) |
| 7781 | (c-with-syntax-table (if (c-major-mode-is 'c++-mode) |
| 7782 | c++-template-syntax-table |
| 7783 | (syntax-table)) |
| 7784 | (save-excursion |
| 7785 | (and |
| 7786 | (progn |
| 7787 | (while ; keep going back to "[;={"s until we either find |
| 7788 | ; no more, or get to one which isn't an "operator =" |
| 7789 | (and (c-syntactic-re-search-forward "[;={]" start t t t) |
| 7790 | (eq (char-before) ?=) |
| 7791 | c-overloadable-operators-regexp |
| 7792 | c-opt-op-identifier-prefix |
| 7793 | (save-excursion |
| 7794 | (eq (c-backward-token-2) 0) |
| 7795 | (looking-at c-overloadable-operators-regexp) |
| 7796 | (eq (c-backward-token-2) 0) |
| 7797 | (looking-at c-opt-op-identifier-prefix)))) |
| 7798 | (eq (char-before) ?=)) |
| 7799 | (c-syntactic-re-search-forward "[;{]" start t t) |
| 7800 | (eq (char-before) ?{) |
| 7801 | (c-safe (goto-char (c-up-list-forward (point))) t) |
| 7802 | (not (c-syntactic-re-search-forward ";" start t t)))))) |
| 7803 | (cons 'same nil) |
| 7804 | (cons move nil))))) |
| 7805 | |
| 7806 | (defun c-end-of-decl-1 () |
| 7807 | ;; Assuming point is at the start of a declaration (as detected by |
| 7808 | ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike |
| 7809 | ;; `c-beginning-of-decl-1', this function handles the case when a |
| 7810 | ;; block is followed by identifiers in e.g. struct declarations in C |
| 7811 | ;; or C++. If a proper end was found then t is returned, otherwise |
| 7812 | ;; point is moved as far as possible within the current sexp and nil |
| 7813 | ;; is returned. This function doesn't handle macros; use |
| 7814 | ;; `c-end-of-macro' instead in those cases. |
| 7815 | ;; |
| 7816 | ;; This function might do hidden buffer changes. |
| 7817 | (let ((start (point)) |
| 7818 | (decl-syntax-table (if (c-major-mode-is 'c++-mode) |
| 7819 | c++-template-syntax-table |
| 7820 | (syntax-table)))) |
| 7821 | (catch 'return |
| 7822 | (c-search-decl-header-end) |
| 7823 | |
| 7824 | (when (and c-recognize-knr-p |
| 7825 | (eq (char-before) ?\;) |
| 7826 | (c-in-knr-argdecl start)) |
| 7827 | ;; Stopped at the ';' in a K&R argdecl section which is |
| 7828 | ;; detected using the same criteria as in |
| 7829 | ;; `c-beginning-of-decl-1'. Move to the following block |
| 7830 | ;; start. |
| 7831 | (c-syntactic-re-search-forward "{" nil 'move t)) |
| 7832 | |
| 7833 | (when (eq (char-before) ?{) |
| 7834 | ;; Encountered a block in the declaration. Jump over it. |
| 7835 | (condition-case nil |
| 7836 | (goto-char (c-up-list-forward (point))) |
| 7837 | (error (goto-char (point-max)) |
| 7838 | (throw 'return nil))) |
| 7839 | (if (or (not c-opt-block-decls-with-vars-key) |
| 7840 | (save-excursion |
| 7841 | (c-with-syntax-table decl-syntax-table |
| 7842 | (let ((lim (point))) |
| 7843 | (goto-char start) |
| 7844 | (not (and |
| 7845 | ;; Check for `c-opt-block-decls-with-vars-key' |
| 7846 | ;; before the first paren. |
| 7847 | (c-syntactic-re-search-forward |
| 7848 | (concat "[;=\(\[{]\\|\\(" |
| 7849 | c-opt-block-decls-with-vars-key |
| 7850 | "\\)") |
| 7851 | lim t t t) |
| 7852 | (match-beginning 1) |
| 7853 | (not (eq (char-before) ?_)) |
| 7854 | ;; Check that the first following paren is |
| 7855 | ;; the block. |
| 7856 | (c-syntactic-re-search-forward "[;=\(\[{]" |
| 7857 | lim t t t) |
| 7858 | (eq (char-before) ?{))))))) |
| 7859 | ;; The declaration doesn't have any of the |
| 7860 | ;; `c-opt-block-decls-with-vars' keywords in the |
| 7861 | ;; beginning, so it ends here at the end of the block. |
| 7862 | (throw 'return t))) |
| 7863 | |
| 7864 | (c-with-syntax-table decl-syntax-table |
| 7865 | (while (progn |
| 7866 | (if (eq (char-before) ?\;) |
| 7867 | (throw 'return t)) |
| 7868 | (c-syntactic-re-search-forward ";" nil 'move t)))) |
| 7869 | nil))) |
| 7870 | |
| 7871 | (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit) |
| 7872 | ;; Assuming the point is at an open brace, check if it starts a |
| 7873 | ;; block that contains another declaration level, i.e. that isn't a |
| 7874 | ;; statement block or a brace list, and if so return non-nil. |
| 7875 | ;; |
| 7876 | ;; If the check is successful, the return value is the start of the |
| 7877 | ;; keyword that tells what kind of construct it is, i.e. typically |
| 7878 | ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then |
| 7879 | ;; the point will be at the start of the construct, before any |
| 7880 | ;; leading specifiers, otherwise it's at the returned position. |
| 7881 | ;; |
| 7882 | ;; The point is clobbered if the check is unsuccessful. |
| 7883 | ;; |
| 7884 | ;; CONTAINING-SEXP is the position of the open of the surrounding |
| 7885 | ;; paren, or nil if none. |
| 7886 | ;; |
| 7887 | ;; The optional LIMIT limits the backward search for the start of |
| 7888 | ;; the construct. It's assumed to be at a syntactically relevant |
| 7889 | ;; position. |
| 7890 | ;; |
| 7891 | ;; If any template arglists are found in the searched region before |
| 7892 | ;; the open brace, they get marked with paren syntax. |
| 7893 | ;; |
| 7894 | ;; This function might do hidden buffer changes. |
| 7895 | |
| 7896 | (let ((open-brace (point)) kwd-start first-specifier-pos) |
| 7897 | (c-syntactic-skip-backward c-block-prefix-charset limit t) |
| 7898 | |
| 7899 | (when (and c-recognize-<>-arglists |
| 7900 | (eq (char-before) ?>)) |
| 7901 | ;; Could be at the end of a template arglist. |
| 7902 | (let ((c-parse-and-markup-<>-arglists t) |
| 7903 | (c-disallow-comma-in-<>-arglists |
| 7904 | (and containing-sexp |
| 7905 | (not (eq (char-after containing-sexp) ?{))))) |
| 7906 | (while (and |
| 7907 | (c-backward-<>-arglist nil limit) |
| 7908 | (progn |
| 7909 | (c-syntactic-skip-backward c-block-prefix-charset limit t) |
| 7910 | (eq (char-before) ?>)))))) |
| 7911 | |
| 7912 | ;; Note: Can't get bogus hits inside template arglists below since they |
| 7913 | ;; have gotten paren syntax above. |
| 7914 | (when (and |
| 7915 | ;; If `goto-start' is set we begin by searching for the |
| 7916 | ;; first possible position of a leading specifier list. |
| 7917 | ;; The `c-decl-block-key' search continues from there since |
| 7918 | ;; we know it can't match earlier. |
| 7919 | (if goto-start |
| 7920 | (when (c-syntactic-re-search-forward c-symbol-start |
| 7921 | open-brace t t) |
| 7922 | (goto-char (setq first-specifier-pos (match-beginning 0))) |
| 7923 | t) |
| 7924 | t) |
| 7925 | |
| 7926 | (cond |
| 7927 | ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t) |
| 7928 | (goto-char (setq kwd-start (match-beginning 0))) |
| 7929 | (or |
| 7930 | |
| 7931 | ;; Found a keyword that can't be a type? |
| 7932 | (match-beginning 1) |
| 7933 | |
| 7934 | ;; Can be a type too, in which case it's the return type of a |
| 7935 | ;; function (under the assumption that no declaration level |
| 7936 | ;; block construct starts with a type). |
| 7937 | (not (c-forward-type)) |
| 7938 | |
| 7939 | ;; Jumped over a type, but it could be a declaration keyword |
| 7940 | ;; followed by the declared identifier that we've jumped over |
| 7941 | ;; instead (e.g. in "class Foo {"). If it indeed is a type |
| 7942 | ;; then we should be at the declarator now, so check for a |
| 7943 | ;; valid declarator start. |
| 7944 | ;; |
| 7945 | ;; Note: This doesn't cope with the case when a declared |
| 7946 | ;; identifier is followed by e.g. '(' in a language where '(' |
| 7947 | ;; also might be part of a declarator expression. Currently |
| 7948 | ;; there's no such language. |
| 7949 | (not (or (looking-at c-symbol-start) |
| 7950 | (looking-at c-type-decl-prefix-key))))) |
| 7951 | |
| 7952 | ;; In Pike a list of modifiers may be followed by a brace |
| 7953 | ;; to make them apply to many identifiers. Note that the |
| 7954 | ;; match data will be empty on return in this case. |
| 7955 | ((and (c-major-mode-is 'pike-mode) |
| 7956 | (progn |
| 7957 | (goto-char open-brace) |
| 7958 | (= (c-backward-token-2) 0)) |
| 7959 | (looking-at c-specifier-key) |
| 7960 | ;; Use this variant to avoid yet another special regexp. |
| 7961 | (c-keyword-member (c-keyword-sym (match-string 1)) |
| 7962 | 'c-modifier-kwds)) |
| 7963 | (setq kwd-start (point)) |
| 7964 | t))) |
| 7965 | |
| 7966 | ;; Got a match. |
| 7967 | |
| 7968 | (if goto-start |
| 7969 | ;; Back up over any preceding specifiers and their clauses |
| 7970 | ;; by going forward from `first-specifier-pos', which is the |
| 7971 | ;; earliest possible position where the specifier list can |
| 7972 | ;; start. |
| 7973 | (progn |
| 7974 | (goto-char first-specifier-pos) |
| 7975 | |
| 7976 | (while (< (point) kwd-start) |
| 7977 | (if (looking-at c-symbol-key) |
| 7978 | ;; Accept any plain symbol token on the ground that |
| 7979 | ;; it's a specifier masked through a macro (just |
| 7980 | ;; like `c-forward-decl-or-cast-1' skip forward over |
| 7981 | ;; such tokens). |
| 7982 | ;; |
| 7983 | ;; Could be more restrictive wrt invalid keywords, |
| 7984 | ;; but that'd only occur in invalid code so there's |
| 7985 | ;; no use spending effort on it. |
| 7986 | (let ((end (match-end 0))) |
| 7987 | (unless (c-forward-keyword-clause 0) |
| 7988 | (goto-char end) |
| 7989 | (c-forward-syntactic-ws))) |
| 7990 | |
| 7991 | ;; Can't parse a declaration preamble and is still |
| 7992 | ;; before `kwd-start'. That means `first-specifier-pos' |
| 7993 | ;; was in some earlier construct. Search again. |
| 7994 | (if (c-syntactic-re-search-forward c-symbol-start |
| 7995 | kwd-start 'move t) |
| 7996 | (goto-char (setq first-specifier-pos (match-beginning 0))) |
| 7997 | ;; Got no preamble before the block declaration keyword. |
| 7998 | (setq first-specifier-pos kwd-start)))) |
| 7999 | |
| 8000 | (goto-char first-specifier-pos)) |
| 8001 | (goto-char kwd-start)) |
| 8002 | |
| 8003 | kwd-start))) |
| 8004 | |
| 8005 | (defun c-search-uplist-for-classkey (paren-state) |
| 8006 | ;; Check if the closest containing paren sexp is a declaration |
| 8007 | ;; block, returning a 2 element vector in that case. Aref 0 |
| 8008 | ;; contains the bufpos at boi of the class key line, and aref 1 |
| 8009 | ;; contains the bufpos of the open brace. This function is an |
| 8010 | ;; obsolete wrapper for `c-looking-at-decl-block'. |
| 8011 | ;; |
| 8012 | ;; This function might do hidden buffer changes. |
| 8013 | (let ((open-paren-pos (c-most-enclosing-brace paren-state))) |
| 8014 | (when open-paren-pos |
| 8015 | (save-excursion |
| 8016 | (goto-char open-paren-pos) |
| 8017 | (when (and (eq (char-after) ?{) |
| 8018 | (c-looking-at-decl-block |
| 8019 | (c-safe-position open-paren-pos paren-state) |
| 8020 | nil)) |
| 8021 | (back-to-indentation) |
| 8022 | (vector (point) open-paren-pos)))))) |
| 8023 | |
| 8024 | (defmacro c-pull-open-brace (ps) |
| 8025 | ;; Pull the next open brace from PS (which has the form of paren-state), |
| 8026 | ;; skipping over any brace pairs. Returns NIL when PS is exhausted. |
| 8027 | `(progn |
| 8028 | (while (consp (car ,ps)) |
| 8029 | (setq ,ps (cdr ,ps))) |
| 8030 | (prog1 (car ,ps) |
| 8031 | (setq ,ps (cdr ,ps))))) |
| 8032 | |
| 8033 | (defun c-most-enclosing-decl-block (paren-state) |
| 8034 | ;; Return the buffer position of the most enclosing decl-block brace (in the |
| 8035 | ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if |
| 8036 | ;; none was found. |
| 8037 | (let* ((open-brace (c-pull-open-brace paren-state)) |
| 8038 | (next-open-brace (c-pull-open-brace paren-state))) |
| 8039 | (while (and open-brace |
| 8040 | (save-excursion |
| 8041 | (goto-char open-brace) |
| 8042 | (not (c-looking-at-decl-block next-open-brace nil)))) |
| 8043 | (setq open-brace next-open-brace |
| 8044 | next-open-brace (c-pull-open-brace paren-state))) |
| 8045 | open-brace)) |
| 8046 | |
| 8047 | (defun c-inside-bracelist-p (containing-sexp paren-state) |
| 8048 | ;; return the buffer position of the beginning of the brace list |
| 8049 | ;; statement if we're inside a brace list, otherwise return nil. |
| 8050 | ;; CONTAINING-SEXP is the buffer pos of the innermost containing |
| 8051 | ;; paren. PAREN-STATE is the remainder of the state of enclosing |
| 8052 | ;; braces |
| 8053 | ;; |
| 8054 | ;; N.B.: This algorithm can potentially get confused by cpp macros |
| 8055 | ;; placed in inconvenient locations. It's a trade-off we make for |
| 8056 | ;; speed. |
| 8057 | ;; |
| 8058 | ;; This function might do hidden buffer changes. |
| 8059 | (or |
| 8060 | ;; This will pick up brace list declarations. |
| 8061 | (c-safe |
| 8062 | (save-excursion |
| 8063 | (goto-char containing-sexp) |
| 8064 | (c-forward-sexp -1) |
| 8065 | (let (bracepos) |
| 8066 | (if (and (or (looking-at c-brace-list-key) |
| 8067 | (progn (c-forward-sexp -1) |
| 8068 | (looking-at c-brace-list-key))) |
| 8069 | (setq bracepos (c-down-list-forward (point))) |
| 8070 | (not (c-crosses-statement-barrier-p (point) |
| 8071 | (- bracepos 2)))) |
| 8072 | (point))))) |
| 8073 | ;; this will pick up array/aggregate init lists, even if they are nested. |
| 8074 | (save-excursion |
| 8075 | (let ((class-key |
| 8076 | ;; Pike can have class definitions anywhere, so we must |
| 8077 | ;; check for the class key here. |
| 8078 | (and (c-major-mode-is 'pike-mode) |
| 8079 | c-decl-block-key)) |
| 8080 | bufpos braceassignp lim next-containing) |
| 8081 | (while (and (not bufpos) |
| 8082 | containing-sexp) |
| 8083 | (when paren-state |
| 8084 | (if (consp (car paren-state)) |
| 8085 | (setq lim (cdr (car paren-state)) |
| 8086 | paren-state (cdr paren-state)) |
| 8087 | (setq lim (car paren-state))) |
| 8088 | (when paren-state |
| 8089 | (setq next-containing (car paren-state) |
| 8090 | paren-state (cdr paren-state)))) |
| 8091 | (goto-char containing-sexp) |
| 8092 | (if (c-looking-at-inexpr-block next-containing next-containing) |
| 8093 | ;; We're in an in-expression block of some kind. Do not |
| 8094 | ;; check nesting. We deliberately set the limit to the |
| 8095 | ;; containing sexp, so that c-looking-at-inexpr-block |
| 8096 | ;; doesn't check for an identifier before it. |
| 8097 | (setq containing-sexp nil) |
| 8098 | ;; see if the open brace is preceded by = or [...] in |
| 8099 | ;; this statement, but watch out for operator= |
| 8100 | (setq braceassignp 'dontknow) |
| 8101 | (c-backward-token-2 1 t lim) |
| 8102 | ;; Checks to do only on the first sexp before the brace. |
| 8103 | (when (and c-opt-inexpr-brace-list-key |
| 8104 | (eq (char-after) ?\[)) |
| 8105 | ;; In Java, an initialization brace list may follow |
| 8106 | ;; directly after "new Foo[]", so check for a "new" |
| 8107 | ;; earlier. |
| 8108 | (while (eq braceassignp 'dontknow) |
| 8109 | (setq braceassignp |
| 8110 | (cond ((/= (c-backward-token-2 1 t lim) 0) nil) |
| 8111 | ((looking-at c-opt-inexpr-brace-list-key) t) |
| 8112 | ((looking-at "\\sw\\|\\s_\\|[.[]") |
| 8113 | ;; Carry on looking if this is an |
| 8114 | ;; identifier (may contain "." in Java) |
| 8115 | ;; or another "[]" sexp. |
| 8116 | 'dontknow) |
| 8117 | (t nil))))) |
| 8118 | ;; Checks to do on all sexps before the brace, up to the |
| 8119 | ;; beginning of the statement. |
| 8120 | (while (eq braceassignp 'dontknow) |
| 8121 | (cond ((eq (char-after) ?\;) |
| 8122 | (setq braceassignp nil)) |
| 8123 | ((and class-key |
| 8124 | (looking-at class-key)) |
| 8125 | (setq braceassignp nil)) |
| 8126 | ((eq (char-after) ?=) |
| 8127 | ;; We've seen a =, but must check earlier tokens so |
| 8128 | ;; that it isn't something that should be ignored. |
| 8129 | (setq braceassignp 'maybe) |
| 8130 | (while (and (eq braceassignp 'maybe) |
| 8131 | (zerop (c-backward-token-2 1 t lim))) |
| 8132 | (setq braceassignp |
| 8133 | (cond |
| 8134 | ;; Check for operator = |
| 8135 | ((and c-opt-op-identifier-prefix |
| 8136 | (looking-at c-opt-op-identifier-prefix)) |
| 8137 | nil) |
| 8138 | ;; Check for `<opchar>= in Pike. |
| 8139 | ((and (c-major-mode-is 'pike-mode) |
| 8140 | (or (eq (char-after) ?`) |
| 8141 | ;; Special case for Pikes |
| 8142 | ;; `[]=, since '[' is not in |
| 8143 | ;; the punctuation class. |
| 8144 | (and (eq (char-after) ?\[) |
| 8145 | (eq (char-before) ?`)))) |
| 8146 | nil) |
| 8147 | ((looking-at "\\s.") 'maybe) |
| 8148 | ;; make sure we're not in a C++ template |
| 8149 | ;; argument assignment |
| 8150 | ((and |
| 8151 | (c-major-mode-is 'c++-mode) |
| 8152 | (save-excursion |
| 8153 | (let ((here (point)) |
| 8154 | (pos< (progn |
| 8155 | (skip-chars-backward "^<>") |
| 8156 | (point)))) |
| 8157 | (and (eq (char-before) ?<) |
| 8158 | (not (c-crosses-statement-barrier-p |
| 8159 | pos< here)) |
| 8160 | (not (c-in-literal)) |
| 8161 | )))) |
| 8162 | nil) |
| 8163 | (t t)))))) |
| 8164 | (if (and (eq braceassignp 'dontknow) |
| 8165 | (/= (c-backward-token-2 1 t lim) 0)) |
| 8166 | (setq braceassignp nil))) |
| 8167 | (if (not braceassignp) |
| 8168 | (if (eq (char-after) ?\;) |
| 8169 | ;; Brace lists can't contain a semicolon, so we're done. |
| 8170 | (setq containing-sexp nil) |
| 8171 | ;; Go up one level. |
| 8172 | (setq containing-sexp next-containing |
| 8173 | lim nil |
| 8174 | next-containing nil)) |
| 8175 | ;; we've hit the beginning of the aggregate list |
| 8176 | (c-beginning-of-statement-1 |
| 8177 | (c-most-enclosing-brace paren-state)) |
| 8178 | (setq bufpos (point)))) |
| 8179 | ) |
| 8180 | bufpos)) |
| 8181 | )) |
| 8182 | |
| 8183 | (defun c-looking-at-special-brace-list (&optional lim) |
| 8184 | Content-type: text/html
HCoop Git - bpt/emacs.git/blame_incremental - lisp/progmodes/cc-engine.el
500 - Internal Server Error
Malformed UTF-8 character (fatal) at (eval 8) line 1, <$fd> line 8184.