Function: calculator-op

calculator-op is an interactive and byte-compiled function defined in calculator.el.gz.

Signature

(calculator-op &optional KEYS)

Documentation

Enter an operator on the stack, doing all necessary reductions.

Optional string argument KEYS will force using it as the keys entered.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/calculator.el.gz
(defun calculator-op (&optional keys)
  "Enter an operator on the stack, doing all necessary reductions.
Optional string argument KEYS will force using it as the keys entered."
  (interactive)
  (catch 'op-error
    (let* ((last-inp (calculator-last-input keys))
           (op (assoc last-inp calculator-operators)))
      (calculator-clear-fragile op)
      (calculator-push-curnum)
      (when (and (= 2 (calculator-op-arity op))
                 (not (numberp (car calculator-stack))))
        ;; we have a binary operator but no number -- search for a
        ;; prefix version
        (setq op (assoc last-inp (cdr (memq op calculator-operators))))
        (unless (and op (= -1 (calculator-op-arity op)))
          (calculator-message "Binary operator without a first operand")
          (throw 'op-error nil)))
      (calculator-reduce-stack
       (cond ((eq (nth 1 op) '\() 10)
             ((eq (nth 1 op) '\)) 0)
             (t (calculator-op-prec op))))
      (when (let ((hasnum (numberp (car calculator-stack))))
              (pcase (calculator-op-arity op)
                (-1 hasnum)
                ((or 1 2) (not hasnum))))
        (calculator-message "Incomplete expression")
        (throw 'op-error nil))
      (push op calculator-stack)
      (calculator-reduce-stack (calculator-op-prec op))
      (when (and (= (length calculator-stack) 1)
                 (numberp (car calculator-stack)))
        ;; the display is fragile if it contains only one number
        (setq calculator-display-fragile t)
        (when calculator-add-saved ; add number to the saved-list
          (push (car calculator-stack)
                (nthcdr calculator-saved-ptr calculator-saved-list))))
      (calculator-update-display))))