1 ;;; calc-mode.el --- calculator modes 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 ;; Declare functions which are defined elsewhere.
36 (declare-function calc-embedded-save-original-modes
"calc-embed" ())
39 (defun calc-line-numbering (n)
42 (message (if (calc-change-mode 'calc-line-numbering n t t
)
43 "Displaying stack level numbers"
44 "Hiding stack level numbers"))))
46 (defun calc-line-breaking (n)
50 (and (> (setq n
(prefix-numeric-value n
)) 0)
53 (not calc-line-breaking
)))
54 (if (calc-change-mode 'calc-line-breaking n t
)
55 (if (integerp calc-line-breaking
)
56 (message "Breaking lines longer than %d characters" n
)
57 (message "Breaking long lines in Stack display"))
58 (message "Not breaking long lines in Stack display"))))
61 (defun calc-left-justify (n)
64 (and n
(setq n
(prefix-numeric-value n
)))
65 (calc-change-mode '(calc-display-just calc-display-origin
)
68 (message "Displaying stack entries indented by %d" n
)
69 (message "Displaying stack entries left-justified"))))
71 (defun calc-center-justify (n)
74 (and n
(setq n
(prefix-numeric-value n
)))
75 (calc-change-mode '(calc-display-just calc-display-origin
)
78 (message "Displaying stack entries centered on column %d" n
)
79 (message "Displaying stack entries centered in window"))))
81 (defun calc-right-justify (n)
84 (and n
(setq n
(prefix-numeric-value n
)))
85 (calc-change-mode '(calc-display-just calc-display-origin
)
88 (message "Displaying stack entries right-justified to column %d" n
)
89 (message "Displaying stack entries right-justified in window"))))
91 (defun calc-left-label (s)
92 (interactive "sLefthand label: ")
95 (setq s
(concat s
" ")))
96 (calc-change-mode 'calc-left-label s t
)))
98 (defun calc-right-label (s)
99 (interactive "sRighthand label: ")
102 (setq s
(concat " " s
)))
103 (calc-change-mode 'calc-right-label s t
)))
105 (defun calc-auto-why (n)
110 (setq n
(prefix-numeric-value n
))
111 (if (<= n
0) (setq n nil
)
112 (if (> n
1) (setq n t
))))
113 (setq n
(and (not (eq calc-auto-why t
)) (if calc-auto-why t
1))))
114 (calc-change-mode 'calc-auto-why n nil
)
116 (message "User must press `w' to explain unsimplified results"))
118 (message "Automatically doing `w' to explain unsimplified results"))
120 (message "Automatically doing `w' only for unusual messages")))))
122 (defun calc-group-digits (n)
127 (setq n
(prefix-numeric-value n
))
128 (cond ((or (> n
0) (< n -
1)))
132 (setq n calc-group-digits
))))
133 (setq n
(not calc-group-digits
)))
134 (calc-change-mode 'calc-group-digits n t
)
136 (message "Grouping is off"))
138 (message "Grouping every %d digits" (math-abs n
)))
140 (message "Grouping is on")))))
142 (defun calc-group-char (ch)
143 (interactive "cGrouping character: ")
146 (error "Control characters not allowed for grouping"))
149 (setq ch
(char-to-string ch
)))
150 (calc-change-mode 'calc-group-char ch calc-group-digits
)
151 (message "Digit grouping character is \"%s\"" ch
)))
153 (defun calc-point-char (ch)
154 (interactive "cCharacter to use as decimal point: ")
157 (error "Control characters not allowed as decimal point"))
158 (calc-change-mode 'calc-point-char
(char-to-string ch
) t
)
159 (message "Decimal point character is \"%c\"" ch
)))
161 (defun calc-normal-notation (n)
164 (calc-change-mode 'calc-float-format
165 (let* ((val (if n
(prefix-numeric-value n
) 0))
166 (mode (/ (+ val
5000) 10000)))
167 (if (or (< val -
5000) (> mode
3))
168 (error "Prefix out of range"))
169 (setq n
(list (aref [float sci eng fix
] mode
)
170 (- (%
(+ val
5000) 10000) 5000))))
173 (message "Displaying floating-point numbers normally")
176 "Displaying floating-point numbers with %d significant digits"
178 (message "Displaying floating-point numbers with (precision%d)"
181 (defun calc-fix-notation (n)
182 (interactive "NDigits after decimal point: ")
184 (calc-change-mode 'calc-float-format
185 (setq n
(list 'fix
(if n
(prefix-numeric-value n
) 0)))
187 (message "Displaying floats with %d digits after decimal"
188 (math-abs (nth 1 n
)))))
190 (defun calc-sci-notation (n)
193 (calc-change-mode 'calc-float-format
194 (setq n
(list 'sci
(if n
(prefix-numeric-value n
) 0)))
197 (message "Displaying floats in scientific notation")
199 (message "Displaying scientific notation with %d significant digits"
201 (message "Displaying scientific notation with (precision%d)"
204 (defun calc-eng-notation (n)
207 (calc-change-mode 'calc-float-format
208 (setq n
(list 'eng
(if n
(prefix-numeric-value n
) 0)))
211 (message "Displaying floats in engineering notation")
213 (message "Displaying engineering notation with %d significant digits"
215 (message "Displaying engineering notation with (precision%d)"
219 (defun calc-truncate-stack (n &optional rel
)
222 (let ((oldtop calc-stack-top
)
223 (newtop calc-stack-top
))
224 (calc-record-undo (list 'set
'saved-stack-top calc-stack-top
))
225 (let ((calc-stack-top 0)
226 (nn (prefix-numeric-value n
)))
231 (setq nn
(+ oldtop nn
))
233 (setq nn
(+ nn
(calc-stack-size)))
237 (if (> nn
(calc-stack-size))
240 (max 1 (calc-locate-cursor-element (point)))))
241 (if (= newtop oldtop
)
243 (calc-pop-stack 1 oldtop t
)
244 (calc-push-list '(top-of-stack) newtop
)
245 (if calc-line-numbering
247 (calc-record-undo (list 'set
'saved-stack-top
0))
248 (setq calc-stack-top newtop
))))
250 (defun calc-truncate-up (n)
252 (calc-truncate-stack n t
))
254 (defun calc-truncate-down (n)
256 (calc-truncate-stack (- n
) t
))
258 (defun calc-display-raw (arg)
261 (setq calc-display-raw
(if calc-display-raw nil
(if arg
0 t
)))
264 (message "Press d ' again to cancel \"raw\" display mode"))))
271 (defun calc-save-modes ()
275 (vals (mapcar (function (lambda (v) (symbol-value (car v
))))
276 calc-mode-var-list
)))
277 (unless calc-settings-file
278 (error "No `calc-settings-file' specified"))
279 (set-buffer (find-file-noselect (substitute-in-file-name
280 calc-settings-file
)))
281 (goto-char (point-min))
282 (if (and (search-forward ";;; Mode settings stored by Calc" nil t
)
286 (search-forward "\n;;; End of mode settings" nil t
)))
290 (delete-region pos
(point)))
291 (goto-char (point-max))
294 (insert ";;; Mode settings stored by Calc on " (current-time-string) "\n")
295 (let ((list calc-mode-var-list
))
297 (let* ((v (car (car list
)))
298 (def (nth 1 (car list
)))
302 (insert "(setq " (symbol-name v
) " ")
303 (if (and (or (listp val
)
305 (not (memq val
'(nil t
))))
307 (insert (prin1-to-string val
) ")\n"))))
308 (setq list
(cdr list
)
310 (run-hooks 'calc-mode-save-hook
)
311 (insert ";;; End of mode settings\n")
313 (if calc-embedded-info
314 (calc-embedded-save-original-modes)))))
316 (defun calc-settings-file-name (name &optional arg
)
318 (list (read-file-name (format "Settings file name (normally %s): "
319 (abbreviate-file-name calc-settings-file
)))
322 (setq arg
(if arg
(prefix-numeric-value arg
) 0))
323 (if (string-equal (file-name-nondirectory name
) "")
324 (message "Calc settings file is \"%s\"" calc-settings-file
)
325 (if (< (math-abs arg
) 2)
326 (let ((list calc-mode-var-list
))
328 (set (car (car list
)) (nth 1 (car list
)))
329 (setq list
(cdr list
)))))
330 (setq calc-settings-file name
)
333 (equal user-init-file calc-settings-file
)
337 (message "New file")))))
339 (defun math-get-modes-vec ()
345 (+ (if (<= (nth 1 calc-float-format
) 0)
346 (+ calc-internal-prec
(nth 1 calc-float-format
))
347 (nth 1 calc-float-format
))
348 (cdr (assq (car calc-float-format
)
349 '((float .
0) (sci .
10000)
350 (eng .
20000) (fix .
30000)))))
351 (cond ((eq calc-angle-mode
'rad
) 2)
352 ((eq calc-angle-mode
'hms
) 3)
354 (if calc-symbolic-mode
1 0)
355 (if calc-prefer-frac
1 0)
356 (if (eq calc-complex-mode
'polar
) 1 0)
357 (cond ((eq calc-matrix-mode
'scalar
) 0)
358 ((eq calc-matrix-mode
'matrix
) -
2)
359 ((eq calc-matrix-mode
'sqmatrix
) -
3)
362 (cond ((eq calc-simplify-mode
'none
) -
1)
363 ((eq calc-simplify-mode
'num
) 0)
364 ((eq calc-simplify-mode
'binary
) 2)
365 ((eq calc-simplify-mode
'alg
) 3)
366 ((eq calc-simplify-mode
'ext
) 4)
367 ((eq calc-simplify-mode
'units
) 5)
369 (cond ((eq calc-infinite-mode
1) 0)
370 (calc-infinite-mode 1)
373 (defun calc-get-modes (n)
376 (let ((modes (math-get-modes-vec)))
377 (calc-enter-result 0 "mode"
379 (if (and (>= (setq n
(prefix-numeric-value n
)) 1)
380 (< n
(length modes
)))
382 (error "Prefix out of range"))
385 (defun calc-shift-prefix (arg)
388 (setq calc-shift-prefix
(if arg
389 (> (prefix-numeric-value arg
) 0)
390 (not calc-shift-prefix
)))
392 (message (if calc-shift-prefix
393 "Prefix keys are now case-insensitive"
394 "Prefix keys must be unshifted (except V, Z)"))))
396 (defun calc-mode-record-mode (n)
399 (calc-change-mode 'calc-mode-save-mode
401 (cond ((not calc-embedded-info
)
402 (if (eq calc-mode-save-mode
'save
)
404 ((eq calc-mode-save-mode
'local
) 'edit
)
405 ((eq calc-mode-save-mode
'edit
) 'perm
)
406 ((eq calc-mode-save-mode
'perm
) 'global
)
407 ((eq calc-mode-save-mode
'global
) 'save
)
408 ((eq calc-mode-save-mode
'save
) nil
)
409 ((eq calc-mode-save-mode nil
) 'local
)))
410 ((= (setq n
(prefix-numeric-value n
)) 0) nil
)
417 (cond ((and (eq calc-mode-save-mode
'local
) calc-embedded-info
)
418 "Recording mode changes with [calc-mode: ...]")
419 ((eq calc-mode-save-mode
'edit
)
420 "Recording mode changes with [calc-edit-mode: ...]")
421 ((eq calc-mode-save-mode
'perm
)
422 "Recording mode changes with [calc-perm-mode: ...]")
423 ((eq calc-mode-save-mode
'global
)
424 "Recording mode changes with [calc-global-mode: ...]")
425 ((eq calc-mode-save-mode
'save
)
426 (format "Recording mode changes in \"%s\""
429 "Not recording mode changes permanently")))))
431 (defun calc-total-algebraic-mode (flag)
434 (if (eq calc-algebraic-mode
'total
)
435 (calc-algebraic-mode nil
)
436 (calc-change-mode '(calc-algebraic-mode calc-incomplete-algebraic-mode
)
438 (use-local-map calc-alg-map
)
440 "All keys begin algebraic entry; use Meta (ESC) for Calc keys"))))
442 (defun calc-algebraic-mode (flag)
446 (calc-change-mode '(calc-algebraic-mode
447 calc-incomplete-algebraic-mode
)
448 (list nil
(not calc-incomplete-algebraic-mode
)))
449 (calc-change-mode '(calc-algebraic-mode calc-incomplete-algebraic-mode
)
450 (list (not calc-algebraic-mode
) nil
)))
451 (use-local-map calc-mode-map
)
452 (message (if calc-algebraic-mode
453 "Numeric keys and ( and [ begin algebraic entry"
454 (if calc-incomplete-algebraic-mode
455 "Only ( and [ begin algebraic entry"
456 "No keys except ' and $ begin algebraic entry")))))
458 (defun calc-symbolic-mode (n)
462 (message (if (calc-change-mode 'calc-symbolic-mode n nil t
)
463 "Inexact computations like sqrt(2) are deferred"
464 "Numerical computations are always done immediately"))))
466 (defun calc-infinite-mode (n)
471 (calc-change-mode 'calc-infinite-mode
1)
472 (message "Computations like 1 / 0 produce \"inf\""))
473 (message (if (calc-change-mode 'calc-infinite-mode n nil t
)
474 "Computations like 1 / 0 produce \"uinf\""
475 "Computations like 1 / 0 are left unsimplified")))))
477 (defun calc-matrix-mode (arg)
480 (calc-change-mode 'calc-matrix-mode
481 (cond ((eq arg
0) 'scalar
)
482 ((< (prefix-numeric-value arg
) 1)
483 (and (< (prefix-numeric-value arg
) -
1) 'matrix
))
485 (if (consp arg
) 'sqmatrix
486 (prefix-numeric-value arg
)))
487 ((eq calc-matrix-mode
'matrix
) 'scalar
)
488 ((eq calc-matrix-mode
'scalar
) nil
)
490 (if (integerp calc-matrix-mode
)
491 (message "Variables are assumed to be %dx%d matrices"
492 calc-matrix-mode calc-matrix-mode
)
493 (message (if (eq calc-matrix-mode
'matrix
)
494 "Variables are assumed to be matrices"
495 (if (eq calc-matrix-mode
'sqmatrix
)
496 "Variables are assumed to be square matrices"
498 "Variables are assumed to be scalars (non-matrices)"
499 "Variables are not assumed to be matrix or scalar")))))))
501 (defun calc-set-simplify-mode (mode arg msg
)
502 (calc-change-mode 'calc-simplify-mode
504 (and (> (prefix-numeric-value arg
) 0)
506 (and (not (eq calc-simplify-mode mode
))
508 (message "%s" (if (eq calc-simplify-mode mode
)
510 "Default simplifications enabled")))
512 (defun calc-no-simplify-mode (arg)
515 (calc-set-simplify-mode 'none arg
516 "All default simplifications are disabled")))
518 (defun calc-num-simplify-mode (arg)
521 (calc-set-simplify-mode 'num arg
522 "Default simplifications apply only if arguments are numeric")))
524 (defun calc-default-simplify-mode (arg)
528 (calc-set-simplify-mode
529 nil nil
"Usual default simplifications are enabled")))
530 ((= arg
0) (calc-num-simplify-mode 1))
531 ((< arg
0) (calc-no-simplify-mode 1))
532 ((= arg
2) (calc-bin-simplify-mode 1))
533 ((= arg
3) (calc-alg-simplify-mode 1))
534 ((= arg
4) (calc-ext-simplify-mode 1))
535 ((= arg
5) (calc-units-simplify-mode 1))
536 (t (error "Prefix argument out of range"))))
538 (defun calc-bin-simplify-mode (arg)
541 (calc-set-simplify-mode 'binary arg
542 (format "Binary simplification occurs by default (word size=%d)"
545 (defun calc-alg-simplify-mode (arg)
548 (calc-set-simplify-mode 'alg arg
549 "Algebraic simplification occurs by default")))
551 (defun calc-ext-simplify-mode (arg)
554 (calc-set-simplify-mode 'ext arg
555 "Extended algebraic simplification occurs by default")))
557 (defun calc-units-simplify-mode (arg)
560 (calc-set-simplify-mode 'units arg
561 "Units simplification occurs by default")))
563 (defun calc-auto-recompute (arg)
566 (calc-change-mode 'calc-auto-recompute arg nil t
)
567 (calc-refresh-evaltos)
568 (message (if calc-auto-recompute
569 "Automatically recomputing `=>' forms when necessary"
570 "Not recomputing `=>' forms automatically"))))
572 (defun calc-working (n)
576 (calc-pop-push-record 0 "work"
577 (cond ((eq calc-display-working-message t
) 1)
578 (calc-display-working-message 2)
580 ((eq n
2) (calc-change-mode 'calc-display-working-message
'lots
))
581 ((eq n
0) (calc-change-mode 'calc-display-working-message nil
))
582 ((eq n
1) (calc-change-mode 'calc-display-working-message t
)))
583 (cond ((eq calc-display-working-message t
)
584 (message "\"Working...\" messages enabled"))
585 (calc-display-working-message
586 (message "Detailed \"Working...\" messages enabled"))
588 (message "\"Working...\" messages disabled")))))
590 (defun calc-always-load-extensions ()
593 (if (setq calc-always-load-extensions
(not calc-always-load-extensions
))
594 (message "Always loading extensions package")
595 (message "Loading extensions package on demand only"))))
598 (defun calc-matrix-left-justify ()
601 (calc-change-mode 'calc-matrix-just nil t
)
602 (message "Matrix elements will be left-justified in columns")))
604 (defun calc-matrix-center-justify ()
607 (calc-change-mode 'calc-matrix-just
'center t
)
608 (message "Matrix elements will be centered in columns")))
610 (defun calc-matrix-right-justify ()
613 (calc-change-mode 'calc-matrix-just
'right t
)
614 (message "Matrix elements will be right-justified in columns")))
616 (defun calc-full-vectors (n)
619 (message (if (calc-change-mode 'calc-full-vectors n t t
)
620 "Displaying long vectors in full"
621 "Displaying long vectors in [a, b, c, ..., z] notation"))))
623 (defun calc-full-trail-vectors (n)
626 (message (if (calc-change-mode 'calc-full-trail-vectors n nil t
)
627 "Recording long vectors in full"
628 "Recording long vectors in [a, b, c, ..., z] notation"))))
630 (defun calc-break-vectors (n)
633 (message (if (calc-change-mode 'calc-break-vectors n t t
)
634 "Displaying vector elements one-per-line"
635 "Displaying vector elements all on one line"))))
637 (defun calc-vector-commas ()
640 (if (calc-change-mode 'calc-vector-commas
(if calc-vector-commas nil
",") t
)
641 (message "Separating vector elements with \",\"")
642 (message "Separating vector elements with spaces"))))
644 (defun calc-vector-brackets ()
647 (if (calc-change-mode 'calc-vector-brackets
648 (if (equal calc-vector-brackets
"[]") nil
"[]") t
)
649 (message "Surrounding vectors with \"[]\"")
650 (message "Not surrounding vectors with brackets"))))
652 (defun calc-vector-braces ()
655 (if (calc-change-mode 'calc-vector-brackets
656 (if (equal calc-vector-brackets
"{}") nil
"{}") t
)
657 (message "Surrounding vectors with \"{}\"")
658 (message "Not surrounding vectors with brackets"))))
660 (defun calc-vector-parens ()
663 (if (calc-change-mode 'calc-vector-brackets
664 (if (equal calc-vector-brackets
"()") nil
"()") t
)
665 (message "Surrounding vectors with \"()\"")
666 (message "Not surrounding vectors with brackets"))))
668 (defun calc-matrix-brackets (arg)
669 (interactive "sCode letters (R, O, C, P): ")
671 (let ((code (append (and (string-match "[rR]" arg
) '(R))
672 (and (string-match "[oO]" arg
) '(O))
673 (and (string-match "[cC]" arg
) '(C))
674 (and (string-match "[pP]" arg
) '(P))))
675 (bad (string-match "[^rRoOcCpP ]" arg
)))
677 (error "Unrecognized character: %c" (aref arg bad
)))
678 (calc-change-mode 'calc-matrix-brackets code t
))))
682 ;;; arch-tag: ecc70eea-c712-43f2-9085-4205e58d6ddf
683 ;;; calc-mode.el ends here