1 ;;; calc-mode.el --- calculator modes for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Colin Walters <walters@debian.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
30 ;; This file is autoloaded from calc-ext.el.
35 (defun calc-Need-calc-mode () nil)
38 (defun calc-line-numbering (n)
41 (message (if (calc-change-mode 'calc-line-numbering n t t)
42 "Displaying stack level numbers"
43 "Hiding stack level numbers"))))
45 (defun calc-line-breaking (n)
49 (and (> (setq n (prefix-numeric-value n)) 0)
52 (not calc-line-breaking)))
53 (if (calc-change-mode 'calc-line-breaking n t)
54 (if (integerp calc-line-breaking)
55 (message "Breaking lines longer than %d characters" n)
56 (message "Breaking long lines in Stack display"))
57 (message "Not breaking long lines in Stack display"))))
60 (defun calc-left-justify (n)
63 (and n (setq n (prefix-numeric-value n)))
64 (calc-change-mode '(calc-display-just calc-display-origin)
67 (message "Displaying stack entries indented by %d" n)
68 (message "Displaying stack entries left-justified"))))
70 (defun calc-center-justify (n)
73 (and n (setq n (prefix-numeric-value n)))
74 (calc-change-mode '(calc-display-just calc-display-origin)
77 (message "Displaying stack entries centered on column %d" n)
78 (message "Displaying stack entries centered in window"))))
80 (defun calc-right-justify (n)
83 (and n (setq n (prefix-numeric-value n)))
84 (calc-change-mode '(calc-display-just calc-display-origin)
87 (message "Displaying stack entries right-justified to column %d" n)
88 (message "Displaying stack entries right-justified in window"))))
90 (defun calc-left-label (s)
91 (interactive "sLefthand label: ")
94 (setq s (concat s " ")))
95 (calc-change-mode 'calc-left-label s t)))
97 (defun calc-right-label (s)
98 (interactive "sRighthand label: ")
101 (setq s (concat " " s)))
102 (calc-change-mode 'calc-right-label s t)))
104 (defun calc-auto-why (n)
109 (setq n (prefix-numeric-value n))
110 (if (<= n 0) (setq n nil)
111 (if (> n 1) (setq n t))))
112 (setq n (and (not (eq calc-auto-why t)) (if calc-auto-why t 1))))
113 (calc-change-mode 'calc-auto-why n nil)
115 (message "User must press `w' to explain unsimplified results"))
117 (message "Automatically doing `w' to explain unsimplified results"))
119 (message "Automatically doing `w' only for unusual messages")))))
121 (defun calc-group-digits (n)
126 (setq n (prefix-numeric-value n))
127 (cond ((or (> n 0) (< n -1)))
131 (setq n calc-group-digits))))
132 (setq n (not calc-group-digits)))
133 (calc-change-mode 'calc-group-digits n t)
135 (message "Grouping is off"))
137 (message "Grouping every %d digits" (math-abs n)))
139 (message "Grouping is on")))))
141 (defun calc-group-char (ch)
142 (interactive "cGrouping character: ")
145 (error "Control characters not allowed for grouping"))
148 (setq ch (char-to-string ch)))
149 (calc-change-mode 'calc-group-char ch calc-group-digits)
150 (message "Digit grouping character is \"%s\"" ch)))
152 (defun calc-point-char (ch)
153 (interactive "cCharacter to use as decimal point: ")
156 (error "Control characters not allowed as decimal point"))
157 (calc-change-mode 'calc-point-char (char-to-string ch) t)
158 (message "Decimal point character is \"%c\"" ch)))
160 (defun calc-normal-notation (n)
163 (calc-change-mode 'calc-float-format
164 (let* ((val (if n (prefix-numeric-value n) 0))
165 (mode (/ (+ val 5000) 10000)))
166 (if (or (< val -5000) (> mode 3))
167 (error "Prefix out of range"))
168 (setq n (list (aref [float sci eng fix] mode)
169 (- (% (+ val 5000) 10000) 5000))))
172 (message "Displaying floating-point numbers normally")
175 "Displaying floating-point numbers with %d significant digits"
177 (message "Displaying floating-point numbers with (precision%d)"
180 (defun calc-fix-notation (n)
181 (interactive "NDigits after decimal point: ")
183 (calc-change-mode 'calc-float-format
184 (setq n (list 'fix (if n (prefix-numeric-value n) 0)))
186 (message "Displaying floats with %d digits after decimal"
187 (math-abs (nth 1 n)))))
189 (defun calc-sci-notation (n)
192 (calc-change-mode 'calc-float-format
193 (setq n (list 'sci (if n (prefix-numeric-value n) 0)))
196 (message "Displaying floats in scientific notation")
198 (message "Displaying scientific notation with %d significant digits"
200 (message "Displaying scientific notation with (precision%d)"
203 (defun calc-eng-notation (n)
206 (calc-change-mode 'calc-float-format
207 (setq n (list 'eng (if n (prefix-numeric-value n) 0)))
210 (message "Displaying floats in engineering notation")
212 (message "Displaying engineering notation with %d significant digits"
214 (message "Displaying engineering notation with (precision%d)"
218 (defun calc-truncate-stack (n &optional rel)
221 (let ((oldtop calc-stack-top)
222 (newtop calc-stack-top))
223 (calc-record-undo (list 'set 'saved-stack-top calc-stack-top))
224 (let ((calc-stack-top 0)
225 (nn (prefix-numeric-value n)))
230 (setq nn (+ oldtop nn))
232 (setq nn (+ nn (calc-stack-size)))
236 (if (> nn (calc-stack-size))
239 (max 1 (calc-locate-cursor-element (point)))))
240 (if (= newtop oldtop)
242 (calc-pop-stack 1 oldtop t)
243 (calc-push-list '(top-of-stack) newtop)
244 (if calc-line-numbering
246 (calc-record-undo (list 'set 'saved-stack-top 0))
247 (setq calc-stack-top newtop))))
249 (defun calc-truncate-up (n)
251 (calc-truncate-stack n t))
253 (defun calc-truncate-down (n)
255 (calc-truncate-stack (- n) t))
257 (defun calc-display-raw (arg)
260 (setq calc-display-raw (if calc-display-raw nil (if arg 0 t)))
263 (message "Press d ' again to cancel \"raw\" display mode"))))
270 (defun calc-save-modes ()
274 (vals (mapcar (function (lambda (v) (symbol-value (car v))))
275 calc-mode-var-list)))
276 (unless calc-settings-file
277 (error "No `calc-settings-file' specified"))
278 (set-buffer (find-file-noselect (substitute-in-file-name
279 calc-settings-file)))
280 (goto-char (point-min))
281 (if (and (search-forward ";;; Mode settings stored by Calc" nil t)
285 (search-forward "\n;;; End of mode settings" nil t)))
289 (delete-region pos (point)))
290 (goto-char (point-max))
293 (insert ";;; Mode settings stored by Calc on " (current-time-string) "\n")
294 (let ((list calc-mode-var-list))
296 (let* ((v (car (car list)))
297 (def (nth 1 (car list)))
301 (insert "(setq " (symbol-name v) " ")
302 (if (and (or (listp val)
304 (not (memq val '(nil t))))
306 (insert (prin1-to-string val) ")\n"))))
307 (setq list (cdr list)
309 (run-hooks 'calc-mode-save-hook)
310 (insert ";;; End of mode settings\n")
313 (defun calc-settings-file-name (name &optional arg)
315 (list (read-file-name (format "Settings file name (normally %s): "
316 (abbreviate-file-name (or user-init-file
320 (setq arg (if arg (prefix-numeric-value arg) 0))
322 (message "Calc settings file is \"%s\"" calc-settings-file)
323 (if (< (math-abs arg) 2)
324 (let ((list calc-mode-var-list))
326 (set (car (car list)) (nth 1 (car list)))
327 (setq list (cdr list)))))
328 ;; FIXME: we should use ~/.calc or so in order to avoid
329 ;; reexecuting ~/.emacs (it's not always idempotent) -cgw 2001.11.12
330 (setq calc-settings-file name)
333 (string-match "\\.emacs" 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)
361 (cond ((eq calc-simplify-mode 'none) -1)
362 ((eq calc-simplify-mode 'num) 0)
363 ((eq calc-simplify-mode 'binary) 2)
364 ((eq calc-simplify-mode 'alg) 3)
365 ((eq calc-simplify-mode 'ext) 4)
366 ((eq calc-simplify-mode 'units) 5)
368 (cond ((eq calc-infinite-mode 1) 0)
369 (calc-infinite-mode 1)
372 (defun calc-get-modes (n)
375 (let ((modes (math-get-modes-vec)))
376 (calc-enter-result 0 "mode"
378 (if (and (>= (setq n (prefix-numeric-value n)) 1)
379 (< n (length modes)))
381 (error "Prefix out of range"))
384 (defun calc-shift-prefix (arg)
387 (setq calc-shift-prefix (if arg
388 (> (prefix-numeric-value arg) 0)
389 (not calc-shift-prefix)))
391 (message (if calc-shift-prefix
392 "Prefix keys are now case-insensitive"
393 "Prefix keys must be unshifted (except V, Z)"))))
395 (defun calc-mode-record-mode (n)
398 (calc-change-mode 'calc-mode-save-mode
400 (cond ((not calc-embedded-info)
401 (if (eq calc-mode-save-mode 'save)
403 ((eq calc-mode-save-mode 'local) 'edit)
404 ((eq calc-mode-save-mode 'edit) 'perm)
405 ((eq calc-mode-save-mode 'perm) 'global)
406 ((eq calc-mode-save-mode 'global) 'save)
407 ((eq calc-mode-save-mode 'save) nil)
408 ((eq calc-mode-save-mode nil) 'local)))
409 ((= (setq n (prefix-numeric-value n)) 0) nil)
415 (message (cond ((and (eq calc-mode-save-mode 'local) calc-embedded-info)
416 "Recording mode changes with [calc-mode: ...]")
417 ((eq calc-mode-save-mode 'edit)
418 "Recording mode changes with [calc-edit-mode: ...]")
419 ((eq calc-mode-save-mode 'perm)
420 "Recording mode changes with [calc-perm-mode: ...]")
421 ((eq calc-mode-save-mode 'global)
422 "Recording mode changes with [calc-global-mode: ...]")
423 ((eq calc-mode-save-mode 'save)
424 (format "Recording mode changes in \"%s\""
427 "Not recording mode changes permanently")))))
429 (defun calc-total-algebraic-mode (flag)
431 (if calc-emacs-type-19
432 (error "Total algebraic mode not yet supported for Emacs 19"))
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))
484 (arg (prefix-numeric-value arg))
485 ((eq calc-matrix-mode 'matrix) 'scalar)
486 ((eq calc-matrix-mode 'scalar) nil)
488 (if (integerp calc-matrix-mode)
489 (message "Variables are assumed to be %dx%d matrices"
490 calc-matrix-mode calc-matrix-mode)
491 (message (if (eq calc-matrix-mode 'matrix)
492 "Variables are assumed to be matrices"
494 "Variables are assumed to be scalars (non-matrices)"
495 "Variables are not assumed to be matrix or scalar"))))))
497 (defun calc-set-simplify-mode (mode arg msg)
498 (calc-change-mode 'calc-simplify-mode
500 (and (> (prefix-numeric-value arg) 0)
502 (and (not (eq calc-simplify-mode mode))
504 (message (if (eq calc-simplify-mode mode)
506 "Default simplifications enabled")))
508 (defun calc-no-simplify-mode (arg)
511 (calc-set-simplify-mode 'none arg
512 "All default simplifications are disabled")))
514 (defun calc-num-simplify-mode (arg)
517 (calc-set-simplify-mode 'num arg
518 "Default simplifications apply only if arguments are numeric")))
520 (defun calc-default-simplify-mode (arg)
524 (calc-set-simplify-mode
525 nil nil "Usual default simplifications are enabled")))
526 ((= arg 0) (calc-num-simplify-mode 1))
527 ((< arg 0) (calc-no-simplify-mode 1))
528 ((= arg 2) (calc-bin-simplify-mode 1))
529 ((= arg 3) (calc-alg-simplify-mode 1))
530 ((= arg 4) (calc-ext-simplify-mode 1))
531 ((= arg 5) (calc-units-simplify-mode 1))
532 (t (error "Prefix argument out of range"))))
534 (defun calc-bin-simplify-mode (arg)
537 (calc-set-simplify-mode 'binary arg
538 (format "Binary simplification occurs by default (word size=%d)"
541 (defun calc-alg-simplify-mode (arg)
544 (calc-set-simplify-mode 'alg arg
545 "Algebraic simplification occurs by default")))
547 (defun calc-ext-simplify-mode (arg)
550 (calc-set-simplify-mode 'ext arg
551 "Extended algebraic simplification occurs by default")))
553 (defun calc-units-simplify-mode (arg)
556 (calc-set-simplify-mode 'units arg
557 "Units simplification occurs by default")))
559 (defun calc-auto-recompute (arg)
562 (calc-change-mode 'calc-auto-recompute arg nil t)
563 (calc-refresh-evaltos)
564 (message (if calc-auto-recompute
565 "Automatically recomputing `=>' forms when necessary"
566 "Not recomputing `=>' forms automatically"))))
568 (defun calc-working (n)
572 (calc-pop-push-record 0 "work"
573 (cond ((eq calc-display-working-message t) 1)
574 (calc-display-working-message 2)
576 ((eq n 2) (calc-change-mode 'calc-display-working-message 'lots))
577 ((eq n 0) (calc-change-mode 'calc-display-working-message nil))
578 ((eq n 1) (calc-change-mode 'calc-display-working-message t)))
579 (cond ((eq calc-display-working-message t)
580 (message "\"Working...\" messages enabled"))
581 (calc-display-working-message
582 (message "Detailed \"Working...\" messages enabled"))
584 (message "\"Working...\" messages disabled")))))
586 (defun calc-always-load-extensions ()
589 (if (setq calc-always-load-extensions (not calc-always-load-extensions))
590 (message "Always loading extensions package")
591 (message "Loading extensions package on demand only"))))
594 (defun calc-matrix-left-justify ()
597 (calc-change-mode 'calc-matrix-just nil t)
598 (message "Matrix elements will be left-justified in columns")))
600 (defun calc-matrix-center-justify ()
603 (calc-change-mode 'calc-matrix-just 'center t)
604 (message "Matrix elements will be centered in columns")))
606 (defun calc-matrix-right-justify ()
609 (calc-change-mode 'calc-matrix-just 'right t)
610 (message "Matrix elements will be right-justified in columns")))
612 (defun calc-full-vectors (n)
615 (message (if (calc-change-mode 'calc-full-vectors n t t)
616 "Displaying long vectors in full"
617 "Displaying long vectors in [a, b, c, ..., z] notation"))))
619 (defun calc-full-trail-vectors (n)
622 (message (if (calc-change-mode 'calc-full-trail-vectors n nil t)
623 "Recording long vectors in full"
624 "Recording long vectors in [a, b, c, ..., z] notation"))))
626 (defun calc-break-vectors (n)
629 (message (if (calc-change-mode 'calc-break-vectors n t t)
630 "Displaying vector elements one-per-line"
631 "Displaying vector elements all on one line"))))
633 (defun calc-vector-commas ()
636 (if (calc-change-mode 'calc-vector-commas (if calc-vector-commas nil ",") t)
637 (message "Separating vector elements with \",\"")
638 (message "Separating vector elements with spaces"))))
640 (defun calc-vector-brackets ()
643 (if (calc-change-mode 'calc-vector-brackets
644 (if (equal calc-vector-brackets "[]") nil "[]") t)
645 (message "Surrounding vectors with \"[]\"")
646 (message "Not surrounding vectors with brackets"))))
648 (defun calc-vector-braces ()
651 (if (calc-change-mode 'calc-vector-brackets
652 (if (equal calc-vector-brackets "{}") nil "{}") t)
653 (message "Surrounding vectors with \"{}\"")
654 (message "Not surrounding vectors with brackets"))))
656 (defun calc-vector-parens ()
659 (if (calc-change-mode 'calc-vector-brackets
660 (if (equal calc-vector-brackets "()") nil "()") t)
661 (message "Surrounding vectors with \"()\"")
662 (message "Not surrounding vectors with brackets"))))
664 (defun calc-matrix-brackets (arg)
665 (interactive "sCode letters (R, O, C, P): ")
667 (let ((code (append (and (string-match "[rR]" arg) '(R))
668 (and (string-match "[oO]" arg) '(O))
669 (and (string-match "[cC]" arg) '(C))
670 (and (string-match "[pP]" arg) '(P))))
671 (bad (string-match "[^rRoOcCpP ]" arg)))
673 (error "Unrecognized character: %c" (aref arg bad)))
674 (calc-change-mode 'calc-matrix-brackets code t))))
676 ;;; calc-mode.el ends here