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