Trailing whitepace deleted.
[emacs.git] / lisp / calc / calcsel2.el
blob25a5b529e6ffd47966959b00b1ca51f9a503239c
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.
26 ;;; Commentary:
28 ;;; Code:
30 ;; This file is autoloaded from calc-ext.el.
31 (require 'calc-ext)
33 (require 'calc-macs)
35 (defun calc-Need-calc-sel-2 () nil)
38 (defun calc-commute-left (arg)
39 (interactive "p")
40 (if (< arg 0)
41 (calc-commute-right (- arg))
42 (calc-wrapper
43 (calc-preserve-point)
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))
49 (expr (car entry))
50 (sel (calc-auto-selection entry))
51 parent new)
52 (or (and sel
53 (consp (setq parent (calc-find-assoc-parent-formula
54 expr sel))))
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
61 parent outer
62 (cond
63 ((memq (car outer)
64 (nth 1 (assq (car-safe (nth 1 outer))
65 calc-assoc-ops)))
66 (let* ((other (nth 2 (nth 1 outer)))
67 (new (calc-build-assoc-term
68 (car (nth 1 outer))
69 (calc-build-assoc-term
70 (car outer)
71 (nth 1 (nth 1 outer))
72 sel)
73 other)))
74 (setq sel (nth 2 (nth 1 new)))
75 new))
76 ((eq (car outer) '-)
77 (calc-build-assoc-term
79 (setq sel (math-neg sel))
80 (nth 1 outer)))
81 ((eq (car outer) '/)
82 (calc-build-assoc-term
84 (setq sel (calcFunc-div 1 sel))
85 (nth 1 outer)))
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))))
92 (setq new nil)
93 (setq new (calc-build-assoc-term
94 (car next)
95 sel
96 (calc-build-assoc-term
97 (car next) (nth 1 next) (nth 2 outer)))
98 sel (nth 1 new)
99 new (calc-replace-sub-formula
100 parent next new))))))
101 (if (eq (nth 1 parent) sel)
102 (setq new nil)
103 (let ((p (nthcdr (1- (calc-find-sub-formula parent sel))
104 (setq new (copy-sequence parent)))))
105 (setcar (cdr p) (car p))
106 (setcar p sel))))
107 (if (null new)
108 (if arg
109 (error "Term is already leftmost")
110 (or reselect
111 (calc-pop-push-list 1 (list expr) num '(nil)))
112 (setq arg 0))
113 (calc-pop-push-record-list
114 1 "left"
115 (list (calc-replace-sub-formula expr parent new))
117 (list (and (or (not (eq arg 0)) reselect)
118 sel))))))))))
120 (defun calc-commute-right (arg)
121 (interactive "p")
122 (if (< arg 0)
123 (calc-commute-left (- arg))
124 (calc-wrapper
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))
131 (expr (car entry))
132 (sel (calc-auto-selection entry))
133 parent new)
134 (or (and sel
135 (consp (setq parent (calc-find-assoc-parent-formula
136 expr sel))))
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
143 parent outer
144 (if (memq (car outer)
145 (nth 2 (assq (car-safe (nth 2 outer))
146 calc-assoc-ops)))
147 (let ((other (nth 1 (nth 2 outer))))
148 (calc-build-assoc-term
149 (car outer)
150 other
151 (calc-build-assoc-term
152 (car (nth 2 outer))
154 (nth 2 (nth 2 outer)))))
155 (let ((new (cond
156 ((eq (car outer) '-)
157 (calc-build-assoc-term
159 (math-neg (nth 2 outer))
160 sel))
161 ((eq (car outer) '/)
162 (calc-build-assoc-term
164 (calcFunc-div 1 (nth 2 outer))
165 sel))
166 (t (calc-build-assoc-term
167 (car outer)
168 (nth 2 outer)
169 sel)))))
170 (setq sel (nth 2 new))
171 new))))
172 (let ((next (calc-find-parent-formula parent outer)))
173 (if (not (and (consp next)
174 (eq outer (nth 1 next))))
175 (setq new nil)
176 (setq new (calc-build-assoc-term
177 (car outer)
178 (calc-build-assoc-term
179 (car next) (nth 1 outer) (nth 2 next))
180 sel)
181 sel (nth 2 new)
182 new (calc-replace-sub-formula
183 parent next new))))))
184 (if (eq (nth (1- (length parent)) parent) sel)
185 (setq new nil)
186 (let ((p (nthcdr (calc-find-sub-formula parent sel)
187 (setq new (copy-sequence parent)))))
188 (setcar p (nth 1 p))
189 (setcar (cdr p) sel))))
190 (if (null new)
191 (if arg
192 (error "Term is already rightmost")
193 (or reselect
194 (calc-pop-push-list 1 (list expr) num '(nil)))
195 (setq arg 0))
196 (calc-pop-push-record-list
197 1 "rght"
198 (list (calc-replace-sub-formula expr parent new))
200 (list (and (or (not (eq arg 0)) reselect)
201 sel))))))))))
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 ()
225 (interactive)
226 (calc-wrapper
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))
231 (expr (car entry))
232 (sel (or (calc-auto-selection entry) expr)))
233 (or (and (not (math-primp sel))
234 (= (length sel) 2))
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 ()
243 (interactive)
244 (calc-slow-wrapper
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))
249 (expr (car entry))
250 (sel (or (calc-auto-selection entry) (error "No selection")))
251 (eqn sel)
252 soln)
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))
257 (or soln
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))
262 soln
263 (list (nth 1 (assq (car soln) calc-tweak-eqn-table))
264 (nth 2 soln)
265 (nth 1 soln)))))
266 (calc-pop-push-record-list 1 "isol"
267 (list (calc-replace-sub-formula
268 expr eqn soln))
270 (list (and reselect sel)))
271 (calc-handle-whys))))
273 (defun calc-sel-commute (many)
274 (interactive "P")
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)
280 (interactive "P")
281 (calc-rewrite-selection "JumpRules" many "jump"))
283 (defun calc-sel-distribute (many)
284 (interactive "P")
285 (calc-rewrite-selection "DistribRules" many "dist"))
287 (defun calc-sel-merge (many)
288 (interactive "P")
289 (calc-rewrite-selection "MergeRules" many "merg"))
291 (defun calc-sel-negate (many)
292 (interactive "P")
293 (calc-rewrite-selection "NegateRules" many "jneg"))
295 (defun calc-sel-invert (many)
296 (interactive "P")
297 (calc-rewrite-selection "InvertRules" many "jinv"))
299 ;;; calcsel2.el ends here