comparison lisp/calc/calc.el @ 41270:711f18abaf57

(calc-record-compilation-date): Remove. (calc-bug-address): Update. (calc-settings-file): Use `user-init-file'. Change all toplevel `setq' forms to `defvar' forms, and move them before their first use. Use `when', `unless'. Remove trailing periods from error forms. Add description and headers suggested by Emacs Lisp coding conventions.
author Colin Walters <walters@gnu.org>
date Mon, 19 Nov 2001 07:43:05 +0000
parents 73f364fd8aaa
children 1c09d8ddd155
comparison
equal deleted inserted replaced
41269:8b4a880d7759 41270:711f18abaf57
1 ;; Calculator for GNU Emacs, part I 1 ;;; calc.el ---
2
2 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. 3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com. 4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Colin Walters <walters@debian.org>
7 ;; Keywords: convenience, extensions
4 8
5 ;; This file is part of GNU Emacs. 9 ;; This file is part of GNU Emacs.
6 10
7 ;; GNU Emacs is distributed in the hope that it will be useful, 11 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor 12 ;; but WITHOUT ANY WARRANTY. No author or distributor
17 ;; supposed to have been given to you along with GNU Emacs so you 21 ;; 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 22 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice 23 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies. 24 ;; and this notice must be preserved on all copies.
21 25
22 26 ;;; Commentary:
23 27
24 ;;; Calc is split into many files. This file is the main entry point. 28 ;;; Calc is split into many files. This file is the main entry point.
25 ;;; This file includes autoload commands for various other basic Calc 29 ;;; This file includes autoload commands for various other basic Calc
26 ;;; facilities. The more advanced features are based in calc-ext, which 30 ;;; facilities. The more advanced features are based in calc-ext, which
27 ;;; in turn contains autoloads for the rest of the Calc files. This 31 ;;; in turn contains autoloads for the rest of the Calc files. This
62 ;;; of the prefixes "math", "Math", or "calc", with the exceptions of 66 ;;; of the prefixes "math", "Math", or "calc", with the exceptions of
63 ;;; "full-calc", "full-calc-keypad", "another-calc", "quick-calc", 67 ;;; "full-calc", "full-calc-keypad", "another-calc", "quick-calc",
64 ;;; "report-calc-bug", and "defmath". User-accessible variables begin 68 ;;; "report-calc-bug", and "defmath". User-accessible variables begin
65 ;;; with "var-". 69 ;;; with "var-".
66 70
71 ;;; Code:
67 72
68 73
69 (provide 'calc) 74 (provide 'calc)
70 (require 'calc-macs) 75 (require 'calc-macs)
71
72 (defun calc-record-compilation-date ()
73 (calc-record-compilation-date-macro))
74 (calc-record-compilation-date)
75
76 76
77 ;;; The "###autoload" comment will be used by Emacs version 19 for 77 ;;; The "###autoload" comment will be used by Emacs version 19 for
78 ;;; maintaining the loaddefs.el file automatically. 78 ;;; maintaining the loaddefs.el file automatically.
79 79
80 ;;;###autoload 80 ;;;###autoload
81 (defvar calc-info-filename "calc.info" 81 (defvar calc-info-filename "calc.info"
82 "*File name in which to look for the Calculator's Info documentation.") 82 "*File name in which to look for the Calculator's Info documentation.")
83 83
84 ;;;###autoload 84 ;;;###autoload
85 (defvar calc-settings-file "~/.emacs" 85 (defvar calc-settings-file user-init-file
86 "*File in which to record permanent settings; default is \"~/.emacs\".") 86 "*File in which to record permanent settings; default is `user-init-file'.")
87 87
88 ;;;###autoload 88 ;;;###autoload
89 (defvar calc-autoload-directory nil 89 (defvar calc-autoload-directory nil
90 "Name of directory from which additional \".elc\" files for Calc should be 90 "Name of directory from which additional \".elc\" files for Calc should be
91 loaded. Should include a trailing \"/\". 91 loaded. Should include a trailing \"/\".
102 102
103 ;;;###autoload 103 ;;;###autoload
104 (defvar calc-gnuplot-print-command "lp %s" 104 (defvar calc-gnuplot-print-command "lp %s"
105 "*Name of command for printing GNUPLOT output; %s = file name to print.") 105 "*Name of command for printing GNUPLOT output; %s = file name to print.")
106 106
107
108 ;; Address of the author of Calc, for use by report-calc-bug. 107 ;; Address of the author of Calc, for use by report-calc-bug.
109 (defvar calc-bug-address "daveg@synaptics.com") 108 (defvar calc-bug-address "walters@debian.org")
110
111 109
112 ;; If T, scan keymaps to find all DEL-like keys. 110 ;; If T, scan keymaps to find all DEL-like keys.
113 ;; If NIL, only DEL itself is mapped to calc-pop. 111 ;; If NIL, only DEL itself is mapped to calc-pop.
114 (defvar calc-scan-for-dels t) 112 (defvar calc-scan-for-dels t)
115 113
116 114
117 115
118 (defvar calc-extensions-loaded nil) 116 (defvar calc-extensions-loaded nil)
119
120 117
121 118
122 ;;; IDEAS: 119 ;;; IDEAS:
123 ;;; 120 ;;;
124 ;;; Fix rewrite mechanism to do less gratuitous rearrangement of terms. 121 ;;; Fix rewrite mechanism to do less gratuitous rearrangement of terms.
734 (mapcar (function (lambda (v) (or (boundp (car v)) (set (car v) (nth 1 v))))) 731 (mapcar (function (lambda (v) (or (boundp (car v)) (set (car v) (nth 1 v)))))
735 calc-mode-var-list) 732 calc-mode-var-list)
736 (mapcar (function (lambda (v) (or (boundp v) (set v nil)))) 733 (mapcar (function (lambda (v) (or (boundp v) (set v nil))))
737 calc-local-var-list) 734 calc-local-var-list)
738 735
739 (if (boundp 'calc-mode-map) 736 (unless (boundp 'calc-mode-map)
740 nil
741 (setq calc-mode-map (make-keymap)) 737 (setq calc-mode-map (make-keymap))
742 (suppress-keymap calc-mode-map t) 738 (suppress-keymap calc-mode-map t)
743 (define-key calc-mode-map "+" 'calc-plus) 739 (define-key calc-mode-map "+" 'calc-plus)
744 (define-key calc-mode-map "-" 'calc-minus) 740 (define-key calc-mode-map "-" 'calc-minus)
745 (define-key calc-mode-map "*" 'calc-times) 741 (define-key calc-mode-map "*" 'calc-times)
917 ("calc-misc" another-calc calc-big-or-small calc-dispatch-help 913 ("calc-misc" another-calc calc-big-or-small calc-dispatch-help
918 calc-help calc-info calc-info-summary calc-inv calc-last-args-stub 914 calc-help calc-info calc-info-summary calc-inv calc-last-args-stub
919 calc-missing-key calc-mod calc-other-window calc-over calc-percent 915 calc-missing-key calc-mod calc-other-window calc-over calc-percent
920 calc-pop-above calc-power calc-roll-down calc-roll-up 916 calc-pop-above calc-power calc-roll-down calc-roll-up
921 calc-shift-Y-prefix-help calc-tutorial calcDigit-letter 917 calc-shift-Y-prefix-help calc-tutorial calcDigit-letter
922 report-calc-bug) 918 report-calc-bug))))
923
924 ))
925 )
926 919
927 (calc-init-base) 920 (calc-init-base)
928 921
929 922
930 ;;;###autoload (global-set-key "\e#" 'calc-dispatch) 923 ;;;###autoload (global-set-key "\e#" 'calc-dispatch)
931 924
932 ;;;###autoload 925 ;;;###autoload
933 (defun calc-dispatch (&optional arg) 926 (defun calc-dispatch (&optional arg)
934 "Invoke the GNU Emacs Calculator. See calc-dispatch-help for details." 927 "Invoke the GNU Emacs Calculator. See `calc-dispatch-help' for details."
935 (interactive "P") 928 (interactive "P")
936 (sit-for echo-keystrokes) 929 (sit-for echo-keystrokes)
937 (condition-case err ; look for other keys bound to calc-dispatch 930 (condition-case err ; look for other keys bound to calc-dispatch
938 (let ((keys (this-command-keys))) 931 (let ((keys (this-command-keys)))
939 (or (not (stringp keys)) 932 (or (not (stringp keys))
946 (define-key calc-dispatch-map (substring keys 0 1) nil)) 939 (define-key calc-dispatch-map (substring keys 0 1) nil))
947 (define-key calc-dispatch-map keys 'calc-same-interface)))) 940 (define-key calc-dispatch-map keys 'calc-same-interface))))
948 (error nil)) 941 (error nil))
949 (calc-do-dispatch arg)) 942 (calc-do-dispatch arg))
950 943
944 (defvar calc-dispatch-help nil)
951 (defun calc-do-dispatch (arg) 945 (defun calc-do-dispatch (arg)
952 (let ((key (calc-read-key-sequence 946 (let ((key (calc-read-key-sequence
953 (if calc-dispatch-help 947 (if calc-dispatch-help
954 "Calc options: Calc, Keypad, Quick, Embed; eXit; Info, Tutorial; Grab; ?=more" 948 "Calc options: Calc, Keypad, Quick, Embed; eXit; Info, Tutorial; Grab; ?=more"
955 (format "%s (Type ? for a list of Calc options)" 949 (format "%s (Type ? for a list of Calc options)"
960 (if key 954 (if key
961 (progn 955 (progn
962 (or (commandp key) (calc-extensions)) 956 (or (commandp key) (calc-extensions))
963 (call-interactively key)) 957 (call-interactively key))
964 (beep)))) 958 (beep))))
965 (setq calc-dispatch-help nil)
966 959
967 (defun calc-read-key-sequence (prompt map) 960 (defun calc-read-key-sequence (prompt map)
968 (let ((prompt2 (format "%s " (key-description (this-command-keys)))) 961 (let ((prompt2 (format "%s " (key-description (this-command-keys))))
969 (glob (current-global-map)) 962 (glob (current-global-map))
970 (loc (current-local-map))) 963 (loc (current-local-map)))
1033 calc-stack) 1026 calc-stack)
1034 (setq calc-stack (list (list 'top-of-stack 1027 (setq calc-stack (list (list 'top-of-stack
1035 1 nil)))))) 1028 1 nil))))))
1036 (setq calc-stack-top (- (length calc-stack) calc-stack-top -1)) 1029 (setq calc-stack-top (- (length calc-stack) calc-stack-top -1))
1037 (or calc-loaded-settings-file 1030 (or calc-loaded-settings-file
1031 (null calc-settings-file)
1038 (string-match "\\.emacs" calc-settings-file) 1032 (string-match "\\.emacs" calc-settings-file)
1039 (progn 1033 (progn
1040 (setq calc-loaded-settings-file t) 1034 (setq calc-loaded-settings-file t)
1041 (load calc-settings-file t))) ; t = missing-ok 1035 (load calc-settings-file t))) ; t = missing-ok
1042 (if (and (eq window-system 'x) (boundp 'mouse-map)) 1036 (if (and (eq window-system 'x) (boundp 'mouse-map))
1061 (eval (cons 'progn calc-defs)) 1055 (eval (cons 'progn calc-defs))
1062 (setq calc-defs nil) 1056 (setq calc-defs nil)
1063 (calc-set-mode-line))) 1057 (calc-set-mode-line)))
1064 (calc-check-defines)) 1058 (calc-check-defines))
1065 1059
1060 (defvar calc-check-defines 'calc-check-defines) ; suitable for run-hooks
1066 (defun calc-check-defines () 1061 (defun calc-check-defines ()
1067 (if (symbol-plist 'calc-define) 1062 (if (symbol-plist 'calc-define)
1068 (let ((plist (copy-sequence (symbol-plist 'calc-define)))) 1063 (let ((plist (copy-sequence (symbol-plist 'calc-define))))
1069 (while (and plist (null (nth 1 plist))) 1064 (while (and plist (null (nth 1 plist)))
1070 (setq plist (cdr (cdr plist)))) 1065 (setq plist (cdr (cdr plist))))
1078 (eval (nth 1 plist)) 1073 (eval (nth 1 plist))
1079 (setq plist (cdr (cdr plist)))) 1074 (setq plist (cdr (cdr plist))))
1080 ;; See if this has added any more calc-define properties. 1075 ;; See if this has added any more calc-define properties.
1081 (calc-check-defines)) 1076 (calc-check-defines))
1082 (setplist 'calc-define nil))))) 1077 (setplist 'calc-define nil)))))
1083 (setq calc-check-defines 'calc-check-defines) ; suitable for run-hooks
1084 1078
1085 (defun calc-trail-mode (&optional buf) 1079 (defun calc-trail-mode (&optional buf)
1086 "Calc Trail mode. 1080 "Calc Trail mode.
1087 This mode is used by the *Calc Trail* buffer, which records all results 1081 This mode is used by the *Calc Trail* buffer, which records all results
1088 obtained by the GNU Emacs Calculator. 1082 obtained by the GNU Emacs Calculator.
1105 (progn 1099 (progn
1106 (make-local-variable 'calc-main-buffer) 1100 (make-local-variable 'calc-main-buffer)
1107 (setq calc-main-buffer buf))) 1101 (setq calc-main-buffer buf)))
1108 (if (= (buffer-size) 0) 1102 (if (= (buffer-size) 0)
1109 (let ((buffer-read-only nil)) 1103 (let ((buffer-read-only nil))
1110 (insert "Emacs Calculator v" calc-version " by Dave Gillespie, " 1104 (insert "Emacs Calculator v" calc-version " by Dave Gillespie\n")))
1111 "installed " calc-installed-date "\n")))
1112 (run-hooks 'calc-trail-mode-hook)) 1105 (run-hooks 'calc-trail-mode-hook))
1113 1106
1114 (defun calc-create-buffer () 1107 (defun calc-create-buffer ()
1115 (set-buffer (get-buffer-create "*Calculator*")) 1108 (set-buffer (get-buffer-create "*Calculator*"))
1116 (or (eq major-mode 'calc-mode) 1109 (or (eq major-mode 'calc-mode)
1170 (save-excursion 1163 (save-excursion
1171 (set-buffer (calc-trail-buffer)) 1164 (set-buffer (calc-trail-buffer))
1172 (and calc-display-trail 1165 (and calc-display-trail
1173 (= (window-width) (frame-width)) 1166 (= (window-width) (frame-width))
1174 (calc-trail-display 1 t))) 1167 (calc-trail-display 1 t)))
1175 (message "Welcome to the GNU Emacs Calculator! Press `?' or `h' for help, `q' to quit.") 1168 (message "Welcome to the GNU Emacs Calculator! Press `?' or `h' for help, `q' to quit")
1176 (run-hooks 'calc-start-hook) 1169 (run-hooks 'calc-start-hook)
1177 (and (windowp full-display) 1170 (and (windowp full-display)
1178 (window-point full-display) 1171 (window-point full-display)
1179 (select-window full-display)) 1172 (select-window full-display))
1180 (calc-check-defines) 1173 (calc-check-defines)
1219 (MacEdit-cancel-edit) 1212 (MacEdit-cancel-edit)
1220 (if (and (interactive-p) 1213 (if (and (interactive-p)
1221 calc-embedded-info 1214 calc-embedded-info
1222 (eq (current-buffer) (aref calc-embedded-info 0))) 1215 (eq (current-buffer) (aref calc-embedded-info 0)))
1223 (calc-embedded nil) 1216 (calc-embedded nil)
1224 (or (eq major-mode 'calc-mode) 1217 (unless (eq major-mode 'calc-mode)
1225 (calc-create-buffer)) 1218 (calc-create-buffer))
1226 (run-hooks 'calc-end-hook) 1219 (run-hooks 'calc-end-hook)
1227 (setq calc-undo-list nil calc-redo-list nil) 1220 (setq calc-undo-list nil calc-redo-list nil)
1228 (mapcar (function (lambda (v) (set-default v (symbol-value v)))) 1221 (mapcar (function (lambda (v) (set-default v (symbol-value v))))
1229 calc-local-var-list) 1222 calc-local-var-list)
1230 (let ((buf (current-buffer)) 1223 (let ((buf (current-buffer))
1274 (interactive) 1267 (interactive)
1275 (calc-extensions) 1268 (calc-extensions)
1276 (calc-do-keypad t (interactive-p))) 1269 (calc-do-keypad t (interactive-p)))
1277 1270
1278 1271
1272 (defvar calc-aborted-prefix nil)
1273 (defvar calc-start-time nil)
1279 ;;; Note that modifications to this function may break calc-pass-errors. 1274 ;;; Note that modifications to this function may break calc-pass-errors.
1280 (defun calc-do (do-body &optional do-slow) 1275 (defun calc-do (do-body &optional do-slow)
1281 (calc-check-defines) 1276 (calc-check-defines)
1282 (let* ((calc-command-flags nil) 1277 (let* ((calc-command-flags nil)
1283 (calc-start-time (and calc-timing (not calc-start-time) 1278 (calc-start-time (and calc-timing (not calc-start-time)
1293 (calc-embedded-select-buffer) 1288 (calc-embedded-select-buffer)
1294 (calc-select-buffer)) 1289 (calc-select-buffer))
1295 (and (eq calc-algebraic-mode 'total) 1290 (and (eq calc-algebraic-mode 'total)
1296 (calc-extensions) 1291 (calc-extensions)
1297 (use-local-map calc-alg-map)) 1292 (use-local-map calc-alg-map))
1298 (and do-slow calc-display-working-message 1293 (when (and do-slow calc-display-working-message)
1299 (progn 1294 (message "Working...")
1300 (message "Working...") 1295 (calc-set-command-flag 'clear-message))
1301 (calc-set-command-flag 'clear-message)))
1302 (funcall do-body) 1296 (funcall do-body)
1303 (setq calc-aborted-prefix nil) 1297 (setq calc-aborted-prefix nil)
1304 (and (memq 'renum-stack calc-command-flags) 1298 (when (memq 'renum-stack calc-command-flags)
1305 (calc-renumber-stack)) 1299 (calc-renumber-stack))
1306 (and (memq 'clear-message calc-command-flags) 1300 (when (memq 'clear-message calc-command-flags)
1307 (message ""))) 1301 (message "")))
1308 (error 1302 (error
1309 (if (and (eq (car err) 'error) 1303 (if (and (eq (car err) 'error)
1310 (stringp (nth 1 err)) 1304 (stringp (nth 1 err))
1311 (string-match "max-specpdl-size\\|max-lisp-eval-depth" 1305 (string-match "max-specpdl-size\\|max-lisp-eval-depth"
1312 (nth 1 err))) 1306 (nth 1 err)))
1313 (error "Computation got stuck or ran too long. Type `M' to increase the limit.") 1307 (error "Computation got stuck or ran too long. Type `M' to increase the limit")
1314 (setq calc-aborted-prefix nil) 1308 (setq calc-aborted-prefix nil)
1315 (signal (car err) (cdr err))))) 1309 (signal (car err) (cdr err)))))
1316 (setq calc-old-aborted-prefix calc-aborted-prefix) 1310 (setq calc-old-aborted-prefix calc-aborted-prefix)
1317 (and calc-aborted-prefix 1311 (when calc-aborted-prefix
1318 (calc-record "<Aborted>" calc-aborted-prefix)) 1312 (calc-record "<Aborted>" calc-aborted-prefix))
1319 (and calc-start-time 1313 (and calc-start-time
1320 (let* ((calc-internal-prec 12) 1314 (let* ((calc-internal-prec 12)
1321 (calc-date-format nil) 1315 (calc-date-format nil)
1322 (end-time (current-time-string)) 1316 (end-time (current-time-string))
1323 (time (if (equal calc-start-time end-time) 1317 (time (if (equal calc-start-time end-time)
1338 (move-to-column calc-final-point-column)) 1332 (move-to-column calc-final-point-column))
1339 (save-excursion 1333 (save-excursion
1340 (calc-select-buffer) 1334 (calc-select-buffer)
1341 (goto-line calc-final-point-line) 1335 (goto-line calc-final-point-line)
1342 (move-to-column calc-final-point-column)))) 1336 (move-to-column calc-final-point-column))))
1343 (or (memq 'keep-flags calc-command-flags) 1337 (unless (memq 'keep-flags calc-command-flags)
1344 (save-excursion 1338 (save-excursion
1345 (calc-select-buffer) 1339 (calc-select-buffer)
1346 (setq calc-inverse-flag nil 1340 (setq calc-inverse-flag nil
1347 calc-hyperbolic-flag nil 1341 calc-hyperbolic-flag nil
1348 calc-keep-args-flag nil))) 1342 calc-keep-args-flag nil)))
1349 (and (memq 'do-edit calc-command-flags) 1343 (when (memq 'do-edit calc-command-flags)
1350 (switch-to-buffer (get-buffer-create "*Calc Edit*"))) 1344 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
1351 (calc-set-mode-line) 1345 (calc-set-mode-line)
1352 (and calc-embedded-info 1346 (when calc-embedded-info
1353 (calc-embedded-finish-command)))) 1347 (calc-embedded-finish-command))))
1354 (identity nil)) ; allow a GC after timing is done 1348 (identity nil)) ; allow a GC after timing is done
1355 1349
1356 (setq calc-aborted-prefix nil)
1357 (setq calc-start-time nil)
1358 1350
1359 (defun calc-set-command-flag (f) 1351 (defun calc-set-command-flag (f)
1360 (if (not (memq f calc-command-flags)) 1352 (unless (memq f calc-command-flags)
1361 (setq calc-command-flags (cons f calc-command-flags)))) 1353 (setq calc-command-flags (cons f calc-command-flags))))
1362 1354
1363 (defun calc-select-buffer () 1355 (defun calc-select-buffer ()
1364 (or (eq major-mode 'calc-mode) 1356 (or (eq major-mode 'calc-mode)
1365 (if calc-main-buffer 1357 (if calc-main-buffer
1366 (set-buffer calc-main-buffer) 1358 (set-buffer calc-main-buffer)
3330 ( "&&&" calcFunc-pand 80 81 ) 3322 ( "&&&" calcFunc-pand 80 81 )
3331 ( "|||" calcFunc-por 75 76 ) 3323 ( "|||" calcFunc-por 75 76 )
3332 ( ":=" calcFunc-assign 51 50 ) 3324 ( ":=" calcFunc-assign 51 50 )
3333 ( "::" calcFunc-condition 45 46 ) 3325 ( "::" calcFunc-condition 45 46 )
3334 ( "=>" calcFunc-evalto 40 41 ) 3326 ( "=>" calcFunc-evalto 40 41 )
3335 ( "=>" calcFunc-evalto 40 -1 ) 3327 ( "=>" calcFunc-evalto 40 -1 )))
3336 )) 3328 (defvar math-expr-opers math-standard-opers)
3337 (setq math-expr-opers math-standard-opers)
3338
3339 3329
3340 ;;;###autoload 3330 ;;;###autoload
3341 (defun calc-grab-region (top bot arg) 3331 (defun calc-grab-region (top bot arg)
3342 "Parse the region as a vector of numbers and push it on the Calculator stack." 3332 "Parse the region as a vector of numbers and push it on the Calculator stack."
3343 (interactive "r\nP") 3333 (interactive "r\nP")