~ubuntu-branches/ubuntu/karmic/emacs-snapshot/karmic

« back to all changes in this revision

Viewing changes to lisp/calc/calc.el

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2009-04-05 09:14:30 UTC
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: james.westby@ubuntu.com-20090405091430-nw07lynn2arotjbe
Tags: upstream-20090320
ImportĀ upstreamĀ versionĀ 20090320

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
;;; calc.el --- the GNU Emacs calculator
2
2
 
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.
5
5
 
6
6
;; Author: David Gillespie <daveg@synaptics.com>
7
7
;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
230
230
;;;###autoload
231
231
(defcustom calc-settings-file
232
232
  (convert-standard-filename "~/.calc.el")
233
 
  "*File in which to record permanent settings."
 
233
  "File in which to record permanent settings."
234
234
  :group 'calc
235
235
  :type '(file))
236
236
 
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."
250
250
  :group 'calc
251
251
  :type '(alist :key-type (symbol :tag "Major mode")
252
252
                :value-type (symbol :tag "Calc language")))
253
253
 
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."
257
257
  :group 'calc
258
258
  :type '(regexp))
259
259
 
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'."
273
273
  :group 'calc
274
274
  :type '(alist :key-type (symbol :tag "Major mode")
275
275
                :value-type (regexp :tag "Regexp to announce formula")))
276
276
 
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."
280
280
  :group 'calc
281
281
  :type '(regexp))
282
282
 
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."
286
286
  :group 'calc
287
287
  :type '(regexp))
288
288
 
289
289
(defcustom calc-embedded-open-close-formula-alist
290
290
  nil
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."
292
292
  :group 'calc
293
293
  :type '(alist :key-type (symbol :tag "Major mode")
294
294
                :value-type (list (regexp :tag "Opening formula delimiter")
302
302
 
303
303
(defcustom calc-embedded-word-regexp-alist
304
304
  nil
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."
306
306
  :group 'calc
307
307
  :type '(alist :key-type (symbol :tag "Major mode")
308
308
                :value-type (regexp :tag "Regexp for word")))
309
309
 
310
310
(defcustom calc-embedded-open-plain
311
311
  "%%% "
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
314
314
each formula."
315
315
  :group 'calc
317
317
 
318
318
(defcustom calc-embedded-close-plain
319
319
  " %%%\n"
320
 
  "*A string which is the closing delimiter for a \"plain\" formula.
 
320
  "A string which is the closing delimiter for a \"plain\" formula.
321
321
See calc-embedded-open-plain."
322
322
  :group 'calc
323
323
  :type '(string))
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."
338
338
  :group 'calc
339
339
  :type '(alist :key-type (symbol :tag "Major mode")
340
340
                :value-type (list (string :tag "Opening \"plain\" delimiter")
342
342
 
343
343
(defcustom calc-embedded-open-new-formula
344
344
  "\n\n"
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."
346
346
  :group 'calc
347
347
  :type '(string))
348
348
 
349
349
(defcustom calc-embedded-close-new-formula
350
350
  "\n\n"
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."
352
352
  :group 'calc
353
353
  :type '(string))
354
354
 
355
355
(defcustom calc-embedded-open-close-new-formula-alist
356
356
  nil
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."
358
358
  :group 'calc
359
359
  :type '(alist :key-type (symbol :tag "Major mode")
360
360
                :value-type (list (string :tag "Opening new formula delimiter")
362
362
 
363
363
(defcustom calc-embedded-open-mode
364
364
  "% "
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."
367
367
  :group 'calc
368
368
  :type '(string))
369
369
 
370
370
(defcustom calc-embedded-close-mode
371
371
  "\n"
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."
374
374
  :group 'calc
375
375
  :type '(string))
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."
390
390
  :group 'calc
391
391
  :type '(alist :key-type (symbol :tag "Major mode")
392
392
                :value-type (list (string :tag "Opening annotation delimiter")
394
394
 
395
395
(defcustom calc-gnuplot-name
396
396
  "gnuplot"
397
 
  "*Name of GNUPLOT program, for calc-graph features."
 
397
  "Name of GNUPLOT program, for calc-graph features."
398
398
  :group 'calc
399
399
  :type '(string))
400
400
 
401
401
(defcustom calc-gnuplot-plot-command
402
402
  nil
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."
404
404
  :group 'calc
405
405
  :type '(choice (string) (sexp)))
406
406
 
407
407
(defcustom calc-gnuplot-print-command
408
408
  "lp %s"
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."
410
410
  :group 'calc
411
411
  :type '(choice (string) (sexp)))
412
412
 
413
413
(defcustom calc-multiplication-has-precedence
414
414
  t
415
 
  "*If non-nil, multiplication has precedence over division
 
415
  "If non-nil, multiplication has precedence over division
416
416
in normal mode."
417
417
  :group 'calc
418
418
  :type 'boolean)
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.
1427
1430
        (if (and win
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)))
1432
1435
        (progn
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))))))
2144
2147
  (interactive)
2145
2148
  (calc-wrapper
2146
2149
   (if (or calc-algebraic-mode
2147
 
           (and (> calc-number-radix 14) (eq last-command-char ?e)))
 
2150
           (and (> calc-number-radix 14) (eq last-command-event ?e)))
2148
2151
       (calc-alg-digit-entry)
2149
2152
     (calc-unread-command)
2150
2153
     (setq calc-aborted-prefix nil)
2193
2196
      (progn
2194
2197
        (beep)
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))
2200
 
                                        last-input-char
 
2201
               (calc-unread-command (if (and (eq last-command-event 27)
 
2202
                                             (>= last-input-event 128))
 
2203
                                        last-input-event
2201
2204
                                      nil))))
2202
2205
    (exit-minibuffer)))
2203
2206
 
2210
2213
(defun calcDigit-key ()
2211
2214
  (interactive)
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 ?:))
2229
2232
        (insert "1"))
2230
2233
    (if (and (calc-minibuffer-contains "[-+]?[0-9]+#\\'")
2231
 
             (eq last-command-char ?.))
 
2234
             (eq last-command-event ?.))
2232
2235
        (insert "0"))
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))
2235
2238
        (insert "1"))
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)))
2243
2246
    (cond
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)))
2254
2257
            (delete-char 1)
