1 ;;; calc-aent.el --- algebraic entry functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Dave Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 ;; This file is autoloaded from calc.el.
35 ;; Declare functions which are defined elsewhere.
36 (declare-function calc-refresh-evaltos
"calc-ext" (&optional which-var
))
37 (declare-function calc-execute-kbd-macro
"calc-prog" (mac arg
&rest prefix
))
38 (declare-function math-is-true
"calc-ext" (expr))
39 (declare-function calc-explain-why
"calc-stuff" (why &optional more
))
40 (declare-function calc-alg-edit
"calc-yank" (str))
41 (declare-function math-composite-inequalities
"calc-prog" (x op
))
42 (declare-function math-flatten-lands
"calc-rewr" (expr))
43 (declare-function math-multi-subst
"calc-map" (expr olds news
))
44 (declare-function calcFunc-vmatches
"calc-rewr" (expr pat
))
45 (declare-function math-simplify
"calc-alg" (top-expr))
46 (declare-function math-known-matrixp
"calc-arith" (a))
47 (declare-function math-parse-fortran-subscr
"calc-lang" (sym args
))
48 (declare-function math-to-radians-2
"calc-math" (a))
49 (declare-function math-read-string
"calc-ext" ())
50 (declare-function math-read-brackets
"calc-vec" (space-sep math-rb-close
))
51 (declare-function math-read-angle-brackets
"calc-forms" ())
52 (declare-function math-to-percentsigns
"calccomp" (x))
54 (defvar calc-quick-calc-history nil
55 "The history list for quick-calc.")
58 (defun calc-do-quick-calc ()
61 (if (eq major-mode
'calc-mode
)
62 (calc-algebraic-entry t
)
66 (let* ((calc-command-flags nil
)
67 (calc-dollar-values calc-quick-prev-results
)
69 (enable-recursive-minibuffers t
)
70 (calc-language (if (memq calc-language
'(nil big
))
72 (entry (calc-do-alg-entry "" "Quick calc: " t
'calc-quick-calc-history
))
73 (alg-exp (mapcar 'math-evaluate-expr entry
)))
74 (when (and (= (length alg-exp
) 1)
75 (eq (car-safe (car alg-exp
)) 'calcFunc-assign
)
76 (= (length (car alg-exp
)) 3)
77 (eq (car-safe (nth 1 (car alg-exp
))) 'var
))
78 (set (nth 2 (nth 1 (car alg-exp
))) (nth 2 (car alg-exp
)))
79 (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp
))))
80 (setq alg-exp
(list (nth 2 (car alg-exp
)))))
81 (setq calc-quick-prev-results alg-exp
82 buf
(mapconcat (function (lambda (x)
83 (math-format-value x
1000)))
87 (if (and (= (length alg-exp
) 1)
88 (memq (car-safe (car alg-exp
)) '(nil bigpos bigneg
))
90 (= calc-number-radix
10))
91 (setq buf
(concat buf
" ("
92 (let ((calc-number-radix 16))
93 (math-format-value (car alg-exp
) 1000))
95 (let ((calc-number-radix 8))
96 (math-format-value (car alg-exp
) 1000))
98 (let ((calc-number-radix 2))
99 (math-format-value (car alg-exp
) 1000))
100 (if (and (integerp (car alg-exp
))
102 (< (car alg-exp
) 127))
103 (format ", \"%c\"" (car alg-exp
))
106 (if (and (< (length buf
) (frame-width)) (= (length entry
) 1)
107 (featurep 'calc-ext
))
108 (let ((long (concat (math-format-value (car entry
) 1000)
110 (if (<= (length long
) (- (frame-width) 8))
113 (message "Result: %s" buf
)))
114 (if (eq last-command-char
10)
116 (kill-new shortbuf
)))))
119 (defun calc-do-calc-eval (str separator args
)
125 ((and (consp str
) (not (symbolp (car str
))))
126 (let ((calc-language nil
)
127 (math-expr-opers (math-standard-ops))
128 (calc-internal-prec 12)
130 (calc-symbolic-mode nil
)
131 (calc-matrix-mode nil
)
132 (calc-angle-mode 'deg
)
133 (calc-number-radix 10)
134 (calc-leading-zeros nil
)
135 (calc-group-digits nil
)
136 (calc-point-char ".")
137 (calc-frac-format '(":" nil
))
138 (calc-prefer-frac nil
)
139 (calc-hms-format "%s@ %s' %s\"")
140 (calc-date-format '((H ":" mm C SS pp
" ")
141 Www
" " Mmm
" " D
", " YYYY
))
142 (calc-float-format '(float 0))
143 (calc-full-float-format '(float 0))
144 (calc-complex-format nil
)
145 (calc-matrix-just nil
)
146 (calc-full-vectors t
)
147 (calc-break-vectors nil
)
148 (calc-vector-commas ",")
149 (calc-vector-brackets "[]")
150 (calc-matrix-brackets '(R O
))
151 (calc-complex-mode 'cplx
)
152 (calc-infinite-mode nil
)
153 (calc-display-strings nil
)
154 (calc-simplify-mode nil
)
155 (calc-display-working-message 'lots
)
158 (set (car strp
) (nth 1 strp
))
159 (setq strp
(cdr (cdr strp
))))
160 (calc-do-calc-eval (car str
) separator args
)))
161 ((eq separator
'eval
)
163 ((eq separator
'macro
)
165 (let* ((calc-buffer (current-buffer))
166 (calc-window (get-buffer-window calc-buffer
))
167 (save-window (selected-window)))
171 (select-window calc-window
)
172 (calc-execute-kbd-macro str nil
(car args
)))
173 (and (window-point save-window
)
174 (select-window save-window
)))
175 (save-window-excursion
176 (select-window (get-largest-window))
177 (switch-to-buffer calc-buffer
)
178 (calc-execute-kbd-macro str nil
(car args
)))))
181 (or (not (integerp str
))
183 (calc-pop (min str
(calc-stack-size))))
188 (<= str
(calc-stack-size))
189 (math-format-value (calc-top-n str
(car args
)) 1000)))
190 ((eq separator
'rawtop
)
193 (<= str
(calc-stack-size))
194 (calc-top-n str
(car args
))))
196 (let* ((calc-command-flags nil
)
198 (calc-language (if (memq calc-language
'(nil big
))
199 'flat calc-language
))
200 (calc-dollar-values (mapcar
205 (setq x
(math-read-exprs x
))
215 (res (if (stringp str
)
216 (math-read-exprs str
)
219 (if (eq (car res
) 'error
)
220 (calc-eval-error (cdr res
))
221 (setq res
(mapcar 'calc-normalize res
))
222 (and (memq 'clear-message calc-command-flags
)
224 (cond ((eq separator
'pred
)
226 (if (= (length res
) 1)
227 (math-is-true (car res
))
228 (calc-eval-error '(0 "Single value expected"))))
230 (if (= (length res
) 1)
232 (calc-eval-error '(0 "Single value expected"))))
233 ((eq separator
'list
)
235 ((memq separator
'(num rawnum
))
236 (if (= (length res
) 1)
237 (if (math-constp (car res
))
238 (if (eq separator
'num
)
239 (math-format-value (car res
) 1000)
244 (calc-explain-why (car calc-next-why
))
245 "Number expected"))))
246 (calc-eval-error '(0 "Single value expected"))))
247 ((eq separator
'push
)
251 (setq buf
(concat buf
252 (and buf
(or separator
", "))
253 (math-format-value (car res
) 1000))
257 (defvar calc-eval-error nil
258 "Determines how calc handles errors.
259 If nil, return a list containing the character position of error.
260 STRING means return error message as string rather than list.
261 The value t means abort and give an error message.")
263 (defun calc-eval-error (msg)
265 (if (eq calc-eval-error
'string
)
267 (error "%s" (nth 1 msg
)))
271 ;;;; Reading an expression in algebraic form.
274 (defun calc-auto-algebraic-entry (&optional prefix
)
276 (calc-algebraic-entry prefix t
))
279 (defun calc-algebraic-entry (&optional prefix auto
)
282 (let ((calc-language (if prefix nil calc-language
))
283 (math-expr-opers (if prefix
(math-standard-ops) (math-expr-ops))))
284 (calc-alg-entry (and auto
(char-to-string last-command-char
))))))
286 (defvar calc-alg-entry-history nil
287 "History for algebraic entry.")
290 (defun calc-alg-entry (&optional initial prompt
)
291 (let* ((sel-mode nil
)
292 (calc-dollar-values (mapcar 'calc-get-stack-element
293 (nthcdr calc-stack-top calc-stack
)))
296 (alg-exp (calc-do-alg-entry initial prompt t
'calc-alg-entry-history
)))
297 (if (stringp alg-exp
)
300 (calc-alg-edit alg-exp
))
301 (let* ((calc-simplify-mode (if (eq last-command-char ?\C-j
)
304 (nvals (mapcar 'calc-normalize alg-exp
)))
306 (calc-record (if (featurep 'calc-ext
) (car alg-exp
) (car nvals
))
308 (calc-pop-push-record-list calc-dollar-used
309 (and (not (equal (car alg-exp
)
314 (setq alg-exp
(cdr alg-exp
)
316 calc-dollar-used
0)))
317 (calc-handle-whys))))
319 (defvar calc-alg-ent-map nil
320 "The keymap used for algebraic entry.")
322 (defvar calc-alg-ent-esc-map nil
323 "The keymap used for escapes in algebraic entry.")
325 (defvar calc-alg-exp
)
328 (defun calc-do-alg-entry (&optional initial prompt no-normalize history
)
329 (let* ((calc-buffer (current-buffer))
330 (blink-paren-function 'calcAlg-blink-matching-open
)
331 (calc-alg-exp 'error
))
332 (unless calc-alg-ent-map
333 (setq calc-alg-ent-map
(copy-keymap minibuffer-local-map
))
334 (define-key calc-alg-ent-map
"'" 'calcAlg-previous
)
335 (define-key calc-alg-ent-map
"`" 'calcAlg-edit
)
336 (define-key calc-alg-ent-map
"\C-m" 'calcAlg-enter
)
337 (define-key calc-alg-ent-map
"\C-j" 'calcAlg-enter
)
339 (setq calc-alg-ent-esc-map
(copy-keymap esc-map
))
341 (aset (nth 1 calc-alg-ent-esc-map
) i
'calcAlg-escape
)
343 (define-key calc-alg-ent-map
"\e" nil
)
344 (if (eq calc-algebraic-mode
'total
)
345 (define-key calc-alg-ent-map
"\e" calc-alg-ent-esc-map
)
346 (define-key calc-alg-ent-map
"\e+" 'calcAlg-plus-minus
)
347 (define-key calc-alg-ent-map
"\em" 'calcAlg-mod
)
348 (define-key calc-alg-ent-map
"\e=" 'calcAlg-equals
)
349 (define-key calc-alg-ent-map
"\e\r" 'calcAlg-equals
)
350 (define-key calc-alg-ent-map
"\ep" 'previous-history-element
)
351 (define-key calc-alg-ent-map
"\en" 'next-history-element
)
352 (define-key calc-alg-ent-map
"\e%" 'self-insert-command
))
353 (setq calc-aborted-prefix nil
)
354 (let ((buf (read-from-minibuffer (or prompt
"Algebraic: ")
356 calc-alg-ent-map nil history
)))
357 (when (eq calc-alg-exp
'error
)
358 (when (eq (car-safe (setq calc-alg-exp
(math-read-exprs buf
))) 'error
)
359 (setq calc-alg-exp nil
)))
360 (setq calc-aborted-prefix
"alg'")
362 (and calc-alg-exp
(setq calc-alg-exp
(mapcar 'calc-normalize calc-alg-exp
))))
365 (defun calcAlg-plus-minus ()
367 (if (calc-minibuffer-contains ".* \\'")
371 (defun calcAlg-mod ()
373 (if (not (calc-minibuffer-contains ".* \\'"))
375 (if (calc-minibuffer-contains ".* mod +\\'")
376 (if calc-previous-modulo
377 (insert (math-format-flat-expr calc-previous-modulo
0))
381 (defun calcAlg-previous ()
383 (if (calc-minibuffer-contains "\\'")
384 (previous-history-element 1)
387 (defun calcAlg-equals ()
391 (if (consp calc-alg-exp
)
392 (progn (setq prefix-arg
(length calc-alg-exp
))
393 (calc-unread-command ?
=)))))
395 (defun calcAlg-escape ()
397 (calc-unread-command)
400 (use-local-map calc-mode-map
))
403 (defvar calc-plain-entry nil
)
404 (defun calcAlg-edit ()
406 (if (or (not calc-plain-entry
)
407 (calc-minibuffer-contains
408 "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
410 (setq calc-alg-exp
(minibuffer-contents))
415 (defun calcAlg-enter ()
417 (let* ((str (minibuffer-contents))
418 (exp (and (> (length str
) 0)
420 (set-buffer calc-buffer
)
421 (math-read-exprs str
)))))
422 (if (eq (car-safe exp
) 'error
)
424 (goto-char (minibuffer-prompt-end))
425 (forward-char (nth 1 exp
))
427 (calc-temp-minibuffer-message
428 (concat " [" (or (nth 2 exp
) "Error") "]"))
429 (calc-clear-unread-commands))
430 (setq calc-alg-exp
(if (calc-minibuffer-contains "\\` *\\[ *\\'")
435 (defun calcAlg-blink-matching-open ()
436 (let ((rightpt (point))
438 (rightchar (preceding-char))
444 (setq leftpt
(scan-sexps rightpt -
1)
445 leftchar
(char-after leftpt
))
448 (or (and (= rightchar ?\
))
450 (and (= rightchar ?\
])
454 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
455 (let ((leftsaved (aref (syntax-table) leftchar
))
456 (rightsaved (aref (syntax-table) rightchar
)))
459 (cond ((= leftchar ?\
[)
460 (aset (syntax-table) leftchar
(cons 4 ?\
)))
461 (aset (syntax-table) rightchar
(cons 5 ?\
[)))
463 (aset (syntax-table) leftchar
(cons 4 ?\
]))
464 (aset (syntax-table) rightchar
(cons 5 ?\
())))
465 (blink-matching-open))
466 (aset (syntax-table) leftchar leftsaved
)
467 (aset (syntax-table) rightchar rightsaved
)))
468 (blink-matching-open))))
471 (defun calc-alg-digit-entry ()
473 (cond ((eq last-command-char ?e
)
474 (if (> calc-number-radix
14) (format "%d.^" calc-number-radix
) "1e"))
475 ((eq last-command-char ?
#) (format "%d#" calc-number-radix
))
476 ((eq last-command-char ?_
) "-")
477 ((eq last-command-char ?
@) "0@ ")
478 (t (char-to-string last-command-char
)))))
480 ;; The variable calc-digit-value is initially declared in calc.el,
481 ;; but can be set by calcDigit-algebraic and calcDigit-edit.
482 (defvar calc-digit-value
)
485 (defun calcDigit-algebraic ()
487 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
489 (setq calc-digit-value
(minibuffer-contents))
493 (defun calcDigit-edit ()
495 (calc-unread-command)
496 (setq calc-digit-value
(minibuffer-contents))
500 ;;; Algebraic expression parsing. [Public]
502 (defvar math-read-replacement-list
504 ("±" "+/-") ; plus or minus
505 ("×" "*") ; multiplication sign
506 ("÷" ":") ; division sign
507 ("−" "-") ; subtraction sign
508 ("∕" "/") ; division sign
509 ("∗" "*") ; asterisk multiplication
510 ("∞" "inf") ; infinity symbol
564 "A list whose elements (old new) indicate replacements to make
565 in Calc algebraic input.")
567 (defvar math-read-superscripts
568 "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁽⁾ⁿⁱ" ; 0123456789+-()ni
569 "A string consisting of the superscripts allowed by Calc.")
571 (defvar math-read-subscripts
572 "₀₁₂₃₄₅₆₇₈₉₊₋₍₎" ; 0123456789+-()
573 "A string consisting of the subscripts allowed by Calc.")
576 (defun math-read-preprocess-string (str)
577 "Replace some substrings of STR by Calc equivalents."
579 (replace-regexp-in-string (concat "[" math-read-superscripts
"]+")
582 (replace-regexp-in-string (concat "[" math-read-subscripts
"]+")
584 (let ((rep-list math-read-replacement-list
))
587 (replace-regexp-in-string (nth 0 (car rep-list
))
588 (nth 1 (car rep-list
)) str
))
589 (setq rep-list
(cdr rep-list
))))
592 ;; The next few variables are local to math-read-exprs (and math-read-expr
593 ;; in calc-ext.el), but are set in functions they call.
595 (defvar math-exp-pos
)
596 (defvar math-exp-str
)
597 (defvar math-exp-old-pos
)
598 (defvar math-exp-token
)
599 (defvar math-exp-keep-spaces
)
600 (defvar math-expr-data
)
603 (defun math-read-exprs (math-exp-str)
604 (let ((math-exp-pos 0)
606 (math-exp-keep-spaces nil
)
607 math-exp-token math-expr-data
)
608 (setq math-exp-str
(math-read-preprocess-string math-exp-str
))
609 (if (memq calc-language calc-lang-allow-percentsigns
)
610 (setq math-exp-str
(math-remove-percentsigns math-exp-str
)))
611 (if calc-language-input-filter
612 (setq math-exp-str
(funcall calc-language-input-filter math-exp-str
)))
613 (while (setq math-exp-token
614 (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str
))
616 (concat (substring math-exp-str
0 math-exp-token
) "\\dots"
617 (substring math-exp-str
(+ math-exp-token
2)))))
618 (math-build-parse-table)
620 (let ((val (catch 'syntax
(math-read-expr-list))))
622 (list 'error math-exp-old-pos val
)
623 (if (equal math-exp-token
'end
)
625 (list 'error math-exp-old-pos
"Syntax error"))))))
628 (defun math-read-expr-list ()
629 (let* ((math-exp-keep-spaces nil
)
630 (val (list (math-read-expr-level 0)))
632 (while (equal math-expr-data
",")
634 (let ((rest (list (math-read-expr-level 0))))
639 (defvar calc-user-parse-table nil
)
640 (defvar calc-last-main-parse-table nil
)
641 (defvar calc-last-user-lang-parse-table nil
)
642 (defvar calc-last-lang-parse-table nil
)
643 (defvar calc-user-tokens nil
)
644 (defvar calc-user-token-chars nil
)
646 (defvar math-toks nil
647 "Tokens to pass between math-build-parse-table and math-find-user-tokens.")
650 (defun math-build-parse-table ()
651 (let ((mtab (cdr (assq nil calc-user-parse-tables
)))
652 (ltab (cdr (assq calc-language calc-user-parse-tables
)))
653 (lltab (get calc-language
'math-parse-table
)))
654 (or (and (eq mtab calc-last-main-parse-table
)
655 (eq ltab calc-last-user-lang-parse-table
)
656 (eq lltab calc-last-lang-parse-table
))
657 (let ((p (append mtab ltab lltab
))
659 (setq calc-user-parse-table p
)
660 (setq calc-user-token-chars nil
)
662 (math-find-user-tokens (car (car p
)))
664 (setq calc-user-tokens
(mapconcat 'identity
665 (sort (mapcar 'car math-toks
)
666 (function (lambda (x y
)
670 calc-last-main-parse-table mtab
671 calc-last-user-lang-parse-table ltab
672 calc-last-lang-parse-table lltab
)))))
675 (defun math-find-user-tokens (p)
677 (cond ((and (stringp (car p
))
678 (or (> (length (car p
)) 1) (equal (car p
) "$")
679 (equal (car p
) "\""))
680 (string-match "[^a-zA-Z0-9]" (car p
)))
681 (let ((s (regexp-quote (car p
))))
682 (if (string-match "\\`[a-zA-Z0-9]" s
)
683 (setq s
(concat "\\<" s
)))
684 (if (string-match "[a-zA-Z0-9]\\'" s
)
685 (setq s
(concat s
"\\>")))
686 (or (assoc s math-toks
)
688 (setq math-toks
(cons (list s
) math-toks
))
689 (or (memq (aref (car p
) 0) calc-user-token-chars
)
690 (setq calc-user-token-chars
691 (cons (aref (car p
) 0)
692 calc-user-token-chars
)))))))
694 (math-find-user-tokens (nth 1 (car p
)))
695 (or (eq (car (car p
)) '\?)
696 (math-find-user-tokens (nth 2 (car p
))))))
700 (defun math-read-token ()
701 (if (>= math-exp-pos
(length math-exp-str
))
702 (setq math-exp-old-pos math-exp-pos
704 math-expr-data
"\000")
706 (ch (aref math-exp-str math-exp-pos
)))
707 (setq math-exp-old-pos math-exp-pos
)
708 (cond ((memq ch
'(32 10 9))
709 (setq math-exp-pos
(1+ math-exp-pos
))
710 (if math-exp-keep-spaces
711 (setq math-exp-token
'space
714 ((and (memq ch calc-user-token-chars
)
715 (let ((case-fold-search nil
))
717 calc-user-tokens math-exp-str math-exp-pos
)
719 (setq math-exp-token
'punc
720 math-expr-data
(math-match-substring math-exp-str
0)
721 math-exp-pos
(match-end 0)))
722 ((or (and (>= ch ?a
) (<= ch ?z
))
723 (and (>= ch ?A
) (<= ch ?Z
)))
726 ((and (memq calc-language calc-lang-allow-underscores
)
727 (memq calc-language calc-lang-allow-percentsigns
))
729 ((memq calc-language calc-lang-allow-underscores
)
731 (t "[a-zA-Z0-9'#]*"))
732 math-exp-str math-exp-pos
)
733 (setq math-exp-token
'symbol
734 math-exp-pos
(match-end 0)
735 math-expr-data
(math-restore-dashes
736 (math-match-substring math-exp-str
0)))
737 (if (setq adfn
(get calc-language
'math-lang-adjust-words
))
739 ((or (and (>= ch ?
0) (<= ch ?
9))
741 (eq (string-match "\\.[0-9]" math-exp-str math-exp-pos
)
744 (eq (string-match "_\\.?[0-9]" math-exp-str math-exp-pos
)
746 (or (eq math-exp-pos
0)
747 (and (not (memq calc-language
748 calc-lang-allow-underscores
))
749 (eq (string-match "[^])}\"a-zA-Z0-9'$]_"
750 math-exp-str
(1- math-exp-pos
))
751 (1- math-exp-pos
))))))
752 (or (and (memq calc-language calc-lang-c-type-hex
)
753 (string-match "0[xX][0-9a-fA-F]+" math-exp-str math-exp-pos
))
754 (string-match "_?\\([0-9]+.?0*@ *\\)?\\([0-9]+.?0*' *\\)?\\(0*\\([2-9]\\|1[0-4]\\)\\(#\\|\\^\\^\\)[0-9a-dA-D.]+[eE][-+_]?[0-9]+\\|0*\\([2-9]\\|[0-2][0-9]\\|3[0-6]\\)\\(#\\|\\^\\^\\)[0-9a-zA-Z:.]+\\|[0-9]+:[0-9:]+\\|[0-9.]+\\([eE][-+_]?[0-9]+\\)?\"?\\)?"
755 math-exp-str math-exp-pos
))
756 (setq math-exp-token
'number
757 math-expr-data
(math-match-substring math-exp-str
0)
758 math-exp-pos
(match-end 0)))
760 (assq ch
(get calc-language
'math-lang-read-symbol
)))
764 (if (eq (string-match "\\$\\([1-9][0-9]*\\)" math-exp-str math-exp-pos
)
766 (setq math-expr-data
(- (string-to-number (math-match-substring
768 (string-match "\\$+" math-exp-str math-exp-pos
)
769 (setq math-expr-data
(- (match-end 0) (match-beginning 0))))
770 (setq math-exp-token
'dollar
771 math-exp-pos
(match-end 0)))
773 (if (eq (string-match "#\\([1-9][0-9]*\\)" math-exp-str math-exp-pos
)
775 (setq math-expr-data
(string-to-number
776 (math-match-substring math-exp-str
1))
777 math-exp-pos
(match-end 0))
778 (setq math-expr-data
1
779 math-exp-pos
(1+ math-exp-pos
)))
780 (setq math-exp-token
'hash
))
781 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
782 math-exp-str math-exp-pos
)
784 (setq math-exp-token
'punc
785 math-expr-data
(math-match-substring math-exp-str
0)
786 math-exp-pos
(match-end 0)))
788 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
789 math-exp-str math-exp-pos
))
790 (setq math-exp-token
'string
791 math-expr-data
(math-match-substring math-exp-str
1)
792 math-exp-pos
(match-end 0)))
793 ((and (setq adfn
(get calc-language
'math-lang-read
))
795 (eval (nth 1 adfn
))))
796 ((eq (string-match "%%.*$" math-exp-str math-exp-pos
) math-exp-pos
)
797 (setq math-exp-pos
(match-end 0))
800 (if (setq adfn
(assq ch
(get calc-language
'math-punc-table
)))
801 (setq ch
(cdr adfn
)))
802 (setq math-exp-token
'punc
803 math-expr-data
(char-to-string ch
)
804 math-exp-pos
(1+ math-exp-pos
)))))))
806 (defconst math-alg-inequalities
807 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
808 calcFunc-eq calcFunc-neq
))
810 (defun math-read-expr-level (exp-prec &optional exp-term
)
811 (let* ((math-expr-opers (math-expr-ops))
812 (x (math-read-factor))
815 (while (and (or (and calc-user-parse-table
816 (setq op
(calc-check-user-syntax x exp-prec
))
818 op
'("2x" ident
999999 -
1)))
819 (and (setq op
(assoc math-expr-data math-expr-opers
))
821 (or (and (setq op2
(assoc
823 (cdr (memq op math-expr-opers
))))
824 (eq (= (nth 3 op
) -
1)
826 (eq (= (nth 3 op2
) -
1)
827 (not (math-factor-after)))
830 (and (or (eq (nth 2 op
) -
1)
831 (memq math-exp-token
'(symbol number dollar hash
))
832 (equal math-expr-data
"(")
833 (and (equal math-expr-data
"[")
836 'math-function-open
) "["))
837 (not (and math-exp-keep-spaces
838 (eq (car-safe x
) 'vec
)))))
839 (or (not (setq op
(assoc math-expr-data math-expr-opers
)))
841 (or (not calc-user-parse-table
)
842 (not (eq math-exp-token
'symbol
))
843 (let ((p calc-user-parse-table
))
846 (car (car (car p
)))))
848 (nth 1 (car (car p
)))
852 (setq op
(assoc "2x" math-expr-opers
))))
853 (not (and exp-term
(equal math-expr-data exp-term
)))
854 (>= (nth 2 op
) exp-prec
))
855 (if (not (equal (car op
) "2x"))
857 (and (memq (nth 1 op
) '(sdev mod
))
859 (setq x
(cond ((consp (nth 1 op
))
860 (funcall (car (nth 1 op
)) x op
))
862 (if (eq (nth 1 op
) 'ident
)
864 (if (eq (nth 1 op
) 'closing
)
865 (if (eq (nth 2 op
) exp-prec
)
869 (throw 'syntax
"Mismatched delimiters"))
870 (list (nth 1 op
) x
))))
872 (memq (nth 1 op
) math-alg-inequalities
)
873 (memq (car-safe x
) math-alg-inequalities
))
875 (math-composite-inequalities x op
))
878 (math-read-expr-level (nth 3 op
) exp-term
))))
882 ;; calc-arg-values is defined in calc-ext.el, but is used here.
883 (defvar calc-arg-values
)
886 (defun calc-check-user-syntax (&optional x prec
)
887 (let ((p calc-user-parse-table
)
892 (setq rule
(car (car p
)))
894 (and (integerp (car rule
))
896 (equal math-expr-data
897 (car (setq rule
(cdr rule
)))))
898 (equal math-expr-data
(car rule
)))))
899 (let ((save-exp-pos math-exp-pos
)
900 (save-exp-old-pos math-exp-old-pos
)
901 (save-exp-token math-exp-token
)
902 (save-exp-data math-expr-data
))
904 (setq matches
(calc-match-user-syntax rule
))))
911 (setq matches
(cons x matches
)))
912 (setq match
(cdr (car p
)))
913 (while (and (eq (car-safe match
)
915 (= (length match
) 3))
916 (setq conds
(append (math-flatten-lands
919 match
(nth 1 match
)))
920 (while (and conds match
)
922 (cond ((eq (car-safe (car conds
))
924 (setq temp
(car conds
))
925 (or (= (length temp
) 3)
926 (and (= (length temp
) 2)
927 (eq (car-safe (nth 1 temp
))
929 (= (length (nth 1 temp
)) 3)
930 (setq temp
(nth 1 temp
)))
938 args
(cons (nth 1 temp
)
940 ((and (eq (car-safe (car conds
))
942 (= (length (car conds
)) 3))
943 (setq temp
(calcFunc-vmatches
947 (nth 2 (car conds
))))
950 (while (setq temp
(cdr temp
))
951 (setq matches
(cons (nth 2 (car temp
))
953 args
(cons (nth 1 (car temp
))
956 (or (math-is-true (math-simplify
961 (setq conds
(cdr conds
)))
963 (not (setq match
(math-multi-subst
964 match args matches
)))
965 (setq math-exp-old-pos save-exp-old-pos
966 math-exp-token save-exp-token
967 math-expr-data save-exp-data
968 math-exp-pos save-exp-pos
)))))))
973 (defun calc-match-user-syntax (p &optional term
)
975 (save-exp-pos math-exp-pos
)
976 (save-exp-old-pos math-exp-old-pos
)
977 (save-exp-token math-exp-token
)
978 (save-exp-data math-expr-data
)
981 (cond ((stringp (car p
))
982 (and (equal math-expr-data
(car p
))
987 (and (setq m
(catch 'syntax
988 (math-read-expr-level
991 (if (consp (nth 1 p
))
992 (car (nth 1 (nth 1 p
)))
996 (setq matches
(nconc matches
(list m
)))))
997 ((eq (car (car p
)) '\?)
998 (setq m
(calc-match-user-syntax (nth 1 (car p
))))
1003 (cons 'vec
(and (listp m
) m
))))))
1004 (or (listp m
) (not (nth 2 (car p
)))
1005 (not (eq (aref (car (nth 2 (car p
))) 0) ?\$
))
1006 (eq math-exp-token
'end
)))
1008 (setq m
(calc-match-user-syntax (nth 1 (car p
))
1009 (car (nth 2 (car p
)))))
1011 (let ((vec (cons 'vec m
))
1014 (setq opos math-exp-pos
1015 mm
(calc-match-user-syntax
1018 (car (nth 2 (car p
))))))
1019 (> math-exp-pos opos
))
1020 (setq vec
(nconc vec mm
)))
1021 (setq matches
(nconc matches
(list vec
))))
1022 (and (eq (car (car p
)) '*)
1023 (setq matches
(nconc matches
(list '(vec)))))))))
1026 (setq math-exp-pos save-exp-pos
1027 math-exp-old-pos save-exp-old-pos
1028 math-exp-token save-exp-token
1029 math-expr-data save-exp-data
1034 (defun math-remove-dashes (x)
1035 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x
)
1037 (concat (math-match-substring x
1) "#" (math-match-substring x
2)))
1040 (defun math-remove-percentsigns (x)
1041 (if (string-match "\\`\\(.*\\)%\\(.*\\)\\'" x
)
1042 (math-remove-percentsigns
1043 (concat (math-match-substring x
1) "o'o" (math-match-substring x
2)))
1046 (defun math-restore-dashes (x)
1047 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x
)
1048 (math-restore-dashes
1049 (concat (math-match-substring x
1) "-" (math-match-substring x
2)))
1052 (defun math-restore-placeholders (x)
1053 "Replace placeholders by the proper characters in the symbol x.
1054 This includes `#' for `_' and `'' for `%'.
1055 If the current Calc language does not use placeholders, return nil."
1056 (if (or (memq calc-language calc-lang-allow-underscores
)
1057 (memq calc-language calc-lang-allow-percentsigns
))
1058 (let ((sx (symbol-name x
)))
1059 (when (memq calc-language calc-lang-allow-percentsigns
)
1061 (setq sx
(math-to-percentsigns sx
)))
1062 (if (memq calc-language calc-lang-allow-underscores
)
1063 (setq sx
(math-string-restore-underscores sx
)))
1066 (defun math-string-restore-underscores (x)
1067 "Replace pound signs by underscores in the string x."
1068 (if (string-match "\\`\\(.*\\)#\\(.*\\)\\'" x
)
1069 (math-string-restore-underscores
1070 (concat (math-match-substring x
1) "_" (math-match-substring x
2)))
1074 (defun math-read-if (cond op
)
1075 (let ((then (math-read-expr-level 0)))
1076 (or (equal math-expr-data
":")
1077 (throw 'syntax
"Expected ':'"))
1079 (list 'calcFunc-if cond then
(math-read-expr-level (nth 3 op
)))))
1081 (defun math-factor-after ()
1082 (let ((math-exp-pos math-exp-pos
)
1083 math-exp-old-pos math-exp-token math-expr-data
)
1085 (or (memq math-exp-token
'(number symbol dollar hash string
))
1086 (and (assoc math-expr-data
'(("-") ("+") ("!") ("|") ("/")))
1087 (assoc (concat "u" math-expr-data
) math-expr-opers
))
1088 (eq (nth 2 (assoc math-expr-data math-expr-opers
)) -
1)
1089 (assoc math-expr-data
'(("(") ("[") ("{"))))))
1091 (defun math-read-factor ()
1092 (let ((math-expr-opers (math-expr-ops))
1094 (cond ((eq math-exp-token
'number
)
1095 (let ((num (math-read-number math-expr-data
)))
1098 (setq math-exp-old-pos math-exp-pos
)
1099 (throw 'syntax
"Bad format")))
1101 (if (and math-read-expr-quotes
1105 ((and calc-user-parse-table
1106 (setq op
(calc-check-user-syntax)))
1108 ((or (equal math-expr-data
"-")
1109 (equal math-expr-data
"+")
1110 (equal math-expr-data
"!")
1111 (equal math-expr-data
"|")
1112 (equal math-expr-data
"/"))
1113 (setq math-expr-data
(concat "u" math-expr-data
))
1115 ((and (setq op
(assoc math-expr-data math-expr-opers
))
1117 (if (consp (nth 1 op
))
1118 (funcall (car (nth 1 op
)) op
)
1120 (let ((val (math-read-expr-level (nth 3 op
))))
1121 (cond ((eq (nth 1 op
) 'ident
)
1123 ((and (Math-numberp val
)
1124 (equal (car op
) "u-"))
1126 (t (list (nth 1 op
) val
))))))
1127 ((eq math-exp-token
'symbol
)
1128 (let ((sym (intern math-expr-data
)))
1130 (if (equal math-expr-data calc-function-open
)
1131 (let ((f (assq sym math-expr-function-mapping
)))
1134 (funcall (car (cdr f
)) f sym
)
1135 (let ((args (if (or (equal math-expr-data calc-function-close
)
1136 (eq math-exp-token
'end
))
1138 (math-read-expr-list))))
1139 (if (not (or (equal math-expr-data calc-function-close
)
1140 (eq math-exp-token
'end
)))
1141 (throw 'syntax
"Expected `)'"))
1143 (if (and (memq calc-language
1144 calc-lang-parens-are-subscripts
)
1147 (let ((calc-matrix-mode 'scalar
))
1152 (symbol-name sym
)))))))
1153 (math-parse-fortran-subscr sym args
)
1156 (and (= (aref (symbol-name sym
) 0) ?
\\)
1157 (< (prefix-numeric-value calc-language-option
)
1159 (setq sym
(intern (substring (symbol-name sym
)
1161 (or (string-match "-" (symbol-name sym
))
1164 (symbol-name sym
))))))
1166 (if math-read-expr-quotes
1168 (let ((val (list 'var
1169 (intern (math-remove-dashes
1171 (if (string-match "-" (symbol-name sym
))
1173 (intern (concat "var-"
1174 (symbol-name sym
)))))))
1176 (assq (nth 1 val
) math-expr-variable-mapping
)
1177 (assq (math-restore-placeholders (nth 1 val
))
1178 math-expr-variable-mapping
))))
1179 (and v
(setq val
(if (consp (cdr v
))
1180 (funcall (car (cdr v
)) v val
)
1183 (substring (symbol-name (cdr v
))
1186 (while (and (memq calc-language
1187 calc-lang-brackets-are-subscripts
)
1188 (equal math-expr-data
"["))
1190 (let ((el (math-read-expr-list)))
1192 (setq val
(append (list 'calcFunc-subscr val
)
1194 (setq el
(cdr el
))))
1195 (if (equal math-expr-data
"]")
1197 (throw 'syntax
"Expected ']'")))
1199 ((eq math-exp-token
'dollar
)
1200 (let ((abs (if (> math-expr-data
0) math-expr-data
(- math-expr-data
))))
1201 (if (>= (length calc-dollar-values
) abs
)
1202 (let ((num math-expr-data
))
1204 (setq calc-dollar-used
(max calc-dollar-used num
))
1205 (math-check-complete (nth (1- abs
) calc-dollar-values
)))
1206 (throw 'syntax
(if calc-dollar-values
1208 "$'s not allowed in this context")))))
1209 ((eq math-exp-token
'hash
)
1210 (or calc-hashes-used
1211 (throw 'syntax
"#'s not allowed in this context"))
1213 (if (<= math-expr-data
(length calc-arg-values
))
1214 (let ((num math-expr-data
))
1216 (setq calc-hashes-used
(max calc-hashes-used num
))
1217 (nth (1- num
) calc-arg-values
))
1218 (throw 'syntax
"Too many # arguments")))
1219 ((equal math-expr-data
"(")
1220 (let* ((exp (let ((math-exp-keep-spaces nil
))
1222 (if (or (equal math-expr-data
"\\dots")
1223 (equal math-expr-data
"\\ldots"))
1224 '(neg (var inf var-inf
))
1225 (math-read-expr-level 0)))))
1226 (let ((math-exp-keep-spaces nil
))
1228 ((equal math-expr-data
",")
1231 (let ((exp2 (math-read-expr-level 0)))
1233 (if (and exp2
(Math-realp exp
) (Math-realp exp2
))
1234 (math-normalize (list 'cplx exp exp2
))
1235 (list '+ exp
(list '* exp2
'(var i var-i
))))))))
1236 ((equal math-expr-data
";")
1239 (let ((exp2 (math-read-expr-level 0)))
1240 (setq exp
(if (and exp2
(Math-realp exp
)
1242 (math-normalize (list 'polar exp exp2
))
1247 (math-to-radians-2 exp2
)
1248 '(var i var-i
)))))))))
1249 ((or (equal math-expr-data
"\\dots")
1250 (equal math-expr-data
"\\ldots"))
1253 (let ((exp2 (if (or (equal math-expr-data
")")
1254 (equal math-expr-data
"]")
1255 (eq math-exp-token
'end
))
1257 (math-read-expr-level 0))))
1260 (if (equal math-expr-data
")") 0 1)
1263 (if (not (or (equal math-expr-data
")")
1264 (and (equal math-expr-data
"]") (eq (car-safe exp
) 'intv
))
1265 (eq math-exp-token
'end
)))
1266 (throw 'syntax
"Expected `)'"))
1269 ((eq math-exp-token
'string
)
1272 ((equal math-expr-data
"[")
1274 (math-read-brackets t
"]"))
1275 ((equal math-expr-data
"{")
1277 (math-read-brackets nil
"}"))
1278 ((equal math-expr-data
"<")
1280 (math-read-angle-brackets))
1281 (t (throw 'syntax
"Expected a number")))))
1283 (provide 'calc-aent
)
1286 ;; generated-autoload-file: "calc-loaddefs.el"
1289 ;; arch-tag: 5599e45d-e51e-44bb-9a20-9f4ed8c96c32
1290 ;;; calc-aent.el ends here