Commit | Line | Data |
---|---|---|
136211a9 | 1 | ;; Calculator for GNU Emacs, part II [calc-embed.el] |
af32d76e | 2 | ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
136211a9 EZ |
3 | ;; Written by Dave Gillespie, daveg@synaptics.com. |
4 | ||
5 | ;; This file is part of GNU Emacs. | |
6 | ||
7 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
8 | ;; but WITHOUT ANY WARRANTY. No author or distributor | |
9 | ;; accepts responsibility to anyone for the consequences of using it | |
10 | ;; or for whether it serves any particular purpose or works at all, | |
11 | ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
12 | ;; License for full details. | |
13 | ||
14 | ;; Everyone is granted permission to copy, modify and redistribute | |
15 | ;; GNU Emacs, but only under the conditions described in the | |
16 | ;; GNU Emacs General Public License. A copy of this license is | |
17 | ;; supposed to have been given to you along with GNU Emacs so you | |
18 | ;; can know your rights and responsibilities. It should be in a | |
19 | ;; file named COPYING. Among other things, the copyright notice | |
20 | ;; and this notice must be preserved on all copies. | |
21 | ||
22 | ||
23 | ||
24 | ;; This file is autoloaded from calc-ext.el. | |
25 | (require 'calc-ext) | |
26 | ||
27 | (require 'calc-macs) | |
28 | ||
29 | (defun calc-Need-calc-embed () nil) | |
30 | ||
31 | ||
32 | (defun calc-show-plain (n) | |
33 | (interactive "P") | |
34 | (calc-wrapper | |
35 | (calc-set-command-flag 'renum-stack) | |
36 | (message (if (calc-change-mode 'calc-show-plain n nil t) | |
37 | "Including \"plain\" formulas in Calc Embedded mode." | |
bf77c646 | 38 | "Omitting \"plain\" formulas in Calc Embedded mode.")))) |
136211a9 EZ |
39 | |
40 | ||
41 | ||
42 | ||
43 | ;;; Things to do for Embedded Mode: | |
44 | ;;; | |
45 | ;;; Detect and strip off unexpected labels during reading. | |
46 | ;;; | |
47 | ;;; Get calc-grab-region to use math-read-big-expr. | |
48 | ;;; If calc-show-plain, main body should have only righthand side of => expr. | |
49 | ;;; Handle tabs that have crept into embedded formulas. | |
50 | ;;; After "switching to new formula", home cursor to that formula. | |
51 | ;;; Do something like \evalto ... \to for \gets operators. | |
52 | ;;; | |
53 | ||
54 | ||
55 | (defvar calc-embedded-modes nil) | |
56 | (defvar calc-embedded-globals nil) | |
57 | (defvar calc-embedded-active nil) | |
58 | ||
59 | (make-variable-buffer-local 'calc-embedded-all-active) | |
60 | (make-variable-buffer-local 'calc-embedded-some-active) | |
61 | ||
62 | ||
63 | (defvar calc-embedded-open-formula "\\`\\|^\n\\|\\$\\$?\\|\\\\\\[\\|^\\\\begin.*\n\\|^@.*\n\\|^\\.EQ.*\n\\|\\\\(\\|^%\n\\|^\\.\\\\\"\n" | |
64 | "*A regular expression for the opening delimiter of a formula used by | |
65 | calc-embedded.") | |
66 | ||
67 | (defvar calc-embedded-close-formula "\\'\\|\n$\\|\\$\\$?\\|\\\\]\\|^\\\\end.*\n\\|^@.*\n\\|^\\.EN.*\n\\|\\\\)\\|\n%\n\\|^\\.\\\\\"\n" | |
68 | "*A regular expression for the closing delimiter of a formula used by | |
69 | calc-embedded.") | |
70 | ||
71 | (defvar calc-embedded-open-word "^\\|[^-+0-9.eE]" | |
72 | "*A regular expression for the opening delimiter of a formula used by | |
73 | calc-embedded-word.") | |
74 | ||
75 | (defvar calc-embedded-close-word "$\\|[^-+0-9.eE]" | |
76 | "*A regular expression for the closing delimiter of a formula used by | |
77 | calc-embedded-word.") | |
78 | ||
79 | (defvar calc-embedded-open-plain "%%% " | |
80 | "*A string which is the opening delimiter for a \"plain\" formula. | |
81 | If calc-show-plain mode is enabled, this is inserted at the front of | |
82 | each formula.") | |
83 | ||
84 | (defvar calc-embedded-close-plain " %%%\n" | |
85 | "*A string which is the closing delimiter for a \"plain\" formula. | |
86 | See calc-embedded-open-plain.") | |
87 | ||
88 | (defvar calc-embedded-open-new-formula "\n\n" | |
89 | "*A string which is inserted at front of formula by calc-embedded-new-formula.") | |
90 | ||
91 | (defvar calc-embedded-close-new-formula "\n\n" | |
92 | "*A string which is inserted at end of formula by calc-embedded-new-formula.") | |
93 | ||
94 | (defvar calc-embedded-announce-formula "%Embed\n\\(% .*\n\\)*" | |
95 | "*A regular expression which is sure to be followed by a calc-embedded formula." ) | |
96 | ||
97 | (defvar calc-embedded-open-mode "% " | |
98 | "*A string which should precede calc-embedded mode annotations. | |
99 | This is not required to be present for user-written mode annotations.") | |
100 | ||
101 | (defvar calc-embedded-close-mode "\n" | |
102 | "*A string which should follow calc-embedded mode annotations. | |
103 | This is not required to be present for user-written mode annotations.") | |
104 | ||
105 | ||
106 | (defconst calc-embedded-mode-vars '(("precision" . calc-internal-prec) | |
107 | ("word-size" . calc-word-size) | |
108 | ("angles" . calc-angle-mode) | |
109 | ("symbolic" . calc-symbolic-mode) | |
110 | ("matrix" . calc-matrix-mode) | |
111 | ("fractions" . calc-prefer-frac) | |
112 | ("complex" . calc-complex-mode) | |
113 | ("simplify" . calc-simplify-mode) | |
114 | ("language" . the-language) | |
115 | ("plain" . calc-show-plain) | |
116 | ("break" . calc-line-breaking) | |
117 | ("justify" . the-display-just) | |
118 | ("left-label" . calc-left-label) | |
119 | ("right-label" . calc-right-label) | |
120 | ("radix" . calc-number-radix) | |
121 | ("leading-zeros" . calc-leading-zeros) | |
122 | ("grouping" . calc-group-digits) | |
123 | ("group-char" . calc-group-char) | |
124 | ("point-char" . calc-point-char) | |
125 | ("frac-format" . calc-frac-format) | |
126 | ("float-format" . calc-float-format) | |
127 | ("complex-format" . calc-complex-format) | |
128 | ("hms-format" . calc-hms-format) | |
129 | ("date-format" . calc-date-format) | |
130 | ("matrix-justify" . calc-matrix-just) | |
131 | ("full-vectors" . calc-full-vectors) | |
132 | ("break-vectors" . calc-break-vectors) | |
133 | ("vector-commas" . calc-vector-commas) | |
134 | ("vector-brackets" . calc-vector-brackets) | |
135 | ("matrix-brackets" . calc-matrix-brackets) | |
136 | ("strings" . calc-display-strings) | |
137 | )) | |
138 | ||
139 | ||
140 | ;;; Format of calc-embedded-info vector: | |
141 | ;;; 0 Editing buffer. | |
142 | ;;; 1 Calculator buffer. | |
143 | ;;; 2 Top of current formula (marker). | |
144 | ;;; 3 Bottom of current formula (marker). | |
145 | ;;; 4 Top of current formula's delimiters (marker). | |
146 | ;;; 5 Bottom of current formula's delimiters (marker). | |
147 | ;;; 6 String representation of current formula. | |
148 | ;;; 7 Non-nil if formula is embedded within a single line. | |
149 | ;;; 8 Internal representation of current formula. | |
150 | ;;; 9 Variable assigned by this formula, or nil. | |
151 | ;;; 10 List of variables upon which this formula depends. | |
152 | ;;; 11 Evaluated value of the formula, or nil. | |
153 | ;;; 12 Mode settings for current formula. | |
154 | ;;; 13 Local mode settings for current formula. | |
155 | ;;; 14 Permanent mode settings for current formula. | |
156 | ;;; 15 Global mode settings for editing buffer. | |
157 | ||
158 | ||
159 | ;;; calc-embedded-active is an a-list keyed on buffers; each cdr is a | |
160 | ;;; sorted list of calc-embedded-infos in that buffer. We do this | |
161 | ;;; rather than using buffer-local variables because the latter are | |
162 | ;;; thrown away when a buffer changes major modes. | |
163 | ||
164 | ||
165 | (defun calc-do-embedded (arg end obeg oend) | |
166 | (if calc-embedded-info | |
167 | ||
168 | ;; Turn embedded mode off or switch to a new buffer. | |
169 | (cond ((eq (current-buffer) (aref calc-embedded-info 1)) | |
170 | (let ((calcbuf (current-buffer)) | |
171 | (buf (aref calc-embedded-info 0))) | |
172 | (calc-embedded-original-buffer t) | |
173 | (calc-embedded nil) | |
174 | (switch-to-buffer calcbuf))) | |
175 | ||
176 | ((eq (current-buffer) (aref calc-embedded-info 0)) | |
177 | (let* ((info calc-embedded-info) | |
178 | (mode calc-embedded-modes)) | |
179 | (save-excursion | |
180 | (set-buffer (aref info 1)) | |
181 | (if (and (> (calc-stack-size) 0) | |
182 | (equal (calc-top 1 'full) (aref info 8))) | |
183 | (let ((calc-no-refresh-evaltos t)) | |
184 | (if (calc-top 1 'sel) | |
185 | (calc-unselect 1)) | |
186 | (calc-embedded-set-modes | |
187 | (aref info 15) (aref info 12) (aref info 14)) | |
188 | (let ((calc-embedded-info nil)) | |
189 | (calc-wrapper (calc-pop-stack)))) | |
190 | (calc-set-mode-line))) | |
191 | (setq calc-embedded-info nil | |
192 | mode-line-buffer-identification (car mode) | |
193 | truncate-lines (nth 2 mode) | |
194 | buffer-read-only nil) | |
195 | (use-local-map (nth 1 mode)) | |
196 | (set-buffer-modified-p (buffer-modified-p)) | |
197 | (or calc-embedded-quiet | |
198 | (message "Back to %s mode." mode-name)))) | |
199 | ||
200 | (t | |
201 | (if (buffer-name (aref calc-embedded-info 0)) | |
202 | (save-excursion | |
203 | (set-buffer (aref calc-embedded-info 0)) | |
af32d76e EZ |
204 | (or (y-or-n-p (format "Cancel Calc Embedded mode in buffer %s? " |
205 | (buffer-name))) | |
136211a9 EZ |
206 | (keyboard-quit)) |
207 | (calc-embedded nil))) | |
208 | (calc-embedded arg end obeg oend))) | |
209 | ||
210 | ;; Turn embedded mode on. | |
211 | (calc-plain-buffer-only) | |
212 | (let ((modes (list mode-line-buffer-identification | |
213 | (current-local-map) | |
214 | truncate-lines)) | |
215 | top bot outer-top outer-bot | |
216 | info chg ident) | |
217 | (barf-if-buffer-read-only) | |
218 | (or calc-embedded-globals | |
219 | (calc-find-globals)) | |
220 | (setq info (calc-embedded-make-info (point) nil t arg end obeg oend)) | |
221 | (if (eq (car-safe (aref info 8)) 'error) | |
222 | (progn | |
223 | (goto-char (nth 1 (aref info 8))) | |
224 | (error (nth 2 (aref info 8))))) | |
225 | (let ((mode-line-buffer-identification mode-line-buffer-identification) | |
226 | (calc-embedded-info info) | |
227 | (calc-embedded-no-reselect t)) | |
228 | (calc-wrapper | |
229 | (let* ((okay nil) | |
230 | (calc-no-refresh-evaltos t)) | |
231 | (setq chg (calc-embedded-set-modes | |
232 | (aref info 15) (aref info 12) (aref info 13))) | |
233 | (if (aref info 8) | |
234 | (calc-push (calc-normalize (aref info 8))) | |
235 | (calc-alg-entry))) | |
236 | (setq calc-undo-list nil | |
237 | calc-redo-list nil | |
238 | ident mode-line-buffer-identification))) | |
239 | (setq calc-embedded-info info | |
240 | calc-embedded-modes modes | |
241 | mode-line-buffer-identification ident | |
242 | truncate-lines t | |
243 | buffer-read-only t) | |
244 | (set-buffer-modified-p (buffer-modified-p)) | |
245 | (use-local-map calc-mode-map) | |
246 | (setq calc-no-refresh-evaltos nil) | |
247 | (and chg calc-any-evaltos (calc-wrapper (calc-refresh-evaltos))) | |
248 | (or (eq calc-embedded-quiet t) | |
249 | (message "Embedded Calc mode enabled. %s to return to normal." | |
250 | (if calc-embedded-quiet | |
251 | "Type `M-# x'" | |
252 | "Give this command again"))))) | |
bf77c646 | 253 | (scroll-down 0)) ; fix a bug which occurs when truncate-lines is changed. |
136211a9 EZ |
254 | (setq calc-embedded-quiet nil) |
255 | ||
256 | ||
257 | (defun calc-embedded-select (arg) | |
258 | (interactive "P") | |
259 | (calc-embedded arg) | |
260 | (and calc-embedded-info | |
261 | (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-evalto) | |
262 | (calc-select-part 1)) | |
263 | (and calc-embedded-info | |
264 | (or (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-assign) | |
265 | (and (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-evalto) | |
266 | (eq (car-safe (nth 1 (aref calc-embedded-info 8))) | |
267 | 'calcFunc-assign))) | |
bf77c646 | 268 | (calc-select-part 2))) |
136211a9 EZ |
269 | |
270 | ||
271 | (defun calc-embedded-update-formula (arg) | |
272 | (interactive "P") | |
273 | (if arg | |
274 | (let ((entry (assq (current-buffer) calc-embedded-active))) | |
275 | (while (setq entry (cdr entry)) | |
276 | (and (eq (car-safe (aref (car entry) 8)) 'calcFunc-evalto) | |
277 | (or (not (consp arg)) | |
278 | (and (<= (aref (car entry) 2) (region-beginning)) | |
279 | (>= (aref (car entry) 3) (region-end)))) | |
280 | (save-excursion | |
281 | (calc-embedded-update (car entry) 14 t t))))) | |
282 | (if (and calc-embedded-info | |
283 | (eq (current-buffer) (aref calc-embedded-info 0)) | |
284 | (>= (point) (aref calc-embedded-info 4)) | |
285 | (<= (point) (aref calc-embedded-info 5))) | |
286 | (calc-evaluate 1) | |
287 | (let* ((opt (point)) | |
288 | (info (calc-embedded-make-info (point) nil t)) | |
289 | (pt (- opt (aref info 4)))) | |
290 | (or (eq (car-safe (aref info 8)) 'error) | |
291 | (progn | |
292 | (save-excursion | |
293 | (calc-embedded-update info 14 'eval t)) | |
bf77c646 | 294 | (goto-char (+ (aref info 4) pt)))))))) |
136211a9 EZ |
295 | |
296 | ||
297 | (defun calc-embedded-edit (arg) | |
298 | (interactive "P") | |
299 | (let ((info (calc-embedded-make-info (point) nil t arg)) | |
300 | str) | |
301 | (if (eq (car-safe (aref info 8)) 'error) | |
302 | (progn | |
303 | (goto-char (nth 1 (aref info 8))) | |
304 | (error (nth 2 (aref info 8))))) | |
305 | (calc-wrapper | |
306 | (setq str (math-showing-full-precision | |
af32d76e | 307 | (math-format-nice-expr (aref info 8) (frame-width)))) |
136211a9 EZ |
308 | (calc-edit-mode (list 'calc-embedded-finish-edit info)) |
309 | (insert str "\n"))) | |
bf77c646 | 310 | (calc-show-edit-buffer)) |
136211a9 EZ |
311 | |
312 | (defun calc-embedded-finish-edit (info) | |
313 | (let ((buf (current-buffer)) | |
314 | (str (buffer-substring (point) (point-max))) | |
315 | (start (point)) | |
316 | pos) | |
317 | (switch-to-buffer calc-original-buffer) | |
318 | (let ((val (save-excursion | |
319 | (set-buffer (aref info 1)) | |
320 | (let ((calc-language nil) | |
321 | (math-expr-opers math-standard-opers)) | |
322 | (math-read-expr str))))) | |
323 | (if (eq (car-safe val) 'error) | |
324 | (progn | |
325 | (switch-to-buffer buf) | |
326 | (goto-char (+ start (nth 1 val))) | |
327 | (error (nth 2 val)))) | |
328 | (calc-embedded-original-buffer t info) | |
329 | (aset info 8 val) | |
bf77c646 | 330 | (calc-embedded-update info 14 t t)))) |
136211a9 EZ |
331 | |
332 | (defun calc-do-embedded-activate (arg cbuf) | |
333 | (calc-plain-buffer-only) | |
334 | (if arg | |
335 | (calc-embedded-forget)) | |
336 | (calc-find-globals) | |
337 | (if (< (prefix-numeric-value arg) 0) | |
338 | (message "Deactivating %s for Calc Embedded mode." (buffer-name)) | |
339 | (message "Activating %s for Calc Embedded mode..." (buffer-name)) | |
340 | (save-excursion | |
341 | (let* ((active (assq (current-buffer) calc-embedded-active)) | |
342 | (info active) | |
343 | (pat " := \\| \\\\gets \\| => \\| \\\\evalto ")) | |
344 | (if calc-embedded-announce-formula | |
345 | (setq pat (format "%s\\|\\(%s\\)" | |
346 | pat calc-embedded-announce-formula))) | |
347 | (while (setq info (cdr info)) | |
348 | (or (equal (buffer-substring (aref (car info) 2) (aref (car info) 3)) | |
349 | (aref (car info) 6)) | |
350 | (setcdr active (delq (car info) (cdr active))))) | |
351 | (goto-char (point-min)) | |
352 | (while (re-search-forward pat nil t) | |
353 | (if (looking-at calc-embedded-open-formula) | |
354 | (goto-char (match-end 1))) | |
355 | (setq info (calc-embedded-make-info (point) cbuf nil)) | |
356 | (or (eq (car-safe (aref info 8)) 'error) | |
357 | (goto-char (aref info 5)))))) | |
358 | (message "Activating %s for Calc Embedded mode...done" (buffer-name))) | |
bf77c646 | 359 | (calc-embedded-active-state t)) |
136211a9 EZ |
360 | |
361 | (defun calc-plain-buffer-only () | |
362 | (if (memq major-mode '(calc-mode calc-trail-mode calc-edit-mode)) | |
bf77c646 | 363 | (error "This command should be used in a normal editing buffer"))) |
136211a9 EZ |
364 | |
365 | (defun calc-embedded-active-state (state) | |
366 | (or (assq 'calc-embedded-all-active minor-mode-alist) | |
367 | (setq minor-mode-alist | |
368 | (cons '(calc-embedded-all-active " Active") | |
369 | (cons '(calc-embedded-some-active " ~Active") | |
370 | minor-mode-alist)))) | |
371 | (let ((active (assq (current-buffer) calc-embedded-active))) | |
372 | (or (cdr active) | |
373 | (setq state nil))) | |
374 | (and (eq state 'more) calc-embedded-all-active (setq state t)) | |
375 | (setq calc-embedded-all-active (eq state t) | |
376 | calc-embedded-some-active (not (memq state '(nil t)))) | |
bf77c646 | 377 | (set-buffer-modified-p (buffer-modified-p))) |
136211a9 EZ |
378 | |
379 | ||
380 | (defun calc-embedded-original-buffer (switch &optional info) | |
381 | (or info (setq info calc-embedded-info)) | |
382 | (or (buffer-name (aref info 0)) | |
383 | (progn | |
384 | (error "Calc embedded mode: Original buffer has been killed"))) | |
385 | (if switch | |
bf77c646 | 386 | (set-buffer (aref info 0)))) |
136211a9 EZ |
387 | |
388 | (defun calc-embedded-word () | |
389 | (interactive) | |
bf77c646 | 390 | (calc-embedded '(4))) |
136211a9 EZ |
391 | |
392 | (defun calc-embedded-mark-formula (&optional body-only) | |
393 | "Put point at the beginning of this Calc formula, mark at the end. | |
394 | This normally marks the whole formula, including surrounding delimiters. | |
395 | With any prefix argument, marks only the formula itself." | |
396 | (interactive "P") | |
397 | (and (eq major-mode 'calc-mode) | |
398 | (error "This command should be used in a normal editing buffer")) | |
399 | (let (top bot outer-top outer-bot) | |
400 | (save-excursion | |
401 | (calc-embedded-find-bounds body-only)) | |
402 | (push-mark (if body-only bot outer-bot) t) | |
bf77c646 | 403 | (goto-char (if body-only top outer-top)))) |
136211a9 EZ |
404 | |
405 | (defun calc-embedded-find-bounds (&optional plain) | |
406 | ;; (while (and (bolp) (eq (following-char) ?\n)) | |
407 | ;; (forward-char 1)) | |
408 | (and (eolp) (bolp) (not (eq (char-after (- (point) 2)) ?\n)) | |
409 | (forward-char -1)) | |
410 | (let ((home (point))) | |
411 | (or (and (looking-at calc-embedded-open-formula) | |
412 | (not (looking-at calc-embedded-close-formula))) | |
413 | (re-search-backward calc-embedded-open-formula nil t) | |
414 | (error "Can't find start of formula")) | |
415 | (and (eq (preceding-char) ?\$) ; backward search for \$\$? won't back | |
416 | (eq (following-char) ?\$) ; up over a second $, so do it by hand. | |
417 | (forward-char -1)) | |
418 | (setq outer-top (point)) | |
419 | (goto-char (match-end 0)) | |
420 | (if (eq (following-char) ?\n) | |
421 | (forward-char 1)) | |
422 | (or (bolp) | |
423 | (while (eq (following-char) ?\ ) | |
424 | (forward-char 1))) | |
425 | (or (eq plain 'plain) | |
426 | (if (looking-at (regexp-quote calc-embedded-open-plain)) | |
427 | (progn | |
428 | (goto-char (match-end 0)) | |
429 | (search-forward calc-embedded-close-plain)))) | |
430 | (setq top (point)) | |
431 | (or (re-search-forward calc-embedded-close-formula nil t) | |
432 | (error "Can't find end of formula")) | |
433 | (if (< (point) home) | |
434 | (error "Not inside a formula")) | |
435 | (and (eq (following-char) ?\n) (not (bolp)) | |
436 | (forward-char 1)) | |
437 | (setq outer-bot (point)) | |
438 | (goto-char (match-beginning 0)) | |
439 | (if (eq (preceding-char) ?\n) | |
440 | (backward-char 1)) | |
441 | (or (eolp) | |
442 | (while (eq (preceding-char) ?\ ) | |
443 | (backward-char 1))) | |
bf77c646 | 444 | (setq bot (point)))) |
136211a9 EZ |
445 | |
446 | (defun calc-embedded-kill-formula () | |
447 | "Kill the formula surrounding point. | |
448 | If Calc Embedded mode was active, this deactivates it. | |
449 | The formula (including its surrounding delimiters) is saved in the kill ring. | |
450 | The command \\[yank] can retrieve it from there." | |
451 | (interactive) | |
452 | (and calc-embedded-info | |
453 | (calc-embedded nil)) | |
454 | (calc-embedded-mark-formula) | |
455 | (kill-region (point) (mark)) | |
bf77c646 | 456 | (pop-mark)) |
136211a9 EZ |
457 | |
458 | (defun calc-embedded-copy-formula-as-kill () | |
459 | "Save the formula surrounding point as if killed, but don't kill it." | |
460 | (interactive) | |
461 | (save-excursion | |
462 | (calc-embedded-mark-formula) | |
463 | (copy-region-as-kill (point) (mark)) | |
bf77c646 | 464 | (pop-mark))) |
136211a9 EZ |
465 | |
466 | (defun calc-embedded-duplicate () | |
467 | (interactive) | |
468 | (let ((already calc-embedded-info) | |
469 | top bot outer-top outer-bot new-top) | |
470 | (if calc-embedded-info | |
471 | (progn | |
472 | (setq top (+ (aref calc-embedded-info 2)) | |
473 | bot (+ (aref calc-embedded-info 3)) | |
474 | outer-top (+ (aref calc-embedded-info 4)) | |
475 | outer-bot (+ (aref calc-embedded-info 5))) | |
476 | (calc-embedded nil)) | |
477 | (calc-embedded-find-bounds)) | |
478 | (goto-char outer-bot) | |
479 | (insert "\n") | |
480 | (setq new-top (point)) | |
481 | (insert-buffer-substring (current-buffer) outer-top outer-bot) | |
482 | (goto-char (+ new-top (- top outer-top))) | |
483 | (let ((calc-embedded-quiet (if already t 'x))) | |
484 | (calc-embedded (+ new-top (- top outer-top)) | |
485 | (+ new-top (- bot outer-top)) | |
486 | new-top | |
bf77c646 | 487 | (+ new-top (- outer-bot outer-top)))))) |
136211a9 EZ |
488 | |
489 | (defun calc-embedded-next (arg) | |
490 | (interactive "P") | |
491 | (setq arg (prefix-numeric-value arg)) | |
492 | (let* ((active (cdr (assq (current-buffer) calc-embedded-active))) | |
493 | (p active) | |
494 | (num (length active))) | |
495 | (or active | |
496 | (error "No active formulas in buffer")) | |
497 | (cond ((= arg 0)) | |
498 | ((= arg -1) | |
499 | (if (<= (point) (aref (car active) 3)) | |
500 | (goto-char (aref (nth (1- num) active) 2)) | |
501 | (while (and (cdr p) | |
502 | (> (point) (aref (nth 1 p) 3))) | |
503 | (setq p (cdr p))) | |
504 | (goto-char (aref (car p) 2)))) | |
505 | ((< arg -1) | |
506 | (calc-embedded-next -1) | |
507 | (calc-embedded-next (+ (* num 1000) arg 1))) | |
508 | (t | |
509 | (setq arg (1+ (% (1- arg) num))) | |
510 | (while (and p (>= (point) (aref (car p) 2))) | |
511 | (setq p (cdr p))) | |
512 | (while (> (setq arg (1- arg)) 0) | |
513 | (setq p (if p (cdr p) (cdr active)))) | |
bf77c646 | 514 | (goto-char (aref (car (or p active)) 2)))))) |
136211a9 EZ |
515 | |
516 | (defun calc-embedded-previous (arg) | |
517 | (interactive "p") | |
bf77c646 | 518 | (calc-embedded-next (- (prefix-numeric-value arg)))) |
136211a9 EZ |
519 | |
520 | (defun calc-embedded-new-formula () | |
521 | (interactive) | |
522 | (and (eq major-mode 'calc-mode) | |
523 | (error "This command should be used in a normal editing buffer")) | |
524 | (if calc-embedded-info | |
525 | (calc-embedded nil)) | |
526 | (let (top bot outer-top outer-bot) | |
527 | (if (and (eq (preceding-char) ?\n) | |
528 | (string-match "\\`\n" calc-embedded-open-new-formula)) | |
529 | (progn | |
530 | (setq outer-top (1- (point))) | |
531 | (forward-char -1) | |
532 | (insert (substring calc-embedded-open-new-formula 1))) | |
533 | (setq outer-top (point)) | |
534 | (insert calc-embedded-open-new-formula)) | |
535 | (setq top (point)) | |
536 | (insert " ") | |
537 | (setq bot (point)) | |
538 | (insert calc-embedded-close-new-formula) | |
539 | (if (and (eq (following-char) ?\n) | |
540 | (string-match "\n\\'" calc-embedded-close-new-formula)) | |
541 | (delete-char 1)) | |
542 | (setq outer-bot (point)) | |
543 | (goto-char top) | |
544 | (let ((calc-embedded-quiet 'x)) | |
bf77c646 | 545 | (calc-embedded top bot outer-top outer-bot)))) |
136211a9 EZ |
546 | |
547 | (defun calc-embedded-forget () | |
548 | (interactive) | |
549 | (setq calc-embedded-active (delq (assq (current-buffer) calc-embedded-active) | |
550 | calc-embedded-active)) | |
bf77c646 | 551 | (calc-embedded-active-state nil)) |
136211a9 EZ |
552 | |
553 | ||
554 | (defun calc-embedded-set-modes (gmodes modes local-modes &optional temp) | |
555 | (let ((the-language (calc-embedded-language)) | |
556 | (the-display-just (calc-embedded-justify)) | |
557 | (v gmodes) | |
558 | (changed nil) | |
559 | found value) | |
560 | (while v | |
561 | (or (symbolp (car v)) | |
562 | (and (setq found (assq (car (car v)) modes)) | |
563 | (not (eq (cdr found) 'default))) | |
564 | (and (setq found (assq (car (car v)) local-modes)) | |
565 | (not (eq (cdr found) 'default))) | |
566 | (progn | |
567 | (if (eq (setq value (cdr (car v))) 'default) | |
568 | (setq value (cdr (assq (car (car v)) calc-mode-var-list)))) | |
569 | (equal (symbol-value (car (car v))) value)) | |
570 | (progn | |
571 | (setq changed t) | |
572 | (if temp (setq prev-modes (cons (cons (car (car v)) | |
573 | (symbol-value (car (car v)))) | |
574 | prev-modes))) | |
575 | (set (car (car v)) value))) | |
576 | (setq v (cdr v))) | |
577 | (setq v modes) | |
578 | (while v | |
579 | (or (and (setq found (assq (car (car v)) local-modes)) | |
580 | (not (eq (cdr found) 'default))) | |
581 | (eq (setq value (cdr (car v))) 'default) | |
582 | (equal (symbol-value (car (car v))) value) | |
583 | (progn | |
584 | (setq changed t) | |
585 | (if temp (setq prev-modes (cons (cons (car (car v)) | |
586 | (symbol-value (car (car v)))) | |
587 | prev-modes))) | |
588 | (set (car (car v)) value))) | |
589 | (setq v (cdr v))) | |
590 | (setq v local-modes) | |
591 | (while v | |
592 | (or (eq (setq value (cdr (car v))) 'default) | |
593 | (equal (symbol-value (car (car v))) value) | |
594 | (progn | |
595 | (setq changed t) | |
596 | (if temp (setq prev-modes (cons (cons (car (car v)) | |
597 | (symbol-value (car (car v)))) | |
598 | prev-modes))) | |
599 | (set (car (car v)) value))) | |
600 | (setq v (cdr v))) | |
601 | (and changed (not (eq temp t)) | |
602 | (progn | |
603 | (calc-embedded-set-justify the-display-just) | |
604 | (calc-embedded-set-language the-language))) | |
605 | (and changed (not temp) | |
606 | (progn | |
607 | (setq calc-full-float-format (list (if (eq (car calc-float-format) | |
608 | 'fix) | |
609 | 'float | |
610 | (car calc-float-format)) | |
611 | 0)) | |
612 | (calc-refresh))) | |
bf77c646 | 613 | changed)) |
136211a9 EZ |
614 | |
615 | (defun calc-embedded-language () | |
616 | (if calc-language-option | |
617 | (list calc-language calc-language-option) | |
bf77c646 | 618 | calc-language)) |
136211a9 EZ |
619 | |
620 | (defun calc-embedded-set-language (lang) | |
621 | (let ((option nil)) | |
622 | (if (consp lang) | |
623 | (setq option (nth 1 lang) | |
624 | lang (car lang))) | |
625 | (or (and (eq lang calc-language) | |
626 | (equal option calc-language-option)) | |
bf77c646 | 627 | (calc-set-language lang option t)))) |
136211a9 EZ |
628 | |
629 | (defun calc-embedded-justify () | |
630 | (if calc-display-origin | |
631 | (list calc-display-just calc-display-origin) | |
bf77c646 | 632 | calc-display-just)) |
136211a9 EZ |
633 | |
634 | (defun calc-embedded-set-justify (just) | |
635 | (if (consp just) | |
636 | (setq calc-display-origin (nth 1 just) | |
637 | calc-display-just (car just)) | |
638 | (setq calc-display-just just | |
bf77c646 | 639 | calc-display-origin nil))) |
136211a9 EZ |
640 | |
641 | ||
642 | (defun calc-find-globals () | |
643 | (interactive) | |
644 | (and (eq major-mode 'calc-mode) | |
645 | (error "This command should be used in a normal editing buffer")) | |
646 | (make-local-variable 'calc-embedded-globals) | |
647 | (let ((case-fold-search nil) | |
648 | (modes nil) | |
649 | (save-pt (point)) | |
650 | found value) | |
651 | (goto-char (point-min)) | |
652 | (while (re-search-forward "\\[calc-global-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)\\]" nil t) | |
653 | (and (setq found (assoc (buffer-substring (match-beginning 1) | |
654 | (match-end 1)) | |
655 | calc-embedded-mode-vars)) | |
656 | (or (assq (cdr found) modes) | |
657 | (setq modes (cons (cons (cdr found) | |
658 | (car (read-from-string | |
659 | (buffer-substring | |
660 | (match-beginning 2) | |
661 | (match-end 2))))) | |
662 | modes))))) | |
663 | (setq calc-embedded-globals (cons t modes)) | |
bf77c646 | 664 | (goto-char save-pt))) |
136211a9 EZ |
665 | |
666 | (defun calc-embedded-find-modes () | |
667 | (let ((case-fold-search nil) | |
668 | (save-pt (point)) | |
669 | (no-defaults t) | |
670 | (modes nil) | |
671 | (emodes nil) | |
672 | (pmodes nil) | |
673 | found value) | |
674 | (while (and no-defaults (search-backward "[calc-" nil t)) | |
675 | (forward-char 6) | |
676 | (or (and (looking-at "mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]") | |
677 | (setq found (assoc (buffer-substring (match-beginning 1) | |
678 | (match-end 1)) | |
679 | calc-embedded-mode-vars)) | |
680 | (or (assq (cdr found) modes) | |
681 | (setq modes (cons (cons (cdr found) | |
682 | (car (read-from-string | |
683 | (buffer-substring | |
684 | (match-beginning 2) | |
685 | (match-end 2))))) | |
686 | modes)))) | |
687 | (and (looking-at "perm-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]") | |
688 | (setq found (assoc (buffer-substring (match-beginning 1) | |
689 | (match-end 1)) | |
690 | calc-embedded-mode-vars)) | |
691 | (or (assq (cdr found) pmodes) | |
692 | (setq pmodes (cons (cons (cdr found) | |
693 | (car (read-from-string | |
694 | (buffer-substring | |
695 | (match-beginning 2) | |
696 | (match-end 2))))) | |
697 | pmodes)))) | |
698 | (and (looking-at "edit-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]") | |
699 | (setq found (assoc (buffer-substring (match-beginning 1) | |
700 | (match-end 1)) | |
701 | calc-embedded-mode-vars)) | |
702 | (or (assq (cdr found) emodes) | |
703 | (setq emodes (cons (cons (cdr found) | |
704 | (car (read-from-string | |
705 | (buffer-substring | |
706 | (match-beginning 2) | |
707 | (match-end 2))))) | |
708 | emodes)))) | |
709 | (and (looking-at "defaults]") | |
710 | (setq no-defaults nil))) | |
711 | (backward-char 6)) | |
712 | (goto-char save-pt) | |
bf77c646 | 713 | (list modes emodes pmodes))) |
136211a9 EZ |
714 | |
715 | ||
716 | (defun calc-embedded-make-info (point cbuf fresh &optional | |
717 | top bot outer-top outer-bot) | |
718 | (let* ((bufentry (assq (current-buffer) calc-embedded-active)) | |
719 | (found bufentry) | |
720 | (force (and fresh top)) | |
721 | (fixed top) | |
722 | (new-info nil) | |
723 | info str) | |
724 | (or found | |
725 | (setq found (list (current-buffer)) | |
726 | calc-embedded-active (cons found calc-embedded-active))) | |
727 | (while (and (cdr found) | |
728 | (> point (aref (car (cdr found)) 3))) | |
729 | (setq found (cdr found))) | |
730 | (if (and (cdr found) | |
731 | (>= point (aref (nth 1 found) 2))) | |
732 | (setq info (nth 1 found)) | |
733 | (setq info (make-vector 16 nil) | |
734 | new-info t | |
735 | fresh t) | |
736 | (aset info 0 (current-buffer)) | |
737 | (aset info 1 (or cbuf (save-excursion | |
738 | (calc-create-buffer) | |
739 | (current-buffer))))) | |
740 | (if (and (integerp top) (not bot)) ; started with a user-supplied argument | |
741 | (progn | |
742 | (if (= (setq arg (prefix-numeric-value arg)) 0) | |
743 | (progn | |
744 | (aset info 2 (copy-marker (region-beginning))) | |
745 | (aset info 3 (copy-marker (region-end)))) | |
746 | (aset info (if (> arg 0) 2 3) (point-marker)) | |
747 | (forward-line arg) | |
748 | (aset info (if (> arg 0) 3 2) (point-marker))) | |
749 | (aset info 4 (copy-marker (aref info 2))) | |
750 | (aset info 5 (copy-marker (aref info 3)))) | |
751 | (if (aref info 4) | |
752 | (setq top (aref info 2) | |
753 | fixed top) | |
754 | (if (consp top) | |
755 | (let ((calc-embedded-open-formula calc-embedded-open-word) | |
756 | (calc-embedded-close-formula calc-embedded-close-word)) | |
757 | (calc-embedded-find-bounds 'plain)) | |
758 | (or top | |
759 | (calc-embedded-find-bounds 'plain))) | |
760 | (aset info 2 (copy-marker (min top bot))) | |
761 | (aset info 3 (copy-marker (max top bot))) | |
762 | (aset info 4 (copy-marker (or outer-top (aref info 2)))) | |
763 | (aset info 5 (copy-marker (or outer-bot (aref info 3)))))) | |
764 | (goto-char (aref info 2)) | |
765 | (if new-info | |
766 | (progn | |
767 | (or (bolp) (aset info 7 t)) | |
768 | (goto-char (aref info 3)) | |
769 | (or (bolp) (eolp) (aset info 7 t)))) | |
770 | (if fresh | |
771 | (let ((modes (calc-embedded-find-modes))) | |
772 | (aset info 12 (car modes)) | |
773 | (aset info 13 (nth 1 modes)) | |
774 | (aset info 14 (nth 2 modes)))) | |
775 | (aset info 15 calc-embedded-globals) | |
776 | (setq str (buffer-substring (aref info 2) (aref info 3))) | |
777 | (if (or force | |
778 | (not (equal str (aref info 6)))) | |
779 | (if (and fixed (aref info 6)) | |
780 | (progn | |
781 | (aset info 4 nil) | |
782 | (calc-embedded-make-info point cbuf nil) | |
783 | (setq new-info nil)) | |
784 | (let* ((open-plain calc-embedded-open-plain) | |
785 | (close-plain calc-embedded-close-plain) | |
786 | (pref-len (length open-plain)) | |
787 | (vars-used nil) | |
788 | suff-pos val temp) | |
789 | (save-excursion | |
790 | (set-buffer (aref info 1)) | |
791 | (calc-embedded-set-modes (aref info 15) | |
792 | (aref info 12) (aref info 14)) | |
793 | (if (and (> (length str) pref-len) | |
794 | (equal (substring str 0 pref-len) open-plain) | |
795 | (setq suff-pos (string-match (regexp-quote close-plain) | |
796 | str pref-len))) | |
797 | (setq val (math-read-plain-expr | |
798 | (substring str pref-len suff-pos))) | |
799 | (if (string-match "[^ \t\n]" str) | |
800 | (setq pref-len 0 | |
801 | val (math-read-big-expr str)) | |
802 | (setq val nil)))) | |
803 | (if (eq (car-safe val) 'error) | |
804 | (setq val (list 'error | |
805 | (+ (aref info 2) pref-len (nth 1 val)) | |
806 | (nth 2 val)))) | |
807 | (aset info 6 str) | |
808 | (aset info 8 val) | |
809 | (setq temp val) | |
810 | (if (eq (car-safe temp) 'calcFunc-evalto) | |
811 | (setq temp (nth 1 temp)) | |
812 | (if (eq (car-safe temp) 'error) | |
813 | (if new-info | |
814 | (setq new-info nil) | |
815 | (setcdr found (delq info (cdr found))) | |
816 | (calc-embedded-active-state 'less)))) | |
817 | (aset info 9 (and (eq (car-safe temp) 'calcFunc-assign) | |
818 | (nth 1 temp))) | |
819 | (if (memq (car-safe val) '(calcFunc-evalto calcFunc-assign)) | |
820 | (calc-embedded-find-vars val)) | |
821 | (aset info 10 vars-used) | |
822 | (aset info 11 nil)))) | |
823 | (if new-info | |
824 | (progn | |
825 | (setcdr found (cons info (cdr found))) | |
826 | (calc-embedded-active-state 'more))) | |
bf77c646 | 827 | info)) |
136211a9 EZ |
828 | |
829 | (defun calc-embedded-find-vars (x) | |
830 | (cond ((Math-primp x) | |
831 | (and (eq (car-safe x) 'var) | |
832 | (not (assoc x vars-used)) | |
833 | (setq vars-used (cons (list x) vars-used)))) | |
834 | ((eq (car x) 'calcFunc-evalto) | |
835 | (calc-embedded-find-vars (nth 1 x))) | |
836 | ((eq (car x) 'calcFunc-assign) | |
837 | (calc-embedded-find-vars (nth 2 x))) | |
838 | (t | |
839 | (and (eq (car x) 'calcFunc-subscr) | |
840 | (eq (car-safe (nth 1 x)) 'var) | |
841 | (Math-primp (nth 2 x)) | |
842 | (not (assoc x vars-used)) | |
843 | (setq vars-used (cons (list x) vars-used))) | |
844 | (while (setq x (cdr x)) | |
bf77c646 | 845 | (calc-embedded-find-vars (car x)))))) |
136211a9 EZ |
846 | |
847 | ||
848 | (defun calc-embedded-evaluate-expr (x) | |
849 | (let ((vars-used (aref calc-embedded-info 10))) | |
850 | (or vars-used (calc-embedded-find-vars x)) | |
851 | (if vars-used | |
852 | (let ((active (assq (aref calc-embedded-info 0) calc-embedded-active)) | |
853 | (args nil)) | |
854 | (save-excursion | |
855 | (calc-embedded-original-buffer t) | |
856 | (or active | |
857 | (progn | |
858 | (calc-embedded-activate) | |
859 | (setq active (assq (aref calc-embedded-info 0) | |
860 | calc-embedded-active)))) | |
861 | (while vars-used | |
862 | (calc-embedded-eval-get-var (car (car vars-used)) active) | |
863 | (setq vars-used (cdr vars-used)))) | |
864 | (calc-embedded-subst x)) | |
bf77c646 | 865 | (calc-normalize (math-evaluate-expr-rec x))))) |
136211a9 EZ |
866 | |
867 | (defun calc-embedded-subst (x) | |
868 | (if (and (eq (car-safe x) 'calcFunc-evalto) (cdr x)) | |
869 | (let ((rhs (calc-embedded-subst (nth 1 x)))) | |
870 | (list 'calcFunc-evalto | |
871 | (nth 1 x) | |
872 | (if (eq (car-safe rhs) 'calcFunc-assign) (nth 2 rhs) rhs))) | |
873 | (if (and (eq (car-safe x) 'calcFunc-assign) (= (length x) 3)) | |
874 | (list 'calcFunc-assign | |
875 | (nth 1 x) | |
876 | (calc-embedded-subst (nth 2 x))) | |
bf77c646 | 877 | (calc-normalize (math-evaluate-expr-rec (math-multi-subst-rec x)))))) |
136211a9 EZ |
878 | |
879 | (defun calc-embedded-eval-get-var (var base) | |
880 | (let ((entry base) | |
881 | (point (aref calc-embedded-info 2)) | |
882 | (last nil) | |
883 | val) | |
884 | (while (and (setq entry (cdr entry)) | |
885 | (or (not (equal var (aref (car entry) 9))) | |
886 | (and (> point (aref (car entry) 3)) | |
887 | (setq last entry))))) | |
888 | (if last | |
889 | (setq entry last)) | |
890 | (if entry | |
891 | (progn | |
892 | (setq entry (car entry)) | |
893 | (if (equal (buffer-substring (aref entry 2) (aref entry 3)) | |
894 | (aref entry 6)) | |
895 | (progn | |
896 | (or (aref entry 11) | |
897 | (save-excursion | |
898 | (calc-embedded-update entry 14 t nil))) | |
899 | (setq val (aref entry 11)) | |
900 | (if (eq (car-safe val) 'calcFunc-evalto) | |
901 | (setq val (nth 2 val))) | |
902 | (if (eq (car-safe val) 'calcFunc-assign) | |
903 | (setq val (nth 2 val))) | |
904 | (setq args (cons (cons var val) args))) | |
905 | (calc-embedded-activate) | |
bf77c646 | 906 | (calc-embedded-eval-get-var var base)))))) |
136211a9 EZ |
907 | |
908 | ||
909 | (defun calc-embedded-update (info which need-eval need-display | |
910 | &optional str entry old-val) | |
911 | (let* ((prev-modes nil) | |
912 | (open-plain calc-embedded-open-plain) | |
913 | (close-plain calc-embedded-close-plain) | |
914 | (vars-used nil) | |
915 | (evalled nil) | |
916 | (val (aref info 8)) | |
917 | (old-eval (aref info 11))) | |
918 | (or old-val (setq old-val val)) | |
919 | (if (eq (car-safe val) 'calcFunc-evalto) | |
920 | (setq need-display t)) | |
921 | (unwind-protect | |
922 | (progn | |
923 | (set-buffer (aref info 1)) | |
924 | (and which | |
925 | (calc-embedded-set-modes (aref info 15) (aref info 12) | |
926 | (aref info which) | |
927 | (if need-display 'full t))) | |
928 | (if (memq (car-safe val) '(calcFunc-evalto calcFunc-assign)) | |
929 | (calc-embedded-find-vars val)) | |
930 | (if need-eval | |
931 | (let ((calc-embedded-info info)) | |
932 | (setq val (math-evaluate-expr val) | |
933 | evalled val))) | |
934 | (if (or (eq need-eval 'eval) (eq (car-safe val) 'calcFunc-evalto)) | |
935 | (aset info 8 val)) | |
936 | (aset info 9 nil) | |
937 | (aset info 10 vars-used) | |
938 | (aset info 11 nil) | |
939 | (if (or need-display (eq (car-safe val) 'calcFunc-evalto)) | |
940 | (let ((extra (if (eq calc-language 'big) 1 0))) | |
941 | (or entry (setq entry (list val 1 nil))) | |
942 | (or str (progn | |
943 | (setq str (let ((calc-line-numbering nil)) | |
944 | (math-format-stack-value entry))) | |
945 | (if (eq calc-language 'big) | |
946 | (setq str (substring str 0 -1))))) | |
947 | (and calc-show-plain | |
948 | (setq str (concat open-plain | |
949 | (math-showing-full-precision | |
950 | (math-format-flat-expr val 0)) | |
951 | close-plain | |
952 | str))) | |
953 | (save-excursion | |
954 | (calc-embedded-original-buffer t info) | |
955 | (or (equal str (aref info 6)) | |
956 | (let ((delta (- (aref info 5) (aref info 3))) | |
957 | (buffer-read-only nil)) | |
958 | (goto-char (aref info 2)) | |
959 | (delete-region (point) (aref info 3)) | |
960 | (and (> (nth 1 entry) (1+ extra)) | |
961 | (aref info 7) | |
962 | (progn | |
963 | (aset info 7 nil) | |
964 | (delete-horizontal-space) | |
965 | (insert "\n\n") | |
966 | (delete-horizontal-space) | |
967 | (backward-char 1))) | |
968 | (insert str) | |
969 | (set-marker (aref info 3) (point)) | |
970 | (set-marker (aref info 5) (+ (point) delta)) | |
971 | (aset info 6 str)))))) | |
972 | (if (eq (car-safe val) 'calcFunc-evalto) | |
973 | (progn | |
974 | (setq evalled (nth 2 val) | |
975 | val (nth 1 val)))) | |
976 | (if (eq (car-safe val) 'calcFunc-assign) | |
977 | (progn | |
978 | (aset info 9 (nth 1 val)) | |
979 | (aset info 11 (or evalled | |
980 | (let ((calc-embedded-info info)) | |
981 | (math-evaluate-expr (nth 2 val))))) | |
982 | (or (equal old-eval (aref info 11)) | |
983 | (calc-embedded-var-change (nth 1 val) (aref info 0)))) | |
984 | (if (eq (car-safe old-val) 'calcFunc-evalto) | |
985 | (setq old-val (nth 1 old-val))) | |
986 | (if (eq (car-safe old-val) 'calcFunc-assign) | |
987 | (calc-embedded-var-change (nth 1 old-val) (aref info 0))))) | |
988 | (set-buffer (aref info 1)) | |
989 | (while prev-modes | |
990 | (cond ((eq (car (car prev-modes)) 'the-language) | |
991 | (if need-display | |
992 | (calc-embedded-set-language (cdr (car prev-modes))))) | |
993 | ((eq (car (car prev-modes)) 'the-display-just) | |
994 | (if need-display | |
995 | (calc-embedded-set-justify (cdr (car prev-modes))))) | |
996 | (t | |
997 | (set (car (car prev-modes)) (cdr (car prev-modes))))) | |
bf77c646 | 998 | (setq prev-modes (cdr prev-modes)))))) |
136211a9 EZ |
999 | |
1000 | ||
1001 | ||
1002 | ||
1003 | ;;; These are hooks called by the main part of Calc. | |
1004 | ||
1005 | (defun calc-embedded-select-buffer () | |
1006 | (if (eq (current-buffer) (aref calc-embedded-info 0)) | |
1007 | (let ((info calc-embedded-info) | |
1008 | horiz vert) | |
1009 | (if (and (or (< (point) (aref info 4)) | |
1010 | (> (point) (aref info 5))) | |
1011 | (not calc-embedded-no-reselect)) | |
1012 | (let ((calc-embedded-quiet t)) | |
1013 | (message "(Switching Calc Embedded mode to new formula.)") | |
1014 | (calc-embedded nil) | |
1015 | (calc-embedded nil))) | |
1016 | (setq horiz (max (min (current-column) (- (point) (aref info 2))) 0) | |
1017 | vert (if (<= (aref info 2) (point)) | |
1018 | (- (count-lines (aref info 2) (point)) | |
1019 | (if (bolp) 0 1)) | |
1020 | 0)) | |
1021 | (set-buffer (aref info 1)) | |
1022 | (if calc-show-plain | |
1023 | (if (= vert 0) | |
1024 | (setq horiz 0) | |
1025 | (setq vert (1- vert)))) | |
1026 | (calc-cursor-stack-index 1) | |
1027 | (if calc-line-numbering | |
1028 | (setq horiz (+ horiz 4))) | |
1029 | (if (> vert 0) | |
1030 | (forward-line vert)) | |
1031 | (forward-char (min horiz | |
1032 | (- (point-max) (point))))) | |
bf77c646 | 1033 | (calc-select-buffer))) |
136211a9 EZ |
1034 | (setq calc-embedded-no-reselect nil) |
1035 | ||
1036 | (defun calc-embedded-finish-command () | |
1037 | (let ((buf (current-buffer)) | |
1038 | horiz vert) | |
1039 | (save-excursion | |
1040 | (set-buffer (aref calc-embedded-info 1)) | |
1041 | (if (> (calc-stack-size) 0) | |
1042 | (let ((pt (point)) | |
1043 | (col (current-column)) | |
1044 | (bol (bolp))) | |
1045 | (calc-cursor-stack-index 0) | |
1046 | (if (< pt (point)) | |
1047 | (progn | |
1048 | (calc-cursor-stack-index 1) | |
1049 | (if (>= pt (point)) | |
1050 | (progn | |
1051 | (setq horiz (- col (if calc-line-numbering 4 0)) | |
1052 | vert (- (count-lines (point) pt) | |
1053 | (if bol 0 1))) | |
1054 | (if calc-show-plain | |
1055 | (setq vert (max 1 (1+ vert)))))))) | |
1056 | (goto-char pt)))) | |
1057 | (if horiz | |
1058 | (progn | |
1059 | (set-buffer (aref calc-embedded-info 0)) | |
1060 | (goto-char (aref calc-embedded-info 2)) | |
1061 | (if (> vert 0) | |
1062 | (forward-line vert)) | |
1063 | (forward-char (max horiz 0)) | |
bf77c646 | 1064 | (set-buffer buf))))) |
136211a9 EZ |
1065 | |
1066 | (defun calc-embedded-stack-change () | |
1067 | (or calc-executing-macro | |
1068 | (save-excursion | |
1069 | (set-buffer (aref calc-embedded-info 1)) | |
1070 | (let* ((info calc-embedded-info) | |
1071 | (extra-line (if (eq calc-language 'big) 1 0)) | |
1072 | (the-point (point)) | |
1073 | (empty (= (calc-stack-size) 0)) | |
1074 | (entry (if empty | |
1075 | (list '(var empty var-empty) 1 nil) | |
1076 | (calc-top 1 'entry))) | |
1077 | (old-val (aref info 8)) | |
1078 | top bot str) | |
1079 | (if empty | |
1080 | (setq str "empty") | |
1081 | (save-excursion | |
1082 | (calc-cursor-stack-index 1) | |
1083 | (setq top (point)) | |
1084 | (calc-cursor-stack-index 0) | |
1085 | (setq bot (- (point) extra-line)) | |
1086 | (setq str (buffer-substring top (- bot 1)))) | |
1087 | (if calc-line-numbering | |
1088 | (let ((pos 0)) | |
1089 | (setq str (substring str 4)) | |
1090 | (while (setq pos (string-match "\n...." str pos)) | |
1091 | (setq str (concat (substring str 0 (1+ pos)) | |
1092 | (substring str (+ pos 5))) | |
1093 | pos (1+ pos)))))) | |
1094 | (calc-embedded-original-buffer t) | |
1095 | (aset info 8 (car entry)) | |
bf77c646 | 1096 | (calc-embedded-update info 13 nil t str entry old-val))))) |
136211a9 EZ |
1097 | |
1098 | (defun calc-embedded-mode-line-change () | |
1099 | (let ((str mode-line-buffer-identification)) | |
1100 | (save-excursion | |
1101 | (calc-embedded-original-buffer t) | |
1102 | (setq mode-line-buffer-identification str) | |
bf77c646 | 1103 | (set-buffer-modified-p (buffer-modified-p))))) |
136211a9 EZ |
1104 | |
1105 | (defun calc-embedded-modes-change (vars) | |
1106 | (if (eq (car vars) 'calc-language) (setq vars '(the-language))) | |
1107 | (if (eq (car vars) 'calc-display-just) (setq vars '(the-display-just))) | |
1108 | (while (and vars | |
1109 | (not (rassq (car vars) calc-embedded-mode-vars))) | |
1110 | (setq vars (cdr vars))) | |
1111 | (if (and vars calc-mode-save-mode (not (eq calc-mode-save-mode 'save))) | |
1112 | (save-excursion | |
1113 | (let* ((save-mode calc-mode-save-mode) | |
1114 | (header (if (eq save-mode 'local) | |
1115 | "calc-mode:" | |
1116 | (format "calc-%s-mode:" save-mode))) | |
1117 | (the-language (calc-embedded-language)) | |
1118 | (the-display-just (calc-embedded-justify)) | |
1119 | (values (mapcar 'symbol-value vars)) | |
1120 | (num (cond ((eq save-mode 'local) 12) | |
1121 | ((eq save-mode 'edit) 13) | |
1122 | ((eq save-mode 'perm) 14) | |
1123 | (t nil))) | |
1124 | base limit mname mlist) | |
1125 | (calc-embedded-original-buffer t) | |
1126 | (save-excursion | |
1127 | (if (eq save-mode 'global) | |
1128 | (setq base (point-max) | |
1129 | limit (point-min) | |
1130 | mlist calc-embedded-globals) | |
1131 | (goto-char (aref calc-embedded-info 4)) | |
1132 | (beginning-of-line) | |
1133 | (setq base (point) | |
1134 | limit (max (- (point) 1000) (point-min)) | |
1135 | mlist (and num (aref calc-embedded-info num))) | |
1136 | (and (re-search-backward | |
1137 | (format "\\(%s\\)[^\001]*\\(%s\\)\\|\\[calc-defaults]" | |
1138 | calc-embedded-open-formula | |
1139 | calc-embedded-close-formula) limit t) | |
1140 | (setq limit (point)))) | |
1141 | (while vars | |
1142 | (goto-char base) | |
1143 | (if (setq mname (car (rassq (car vars) | |
1144 | calc-embedded-mode-vars))) | |
1145 | (let ((buffer-read-only nil) | |
1146 | (found (assq (car vars) mlist))) | |
1147 | (if found | |
1148 | (setcdr found (car values)) | |
1149 | (setq mlist (cons (cons (car vars) (car values)) mlist)) | |
1150 | (if num | |
1151 | (aset calc-embedded-info num mlist) | |
1152 | (if (eq save-mode 'global) | |
1153 | (setq calc-embedded-globals mlist)))) | |
1154 | (if (re-search-backward | |
1155 | (format "\\[%s *%s: *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]" | |
1156 | header mname) | |
1157 | limit t) | |
1158 | (progn | |
1159 | (goto-char (match-beginning 1)) | |
1160 | (delete-region (point) (match-end 1)) | |
1161 | (insert (prin1-to-string (car values)))) | |
1162 | (goto-char base) | |
1163 | (insert-before-markers | |
1164 | calc-embedded-open-mode | |
1165 | "[" header " " mname ": " | |
1166 | (prin1-to-string (car values)) "]" | |
1167 | calc-embedded-close-mode)))) | |
1168 | (setq vars (cdr vars) | |
bf77c646 | 1169 | values (cdr values)))))))) |
136211a9 EZ |
1170 | |
1171 | (defun calc-embedded-var-change (var &optional buf) | |
1172 | (if (symbolp var) | |
1173 | (setq var (list 'var | |
1174 | (if (string-match "\\`var-.+\\'" | |
1175 | (symbol-name var)) | |
1176 | (intern (substring (symbol-name var) 4)) | |
1177 | var) | |
1178 | var))) | |
1179 | (save-excursion | |
1180 | (let ((manual (not calc-auto-recompute)) | |
1181 | (bp calc-embedded-active) | |
1182 | (first t)) | |
1183 | (if buf (setq bp (memq (assq buf bp) bp))) | |
1184 | (while bp | |
1185 | (let ((calc-embedded-no-reselect t) | |
1186 | (p (and (buffer-name (car (car bp))) | |
1187 | (cdr (car bp))))) | |
1188 | (while p | |
1189 | (if (assoc var (aref (car p) 10)) | |
1190 | (if manual | |
1191 | (if (aref (car p) 11) | |
1192 | (progn | |
1193 | (aset (car p) 11 nil) | |
1194 | (if (aref (car p) 9) | |
1195 | (calc-embedded-var-change (aref (car p) 9))))) | |
1196 | (set-buffer (aref (car p) 0)) | |
1197 | (if (equal (buffer-substring (aref (car p) 2) | |
1198 | (aref (car p) 3)) | |
1199 | (aref (car p) 6)) | |
1200 | (let ((calc-embedded-info nil)) | |
1201 | (or calc-embedded-quiet | |
1202 | (message "Recomputing...")) | |
1203 | (setq first nil) | |
1204 | (calc-wrapper | |
1205 | (set-buffer (aref (car p) 0)) | |
1206 | (calc-embedded-update (car p) 14 t nil))) | |
1207 | (setcdr (car bp) (delq (car p) (cdr (car bp)))) | |
1208 | (message | |
1209 | "(Tried to recompute but formula was changed or missing.)")))) | |
1210 | (setq p (cdr p)))) | |
1211 | (setq bp (if buf nil (cdr bp)))) | |
bf77c646 | 1212 | (or first calc-embedded-quiet (message ""))))) |
136211a9 | 1213 | |
bf77c646 | 1214 | ;;; calc-embed.el ends here |
136211a9 EZ |
1215 | |
1216 | ||
1217 |