calc-embed.el 50 KB

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