calc-embed.el 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. ;;; calc-embed.el --- embed Calc in a buffer
  2. ;; Copyright (C) 1990-1993, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: David Gillespie <daveg@synaptics.com>
  4. ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;; This file is autoloaded from calc-ext.el.
  19. (require 'calc-ext)
  20. (require 'calc-macs)
  21. ;; Declare functions which are defined elsewhere.
  22. (declare-function thing-at-point-looking-at "thingatpt" (regexp))
  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 "Embedded Calc mode enabled; %s to return to normal"
  296. (if calc-embedded-quiet
  297. "Type `C-x * x'"
  298. "Give this command again")))))
  299. (scroll-down 0)) ; fix a bug which occurs when truncate-lines is changed.
  300. (defun calc-embedded-select (arg)
  301. (interactive "P")
  302. (calc-embedded arg)
  303. (and calc-embedded-info
  304. (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-evalto)
  305. (calc-select-part 1))
  306. (and calc-embedded-info
  307. (or (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-assign)
  308. (and (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-evalto)
  309. (eq (car-safe (nth 1 (aref calc-embedded-info 8)))
  310. 'calcFunc-assign)))
  311. (calc-select-part 2)))
  312. (defun calc-embedded-update-formula (calc-embed-arg)
  313. (interactive "P")
  314. (if calc-embed-arg
  315. (let ((entry (assq (current-buffer) calc-embedded-active)))
  316. (while (setq entry (cdr entry))
  317. (and (eq (car-safe (aref (car entry) 8)) 'calcFunc-evalto)
  318. (or (not (consp calc-embed-arg))
  319. (and (<= (aref (car entry) 2) (region-beginning))
  320. (>= (aref (car entry) 3) (region-end))))
  321. (save-excursion
  322. (calc-embedded-update (car entry) 14 t t)))))
  323. (if (and calc-embedded-info
  324. (eq (current-buffer) (aref calc-embedded-info 0))
  325. (>= (point) (aref calc-embedded-info 4))
  326. (<= (point) (aref calc-embedded-info 5)))
  327. (calc-evaluate 1)
  328. (let* ((opt (point))
  329. (info (calc-embedded-make-info (point) nil t))
  330. (pt (- opt (aref info 4))))
  331. (or (eq (car-safe (aref info 8)) 'error)
  332. (progn
  333. (save-excursion
  334. (calc-embedded-update info 14 'eval t))
  335. (goto-char (+ (aref info 4) pt))))))))
  336. (defun calc-embedded-edit (calc-embed-arg)
  337. (interactive "P")
  338. (let ((info (calc-embedded-make-info (point) nil t calc-embed-arg))
  339. str)
  340. (if (eq (car-safe (aref info 8)) 'error)
  341. (progn
  342. (goto-char (nth 1 (aref info 8)))
  343. (error (nth 2 (aref info 8)))))
  344. (calc-wrapper
  345. (setq str (math-showing-full-precision
  346. (math-format-nice-expr (aref info 8) (frame-width))))
  347. (calc-edit-mode (list 'calc-embedded-finish-edit info))
  348. (insert str "\n")))
  349. (calc-show-edit-buffer))
  350. (defvar calc-original-buffer)
  351. (defvar calc-edit-top)
  352. (defun calc-embedded-finish-edit (info)
  353. (let ((buf (current-buffer))
  354. (str (buffer-substring calc-edit-top (point-max)))
  355. (start (point))
  356. pos)
  357. (switch-to-buffer calc-original-buffer)
  358. (let ((val (with-current-buffer (aref info 1)
  359. (let ((calc-language nil)
  360. (math-expr-opers (math-standard-ops)))
  361. (math-read-expr str)))))
  362. (if (eq (car-safe val) 'error)
  363. (progn
  364. (switch-to-buffer buf)
  365. (goto-char (+ start (nth 1 val)))
  366. (error (nth 2 val))))
  367. (calc-embedded-original-buffer t info)
  368. (aset info 8 val)
  369. (calc-embedded-update info 14 t t))))
  370. ;;;###autoload
  371. (defun calc-do-embedded-activate (calc-embed-arg cbuf)
  372. (calc-plain-buffer-only)
  373. (if calc-embed-arg
  374. (calc-embedded-forget))
  375. (calc-find-globals)
  376. (if (< (prefix-numeric-value calc-embed-arg) 0)
  377. (message "Deactivating %s for Calc Embedded mode" (buffer-name))
  378. (message "Activating %s for Calc Embedded mode..." (buffer-name))
  379. (save-excursion
  380. (let* ((active (assq (current-buffer) calc-embedded-active))
  381. (info active)
  382. (pat " := \\| \\\\gets \\| => \\| \\\\evalto "))
  383. (if calc-embedded-announce-formula
  384. (setq pat (format "%s\\|\\(%s\\)"
  385. pat calc-embedded-announce-formula)))
  386. (while (setq info (cdr info))
  387. (or (equal (buffer-substring (aref (car info) 2) (aref (car info) 3))
  388. (aref (car info) 6))
  389. (setcdr active (delq (car info) (cdr active)))))
  390. (goto-char (point-min))
  391. (while (re-search-forward pat nil t)
  392. ;;; (if (looking-at calc-embedded-open-formula)
  393. ;;; (goto-char (match-end 1)))
  394. (setq info (calc-embedded-make-info (point) cbuf nil))
  395. (or (eq (car-safe (aref info 8)) 'error)
  396. (goto-char (aref info 5))))))
  397. (message "Activating %s for Calc Embedded mode...done" (buffer-name)))
  398. (calc-embedded-active-state t))
  399. (defun calc-plain-buffer-only ()
  400. (if (memq major-mode '(calc-mode calc-trail-mode calc-edit-mode))
  401. (error "This command should be used in a normal editing buffer")))
  402. (defun calc-embedded-active-state (state)
  403. (or (assq 'calc-embedded-all-active minor-mode-alist)
  404. (setq minor-mode-alist
  405. (cons '(calc-embedded-all-active " Active")
  406. (cons '(calc-embedded-some-active " ~Active")
  407. minor-mode-alist))))
  408. (let ((active (assq (current-buffer) calc-embedded-active)))
  409. (or (cdr active)
  410. (setq state nil)))
  411. (and (eq state 'more) calc-embedded-all-active (setq state t))
  412. (setq calc-embedded-all-active (eq state t)
  413. calc-embedded-some-active (not (memq state '(nil t))))
  414. (set-buffer-modified-p (buffer-modified-p)))
  415. (defun calc-embedded-original-buffer (switch &optional info)
  416. (or info (setq info calc-embedded-info))
  417. (or (buffer-name (aref info 0))
  418. (progn
  419. (error "Calc embedded mode: Original buffer has been killed")))
  420. (if switch
  421. (set-buffer (aref info 0))))
  422. (defun calc-embedded-word ()
  423. (interactive)
  424. (calc-embedded '(t)))
  425. (defun calc-embedded-mark-formula (&optional body-only)
  426. "Put point at the beginning of this Calc formula, mark at the end.
  427. This normally marks the whole formula, including surrounding delimiters.
  428. With any prefix argument, marks only the formula itself."
  429. (interactive "P")
  430. (and (eq major-mode 'calc-mode)
  431. (error "This command should be used in a normal editing buffer"))
  432. (let (calc-embed-top calc-embed-bot calc-embed-outer-top calc-embed-outer-bot)
  433. (save-excursion
  434. (calc-embedded-find-bounds body-only))
  435. (push-mark (if body-only calc-embed-bot calc-embed-outer-bot) t)
  436. (goto-char (if body-only calc-embed-top calc-embed-outer-top))))
  437. (defun calc-embedded-find-bounds (&optional plain)
  438. ;; (while (and (bolp) (eq (following-char) ?\n))
  439. ;; (forward-char 1))
  440. (and (eolp) (bolp) (not (eq (char-after (- (point) 2)) ?\n))
  441. (forward-char -1))
  442. (let ((home (point)))
  443. (or (and (looking-at calc-embedded-open-formula)
  444. (not (looking-at calc-embedded-close-formula)))
  445. (re-search-backward calc-embedded-open-formula nil t)
  446. (error "Can't find start of formula"))
  447. (and (eq (preceding-char) ?\$) ; backward search for \$\$? won't back
  448. (eq (following-char) ?\$) ; up over a second $, so do it by hand.
  449. (forward-char -1))
  450. (setq calc-embed-outer-top (point))
  451. (goto-char (match-end 0))
  452. (if (looking-at "[ \t]*$")
  453. (end-of-line))
  454. (if (eq (following-char) ?\n)
  455. (forward-char 1))
  456. (or (bolp)
  457. (while (eq (following-char) ?\ )
  458. (forward-char 1)))
  459. (or (eq plain 'plain)
  460. (if (looking-at (regexp-quote calc-embedded-open-plain))
  461. (progn
  462. (goto-char (match-end 0))
  463. (search-forward calc-embedded-close-plain))))
  464. (setq calc-embed-top (point))
  465. (or (re-search-forward calc-embedded-close-formula nil t)
  466. (error "Can't find end of formula"))
  467. (if (< (point) home)
  468. (error "Not inside a formula"))
  469. (and (eq (following-char) ?\n) (not (bolp))
  470. (forward-char 1))
  471. (setq calc-embed-outer-bot (point))
  472. (goto-char (match-beginning 0))
  473. (if (eq (preceding-char) ?\n)
  474. (backward-char 1))
  475. (or (eolp)
  476. (while (eq (preceding-char) ?\ )
  477. (backward-char 1)))
  478. (setq calc-embed-bot (point))))
  479. (defun calc-embedded-kill-formula ()
  480. "Kill the formula surrounding point.
  481. If Calc Embedded mode was active, this deactivates it.
  482. The formula (including its surrounding delimiters) is saved in the kill ring.
  483. The command \\[yank] can retrieve it from there."
  484. (interactive)
  485. (and calc-embedded-info
  486. (calc-embedded nil))
  487. (calc-embedded-mark-formula)
  488. (kill-region (point) (mark))
  489. (pop-mark))
  490. (defun calc-embedded-copy-formula-as-kill ()
  491. "Save the formula surrounding point as if killed, but don't kill it."
  492. (interactive)
  493. (save-excursion
  494. (calc-embedded-mark-formula)
  495. (copy-region-as-kill (point) (mark))
  496. (pop-mark)))
  497. (defun calc-embedded-duplicate ()
  498. (interactive)
  499. (let ((already calc-embedded-info)
  500. calc-embed-top calc-embed-bot calc-embed-outer-top calc-embed-outer-bot new-top)
  501. (if calc-embedded-info
  502. (progn
  503. (setq calc-embed-top (+ (aref calc-embedded-info 2))
  504. calc-embed-bot (+ (aref calc-embedded-info 3))
  505. calc-embed-outer-top (+ (aref calc-embedded-info 4))
  506. calc-embed-outer-bot (+ (aref calc-embedded-info 5)))
  507. (calc-embedded nil))
  508. (calc-embedded-find-bounds))
  509. (goto-char calc-embed-outer-bot)
  510. (insert "\n")
  511. (setq new-top (point))
  512. (insert-buffer-substring (current-buffer)
  513. calc-embed-outer-top calc-embed-outer-bot)
  514. (goto-char (+ new-top (- calc-embed-top calc-embed-outer-top)))
  515. (let ((calc-embedded-quiet (if already t 'x)))
  516. (calc-embedded (+ new-top (- calc-embed-top calc-embed-outer-top))
  517. (+ new-top (- calc-embed-bot calc-embed-outer-top))
  518. new-top
  519. (+ new-top (- calc-embed-outer-bot calc-embed-outer-top))))))
  520. (defun calc-embedded-next (arg)
  521. (interactive "P")
  522. (setq arg (prefix-numeric-value arg))
  523. (let* ((active (cdr (assq (current-buffer) calc-embedded-active)))
  524. (p active)
  525. (num (length active)))
  526. (or active
  527. (error "No active formulas in buffer"))
  528. (cond ((= arg 0))
  529. ((= arg -1)
  530. (if (<= (point) (aref (car active) 3))
  531. (goto-char (aref (nth (1- num) active) 2))
  532. (while (and (cdr p)
  533. (> (point) (aref (nth 1 p) 3)))
  534. (setq p (cdr p)))
  535. (goto-char (aref (car p) 2))))
  536. ((< arg -1)
  537. (calc-embedded-next -1)
  538. (calc-embedded-next (+ (* num 1000) arg 1)))
  539. (t
  540. (setq arg (1+ (% (1- arg) num)))
  541. (while (and p (>= (point) (aref (car p) 2)))
  542. (setq p (cdr p)))
  543. (while (> (setq arg (1- arg)) 0)
  544. (setq p (if p (cdr p) (cdr active))))
  545. (goto-char (aref (car (or p active)) 2))))))
  546. (defun calc-embedded-previous (arg)
  547. (interactive "p")
  548. (calc-embedded-next (- (prefix-numeric-value arg))))
  549. (defun calc-embedded-new-formula ()
  550. (interactive)
  551. (and (eq major-mode 'calc-mode)
  552. (error "This command should be used in a normal editing buffer"))
  553. (if calc-embedded-info
  554. (calc-embedded nil))
  555. (let (calc-embed-top calc-embed-bot calc-embed-outer-top calc-embed-outer-bot)
  556. (if (and (eq (preceding-char) ?\n)
  557. (string-match "\\`\n" calc-embedded-open-new-formula))
  558. (progn
  559. (setq calc-embed-outer-top (1- (point)))
  560. (forward-char -1)
  561. (insert (substring calc-embedded-open-new-formula 1)))
  562. (setq calc-embed-outer-top (point))
  563. (insert calc-embedded-open-new-formula))
  564. (setq calc-embed-top (point))
  565. (insert " ")
  566. (setq calc-embed-bot (point))
  567. (insert calc-embedded-close-new-formula)
  568. (if (and (eq (following-char) ?\n)
  569. (string-match "\n\\'" calc-embedded-close-new-formula))
  570. (delete-char 1))
  571. (setq calc-embed-outer-bot (point))
  572. (goto-char calc-embed-top)
  573. (let ((calc-embedded-quiet 'x))
  574. (calc-embedded calc-embed-top calc-embed-bot calc-embed-outer-top calc-embed-outer-bot))))
  575. (defun calc-embedded-forget ()
  576. (interactive)
  577. (setq calc-embedded-active (delq (assq (current-buffer) calc-embedded-active)
  578. calc-embedded-active))
  579. (calc-embedded-active-state nil))
  580. ;; The variables calc-embed-prev-modes is local to calc-embedded-update,
  581. ;; but is used by calc-embedded-set-modes.
  582. (defvar calc-embed-prev-modes)
  583. (defun calc-embedded-set-modes (gmodes modes local-modes &optional temp)
  584. (let ((the-language (calc-embedded-language))
  585. (the-display-just (calc-embedded-justify))
  586. (v gmodes)
  587. (changed nil)
  588. found value)
  589. (while v
  590. (or (symbolp (car v))
  591. (and (setq found (assq (car (car v)) modes))
  592. (not (eq (cdr found) 'default)))
  593. (and (setq found (assq (car (car v)) local-modes))
  594. (not (eq (cdr found) 'default)))
  595. (progn
  596. (if (eq (setq value (cdr (car v))) 'default)
  597. (setq value (list (nth 1 (assq (car (car v)) calc-mode-var-list)))))
  598. (equal (symbol-value (car (car v))) value))
  599. (progn
  600. (setq changed t)
  601. (if temp (setq calc-embed-prev-modes
  602. (cons (cons (car (car v))
  603. (symbol-value (car (car v))))
  604. calc-embed-prev-modes)))
  605. (set (car (car v)) value)))
  606. (setq v (cdr v)))
  607. (setq v modes)
  608. (while v
  609. (or (and (setq found (assq (car (car v)) local-modes))
  610. (not (eq (cdr found) 'default)))
  611. (eq (setq value (cdr (car v))) 'default)
  612. (equal (symbol-value (car (car v))) value)
  613. (progn
  614. (setq changed t)
  615. (if temp (setq calc-embed-prev-modes (cons (cons (car (car v))
  616. (symbol-value (car (car v))))
  617. calc-embed-prev-modes)))
  618. (set (car (car v)) value)))
  619. (setq v (cdr v)))
  620. (setq v local-modes)
  621. (while v
  622. (or (eq (setq value (cdr (car v))) 'default)
  623. (equal (symbol-value (car (car v))) value)
  624. (progn
  625. (setq changed t)
  626. (if temp (setq calc-embed-prev-modes (cons (cons (car (car v))
  627. (symbol-value (car (car v))))
  628. calc-embed-prev-modes)))
  629. (set (car (car v)) value)))
  630. (setq v (cdr v)))
  631. (and changed (not (eq temp t))
  632. (progn
  633. (calc-embedded-set-justify the-display-just)
  634. (calc-embedded-set-language the-language)))
  635. (and changed (not temp)
  636. (progn
  637. (setq calc-full-float-format (list (if (eq (car calc-float-format)
  638. 'fix)
  639. 'float
  640. (car calc-float-format))
  641. 0))
  642. (calc-refresh)))
  643. changed))
  644. (defun calc-embedded-language ()
  645. (if calc-language-option
  646. (list calc-language calc-language-option)
  647. calc-language))
  648. (defun calc-embedded-set-language (lang)
  649. (let ((option nil))
  650. (if (consp lang)
  651. (setq option (nth 1 lang)
  652. lang (car lang)))
  653. (or (and (eq lang calc-language)
  654. (equal option calc-language-option))
  655. (calc-set-language lang option t))))
  656. (defun calc-embedded-justify ()
  657. (if calc-display-origin
  658. (list calc-display-just calc-display-origin)
  659. calc-display-just))
  660. (defun calc-embedded-set-justify (just)
  661. (if (consp just)
  662. (setq calc-display-origin (nth 1 just)
  663. calc-display-just (car just))
  664. (setq calc-display-just just
  665. calc-display-origin nil)))
  666. (defun calc-find-globals ()
  667. (interactive)
  668. (and (eq major-mode 'calc-mode)
  669. (error "This command should be used in a normal editing buffer"))
  670. (make-local-variable 'calc-embedded-globals)
  671. (let ((case-fold-search nil)
  672. (modes nil)
  673. (save-pt (point))
  674. found value)
  675. (goto-char (point-min))
  676. (while (re-search-forward "\\[calc-global-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)\\]" nil t)
  677. (and (setq found (assoc (buffer-substring (match-beginning 1)
  678. (match-end 1))
  679. calc-embedded-mode-vars))
  680. (or (assq (cdr found) modes)
  681. (setq modes (cons (cons (cdr found)
  682. (car (read-from-string
  683. (buffer-substring
  684. (match-beginning 2)
  685. (match-end 2)))))
  686. modes)))))
  687. (setq calc-embedded-globals (cons t modes))
  688. (goto-char save-pt)))
  689. (defun calc-embedded-find-modes ()
  690. (let ((case-fold-search nil)
  691. (save-pt (point))
  692. (no-defaults t)
  693. (modes nil)
  694. (emodes nil)
  695. (pmodes nil)
  696. found value)
  697. (while (and no-defaults (search-backward "[calc-" nil t))
  698. (forward-char 6)
  699. (or (and (looking-at "mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
  700. (setq found (assoc (buffer-substring (match-beginning 1)
  701. (match-end 1))
  702. calc-embedded-mode-vars))
  703. (or (assq (cdr found) modes)
  704. (setq modes (cons (cons (cdr found)
  705. (car (read-from-string
  706. (buffer-substring
  707. (match-beginning 2)
  708. (match-end 2)))))
  709. modes))))
  710. (and (looking-at "perm-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
  711. (setq found (assoc (buffer-substring (match-beginning 1)
  712. (match-end 1))
  713. calc-embedded-mode-vars))
  714. (or (assq (cdr found) pmodes)
  715. (setq pmodes (cons (cons (cdr found)
  716. (car (read-from-string
  717. (buffer-substring
  718. (match-beginning 2)
  719. (match-end 2)))))
  720. pmodes))))
  721. (and (looking-at "edit-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
  722. (setq found (assoc (buffer-substring (match-beginning 1)
  723. (match-end 1))
  724. calc-embedded-mode-vars))
  725. (or (assq (cdr found) emodes)
  726. (setq emodes (cons (cons (cdr found)
  727. (car (read-from-string
  728. (buffer-substring
  729. (match-beginning 2)
  730. (match-end 2)))))
  731. emodes))))
  732. (and (looking-at "defaults]")
  733. (setq no-defaults nil)))
  734. (backward-char 6))
  735. (goto-char save-pt)
  736. (unless (assq 'the-language modes)
  737. (let ((lang (assoc major-mode calc-language-alist)))
  738. (if lang
  739. (setq modes (cons (cons 'the-language (cdr lang))
  740. modes)))))
  741. (list modes emodes pmodes)))
  742. ;; The variable calc-embed-vars-used is local to calc-embedded-make-info,
  743. ;; calc-embedded-evaluate-expr and calc-embedded-update, but is
  744. ;; used by calc-embedded-find-vars, which is called by the above functions.
  745. (defvar calc-embed-vars-used)
  746. (defun calc-embedded-make-info (point cbuf fresh &optional
  747. calc-embed-top calc-embed-bot
  748. calc-embed-outer-top calc-embed-outer-bot)
  749. (let* ((bufentry (assq (current-buffer) calc-embedded-active))
  750. (found bufentry)
  751. (force (and fresh calc-embed-top (null (equal calc-embed-top '(t)))))
  752. (fixed calc-embed-top)
  753. (new-info nil)
  754. info str)
  755. (or found
  756. (and
  757. (setq found (list (current-buffer))
  758. calc-embedded-active (cons found calc-embedded-active)
  759. calc-embedded-firsttime-buf t)
  760. (let ((newann (assoc major-mode calc-embedded-announce-formula-alist))
  761. (newform (assoc major-mode calc-embedded-open-close-formula-alist))
  762. (newword (assoc major-mode calc-embedded-word-regexp-alist))
  763. (newplain (assoc major-mode calc-embedded-open-close-plain-alist))
  764. (newnewform
  765. (assoc major-mode calc-embedded-open-close-new-formula-alist))
  766. (newmode (assoc major-mode calc-embedded-open-close-mode-alist)))
  767. (when newann
  768. (make-local-variable 'calc-embedded-announce-formula)
  769. (setq calc-embedded-announce-formula (cdr newann)))
  770. (when newform
  771. (make-local-variable 'calc-embedded-open-formula)
  772. (make-local-variable 'calc-embedded-close-formula)
  773. (setq calc-embedded-open-formula (nth 0 (cdr newform)))
  774. (setq calc-embedded-close-formula (nth 1 (cdr newform))))
  775. (when newword
  776. (make-local-variable 'calc-embedded-word-regexp)
  777. (setq calc-embedded-word-regexp (nth 1 newword)))
  778. (when newplain
  779. (make-local-variable 'calc-embedded-open-plain)
  780. (make-local-variable 'calc-embedded-close-plain)
  781. (setq calc-embedded-open-plain (nth 0 (cdr newplain)))
  782. (setq calc-embedded-close-plain (nth 1 (cdr newplain))))
  783. (when newnewform
  784. (make-local-variable 'calc-embedded-open-new-formula)
  785. (make-local-variable 'calc-embedded-close-new-formula)
  786. (setq calc-embedded-open-new-formula (nth 0 (cdr newnewform)))
  787. (setq calc-embedded-close-new-formula (nth 1 (cdr newnewform))))
  788. (when newmode
  789. (make-local-variable 'calc-embedded-open-mode)
  790. (make-local-variable 'calc-embedded-close-mode)
  791. (setq calc-embedded-open-mode (nth 0 (cdr newmode)))
  792. (setq calc-embedded-close-mode (nth 1 (cdr newmode)))))))
  793. (while (and (cdr found)
  794. (> point (aref (car (cdr found)) 3)))
  795. (setq found (cdr found)))
  796. (if (and (cdr found)
  797. (>= point (aref (nth 1 found) 2)))
  798. (setq info (nth 1 found))
  799. (setq calc-embedded-firsttime-formula t)
  800. (setq info (make-vector 16 nil)
  801. new-info t
  802. fresh t)
  803. (aset info 0 (current-buffer))
  804. (aset info 1 (or cbuf (save-excursion
  805. (calc-create-buffer)
  806. (current-buffer)))))
  807. (if (and
  808. (or (integerp calc-embed-top) (equal calc-embed-top '(4)))
  809. (not calc-embed-bot))
  810. ; started with a user-supplied argument
  811. (progn
  812. (if (equal calc-embed-top '(4))
  813. (progn
  814. (aset info 2 (copy-marker (line-beginning-position)))
  815. (aset info 3 (copy-marker (line-end-position))))
  816. (if (= (setq calc-embed-arg (prefix-numeric-value calc-embed-arg)) 0)
  817. (progn
  818. (aset info 2 (copy-marker (region-beginning)))
  819. (aset info 3 (copy-marker (region-end))))
  820. (aset info (if (> calc-embed-arg 0) 2 3) (point-marker))
  821. (if (> calc-embed-arg 0)
  822. (progn
  823. (forward-line (1- calc-embed-arg))
  824. (end-of-line))
  825. (forward-line (1+ calc-embed-arg)))
  826. (aset info (if (> calc-embed-arg 0) 3 2) (point-marker))))
  827. (aset info 4 (copy-marker (aref info 2)))
  828. (aset info 5 (copy-marker (aref info 3))))
  829. (if (aref info 4)
  830. (setq calc-embed-top (aref info 2)
  831. fixed calc-embed-top)
  832. (if (consp calc-embed-top)
  833. (progn
  834. (require 'thingatpt)
  835. (if (thing-at-point-looking-at calc-embedded-word-regexp)
  836. (progn
  837. (setq calc-embed-top (copy-marker (match-beginning 0)))
  838. (setq calc-embed-bot (copy-marker (match-end 0)))
  839. (setq calc-embed-outer-top calc-embed-top)
  840. (setq calc-embed-outer-bot calc-embed-bot))
  841. (setq calc-embed-top (point-marker))
  842. (setq calc-embed-bot (point-marker))
  843. (setq calc-embed-outer-top calc-embed-top)
  844. (setq calc-embed-outer-bot calc-embed-bot)))
  845. (or calc-embed-top
  846. (calc-embedded-find-bounds 'plain)))
  847. (aset info 2 (copy-marker (min calc-embed-top calc-embed-bot)))
  848. (aset info 3 (copy-marker (max calc-embed-top calc-embed-bot)))
  849. (aset info 4 (copy-marker (or calc-embed-outer-top (aref info 2))))
  850. (aset info 5 (copy-marker (or calc-embed-outer-bot (aref info 3))))))
  851. (goto-char (aref info 2))
  852. (if new-info
  853. (progn
  854. (or (bolp) (aset info 7 t))
  855. (goto-char (aref info 3))
  856. (or (bolp) (eolp) (aset info 7 t))))
  857. (if fresh
  858. (let ((modes (calc-embedded-find-modes)))
  859. (aset info 12 (car modes))
  860. (aset info 13 (nth 1 modes))
  861. (aset info 14 (nth 2 modes))))
  862. (aset info 15 calc-embedded-globals)
  863. (setq str (buffer-substring (aref info 2) (aref info 3)))
  864. (if (or force
  865. (not (equal str (aref info 6))))
  866. (if (and fixed (aref info 6))
  867. (progn
  868. (aset info 4 nil)
  869. (calc-embedded-make-info point cbuf nil)
  870. (setq new-info nil))
  871. (let* ((open-plain calc-embedded-open-plain)
  872. (close-plain calc-embedded-close-plain)
  873. (pref-len (length open-plain))
  874. (calc-embed-vars-used nil)
  875. suff-pos val temp)
  876. (with-current-buffer (aref info 1)
  877. (calc-embedded-set-modes (aref info 15)
  878. (aref info 12) (aref info 14))
  879. (if (and (> (length str) pref-len)
  880. (equal (substring str 0 pref-len) open-plain)
  881. (setq suff-pos (string-match (regexp-quote close-plain)
  882. str pref-len)))
  883. (setq val (math-read-plain-expr
  884. (substring str pref-len suff-pos)))
  885. (if (string-match "[^ \t\n]" str)
  886. (setq pref-len 0
  887. val (condition-case nil
  888. (math-read-big-expr str)
  889. (error (math-read-expr str))))
  890. (setq val nil))))
  891. (if (eq (car-safe val) 'error)
  892. (setq val (list 'error
  893. (+ (aref info 2) pref-len (nth 1 val))
  894. (nth 2 val))))
  895. (aset info 6 str)
  896. (aset info 8 val)
  897. (setq temp val)
  898. (if (eq (car-safe temp) 'calcFunc-evalto)
  899. (setq temp (nth 1 temp))
  900. (if (eq (car-safe temp) 'error)
  901. (if new-info
  902. (setq new-info nil)
  903. (setcdr found (delq info (cdr found)))
  904. (calc-embedded-active-state 'less))))
  905. (aset info 9 (and (eq (car-safe temp) 'calcFunc-assign)
  906. (nth 1 temp)))
  907. (if (memq (car-safe val) '(calcFunc-evalto calcFunc-assign))
  908. (calc-embedded-find-vars val))
  909. (aset info 10 calc-embed-vars-used)
  910. (aset info 11 nil))))
  911. (if new-info
  912. (progn
  913. (setcdr found (cons info (cdr found)))
  914. (calc-embedded-active-state 'more)))
  915. info))
  916. (defun calc-embedded-find-vars (x)
  917. (cond ((Math-primp x)
  918. (and (eq (car-safe x) 'var)
  919. (not (assoc x calc-embed-vars-used))
  920. (setq calc-embed-vars-used (cons (list x) calc-embed-vars-used))))
  921. ((eq (car x) 'calcFunc-evalto)
  922. (calc-embedded-find-vars (nth 1 x)))
  923. ((eq (car x) 'calcFunc-assign)
  924. (calc-embedded-find-vars (nth 2 x)))
  925. (t
  926. (and (eq (car x) 'calcFunc-subscr)
  927. (eq (car-safe (nth 1 x)) 'var)
  928. (Math-primp (nth 2 x))
  929. (not (assoc x calc-embed-vars-used))
  930. (setq calc-embed-vars-used (cons (list x) calc-embed-vars-used)))
  931. (while (setq x (cdr x))
  932. (calc-embedded-find-vars (car x))))))
  933. (defvar math-ms-args)
  934. (defun calc-embedded-evaluate-expr (x)
  935. (let ((calc-embed-vars-used (aref calc-embedded-info 10)))
  936. (or calc-embed-vars-used (calc-embedded-find-vars x))
  937. (if calc-embed-vars-used
  938. (let ((active (assq (aref calc-embedded-info 0) calc-embedded-active))
  939. (math-ms-args nil))
  940. (save-excursion
  941. (calc-embedded-original-buffer t)
  942. (or active
  943. (progn
  944. (calc-embedded-activate)
  945. (setq active (assq (aref calc-embedded-info 0)
  946. calc-embedded-active))))
  947. (while calc-embed-vars-used
  948. (calc-embedded-eval-get-var (car (car calc-embed-vars-used)) active)
  949. (setq calc-embed-vars-used (cdr calc-embed-vars-used))))
  950. (calc-embedded-subst x))
  951. (calc-normalize (math-evaluate-expr-rec x)))))
  952. (defun calc-embedded-subst (x)
  953. (if (and (eq (car-safe x) 'calcFunc-evalto) (cdr x))
  954. (let ((rhs (calc-embedded-subst (nth 1 x))))
  955. (list 'calcFunc-evalto
  956. (nth 1 x)
  957. (if (eq (car-safe rhs) 'calcFunc-assign) (nth 2 rhs) rhs)))
  958. (if (and (eq (car-safe x) 'calcFunc-assign) (= (length x) 3))
  959. (list 'calcFunc-assign
  960. (nth 1 x)
  961. (calc-embedded-subst (nth 2 x)))
  962. (calc-normalize (math-evaluate-expr-rec (math-multi-subst-rec x))))))
  963. (defun calc-embedded-eval-get-var (var base)
  964. (let ((entry base)
  965. (point (aref calc-embedded-info 2))
  966. (last nil)
  967. val)
  968. (while (and (setq entry (cdr entry))
  969. (or (not (equal var (aref (car entry) 9)))
  970. (and (> point (aref (car entry) 3))
  971. (setq last entry)))))
  972. (if last
  973. (setq entry last))
  974. (if entry
  975. (progn
  976. (setq entry (car entry))
  977. (if (equal (buffer-substring (aref entry 2) (aref entry 3))
  978. (aref entry 6))
  979. (progn
  980. (or (aref entry 11)
  981. (save-excursion
  982. (calc-embedded-update entry 14 t nil)))
  983. (setq val (aref entry 11))
  984. (if (eq (car-safe val) 'calcFunc-evalto)
  985. (setq val (nth 2 val)))
  986. (if (eq (car-safe val) 'calcFunc-assign)
  987. (setq val (nth 2 val)))
  988. (setq math-ms-args (cons (cons var val) math-ms-args)))
  989. (calc-embedded-activate)
  990. (calc-embedded-eval-get-var var base))))))
  991. (defun calc-embedded-update (info which need-eval need-display
  992. &optional str entry old-val)
  993. (let* ((calc-embed-prev-modes nil)
  994. (open-plain calc-embedded-open-plain)
  995. (close-plain calc-embedded-close-plain)
  996. (calc-embed-vars-used nil)
  997. (evalled nil)
  998. (val (aref info 8))
  999. (old-eval (aref info 11)))
  1000. (or old-val (setq old-val val))
  1001. (if (eq (car-safe val) 'calcFunc-evalto)
  1002. (setq need-display t))
  1003. (unwind-protect
  1004. (progn
  1005. (set-buffer (aref info 1))
  1006. (and which
  1007. (calc-embedded-set-modes (aref info 15) (aref info 12)
  1008. (aref info which)
  1009. (if need-display 'full t)))
  1010. (if (memq (car-safe val) '(calcFunc-evalto calcFunc-assign))
  1011. (calc-embedded-find-vars val))
  1012. (if need-eval
  1013. (let ((calc-embedded-info info))
  1014. (setq val (math-evaluate-expr val)
  1015. evalled val)))
  1016. (if (or (eq need-eval 'eval) (eq (car-safe val) 'calcFunc-evalto))
  1017. (aset info 8 val))
  1018. (aset info 9 nil)
  1019. (aset info 10 calc-embed-vars-used)
  1020. (aset info 11 nil)
  1021. (if (or need-display (eq (car-safe val) 'calcFunc-evalto))
  1022. (let ((extra (if (eq calc-language 'big) 1 0)))
  1023. (or entry (setq entry (list val 1 nil)))
  1024. (or str (progn
  1025. (setq str (let ((calc-line-numbering nil))
  1026. (math-format-stack-value entry)))
  1027. (if (eq calc-language 'big)
  1028. (setq str (substring str 0 -1)))))
  1029. (and calc-show-plain
  1030. (setq str (concat open-plain
  1031. (math-showing-full-precision
  1032. (math-format-flat-expr val 0))
  1033. close-plain
  1034. str)))
  1035. (save-excursion
  1036. (calc-embedded-original-buffer t info)
  1037. (or (equal str (aref info 6))
  1038. (let ((delta (- (aref info 5) (aref info 3)))
  1039. (adjbot 0)
  1040. (buffer-read-only nil))
  1041. (goto-char (aref info 2))
  1042. (delete-region (point) (aref info 3))
  1043. (and (> (nth 1 entry) (1+ extra))
  1044. (aref info 7)
  1045. (progn
  1046. (delete-horizontal-space)
  1047. (if (looking-at "\n")
  1048. ;; If there's a newline there, don't add one
  1049. (insert "\n")
  1050. (insert "\n\n")
  1051. (delete-horizontal-space)
  1052. (setq adjbot 1)
  1053. ; (setq delta (1+ delta))
  1054. (backward-char 1))))
  1055. (insert str)
  1056. (set-marker (aref info 3) (+ (point) adjbot))
  1057. (set-marker (aref info 5) (+ (point) delta))
  1058. (aset info 6 str))))))
  1059. (if (eq (car-safe val) 'calcFunc-evalto)
  1060. (progn
  1061. (setq evalled (nth 2 val)
  1062. val (nth 1 val))))
  1063. (if (eq (car-safe val) 'calcFunc-assign)
  1064. (progn
  1065. (aset info 9 (nth 1 val))
  1066. (aset info 11 (or evalled
  1067. (let ((calc-embedded-info info))
  1068. (math-evaluate-expr (nth 2 val)))))
  1069. (or (equal old-eval (aref info 11))
  1070. (calc-embedded-var-change (nth 1 val) (aref info 0))))
  1071. (if (eq (car-safe old-val) 'calcFunc-evalto)
  1072. (setq old-val (nth 1 old-val)))
  1073. (if (eq (car-safe old-val) 'calcFunc-assign)
  1074. (calc-embedded-var-change (nth 1 old-val) (aref info 0)))))
  1075. (set-buffer (aref info 1))
  1076. (while calc-embed-prev-modes
  1077. (cond ((eq (car (car calc-embed-prev-modes)) 'the-language)
  1078. (if need-display
  1079. (calc-embedded-set-language (cdr (car calc-embed-prev-modes)))))
  1080. ((eq (car (car calc-embed-prev-modes)) 'the-display-just)
  1081. (if need-display
  1082. (calc-embedded-set-justify (cdr (car calc-embed-prev-modes)))))
  1083. (t
  1084. (set (car (car calc-embed-prev-modes))
  1085. (cdr (car calc-embed-prev-modes)))))
  1086. (setq calc-embed-prev-modes (cdr calc-embed-prev-modes))))))
  1087. ;;; These are hooks called by the main part of Calc.
  1088. (defvar calc-embedded-no-reselect nil)
  1089. (defun calc-embedded-select-buffer ()
  1090. (if (eq (current-buffer) (aref calc-embedded-info 0))
  1091. (let ((info calc-embedded-info)
  1092. horiz vert)
  1093. (if (and (or (< (point) (aref info 4))
  1094. (> (point) (aref info 5)))
  1095. (not calc-embedded-no-reselect))
  1096. (let ((calc-embedded-quiet t))
  1097. (message "(Switching Calc Embedded mode to new formula.)")
  1098. (calc-embedded nil)
  1099. (calc-embedded nil)))
  1100. (setq horiz (max (min (current-column) (- (point) (aref info 2))) 0)
  1101. vert (if (<= (aref info 2) (point))
  1102. (- (count-lines (aref info 2) (point))
  1103. (if (bolp) 0 1))
  1104. 0))
  1105. (set-buffer (aref info 1))
  1106. (if calc-show-plain
  1107. (if (= vert 0)
  1108. (setq horiz 0)
  1109. (setq vert (1- vert))))
  1110. (calc-cursor-stack-index 1)
  1111. (if calc-line-numbering
  1112. (setq horiz (+ horiz 4)))
  1113. (if (> vert 0)
  1114. (forward-line vert))
  1115. (forward-char (min horiz
  1116. (- (point-max) (point)))))
  1117. (calc-select-buffer)))
  1118. (defun calc-embedded-finish-command ()
  1119. (let ((buf (current-buffer))
  1120. horiz vert)
  1121. (with-current-buffer (aref calc-embedded-info 1)
  1122. (if (> (calc-stack-size) 0)
  1123. (let ((pt (point))
  1124. (col (current-column))
  1125. (bol (bolp)))
  1126. (calc-cursor-stack-index 0)
  1127. (if (< pt (point))
  1128. (progn
  1129. (calc-cursor-stack-index 1)
  1130. (if (>= pt (point))
  1131. (progn
  1132. (setq horiz (- col (if calc-line-numbering 4 0))
  1133. vert (- (count-lines (point) pt)
  1134. (if bol 0 1)))
  1135. (if calc-show-plain
  1136. (setq vert (max 1 (1+ vert))))))))
  1137. (goto-char pt))))
  1138. (if horiz
  1139. (progn
  1140. (set-buffer (aref calc-embedded-info 0))
  1141. (goto-char (aref calc-embedded-info 2))
  1142. (if (> vert 0)
  1143. (forward-line vert))
  1144. (forward-char (max horiz 0))
  1145. (set-buffer buf)))))
  1146. (defun calc-embedded-stack-change ()
  1147. (or calc-executing-macro
  1148. (with-current-buffer (aref calc-embedded-info 1)
  1149. (let* ((info calc-embedded-info)
  1150. (extra-line (if (eq calc-language 'big) 1 0))
  1151. (the-point (point))
  1152. (empty (= (calc-stack-size) 0))
  1153. (entry (if empty
  1154. (list '(var empty var-empty) 1 nil)
  1155. (calc-top 1 'entry)))
  1156. (old-val (aref info 8))
  1157. top bot str)
  1158. (if empty
  1159. (setq str "empty")
  1160. (save-excursion
  1161. (calc-cursor-stack-index 1)
  1162. (setq top (point))
  1163. (calc-cursor-stack-index 0)
  1164. (setq bot (- (point) extra-line))
  1165. (setq str (buffer-substring top (- bot 1))))
  1166. (if calc-line-numbering
  1167. (let ((pos 0))
  1168. (setq str (substring str 4))
  1169. (while (setq pos (string-match "\n...." str pos))
  1170. (setq str (concat (substring str 0 (1+ pos))
  1171. (substring str (+ pos 5)))
  1172. pos (1+ pos))))))
  1173. (calc-embedded-original-buffer t)
  1174. (aset info 8 (car entry))
  1175. (calc-embedded-update info 13 nil t str entry old-val)))))
  1176. (defun calc-embedded-mode-line-change ()
  1177. (let ((str mode-line-buffer-identification))
  1178. (save-excursion
  1179. (calc-embedded-original-buffer t)
  1180. (setq mode-line-buffer-identification str)
  1181. (set-buffer-modified-p (buffer-modified-p)))))
  1182. (defun calc-embedded-modes-change (vars)
  1183. (if (eq (car vars) 'calc-language) (setq vars '(the-language)))
  1184. (if (eq (car vars) 'calc-display-just) (setq vars '(the-display-just)))
  1185. (while (and vars
  1186. (not (rassq (car vars) calc-embedded-mode-vars)))
  1187. (setq vars (cdr vars)))
  1188. (if (and vars calc-mode-save-mode (not (eq calc-mode-save-mode 'save)))
  1189. (save-excursion
  1190. (let* ((save-mode calc-mode-save-mode)
  1191. (header (if (eq save-mode 'local)
  1192. "calc-mode:"
  1193. (format "calc-%s-mode:" save-mode)))
  1194. (the-language (calc-embedded-language))
  1195. (the-display-just (calc-embedded-justify))
  1196. (values (mapcar 'symbol-value vars))
  1197. (num (cond ((eq save-mode 'local) 12)
  1198. ((eq save-mode 'edit) 13)
  1199. ((eq save-mode 'perm) 14)
  1200. (t nil)))
  1201. base limit mname mlist)
  1202. (calc-embedded-original-buffer t)
  1203. (save-excursion
  1204. (if (eq save-mode 'global)
  1205. (setq base (point-max)
  1206. limit (point-min)
  1207. mlist calc-embedded-globals)
  1208. (goto-char (aref calc-embedded-info 4))
  1209. (beginning-of-line)
  1210. (setq base (point)
  1211. limit (max (- (point) 1000) (point-min))
  1212. mlist (and num (aref calc-embedded-info num)))
  1213. (and (re-search-backward
  1214. (format "\\(%s\\)[^\001]*\\(%s\\)\\|\\[calc-defaults]"
  1215. calc-embedded-open-formula
  1216. calc-embedded-close-formula) limit t)
  1217. (setq limit (point))))
  1218. (while vars
  1219. (goto-char base)
  1220. (if (setq mname (car (rassq (car vars)
  1221. calc-embedded-mode-vars)))
  1222. (let ((buffer-read-only nil)
  1223. (found (assq (car vars) mlist)))
  1224. (if found
  1225. (setcdr found (car values))
  1226. (setq mlist (cons (cons (car vars) (car values)) mlist))
  1227. (if num
  1228. (aset calc-embedded-info num mlist)
  1229. (if (eq save-mode 'global)
  1230. (setq calc-embedded-globals mlist))))
  1231. (if (re-search-backward
  1232. (format "\\[%s *%s: *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]"
  1233. header mname)
  1234. limit t)
  1235. (progn
  1236. (goto-char (match-beginning 1))
  1237. (delete-region (point) (match-end 1))
  1238. (insert (prin1-to-string (car values))))
  1239. (goto-char base)
  1240. (insert-before-markers
  1241. calc-embedded-open-mode
  1242. "[" header " " mname ": "
  1243. (prin1-to-string (car values)) "]"
  1244. calc-embedded-close-mode))))
  1245. (setq vars (cdr vars)
  1246. values (cdr values))))))
  1247. (when (and vars (eq calc-mode-save-mode 'save))
  1248. (calc-embedded-save-original-modes))))
  1249. (defun calc-embedded-var-change (var &optional buf)
  1250. (if (symbolp var)
  1251. (setq var (list 'var
  1252. (if (string-match "\\`var-.+\\'"
  1253. (symbol-name var))
  1254. (intern (substring (symbol-name var) 4))
  1255. var)
  1256. var)))
  1257. (save-excursion
  1258. (let ((manual (not calc-auto-recompute))
  1259. (bp calc-embedded-active)
  1260. (first t))
  1261. (if buf (setq bp (memq (assq buf bp) bp)))
  1262. (while bp
  1263. (let ((calc-embedded-no-reselect t)
  1264. (p (and (buffer-name (car (car bp)))
  1265. (cdr (car bp)))))
  1266. (while p
  1267. (if (assoc var (aref (car p) 10))
  1268. (if manual
  1269. (if (aref (car p) 11)
  1270. (progn
  1271. (aset (car p) 11 nil)
  1272. (if (aref (car p) 9)
  1273. (calc-embedded-var-change (aref (car p) 9)))))
  1274. (set-buffer (aref (car p) 0))
  1275. (if (equal (buffer-substring (aref (car p) 2)
  1276. (aref (car p) 3))
  1277. (aref (car p) 6))
  1278. (let ((calc-embedded-info nil))
  1279. (or calc-embedded-quiet
  1280. (message "Recomputing..."))
  1281. (setq first nil)
  1282. (calc-wrapper
  1283. (set-buffer (aref (car p) 0))
  1284. (calc-embedded-update (car p) 14 t nil)))
  1285. (setcdr (car bp) (delq (car p) (cdr (car bp))))
  1286. (message
  1287. "(Tried to recompute but formula was changed or missing)"))))
  1288. (setq p (cdr p))))
  1289. (setq bp (if buf nil (cdr bp))))
  1290. (or first calc-embedded-quiet (message "")))))
  1291. (provide 'calc-embed)
  1292. ;; Local variables:
  1293. ;; generated-autoload-file: "calc-loaddefs.el"
  1294. ;; End:
  1295. ;;; calc-embed.el ends here