Function: ibuffer-compile-format

ibuffer-compile-format is a byte-compiled function defined in ibuffer.el.gz.

Signature

(ibuffer-compile-format FORMAT)

Source Code

;; Defined in /usr/src/emacs/lisp/ibuffer.el.gz
(defun ibuffer-compile-format (format)
  (let ((result nil)
	;; We use these variables to keep track of which variables
	;; inside the generated function we need to bind, since
	;; binding variables in Emacs takes time.
	(vars-used ()))
    (dolist (form format)
      (push
       ;; Generate a form based on a particular format entry, like
       ;; " ", mark, or (mode 16 16 :right).
       (if (stringp form)
	   ;; It's a string; all we need to do is insert it.
	   `(insert ,form)
	 (let* ((form (ibuffer-expand-format-entry form))
		(sym (nth 0 form))
		(min (nth 1 form))
		(max (nth 2 form))
		(align (nth 3 form))
		(elide (nth 4 form)))
	   (let* ((from-end-p (when (cl-minusp min)
				(setq min (- min))
				t))
		  (letbindings nil)
		  (outforms nil)
		  minform
		  maxform
		  min-used max-used strlen-used)
	     (when (or (not (integerp min)) (>= min 0))
	       ;; This is a complex case; they want it limited to a
	       ;; minimum size.
	       (setq min-used t)
               (setq strlen-used t)
	       (setq vars-used '(str strlen tmp1 tmp2))
	       ;; Generate code to limit the string to a minimum size.
	       (setq minform `(progn
				(setq str
				      ,(ibuffer-compile-make-format-form
					'str
					`(- ,(if (integerp min)
						 min
					       'min)
					    strlen)
					align)))))
	     (when (or (not (integerp max)) (> max 0))
	       (setq max-used t)
               (cl-pushnew 'str vars-used)
	       ;; Generate code to limit the string to a maximum size.
	       (setq maxform `(progn
				(setq str
				      ,(ibuffer-compile-make-substring-form
					'str
					(if (integerp max)
					    max
					  'max)
					from-end-p))
				(setq strlen (string-width str))
				(setq str
				      ,(ibuffer-compile-make-eliding-form
                                        'str elide from-end-p)))))
	     ;; Now, put these forms together with the rest of the code.
	     (let ((callform
		    ;; Is this an "inline" column?  This means we have
		    ;; to get the code from the
		    ;; `ibuffer-inline-columns' alist and insert it
		    ;; into our generated code.  Otherwise, we just
		    ;; generate a call to the column function.
                    (if-let ((it (assq sym ibuffer-inline-columns)))
			(nth 1 it)
		      `(or (,sym buffer mark) "")))
		   ;; You're not expected to understand this.  Hell, I
		   ;; don't even understand it, and I wrote it five
		   ;; minutes ago.
		   (insertgenfn
                    (if (get sym 'ibuffer-column-summarizer)
                        ;; I really, really wish Emacs Lisp had closures.
                        ;; FIXME: Elisp does have them now.
                        (lambda (arg sym)
                          `(insert
                            (let ((ret ,arg))
                              (put ',sym 'ibuffer-column-summary
                                   (cons ret (get ',sym
                                                  'ibuffer-column-summary)))
                              ret)))
                      (lambda (arg _sym)
                        `(insert ,arg))))
		   (mincompform `(< strlen ,(if (integerp min)
						min
					      'min)))
		   (maxcompform `(> strlen ,(if (integerp max)
						max
					      'max))))
	       (if (or min-used max-used)
		   ;; The complex case, where we have to limit the
		   ;; form to a maximum or minimum size.
		   (progn
		     (when (and min-used (not (integerp min)))
		       (push `(min ,min) letbindings))
		     (when (and max-used (not (integerp max)))
		       (push `(max ,max) letbindings))
		     (push
		      (if (and min-used max-used)
			  `(if ,mincompform
			       ,minform
			     (if ,maxcompform
				 ,maxform))
			(if min-used
			    `(when ,mincompform
			       ,minform)
			  `(when ,maxcompform
			     ,maxform)))
		      outforms)
		     (push `(setq str ,callform
                                  ,@(when strlen-used
                                      '(strlen (string-width str))))
			   outforms)
		     (setq outforms
			   (append outforms
                                   (list (funcall insertgenfn 'str sym)))))
		 ;; The simple case; just insert the string.
		 (push (funcall insertgenfn callform sym) outforms))
	       ;; Finally, return a `let' form which binds the
	       ;; variables in `letbindings', and contains all the
	       ;; code in `outforms'.
	       `(let ,letbindings
		  ,@outforms)))))
       result))
    ;; We don't want to unconditionally load the byte-compiler.
    (funcall (if (or ibuffer-always-compile-formats
                     (featurep 'bytecomp))
                 #'byte-compile
               #'identity)
             ;; Here, we actually create a lambda form which
             ;; inserts all the generated forms for each entry
             ;; in the format string.
             `(lambda (buffer mark)
                (let ,vars-used
                  ,@(nreverse result))))))