1 ;;; calc-yank.el --- kill-ring functionality for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file is autoloaded from calc-ext.el.
33 ;;; Kill ring commands.
35 (defun calc-kill (nn &optional no-delete
)
37 (if (eq major-mode
'calc-mode
)
40 (calc-set-command-flag 'no-align
)
41 (let ((num (max (calc-locate-cursor-element (point)) 1))
42 (n (prefix-numeric-value nn
)))
49 (calc-check-stack num
)
50 (let ((stuff (calc-top-list n
(- num n -
1))))
51 (calc-cursor-stack-index num
)
52 (let ((first (point)))
53 (calc-cursor-stack-index (- num n
))
55 (backward-char 1)) ; don't include newline for raw C-k
56 (copy-region-as-kill first
(point))
58 (calc-pop-stack n
(- num n -
1))))
59 (setq calc-last-kill
(cons (car kill-ring
) stuff
)))))
62 (defun calc-force-refresh ()
63 (if (or calc-executing-macro calc-display-dirty
)
64 (let ((calc-executing-macro nil
))
67 (defun calc-locate-cursor-element (pt)
69 (goto-char (point-max))
70 (calc-locate-cursor-scan (- calc-stack-top
) calc-stack pt
)))
72 (defun calc-locate-cursor-scan (n stack pt
)
73 (if (or (<= (point) pt
)
76 (forward-line (- (nth 1 (car stack
))))
77 (calc-locate-cursor-scan (1+ n
) (cdr stack
) pt
)))
79 (defun calc-kill-region (top bot
&optional no-delete
)
81 (if (eq major-mode
'calc-mode
)
84 (calc-set-command-flag 'no-align
)
85 (let* ((top-num (calc-locate-cursor-element top
))
86 (top-pos (save-excursion
87 (calc-cursor-stack-index top-num
)
89 (bot-num (calc-locate-cursor-element (1- bot
)))
90 (bot-pos (save-excursion
91 (calc-cursor-stack-index (max 0 (1- bot-num
)))
93 (num (- top-num bot-num -
1)))
94 (copy-region-as-kill top-pos bot-pos
)
95 (setq calc-last-kill
(cons (car kill-ring
)
96 (calc-top-list num bot-num
)))
98 (calc-pop-stack num bot-num
))))
100 (copy-region-as-kill top bot
)
101 (kill-region top bot
))))
103 (defun calc-copy-as-kill (n)
107 (defun calc-copy-region-as-kill (top bot
)
109 (calc-kill-region top bot t
))
111 ;; This function uses calc-last-kill if possible to get an exact result,
112 ;; otherwise it just parses the yanked string.
113 ;; Modified to use Emacs 19 extended concept of kill-ring. -- daveg 12/15/96
118 (calc-pop-push-record-list
120 (let ((thing (if (fboundp 'current-kill
)
122 (car kill-ring-yank-pointer
))))
123 (if (eq (car-safe calc-last-kill
) thing
)
126 (let ((val (math-read-exprs (calc-clean-newlines thing
))))
127 (if (eq (car-safe val
) 'error
)
129 (setq val
(math-read-exprs thing
))
130 (if (eq (car-safe val
) 'error
)
131 (error "Bad format in yanked data")
135 ;;; The Calc set- and get-register commands are modified versions of functions
138 (defvar calc-register-alist nil
139 "Alist of elements (NAME . (TEXT . CALCVAL)).
140 NAME is a character (a number).
141 TEXT and CALCVAL are the TEXT and internal structure of stack entries.")
143 (defun calc-set-register (register text calcval
)
144 "Set the contents of the Calc register REGISTER to (TEXT . CALCVAL),
145 as well as set the contents of the Emacs register REGISTER to TEXT."
146 (set-register register text
)
147 (let ((aelt (assq register calc-register-alist
)))
149 (setcdr aelt
(cons text calcval
))
150 (push (cons register
(cons text calcval
)) calc-register-alist
))))
152 (defun calc-get-register (reg)
153 "Return the CALCVAL portion of the contents of the Calc register REG,
154 unless the TEXT portion doesn't match the contents of the Emacs register REG,
155 in which case either return the contents of the Emacs register (if it is
157 (let ((cval (cdr (assq reg calc-register-alist
)))
158 (val (cdr (assq reg register-alist
))))
160 (if (and (stringp (car cval
))
161 (string= (car cval
) val
))
165 (defun calc-copy-to-register (register start end
&optional delete-flag
)
166 "Copy the lines in the region into register REGISTER.
167 With prefix arg, delete as well."
168 (interactive "cCopy to register: \nr\nP")
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."
188 (interactive "cInsert register: ")
189 (if (eq major-mode
'calc-mode
)
190 (let ((val (calc-get-register register
)))
192 (calc-pop-push-record-list
195 (error "Bad format in register data")
198 (let ((nval (math-read-exprs (calc-clean-newlines val
))))
199 (if (eq (car-safe nval
) 'error
)
201 (setq nval
(math-read-exprs val
))
202 (if (eq (car-safe nval
) 'error
)
203 (error "Bad format in register data")
206 (insert-register register
)))
208 (defun calc-add-to-register (register start end prepend delete-flag
)
209 "Add the lines in the region to register REGISTER.
210 If PREPEND is non-nil, add them to the beginning of the register,
211 otherwise the end. If DELETE-FLAG is non-nil, also delete the region."
212 (let* ((top-num (calc-locate-cursor-element start
))
213 (top-pos (save-excursion
214 (calc-cursor-stack-index top-num
)
216 (bot-num (calc-locate-cursor-element (1- end
)))
217 (bot-pos (save-excursion
218 (calc-cursor-stack-index (max 0 (1- bot-num
)))
220 (num (- top-num bot-num -
1))
221 (str (buffer-substring top-pos bot-pos
))
222 (calcval (calc-top-list num bot-num
))
223 (cval (cdr (assq register calc-register-alist
))))
225 (calc-set-register register str calcval
)
229 (concat str
(car cval
))
230 (append calcval
(cdr cval
)))
233 (concat (car cval
) str
)
234 (append (cdr cval
) calcval
))))
237 (calc-pop-stack num bot-num
)))))
239 (defun calc-append-to-register (register start end
&optional delete-flag
)
240 "Copy the lines in the region to the end of register REGISTER.
241 With prefix arg, also delete the region."
242 (interactive "cAppend to register: \nr\nP")
243 (if (eq major-mode
'calc-mode
)
244 (calc-add-to-register register start end nil delete-flag
)
245 (append-to-register register start end delete-flag
)))
247 (defun calc-prepend-to-register (register start end
&optional delete-flag
)
248 "Copy the lines in the region to the beginning of register REGISTER.
249 With prefix arg, also delete the region."
250 (interactive "cPrepend to register: \nr\nP")
251 (if (eq major-mode
'calc-mode
)
252 (calc-add-to-register register start end t delete-flag
)
253 (prepend-to-register register start end delete-flag
)))
257 (defun calc-clean-newlines (s)
260 ;; Omit leading/trailing whitespace
261 ((or (string-match "\\`[ \n\r]+\\([^\001]*\\)\\'" s
)
262 (string-match "\\`\\([^\001]*\\)[ \n\r]+\\'" s
))
263 (calc-clean-newlines (math-match-substring s
1)))
265 ;; Convert newlines to commas
266 ((string-match "\\`\\(.*\\)[\n\r]+\\([^\001]*\\)\\'" s
)
267 (calc-clean-newlines (concat (math-match-substring s
1) ","
268 (math-match-substring s
2))))
273 (defun calc-do-grab-region (top bot arg
)
274 (when (memq major-mode
'(calc-mode calc-trail-mode
))
275 (error "This command works only in a regular text buffer"))
276 (let* ((from-buffer (current-buffer))
277 (calc-was-started (get-buffer-window "*Calculator*"))
283 (setq arg
(prefix-numeric-value arg
))
297 (setq data
(buffer-substring top bot
))
300 (setq vals
(math-read-expr data
))
301 (setq vals
(math-read-expr (concat "[" data
"]")))
302 (and (eq (car-safe vals
) 'vec
)
304 (eq (car-safe (nth 1 vals
)) 'vec
)
305 (setq vals
(nth 1 vals
))))
306 (if (eq (car-safe vals
) 'error
)
309 (pop-to-buffer from-buffer
)
311 (switch-to-buffer from-buffer
))
313 (forward-char (+ (nth 1 vals
) (if single
0 1)))
314 (error (nth 2 vals
))))
316 (calc-enter-result 0 "grab" vals
))))
319 (defun calc-do-grab-rectangle (top bot arg
&optional reduce
)
320 (and (memq major-mode
'(calc-mode calc-trail-mode
))
321 (error "This command works only in a regular text buffer"))
322 (let* ((col1 (save-excursion (goto-char top
) (current-column)))
323 (col2 (save-excursion (goto-char bot
) (current-column)))
324 (from-buffer (current-buffer))
325 (calc-was-started (get-buffer-window "*Calculator*"))
326 data mat vals lnum pt pos
)
330 (error "Point and mark must be at beginning of line, or define a rectangle"))
332 (while (< (point) bot
)
335 (setq data
(cons (buffer-substring pt
(1- (point))) data
)))
336 (setq data
(nreverse data
)))
337 (setq data
(extract-rectangle top bot
)))
339 (setq mat
(list 'vec
)
342 (setq arg
(if (consp arg
) 0 (prefix-numeric-value arg
))))
350 (let ((w (length (car data
)))
355 (math-read-expr (substring (car data
) pos
))
356 (math-read-expr (substring (car data
) pos j
))))
357 (if (eq (car-safe v
) 'error
)
359 (setq vals
(nconc vals
(list v
))
361 (if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
363 (setq vals
(list 'vec
(string-to-number (car data
))))
365 (string-match "[[{][^][{}]*[]}]" (car data
)))
366 (setq pos
(match-beginning 0)
367 vals
(math-read-expr (math-match-substring (car data
) 0)))
368 (let ((s (if (string-match
369 "\\`\\([0-9]+:[ \t]\\)?\\(.*[^, \t]\\)[, \t]*\\'"
371 (math-match-substring (car data
) 2)
374 vals
(math-read-expr (concat "[" s
"]")))
375 (if (eq (car-safe vals
) 'error
)
376 (let ((v2 (math-read-expr s
)))
377 (unless (eq (car-safe v2
) 'error
)
378 (setq vals
(list 'vec v2
)))))))))
379 (if (eq (car-safe vals
) 'error
)
382 (pop-to-buffer from-buffer
)
384 (switch-to-buffer from-buffer
))
387 (forward-char (+ (nth 1 vals
) (min col1 col2
) pos
))
388 (error (nth 2 vals
))))
389 (unless (equal vals
'(vec))
390 (setq mat
(cons vals mat
)))
391 (setq data
(cdr data
)
395 (calc-enter-result 0 "grb+" (list reduce
'(var add var-add
)
397 (calc-enter-result 0 "grab" (nreverse mat
))))))
400 (defun calc-copy-to-buffer (nn)
401 "Copy the top of stack into an editing buffer."
403 (let ((thebuf (and (not (memq major-mode
'(calc-mode calc-trail-mode
)))
410 (let ((n (prefix-numeric-value nn
))
411 (eat-lnums calc-line-numbering
)
412 (big-offset (if (eq calc-language
'big
) 1 0))
414 (setq oldbuf
(current-buffer)
416 (calc-find-writable-buffer (buffer-list) 0)
417 (calc-find-writable-buffer (buffer-list) 1)
418 (error "No other buffer")))
419 (cond ((and (or (null nn
)
421 (= (calc-substack-height 0)
422 (- (1- (calc-substack-height 1)) big-offset
)))
423 (calc-cursor-stack-index 1)
425 (if calc-line-numbering
"[0-9]+: *[^ \n]" " *[^ \n]"))
426 (goto-char (1- (match-end 0))))
429 (calc-cursor-stack-index 0)
430 (setq bot
(- (1- (point)) big-offset
)))
432 (calc-cursor-stack-index n
)
434 (calc-cursor-stack-index 0)
435 (setq bot
(- (point) big-offset
)))
437 (calc-cursor-stack-index (- n
))
439 (calc-cursor-stack-index (1- (- n
)))
442 (goto-char (point-min))
445 (calc-cursor-stack-index 0)
447 (with-current-buffer newbuf
449 (kill-region (region-beginning) (region-end)))
450 (push-mark (point) t
)
451 (if (and overwrite-mode
(not (consp nn
)))
452 (calc-overwrite-string (with-current-buffer oldbuf
453 (buffer-substring top bot
))
455 (or (bolp) (setq eat-lnums nil
))
456 (insert-buffer-substring oldbuf top bot
)
459 (while (and (> (point) (mark))
462 (>= (point) (mark))))
467 (setq movept
(point)))
468 (when (get-buffer-window (current-buffer))
469 (set-window-point (get-buffer-window (current-buffer))
473 (when (and (consp nn
)
476 (switch-to-buffer newbuf
))))
478 (defun calc-overwrite-string (str eat-lnums
)
479 (when (string-match "\n\\'" str
)
480 (setq str
(substring str
0 -
1)))
482 (setq str
(substring str
4)))
483 (if (and (string-match "\\`[-+]?[0-9.]+\\(e-?[0-9]+\\)?\\'" str
)
484 (looking-at "[-+]?[0-9.]+\\(e-?[0-9]+\\)?"))
486 (delete-region (point) (match-end 0))
489 (while (< i
(length str
))
490 (if (= (setq last-command-event
(aref str i
)) ?
\n)
491 (or (= i
(1- (length str
)))
494 (delete-region pt
(point))
498 (if eat-lnums
(setq i
(+ i
4)))))
499 (self-insert-command 1))
502 ;; First, require that buffer is visible and does not begin with "*"
503 ;; Second, require only that it not begin with "*Calc"
504 (defun calc-find-writable-buffer (buf mode
)
506 (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
507 (buffer-name (car buf
)))
509 (or (string-match "\\`\\*.*" (buffer-name (car buf
)))
510 (not (get-buffer-window (car buf
))))))
511 (calc-find-writable-buffer (cdr buf
) mode
)
519 (setq n
(calc-stack-size)))
522 (list (math-showing-full-precision
524 (function (lambda (x)
525 (math-format-flat-expr x
0)))
528 (if (math-vectorp x
) (setq allow-ret t
))
529 (math-format-nice-expr x
(frame-width)))))
532 (calc-top-list 1 (- n
)))))))
533 (calc-edit-mode (list 'calc-finish-stack-edit
(or flag n
)) allow-ret
)
535 (insert (car list
) "\n")
536 (setq list
(cdr list
)))))
537 (calc-show-edit-buffer))
539 (defun calc-alg-edit (str)
540 (calc-edit-mode '(calc-finish-stack-edit 0))
541 (calc-show-edit-buffer)
544 (calc-set-command-flag 'do-edit
))
546 (defvar calc-edit-mode-map
547 (let ((map (make-sparse-keymap)))
548 (define-key map
"\n" 'calc-edit-finish
)
549 (define-key map
"\r" 'calc-edit-return
)
550 (define-key map
"\C-c\C-c" 'calc-edit-finish
)
552 "Keymap for use by the calc-edit command.")
554 (defvar calc-original-buffer
)
555 (defvar calc-return-buffer
)
556 (defvar calc-one-window
)
557 (defvar calc-edit-handler
)
558 (defvar calc-restore-trail
)
559 (defvar calc-allow-ret
)
560 (defvar calc-edit-top
)
562 (defun calc-edit-mode (&optional handler allow-ret title
)
563 "Calculator editing mode. Press RET, LFD, or C-c C-c to finish.
564 To cancel the edit, simply kill the *Calc Edit* buffer."
567 (error "This command can be used only indirectly through calc-edit"))
568 (let ((oldbuf (current-buffer))
569 (buf (get-buffer-create "*Calc Edit*")))
571 (kill-all-local-variables)
572 (use-local-map calc-edit-mode-map
)
573 (setq buffer-read-only nil
)
574 (setq truncate-lines nil
)
575 (setq major-mode
'calc-edit-mode
)
576 (setq mode-name
"Calc Edit")
577 (run-mode-hooks 'calc-edit-mode-hook
)
578 (make-local-variable 'calc-original-buffer
)
579 (setq calc-original-buffer oldbuf
)
580 (make-local-variable 'calc-return-buffer
)
581 (setq calc-return-buffer oldbuf
)
582 (make-local-variable 'calc-one-window
)
583 (setq calc-one-window
(and (one-window-p t
) pop-up-windows
))
584 (make-local-variable 'calc-edit-handler
)
585 (setq calc-edit-handler handler
)
586 (make-local-variable 'calc-restore-trail
)
587 (setq calc-restore-trail
(get-buffer-window (calc-trail-buffer)))
588 (make-local-variable 'calc-allow-ret
)
589 (setq calc-allow-ret allow-ret
)
590 (let ((inhibit-read-only t
))
592 (add-hook 'kill-buffer-hook
(lambda ()
593 (let ((calc-edit-handler nil
))
594 (calc-edit-finish t
))
595 (message "(Cancelled)")) t t
)
598 (or title title
"Calc Edit Mode. ")
600 (if allow-ret
"" " or RET")
601 " to finish, `C-x k RET' to cancel.\n\n")
602 'font-lock-face
'italic
'read-only t
'rear-nonsticky t
'front-sticky t
))
603 (make-local-variable 'calc-edit-top
)
604 (setq calc-edit-top
(point))))
605 (put 'calc-edit-mode
'mode-class
'special
)
607 (defun calc-show-edit-buffer ()
608 (let ((buf (current-buffer)))
609 (if (and (one-window-p t
) pop-up-windows
)
610 (pop-to-buffer (get-buffer-create "*Calc Edit*"))
611 (and calc-embedded-info
(get-buffer-window (aref calc-embedded-info
1))
612 (select-window (get-buffer-window (aref calc-embedded-info
1))))
613 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
614 (setq calc-return-buffer buf
)
615 (if (and (< (window-width) (frame-width))
617 (let ((win (get-buffer-window (calc-trail-buffer))))
619 (delete-window win
))))
620 (set-buffer-modified-p nil
)
621 (goto-char calc-edit-top
)))
623 (defun calc-edit-return ()
625 (if (and (boundp 'calc-allow-ret
) calc-allow-ret
)
629 ;; The variable calc-edit-disp-trail is local to calc-edit finish, but
630 ;; is used by calc-finish-selection-edit and calc-finish-stack-edit.
631 (defvar calc-edit-disp-trail
)
633 (defun calc-edit-finish (&optional keep
)
634 "Finish calc-edit mode. Parse buffer contents and push them on the stack."
636 (message "Working...")
637 (or (and (boundp 'calc-original-buffer
)
638 (boundp 'calc-return-buffer
)
639 (boundp 'calc-one-window
)
640 (boundp 'calc-edit-handler
)
641 (boundp 'calc-restore-trail
)
642 (eq major-mode
'calc-edit-mode
))
643 (error "This command is valid only in buffers created by calc-edit"))
644 (let ((buf (current-buffer))
645 (original calc-original-buffer
)
646 (return calc-return-buffer
)
647 (one-window calc-one-window
)
648 (calc-edit-disp-trail calc-restore-trail
))
650 (when (or (null (buffer-name original
))
652 (set-buffer original
)
653 (not (eq major-mode
'calc-mode
))))
654 (error "Original calculator buffer has been corrupted")))
655 (goto-char calc-edit-top
)
656 (if (buffer-modified-p)
657 (eval calc-edit-handler
))
658 (if (and one-window
(not (one-window-p t
)))
660 (if (get-buffer-window return
)
661 (select-window (get-buffer-window return
))
662 (switch-to-buffer return
))
666 (if calc-edit-disp-trail
668 (calc-trail-display 1 t
)))
671 (defun calc-edit-cancel ()
672 "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
674 (let ((calc-edit-handler nil
))
676 (message "(Cancelled)"))
678 (defun calc-finish-stack-edit (num)
679 (let ((buf (current-buffer))
680 (str (buffer-substring calc-edit-top
(point-max)))
683 (if (and (integerp num
) (> num
1))
684 (while (setq pos
(string-match "\n." str
))
686 (switch-to-buffer calc-original-buffer
)
687 (let ((vals (let ((calc-language nil
)
688 (math-expr-opers (math-standard-ops)))
689 (and (string-match "[^\n\t ]" str
)
690 (math-read-exprs str
)))))
691 (when (eq (car-safe vals
) 'error
)
692 (switch-to-buffer buf
)
693 (goto-char (+ start
(nth 1 vals
)))
694 (error (nth 2 vals
)))
699 (calc-refresh-evaltos num
))
700 (if calc-edit-disp-trail
701 (calc-trail-display 1 t
))
703 (let ((calc-simplify-mode (if (eq last-command-event ?\C-j
)
705 calc-simplify-mode
)))
707 (calc-enter-result num
"edit" vals
)
708 (calc-enter-result 1 "edit" vals
(- num
))))))))))
713 ;; generated-autoload-file: "calc-loaddefs.el"
716 ;; arch-tag: ca61019e-caca-4daa-b32c-b6afe372d5b5
717 ;;; calc-yank.el ends here