Commit | Line | Data |
---|---|---|
3132f345 CW |
1 | ;;; calc-aent.el --- algebraic entry functions for Calc |
2 | ||
ba318903 | 3 | ;; Copyright (C) 1990-1993, 2001-2014 Free Software Foundation, Inc. |
3132f345 CW |
4 | |
5 | ;; Author: Dave Gillespie <daveg@synaptics.com> | |
e8fff8ed | 6 | ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com> |
136211a9 EZ |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
662c9c64 | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
7c671b23 | 11 | ;; it under the terms of the GNU General Public License as published by |
662c9c64 GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
7c671b23 | 14 | |
136211a9 | 15 | ;; GNU Emacs is distributed in the hope that it will be useful, |
7c671b23 GM |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
662c9c64 | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
136211a9 | 22 | |
3132f345 | 23 | ;;; Commentary: |
136211a9 | 24 | |
3132f345 | 25 | ;;; Code: |
136211a9 EZ |
26 | |
27 | ;; This file is autoloaded from calc.el. | |
136211a9 | 28 | |
5eb3eeda | 29 | (require 'calc) |
136211a9 EZ |
30 | (require 'calc-macs) |
31 | ||
2f78f904 JB |
32 | ;; Declare functions which are defined elsewhere. |
33 | (declare-function calc-refresh-evaltos "calc-ext" (&optional which-var)) | |
34 | (declare-function calc-execute-kbd-macro "calc-prog" (mac arg &rest prefix)) | |
35 | (declare-function math-is-true "calc-ext" (expr)) | |
36 | (declare-function calc-explain-why "calc-stuff" (why &optional more)) | |
37 | (declare-function calc-alg-edit "calc-yank" (str)) | |
38 | (declare-function math-composite-inequalities "calc-prog" (x op)) | |
39 | (declare-function math-flatten-lands "calc-rewr" (expr)) | |
40 | (declare-function math-multi-subst "calc-map" (expr olds news)) | |
41 | (declare-function calcFunc-vmatches "calc-rewr" (expr pat)) | |
42 | (declare-function math-simplify "calc-alg" (top-expr)) | |
43 | (declare-function math-known-matrixp "calc-arith" (a)) | |
44 | (declare-function math-parse-fortran-subscr "calc-lang" (sym args)) | |
f20def1f | 45 | (declare-function math-to-radians-2 "calc-math" (a &optional force-symbolic)) |
2f78f904 JB |
46 | (declare-function math-read-string "calc-ext" ()) |
47 | (declare-function math-read-brackets "calc-vec" (space-sep math-rb-close)) | |
48 | (declare-function math-read-angle-brackets "calc-forms" ()) | |
d039d4d8 | 49 | (declare-function math-to-percentsigns "calccomp" (x)) |
2f78f904 | 50 | |
0b504d64 JB |
51 | (defvar calc-quick-calc-history nil |
52 | "The history list for quick-calc.") | |
53 | ||
2378f044 | 54 | ;;;###autoload |
136211a9 | 55 | (defun calc-do-quick-calc () |
0b504d64 | 56 | (require 'calc-ext) |
136211a9 EZ |
57 | (calc-check-defines) |
58 | (if (eq major-mode 'calc-mode) | |
59 | (calc-algebraic-entry t) | |
60 | (let (buf shortbuf) | |
61 | (save-excursion | |
62 | (calc-create-buffer) | |
63 | (let* ((calc-command-flags nil) | |
64 | (calc-dollar-values calc-quick-prev-results) | |
65 | (calc-dollar-used 0) | |
66 | (enable-recursive-minibuffers t) | |
67 | (calc-language (if (memq calc-language '(nil big)) | |
68 | 'flat calc-language)) | |
0b504d64 JB |
69 | (entry (calc-do-alg-entry "" "Quick calc: " t 'calc-quick-calc-history)) |
70 | (alg-exp (mapcar 'math-evaluate-expr entry))) | |
3132f345 CW |
71 | (when (and (= (length alg-exp) 1) |
72 | (eq (car-safe (car alg-exp)) 'calcFunc-assign) | |
73 | (= (length (car alg-exp)) 3) | |
74 | (eq (car-safe (nth 1 (car alg-exp))) 'var)) | |
3132f345 CW |
75 | (set (nth 2 (nth 1 (car alg-exp))) (nth 2 (car alg-exp))) |
76 | (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp)))) | |
77 | (setq alg-exp (list (nth 2 (car alg-exp))))) | |
136211a9 EZ |
78 | (setq calc-quick-prev-results alg-exp |
79 | buf (mapconcat (function (lambda (x) | |
80 | (math-format-value x 1000))) | |
81 | alg-exp | |
82 | " ") | |
83 | shortbuf buf) | |
84 | (if (and (= (length alg-exp) 1) | |
85 | (memq (car-safe (car alg-exp)) '(nil bigpos bigneg)) | |
86 | (< (length buf) 20) | |
87 | (= calc-number-radix 10)) | |
88 | (setq buf (concat buf " (" | |
89 | (let ((calc-number-radix 16)) | |
90 | (math-format-value (car alg-exp) 1000)) | |
91 | ", " | |
92 | (let ((calc-number-radix 8)) | |
93 | (math-format-value (car alg-exp) 1000)) | |
8f26ce73 JB |
94 | ", " |
95 | (let ((calc-number-radix 2)) | |
96 | (math-format-value (car alg-exp) 1000)) | |
136211a9 EZ |
97 | (if (and (integerp (car alg-exp)) |
98 | (> (car alg-exp) 0) | |
99 | (< (car alg-exp) 127)) | |
100 | (format ", \"%c\"" (car alg-exp)) | |
101 | "") | |
102 | ")"))) | |
f20be8fe | 103 | (if (and (< (length buf) (frame-width)) (= (length entry) 1) |
5eb3eeda | 104 | (featurep 'calc-ext)) |
136211a9 EZ |
105 | (let ((long (concat (math-format-value (car entry) 1000) |
106 | " => " buf))) | |
f20be8fe | 107 | (if (<= (length long) (- (frame-width) 8)) |
136211a9 EZ |
108 | (setq buf long)))) |
109 | (calc-handle-whys) | |
110 | (message "Result: %s" buf))) | |
e93c003e | 111 | (if (eq last-command-event 10) |
136211a9 | 112 | (insert shortbuf) |
3cedbf72 | 113 | (kill-new shortbuf))))) |
136211a9 | 114 | |
2378f044 | 115 | ;;;###autoload |
136211a9 EZ |
116 | (defun calc-do-calc-eval (str separator args) |
117 | (calc-check-defines) | |
118 | (catch 'calc-error | |
119 | (save-excursion | |
120 | (calc-create-buffer) | |
121 | (cond | |
122 | ((and (consp str) (not (symbolp (car str)))) | |
123 | (let ((calc-language nil) | |
16819e48 | 124 | (math-expr-opers (math-standard-ops)) |
136211a9 EZ |
125 | (calc-internal-prec 12) |
126 | (calc-word-size 32) | |
127 | (calc-symbolic-mode nil) | |
128 | (calc-matrix-mode nil) | |
129 | (calc-angle-mode 'deg) | |
130 | (calc-number-radix 10) | |
8768b132 | 131 | (calc-twos-complement-mode nil) |
136211a9 EZ |
132 | (calc-leading-zeros nil) |
133 | (calc-group-digits nil) | |
134 | (calc-point-char ".") | |
135 | (calc-frac-format '(":" nil)) | |
136 | (calc-prefer-frac nil) | |
137 | (calc-hms-format "%s@ %s' %s\"") | |
138 | (calc-date-format '((H ":" mm C SS pp " ") | |
139 | Www " " Mmm " " D ", " YYYY)) | |
140 | (calc-float-format '(float 0)) | |
141 | (calc-full-float-format '(float 0)) | |
142 | (calc-complex-format nil) | |
143 | (calc-matrix-just nil) | |
144 | (calc-full-vectors t) | |
145 | (calc-break-vectors nil) | |
146 | (calc-vector-commas ",") | |
147 | (calc-vector-brackets "[]") | |
148 | (calc-matrix-brackets '(R O)) | |
149 | (calc-complex-mode 'cplx) | |
150 | (calc-infinite-mode nil) | |
151 | (calc-display-strings nil) | |
152 | (calc-simplify-mode nil) | |
153 | (calc-display-working-message 'lots) | |
154 | (strp (cdr str))) | |
155 | (while strp | |
156 | (set (car strp) (nth 1 strp)) | |
157 | (setq strp (cdr (cdr strp)))) | |
158 | (calc-do-calc-eval (car str) separator args))) | |
159 | ((eq separator 'eval) | |
160 | (eval str)) | |
161 | ((eq separator 'macro) | |
5eb3eeda | 162 | (require 'calc-ext) |
136211a9 EZ |
163 | (let* ((calc-buffer (current-buffer)) |
164 | (calc-window (get-buffer-window calc-buffer)) | |
165 | (save-window (selected-window))) | |
166 | (if calc-window | |
167 | (unwind-protect | |
168 | (progn | |
169 | (select-window calc-window) | |
170 | (calc-execute-kbd-macro str nil (car args))) | |
171 | (and (window-point save-window) | |
172 | (select-window save-window))) | |
173 | (save-window-excursion | |
174 | (select-window (get-largest-window)) | |
175 | (switch-to-buffer calc-buffer) | |
176 | (calc-execute-kbd-macro str nil (car args))))) | |
177 | nil) | |
178 | ((eq separator 'pop) | |
179 | (or (not (integerp str)) | |
180 | (= str 0) | |
181 | (calc-pop (min str (calc-stack-size)))) | |
182 | (calc-stack-size)) | |
183 | ((eq separator 'top) | |
184 | (and (integerp str) | |
185 | (> str 0) | |
186 | (<= str (calc-stack-size)) | |
187 | (math-format-value (calc-top-n str (car args)) 1000))) | |
188 | ((eq separator 'rawtop) | |
189 | (and (integerp str) | |
190 | (> str 0) | |
191 | (<= str (calc-stack-size)) | |
192 | (calc-top-n str (car args)))) | |
193 | (t | |
194 | (let* ((calc-command-flags nil) | |
195 | (calc-next-why nil) | |
196 | (calc-language (if (memq calc-language '(nil big)) | |
197 | 'flat calc-language)) | |
198 | (calc-dollar-values (mapcar | |
199 | (function | |
200 | (lambda (x) | |
201 | (if (stringp x) | |
202 | (progn | |
203 | (setq x (math-read-exprs x)) | |
204 | (if (eq (car-safe x) | |
205 | 'error) | |
206 | (throw 'calc-error | |
207 | (calc-eval-error | |
208 | (cdr x))) | |
209 | (car x))) | |
210 | x))) | |
211 | args)) | |
212 | (calc-dollar-used 0) | |
213 | (res (if (stringp str) | |
214 | (math-read-exprs str) | |
215 | (list str))) | |
216 | buf) | |
217 | (if (eq (car res) 'error) | |
218 | (calc-eval-error (cdr res)) | |
219 | (setq res (mapcar 'calc-normalize res)) | |
220 | (and (memq 'clear-message calc-command-flags) | |
221 | (message "")) | |
222 | (cond ((eq separator 'pred) | |
5eb3eeda | 223 | (require 'calc-ext) |
136211a9 EZ |
224 | (if (= (length res) 1) |
225 | (math-is-true (car res)) | |
226 | (calc-eval-error '(0 "Single value expected")))) | |
227 | ((eq separator 'raw) | |
228 | (if (= (length res) 1) | |
229 | (car res) | |
230 | (calc-eval-error '(0 "Single value expected")))) | |
231 | ((eq separator 'list) | |
232 | res) | |
233 | ((memq separator '(num rawnum)) | |
234 | (if (= (length res) 1) | |
235 | (if (math-constp (car res)) | |
236 | (if (eq separator 'num) | |
237 | (math-format-value (car res) 1000) | |
238 | (car res)) | |
239 | (calc-eval-error | |
240 | (list 0 | |
241 | (if calc-next-why | |
242 | (calc-explain-why (car calc-next-why)) | |
243 | "Number expected")))) | |
244 | (calc-eval-error '(0 "Single value expected")))) | |
245 | ((eq separator 'push) | |
246 | (calc-push-list res) | |
247 | nil) | |
248 | (t (while res | |
249 | (setq buf (concat buf | |
250 | (and buf (or separator ", ")) | |
251 | (math-format-value (car res) 1000)) | |
252 | res (cdr res))) | |
07ff2bc8 | 253 | buf))))))))) |
136211a9 | 254 | |
d37f2979 JB |
255 | (defvar calc-eval-error nil |
256 | "Determines how calc handles errors. | |
8f2904a5 | 257 | If nil, return a list containing the character position of error. |
d37f2979 | 258 | STRING means return error message as string rather than list. |
8f2904a5 | 259 | The value t means abort and give an error message.") |
d37f2979 | 260 | |
136211a9 | 261 | (defun calc-eval-error (msg) |
d37f2979 | 262 | (if calc-eval-error |
136211a9 EZ |
263 | (if (eq calc-eval-error 'string) |
264 | (nth 1 msg) | |
265 | (error "%s" (nth 1 msg))) | |
07ff2bc8 | 266 | msg)) |
136211a9 EZ |
267 | |
268 | ||
269 | ;;;; Reading an expression in algebraic form. | |
270 | ||
2378f044 | 271 | ;;;###autoload |
136211a9 EZ |
272 | (defun calc-auto-algebraic-entry (&optional prefix) |
273 | (interactive "P") | |
07ff2bc8 | 274 | (calc-algebraic-entry prefix t)) |
136211a9 | 275 | |
2378f044 | 276 | ;;;###autoload |
136211a9 EZ |
277 | (defun calc-algebraic-entry (&optional prefix auto) |
278 | (interactive "P") | |
279 | (calc-wrapper | |
280 | (let ((calc-language (if prefix nil calc-language)) | |
16819e48 | 281 | (math-expr-opers (if prefix (math-standard-ops) (math-expr-ops)))) |
e93c003e | 282 | (calc-alg-entry (and auto (char-to-string last-command-event)))))) |
136211a9 | 283 | |
0b504d64 JB |
284 | (defvar calc-alg-entry-history nil |
285 | "History for algebraic entry.") | |
286 | ||
2378f044 | 287 | ;;;###autoload |
136211a9 | 288 | (defun calc-alg-entry (&optional initial prompt) |
e6f0a80d | 289 | (let* ((calc-dollar-values (mapcar #'calc-get-stack-element |
136211a9 EZ |
290 | (nthcdr calc-stack-top calc-stack))) |
291 | (calc-dollar-used 0) | |
292 | (calc-plain-entry t) | |
0b504d64 | 293 | (alg-exp (calc-do-alg-entry initial prompt t 'calc-alg-entry-history))) |
136211a9 EZ |
294 | (if (stringp alg-exp) |
295 | (progn | |
5eb3eeda | 296 | (require 'calc-ext) |
136211a9 | 297 | (calc-alg-edit alg-exp)) |
e93c003e | 298 | (let* ((calc-simplify-mode (if (eq last-command-event ?\C-j) |
136211a9 EZ |
299 | 'none |
300 | calc-simplify-mode)) | |
301 | (nvals (mapcar 'calc-normalize alg-exp))) | |
302 | (while alg-exp | |
5eb3eeda | 303 | (calc-record (if (featurep 'calc-ext) (car alg-exp) (car nvals)) |
136211a9 EZ |
304 | "alg'") |
305 | (calc-pop-push-record-list calc-dollar-used | |
306 | (and (not (equal (car alg-exp) | |
307 | (car nvals))) | |
5eb3eeda | 308 | (featurep 'calc-ext) |
136211a9 EZ |
309 | "") |
310 | (list (car nvals))) | |
311 | (setq alg-exp (cdr alg-exp) | |
312 | nvals (cdr nvals) | |
313 | calc-dollar-used 0))) | |
07ff2bc8 | 314 | (calc-handle-whys)))) |
136211a9 | 315 | |
0da20854 SM |
316 | (defvar calc-alg-ent-map |
317 | (let ((map (make-sparse-keymap))) | |
318 | (set-keymap-parent map minibuffer-local-map) | |
319 | (define-key map "'" 'calcAlg-previous) | |
320 | (define-key map "`" 'calcAlg-edit) | |
321 | (define-key map "\C-m" 'calcAlg-enter) | |
322 | (define-key map "\C-j" 'calcAlg-enter) | |
323 | map) | |
1480882c JB |
324 | "The keymap used for algebraic entry.") |
325 | ||
0da20854 SM |
326 | (defvar calc-alg-ent-esc-map |
327 | (let ((map (make-keymap)) | |
328 | (i 33)) | |
329 | (set-keymap-parent map esc-map) | |
330 | (while (< i 127) | |
331 | (define-key map (vector i) 'calcAlg-escape) | |
332 | (setq i (1+ i))) | |
333 | map) | |
1480882c JB |
334 | "The keymap used for escapes in algebraic entry.") |
335 | ||
3cedbf72 JB |
336 | (defvar calc-alg-exp) |
337 | ||
2378f044 | 338 | ;;;###autoload |
0b504d64 | 339 | (defun calc-do-alg-entry (&optional initial prompt no-normalize history) |
136211a9 | 340 | (let* ((calc-buffer (current-buffer)) |
0da20854 | 341 | (blink-matching-check-function 'calcAlg-blink-matching-check) |
3cedbf72 | 342 | (calc-alg-exp 'error)) |
f1625eaa | 343 | (define-key calc-alg-ent-map "\e" nil) |
136211a9 EZ |
344 | (if (eq calc-algebraic-mode 'total) |
345 | (define-key calc-alg-ent-map "\e" calc-alg-ent-esc-map) | |
0b504d64 | 346 | (define-key calc-alg-ent-map "\e+" 'calcAlg-plus-minus) |
136211a9 EZ |
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) | |
0b504d64 JB |
350 | (define-key calc-alg-ent-map "\ep" 'previous-history-element) |
351 | (define-key calc-alg-ent-map "\en" 'next-history-element) | |
136211a9 EZ |
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: ") | |
355 | (or initial "") | |
0b504d64 | 356 | calc-alg-ent-map nil history))) |
3cedbf72 JB |
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))) | |
136211a9 EZ |
360 | (setq calc-aborted-prefix "alg'") |
361 | (or no-normalize | |
3cedbf72 JB |
362 | (and calc-alg-exp (setq calc-alg-exp (mapcar 'calc-normalize calc-alg-exp)))) |
363 | calc-alg-exp))) | |
136211a9 EZ |
364 | |
365 | (defun calcAlg-plus-minus () | |
366 | (interactive) | |
367 | (if (calc-minibuffer-contains ".* \\'") | |
368 | (insert "+/- ") | |
07ff2bc8 | 369 | (insert " +/- "))) |
136211a9 EZ |
370 | |
371 | (defun calcAlg-mod () | |
372 | (interactive) | |
373 | (if (not (calc-minibuffer-contains ".* \\'")) | |
374 | (insert " ")) | |
375 | (if (calc-minibuffer-contains ".* mod +\\'") | |
376 | (if calc-previous-modulo | |
377 | (insert (math-format-flat-expr calc-previous-modulo 0)) | |
378 | (beep)) | |
07ff2bc8 | 379 | (insert "mod "))) |
136211a9 EZ |
380 | |
381 | (defun calcAlg-previous () | |
382 | (interactive) | |
aa9208fb | 383 | (if (calc-minibuffer-contains "\\'") |
0b504d64 | 384 | (previous-history-element 1) |
07ff2bc8 | 385 | (insert "'"))) |
136211a9 EZ |
386 | |
387 | (defun calcAlg-equals () | |
388 | (interactive) | |
389 | (unwind-protect | |
390 | (calcAlg-enter) | |
3cedbf72 JB |
391 | (if (consp calc-alg-exp) |
392 | (progn (setq prefix-arg (length calc-alg-exp)) | |
07ff2bc8 | 393 | (calc-unread-command ?=))))) |
136211a9 EZ |
394 | |
395 | (defun calcAlg-escape () | |
396 | (interactive) | |
397 | (calc-unread-command) | |
398 | (save-excursion | |
399 | (calc-select-buffer) | |
400 | (use-local-map calc-mode-map)) | |
07ff2bc8 | 401 | (calcAlg-enter)) |
136211a9 | 402 | |
3132f345 | 403 | (defvar calc-plain-entry nil) |
136211a9 EZ |
404 | (defun calcAlg-edit () |
405 | (interactive) | |
406 | (if (or (not calc-plain-entry) | |
407 | (calc-minibuffer-contains | |
408 | "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'")) | |
409 | (insert "`") | |
3cedbf72 | 410 | (setq calc-alg-exp (minibuffer-contents)) |
07ff2bc8 | 411 | (exit-minibuffer))) |
136211a9 | 412 | |
d37f2979 JB |
413 | (defvar calc-buffer) |
414 | ||
136211a9 EZ |
415 | (defun calcAlg-enter () |
416 | (interactive) | |
f20be8fe | 417 | (let* ((str (minibuffer-contents)) |
136211a9 | 418 | (exp (and (> (length str) 0) |
6df9b6d7 | 419 | (with-current-buffer calc-buffer |
136211a9 EZ |
420 | (math-read-exprs str))))) |
421 | (if (eq (car-safe exp) 'error) | |
422 | (progn | |
f20be8fe | 423 | (goto-char (minibuffer-prompt-end)) |
136211a9 EZ |
424 | (forward-char (nth 1 exp)) |
425 | (beep) | |
426 | (calc-temp-minibuffer-message | |
427 | (concat " [" (or (nth 2 exp) "Error") "]")) | |
428 | (calc-clear-unread-commands)) | |
3cedbf72 | 429 | (setq calc-alg-exp (if (calc-minibuffer-contains "\\` *\\[ *\\'") |
136211a9 EZ |
430 | '((incomplete vec)) |
431 | exp)) | |
07ff2bc8 | 432 | (exit-minibuffer)))) |
136211a9 | 433 | |
0da20854 SM |
434 | (defun calcAlg-blink-matching-check (leftpt rightpt) |
435 | (let ((rightchar (char-before rightpt)) | |
436 | (leftchar (if leftpt (char-after leftpt)))) | |
a1a6b428 JB |
437 | (if (and leftpt |
438 | (or (and (= rightchar ?\)) | |
439 | (= leftchar ?\[)) | |
440 | (and (= rightchar ?\]) | |
441 | (= leftchar ?\())) | |
442 | (save-excursion | |
443 | (goto-char leftpt) | |
444 | (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)"))) | |
0da20854 SM |
445 | ;; [2..5) perfectly valid! |
446 | nil | |
447 | (blink-matching-check-mismatch leftpt rightpt)))) | |
136211a9 | 448 | |
2378f044 | 449 | ;;;###autoload |
136211a9 | 450 | (defun calc-alg-digit-entry () |
a1506d29 | 451 | (calc-alg-entry |
e93c003e | 452 | (cond ((eq last-command-event ?e) |
136211a9 | 453 | (if (> calc-number-radix 14) (format "%d.^" calc-number-radix) "1e")) |
e93c003e GM |
454 | ((eq last-command-event ?#) (format "%d#" calc-number-radix)) |
455 | ((eq last-command-event ?_) "-") | |
456 | ((eq last-command-event ?@) "0@ ") | |
457 | (t (char-to-string last-command-event))))) | |
136211a9 | 458 | |
d37f2979 JB |
459 | ;; The variable calc-digit-value is initially declared in calc.el, |
460 | ;; but can be set by calcDigit-algebraic and calcDigit-edit. | |
461 | (defvar calc-digit-value) | |
462 | ||
2378f044 | 463 | ;;;###autoload |
136211a9 EZ |
464 | (defun calcDigit-algebraic () |
465 | (interactive) | |
466 | (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'") | |
467 | (calcDigit-key) | |
f20be8fe | 468 | (setq calc-digit-value (minibuffer-contents)) |
07ff2bc8 | 469 | (exit-minibuffer))) |
136211a9 | 470 | |
2378f044 | 471 | ;;;###autoload |
136211a9 EZ |
472 | (defun calcDigit-edit () |
473 | (interactive) | |
474 | (calc-unread-command) | |
f20be8fe | 475 | (setq calc-digit-value (minibuffer-contents)) |
07ff2bc8 | 476 | (exit-minibuffer)) |
136211a9 EZ |
477 | |
478 | ||
479 | ;;; Algebraic expression parsing. [Public] | |
480 | ||
c59d10e0 JB |
481 | (defvar math-read-replacement-list |
482 | '(;; Misc symbols | |
483 | ("±" "+/-") ; plus or minus | |
484 | ("×" "*") ; multiplication sign | |
485 | ("÷" ":") ; division sign | |
486 | ("−" "-") ; subtraction sign | |
487 | ("∕" "/") ; division sign | |
488 | ("∗" "*") ; asterisk multiplication | |
489 | ("∞" "inf") ; infinity symbol | |
490 | ("≤" "<=") | |
491 | ("≥" ">=") | |
492 | ("≦" "<=") | |
493 | ("≧" ">=") | |
ae6bc504 | 494 | ("µ" "μ") |
c59d10e0 JB |
495 | ;; fractions |
496 | ("¼" "(1:4)") ; 1/4 | |
497 | ("½" "(1:2)") ; 1/2 | |
498 | ("¾" "(3:4)") ; 3/4 | |
499 | ("⅓" "(1:3)") ; 1/3 | |
500 | ("⅔" "(2:3)") ; 2/3 | |
501 | ("⅕" "(1:5)") ; 1/5 | |
502 | ("⅖" "(2:5)") ; 2/5 | |
503 | ("⅗" "(3:5)") ; 3/5 | |
504 | ("⅘" "(4:5)") ; 4/5 | |
505 | ("⅙" "(1:6)") ; 1/6 | |
506 | ("⅚" "(5:6)") ; 5/6 | |
507 | ("⅛" "(1:8)") ; 1/8 | |
508 | ("⅜" "(3:8)") ; 3/8 | |
509 | ("⅝" "(5:8)") ; 5/8 | |
510 | ("⅞" "(7:8)") ; 7/8 | |
511 | ("⅟" "1:") ; 1/... | |
512 | ;; superscripts | |
513 | ("⁰" "0") ; 0 | |
514 | ("¹" "1") ; 1 | |
515 | ("²" "2") ; 2 | |
516 | ("³" "3") ; 3 | |
517 | ("⁴" "4") ; 4 | |
518 | ("⁵" "5") ; 5 | |
519 | ("⁶" "6") ; 6 | |
520 | ("⁷" "7") ; 7 | |
521 | ("⁸" "8") ; 8 | |
522 | ("⁹" "9") ; 9 | |
523 | ("⁺" "+") ; + | |
524 | ("⁻" "-") ; - | |
525 | ("⁽" "(") ; ( | |
526 | ("⁾" ")") ; ) | |
527 | ("ⁿ" "n") ; n | |
bf7bae80 JB |
528 | ("ⁱ" "i") ; i |
529 | ;; subscripts | |
530 | ("₀" "0") ; 0 | |
531 | ("₁" "1") ; 1 | |
532 | ("₂" "2") ; 2 | |
533 | ("₃" "3") ; 3 | |
534 | ("₄" "4") ; 4 | |
535 | ("₅" "5") ; 5 | |
536 | ("₆" "6") ; 6 | |
537 | ("₇" "7") ; 7 | |
538 | ("₈" "8") ; 8 | |
539 | ("₉" "9") ; 9 | |
540 | ("₊" "+") ; + | |
541 | ("₋" "-") ; - | |
542 | ("₍" "(") ; ( | |
543 | ("₎" ")")) ; ) | |
c59d10e0 JB |
544 | "A list whose elements (old new) indicate replacements to make |
545 | in Calc algebraic input.") | |
546 | ||
547 | (defvar math-read-superscripts | |
548 | "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁽⁾ⁿⁱ" ; 0123456789+-()ni | |
549 | "A string consisting of the superscripts allowed by Calc.") | |
550 | ||
bf7bae80 JB |
551 | (defvar math-read-subscripts |
552 | "₀₁₂₃₄₅₆₇₈₉₊₋₍₎" ; 0123456789+-() | |
553 | "A string consisting of the subscripts allowed by Calc.") | |
554 | ||
2378f044 | 555 | ;;;###autoload |
c59d10e0 JB |
556 | (defun math-read-preprocess-string (str) |
557 | "Replace some substrings of STR by Calc equivalents." | |
558 | (setq str | |
559 | (replace-regexp-in-string (concat "[" math-read-superscripts "]+") | |
560 | "^(\\&)" str)) | |
bf7bae80 JB |
561 | (setq str |
562 | (replace-regexp-in-string (concat "[" math-read-subscripts "]+") | |
563 | "_(\\&)" str)) | |
c59d10e0 JB |
564 | (let ((rep-list math-read-replacement-list)) |
565 | (while rep-list | |
566 | (setq str | |
567 | (replace-regexp-in-string (nth 0 (car rep-list)) | |
568 | (nth 1 (car rep-list)) str)) | |
569 | (setq rep-list (cdr rep-list)))) | |
570 | str) | |
571 | ||
d37f2979 JB |
572 | ;; The next few variables are local to math-read-exprs (and math-read-expr |
573 | ;; in calc-ext.el), but are set in functions they call. | |
3cedbf72 JB |
574 | |
575 | (defvar math-exp-pos) | |
576 | (defvar math-exp-str) | |
577 | (defvar math-exp-old-pos) | |
578 | (defvar math-exp-token) | |
579 | (defvar math-exp-keep-spaces) | |
d37f2979 | 580 | (defvar math-expr-data) |
3cedbf72 | 581 | |
2378f044 | 582 | ;;;###autoload |
3cedbf72 JB |
583 | (defun math-read-exprs (math-exp-str) |
584 | (let ((math-exp-pos 0) | |
585 | (math-exp-old-pos 0) | |
586 | (math-exp-keep-spaces nil) | |
587 | math-exp-token math-expr-data) | |
c4446fc2 | 588 | (setq math-exp-str (math-read-preprocess-string math-exp-str)) |
d039d4d8 JB |
589 | (if (memq calc-language calc-lang-allow-percentsigns) |
590 | (setq math-exp-str (math-remove-percentsigns math-exp-str))) | |
136211a9 | 591 | (if calc-language-input-filter |
3cedbf72 | 592 | (setq math-exp-str (funcall calc-language-input-filter math-exp-str))) |
be19ef0b | 593 | (while (setq math-exp-token |
d039d4d8 | 594 | (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str)) |
be19ef0b | 595 | (setq math-exp-str |
d039d4d8 | 596 | (concat (substring math-exp-str 0 math-exp-token) "\\dots" |
3cedbf72 | 597 | (substring math-exp-str (+ math-exp-token 2))))) |
136211a9 EZ |
598 | (math-build-parse-table) |
599 | (math-read-token) | |
600 | (let ((val (catch 'syntax (math-read-expr-list)))) | |
601 | (if (stringp val) | |
3cedbf72 JB |
602 | (list 'error math-exp-old-pos val) |
603 | (if (equal math-exp-token 'end) | |
136211a9 | 604 | val |
3cedbf72 | 605 | (list 'error math-exp-old-pos "Syntax error")))))) |
136211a9 | 606 | |
2378f044 | 607 | ;;;###autoload |
136211a9 | 608 | (defun math-read-expr-list () |
3cedbf72 | 609 | (let* ((math-exp-keep-spaces nil) |
136211a9 EZ |
610 | (val (list (math-read-expr-level 0))) |
611 | (last val)) | |
abd880c3 | 612 | (while (equal math-expr-data ",") |
136211a9 EZ |
613 | (math-read-token) |
614 | (let ((rest (list (math-read-expr-level 0)))) | |
615 | (setcdr last rest) | |
616 | (setq last rest))) | |
07ff2bc8 | 617 | val)) |
136211a9 | 618 | |
3132f345 CW |
619 | (defvar calc-user-parse-table nil) |
620 | (defvar calc-last-main-parse-table nil) | |
b2e7f0f0 | 621 | (defvar calc-last-user-lang-parse-table nil) |
3132f345 CW |
622 | (defvar calc-last-lang-parse-table nil) |
623 | (defvar calc-user-tokens nil) | |
624 | (defvar calc-user-token-chars nil) | |
136211a9 | 625 | |
3cedbf72 JB |
626 | (defvar math-toks nil |
627 | "Tokens to pass between math-build-parse-table and math-find-user-tokens.") | |
628 | ||
2378f044 | 629 | ;;;###autoload |
136211a9 EZ |
630 | (defun math-build-parse-table () |
631 | (let ((mtab (cdr (assq nil calc-user-parse-tables))) | |
b2e7f0f0 JB |
632 | (ltab (cdr (assq calc-language calc-user-parse-tables))) |
633 | (lltab (get calc-language 'math-parse-table))) | |
136211a9 | 634 | (or (and (eq mtab calc-last-main-parse-table) |
b2e7f0f0 JB |
635 | (eq ltab calc-last-user-lang-parse-table) |
636 | (eq lltab calc-last-lang-parse-table)) | |
637 | (let ((p (append mtab ltab lltab)) | |
3cedbf72 | 638 | (math-toks nil)) |
136211a9 EZ |
639 | (setq calc-user-parse-table p) |
640 | (setq calc-user-token-chars nil) | |
641 | (while p | |
642 | (math-find-user-tokens (car (car p))) | |
643 | (setq p (cdr p))) | |
644 | (setq calc-user-tokens (mapconcat 'identity | |
3cedbf72 | 645 | (sort (mapcar 'car math-toks) |
136211a9 EZ |
646 | (function (lambda (x y) |
647 | (> (length x) | |
648 | (length y))))) | |
649 | "\\|") | |
650 | calc-last-main-parse-table mtab | |
b2e7f0f0 JB |
651 | calc-last-user-lang-parse-table ltab |
652 | calc-last-lang-parse-table lltab))))) | |
136211a9 | 653 | |
2378f044 | 654 | ;;;###autoload |
3cedbf72 | 655 | (defun math-find-user-tokens (p) |
136211a9 EZ |
656 | (while p |
657 | (cond ((and (stringp (car p)) | |
658 | (or (> (length (car p)) 1) (equal (car p) "$") | |
659 | (equal (car p) "\"")) | |
ae6bc504 | 660 | (string-match "[^a-zA-Zα-ωΑ-Ω0-9]" (car p))) |
136211a9 | 661 | (let ((s (regexp-quote (car p)))) |
ae6bc504 | 662 | (if (string-match "\\`[a-zA-Zα-ωΑ-Ω0-9]" s) |
136211a9 | 663 | (setq s (concat "\\<" s))) |
ae6bc504 | 664 | (if (string-match "[a-zA-Zα-ωΑ-Ω0-9]\\'" s) |
136211a9 | 665 | (setq s (concat s "\\>"))) |
3cedbf72 | 666 | (or (assoc s math-toks) |
136211a9 | 667 | (progn |
3cedbf72 | 668 | (setq math-toks (cons (list s) math-toks)) |
136211a9 EZ |
669 | (or (memq (aref (car p) 0) calc-user-token-chars) |
670 | (setq calc-user-token-chars | |
671 | (cons (aref (car p) 0) | |
672 | calc-user-token-chars))))))) | |
673 | ((consp (car p)) | |
674 | (math-find-user-tokens (nth 1 (car p))) | |
675 | (or (eq (car (car p)) '\?) | |
676 | (math-find-user-tokens (nth 2 (car p)))))) | |
07ff2bc8 | 677 | (setq p (cdr p)))) |
136211a9 | 678 | |
2378f044 | 679 | ;;;###autoload |
136211a9 | 680 | (defun math-read-token () |
3cedbf72 JB |
681 | (if (>= math-exp-pos (length math-exp-str)) |
682 | (setq math-exp-old-pos math-exp-pos | |
683 | math-exp-token 'end | |
abd880c3 | 684 | math-expr-data "\000") |
2807e8e4 JB |
685 | (let (adfn |
686 | (ch (aref math-exp-str math-exp-pos))) | |
3cedbf72 | 687 | (setq math-exp-old-pos math-exp-pos) |
136211a9 | 688 | (cond ((memq ch '(32 10 9)) |
3cedbf72 JB |
689 | (setq math-exp-pos (1+ math-exp-pos)) |
690 | (if math-exp-keep-spaces | |
691 | (setq math-exp-token 'space | |
abd880c3 | 692 | math-expr-data " ") |
136211a9 EZ |
693 | (math-read-token))) |
694 | ((and (memq ch calc-user-token-chars) | |
695 | (let ((case-fold-search nil)) | |
be19ef0b | 696 | (eq (string-match |
d039d4d8 | 697 | calc-user-tokens math-exp-str math-exp-pos) |
3cedbf72 JB |
698 | math-exp-pos))) |
699 | (setq math-exp-token 'punc | |
700 | math-expr-data (math-match-substring math-exp-str 0) | |
701 | math-exp-pos (match-end 0))) | |
136211a9 | 702 | ((or (and (>= ch ?a) (<= ch ?z)) |
ae6bc504 JB |
703 | (and (>= ch ?A) (<= ch ?Z)) |
704 | (and (>= ch ?α) (<= ch ?ω)) | |
705 | (and (>= ch ?Α) (<= ch ?Ω))) | |
be19ef0b | 706 | (string-match |
d039d4d8 JB |
707 | (cond |
708 | ((and (memq calc-language calc-lang-allow-underscores) | |
709 | (memq calc-language calc-lang-allow-percentsigns)) | |
ae6bc504 | 710 | "[a-zA-Zα-ωΑ-Ω0-9_'#]*") |
d039d4d8 | 711 | ((memq calc-language calc-lang-allow-underscores) |
ae6bc504 JB |
712 | "[a-zA-Zα-ωΑ-Ω0-9_#]*") |
713 | (t "[a-zA-Zα-ωΑ-Ω0-9'#]*")) | |
d039d4d8 | 714 | math-exp-str math-exp-pos) |
3cedbf72 JB |
715 | (setq math-exp-token 'symbol |
716 | math-exp-pos (match-end 0) | |
abd880c3 | 717 | math-expr-data (math-restore-dashes |
3cedbf72 | 718 | (math-match-substring math-exp-str 0))) |
2807e8e4 JB |
719 | (if (setq adfn (get calc-language 'math-lang-adjust-words)) |
720 | (funcall adfn))) | |
136211a9 EZ |
721 | ((or (and (>= ch ?0) (<= ch ?9)) |
722 | (and (eq ch '?\.) | |
8f2904a5 | 723 | (eq (string-match "\\.[0-9]" math-exp-str math-exp-pos) |
3cedbf72 | 724 | math-exp-pos)) |
136211a9 | 725 | (and (eq ch '?_) |
8f2904a5 | 726 | (eq (string-match "_\\.?[0-9]" math-exp-str math-exp-pos) |
3cedbf72 JB |
727 | math-exp-pos) |
728 | (or (eq math-exp-pos 0) | |
be19ef0b | 729 | (and (not (memq calc-language |
2807e8e4 | 730 | calc-lang-allow-underscores)) |
ae6bc504 | 731 | (eq (string-match "[^])}\"a-zA-Zα-ωΑ-Ω0-9'$]_" |
3cedbf72 JB |
732 | math-exp-str (1- math-exp-pos)) |
733 | (1- math-exp-pos)))))) | |
2807e8e4 | 734 | (or (and (memq calc-language calc-lang-c-type-hex) |
3cedbf72 | 735 | (string-match "0[xX][0-9a-fA-F]+" math-exp-str math-exp-pos)) |
ae6bc504 | 736 | (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]+\\)?\"?\\)?" |
3cedbf72 JB |
737 | math-exp-str math-exp-pos)) |
738 | (setq math-exp-token 'number | |
739 | math-expr-data (math-match-substring math-exp-str 0) | |
740 | math-exp-pos (match-end 0))) | |
be19ef0b | 741 | ((and (setq adfn |
2807e8e4 JB |
742 | (assq ch (get calc-language 'math-lang-read-symbol))) |
743 | (eval (nth 1 adfn))) | |
744 | (eval (nth 2 adfn))) | |
136211a9 | 745 | ((eq ch ?\$) |
2807e8e4 JB |
746 | (if (eq (string-match "\\$\\([1-9][0-9]*\\)" math-exp-str math-exp-pos) |
747 | math-exp-pos) | |
748 | (setq math-expr-data (- (string-to-number (math-match-substring | |
749 | math-exp-str 1)))) | |
750 | (string-match "\\$+" math-exp-str math-exp-pos) | |
751 | (setq math-expr-data (- (match-end 0) (match-beginning 0)))) | |
752 | (setq math-exp-token 'dollar | |
753 | math-exp-pos (match-end 0))) | |
136211a9 | 754 | ((eq ch ?\#) |
3cedbf72 JB |
755 | (if (eq (string-match "#\\([1-9][0-9]*\\)" math-exp-str math-exp-pos) |
756 | math-exp-pos) | |
36692c4a | 757 | (setq math-expr-data (string-to-number |
3cedbf72 JB |
758 | (math-match-substring math-exp-str 1)) |
759 | math-exp-pos (match-end 0)) | |
abd880c3 | 760 | (setq math-expr-data 1 |
3cedbf72 JB |
761 | math-exp-pos (1+ math-exp-pos))) |
762 | (setq math-exp-token 'hash)) | |
136211a9 | 763 | ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>" |
3cedbf72 JB |
764 | math-exp-str math-exp-pos) |
765 | math-exp-pos) | |
766 | (setq math-exp-token 'punc | |
767 | math-expr-data (math-match-substring math-exp-str 0) | |
768 | math-exp-pos (match-end 0))) | |
136211a9 | 769 | ((and (eq ch ?\") |
8f2904a5 | 770 | (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)" |
3cedbf72 | 771 | math-exp-str math-exp-pos)) |
2807e8e4 JB |
772 | (setq math-exp-token 'string |
773 | math-expr-data (math-match-substring math-exp-str 1) | |
774 | math-exp-pos (match-end 0))) | |
5fb4539b | 775 | ((and (setq adfn (get calc-language 'math-lang-read)) |
2807e8e4 | 776 | (eval (nth 0 adfn)) |
5fb4539b | 777 | (eval (nth 1 adfn)))) |
3cedbf72 JB |
778 | ((eq (string-match "%%.*$" math-exp-str math-exp-pos) math-exp-pos) |
779 | (setq math-exp-pos (match-end 0)) | |
136211a9 EZ |
780 | (math-read-token)) |
781 | (t | |
2807e8e4 JB |
782 | (if (setq adfn (assq ch (get calc-language 'math-punc-table))) |
783 | (setq ch (cdr adfn))) | |
3cedbf72 | 784 | (setq math-exp-token 'punc |
abd880c3 | 785 | math-expr-data (char-to-string ch) |
3cedbf72 | 786 | math-exp-pos (1+ math-exp-pos))))))) |
136211a9 | 787 | |
d37f2979 JB |
788 | (defconst math-alg-inequalities |
789 | '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq | |
790 | calcFunc-eq calcFunc-neq)) | |
136211a9 EZ |
791 | |
792 | (defun math-read-expr-level (exp-prec &optional exp-term) | |
16819e48 | 793 | (let* ((math-expr-opers (math-expr-ops)) |
be19ef0b GM |
794 | (x (math-read-factor)) |
795 | (first t) | |
16819e48 | 796 | op op2) |
136211a9 EZ |
797 | (while (and (or (and calc-user-parse-table |
798 | (setq op (calc-check-user-syntax x exp-prec)) | |
799 | (setq x op | |
800 | op '("2x" ident 999999 -1))) | |
abd880c3 | 801 | (and (setq op (assoc math-expr-data math-expr-opers)) |
136211a9 EZ |
802 | (/= (nth 2 op) -1) |
803 | (or (and (setq op2 (assoc | |
abd880c3 | 804 | math-expr-data |
136211a9 EZ |
805 | (cdr (memq op math-expr-opers)))) |
806 | (eq (= (nth 3 op) -1) | |
807 | (/= (nth 3 op2) -1)) | |
808 | (eq (= (nth 3 op2) -1) | |
809 | (not (math-factor-after))) | |
810 | (setq op op2)) | |
811 | t)) | |
812 | (and (or (eq (nth 2 op) -1) | |
3cedbf72 | 813 | (memq math-exp-token '(symbol number dollar hash)) |
abd880c3 JB |
814 | (equal math-expr-data "(") |
815 | (and (equal math-expr-data "[") | |
be19ef0b GM |
816 | (not (equal |
817 | (get calc-language | |
2807e8e4 | 818 | 'math-function-open) "[")) |
3cedbf72 | 819 | (not (and math-exp-keep-spaces |
136211a9 | 820 | (eq (car-safe x) 'vec))))) |
abd880c3 | 821 | (or (not (setq op (assoc math-expr-data math-expr-opers))) |
136211a9 EZ |
822 | (/= (nth 2 op) -1)) |
823 | (or (not calc-user-parse-table) | |
3cedbf72 | 824 | (not (eq math-exp-token 'symbol)) |
136211a9 EZ |
825 | (let ((p calc-user-parse-table)) |
826 | (while (and p | |
827 | (or (not (integerp | |
828 | (car (car (car p))))) | |
829 | (not (equal | |
830 | (nth 1 (car (car p))) | |
abd880c3 | 831 | math-expr-data)))) |
136211a9 EZ |
832 | (setq p (cdr p))) |
833 | (not p))) | |
834 | (setq op (assoc "2x" math-expr-opers)))) | |
abd880c3 | 835 | (not (and exp-term (equal math-expr-data exp-term))) |
136211a9 EZ |
836 | (>= (nth 2 op) exp-prec)) |
837 | (if (not (equal (car op) "2x")) | |
838 | (math-read-token)) | |
839 | (and (memq (nth 1 op) '(sdev mod)) | |
5eb3eeda | 840 | (require 'calc-ext)) |
136211a9 EZ |
841 | (setq x (cond ((consp (nth 1 op)) |
842 | (funcall (car (nth 1 op)) x op)) | |
843 | ((eq (nth 3 op) -1) | |
844 | (if (eq (nth 1 op) 'ident) | |
845 | x | |
846 | (if (eq (nth 1 op) 'closing) | |
847 | (if (eq (nth 2 op) exp-prec) | |
848 | (progn | |
849 | (setq exp-prec 1000) | |
850 | x) | |
851 | (throw 'syntax "Mismatched delimiters")) | |
852 | (list (nth 1 op) x)))) | |
853 | ((and (not first) | |
854 | (memq (nth 1 op) math-alg-inequalities) | |
855 | (memq (car-safe x) math-alg-inequalities)) | |
5eb3eeda | 856 | (require 'calc-ext) |
136211a9 EZ |
857 | (math-composite-inequalities x op)) |
858 | (t (list (nth 1 op) | |
859 | x | |
860 | (math-read-expr-level (nth 3 op) exp-term)))) | |
861 | first nil)) | |
07ff2bc8 | 862 | x)) |
136211a9 | 863 | |
35cdbd30 JB |
864 | ;; calc-arg-values is defined in calc-ext.el, but is used here. |
865 | (defvar calc-arg-values) | |
866 | ||
2378f044 | 867 | ;;;###autoload |
136211a9 EZ |
868 | (defun calc-check-user-syntax (&optional x prec) |
869 | (let ((p calc-user-parse-table) | |
870 | (matches nil) | |
871 | match rule) | |
872 | (while (and p | |
873 | (or (not (progn | |
874 | (setq rule (car (car p))) | |
875 | (if x | |
876 | (and (integerp (car rule)) | |
877 | (>= (car rule) prec) | |
abd880c3 | 878 | (equal math-expr-data |
136211a9 | 879 | (car (setq rule (cdr rule))))) |
abd880c3 | 880 | (equal math-expr-data (car rule))))) |
3cedbf72 JB |
881 | (let ((save-exp-pos math-exp-pos) |
882 | (save-exp-old-pos math-exp-old-pos) | |
883 | (save-exp-token math-exp-token) | |
abd880c3 | 884 | (save-exp-data math-expr-data)) |
136211a9 EZ |
885 | (or (not (listp |
886 | (setq matches (calc-match-user-syntax rule)))) | |
887 | (let ((args (progn | |
5eb3eeda | 888 | (require 'calc-ext) |
136211a9 EZ |
889 | calc-arg-values)) |
890 | (conds nil) | |
891 | temp) | |
892 | (if x | |
893 | (setq matches (cons x matches))) | |
894 | (setq match (cdr (car p))) | |
895 | (while (and (eq (car-safe match) | |
896 | 'calcFunc-condition) | |
897 | (= (length match) 3)) | |
898 | (setq conds (append (math-flatten-lands | |
899 | (nth 2 match)) | |
900 | conds) | |
901 | match (nth 1 match))) | |
902 | (while (and conds match) | |
5eb3eeda | 903 | (require 'calc-ext) |
136211a9 EZ |
904 | (cond ((eq (car-safe (car conds)) |
905 | 'calcFunc-let) | |
906 | (setq temp (car conds)) | |
907 | (or (= (length temp) 3) | |
908 | (and (= (length temp) 2) | |
909 | (eq (car-safe (nth 1 temp)) | |
910 | 'calcFunc-assign) | |
911 | (= (length (nth 1 temp)) 3) | |
912 | (setq temp (nth 1 temp))) | |
913 | (setq match nil)) | |
914 | (setq matches (cons | |
915 | (math-normalize | |
916 | (math-multi-subst | |
917 | (nth 2 temp) | |
918 | args matches)) | |
919 | matches) | |
920 | args (cons (nth 1 temp) | |
921 | args))) | |
922 | ((and (eq (car-safe (car conds)) | |
923 | 'calcFunc-matches) | |
924 | (= (length (car conds)) 3)) | |
925 | (setq temp (calcFunc-vmatches | |
926 | (math-multi-subst | |
927 | (nth 1 (car conds)) | |
928 | args matches) | |
929 | (nth 2 (car conds)))) | |
930 | (if (eq temp 0) | |
931 | (setq match nil) | |
932 | (while (setq temp (cdr temp)) | |
933 | (setq matches (cons (nth 2 (car temp)) | |
934 | matches) | |
935 | args (cons (nth 1 (car temp)) | |
936 | args))))) | |
937 | (t | |
938 | (or (math-is-true (math-simplify | |
939 | (math-multi-subst | |
940 | (car conds) | |
941 | args matches))) | |
942 | (setq match nil)))) | |
943 | (setq conds (cdr conds))) | |
944 | (if match | |
945 | (not (setq match (math-multi-subst | |
946 | match args matches))) | |
3cedbf72 JB |
947 | (setq math-exp-old-pos save-exp-old-pos |
948 | math-exp-token save-exp-token | |
abd880c3 | 949 | math-expr-data save-exp-data |
3cedbf72 | 950 | math-exp-pos save-exp-pos))))))) |
136211a9 | 951 | (setq p (cdr p))) |
07ff2bc8 | 952 | (and p match))) |
136211a9 | 953 | |
2378f044 | 954 | ;;;###autoload |
136211a9 EZ |
955 | (defun calc-match-user-syntax (p &optional term) |
956 | (let ((matches nil) | |
3cedbf72 JB |
957 | (save-exp-pos math-exp-pos) |
958 | (save-exp-old-pos math-exp-old-pos) | |
959 | (save-exp-token math-exp-token) | |
960 | (save-exp-data math-expr-data) | |
961 | m) | |
136211a9 EZ |
962 | (while (and p |
963 | (cond ((stringp (car p)) | |
abd880c3 | 964 | (and (equal math-expr-data (car p)) |
136211a9 EZ |
965 | (progn |
966 | (math-read-token) | |
967 | t))) | |
968 | ((integerp (car p)) | |
969 | (and (setq m (catch 'syntax | |
970 | (math-read-expr-level | |
971 | (car p) | |
972 | (if (cdr p) | |
973 | (if (consp (nth 1 p)) | |
974 | (car (nth 1 (nth 1 p))) | |
975 | (nth 1 p)) | |
976 | term)))) | |
977 | (not (stringp m)) | |
978 | (setq matches (nconc matches (list m))))) | |
979 | ((eq (car (car p)) '\?) | |
980 | (setq m (calc-match-user-syntax (nth 1 (car p)))) | |
981 | (or (nth 2 (car p)) | |
982 | (setq matches | |
983 | (nconc matches | |
984 | (list | |
985 | (cons 'vec (and (listp m) m)))))) | |
986 | (or (listp m) (not (nth 2 (car p))) | |
987 | (not (eq (aref (car (nth 2 (car p))) 0) ?\$)) | |
3cedbf72 | 988 | (eq math-exp-token 'end))) |
136211a9 EZ |
989 | (t |
990 | (setq m (calc-match-user-syntax (nth 1 (car p)) | |
991 | (car (nth 2 (car p))))) | |
992 | (if (listp m) | |
993 | (let ((vec (cons 'vec m)) | |
994 | opos mm) | |
995 | (while (and (listp | |
3cedbf72 | 996 | (setq opos math-exp-pos |
136211a9 EZ |
997 | mm (calc-match-user-syntax |
998 | (or (nth 2 (car p)) | |
999 | (nth 1 (car p))) | |
1000 | (car (nth 2 (car p)))))) | |
3cedbf72 | 1001 | (> math-exp-pos opos)) |
136211a9 EZ |
1002 | (setq vec (nconc vec mm))) |
1003 | (setq matches (nconc matches (list vec)))) | |
1004 | (and (eq (car (car p)) '*) | |
1005 | (setq matches (nconc matches (list '(vec))))))))) | |
1006 | (setq p (cdr p))) | |
1007 | (if p | |
3cedbf72 JB |
1008 | (setq math-exp-pos save-exp-pos |
1009 | math-exp-old-pos save-exp-old-pos | |
1010 | math-exp-token save-exp-token | |
abd880c3 | 1011 | math-expr-data save-exp-data |
136211a9 | 1012 | matches "Failed")) |
07ff2bc8 | 1013 | matches)) |
136211a9 | 1014 | |
2378f044 | 1015 | ;;;###autoload |
136211a9 EZ |
1016 | (defun math-remove-dashes (x) |
1017 | (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x) | |
1018 | (math-remove-dashes | |
1019 | (concat (math-match-substring x 1) "#" (math-match-substring x 2))) | |
07ff2bc8 | 1020 | x)) |
136211a9 | 1021 | |
d039d4d8 | 1022 | (defun math-remove-percentsigns (x) |
d039d4d8 JB |
1023 | (if (string-match "\\`\\(.*\\)%\\(.*\\)\\'" x) |
1024 | (math-remove-percentsigns | |
c0db5755 | 1025 | (concat (math-match-substring x 1) "o'o" (math-match-substring x 2))) |
d039d4d8 JB |
1026 | x)) |
1027 | ||
136211a9 EZ |
1028 | (defun math-restore-dashes (x) |
1029 | (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x) | |
1030 | (math-restore-dashes | |
1031 | (concat (math-match-substring x 1) "-" (math-match-substring x 2))) | |
07ff2bc8 | 1032 | x)) |
136211a9 | 1033 | |
d039d4d8 JB |
1034 | (defun math-restore-placeholders (x) |
1035 | "Replace placeholders by the proper characters in the symbol x. | |
1036 | This includes `#' for `_' and `'' for `%'. | |
1037 | If the current Calc language does not use placeholders, return nil." | |
1038 | (if (or (memq calc-language calc-lang-allow-underscores) | |
1039 | (memq calc-language calc-lang-allow-percentsigns)) | |
1040 | (let ((sx (symbol-name x))) | |
1041 | (when (memq calc-language calc-lang-allow-percentsigns) | |
1042 | (require 'calccomp) | |
1043 | (setq sx (math-to-percentsigns sx))) | |
1044 | (if (memq calc-language calc-lang-allow-underscores) | |
1045 | (setq sx (math-string-restore-underscores sx))) | |
1046 | (intern-soft sx)))) | |
57f8977d JB |
1047 | |
1048 | (defun math-string-restore-underscores (x) | |
1049 | "Replace pound signs by underscores in the string x." | |
1050 | (if (string-match "\\`\\(.*\\)#\\(.*\\)\\'" x) | |
1051 | (math-string-restore-underscores | |
1052 | (concat (math-match-substring x 1) "_" (math-match-substring x 2))) | |
1053 | x)) | |
1054 | ||
2378f044 | 1055 | ;;;###autoload |
136211a9 EZ |
1056 | (defun math-read-if (cond op) |
1057 | (let ((then (math-read-expr-level 0))) | |
abd880c3 | 1058 | (or (equal math-expr-data ":") |
136211a9 EZ |
1059 | (throw 'syntax "Expected ':'")) |
1060 | (math-read-token) | |
07ff2bc8 | 1061 | (list 'calcFunc-if cond then (math-read-expr-level (nth 3 op))))) |
136211a9 EZ |
1062 | |
1063 | (defun math-factor-after () | |
3cedbf72 JB |
1064 | (let ((math-exp-pos math-exp-pos) |
1065 | math-exp-old-pos math-exp-token math-expr-data) | |
136211a9 | 1066 | (math-read-token) |
3cedbf72 | 1067 | (or (memq math-exp-token '(number symbol dollar hash string)) |
abd880c3 JB |
1068 | (and (assoc math-expr-data '(("-") ("+") ("!") ("|") ("/"))) |
1069 | (assoc (concat "u" math-expr-data) math-expr-opers)) | |
1070 | (eq (nth 2 (assoc math-expr-data math-expr-opers)) -1) | |
1071 | (assoc math-expr-data '(("(") ("[") ("{")))))) | |
136211a9 EZ |
1072 | |
1073 | (defun math-read-factor () | |
16819e48 JB |
1074 | (let ((math-expr-opers (math-expr-ops)) |
1075 | op) | |
3cedbf72 | 1076 | (cond ((eq math-exp-token 'number) |
abd880c3 | 1077 | (let ((num (math-read-number math-expr-data))) |
136211a9 EZ |
1078 | (if (not num) |
1079 | (progn | |
3cedbf72 | 1080 | (setq math-exp-old-pos math-exp-pos) |
136211a9 EZ |
1081 | (throw 'syntax "Bad format"))) |
1082 | (math-read-token) | |
1083 | (if (and math-read-expr-quotes | |
1084 | (consp num)) | |
1085 | (list 'quote num) | |
1086 | num))) | |
1087 | ((and calc-user-parse-table | |
1088 | (setq op (calc-check-user-syntax))) | |
1089 | op) | |
abd880c3 JB |
1090 | ((or (equal math-expr-data "-") |
1091 | (equal math-expr-data "+") | |
1092 | (equal math-expr-data "!") | |
1093 | (equal math-expr-data "|") | |
1094 | (equal math-expr-data "/")) | |
1095 | (setq math-expr-data (concat "u" math-expr-data)) | |
136211a9 | 1096 | (math-read-factor)) |
abd880c3 | 1097 | ((and (setq op (assoc math-expr-data math-expr-opers)) |
136211a9 EZ |
1098 | (eq (nth 2 op) -1)) |
1099 | (if (consp (nth 1 op)) | |
1100 | (funcall (car (nth 1 op)) op) | |
1101 | (math-read-token) | |
1102 | (let ((val (math-read-expr-level (nth 3 op)))) | |
1103 | (cond ((eq (nth 1 op) 'ident) | |
1104 | val) | |
1105 | ((and (Math-numberp val) | |
1106 | (equal (car op) "u-")) | |
1107 | (math-neg val)) | |
1108 | (t (list (nth 1 op) val)))))) | |
3cedbf72 | 1109 | ((eq math-exp-token 'symbol) |
abd880c3 | 1110 | (let ((sym (intern math-expr-data))) |
136211a9 | 1111 | (math-read-token) |
abd880c3 | 1112 | (if (equal math-expr-data calc-function-open) |
136211a9 EZ |
1113 | (let ((f (assq sym math-expr-function-mapping))) |
1114 | (math-read-token) | |
1115 | (if (consp (cdr f)) | |
1116 | (funcall (car (cdr f)) f sym) | |
abd880c3 | 1117 | (let ((args (if (or (equal math-expr-data calc-function-close) |
3cedbf72 | 1118 | (eq math-exp-token 'end)) |
136211a9 EZ |
1119 | nil |
1120 | (math-read-expr-list)))) | |
abd880c3 | 1121 | (if (not (or (equal math-expr-data calc-function-close) |
3cedbf72 | 1122 | (eq math-exp-token 'end))) |
136211a9 EZ |
1123 | (throw 'syntax "Expected `)'")) |
1124 | (math-read-token) | |
be19ef0b GM |
1125 | (if (and (memq calc-language |
1126 | calc-lang-parens-are-subscripts) | |
2807e8e4 | 1127 | args |
5eb3eeda | 1128 | (require 'calc-ext) |
136211a9 EZ |
1129 | (let ((calc-matrix-mode 'scalar)) |
1130 | (math-known-matrixp | |
1131 | (list 'var sym | |
1132 | (intern | |
1133 | (concat "var-" | |
1134 | (symbol-name sym))))))) | |
1135 | (math-parse-fortran-subscr sym args) | |
1136 | (if f | |
1137 | (setq sym (cdr f)) | |
1138 | (and (= (aref (symbol-name sym) 0) ?\\) | |
1139 | (< (prefix-numeric-value calc-language-option) | |
1140 | 0) | |
1141 | (setq sym (intern (substring (symbol-name sym) | |
1142 | 1)))) | |
1143 | (or (string-match "-" (symbol-name sym)) | |
1144 | (setq sym (intern | |
1145 | (concat "calcFunc-" | |
1146 | (symbol-name sym)))))) | |
1147 | (cons sym args))))) | |
1148 | (if math-read-expr-quotes | |
1149 | sym | |
1150 | (let ((val (list 'var | |
1151 | (intern (math-remove-dashes | |
1152 | (symbol-name sym))) | |
1153 | (if (string-match "-" (symbol-name sym)) | |
1154 | sym | |
1155 | (intern (concat "var-" | |
1156 | (symbol-name sym))))))) | |
57f8977d JB |
1157 | (let ((v (or |
1158 | (assq (nth 1 val) math-expr-variable-mapping) | |
d039d4d8 | 1159 | (assq (math-restore-placeholders (nth 1 val)) |
57f8977d | 1160 | math-expr-variable-mapping)))) |
136211a9 EZ |
1161 | (and v (setq val (if (consp (cdr v)) |
1162 | (funcall (car (cdr v)) v val) | |
1163 | (list 'var | |
1164 | (intern | |
1165 | (substring (symbol-name (cdr v)) | |
1166 | 4)) | |
1167 | (cdr v)))))) | |
be19ef0b | 1168 | (while (and (memq calc-language |
2807e8e4 | 1169 | calc-lang-brackets-are-subscripts) |
abd880c3 | 1170 | (equal math-expr-data "[")) |
136211a9 | 1171 | (math-read-token) |
2807e8e4 JB |
1172 | (let ((el (math-read-expr-list))) |
1173 | (while el | |
1174 | (setq val (append (list 'calcFunc-subscr val) | |
1175 | (list (car el)))) | |
1176 | (setq el (cdr el)))) | |
abd880c3 | 1177 | (if (equal math-expr-data "]") |
136211a9 EZ |
1178 | (math-read-token) |
1179 | (throw 'syntax "Expected ']'"))) | |
1180 | val))))) | |
3cedbf72 | 1181 | ((eq math-exp-token 'dollar) |
abd880c3 | 1182 | (let ((abs (if (> math-expr-data 0) math-expr-data (- math-expr-data)))) |
136211a9 | 1183 | (if (>= (length calc-dollar-values) abs) |
abd880c3 | 1184 | (let ((num math-expr-data)) |
136211a9 EZ |
1185 | (math-read-token) |
1186 | (setq calc-dollar-used (max calc-dollar-used num)) | |
1187 | (math-check-complete (nth (1- abs) calc-dollar-values))) | |
1188 | (throw 'syntax (if calc-dollar-values | |
1189 | "Too many $'s" | |
1190 | "$'s not allowed in this context"))))) | |
3cedbf72 | 1191 | ((eq math-exp-token 'hash) |
136211a9 EZ |
1192 | (or calc-hashes-used |
1193 | (throw 'syntax "#'s not allowed in this context")) | |
5eb3eeda | 1194 | (require 'calc-ext) |
abd880c3 JB |
1195 | (if (<= math-expr-data (length calc-arg-values)) |
1196 | (let ((num math-expr-data)) | |
136211a9 EZ |
1197 | (math-read-token) |
1198 | (setq calc-hashes-used (max calc-hashes-used num)) | |
1199 | (nth (1- num) calc-arg-values)) | |
1200 | (throw 'syntax "Too many # arguments"))) | |
abd880c3 | 1201 | ((equal math-expr-data "(") |
3cedbf72 | 1202 | (let* ((exp (let ((math-exp-keep-spaces nil)) |
136211a9 | 1203 | (math-read-token) |
abd880c3 JB |
1204 | (if (or (equal math-expr-data "\\dots") |
1205 | (equal math-expr-data "\\ldots")) | |
136211a9 EZ |
1206 | '(neg (var inf var-inf)) |
1207 | (math-read-expr-level 0))))) | |
3cedbf72 | 1208 | (let ((math-exp-keep-spaces nil)) |
136211a9 | 1209 | (cond |
abd880c3 | 1210 | ((equal math-expr-data ",") |
136211a9 EZ |
1211 | (progn |
1212 | (math-read-token) | |
1213 | (let ((exp2 (math-read-expr-level 0))) | |
1214 | (setq exp | |
1215 | (if (and exp2 (Math-realp exp) (Math-realp exp2)) | |
1216 | (math-normalize (list 'cplx exp exp2)) | |
1217 | (list '+ exp (list '* exp2 '(var i var-i)))))))) | |
abd880c3 | 1218 | ((equal math-expr-data ";") |
136211a9 EZ |
1219 | (progn |
1220 | (math-read-token) | |
1221 | (let ((exp2 (math-read-expr-level 0))) | |
1222 | (setq exp (if (and exp2 (Math-realp exp) | |
1223 | (Math-anglep exp2)) | |
1224 | (math-normalize (list 'polar exp exp2)) | |
5eb3eeda | 1225 | (require 'calc-ext) |
136211a9 EZ |
1226 | (list '* exp |
1227 | (list 'calcFunc-exp | |
1228 | (list '* | |
1229 | (math-to-radians-2 exp2) | |
1230 | '(var i var-i))))))))) | |
abd880c3 JB |
1231 | ((or (equal math-expr-data "\\dots") |
1232 | (equal math-expr-data "\\ldots")) | |
136211a9 EZ |
1233 | (progn |
1234 | (math-read-token) | |
abd880c3 JB |
1235 | (let ((exp2 (if (or (equal math-expr-data ")") |
1236 | (equal math-expr-data "]") | |
3cedbf72 | 1237 | (eq math-exp-token 'end)) |
136211a9 EZ |
1238 | '(var inf var-inf) |
1239 | (math-read-expr-level 0)))) | |
1240 | (setq exp | |
1241 | (list 'intv | |
abd880c3 | 1242 | (if (equal math-expr-data ")") 0 1) |
136211a9 EZ |
1243 | exp |
1244 | exp2))))))) | |
abd880c3 JB |
1245 | (if (not (or (equal math-expr-data ")") |
1246 | (and (equal math-expr-data "]") (eq (car-safe exp) 'intv)) | |
3cedbf72 | 1247 | (eq math-exp-token 'end))) |
136211a9 EZ |
1248 | (throw 'syntax "Expected `)'")) |
1249 | (math-read-token) | |
1250 | exp)) | |
3cedbf72 | 1251 | ((eq math-exp-token 'string) |
5eb3eeda | 1252 | (require 'calc-ext) |
136211a9 | 1253 | (math-read-string)) |
abd880c3 | 1254 | ((equal math-expr-data "[") |
5eb3eeda | 1255 | (require 'calc-ext) |
136211a9 | 1256 | (math-read-brackets t "]")) |
abd880c3 | 1257 | ((equal math-expr-data "{") |
5eb3eeda | 1258 | (require 'calc-ext) |
136211a9 | 1259 | (math-read-brackets nil "}")) |
abd880c3 | 1260 | ((equal math-expr-data "<") |
5eb3eeda | 1261 | (require 'calc-ext) |
136211a9 | 1262 | (math-read-angle-brackets)) |
07ff2bc8 | 1263 | (t (throw 'syntax "Expected a number"))))) |
136211a9 | 1264 | |
47233535 JB |
1265 | (provide 'calc-aent) |
1266 | ||
2378f044 | 1267 | ;; Local variables: |
be19ef0b | 1268 | ;; coding: utf-8 |
2378f044 SM |
1269 | ;; generated-autoload-file: "calc-loaddefs.el" |
1270 | ;; End: | |
1271 | ||
07ff2bc8 | 1272 | ;;; calc-aent.el ends here |