1 ;;; calcsel2.el --- selection functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 ;; This file is autoloaded from calc-ext.el.
35 ;; The variable calc-keep-selection is declared and set in calc-sel.el.
36 (defvar calc-keep-selection
)
38 ;; The variable calc-sel-reselect is local to the methods below,
39 ;; but is used by some functions in calc-sel.el which are called
40 ;; by the functions below.
42 (defun calc-commute-left (arg)
45 (calc-commute-right (- arg
))
48 (let ((num (max 1 (calc-locate-cursor-element (point))))
49 (calc-sel-reselect calc-keep-selection
))
50 (if (= arg
0) (setq arg nil
))
51 (while (or (null arg
) (>= (setq arg
(1- arg
)) 0))
52 (let* ((entry (calc-top num
'entry
))
54 (sel (calc-auto-selection entry
))
57 (consp (setq parent
(calc-find-assoc-parent-formula
59 (error "No term is selected"))
60 (if (and calc-assoc-selections
61 (assq (car parent
) calc-assoc-ops
))
62 (let ((outer (calc-find-parent-formula parent sel
)))
63 (if (eq sel
(nth 2 outer
))
64 (setq new
(calc-replace-sub-formula
68 (nth 1 (assq (car-safe (nth 1 outer
))
70 (let* ((other (nth 2 (nth 1 outer
)))
71 (new (calc-build-assoc-term
73 (calc-build-assoc-term
78 (setq sel
(nth 2 (nth 1 new
)))
81 (calc-build-assoc-term
83 (setq sel
(math-neg sel
))
86 (calc-build-assoc-term
88 (setq sel
(calcFunc-div 1 sel
))
90 (t (calc-build-assoc-term
91 (car outer
) sel
(nth 1 outer
))))))
92 (let ((next (calc-find-parent-formula parent outer
)))
93 (if (not (and (consp next
)
94 (eq outer
(nth 2 next
))
95 (eq (car next
) (car outer
))))
97 (setq new
(calc-build-assoc-term
100 (calc-build-assoc-term
101 (car next
) (nth 1 next
) (nth 2 outer
)))
103 new
(calc-replace-sub-formula
104 parent next new
))))))
105 (if (eq (nth 1 parent
) sel
)
107 (let ((p (nthcdr (1- (calc-find-sub-formula parent sel
))
108 (setq new
(copy-sequence parent
)))))
109 (setcar (cdr p
) (car p
))
113 (error "Term is already leftmost")
114 (or calc-sel-reselect
115 (calc-pop-push-list 1 (list expr
) num
'(nil)))
117 (calc-pop-push-record-list
119 (list (calc-replace-sub-formula expr parent new
))
121 (list (and (or (not (eq arg
0)) calc-sel-reselect
)
124 (defun calc-commute-right (arg)
127 (calc-commute-left (- arg
))
129 (calc-preserve-point)
130 (let ((num (max 1 (calc-locate-cursor-element (point))))
131 (calc-sel-reselect calc-keep-selection
))
132 (if (= arg
0) (setq arg nil
))
133 (while (or (null arg
) (>= (setq arg
(1- arg
)) 0))
134 (let* ((entry (calc-top num
'entry
))
136 (sel (calc-auto-selection entry
))
139 (consp (setq parent
(calc-find-assoc-parent-formula
141 (error "No term is selected"))
142 (if (and calc-assoc-selections
143 (assq (car parent
) calc-assoc-ops
))
144 (let ((outer (calc-find-parent-formula parent sel
)))
145 (if (eq sel
(nth 1 outer
))
146 (setq new
(calc-replace-sub-formula
148 (if (memq (car outer
)
149 (nth 2 (assq (car-safe (nth 2 outer
))
151 (let ((other (nth 1 (nth 2 outer
))))
152 (calc-build-assoc-term
155 (calc-build-assoc-term
158 (nth 2 (nth 2 outer
)))))
161 (calc-build-assoc-term
163 (math-neg (nth 2 outer
))
166 (calc-build-assoc-term
168 (calcFunc-div 1 (nth 2 outer
))
170 (t (calc-build-assoc-term
174 (setq sel
(nth 2 new
))
176 (let ((next (calc-find-parent-formula parent outer
)))
177 (if (not (and (consp next
)
178 (eq outer
(nth 1 next
))))
180 (setq new
(calc-build-assoc-term
182 (calc-build-assoc-term
183 (car next
) (nth 1 outer
) (nth 2 next
))
186 new
(calc-replace-sub-formula
187 parent next new
))))))
188 (if (eq (nth (1- (length parent
)) parent
) sel
)
190 (let ((p (nthcdr (calc-find-sub-formula parent sel
)
191 (setq new
(copy-sequence parent
)))))
193 (setcar (cdr p
) sel
))))
196 (error "Term is already rightmost")
197 (or calc-sel-reselect
198 (calc-pop-push-list 1 (list expr
) num
'(nil)))
200 (calc-pop-push-record-list
202 (list (calc-replace-sub-formula expr parent new
))
204 (list (and (or (not (eq arg
0)) calc-sel-reselect
)
207 (defun calc-build-assoc-term (op lhs rhs
)
208 (cond ((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
'-
) (or (math-looks-negp rhs
)
214 (and (eq (car-safe rhs
) 'cplx
)
215 (math-negp (nth 1 rhs
))
216 (eq (nth 2 rhs
) 0))))
217 (list '+ lhs
(math-neg 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 ((and (eq op
'/) (and (eq (car-safe rhs
) '/)
223 (or (math-equal-int (nth 1 rhs
) 1)
224 (equal (nth 1 rhs
) '(cplx 1 0)))))
225 (list '/ lhs
(nth 2 rhs
)))
226 (t (list op lhs rhs
))))
228 (defun calc-sel-unpack ()
231 (calc-preserve-point)
232 (let* ((num (max 1 (calc-locate-cursor-element (point))))
233 (calc-sel-reselect calc-keep-selection
)
234 (entry (calc-top num
'entry
))
236 (sel (or (calc-auto-selection entry
) expr
)))
237 (or (and (not (math-primp sel
))
239 (error "Selection must be a function of one argument"))
240 (calc-pop-push-record-list 1 "unpk"
241 (list (calc-replace-sub-formula
242 expr sel
(nth 1 sel
)))
244 (list (and calc-sel-reselect
(nth 1 sel
)))))))
246 (defun calc-sel-isolate ()
249 (calc-preserve-point)
250 (let* ((num (max 1 (calc-locate-cursor-element (point))))
251 (calc-sel-reselect calc-keep-selection
)
252 (entry (calc-top num
'entry
))
254 (sel (or (calc-auto-selection entry
) (error "No selection")))
257 (while (and (or (consp (setq eqn
(calc-find-parent-formula expr eqn
)))
258 (error "Selection must be a member of an equation"))
259 (not (assq (car eqn
) calc-tweak-eqn-table
))))
260 (setq soln
(math-solve-eqn eqn sel calc-hyperbolic-flag
))
262 (error "No solution found"))
263 (setq soln
(calc-encase-atoms
264 (if (eq (not (calc-find-sub-formula (nth 2 eqn
) sel
))
265 (eq (nth 1 soln
) sel
))
267 (list (nth 1 (assq (car soln
) calc-tweak-eqn-table
))
270 (calc-pop-push-record-list 1 "isol"
271 (list (calc-replace-sub-formula
274 (list (and calc-sel-reselect sel
)))
275 (calc-handle-whys))))
277 (defun calc-sel-commute (many)
279 (let ((calc-assoc-selections nil
))
280 (calc-rewrite-selection "CommuteRules" many
"cmut"))
281 (calc-set-mode-line))
283 (defun calc-sel-jump-equals (many)
285 (calc-rewrite-selection "JumpRules" many
"jump"))
287 (defun calc-sel-distribute (many)
289 (calc-rewrite-selection "DistribRules" many
"dist"))
291 (defun calc-sel-merge (many)
293 (calc-rewrite-selection "MergeRules" many
"merg"))
295 (defun calc-sel-negate (many)
297 (calc-rewrite-selection "NegateRules" many
"jneg"))
299 (defun calc-sel-invert (many)
301 (calc-rewrite-selection "InvertRules" many
"jinv"))
305 ;;; arch-tag: 7c5b8d65-b8f0-45d9-820d-9930f8ee114b
306 ;;; calcsel2.el ends here