Function: eshell-rewrite-if-command

eshell-rewrite-if-command is a byte-compiled function defined in esh-cmd.el.gz.

Signature

(eshell-rewrite-if-command TERMS)

Documentation

Rewrite an if command into its equivalent Eshell command form.

Because the implementation of if relies upon conditional evaluation of its argument (i.e., use of a Lisp special form), it must be implemented via rewriting, rather than as a function.

Source Code

;; Defined in /usr/src/emacs/lisp/eshell/esh-cmd.el.gz
(defun eshell-rewrite-if-command (terms)
  "Rewrite an `if' command into its equivalent Eshell command form.
Because the implementation of `if' relies upon conditional
evaluation of its argument (i.e., use of a Lisp special form), it
must be implemented via rewriting, rather than as a function."
  (when (and (stringp (car terms))
             (member (car terms) '("if" "unless")))
    (unless (cadr terms)
      (error "Missing test for `while' command"))
    (let ((condition (eshell-silence-test-command (cadr terms)))
          (then (caddr terms))
          (else (if (equal (nth 3 terms) "else")
                    ;; If there's an "else" keyword, allow chaining
                    ;; together multiple "if" forms...
                    (or (eshell-rewrite-if-command (nthcdr 4 terms))
                        (nth 4 terms))
                  ;; ... otherwise, only allow a single "else" block
                  ;; (without the keyword) as before for compatibility.
                  (nth 3 terms))))
      (unless (string= (car terms) "if")
        (setq condition `(not ,condition)))
      `(if ,condition ,then ,else))))