Mercurial > emacs
annotate lisp/calc/calcsel2.el @ 41266:c08a55ae8e5d
(calc-wrapper, calc-slow-wrapper)
(math-showing-full-precision, math-with-extra-prec, math-working)
(calc-with-default-simplification)
(calc-with-trail-buffer): Use backtick.
(Math-zerop, Math-integer-negp, Math-integer-posp, Math-negp)
(Math-looks-negp, Math-posp, Math-integerp, Math-natnump)
(Math-ratp, Math-realp, Math-anglep, Math-numberp, Math-scalarp)
(Math-vectorp, Math-messy-integerp, Math-objectp, Math-objvecp)
(Math-integer-neg, Math-equal, Math-lessp, Math-primp)
(Math-num-integerp, Math-bignum-test, Math-equal-int)
(Math-natnum-lessp, math-format-radix-digit): Change to `defsubst'.
(calc-record-compilation-date-macro): Deleted. Callers updated.
(math-format-radix-digit): Move to calc-bin.el.
Change all toplevel `setq' forms to `defvar' forms, and move them
before their first use. Use `when', `unless'. Remove trailing
periods from error forms. Add description and headers suggested by
Emacs Lisp coding conventions.
author | Colin Walters <walters@gnu.org> |
---|---|
date | Mon, 19 Nov 2001 07:35:49 +0000 |
parents | 73f364fd8aaa |
children | fcd507927105 |
rev | line source |
---|---|
40785 | 1 ;; Calculator for GNU Emacs, part II [calc-sel-2.el] |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
40785 | 3 ;; Written by Dave Gillespie, daveg@synaptics.com. |
4 | |
5 ;; This file is part of GNU Emacs. | |
6 | |
7 ;; GNU Emacs is distributed in the hope that it will be useful, | |
8 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
9 ;; accepts responsibility to anyone for the consequences of using it | |
10 ;; or for whether it serves any particular purpose or works at all, | |
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
12 ;; License for full details. | |
13 | |
14 ;; Everyone is granted permission to copy, modify and redistribute | |
15 ;; GNU Emacs, but only under the conditions described in the | |
16 ;; GNU Emacs General Public License. A copy of this license is | |
17 ;; supposed to have been given to you along with GNU Emacs so you | |
18 ;; can know your rights and responsibilities. It should be in a | |
19 ;; file named COPYING. Among other things, the copyright notice | |
20 ;; and this notice must be preserved on all copies. | |
21 | |
22 | |
23 | |
24 ;; This file is autoloaded from calc-ext.el. | |
25 (require 'calc-ext) | |
26 | |
27 (require 'calc-macs) | |
28 | |
29 (defun calc-Need-calc-sel-2 () nil) | |
30 | |
31 | |
32 (defun calc-commute-left (arg) | |
33 (interactive "p") | |
34 (if (< arg 0) | |
35 (calc-commute-right (- arg)) | |
36 (calc-wrapper | |
37 (calc-preserve-point) | |
38 (let ((num (max 1 (calc-locate-cursor-element (point)))) | |
39 (reselect calc-keep-selection)) | |
40 (if (= arg 0) (setq arg nil)) | |
41 (while (or (null arg) (>= (setq arg (1- arg)) 0)) | |
42 (let* ((entry (calc-top num 'entry)) | |
43 (expr (car entry)) | |
44 (sel (calc-auto-selection entry)) | |
45 parent new) | |
46 (or (and sel | |
47 (consp (setq parent (calc-find-assoc-parent-formula | |
48 expr sel)))) | |
49 (error "No term is selected")) | |
50 (if (and calc-assoc-selections | |
51 (assq (car parent) calc-assoc-ops)) | |
52 (let ((outer (calc-find-parent-formula parent sel))) | |
53 (if (eq sel (nth 2 outer)) | |
54 (setq new (calc-replace-sub-formula | |
55 parent outer | |
56 (cond | |
57 ((memq (car outer) | |
58 (nth 1 (assq (car-safe (nth 1 outer)) | |
59 calc-assoc-ops))) | |
60 (let* ((other (nth 2 (nth 1 outer))) | |
61 (new (calc-build-assoc-term | |
62 (car (nth 1 outer)) | |
63 (calc-build-assoc-term | |
64 (car outer) | |
65 (nth 1 (nth 1 outer)) | |
66 sel) | |
67 other))) | |
68 (setq sel (nth 2 (nth 1 new))) | |
69 new)) | |
70 ((eq (car outer) '-) | |
71 (calc-build-assoc-term | |
72 '+ | |
73 (setq sel (math-neg sel)) | |
74 (nth 1 outer))) | |
75 ((eq (car outer) '/) | |
76 (calc-build-assoc-term | |
77 '* | |
78 (setq sel (calcFunc-div 1 sel)) | |
79 (nth 1 outer))) | |
80 (t (calc-build-assoc-term | |
81 (car outer) sel (nth 1 outer)))))) | |
82 (let ((next (calc-find-parent-formula parent outer))) | |
83 (if (not (and (consp next) | |
84 (eq outer (nth 2 next)) | |
85 (eq (car next) (car outer)))) | |
86 (setq new nil) | |
87 (setq new (calc-build-assoc-term | |
88 (car next) | |
89 sel | |
90 (calc-build-assoc-term | |
91 (car next) (nth 1 next) (nth 2 outer))) | |
92 sel (nth 1 new) | |
93 new (calc-replace-sub-formula | |
94 parent next new)))))) | |
95 (if (eq (nth 1 parent) sel) | |
96 (setq new nil) | |
97 (let ((p (nthcdr (1- (calc-find-sub-formula parent sel)) | |
98 (setq new (copy-sequence parent))))) | |
99 (setcar (cdr p) (car p)) | |
100 (setcar p sel)))) | |
101 (if (null new) | |
102 (if arg | |
103 (error "Term is already leftmost") | |
104 (or reselect | |
105 (calc-pop-push-list 1 (list expr) num '(nil))) | |
106 (setq arg 0)) | |
107 (calc-pop-push-record-list | |
108 1 "left" | |
109 (list (calc-replace-sub-formula expr parent new)) | |
110 num | |
111 (list (and (or (not (eq arg 0)) reselect) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
112 sel)))))))))) |
40785 | 113 |
114 (defun calc-commute-right (arg) | |
115 (interactive "p") | |
116 (if (< arg 0) | |
117 (calc-commute-left (- arg)) | |
118 (calc-wrapper | |
119 (calc-preserve-point) | |
120 (let ((num (max 1 (calc-locate-cursor-element (point)))) | |
121 (reselect calc-keep-selection)) | |
122 (if (= arg 0) (setq arg nil)) | |
123 (while (or (null arg) (>= (setq arg (1- arg)) 0)) | |
124 (let* ((entry (calc-top num 'entry)) | |
125 (expr (car entry)) | |
126 (sel (calc-auto-selection entry)) | |
127 parent new) | |
128 (or (and sel | |
129 (consp (setq parent (calc-find-assoc-parent-formula | |
130 expr sel)))) | |
131 (error "No term is selected")) | |
132 (if (and calc-assoc-selections | |
133 (assq (car parent) calc-assoc-ops)) | |
134 (let ((outer (calc-find-parent-formula parent sel))) | |
135 (if (eq sel (nth 1 outer)) | |
136 (setq new (calc-replace-sub-formula | |
137 parent outer | |
138 (if (memq (car outer) | |
139 (nth 2 (assq (car-safe (nth 2 outer)) | |
140 calc-assoc-ops))) | |
141 (let ((other (nth 1 (nth 2 outer)))) | |
142 (calc-build-assoc-term | |
143 (car outer) | |
144 other | |
145 (calc-build-assoc-term | |
146 (car (nth 2 outer)) | |
147 sel | |
148 (nth 2 (nth 2 outer))))) | |
149 (let ((new (cond | |
150 ((eq (car outer) '-) | |
151 (calc-build-assoc-term | |
152 '+ | |
153 (math-neg (nth 2 outer)) | |
154 sel)) | |
155 ((eq (car outer) '/) | |
156 (calc-build-assoc-term | |
157 '* | |
158 (calcFunc-div 1 (nth 2 outer)) | |
159 sel)) | |
160 (t (calc-build-assoc-term | |
161 (car outer) | |
162 (nth 2 outer) | |
163 sel))))) | |
164 (setq sel (nth 2 new)) | |
165 new)))) | |
166 (let ((next (calc-find-parent-formula parent outer))) | |
167 (if (not (and (consp next) | |
168 (eq outer (nth 1 next)))) | |
169 (setq new nil) | |
170 (setq new (calc-build-assoc-term | |
171 (car outer) | |
172 (calc-build-assoc-term | |
173 (car next) (nth 1 outer) (nth 2 next)) | |
174 sel) | |
175 sel (nth 2 new) | |
176 new (calc-replace-sub-formula | |
177 parent next new)))))) | |
178 (if (eq (nth (1- (length parent)) parent) sel) | |
179 (setq new nil) | |
180 (let ((p (nthcdr (calc-find-sub-formula parent sel) | |
181 (setq new (copy-sequence parent))))) | |
182 (setcar p (nth 1 p)) | |
183 (setcar (cdr p) sel)))) | |
184 (if (null new) | |
185 (if arg | |
186 (error "Term is already rightmost") | |
187 (or reselect | |
188 (calc-pop-push-list 1 (list expr) num '(nil))) | |
189 (setq arg 0)) | |
190 (calc-pop-push-record-list | |
191 1 "rght" | |
192 (list (calc-replace-sub-formula expr parent new)) | |
193 num | |
194 (list (and (or (not (eq arg 0)) reselect) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
195 sel)))))))))) |
40785 | 196 |
197 (defun calc-build-assoc-term (op lhs rhs) | |
198 (cond ((and (eq op '+) (or (math-looks-negp rhs) | |
199 (and (eq (car-safe rhs) 'cplx) | |
200 (math-negp (nth 1 rhs)) | |
201 (eq (nth 2 rhs) 0)))) | |
202 (list '- lhs (math-neg rhs))) | |
203 ((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 '*) (and (eq (car-safe rhs) '/) | |
209 (or (math-equal-int (nth 1 rhs) 1) | |
210 (equal (nth 1 rhs) '(cplx 1 0))))) | |
211 (list '/ lhs (nth 2 rhs))) | |
212 ((and (eq op '/) (and (eq (car-safe rhs) '/) | |
213 (or (math-equal-int (nth 1 rhs) 1) | |
214 (equal (nth 1 rhs) '(cplx 1 0))))) | |
215 (list '/ lhs (nth 2 rhs))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
216 (t (list op lhs rhs)))) |
40785 | 217 |
218 (defun calc-sel-unpack () | |
219 (interactive) | |
220 (calc-wrapper | |
221 (calc-preserve-point) | |
222 (let* ((num (max 1 (calc-locate-cursor-element (point)))) | |
223 (reselect calc-keep-selection) | |
224 (entry (calc-top num 'entry)) | |
225 (expr (car entry)) | |
226 (sel (or (calc-auto-selection entry) expr))) | |
227 (or (and (not (math-primp sel)) | |
228 (= (length sel) 2)) | |
229 (error "Selection must be a function of one argument")) | |
230 (calc-pop-push-record-list 1 "unpk" | |
231 (list (calc-replace-sub-formula | |
232 expr sel (nth 1 sel))) | |
233 num | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
234 (list (and reselect (nth 1 sel))))))) |
40785 | 235 |
236 (defun calc-sel-isolate () | |
237 (interactive) | |
238 (calc-slow-wrapper | |
239 (calc-preserve-point) | |
240 (let* ((num (max 1 (calc-locate-cursor-element (point)))) | |
241 (reselect calc-keep-selection) | |
242 (entry (calc-top num 'entry)) | |
243 (expr (car entry)) | |
244 (sel (or (calc-auto-selection entry) (error "No selection"))) | |
245 (eqn sel) | |
246 soln) | |
247 (while (and (or (consp (setq eqn (calc-find-parent-formula expr eqn))) | |
248 (error "Selection must be a member of an equation")) | |
249 (not (assq (car eqn) calc-tweak-eqn-table)))) | |
250 (setq soln (math-solve-eqn eqn sel calc-hyperbolic-flag)) | |
251 (or soln | |
252 (error "No solution found")) | |
253 (setq soln (calc-encase-atoms | |
254 (if (eq (not (calc-find-sub-formula (nth 2 eqn) sel)) | |
255 (eq (nth 1 soln) sel)) | |
256 soln | |
257 (list (nth 1 (assq (car soln) calc-tweak-eqn-table)) | |
258 (nth 2 soln) | |
259 (nth 1 soln))))) | |
260 (calc-pop-push-record-list 1 "isol" | |
261 (list (calc-replace-sub-formula | |
262 expr eqn soln)) | |
263 num | |
264 (list (and reselect sel))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
265 (calc-handle-whys)))) |
40785 | 266 |
267 (defun calc-sel-commute (many) | |
268 (interactive "P") | |
269 (let ((calc-assoc-selections nil)) | |
270 (calc-rewrite-selection "CommuteRules" many "cmut")) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
271 (calc-set-mode-line)) |
40785 | 272 |
273 (defun calc-sel-jump-equals (many) | |
274 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
275 (calc-rewrite-selection "JumpRules" many "jump")) |
40785 | 276 |
277 (defun calc-sel-distribute (many) | |
278 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
279 (calc-rewrite-selection "DistribRules" many "dist")) |
40785 | 280 |
281 (defun calc-sel-merge (many) | |
282 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
283 (calc-rewrite-selection "MergeRules" many "merg")) |
40785 | 284 |
285 (defun calc-sel-negate (many) | |
286 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
287 (calc-rewrite-selection "NegateRules" many "jneg")) |
40785 | 288 |
289 (defun calc-sel-invert (many) | |
290 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
291 (calc-rewrite-selection "InvertRules" many "jinv")) |
40785 | 292 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
293 ;;; calcsel2.el ends here |