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 (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 (defun calc-clean-newlines (s)
138 ;; Omit leading/trailing whitespace
139 ((or (string-match "\\`[ \n\r]+\\([^\001]*\\)\\'" s)
140 (string-match "\\`\\([^\001]*\\)[ \n\r]+\\'" s))
141 (calc-clean-newlines (math-match-substring s 1)))
143 ;; Convert newlines to commas
144 ((string-match "\\`\\(.*\\)[\n\r]+\\([^\001]*\\)\\'" s)
145 (calc-clean-newlines (concat (math-match-substring s 1) ","
146 (math-match-substring s 2))))
151 (defun calc-do-grab-region (top bot arg)
152 (when (memq major-mode '(calc-mode calc-trail-mode))
153 (error "This command works only in a regular text buffer"))
154 (let* ((from-buffer (current-buffer))
155 (calc-was-started (get-buffer-window "*Calculator*"))
161 (setq arg (prefix-numeric-value arg))
175 (setq data (buffer-substring top bot))
178 (setq vals (math-read-expr data))
179 (setq vals (math-read-expr (concat "[" data "]")))
180 (and (eq (car-safe vals) 'vec)
182 (eq (car-safe (nth 1 vals)) 'vec)
183 (setq vals (nth 1 vals))))
184 (if (eq (car-safe vals) 'error)
187 (pop-to-buffer from-buffer)
189 (switch-to-buffer from-buffer))
191 (forward-char (+ (nth 1 vals) (if single 0 1)))
192 (error (nth 2 vals))))
194 (calc-enter-result 0 "grab" vals))))
197 (defun calc-do-grab-rectangle (top bot arg &optional reduce)
198 (and (memq major-mode '(calc-mode calc-trail-mode))
199 (error "This command works only in a regular text buffer"))
200 (let* ((col1 (save-excursion (goto-char top) (current-column)))
201 (col2 (save-excursion (goto-char bot) (current-column)))
202 (from-buffer (current-buffer))
203 (calc-was-started (get-buffer-window "*Calculator*"))
204 data mat vals lnum pt pos)
208 (error "Point and mark must be at beginning of line, or define a rectangle"))
210 (while (< (point) bot)
213 (setq data (cons (buffer-substring pt (1- (point))) data)))
214 (setq data (nreverse data)))
215 (setq data (extract-rectangle top bot)))
217 (setq mat (list 'vec)
220 (setq arg (if (consp arg) 0 (prefix-numeric-value arg))))
228 (let ((w (length (car data)))
233 (math-read-expr (substring (car data) pos))
234 (math-read-expr (substring (car data) pos j))))
235 (if (eq (car-safe v) 'error)
237 (setq vals (nconc vals (list v))
239 (if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
241 (setq vals (list 'vec (string-to-number (car data))))
243 (string-match "[[{][^][{}]*[]}]" (car data)))
244 (setq pos (match-beginning 0)
245 vals (math-read-expr (math-match-substring (car data) 0)))
246 (let ((s (if (string-match
247 "\\`\\([0-9]+:[ \t]\\)?\\(.*[^, \t]\\)[, \t]*\\'"
249 (math-match-substring (car data) 2)
252 vals (math-read-expr (concat "[" s "]")))
253 (if (eq (car-safe vals) 'error)
254 (let ((v2 (math-read-expr s)))
255 (unless (eq (car-safe v2) 'error)
256 (setq vals (list 'vec v2)))))))))
257 (if (eq (car-safe vals) 'error)
260 (pop-to-buffer from-buffer)
262 (switch-to-buffer from-buffer))
265 (forward-char (+ (nth 1 vals) (min col1 col2) pos))
266 (error (nth 2 vals))))
267 (unless (equal vals '(vec))
268 (setq mat (cons vals mat)))
269 (setq data (cdr data)
273 (calc-enter-result 0 "grb+" (list reduce '(var add var-add)
275 (calc-enter-result 0 "grab" (nreverse mat))))))
278 (defun calc-copy-to-buffer (nn)
279 "Copy the top of stack into an editing buffer."
281 (let ((thebuf (and (not (memq major-mode '(calc-mode calc-trail-mode)))
288 (let ((n (prefix-numeric-value nn))
289 (eat-lnums calc-line-numbering)
290 (big-offset (if (eq calc-language 'big) 1 0))
292 (setq oldbuf (current-buffer)
294 (calc-find-writable-buffer (buffer-list) 0)
295 (calc-find-writable-buffer (buffer-list) 1)
296 (error "No other buffer")))
297 (cond ((and (or (null nn)
299 (= (calc-substack-height 0)
300 (- (1- (calc-substack-height 1)) big-offset)))
301 (calc-cursor-stack-index 1)
303 (if calc-line-numbering "[0-9]+: *[^ \n]" " *[^ \n]"))
304 (goto-char (1- (match-end 0))))
307 (calc-cursor-stack-index 0)
308 (setq bot (- (1- (point)) big-offset)))
310 (calc-cursor-stack-index n)
312 (calc-cursor-stack-index 0)
313 (setq bot (- (point) big-offset)))
315 (calc-cursor-stack-index (- n))
317 (calc-cursor-stack-index (1- (- n)))
320 (goto-char (point-min))
323 (calc-cursor-stack-index 0)
328 (kill-region (region-beginning) (region-end)))
329 (push-mark (point) t)
330 (if (and overwrite-mode (not (consp nn)))
331 (calc-overwrite-string (save-excursion
333 (buffer-substring top bot))
335 (or (bolp) (setq eat-lnums nil))
336 (insert-buffer-substring oldbuf top bot)
339 (while (and (> (point) (mark))
342 (>= (point) (mark))))
347 (setq movept (point)))
348 (when (get-buffer-window (current-buffer))
349 (set-window-point (get-buffer-window (current-buffer))
353 (when (and (consp nn)
356 (switch-to-buffer newbuf))))
358 (defun calc-overwrite-string (str eat-lnums)
359 (when (string-match "\n\\'" str)
360 (setq str (substring str 0 -1)))
362 (setq str (substring str 4)))
363 (if (and (string-match "\\`[-+]?[0-9.]+\\(e-?[0-9]+\\)?\\'" str)
364 (looking-at "[-+]?[0-9.]+\\(e-?[0-9]+\\)?"))
366 (delete-region (point) (match-end 0))
369 (while (< i (length str))
370 (if (= (setq last-command-char (aref str i)) ?\n)
371 (or (= i (1- (length str)))
374 (delete-region pt (point))
378 (if eat-lnums (setq i (+ i 4)))))
379 (self-insert-command 1))
382 ;; First, require that buffer is visible and does not begin with "*"
383 ;; Second, require only that it not begin with "*Calc"
384 (defun calc-find-writable-buffer (buf mode)
386 (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
387 (buffer-name (car buf)))
389 (or (string-match "\\`\\*.*" (buffer-name (car buf)))
390 (not (get-buffer-window (car buf))))))
391 (calc-find-writable-buffer (cdr buf) mode)
399 (setq n (calc-stack-size)))
402 (list (math-showing-full-precision
404 (function (lambda (x)
405 (math-format-flat-expr x 0)))
408 (if (math-vectorp x) (setq allow-ret t))
409 (math-format-nice-expr x (frame-width)))))
412 (calc-top-list 1 (- n)))))))
413 (calc-edit-mode (list 'calc-finish-stack-edit (or flag n)) allow-ret)
415 (insert (car list) "\n")
416 (setq list (cdr list)))))
417 (calc-show-edit-buffer))
419 (defun calc-alg-edit (str)
420 (calc-edit-mode '(calc-finish-stack-edit 0))
421 (calc-show-edit-buffer)
424 (calc-set-command-flag 'do-edit))
426 (defvar calc-edit-mode-map
427 (let ((map (make-sparse-keymap)))
428 (define-key map "\n" 'calc-edit-finish)
429 (define-key map "\r" 'calc-edit-return)
430 (define-key map "\C-c\C-c" 'calc-edit-finish)
432 "Keymap for use by the calc-edit command.")
434 (defvar calc-original-buffer)
435 (defvar calc-return-buffer)
436 (defvar calc-one-window)
437 (defvar calc-edit-handler)
438 (defvar calc-restore-trail)
439 (defvar calc-allow-ret)
440 (defvar calc-edit-top)
442 (defun calc-edit-mode (&optional handler allow-ret title)
443 "Calculator editing mode. Press RET, LFD, or C-c C-c to finish.
444 To cancel the edit, simply kill the *Calc Edit* buffer."
447 (error "This command can be used only indirectly through calc-edit"))
448 (let ((oldbuf (current-buffer))
449 (buf (get-buffer-create "*Calc Edit*")))
451 (kill-all-local-variables)
452 (use-local-map calc-edit-mode-map)
453 (setq buffer-read-only nil)
454 (setq truncate-lines nil)
455 (setq major-mode 'calc-edit-mode)
456 (setq mode-name "Calc Edit")
457 (run-mode-hooks 'calc-edit-mode-hook)
458 (make-local-variable 'calc-original-buffer)
459 (setq calc-original-buffer oldbuf)
460 (make-local-variable 'calc-return-buffer)
461 (setq calc-return-buffer oldbuf)
462 (make-local-variable 'calc-one-window)
463 (setq calc-one-window (and (one-window-p t) pop-up-windows))
464 (make-local-variable 'calc-edit-handler)
465 (setq calc-edit-handler handler)
466 (make-local-variable 'calc-restore-trail)
467 (setq calc-restore-trail (get-buffer-window (calc-trail-buffer)))
468 (make-local-variable 'calc-allow-ret)
469 (setq calc-allow-ret allow-ret)
470 (let ((inhibit-read-only t))
472 (add-hook 'kill-buffer-hook (lambda ()
473 (let ((calc-edit-handler nil))
474 (calc-edit-finish t))
475 (message "(Cancelled)")) t t)
478 (or title title "Calc Edit Mode. ")
480 (if allow-ret "" " or RET")
481 " to finish, `C-x k RET' to cancel.\n\n")
482 'font-lock-face 'italic 'read-only t 'rear-nonsticky t 'front-sticky t))
483 (make-local-variable 'calc-edit-top)
484 (setq calc-edit-top (point))))
485 (put 'calc-edit-mode 'mode-class 'special)
487 (defun calc-show-edit-buffer ()
488 (let ((buf (current-buffer)))
489 (if (and (one-window-p t) pop-up-windows)
490 (pop-to-buffer (get-buffer-create "*Calc Edit*"))
491 (and calc-embedded-info (get-buffer-window (aref calc-embedded-info 1))
492 (select-window (get-buffer-window (aref calc-embedded-info 1))))
493 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
494 (setq calc-return-buffer buf)
495 (if (and (< (window-width) (frame-width))
497 (let ((win (get-buffer-window (calc-trail-buffer))))
499 (delete-window win))))
500 (set-buffer-modified-p nil)
501 (goto-char calc-edit-top)))
503 (defun calc-edit-return ()
505 (if (and (boundp 'calc-allow-ret) calc-allow-ret)
509 ;; The variable calc-edit-disp-trail is local to calc-edit finish, but
510 ;; is used by calc-finish-selection-edit and calc-finish-stack-edit.
511 (defvar calc-edit-disp-trail)
513 (defun calc-edit-finish (&optional keep)
514 "Finish calc-edit mode. Parse buffer contents and push them on the stack."
516 (message "Working...")
517 (or (and (boundp 'calc-original-buffer)
518 (boundp 'calc-return-buffer)
519 (boundp 'calc-one-window)
520 (boundp 'calc-edit-handler)
521 (boundp 'calc-restore-trail)
522 (eq major-mode 'calc-edit-mode))
523 (error "This command is valid only in buffers created by calc-edit"))
524 (let ((buf (current-buffer))
525 (original calc-original-buffer)
526 (return calc-return-buffer)
527 (one-window calc-one-window)
528 (calc-edit-disp-trail calc-restore-trail))
530 (when (or (null (buffer-name original))
532 (set-buffer original)
533 (not (eq major-mode 'calc-mode))))
534 (error "Original calculator buffer has been corrupted")))
535 (goto-char calc-edit-top)
536 (if (buffer-modified-p)
537 (eval calc-edit-handler))
538 (if (and one-window (not (one-window-p t)))
540 (if (get-buffer-window return)
541 (select-window (get-buffer-window return))
542 (switch-to-buffer return))
546 (if calc-edit-disp-trail
548 (calc-trail-display 1 t)))
551 (defun calc-edit-cancel ()
552 "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
554 (let ((calc-edit-handler nil))
556 (message "(Cancelled)"))
558 (defun calc-finish-stack-edit (num)
559 (let ((buf (current-buffer))
560 (str (buffer-substring calc-edit-top (point-max)))
563 (if (and (integerp num) (> num 1))
564 (while (setq pos (string-match "\n." str))
566 (switch-to-buffer calc-original-buffer)
567 (let ((vals (let ((calc-language nil)
568 (math-expr-opers (math-standard-ops)))
569 (and (string-match "[^\n\t ]" str)
570 (math-read-exprs str)))))
571 (when (eq (car-safe vals) 'error)
572 (switch-to-buffer buf)
573 (goto-char (+ start (nth 1 vals)))
574 (error (nth 2 vals)))
579 (calc-refresh-evaltos num))
580 (if calc-edit-disp-trail
581 (calc-trail-display 1 t))
583 (let ((calc-simplify-mode (if (eq last-command-char ?\C-j)
585 calc-simplify-mode)))
587 (calc-enter-result num "edit" vals)
588 (calc-enter-result 1 "edit" vals (- num))))))))))
593 ;; generated-autoload-file: "calc-loaddefs.el"
596 ;; arch-tag: ca61019e-caca-4daa-b32c-b6afe372d5b5
597 ;;; calc-yank.el ends here