1 ;;; calc-yank.el --- kill-ring functionality for Calc
3 ;; Copyright (C) 1990-1993, 2001-2011 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 (let ((aelt (assq register calc-register-alist
)))
148 (setcdr aelt
(cons text calcval
))
149 (push (cons register
(cons text calcval
)) calc-register-alist
))))
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
156 (let ((cval (cdr (assq reg calc-register-alist
)))
157 (val (cdr (assq reg register-alist
))))
159 (if (and (stringp (car cval
))
160 (string= (car cval
) val
))
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 (interactive "cCopy to register: \nr\nP")
168 (if (eq major-mode
'calc-mode
)
169 (let* ((top-num (calc-locate-cursor-element start
))
170 (top-pos (save-excursion
171 (calc-cursor-stack-index top-num
)
173 (bot-num (calc-locate-cursor-element (1- end
)))
174 (bot-pos (save-excursion
175 (calc-cursor-stack-index (max 0 (1- bot-num
)))
177 (num (- top-num bot-num -
1))
178 (str (buffer-substring top-pos bot-pos
)))
179 (calc-set-register register str
(calc-top-list num bot-num
))
182 (calc-pop-stack num bot-num
))))
183 (copy-to-register register start end delete-flag
)))
185 (defun calc-insert-register (register)
186 "Insert the contents of register REGISTER."
187 (interactive "cInsert register: ")
188 (if (eq major-mode
'calc-mode
)
189 (let ((val (calc-get-register register
)))
191 (calc-pop-push-record-list
194 (error "Bad format in register data")
197 (let ((nval (math-read-exprs (calc-clean-newlines val
))))
198 (if (eq (car-safe nval
) 'error
)
200 (setq nval
(math-read-exprs val
))
201 (if (eq (car-safe nval
) 'error
)
202 (error "Bad format in register data")
205 (insert-register register
)))
207 (defun calc-add-to-register (register start end prepend delete-flag
)
208 "Add the lines in the region to register REGISTER.
209 If PREPEND is non-nil, add them to the beginning of the register,
210 otherwise the end. If DELETE-FLAG is non-nil, also delete the region."
211 (let* ((top-num (calc-locate-cursor-element start
))
212 (top-pos (save-excursion
213 (calc-cursor-stack-index top-num
)
215 (bot-num (calc-locate-cursor-element (1- end
)))
216 (bot-pos (save-excursion
217 (calc-cursor-stack-index (max 0 (1- bot-num
)))
219 (num (- top-num bot-num -
1))
220 (str (buffer-substring top-pos bot-pos
))
221 (calcval (calc-top-list num bot-num
))
222 (cval (cdr (assq register calc-register-alist
))))
224 (calc-set-register register str calcval
)
228 (concat str
(car cval
))
229 (append calcval
(cdr cval
)))
232 (concat (car cval
) str
)
233 (append (cdr cval
) calcval
))))
236 (calc-pop-stack num bot-num
)))))
238 (defun calc-append-to-register (register start end
&optional delete-flag
)
239 "Copy the lines in the region to the end of register REGISTER.
240 With prefix arg, also delete the region."
241 (interactive "cAppend to register: \nr\nP")
242 (if (eq major-mode
'calc-mode
)
243 (calc-add-to-register register start end nil delete-flag
)
244 (append-to-register register start end delete-flag
)))
246 (defun calc-prepend-to-register (register start end
&optional delete-flag
)
247 "Copy the lines in the region to the beginning of register REGISTER.
248 With prefix arg, also delete the region."
249 (interactive "cPrepend to register: \nr\nP")
250 (if (eq major-mode
'calc-mode
)
251 (calc-add-to-register register start end t delete-flag
)
252 (prepend-to-register register start end delete-flag
)))
256 (defun calc-clean-newlines (s)
259 ;; Omit leading/trailing whitespace
260 ((or (string-match "\\`[ \n\r]+\\([^\001]*\\)\\'" s
)
261 (string-match "\\`\\([^\001]*\\)[ \n\r]+\\'" s
))
262 (calc-clean-newlines (math-match-substring s
1)))
264 ;; Convert newlines to commas
265 ((string-match "\\`\\(.*\\)[\n\r]+\\([^\001]*\\)\\'" s
)
266 (calc-clean-newlines (concat (math-match-substring s
1) ","
267 (math-match-substring s
2))))
272 (defun calc-do-grab-region (top bot arg
)
273 (when (memq major-mode
'(calc-mode calc-trail-mode
))
274 (error "This command works only in a regular text buffer"))
275 (let* ((from-buffer (current-buffer))
276 (calc-was-started (get-buffer-window "*Calculator*"))
282 (setq arg
(prefix-numeric-value arg
))
284 (setq top
(point-at-bol)
293 (setq data
(buffer-substring top bot
))
296 (setq vals
(math-read-expr data
))
297 (setq vals
(math-read-expr (concat "[" data
"]")))
298 (and (eq (car-safe vals
) 'vec
)
300 (eq (car-safe (nth 1 vals
)) 'vec
)
301 (setq vals
(nth 1 vals
))))
302 (if (eq (car-safe vals
) 'error
)
305 (pop-to-buffer from-buffer
)
307 (switch-to-buffer from-buffer
))
309 (forward-char (+ (nth 1 vals
) (if single
0 1)))
310 (error (nth 2 vals
))))
312 (calc-enter-result 0 "grab" vals
))))
315 (defun calc-do-grab-rectangle (top bot arg
&optional reduce
)
316 (and (memq major-mode
'(calc-mode calc-trail-mode
))
317 (error "This command works only in a regular text buffer"))
318 (let* ((col1 (save-excursion (goto-char top
) (current-column)))
319 (col2 (save-excursion (goto-char bot
) (current-column)))
320 (from-buffer (current-buffer))
321 (calc-was-started (get-buffer-window "*Calculator*"))
322 data mat vals lnum pt pos
)
326 (error "Point and mark must be at beginning of line, or define a rectangle"))
328 (while (< (point) bot
)
331 (setq data
(cons (buffer-substring pt
(1- (point))) data
)))
332 (setq data
(nreverse data
)))
333 (setq data
(extract-rectangle top bot
)))
335 (setq mat
(list 'vec
)
338 (setq arg
(if (consp arg
) 0 (prefix-numeric-value arg
))))
346 (let ((w (length (car data
)))
351 (math-read-expr (substring (car data
) pos
))
352 (math-read-expr (substring (car data
) pos j
))))
353 (if (eq (car-safe v
) 'error
)
355 (setq vals
(nconc vals
(list v
))
357 (if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
359 (setq vals
(list 'vec
(string-to-number (car data
))))
361 (string-match "[[{][^][{}]*[]}]" (car data
)))
362 (setq pos
(match-beginning 0)
363 vals
(math-read-expr (math-match-substring (car data
) 0)))
364 (let ((s (if (string-match
365 "\\`\\([0-9]+:[ \t]\\)?\\(.*[^, \t]\\)[, \t]*\\'"
367 (math-match-substring (car data
) 2)
370 vals
(math-read-expr (concat "[" s
"]")))
371 (if (eq (car-safe vals
) 'error
)
372 (let ((v2 (math-read-expr s
)))
373 (unless (eq (car-safe v2
) 'error
)
374 (setq vals
(list 'vec v2
)))))))))
375 (if (eq (car-safe vals
) 'error
)
378 (pop-to-buffer from-buffer
)
380 (switch-to-buffer from-buffer
))
383 (forward-char (+ (nth 1 vals
) (min col1 col2
) pos
))
384 (error (nth 2 vals
))))
385 (unless (equal vals
'(vec))
386 (setq mat
(cons vals mat
)))
387 (setq data
(cdr data
)
391 (calc-enter-result 0 "grb+" (list reduce
'(var add var-add
)
393 (calc-enter-result 0 "grab" (nreverse mat
))))))
396 (defun calc-copy-to-buffer (nn)
397 "Copy the top of stack into an editing buffer."
399 (let ((thebuf (and (not (memq major-mode
'(calc-mode calc-trail-mode
)))
406 (let ((n (prefix-numeric-value nn
))
407 (eat-lnums calc-line-numbering
)
408 (big-offset (if (eq calc-language
'big
) 1 0))
410 (setq oldbuf
(current-buffer)
412 (calc-find-writable-buffer (buffer-list) 0)
413 (calc-find-writable-buffer (buffer-list) 1)
414 (error "No other buffer")))
415 (cond ((and (or (null nn
)
417 (= (calc-substack-height 0)
418 (- (1- (calc-substack-height 1)) big-offset
)))
419 (calc-cursor-stack-index 1)
421 (if calc-line-numbering
"[0-9]+: *[^ \n]" " *[^ \n]"))
422 (goto-char (1- (match-end 0))))
425 (calc-cursor-stack-index 0)
426 (setq bot
(- (1- (point)) big-offset
)))
428 (calc-cursor-stack-index n
)
430 (calc-cursor-stack-index 0)
431 (setq bot
(- (point) big-offset
)))
433 (calc-cursor-stack-index (- n
))
435 (calc-cursor-stack-index (1- (- n
)))
438 (goto-char (point-min))
441 (calc-cursor-stack-index 0)
443 (with-current-buffer newbuf
445 (kill-region (region-beginning) (region-end)))
446 (push-mark (point) t
)
447 (if (and overwrite-mode
(not (consp nn
)))
448 (calc-overwrite-string (with-current-buffer oldbuf
449 (buffer-substring top bot
))
451 (or (bolp) (setq eat-lnums nil
))
452 (insert-buffer-substring oldbuf top bot
)
455 (while (and (> (point) (mark))
458 (>= (point) (mark))))
463 (setq movept
(point)))
464 (when (get-buffer-window (current-buffer))
465 (set-window-point (get-buffer-window (current-buffer))
469 (when (and (consp nn
)
472 (switch-to-buffer newbuf
))))
474 (defun calc-overwrite-string (str eat-lnums
)
475 (when (string-match "\n\\'" str
)
476 (setq str
(substring str
0 -
1)))
478 (setq str
(substring str
4)))
479 (if (and (string-match "\\`[-+]?[0-9.]+\\(e-?[0-9]+\\)?\\'" str
)
480 (looking-at "[-+]?[0-9.]+\\(e-?[0-9]+\\)?"))
482 (delete-region (point) (match-end 0))
485 (while (< i
(length str
))
486 (if (= (setq last-command-event
(aref str i
)) ?
\n)
487 (or (= i
(1- (length str
)))
490 (delete-region pt
(point))
494 (if eat-lnums
(setq i
(+ i
4)))))
495 (self-insert-command 1))
498 ;; First, require that buffer is visible and does not begin with "*"
499 ;; Second, require only that it not begin with "*Calc"
500 (defun calc-find-writable-buffer (buf mode
)
502 (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
503 (buffer-name (car buf
)))
505 (or (string-match "\\`\\*.*" (buffer-name (car buf
)))
506 (not (get-buffer-window (car buf
))))))
507 (calc-find-writable-buffer (cdr buf
) mode
)
515 (setq n
(calc-stack-size)))
518 (list (math-showing-full-precision
520 (function (lambda (x)
521 (math-format-flat-expr x
0)))
524 (if (math-vectorp x
) (setq allow-ret t
))
525 (math-format-nice-expr x
(frame-width)))))
528 (calc-top-list 1 (- n
)))))))
529 (calc-edit-mode (list 'calc-finish-stack-edit
(or flag n
)) allow-ret
)
531 (insert (car list
) "\n")
532 (setq list
(cdr list
)))))
533 (calc-show-edit-buffer))
535 (defun calc-alg-edit (str)
536 (calc-edit-mode '(calc-finish-stack-edit 0))
537 (calc-show-edit-buffer)
540 (calc-set-command-flag 'do-edit
))
542 (defvar calc-edit-mode-map
543 (let ((map (make-sparse-keymap)))
544 (define-key map
"\n" 'calc-edit-finish
)
545 (define-key map
"\r" 'calc-edit-return
)
546 (define-key map
"\C-c\C-c" 'calc-edit-finish
)
548 "Keymap for use by the calc-edit command.")
550 (defvar calc-original-buffer
)
551 (defvar calc-return-buffer
)
552 (defvar calc-one-window
)
553 (defvar calc-edit-handler
)
554 (defvar calc-restore-trail
)
555 (defvar calc-allow-ret
)
556 (defvar calc-edit-top
)
558 (defun calc-edit-mode (&optional handler allow-ret title
)
559 "Calculator editing mode. Press RET, LFD, or C-c C-c to finish.
560 To cancel the edit, simply kill the *Calc Edit* buffer."
563 (error "This command can be used only indirectly through calc-edit"))
564 (let ((oldbuf (current-buffer))
565 (buf (get-buffer-create "*Calc Edit*")))
567 (kill-all-local-variables)
568 (use-local-map calc-edit-mode-map
)
569 (setq buffer-read-only nil
)
570 (setq truncate-lines nil
)
571 (setq major-mode
'calc-edit-mode
)
572 (setq mode-name
"Calc Edit")
573 (run-mode-hooks 'calc-edit-mode-hook
)
574 (make-local-variable 'calc-original-buffer
)
575 (setq calc-original-buffer oldbuf
)
576 (make-local-variable 'calc-return-buffer
)
577 (setq calc-return-buffer oldbuf
)
578 (make-local-variable 'calc-one-window
)
579 (setq calc-one-window
(and (one-window-p t
) pop-up-windows
))
580 (make-local-variable 'calc-edit-handler
)
581 (setq calc-edit-handler handler
)
582 (make-local-variable 'calc-restore-trail
)
583 (setq calc-restore-trail
(get-buffer-window (calc-trail-buffer)))
584 (make-local-variable 'calc-allow-ret
)
585 (setq calc-allow-ret allow-ret
)
586 (let ((inhibit-read-only t
))
588 (add-hook 'kill-buffer-hook
(lambda ()
589 (let ((calc-edit-handler nil
))
590 (calc-edit-finish t
))
591 (message "(Cancelled)")) t t
)
594 (or title title
"Calc Edit Mode. ")
596 (if allow-ret
"" " or RET")
597 " to finish, `C-x k RET' to cancel.\n\n")
598 'font-lock-face
'italic
'read-only t
'rear-nonsticky t
'front-sticky t
))
599 (make-local-variable 'calc-edit-top
)
600 (setq calc-edit-top
(point))))
601 (put 'calc-edit-mode
'mode-class
'special
)
603 (defun calc-show-edit-buffer ()
604 (let ((buf (current-buffer)))
605 (if (and (one-window-p t
) pop-up-windows
)
606 (pop-to-buffer (get-buffer-create "*Calc Edit*"))
607 (and calc-embedded-info
(get-buffer-window (aref calc-embedded-info
1))
608 (select-window (get-buffer-window (aref calc-embedded-info
1))))
609 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
610 (setq calc-return-buffer buf
)
611 (if (and (< (window-width) (frame-width))
613 (let ((win (get-buffer-window (calc-trail-buffer))))
615 (delete-window win
))))
616 (set-buffer-modified-p nil
)
617 (goto-char calc-edit-top
)))
619 (defun calc-edit-return ()
621 (if (and (boundp 'calc-allow-ret
) calc-allow-ret
)
625 ;; The variable calc-edit-disp-trail is local to calc-edit finish, but
626 ;; is used by calc-finish-selection-edit and calc-finish-stack-edit.
627 (defvar calc-edit-disp-trail
)
629 (defun calc-edit-finish (&optional keep
)
630 "Finish calc-edit mode. Parse buffer contents and push them on the stack."
632 (message "Working...")
633 (or (and (boundp 'calc-original-buffer
)
634 (boundp 'calc-return-buffer
)
635 (boundp 'calc-one-window
)
636 (boundp 'calc-edit-handler
)
637 (boundp 'calc-restore-trail
)
638 (eq major-mode
'calc-edit-mode
))
639 (error "This command is valid only in buffers created by calc-edit"))
640 (let ((buf (current-buffer))
641 (original calc-original-buffer
)
642 (return calc-return-buffer
)
643 (one-window calc-one-window
)
644 (calc-edit-disp-trail calc-restore-trail
))
646 (when (or (null (buffer-name original
))
648 (set-buffer original
)
649 (not (eq major-mode
'calc-mode
))))
650 (error "Original calculator buffer has been corrupted")))
651 (goto-char calc-edit-top
)
652 (if (buffer-modified-p)
653 (eval calc-edit-handler
))
654 (if (and one-window
(not (one-window-p t
)))
656 (if (get-buffer-window return
)
657 (select-window (get-buffer-window return
))
658 (switch-to-buffer return
))
662 (if calc-edit-disp-trail
664 (calc-trail-display 1 t
)))
667 (defun calc-edit-cancel ()
668 "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
670 (let ((calc-edit-handler nil
))
672 (message "(Cancelled)"))
674 (defun calc-finish-stack-edit (num)
675 (let ((buf (current-buffer))
676 (str (buffer-substring calc-edit-top
(point-max)))
679 (if (and (integerp num
) (> num
1))
680 (while (setq pos
(string-match "\n." str
))
682 (switch-to-buffer calc-original-buffer
)
683 (let ((vals (let ((calc-language nil
)
684 (math-expr-opers (math-standard-ops)))
685 (and (string-match "[^\n\t ]" str
)
686 (math-read-exprs str
)))))
687 (when (eq (car-safe vals
) 'error
)
688 (switch-to-buffer buf
)
689 (goto-char (+ start
(nth 1 vals
)))
690 (error (nth 2 vals
)))
695 (calc-refresh-evaltos num
))
696 (if calc-edit-disp-trail
697 (calc-trail-display 1 t
))
699 (let ((calc-simplify-mode (if (eq last-command-event ?\C-j
)
701 calc-simplify-mode
)))
703 (calc-enter-result num
"edit" vals
)
704 (calc-enter-result 1 "edit" vals
(- num
))))))))))
709 ;; generated-autoload-file: "calc-loaddefs.el"
712 ;;; calc-yank.el ends here