1 ;;; calcsel2.el --- selection functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2016 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;; This file is autoloaded from calc-ext.el.
31 ;; The variable calc-keep-selection is declared and set in calc-sel.el.
32 (defvar calc-keep-selection
)
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.
38 (defun calc-commute-left (arg)
41 (calc-commute-right (- arg
))
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
))
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")
110 (or calc-sel-reselect
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)) calc-sel-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 (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
))
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")
193 (or calc-sel-reselect
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)) calc-sel-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 (calc-sel-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 calc-sel-reselect
(nth 1 sel
)))))))
242 (defun calc-sel-isolate ()
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
))
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 calc-sel-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"))
301 ;;; calcsel2.el ends here