1 ;;; calcsel2.el --- 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-2 () nil)
38 (defun calc-commute-left (arg)
41 (calc-commute-right (- arg))
44 (let ((num (max 1 (calc-locate-cursor-element (point))))
45 (reselect calc-keep-selection))
46 (if (= arg 0) (setq arg nil))
47 (while (or (null arg) (>= (setq arg (1- arg)) 0))
48 (let* ((entry (calc-top num 'entry))
50 (sel (calc-auto-selection entry))
53 (consp (setq parent (calc-find-assoc-parent-formula
55 (error "No term is selected"))
56 (if (and calc-assoc-selections
57 (assq (car parent) calc-assoc-ops))
58 (let ((outer (calc-find-parent-formula parent sel)))
59 (if (eq sel (nth 2 outer))
60 (setq new (calc-replace-sub-formula
64 (nth 1 (assq (car-safe (nth 1 outer))
66 (let* ((other (nth 2 (nth 1 outer)))
67 (new (calc-build-assoc-term
69 (calc-build-assoc-term
74 (setq sel (nth 2 (nth 1 new)))
77 (calc-build-assoc-term
79 (setq sel (math-neg sel))
82 (calc-build-assoc-term
84 (setq sel (calcFunc-div 1 sel))
86 (t (calc-build-assoc-term
87 (car outer) sel (nth 1 outer))))))
88 (let ((next (calc-find-parent-formula parent outer)))
89 (if (not (and (consp next)
90 (eq outer (nth 2 next))
91 (eq (car next) (car outer))))
93 (setq new (calc-build-assoc-term
96 (calc-build-assoc-term
97 (car next) (nth 1 next) (nth 2 outer)))
99 new (calc-replace-sub-formula
100 parent next new))))))
101 (if (eq (nth 1 parent) sel)
103 (let ((p (nthcdr (1- (calc-find-sub-formula parent sel))
104 (setq new (copy-sequence parent)))))
105 (setcar (cdr p) (car p))
109 (error "Term is already leftmost")
111 (calc-pop-push-list 1 (list expr) num '(nil)))
113 (calc-pop-push-record-list
115 (list (calc-replace-sub-formula expr parent new))
117 (list (and (or (not (eq arg 0)) reselect)
120 (defun calc-commute-right (arg)
123 (calc-commute-left (- arg))
125 (calc-preserve-point)
126 (let ((num (max 1 (calc-locate-cursor-element (point))))
127 (reselect calc-keep-selection))
128 (if (= arg 0) (setq arg nil))
129 (while (or (null arg) (>= (setq arg (1- arg)) 0))
130 (let* ((entry (calc-top num 'entry))
132 (sel (calc-auto-selection entry))
135 (consp (setq parent (calc-find-assoc-parent-formula
137 (error "No term is selected"))
138 (if (and calc-assoc-selections
139 (assq (car parent) calc-assoc-ops))
140 (let ((outer (calc-find-parent-formula parent sel)))
141 (if (eq sel (nth 1 outer))
142 (setq new (calc-replace-sub-formula
144 (if (memq (car outer)
145 (nth 2 (assq (car-safe (nth 2 outer))
147 (let ((other (nth 1 (nth 2 outer))))
148 (calc-build-assoc-term
151 (calc-build-assoc-term
154 (nth 2 (nth 2 outer)))))
157 (calc-build-assoc-term
159 (math-neg (nth 2 outer))
162 (calc-build-assoc-term
164 (calcFunc-div 1 (nth 2 outer))
166 (t (calc-build-assoc-term
170 (setq sel (nth 2 new))
172 (let ((next (calc-find-parent-formula parent outer)))
173 (if (not (and (consp next)
174 (eq outer (nth 1 next))))
176 (setq new (calc-build-assoc-term
178 (calc-build-assoc-term
179 (car next) (nth 1 outer) (nth 2 next))
182 new (calc-replace-sub-formula
183 parent next new))))))
184 (if (eq (nth (1- (length parent)) parent) sel)
186 (let ((p (nthcdr (calc-find-sub-formula parent sel)
187 (setq new (copy-sequence parent)))))
189 (setcar (cdr p) sel))))
192 (error "Term is already rightmost")
194 (calc-pop-push-list 1 (list expr) num '(nil)))
196 (calc-pop-push-record-list
198 (list (calc-replace-sub-formula expr parent new))
200 (list (and (or (not (eq arg 0)) reselect)
203 (defun calc-build-assoc-term (op lhs rhs)
204 (cond ((and (eq op '+) (or (math-looks-negp rhs)
205 (and (eq (car-safe rhs) 'cplx)
206 (math-negp (nth 1 rhs))
207 (eq (nth 2 rhs) 0))))
208 (list '- lhs (math-neg rhs)))
209 ((and (eq op '-) (or (math-looks-negp rhs)
210 (and (eq (car-safe rhs) 'cplx)
211 (math-negp (nth 1 rhs))
212 (eq (nth 2 rhs) 0))))
213 (list '+ lhs (math-neg rhs)))
214 ((and (eq op '*) (and (eq (car-safe rhs) '/)
215 (or (math-equal-int (nth 1 rhs) 1)
216 (equal (nth 1 rhs) '(cplx 1 0)))))
217 (list '/ lhs (nth 2 rhs)))
218 ((and (eq op '/) (and (eq (car-safe rhs) '/)
219 (or (math-equal-int (nth 1 rhs) 1)
220 (equal (nth 1 rhs) '(cplx 1 0)))))
221 (list '/ lhs (nth 2 rhs)))
222 (t (list op lhs rhs))))
224 (defun calc-sel-unpack ()
227 (calc-preserve-point)
228 (let* ((num (max 1 (calc-locate-cursor-element (point))))
229 (reselect calc-keep-selection)
230 (entry (calc-top num 'entry))
232 (sel (or (calc-auto-selection entry) expr)))
233 (or (and (not (math-primp sel))
235 (error "Selection must be a function of one argument"))
236 (calc-pop-push-record-list 1 "unpk"
237 (list (calc-replace-sub-formula
238 expr sel (nth 1 sel)))
240 (list (and reselect (nth 1 sel)))))))
242 (defun calc-sel-isolate ()
245 (calc-preserve-point)
246 (let* ((num (max 1 (calc-locate-cursor-element (point))))
247 (reselect calc-keep-selection)
248 (entry (calc-top num 'entry))
250 (sel (or (calc-auto-selection entry) (error "No selection")))
253 (while (and (or (consp (setq eqn (calc-find-parent-formula expr eqn)))
254 (error "Selection must be a member of an equation"))
255 (not (assq (car eqn) calc-tweak-eqn-table))))
256 (setq soln (math-solve-eqn eqn sel calc-hyperbolic-flag))
258 (error "No solution found"))
259 (setq soln (calc-encase-atoms
260 (if (eq (not (calc-find-sub-formula (nth 2 eqn) sel))
261 (eq (nth 1 soln) sel))
263 (list (nth 1 (assq (car soln) calc-tweak-eqn-table))
266 (calc-pop-push-record-list 1 "isol"
267 (list (calc-replace-sub-formula
270 (list (and reselect sel)))
271 (calc-handle-whys))))
273 (defun calc-sel-commute (many)
275 (let ((calc-assoc-selections nil))
276 (calc-rewrite-selection "CommuteRules" many "cmut"))
277 (calc-set-mode-line))
279 (defun calc-sel-jump-equals (many)
281 (calc-rewrite-selection "JumpRules" many "jump"))
283 (defun calc-sel-distribute (many)
285 (calc-rewrite-selection "DistribRules" many "dist"))
287 (defun calc-sel-merge (many)
289 (calc-rewrite-selection "MergeRules" many "merg"))
291 (defun calc-sel-negate (many)
293 (calc-rewrite-selection "NegateRules" many "jneg"))
295 (defun calc-sel-invert (many)
297 (calc-rewrite-selection "InvertRules" many "jinv"))
299 ;;; calcsel2.el ends here