Function: c-remove-stale-state-cache

c-remove-stale-state-cache is a byte-compiled function defined in cc-engine.el.gz.

Signature

(c-remove-stale-state-cache START-POINT HERE PPS-POINT)

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/cc-engine.el.gz
(defun c-remove-stale-state-cache (start-point here pps-point)
  ;; Remove stale entries from the `c-state-cache', i.e. those which will
  ;; not be in it when it is amended for position HERE.  This may involve
  ;; replacing a CONS element for a brace pair containing HERE with its car.
  ;; Additionally, the "outermost" open-brace entry before HERE will be
  ;; converted to a cons if the matching close-brace is below HERE.
  ;;
  ;; START-POINT is a "maximal" "safe position" - there must be no open
  ;; parens/braces/brackets between START-POINT and HERE.
  ;;
  ;; As a second thing, calculate the result of parse-partial-sexp at
  ;; PPS-POINT, w.r.t. START-POINT.  The motivation here is that
  ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
  ;; adjust it to get outside a string/comment.	 (Sorry about this!  The code
  ;; needs to be FAST).
  ;;
  ;; Return a list (GOOD-POS SCAN-BACK-POS CONS-SEPARATED PPS-STATE), where
  ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
  ;;   to be good (we aim for this to be as high as possible);
  ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
  ;;   preceding POS which needs to be recorded in `c-state-cache'.  It is a
  ;;   position to scan backwards from.  It is the position of the "{" of the
  ;;   last element to be removed from `c-state-cache', when that elt is a
  ;;   cons, otherwise nil.
  ;; o - CONS-SEPARATED is t when a cons element in `c-state-cache' has been
  ;;   replaced by its car because HERE lies inside the brace pair represented
  ;;   by the cons.
  ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
  (save-excursion
    (save-restriction
      (narrow-to-region 1 (point-max))
      (let* ((in-macro-start   ; start of macro containing HERE or nil.
	      (save-excursion
		(goto-char here)
		(and (c-beginning-of-macro)
		     (point))))
	     (start-point-actual-macro-start ; Start of macro containing
					     ; start-point or nil
	      (and (< start-point here)
		   (save-excursion
		     (goto-char start-point)
		     (and (c-beginning-of-macro)
			  (point)))))
	     (start-point-actual-macro-end ; End of this macro, (maybe
					; HERE), or nil.
	      (and start-point-actual-macro-start
		   (save-excursion
		     (goto-char start-point-actual-macro-start)
		     (c-end-of-macro)
		     (point))))
	     pps-state			; Will be 9 or 10 elements long.
	     pos
	     upper-lim	   ; ,beyond which `c-state-cache' entries are removed
	     scan-back-pos
	     cons-separated
	     pair-beg target-depth)

	;; Remove entries beyond HERE.  Also remove any entries inside
	;; a macro, unless HERE is in the same macro.
	(setq upper-lim
	      (if (or (null c-state-old-cpp-beg)
		      (and (> here c-state-old-cpp-beg)
			   (< here c-state-old-cpp-end)))
		  here
		(min here c-state-old-cpp-beg)))
	(while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
	  (setq scan-back-pos (car-safe (car c-state-cache)))
	  (setq c-state-cache (cdr c-state-cache)))

	;; If `upper-lim' is inside the last recorded brace pair, remove its
	;; RBrace and indicate we'll need to search backwards for a previous
	;; brace pair.
	(when (and c-state-cache
		   (consp (car c-state-cache))
		   (> (cdar c-state-cache) upper-lim))
	  (setq c-state-cache (cons (caar c-state-cache) (cdr c-state-cache)))
	  (setq scan-back-pos (car c-state-cache)
		cons-separated t))

	;; The next loop jumps forward out of a nested level of parens each
	;; time round; the corresponding elements in `c-state-cache' are
	;; removed.  `pos' is just after the brace-pair or the open paren at
	;; (car c-state-cache).  There can be no open parens/braces/brackets
	;; between `start-point'/`start-point-actual-macro-start' and HERE,
	;; due to the interface spec to this function.
	(setq pos (if (and start-point-actual-macro-end
			   (not (eq start-point-actual-macro-start
				    in-macro-start)))
		      (1+ start-point-actual-macro-end) ; get outside the macro as
					; marked by a `category' text property.
		    start-point))
	(goto-char pos)
	(while (and c-state-cache
		    (or (numberp (car c-state-cache)) ; Have we a { at all?
			(cdr c-state-cache))
		    (< (point) here))
	  (cond
	   ((null pps-state)		; first time through
	    (setq target-depth -1))
	   ((eq (car pps-state) target-depth) ; found closing ),},]
	    (setq target-depth (1- (car pps-state))))
	   ;; Do nothing when we've merely reached pps-point.
	   )

	  ;; Scan!
	  (setq pps-state
		(c-sc-parse-partial-sexp
		 (point) (if (< (point) pps-point) pps-point here)
		 target-depth
		 nil pps-state))

	  (when (eq (car pps-state) target-depth)
	    (setq pos (point))	     ; POS is now just after an R-paren/brace.
	    (cond
	     ((and (consp (car c-state-cache))
		   (eq (point) (cdar c-state-cache)))
		;; We've just moved out of the paren pair containing the brace-pair
		;; at (car c-state-cache).  `pair-beg' is where the open paren is,
		;; and is potentially where the open brace of a cons in
		;; c-state-cache will be.
	      (setq pair-beg (car-safe (cdr c-state-cache))
		    c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
	     ((numberp (car c-state-cache))
	      (setq pair-beg (car c-state-cache)
		    c-state-cache (cdr c-state-cache))) ; remove this
					; containing Lparen
	     ((numberp (cadr c-state-cache))
	      (setq pair-beg (cadr c-state-cache)
		    c-state-cache (cddr c-state-cache))) ; Remove a paren pair
					; together with enclosed brace pair.
	     ;; (t nil)			; Ignore an unmated Rparen.
	     )))

	(if (< (point) pps-point)
	    (setq pps-state (c-sc-parse-partial-sexp
			     (point) pps-point
			     nil nil ; TARGETDEPTH, STOPBEFORE
			     pps-state)))

	;; If the last paren pair we moved out of was actually a brace pair,
	;; insert it into `c-state-cache'.
	(when (and pair-beg (eq (char-after pair-beg) ?{))
	  (if (consp (car-safe c-state-cache))
	      (setq c-state-cache (cdr c-state-cache)))
	  (setq c-state-cache (cons (cons pair-beg pos)
				    c-state-cache)))

	(list pos scan-back-pos cons-separated pps-state)))))