1 ;;; calc-yank.el --- kill-ring functionality for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <belanger@truman.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
29 ;; This file is autoloaded from calc-ext.el.
34 (defun calc-Need-calc-yank () nil)
37 ;;; Kill ring commands.
39 (defun calc-kill (nn &optional no-delete)
41 (if (eq major-mode 'calc-mode)
44 (calc-set-command-flag 'no-align)
45 (let ((num (max (calc-locate-cursor-element (point)) 1))
46 (n (prefix-numeric-value nn)))
53 (let ((stuff (calc-top-list n (- num n -1))))
54 (calc-cursor-stack-index num)
55 (let ((first (point)))
56 (calc-cursor-stack-index (- num n))
58 (backward-char 1)) ; don't include newline for raw C-k
59 (copy-region-as-kill first (point))
61 (calc-pop-stack n (- num n -1))))
62 (setq calc-last-kill (cons (car kill-ring) stuff)))))
65 (defun calc-force-refresh ()
66 (if (or calc-executing-macro calc-display-dirty)
67 (let ((calc-executing-macro nil))
70 (defun calc-locate-cursor-element (pt)
72 (goto-char (point-max))
73 (calc-locate-cursor-scan (- calc-stack-top) calc-stack pt)))
75 (defun calc-locate-cursor-scan (n stack pt)
76 (if (or (<= (point) pt)
79 (forward-line (- (nth 1 (car stack))))
80 (calc-locate-cursor-scan (1+ n) (cdr stack) pt)))
82 (defun calc-kill-region (top bot &optional no-delete)
84 (if (eq major-mode 'calc-mode)
87 (calc-set-command-flag 'no-align)
88 (let* ((top-num (calc-locate-cursor-element top))
89 (bot-num (calc-locate-cursor-element (1- bot)))
90 (num (- top-num bot-num -1)))
91 (copy-region-as-kill top bot)
92 (setq calc-last-kill (cons (car kill-ring)
93 (calc-top-list num bot-num)))
95 (calc-pop-stack num bot-num))))
97 (copy-region-as-kill top bot)
98 (kill-region top bot))))
100 (defun calc-copy-as-kill (n)
104 (defun calc-copy-region-as-kill (top bot)
106 (calc-kill-region top bot t))
108 ;;; This function uses calc-last-kill if possible to get an exact result,
109 ;;; otherwise it just parses the yanked string.
110 ;;; Modified to use Emacs 19 extended concept of kill-ring. -- daveg 12/15/96
114 (calc-pop-push-record-list
116 (let ((thing (if (fboundp 'current-kill)
118 (car kill-ring-yank-pointer))))
119 (if (eq (car-safe calc-last-kill) thing)
122 (let ((val (math-read-exprs (calc-clean-newlines thing))))
123 (if (eq (car-safe val) 'error)
125 (setq val (math-read-exprs thing))
126 (if (eq (car-safe val) 'error)
127 (error "Bad format in yanked data")
131 (defun calc-clean-newlines (s)
134 ;; Omit leading/trailing whitespace
135 ((or (string-match "\\`[ \n\r]+\\([^\001]*\\)\\'" s)
136 (string-match "\\`\\([^\001]*\\)[ \n\r]+\\'" s))
137 (calc-clean-newlines (math-match-substring s 1)))
139 ;; Convert newlines to commas
140 ((string-match "\\`\\(.*\\)[\n\r]+\\([^\001]*\\)\\'" s)
141 (calc-clean-newlines (concat (math-match-substring s 1) ","
142 (math-match-substring s 2))))
147 (defun calc-do-grab-region (top bot arg)
148 (when (memq major-mode '(calc-mode calc-trail-mode))
149 (error "This command works only in a regular text buffer"))
150 (let* ((from-buffer (current-buffer))
151 (calc-was-started (get-buffer-window "*Calculator*"))
157 (setq arg (prefix-numeric-value arg))
171 (setq data (buffer-substring top bot))
174 (setq vals (math-read-expr data))
175 (setq vals (math-read-expr (concat "[" data "]")))
176 (and (eq (car-safe vals) 'vec)
178 (eq (car-safe (nth 1 vals)) 'vec)
179 (setq vals (nth 1 vals))))
180 (if (eq (car-safe vals) 'error)
183 (pop-to-buffer from-buffer)
185 (switch-to-buffer from-buffer))
187 (forward-char (+ (nth 1 vals) (if single 0 1)))
188 (error (nth 2 vals))))
190 (calc-enter-result 0 "grab" vals))))
193 (defun calc-do-grab-rectangle (top bot arg &optional reduce)
194 (and (memq major-mode '(calc-mode calc-trail-mode))
195 (error "This command works only in a regular text buffer"))
196 (let* ((col1 (save-excursion (goto-char top) (current-column)))
197 (col2 (save-excursion (goto-char bot) (current-column)))
198 (from-buffer (current-buffer))
199 (calc-was-started (get-buffer-window "*Calculator*"))
200 data mat vals lnum pt pos)
204 (error "Point and mark must be at beginning of line, or define a rectangle"))
206 (while (< (point) bot)
209 (setq data (cons (buffer-substring pt (1- (point))) data)))
210 (setq data (nreverse data)))
211 (setq data (extract-rectangle top bot)))
213 (setq mat (list 'vec)
216 (setq arg (if (consp arg) 0 (prefix-numeric-value arg))))
224 (let ((w (length (car data)))
229 (math-read-expr (substring (car data) pos))
230 (math-read-expr (substring (car data) pos j))))
231 (if (eq (car-safe v) 'error)
233 (setq vals (nconc vals (list v))
235 (if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
237 (setq vals (list 'vec (string-to-int (car data))))
239 (string-match "[[{][^][{}]*[]}]" (car data)))
240 (setq pos (match-beginning 0)
241 vals (math-read-expr (math-match-substring (car data) 0)))
242 (let ((s (if (string-match
243 "\\`\\([0-9]+:[ \t]\\)?\\(.*[^, \t]\\)[, \t]*\\'"
245 (math-match-substring (car data) 2)
248 vals (math-read-expr (concat "[" s "]")))
249 (if (eq (car-safe vals) 'error)
250 (let ((v2 (math-read-expr s)))
251 (unless (eq (car-safe v2) 'error)
252 (setq vals (list 'vec v2)))))))))
253 (if (eq (car-safe vals) 'error)
256 (pop-to-buffer from-buffer)
258 (switch-to-buffer from-buffer))
261 (forward-char (+ (nth 1 vals) (min col1 col2) pos))
262 (error (nth 2 vals))))
263 (unless (equal vals '(vec))
264 (setq mat (cons vals mat)))
265 (setq data (cdr data)
269 (calc-enter-result 0 "grb+" (list reduce '(var add var-add)
271 (calc-enter-result 0 "grab" (nreverse mat))))))
274 (defun calc-copy-to-buffer (nn)
275 "Copy the top of stack into an editing buffer."
277 (let ((thebuf (and (not (memq major-mode '(calc-mode calc-trail-mode)))
284 (let ((n (prefix-numeric-value nn))
285 (eat-lnums calc-line-numbering)
286 (big-offset (if (eq calc-language 'big) 1 0))
288 (setq oldbuf (current-buffer)
290 (calc-find-writable-buffer (buffer-list) 0)
291 (calc-find-writable-buffer (buffer-list) 1)
292 (error "No other buffer")))
293 (cond ((and (or (null nn)
295 (= (calc-substack-height 0)
296 (- (1- (calc-substack-height 1)) big-offset)))
297 (calc-cursor-stack-index 1)
299 (if calc-line-numbering "[0-9]+: *[^ \n]" " *[^ \n]"))
300 (goto-char (1- (match-end 0))))
303 (calc-cursor-stack-index 0)
304 (setq bot (- (1- (point)) big-offset)))
306 (calc-cursor-stack-index n)
308 (calc-cursor-stack-index 0)
309 (setq bot (- (point) big-offset)))
311 (calc-cursor-stack-index (- n))
313 (calc-cursor-stack-index (1- (- n)))
316 (goto-char (point-min))
319 (calc-cursor-stack-index 0)
324 (kill-region (region-beginning) (region-end)))
325 (push-mark (point) t)
326 (if (and overwrite-mode (not (consp nn)))
327 (calc-overwrite-string (save-excursion
329 (buffer-substring top bot))
331 (or (bolp) (setq eat-lnums nil))
332 (insert-buffer-substring oldbuf top bot)
335 (while (and (> (point) (mark))
338 (>= (point) (mark))))
343 (setq movept (point)))
344 (when (get-buffer-window (current-buffer))
345 (set-window-point (get-buffer-window (current-buffer))
349 (when (and (consp nn)
352 (switch-to-buffer newbuf))))
354 (defun calc-overwrite-string (str eat-lnums)
355 (when (string-match "\n\\'" str)
356 (setq str (substring str 0 -1)))
358 (setq str (substring str 4)))
359 (if (and (string-match "\\`[-+]?[0-9.]+\\(e-?[0-9]+\\)?\\'" str)
360 (looking-at "[-+]?[0-9.]+\\(e-?[0-9]+\\)?"))
362 (delete-region (point) (match-end 0))
365 (while (< i (length str))
366 (if (= (setq last-command-char (aref str i)) ?\n)
367 (or (= i (1- (length str)))
370 (delete-region pt (point))
374 (if eat-lnums (setq i (+ i 4)))))
375 (self-insert-command 1))
378 ;;; First, require that buffer is visible and does not begin with "*"
379 ;;; Second, require only that it not begin with "*Calc"
380 (defun calc-find-writable-buffer (buf mode)
382 (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
383 (buffer-name (car buf)))
385 (or (string-match "\\`\\*.*" (buffer-name (car buf)))
386 (not (get-buffer-window (car buf))))))
387 (calc-find-writable-buffer (cdr buf) mode)
395 (setq n (calc-stack-size)))
398 (list (math-showing-full-precision
400 (function (lambda (x)
401 (math-format-flat-expr x 0)))
404 (if (math-vectorp x) (setq allow-ret t))
405 (math-format-nice-expr x (frame-width)))))
408 (calc-top-list 1 (- n)))))))
409 (calc-edit-mode (list 'calc-finish-stack-edit (or flag n)) allow-ret)
411 (insert (car list) "\n")
412 (setq list (cdr list)))))
413 (calc-show-edit-buffer))
415 (defun calc-alg-edit (str)
416 (calc-edit-mode '(calc-finish-stack-edit 0))
417 (calc-show-edit-buffer)
420 (calc-set-command-flag 'do-edit))
422 (defvar calc-edit-mode-map nil "Keymap for use by the calc-edit command.")
423 (if calc-edit-mode-map
425 (setq calc-edit-mode-map (make-sparse-keymap))
426 (define-key calc-edit-mode-map "\n" 'calc-edit-finish)
427 (define-key calc-edit-mode-map "\r" 'calc-edit-return)
428 (define-key calc-edit-mode-map "\C-c\C-c" 'calc-edit-finish))
430 (defvar calc-original-buffer)
431 (defvar calc-return-buffer)
432 (defvar calc-one-window)
433 (defvar calc-edit-handler)
434 (defvar calc-restore-trail)
435 (defvar calc-allow-ret)
437 (defun calc-edit-mode (&optional handler allow-ret title)
438 "Calculator editing mode. Press RET, LFD, or C-c C-c to finish.
439 To cancel the edit, simply kill the *Calc Edit* buffer."
442 (error "This command can be used only indirectly through calc-edit"))
443 (let ((oldbuf (current-buffer))
444 (buf (get-buffer-create "*Calc Edit*")))
446 (kill-all-local-variables)
447 (use-local-map calc-edit-mode-map)
448 (setq buffer-read-only nil)
449 (setq truncate-lines nil)
450 (setq major-mode 'calc-edit-mode)
451 (setq mode-name "Calc Edit")
452 (run-hooks 'calc-edit-mode-hook)
453 (make-local-variable 'calc-original-buffer)
454 (setq calc-original-buffer oldbuf)
455 (make-local-variable 'calc-return-buffer)
456 (setq calc-return-buffer oldbuf)
457 (make-local-variable 'calc-one-window)
458 (setq calc-one-window (and (one-window-p t) pop-up-windows))
459 (make-local-variable 'calc-edit-handler)
460 (setq calc-edit-handler handler)
461 (make-local-variable 'calc-restore-trail)
462 (setq calc-restore-trail (get-buffer-window (calc-trail-buffer)))
463 (make-local-variable 'calc-allow-ret)
464 (setq calc-allow-ret allow-ret)
466 (insert (or title title "Calc Edit Mode")
468 (if (eq (lookup-key (current-global-map) "\e#") 'calc-dispatch)
470 (if allow-ret "C-c C-c" "RET"))
472 (if (eq (lookup-key (current-global-map) "\e#") 'calc-dispatch)
476 (put 'calc-edit-mode 'mode-class 'special)
478 (defun calc-show-edit-buffer ()
479 (let ((buf (current-buffer)))
480 (if (and (one-window-p t) pop-up-windows)
481 (pop-to-buffer (get-buffer-create "*Calc Edit*"))
482 (and calc-embedded-info (get-buffer-window (aref calc-embedded-info 1))
483 (select-window (get-buffer-window (aref calc-embedded-info 1))))
484 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
485 (setq calc-return-buffer buf)
486 (if (and (< (window-width) (frame-width))
488 (let ((win (get-buffer-window (calc-trail-buffer))))
490 (delete-window win))))
491 (set-buffer-modified-p nil)
492 (goto-char (point-min))
495 (defun calc-edit-return ()
497 (if (and (boundp 'calc-allow-ret) calc-allow-ret)
501 ;; The variable calc-edit-disp-trail is local to calc-edit finish, but
502 ;; is used by calc-finish-selection-edit and calc-finish-stack-edit.
503 (defvar calc-edit-disp-trail)
505 (defun calc-edit-finish (&optional keep)
506 "Finish calc-edit mode. Parse buffer contents and push them on the stack."
508 (message "Working...")
509 (or (and (boundp 'calc-original-buffer)
510 (boundp 'calc-return-buffer)
511 (boundp 'calc-one-window)
512 (boundp 'calc-edit-handler)
513 (boundp 'calc-restore-trail)
514 (eq major-mode 'calc-edit-mode))
515 (error "This command is valid only in buffers created by calc-edit"))
516 (let ((buf (current-buffer))
517 (original calc-original-buffer)
518 (return calc-return-buffer)
519 (one-window calc-one-window)
520 (calc-edit-disp-trail calc-restore-trail))
522 (when (or (null (buffer-name original))
524 (set-buffer original)
525 (not (eq major-mode 'calc-mode))))
526 (error "Original calculator buffer has been corrupted")))
527 (goto-char (point-min))
528 (when (looking-at "Calc Edit\\|Editing ")
530 (if (buffer-modified-p)
531 (eval calc-edit-handler))
534 (if (get-buffer-window return)
535 (select-window (get-buffer-window return))
536 (switch-to-buffer return))
540 (if calc-edit-disp-trail
542 (calc-trail-display 1 t)))
545 (defun calc-edit-cancel ()
546 "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
548 (let ((calc-edit-handler nil))
550 (message "(Cancelled)"))
552 (defun calc-finish-stack-edit (num)
553 (let ((buf (current-buffer))
554 (str (buffer-substring (point) (point-max)))
557 (if (and (integerp num) (> num 1))
558 (while (setq pos (string-match "\n." str))
560 (switch-to-buffer calc-original-buffer)
561 (let ((vals (let ((calc-language nil)
562 (math-expr-opers math-standard-opers))
563 (and (string-match "[^\n\t ]" str)
564 (math-read-exprs str)))))
565 (when (eq (car-safe vals) 'error)
566 (switch-to-buffer buf)
567 (goto-char (+ start (nth 1 vals)))
568 (error (nth 2 vals)))
573 (calc-refresh-evaltos num))
574 (if calc-edit-disp-trail
575 (calc-trail-display 1 t))
577 (let ((calc-simplify-mode (if (eq last-command-char ?\C-j)
579 calc-simplify-mode)))
581 (calc-enter-result num "edit" vals)
582 (calc-enter-result 1 "edit" vals (- num))))))))))
584 ;;; arch-tag: ca61019e-caca-4daa-b32c-b6afe372d5b5
585 ;;; calc-yank.el ends here