1 ;;; calc-mode.el --- calculator modes for Calc
3 ;; Copyright (C) 1990-1993, 2001-2018 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 <https://www.gnu.org/licenses/>.
26 ;; This file is autoloaded from calc-ext.el.
31 ;; Declare functions which are defined elsewhere.
32 (declare-function calc-embedded-save-original-modes
"calc-embed" ())
35 (defun calc-line-numbering (n)
38 (message (if (calc-change-mode 'calc-line-numbering n t t
)
39 "Displaying stack level numbers"
40 "Hiding stack level numbers"))))
42 (defun calc-line-breaking (n)
46 (and (> (setq n
(prefix-numeric-value n
)) 0)
49 (not calc-line-breaking
)))
50 (if (calc-change-mode 'calc-line-breaking n t
)
51 (if (integerp calc-line-breaking
)
52 (message "Breaking lines longer than %d characters" n
)
53 (message "Breaking long lines in Stack display"))
54 (message "Not breaking long lines in Stack display"))))
57 (defun calc-left-justify (n)
60 (and n
(setq n
(prefix-numeric-value n
)))
61 (calc-change-mode '(calc-display-just calc-display-origin
)
64 (message "Displaying stack entries indented by %d" n
)
65 (message "Displaying stack entries left-justified"))))
67 (defun calc-center-justify (n)
70 (and n
(setq n
(prefix-numeric-value n
)))
71 (calc-change-mode '(calc-display-just calc-display-origin
)
74 (message "Displaying stack entries centered on column %d" n
)
75 (message "Displaying stack entries centered in window"))))
77 (defun calc-right-justify (n)
80 (and n
(setq n
(prefix-numeric-value n
)))
81 (calc-change-mode '(calc-display-just calc-display-origin
)
84 (message "Displaying stack entries right-justified to column %d" n
)
85 (message "Displaying stack entries right-justified in window"))))
87 (defun calc-left-label (s)
88 (interactive "sLefthand label: ")
91 (setq s
(concat s
" ")))
92 (calc-change-mode 'calc-left-label s t
)))
94 (defun calc-right-label (s)
95 (interactive "sRighthand label: ")
98 (setq s
(concat " " s
)))
99 (calc-change-mode 'calc-right-label s t
)))
101 (defun calc-auto-why (n)
106 (setq n
(prefix-numeric-value n
))
107 (if (<= n
0) (setq n nil
)
108 (if (> n
1) (setq n t
))))
109 (setq n
(and (not (eq calc-auto-why t
)) (if calc-auto-why t
1))))
110 (calc-change-mode 'calc-auto-why n nil
)
112 (message "User must press `w' to explain unsimplified results"))
114 (message "Automatically doing `w' to explain unsimplified results"))
116 (message "Automatically doing `w' only for unusual messages")))))
118 (defun calc-group-digits (n)
123 (setq n
(prefix-numeric-value n
))
124 (cond ((or (> n
0) (< n -
1)))
128 (setq n calc-group-digits
))))
129 (setq n
(not calc-group-digits
)))
130 (calc-change-mode 'calc-group-digits n t
)
132 (message "Grouping is off"))
134 (message "Grouping every %d digits" (math-abs n
)))
136 (message "Grouping is on")))))
138 (defun calc-group-char (ch)
139 (interactive "cGrouping character: ")
142 (error "Control characters not allowed for grouping"))
145 (setq ch
(char-to-string ch
)))
146 (calc-change-mode 'calc-group-char ch calc-group-digits
)
147 (message "Digit grouping character is \"%s\"" ch
)))
149 (defun calc-point-char (ch)
150 (interactive "cCharacter to use as decimal point: ")
153 (error "Control characters not allowed as decimal point"))
154 (calc-change-mode 'calc-point-char
(char-to-string ch
) t
)
155 (message "Decimal point character is \"%c\"" ch
)))
157 (defun calc-normal-notation (n)
160 (calc-change-mode 'calc-float-format
161 (let* ((val (if n
(prefix-numeric-value n
) 0))
162 (mode (/ (+ val
5000) 10000)))
163 (if (or (< val -
5000) (> mode
3))
164 (error "Prefix out of range"))
165 (setq n
(list (aref [float sci eng fix
] mode
)
166 (- (%
(+ val
5000) 10000) 5000))))
169 (message "Displaying floating-point numbers normally")
172 "Displaying floating-point numbers with %d significant digits"
174 (message "Displaying floating-point numbers with (precision%d)"
177 (defun calc-fix-notation (n)
178 (interactive "NDigits after decimal point: ")
180 (calc-change-mode 'calc-float-format
181 (setq n
(list 'fix
(if n
(prefix-numeric-value n
) 0)))
183 (message "Displaying floats with %d digits after decimal"
184 (math-abs (nth 1 n
)))))
186 (defun calc-sci-notation (n)
189 (calc-change-mode 'calc-float-format
190 (setq n
(list 'sci
(if n
(prefix-numeric-value n
) 0)))
193 (message "Displaying floats in scientific notation")
195 (message "Displaying scientific notation with %d significant digits"
197 (message "Displaying scientific notation with (precision%d)"
200 (defun calc-eng-notation (n)
203 (calc-change-mode 'calc-float-format
204 (setq n
(list 'eng
(if n
(prefix-numeric-value n
) 0)))
207 (message "Displaying floats in engineering notation")
209 (message "Displaying engineering notation with %d significant digits"
211 (message "Displaying engineering notation with (precision%d)"
215 (defun calc-truncate-stack (n &optional rel
)
218 (let ((oldtop calc-stack-top
)
219 (newtop calc-stack-top
))
220 (calc-record-undo (list 'set
'saved-stack-top calc-stack-top
))
221 (let ((calc-stack-top 0)
222 (nn (prefix-numeric-value n
)))
227 (setq nn
(+ oldtop nn
))
229 (setq nn
(+ nn
(calc-stack-size)))
233 (if (> nn
(calc-stack-size))
236 (max 1 (calc-locate-cursor-element (point)))))
237 (if (= newtop oldtop
)
239 (calc-pop-stack 1 oldtop t
)
240 (calc-push-list '(top-of-stack) newtop
)
241 (if calc-line-numbering
243 (calc-record-undo (list 'set
'saved-stack-top
0))
244 (setq calc-stack-top newtop
))))
246 (defun calc-truncate-up (n)
248 (calc-truncate-stack n t
))
250 (defun calc-truncate-down (n)
252 (calc-truncate-stack (- n
) t
))
254 (defun calc-display-raw (arg)
257 (setq calc-display-raw
(if calc-display-raw nil
(if arg
0 t
)))
260 (message "%s" "Press d ' again to cancel \"raw\" display mode"))))
267 (defun calc-save-modes ()
271 (vals (mapcar (function (lambda (v) (symbol-value (car v
))))
272 calc-mode-var-list
)))
273 (unless calc-settings-file
274 (error "No `calc-settings-file' specified"))
275 (set-buffer (find-file-noselect (substitute-in-file-name
276 calc-settings-file
)))
277 (goto-char (point-min))
278 (if (and (search-forward ";;; Mode settings stored by Calc" nil t
)
282 (search-forward "\n;;; End of mode settings" nil t
)))
286 (delete-region pos
(point)))
287 (goto-char (point-max))
290 (insert ";;; Mode settings stored by Calc on " (current-time-string) "\n")
291 (let ((list calc-mode-var-list
))
293 (let* ((v (car (car list
)))
294 (def (nth 1 (car list
)))
298 (insert "(setq " (symbol-name v
) " ")
299 (if (and (or (listp val
)
301 (not (memq val
'(nil t
))))
303 (insert (prin1-to-string val
) ")\n"))))
304 (setq list
(cdr list
)
306 (run-hooks 'calc-mode-save-hook
)
307 (insert ";;; End of mode settings\n")
309 (if calc-embedded-info
310 (calc-embedded-save-original-modes)))))
312 (defun calc-settings-file-name (name &optional arg
)
314 (list (read-file-name (format "Settings file name (normally %s): "
315 (abbreviate-file-name calc-settings-file
)))
318 (setq arg
(if arg
(prefix-numeric-value arg
) 0))
319 (if (string-equal (file-name-nondirectory name
) "")
320 (message "Calc settings file is \"%s\"" calc-settings-file
)
321 (if (< (math-abs arg
) 2)
322 (let ((list calc-mode-var-list
))
324 (set (car (car list
)) (nth 1 (car list
)))
325 (setq list
(cdr list
)))))
326 (setq calc-settings-file name
)
329 (equal user-init-file 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)
355 ((eq calc-matrix-mode
'sqmatrix
) -
3)
358 (cond ((eq calc-simplify-mode
'none
) -
1)
359 ((eq calc-simplify-mode
'num
) 0)
360 ((eq calc-simplify-mode
'binary
) 2)
361 ((eq calc-simplify-mode
'alg
) 3)
362 ((eq calc-simplify-mode
'ext
) 4)
363 ((eq calc-simplify-mode
'units
) 5)
365 (cond ((eq calc-infinite-mode
1) 0)
366 (calc-infinite-mode 1)
369 (defun calc-get-modes (n)
372 (let ((modes (math-get-modes-vec)))
373 (calc-enter-result 0 "mode"
375 (if (and (>= (setq n
(prefix-numeric-value n
)) 1)
376 (< n
(length modes
)))
378 (error "Prefix out of range"))
381 (defun calc-shift-prefix (arg)
384 (setq calc-shift-prefix
(if arg
385 (> (prefix-numeric-value arg
) 0)
386 (not calc-shift-prefix
)))
388 (message (if calc-shift-prefix
389 "Prefix keys are now case-insensitive"
390 "Prefix keys must be unshifted (except V, Z)"))))
392 (defun calc-mode-record-mode (n)
395 (calc-change-mode 'calc-mode-save-mode
397 (cond ((not calc-embedded-info
)
398 (if (eq calc-mode-save-mode
'save
)
400 ((eq calc-mode-save-mode
'local
) 'edit
)
401 ((eq calc-mode-save-mode
'edit
) 'perm
)
402 ((eq calc-mode-save-mode
'perm
) 'global
)
403 ((eq calc-mode-save-mode
'global
) 'save
)
404 ((eq calc-mode-save-mode
'save
) nil
)
405 ((eq calc-mode-save-mode nil
) 'local
)))
406 ((= (setq n
(prefix-numeric-value n
)) 0) nil
)
413 (cond ((and (eq calc-mode-save-mode
'local
) calc-embedded-info
)
414 "Recording mode changes with [calc-mode: ...]")
415 ((eq calc-mode-save-mode
'edit
)
416 "Recording mode changes with [calc-edit-mode: ...]")
417 ((eq calc-mode-save-mode
'perm
)
418 "Recording mode changes with [calc-perm-mode: ...]")
419 ((eq calc-mode-save-mode
'global
)
420 "Recording mode changes with [calc-global-mode: ...]")
421 ((eq calc-mode-save-mode
'save
)
422 (format "Recording mode changes in \"%s\""
425 "Not recording mode changes permanently")))))
427 (defun calc-total-algebraic-mode (flag)
430 (if (eq calc-algebraic-mode
'total
)
431 (calc-algebraic-mode nil
)
432 (calc-change-mode '(calc-algebraic-mode calc-incomplete-algebraic-mode
)
434 (use-local-map calc-alg-map
)
436 "All keys begin algebraic entry; use Meta (ESC) for Calc keys"))))
438 (defun calc-algebraic-mode (flag)
442 (calc-change-mode '(calc-algebraic-mode
443 calc-incomplete-algebraic-mode
)
444 (list nil
(not calc-incomplete-algebraic-mode
)))
445 (calc-change-mode '(calc-algebraic-mode calc-incomplete-algebraic-mode
)
446 (list (not calc-algebraic-mode
) nil
)))
447 (use-local-map calc-mode-map
)
448 (message "%s" (if calc-algebraic-mode
449 "Numeric keys and ( and [ begin algebraic entry"
450 (if calc-incomplete-algebraic-mode
451 "Only ( and [ begin algebraic entry"
452 "No keys except ' and $ begin algebraic entry")))))
454 (defun calc-symbolic-mode (n)
458 (message (if (calc-change-mode 'calc-symbolic-mode n nil t
)
459 "Inexact computations like sqrt(2) are deferred"
460 "Numerical computations are always done immediately"))))
462 (defun calc-infinite-mode (n)
467 (calc-change-mode 'calc-infinite-mode
1)
468 (message "Computations like 1 / 0 produce \"inf\""))
469 (message (if (calc-change-mode 'calc-infinite-mode n nil t
)
470 "Computations like 1 / 0 produce \"uinf\""
471 "Computations like 1 / 0 are left unsimplified")))))
473 (defun calc-matrix-mode (arg)
476 (calc-change-mode 'calc-matrix-mode
477 (cond ((eq arg
0) 'scalar
)
478 ((< (prefix-numeric-value arg
) 1)
479 (and (< (prefix-numeric-value arg
) -
1) 'matrix
))
481 (if (consp arg
) 'sqmatrix
482 (prefix-numeric-value arg
)))
483 ((eq calc-matrix-mode
'matrix
) 'scalar
)
484 ((eq calc-matrix-mode
'scalar
) nil
)
486 (if (integerp calc-matrix-mode
)
487 (message "Variables are assumed to be %dx%d matrices"
488 calc-matrix-mode calc-matrix-mode
)
489 (message (if (eq calc-matrix-mode
'matrix
)
490 "Variables are assumed to be matrices"
491 (if (eq calc-matrix-mode
'sqmatrix
)
492 "Variables are assumed to be square 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
501 ((eq calc-simplify-mode mode
)
504 (message "%s" (if (eq calc-simplify-mode mode
)
506 "Algebraic simplification occurs by default")))
508 (defun calc-no-simplify-mode (arg)
511 (calc-set-simplify-mode 'none arg
512 "Simplification is disabled")))
514 (defun calc-num-simplify-mode (arg)
517 (calc-set-simplify-mode 'num arg
518 "Basic simplifications apply only if arguments are numeric")))
520 (defun calc-default-simplify-mode (arg)
522 (cond ((or (not arg
) (= arg
3))
524 (calc-set-simplify-mode
525 'alg nil
"Algebraic simplification occurs by default")))
528 (calc-set-simplify-mode
529 nil nil
"Only basic simplifications occur by default")))
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
4) (calc-ext-simplify-mode 1))
534 ((= arg
5) (calc-units-simplify-mode 1))
535 (t (error "Prefix argument out of range"))))
537 (defun calc-bin-simplify-mode (arg)
540 (calc-set-simplify-mode 'binary arg
541 (format "Binary simplification occurs by default (word size=%d)"
544 (defun calc-basic-simplify-mode (arg)
547 (calc-set-simplify-mode nil arg
548 "Only basic simplifications occur by default")))
550 (defun calc-alg-simplify-mode (arg)
553 (calc-set-simplify-mode 'alg arg
554 "Algebraic simplification occurs by default")))
556 (defun calc-ext-simplify-mode (arg)
559 (calc-set-simplify-mode 'ext arg
560 "Extended algebraic simplification occurs by default")))
562 (defun calc-units-simplify-mode (arg)
565 (calc-set-simplify-mode 'units arg
566 "Units simplification occurs by default")))
568 (defun calc-auto-recompute (arg)
571 (calc-change-mode 'calc-auto-recompute arg nil t
)
572 (calc-refresh-evaltos)
573 (message (if calc-auto-recompute
574 "Automatically recomputing `=>' forms when necessary"
575 "Not recomputing `=>' forms automatically"))))
577 (defun calc-working (n)
581 (calc-pop-push-record 0 "work"
582 (cond ((eq calc-display-working-message t
) 1)
583 (calc-display-working-message 2)
585 ((eq n
2) (calc-change-mode 'calc-display-working-message
'lots
))
586 ((eq n
0) (calc-change-mode 'calc-display-working-message nil
))
587 ((eq n
1) (calc-change-mode 'calc-display-working-message t
)))
588 (cond ((eq calc-display-working-message t
)
589 (message "\"Working...\" messages enabled"))
590 (calc-display-working-message
591 (message "Detailed \"Working...\" messages enabled"))
593 (message "\"Working...\" messages disabled")))))
595 (defun calc-always-load-extensions ()
598 (if (setq calc-always-load-extensions
(not calc-always-load-extensions
))
599 (message "Always loading extensions package")
600 (message "Loading extensions package on demand only"))))
603 (defun calc-matrix-left-justify ()
606 (calc-change-mode 'calc-matrix-just nil t
)
607 (message "Matrix elements will be left-justified in columns")))
609 (defun calc-matrix-center-justify ()
612 (calc-change-mode 'calc-matrix-just
'center t
)
613 (message "Matrix elements will be centered in columns")))
615 (defun calc-matrix-right-justify ()
618 (calc-change-mode 'calc-matrix-just
'right t
)
619 (message "Matrix elements will be right-justified in columns")))
621 (defun calc-full-vectors (n)
624 (message (if (calc-change-mode 'calc-full-vectors n t t
)
625 "Displaying long vectors in full"
626 "Displaying long vectors in [a, b, c, ..., z] notation"))))
628 (defun calc-full-trail-vectors (n)
631 (message (if (calc-change-mode 'calc-full-trail-vectors n nil t
)
632 "Recording long vectors in full"
633 "Recording long vectors in [a, b, c, ..., z] notation"))))
635 (defun calc-break-vectors (n)
638 (message (if (calc-change-mode 'calc-break-vectors n t t
)
639 "Displaying vector elements one-per-line"
640 "Displaying vector elements all on one line"))))
642 (defun calc-vector-commas ()
645 (if (calc-change-mode 'calc-vector-commas
(if calc-vector-commas nil
",") t
)
646 (message "Separating vector elements with \",\"")
647 (message "Separating vector elements with spaces"))))
649 (defun calc-vector-brackets ()
652 (if (calc-change-mode 'calc-vector-brackets
653 (if (equal calc-vector-brackets
"[]") nil
"[]") t
)
654 (message "Surrounding vectors with \"[]\"")
655 (message "Not surrounding vectors with brackets"))))
657 (defun calc-vector-braces ()
660 (if (calc-change-mode 'calc-vector-brackets
661 (if (equal calc-vector-brackets
"{}") nil
"{}") t
)
662 (message "Surrounding vectors with \"{}\"")
663 (message "Not surrounding vectors with brackets"))))
665 (defun calc-vector-parens ()
668 (if (calc-change-mode 'calc-vector-brackets
669 (if (equal calc-vector-brackets
"()") nil
"()") t
)
670 (message "Surrounding vectors with \"()\"")
671 (message "Not surrounding vectors with brackets"))))
673 (defun calc-matrix-brackets (arg)
674 (interactive "sCode letters (R, O, C): ")
676 (let ((code (append (and (string-match "[rR]" arg
) '(R))
677 (and (string-match "[oO]" arg
) '(O))
678 (and (string-match "[cC]" arg
) '(C))
679 (and (string-match "[pP]" arg
) '(P))))
680 (bad (string-match "[^rRoOcCpP ]" arg
)))
682 (error "Unrecognized character: %c" (aref arg bad
)))
683 (calc-change-mode 'calc-matrix-brackets code t
))))
687 ;;; calc-mode.el ends here