2255
2258
          (insert "-")))
2256
2259
      (goto-char (point-max)))
2257
 
     ((eq last-command-char ?p)
 
2260
     ((eq last-command-event ?p)
2258
2261
      (if (or (calc-minibuffer-contains ".*\\+/-.*")
2259
2262
              (calc-minibuffer-contains ".*mod.*")
2260
2263
              (calc-minibuffer-contains ".*#.*")
2263
2266
        (if (not (calc-minibuffer-contains ".* \\'"))
2264
2267
            (insert " "))
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 ".*\\+/-.*")
2278
2281
              (insert " "))
2279
2282
          (insert "mod "))))
2280
2283
     (t
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)
2287
2290
                          (<= radix 36)
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))))
2291
2294
                                (and dig
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))
2297
2300
              (insert " "))
2298
2301
        (if (and (eq this-command last-command)
2299
 
                 (eq last-command-char ?.))
 
2302
                 (eq last-command-event ?.))
2300
2303
            (progn
2301
2304
              (require 'calc-ext)
2302
2305
              (calc-digit-dots))
2304
2307
          (beep)
2305
2308
          (calc-temp-minibuffer-message " [Bad format]"))))))
2306
2309
  (setq calc-prev-prev-char calc-prev-char
2307
 
        calc-prev-char last-command-char))
 
2310
        calc-prev-char last-command-event))
2308
2311
 
2309
2312
 
2310
2313
(defun calcDigit-backspace ()
2321
2324
        (t (backward-delete-char 1)))
2322
2325
  (if (= (calc-minibuffer-size) 0)
2323
2326
      (progn
2324
 
        (setq last-command-char 13)
 
2327
        (setq last-command-event 13)
2325
2328
        (calcDigit-nondigit))))
2326
2329
 
2327
2330