1 ;; Calculator for GNU Emacs, part II [calc-mode.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
24 ;; This file is autoloaded from calc-ext.el.
29 (defun calc-Need-calc-mode () nil)
32 (defun calc-line-numbering (n)
35 (message (if (calc-change-mode 'calc-line-numbering n t t)
36 "Displaying stack level numbers."
37 "Hiding stack level numbers.")))
40 (defun calc-line-breaking (n)
44 (and (> (setq n (prefix-numeric-value n)) 0)
47 (not calc-line-breaking)))
48 (if (calc-change-mode 'calc-line-breaking n t)
49 (if (integerp calc-line-breaking)
50 (message "Breaking lines longer than %d characters." n)
51 (message "Breaking long lines in Stack display."))
52 (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.")))
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.")))
78 (defun calc-right-justify (n)
81 (and n (setq n (prefix-numeric-value n)))
82 (calc-change-mode '(calc-display-just calc-display-origin)
85 (message "Displaying stack entries right-justified to column %d." n)
86 (message "Displaying stack entries right-justified in window.")))
89 (defun calc-left-label (s)
90 (interactive "sLefthand label: ")
93 (setq s (concat s " ")))
94 (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))
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."))))
123 (defun calc-group-digits (n)
128 (setq n (prefix-numeric-value n))
129 (cond ((or (> n 0) (< n -1)))
133 (setq n calc-group-digits))))
134 (setq n (not calc-group-digits)))
135 (calc-change-mode 'calc-group-digits n t)
137 (message "Grouping is off."))
139 (message "Grouping every %d digits." (math-abs n)))
141 (message "Grouping is on."))))
144 (defun calc-group-char (ch)
145 (interactive "cGrouping character: ")
148 (error "Control characters not allowed for grouping."))
151 (setq ch (char-to-string ch)))
152 (calc-change-mode 'calc-group-char ch calc-group-digits)
153 (message "Digit grouping character is \"%s\"." ch))
156 (defun calc-point-char (ch)
157 (interactive "cCharacter to use as decimal point: ")
160 (error "Control characters not allowed as decimal point."))
161 (calc-change-mode 'calc-point-char (char-to-string ch) t)
162 (message "Decimal point character is \"%c\"." ch))
165 (defun calc-normal-notation (n)
168 (calc-change-mode 'calc-float-format
169 (let* ((val (if n (prefix-numeric-value n) 0))
170 (mode (/ (+ val 5000) 10000)))
171 (if (or (< val -5000) (> mode 3))
172 (error "Prefix out of range"))
173 (setq n (list (aref [float sci eng fix] mode)
174 (- (% (+ val 5000) 10000) 5000))))
177 (message "Displaying floating-point numbers normally.")
180 "Displaying floating-point numbers with %d significant digits."
182 (message "Displaying floating-point numbers with (precision%d)."
186 (defun calc-fix-notation (n)
187 (interactive "NDigits after decimal point: ")
189 (calc-change-mode 'calc-float-format
190 (setq n (list 'fix (if n (prefix-numeric-value n) 0)))
192 (message "Displaying floats with %d digits after decimal."
193 (math-abs (nth 1 n))))
196 (defun calc-sci-notation (n)
199 (calc-change-mode 'calc-float-format
200 (setq n (list 'sci (if n (prefix-numeric-value n) 0)))
203 (message "Displaying floats in scientific notation.")
205 (message "Displaying scientific notation with %d significant digits."
207 (message "Displaying scientific notation with (precision%d)."
211 (defun calc-eng-notation (n)
214 (calc-change-mode 'calc-float-format
215 (setq n (list 'eng (if n (prefix-numeric-value n) 0)))
218 (message "Displaying floats in engineering notation.")
220 (message "Displaying engineering notation with %d significant digits."
222 (message "Displaying engineering notation with (precision%d)."
227 (defun calc-truncate-stack (n &optional rel)
230 (let ((oldtop calc-stack-top)
231 (newtop calc-stack-top))
232 (calc-record-undo (list 'set 'saved-stack-top calc-stack-top))
233 (let ((calc-stack-top 0)
234 (nn (prefix-numeric-value n)))
239 (setq nn (+ oldtop nn))
241 (setq nn (+ nn (calc-stack-size)))
245 (if (> nn (calc-stack-size))
248 (max 1 (calc-locate-cursor-element (point)))))
249 (if (= newtop oldtop)
251 (calc-pop-stack 1 oldtop t)
252 (calc-push-list '(top-of-stack) newtop)
253 (if calc-line-numbering
255 (calc-record-undo (list 'set 'saved-stack-top 0))
256 (setq calc-stack-top newtop)))
259 (defun calc-truncate-up (n)
261 (calc-truncate-stack n t)
264 (defun calc-truncate-down (n)
266 (calc-truncate-stack (- n) t)
269 (defun calc-display-raw (arg)
272 (setq calc-display-raw (if calc-display-raw nil (if arg 0 t)))
275 (message "Press d ' again to cancel \"raw\" display mode.")))
283 (defun calc-save-modes (&optional quiet)
287 (vals (mapcar (function (lambda (v) (symbol-value (car v))))
288 calc-mode-var-list)))
289 (set-buffer (find-file-noselect (substitute-in-file-name
290 calc-settings-file)))
291 (goto-char (point-min))
292 (if (and (search-forward ";;; Mode settings stored by Calc" nil t)
296 (search-forward "\n;;; End of mode settings" nil t)))
300 (delete-region pos (point)))
301 (goto-char (point-max))
304 (insert ";;; Mode settings stored by Calc on " (current-time-string) "\n")
305 (let ((list calc-mode-var-list))
307 (let* ((v (car (car list)))
308 (def (nth 1 (car list)))
312 (insert "(setq " (symbol-name v) " ")
313 (if (and (or (listp val)
315 (not (memq val '(nil t))))
317 (insert (prin1-to-string val) ")\n"))))
318 (setq list (cdr list)
320 (run-hooks 'calc-mode-save-hook)
321 (insert ";;; End of mode settings\n")
323 (let ((executing-kbd-macro "")) ; what a kludge!
328 (defun calc-settings-file-name (name &optional arg)
329 (interactive "sSettings file name (normally ~/.calc.el): \nP")
331 (setq arg (if arg (prefix-numeric-value arg) 0))
333 (message "Calc settings file is \"%s\"" calc-settings-file)
334 (if (< (math-abs arg) 2)
335 (let ((list calc-mode-var-list))
337 (set (car (car list)) (nth 1 (car list)))
338 (setq list (cdr list)))))
339 (setq calc-settings-file name)
340 (or (and (string-match "\\.calc.el" calc-settings-file)
344 (message "New file"))))
347 (defun math-get-modes-vec ()
353 (+ (if (<= (nth 1 calc-float-format) 0)
354 (+ calc-internal-prec (nth 1 calc-float-format))
355 (nth 1 calc-float-format))
356 (cdr (assq (car calc-float-format)
357 '((float . 0) (sci . 10000)
358 (eng . 20000) (fix . 30000)))))
359 (cond ((eq calc-angle-mode 'rad) 2)
360 ((eq calc-angle-mode 'hms) 3)
362 (if calc-symbolic-mode 1 0)
363 (if calc-prefer-frac 1 0)
364 (if (eq calc-complex-mode 'polar) 1 0)
365 (cond ((eq calc-matrix-mode 'scalar) 0)
366 ((eq calc-matrix-mode 'matrix) -2)
369 (cond ((eq calc-simplify-mode 'none) -1)
370 ((eq calc-simplify-mode 'num) 0)
371 ((eq calc-simplify-mode 'binary) 2)
372 ((eq calc-simplify-mode 'alg) 3)
373 ((eq calc-simplify-mode 'ext) 4)
374 ((eq calc-simplify-mode 'units) 5)
376 (cond ((eq calc-infinite-mode 1) 0)
377 (calc-infinite-mode 1)
381 (defun calc-get-modes (n)
384 (let ((modes (math-get-modes-vec)))
385 (calc-enter-result 0 "mode"
387 (if (and (>= (setq n (prefix-numeric-value n)) 1)
388 (< n (length modes)))
390 (error "Prefix out of range"))
394 (defun calc-shift-prefix (arg)
397 (setq calc-shift-prefix (if arg
398 (> (prefix-numeric-value arg) 0)
399 (not calc-shift-prefix)))
401 (message (if calc-shift-prefix
402 "Prefix keys are now case-insensitive"
403 "Prefix keys must be unshifted (except V, Z)")))
406 (defun calc-mode-record-mode (n)
409 (calc-change-mode 'calc-mode-save-mode
411 (cond ((not calc-embedded-info)
412 (if (eq calc-mode-save-mode 'save)
414 ((eq calc-mode-save-mode 'local) 'edit)
415 ((eq calc-mode-save-mode 'edit) 'perm)
416 ((eq calc-mode-save-mode 'perm) 'global)
417 ((eq calc-mode-save-mode 'global) 'save)
418 ((eq calc-mode-save-mode 'save) nil)
419 ((eq calc-mode-save-mode nil) 'local)))
420 ((= (setq n (prefix-numeric-value n)) 0) nil)
426 (message (cond ((and (eq calc-mode-save-mode 'local) calc-embedded-info)
427 "Recording mode changes with [calc-mode: ...]")
428 ((eq calc-mode-save-mode 'edit)
429 "Recording mode changes with [calc-edit-mode: ...]")
430 ((eq calc-mode-save-mode 'perm)
431 "Recording mode changes with [calc-perm-mode: ...]")
432 ((eq calc-mode-save-mode 'global)
433 "Recording mode changes with [calc-global-mode: ...]")
434 ((eq calc-mode-save-mode 'save)
435 (format "Recording mode changes in \"%s\"."
438 "Not recording mode changes permanently."))))
441 (defun calc-total-algebraic-mode (flag)
443 (if calc-emacs-type-19
444 (error "Total algebraic mode not yet supported for Emacs 19"))
446 (if (eq calc-algebraic-mode 'total)
447 (calc-algebraic-mode nil)
448 (calc-change-mode '(calc-algebraic-mode calc-incomplete-algebraic-mode)
450 (use-local-map calc-alg-map)
452 "All keys begin algebraic entry; use Meta (ESC) for Calc keys.")))
455 (defun calc-algebraic-mode (flag)
459 (calc-change-mode '(calc-algebraic-mode
460 calc-incomplete-algebraic-mode)
461 (list nil (not calc-incomplete-algebraic-mode)))
462 (calc-change-mode '(calc-algebraic-mode calc-incomplete-algebraic-mode)
463 (list (not calc-algebraic-mode) nil)))
464 (use-local-map calc-mode-map)
465 (message (if calc-algebraic-mode
466 "Numeric keys and ( and [ begin algebraic entry."
467 (if calc-incomplete-algebraic-mode
468 "Only ( and [ begin algebraic entry."
469 "No keys except ' and $ begin algebraic entry."))))
472 (defun calc-symbolic-mode (n)
476 (message (if (calc-change-mode 'calc-symbolic-mode n nil t)
477 "Inexact computations like sqrt(2) are deferred."
478 "Numerical computations are always done immediately.")))
481 (defun calc-infinite-mode (n)
486 (calc-change-mode 'calc-infinite-mode 1)
487 (message "Computations like 1 / 0 produce \"inf\"."))
488 (message (if (calc-change-mode 'calc-infinite-mode n nil t)
489 "Computations like 1 / 0 produce \"uinf\"."
490 "Computations like 1 / 0 are left unsimplified."))))
493 (defun calc-matrix-mode (arg)
496 (calc-change-mode 'calc-matrix-mode
497 (cond ((eq arg 0) 'scalar)
498 ((< (prefix-numeric-value arg) 1)
499 (and (< (prefix-numeric-value arg) -1) 'matrix))
500 (arg (prefix-numeric-value arg))
501 ((eq calc-matrix-mode 'matrix) 'scalar)
502 ((eq calc-matrix-mode 'scalar) nil)
504 (if (integerp calc-matrix-mode)
505 (message "Variables are assumed to be %dx%d matrices."
506 calc-matrix-mode calc-matrix-mode)
507 (message (if (eq calc-matrix-mode 'matrix)
508 "Variables are assumed to be matrices."
510 "Variables are assumed to be scalars (non-matrices)."
511 "Variables are not assumed to be matrix or scalar.")))))
514 (defun calc-set-simplify-mode (mode arg msg)
515 (calc-change-mode 'calc-simplify-mode
517 (and (> (prefix-numeric-value arg) 0)
519 (and (not (eq calc-simplify-mode mode))
521 (message (if (eq calc-simplify-mode mode)
523 "Default simplifications enabled."))
526 (defun calc-no-simplify-mode (arg)
529 (calc-set-simplify-mode 'none arg
530 "All default simplifications are disabled."))
533 (defun calc-num-simplify-mode (arg)
536 (calc-set-simplify-mode 'num arg
537 "Default simplifications apply only if arguments are numeric."))
540 (defun calc-default-simplify-mode (arg)
544 (calc-set-simplify-mode
545 nil nil "Usual default simplifications are enabled.")))
546 ((= arg 0) (calc-num-simplify-mode 1))
547 ((< arg 0) (calc-no-simplify-mode 1))
548 ((= arg 2) (calc-bin-simplify-mode 1))
549 ((= arg 3) (calc-alg-simplify-mode 1))
550 ((= arg 4) (calc-ext-simplify-mode 1))
551 ((= arg 5) (calc-units-simplify-mode 1))
552 (t (error "Prefix argument out of range")))
555 (defun calc-bin-simplify-mode (arg)
558 (calc-set-simplify-mode 'binary arg
559 (format "Binary simplification occurs by default (word size=%d)."
563 (defun calc-alg-simplify-mode (arg)
566 (calc-set-simplify-mode 'alg arg
567 "Algebraic simplification occurs by default."))
570 (defun calc-ext-simplify-mode (arg)
573 (calc-set-simplify-mode 'ext arg
574 "Extended algebraic simplification occurs by default."))
577 (defun calc-units-simplify-mode (arg)
580 (calc-set-simplify-mode 'units arg
581 "Units simplification occurs by default."))
584 (defun calc-auto-recompute (arg)
587 (calc-change-mode 'calc-auto-recompute arg nil t)
588 (calc-refresh-evaltos)
589 (message (if calc-auto-recompute
590 "Automatically recomputing `=>' forms when necessary."
591 "Not recomputing `=>' forms automatically.")))
594 (defun calc-working (n)
598 (calc-pop-push-record 0 "work"
599 (cond ((eq calc-display-working-message t) 1)
600 (calc-display-working-message 2)
602 ((eq n 2) (calc-change-mode 'calc-display-working-message 'lots))
603 ((eq n 0) (calc-change-mode 'calc-display-working-message nil))
604 ((eq n 1) (calc-change-mode 'calc-display-working-message t)))
605 (cond ((eq calc-display-working-message t)
606 (message "\"Working...\" messages enabled."))
607 (calc-display-working-message
608 (message "Detailed \"Working...\" messages enabled."))
610 (message "\"Working...\" messages disabled."))))
613 (defun calc-always-load-extensions ()
616 (if (setq calc-always-load-extensions (not calc-always-load-extensions))
617 (message "Always loading extensions package.")
618 (message "Loading extensions package on demand only.")))
622 (defun calc-matrix-left-justify ()
625 (calc-change-mode 'calc-matrix-just nil t)
626 (message "Matrix elements will be left-justified in columns."))
629 (defun calc-matrix-center-justify ()
632 (calc-change-mode 'calc-matrix-just 'center t)
633 (message "Matrix elements will be centered in columns."))
636 (defun calc-matrix-right-justify ()
639 (calc-change-mode 'calc-matrix-just 'right t)
640 (message "Matrix elements will be right-justified in columns."))
643 (defun calc-full-vectors (n)
646 (message (if (calc-change-mode 'calc-full-vectors n t t)
647 "Displaying long vectors in full."
648 "Displaying long vectors in [a, b, c, ..., z] notation.")))
651 (defun calc-full-trail-vectors (n)
654 (message (if (calc-change-mode 'calc-full-trail-vectors n nil t)
655 "Recording long vectors in full."
656 "Recording long vectors in [a, b, c, ..., z] notation.")))
659 (defun calc-break-vectors (n)
662 (message (if (calc-change-mode 'calc-break-vectors n t t)
663 "Displaying vector elements one-per-line."
664 "Displaying vector elements all on one line.")))
667 (defun calc-vector-commas ()
670 (if (calc-change-mode 'calc-vector-commas (if calc-vector-commas nil ",") t)
671 (message "Separating vector elements with \",\".")
672 (message "Separating vector elements with spaces.")))
675 (defun calc-vector-brackets ()
678 (if (calc-change-mode 'calc-vector-brackets
679 (if (equal calc-vector-brackets "[]") nil "[]") t)
680 (message "Surrounding vectors with \"[]\".")
681 (message "Not surrounding vectors with brackets.")))
684 (defun calc-vector-braces ()
687 (if (calc-change-mode 'calc-vector-brackets
688 (if (equal calc-vector-brackets "{}") nil "{}") t)
689 (message "Surrounding vectors with \"{}\".")
690 (message "Not surrounding vectors with brackets.")))
693 (defun calc-vector-parens ()
696 (if (calc-change-mode 'calc-vector-brackets
697 (if (equal calc-vector-brackets "()") nil "()") t)
698 (message "Surrounding vectors with \"()\".")
699 (message "Not surrounding vectors with brackets.")))
702 (defun calc-matrix-brackets (arg)
703 (interactive "sCode letters (R, O, C, P): ")
705 (let ((code (append (and (string-match "[rR]" arg) '(R))
706 (and (string-match "[oO]" arg) '(O))
707 (and (string-match "[cC]" arg) '(C))
708 (and (string-match "[pP]" arg) '(P))))
709 (bad (string-match "[^rRoOcCpP ]" arg)))
711 (error "Unrecognized character: %c" (aref arg bad)))
712 (calc-change-mode 'calc-matrix-brackets code t)))