1 ;; Calculator for GNU Emacs, part II [calc-embed.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
5 ;; This file is part of GNU Emacs.
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.
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.
24 ;; This file is autoloaded from calc-ext.el.
29 (defun calc-Need-calc-embed () nil)
32 (defun calc-show-plain (n)
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."
38 "Omitting \"plain\" formulas in Calc Embedded mode."))))
43 ;;; Things to do for Embedded Mode:
45 ;;; Detect and strip off unexpected labels during reading.
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.
55 (defvar calc-embedded-modes nil)
56 (defvar calc-embedded-globals nil)
57 (defvar calc-embedded-active nil)
59 (make-variable-buffer-local 'calc-embedded-all-active)
60 (make-variable-buffer-local 'calc-embedded-some-active)
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
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
71 (defvar calc-embedded-open-word "^\\|[^-+0-9.eE]"
72 "*A regular expression for the opening delimiter of a formula used by
75 (defvar calc-embedded-close-word "$\\|[^-+0-9.eE]"
76 "*A regular expression for the closing delimiter of a formula used by
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
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.")
88 (defvar calc-embedded-open-new-formula "\n\n"
89 "*A string which is inserted at front of formula by calc-embedded-new-formula.")
91 (defvar calc-embedded-close-new-formula "\n\n"
92 "*A string which is inserted at end of formula by calc-embedded-new-formula.")
94 (defvar calc-embedded-announce-formula "%Embed\n\\(% .*\n\\)*"
95 "*A regular expression which is sure to be followed by a calc-embedded formula." )
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.")
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.")
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)
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.
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.
165 (defun calc-do-embedded (arg end obeg oend)
166 (if calc-embedded-info
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)
174 (switch-to-buffer calcbuf)))
176 ((eq (current-buffer) (aref calc-embedded-info 0))
177 (let* ((info calc-embedded-info)
178 (mode calc-embedded-modes))
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)
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))))
201 (if (buffer-name (aref calc-embedded-info 0))
203 (set-buffer (aref calc-embedded-info 0))
204 (or (y-or-n-p (format "Cancel Calc Embedded mode in buffer %s? "
207 (calc-embedded nil)))
208 (calc-embedded arg end obeg oend)))
210 ;; Turn embedded mode on.
211 (calc-plain-buffer-only)
212 (let ((modes (list mode-line-buffer-identification
215 top bot outer-top outer-bot
217 (barf-if-buffer-read-only)
218 (or calc-embedded-globals
220 (setq info (calc-embedded-make-info (point) nil t arg end obeg oend))
221 (if (eq (car-safe (aref info 8)) 'error)
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))
230 (calc-no-refresh-evaltos t))
231 (setq chg (calc-embedded-set-modes
232 (aref info 15) (aref info 12) (aref info 13)))
234 (calc-push (calc-normalize (aref info 8)))
236 (setq calc-undo-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
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
252 "Give this command again")))))
253 (scroll-down 0)) ; fix a bug which occurs when truncate-lines is changed.
254 (setq calc-embedded-quiet nil)
257 (defun calc-embedded-select (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)))
268 (calc-select-part 2)))
271 (defun calc-embedded-update-formula (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))))
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)))
288 (info (calc-embedded-make-info (point) nil t))
289 (pt (- opt (aref info 4))))
290 (or (eq (car-safe (aref info 8)) 'error)
293 (calc-embedded-update info 14 'eval t))
294 (goto-char (+ (aref info 4) pt))))))))
297 (defun calc-embedded-edit (arg)
299 (let ((info (calc-embedded-make-info (point) nil t arg))
301 (if (eq (car-safe (aref info 8)) 'error)
303 (goto-char (nth 1 (aref info 8)))
304 (error (nth 2 (aref info 8)))))
306 (setq str (math-showing-full-precision
307 (math-format-nice-expr (aref info 8) (frame-width))))
308 (calc-edit-mode (list 'calc-embedded-finish-edit info))
310 (calc-show-edit-buffer))
312 (defun calc-embedded-finish-edit (info)
313 (let ((buf (current-buffer))
314 (str (buffer-substring (point) (point-max)))
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)
325 (switch-to-buffer buf)
326 (goto-char (+ start (nth 1 val)))
327 (error (nth 2 val))))
328 (calc-embedded-original-buffer t info)
330 (calc-embedded-update info 14 t t))))
332 (defun calc-do-embedded-activate (arg cbuf)
333 (calc-plain-buffer-only)
335 (calc-embedded-forget))
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))
341 (let* ((active (assq (current-buffer) calc-embedded-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))
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)))
359 (calc-embedded-active-state t))
361 (defun calc-plain-buffer-only ()
362 (if (memq major-mode '(calc-mode calc-trail-mode calc-edit-mode))
363 (error "This command should be used in a normal editing buffer")))
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")
371 (let ((active (assq (current-buffer) calc-embedded-active)))
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))))
377 (set-buffer-modified-p (buffer-modified-p)))
380 (defun calc-embedded-original-buffer (switch &optional info)
381 (or info (setq info calc-embedded-info))
382 (or (buffer-name (aref info 0))
384 (error "Calc embedded mode: Original buffer has been killed")))
386 (set-buffer (aref info 0))))
388 (defun calc-embedded-word ()
390 (calc-embedded '(4)))
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."
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)
401 (calc-embedded-find-bounds body-only))
402 (push-mark (if body-only bot outer-bot) t)
403 (goto-char (if body-only top outer-top))))
405 (defun calc-embedded-find-bounds (&optional plain)
406 ;; (while (and (bolp) (eq (following-char) ?\n))
408 (and (eolp) (bolp) (not (eq (char-after (- (point) 2)) ?\n))
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.
418 (setq outer-top (point))
419 (goto-char (match-end 0))
420 (if (eq (following-char) ?\n)
423 (while (eq (following-char) ?\ )
425 (or (eq plain 'plain)
426 (if (looking-at (regexp-quote calc-embedded-open-plain))
428 (goto-char (match-end 0))
429 (search-forward calc-embedded-close-plain))))
431 (or (re-search-forward calc-embedded-close-formula nil t)
432 (error "Can't find end of formula"))
434 (error "Not inside a formula"))
435 (and (eq (following-char) ?\n) (not (bolp))
437 (setq outer-bot (point))
438 (goto-char (match-beginning 0))
439 (if (eq (preceding-char) ?\n)
442 (while (eq (preceding-char) ?\ )
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."
452 (and calc-embedded-info
454 (calc-embedded-mark-formula)
455 (kill-region (point) (mark))
458 (defun calc-embedded-copy-formula-as-kill ()
459 "Save the formula surrounding point as if killed, but don't kill it."
462 (calc-embedded-mark-formula)
463 (copy-region-as-kill (point) (mark))
466 (defun calc-embedded-duplicate ()
468 (let ((already calc-embedded-info)
469 top bot outer-top outer-bot new-top)
470 (if calc-embedded-info
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)))
477 (calc-embedded-find-bounds))
478 (goto-char outer-bot)
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))
487 (+ new-top (- outer-bot outer-top))))))
489 (defun calc-embedded-next (arg)
491 (setq arg (prefix-numeric-value arg))
492 (let* ((active (cdr (assq (current-buffer) calc-embedded-active)))
494 (num (length active)))
496 (error "No active formulas in buffer"))
499 (if (<= (point) (aref (car active) 3))
500 (goto-char (aref (nth (1- num) active) 2))
502 (> (point) (aref (nth 1 p) 3)))
504 (goto-char (aref (car p) 2))))
506 (calc-embedded-next -1)
507 (calc-embedded-next (+ (* num 1000) arg 1)))
509 (setq arg (1+ (% (1- arg) num)))
510 (while (and p (>= (point) (aref (car p) 2)))
512 (while (> (setq arg (1- arg)) 0)
513 (setq p (if p (cdr p) (cdr active))))
514 (goto-char (aref (car (or p active)) 2))))))
516 (defun calc-embedded-previous (arg)
518 (calc-embedded-next (- (prefix-numeric-value arg))))
520 (defun calc-embedded-new-formula ()
522 (and (eq major-mode 'calc-mode)
523 (error "This command should be used in a normal editing buffer"))
524 (if calc-embedded-info
526 (let (top bot outer-top outer-bot)
527 (if (and (eq (preceding-char) ?\n)
528 (string-match "\\`\n" calc-embedded-open-new-formula))
530 (setq outer-top (1- (point)))
532 (insert (substring calc-embedded-open-new-formula 1)))
533 (setq outer-top (point))
534 (insert calc-embedded-open-new-formula))
538 (insert calc-embedded-close-new-formula)
539 (if (and (eq (following-char) ?\n)
540 (string-match "\n\\'" calc-embedded-close-new-formula))
542 (setq outer-bot (point))
544 (let ((calc-embedded-quiet 'x))
545 (calc-embedded top bot outer-top outer-bot))))
547 (defun calc-embedded-forget ()
549 (setq calc-embedded-active (delq (assq (current-buffer) calc-embedded-active)
550 calc-embedded-active))
551 (calc-embedded-active-state nil))
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))
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)))
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))
572 (if temp (setq prev-modes (cons (cons (car (car v))
573 (symbol-value (car (car v))))
575 (set (car (car v)) value)))
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)
585 (if temp (setq prev-modes (cons (cons (car (car v))
586 (symbol-value (car (car v))))
588 (set (car (car v)) value)))
592 (or (eq (setq value (cdr (car v))) 'default)
593 (equal (symbol-value (car (car v))) value)
596 (if temp (setq prev-modes (cons (cons (car (car v))
597 (symbol-value (car (car v))))
599 (set (car (car v)) value)))
601 (and changed (not (eq temp t))
603 (calc-embedded-set-justify the-display-just)
604 (calc-embedded-set-language the-language)))
605 (and changed (not temp)
607 (setq calc-full-float-format (list (if (eq (car calc-float-format)
610 (car calc-float-format))
615 (defun calc-embedded-language ()
616 (if calc-language-option
617 (list calc-language calc-language-option)
620 (defun calc-embedded-set-language (lang)
623 (setq option (nth 1 lang)
625 (or (and (eq lang calc-language)
626 (equal option calc-language-option))
627 (calc-set-language lang option t))))
629 (defun calc-embedded-justify ()
630 (if calc-display-origin
631 (list calc-display-just calc-display-origin)
634 (defun calc-embedded-set-justify (just)
636 (setq calc-display-origin (nth 1 just)
637 calc-display-just (car just))
638 (setq calc-display-just just
639 calc-display-origin nil)))
642 (defun calc-find-globals ()
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)
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)
655 calc-embedded-mode-vars))
656 (or (assq (cdr found) modes)
657 (setq modes (cons (cons (cdr found)
658 (car (read-from-string
663 (setq calc-embedded-globals (cons t modes))
664 (goto-char save-pt)))
666 (defun calc-embedded-find-modes ()
667 (let ((case-fold-search nil)
674 (while (and no-defaults (search-backward "[calc-" nil t))
676 (or (and (looking-at "mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
677 (setq found (assoc (buffer-substring (match-beginning 1)
679 calc-embedded-mode-vars))
680 (or (assq (cdr found) modes)
681 (setq modes (cons (cons (cdr found)
682 (car (read-from-string
687 (and (looking-at "perm-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
688 (setq found (assoc (buffer-substring (match-beginning 1)
690 calc-embedded-mode-vars))
691 (or (assq (cdr found) pmodes)
692 (setq pmodes (cons (cons (cdr found)
693 (car (read-from-string
698 (and (looking-at "edit-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
699 (setq found (assoc (buffer-substring (match-beginning 1)
701 calc-embedded-mode-vars))
702 (or (assq (cdr found) emodes)
703 (setq emodes (cons (cons (cdr found)
704 (car (read-from-string
709 (and (looking-at "defaults]")
710 (setq no-defaults nil)))
713 (list modes emodes pmodes)))
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))
720 (force (and fresh top))
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)))
731 (>= point (aref (nth 1 found) 2)))
732 (setq info (nth 1 found))
733 (setq info (make-vector 16 nil)
736 (aset info 0 (current-buffer))
737 (aset info 1 (or cbuf (save-excursion
740 (if (and (integerp top) (not bot)) ; started with a user-supplied argument
742 (if (= (setq arg (prefix-numeric-value arg)) 0)
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))
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))))
752 (setq top (aref info 2)
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))
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))
767 (or (bolp) (aset info 7 t))
768 (goto-char (aref info 3))
769 (or (bolp) (eolp) (aset info 7 t))))
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)))
778 (not (equal str (aref info 6))))
779 (if (and fixed (aref info 6))
782 (calc-embedded-make-info point cbuf nil)
784 (let* ((open-plain calc-embedded-open-plain)
785 (close-plain calc-embedded-close-plain)
786 (pref-len (length open-plain))
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)
797 (setq val (math-read-plain-expr
798 (substring str pref-len suff-pos)))
799 (if (string-match "[^ \t\n]" str)
801 val (math-read-big-expr str))
803 (if (eq (car-safe val) 'error)
804 (setq val (list 'error
805 (+ (aref info 2) pref-len (nth 1 val))
810 (if (eq (car-safe temp) 'calcFunc-evalto)
811 (setq temp (nth 1 temp))
812 (if (eq (car-safe temp) 'error)
815 (setcdr found (delq info (cdr found)))
816 (calc-embedded-active-state 'less))))
817 (aset info 9 (and (eq (car-safe temp) 'calcFunc-assign)
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))))
825 (setcdr found (cons info (cdr found)))
826 (calc-embedded-active-state 'more)))
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)))
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))
845 (calc-embedded-find-vars (car x))))))
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))
852 (let ((active (assq (aref calc-embedded-info 0) calc-embedded-active))
855 (calc-embedded-original-buffer t)
858 (calc-embedded-activate)
859 (setq active (assq (aref calc-embedded-info 0)
860 calc-embedded-active))))
862 (calc-embedded-eval-get-var (car (car vars-used)) active)
863 (setq vars-used (cdr vars-used))))
864 (calc-embedded-subst x))
865 (calc-normalize (math-evaluate-expr-rec x)))))
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
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
876 (calc-embedded-subst (nth 2 x)))
877 (calc-normalize (math-evaluate-expr-rec (math-multi-subst-rec x))))))
879 (defun calc-embedded-eval-get-var (var base)
881 (point (aref calc-embedded-info 2))
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)))))
892 (setq entry (car entry))
893 (if (equal (buffer-substring (aref entry 2) (aref entry 3))
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)
906 (calc-embedded-eval-get-var var base))))))
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)
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))
923 (set-buffer (aref info 1))
925 (calc-embedded-set-modes (aref info 15) (aref info 12)
927 (if need-display 'full t)))
928 (if (memq (car-safe val) '(calcFunc-evalto calcFunc-assign))
929 (calc-embedded-find-vars val))
931 (let ((calc-embedded-info info))
932 (setq val (math-evaluate-expr val)
934 (if (or (eq need-eval 'eval) (eq (car-safe val) 'calcFunc-evalto))
937 (aset info 10 vars-used)
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)))
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)))))
948 (setq str (concat open-plain
949 (math-showing-full-precision
950 (math-format-flat-expr val 0))
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))
964 (delete-horizontal-space)
966 (delete-horizontal-space)
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)
974 (setq evalled (nth 2 val)
976 (if (eq (car-safe val) 'calcFunc-assign)
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))
990 (cond ((eq (car (car prev-modes)) 'the-language)
992 (calc-embedded-set-language (cdr (car prev-modes)))))
993 ((eq (car (car prev-modes)) 'the-display-just)
995 (calc-embedded-set-justify (cdr (car prev-modes)))))
997 (set (car (car prev-modes)) (cdr (car prev-modes)))))
998 (setq prev-modes (cdr prev-modes))))))
1003 ;;; These are hooks called by the main part of Calc.
1005 (defun calc-embedded-select-buffer ()
1006 (if (eq (current-buffer) (aref calc-embedded-info 0))
1007 (let ((info calc-embedded-info)
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.)")
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))
1021 (set-buffer (aref info 1))
1025 (setq vert (1- vert))))
1026 (calc-cursor-stack-index 1)
1027 (if calc-line-numbering
1028 (setq horiz (+ horiz 4)))
1030 (forward-line vert))
1031 (forward-char (min horiz
1032 (- (point-max) (point)))))
1033 (calc-select-buffer)))
1034 (setq calc-embedded-no-reselect nil)
1036 (defun calc-embedded-finish-command ()
1037 (let ((buf (current-buffer))
1040 (set-buffer (aref calc-embedded-info 1))
1041 (if (> (calc-stack-size) 0)
1043 (col (current-column))
1045 (calc-cursor-stack-index 0)
1048 (calc-cursor-stack-index 1)
1051 (setq horiz (- col (if calc-line-numbering 4 0))
1052 vert (- (count-lines (point) pt)
1055 (setq vert (max 1 (1+ vert))))))))
1059 (set-buffer (aref calc-embedded-info 0))
1060 (goto-char (aref calc-embedded-info 2))
1062 (forward-line vert))
1063 (forward-char (max horiz 0))
1064 (set-buffer buf)))))
1066 (defun calc-embedded-stack-change ()
1067 (or calc-executing-macro
1069 (set-buffer (aref calc-embedded-info 1))
1070 (let* ((info calc-embedded-info)
1071 (extra-line (if (eq calc-language 'big) 1 0))
1073 (empty (= (calc-stack-size) 0))
1075 (list '(var empty var-empty) 1 nil)
1076 (calc-top 1 'entry)))
1077 (old-val (aref info 8))
1082 (calc-cursor-stack-index 1)
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
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)))
1094 (calc-embedded-original-buffer t)
1095 (aset info 8 (car entry))
1096 (calc-embedded-update info 13 nil t str entry old-val)))))
1098 (defun calc-embedded-mode-line-change ()
1099 (let ((str mode-line-buffer-identification))
1101 (calc-embedded-original-buffer t)
1102 (setq mode-line-buffer-identification str)
1103 (set-buffer-modified-p (buffer-modified-p)))))
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)))
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)))
1113 (let* ((save-mode calc-mode-save-mode)
1114 (header (if (eq save-mode 'local)
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)
1124 base limit mname mlist)
1125 (calc-embedded-original-buffer t)
1127 (if (eq save-mode 'global)
1128 (setq base (point-max)
1130 mlist calc-embedded-globals)
1131 (goto-char (aref calc-embedded-info 4))
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))))
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)))
1148 (setcdr found (car values))
1149 (setq mlist (cons (cons (car vars) (car values)) mlist))
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]+\\)]"
1159 (goto-char (match-beginning 1))
1160 (delete-region (point) (match-end 1))
1161 (insert (prin1-to-string (car values))))
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)
1169 values (cdr values))))))))
1171 (defun calc-embedded-var-change (var &optional buf)
1173 (setq var (list 'var
1174 (if (string-match "\\`var-.+\\'"
1176 (intern (substring (symbol-name var) 4))
1180 (let ((manual (not calc-auto-recompute))
1181 (bp calc-embedded-active)
1183 (if buf (setq bp (memq (assq buf bp) bp)))
1185 (let ((calc-embedded-no-reselect t)
1186 (p (and (buffer-name (car (car bp)))
1189 (if (assoc var (aref (car p) 10))
1191 (if (aref (car p) 11)
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)
1200 (let ((calc-embedded-info nil))
1201 (or calc-embedded-quiet
1202 (message "Recomputing..."))
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))))
1209 "(Tried to recompute but formula was changed or missing.)"))))
1211 (setq bp (if buf nil (cdr bp))))
1212 (or first calc-embedded-quiet (message "")))))
1214 ;;; calc-embed.el ends here