Document reserved keys
[emacs.git] / lisp / calc / calc-embed.el
blob6573c07bff02f7d627dfed30d509d591021e8ddb
1 ;;; calc-embed.el --- embed Calc in a buffer
3 ;; Copyright (C) 1990-1993, 2001-2018 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;;; Code:
26 ;; This file is autoloaded from calc-ext.el.
28 (require 'calc-ext)
29 (require 'calc-macs)
30 (require 'cl-lib)
32 ;; Declare functions which are defined elsewhere.
33 (declare-function thing-at-point-looking-at "thingatpt"
34 (regexp &optional distance))
37 (defun calc-show-plain (n)
38 (interactive "P")
39 (calc-wrapper
40 (calc-set-command-flag 'renum-stack)
41 (message (if (calc-change-mode 'calc-show-plain n nil t)
42 "Including \"plain\" formulas in Calc Embedded mode"
43 "Omitting \"plain\" formulas in Calc Embedded mode"))))
46 (defvar calc-embedded-modes nil)
47 (defvar calc-embedded-globals nil)
48 (defvar calc-embedded-active nil)
49 (defvar calc-embedded-all-active nil)
50 (make-variable-buffer-local 'calc-embedded-all-active)
51 (defvar calc-embedded-some-active nil)
52 (make-variable-buffer-local 'calc-embedded-some-active)
54 ;; The following variables are customizable and defined in calc.el.
55 (defvar calc-embedded-announce-formula)
56 (defvar calc-embedded-open-formula)
57 (defvar calc-embedded-close-formula)
58 (defvar calc-embedded-open-plain)
59 (defvar calc-embedded-close-plain)
60 (defvar calc-embedded-open-new-formula)
61 (defvar calc-embedded-close-new-formula)
62 (defvar calc-embedded-open-mode)
63 (defvar calc-embedded-close-mode)
64 (defvar calc-embedded-word-regexp)
66 (defconst calc-embedded-mode-vars '(("twos-complement" . calc-twos-complement-mode)
67 ("precision" . calc-internal-prec)
68 ("word-size" . calc-word-size)
69 ("angles" . calc-angle-mode)
70 ("symbolic" . calc-symbolic-mode)
71 ("matrix" . calc-matrix-mode)
72 ("fractions" . calc-prefer-frac)
73 ("complex" . calc-complex-mode)
74 ("simplify" . calc-simplify-mode)
75 ("language" . the-language)
76 ("plain" . calc-show-plain)
77 ("break" . calc-line-breaking)
78 ("justify" . the-display-just)
79 ("left-label" . calc-left-label)
80 ("right-label" . calc-right-label)
81 ("radix" . calc-number-radix)
82 ("leading-zeros" . calc-leading-zeros)
83 ("grouping" . calc-group-digits)
84 ("group-char" . calc-group-char)
85 ("point-char" . calc-point-char)
86 ("frac-format" . calc-frac-format)
87 ("float-format" . calc-float-format)
88 ("complex-format" . calc-complex-format)
89 ("hms-format" . calc-hms-format)
90 ("date-format" . calc-date-format)
91 ("matrix-justify" . calc-matrix-just)
92 ("full-vectors" . calc-full-vectors)
93 ("break-vectors" . calc-break-vectors)
94 ("vector-commas" . calc-vector-commas)
95 ("vector-brackets" . calc-vector-brackets)
96 ("matrix-brackets" . calc-matrix-brackets)
97 ("strings" . calc-display-strings)
101 ;; Format of calc-embedded-info vector:
102 ;; 0 Editing buffer.
103 ;; 1 Calculator buffer.
104 ;; 2 Top of current formula (marker).
105 ;; 3 Bottom of current formula (marker).
106 ;; 4 Top of current formula's delimiters (marker).
107 ;; 5 Bottom of current formula's delimiters (marker).
108 ;; 6 String representation of current formula.
109 ;; 7 Non-nil if formula is embedded within a single line.
110 ;; 8 Internal representation of current formula.
111 ;; 9 Variable assigned by this formula, or nil.
112 ;; 10 List of variables upon which this formula depends.
113 ;; 11 Evaluated value of the formula, or nil.
114 ;; 12 Mode settings for current formula.
115 ;; 13 Local mode settings for current formula.
116 ;; 14 Permanent mode settings for current formula.
117 ;; 15 Global mode settings for editing buffer.
120 ;; calc-embedded-active is an a-list keyed on buffers; each cdr is a
121 ;; sorted list of calc-embedded-infos in that buffer. We do this
122 ;; rather than using buffer-local variables because the latter are
123 ;; thrown away when a buffer changes major modes.
125 (defvar calc-embedded-original-modes nil
126 "The mode settings for Calc buffer when put in embedded mode.")
128 (defun calc-embedded-save-original-modes ()
129 "Save the current Calc modes when entering embedded mode."
130 (let ((calcbuf (save-excursion
131 (calc-create-buffer)
132 (current-buffer)))
133 lang modes)
134 (if calcbuf
135 (with-current-buffer calcbuf
136 (setq lang
137 (cons calc-language calc-language-option))
138 (setq modes
139 (list (cons 'calc-display-just
140 calc-display-just)
141 (cons 'calc-display-origin
142 calc-display-origin)))
143 (let ((v calc-embedded-mode-vars))
144 (while v
145 (let ((var (cdr (car v))))
146 (unless (memq var '(the-language the-display-just))
147 (setq modes
148 (cons (cons var (symbol-value var))
149 modes))))
150 (setq v (cdr v))))
151 (setq calc-embedded-original-modes (cons lang modes)))
152 (setq calc-embedded-original-modes nil))))
154 (defun calc-embedded-preserve-modes ()
155 "Preserve the current modes when leaving embedded mode."
156 (interactive)
157 (if calc-embedded-info
158 (progn
159 (calc-embedded-save-original-modes)
160 (message "Current modes will be preserved when leaving embedded mode."))
161 (message "Not in embedded mode.")))
163 (defun calc-embedded-restore-original-modes (calcbuf)
164 "Restore the original Calc modes when leaving embedded mode."
165 (let ((changed nil)
166 (lang (car calc-embedded-original-modes))
167 (modes (cdr calc-embedded-original-modes)))
168 (if (and calcbuf calc-embedded-original-modes)
169 (with-current-buffer calcbuf
170 (unless (and
171 (equal calc-language (car lang))
172 (equal calc-language-option (cdr lang)))
173 (calc-set-language (car lang) (cdr lang))
174 (setq changed t))
175 (while modes
176 (let ((mode (car modes)))
177 (unless (equal (symbol-value (car mode)) (cdr mode))
178 (set (car mode) (cdr mode))
179 (setq changed t)))
180 (setq modes (cdr modes)))
181 (when changed
182 (calc-refresh)
183 (calc-set-mode-line))))
184 (setq calc-embedded-original-modes nil)))
186 ;; The variables calc-embed-outer-top, calc-embed-outer-bot,
187 ;; calc-embed-top and calc-embed-bot are
188 ;; local to calc-do-embedded, calc-embedded-mark-formula,
189 ;; calc-embedded-duplicate, calc-embedded-new-formula and
190 ;; calc-embedded-make-info, but are used by calc-embedded-find-bounds,
191 ;; which is called (directly or indirectly) by the above functions.
192 (defvar calc-embed-outer-top)
193 (defvar calc-embed-outer-bot)
194 (defvar calc-embed-top)
195 (defvar calc-embed-bot)
197 ;; The variable calc-embed-arg is local to calc-do-embedded,
198 ;; calc-embedded-update-formula, calc-embedded-edit and
199 ;; calc-do-embedded-activate, but is used by
200 ;; calc-embedded-make-info, which is called by the above
201 ;; functions.
202 (defvar calc-embed-arg)
204 (defvar calc-embedded-quiet nil)
206 (defvar calc-embedded-firsttime)
207 (defvar calc-embedded-firsttime-buf)
208 (defvar calc-embedded-firsttime-formula)
210 ;; The following is to take care of any minor modes which override
211 ;; a Calc command.
212 (defvar calc-override-minor-modes-map
213 (make-sparse-keymap)
214 "A list of keybindings that might be overwritten by minor modes.")
216 ;; Add any keys that might be overwritten here.
217 (define-key calc-override-minor-modes-map "`" 'calc-edit)
219 (defvar calc-override-minor-modes
220 (cons t calc-override-minor-modes-map))
222 (defun calc-do-embedded (calc-embed-arg end obeg oend)
223 (if calc-embedded-info
225 ;; Turn embedded mode off or switch to a new buffer.
226 (cond ((eq (current-buffer) (aref calc-embedded-info 1))
227 (let ((calcbuf (current-buffer))
228 (buf (aref calc-embedded-info 0)))
229 (calc-embedded-original-buffer t)
230 (calc-embedded nil)
231 (switch-to-buffer calcbuf)))
233 ((eq (current-buffer) (aref calc-embedded-info 0))
234 (let* ((info calc-embedded-info)
235 (mode calc-embedded-modes)
236 (calcbuf (aref calc-embedded-info 1)))
237 (with-current-buffer (aref info 1)
238 (if (and (> (calc-stack-size) 0)
239 (equal (calc-top 1 'full) (aref info 8)))
240 (let ((calc-no-refresh-evaltos t))
241 (if (calc-top 1 'sel)
242 (calc-unselect 1))
243 (calc-embedded-set-modes
244 (aref info 15) (aref info 12) (aref info 14))
245 (let ((calc-embedded-info nil))
246 (calc-wrapper (calc-pop-stack))))
247 (calc-set-mode-line)))
248 (setq calc-embedded-info nil
249 mode-line-buffer-identification (car mode)
250 truncate-lines (nth 2 mode)
251 buffer-read-only nil)
252 (use-local-map (nth 1 mode))
253 (setq minor-mode-overriding-map-alist
254 (remq calc-override-minor-modes minor-mode-overriding-map-alist))
255 (set-buffer-modified-p (buffer-modified-p))
256 (calc-embedded-restore-original-modes calcbuf)
257 (or calc-embedded-quiet
258 (message "Back to %s mode" (format-mode-line mode-name)))))
261 (if (buffer-name (aref calc-embedded-info 0))
262 (with-current-buffer (aref calc-embedded-info 0)
263 (or (y-or-n-p (format "Cancel Calc Embedded mode in buffer %s? "
264 (buffer-name)))
265 (keyboard-quit))
266 (calc-embedded nil)))
267 (calc-embedded calc-embed-arg end obeg oend)))
269 ;; Turn embedded mode on.
270 (calc-plain-buffer-only)
271 (let ((modes (list mode-line-buffer-identification
272 (current-local-map)
273 truncate-lines))
274 (calc-embedded-firsttime (not calc-embedded-active))
275 (calc-embedded-firsttime-buf nil)
276 (calc-embedded-firsttime-formula nil)
277 calc-embed-top calc-embed-bot calc-embed-outer-top calc-embed-outer-bot
278 info chg ident)
279 (barf-if-buffer-read-only)
280 (calc-embedded-save-original-modes)
281 (or calc-embedded-globals
282 (calc-find-globals))
283 (setq info
284 (calc-embedded-make-info (point) nil t calc-embed-arg end obeg oend))
285 (if (eq (car-safe (aref info 8)) 'error)
286 (progn
287 (setq calc-embedded-original-modes nil)
288 (goto-char (nth 1 (aref info 8)))
289 (error (nth 2 (aref info 8)))))
290 (let ((mode-line-buffer-identification mode-line-buffer-identification)
291 (calc-embedded-info info)
292 (calc-embedded-no-reselect t))
293 (calc-wrapper
294 (let* ((okay nil)
295 (calc-no-refresh-evaltos t))
296 (if (aref info 8)
297 (progn
298 (calc-push (calc-normalize (aref info 8)))
299 (setq chg (calc-embedded-set-modes
300 (aref info 15) (aref info 12) (aref info 13))))
301 (setq chg (calc-embedded-set-modes
302 (aref info 15) (aref info 12) (aref info 13)))
303 (calc-alg-entry)))
304 (setq calc-undo-list nil
305 calc-redo-list nil
306 ident mode-line-buffer-identification)))
307 (setq calc-embedded-info info
308 calc-embedded-modes modes
309 mode-line-buffer-identification ident
310 truncate-lines t
311 buffer-read-only t)
312 (set-buffer-modified-p (buffer-modified-p))
313 (use-local-map calc-mode-map)
314 (setq minor-mode-overriding-map-alist
315 (cons calc-override-minor-modes
316 minor-mode-overriding-map-alist))
317 (setq calc-no-refresh-evaltos nil)
318 (and chg calc-any-evaltos (calc-wrapper (calc-refresh-evaltos)))
319 (let (str)
320 (save-excursion
321 (calc-select-buffer)
322 (setq str mode-line-buffer-identification))
323 (unless (equal str mode-line-buffer-identification)
324 (setq mode-line-buffer-identification str)
325 (set-buffer-modified-p (buffer-modified-p))))
326 (if calc-embedded-firsttime
327 (run-hooks 'calc-embedded-mode-hook))
328 (if calc-embedded-firsttime-buf
329 (run-hooks 'calc-embedded-new-buffer-hook))
330 (if calc-embedded-firsttime-formula
331 (run-hooks 'calc-embedded-new-formula-hook))
332 (or (eq calc-embedded-quiet t)
333 (message (concat
334 "Embedded Calc mode enabled; "
335 (if calc-embedded-quiet
336 "Type `C-x * x'"
337 "Give this command again")
338 " to return to normal")))))
339 (scroll-down 0)) ; fix a bug which occurs when truncate-lines is changed.
342 (defun calc-embedded-select (arg)
343 (interactive "P")
344 (calc-embedded arg)
345 (and calc-embedded-info
346 (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-evalto)
347 (calc-select-part 1))
348 (and calc-embedded-info
349 (or (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-assign)
350 (and (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-evalto)
351 (eq (car-safe (nth 1 (aref calc-embedded-info 8)))
352 'calcFunc-assign)))
353 (calc-select-part 2)))
356 (defun calc-embedded-update-formula (calc-embed-arg)
357 (interactive "P")
358 (if calc-embed-arg
359 (let ((entry (assq (current-buffer) calc-embedded-active)))
360 (while (setq entry (cdr entry))
361 (and (eq (car-safe (aref (car entry) 8)) 'calcFunc-evalto)
362 (or (not (consp calc-embed-arg))
363 (and (<= (aref (car entry) 2) (region-beginning))
364 (>= (aref (car entry) 3) (region-end))))
365 (save-excursion
366 (calc-embedded-update (car entry) 14 t t)))))
367 (if (and calc-embedded-info
368 (eq (current-buffer) (aref calc-embedded-info 0))
369 (>= (point) (aref calc-embedded-info 4))
370 (<= (point) (aref calc-embedded-info 5)))
371 (calc-evaluate 1)
372 (let* ((opt (point))
373 (info (calc-embedded-make-info (point) nil t))
374 (pt (- opt (aref info 4))))
375 (or (eq (car-safe (aref info 8)) 'error)
376 (progn
377 (save-excursion
378 (calc-embedded-update info 14 'eval t))
379 (goto-char (+ (aref info 4) pt))))))))
382 (defun calc-embedded-edit (calc-embed-arg)
383 (interactive "P")
384 (let ((info (calc-embedded-make-info (point) nil t calc-embed-arg))
385 str)
386 (if (eq (car-safe (aref info 8)) 'error)
387 (progn
388 (goto-char (nth 1 (aref info 8)))
389 (error (nth 2 (aref info 8)))))
390 (calc-wrapper
391 (setq str (math-showing-full-precision
392 (math-format-nice-expr (aref info 8) (frame-width))))
393 (calc-edit-mode (list 'calc-embedded-finish-edit info))
394 (insert str "\n")))
395 (calc-show-edit-buffer))
397 (defvar calc-original-buffer)
398 (defvar calc-edit-top)
399 (defun calc-embedded-finish-edit (info)
400 (let ((buf (current-buffer))
401 (str (buffer-substring calc-edit-top (point-max)))
402 (start (point))
403 pos)
404 (switch-to-buffer calc-original-buffer)
405 (let ((val (with-current-buffer (aref info 1)
406 (let ((calc-language nil)
407 (math-expr-opers (math-standard-ops)))
408 (math-read-expr str)))))
409 (if (eq (car-safe val) 'error)
410 (progn
411 (switch-to-buffer buf)
412 (goto-char (+ start (nth 1 val)))
413 (error (nth 2 val))))
414 (calc-embedded-original-buffer t info)
415 (aset info 8 val)
416 (calc-embedded-update info 14 t t))))
418 ;;;###autoload
419 (defun calc-do-embedded-activate (calc-embed-arg cbuf)
420 (calc-plain-buffer-only)
421 (if calc-embed-arg
422 (calc-embedded-forget))
423 (calc-find-globals)
424 (if (< (prefix-numeric-value calc-embed-arg) 0)
425 (message "Deactivating %s for Calc Embedded mode" (buffer-name))
426 (message "Activating %s for Calc Embedded mode..." (buffer-name))
427 (save-excursion
428 (let* ((active (assq (current-buffer) calc-embedded-active))
429 (info active)
430 (pat " := \\| \\\\gets \\| => \\| \\\\evalto "))
431 (if calc-embedded-announce-formula
432 (setq pat (format "%s\\|\\(%s\\)"
433 pat calc-embedded-announce-formula)))
434 (while (setq info (cdr info))
435 (or (equal (buffer-substring (aref (car info) 2) (aref (car info) 3))
436 (aref (car info) 6))
437 (setcdr active (delq (car info) (cdr active)))))
438 (goto-char (point-min))
439 (while (re-search-forward pat nil t)
440 ;;; (if (looking-at calc-embedded-open-formula)
441 ;;; (goto-char (match-end 1)))
442 (setq info (calc-embedded-make-info (point) cbuf nil))
443 (or (eq (car-safe (aref info 8)) 'error)
444 (goto-char (aref info 5))))))
445 (message "Activating %s for Calc Embedded mode...done" (buffer-name)))
446 (calc-embedded-active-state t))
448 (defun calc-plain-buffer-only ()
449 (if (memq major-mode '(calc-mode calc-trail-mode calc-edit-mode))
450 (error "This command should be used in a normal editing buffer")))
452 (defun calc-embedded-active-state (state)
453 (or (assq 'calc-embedded-all-active minor-mode-alist)
454 (setq minor-mode-alist
455 (cons '(calc-embedded-all-active " Active")
456 (cons '(calc-embedded-some-active " ~Active")
457 minor-mode-alist))))
458 (let ((active (assq (current-buffer) calc-embedded-active)))
459 (or (cdr active)
460 (setq state nil)))
461 (and (eq state 'more) calc-embedded-all-active (setq state t))
462 (setq calc-embedded-all-active (eq state t)
463 calc-embedded-some-active (not (memq state '(nil t))))
464 (set-buffer-modified-p (buffer-modified-p)))
467 (defun calc-embedded-original-buffer (switch &optional info)
468 (or info (setq info calc-embedded-info))
469 (or (buffer-name (aref info 0))
470 (progn
471 (error "Calc embedded mode: Original buffer has been killed")))
472 (if switch
473 (set-buffer (aref info 0))))
475 (defun calc-embedded-word ()
476 (interactive)
477 (calc-embedded '(t)))
479 (defun calc-embedded-mark-formula (&optional body-only)
480 "Put point at the beginning of this Calc formula, mark at the end.
481 This normally marks the whole formula, including surrounding delimiters.
482 With any prefix argument, marks only the formula itself."
483 (interactive "P")
484 (and (eq major-mode 'calc-mode)
485 (error "This command should be used in a normal editing buffer"))
486 (let (calc-embed-top calc-embed-bot calc-embed-outer-top calc-embed-outer-bot)
487 (save-excursion
488 (calc-embedded-find-bounds body-only))
489 (push-mark (if body-only calc-embed-bot calc-embed-outer-bot) t)
490 (goto-char (if body-only calc-embed-top calc-embed-outer-top))))
492 (defun calc-embedded-find-bounds (&optional plain)
493 ;; (while (and (bolp) (eq (following-char) ?\n))
494 ;; (forward-char 1))
495 (and (eolp) (bolp) (not (eq (char-after (- (point) 2)) ?\n))
496 (forward-char -1))
497 (let ((home (point)))
498 (or (and (looking-at calc-embedded-open-formula)
499 (not (looking-at calc-embedded-close-formula)))
500 (re-search-backward calc-embedded-open-formula nil t)
501 (error "Can't find start of formula"))
502 (and (eq (preceding-char) ?\$) ; backward search for \$\$? won't back
503 (eq (following-char) ?\$) ; up over a second $, so do it by hand.
504 (forward-char -1))
505 (setq calc-embed-outer-top (point))
506 (goto-char (match-end 0))
507 (if (looking-at "[ \t]*$")
508 (end-of-line))
509 (if (eq (following-char) ?\n)
510 (forward-char 1))
511 (or (bolp)
512 (while (eq (following-char) ?\ )
513 (forward-char 1)))
514 (or (eq plain 'plain)
515 (if (looking-at (regexp-quote calc-embedded-open-plain))
516 (progn
517 (goto-char (match-end 0))
518 (search-forward calc-embedded-close-plain))))
519 (setq calc-embed-top (point))
520 (or (re-search-forward calc-embedded-close-formula nil t)
521 (error "Can't find end of formula"))
522 (if (< (point) home)
523 (error "Not inside a formula"))
524 (and (eq (following-char) ?\n) (not (bolp))
525 (forward-char 1))
526 (setq calc-embed-outer-bot (point))
527 (goto-char (match-beginning 0))
528 (if (eq (preceding-char) ?\n)
529 (backward-char 1))
530 (or (eolp)
531 (while (eq (preceding-char) ?\ )
532 (backward-char 1)))
533 (setq calc-embed-bot (point))))
535 (defun calc-embedded-kill-formula ()
536 "Kill the formula surrounding point.
537 If Calc Embedded mode was active, this deactivates it.
538 The formula (including its surrounding delimiters) is saved in the kill ring.
539 The command \\[yank] can retrieve it from there."
540 (interactive)
541 (and calc-embedded-info
542 (calc-embedded nil))
543 (calc-embedded-mark-formula)
544 (kill-region (point) (mark))
545 (pop-mark))
547 (defun calc-embedded-copy-formula-as-kill ()
548 "Save the formula surrounding point as if killed, but don't kill it."
549 (interactive)
550 (save-excursion
551 (calc-embedded-mark-formula)
552 (copy-region-as-kill (point) (mark))
553 (pop-mark)))
555 (defun calc-embedded-duplicate ()
556 (interactive)
557 (let ((already calc-embedded-info)
558 calc-embed-top calc-embed-bot calc-embed-outer-top calc-embed-outer-bot new-top)
559 (if calc-embedded-info
560 (progn
561 (setq calc-embed-top (+ (aref calc-embedded-info 2))
562 calc-embed-bot (+ (aref calc-embedded-info 3))
563 calc-embed-outer-top (+ (aref calc-embedded-info 4))
564 calc-embed-outer-bot (+ (aref calc-embedded-info 5)))
565 (calc-embedded nil))
566 (calc-embedded-find-bounds))
567 (goto-char calc-embed-outer-bot)
568 (insert "\n")
569 (setq new-top (point))
570 (insert-buffer-substring (current-buffer)
571 calc-embed-outer-top calc-embed-outer-bot)
572 (goto-char (+ new-top (- calc-embed-top calc-embed-outer-top)))
573 (let ((calc-embedded-quiet (if already t 'x)))
574 (calc-embedded (+ new-top (- calc-embed-top calc-embed-outer-top))
575 (+ new-top (- calc-embed-bot calc-embed-outer-top))
576 new-top
577 (+ new-top (- calc-embed-outer-bot calc-embed-outer-top))))))
579 (defun calc-embedded-next (arg)
580 (interactive "P")
581 (setq arg (prefix-numeric-value arg))
582 (let* ((active (cdr (assq (current-buffer) calc-embedded-active)))
583 (p active)
584 (num (length active)))
585 (or active
586 (error "No active formulas in buffer"))
587 (cond ((= arg 0))
588 ((= arg -1)
589 (if (<= (point) (aref (car active) 3))
590 (goto-char (aref (nth (1- num) active) 2))
591 (while (and (cdr p)
592 (> (point) (aref (nth 1 p) 3)))
593 (setq p (cdr p)))
594 (goto-char (aref (car p) 2))))
595 ((< arg -1)
596 (calc-embedded-next -1)
597 (calc-embedded-next (+ (* num 1000) arg 1)))
599 (setq arg (1+ (% (1- arg) num)))
600 (while (and p (>= (point) (aref (car p) 2)))
601 (setq p (cdr p)))
602 (while (> (setq arg (1- arg)) 0)
603 (setq p (if p (cdr p) (cdr active))))
604 (goto-char (aref (car (or p active)) 2))))))
606 (defun calc-embedded-previous (arg)
607 (interactive "p")
608 (calc-embedded-next (- (prefix-numeric-value arg))))
610 (defun calc-embedded-new-formula ()
611 (interactive)
612 (and (eq major-mode 'calc-mode)
613 (error "This command should be used in a normal editing buffer"))
614 (if calc-embedded-info
615 (calc-embedded nil))
616 (let (calc-embed-top calc-embed-bot calc-embed-outer-top calc-embed-outer-bot)
617 (if (and (eq (preceding-char) ?\n)
618 (string-match "\\`\n" calc-embedded-open-new-formula))
619 (progn
620 (setq calc-embed-outer-top (1- (point)))
621 (forward-char -1)
622 (insert (substring calc-embedded-open-new-formula 1)))
623 (setq calc-embed-outer-top (point))
624 (insert calc-embedded-open-new-formula))
625 (setq calc-embed-top (point))
626 (insert " ")
627 (setq calc-embed-bot (point))
628 (insert calc-embedded-close-new-formula)
629 (if (and (eq (following-char) ?\n)
630 (string-match "\n\\'" calc-embedded-close-new-formula))
631 (delete-char 1))
632 (setq calc-embed-outer-bot (point))
633 (goto-char calc-embed-top)
634 (let ((calc-embedded-quiet 'x))
635 (calc-embedded calc-embed-top calc-embed-bot calc-embed-outer-top calc-embed-outer-bot))))
637 (defun calc-embedded-forget ()
638 (interactive)
639 (setq calc-embedded-active (delq (assq (current-buffer) calc-embedded-active)
640 calc-embedded-active))
641 (calc-embedded-active-state nil))
643 ;; The variables calc-embed-prev-modes is local to calc-embedded-update,
644 ;; but is used by calc-embedded-set-modes.
645 (defvar calc-embed-prev-modes)
647 (defun calc-embedded-set-modes (gmodes modes local-modes &optional temp)
648 (let ((the-language (calc-embedded-language))
649 (the-display-just (calc-embedded-justify))
650 (v gmodes)
651 (changed nil)
652 found value)
653 (while v
654 (or (symbolp (car v))
655 (and (setq found (assq (car (car v)) modes))
656 (not (eq (cdr found) 'default)))
657 (and (setq found (assq (car (car v)) local-modes))
658 (not (eq (cdr found) 'default)))
659 (progn
660 (if (eq (setq value (cdr (car v))) 'default)
661 (setq value (list (nth 1 (assq (car (car v)) calc-mode-var-list)))))
662 (equal (symbol-value (car (car v))) value))
663 (progn
664 (setq changed t)
665 (if temp (setq calc-embed-prev-modes
666 (cons (cons (car (car v))
667 (symbol-value (car (car v))))
668 calc-embed-prev-modes)))
669 (set (car (car v)) value)))
670 (setq v (cdr v)))
671 (setq v modes)
672 (while v
673 (or (and (setq found (assq (car (car v)) local-modes))
674 (not (eq (cdr found) 'default)))
675 (eq (setq value (cdr (car v))) 'default)
676 (equal (symbol-value (car (car v))) value)
677 (progn
678 (setq changed t)
679 (if temp (setq calc-embed-prev-modes (cons (cons (car (car v))
680 (symbol-value (car (car v))))
681 calc-embed-prev-modes)))
682 (set (car (car v)) value)))
683 (setq v (cdr v)))
684 (setq v local-modes)
685 (while v
686 (or (eq (setq value (cdr (car v))) 'default)
687 (equal (symbol-value (car (car v))) value)
688 (progn
689 (setq changed t)
690 (if temp (setq calc-embed-prev-modes (cons (cons (car (car v))
691 (symbol-value (car (car v))))
692 calc-embed-prev-modes)))
693 (set (car (car v)) value)))
694 (setq v (cdr v)))
695 (and changed (not (eq temp t))
696 (progn
697 (calc-embedded-set-justify the-display-just)
698 (calc-embedded-set-language the-language)))
699 (and changed (not temp)
700 (progn
701 (setq calc-full-float-format (list (if (eq (car calc-float-format)
702 'fix)
703 'float
704 (car calc-float-format))
706 (calc-refresh)))
707 changed))
709 (defun calc-embedded-language ()
710 (if calc-language-option
711 (list calc-language calc-language-option)
712 calc-language))
714 (defun calc-embedded-set-language (lang)
715 (let ((option nil))
716 (if (consp lang)
717 (setq option (nth 1 lang)
718 lang (car lang)))
719 (or (and (eq lang calc-language)
720 (equal option calc-language-option))
721 (calc-set-language lang option t))))
723 (defun calc-embedded-justify ()
724 (if calc-display-origin
725 (list calc-display-just calc-display-origin)
726 calc-display-just))
728 (defun calc-embedded-set-justify (just)
729 (if (consp just)
730 (setq calc-display-origin (nth 1 just)
731 calc-display-just (car just))
732 (setq calc-display-just just
733 calc-display-origin nil)))
736 (defun calc-find-globals ()
737 (interactive)
738 (and (eq major-mode 'calc-mode)
739 (error "This command should be used in a normal editing buffer"))
740 (make-local-variable 'calc-embedded-globals)
741 (let ((case-fold-search nil)
742 (modes nil)
743 (save-pt (point))
744 found value)
745 (goto-char (point-min))
746 (while (re-search-forward "\\[calc-global-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)\\]" nil t)
747 (and (setq found (assoc (buffer-substring (match-beginning 1)
748 (match-end 1))
749 calc-embedded-mode-vars))
750 (or (assq (cdr found) modes)
751 (setq modes (cons (cons (cdr found)
752 (car (read-from-string
753 (buffer-substring
754 (match-beginning 2)
755 (match-end 2)))))
756 modes)))))
757 (setq calc-embedded-globals (cons t modes))
758 (goto-char save-pt)))
760 (defun calc-embedded-find-modes ()
761 (let ((case-fold-search nil)
762 (save-pt (point))
763 (no-defaults t)
764 (modes nil)
765 (emodes nil)
766 (pmodes nil)
767 found value)
768 (while (and no-defaults (search-backward "[calc-" nil t))
769 (forward-char 6)
770 (or (and (looking-at "mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
771 (setq found (assoc (buffer-substring (match-beginning 1)
772 (match-end 1))
773 calc-embedded-mode-vars))
774 (or (assq (cdr found) modes)
775 (setq modes (cons (cons (cdr found)
776 (car (read-from-string
777 (buffer-substring
778 (match-beginning 2)
779 (match-end 2)))))
780 modes))))
781 (and (looking-at "perm-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
782 (setq found (assoc (buffer-substring (match-beginning 1)
783 (match-end 1))
784 calc-embedded-mode-vars))
785 (or (assq (cdr found) pmodes)
786 (setq pmodes (cons (cons (cdr found)
787 (car (read-from-string
788 (buffer-substring
789 (match-beginning 2)
790 (match-end 2)))))
791 pmodes))))
792 (and (looking-at "edit-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
793 (setq found (assoc (buffer-substring (match-beginning 1)
794 (match-end 1))
795 calc-embedded-mode-vars))
796 (or (assq (cdr found) emodes)
797 (setq emodes (cons (cons (cdr found)
798 (car (read-from-string
799 (buffer-substring
800 (match-beginning 2)
801 (match-end 2)))))
802 emodes))))
803 (and (looking-at "defaults]")
804 (setq no-defaults nil)))
805 (backward-char 6))
806 (goto-char save-pt)
807 (unless (assq 'the-language modes)
808 (let ((lang (cl-assoc-if #'derived-mode-p calc-language-alist)))
809 (if lang
810 (setq modes (cons (cons 'the-language (cdr lang))
811 modes)))))
812 (list modes emodes pmodes)))
814 ;; The variable calc-embed-vars-used is local to calc-embedded-make-info,
815 ;; calc-embedded-evaluate-expr and calc-embedded-update, but is
816 ;; used by calc-embedded-find-vars, which is called by the above functions.
817 (defvar calc-embed-vars-used)
819 (defun calc-embedded-make-info (point cbuf fresh &optional
820 calc-embed-top calc-embed-bot
821 calc-embed-outer-top calc-embed-outer-bot)
822 (let* ((bufentry (assq (current-buffer) calc-embedded-active))
823 (found bufentry)
824 (force (and fresh calc-embed-top (null (equal calc-embed-top '(t)))))
825 (fixed calc-embed-top)
826 (new-info nil)
827 info str)
828 (or found
829 (and
830 (setq found (list (current-buffer))
831 calc-embedded-active (cons found calc-embedded-active)
832 calc-embedded-firsttime-buf t)
833 (let ((newann (cl-assoc-if #'derived-mode-p
834 calc-embedded-announce-formula-alist))
835 (newform (cl-assoc-if #'derived-mode-p
836 calc-embedded-open-close-formula-alist))
837 (newword (cl-assoc-if #'derived-mode-p
838 calc-embedded-word-regexp-alist))
839 (newplain (cl-assoc-if #'derived-mode-p
840 calc-embedded-open-close-plain-alist))
841 (newnewform
842 (cl-assoc-if #'derived-mode-p
843 calc-embedded-open-close-new-formula-alist))
844 (newmode (cl-assoc-if #'derived-mode-p
845 calc-embedded-open-close-mode-alist)))
846 (when newann
847 (make-local-variable 'calc-embedded-announce-formula)
848 (setq calc-embedded-announce-formula (cdr newann)))
849 (when newform
850 (make-local-variable 'calc-embedded-open-formula)
851 (make-local-variable 'calc-embedded-close-formula)
852 (setq calc-embedded-open-formula (nth 0 (cdr newform)))
853 (setq calc-embedded-close-formula (nth 1 (cdr newform))))
854 (when newword
855 (make-local-variable 'calc-embedded-word-regexp)
856 (setq calc-embedded-word-regexp (nth 1 newword)))
857 (when newplain
858 (make-local-variable 'calc-embedded-open-plain)
859 (make-local-variable 'calc-embedded-close-plain)
860 (setq calc-embedded-open-plain (nth 0 (cdr newplain)))
861 (setq calc-embedded-close-plain (nth 1 (cdr newplain))))
862 (when newnewform
863 (make-local-variable 'calc-embedded-open-new-formula)
864 (make-local-variable 'calc-embedded-close-new-formula)
865 (setq calc-embedded-open-new-formula (nth 0 (cdr newnewform)))
866 (setq calc-embedded-close-new-formula (nth 1 (cdr newnewform))))
867 (when newmode
868 (make-local-variable 'calc-embedded-open-mode)
869 (make-local-variable 'calc-embedded-close-mode)
870 (setq calc-embedded-open-mode (nth 0 (cdr newmode)))
871 (setq calc-embedded-close-mode (nth 1 (cdr newmode)))))))
872 (while (and (cdr found)
873 (> point (aref (car (cdr found)) 3)))
874 (setq found (cdr found)))
875 (if (and (cdr found)
876 (>= point (aref (nth 1 found) 2)))
877 (setq info (nth 1 found))
878 (setq calc-embedded-firsttime-formula t)
879 (setq info (make-vector 16 nil)
880 new-info t
881 fresh t)
882 (aset info 0 (current-buffer))
883 (aset info 1 (or cbuf (save-excursion
884 (calc-create-buffer)
885 (current-buffer)))))
886 (if (and
887 (or (integerp calc-embed-top) (equal calc-embed-top '(4)))
888 (not calc-embed-bot))
889 ; started with a user-supplied argument
890 (progn
891 (if (equal calc-embed-top '(4))
892 (progn
893 (aset info 2 (copy-marker (line-beginning-position)))
894 (aset info 3 (copy-marker (line-end-position))))
895 (if (= (setq calc-embed-arg (prefix-numeric-value calc-embed-arg)) 0)
896 (progn
897 (aset info 2 (copy-marker (region-beginning)))
898 (aset info 3 (copy-marker (region-end))))
899 (aset info (if (> calc-embed-arg 0) 2 3) (point-marker))
900 (if (> calc-embed-arg 0)
901 (progn
902 (forward-line (1- calc-embed-arg))
903 (end-of-line))
904 (forward-line (1+ calc-embed-arg)))
905 (aset info (if (> calc-embed-arg 0) 3 2) (point-marker))))
906 (aset info 4 (copy-marker (aref info 2)))
907 (aset info 5 (copy-marker (aref info 3))))
908 (if (aref info 4)
909 (setq calc-embed-top (aref info 2)
910 fixed calc-embed-top)
911 (if (consp calc-embed-top)
912 (progn
913 (require 'thingatpt)
914 (if (thing-at-point-looking-at calc-embedded-word-regexp)
915 (progn
916 (setq calc-embed-top (copy-marker (match-beginning 0)))
917 (setq calc-embed-bot (copy-marker (match-end 0)))
918 (setq calc-embed-outer-top calc-embed-top)
919 (setq calc-embed-outer-bot calc-embed-bot))
920 (setq calc-embed-top (point-marker))
921 (setq calc-embed-bot (point-marker))
922 (setq calc-embed-outer-top calc-embed-top)
923 (setq calc-embed-outer-bot calc-embed-bot)))
924 (or calc-embed-top
925 (calc-embedded-find-bounds 'plain)))
926 (aset info 2 (copy-marker (min calc-embed-top calc-embed-bot)))
927 (aset info 3 (copy-marker (max calc-embed-top calc-embed-bot)))
928 (aset info 4 (copy-marker (or calc-embed-outer-top (aref info 2))))
929 (aset info 5 (copy-marker (or calc-embed-outer-bot (aref info 3))))))
930 (goto-char (aref info 2))
931 (if new-info
932 (progn
933 (or (bolp) (aset info 7 t))
934 (goto-char (aref info 3))
935 (or (bolp) (eolp) (aset info 7 t))))
936 (if fresh
937 (let ((modes (calc-embedded-find-modes)))
938 (aset info 12 (car modes))
939 (aset info 13 (nth 1 modes))
940 (aset info 14 (nth 2 modes))))
941 (aset info 15 calc-embedded-globals)
942 (setq str (buffer-substring (aref info 2) (aref info 3)))
943 (if (or force
944 (not (equal str (aref info 6))))
945 (if (and fixed (aref info 6))
946 (progn
947 (aset info 4 nil)
948 (calc-embedded-make-info point cbuf nil)
949 (setq new-info nil))
950 (let* ((open-plain calc-embedded-open-plain)
951 (close-plain calc-embedded-close-plain)
952 (pref-len (length open-plain))
953 (calc-embed-vars-used nil)
954 suff-pos val temp)
955 (with-current-buffer (aref info 1)
956 (calc-embedded-set-modes (aref info 15)
957 (aref info 12) (aref info 14))
958 (if (and (> (length str) pref-len)
959 (equal (substring str 0 pref-len) open-plain)
960 (setq suff-pos (string-match (regexp-quote close-plain)
961 str pref-len)))
962 (setq val (math-read-plain-expr
963 (substring str pref-len suff-pos)))
964 (if (string-match "[^ \t\n]" str)
965 (setq pref-len 0
966 val (condition-case nil
967 (math-read-big-expr str)
968 (error (math-read-expr str))))
969 (setq val nil))))
970 (if (eq (car-safe val) 'error)
971 (setq val (list 'error
972 (+ (aref info 2) pref-len (nth 1 val))
973 (nth 2 val))))
974 (aset info 6 str)
975 (aset info 8 val)
976 (setq temp val)
977 (if (eq (car-safe temp) 'calcFunc-evalto)
978 (setq temp (nth 1 temp))
979 (if (eq (car-safe temp) 'error)
980 (if new-info
981 (setq new-info nil)
982 (setcdr found (delq info (cdr found)))
983 (calc-embedded-active-state 'less))))
984 (aset info 9 (and (eq (car-safe temp) 'calcFunc-assign)
985 (nth 1 temp)))
986 (if (memq (car-safe val) '(calcFunc-evalto calcFunc-assign))
987 (calc-embedded-find-vars val))
988 (aset info 10 calc-embed-vars-used)
989 (aset info 11 nil))))
990 (if new-info
991 (progn
992 (setcdr found (cons info (cdr found)))
993 (calc-embedded-active-state 'more)))
994 info))
996 (defun calc-embedded-find-vars (x)
997 (cond ((Math-primp x)
998 (and (eq (car-safe x) 'var)
999 (not (assoc x calc-embed-vars-used))
1000 (setq calc-embed-vars-used (cons (list x) calc-embed-vars-used))))
1001 ((eq (car x) 'calcFunc-evalto)
1002 (calc-embedded-find-vars (nth 1 x)))
1003 ((eq (car x) 'calcFunc-assign)
1004 (calc-embedded-find-vars (nth 2 x)))
1006 (and (eq (car x) 'calcFunc-subscr)
1007 (eq (car-safe (nth 1 x)) 'var)
1008 (Math-primp (nth 2 x))
1009 (not (assoc x calc-embed-vars-used))
1010 (setq calc-embed-vars-used (cons (list x) calc-embed-vars-used)))
1011 (while (setq x (cdr x))
1012 (calc-embedded-find-vars (car x))))))
1014 (defvar math-ms-args)
1015 (defun calc-embedded-evaluate-expr (x)
1016 (let ((calc-embed-vars-used (aref calc-embedded-info 10)))
1017 (or calc-embed-vars-used (calc-embedded-find-vars x))
1018 (if calc-embed-vars-used
1019 (let ((active (assq (aref calc-embedded-info 0) calc-embedded-active))
1020 (math-ms-args nil))
1021 (save-excursion
1022 (calc-embedded-original-buffer t)
1023 (or active
1024 (progn
1025 (calc-embedded-activate)
1026 (setq active (assq (aref calc-embedded-info 0)
1027 calc-embedded-active))))
1028 (while calc-embed-vars-used
1029 (calc-embedded-eval-get-var (car (car calc-embed-vars-used)) active)
1030 (setq calc-embed-vars-used (cdr calc-embed-vars-used))))
1031 (calc-embedded-subst x))
1032 (calc-normalize (math-evaluate-expr-rec x)))))
1034 (defun calc-embedded-subst (x)
1035 (if (and (eq (car-safe x) 'calcFunc-evalto) (cdr x))
1036 (let ((rhs (calc-embedded-subst (nth 1 x))))
1037 (list 'calcFunc-evalto
1038 (nth 1 x)
1039 (if (eq (car-safe rhs) 'calcFunc-assign) (nth 2 rhs) rhs)))
1040 (if (and (eq (car-safe x) 'calcFunc-assign) (= (length x) 3))
1041 (list 'calcFunc-assign
1042 (nth 1 x)
1043 (calc-embedded-subst (nth 2 x)))
1044 (calc-normalize (math-evaluate-expr-rec (math-multi-subst-rec x))))))
1046 (defun calc-embedded-eval-get-var (var base)
1047 (let ((entry base)
1048 (point (aref calc-embedded-info 2))
1049 (last nil)
1050 val)
1051 (while (and (setq entry (cdr entry))
1052 (or (not (equal var (aref (car entry) 9)))
1053 (and (> point (aref (car entry) 3))
1054 (setq last entry)))))
1055 (if last
1056 (setq entry last))
1057 (if entry
1058 (progn
1059 (setq entry (car entry))
1060 (if (equal (buffer-substring (aref entry 2) (aref entry 3))
1061 (aref entry 6))
1062 (progn
1063 (or (aref entry 11)
1064 (save-excursion
1065 (calc-embedded-update entry 14 t nil)))
1066 (setq val (aref entry 11))
1067 (if (eq (car-safe val) 'calcFunc-evalto)
1068 (setq val (nth 2 val)))
1069 (if (eq (car-safe val) 'calcFunc-assign)
1070 (setq val (nth 2 val)))
1071 (setq math-ms-args (cons (cons var val) math-ms-args)))
1072 (calc-embedded-activate)
1073 (calc-embedded-eval-get-var var base))))))
1076 (defun calc-embedded-update (info which need-eval need-display
1077 &optional str entry old-val)
1078 (let* ((calc-embed-prev-modes nil)
1079 (open-plain calc-embedded-open-plain)
1080 (close-plain calc-embedded-close-plain)
1081 (calc-embed-vars-used nil)
1082 (evalled nil)
1083 (val (aref info 8))
1084 (old-eval (aref info 11)))
1085 (or old-val (setq old-val val))
1086 (if (eq (car-safe val) 'calcFunc-evalto)
1087 (setq need-display t))
1088 (unwind-protect
1089 (progn
1090 (set-buffer (aref info 1))
1091 (and which
1092 (calc-embedded-set-modes (aref info 15) (aref info 12)
1093 (aref info which)
1094 (if need-display 'full t)))
1095 (if (memq (car-safe val) '(calcFunc-evalto calcFunc-assign))
1096 (calc-embedded-find-vars val))
1097 (if need-eval
1098 (let ((calc-embedded-info info))
1099 (setq val (math-evaluate-expr val)
1100 evalled val)))
1101 (if (or (eq need-eval 'eval) (eq (car-safe val) 'calcFunc-evalto))
1102 (aset info 8 val))
1103 (aset info 9 nil)
1104 (aset info 10 calc-embed-vars-used)
1105 (aset info 11 nil)
1106 (if (or need-display (eq (car-safe val) 'calcFunc-evalto))
1107 (let ((extra (if (eq calc-language 'big) 1 0)))
1108 (or entry (setq entry (list val 1 nil)))
1109 (or str (progn
1110 (setq str (let ((calc-line-numbering nil))
1111 (math-format-stack-value entry)))
1112 (if (eq calc-language 'big)
1113 (setq str (substring str 0 -1)))))
1114 (and calc-show-plain
1115 (setq str (concat open-plain
1116 (math-showing-full-precision
1117 (math-format-flat-expr val 0))
1118 close-plain
1119 str)))
1120 (save-excursion
1121 (calc-embedded-original-buffer t info)
1122 (or (equal str (aref info 6))
1123 (let ((delta (- (aref info 5) (aref info 3)))
1124 (adjbot 0)
1125 (buffer-read-only nil))
1126 (goto-char (aref info 2))
1127 (delete-region (point) (aref info 3))
1128 (and (> (nth 1 entry) (1+ extra))
1129 (aref info 7)
1130 (progn
1131 (delete-horizontal-space)
1132 (if (looking-at "\n")
1133 ;; If there's a newline there, don't add one
1134 (insert "\n")
1135 (insert "\n\n")
1136 (delete-horizontal-space)
1137 (setq adjbot 1)
1138 ; (setq delta (1+ delta))
1139 (backward-char 1))))
1140 (insert str)
1141 (set-marker (aref info 3) (+ (point) adjbot))
1142 (set-marker (aref info 5) (+ (point) delta))
1143 (aset info 6 str))))))
1144 (if (eq (car-safe val) 'calcFunc-evalto)
1145 (progn
1146 (setq evalled (nth 2 val)
1147 val (nth 1 val))))
1148 (if (eq (car-safe val) 'calcFunc-assign)
1149 (progn
1150 (aset info 9 (nth 1 val))
1151 (aset info 11 (or evalled
1152 (let ((calc-embedded-info info))
1153 (math-evaluate-expr (nth 2 val)))))
1154 (or (equal old-eval (aref info 11))
1155 (calc-embedded-var-change (nth 1 val) (aref info 0))))
1156 (if (eq (car-safe old-val) 'calcFunc-evalto)
1157 (setq old-val (nth 1 old-val)))
1158 (if (eq (car-safe old-val) 'calcFunc-assign)
1159 (calc-embedded-var-change (nth 1 old-val) (aref info 0)))))
1160 (set-buffer (aref info 1))
1161 (while calc-embed-prev-modes
1162 (cond ((eq (car (car calc-embed-prev-modes)) 'the-language)
1163 (if need-display
1164 (calc-embedded-set-language (cdr (car calc-embed-prev-modes)))))
1165 ((eq (car (car calc-embed-prev-modes)) 'the-display-just)
1166 (if need-display
1167 (calc-embedded-set-justify (cdr (car calc-embed-prev-modes)))))
1169 (set (car (car calc-embed-prev-modes))
1170 (cdr (car calc-embed-prev-modes)))))
1171 (setq calc-embed-prev-modes (cdr calc-embed-prev-modes))))))
1176 ;;; These are hooks called by the main part of Calc.
1178 (defvar calc-embedded-no-reselect nil)
1179 (defun calc-embedded-select-buffer ()
1180 (if (eq (current-buffer) (aref calc-embedded-info 0))
1181 (let ((info calc-embedded-info)
1182 horiz vert)
1183 (if (and (or (< (point) (aref info 4))
1184 (> (point) (aref info 5)))
1185 (not calc-embedded-no-reselect))
1186 (let ((calc-embedded-quiet t))
1187 (message "(Switching Calc Embedded mode to new formula.)")
1188 (calc-embedded nil)
1189 (calc-embedded nil)))
1190 (setq horiz (max (min (current-column) (- (point) (aref info 2))) 0)
1191 vert (if (<= (aref info 2) (point))
1192 (- (count-lines (aref info 2) (point))
1193 (if (bolp) 0 1))
1195 (set-buffer (aref info 1))
1196 (if calc-show-plain
1197 (if (= vert 0)
1198 (setq horiz 0)
1199 (setq vert (1- vert))))
1200 (calc-cursor-stack-index 1)
1201 (if calc-line-numbering
1202 (setq horiz (+ horiz 4)))
1203 (if (> vert 0)
1204 (forward-line vert))
1205 (forward-char (min horiz
1206 (- (point-max) (point)))))
1207 (calc-select-buffer)))
1209 (defun calc-embedded-finish-command ()
1210 (let ((buf (current-buffer))
1211 horiz vert)
1212 (with-current-buffer (aref calc-embedded-info 1)
1213 (if (> (calc-stack-size) 0)
1214 (let ((pt (point))
1215 (col (current-column))
1216 (bol (bolp)))
1217 (calc-cursor-stack-index 0)
1218 (if (< pt (point))
1219 (progn
1220 (calc-cursor-stack-index 1)
1221 (if (>= pt (point))
1222 (progn
1223 (setq horiz (- col (if calc-line-numbering 4 0))
1224 vert (- (count-lines (point) pt)
1225 (if bol 0 1)))
1226 (if calc-show-plain
1227 (setq vert (max 1 (1+ vert))))))))
1228 (goto-char pt))))
1229 (if horiz
1230 (progn
1231 (set-buffer (aref calc-embedded-info 0))
1232 (goto-char (aref calc-embedded-info 2))
1233 (if (> vert 0)
1234 (forward-line vert))
1235 (forward-char (max horiz 0))
1236 (set-buffer buf)))))
1238 (defun calc-embedded-stack-change ()
1239 (or calc-executing-macro
1240 (with-current-buffer (aref calc-embedded-info 1)
1241 (let* ((info calc-embedded-info)
1242 (extra-line (if (eq calc-language 'big) 1 0))
1243 (the-point (point))
1244 (empty (= (calc-stack-size) 0))
1245 (entry (if empty
1246 (list '(var empty var-empty) 1 nil)
1247 (calc-top 1 'entry)))
1248 (old-val (aref info 8))
1249 top bot str)
1250 (if empty
1251 (setq str "empty")
1252 (save-excursion
1253 (calc-cursor-stack-index 1)
1254 (setq top (point))
1255 (calc-cursor-stack-index 0)
1256 (setq bot (- (point) extra-line))
1257 (setq str (buffer-substring top (- bot 1))))
1258 (if calc-line-numbering
1259 (let ((pos 0))
1260 (setq str (substring str 4))
1261 (while (setq pos (string-match "\n...." str pos))
1262 (setq str (concat (substring str 0 (1+ pos))
1263 (substring str (+ pos 5)))
1264 pos (1+ pos))))))
1265 (calc-embedded-original-buffer t)
1266 (aset info 8 (car entry))
1267 (calc-embedded-update info 13 nil t str entry old-val)))))
1269 (defun calc-embedded-mode-line-change ()
1270 (let ((str mode-line-buffer-identification))
1271 (save-excursion
1272 (calc-embedded-original-buffer t)
1273 (setq mode-line-buffer-identification str)
1274 (set-buffer-modified-p (buffer-modified-p)))))
1276 (defun calc-embedded-modes-change (vars)
1277 (if (eq (car vars) 'calc-language) (setq vars '(the-language)))
1278 (if (eq (car vars) 'calc-display-just) (setq vars '(the-display-just)))
1279 (while (and vars
1280 (not (rassq (car vars) calc-embedded-mode-vars)))
1281 (setq vars (cdr vars)))
1282 (if (and vars calc-mode-save-mode (not (eq calc-mode-save-mode 'save)))
1283 (save-excursion
1284 (let* ((save-mode calc-mode-save-mode)
1285 (header (if (eq save-mode 'local)
1286 "calc-mode:"
1287 (format "calc-%s-mode:" save-mode)))
1288 (the-language (calc-embedded-language))
1289 (the-display-just (calc-embedded-justify))
1290 (values (mapcar 'symbol-value vars))
1291 (num (cond ((eq save-mode 'local) 12)
1292 ((eq save-mode 'edit) 13)
1293 ((eq save-mode 'perm) 14)
1294 (t nil)))
1295 base limit mname mlist)
1296 (calc-embedded-original-buffer t)
1297 (save-excursion
1298 (if (eq save-mode 'global)
1299 (setq base (point-max)
1300 limit (point-min)
1301 mlist calc-embedded-globals)
1302 (goto-char (aref calc-embedded-info 4))
1303 (beginning-of-line)
1304 (setq base (point)
1305 limit (max (- (point) 1000) (point-min))
1306 mlist (and num (aref calc-embedded-info num)))
1307 (and (re-search-backward
1308 (format "\\(%s\\)[^\001]*\\(%s\\)\\|\\[calc-defaults]"
1309 calc-embedded-open-formula
1310 calc-embedded-close-formula) limit t)
1311 (setq limit (point))))
1312 (while vars
1313 (goto-char base)
1314 (if (setq mname (car (rassq (car vars)
1315 calc-embedded-mode-vars)))
1316 (let ((buffer-read-only nil)
1317 (found (assq (car vars) mlist)))
1318 (if found
1319 (setcdr found (car values))
1320 (setq mlist (cons (cons (car vars) (car values)) mlist))
1321 (if num
1322 (aset calc-embedded-info num mlist)
1323 (if (eq save-mode 'global)
1324 (setq calc-embedded-globals mlist))))
1325 (if (re-search-backward
1326 (format "\\[%s *%s: *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]"
1327 header mname)
1328 limit t)
1329 (progn
1330 (goto-char (match-beginning 1))
1331 (delete-region (point) (match-end 1))
1332 (insert (prin1-to-string (car values))))
1333 (goto-char base)
1334 (insert-before-markers
1335 calc-embedded-open-mode
1336 "[" header " " mname ": "
1337 (prin1-to-string (car values)) "]"
1338 calc-embedded-close-mode))))
1339 (setq vars (cdr vars)
1340 values (cdr values))))))
1341 (when (and vars (eq calc-mode-save-mode 'save))
1342 (calc-embedded-save-original-modes))))
1344 (defun calc-embedded-var-change (var &optional buf)
1345 (if (symbolp var)
1346 (setq var (list 'var
1347 (if (string-match "\\`var-.+\\'"
1348 (symbol-name var))
1349 (intern (substring (symbol-name var) 4))
1350 var)
1351 var)))
1352 (save-excursion
1353 (let ((manual (not calc-auto-recompute))
1354 (bp calc-embedded-active)
1355 (first t))
1356 (if buf (setq bp (memq (assq buf bp) bp)))
1357 (while bp
1358 (let ((calc-embedded-no-reselect t)
1359 (p (and (buffer-name (car (car bp)))
1360 (cdr (car bp)))))
1361 (while p
1362 (if (assoc var (aref (car p) 10))
1363 (if manual
1364 (if (aref (car p) 11)
1365 (progn
1366 (aset (car p) 11 nil)
1367 (if (aref (car p) 9)
1368 (calc-embedded-var-change (aref (car p) 9)))))
1369 (set-buffer (aref (car p) 0))
1370 (if (equal (buffer-substring (aref (car p) 2)
1371 (aref (car p) 3))
1372 (aref (car p) 6))
1373 (let ((calc-embedded-info nil))
1374 (or calc-embedded-quiet
1375 (message "Recomputing..."))
1376 (setq first nil)
1377 (calc-wrapper
1378 (set-buffer (aref (car p) 0))
1379 (calc-embedded-update (car p) 14 t nil)))
1380 (setcdr (car bp) (delq (car p) (cdr (car bp))))
1381 (message
1382 "(Tried to recompute but formula was changed or missing)"))))
1383 (setq p (cdr p))))
1384 (setq bp (if buf nil (cdr bp))))
1385 (or first calc-embedded-quiet (message "")))))
1387 (provide 'calc-embed)
1389 ;; Local variables:
1390 ;; generated-autoload-file: "calc-loaddefs.el"
1391 ;; End:
1393 ;;; calc-embed.el ends here