Suppress localized settings in Tramp test.
[emacs.git] / lisp / calc / calc-yank.el
blob726a7202a8ade78aedf2f04389eeabd6d63e02c6
1 ;;; calc-yank.el --- kill-ring functionality for Calc
3 ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
25 ;;; Code:
27 ;; This file is autoloaded from calc-ext.el.
29 (require 'calc-ext)
30 (require 'calc-macs)
32 ;;; Kill ring commands.
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)))
61 (defun calc-force-refresh ()
62 (if (or calc-executing-macro calc-display-dirty)
63 (let ((calc-executing-macro nil))
64 (calc-refresh))))
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)))
71 (defun calc-locate-cursor-scan (n stack pt)
72 (if (or (<= (point) pt)
73 (null stack))
75 (forward-line (- (nth 1 (car stack))))
76 (calc-locate-cursor-scan (1+ n) (cdr stack) pt)))
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))))
102 (defun calc-copy-as-kill (n)
103 (interactive "P")
104 (calc-kill n t))
106 (defun calc-copy-region-as-kill (top bot)
107 (interactive "r")
108 (calc-kill-region top bot t))
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))))))))
134 ;;; The Calc set- and get-register commands are modified versions of functions
135 ;;; in register.el
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.")
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 (setf (alist-get register calc-register-alist) (cons text calcval)))
148 (defun calc-get-register (reg)
149 "Return the CALCVAL portion of the contents of the Calc register REG,
150 unless the TEXT portion doesn't match the contents of the Emacs register REG,
151 in which case either return the contents of the Emacs register (if it is
152 text) or `nil'."
153 (let ((cval (cdr (assq reg calc-register-alist)))
154 (val (cdr (assq reg register-alist))))
155 (if (stringp val)
156 (if (and (stringp (car cval))
157 (string= (car cval) val))
158 (cdr cval)
159 val))))
161 (defun calc-copy-to-register (register start end &optional delete-flag)
162 "Copy the lines in the region into register REGISTER.
163 With prefix arg, delete as well.
165 Interactively, reads the register using `register-read-with-preview'."
166 (interactive (list (register-read-with-preview "Copy to register: ")
167 (region-beginning) (region-end)
168 current-prefix-arg))
169 (if (eq major-mode 'calc-mode)
170 (let* ((top-num (calc-locate-cursor-element start))
171 (top-pos (save-excursion
172 (calc-cursor-stack-index top-num)
173 (point)))
174 (bot-num (calc-locate-cursor-element (1- end)))
175 (bot-pos (save-excursion
176 (calc-cursor-stack-index (max 0 (1- bot-num)))
177 (point)))
178 (num (- top-num bot-num -1))
179 (str (buffer-substring top-pos bot-pos)))
180 (calc-set-register register str (calc-top-list num bot-num))
181 (if delete-flag
182 (calc-wrapper
183 (calc-pop-stack num bot-num))))
184 (copy-to-register register start end delete-flag)))
186 (defun calc-insert-register (register)
187 "Insert the contents of register REGISTER.
189 Interactively, reads the register using `register-read-with-preview'."
190 (interactive (list (register-read-with-preview "Insert register: ")))
191 (if (eq major-mode 'calc-mode)
192 (let ((val (calc-get-register register)))
193 (calc-wrapper
194 (calc-pop-push-record-list
195 0 "insr"
196 (if (not val)
197 (error "Bad format in register data")
198 (if (consp val)
200 (let ((nval (math-read-exprs (calc-clean-newlines val))))
201 (if (eq (car-safe nval) 'error)
202 (progn
203 (setq nval (math-read-exprs val))
204 (if (eq (car-safe nval) 'error)
205 (error "Bad format in register data")
206 nval))
207 nval)))))))
208 (insert-register register)))
210 (defun calc-add-to-register (register start end prepend delete-flag)
211 "Add the lines in the region to register REGISTER.
212 If PREPEND is non-nil, add them to the beginning of the register,
213 otherwise the end. If DELETE-FLAG is non-nil, also delete the region."
214 (let* ((top-num (calc-locate-cursor-element start))
215 (top-pos (save-excursion
216 (calc-cursor-stack-index top-num)
217 (point)))
218 (bot-num (calc-locate-cursor-element (1- end)))
219 (bot-pos (save-excursion
220 (calc-cursor-stack-index (max 0 (1- bot-num)))
221 (point)))
222 (num (- top-num bot-num -1))
223 (str (buffer-substring top-pos bot-pos))
224 (calcval (calc-top-list num bot-num))
225 (cval (cdr (assq register calc-register-alist))))
226 (if (not cval)
227 (calc-set-register register str calcval)
228 (if prepend
229 (calc-set-register
230 register
231 (concat str (car cval))
232 (append calcval (cdr cval)))
233 (calc-set-register
234 register
235 (concat (car cval) str)
236 (append (cdr cval) calcval))))
237 (if delete-flag
238 (calc-wrapper
239 (calc-pop-stack num bot-num)))))
241 (defun calc-append-to-register (register start end &optional delete-flag)
242 "Copy the lines in the region to the end of register REGISTER.
243 With prefix arg, also delete the region.
245 Interactively, reads the register using `register-read-with-preview'."
246 (interactive (list (register-read-with-preview "Append to register: ")
247 (region-beginning) (region-end)
248 current-prefix-arg))
249 (if (eq major-mode 'calc-mode)
250 (calc-add-to-register register start end nil delete-flag)
251 (append-to-register register start end delete-flag)))
253 (defun calc-prepend-to-register (register start end &optional delete-flag)
254 "Copy the lines in the region to the beginning of register REGISTER.
255 With prefix arg, also delete the region.
257 Interactively, reads the register using `register-read-with-preview'."
258 (interactive (list (register-read-with-preview "Prepend to register: ")
259 (region-beginning) (region-end)
260 current-prefix-arg))
261 (if (eq major-mode 'calc-mode)
262 (calc-add-to-register register start end t delete-flag)
263 (prepend-to-register register start end delete-flag)))
267 (defun calc-clean-newlines (s)
268 (cond
270 ;; Omit leading/trailing whitespace
271 ((or (string-match "\\`[ \n\r]+\\([^\001]*\\)\\'" s)
272 (string-match "\\`\\([^\001]*\\)[ \n\r]+\\'" s))
273 (calc-clean-newlines (math-match-substring s 1)))
275 ;; Convert newlines to commas
276 ((string-match "\\`\\(.*\\)[\n\r]+\\([^\001]*\\)\\'" s)
277 (calc-clean-newlines (concat (math-match-substring s 1) ","
278 (math-match-substring s 2))))
280 (t s)))
283 (defun calc-do-grab-region (top bot arg)
284 (when (memq major-mode '(calc-mode calc-trail-mode))
285 (error "This command works only in a regular text buffer"))
286 (let* ((from-buffer (current-buffer))
287 (calc-was-started (get-buffer-window "*Calculator*"))
288 (single nil)
289 data vals pos)
290 (if arg
291 (if (consp arg)
292 (setq single t)
293 (setq arg (prefix-numeric-value arg))
294 (if (= arg 0)
295 (setq top (point-at-bol)
296 bot (point-at-eol))
297 (save-excursion
298 (setq top (point))
299 (forward-line arg)
300 (if (> arg 0)
301 (setq bot (point))
302 (setq bot top
303 top (point)))))))
304 (setq data (buffer-substring top bot))
305 (calc)
306 (if single
307 (setq vals (math-read-expr data))
308 (setq vals (math-read-expr (concat "[" data "]")))
309 (and (eq (car-safe vals) 'vec)
310 (= (length vals) 2)
311 (eq (car-safe (nth 1 vals)) 'vec)
312 (setq vals (nth 1 vals))))
313 (if (eq (car-safe vals) 'error)
314 (progn
315 (if calc-was-started
316 (pop-to-buffer from-buffer)
317 (calc-quit t)
318 (switch-to-buffer from-buffer))
319 (goto-char top)
320 (forward-char (+ (nth 1 vals) (if single 0 1)))
321 (error (nth 2 vals))))
322 (calc-slow-wrapper
323 (calc-enter-result 0 "grab" vals))))
326 (defun calc-do-grab-rectangle (top bot arg &optional reduce)
327 (and (memq major-mode '(calc-mode calc-trail-mode))
328 (error "This command works only in a regular text buffer"))
329 (let* ((col1 (save-excursion (goto-char top) (current-column)))
330 (col2 (save-excursion (goto-char bot) (current-column)))
331 (from-buffer (current-buffer))
332 (calc-was-started (get-buffer-window "*Calculator*"))
333 data mat vals lnum pt pos)
334 (if (= col1 col2)
335 (save-excursion
336 (unless (= col1 0)
337 (error "Point and mark must be at beginning of line, or define a rectangle"))
338 (goto-char top)
339 (while (< (point) bot)
340 (setq pt (point))
341 (forward-line 1)
342 (setq data (cons (buffer-substring pt (1- (point))) data)))
343 (setq data (nreverse data)))
344 (setq data (extract-rectangle top bot)))
345 (calc)
346 (setq mat (list 'vec)
347 lnum 0)
348 (when arg
349 (setq arg (if (consp arg) 0 (prefix-numeric-value arg))))
350 (while data
351 (if (natnump arg)
352 (progn
353 (if (= arg 0)
354 (setq arg 1000000))
355 (setq pos 0
356 vals (list 'vec))
357 (let ((w (length (car data)))
358 j v)
359 (while (< pos w)
360 (setq j (+ pos arg)
361 v (if (>= j w)
362 (math-read-expr (substring (car data) pos))
363 (math-read-expr (substring (car data) pos j))))
364 (if (eq (car-safe v) 'error)
365 (setq vals v w 0)
366 (setq vals (nconc vals (list v))
367 pos j)))))
368 (if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
369 (car data))
370 (setq vals (list 'vec (string-to-number (car data))))
371 (if (and (null arg)
372 (string-match "[[{][^][{}]*[]}]" (car data)))
373 (setq pos (match-beginning 0)
374 vals (math-read-expr (math-match-substring (car data) 0)))
375 (let ((s (if (string-match
376 "\\`\\([0-9]+:[ \t]\\)?\\(.*[^, \t]\\)[, \t]*\\'"
377 (car data))
378 (math-match-substring (car data) 2)
379 (car data))))
380 (setq pos -1
381 vals (math-read-expr (concat "[" s "]")))
382 (if (eq (car-safe vals) 'error)
383 (let ((v2 (math-read-expr s)))
384 (unless (eq (car-safe v2) 'error)
385 (setq vals (list 'vec v2)))))))))
386 (if (eq (car-safe vals) 'error)
387 (progn
388 (if calc-was-started
389 (pop-to-buffer from-buffer)
390 (calc-quit t)
391 (switch-to-buffer from-buffer))
392 (goto-char top)
393 (forward-line lnum)
394 (forward-char (+ (nth 1 vals) (min col1 col2) pos))
395 (error (nth 2 vals))))
396 (unless (equal vals '(vec))
397 (setq mat (cons vals mat)))
398 (setq data (cdr data)
399 lnum (1+ lnum)))
400 (calc-slow-wrapper
401 (if reduce
402 (calc-enter-result 0 "grb+" (list reduce '(var add var-add)
403 (nreverse mat)))
404 (calc-enter-result 0 "grab" (nreverse mat))))))
407 (defun calc-copy-to-buffer (nn)
408 "Copy the top of stack into an editing buffer."
409 (interactive "P")
410 (let ((thebuf (and (not (memq major-mode '(calc-mode calc-trail-mode)))
411 (current-buffer)))
412 (movept nil)
413 oldbuf newbuf)
414 (calc-wrapper
415 (save-excursion
416 (calc-force-refresh)
417 (let ((n (prefix-numeric-value nn))
418 (eat-lnums calc-line-numbering)
419 (big-offset (if (eq calc-language 'big) 1 0))
420 top bot)
421 (setq oldbuf (current-buffer)
422 newbuf (or thebuf
423 (calc-find-writable-buffer (buffer-list) 0)
424 (calc-find-writable-buffer (buffer-list) 1)
425 (error "No other buffer")))
426 (cond ((and (or (null nn)
427 (consp nn))
428 (= (calc-substack-height 0)
429 (- (1- (calc-substack-height 1)) big-offset)))
430 (calc-cursor-stack-index 1)
431 (if (looking-at
432 (if calc-line-numbering "[0-9]+: *[^ \n]" " *[^ \n]"))
433 (goto-char (1- (match-end 0))))
434 (setq eat-lnums nil
435 top (point))
436 (calc-cursor-stack-index 0)
437 (setq bot (- (1- (point)) big-offset)))
438 ((> n 0)
439 (calc-cursor-stack-index n)
440 (setq top (point))
441 (calc-cursor-stack-index 0)
442 (setq bot (- (point) big-offset)))
443 ((< n 0)
444 (calc-cursor-stack-index (- n))
445 (setq top (point))
446 (calc-cursor-stack-index (1- (- n)))
447 (setq bot (point)))
449 (goto-char (point-min))
450 (forward-line 1)
451 (setq top (point))
452 (calc-cursor-stack-index 0)
453 (setq bot (point))))
454 (with-current-buffer newbuf
455 (if (consp nn)
456 (kill-region (region-beginning) (region-end)))
457 (push-mark (point) t)
458 (if (and overwrite-mode (not (consp nn)))
459 (calc-overwrite-string (with-current-buffer oldbuf
460 (buffer-substring top bot))
461 eat-lnums)
462 (or (bolp) (setq eat-lnums nil))
463 (insert-buffer-substring oldbuf top bot)
464 (and eat-lnums
465 (let ((n 1))
466 (while (and (> (point) (mark))
467 (progn
468 (forward-line -1)
469 (>= (point) (mark))))
470 (delete-char 4)
471 (setq n (1+ n)))
472 (forward-line n))))
473 (when thebuf
474 (setq movept (point)))
475 (when (get-buffer-window (current-buffer))
476 (set-window-point (get-buffer-window (current-buffer))
477 (point)))))))
478 (when movept
479 (goto-char movept))
480 (when (and (consp nn)
481 (not thebuf))
482 (calc-quit t)
483 (switch-to-buffer newbuf))))
485 (defun calc-overwrite-string (str eat-lnums)
486 (when (string-match "\n\\'" str)
487 (setq str (substring str 0 -1)))
488 (when eat-lnums
489 (setq str (substring str 4)))
490 (if (and (string-match "\\`[-+]?[0-9.]+\\(e-?[0-9]+\\)?\\'" str)
491 (looking-at "[-+]?[0-9.]+\\(e-?[0-9]+\\)?"))
492 (progn
493 (delete-region (point) (match-end 0))
494 (insert str))
495 (let ((i 0))
496 (while (< i (length str))
497 (if (= (setq last-command-event (aref str i)) ?\n)
498 (or (= i (1- (length str)))
499 (let ((pt (point)))
500 (end-of-line)
501 (delete-region pt (point))
502 (if (eobp)
503 (insert "\n")
504 (forward-char 1))
505 (if eat-lnums (setq i (+ i 4)))))
506 (self-insert-command 1))
507 (setq i (1+ i))))))
509 ;; First, require that buffer is visible and does not begin with "*"
510 ;; Second, require only that it not begin with "*Calc"
511 (defun calc-find-writable-buffer (buf mode)
512 (and buf
513 (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
514 (buffer-name (car buf)))
515 (and (= mode 0)
516 (or (string-match "\\`\\*.*" (buffer-name (car buf)))
517 (not (get-buffer-window (car buf))))))
518 (calc-find-writable-buffer (cdr buf) mode)
519 (car buf))))
522 (defun calc-edit (n)
523 (interactive "p")
524 (calc-slow-wrapper
525 (when (eq n 0)
526 (setq n (calc-stack-size)))
527 (let* ((flag nil)
528 (allow-ret (> n 1))
529 (list (math-showing-full-precision
530 (mapcar (if (> n 1)
531 (function (lambda (x)
532 (math-format-flat-expr x 0)))
533 (function
534 (lambda (x)
535 (if (math-vectorp x) (setq allow-ret t))
536 (math-format-nice-expr x (frame-width)))))
537 (if (> n 0)
538 (calc-top-list n)
539 (calc-top-list 1 (- n)))))))
540 (calc-edit-mode (list 'calc-finish-stack-edit (or flag n)) allow-ret)
541 (while list
542 (insert (car list) "\n")
543 (setq list (cdr list)))))
544 (calc-show-edit-buffer))
546 (defun calc-alg-edit (str)
547 (calc-edit-mode '(calc-finish-stack-edit 0))
548 (calc-show-edit-buffer)
549 (insert str "\n")
550 (backward-char 1)
551 (calc-set-command-flag 'do-edit))
553 (defvar calc-edit-mode-map
554 (let ((map (make-sparse-keymap)))
555 (define-key map "\n" 'calc-edit-finish)
556 (define-key map "\r" 'calc-edit-return)
557 (define-key map "\C-c\C-c" 'calc-edit-finish)
558 map)
559 "Keymap for use by the calc-edit command.")
561 (defvar calc-original-buffer)
562 (defvar calc-return-buffer)
563 (defvar calc-one-window)
564 (defvar calc-edit-handler)
565 (defvar calc-restore-trail)
566 (defvar calc-allow-ret)
567 (defvar calc-edit-top)
569 (defun calc-edit-mode (&optional handler allow-ret title)
570 "Calculator editing mode. Press RET, LFD, or C-c C-c to finish.
571 To cancel the edit, simply kill the *Calc Edit* buffer."
572 (interactive)
573 (unless handler
574 (error "This command can be used only indirectly through calc-edit"))
575 (let ((oldbuf (current-buffer))
576 (buf (get-buffer-create "*Calc Edit*")))
577 (set-buffer buf)
578 (kill-all-local-variables)
579 (use-local-map calc-edit-mode-map)
580 (setq buffer-read-only nil)
581 (setq truncate-lines nil)
582 (setq major-mode 'calc-edit-mode)
583 (setq mode-name "Calc Edit")
584 (run-mode-hooks 'calc-edit-mode-hook)
585 (make-local-variable 'calc-original-buffer)
586 (setq calc-original-buffer oldbuf)
587 (make-local-variable 'calc-return-buffer)
588 (setq calc-return-buffer oldbuf)
589 (make-local-variable 'calc-one-window)
590 (setq calc-one-window (and (one-window-p t) pop-up-windows))
591 (make-local-variable 'calc-edit-handler)
592 (setq calc-edit-handler handler)
593 (make-local-variable 'calc-restore-trail)
594 (setq calc-restore-trail (get-buffer-window (calc-trail-buffer)))
595 (make-local-variable 'calc-allow-ret)
596 (setq calc-allow-ret allow-ret)
597 (let ((inhibit-read-only t))
598 (erase-buffer))
599 (add-hook 'kill-buffer-hook (lambda ()
600 (let ((calc-edit-handler nil))
601 (calc-edit-finish t))
602 (message "(Canceled)")) t t)
603 (insert (propertize
604 (concat
605 (or title title "Calc Edit Mode. ")
606 "Press `C-c C-c'"
607 (if allow-ret "" " or RET")
608 " to finish, `C-x k RET' to cancel.\n\n")
609 'font-lock-face 'italic 'read-only t 'rear-nonsticky t 'front-sticky t))
610 (make-local-variable 'calc-edit-top)
611 (setq calc-edit-top (point))))
612 (put 'calc-edit-mode 'mode-class 'special)
614 (defun calc-show-edit-buffer ()
615 (let ((buf (current-buffer)))
616 (if (and (one-window-p t) pop-up-windows)
617 (pop-to-buffer (get-buffer-create "*Calc Edit*"))
618 (and calc-embedded-info (get-buffer-window (aref calc-embedded-info 1))
619 (select-window (get-buffer-window (aref calc-embedded-info 1))))
620 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
621 (setq calc-return-buffer buf)
622 (if (and (< (window-width) (frame-width))
623 calc-display-trail)
624 (let ((win (get-buffer-window (calc-trail-buffer))))
625 (if win
626 (delete-window win))))
627 (set-buffer-modified-p nil)
628 (goto-char calc-edit-top)))
630 (defun calc-edit-return ()
631 (interactive)
632 (if (and (boundp 'calc-allow-ret) calc-allow-ret)
633 (newline)
634 (calc-edit-finish)))
636 ;; The variable calc-edit-disp-trail is local to calc-edit finish, but
637 ;; is used by calc-finish-selection-edit and calc-finish-stack-edit.
638 (defvar calc-edit-disp-trail)
640 (defun calc-edit-finish (&optional keep)
641 "Finish calc-edit mode. Parse buffer contents and push them on the stack."
642 (interactive "P")
643 (message "Working...")
644 (or (and (boundp 'calc-original-buffer)
645 (boundp 'calc-return-buffer)
646 (boundp 'calc-one-window)
647 (boundp 'calc-edit-handler)
648 (boundp 'calc-restore-trail)
649 (eq major-mode 'calc-edit-mode))
650 (error "This command is valid only in buffers created by calc-edit"))
651 (let ((buf (current-buffer))
652 (original calc-original-buffer)
653 (return calc-return-buffer)
654 (one-window calc-one-window)
655 (calc-edit-disp-trail calc-restore-trail))
656 (save-excursion
657 (when (or (null (buffer-name original))
658 (progn
659 (set-buffer original)
660 (not (eq major-mode 'calc-mode))))
661 (error "Original calculator buffer has been corrupted")))
662 (goto-char calc-edit-top)
663 (if (buffer-modified-p)
664 (eval calc-edit-handler))
665 (if (and one-window (not (one-window-p t)))
666 (delete-window))
667 (if (get-buffer-window return)
668 (select-window (get-buffer-window return))
669 (switch-to-buffer return))
670 (if keep
671 (bury-buffer buf)
672 (kill-buffer buf))
673 (if calc-edit-disp-trail
674 (calc-wrapper
675 (calc-trail-display 1 t)))
676 (message "")))
678 (defun calc-edit-cancel ()
679 "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
680 (interactive)
681 (let ((calc-edit-handler nil))
682 (calc-edit-finish))
683 (message "(Canceled)"))
685 (defun calc-finish-stack-edit (num)
686 (let ((buf (current-buffer))
687 (str (buffer-substring calc-edit-top (point-max)))
688 (start (point))
689 pos)
690 (if (and (integerp num) (> num 1))
691 (while (setq pos (string-match "\n." str))
692 (aset str pos ?\,)))
693 (switch-to-buffer calc-original-buffer)
694 (let ((vals (let ((calc-language nil)
695 (math-expr-opers (math-standard-ops)))
696 (and (string-match "[^\n\t ]" str)
697 (math-read-exprs str)))))
698 (when (eq (car-safe vals) 'error)
699 (switch-to-buffer buf)
700 (goto-char (+ start (nth 1 vals)))
701 (error (nth 2 vals)))
702 (calc-wrapper
703 (if (symbolp num)
704 (progn
705 (set num (car vals))
706 (calc-refresh-evaltos num))
707 (if calc-edit-disp-trail
708 (calc-trail-display 1 t))
709 (and vals
710 (let ((calc-simplify-mode (if (eq last-command-event ?\C-j)
711 'none
712 calc-simplify-mode)))
713 (if (>= num 0)
714 (calc-enter-result num "edit" vals)
715 (calc-enter-result 1 "edit" vals (- num))))))))))
717 (provide 'calc-yank)
719 ;; Local variables:
720 ;; generated-autoload-file: "calc-loaddefs.el"
721 ;; End:
723 ;;; calc-yank.el ends here