calc-yank.el 28 KB

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