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 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
31 ;; This file is autoloaded from calc-ext.el.
36 (defun calc-Need-calc-mode () nil
)
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")
314 (defun calc-settings-file-name (name &optional arg
)
316 (list (read-file-name (format "Settings file name (normally %s): "
317 (abbreviate-file-name (or user-init-file
321 (setq arg
(if arg
(prefix-numeric-value arg
) 0))
323 (message "Calc settings file is \"%s\"" calc-settings-file
)
324 (if (< (math-abs arg
) 2)
325 (let ((list calc-mode-var-list
))
327 (set (car (car list
)) (nth 1 (car list
)))
328 (setq list
(cdr list
)))))
329 ;; FIXME: we should use ~/.calc or so in order to avoid
330 ;; reexecuting ~/.emacs (it's not always idempotent) -cgw 2001.11.12
331 (setq calc-settings-file name
)
334 (string-match "\\.emacs" calc-settings-file
)
338 (message "New file")))))
340 (defun math-get-modes-vec ()
346 (+ (if (<= (nth 1 calc-float-format
) 0)
347 (+ calc-internal-prec
(nth 1 calc-float-format
))
348 (nth 1 calc-float-format
))
349 (cdr (assq (car calc-float-format
)
350 '((float .
0) (sci .
10000)
351 (eng .
20000) (fix .
30000)))))
352 (cond ((eq calc-angle-mode
'rad
) 2)
353 ((eq calc-angle-mode
'hms
) 3)
355 (if calc-symbolic-mode
1 0)
356 (if calc-prefer-frac
1 0)
357 (if (eq calc-complex-mode
'polar
) 1 0)
358 (cond ((eq calc-matrix-mode
'scalar
) 0)
359 ((eq calc-matrix-mode
'matrix
) -
2)
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
)
416 (message (cond ((and (eq calc-mode-save-mode
'local
) calc-embedded-info
)
417 "Recording mode changes with [calc-mode: ...]")
418 ((eq calc-mode-save-mode
'edit
)
419 "Recording mode changes with [calc-edit-mode: ...]")
420 ((eq calc-mode-save-mode
'perm
)
421 "Recording mode changes with [calc-perm-mode: ...]")
422 ((eq calc-mode-save-mode
'global
)
423 "Recording mode changes with [calc-global-mode: ...]")
424 ((eq calc-mode-save-mode
'save
)
425 (format "Recording mode changes in \"%s\""
428 "Not recording mode changes permanently")))))
430 (defun calc-total-algebraic-mode (flag)
432 (if calc-emacs-type-19
433 (error "Total algebraic mode not yet supported for Emacs 19"))
435 (if (eq calc-algebraic-mode
'total
)
436 (calc-algebraic-mode nil
)
437 (calc-change-mode '(calc-algebraic-mode calc-incomplete-algebraic-mode
)
439 (use-local-map calc-alg-map
)
441 "All keys begin algebraic entry; use Meta (ESC) for Calc keys"))))
443 (defun calc-algebraic-mode (flag)
447 (calc-change-mode '(calc-algebraic-mode
448 calc-incomplete-algebraic-mode
)
449 (list nil
(not calc-incomplete-algebraic-mode
)))
450 (calc-change-mode '(calc-algebraic-mode calc-incomplete-algebraic-mode
)
451 (list (not calc-algebraic-mode
) nil
)))
452 (use-local-map calc-mode-map
)
453 (message (if calc-algebraic-mode
454 "Numeric keys and ( and [ begin algebraic entry"
455 (if calc-incomplete-algebraic-mode
456 "Only ( and [ begin algebraic entry"
457 "No keys except ' and $ begin algebraic entry")))))
459 (defun calc-symbolic-mode (n)
463 (message (if (calc-change-mode 'calc-symbolic-mode n nil t
)
464 "Inexact computations like sqrt(2) are deferred"
465 "Numerical computations are always done immediately"))))
467 (defun calc-infinite-mode (n)
472 (calc-change-mode 'calc-infinite-mode
1)
473 (message "Computations like 1 / 0 produce \"inf\""))
474 (message (if (calc-change-mode 'calc-infinite-mode n nil t
)
475 "Computations like 1 / 0 produce \"uinf\""
476 "Computations like 1 / 0 are left unsimplified")))))
478 (defun calc-matrix-mode (arg)
481 (calc-change-mode 'calc-matrix-mode
482 (cond ((eq arg
0) 'scalar
)
483 ((< (prefix-numeric-value arg
) 1)
484 (and (< (prefix-numeric-value arg
) -
1) 'matrix
))
485 (arg (prefix-numeric-value arg
))
486 ((eq calc-matrix-mode
'matrix
) 'scalar
)
487 ((eq calc-matrix-mode
'scalar
) nil
)
489 (if (integerp calc-matrix-mode
)
490 (message "Variables are assumed to be %dx%d matrices"
491 calc-matrix-mode calc-matrix-mode
)
492 (message (if (eq calc-matrix-mode
'matrix
)
493 "Variables are assumed to be matrices"
495 "Variables are assumed to be scalars (non-matrices)"
496 "Variables are not assumed to be matrix or scalar"))))))
498 (defun calc-set-simplify-mode (mode arg msg
)
499 (calc-change-mode 'calc-simplify-mode
501 (and (> (prefix-numeric-value arg
) 0)
503 (and (not (eq calc-simplify-mode mode
))
505 (message (if (eq calc-simplify-mode mode
)
507 "Default simplifications enabled")))
509 (defun calc-no-simplify-mode (arg)
512 (calc-set-simplify-mode 'none arg
513 "All default simplifications are disabled")))
515 (defun calc-num-simplify-mode (arg)
518 (calc-set-simplify-mode 'num arg
519 "Default simplifications apply only if arguments are numeric")))
521 (defun calc-default-simplify-mode (arg)
525 (calc-set-simplify-mode
526 nil nil
"Usual default simplifications are enabled")))
527 ((= arg
0) (calc-num-simplify-mode 1))
528 ((< arg
0) (calc-no-simplify-mode 1))
529 ((= arg
2) (calc-bin-simplify-mode 1))
530 ((= arg
3) (calc-alg-simplify-mode 1))
531 ((= arg
4) (calc-ext-simplify-mode 1))
532 ((= arg
5) (calc-units-simplify-mode 1))
533 (t (error "Prefix argument out of range"))))
535 (defun calc-bin-simplify-mode (arg)
538 (calc-set-simplify-mode 'binary arg
539 (format "Binary simplification occurs by default (word size=%d)"
542 (defun calc-alg-simplify-mode (arg)
545 (calc-set-simplify-mode 'alg arg
546 "Algebraic simplification occurs by default")))
548 (defun calc-ext-simplify-mode (arg)
551 (calc-set-simplify-mode 'ext arg
552 "Extended algebraic simplification occurs by default")))
554 (defun calc-units-simplify-mode (arg)
557 (calc-set-simplify-mode 'units arg
558 "Units simplification occurs by default")))
560 (defun calc-auto-recompute (arg)
563 (calc-change-mode 'calc-auto-recompute arg nil t
)
564 (calc-refresh-evaltos)
565 (message (if calc-auto-recompute
566 "Automatically recomputing `=>' forms when necessary"
567 "Not recomputing `=>' forms automatically"))))
569 (defun calc-working (n)
573 (calc-pop-push-record 0 "work"
574 (cond ((eq calc-display-working-message t
) 1)
575 (calc-display-working-message 2)
577 ((eq n
2) (calc-change-mode 'calc-display-working-message
'lots
))
578 ((eq n
0) (calc-change-mode 'calc-display-working-message nil
))
579 ((eq n
1) (calc-change-mode 'calc-display-working-message t
)))
580 (cond ((eq calc-display-working-message t
)
581 (message "\"Working...\" messages enabled"))
582 (calc-display-working-message
583 (message "Detailed \"Working...\" messages enabled"))
585 (message "\"Working...\" messages disabled")))))
587 (defun calc-always-load-extensions ()
590 (if (setq calc-always-load-extensions
(not calc-always-load-extensions
))
591 (message "Always loading extensions package")
592 (message "Loading extensions package on demand only"))))
595 (defun calc-matrix-left-justify ()
598 (calc-change-mode 'calc-matrix-just nil t
)
599 (message "Matrix elements will be left-justified in columns")))
601 (defun calc-matrix-center-justify ()
604 (calc-change-mode 'calc-matrix-just
'center t
)
605 (message "Matrix elements will be centered in columns")))
607 (defun calc-matrix-right-justify ()
610 (calc-change-mode 'calc-matrix-just
'right t
)
611 (message "Matrix elements will be right-justified in columns")))
613 (defun calc-full-vectors (n)
616 (message (if (calc-change-mode 'calc-full-vectors n t t
)
617 "Displaying long vectors in full"
618 "Displaying long vectors in [a, b, c, ..., z] notation"))))
620 (defun calc-full-trail-vectors (n)
623 (message (if (calc-change-mode 'calc-full-trail-vectors n nil t
)
624 "Recording long vectors in full"
625 "Recording long vectors in [a, b, c, ..., z] notation"))))
627 (defun calc-break-vectors (n)
630 (message (if (calc-change-mode 'calc-break-vectors n t t
)
631 "Displaying vector elements one-per-line"
632 "Displaying vector elements all on one line"))))
634 (defun calc-vector-commas ()
637 (if (calc-change-mode 'calc-vector-commas
(if calc-vector-commas nil
",") t
)
638 (message "Separating vector elements with \",\"")
639 (message "Separating vector elements with spaces"))))
641 (defun calc-vector-brackets ()
644 (if (calc-change-mode 'calc-vector-brackets
645 (if (equal calc-vector-brackets
"[]") nil
"[]") t
)
646 (message "Surrounding vectors with \"[]\"")
647 (message "Not surrounding vectors with brackets"))))
649 (defun calc-vector-braces ()
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-parens ()
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-matrix-brackets (arg)
666 (interactive "sCode letters (R, O, C, P): ")
668 (let ((code (append (and (string-match "[rR]" arg
) '(R))
669 (and (string-match "[oO]" arg
) '(O))
670 (and (string-match "[cC]" arg
) '(C))
671 (and (string-match "[pP]" arg
) '(P))))
672 (bad (string-match "[^rRoOcCpP ]" arg
)))
674 (error "Unrecognized character: %c" (aref arg bad
)))
675 (calc-change-mode 'calc-matrix-brackets code t
))))
677 ;;; arch-tag: ecc70eea-c712-43f2-9085-4205e58d6ddf
678 ;;; calc-mode.el ends here