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