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/>.
27 ;; This file is autoloaded from calc-ext.el.
32 ;;; Kill ring commands.
34 (defun calc-kill (nn &optional no-delete
)
36 (if (eq major-mode
'calc-mode
)
39 (calc-set-command-flag 'no-align
)
40 (let ((num (max (calc-locate-cursor-element (point)) 1))
41 (n (prefix-numeric-value nn
)))
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
))
54 (backward-char 1)) ; don't include newline for raw C-k
55 (copy-region-as-kill first
(point))
57 (calc-pop-stack n
(- num n -
1))))
58 (setq calc-last-kill
(cons (car kill-ring
) stuff
)))))
61 (defun calc-force-refresh ()
62 (if (or calc-executing-macro calc-display-dirty
)
63 (let ((calc-executing-macro nil
))
66 (defun calc-locate-cursor-element (pt)
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
)
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
)
80 (if (eq major-mode
'calc-mode
)
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
)
88 (bot-num (calc-locate-cursor-element (1- bot
)))
89 (bot-pos (save-excursion
90 (calc-cursor-stack-index (max 0 (1- bot-num
)))
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
)))
97 (calc-pop-stack num bot-num
))))
99 (copy-region-as-kill top bot
)
100 (kill-region top bot
))))
102 (defun calc-copy-as-kill (n)
106 (defun calc-copy-region-as-kill (top bot
)
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
117 (calc-pop-push-record-list
119 (let ((thing (if (fboundp 'current-kill
)
121 (car kill-ring-yank-pointer
))))
122 (if (eq (car-safe calc-last-kill
) thing
)
125 (let ((val (math-read-exprs (calc-clean-newlines thing
))))
126 (if (eq (car-safe val
) 'error
)
128 (setq val
(math-read-exprs thing
))
129 (if (eq (car-safe val
) 'error
)
130 (error "Bad format in yanked data")
134 ;;; The Calc set- and get-register commands are modified versions of functions
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
153 (let ((cval (cdr (assq reg calc-register-alist
)))
154 (val (cdr (assq reg register-alist
))))
156 (if (and (stringp (car cval
))
157 (string= (car cval
) 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)
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
)
174 (bot-num (calc-locate-cursor-element (1- end
)))
175 (bot-pos (save-excursion
176 (calc-cursor-stack-index (max 0 (1- bot-num
)))
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
))
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
)))
194 (calc-pop-push-record-list
197 (error "Bad format in register data")
200 (let ((nval (math-read-exprs (calc-clean-newlines val
))))
201 (if (eq (car-safe nval
) 'error
)
203 (setq nval
(math-read-exprs val
))
204 (if (eq (car-safe nval
) 'error
)
205 (error "Bad format in register data")
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
)
218 (bot-num (calc-locate-cursor-element (1- end
)))
219 (bot-pos (save-excursion
220 (calc-cursor-stack-index (max 0 (1- bot-num
)))
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
))))
227 (calc-set-register register str calcval
)
231 (concat str
(car cval
))
232 (append calcval
(cdr cval
)))
235 (concat (car cval
) str
)
236 (append (cdr cval
) calcval
))))
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)
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)
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)
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))))
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*"))
293 (setq arg
(prefix-numeric-value arg
))
295 (setq top
(point-at-bol)
304 (setq data
(buffer-substring top bot
))
307 (setq vals
(math-read-expr data
))
308 (setq vals
(math-read-expr (concat "[" data
"]")))
309 (and (eq (car-safe vals
) 'vec
)
311 (eq (car-safe (nth 1 vals
)) 'vec
)
312 (setq vals
(nth 1 vals
))))
313 (if (eq (car-safe vals
) 'error
)
316 (pop-to-buffer from-buffer
)
318 (switch-to-buffer from-buffer
))
320 (forward-char (+ (nth 1 vals
) (if single
0 1)))
321 (error (nth 2 vals
))))
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
)
337 (error "Point and mark must be at beginning of line, or define a rectangle"))
339 (while (< (point) bot
)
342 (setq data
(cons (buffer-substring pt
(1- (point))) data
)))
343 (setq data
(nreverse data
)))
344 (setq data
(extract-rectangle top bot
)))
346 (setq mat
(list 'vec
)
349 (setq arg
(if (consp arg
) 0 (prefix-numeric-value arg
))))
357 (let ((w (length (car data
)))
362 (math-read-expr (substring (car data
) pos
))
363 (math-read-expr (substring (car data
) pos j
))))
364 (if (eq (car-safe v
) 'error
)
366 (setq vals
(nconc vals
(list v
))
368 (if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
370 (setq vals
(list 'vec
(string-to-number (car data
))))
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]*\\'"
378 (math-match-substring (car data
) 2)
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
)
389 (pop-to-buffer from-buffer
)
391 (switch-to-buffer from-buffer
))
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
)
402 (calc-enter-result 0 "grb+" (list reduce
'(var add var-add
)
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."
410 (let ((thebuf (and (not (memq major-mode
'(calc-mode calc-trail-mode
)))
417 (let ((n (prefix-numeric-value nn
))
418 (eat-lnums calc-line-numbering
)
419 (big-offset (if (eq calc-language
'big
) 1 0))
421 (setq oldbuf
(current-buffer)
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
)
428 (= (calc-substack-height 0)
429 (- (1- (calc-substack-height 1)) big-offset
)))
430 (calc-cursor-stack-index 1)
432 (if calc-line-numbering
"[0-9]+: *[^ \n]" " *[^ \n]"))
433 (goto-char (1- (match-end 0))))
436 (calc-cursor-stack-index 0)
437 (setq bot
(- (1- (point)) big-offset
)))
439 (calc-cursor-stack-index n
)
441 (calc-cursor-stack-index 0)
442 (setq bot
(- (point) big-offset
)))
444 (calc-cursor-stack-index (- n
))
446 (calc-cursor-stack-index (1- (- n
)))
449 (goto-char (point-min))
452 (calc-cursor-stack-index 0)
454 (with-current-buffer newbuf
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
))
462 (or (bolp) (setq eat-lnums nil
))
463 (insert-buffer-substring oldbuf top bot
)
466 (while (and (> (point) (mark))
469 (>= (point) (mark))))
474 (setq movept
(point)))
475 (when (get-buffer-window (current-buffer))
476 (set-window-point (get-buffer-window (current-buffer))
480 (when (and (consp nn
)
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)))
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]+\\)?"))
493 (delete-region (point) (match-end 0))
496 (while (< i
(length str
))
497 (if (= (setq last-command-event
(aref str i
)) ?
\n)
498 (or (= i
(1- (length str
)))
501 (delete-region pt
(point))
505 (if eat-lnums
(setq i
(+ i
4)))))
506 (self-insert-command 1))
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
)
513 (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
514 (buffer-name (car buf
)))
516 (or (string-match "\\`\\*.*" (buffer-name (car buf
)))
517 (not (get-buffer-window (car buf
))))))
518 (calc-find-writable-buffer (cdr buf
) mode
)
526 (setq n
(calc-stack-size)))
529 (list (math-showing-full-precision
531 (function (lambda (x)
532 (math-format-flat-expr x
0)))
535 (if (math-vectorp x
) (setq allow-ret t
))
536 (math-format-nice-expr x
(frame-width)))))
539 (calc-top-list 1 (- n
)))))))
540 (calc-edit-mode (list 'calc-finish-stack-edit
(or flag n
)) allow-ret
)
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)
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
)
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."
574 (error "This command can be used only indirectly through calc-edit"))
575 (let ((oldbuf (current-buffer))
576 (buf (get-buffer-create "*Calc Edit*")))
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
))
599 (add-hook 'kill-buffer-hook
(lambda ()
600 (let ((calc-edit-handler nil
))
601 (calc-edit-finish t
))
602 (message "(Canceled)")) t t
)
605 (or title title
"Calc Edit Mode. ")
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))
624 (let ((win (get-buffer-window (calc-trail-buffer))))
626 (delete-window win
))))
627 (set-buffer-modified-p nil
)
628 (goto-char calc-edit-top
)))
630 (defun calc-edit-return ()
632 (if (and (boundp 'calc-allow-ret
) calc-allow-ret
)
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."
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
))
657 (when (or (null (buffer-name original
))
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
)))
667 (if (get-buffer-window return
)
668 (select-window (get-buffer-window return
))
669 (switch-to-buffer return
))
673 (if calc-edit-disp-trail
675 (calc-trail-display 1 t
)))
678 (defun calc-edit-cancel ()
679 "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
681 (let ((calc-edit-handler nil
))
683 (message "(Canceled)"))
685 (defun calc-finish-stack-edit (num)
686 (let ((buf (current-buffer))
687 (str (buffer-substring calc-edit-top
(point-max)))
690 (if (and (integerp num
) (> num
1))
691 (while (setq pos
(string-match "\n." str
))
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
)))
706 (calc-refresh-evaltos num
))
707 (if calc-edit-disp-trail
708 (calc-trail-display 1 t
))
710 (let ((calc-simplify-mode (if (eq last-command-event ?\C-j
)
712 calc-simplify-mode
)))
714 (calc-enter-result num
"edit" vals
)
715 (calc-enter-result 1 "edit" vals
(- num
))))))))))
720 ;; generated-autoload-file: "calc-loaddefs.el"
723 ;;; calc-yank.el ends here