Function: calc-commute-right

calc-commute-right is an autoloaded, interactive and byte-compiled function defined in calcsel2.el.gz.

Signature

(calc-commute-right ARG)

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/calc/calcsel2.el.gz
(defun calc-commute-right (arg)
  (interactive "p")
  (if (< arg 0)
      (calc-commute-left (- arg))
    (calc-wrapper
     (calc-preserve-point)
     (let ((num (max 1 (calc-locate-cursor-element (point))))
	   (calc-sel-reselect calc-keep-selection))
       (if (= arg 0) (setq arg nil))
       (while (or (null arg) (>= (setq arg (1- arg)) 0))
	 (let* ((entry (calc-top num 'entry))
		(expr (car entry))
		(sel (calc-auto-selection entry))
		parent new)
	   (or (and sel
		    (consp (setq parent (calc-find-assoc-parent-formula
					 expr sel))))
	       (error "No term is selected"))
	   (if (and calc-assoc-selections
		    (assq (car parent) calc-assoc-ops))
	       (let ((outer (calc-find-parent-formula parent sel)))
		 (if (eq sel (nth 1 outer))
		     (setq new (calc-replace-sub-formula
				parent outer
				(if (memq (car outer)
					  (nth 2 (assq (car-safe (nth 2 outer))
						       calc-assoc-ops)))
				    (let ((other (nth 1 (nth 2 outer))))
				      (calc-build-assoc-term
				       (car outer)
				       other
				       (calc-build-assoc-term
					(car (nth 2 outer))
					sel
					(nth 2 (nth 2 outer)))))
				  (let ((new (cond
					      ((eq (car outer) '-)
					       (calc-build-assoc-term
						'+
						(math-neg (nth 2 outer))
						sel))
					      ((eq (car outer) '/)
					       (calc-build-assoc-term
						'*
						(calcFunc-div 1 (nth 2 outer))
						sel))
					      (t (calc-build-assoc-term
						  (car outer)
						  (nth 2 outer)
						  sel)))))
				    (setq sel (nth 2 new))
				    new))))
		   (let ((next (calc-find-parent-formula parent outer)))
		     (if (not (and (consp next)
				   (eq outer (nth 1 next))))
			 (setq new nil)
		       (setq new (calc-build-assoc-term
				  (car outer)
				  (calc-build-assoc-term
				   (car next) (nth 1 outer) (nth 2 next))
				  sel)
			     sel (nth 2 new)
			     new (calc-replace-sub-formula
				  parent next new))))))
	     (if (eq (nth (1- (length parent)) parent) sel)
		 (setq new nil)
	       (let ((p (nthcdr (calc-find-sub-formula parent sel)
				(setq new (copy-sequence parent)))))
		 (setcar p (nth 1 p))
		 (setcar (cdr p) sel))))
	   (if (null new)
	       (if arg
		   (error "Term is already rightmost")
		 (or calc-sel-reselect
		     (calc-pop-push-list 1 (list expr) num '(nil)))
		 (setq arg 0))
	     (calc-pop-push-record-list
	      1 "rght"
	      (list (calc-replace-sub-formula expr parent new))
	      num
	      (list (and (or (not (eq arg 0)) calc-sel-reselect)
			 sel))))))))))