(math-standard-units): Redefine Watt hour as W*hr.
[bpt/emacs.git] / lisp / calc / calcsel2.el
1 ;;; calcsel2.el --- selection functions for Calc
2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <belanger@truman.edu>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
16
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 ;; This file is autoloaded from calc-ext.el.
30
31 (require 'calc-ext)
32 (require 'calc-macs)
33
34 ;; The variable calc-keep-selection is declared and set in calc-sel.el.
35 (defvar calc-keep-selection)
36
37 (defun calc-commute-left (arg)
38 (interactive "p")
39 (if (< arg 0)
40 (calc-commute-right (- arg))
41 (calc-wrapper
42 (calc-preserve-point)
43 (let ((num (max 1 (calc-locate-cursor-element (point))))
44 (reselect calc-keep-selection))
45 (if (= arg 0) (setq arg nil))
46 (while (or (null arg) (>= (setq arg (1- arg)) 0))
47 (let* ((entry (calc-top num 'entry))
48 (expr (car entry))
49 (sel (calc-auto-selection entry))
50 parent new)
51 (or (and sel
52 (consp (setq parent (calc-find-assoc-parent-formula
53 expr sel))))
54 (error "No term is selected"))
55 (if (and calc-assoc-selections
56 (assq (car parent) calc-assoc-ops))
57 (let ((outer (calc-find-parent-formula parent sel)))
58 (if (eq sel (nth 2 outer))
59 (setq new (calc-replace-sub-formula
60 parent outer
61 (cond
62 ((memq (car outer)
63 (nth 1 (assq (car-safe (nth 1 outer))
64 calc-assoc-ops)))
65 (let* ((other (nth 2 (nth 1 outer)))
66 (new (calc-build-assoc-term
67 (car (nth 1 outer))
68 (calc-build-assoc-term
69 (car outer)
70 (nth 1 (nth 1 outer))
71 sel)
72 other)))
73 (setq sel (nth 2 (nth 1 new)))
74 new))
75 ((eq (car outer) '-)
76 (calc-build-assoc-term
77 '+
78 (setq sel (math-neg sel))
79 (nth 1 outer)))
80 ((eq (car outer) '/)
81 (calc-build-assoc-term
82 '*
83 (setq sel (calcFunc-div 1 sel))
84 (nth 1 outer)))
85 (t (calc-build-assoc-term
86 (car outer) sel (nth 1 outer))))))
87 (let ((next (calc-find-parent-formula parent outer)))
88 (if (not (and (consp next)
89 (eq outer (nth 2 next))
90 (eq (car next) (car outer))))
91 (setq new nil)
92 (setq new (calc-build-assoc-term
93 (car next)
94 sel
95 (calc-build-assoc-term
96 (car next) (nth 1 next) (nth 2 outer)))
97 sel (nth 1 new)
98 new (calc-replace-sub-formula
99 parent next new))))))
100 (if (eq (nth 1 parent) sel)
101 (setq new nil)
102 (let ((p (nthcdr (1- (calc-find-sub-formula parent sel))
103 (setq new (copy-sequence parent)))))
104 (setcar (cdr p) (car p))
105 (setcar p sel))))
106 (if (null new)
107 (if arg
108 (error "Term is already leftmost")
109 (or reselect
110 (calc-pop-push-list 1 (list expr) num '(nil)))
111 (setq arg 0))
112 (calc-pop-push-record-list
113 1 "left"
114 (list (calc-replace-sub-formula expr parent new))
115 num
116 (list (and (or (not (eq arg 0)) reselect)
117 sel))))))))))
118
119 (defun calc-commute-right (arg)
120 (interactive "p")
121 (if (< arg 0)
122 (calc-commute-left (- arg))
123 (calc-wrapper
124 (calc-preserve-point)
125 (let ((num (max 1 (calc-locate-cursor-element (point))))
126 (reselect calc-keep-selection))
127 (if (= arg 0) (setq arg nil))
128 (while (or (null arg) (>= (setq arg (1- arg)) 0))
129 (let* ((entry (calc-top num 'entry))
130 (expr (car entry))
131 (sel (calc-auto-selection entry))
132 parent new)
133 (or (and sel
134 (consp (setq parent (calc-find-assoc-parent-formula
135 expr sel))))
136 (error "No term is selected"))
137 (if (and calc-assoc-selections
138 (assq (car parent) calc-assoc-ops))
139 (let ((outer (calc-find-parent-formula parent sel)))
140 (if (eq sel (nth 1 outer))
141 (setq new (calc-replace-sub-formula
142 parent outer
143 (if (memq (car outer)
144 (nth 2 (assq (car-safe (nth 2 outer))
145 calc-assoc-ops)))
146 (let ((other (nth 1 (nth 2 outer))))
147 (calc-build-assoc-term
148 (car outer)
149 other
150 (calc-build-assoc-term
151 (car (nth 2 outer))
152 sel
153 (nth 2 (nth 2 outer)))))
154 (let ((new (cond
155 ((eq (car outer) '-)
156 (calc-build-assoc-term
157 '+
158 (math-neg (nth 2 outer))
159 sel))
160 ((eq (car outer) '/)
161 (calc-build-assoc-term
162 '*
163 (calcFunc-div 1 (nth 2 outer))
164 sel))
165 (t (calc-build-assoc-term
166 (car outer)
167 (nth 2 outer)
168 sel)))))
169 (setq sel (nth 2 new))
170 new))))
171 (let ((next (calc-find-parent-formula parent outer)))
172 (if (not (and (consp next)
173 (eq outer (nth 1 next))))
174 (setq new nil)
175 (setq new (calc-build-assoc-term
176 (car outer)
177 (calc-build-assoc-term
178 (car next) (nth 1 outer) (nth 2 next))
179 sel)
180 sel (nth 2 new)
181 new (calc-replace-sub-formula
182 parent next new))))))
183 (if (eq (nth (1- (length parent)) parent) sel)
184 (setq new nil)
185 (let ((p (nthcdr (calc-find-sub-formula parent sel)
186 (setq new (copy-sequence parent)))))
187 (setcar p (nth 1 p))
188 (setcar (cdr p) sel))))
189 (if (null new)
190 (if arg
191 (error "Term is already rightmost")
192 (or reselect
193 (calc-pop-push-list 1 (list expr) num '(nil)))
194 (setq arg 0))
195 (calc-pop-push-record-list
196 1 "rght"
197 (list (calc-replace-sub-formula expr parent new))
198 num
199 (list (and (or (not (eq arg 0)) reselect)
200 sel))))))))))
201
202 (defun calc-build-assoc-term (op lhs rhs)
203 (cond ((and (eq op '+) (or (math-looks-negp rhs)
204 (and (eq (car-safe rhs) 'cplx)
205 (math-negp (nth 1 rhs))
206 (eq (nth 2 rhs) 0))))
207 (list '- lhs (math-neg rhs)))
208 ((and (eq op '-) (or (math-looks-negp rhs)
209 (and (eq (car-safe rhs) 'cplx)
210 (math-negp (nth 1 rhs))
211 (eq (nth 2 rhs) 0))))
212 (list '+ lhs (math-neg rhs)))
213 ((and (eq op '*) (and (eq (car-safe rhs) '/)
214 (or (math-equal-int (nth 1 rhs) 1)
215 (equal (nth 1 rhs) '(cplx 1 0)))))
216 (list '/ lhs (nth 2 rhs)))
217 ((and (eq op '/) (and (eq (car-safe rhs) '/)
218 (or (math-equal-int (nth 1 rhs) 1)
219 (equal (nth 1 rhs) '(cplx 1 0)))))
220 (list '/ lhs (nth 2 rhs)))
221 (t (list op lhs rhs))))
222
223 (defun calc-sel-unpack ()
224 (interactive)
225 (calc-wrapper
226 (calc-preserve-point)
227 (let* ((num (max 1 (calc-locate-cursor-element (point))))
228 (reselect calc-keep-selection)
229 (entry (calc-top num 'entry))
230 (expr (car entry))
231 (sel (or (calc-auto-selection entry) expr)))
232 (or (and (not (math-primp sel))
233 (= (length sel) 2))
234 (error "Selection must be a function of one argument"))
235 (calc-pop-push-record-list 1 "unpk"
236 (list (calc-replace-sub-formula
237 expr sel (nth 1 sel)))
238 num
239 (list (and reselect (nth 1 sel)))))))
240
241 (defun calc-sel-isolate ()
242 (interactive)
243 (calc-slow-wrapper
244 (calc-preserve-point)
245 (let* ((num (max 1 (calc-locate-cursor-element (point))))
246 (reselect calc-keep-selection)
247 (entry (calc-top num 'entry))
248 (expr (car entry))
249 (sel (or (calc-auto-selection entry) (error "No selection")))
250 (eqn sel)
251 soln)
252 (while (and (or (consp (setq eqn (calc-find-parent-formula expr eqn)))
253 (error "Selection must be a member of an equation"))
254 (not (assq (car eqn) calc-tweak-eqn-table))))
255 (setq soln (math-solve-eqn eqn sel calc-hyperbolic-flag))
256 (or soln
257 (error "No solution found"))
258 (setq soln (calc-encase-atoms
259 (if (eq (not (calc-find-sub-formula (nth 2 eqn) sel))
260 (eq (nth 1 soln) sel))
261 soln
262 (list (nth 1 (assq (car soln) calc-tweak-eqn-table))
263 (nth 2 soln)
264 (nth 1 soln)))))
265 (calc-pop-push-record-list 1 "isol"
266 (list (calc-replace-sub-formula
267 expr eqn soln))
268 num
269 (list (and reselect sel)))
270 (calc-handle-whys))))
271
272 (defun calc-sel-commute (many)
273 (interactive "P")
274 (let ((calc-assoc-selections nil))
275 (calc-rewrite-selection "CommuteRules" many "cmut"))
276 (calc-set-mode-line))
277
278 (defun calc-sel-jump-equals (many)
279 (interactive "P")
280 (calc-rewrite-selection "JumpRules" many "jump"))
281
282 (defun calc-sel-distribute (many)
283 (interactive "P")
284 (calc-rewrite-selection "DistribRules" many "dist"))
285
286 (defun calc-sel-merge (many)
287 (interactive "P")
288 (calc-rewrite-selection "MergeRules" many "merg"))
289
290 (defun calc-sel-negate (many)
291 (interactive "P")
292 (calc-rewrite-selection "NegateRules" many "jneg"))
293
294 (defun calc-sel-invert (many)
295 (interactive "P")
296 (calc-rewrite-selection "InvertRules" many "jinv"))
297
298 (provide 'calcsel2)
299
300 ;;; arch-tag: 7c5b8d65-b8f0-45d9-820d-9930f8ee114b
301 ;;; calcsel2.el ends here