]> code.delx.au - gnu-emacs/blob - lisp/calc/calcsel2.el
Update copyright year to 2016
[gnu-emacs] / lisp / calc / calcsel2.el
1 ;;; calcsel2.el --- selection functions for Calc
2
3 ;; Copyright (C) 1990-1993, 2001-2016 Free Software Foundation, Inc.
4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 ;; This file is autoloaded from calc-ext.el.
27
28 (require 'calc-ext)
29 (require 'calc-macs)
30
31 ;; The variable calc-keep-selection is declared and set in calc-sel.el.
32 (defvar calc-keep-selection)
33
34 ;; The variable calc-sel-reselect is local to the methods below,
35 ;; but is used by some functions in calc-sel.el which are called
36 ;; by the functions below.
37
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 (calc-sel-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
78 '+
79 (setq sel (math-neg sel))
80 (nth 1 outer)))
81 ((eq (car outer) '/)
82 (calc-build-assoc-term
83 '*
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 calc-sel-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))
116 num
117 (list (and (or (not (eq arg 0)) calc-sel-reselect)
118 sel))))))))))
119
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 (calc-sel-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))
153 sel
154 (nth 2 (nth 2 outer)))))
155 (let ((new (cond
156 ((eq (car outer) '-)
157 (calc-build-assoc-term
158 '+
159 (math-neg (nth 2 outer))
160 sel))
161 ((eq (car outer) '/)
162 (calc-build-assoc-term
163 '*
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 calc-sel-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))
199 num
200 (list (and (or (not (eq arg 0)) calc-sel-reselect)
201 sel))))))))))
202
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))))
223
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 (calc-sel-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)))
239 num
240 (list (and calc-sel-reselect (nth 1 sel)))))))
241
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 (calc-sel-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))
269 num
270 (list (and calc-sel-reselect sel)))
271 (calc-handle-whys))))
272
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))
278
279 (defun calc-sel-jump-equals (many)
280 (interactive "P")
281 (calc-rewrite-selection "JumpRules" many "jump"))
282
283 (defun calc-sel-distribute (many)
284 (interactive "P")
285 (calc-rewrite-selection "DistribRules" many "dist"))
286
287 (defun calc-sel-merge (many)
288 (interactive "P")
289 (calc-rewrite-selection "MergeRules" many "merg"))
290
291 (defun calc-sel-negate (many)
292 (interactive "P")
293 (calc-rewrite-selection "NegateRules" many "jneg"))
294
295 (defun calc-sel-invert (many)
296 (interactive "P")
297 (calc-rewrite-selection "InvertRules" many "jinv"))
298
299 (provide 'calcsel2)
300
301 ;;; calcsel2.el ends here