]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/cc-engine.el
merge trunk
[gnu-emacs] / lisp / progmodes / cc-engine.el
index 6f3004e274a0fa7017d0b74b7aafaaf2e545c9b9..0865ddfed690cddad87e48eccc3c16fec6a6f725 100644 (file)
       (not prevstate)
     (> arg 0)))
 
-;; Dynamically bound cache for `c-in-literal'.
-(defvar c-in-literal-cache t)
-
 \f
 ;; Basic handling of preprocessor directives.
 
@@ -893,7 +890,7 @@ comment at the start of cc-engine.el for more info."
                          ((eq sym 'while)
                           ;; Is this a real while, or a do-while?
                           ;; The next `when' triggers unless we are SURE that
-                          ;; the `while' is not the tailend of a `do-while'.
+                          ;; the `while' is not the tail end of a `do-while'.
                           (when (or (not pptok)
                                     (memq (char-after pptok) delims)
                                     ;; The following kludge is to prevent
@@ -2093,28 +2090,35 @@ comment at the start of cc-engine.el for more info."
 ;; `c-state-literal-at'.
 
 (defsubst c-state-pp-to-literal (from to)
-  ;; Do a parse-partial-sexp from FROM to TO, returning the bounds of any
-  ;; literal at TO as a cons, otherwise NIL.
-  ;; FROM must not be in a literal, and the buffer should already be wide
-  ;; enough.
+  ;; Do a parse-partial-sexp from FROM to TO, returning either
+  ;;     (STATE TYPE (BEG . END))     if TO is in a literal; or
+  ;;     (STATE)                      otherwise,
+  ;; where STATE is the parsing state at TO, TYPE is the type of the literal
+  ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal.
+  ;;
+  ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
+  ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
+  ;; STATE are valid.
   (save-excursion
-    (let ((s (parse-partial-sexp from to)))
+    (let ((s (parse-partial-sexp from to))
+         ty)
       (when (or (nth 3 s) (nth 4 s))   ; in a string or comment
+       (setq ty (cond
+                 ((nth 3 s) 'string)
+                 ((eq (nth 7 s) t) 'c++)
+                 (t 'c)))
        (parse-partial-sexp (point) (point-max)
                            nil                  ; TARGETDEPTH
                            nil                  ; STOPBEFORE
                            s                    ; OLDSTATE
-                           'syntax-table)       ; stop at end of literal
-       (cons (nth 8 s) (point))))))
+                           'syntax-table))      ; stop at end of literal
+      (if ty
+         `(,s ,ty (,(nth 8 s) . ,(point)))
+       `(,s)))))
 
-(defun c-state-literal-at (here)
-  ;; If position HERE is inside a literal, return (START . END), the
-  ;; boundaries of the literal (which may be outside the accessible bit of the
-  ;; buffer).  Otherwise, return nil.
-  ;;
-  ;; This function is almost the same as `c-literal-limits'.  It differs in
-  ;; that it is a lower level function, and that it rigourously follows the
-  ;; syntax from BOB, whereas `c-literal-limits' uses a "local" safe position.
+(defun c-state-safe-place (here)
+  ;; Return a buffer position before HERE which is "safe", i.e. outside any
+  ;; string, comment, or macro.
   ;;
   ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'.  This cache
   ;; MAY NOT contain any positions within macros, since macros are frequently
@@ -2137,7 +2141,7 @@ comment at the start of cc-engine.el for more info."
 
        (while (<= (setq npos (+ pos c-state-nonlit-pos-interval))
                   here)
-         (setq lit (c-state-pp-to-literal pos npos))
+         (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
          (setq pos (or (cdr lit) npos)) ; end of literal containing npos.
          (goto-char pos)
          (when (and (c-beginning-of-macro) (/= (point) pos))
@@ -2148,9 +2152,22 @@ comment at the start of cc-engine.el for more info."
 
        (if (> pos c-state-nonlit-pos-cache-limit)
            (setq c-state-nonlit-pos-cache-limit pos))
-       (if (< pos here)
-           (setq lit (c-state-pp-to-literal pos here)))
-       lit))))
+       pos))))
+       
+(defun c-state-literal-at (here)
+  ;; If position HERE is inside a literal, return (START . END), the
+  ;; boundaries of the literal (which may be outside the accessible bit of the
+  ;; buffer).  Otherwise, return nil.
+  ;;
+  ;; This function is almost the same as `c-literal-limits'.  Previously, it
+  ;; differed in that it was a lower level function, and that it rigourously
+  ;; followed the syntax from BOB.  `c-literal-limits' is now (2011-12)
+  ;; virtually identical to this function.
+  (save-restriction
+    (widen)
+    (save-excursion
+      (let ((pos (c-state-safe-place here)))
+           (car (cddr (c-state-pp-to-literal pos here)))))))
 
 (defsubst c-state-lit-beg (pos)
   ;; Return the start of the literal containing POS, or POS itself.
@@ -2302,7 +2319,7 @@ comment at the start of cc-engine.el for more info."
 ;; `c-parse-state', or nil.
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;; Defuns which analyse the buffer, yet don't change `c-state-cache'.
+;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
 (defun c-get-fallback-scan-pos (here)
   ;; Return a start position for building `c-state-cache' from
   ;; scratch.  This will be at the top level, 2 defuns back.
@@ -2364,7 +2381,7 @@ comment at the start of cc-engine.el for more info."
 
 (defun c-parse-state-get-strategy (here good-pos)
   ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
-  ;; to minimise the amount of scanning.  HERE is the pertinent position in
+  ;; to minimize the amount of scanning.  HERE is the pertinent position in
   ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
   ;; its head trimmed) is known to be good, or nil if there is no such
   ;; position.
@@ -2548,7 +2565,7 @@ comment at the start of cc-engine.el for more info."
                        c-state-cache)))
        ;; N.B.  This defsubst codes one method for the simple, normal case,
        ;; and a more sophisticated, slower way for the general case.  Don't
-       ;; eliminate this defsubst - it's a speed optimisation.
+       ;; eliminate this defsubst - it's a speed optimization.
        (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
 
 (defun c-append-to-state-cache (from)
@@ -2788,7 +2805,7 @@ comment at the start of cc-engine.el for more info."
   ;;
   ;; This function must only be called only when (> `c-state-cache-good-pos'
   ;; HERE).  Usually the gap between CACHE-POS and HERE is large.  It is thus
-  ;; optimised to eliminate (or minimise) scanning between these two
+  ;; optimized to eliminate (or minimize) scanning between these two
   ;; positions.
   ;;
   ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
@@ -2820,7 +2837,7 @@ comment at the start of cc-engine.el for more info."
                                        ; or `here' itself.
        here- here+                  ; start/end of macro around HERE, or HERE
        (here-bol (c-point 'bol here))
-       (too-far-back (max (- here c-state-cache-too-far) 1)))
+       (too-far-back (max (- here c-state-cache-too-far) (point-min))))
 
     ;; Remove completely irrelevant entries from `c-state-cache'.
     (while (and c-state-cache
@@ -2964,9 +2981,9 @@ comment at the start of cc-engine.el for more info."
            c-state-cache-good-pos nil
            c-state-min-scan-pos nil)
 
-;;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value below
-;;; `here'.  To maintain its consistency, we may need to insert a new brace
-;;; pair.
+    ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
+    ;; below `here'.  To maintain its consistency, we may need to insert a new
+    ;; brace pair.
     (let ((here-bol (c-point 'bol here))
          too-high-pa             ; recorded {/(/[ next above here, or nil.
          dropped-cons            ; was the last removed element a brace pair?
@@ -4181,7 +4198,7 @@ comment at the start of cc-engine.el for more info."
 \f
 ;; Tools for handling comments and string literals.
 
-(defun c-slow-in-literal (&optional lim detect-cpp)
+(defun c-in-literal (&optional lim detect-cpp)
   "Return the type of literal point is in, if any.
 The return value is `c' if in a C-style comment, `c++' if in a C++
 style comment, `string' if in a string literal, `pound' if DETECT-CPP
@@ -4194,67 +4211,12 @@ The last point calculated is cached if the cache is enabled, i.e. if
 
 Note that this function might do hidden buffer changes.  See the
 comment at the start of cc-engine.el for more info."
-
-  (if (and (vectorp c-in-literal-cache)
-          (= (point) (aref c-in-literal-cache 0)))
-      (aref c-in-literal-cache 1)
-    (let ((rtn (save-excursion
-                (let* ((pos (point))
-                       (lim (or lim (progn
-                                      (c-beginning-of-syntax)
-                                      (point))))
-                       (state (parse-partial-sexp lim pos)))
-                  (cond
-                   ((elt state 3) 'string)
-                   ((elt state 4) (if (elt state 7) 'c++ 'c))
-                   ((and detect-cpp (c-beginning-of-macro lim)) 'pound)
-                   (t nil))))))
-      ;; cache this result if the cache is enabled
-      (if (not c-in-literal-cache)
-         (setq c-in-literal-cache (vector (point) rtn)))
-      rtn)))
-
-;; XEmacs has a built-in function that should make this much quicker.
-;; I don't think we even need the cache, which makes our lives more
-;; complicated anyway.  In this case, lim is only used to detect
-;; cpp directives.
-;;
-;; Note that there is a bug in XEmacs's buffer-syntactic-context when used in
-;; conjunction with syntax-table-properties.  The bug is present in, e.g.,
-;; XEmacs 21.4.4.  It manifested itself thus:
-;;
-;; Starting with an empty AWK Mode buffer, type
-;; /regexp/ {<C-j>
-;; Point gets wrongly left at column 0, rather than being indented to tab-width.
-;;
-;; AWK Mode is designed such that when the first / is typed, it gets the
-;; syntax-table property "string fence".  When the second / is typed, BOTH /s
-;; are given the s-t property "string".  However, buffer-syntactic-context
-;; fails to take account of the change of the s-t property on the opening / to
-;; "string", and reports that the { is within a string started by the second /.
-;;
-;; The workaround for this is for the AWK Mode initialisation to switch the
-;; defalias for c-in-literal to c-slow-in-literal.  This will slow down other
-;; cc-modes in XEmacs whenever an awk-buffer has been initialised.
-;;
-;; (Alan Mackenzie, 2003/4/30).
-
-(defun c-fast-in-literal (&optional lim detect-cpp)
-  ;; This function might do hidden buffer changes.
-  (let ((context (buffer-syntactic-context)))
-    (cond
-     ((eq context 'string) 'string)
-     ((eq context 'comment) 'c++)
-     ((eq context 'block-comment) 'c)
-     ((and detect-cpp (save-excursion (c-beginning-of-macro lim))) 'pound))))
-
-(defalias 'c-in-literal
-  (if (fboundp 'buffer-syntactic-context)
-    'c-fast-in-literal                  ; XEmacs
-    'c-slow-in-literal))                ; GNU Emacs
-
-;; The defalias above isn't enough to shut up the byte compiler.
-(cc-bytecomp-defun c-in-literal)
+  (let* ((safe-place (c-state-safe-place (point)))
+        (lit (c-state-pp-to-literal safe-place (point))))
+    (or (cadr lit)
+       (and detect-cpp
+            (save-excursion (c-beginning-of-macro))
+            'pound))))
 
 (defun c-literal-limits (&optional lim near not-in-delimiter)
   "Return a cons of the beginning and end positions of the comment or
@@ -4273,64 +4235,56 @@ comment at the start of cc-engine.el for more info."
 
   (save-excursion
     (let* ((pos (point))
-          (lim (or lim (progn
-                         (c-beginning-of-syntax)
-                         (point))))
-          (state (parse-partial-sexp lim pos)))
-
-      (cond ((elt state 3)             ; String.
-            (goto-char (elt state 8))
-            (cons (point) (or (c-safe (c-forward-sexp 1) (point))
-                              (point-max))))
-
-           ((elt state 4)              ; Comment.
-            (goto-char (elt state 8))
-            (cons (point) (progn (c-forward-single-comment) (point))))
-
-           ((and (not not-in-delimiter)
-                 (not (elt state 5))
-                 (eq (char-before) ?/)
-                 (looking-at "[/*]"))
-            ;; We're standing in a comment starter.
-            (backward-char 1)
-            (cons (point) (progn (c-forward-single-comment) (point))))
-
-           (near
-            (goto-char pos)
-
-            ;; Search forward for a literal.
-            (skip-chars-forward " \t")
+          (lim (or lim (c-state-safe-place pos)))
+          (pp-to-lit (c-state-pp-to-literal lim pos))
+          (state (car pp-to-lit))
+          (lit-type (cadr pp-to-lit))
+          (lit-limits (car (cddr pp-to-lit))))
 
-            (cond
-             ((looking-at c-string-limit-regexp) ; String.
-              (cons (point) (or (c-safe (c-forward-sexp 1) (point))
-                                (point-max))))
+      (cond
+       (lit-limits)
+       ((and (not not-in-delimiter)
+            (not (elt state 5))
+            (eq (char-before) ?/)
+            (looking-at "[/*]")) ; FIXME!!! use c-line/block-comment-starter.  2008-09-28.
+       ;; We're standing in a comment starter.
+       (backward-char 1)
+       (cons (point) (progn (c-forward-single-comment) (point))))
+
+       (near
+       (goto-char pos)
+       ;; Search forward for a literal.
+       (skip-chars-forward " \t")
+       (cond
+        ((looking-at c-string-limit-regexp) ; String.
+         (cons (point) (or (c-safe (c-forward-sexp 1) (point))
+                           (point-max))))
 
-             ((looking-at c-comment-start-regexp) ; Line or block comment.
-              (cons (point) (progn (c-forward-single-comment) (point))))
+        ((looking-at c-comment-start-regexp) ; Line or block comment.
+         (cons (point) (progn (c-forward-single-comment) (point))))
 
-             (t
-              ;; Search backward.
-              (skip-chars-backward " \t")
+        (t
+         ;; Search backward.
+         (skip-chars-backward " \t")
 
-              (let ((end (point)) beg)
-                (cond
-                 ((save-excursion
-                    (< (skip-syntax-backward c-string-syntax) 0)) ; String.
-                  (setq beg (c-safe (c-backward-sexp 1) (point))))
-
-                 ((and (c-safe (forward-char -2) t)
-                       (looking-at "*/"))
-                  ;; Block comment.  Due to the nature of line
-                  ;; comments, they will always be covered by the
-                  ;; normal case above.
-                  (goto-char end)
-                  (c-backward-single-comment)
-                  ;; If LIM is bogus, beg will be bogus.
-                  (setq beg (point))))
-
-                (if beg (cons beg end))))))
-           ))))
+         (let ((end (point)) beg)
+           (cond
+            ((save-excursion
+               (< (skip-syntax-backward c-string-syntax) 0)) ; String.
+             (setq beg (c-safe (c-backward-sexp 1) (point))))
+
+            ((and (c-safe (forward-char -2) t)
+                  (looking-at "*/"))
+             ;; Block comment.  Due to the nature of line
+             ;; comments, they will always be covered by the
+             ;; normal case above.
+             (goto-char end)
+             (c-backward-single-comment)
+             ;; If LIM is bogus, beg will be bogus.
+             (setq beg (point))))
+
+           (if beg (cons beg end))))))
+       ))))
 
 ;; In case external callers use this; it did have a docstring.
 (defalias 'c-literal-limits-fast 'c-literal-limits)
@@ -4904,7 +4858,7 @@ comment at the start of cc-engine.el for more info."
            (setq cfd-prop-match nil))
 
        (when (/= cfd-macro-end 0)
-         ;; Restore limits if we did macro narrowment above.
+         ;; Restore limits if we did macro narrowing above.
          (narrow-to-region (point-min) cfd-buffer-end)))
 
       (goto-char cfd-continue-pos)
@@ -5052,7 +5006,7 @@ comment at the start of cc-engine.el for more info."
 ;; The strategy now (2010-01) adopted is to mark and unmark < and
 ;; > IN MATCHING PAIRS ONLY.  [Previously, they were marked
 ;; individually when their context so indicated.  This gave rise to
-;; intractible problems when one of a matching pair was deleted, or
+;; intractable problems when one of a matching pair was deleted, or
 ;; pulled into a literal.]
 ;;
 ;; At each buffer change, the syntax-table properties are removed in a
@@ -5965,7 +5919,7 @@ comment at the start of cc-engine.el for more info."
   ;;     `*-font-lock-extra-types');
   ;;   o - 'prefix if it's a known prefix of a type;
   ;;   o - 'found if it's a type that matches one in `c-found-types';
-  ;;   o - 'maybe if it's an identfier that might be a type; or
+  ;;   o - 'maybe if it's an identifier that might be a type; or
   ;;   o -  nil if it can't be a type (the point isn't moved then).
   ;;
   ;; The point is assumed to be at the beginning of a token.
@@ -6467,7 +6421,7 @@ comment at the start of cc-engine.el for more info."
                    (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
                      (setq maybe-typeless t))
 
-                   ;; Haven't matched a type so it's an umambiguous
+                   ;; Haven't matched a type so it's an unambiguous
                    ;; specifier keyword and we know we're in a
                    ;; declaration.
                    (setq at-decl-or-cast t)
@@ -6832,7 +6786,7 @@ comment at the start of cc-engine.el for more info."
                       got-suffix-after-parens
                       (eq (char-after got-suffix-after-parens) ?\())
              ;; Got a type, no declarator but a paren suffix. I.e. it's a
-             ;; normal function call afterall (or perhaps a C++ style object
+             ;; normal function call after all (or perhaps a C++ style object
              ;; instantiation expression).
              (throw 'at-decl-or-cast nil))))
 
@@ -7100,7 +7054,7 @@ comment at the start of cc-engine.el for more info."
   ;;   colon).  Currently (2006-03), this applies only to Objective C's
   ;;   keywords "@private", "@protected", and "@public".  Returns t.
   ;;
-  ;; One of the things which will NOT be recognised as a label is a bit-field
+  ;; One of the things which will NOT be recognized as a label is a bit-field
   ;; element of a struct, something like "int foo:5".
   ;;
   ;; The end of the label is taken to be just after the colon, or the end of
@@ -8832,7 +8786,7 @@ comment at the start of cc-engine.el for more info."
        ;; CASE B.4: Continued statement with block open.  The most
        ;; accurate analysis is perhaps `statement-cont' together with
        ;; `block-open' but we play DWIM and use `substatement-open'
-       ;; instead.  The rationaly is that this typically is a macro
+       ;; instead.  The rationale is that this typically is a macro
        ;; followed by a block which makes it very similar to a
        ;; statement with a substatement block.
        (t
@@ -9151,7 +9105,7 @@ comment at the start of cc-engine.el for more info."
                                    'label))
                            (if (eq step 'up)
                                (setq placeholder (point))
-                             ;; There was no containing statement afterall.
+                             ;; There was no containing statement after all.
                              (goto-char placeholder)))))
                    placeholder))
               (if (looking-at c-block-stmt-2-key)
@@ -9582,7 +9536,7 @@ comment at the start of cc-engine.el for more info."
            (c-add-syntax 'inher-cont (c-point 'boi)))
 
           ;; CASE 5D.5: Continuation of the "expression part" of a
-          ;; top level construct.  Or, perhaps, an unrecognised construct.
+          ;; top level construct.  Or, perhaps, an unrecognized construct.
           (t
            (while (and (setq placeholder (point))
                        (eq (car (c-beginning-of-decl-1 containing-sexp))
@@ -9593,7 +9547,7 @@ comment at the start of cc-engine.el for more info."
                        (< (point) placeholder)))
            (c-add-stmt-syntax
             (cond
-             ((eq (point) placeholder) 'statement) ; unrecognised construct
+             ((eq (point) placeholder) 'statement) ; unrecognized construct
              ;; A preceding comma at the top level means that a
              ;; new variable declaration starts here.  Use
              ;; topmost-intro-cont for it, for consistency with
@@ -9784,7 +9738,7 @@ comment at the start of cc-engine.el for more info."
          (c-beginning-of-statement-1 containing-sexp)
          (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
 
-        ;;CASE 5N: We are at a tompmost continuation line and the only
+        ;;CASE 5N: We are at a topmost continuation line and the only
         ;;preceding items are annotations.
         ((and (c-major-mode-is 'java-mode)
               (setq placeholder (point))