1 ;;; calc-sel.el --- data selection functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
30 ;; This file is autoloaded from calc-ext.el.
35 (defun calc-Need-calc-sel () nil
)
38 ;;; Selection commands.
40 (defvar calc-keep-selection t
)
42 (defun calc-select-here (num &optional once keep
)
45 (calc-prepare-selection)
46 (let ((found (calc-find-selected-part))
47 (entry calc-selection-cache-entry
))
48 (or (and keep
(nth 2 entry
))
51 (setq calc-keep-selection nil
)
52 (message "(Selection will apply to next command only)")))
53 (calc-change-current-selection
55 (if (and num
(> (setq num
(prefix-numeric-value num
)) 0))
57 (while (and (>= (setq num
(1- num
)) 0)
58 (not (eq found
(car entry
))))
59 (setq found
(calc-find-assoc-parent-formula
62 (calc-grow-assoc-formula (car entry
) found
))
65 (defun calc-select-once (num)
67 (calc-select-here num t
))
69 (defun calc-select-here-maybe (num)
71 (calc-select-here num nil t
))
73 (defun calc-select-once-maybe (num)
75 (calc-select-here num t t
))
77 (defun calc-select-additional ()
80 (let (calc-keep-selection)
81 (calc-prepare-selection))
82 (let ((found (calc-find-selected-part))
83 (entry calc-selection-cache-entry
))
84 (calc-change-current-selection
86 (let ((sel (nth 2 entry
)))
89 (while (not (or (eq sel
(car entry
))
90 (calc-find-sub-formula sel found
)))
91 (setq sel
(calc-find-assoc-parent-formula
94 (calc-grow-assoc-formula (car entry
) found
)))
97 (defun calc-select-more (num)
100 (calc-prepare-selection)
101 (let ((entry calc-selection-cache-entry
))
103 (let ((sel (nth 2 entry
)))
104 (while (and (not (eq sel
(car entry
)))
105 (>= (setq num
(1- (prefix-numeric-value num
))) 0))
106 (setq sel
(calc-find-assoc-parent-formula (car entry
) sel
)))
107 (calc-change-current-selection sel
))
108 (calc-select-here num
)))))
110 (defun calc-select-less (num)
113 (calc-prepare-selection)
114 (let ((found (calc-find-selected-part))
115 (entry calc-selection-cache-entry
))
116 (calc-change-current-selection
118 (let ((sel (nth 2 entry
))
122 (>= (setq num
(1- num
)) 0))
124 index
(calc-find-sub-formula sel found
))
125 (and (setq sel
(and index
(nth index old
)))
126 calc-assoc-selections
127 (setq op
(assq (car-safe sel
) calc-assoc-ops
))
128 (memq (car old
) (nth index op
))
129 (setq num
(1+ num
))))
132 (defun calc-select-part (num)
134 (or num
(setq num
(- last-command-char ?
0)))
136 (calc-prepare-selection)
137 (let ((sel (calc-find-nth-part (or (nth 2 calc-selection-cache-entry
)
138 (car calc-selection-cache-entry
))
141 (calc-change-current-selection sel
)
142 (error "%d is not a valid sub-formula index" num
)))))
144 (defun calc-find-nth-part (expr num
)
145 (if (and calc-assoc-selections
146 (assq (car-safe expr
) calc-assoc-ops
))
148 (calc-find-nth-part-rec expr
))
149 (if (eq (car-safe expr
) 'intv
)
150 (and (>= num
1) (<= num
2) (nth (1+ num
) expr
))
151 (and (not (Math-primp expr
)) (>= num
1) (< num
(length expr
))
154 (defun calc-find-nth-part-rec (expr) ; uses num, op
155 (or (if (and (setq op
(assq (car-safe (nth 1 expr
)) calc-assoc-ops
))
156 (memq (car expr
) (nth 1 op
)))
157 (calc-find-nth-part-rec (nth 1 expr
))
158 (and (= (setq num
(1- num
)) 0)
160 (if (and (setq op
(assq (car-safe (nth 2 expr
)) calc-assoc-ops
))
161 (memq (car expr
) (nth 2 op
)))
162 (calc-find-nth-part-rec (nth 2 expr
))
163 (and (= (setq num
(1- num
)) 0)
166 (defun calc-select-next (num)
169 (calc-select-previous (- num
))
171 (calc-prepare-selection)
172 (let* ((entry calc-selection-cache-entry
)
176 (while (>= (setq num
(1- num
)) 0)
177 (let* ((parent (calc-find-parent-formula (car entry
) sel
))
180 (and (eq p t
) (setq p nil
))
181 (while (and (setq p
(cdr p
))
182 (not (eq (car p
) sel
))))
184 (setq sel
(or (and calc-assoc-selections
185 (setq op
(assq (car-safe (nth 1 p
))
187 (memq (car parent
) (nth 2 op
))
190 (if (and calc-assoc-selections
191 (setq op
(assq (car-safe parent
) calc-assoc-ops
))
192 (consp (setq p
(calc-find-parent-formula
193 (car entry
) parent
)))
194 (eq (nth 1 p
) parent
)
195 (memq (car p
) (nth 1 op
)))
197 (error "No \"next\" sub-formula")))))
198 (calc-change-current-selection sel
))
199 (if (Math-primp (car entry
))
200 (calc-change-current-selection (car entry
))
201 (calc-select-part num
)))))))
203 (defun calc-select-previous (num)
206 (calc-select-next (- num
))
208 (calc-prepare-selection)
209 (let* ((entry calc-selection-cache-entry
)
213 (while (>= (setq num
(1- num
)) 0)
214 (let* ((parent (calc-find-parent-formula (car entry
) sel
))
215 (p (cdr-safe parent
))
218 (if (eq (car-safe parent
) 'intv
) (setq p
(cdr p
)))
219 (while (and (not (eq (car p
) sel
))
223 (setq sel
(or (and calc-assoc-selections
224 (setq op
(assq (car-safe prev
)
226 (memq (car parent
) (nth 1 op
))
229 (if (and calc-assoc-selections
230 (setq op
(assq (car-safe parent
) calc-assoc-ops
))
231 (consp (setq p
(calc-find-parent-formula
232 (car entry
) parent
)))
233 (eq (nth 2 p
) parent
)
234 (memq (car p
) (nth 2 op
)))
236 (error "No \"previous\" sub-formula")))))
237 (calc-change-current-selection sel
))
238 (if (Math-primp (car entry
))
239 (calc-change-current-selection (car entry
))
240 (let ((len (if (and calc-assoc-selections
241 (assq (car (car entry
)) calc-assoc-ops
))
243 (calc-find-nth-part-rec (car entry
))
245 (length (car entry
)))))
246 (calc-select-part (- len num
)))))))))
248 (defun calc-find-parent-formula (expr part
)
249 (cond ((eq expr part
) t
)
250 ((Math-primp expr
) nil
)
253 (while (and (setq p
(cdr p
))
254 (not (setq res
(calc-find-parent-formula
257 (if (eq res t
) expr res
))))))
260 (defun calc-find-assoc-parent-formula (expr part
)
261 (calc-grow-assoc-formula expr
(calc-find-parent-formula expr part
)))
263 (defun calc-grow-assoc-formula (expr part
)
264 (if calc-assoc-selections
265 (let ((op (assq (car-safe part
) calc-assoc-ops
)))
268 (while (and (consp (setq new
(calc-find-parent-formula
271 (nth (calc-find-sub-formula new part
) op
)))
276 (defun calc-find-sub-formula (expr part
)
277 (cond ((eq expr part
) t
)
278 ((Math-primp expr
) nil
)
281 (while (and (setq expr
(cdr expr
))
282 (not (calc-find-sub-formula (car expr
) part
)))
286 (defun calc-unselect (num)
289 (calc-prepare-selection num
)
290 (calc-change-current-selection nil
)))
292 (defun calc-clear-selections ()
295 (let ((limit (calc-stack-size))
298 (if (calc-top n
'sel
)
300 (calc-prepare-selection n
)
301 (calc-change-current-selection nil
)))
303 (calc-clear-command-flag 'position-point
)))
305 (defun calc-show-selections (arg)
308 (calc-preserve-point)
309 (setq calc-show-selections
(if arg
310 (> (prefix-numeric-value arg
) 0)
311 (not calc-show-selections
)))
312 (let ((p calc-stack
))
314 (or (null (nth 2 (car p
)))
315 (equal (car p
) calc-selection-cache-entry
)))
318 (let ((calc-selection-cache-default-entry
319 calc-selection-cache-entry
))
321 (and calc-selection-cache-entry
322 (let ((sel (nth 2 calc-selection-cache-entry
)))
323 (setcar (nthcdr 2 calc-selection-cache-entry
) nil
)
324 (calc-change-current-selection sel
)))))
325 (message (if calc-show-selections
326 "Displaying only selected part of formulas"
327 "Displaying all but selected part of formulas"))))
329 (defun calc-preserve-point ()
330 (or (looking-at "\\.\n+\\'")
332 (setq calc-final-point-line
(+ (count-lines (point-min) (point))
334 calc-final-point-column
(current-column))
335 (calc-set-command-flag 'position-point
))))
337 (defun calc-enable-selections (arg)
340 (calc-preserve-point)
341 (setq calc-use-selections
(if arg
342 (> (prefix-numeric-value arg
) 0)
343 (not calc-use-selections
)))
344 (calc-set-command-flag 'renum-stack
)
345 (message (if calc-use-selections
346 "Commands operate only on selected sub-formulas"
347 "Selections of sub-formulas have no effect"))))
349 (defun calc-break-selections (arg)
352 (calc-preserve-point)
353 (setq calc-assoc-selections
(if arg
354 (<= (prefix-numeric-value arg
) 0)
355 (not calc-assoc-selections
)))
356 (message (if calc-assoc-selections
357 "Selection treats a+b+c as a sum of three terms"
358 "Selection treats a+b+c as (a+b)+c"))))
360 (defvar calc-selection-cache-entry nil
)
361 (defun calc-prepare-selection (&optional num
)
362 (or num
(setq num
(calc-locate-cursor-element (point))))
363 (setq calc-selection-true-num num
364 calc-keep-selection t
)
365 (or (> num
0) (setq num
1))
366 ;; (if (or (< num 1) (> num (calc-stack-size)))
367 ;; (error "Cursor must be positioned on a stack element"))
368 (let* ((entry (calc-top num
'entry
))
370 (or (equal entry calc-selection-cache-entry
)
372 (setcar entry
(calc-encase-atoms (car entry
)))
373 (setq calc-selection-cache-entry entry
374 calc-selection-cache-num num
375 calc-selection-cache-comp
376 (let ((math-comp-tagged t
))
377 (math-compose-expr (car entry
) 0))
378 calc-selection-cache-offset
379 (+ (car (math-stack-value-offset calc-selection-cache-comp
))
380 (length calc-left-label
)
381 (if calc-line-numbering
4 0))))))
382 (calc-preserve-point))
384 ;;; The following ensures that no two subformulas will be "eq" to each other!
385 (defun calc-encase-atoms (x)
386 (if (or (not (consp x
))
387 (equal x
'(float 0 0)))
389 (calc-encase-atoms-rec x
)
392 (defun calc-encase-atoms-rec (x)
395 (if (eq (car x
) 'intv
)
397 (while (setq x
(cdr x
))
398 (if (or (not (consp (car x
)))
399 (equal (car x
) '(float 0 0)))
400 (setcar x
(list 'cplx
(car x
) 0))
401 (calc-encase-atoms-rec (car x
)))))))
403 (defun calc-find-selected-part ()
404 (let* ((math-comp-sel-hpos (- (current-column) calc-selection-cache-offset
))
408 (math-comp-sel-vpos (save-excursion
410 (let ((line (point)))
411 (calc-cursor-stack-index
412 calc-selection-cache-num
)
414 (while (< (point) line
)
416 (setq spaces
(+ spaces
417 (current-indentation))
419 (- lcount
(math-comp-ascent
420 calc-selection-cache-comp
) -
1))))
421 (math-comp-sel-cpos (- (point) toppt calc-selection-cache-offset
423 (math-comp-sel-tag nil
))
424 (and (>= math-comp-sel-hpos
0)
425 (> calc-selection-true-num
0)
426 (math-composition-to-string calc-selection-cache-comp
1000000))
427 (nth 1 math-comp-sel-tag
)))
429 (defun calc-change-current-selection (sub-expr)
430 (or (eq sub-expr
(nth 2 calc-selection-cache-entry
))
431 (let ((calc-prepared-composition calc-selection-cache-comp
)
432 (buffer-read-only nil
)
434 (calc-set-command-flag 'renum-stack
)
435 (setcar (nthcdr 2 calc-selection-cache-entry
) sub-expr
)
436 (calc-cursor-stack-index calc-selection-cache-num
)
438 (calc-cursor-stack-index (1- calc-selection-cache-num
))
439 (delete-region top
(point))
440 (let ((calc-selection-cache-default-entry calc-selection-cache-entry
))
441 (insert (math-format-stack-value calc-selection-cache-entry
)
444 (defun calc-top-selected (&optional n m
)
445 (and calc-any-selections
450 (calc-check-stack (+ n m -
1))
451 (let ((top (nthcdr (+ m calc-stack-top -
1) calc-stack
))
453 (while (>= (setq n
(1- n
)) 0)
454 (if (nth 2 (car top
))
455 (setq sel
(if sel t
(nth 2 (car top
)))))
456 (setq top
(cdr top
)))
459 (defun calc-replace-sub-formula (expr old new
)
460 (setq new
(calc-encase-atoms new
))
461 (calc-replace-sub-formula-rec expr
))
463 (defun calc-replace-sub-formula-rec (expr)
464 (cond ((eq expr old
) new
)
465 ((Math-primp expr
) expr
)
468 (mapcar 'calc-replace-sub-formula-rec
(cdr expr
))))))
470 (defun calc-sel-error ()
471 (error "Illegal operation on sub-formulas"))
473 (defun calc-replace-selections (n vals m
)
474 (if (calc-top-selected n m
)
475 (let ((num (length vals
)))
476 (calc-preserve-point)
479 (let* ((old (calc-top-list n m
'entry
))
484 (if (nth 2 (car old
))
485 (setq val
(calc-encase-atoms (car vals
))
486 new
(cons (calc-replace-sub-formula (car (car old
))
491 (setq new
(cons (car vals
) new
)
493 (setq vals
(cdr vals
)
495 (calc-pop-stack n m t
)
496 (calc-push-list (nreverse new
)
497 m
(and calc-keep-selection
(nreverse sel
)))))
499 (let* ((old (calc-top-list n m
'entry
))
501 (while (and old
(not (nth 2 (car old
))))
502 (setq old
(cdr old
)))
504 (while (and (setq more
(cdr more
)) (not (nth 2 (car more
)))))
507 (calc-pop-stack n m t
)
509 (let ((val (calc-encase-atoms (car vals
))))
510 (calc-push-list (list (calc-replace-sub-formula
514 m
(and calc-keep-selection
(list val
))))
515 (calc-push-list vals
))))
516 (t (calc-sel-error))))
517 (calc-pop-stack n m t
)
518 (calc-push-list vals m
)))
520 (defun calc-delete-selection (n)
521 (let ((entry (calc-top n
'entry
)))
523 (if (eq (nth 2 entry
) (car entry
))
525 (calc-pop-stack 1 n t
)
526 (calc-push-list '(0) n
))
527 (let ((parent (calc-find-parent-formula (car entry
) (nth 2 entry
)))
529 (calc-preserve-point)
530 (calc-pop-stack 1 n t
)
531 (cond ((or (memq (car parent
) '(* / %
))
532 (and (eq (car parent
) '^
)
533 (eq (nth 2 parent
) (nth 2 entry
))))
535 ((memq (car parent
) '(vec calcFunc-min calcFunc-max
)))
536 ((and (assq (car parent
) calc-tweak-eqn-table
)
537 (= (length parent
) 3))
543 (calc-push-list (list
545 (calc-replace-sub-formula
548 (if (eq (nth 2 entry
) (nth 1 parent
))
553 (calc-push-list (list
555 (calc-replace-sub-formula (car entry
)
560 (calc-push-list (list
562 (calc-replace-sub-formula (car entry
)
568 (calc-pop-stack 1 n t
))))
570 (defun calc-roll-down-with-selections (n m
)
571 (let ((vals (append (calc-top-list m
1)
572 (calc-top-list (- n m
) (1+ m
))))
573 (sels (append (calc-top-list m
1 'sel
)
574 (calc-top-list (- n m
) (1+ m
) 'sel
))))
575 (calc-pop-push-list n vals
1 sels
)))
577 (defun calc-roll-up-with-selections (n m
)
578 (let ((vals (append (calc-top-list (- n m
) 1)
579 (calc-top-list m
(- n m -
1))))
580 (sels (append (calc-top-list (- n m
) 1 'sel
)
581 (calc-top-list m
(- n m -
1) 'sel
))))
582 (calc-pop-push-list n vals
1 sels
)))
584 (defun calc-auto-selection (entry)
587 (and (boundp 'reselect
) (setq reselect nil
))
588 (calc-prepare-selection)
589 (calc-grow-assoc-formula (car entry
) (calc-find-selected-part)))))
591 (defun calc-copy-selection ()
594 (calc-preserve-point)
595 (let* ((num (max 1 (calc-locate-cursor-element (point))))
596 (entry (calc-top num
'entry
)))
597 (calc-push (or (calc-auto-selection entry
) (car entry
))))))
599 (defun calc-del-selection ()
602 (calc-preserve-point)
603 (let* ((num (max 1 (calc-locate-cursor-element (point))))
604 (entry (calc-top num
'entry
))
605 (sel (calc-auto-selection entry
)))
606 (setcar (nthcdr 2 entry
) (and (not (eq sel
(car entry
))) sel
))
607 (calc-delete-selection num
))))
609 (defun calc-enter-selection ()
612 (calc-preserve-point)
613 (let* ((num (max 1 (calc-locate-cursor-element (point))))
614 (reselect calc-keep-selection
)
615 (entry (calc-top num
'entry
))
617 (sel (or (calc-auto-selection entry
) expr
))
619 (let ((calc-dollar-values (list sel
))
620 (calc-dollar-used 0))
621 (setq alg
(calc-do-alg-entry "" "Replace selection with: "))
624 (setq alg
(calc-encase-atoms (car alg
)))
625 (calc-pop-push-record-list 1 "repl"
626 (list (calc-replace-sub-formula
629 (list (and reselect alg
))))))
630 (calc-handle-whys))))
632 (defun calc-edit-selection ()
635 (calc-preserve-point)
636 (let* ((num (max 1 (calc-locate-cursor-element (point))))
637 (reselect calc-keep-selection
)
638 (entry (calc-top num
'entry
))
640 (sel (or (calc-auto-selection entry
) expr
))
642 (let ((str (math-showing-full-precision
643 (math-format-nice-expr sel
(frame-width)))))
644 (calc-edit-mode (list 'calc-finish-selection-edit
645 num
(list 'quote sel
) reselect
))
647 (calc-show-edit-buffer))
649 (defun calc-finish-selection-edit (num sel reselect
)
650 (let ((buf (current-buffer))
651 (str (buffer-substring (point) (point-max)))
653 (switch-to-buffer calc-original-buffer
)
654 (let ((val (math-read-expr str
)))
655 (if (eq (car-safe val
) 'error
)
657 (switch-to-buffer buf
)
658 (goto-char (+ start
(nth 1 val
)))
659 (error (nth 2 val
))))
661 (calc-preserve-point)
663 (calc-trail-display 1 t
))
664 (setq val
(calc-encase-atoms (calc-normalize val
)))
665 (let ((expr (calc-top num
'full
)))
666 (if (calc-find-sub-formula expr sel
)
667 (calc-pop-push-record-list 1 "edit"
668 (list (calc-replace-sub-formula
671 (list (and reselect val
)))
673 (error "Original selection has been lost")))))))
675 (defun calc-sel-evaluate (arg)
678 (calc-preserve-point)
679 (let* ((num (max 1 (calc-locate-cursor-element (point))))
680 (reselect calc-keep-selection
)
681 (entry (calc-top num
'entry
))
682 (sel (or (calc-auto-selection entry
) (car entry
))))
683 (calc-with-default-simplification
684 (let ((math-simplify-only nil
))
685 (calc-modify-simplify-mode arg
)
686 (let ((val (calc-encase-atoms (calc-normalize sel
))))
687 (calc-pop-push-record-list 1 "jsmp"
688 (list (calc-replace-sub-formula
689 (car entry
) sel val
))
691 (list (and reselect val
))))))
692 (calc-handle-whys))))
694 (defun calc-sel-expand-formula (arg)
697 (calc-preserve-point)
698 (let* ((num (max 1 (calc-locate-cursor-element (point))))
699 (reselect calc-keep-selection
)
700 (entry (calc-top num
'entry
))
701 (sel (or (calc-auto-selection entry
) (car entry
))))
702 (calc-with-default-simplification
703 (let ((math-simplify-only nil
))
704 (calc-modify-simplify-mode arg
)
705 (let* ((math-expand-formulas (> arg
0))
706 (val (calc-normalize sel
))
709 (setq top
(math-expand-formula val
))
710 (setq val
(calc-normalize top
)))
711 (setq val
(calc-encase-atoms val
))
712 (calc-pop-push-record-list 1 "jexf"
713 (list (calc-replace-sub-formula
714 (car entry
) sel val
))
716 (list (and reselect val
))))))
717 (calc-handle-whys))))
719 (defun calc-sel-mult-both-sides (no-simp &optional divide
)
722 (calc-preserve-point)
723 (let* ((num (max 1 (calc-locate-cursor-element (point))))
724 (reselect calc-keep-selection
)
725 (entry (calc-top num
'entry
))
727 (sel (or (calc-auto-selection entry
) expr
))
728 (func (car-safe sel
))
730 (setq alg
(calc-with-default-simplification
731 (car (calc-do-alg-entry ""
733 "Divide both sides by: "
734 "Multiply both sides by: ")))))
737 (if (and (or (eq func
'/)
738 (assq func calc-tweak-eqn-table
))
741 (or (memq func
'(/ calcFunc-eq calcFunc-neq
))
742 (if (math-known-nonposp alg
)
744 (setq func
(nth 1 (assq func
745 calc-tweak-eqn-table
)))
746 (or (math-known-negp alg
)
747 (message "Assuming this factor is nonzero")))
748 (or (math-known-posp alg
)
749 (if (math-known-nonnegp alg
)
750 (message "Assuming this factor is nonzero")
751 (message "Assuming this factor is positive")))))
752 (setq lhs
(list (if divide
'/ '*) (nth 1 sel
) alg
)
753 rhs
(list (if divide
'/ '*) (nth 2 sel
) alg
))
756 (setq lhs
(math-simplify lhs
)
757 rhs
(math-simplify rhs
))
759 (or (Math-equal (nth 1 sel
) 1)
760 (Math-equal (nth 1 sel
) -
1)
761 (and (memq (car-safe (nth 2 sel
)) '(+ -
))
762 (memq (car-safe alg
) '(+ -
))))
763 (setq rhs
(math-expand-term rhs
)))))
764 (setq alg
(calc-encase-atoms
765 (calc-normalize (list func lhs rhs
)))))
766 (setq rhs
(list (if divide
'* '/) sel alg
))
768 (setq rhs
(math-simplify rhs
)))
769 (setq alg
(calc-encase-atoms
770 (calc-normalize (if divide
772 (list '* alg rhs
))))))
773 (calc-pop-push-record-list 1 (if divide
"div" "mult")
774 (list (calc-replace-sub-formula
777 (list (and reselect alg
)))))
778 (calc-handle-whys))))
780 (defun calc-sel-div-both-sides (no-simp)
782 (calc-sel-mult-both-sides no-simp t
))
784 (defun calc-sel-add-both-sides (no-simp &optional subtract
)
787 (calc-preserve-point)
788 (let* ((num (max 1 (calc-locate-cursor-element (point))))
789 (reselect calc-keep-selection
)
790 (entry (calc-top num
'entry
))
792 (sel (or (calc-auto-selection entry
) expr
))
793 (func (car-safe sel
))
795 (setq alg
(calc-with-default-simplification
796 (car (calc-do-alg-entry ""
798 "Subtract from both sides: "
799 "Add to both sides: ")))))
802 (if (and (assq func calc-tweak-eqn-table
)
805 (setq lhs
(list (if subtract
'-
'+) (nth 1 sel
) alg
)
806 rhs
(list (if subtract
'-
'+) (nth 2 sel
) alg
))
808 (setq lhs
(math-simplify lhs
)
809 rhs
(math-simplify rhs
)))
810 (setq alg
(calc-encase-atoms
811 (calc-normalize (list func lhs rhs
)))))
812 (setq rhs
(list (if subtract
'+ '-
) sel alg
))
814 (setq rhs
(math-simplify rhs
)))
815 (setq alg
(calc-encase-atoms
816 (calc-normalize (list (if subtract
'-
'+) alg rhs
)))))
817 (calc-pop-push-record-list 1 (if subtract
"sub" "add")
818 (list (calc-replace-sub-formula
821 (list (and reselect alg
)))))
822 (calc-handle-whys))))
824 (defun calc-sel-sub-both-sides (no-simp)
826 (calc-sel-add-both-sides no-simp t
))
828 ;;; arch-tag: e5169792-777d-428f-bff5-acca66813fa2
829 ;;; calc-sel.el ends here