calc-yank.el 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. ;;; calc-yank.el --- kill-ring functionality for Calc
  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. ;;; Kill ring commands.
  22. (defun calc-kill (nn &optional no-delete)
  23. (interactive "P")
  24. (if (eq major-mode 'calc-mode)
  25. (calc-wrapper
  26. (calc-force-refresh)
  27. (calc-set-command-flag 'no-align)
  28. (let ((num (max (calc-locate-cursor-element (point)) 1))
  29. (n (prefix-numeric-value nn)))
  30. (if (< n 0)
  31. (progn
  32. (if (eobp)
  33. (setq num (1- num)))
  34. (setq num (- num n)
  35. n (- n))))
  36. (calc-check-stack num)
  37. (let ((stuff (calc-top-list n (- num n -1))))
  38. (calc-cursor-stack-index num)
  39. (let ((first (point)))
  40. (calc-cursor-stack-index (- num n))
  41. (if (null nn)
  42. (backward-char 1)) ; don't include newline for raw C-k
  43. (copy-region-as-kill first (point))
  44. (if (not no-delete)
  45. (calc-pop-stack n (- num n -1))))
  46. (setq calc-last-kill (cons (car kill-ring) stuff)))))
  47. (kill-line nn)))
  48. (defun calc-force-refresh ()
  49. (if (or calc-executing-macro calc-display-dirty)
  50. (let ((calc-executing-macro nil))
  51. (calc-refresh))))
  52. (defun calc-locate-cursor-element (pt)
  53. (save-excursion
  54. (goto-char (point-max))
  55. (calc-locate-cursor-scan (- calc-stack-top) calc-stack pt)))
  56. (defun calc-locate-cursor-scan (n stack pt)
  57. (if (or (<= (point) pt)
  58. (null stack))
  59. n
  60. (forward-line (- (nth 1 (car stack))))
  61. (calc-locate-cursor-scan (1+ n) (cdr stack) pt)))
  62. (defun calc-kill-region (top bot &optional no-delete)
  63. (interactive "r")
  64. (if (eq major-mode 'calc-mode)
  65. (calc-wrapper
  66. (calc-force-refresh)
  67. (calc-set-command-flag 'no-align)
  68. (let* ((top-num (calc-locate-cursor-element top))
  69. (top-pos (save-excursion
  70. (calc-cursor-stack-index top-num)
  71. (point)))
  72. (bot-num (calc-locate-cursor-element (1- bot)))
  73. (bot-pos (save-excursion
  74. (calc-cursor-stack-index (max 0 (1- bot-num)))
  75. (point)))
  76. (num (- top-num bot-num -1)))
  77. (copy-region-as-kill top-pos bot-pos)
  78. (setq calc-last-kill (cons (car kill-ring)
  79. (calc-top-list num bot-num)))
  80. (if (not no-delete)
  81. (calc-pop-stack num bot-num))))
  82. (if no-delete
  83. (copy-region-as-kill top bot)
  84. (kill-region top bot))))
  85. (defun calc-copy-as-kill (n)
  86. (interactive "P")
  87. (calc-kill n t))
  88. (defun calc-copy-region-as-kill (top bot)
  89. (interactive "r")
  90. (calc-kill-region top bot t))
  91. ;; This function uses calc-last-kill if possible to get an exact result,
  92. ;; otherwise it just parses the yanked string.
  93. ;; Modified to use Emacs 19 extended concept of kill-ring. -- daveg 12/15/96
  94. ;;;###autoload
  95. (defun calc-yank ()
  96. (interactive)
  97. (calc-wrapper
  98. (calc-pop-push-record-list
  99. 0 "yank"
  100. (let ((thing (if (fboundp 'current-kill)
  101. (current-kill 0 t)
  102. (car kill-ring-yank-pointer))))
  103. (if (eq (car-safe calc-last-kill) thing)
  104. (cdr calc-last-kill)
  105. (if (stringp thing)
  106. (let ((val (math-read-exprs (calc-clean-newlines thing))))
  107. (if (eq (car-safe val) 'error)
  108. (progn
  109. (setq val (math-read-exprs thing))
  110. (if (eq (car-safe val) 'error)
  111. (error "Bad format in yanked data")
  112. val))
  113. val))))))))
  114. ;;; The Calc set- and get-register commands are modified versions of functions
  115. ;;; in register.el
  116. (defvar calc-register-alist nil
  117. "Alist of elements (NAME . (TEXT . CALCVAL)).
  118. NAME is a character (a number).
  119. TEXT and CALCVAL are the TEXT and internal structure of stack entries.")
  120. (defun calc-set-register (register text calcval)
  121. "Set the contents of the Calc register REGISTER to (TEXT . CALCVAL),
  122. as well as set the contents of the Emacs register REGISTER to TEXT."
  123. (set-register register text)
  124. (let ((aelt (assq register calc-register-alist)))
  125. (if aelt
  126. (setcdr aelt (cons text calcval))
  127. (push (cons register (cons text calcval)) calc-register-alist))))
  128. (defun calc-get-register (reg)
  129. "Return the CALCVAL portion of the contents of the Calc register REG,
  130. unless the TEXT portion doesn't match the contents of the Emacs register REG,
  131. in which case either return the contents of the Emacs register (if it is
  132. text) or `nil'."
  133. (let ((cval (cdr (assq reg calc-register-alist)))
  134. (val (cdr (assq reg register-alist))))
  135. (if (stringp val)
  136. (if (and (stringp (car cval))
  137. (string= (car cval) val))
  138. (cdr cval)
  139. val))))
  140. (defun calc-copy-to-register (register start end &optional delete-flag)
  141. "Copy the lines in the region into register REGISTER.
  142. With prefix arg, delete as well."
  143. (interactive "cCopy to register: \nr\nP")
  144. (if (eq major-mode 'calc-mode)
  145. (let* ((top-num (calc-locate-cursor-element start))
  146. (top-pos (save-excursion
  147. (calc-cursor-stack-index top-num)
  148. (point)))
  149. (bot-num (calc-locate-cursor-element (1- end)))
  150. (bot-pos (save-excursion
  151. (calc-cursor-stack-index (max 0 (1- bot-num)))
  152. (point)))
  153. (num (- top-num bot-num -1))
  154. (str (buffer-substring top-pos bot-pos)))
  155. (calc-set-register register str (calc-top-list num bot-num))
  156. (if delete-flag
  157. (calc-wrapper
  158. (calc-pop-stack num bot-num))))
  159. (copy-to-register register start end delete-flag)))
  160. (defun calc-insert-register (register)
  161. "Insert the contents of register REGISTER."
  162. (interactive "cInsert register: ")
  163. (if (eq major-mode 'calc-mode)
  164. (let ((val (calc-get-register register)))
  165. (calc-wrapper
  166. (calc-pop-push-record-list
  167. 0 "insr"
  168. (if (not val)
  169. (error "Bad format in register data")
  170. (if (consp val)
  171. val
  172. (let ((nval (math-read-exprs (calc-clean-newlines val))))
  173. (if (eq (car-safe nval) 'error)
  174. (progn
  175. (setq nval (math-read-exprs val))
  176. (if (eq (car-safe nval) 'error)
  177. (error "Bad format in register data")
  178. nval))
  179. nval)))))))
  180. (insert-register register)))
  181. (defun calc-add-to-register (register start end prepend delete-flag)
  182. "Add the lines in the region to register REGISTER.
  183. If PREPEND is non-nil, add them to the beginning of the register,
  184. otherwise the end. If DELETE-FLAG is non-nil, also delete the region."
  185. (let* ((top-num (calc-locate-cursor-element start))
  186. (top-pos (save-excursion
  187. (calc-cursor-stack-index top-num)
  188. (point)))
  189. (bot-num (calc-locate-cursor-element (1- end)))
  190. (bot-pos (save-excursion
  191. (calc-cursor-stack-index (max 0 (1- bot-num)))
  192. (point)))
  193. (num (- top-num bot-num -1))
  194. (str (buffer-substring top-pos bot-pos))
  195. (calcval (calc-top-list num bot-num))
  196. (cval (cdr (assq register calc-register-alist))))
  197. (if (not cval)
  198. (calc-set-register register str calcval)
  199. (if prepend
  200. (calc-set-register
  201. register
  202. (concat str (car cval))
  203. (append calcval (cdr cval)))
  204. (calc-set-register
  205. register
  206. (concat (car cval) str)
  207. (append (cdr cval) calcval))))
  208. (if delete-flag
  209. (calc-wrapper
  210. (calc-pop-stack num bot-num)))))
  211. (defun calc-append-to-register (register start end &optional delete-flag)
  212. "Copy the lines in the region to the end of register REGISTER.
  213. With prefix arg, also delete the region."
  214. (interactive "cAppend to register: \nr\nP")
  215. (if (eq major-mode 'calc-mode)
  216. (calc-add-to-register register start end nil delete-flag)
  217. (append-to-register register start end delete-flag)))
  218. (defun calc-prepend-to-register (register start end &optional delete-flag)
  219. "Copy the lines in the region to the beginning of register REGISTER.
  220. With prefix arg, also delete the region."
  221. (interactive "cPrepend to register: \nr\nP")
  222. (if (eq major-mode 'calc-mode)
  223. (calc-add-to-register register start end t delete-flag)
  224. (prepend-to-register register start end delete-flag)))
  225. (defun calc-clean-newlines (s)
  226. (cond
  227. ;; Omit leading/trailing whitespace
  228. ((or (string-match "\\`[ \n\r]+\\([^\001]*\\)\\'" s)
  229. (string-match "\\`\\([^\001]*\\)[ \n\r]+\\'" s))
  230. (calc-clean-newlines (math-match-substring s 1)))
  231. ;; Convert newlines to commas
  232. ((string-match "\\`\\(.*\\)[\n\r]+\\([^\001]*\\)\\'" s)
  233. (calc-clean-newlines (concat (math-match-substring s 1) ","
  234. (math-match-substring s 2))))
  235. (t s)))
  236. (defun calc-do-grab-region (top bot arg)
  237. (when (memq major-mode '(calc-mode calc-trail-mode))
  238. (error "This command works only in a regular text buffer"))
  239. (let* ((from-buffer (current-buffer))
  240. (calc-was-started (get-buffer-window "*Calculator*"))
  241. (single nil)
  242. data vals pos)
  243. (if arg
  244. (if (consp arg)
  245. (setq single t)
  246. (setq arg (prefix-numeric-value arg))
  247. (if (= arg 0)
  248. (setq top (point-at-bol)
  249. bot (point-at-eol))
  250. (save-excursion
  251. (setq top (point))
  252. (forward-line arg)
  253. (if (> arg 0)
  254. (setq bot (point))
  255. (setq bot top
  256. top (point)))))))
  257. (setq data (buffer-substring top bot))
  258. (calc)
  259. (if single
  260. (setq vals (math-read-expr data))
  261. (setq vals (math-read-expr (concat "[" data "]")))
  262. (and (eq (car-safe vals) 'vec)
  263. (= (length vals) 2)
  264. (eq (car-safe (nth 1 vals)) 'vec)
  265. (setq vals (nth 1 vals))))
  266. (if (eq (car-safe vals) 'error)
  267. (progn
  268. (if calc-was-started
  269. (pop-to-buffer from-buffer)
  270. (calc-quit t)
  271. (switch-to-buffer from-buffer))
  272. (goto-char top)
  273. (forward-char (+ (nth 1 vals) (if single 0 1)))
  274. (error (nth 2 vals))))
  275. (calc-slow-wrapper
  276. (calc-enter-result 0 "grab" vals))))
  277. (defun calc-do-grab-rectangle (top bot arg &optional reduce)
  278. (and (memq major-mode '(calc-mode calc-trail-mode))
  279. (error "This command works only in a regular text buffer"))
  280. (let* ((col1 (save-excursion (goto-char top) (current-column)))
  281. (col2 (save-excursion (goto-char bot) (current-column)))
  282. (from-buffer (current-buffer))
  283. (calc-was-started (get-buffer-window "*Calculator*"))
  284. data mat vals lnum pt pos)
  285. (if (= col1 col2)
  286. (save-excursion
  287. (unless (= col1 0)
  288. (error "Point and mark must be at beginning of line, or define a rectangle"))
  289. (goto-char top)
  290. (while (< (point) bot)
  291. (setq pt (point))
  292. (forward-line 1)
  293. (setq data (cons (buffer-substring pt (1- (point))) data)))
  294. (setq data (nreverse data)))
  295. (setq data (extract-rectangle top bot)))
  296. (calc)
  297. (setq mat (list 'vec)
  298. lnum 0)
  299. (when arg
  300. (setq arg (if (consp arg) 0 (prefix-numeric-value arg))))
  301. (while data
  302. (if (natnump arg)
  303. (progn
  304. (if (= arg 0)
  305. (setq arg 1000000))
  306. (setq pos 0
  307. vals (list 'vec))
  308. (let ((w (length (car data)))
  309. j v)
  310. (while (< pos w)
  311. (setq j (+ pos arg)
  312. v (if (>= j w)
  313. (math-read-expr (substring (car data) pos))
  314. (math-read-expr (substring (car data) pos j))))
  315. (if (eq (car-safe v) 'error)
  316. (setq vals v w 0)
  317. (setq vals (nconc vals (list v))
  318. pos j)))))
  319. (if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
  320. (car data))
  321. (setq vals (list 'vec (string-to-number (car data))))
  322. (if (and (null arg)
  323. (string-match "[[{][^][{}]*[]}]" (car data)))
  324. (setq pos (match-beginning 0)
  325. vals (math-read-expr (math-match-substring (car data) 0)))
  326. (let ((s (if (string-match
  327. "\\`\\([0-9]+:[ \t]\\)?\\(.*[^, \t]\\)[, \t]*\\'"
  328. (car data))
  329. (math-match-substring (car data) 2)
  330. (car data))))
  331. (setq pos -1
  332. vals (math-read-expr (concat "[" s "]")))
  333. (if (eq (car-safe vals) 'error)
  334. (let ((v2 (math-read-expr s)))
  335. (unless (eq (car-safe v2) 'error)
  336. (setq vals (list 'vec v2)))))))))
  337. (if (eq (car-safe vals) 'error)
  338. (progn
  339. (if calc-was-started
  340. (pop-to-buffer from-buffer)
  341. (calc-quit t)
  342. (switch-to-buffer from-buffer))
  343. (goto-char top)
  344. (forward-line lnum)
  345. (forward-char (+ (nth 1 vals) (min col1 col2) pos))
  346. (error (nth 2 vals))))
  347. (unless (equal vals '(vec))
  348. (setq mat (cons vals mat)))
  349. (setq data (cdr data)
  350. lnum (1+ lnum)))
  351. (calc-slow-wrapper
  352. (if reduce
  353. (calc-enter-result 0 "grb+" (list reduce '(var add var-add)
  354. (nreverse mat)))
  355. (calc-enter-result 0 "grab" (nreverse mat))))))
  356. (defun calc-copy-to-buffer (nn)
  357. "Copy the top of stack into an editing buffer."
  358. (interactive "P")
  359. (let ((thebuf (and (not (memq major-mode '(calc-mode calc-trail-mode)))
  360. (current-buffer)))
  361. (movept nil)
  362. oldbuf newbuf)
  363. (calc-wrapper
  364. (save-excursion
  365. (calc-force-refresh)
  366. (let ((n (prefix-numeric-value nn))
  367. (eat-lnums calc-line-numbering)
  368. (big-offset (if (eq calc-language 'big) 1 0))
  369. top bot)
  370. (setq oldbuf (current-buffer)
  371. newbuf (or thebuf
  372. (calc-find-writable-buffer (buffer-list) 0)
  373. (calc-find-writable-buffer (buffer-list) 1)
  374. (error "No other buffer")))
  375. (cond ((and (or (null nn)
  376. (consp nn))
  377. (= (calc-substack-height 0)
  378. (- (1- (calc-substack-height 1)) big-offset)))
  379. (calc-cursor-stack-index 1)
  380. (if (looking-at
  381. (if calc-line-numbering "[0-9]+: *[^ \n]" " *[^ \n]"))
  382. (goto-char (1- (match-end 0))))
  383. (setq eat-lnums nil
  384. top (point))
  385. (calc-cursor-stack-index 0)
  386. (setq bot (- (1- (point)) big-offset)))
  387. ((> n 0)
  388. (calc-cursor-stack-index n)
  389. (setq top (point))
  390. (calc-cursor-stack-index 0)
  391. (setq bot (- (point) big-offset)))
  392. ((< n 0)
  393. (calc-cursor-stack-index (- n))
  394. (setq top (point))
  395. (calc-cursor-stack-index (1- (- n)))
  396. (setq bot (point)))
  397. (t
  398. (goto-char (point-min))
  399. (forward-line 1)
  400. (setq top (point))
  401. (calc-cursor-stack-index 0)
  402. (setq bot (point))))
  403. (with-current-buffer newbuf
  404. (if (consp nn)
  405. (kill-region (region-beginning) (region-end)))
  406. (push-mark (point) t)
  407. (if (and overwrite-mode (not (consp nn)))
  408. (calc-overwrite-string (with-current-buffer oldbuf
  409. (buffer-substring top bot))
  410. eat-lnums)
  411. (or (bolp) (setq eat-lnums nil))
  412. (insert-buffer-substring oldbuf top bot)
  413. (and eat-lnums
  414. (let ((n 1))
  415. (while (and (> (point) (mark))
  416. (progn
  417. (forward-line -1)
  418. (>= (point) (mark))))
  419. (delete-char 4)
  420. (setq n (1+ n)))
  421. (forward-line n))))
  422. (when thebuf
  423. (setq movept (point)))
  424. (when (get-buffer-window (current-buffer))
  425. (set-window-point (get-buffer-window (current-buffer))
  426. (point)))))))
  427. (when movept
  428. (goto-char movept))
  429. (when (and (consp nn)
  430. (not thebuf))
  431. (calc-quit t)
  432. (switch-to-buffer newbuf))))
  433. (defun calc-overwrite-string (str eat-lnums)
  434. (when (string-match "\n\\'" str)
  435. (setq str (substring str 0 -1)))
  436. (when eat-lnums
  437. (setq str (substring str 4)))
  438. (if (and (string-match "\\`[-+]?[0-9.]+\\(e-?[0-9]+\\)?\\'" str)
  439. (looking-at "[-+]?[0-9.]+\\(e-?[0-9]+\\)?"))
  440. (progn
  441. (delete-region (point) (match-end 0))
  442. (insert str))
  443. (let ((i 0))
  444. (while (< i (length str))
  445. (if (= (setq last-command-event (aref str i)) ?\n)
  446. (or (= i (1- (length str)))
  447. (let ((pt (point)))
  448. (end-of-line)
  449. (delete-region pt (point))
  450. (if (eobp)
  451. (insert "\n")
  452. (forward-char 1))
  453. (if eat-lnums (setq i (+ i 4)))))
  454. (self-insert-command 1))
  455. (setq i (1+ i))))))
  456. ;; First, require that buffer is visible and does not begin with "*"
  457. ;; Second, require only that it not begin with "*Calc"
  458. (defun calc-find-writable-buffer (buf mode)
  459. (and buf
  460. (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
  461. (buffer-name (car buf)))
  462. (and (= mode 0)
  463. (or (string-match "\\`\\*.*" (buffer-name (car buf)))
  464. (not (get-buffer-window (car buf))))))
  465. (calc-find-writable-buffer (cdr buf) mode)
  466. (car buf))))
  467. (defun calc-edit (n)
  468. (interactive "p")
  469. (calc-slow-wrapper
  470. (when (eq n 0)
  471. (setq n (calc-stack-size)))
  472. (let* ((flag nil)
  473. (allow-ret (> n 1))
  474. (list (math-showing-full-precision
  475. (mapcar (if (> n 1)
  476. (function (lambda (x)
  477. (math-format-flat-expr x 0)))
  478. (function
  479. (lambda (x)
  480. (if (math-vectorp x) (setq allow-ret t))
  481. (math-format-nice-expr x (frame-width)))))
  482. (if (> n 0)
  483. (calc-top-list n)
  484. (calc-top-list 1 (- n)))))))
  485. (calc-edit-mode (list 'calc-finish-stack-edit (or flag n)) allow-ret)
  486. (while list
  487. (insert (car list) "\n")
  488. (setq list (cdr list)))))
  489. (calc-show-edit-buffer))
  490. (defun calc-alg-edit (str)
  491. (calc-edit-mode '(calc-finish-stack-edit 0))
  492. (calc-show-edit-buffer)
  493. (insert str "\n")
  494. (backward-char 1)
  495. (calc-set-command-flag 'do-edit))
  496. (defvar calc-edit-mode-map
  497. (let ((map (make-sparse-keymap)))
  498. (define-key map "\n" 'calc-edit-finish)
  499. (define-key map "\r" 'calc-edit-return)
  500. (define-key map "\C-c\C-c" 'calc-edit-finish)
  501. map)
  502. "Keymap for use by the calc-edit command.")
  503. (defvar calc-original-buffer)
  504. (defvar calc-return-buffer)
  505. (defvar calc-one-window)
  506. (defvar calc-edit-handler)
  507. (defvar calc-restore-trail)
  508. (defvar calc-allow-ret)
  509. (defvar calc-edit-top)
  510. (defun calc-edit-mode (&optional handler allow-ret title)
  511. "Calculator editing mode. Press RET, LFD, or C-c C-c to finish.
  512. To cancel the edit, simply kill the *Calc Edit* buffer."
  513. (interactive)
  514. (unless handler
  515. (error "This command can be used only indirectly through calc-edit"))
  516. (let ((oldbuf (current-buffer))
  517. (buf (get-buffer-create "*Calc Edit*")))
  518. (set-buffer buf)
  519. (kill-all-local-variables)
  520. (use-local-map calc-edit-mode-map)
  521. (setq buffer-read-only nil)
  522. (setq truncate-lines nil)
  523. (setq major-mode 'calc-edit-mode)
  524. (setq mode-name "Calc Edit")
  525. (run-mode-hooks 'calc-edit-mode-hook)
  526. (make-local-variable 'calc-original-buffer)
  527. (setq calc-original-buffer oldbuf)
  528. (make-local-variable 'calc-return-buffer)
  529. (setq calc-return-buffer oldbuf)
  530. (make-local-variable 'calc-one-window)
  531. (setq calc-one-window (and (one-window-p t) pop-up-windows))
  532. (make-local-variable 'calc-edit-handler)
  533. (setq calc-edit-handler handler)
  534. (make-local-variable 'calc-restore-trail)
  535. (setq calc-restore-trail (get-buffer-window (calc-trail-buffer)))
  536. (make-local-variable 'calc-allow-ret)
  537. (setq calc-allow-ret allow-ret)
  538. (let ((inhibit-read-only t))
  539. (erase-buffer))
  540. (add-hook 'kill-buffer-hook (lambda ()
  541. (let ((calc-edit-handler nil))
  542. (calc-edit-finish t))
  543. (message "(Cancelled)")) t t)
  544. (insert (propertize
  545. (concat
  546. (or title title "Calc Edit Mode. ")
  547. "Press `C-c C-c'"
  548. (if allow-ret "" " or RET")
  549. " to finish, `C-x k RET' to cancel.\n\n")
  550. 'font-lock-face 'italic 'read-only t 'rear-nonsticky t 'front-sticky t))
  551. (make-local-variable 'calc-edit-top)
  552. (setq calc-edit-top (point))))
  553. (put 'calc-edit-mode 'mode-class 'special)
  554. (defun calc-show-edit-buffer ()
  555. (let ((buf (current-buffer)))
  556. (if (and (one-window-p t) pop-up-windows)
  557. (pop-to-buffer (get-buffer-create "*Calc Edit*"))
  558. (and calc-embedded-info (get-buffer-window (aref calc-embedded-info 1))
  559. (select-window (get-buffer-window (aref calc-embedded-info 1))))
  560. (switch-to-buffer (get-buffer-create "*Calc Edit*")))
  561. (setq calc-return-buffer buf)
  562. (if (and (< (window-width) (frame-width))
  563. calc-display-trail)
  564. (let ((win (get-buffer-window (calc-trail-buffer))))
  565. (if win
  566. (delete-window win))))
  567. (set-buffer-modified-p nil)
  568. (goto-char calc-edit-top)))
  569. (defun calc-edit-return ()
  570. (interactive)
  571. (if (and (boundp 'calc-allow-ret) calc-allow-ret)
  572. (newline)
  573. (calc-edit-finish)))
  574. ;; The variable calc-edit-disp-trail is local to calc-edit finish, but
  575. ;; is used by calc-finish-selection-edit and calc-finish-stack-edit.
  576. (defvar calc-edit-disp-trail)
  577. (defun calc-edit-finish (&optional keep)
  578. "Finish calc-edit mode. Parse buffer contents and push them on the stack."
  579. (interactive "P")
  580. (message "Working...")
  581. (or (and (boundp 'calc-original-buffer)
  582. (boundp 'calc-return-buffer)
  583. (boundp 'calc-one-window)
  584. (boundp 'calc-edit-handler)
  585. (boundp 'calc-restore-trail)
  586. (eq major-mode 'calc-edit-mode))
  587. (error "This command is valid only in buffers created by calc-edit"))
  588. (let ((buf (current-buffer))
  589. (original calc-original-buffer)
  590. (return calc-return-buffer)
  591. (one-window calc-one-window)
  592. (calc-edit-disp-trail calc-restore-trail))
  593. (save-excursion
  594. (when (or (null (buffer-name original))
  595. (progn
  596. (set-buffer original)
  597. (not (eq major-mode 'calc-mode))))
  598. (error "Original calculator buffer has been corrupted")))
  599. (goto-char calc-edit-top)
  600. (if (buffer-modified-p)
  601. (eval calc-edit-handler))
  602. (if (and one-window (not (one-window-p t)))
  603. (delete-window))
  604. (if (get-buffer-window return)
  605. (select-window (get-buffer-window return))
  606. (switch-to-buffer return))
  607. (if keep
  608. (bury-buffer buf)
  609. (kill-buffer buf))
  610. (if calc-edit-disp-trail
  611. (calc-wrapper
  612. (calc-trail-display 1 t)))
  613. (message "")))
  614. (defun calc-edit-cancel ()
  615. "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
  616. (interactive)
  617. (let ((calc-edit-handler nil))
  618. (calc-edit-finish))
  619. (message "(Cancelled)"))
  620. (defun calc-finish-stack-edit (num)
  621. (let ((buf (current-buffer))
  622. (str (buffer-substring calc-edit-top (point-max)))
  623. (start (point))
  624. pos)
  625. (if (and (integerp num) (> num 1))
  626. (while (setq pos (string-match "\n." str))
  627. (aset str pos ?\,)))
  628. (switch-to-buffer calc-original-buffer)
  629. (let ((vals (let ((calc-language nil)
  630. (math-expr-opers (math-standard-ops)))
  631. (and (string-match "[^\n\t ]" str)
  632. (math-read-exprs str)))))
  633. (when (eq (car-safe vals) 'error)
  634. (switch-to-buffer buf)
  635. (goto-char (+ start (nth 1 vals)))
  636. (error (nth 2 vals)))
  637. (calc-wrapper
  638. (if (symbolp num)
  639. (progn
  640. (set num (car vals))
  641. (calc-refresh-evaltos num))
  642. (if calc-edit-disp-trail
  643. (calc-trail-display 1 t))
  644. (and vals
  645. (let ((calc-simplify-mode (if (eq last-command-event ?\C-j)
  646. 'none
  647. calc-simplify-mode)))
  648. (if (>= num 0)
  649. (calc-enter-result num "edit" vals)
  650. (calc-enter-result 1 "edit" vals (- num))))))))))
  651. (provide 'calc-yank)
  652. ;; Local variables:
  653. ;; generated-autoload-file: "calc-loaddefs.el"
  654. ;; End:
  655. ;;; calc-yank.el ends here