Function: c-evaluate-offset

c-evaluate-offset is a byte-compiled function defined in cc-engine.el.gz.

Signature

(c-evaluate-offset OFFSET LANGELEM SYMBOL)

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/cc-engine.el.gz
;; The syntactic symbols so far used in a chain of them.
;; It is used to prevent infinite loops when the OFFSET in `c-evaluate-offset'
;; is itself a syntactic symbol.

(defun c-evaluate-offset (offset langelem symbol)
  ;; Evaluate the offset for OFFSET, returning it either as a number,
  ;; a vector, a symbol (whose value gets used), or nil.
  ;; OFFSET is a number, a function, a syntactic symbol, a variable, a list,
  ;; or a symbol such as +, -, etc.
  ;; LANGELEM is the original language element for which this function is
  ;; being called.
  ;; SYMBOL is the syntactic symbol, used mainly for error messages.
  ;;
  ;; This function might do hidden buffer changes.
  (let*
      (offset1
       (res
	 (cond
	  ((numberp offset) offset)
	  ((vectorp offset) offset)
	  ((null offset)    nil)

	  ((eq offset '+)   c-basic-offset)
	  ((eq offset '-)   (- c-basic-offset))
	  ((eq offset '++)  (* 2 c-basic-offset))
	  ((eq offset '--)  (* 2 (- c-basic-offset)))
	  ((eq offset '*)   (/ c-basic-offset 2))
	  ((eq offset '/)   (/ (- c-basic-offset) 2))

	  ((functionp offset)
	   (c-evaluate-offset
	    (funcall offset
		     (cons (c-langelem-sym langelem)
			   (c-langelem-pos langelem)))
	    langelem symbol))

	  ((setq offset1 (assq offset c-offsets-alist))
	   (when (memq offset c-used-syntactic-symbols)
	     (error "Error evaluating offset %S for %s: \
Infinite loop of syntactic symbols: %S."
		    offset symbol c-used-syntactic-symbols))
	   (let ((c-used-syntactic-symbols
		  (cons symbol c-used-syntactic-symbols)))
	     (c-evaluate-offset (cdr-safe offset1) langelem offset)))

	  ((listp offset)
	   (cond
	    ((eq (car offset) 'quote)
	     (c-benign-error "The offset %S for %s was mistakenly quoted"
			     offset symbol)
	     nil)

	    ((memq (car offset) '(min max))
	     (let (res val (method (car offset)))
	       (setq offset (cdr offset))
	       (while offset
		 (setq val (c-evaluate-offset (car offset) langelem symbol))
		 (cond
		  ((not val))
		  ((not res)
		   (setq res val))
		  ((integerp val)
		   (if (vectorp res)
		       (c-benign-error "\
Error evaluating offset %S for %s: \
Cannot combine absolute offset %S with relative %S in `%s' method"
				       (car offset) symbol res val method)
		     (setq res (funcall method res val))))
		  (t
		   (if (integerp res)
		       (c-benign-error "\
Error evaluating offset %S for %s: \
Cannot combine relative offset %S with absolute %S in `%s' method"
				       (car offset) symbol res val method)
		     (setq res (vector (funcall method (aref res 0)
						(aref val 0)))))))
		 (setq offset (cdr offset)))
	       res))

	    ((eq (car offset) 'add)
	     (let (res val)
	       (setq offset (cdr offset))
	       (while offset
		 (setq val (c-evaluate-offset (car offset) langelem symbol))
		 (cond
		  ((not val))
		  ((not res)
		   (setq res val))
		  ((integerp val)
		   (if (vectorp res)
		       (setq res (vector (+ (aref res 0) val)))
		     (setq res (+ res val))))
		  (t
		   (if (vectorp res)
		       (c-benign-error "\
Error evaluating offset %S for %s: \
Cannot combine absolute offsets %S and %S in `add' method"
				       (car offset) symbol res val)
		     (setq res val))))	; Override.
		 (setq offset (cdr offset)))
	       res))

	    (t
	     (let (res)
	       (when (eq (car offset) 'first)
		 (setq offset (cdr offset)))
	       (while (and (not res) offset)
		 (setq res (c-evaluate-offset (car offset) langelem symbol)
		       offset (cdr offset)))
	       res))))

	  ((and (symbolp offset) (boundp offset))
	   (symbol-value offset))

	  (t
	   (c-benign-error "Unknown offset format %S for %s" offset symbol)
	   nil))))

    (if (or (null res) (integerp res)
	    (and (vectorp res) (>= (length res) 1) (integerp (aref res 0))))
	res
      (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
		      offset symbol res)
      nil)))