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 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 (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 (bot-num (calc-locate-cursor-element (1- bot)))
86 (num (- top-num bot-num -1)))
87 (copy-region-as-kill top bot)
88 (setq calc-last-kill (cons (car kill-ring)
89 (calc-top-list num bot-num)))
91 (calc-pop-stack num bot-num))))
93 (copy-region-as-kill top bot)
94 (kill-region top bot))))
96 (defun calc-copy-as-kill (n)
100 (defun calc-copy-region-as-kill (top bot)
102 (calc-kill-region top bot t))
104 ;; This function uses calc-last-kill if possible to get an exact result,
105 ;; otherwise it just parses the yanked string.
106 ;; Modified to use Emacs 19 extended concept of kill-ring. -- daveg 12/15/96
111 (calc-pop-push-record-list
113 (let ((thing (if (fboundp 'current-kill)
115 (car kill-ring-yank-pointer))))
116 (if (eq (car-safe calc-last-kill) thing)
119 (let ((val (math-read-exprs (calc-clean-newlines thing))))
120 (if (eq (car-safe val) 'error)
122 (setq val (math-read-exprs thing))
123 (if (eq (car-safe val) 'error)
124 (error "Bad format in yanked data")
128 (defun calc-clean-newlines (s)
131 ;; Omit leading/trailing whitespace
132 ((or (string-match "\\`[ \n\r]+\\([^\001]*\\)\\'" s)
133 (string-match "\\`\\([^\001]*\\)[ \n\r]+\\'" s))
134 (calc-clean-newlines (math-match-substring s 1)))
136 ;; Convert newlines to commas
137 ((string-match "\\`\\(.*\\)[\n\r]+\\([^\001]*\\)\\'" s)
138 (calc-clean-newlines (concat (math-match-substring s 1) ","
139 (math-match-substring s 2))))
144 (defun calc-do-grab-region (top bot arg)
145 (when (memq major-mode '(calc-mode calc-trail-mode))
146 (error "This command works only in a regular text buffer"))
147 (let* ((from-buffer (current-buffer))
148 (calc-was-started (get-buffer-window "*Calculator*"))
154 (setq arg (prefix-numeric-value arg))
168 (setq data (buffer-substring top bot))
171 (setq vals (math-read-expr data))
172 (setq vals (math-read-expr (concat "[" data "]")))
173 (and (eq (car-safe vals) 'vec)
175 (eq (car-safe (nth 1 vals)) 'vec)
176 (setq vals (nth 1 vals))))
177 (if (eq (car-safe vals) 'error)
180 (pop-to-buffer from-buffer)
182 (switch-to-buffer from-buffer))
184 (forward-char (+ (nth 1 vals) (if single 0 1)))
185 (error (nth 2 vals))))
187 (calc-enter-result 0 "grab" vals))))
190 (defun calc-do-grab-rectangle (top bot arg &optional reduce)
191 (and (memq major-mode '(calc-mode calc-trail-mode))
192 (error "This command works only in a regular text buffer"))
193 (let* ((col1 (save-excursion (goto-char top) (current-column)))
194 (col2 (save-excursion (goto-char bot) (current-column)))
195 (from-buffer (current-buffer))
196 (calc-was-started (get-buffer-window "*Calculator*"))
197 data mat vals lnum pt pos)
201 (error "Point and mark must be at beginning of line, or define a rectangle"))
203 (while (< (point) bot)
206 (setq data (cons (buffer-substring pt (1- (point))) data)))
207 (setq data (nreverse data)))
208 (setq data (extract-rectangle top bot)))
210 (setq mat (list 'vec)
213 (setq arg (if (consp arg) 0 (prefix-numeric-value arg))))
221 (let ((w (length (car data)))
226 (math-read-expr (substring (car data) pos))
227 (math-read-expr (substring (car data) pos j))))
228 (if (eq (car-safe v) 'error)
230 (setq vals (nconc vals (list v))
232 (if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
234 (setq vals (list 'vec (string-to-number (car data))))
236 (string-match "[[{][^][{}]*[]}]" (car data)))
237 (setq pos (match-beginning 0)
238 vals (math-read-expr (math-match-substring (car data) 0)))
239 (let ((s (if (string-match
240 "\\`\\([0-9]+:[ \t]\\)?\\(.*[^, \t]\\)[, \t]*\\'"
242 (math-match-substring (car data) 2)
245 vals (math-read-expr (concat "[" s "]")))
246 (if (eq (car-safe vals) 'error)
247 (let ((v2 (math-read-expr s)))
248 (unless (eq (car-safe v2) 'error)
249 (setq vals (list 'vec v2)))))))))
250 (if (eq (car-safe vals) 'error)
253 (pop-to-buffer from-buffer)
255 (switch-to-buffer from-buffer))
258 (forward-char (+ (nth 1 vals) (min col1 col2) pos))
259 (error (nth 2 vals))))
260 (unless (equal vals '(vec))
261 (setq mat (cons vals mat)))
262 (setq data (cdr data)
266 (calc-enter-result 0 "grb+" (list reduce '(var add var-add)
268 (calc-enter-result 0 "grab" (nreverse mat))))))
271 (defun calc-copy-to-buffer (nn)
272 "Copy the top of stack into an editing buffer."
274 (let ((thebuf (and (not (memq major-mode '(calc-mode calc-trail-mode)))
281 (let ((n (prefix-numeric-value nn))
282 (eat-lnums calc-line-numbering)
283 (big-offset (if (eq calc-language 'big) 1 0))
285 (setq oldbuf (current-buffer)
287 (calc-find-writable-buffer (buffer-list) 0)
288 (calc-find-writable-buffer (buffer-list) 1)
289 (error "No other buffer")))
290 (cond ((and (or (null nn)
292 (= (calc-substack-height 0)
293 (- (1- (calc-substack-height 1)) big-offset)))
294 (calc-cursor-stack-index 1)
296 (if calc-line-numbering "[0-9]+: *[^ \n]" " *[^ \n]"))
297 (goto-char (1- (match-end 0))))
300 (calc-cursor-stack-index 0)
301 (setq bot (- (1- (point)) big-offset)))
303 (calc-cursor-stack-index n)
305 (calc-cursor-stack-index 0)
306 (setq bot (- (point) big-offset)))
308 (calc-cursor-stack-index (- n))
310 (calc-cursor-stack-index (1- (- n)))
313 (goto-char (point-min))
316 (calc-cursor-stack-index 0)
321 (kill-region (region-beginning) (region-end)))
322 (push-mark (point) t)
323 (if (and overwrite-mode (not (consp nn)))
324 (calc-overwrite-string (save-excursion
326 (buffer-substring top bot))
328 (or (bolp) (setq eat-lnums nil))
329 (insert-buffer-substring oldbuf top bot)
332 (while (and (> (point) (mark))
335 (>= (point) (mark))))
340 (setq movept (point)))
341 (when (get-buffer-window (current-buffer))
342 (set-window-point (get-buffer-window (current-buffer))
346 (when (and (consp nn)
349 (switch-to-buffer newbuf))))
351 (defun calc-overwrite-string (str eat-lnums)
352 (when (string-match "\n\\'" str)
353 (setq str (substring str 0 -1)))
355 (setq str (substring str 4)))
356 (if (and (string-match "\\`[-+]?[0-9.]+\\(e-?[0-9]+\\)?\\'" str)
357 (looking-at "[-+]?[0-9.]+\\(e-?[0-9]+\\)?"))
359 (delete-region (point) (match-end 0))
362 (while (< i (length str))
363 (if (= (setq last-command-char (aref str i)) ?\n)
364 (or (= i (1- (length str)))
367 (delete-region pt (point))
371 (if eat-lnums (setq i (+ i 4)))))
372 (self-insert-command 1))
375 ;; First, require that buffer is visible and does not begin with "*"
376 ;; Second, require only that it not begin with "*Calc"
377 (defun calc-find-writable-buffer (buf mode)
379 (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
380 (buffer-name (car buf)))
382 (or (string-match "\\`\\*.*" (buffer-name (car buf)))
383 (not (get-buffer-window (car buf))))))
384 (calc-find-writable-buffer (cdr buf) mode)
392 (setq n (calc-stack-size)))
395 (list (math-showing-full-precision
397 (function (lambda (x)
398 (math-format-flat-expr x 0)))
401 (if (math-vectorp x) (setq allow-ret t))
402 (math-format-nice-expr x (frame-width)))))
405 (calc-top-list 1 (- n)))))))
406 (calc-edit-mode (list 'calc-finish-stack-edit (or flag n)) allow-ret)
408 (insert (car list) "\n")
409 (setq list (cdr list)))))
410 (calc-show-edit-buffer))
412 (defun calc-alg-edit (str)
413 (calc-edit-mode '(calc-finish-stack-edit 0))
414 (calc-show-edit-buffer)
417 (calc-set-command-flag 'do-edit))
419 (defvar calc-edit-mode-map
420 (let ((map (make-sparse-keymap)))
421 (define-key map "\n" 'calc-edit-finish)
422 (define-key map "\r" 'calc-edit-return)
423 (define-key map "\C-c\C-c" 'calc-edit-finish)
425 "Keymap for use by the calc-edit command.")
427 (defvar calc-original-buffer)
428 (defvar calc-return-buffer)
429 (defvar calc-one-window)
430 (defvar calc-edit-handler)
431 (defvar calc-restore-trail)
432 (defvar calc-allow-ret)
433 (defvar calc-edit-top)
435 (defun calc-edit-mode (&optional handler allow-ret title)
436 "Calculator editing mode. Press RET, LFD, or C-c C-c to finish.
437 To cancel the edit, simply kill the *Calc Edit* buffer."
440 (error "This command can be used only indirectly through calc-edit"))
441 (let ((oldbuf (current-buffer))
442 (buf (get-buffer-create "*Calc Edit*")))
444 (kill-all-local-variables)
445 (use-local-map calc-edit-mode-map)
446 (setq buffer-read-only nil)
447 (setq truncate-lines nil)
448 (setq major-mode 'calc-edit-mode)
449 (setq mode-name "Calc Edit")
450 (run-mode-hooks 'calc-edit-mode-hook)
451 (make-local-variable 'calc-original-buffer)
452 (setq calc-original-buffer oldbuf)
453 (make-local-variable 'calc-return-buffer)
454 (setq calc-return-buffer oldbuf)
455 (make-local-variable 'calc-one-window)
456 (setq calc-one-window (and (one-window-p t) pop-up-windows))
457 (make-local-variable 'calc-edit-handler)
458 (setq calc-edit-handler handler)
459 (make-local-variable 'calc-restore-trail)
460 (setq calc-restore-trail (get-buffer-window (calc-trail-buffer)))
461 (make-local-variable 'calc-allow-ret)
462 (setq calc-allow-ret allow-ret)
463 (let ((inhibit-read-only t))
465 (add-hook 'kill-buffer-hook (lambda ()
466 (let ((calc-edit-handler nil))
467 (calc-edit-finish t))
468 (message "(Cancelled)")) t t)
471 (or title title "Calc Edit Mode. ")
473 (if allow-ret "" " or RET")
474 " to finish, `C-x k RET' to cancel.\n\n")
475 'font-lock-face 'italic 'read-only t 'rear-nonsticky t 'front-sticky t))
476 (make-local-variable 'calc-edit-top)
477 (setq calc-edit-top (point))))
478 (put 'calc-edit-mode 'mode-class 'special)
480 (defun calc-show-edit-buffer ()
481 (let ((buf (current-buffer)))
482 (if (and (one-window-p t) pop-up-windows)
483 (pop-to-buffer (get-buffer-create "*Calc Edit*"))
484 (and calc-embedded-info (get-buffer-window (aref calc-embedded-info 1))
485 (select-window (get-buffer-window (aref calc-embedded-info 1))))
486 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
487 (setq calc-return-buffer buf)
488 (if (and (< (window-width) (frame-width))
490 (let ((win (get-buffer-window (calc-trail-buffer))))
492 (delete-window win))))
493 (set-buffer-modified-p nil)
494 (goto-char calc-edit-top)))
496 (defun calc-edit-return ()
498 (if (and (boundp 'calc-allow-ret) calc-allow-ret)
502 ;; The variable calc-edit-disp-trail is local to calc-edit finish, but
503 ;; is used by calc-finish-selection-edit and calc-finish-stack-edit.
504 (defvar calc-edit-disp-trail)
506 (defun calc-edit-finish (&optional keep)
507 "Finish calc-edit mode. Parse buffer contents and push them on the stack."
509 (message "Working...")
510 (or (and (boundp 'calc-original-buffer)
511 (boundp 'calc-return-buffer)
512 (boundp 'calc-one-window)
513 (boundp 'calc-edit-handler)
514 (boundp 'calc-restore-trail)
515 (eq major-mode 'calc-edit-mode))
516 (error "This command is valid only in buffers created by calc-edit"))
517 (let ((buf (current-buffer))
518 (original calc-original-buffer)
519 (return calc-return-buffer)
520 (one-window calc-one-window)
521 (calc-edit-disp-trail calc-restore-trail))
523 (when (or (null (buffer-name original))
525 (set-buffer original)
526 (not (eq major-mode 'calc-mode))))
527 (error "Original calculator buffer has been corrupted")))
528 (goto-char calc-edit-top)
529 (if (buffer-modified-p)
530 (eval calc-edit-handler))
531 (if (and one-window (not (one-window-p t)))
533 (if (get-buffer-window return)
534 (select-window (get-buffer-window return))
535 (switch-to-buffer return))
539 (if calc-edit-disp-trail
541 (calc-trail-display 1 t)))
544 (defun calc-edit-cancel ()
545 "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
547 (let ((calc-edit-handler nil))
549 (message "(Cancelled)"))
551 (defun calc-finish-stack-edit (num)
552 (let ((buf (current-buffer))
553 (str (buffer-substring calc-edit-top (point-max)))
556 (if (and (integerp num) (> num 1))
557 (while (setq pos (string-match "\n." str))
559 (switch-to-buffer calc-original-buffer)
560 (let ((vals (let ((calc-language nil)
561 (math-expr-opers (math-standard-ops)))
562 (and (string-match "[^\n\t ]" str)
563 (math-read-exprs str)))))
564 (when (eq (car-safe vals) 'error)
565 (switch-to-buffer buf)
566 (goto-char (+ start (nth 1 vals)))
567 (error (nth 2 vals)))
572 (calc-refresh-evaltos num))
573 (if calc-edit-disp-trail
574 (calc-trail-display 1 t))
576 (let ((calc-simplify-mode (if (eq last-command-char ?\C-j)
578 calc-simplify-mode)))
580 (calc-enter-result num "edit" vals)
581 (calc-enter-result 1 "edit" vals (- num))))))))))
586 ;; generated-autoload-file: "calc-loaddefs.el"
589 ;; arch-tag: ca61019e-caca-4daa-b32c-b6afe372d5b5
590 ;;; calc-yank.el ends here