1 ;;; calc-embed.el --- embed Calc in a buffer
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2005 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <belanger@truman.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
29 ;; This file is autoloaded from calc-ext.el.
34 (defun calc-show-plain (n)
37 (calc-set-command-flag 'renum-stack)
38 (message (if (calc-change-mode 'calc-show-plain n nil t)
39 "Including \"plain\" formulas in Calc Embedded mode"
40 "Omitting \"plain\" formulas in Calc Embedded mode"))))
43 (defvar calc-embedded-modes nil)
44 (defvar calc-embedded-globals nil)
45 (defvar calc-embedded-active nil)
46 (defvar calc-embedded-all-active nil)
47 (make-variable-buffer-local 'calc-embedded-all-active)
48 (defvar calc-embedded-some-active nil)
49 (make-variable-buffer-local 'calc-embedded-some-active)
51 ;; The following variables are customizable and defined in calc.el.
52 (defvar calc-embedded-announce-formula)
53 (defvar calc-embedded-open-formula)
54 (defvar calc-embedded-close-formula)
55 (defvar calc-embedded-open-word)
56 (defvar calc-embedded-close-word)
57 (defvar calc-embedded-open-plain)
58 (defvar calc-embedded-close-plain)
59 (defvar calc-embedded-open-new-formula)
60 (defvar calc-embedded-close-new-formula)
61 (defvar calc-embedded-open-mode)
62 (defvar calc-embedded-close-mode)
64 (defconst calc-embedded-mode-vars '(("precision" . calc-internal-prec)
65 ("word-size" . calc-word-size)
66 ("angles" . calc-angle-mode)
67 ("symbolic" . calc-symbolic-mode)
68 ("matrix" . calc-matrix-mode)
69 ("fractions" . calc-prefer-frac)
70 ("complex" . calc-complex-mode)
71 ("simplify" . calc-simplify-mode)
72 ("language" . the-language)
73 ("plain" . calc-show-plain)
74 ("break" . calc-line-breaking)
75 ("justify" . the-display-just)
76 ("left-label" . calc-left-label)
77 ("right-label" . calc-right-label)
78 ("radix" . calc-number-radix)
79 ("leading-zeros" . calc-leading-zeros)
80 ("grouping" . calc-group-digits)
81 ("group-char" . calc-group-char)
82 ("point-char" . calc-point-char)
83 ("frac-format" . calc-frac-format)
84 ("float-format" . calc-float-format)
85 ("complex-format" . calc-complex-format)
86 ("hms-format" . calc-hms-format)
87 ("date-format" . calc-date-format)
88 ("matrix-justify" . calc-matrix-just)
89 ("full-vectors" . calc-full-vectors)
90 ("break-vectors" . calc-break-vectors)
91 ("vector-commas" . calc-vector-commas)
92 ("vector-brackets" . calc-vector-brackets)
93 ("matrix-brackets" . calc-matrix-brackets)
94 ("strings" . calc-display-strings)
98 ;;; Format of calc-embedded-info vector:
100 ;;; 1 Calculator buffer.
101 ;;; 2 Top of current formula (marker).
102 ;;; 3 Bottom of current formula (marker).
103 ;;; 4 Top of current formula's delimiters (marker).
104 ;;; 5 Bottom of current formula's delimiters (marker).
105 ;;; 6 String representation of current formula.
106 ;;; 7 Non-nil if formula is embedded within a single line.
107 ;;; 8 Internal representation of current formula.
108 ;;; 9 Variable assigned by this formula, or nil.
109 ;;; 10 List of variables upon which this formula depends.
110 ;;; 11 Evaluated value of the formula, or nil.
111 ;;; 12 Mode settings for current formula.
112 ;;; 13 Local mode settings for current formula.
113 ;;; 14 Permanent mode settings for current formula.
114 ;;; 15 Global mode settings for editing buffer.
117 ;;; calc-embedded-active is an a-list keyed on buffers; each cdr is a
118 ;;; sorted list of calc-embedded-infos in that buffer. We do this
119 ;;; rather than using buffer-local variables because the latter are
120 ;;; thrown away when a buffer changes major modes.
122 (defvar calc-embedded-original-modes nil
123 "The mode settings for Calc buffer when put in embedded mode.")
125 (defun calc-embedded-save-original-modes ()
126 "Save the current Calc modes when entereding embedded mode."
127 (let ((calcbuf (save-excursion
132 (with-current-buffer calcbuf
134 (cons calc-language calc-language-option))
136 (list (cons 'calc-display-just
138 (cons 'calc-display-origin
139 calc-display-origin)))
140 (let ((v calc-embedded-mode-vars))
142 (let ((var (cdr (car v))))
143 (unless (memq var '(the-language the-display-just))
145 (cons (cons var (symbol-value var))
148 (setq calc-embedded-original-modes (cons lang modes)))
149 (setq calc-embedded-original-modes nil))))
151 (defun calc-embedded-preserve-modes ()
152 "Preserve the current modes when leaving embedded mode."
154 (if calc-embedded-info
156 (calc-embedded-save-original-modes)
157 (message "Current modes will be preserved when leaving embedded mode."))
158 (message "Not in embedded mode.")))
160 (defun calc-embedded-restore-original-modes ()
161 "Restore the original Calc modes when leaving embedded mode."
162 (let ((calcbuf (get-buffer "*Calculator*"))
164 (lang (car calc-embedded-original-modes))
165 (modes (cdr calc-embedded-original-modes)))
166 (if (and calcbuf calc-embedded-original-modes)
167 (with-current-buffer calcbuf
169 (equal calc-language (car lang))
170 (equal calc-language-option (cdr lang)))
171 (calc-set-language (car lang) (cdr lang))
174 (let ((mode (car modes)))
175 (unless (equal (symbol-value (car mode)) (cdr mode))
176 (set (car mode) (cdr mode))
178 (setq modes (cdr modes)))
181 (calc-set-mode-line))))
182 (setq calc-embedded-original-modes nil)))
184 ;; The variables calc-embed-outer-top, calc-embed-outer-bot,
185 ;; calc-embed-top and calc-embed-bot are
186 ;; local to calc-do-embedded, calc-embedded-mark-formula,
187 ;; calc-embedded-duplicate, calc-embedded-new-formula and
188 ;; calc-embedded-make-info, but are used by calc-embedded-find-bounds,
189 ;; which is called (directly or indirectly) by the above functions.
190 (defvar calc-embed-outer-top)
191 (defvar calc-embed-outer-bot)
192 (defvar calc-embed-top)
193 (defvar calc-embed-bot)
195 ;; The variable calc-embed-arg is local to calc-do-embedded,
196 ;; calc-embedded-update-formula, calc-embedded-edit and
197 ;; calc-do-embedded-activate, but is used by
198 ;; calc-embedded-make-info, which is called by the above
200 (defvar calc-embed-arg)
202 (defvar calc-embedded-quiet nil)
203 (defun calc-do-embedded (calc-embed-arg end obeg oend)
204 (if calc-embedded-info
206 ;; Turn embedded mode off or switch to a new buffer.
207 (cond ((eq (current-buffer) (aref calc-embedded-info 1))
208 (let ((calcbuf (current-buffer))
209 (buf (aref calc-embedded-info 0)))
210 (calc-embedded-original-buffer t)
212 (switch-to-buffer calcbuf)))
214 ((eq (current-buffer) (aref calc-embedded-info 0))
215 (let* ((info calc-embedded-info)
216 (mode calc-embedded-modes))
218 (set-buffer (aref info 1))
219 (if (and (> (calc-stack-size) 0)
220 (equal (calc-top 1 'full) (aref info 8)))
221 (let ((calc-no-refresh-evaltos t))
222 (if (calc-top 1 'sel)
224 (calc-embedded-set-modes
225 (aref info 15) (aref info 12) (aref info 14))
226 (let ((calc-embedded-info nil))
227 (calc-wrapper (calc-pop-stack))))
228 (calc-set-mode-line)))
229 (setq calc-embedded-info nil
230 mode-line-buffer-identification (car mode)
231 truncate-lines (nth 2 mode)
232 buffer-read-only nil)
233 (use-local-map (nth 1 mode))
234 (set-buffer-modified-p (buffer-modified-p))
235 (calc-embedded-restore-original-modes)
236 (or calc-embedded-quiet
237 (message "Back to %s mode" mode-name))))
240 (if (buffer-name (aref calc-embedded-info 0))
242 (set-buffer (aref calc-embedded-info 0))
243 (or (y-or-n-p (format "Cancel Calc Embedded mode in buffer %s? "
246 (calc-embedded nil)))
247 (calc-embedded calc-embed-arg end obeg oend)))
249 ;; Turn embedded mode on.
250 (calc-plain-buffer-only)
251 (let ((modes (list mode-line-buffer-identification
254 calc-embed-top calc-embed-bot calc-embed-outer-top calc-embed-outer-bot
256 (barf-if-buffer-read-only)
257 (calc-embedded-save-original-modes)
258 (or calc-embedded-globals
261 (calc-embedded-make-info (point) nil t calc-embed-arg end obeg oend))
262 (if (eq (car-safe (aref info 8)) 'error)
264 (setq calc-embedded-original-modes nil)
265 (goto-char (nth 1 (aref info 8)))
266 (error (nth 2 (aref info 8)))))
267 (let ((mode-line-buffer-identification mode-line-buffer-identification)
268 (calc-embedded-info info)
269 (calc-embedded-no-reselect t))
272 (calc-no-refresh-evaltos t))
275 (calc-push (calc-normalize (aref info 8)))
276 (setq chg (calc-embedded-set-modes
277 (aref info 15) (aref info 12) (aref info 13))))
278 (setq chg (calc-embedded-set-modes
279 (aref info 15) (aref info 12) (aref info 13)))
281 (setq calc-undo-list nil
283 ident mode-line-buffer-identification)))
284 (setq calc-embedded-info info
285 calc-embedded-modes modes
286 mode-line-buffer-identification ident
289 (set-buffer-modified-p (buffer-modified-p))
290 (use-local-map calc-mode-map)
291 (setq calc-no-refresh-evaltos nil)
292 (and chg calc-any-evaltos (calc-wrapper (calc-refresh-evaltos)))
296 (setq str mode-line-buffer-identification))
297 (unless (equal str mode-line-buffer-identification)
298 (setq mode-line-buffer-identification str)
299 (set-buffer-modified-p (buffer-modified-p))))
300 (or (eq calc-embedded-quiet t)
301 (message "Embedded Calc mode enabled; %s to return to normal"
302 (if calc-embedded-quiet
304 "Give this command again")))))
305 (scroll-down 0)) ; fix a bug which occurs when truncate-lines is changed.
308 (defun calc-embedded-select (arg)
311 (and calc-embedded-info
312 (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-evalto)
313 (calc-select-part 1))
314 (and calc-embedded-info
315 (or (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-assign)
316 (and (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-evalto)
317 (eq (car-safe (nth 1 (aref calc-embedded-info 8)))
319 (calc-select-part 2)))
322 (defun calc-embedded-update-formula (calc-embed-arg)
325 (let ((entry (assq (current-buffer) calc-embedded-active)))
326 (while (setq entry (cdr entry))
327 (and (eq (car-safe (aref (car entry) 8)) 'calcFunc-evalto)
328 (or (not (consp calc-embed-arg))
329 (and (<= (aref (car entry) 2) (region-beginning))
330 (>= (aref (car entry) 3) (region-end))))
332 (calc-embedded-update (car entry) 14 t t)))))
333 (if (and calc-embedded-info
334 (eq (current-buffer) (aref calc-embedded-info 0))
335 (>= (point) (aref calc-embedded-info 4))
336 (<= (point) (aref calc-embedded-info 5)))
339 (info (calc-embedded-make-info (point) nil t))
340 (pt (- opt (aref info 4))))
341 (or (eq (car-safe (aref info 8)) 'error)
344 (calc-embedded-update info 14 'eval t))
345 (goto-char (+ (aref info 4) pt))))))))
348 (defun calc-embedded-edit (calc-embed-arg)
350 (let ((info (calc-embedded-make-info (point) nil t calc-embed-arg))
352 (if (eq (car-safe (aref info 8)) 'error)
354 (goto-char (nth 1 (aref info 8)))
355 (error (nth 2 (aref info 8)))))
357 (setq str (math-showing-full-precision
358 (math-format-nice-expr (aref info 8) (frame-width))))
359 (calc-edit-mode (list 'calc-embedded-finish-edit info))
361 (calc-show-edit-buffer))
363 (defvar calc-original-buffer)
364 (defvar calc-edit-top)
365 (defun calc-embedded-finish-edit (info)
366 (let ((buf (current-buffer))
367 (str (buffer-substring calc-edit-top (point-max)))
370 (switch-to-buffer calc-original-buffer)
371 (let ((val (save-excursion
372 (set-buffer (aref info 1))
373 (let ((calc-language nil)
374 (math-expr-opers math-standard-opers))
375 (math-read-expr str)))))
376 (if (eq (car-safe val) 'error)
378 (switch-to-buffer buf)
379 (goto-char (+ start (nth 1 val)))
380 (error (nth 2 val))))
381 (calc-embedded-original-buffer t info)
383 (calc-embedded-update info 14 t t))))
385 (defun calc-do-embedded-activate (calc-embed-arg cbuf)
386 (calc-plain-buffer-only)
388 (calc-embedded-forget))
390 (if (< (prefix-numeric-value calc-embed-arg) 0)
391 (message "Deactivating %s for Calc Embedded mode" (buffer-name))
392 (message "Activating %s for Calc Embedded mode..." (buffer-name))
394 (let* ((active (assq (current-buffer) calc-embedded-active))
396 (pat " := \\| \\\\gets \\| => \\| \\\\evalto "))
397 (if calc-embedded-announce-formula
398 (setq pat (format "%s\\|\\(%s\\)"
399 pat calc-embedded-announce-formula)))
400 (while (setq info (cdr info))
401 (or (equal (buffer-substring (aref (car info) 2) (aref (car info) 3))
403 (setcdr active (delq (car info) (cdr active)))))
404 (goto-char (point-min))
405 (while (re-search-forward pat nil t)
406 ;;; (if (looking-at calc-embedded-open-formula)
407 ;;; (goto-char (match-end 1)))
408 (setq info (calc-embedded-make-info (point) cbuf nil))
409 (or (eq (car-safe (aref info 8)) 'error)
410 (goto-char (aref info 5))))))
411 (message "Activating %s for Calc Embedded mode...done" (buffer-name)))
412 (calc-embedded-active-state t))
414 (defun calc-plain-buffer-only ()
415 (if (memq major-mode '(calc-mode calc-trail-mode calc-edit-mode))
416 (error "This command should be used in a normal editing buffer")))
418 (defun calc-embedded-active-state (state)
419 (or (assq 'calc-embedded-all-active minor-mode-alist)
420 (setq minor-mode-alist
421 (cons '(calc-embedded-all-active " Active")
422 (cons '(calc-embedded-some-active " ~Active")
424 (let ((active (assq (current-buffer) calc-embedded-active)))
427 (and (eq state 'more) calc-embedded-all-active (setq state t))
428 (setq calc-embedded-all-active (eq state t)
429 calc-embedded-some-active (not (memq state '(nil t))))
430 (set-buffer-modified-p (buffer-modified-p)))
433 (defun calc-embedded-original-buffer (switch &optional info)
434 (or info (setq info calc-embedded-info))
435 (or (buffer-name (aref info 0))
437 (error "Calc embedded mode: Original buffer has been killed")))
439 (set-buffer (aref info 0))))
441 (defun calc-embedded-word ()
443 (calc-embedded '(4)))
445 (defun calc-embedded-mark-formula (&optional body-only)
446 "Put point at the beginning of this Calc formula, mark at the end.
447 This normally marks the whole formula, including surrounding delimiters.
448 With any prefix argument, marks only the formula itself."
450 (and (eq major-mode 'calc-mode)
451 (error "This command should be used in a normal editing buffer"))
452 (let (calc-embed-top calc-embed-bot calc-embed-outer-top calc-embed-outer-bot)
454 (calc-embedded-find-bounds body-only))
455 (push-mark (if body-only calc-embed-bot calc-embed-outer-bot) t)
456 (goto-char (if body-only calc-embed-top calc-embed-outer-top))))
458 (defun calc-embedded-find-bounds (&optional plain)
459 ;; (while (and (bolp) (eq (following-char) ?\n))
461 (and (eolp) (bolp) (not (eq (char-after (- (point) 2)) ?\n))
463 (let ((home (point)))
464 (or (and (looking-at calc-embedded-open-formula)
465 (not (looking-at calc-embedded-close-formula)))
466 (re-search-backward calc-embedded-open-formula nil t)
467 (error "Can't find start of formula"))
468 (and (eq (preceding-char) ?\$) ; backward search for \$\$? won't back
469 (eq (following-char) ?\$) ; up over a second $, so do it by hand.
471 (setq calc-embed-outer-top (point))
472 (goto-char (match-end 0))
473 (if (looking-at "[ \t]*$")
475 (if (eq (following-char) ?\n)
478 (while (eq (following-char) ?\ )
480 (or (eq plain 'plain)
481 (if (looking-at (regexp-quote calc-embedded-open-plain))
483 (goto-char (match-end 0))
484 (search-forward calc-embedded-close-plain))))
485 (setq calc-embed-top (point))
486 (or (re-search-forward calc-embedded-close-formula nil t)
487 (error "Can't find end of formula"))
489 (error "Not inside a formula"))
490 (and (eq (following-char) ?\n) (not (bolp))
492 (setq calc-embed-outer-bot (point))
493 (goto-char (match-beginning 0))
494 (if (eq (preceding-char) ?\n)
497 (while (eq (preceding-char) ?\ )
499 (setq calc-embed-bot (point))))
501 (defun calc-embedded-kill-formula ()
502 "Kill the formula surrounding point.
503 If Calc Embedded mode was active, this deactivates it.
504 The formula (including its surrounding delimiters) is saved in the kill ring.
505 The command \\[yank] can retrieve it from there."
507 (and calc-embedded-info
509 (calc-embedded-mark-formula)
510 (kill-region (point) (mark))
513 (defun calc-embedded-copy-formula-as-kill ()
514 "Save the formula surrounding point as if killed, but don't kill it."
517 (calc-embedded-mark-formula)
518 (copy-region-as-kill (point) (mark))
521 (defun calc-embedded-duplicate ()
523 (let ((already calc-embedded-info)
524 calc-embed-top calc-embed-bot calc-embed-outer-top calc-embed-outer-bot new-top)
525 (if calc-embedded-info
527 (setq calc-embed-top (+ (aref calc-embedded-info 2))
528 calc-embed-bot (+ (aref calc-embedded-info 3))
529 calc-embed-outer-top (+ (aref calc-embedded-info 4))
530 calc-embed-outer-bot (+ (aref calc-embedded-info 5)))
532 (calc-embedded-find-bounds))
533 (goto-char calc-embed-outer-bot)
535 (setq new-top (point))
536 (insert-buffer-substring (current-buffer)
537 calc-embed-outer-top calc-embed-outer-bot)
538 (goto-char (+ new-top (- calc-embed-top calc-embed-outer-top)))
539 (let ((calc-embedded-quiet (if already t 'x)))
540 (calc-embedded (+ new-top (- calc-embed-top calc-embed-outer-top))
541 (+ new-top (- calc-embed-bot calc-embed-outer-top))
543 (+ new-top (- calc-embed-outer-bot calc-embed-outer-top))))))
545 (defun calc-embedded-next (arg)
547 (setq arg (prefix-numeric-value arg))
548 (let* ((active (cdr (assq (current-buffer) calc-embedded-active)))
550 (num (length active)))
552 (error "No active formulas in buffer"))
555 (if (<= (point) (aref (car active) 3))
556 (goto-char (aref (nth (1- num) active) 2))
558 (> (point) (aref (nth 1 p) 3)))
560 (goto-char (aref (car p) 2))))
562 (calc-embedded-next -1)
563 (calc-embedded-next (+ (* num 1000) arg 1)))
565 (setq arg (1+ (% (1- arg) num)))
566 (while (and p (>= (point) (aref (car p) 2)))
568 (while (> (setq arg (1- arg)) 0)
569 (setq p (if p (cdr p) (cdr active))))
570 (goto-char (aref (car (or p active)) 2))))))
572 (defun calc-embedded-previous (arg)
574 (calc-embedded-next (- (prefix-numeric-value arg))))
576 (defun calc-embedded-new-formula ()
578 (and (eq major-mode 'calc-mode)
579 (error "This command should be used in a normal editing buffer"))
580 (if calc-embedded-info
582 (let (calc-embed-top calc-embed-bot calc-embed-outer-top calc-embed-outer-bot)
583 (if (and (eq (preceding-char) ?\n)
584 (string-match "\\`\n" calc-embedded-open-new-formula))
586 (setq calc-embed-outer-top (1- (point)))
588 (insert (substring calc-embedded-open-new-formula 1)))
589 (setq calc-embed-outer-top (point))
590 (insert calc-embedded-open-new-formula))
591 (setq calc-embed-top (point))
593 (setq calc-embed-bot (point))
594 (insert calc-embedded-close-new-formula)
595 (if (and (eq (following-char) ?\n)
596 (string-match "\n\\'" calc-embedded-close-new-formula))
598 (setq calc-embed-outer-bot (point))
599 (goto-char calc-embed-top)
600 (let ((calc-embedded-quiet 'x))
601 (calc-embedded calc-embed-top calc-embed-bot calc-embed-outer-top calc-embed-outer-bot))))
603 (defun calc-embedded-forget ()
605 (setq calc-embedded-active (delq (assq (current-buffer) calc-embedded-active)
606 calc-embedded-active))
607 (calc-embedded-active-state nil))
609 ;; The variables calc-embed-prev-modes is local to calc-embedded-update,
610 ;; but is used by calc-embedded-set-modes.
611 (defvar calc-embed-prev-modes)
613 (defun calc-embedded-set-modes (gmodes modes local-modes &optional temp)
614 (let ((the-language (calc-embedded-language))
615 (the-display-just (calc-embedded-justify))
620 (or (symbolp (car v))
621 (and (setq found (assq (car (car v)) modes))
622 (not (eq (cdr found) 'default)))
623 (and (setq found (assq (car (car v)) local-modes))
624 (not (eq (cdr found) 'default)))
626 (if (eq (setq value (cdr (car v))) 'default)
627 (setq value (list (nth 1 (assq (car (car v)) calc-mode-var-list)))))
628 (equal (symbol-value (car (car v))) value))
631 (if temp (setq calc-embed-prev-modes
632 (cons (cons (car (car v))
633 (symbol-value (car (car v))))
634 calc-embed-prev-modes)))
635 (set (car (car v)) value)))
639 (or (and (setq found (assq (car (car v)) local-modes))
640 (not (eq (cdr found) 'default)))
641 (eq (setq value (cdr (car v))) 'default)
642 (equal (symbol-value (car (car v))) value)
645 (if temp (setq calc-embed-prev-modes (cons (cons (car (car v))
646 (symbol-value (car (car v))))
647 calc-embed-prev-modes)))
648 (set (car (car v)) value)))
652 (or (eq (setq value (cdr (car v))) 'default)
653 (equal (symbol-value (car (car v))) value)
656 (if temp (setq calc-embed-prev-modes (cons (cons (car (car v))
657 (symbol-value (car (car v))))
658 calc-embed-prev-modes)))
659 (set (car (car v)) value)))
661 (and changed (not (eq temp t))
663 (calc-embedded-set-justify the-display-just)
664 (calc-embedded-set-language the-language)))
665 (and changed (not temp)
667 (setq calc-full-float-format (list (if (eq (car calc-float-format)
670 (car calc-float-format))
675 (defun calc-embedded-language ()
676 (if calc-language-option
677 (list calc-language calc-language-option)
680 (defun calc-embedded-set-language (lang)
683 (setq option (nth 1 lang)
685 (or (and (eq lang calc-language)
686 (equal option calc-language-option))
687 (calc-set-language lang option t))))
689 (defun calc-embedded-justify ()
690 (if calc-display-origin
691 (list calc-display-just calc-display-origin)
694 (defun calc-embedded-set-justify (just)
696 (setq calc-display-origin (nth 1 just)
697 calc-display-just (car just))
698 (setq calc-display-just just
699 calc-display-origin nil)))
702 (defun calc-find-globals ()
704 (and (eq major-mode 'calc-mode)
705 (error "This command should be used in a normal editing buffer"))
706 (make-local-variable 'calc-embedded-globals)
707 (let ((case-fold-search nil)
711 (goto-char (point-min))
712 (while (re-search-forward "\\[calc-global-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)\\]" nil t)
713 (and (setq found (assoc (buffer-substring (match-beginning 1)
715 calc-embedded-mode-vars))
716 (or (assq (cdr found) modes)
717 (setq modes (cons (cons (cdr found)
718 (car (read-from-string
723 (setq calc-embedded-globals (cons t modes))
724 (goto-char save-pt)))
726 (defun calc-embedded-find-modes ()
727 (let ((case-fold-search nil)
734 (while (and no-defaults (search-backward "[calc-" nil t))
736 (or (and (looking-at "mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
737 (setq found (assoc (buffer-substring (match-beginning 1)
739 calc-embedded-mode-vars))
740 (or (assq (cdr found) modes)
741 (setq modes (cons (cons (cdr found)
742 (car (read-from-string
747 (and (looking-at "perm-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
748 (setq found (assoc (buffer-substring (match-beginning 1)
750 calc-embedded-mode-vars))
751 (or (assq (cdr found) pmodes)
752 (setq pmodes (cons (cons (cdr found)
753 (car (read-from-string
758 (and (looking-at "edit-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
759 (setq found (assoc (buffer-substring (match-beginning 1)
761 calc-embedded-mode-vars))
762 (or (assq (cdr found) emodes)
763 (setq emodes (cons (cons (cdr found)
764 (car (read-from-string
769 (and (looking-at "defaults]")
770 (setq no-defaults nil)))
773 (unless (assq 'the-language modes)
774 (let ((lang (assoc major-mode calc-language-alist)))
776 (setq modes (cons (cons 'the-language (cdr lang))
778 (list modes emodes pmodes)))
780 ;; The variable calc-embed-vars-used is local to calc-embedded-make-info,
781 ;; calc-embedded-evaluate-expr and calc-embedded-update, but is
782 ;; used by calc-embedded-find-vars, which is called by the above functions.
783 (defvar calc-embed-vars-used)
785 (defun calc-embedded-make-info (point cbuf fresh &optional
786 calc-embed-top calc-embed-bot
787 calc-embed-outer-top calc-embed-outer-bot)
788 (let* ((bufentry (assq (current-buffer) calc-embedded-active))
790 (force (and fresh calc-embed-top))
791 (fixed calc-embed-top)
795 (setq found (list (current-buffer))
796 calc-embedded-active (cons found calc-embedded-active)))
797 (while (and (cdr found)
798 (> point (aref (car (cdr found)) 3)))
799 (setq found (cdr found)))
801 (>= point (aref (nth 1 found) 2)))
802 (setq info (nth 1 found))
803 (setq info (make-vector 16 nil)
806 (aset info 0 (current-buffer))
807 (aset info 1 (or cbuf (save-excursion
810 (if (and (integerp calc-embed-top) (not calc-embed-bot))
811 ; started with a user-supplied argument
813 (if (= (setq calc-embed-arg (prefix-numeric-value calc-embed-arg)) 0)
815 (aset info 2 (copy-marker (region-beginning)))
816 (aset info 3 (copy-marker (region-end))))
817 (aset info (if (> calc-embed-arg 0) 2 3) (point-marker))
818 (if (> calc-embed-arg 0)
820 (forward-line (1- calc-embed-arg))
822 (forward-line (1+ calc-embed-arg)))
823 (aset info (if (> calc-embed-arg 0) 3 2) (point-marker)))
824 (aset info 4 (copy-marker (aref info 2)))
825 (aset info 5 (copy-marker (aref info 3))))
827 (setq calc-embed-top (aref info 2)
828 fixed calc-embed-top)
829 (if (consp calc-embed-top)
830 (let ((calc-embedded-open-formula calc-embedded-open-word)
831 (calc-embedded-close-formula calc-embedded-close-word))
832 (calc-embedded-find-bounds 'plain))
834 (calc-embedded-find-bounds 'plain)))
835 (aset info 2 (copy-marker (min calc-embed-top calc-embed-bot)))
836 (aset info 3 (copy-marker (max calc-embed-top calc-embed-bot)))
837 (aset info 4 (copy-marker (or calc-embed-outer-top (aref info 2))))
838 (aset info 5 (copy-marker (or calc-embed-outer-bot (aref info 3))))))
839 (goto-char (aref info 2))
842 (or (bolp) (aset info 7 t))
843 (goto-char (aref info 3))
844 (or (bolp) (eolp) (aset info 7 t))))
846 (let ((modes (calc-embedded-find-modes)))
847 (aset info 12 (car modes))
848 (aset info 13 (nth 1 modes))
849 (aset info 14 (nth 2 modes))))
850 (aset info 15 calc-embedded-globals)
851 (setq str (buffer-substring (aref info 2) (aref info 3)))
853 (not (equal str (aref info 6))))
854 (if (and fixed (aref info 6))
857 (calc-embedded-make-info point cbuf nil)
859 (let* ((open-plain calc-embedded-open-plain)
860 (close-plain calc-embedded-close-plain)
861 (pref-len (length open-plain))
862 (calc-embed-vars-used nil)
865 (set-buffer (aref info 1))
866 (calc-embedded-set-modes (aref info 15)
867 (aref info 12) (aref info 14))
868 (if (and (> (length str) pref-len)
869 (equal (substring str 0 pref-len) open-plain)
870 (setq suff-pos (string-match (regexp-quote close-plain)
872 (setq val (math-read-plain-expr
873 (substring str pref-len suff-pos)))
874 (if (string-match "[^ \t\n]" str)
876 val (math-read-big-expr str))
878 (if (eq (car-safe val) 'error)
879 (setq val (list 'error
880 (+ (aref info 2) pref-len (nth 1 val))
885 (if (eq (car-safe temp) 'calcFunc-evalto)
886 (setq temp (nth 1 temp))
887 (if (eq (car-safe temp) 'error)
890 (setcdr found (delq info (cdr found)))
891 (calc-embedded-active-state 'less))))
892 (aset info 9 (and (eq (car-safe temp) 'calcFunc-assign)
894 (if (memq (car-safe val) '(calcFunc-evalto calcFunc-assign))
895 (calc-embedded-find-vars val))
896 (aset info 10 calc-embed-vars-used)
897 (aset info 11 nil))))
900 (setcdr found (cons info (cdr found)))
901 (calc-embedded-active-state 'more)))
904 (defun calc-embedded-find-vars (x)
905 (cond ((Math-primp x)
906 (and (eq (car-safe x) 'var)
907 (not (assoc x calc-embed-vars-used))
908 (setq calc-embed-vars-used (cons (list x) calc-embed-vars-used))))
909 ((eq (car x) 'calcFunc-evalto)
910 (calc-embedded-find-vars (nth 1 x)))
911 ((eq (car x) 'calcFunc-assign)
912 (calc-embedded-find-vars (nth 2 x)))
914 (and (eq (car x) 'calcFunc-subscr)
915 (eq (car-safe (nth 1 x)) 'var)
916 (Math-primp (nth 2 x))
917 (not (assoc x calc-embed-vars-used))
918 (setq calc-embed-vars-used (cons (list x) calc-embed-vars-used)))
919 (while (setq x (cdr x))
920 (calc-embedded-find-vars (car x))))))
922 (defvar math-ms-args)
923 (defun calc-embedded-evaluate-expr (x)
924 (let ((calc-embed-vars-used (aref calc-embedded-info 10)))
925 (or calc-embed-vars-used (calc-embedded-find-vars x))
926 (if calc-embed-vars-used
927 (let ((active (assq (aref calc-embedded-info 0) calc-embedded-active))
930 (calc-embedded-original-buffer t)
933 (calc-embedded-activate)
934 (setq active (assq (aref calc-embedded-info 0)
935 calc-embedded-active))))
936 (while calc-embed-vars-used
937 (calc-embedded-eval-get-var (car (car calc-embed-vars-used)) active)
938 (setq calc-embed-vars-used (cdr calc-embed-vars-used))))
939 (calc-embedded-subst x))
940 (calc-normalize (math-evaluate-expr-rec x)))))
942 (defun calc-embedded-subst (x)
943 (if (and (eq (car-safe x) 'calcFunc-evalto) (cdr x))
944 (let ((rhs (calc-embedded-subst (nth 1 x))))
945 (list 'calcFunc-evalto
947 (if (eq (car-safe rhs) 'calcFunc-assign) (nth 2 rhs) rhs)))
948 (if (and (eq (car-safe x) 'calcFunc-assign) (= (length x) 3))
949 (list 'calcFunc-assign
951 (calc-embedded-subst (nth 2 x)))
952 (calc-normalize (math-evaluate-expr-rec (math-multi-subst-rec x))))))
954 (defun calc-embedded-eval-get-var (var base)
956 (point (aref calc-embedded-info 2))
959 (while (and (setq entry (cdr entry))
960 (or (not (equal var (aref (car entry) 9)))
961 (and (> point (aref (car entry) 3))
962 (setq last entry)))))
967 (setq entry (car entry))
968 (if (equal (buffer-substring (aref entry 2) (aref entry 3))
973 (calc-embedded-update entry 14 t nil)))
974 (setq val (aref entry 11))
975 (if (eq (car-safe val) 'calcFunc-evalto)
976 (setq val (nth 2 val)))
977 (if (eq (car-safe val) 'calcFunc-assign)
978 (setq val (nth 2 val)))
979 (setq math-ms-args (cons (cons var val) math-ms-args)))
980 (calc-embedded-activate)
981 (calc-embedded-eval-get-var var base))))))
984 (defun calc-embedded-update (info which need-eval need-display
985 &optional str entry old-val)
986 (let* ((calc-embed-prev-modes nil)
987 (open-plain calc-embedded-open-plain)
988 (close-plain calc-embedded-close-plain)
989 (calc-embed-vars-used nil)
992 (old-eval (aref info 11)))
993 (or old-val (setq old-val val))
994 (if (eq (car-safe val) 'calcFunc-evalto)
995 (setq need-display t))
998 (set-buffer (aref info 1))
1000 (calc-embedded-set-modes (aref info 15) (aref info 12)
1002 (if need-display 'full t)))
1003 (if (memq (car-safe val) '(calcFunc-evalto calcFunc-assign))
1004 (calc-embedded-find-vars val))
1006 (let ((calc-embedded-info info))
1007 (setq val (math-evaluate-expr val)
1009 (if (or (eq need-eval 'eval) (eq (car-safe val) 'calcFunc-evalto))
1012 (aset info 10 calc-embed-vars-used)
1014 (if (or need-display (eq (car-safe val) 'calcFunc-evalto))
1015 (let ((extra (if (eq calc-language 'big) 1 0)))
1016 (or entry (setq entry (list val 1 nil)))
1018 (setq str (let ((calc-line-numbering nil))
1019 (math-format-stack-value entry)))
1020 (if (eq calc-language 'big)
1021 (setq str (substring str 0 -1)))))
1022 (and calc-show-plain
1023 (setq str (concat open-plain
1024 (math-showing-full-precision
1025 (math-format-flat-expr val 0))
1029 (calc-embedded-original-buffer t info)
1030 (or (equal str (aref info 6))
1031 (let ((delta (- (aref info 5) (aref info 3)))
1033 (buffer-read-only nil))
1034 (goto-char (aref info 2))
1035 (delete-region (point) (aref info 3))
1036 (and (> (nth 1 entry) (1+ extra))
1039 (delete-horizontal-space)
1040 (if (looking-at "\n")
1041 ;; If there's a newline there, don't add one
1044 (delete-horizontal-space)
1046 ; (setq delta (1+ delta))
1047 (backward-char 1))))
1049 (set-marker (aref info 3) (+ (point) adjbot))
1050 (set-marker (aref info 5) (+ (point) delta))
1051 (aset info 6 str))))))
1052 (if (eq (car-safe val) 'calcFunc-evalto)
1054 (setq evalled (nth 2 val)
1056 (if (eq (car-safe val) 'calcFunc-assign)
1058 (aset info 9 (nth 1 val))
1059 (aset info 11 (or evalled
1060 (let ((calc-embedded-info info))
1061 (math-evaluate-expr (nth 2 val)))))
1062 (or (equal old-eval (aref info 11))
1063 (calc-embedded-var-change (nth 1 val) (aref info 0))))
1064 (if (eq (car-safe old-val) 'calcFunc-evalto)
1065 (setq old-val (nth 1 old-val)))
1066 (if (eq (car-safe old-val) 'calcFunc-assign)
1067 (calc-embedded-var-change (nth 1 old-val) (aref info 0)))))
1068 (set-buffer (aref info 1))
1069 (while calc-embed-prev-modes
1070 (cond ((eq (car (car calc-embed-prev-modes)) 'the-language)
1072 (calc-embedded-set-language (cdr (car calc-embed-prev-modes)))))
1073 ((eq (car (car calc-embed-prev-modes)) 'the-display-just)
1075 (calc-embedded-set-justify (cdr (car calc-embed-prev-modes)))))
1077 (set (car (car calc-embed-prev-modes))
1078 (cdr (car calc-embed-prev-modes)))))
1079 (setq calc-embed-prev-modes (cdr calc-embed-prev-modes))))))
1084 ;;; These are hooks called by the main part of Calc.
1086 (defvar calc-embedded-no-reselect nil)
1087 (defun calc-embedded-select-buffer ()
1088 (if (eq (current-buffer) (aref calc-embedded-info 0))
1089 (let ((info calc-embedded-info)
1091 (if (and (or (< (point) (aref info 4))
1092 (> (point) (aref info 5)))
1093 (not calc-embedded-no-reselect))
1094 (let ((calc-embedded-quiet t))
1095 (message "(Switching Calc Embedded mode to new formula.)")
1097 (calc-embedded nil)))
1098 (setq horiz (max (min (current-column) (- (point) (aref info 2))) 0)
1099 vert (if (<= (aref info 2) (point))
1100 (- (count-lines (aref info 2) (point))
1103 (set-buffer (aref info 1))
1107 (setq vert (1- vert))))
1108 (calc-cursor-stack-index 1)
1109 (if calc-line-numbering
1110 (setq horiz (+ horiz 4)))
1112 (forward-line vert))
1113 (forward-char (min horiz
1114 (- (point-max) (point)))))
1115 (calc-select-buffer)))
1117 (defun calc-embedded-finish-command ()
1118 (let ((buf (current-buffer))
1121 (set-buffer (aref calc-embedded-info 1))
1122 (if (> (calc-stack-size) 0)
1124 (col (current-column))
1126 (calc-cursor-stack-index 0)
1129 (calc-cursor-stack-index 1)
1132 (setq horiz (- col (if calc-line-numbering 4 0))
1133 vert (- (count-lines (point) pt)
1136 (setq vert (max 1 (1+ vert))))))))
1140 (set-buffer (aref calc-embedded-info 0))
1141 (goto-char (aref calc-embedded-info 2))
1143 (forward-line vert))
1144 (forward-char (max horiz 0))
1145 (set-buffer buf)))))
1147 (defun calc-embedded-stack-change ()
1148 (or calc-executing-macro
1150 (set-buffer (aref calc-embedded-info 1))
1151 (let* ((info calc-embedded-info)
1152 (extra-line (if (eq calc-language 'big) 1 0))
1154 (empty (= (calc-stack-size) 0))
1156 (list '(var empty var-empty) 1 nil)
1157 (calc-top 1 'entry)))
1158 (old-val (aref info 8))
1163 (calc-cursor-stack-index 1)
1165 (calc-cursor-stack-index 0)
1166 (setq bot (- (point) extra-line))
1167 (setq str (buffer-substring top (- bot 1))))
1168 (if calc-line-numbering
1170 (setq str (substring str 4))
1171 (while (setq pos (string-match "\n...." str pos))
1172 (setq str (concat (substring str 0 (1+ pos))
1173 (substring str (+ pos 5)))
1175 (calc-embedded-original-buffer t)
1176 (aset info 8 (car entry))
1177 (calc-embedded-update info 13 nil t str entry old-val)))))
1179 (defun calc-embedded-mode-line-change ()
1180 (let ((str mode-line-buffer-identification))
1182 (calc-embedded-original-buffer t)
1183 (setq mode-line-buffer-identification str)
1184 (set-buffer-modified-p (buffer-modified-p)))))
1186 (defun calc-embedded-modes-change (vars)
1187 (if (eq (car vars) 'calc-language) (setq vars '(the-language)))
1188 (if (eq (car vars) 'calc-display-just) (setq vars '(the-display-just)))
1190 (not (rassq (car vars) calc-embedded-mode-vars)))
1191 (setq vars (cdr vars)))
1192 (if (and vars calc-mode-save-mode (not (eq calc-mode-save-mode 'save)))
1194 (let* ((save-mode calc-mode-save-mode)
1195 (header (if (eq save-mode 'local)
1197 (format "calc-%s-mode:" save-mode)))
1198 (the-language (calc-embedded-language))
1199 (the-display-just (calc-embedded-justify))
1200 (values (mapcar 'symbol-value vars))
1201 (num (cond ((eq save-mode 'local) 12)
1202 ((eq save-mode 'edit) 13)
1203 ((eq save-mode 'perm) 14)
1205 base limit mname mlist)
1206 (calc-embedded-original-buffer t)
1208 (if (eq save-mode 'global)
1209 (setq base (point-max)
1211 mlist calc-embedded-globals)
1212 (goto-char (aref calc-embedded-info 4))
1215 limit (max (- (point) 1000) (point-min))
1216 mlist (and num (aref calc-embedded-info num)))
1217 (and (re-search-backward
1218 (format "\\(%s\\)[^\001]*\\(%s\\)\\|\\[calc-defaults]"
1219 calc-embedded-open-formula
1220 calc-embedded-close-formula) limit t)
1221 (setq limit (point))))
1224 (if (setq mname (car (rassq (car vars)
1225 calc-embedded-mode-vars)))
1226 (let ((buffer-read-only nil)
1227 (found (assq (car vars) mlist)))
1229 (setcdr found (car values))
1230 (setq mlist (cons (cons (car vars) (car values)) mlist))
1232 (aset calc-embedded-info num mlist)
1233 (if (eq save-mode 'global)
1234 (setq calc-embedded-globals mlist))))
1235 (if (re-search-backward
1236 (format "\\[%s *%s: *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]"
1240 (goto-char (match-beginning 1))
1241 (delete-region (point) (match-end 1))
1242 (insert (prin1-to-string (car values))))
1244 (insert-before-markers
1245 calc-embedded-open-mode
1246 "[" header " " mname ": "
1247 (prin1-to-string (car values)) "]"
1248 calc-embedded-close-mode))))
1249 (setq vars (cdr vars)
1250 values (cdr values))))))
1251 (when (and vars (eq calc-mode-save-mode 'save))
1252 (calc-embedded-save-original-modes))))
1254 (defun calc-embedded-var-change (var &optional buf)
1256 (setq var (list 'var
1257 (if (string-match "\\`var-.+\\'"
1259 (intern (substring (symbol-name var) 4))
1263 (let ((manual (not calc-auto-recompute))
1264 (bp calc-embedded-active)
1266 (if buf (setq bp (memq (assq buf bp) bp)))
1268 (let ((calc-embedded-no-reselect t)
1269 (p (and (buffer-name (car (car bp)))
1272 (if (assoc var (aref (car p) 10))
1274 (if (aref (car p) 11)
1276 (aset (car p) 11 nil)
1277 (if (aref (car p) 9)
1278 (calc-embedded-var-change (aref (car p) 9)))))
1279 (set-buffer (aref (car p) 0))
1280 (if (equal (buffer-substring (aref (car p) 2)
1283 (let ((calc-embedded-info nil))
1284 (or calc-embedded-quiet
1285 (message "Recomputing..."))
1288 (set-buffer (aref (car p) 0))
1289 (calc-embedded-update (car p) 14 t nil)))
1290 (setcdr (car bp) (delq (car p) (cdr (car bp))))
1292 "(Tried to recompute but formula was changed or missing)"))))
1294 (setq bp (if buf nil (cdr bp))))
1295 (or first calc-embedded-quiet (message "")))))
1297 (provide 'calc-embed)
1299 ;;; arch-tag: 1b8f311e-fba1-40d3-b8c3-1d6f68fd26fc
1300 ;;; calc-embed.el ends here