1 ;;; calc-sel.el --- data selection functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This file is autoloaded from calc-ext.el.
31 ;;; Selection commands.
33 (defvar calc-keep-selection t)
35 (defvar calc-selection-cache-entry nil)
36 (defvar calc-selection-cache-num)
37 (defvar calc-selection-cache-comp)
38 (defvar calc-selection-cache-offset)
39 (defvar calc-selection-true-num)
41 (defun calc-select-here (num &optional once keep)
44 (calc-prepare-selection)
45 (let ((found (calc-find-selected-part))
46 (entry calc-selection-cache-entry))
47 (or (and keep (nth 2 entry))
50 (setq calc-keep-selection nil)
51 (message "(Selection will apply to next command only)")))
52 (calc-change-current-selection
54 (if (and num (> (setq num (prefix-numeric-value num)) 0))
56 (while (and (>= (setq num (1- num)) 0)
57 (not (eq found (car entry))))
58 (setq found (calc-find-assoc-parent-formula
61 (calc-grow-assoc-formula (car entry) found))
64 (defun calc-select-once (num)
66 (calc-select-here num t))
68 (defun calc-select-here-maybe (num)
70 (calc-select-here num nil t))
72 (defun calc-select-once-maybe (num)
74 (calc-select-here num t t))
76 (defun calc-select-additional ()
79 (let (calc-keep-selection)
80 (calc-prepare-selection))
81 (let ((found (calc-find-selected-part))
82 (entry calc-selection-cache-entry))
83 (calc-change-current-selection
85 (let ((sel (nth 2 entry)))
88 (while (not (or (eq sel (car entry))
89 (calc-find-sub-formula sel found)))
90 (setq sel (calc-find-assoc-parent-formula
93 (calc-grow-assoc-formula (car entry) found)))
96 (defun calc-select-more (num)
99 (calc-prepare-selection)
100 (let ((entry calc-selection-cache-entry))
102 (let ((sel (nth 2 entry)))
103 (while (and (not (eq sel (car entry)))
104 (>= (setq num (1- (prefix-numeric-value num))) 0))
105 (setq sel (calc-find-assoc-parent-formula (car entry) sel)))
106 (calc-change-current-selection sel))
107 (calc-select-here num)))))
109 (defun calc-select-less (num)
112 (calc-prepare-selection)
113 (let ((found (calc-find-selected-part))
114 (entry calc-selection-cache-entry))
115 (calc-change-current-selection
117 (let ((sel (nth 2 entry))
121 (>= (setq num (1- num)) 0))
123 index (calc-find-sub-formula sel found))
124 (and (setq sel (and index (nth index old)))
125 calc-assoc-selections
126 (setq op (assq (car-safe sel) calc-assoc-ops))
127 (memq (car old) (nth index op))
128 (setq num (1+ num))))
131 (defun calc-select-part (num)
133 (or num (setq num (- last-command-char ?0)))
135 (calc-prepare-selection)
136 (let ((sel (calc-find-nth-part (or (nth 2 calc-selection-cache-entry)
137 (car calc-selection-cache-entry))
140 (calc-change-current-selection sel)
141 (error "%d is not a valid sub-formula index" num)))))
143 ;; The variables calc-fnp-op and calc-fnp-num are local to
144 ;; calc-find-nth-part (and calc-select-previous) but used by
145 ;; calc-find-nth-part-rec, which is called by them.
147 (defvar calc-fnp-num)
149 (defun calc-find-nth-part (expr calc-fnp-num)
150 (if (and calc-assoc-selections
151 (assq (car-safe expr) calc-assoc-ops))
153 (calc-find-nth-part-rec expr))
154 (if (eq (car-safe expr) 'intv)
155 (and (>= calc-fnp-num 1) (<= calc-fnp-num 2) (nth (1+ calc-fnp-num) expr))
156 (and (not (Math-primp expr)) (>= calc-fnp-num 1) (< calc-fnp-num (length expr))
157 (nth calc-fnp-num expr)))))
159 (defun calc-find-nth-part-rec (expr) ; uses num, op
160 (or (if (and (setq calc-fnp-op (assq (car-safe (nth 1 expr)) calc-assoc-ops))
161 (memq (car expr) (nth 1 calc-fnp-op)))
162 (calc-find-nth-part-rec (nth 1 expr))
163 (and (= (setq calc-fnp-num (1- calc-fnp-num)) 0)
165 (if (and (setq calc-fnp-op (assq (car-safe (nth 2 expr)) calc-assoc-ops))
166 (memq (car expr) (nth 2 calc-fnp-op)))
167 (calc-find-nth-part-rec (nth 2 expr))
168 (and (= (setq calc-fnp-num (1- calc-fnp-num)) 0)
171 (defun calc-select-next (num)
174 (calc-select-previous (- num))
176 (calc-prepare-selection)
177 (let* ((entry calc-selection-cache-entry)
181 (while (>= (setq num (1- num)) 0)
182 (let* ((parent (calc-find-parent-formula (car entry) sel))
185 (and (eq p t) (setq p nil))
186 (while (and (setq p (cdr p))
187 (not (eq (car p) sel))))
189 (setq sel (or (and calc-assoc-selections
190 (setq op (assq (car-safe (nth 1 p))
192 (memq (car parent) (nth 2 op))
195 (if (and calc-assoc-selections
196 (setq op (assq (car-safe parent) calc-assoc-ops))
197 (consp (setq p (calc-find-parent-formula
198 (car entry) parent)))
199 (eq (nth 1 p) parent)
200 (memq (car p) (nth 1 op)))
202 (error "No \"next\" sub-formula")))))
203 (calc-change-current-selection sel))
204 (if (Math-primp (car entry))
205 (calc-change-current-selection (car entry))
206 (calc-select-part num)))))))
208 (defun calc-select-previous (num)
211 (calc-select-next (- num))
213 (calc-prepare-selection)
214 (let* ((entry calc-selection-cache-entry)
218 (while (>= (setq num (1- num)) 0)
219 (let* ((parent (calc-find-parent-formula (car entry) sel))
220 (p (cdr-safe parent))
223 (if (eq (car-safe parent) 'intv) (setq p (cdr p)))
224 (while (and (not (eq (car p) sel))
228 (setq sel (or (and calc-assoc-selections
229 (setq op (assq (car-safe prev)
231 (memq (car parent) (nth 1 op))
234 (if (and calc-assoc-selections
235 (setq op (assq (car-safe parent) calc-assoc-ops))
236 (consp (setq p (calc-find-parent-formula
237 (car entry) parent)))
238 (eq (nth 2 p) parent)
239 (memq (car p) (nth 2 op)))
241 (error "No \"previous\" sub-formula")))))
242 (calc-change-current-selection sel))
243 (if (Math-primp (car entry))
244 (calc-change-current-selection (car entry))
245 (let ((len (if (and calc-assoc-selections
246 (assq (car (car entry)) calc-assoc-ops))
247 (let (calc-fnp-op (calc-fnp-num 0))
248 (calc-find-nth-part-rec (car entry))
250 (length (car entry)))))
251 (calc-select-part (- len num)))))))))
253 (defun calc-find-parent-formula (expr part)
254 (cond ((eq expr part) t)
255 ((Math-primp expr) nil)
258 (while (and (setq p (cdr p))
259 (not (setq res (calc-find-parent-formula
262 (if (eq res t) expr res))))))
265 (defun calc-find-assoc-parent-formula (expr part)
266 (calc-grow-assoc-formula expr (calc-find-parent-formula expr part)))
268 (defun calc-grow-assoc-formula (expr part)
269 (if calc-assoc-selections
270 (let ((op (assq (car-safe part) calc-assoc-ops)))
273 (while (and (consp (setq new (calc-find-parent-formula
276 (nth (calc-find-sub-formula new part) op)))
281 (defun calc-find-sub-formula (expr part)
282 (cond ((eq expr part) t)
283 ((Math-primp expr) nil)
286 (while (and (setq expr (cdr expr))
287 (not (calc-find-sub-formula (car expr) part)))
291 (defun calc-unselect (num)
294 (calc-prepare-selection num)
295 (calc-change-current-selection nil)))
297 (defun calc-clear-selections ()
300 (let ((limit (calc-stack-size))
303 (if (calc-top n 'sel)
305 (calc-prepare-selection n)
306 (calc-change-current-selection nil)))
308 (calc-clear-command-flag 'position-point)))
310 (defvar calc-highlight-selections-with-faces)
312 (defun calc-show-selections (arg)
315 (calc-preserve-point)
316 (setq calc-show-selections (if arg
317 (> (prefix-numeric-value arg) 0)
318 (not calc-show-selections)))
319 (let ((p calc-stack))
321 (or (null (nth 2 (car p)))
322 (equal (car p) calc-selection-cache-entry)))
325 (let ((calc-selection-cache-default-entry
326 calc-selection-cache-entry))
328 (and calc-selection-cache-entry
329 (let ((sel (nth 2 calc-selection-cache-entry)))
330 (setcar (nthcdr 2 calc-selection-cache-entry) nil)
331 (calc-change-current-selection sel)))))
332 (message (if calc-show-selections
333 (if calc-highlight-selections-with-faces
334 "De-emphasizing all but selected part of formulas"
335 "Displaying only selected part of formulas")
336 (if calc-highlight-selections-with-faces
337 "Emphasizing selected part of formulas"
338 "Displaying all but selected part of formulas")))))
340 ;; The variables calc-final-point-line and calc-final-point-column
341 ;; are declared in calc.el, and are used throughout.
342 (defvar calc-final-point-line)
343 (defvar calc-final-point-column)
345 (defun calc-preserve-point ()
346 (or (looking-at "\\.\n+\\'")
348 (setq calc-final-point-line (+ (count-lines (point-min) (point))
350 calc-final-point-column (current-column))
351 (calc-set-command-flag 'position-point))))
353 (defun calc-enable-selections (arg)
356 (calc-preserve-point)
357 (setq calc-use-selections (if arg
358 (> (prefix-numeric-value arg) 0)
359 (not calc-use-selections)))
360 (calc-set-command-flag 'renum-stack)
361 (message (if calc-use-selections
362 "Commands operate only on selected sub-formulas"
363 "Selections of sub-formulas have no effect"))))
365 (defun calc-break-selections (arg)
368 (calc-preserve-point)
369 (setq calc-assoc-selections (if arg
370 (<= (prefix-numeric-value arg) 0)
371 (not calc-assoc-selections)))
372 (message (if calc-assoc-selections
373 "Selection treats a+b+c as a sum of three terms"
374 "Selection treats a+b+c as (a+b)+c"))))
376 (defun calc-prepare-selection (&optional num)
377 (or num (setq num (calc-locate-cursor-element (point))))
378 (setq calc-selection-true-num num
379 calc-keep-selection t)
380 (or (> num 0) (setq num 1))
381 (let* ((entry (calc-top num 'entry)))
382 (or (equal entry calc-selection-cache-entry)
384 (setcar entry (calc-encase-atoms (car entry)))
385 (setq calc-selection-cache-entry entry
386 calc-selection-cache-num num
387 calc-selection-cache-comp
388 (let ((math-comp-tagged t))
389 (math-compose-expr (car entry) 0))
390 calc-selection-cache-offset
391 (+ (car (math-stack-value-offset calc-selection-cache-comp))
392 (length calc-left-label)
393 (if calc-line-numbering 4 0))))))
394 (calc-preserve-point))
396 ;;; The following ensures that no two subformulas will be "eq" to each other!
397 (defun calc-encase-atoms (x)
398 (if (or (not (consp x))
399 (equal x '(float 0 0)))
401 (calc-encase-atoms-rec x)
404 (defun calc-encase-atoms-rec (x)
407 (if (eq (car x) 'intv)
409 (while (setq x (cdr x))
410 (if (or (not (consp (car x)))
411 (equal (car x) '(float 0 0)))
412 (setcar x (list 'cplx (car x) 0))
413 (calc-encase-atoms-rec (car x)))))))
415 ;; The variable math-comp-sel-tag is local to calc-find-selected-part,
416 ;; but is used by math-comp-sel-flat-term and math-comp-add-string-sel
417 ;; in calccomp.el, which are called (indirectly) by calc-find-selected-part.
419 (defun calc-find-selected-part ()
420 (let* ((math-comp-sel-hpos (- (current-column) calc-selection-cache-offset))
424 (math-comp-sel-vpos (save-excursion
426 (let ((line (point)))
427 (calc-cursor-stack-index
428 calc-selection-cache-num)
430 (while (< (point) line)
432 (setq spaces (+ spaces
433 (current-indentation))
435 (- lcount (math-comp-ascent
436 calc-selection-cache-comp) -1))))
437 (math-comp-sel-cpos (- (point) toppt calc-selection-cache-offset
439 (math-comp-sel-tag nil))
440 (and (>= math-comp-sel-hpos 0)
441 (> calc-selection-true-num 0)
442 (math-composition-to-string calc-selection-cache-comp 1000000))
443 (nth 1 math-comp-sel-tag)))
445 (defun calc-change-current-selection (sub-expr)
446 (or (eq sub-expr (nth 2 calc-selection-cache-entry))
447 (let ((calc-prepared-composition calc-selection-cache-comp)
448 (buffer-read-only nil)
450 (calc-set-command-flag 'renum-stack)
451 (setcar (nthcdr 2 calc-selection-cache-entry) sub-expr)
452 (calc-cursor-stack-index calc-selection-cache-num)
454 (calc-cursor-stack-index (1- calc-selection-cache-num))
455 (delete-region top (point))
456 (let ((calc-selection-cache-default-entry calc-selection-cache-entry))
457 (insert (math-format-stack-value calc-selection-cache-entry)
460 (defun calc-top-selected (&optional n m)
461 (and calc-any-selections
466 (calc-check-stack (+ n m -1))
467 (let ((top (nthcdr (+ m calc-stack-top -1) calc-stack))
469 (while (>= (setq n (1- n)) 0)
470 (if (nth 2 (car top))
471 (setq sel (if sel t (nth 2 (car top)))))
472 (setq top (cdr top)))
475 ;; The variables calc-rsf-old and calc-rsf-new are local to
476 ;; calc-replace-sub-formula, but used by calc-replace-sub-formula-rec,
477 ;; which is called by calc-replace-sub-formula.
478 (defvar calc-rsf-old)
479 (defvar calc-rsf-new)
481 (defun calc-replace-sub-formula (expr calc-rsf-old calc-rsf-new)
482 (setq calc-rsf-new (calc-encase-atoms calc-rsf-new))
483 (calc-replace-sub-formula-rec expr))
485 (defun calc-replace-sub-formula-rec (expr)
486 (cond ((eq expr calc-rsf-old) calc-rsf-new)
487 ((Math-primp expr) expr)
490 (mapcar 'calc-replace-sub-formula-rec (cdr expr))))))
492 (defun calc-sel-error ()
493 (error "Invalid operation on sub-formulas"))
495 (defun calc-replace-selections (n vals m)
496 (if (calc-top-selected n m)
497 (let ((num (length vals)))
498 (calc-preserve-point)
501 (let* ((old (calc-top-list n m 'entry))
506 (if (nth 2 (car old))
507 (setq val (calc-encase-atoms (car vals))
508 new (cons (calc-replace-sub-formula (car (car old))
513 (setq new (cons (car vals) new)
515 (setq vals (cdr vals)
517 (calc-pop-stack n m t)
518 (calc-push-list (nreverse new)
519 m (and calc-keep-selection (nreverse sel)))))
521 (let* ((old (calc-top-list n m 'entry))
523 (while (and old (not (nth 2 (car old))))
524 (setq old (cdr old)))
526 (while (and (setq more (cdr more)) (not (nth 2 (car more)))))
529 (calc-pop-stack n m t)
531 (let ((val (calc-encase-atoms (car vals))))
532 (calc-push-list (list (calc-replace-sub-formula
536 m (and calc-keep-selection (list val))))
537 (calc-push-list vals))))
538 (t (calc-sel-error))))
539 (calc-pop-stack n m t)
540 (calc-push-list vals m)))
542 (defun calc-delete-selection (n)
543 (let ((entry (calc-top n 'entry)))
545 (if (eq (nth 2 entry) (car entry))
547 (calc-pop-stack 1 n t)
548 (calc-push-list '(0) n))
549 (let ((parent (calc-find-parent-formula (car entry) (nth 2 entry)))
551 (calc-preserve-point)
552 (calc-pop-stack 1 n t)
553 (cond ((or (memq (car parent) '(* / %))
554 (and (eq (car parent) '^)
555 (eq (nth 2 parent) (nth 2 entry))))
557 ((memq (car parent) '(vec calcFunc-min calcFunc-max)))
558 ((and (assq (car parent) calc-tweak-eqn-table)
559 (= (length parent) 3))
565 (calc-push-list (list
567 (calc-replace-sub-formula
570 (if (eq (nth 2 entry) (nth 1 parent))
575 (calc-push-list (list
577 (calc-replace-sub-formula (car entry)
582 (calc-push-list (list
584 (calc-replace-sub-formula (car entry)
590 (calc-pop-stack 1 n t))))
592 (defun calc-roll-down-with-selections (n m)
593 (let ((vals (append (calc-top-list m 1)
594 (calc-top-list (- n m) (1+ m))))
595 (sels (append (calc-top-list m 1 'sel)
596 (calc-top-list (- n m) (1+ m) 'sel))))
597 (calc-pop-push-list n vals 1 sels)))
599 (defun calc-roll-up-with-selections (n m)
600 (let ((vals (append (calc-top-list (- n m) 1)
601 (calc-top-list m (- n m -1))))
602 (sels (append (calc-top-list (- n m) 1 'sel)
603 (calc-top-list m (- n m -1) 'sel))))
604 (calc-pop-push-list n vals 1 sels)))
606 ;; The variable calc-sel-reselect is local to several functions
607 ;; which call calc-auto-selection.
608 (defvar calc-sel-reselect)
610 (defun calc-auto-selection (entry)
613 (setq calc-sel-reselect nil)
614 (calc-prepare-selection)
615 (calc-grow-assoc-formula (car entry) (calc-find-selected-part)))))
617 (defun calc-copy-selection ()
620 (calc-preserve-point)
621 (let* ((num (max 1 (calc-locate-cursor-element (point))))
622 (entry (calc-top num 'entry)))
623 (calc-push (or (calc-auto-selection entry) (car entry))))))
625 (defun calc-del-selection ()
628 (calc-preserve-point)
629 (let* ((num (max 1 (calc-locate-cursor-element (point))))
630 (entry (calc-top num 'entry))
631 (sel (calc-auto-selection entry)))
632 (setcar (nthcdr 2 entry) (and (not (eq sel (car entry))) sel))
633 (calc-delete-selection num))))
635 (defvar calc-selection-history nil
636 "History for calc selections.")
638 (defun calc-enter-selection ()
641 (calc-preserve-point)
642 (let* ((num (max 1 (calc-locate-cursor-element (point))))
643 (calc-sel-reselect calc-keep-selection)
644 (entry (calc-top num 'entry))
646 (sel (or (calc-auto-selection entry) expr))
648 (let ((calc-dollar-values (list sel))
649 (calc-dollar-used 0))
650 (setq alg (calc-do-alg-entry "" "Replace selection with: " nil
651 'calc-selection-history))
654 (setq alg (calc-encase-atoms (car alg)))
655 (calc-pop-push-record-list 1 "repl"
656 (list (calc-replace-sub-formula
659 (list (and calc-sel-reselect alg))))))
660 (calc-handle-whys))))
662 (defun calc-edit-selection ()
665 (calc-preserve-point)
666 (let* ((num (max 1 (calc-locate-cursor-element (point))))
667 (calc-sel-reselect calc-keep-selection)
668 (entry (calc-top num 'entry))
670 (sel (or (calc-auto-selection entry) expr)))
671 (let ((str (math-showing-full-precision
672 (math-format-nice-expr sel (frame-width)))))
673 (calc-edit-mode (list 'calc-finish-selection-edit
674 num (list 'quote sel) calc-sel-reselect))
676 (calc-show-edit-buffer))
678 (defvar calc-original-buffer)
680 ;; The variable calc-edit-disp-trail is local to calc-edit-finish,
682 (defvar calc-edit-disp-trail)
683 (defvar calc-edit-top)
685 (defun calc-finish-selection-edit (num sel reselect)
686 (let ((buf (current-buffer))
687 (str (buffer-substring calc-edit-top (point-max)))
689 (switch-to-buffer calc-original-buffer)
690 (let ((val (math-read-expr str)))
691 (if (eq (car-safe val) 'error)
693 (switch-to-buffer buf)
694 (goto-char (+ start (nth 1 val)))
695 (error (nth 2 val))))
697 (calc-preserve-point)
698 (if calc-edit-disp-trail
699 (calc-trail-display 1 t))
700 (setq val (calc-encase-atoms (calc-normalize val)))
701 (let ((expr (calc-top num 'full)))
702 (if (calc-find-sub-formula expr sel)
703 (calc-pop-push-record-list 1 "edit"
704 (list (calc-replace-sub-formula
707 (list (and reselect val)))
709 (error "Original selection has been lost")))))))
711 (defun calc-sel-evaluate (arg)
714 (calc-preserve-point)
715 (let* ((num (max 1 (calc-locate-cursor-element (point))))
716 (calc-sel-reselect calc-keep-selection)
717 (entry (calc-top num 'entry))
718 (sel (or (calc-auto-selection entry) (car entry))))
719 (calc-with-default-simplification
720 (let ((math-simplify-only nil))
721 (calc-modify-simplify-mode arg)
722 (let ((val (calc-encase-atoms (calc-normalize sel))))
723 (calc-pop-push-record-list 1 "jsmp"
724 (list (calc-replace-sub-formula
725 (car entry) sel val))
727 (list (and calc-sel-reselect val))))))
728 (calc-handle-whys))))
730 (defun calc-sel-expand-formula (arg)
733 (calc-preserve-point)
734 (let* ((num (max 1 (calc-locate-cursor-element (point))))
735 (calc-sel-reselect calc-keep-selection)
736 (entry (calc-top num 'entry))
737 (sel (or (calc-auto-selection entry) (car entry))))
738 (calc-with-default-simplification
739 (let ((math-simplify-only nil))
740 (calc-modify-simplify-mode arg)
741 (let* ((math-expand-formulas (> arg 0))
742 (val (calc-normalize sel))
745 (setq top (math-expand-formula val))
746 (setq val (calc-normalize top)))
747 (setq val (calc-encase-atoms val))
748 (calc-pop-push-record-list 1 "jexf"
749 (list (calc-replace-sub-formula
750 (car entry) sel val))
752 (list (and calc-sel-reselect val))))))
753 (calc-handle-whys))))
755 (defun calc-sel-mult-both-sides (arg &optional divide)
758 (calc-preserve-point)
759 (let* ((no-simp (consp arg))
760 (num (max 1 (calc-locate-cursor-element (point))))
761 (calc-sel-reselect calc-keep-selection)
762 (entry (calc-top num 'entry))
764 (sel (or (calc-auto-selection entry) expr))
765 (func (car-safe sel))
767 (setq alg (calc-with-default-simplification
768 (car (calc-do-alg-entry ""
770 "Divide both sides by: "
771 "Multiply both sides by: ")
772 nil 'calc-selection-history))))
775 (if (and (or (eq func '/)
776 (assq func calc-tweak-eqn-table))
779 (or (memq func '(/ calcFunc-eq calcFunc-neq))
780 (if (math-known-nonposp alg)
782 (setq func (nth 1 (assq func
783 calc-tweak-eqn-table)))
784 (or (math-known-negp alg)
785 (message "Assuming this factor is nonzero")))
786 (or (math-known-posp alg)
787 (if (math-known-nonnegp alg)
788 (message "Assuming this factor is nonzero")
789 (message "Assuming this factor is positive")))))
790 (setq lhs (list (if divide '/ '*) (nth 1 sel) alg)
791 rhs (list (if divide '/ '*) (nth 2 sel) alg))
794 (setq lhs (math-simplify lhs)
795 rhs (math-simplify rhs))
797 (or (Math-equal (nth 1 sel) 1)
798 (Math-equal (nth 1 sel) -1))
800 (setq rhs (math-expand-term rhs))))))
801 (if (and arg (not no-simp))
802 (setq rhs (math-simplify
803 (calcFunc-expand rhs (unless (= arg 0) arg)))))
804 (setq alg (calc-encase-atoms
805 (calc-normalize (list func lhs rhs)))))
806 (setq rhs (list (if divide '* '/) sel alg))
808 (setq rhs (math-simplify rhs)))
809 (setq alg (calc-encase-atoms
810 (calc-normalize (if divide
812 (list '* alg rhs))))))
813 (calc-pop-push-record-list 1 (if divide "div" "mult")
814 (list (calc-replace-sub-formula
817 (list (and calc-sel-reselect alg)))))
818 (calc-handle-whys))))
820 (defun calc-sel-div-both-sides (no-simp)
822 (calc-sel-mult-both-sides no-simp t))
824 (defun calc-sel-add-both-sides (no-simp &optional subtract)
827 (calc-preserve-point)
828 (let* ((num (max 1 (calc-locate-cursor-element (point))))
829 (calc-sel-reselect calc-keep-selection)
830 (entry (calc-top num 'entry))
832 (sel (or (calc-auto-selection entry) expr))
833 (func (car-safe sel))
835 (setq alg (calc-with-default-simplification
836 (car (calc-do-alg-entry ""
838 "Subtract from both sides: "
839 "Add to both sides: ")
840 nil 'calc-selection-history))))
843 (if (and (assq func calc-tweak-eqn-table)
846 (setq lhs (list (if subtract '- '+) (nth 1 sel) alg)
847 rhs (list (if subtract '- '+) (nth 2 sel) alg))
849 (setq lhs (math-simplify lhs)
850 rhs (math-simplify rhs)))
851 (setq alg (calc-encase-atoms
852 (calc-normalize (list func lhs rhs)))))
853 (setq rhs (list (if subtract '+ '-) sel alg))
855 (setq rhs (math-simplify rhs)))
856 (setq alg (calc-encase-atoms
857 (calc-normalize (list (if subtract '- '+) alg rhs)))))
858 (calc-pop-push-record-list 1 (if subtract "sub" "add")
859 (list (calc-replace-sub-formula
862 (list (and calc-sel-reselect alg)))))
863 (calc-handle-whys))))
865 (defun calc-sel-sub-both-sides (no-simp)
867 (calc-sel-add-both-sides no-simp t))
871 ;;; calc-sel.el ends here