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: Jay Belanger <belanger@truman.edu>
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.
29 ;; This file is autoloaded from calc-ext.el.
34 (defun calc-line-numbering (n)
37 (message (if (calc-change-mode 'calc-line-numbering n t t)
38 "Displaying stack level numbers"
39 "Hiding stack level numbers"))))
41 (defun calc-line-breaking (n)
45 (and (> (setq n (prefix-numeric-value n)) 0)
48 (not calc-line-breaking)))
49 (if (calc-change-mode 'calc-line-breaking n t)
50 (if (integerp calc-line-breaking)
51 (message "Breaking lines longer than %d characters" n)
52 (message "Breaking long lines in Stack display"))
53 (message "Not breaking long lines in Stack display"))))
56 (defun calc-left-justify (n)
59 (and n (setq n (prefix-numeric-value n)))
60 (calc-change-mode '(calc-display-just calc-display-origin)
63 (message "Displaying stack entries indented by %d" n)
64 (message "Displaying stack entries left-justified"))))
66 (defun calc-center-justify (n)
69 (and n (setq n (prefix-numeric-value n)))
70 (calc-change-mode '(calc-display-just calc-display-origin)
73 (message "Displaying stack entries centered on column %d" n)
74 (message "Displaying stack entries centered in window"))))
76 (defun calc-right-justify (n)
79 (and n (setq n (prefix-numeric-value n)))
80 (calc-change-mode '(calc-display-just calc-display-origin)
83 (message "Displaying stack entries right-justified to column %d" n)
84 (message "Displaying stack entries right-justified in window"))))
86 (defun calc-left-label (s)
87 (interactive "sLefthand label: ")
90 (setq s (concat s " ")))
91 (calc-change-mode 'calc-left-label s t)))
93 (defun calc-right-label (s)
94 (interactive "sRighthand label: ")
97 (setq s (concat " " s)))
98 (calc-change-mode 'calc-right-label s t)))
100 (defun calc-auto-why (n)
105 (setq n (prefix-numeric-value n))
106 (if (<= n 0) (setq n nil)
107 (if (> n 1) (setq n t))))
108 (setq n (and (not (eq calc-auto-why t)) (if calc-auto-why t 1))))
109 (calc-change-mode 'calc-auto-why n nil)
111 (message "User must press `w' to explain unsimplified results"))
113 (message "Automatically doing `w' to explain unsimplified results"))
115 (message "Automatically doing `w' only for unusual messages")))))
117 (defun calc-group-digits (n)
122 (setq n (prefix-numeric-value n))
123 (cond ((or (> n 0) (< n -1)))
127 (setq n calc-group-digits))))
128 (setq n (not calc-group-digits)))
129 (calc-change-mode 'calc-group-digits n t)
131 (message "Grouping is off"))
133 (message "Grouping every %d digits" (math-abs n)))
135 (message "Grouping is on")))))
137 (defun calc-group-char (ch)
138 (interactive "cGrouping character: ")
141 (error "Control characters not allowed for grouping"))
144 (setq ch (char-to-string ch)))
145 (calc-change-mode 'calc-group-char ch calc-group-digits)
146 (message "Digit grouping character is \"%s\"" ch)))
148 (defun calc-point-char (ch)
149 (interactive "cCharacter to use as decimal point: ")
152 (error "Control characters not allowed as decimal point"))
153 (calc-change-mode 'calc-point-char (char-to-string ch) t)
154 (message "Decimal point character is \"%c\"" ch)))
156 (defun calc-normal-notation (n)
159 (calc-change-mode 'calc-float-format
160 (let* ((val (if n (prefix-numeric-value n) 0))
161 (mode (/ (+ val 5000) 10000)))
162 (if (or (< val -5000) (> mode 3))
163 (error "Prefix out of range"))
164 (setq n (list (aref [float sci eng fix] mode)
165 (- (% (+ val 5000) 10000) 5000))))
168 (message "Displaying floating-point numbers normally")
171 "Displaying floating-point numbers with %d significant digits"
173 (message "Displaying floating-point numbers with (precision%d)"
176 (defun calc-fix-notation (n)
177 (interactive "NDigits after decimal point: ")
179 (calc-change-mode 'calc-float-format
180 (setq n (list 'fix (if n (prefix-numeric-value n) 0)))
182 (message "Displaying floats with %d digits after decimal"
183 (math-abs (nth 1 n)))))
185 (defun calc-sci-notation (n)
188 (calc-change-mode 'calc-float-format
189 (setq n (list 'sci (if n (prefix-numeric-value n) 0)))
192 (message "Displaying floats in scientific notation")
194 (message "Displaying scientific notation with %d significant digits"
196 (message "Displaying scientific notation with (precision%d)"
199 (defun calc-eng-notation (n)
202 (calc-change-mode 'calc-float-format
203 (setq n (list 'eng (if n (prefix-numeric-value n) 0)))
206 (message "Displaying floats in engineering notation")
208 (message "Displaying engineering notation with %d significant digits"
210 (message "Displaying engineering notation with (precision%d)"
214 (defun calc-truncate-stack (n &optional rel)
217 (let ((oldtop calc-stack-top)
218 (newtop calc-stack-top))
219 (calc-record-undo (list 'set 'saved-stack-top calc-stack-top))
220 (let ((calc-stack-top 0)
221 (nn (prefix-numeric-value n)))
226 (setq nn (+ oldtop nn))
228 (setq nn (+ nn (calc-stack-size)))
232 (if (> nn (calc-stack-size))
235 (max 1 (calc-locate-cursor-element (point)))))
236 (if (= newtop oldtop)
238 (calc-pop-stack 1 oldtop t)
239 (calc-push-list '(top-of-stack) newtop)
240 (if calc-line-numbering
242 (calc-record-undo (list 'set 'saved-stack-top 0))
243 (setq calc-stack-top newtop))))
245 (defun calc-truncate-up (n)
247 (calc-truncate-stack n t))
249 (defun calc-truncate-down (n)
251 (calc-truncate-stack (- n) t))
253 (defun calc-display-raw (arg)
256 (setq calc-display-raw (if calc-display-raw nil (if arg 0 t)))
259 (message "Press d ' again to cancel \"raw\" display mode"))))
266 (defun calc-save-modes ()
270 (vals (mapcar (function (lambda (v) (symbol-value (car v))))
271 calc-mode-var-list)))
272 (unless calc-settings-file
273 (error "No `calc-settings-file' specified"))
274 (set-buffer (find-file-noselect (substitute-in-file-name
275 calc-settings-file)))
276 (goto-char (point-min))
277 (if (and (search-forward ";;; Mode settings stored by Calc" nil t)
281 (search-forward "\n;;; End of mode settings" nil t)))
285 (delete-region pos (point)))
286 (goto-char (point-max))
289 (insert ";;; Mode settings stored by Calc on " (current-time-string) "\n")
290 (let ((list calc-mode-var-list))
292 (let* ((v (car (car list)))
293 (def (nth 1 (car list)))
297 (insert "(setq " (symbol-name v) " ")
298 (if (and (or (listp val)
300 (not (memq val '(nil t))))
302 (insert (prin1-to-string val) ")\n"))))
303 (setq list (cdr list)
305 (run-hooks 'calc-mode-save-hook)
306 (insert ";;; End of mode settings\n")
309 (defun calc-settings-file-name (name &optional arg)
311 (list (read-file-name (format "Settings file name (normally %s): "
312 (abbreviate-file-name (or user-init-file
316 (setq arg (if arg (prefix-numeric-value arg) 0))
318 (message "Calc settings file is \"%s\"" calc-settings-file)
319 (if (< (math-abs arg) 2)
320 (let ((list calc-mode-var-list))
322 (set (car (car list)) (nth 1 (car list)))
323 (setq list (cdr list)))))
324 ;; FIXME: we should use ~/.calc or so in order to avoid
325 ;; reexecuting ~/.emacs (it's not always idempotent) -cgw 2001.11.12
326 (setq calc-settings-file name)
329 (string-match "\\.emacs" calc-settings-file)
333 (message "New file")))))
335 (defun math-get-modes-vec ()
341 (+ (if (<= (nth 1 calc-float-format) 0)
342 (+ calc-internal-prec (nth 1 calc-float-format))
343 (nth 1 calc-float-format))
344 (cdr (assq (car calc-float-format)
345 '((float . 0) (sci . 10000)
346 (eng . 20000) (fix . 30000)))))
347 (cond ((eq calc-angle-mode 'rad) 2)
348 ((eq calc-angle-mode 'hms) 3)
350 (if calc-symbolic-mode 1 0)
351 (if calc-prefer-frac 1 0)
352 (if (eq calc-complex-mode 'polar) 1 0)
353 (cond ((eq calc-matrix-mode 'scalar) 0)
354 ((eq calc-matrix-mode 'matrix) -2)
357 (cond ((eq calc-simplify-mode 'none) -1)
358 ((eq calc-simplify-mode 'num) 0)
359 ((eq calc-simplify-mode 'binary) 2)
360 ((eq calc-simplify-mode 'alg) 3)
361 ((eq calc-simplify-mode 'ext) 4)
362 ((eq calc-simplify-mode 'units) 5)
364 (cond ((eq calc-infinite-mode 1) 0)
365 (calc-infinite-mode 1)
368 (defun calc-get-modes (n)
371 (let ((modes (math-get-modes-vec)))
372 (calc-enter-result 0 "mode"
374 (if (and (>= (setq n (prefix-numeric-value n)) 1)
375 (< n (length modes)))
377 (error "Prefix out of range"))
380 (defun calc-shift-prefix (arg)
383 (setq calc-shift-prefix (if arg
384 (> (prefix-numeric-value arg) 0)
385 (not calc-shift-prefix)))
387 (message (if calc-shift-prefix
388 "Prefix keys are now case-insensitive"
389 "Prefix keys must be unshifted (except V, Z)"))))
391 (defun calc-mode-record-mode (n)
394 (calc-change-mode 'calc-mode-save-mode
396 (cond ((not calc-embedded-info)
397 (if (eq calc-mode-save-mode 'save)
399 ((eq calc-mode-save-mode 'local) 'edit)
400 ((eq calc-mode-save-mode 'edit) 'perm)
401 ((eq calc-mode-save-mode 'perm) 'global)
402 ((eq calc-mode-save-mode 'global) 'save)
403 ((eq calc-mode-save-mode 'save) nil)
404 ((eq calc-mode-save-mode nil) 'local)))
405 ((= (setq n (prefix-numeric-value n)) 0) nil)
411 (message (cond ((and (eq calc-mode-save-mode 'local) calc-embedded-info)
412 "Recording mode changes with [calc-mode: ...]")
413 ((eq calc-mode-save-mode 'edit)
414 "Recording mode changes with [calc-edit-mode: ...]")
415 ((eq calc-mode-save-mode 'perm)
416 "Recording mode changes with [calc-perm-mode: ...]")
417 ((eq calc-mode-save-mode 'global)
418 "Recording mode changes with [calc-global-mode: ...]")
419 ((eq calc-mode-save-mode 'save)
420 (format "Recording mode changes in \"%s\""
423 "Not recording mode changes permanently")))))
425 (defun calc-total-algebraic-mode (flag)
428 (if (eq calc-algebraic-mode 'total)
429 (calc-algebraic-mode nil)
430 (calc-change-mode '(calc-algebraic-mode calc-incomplete-algebraic-mode)
432 (use-local-map calc-alg-map)
434 "All keys begin algebraic entry; use Meta (ESC) for Calc keys"))))
436 (defun calc-algebraic-mode (flag)
440 (calc-change-mode '(calc-algebraic-mode
441 calc-incomplete-algebraic-mode)
442 (list nil (not calc-incomplete-algebraic-mode)))
443 (calc-change-mode '(calc-algebraic-mode calc-incomplete-algebraic-mode)
444 (list (not calc-algebraic-mode) nil)))
445 (use-local-map calc-mode-map)
446 (message (if calc-algebraic-mode
447 "Numeric keys and ( and [ begin algebraic entry"
448 (if calc-incomplete-algebraic-mode
449 "Only ( and [ begin algebraic entry"
450 "No keys except ' and $ begin algebraic entry")))))
452 (defun calc-symbolic-mode (n)
456 (message (if (calc-change-mode 'calc-symbolic-mode n nil t)
457 "Inexact computations like sqrt(2) are deferred"
458 "Numerical computations are always done immediately"))))
460 (defun calc-infinite-mode (n)
465 (calc-change-mode 'calc-infinite-mode 1)
466 (message "Computations like 1 / 0 produce \"inf\""))
467 (message (if (calc-change-mode 'calc-infinite-mode n nil t)
468 "Computations like 1 / 0 produce \"uinf\""
469 "Computations like 1 / 0 are left unsimplified")))))
471 (defun calc-matrix-mode (arg)
474 (calc-change-mode 'calc-matrix-mode
475 (cond ((eq arg 0) 'scalar)
476 ((< (prefix-numeric-value arg) 1)
477 (and (< (prefix-numeric-value arg) -1) 'matrix))
478 (arg (prefix-numeric-value arg))
479 ((eq calc-matrix-mode 'matrix) 'scalar)
480 ((eq calc-matrix-mode 'scalar) nil)
482 (if (integerp calc-matrix-mode)
483 (message "Variables are assumed to be %dx%d matrices"
484 calc-matrix-mode calc-matrix-mode)
485 (message (if (eq calc-matrix-mode 'matrix)
486 "Variables are assumed to be matrices"
488 "Variables are assumed to be scalars (non-matrices)"
489 "Variables are not assumed to be matrix or scalar"))))))
491 (defun calc-set-simplify-mode (mode arg msg)
492 (calc-change-mode 'calc-simplify-mode
494 (and (> (prefix-numeric-value arg) 0)
496 (and (not (eq calc-simplify-mode mode))
498 (message (if (eq calc-simplify-mode mode)
500 "Default simplifications enabled")))
502 (defun calc-no-simplify-mode (arg)
505 (calc-set-simplify-mode 'none arg
506 "All default simplifications are disabled")))
508 (defun calc-num-simplify-mode (arg)
511 (calc-set-simplify-mode 'num arg
512 "Default simplifications apply only if arguments are numeric")))
514 (defun calc-default-simplify-mode (arg)
518 (calc-set-simplify-mode
519 nil nil "Usual default simplifications are enabled")))
520 ((= arg 0) (calc-num-simplify-mode 1))
521 ((< arg 0) (calc-no-simplify-mode 1))
522 ((= arg 2) (calc-bin-simplify-mode 1))
523 ((= arg 3) (calc-alg-simplify-mode 1))
524 ((= arg 4) (calc-ext-simplify-mode 1))
525 ((= arg 5) (calc-units-simplify-mode 1))
526 (t (error "Prefix argument out of range"))))
528 (defun calc-bin-simplify-mode (arg)
531 (calc-set-simplify-mode 'binary arg
532 (format "Binary simplification occurs by default (word size=%d)"
535 (defun calc-alg-simplify-mode (arg)
538 (calc-set-simplify-mode 'alg arg
539 "Algebraic simplification occurs by default")))
541 (defun calc-ext-simplify-mode (arg)
544 (calc-set-simplify-mode 'ext arg
545 "Extended algebraic simplification occurs by default")))
547 (defun calc-units-simplify-mode (arg)
550 (calc-set-simplify-mode 'units arg
551 "Units simplification occurs by default")))
553 (defun calc-auto-recompute (arg)
556 (calc-change-mode 'calc-auto-recompute arg nil t)
557 (calc-refresh-evaltos)
558 (message (if calc-auto-recompute
559 "Automatically recomputing `=>' forms when necessary"
560 "Not recomputing `=>' forms automatically"))))
562 (defun calc-working (n)
566 (calc-pop-push-record 0 "work"
567 (cond ((eq calc-display-working-message t) 1)
568 (calc-display-working-message 2)
570 ((eq n 2) (calc-change-mode 'calc-display-working-message 'lots))
571 ((eq n 0) (calc-change-mode 'calc-display-working-message nil))
572 ((eq n 1) (calc-change-mode 'calc-display-working-message t)))
573 (cond ((eq calc-display-working-message t)
574 (message "\"Working...\" messages enabled"))
575 (calc-display-working-message
576 (message "Detailed \"Working...\" messages enabled"))
578 (message "\"Working...\" messages disabled")))))
580 (defun calc-always-load-extensions ()
583 (if (setq calc-always-load-extensions (not calc-always-load-extensions))
584 (message "Always loading extensions package")
585 (message "Loading extensions package on demand only"))))
588 (defun calc-matrix-left-justify ()
591 (calc-change-mode 'calc-matrix-just nil t)
592 (message "Matrix elements will be left-justified in columns")))
594 (defun calc-matrix-center-justify ()
597 (calc-change-mode 'calc-matrix-just 'center t)
598 (message "Matrix elements will be centered in columns")))
600 (defun calc-matrix-right-justify ()
603 (calc-change-mode 'calc-matrix-just 'right t)
604 (message "Matrix elements will be right-justified in columns")))
606 (defun calc-full-vectors (n)
609 (message (if (calc-change-mode 'calc-full-vectors n t t)
610 "Displaying long vectors in full"
611 "Displaying long vectors in [a, b, c, ..., z] notation"))))
613 (defun calc-full-trail-vectors (n)
616 (message (if (calc-change-mode 'calc-full-trail-vectors n nil t)
617 "Recording long vectors in full"
618 "Recording long vectors in [a, b, c, ..., z] notation"))))
620 (defun calc-break-vectors (n)
623 (message (if (calc-change-mode 'calc-break-vectors n t t)
624 "Displaying vector elements one-per-line"
625 "Displaying vector elements all on one line"))))
627 (defun calc-vector-commas ()
630 (if (calc-change-mode 'calc-vector-commas (if calc-vector-commas nil ",") t)
631 (message "Separating vector elements with \",\"")
632 (message "Separating vector elements with spaces"))))
634 (defun calc-vector-brackets ()
637 (if (calc-change-mode 'calc-vector-brackets
638 (if (equal calc-vector-brackets "[]") nil "[]") t)
639 (message "Surrounding vectors with \"[]\"")
640 (message "Not surrounding vectors with brackets"))))
642 (defun calc-vector-braces ()
645 (if (calc-change-mode 'calc-vector-brackets
646 (if (equal calc-vector-brackets "{}") nil "{}") t)
647 (message "Surrounding vectors with \"{}\"")
648 (message "Not surrounding vectors with brackets"))))
650 (defun calc-vector-parens ()
653 (if (calc-change-mode 'calc-vector-brackets
654 (if (equal calc-vector-brackets "()") nil "()") t)
655 (message "Surrounding vectors with \"()\"")
656 (message "Not surrounding vectors with brackets"))))
658 (defun calc-matrix-brackets (arg)
659 (interactive "sCode letters (R, O, C, P): ")
661 (let ((code (append (and (string-match "[rR]" arg) '(R))
662 (and (string-match "[oO]" arg) '(O))
663 (and (string-match "[cC]" arg) '(C))
664 (and (string-match "[pP]" arg) '(P))))
665 (bad (string-match "[^rRoOcCpP ]" arg)))
667 (error "Unrecognized character: %c" (aref arg bad)))
668 (calc-change-mode 'calc-matrix-brackets code t))))
672 ;;; arch-tag: ecc70eea-c712-43f2-9085-4205e58d6ddf
673 ;;; calc-mode.el ends here