1
1
;;; calc.el --- the GNU Emacs calculator
3
3
;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4
;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4
;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6
6
;; Author: David Gillespie <daveg@synaptics.com>
7
7
;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
246
246
(fortran-mode . fortran)
247
247
(f90-mode . fortran)
248
248
(texinfo-mode . calc-normal-language))
249
"*Alist of major modes with appropriate Calc languages."
249
"Alist of major modes with appropriate Calc languages."
251
251
:type '(alist :key-type (symbol :tag "Major mode")
252
252
:value-type (symbol :tag "Calc language")))
254
254
(defcustom calc-embedded-announce-formula
255
255
"%Embed\n\\(% .*\n\\)*"
256
"*A regular expression which is sure to be followed by a calc-embedded formula."
256
"A regular expression which is sure to be followed by a calc-embedded formula."
269
269
(sgml-mode . "<!-- Embed -->\n\\(<!-- .* -->\n\\)*")
270
270
(xml-mode . "<!-- Embed -->\n\\(<!-- .* -->\n\\)*")
271
271
(texinfo-mode . "@c Embed\n\\(@c .*\n\\)*"))
272
"*Alist of major modes with appropriate values for `calc-embedded-announce-formula'."
272
"Alist of major modes with appropriate values for `calc-embedded-announce-formula'."
274
274
:type '(alist :key-type (symbol :tag "Major mode")
275
275
:value-type (regexp :tag "Regexp to announce formula")))
277
277
(defcustom calc-embedded-open-formula
278
278
"\\`\\|^\n\\|\\$\\$?\\|\\\\\\[\\|^\\\\begin[^{].*\n\\|^\\\\begin{.*[^x]}.*\n\\|^@.*\n\\|^\\.EQ.*\n\\|\\\\(\\|^%\n\\|^\\.\\\\\"\n"
279
"*A regular expression for the opening delimiter of a formula used by calc-embedded."
279
"A regular expression for the opening delimiter of a formula used by calc-embedded."
283
283
(defcustom calc-embedded-close-formula
284
284
"\\'\\|\n$\\|\\$\\$?\\|\\\\]\\|^\\\\end[^{].*\n\\|^\\\\end{.*[^x]}.*\n\\|^@.*\n\\|^\\.EN.*\n\\|\\\\)\\|\n%\n\\|^\\.\\\\\"\n"
285
"*A regular expression for the closing delimiter of a formula used by calc-embedded."
285
"A regular expression for the closing delimiter of a formula used by calc-embedded."
289
289
(defcustom calc-embedded-open-close-formula-alist
291
"*Alist of major modes with pairs of formula delimiters used by calc-embedded."
291
"Alist of major modes with pairs of formula delimiters used by calc-embedded."
293
293
:type '(alist :key-type (symbol :tag "Major mode")
294
294
:value-type (list (regexp :tag "Opening formula delimiter")
303
303
(defcustom calc-embedded-word-regexp-alist
305
"*Alist of major modes with word regexps used by calc-embedded-word."
305
"Alist of major modes with word regexps used by calc-embedded-word."
307
307
:type '(alist :key-type (symbol :tag "Major mode")
308
308
:value-type (regexp :tag "Regexp for word")))
310
310
(defcustom calc-embedded-open-plain
312
"*A string which is the opening delimiter for a \"plain\" formula.
312
"A string which is the opening delimiter for a \"plain\" formula.
313
313
If calc-show-plain mode is enabled, this is inserted at the front of
334
334
(sgml-mode "<!-- %% " " %% -->\n")
335
335
(xml-mode "<!-- %% " " %% -->\n")
336
336
(texinfo-mode "@c %% " " %%\n"))
337
"*Alist of major modes with pairs of delimiters for \"plain\" formulas."
337
"Alist of major modes with pairs of delimiters for \"plain\" formulas."
339
339
:type '(alist :key-type (symbol :tag "Major mode")
340
340
:value-type (list (string :tag "Opening \"plain\" delimiter")
343
343
(defcustom calc-embedded-open-new-formula
345
"*A string which is inserted at front of formula by calc-embedded-new-formula."
345
"A string which is inserted at front of formula by calc-embedded-new-formula."
349
349
(defcustom calc-embedded-close-new-formula
351
"*A string which is inserted at end of formula by calc-embedded-new-formula."
351
"A string which is inserted at end of formula by calc-embedded-new-formula."
355
355
(defcustom calc-embedded-open-close-new-formula-alist
357
"*Alist of major modes with pairs of new formula delimiters used by calc-embedded."
357
"Alist of major modes with pairs of new formula delimiters used by calc-embedded."
359
359
:type '(alist :key-type (symbol :tag "Major mode")
360
360
:value-type (list (string :tag "Opening new formula delimiter")
363
363
(defcustom calc-embedded-open-mode
365
"*A string which should precede calc-embedded mode annotations.
365
"A string which should precede calc-embedded mode annotations.
366
366
This is not required to be present for user-written mode annotations."
370
370
(defcustom calc-embedded-close-mode
372
"*A string which should follow calc-embedded mode annotations.
372
"A string which should follow calc-embedded mode annotations.
373
373
This is not required to be present for user-written mode annotations."
386
386
(sgml-mode "<!-- " " -->\n")
387
387
(xml-mode "<!-- " " -->\n")
388
388
(texinfo-mode "@c " "\n"))
389
"*Alist of major modes with pairs of strings to delimit annotations."
389
"Alist of major modes with pairs of strings to delimit annotations."
391
391
:type '(alist :key-type (symbol :tag "Major mode")
392
392
:value-type (list (string :tag "Opening annotation delimiter")
395
395
(defcustom calc-gnuplot-name
397
"*Name of GNUPLOT program, for calc-graph features."
397
"Name of GNUPLOT program, for calc-graph features."
401
401
(defcustom calc-gnuplot-plot-command
403
"*Name of command for displaying GNUPLOT output; %s = file name to print."
403
"Name of command for displaying GNUPLOT output; %s = file name to print."
405
405
:type '(choice (string) (sexp)))
407
407
(defcustom calc-gnuplot-print-command
409
"*Name of command for printing GNUPLOT output; %s = file name to print."
409
"Name of command for printing GNUPLOT output; %s = file name to print."
411
411
:type '(choice (string) (sexp)))
413
413
(defcustom calc-multiplication-has-precedence
415
"*If non-nil, multiplication has precedence over division
415
"If non-nil, multiplication has precedence over division
1424
1424
(win (get-buffer-window (current-buffer)))
1425
1425
(kbuf (get-buffer "*Calc Keypad*")))
1426
1426
(delete-windows-on (calc-trail-buffer))
1427
;; The next few lines will set `calc-window-height' so that the
1428
;; next time Calc is called, the window will be the same size
1429
;; as the current window.
1428
1431
(< (window-height win) (1- (frame-height)))
1429
(= (window-width win) (frame-width)) ; avoid calc-keypad
1432
(window-full-width-p win) ; avoid calc-keypad
1430
1433
(not (get-buffer-window "*Calc Keypad*")))
1431
1434
(setq calc-window-height (- (window-height win) 2)))
1433
1436
(delete-windows-on buf)
1434
(delete-windows-on kbuf))
1437
(and kbuf (delete-windows-on kbuf)))
1435
1438
(bury-buffer buf)
1436
1439
(bury-buffer calc-trail-buffer)
1437
1440
(and kbuf (bury-buffer kbuf))))))
2195
2198
(calc-temp-minibuffer-message " [Bad format]"))
2196
(or (memq last-command-char '(32 13))
2199
(or (memq last-command-event '(32 13))
2197
2200
(progn (setq prefix-arg current-prefix-arg)
2198
(calc-unread-command (if (and (eq last-command-char 27)
2199
(>= last-input-char 128))
2201
(calc-unread-command (if (and (eq last-command-event 27)
2202
(>= last-input-event 128))
2202
2205
(exit-minibuffer)))
2210
2213
(defun calcDigit-key ()
2212
2215
(goto-char (point-max))
2213
(if (or (and (memq last-command-char '(?+ ?-))
2216
(if (or (and (memq last-command-event '(?+ ?-))
2214
2217
(> (buffer-size) 0)
2215
2218
(/= (preceding-char) ?e))
2216
(and (memq last-command-char '(?m ?s))
2219
(and (memq last-command-event '(?m ?s))
2217
2220
(not (calc-minibuffer-contains "[-+]?[0-9]+\\.?0*[@oh].*"))
2218
2221
(not (calc-minibuffer-contains "[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#.*"))))
2219
2222
(calcDigit-nondigit)
2220
2223
(if (calc-minibuffer-contains "\\([-+]?\\|.* \\)\\'")
2221
(cond ((memq last-command-char '(?. ?@)) (insert "0"))
2222
((and (memq last-command-char '(?o ?h ?m))
2224
(cond ((memq last-command-event '(?. ?@)) (insert "0"))
2225
((and (memq last-command-event '(?o ?h ?m))
2223
2226
(not (calc-minibuffer-contains ".*#.*"))) (insert "0"))
2224
((memq last-command-char '(?: ?e)) (insert "1"))
2225
((eq last-command-char ?#)
2227
((memq last-command-event '(?: ?e)) (insert "1"))
2228
((eq last-command-event ?#)
2226
2229
(insert (int-to-string calc-number-radix)))))
2227
2230
(if (and (calc-minibuffer-contains "\\([-+]?[0-9]+#\\|[^:]*:\\)\\'")
2228
(eq last-command-char ?:))
2231
(eq last-command-event ?:))
2230
2233
(if (and (calc-minibuffer-contains "[-+]?[0-9]+#\\'")
2231
(eq last-command-char ?.))
2234
(eq last-command-event ?.))
2233
2236
(if (and (calc-minibuffer-contains "[-+]?0*\\([2-9]\\|1[0-4]\\)#\\'")
2234
(eq last-command-char ?e))
2237
(eq last-command-event ?e))
2236
(if (or (and (memq last-command-char '(?h ?o ?m ?s ?p))
2239
(if (or (and (memq last-command-event '(?h ?o ?m ?s ?p))
2237
2240
(calc-minibuffer-contains ".*#.*"))
2238
(and (eq last-command-char ?e)
2241
(and (eq last-command-event ?e)
2239
2242
(calc-minibuffer-contains "[-+]?\\(1[5-9]\\|[2-9][0-9]\\)#.*"))
2240
(and (eq last-command-char ?n)
2243
(and (eq last-command-event ?n)
2241
2244
(calc-minibuffer-contains "[-+]?\\(2[4-9]\\|[3-9][0-9]\\)#.*")))
2242
(setq last-command-char (upcase last-command-char)))
2245
(setq last-command-event (upcase last-command-event)))
2244
((memq last-command-char '(?_ ?n))
2247
((memq last-command-event '(?_ ?n))
2245
2248
(goto-char (minibuffer-prompt-end))
2246
2249
(if (and (search-forward " +/- " nil t)
2247
2250
(not (search-forward "e" nil t)))
2263
2266
(if (not (calc-minibuffer-contains ".* \\'"))
2265
2268
(insert "+/- ")))
2266
((and (eq last-command-char ?M)
2269
((and (eq last-command-event ?M)
2267
2270
(not (calc-minibuffer-contains
2268
2271
"[-+]?\\(2[3-9]\\|[3-9][0-9]\\)#.*")))
2269
2272
(if (or (calc-minibuffer-contains ".*\\+/-.*")
2279
2282
(insert "mod "))))
2281
(insert (char-to-string last-command-char))
2284
(insert (char-to-string last-command-event))
2282
2285
(if (or (and (calc-minibuffer-contains "[-+]?\\(.*\\+/- *\\|.*mod *\\)?\\([0-9][0-9]?\\)#[0-9a-zA-Z]*\\(:[0-9a-zA-Z]*\\(:[0-9a-zA-Z]*\\)?\\|.[0-9a-zA-Z]*\\(e[-+]?[0-9]*\\)?\\)?\\'")
2283
2286
(let ((radix (string-to-number
2284
2287
(buffer-substring
2285
2288
(match-beginning 2) (match-end 2)))))
2286
2289
(and (>= radix 2)
2288
(or (memq last-command-char '(?# ?: ?. ?e ?+ ?-))
2291
(or (memq last-command-event '(?# ?: ?. ?e ?+ ?-))
2289
2292
(let ((dig (math-read-radix-digit
2290
(upcase last-command-char))))
2293
(upcase last-command-event))))
2292
2295
(< dig radix)))))))
2293
2296
(calc-minibuffer-contains
2294
2297
"[-+]?\\(.*\\+/- *\\|.*mod *\\)?\\([0-9]+\\.?0*[@oh] *\\)?\\([0-9]+\\.?0*['m] *\\)?[0-9]*\\(\\.?[0-9]*\\(e[-+]?[0-3]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?\\)?\\|[0-9]:\\([0-9]+:\\)?[0-9]*\\)?[\"s]?\\'"))
2295
(if (and (memq last-command-char '(?@ ?o ?h ?\' ?m))
2298
(if (and (memq last-command-event '(?@ ?o ?h ?\' ?m))
2296
2299
(string-match " " calc-hms-format))
2298
2301
(if (and (eq this-command last-command)
2299
(eq last-command-char ?.))
2302
(eq last-command-event ?.))
2301
2304
(require 'calc-ext)
2302
2305
(calc-digit-dots))