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, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 ;; This file is autoloaded from calc-ext.el.
35 ;;; Kill ring commands.
37 (defun calc-kill (nn &optional no-delete)
39 (if (eq major-mode 'calc-mode)
42 (calc-set-command-flag 'no-align)
43 (let ((num (max (calc-locate-cursor-element (point)) 1))
44 (n (prefix-numeric-value nn)))
51 (let ((stuff (calc-top-list n (- num n -1))))
52 (calc-cursor-stack-index num)
53 (let ((first (point)))
54 (calc-cursor-stack-index (- num n))
56 (backward-char 1)) ; don't include newline for raw C-k
57 (copy-region-as-kill first (point))
59 (calc-pop-stack n (- num n -1))))
60 (setq calc-last-kill (cons (car kill-ring) stuff)))))
63 (defun calc-force-refresh ()
64 (if (or calc-executing-macro calc-display-dirty)
65 (let ((calc-executing-macro nil))
68 (defun calc-locate-cursor-element (pt)
70 (goto-char (point-max))
71 (calc-locate-cursor-scan (- calc-stack-top) calc-stack pt)))
73 (defun calc-locate-cursor-scan (n stack pt)
74 (if (or (<= (point) pt)
77 (forward-line (- (nth 1 (car stack))))
78 (calc-locate-cursor-scan (1+ n) (cdr stack) pt)))
80 (defun calc-kill-region (top bot &optional no-delete)
82 (if (eq major-mode 'calc-mode)
85 (calc-set-command-flag 'no-align)
86 (let* ((top-num (calc-locate-cursor-element top))
87 (bot-num (calc-locate-cursor-element (1- bot)))
88 (num (- top-num bot-num -1)))
89 (copy-region-as-kill top bot)
90 (setq calc-last-kill (cons (car kill-ring)
91 (calc-top-list num bot-num)))
93 (calc-pop-stack num bot-num))))
95 (copy-region-as-kill top bot)
96 (kill-region top bot))))
98 (defun calc-copy-as-kill (n)
102 (defun calc-copy-region-as-kill (top bot)
104 (calc-kill-region top bot t))
106 ;; This function uses calc-last-kill if possible to get an exact result,
107 ;; otherwise it just parses the yanked string.
108 ;; Modified to use Emacs 19 extended concept of kill-ring. -- daveg 12/15/96
113 (calc-pop-push-record-list
115 (let ((thing (if (fboundp 'current-kill)
117 (car kill-ring-yank-pointer))))
118 (if (eq (car-safe calc-last-kill) thing)
121 (let ((val (math-read-exprs (calc-clean-newlines thing))))
122 (if (eq (car-safe val) 'error)
124 (setq val (math-read-exprs thing))
125 (if (eq (car-safe val) 'error)
126 (error "Bad format in yanked data")
130 (defun calc-clean-newlines (s)
133 ;; Omit leading/trailing whitespace
134 ((or (string-match "\\`[ \n\r]+\\([^\001]*\\)\\'" s)
135 (string-match "\\`\\([^\001]*\\)[ \n\r]+\\'" s))
136 (calc-clean-newlines (math-match-substring s 1)))
138 ;; Convert newlines to commas
139 ((string-match "\\`\\(.*\\)[\n\r]+\\([^\001]*\\)\\'" s)
140 (calc-clean-newlines (concat (math-match-substring s 1) ","
141 (math-match-substring s 2))))
146 (defun calc-do-grab-region (top bot arg)
147 (when (memq major-mode '(calc-mode calc-trail-mode))
148 (error "This command works only in a regular text buffer"))
149 (let* ((from-buffer (current-buffer))
150 (calc-was-started (get-buffer-window "*Calculator*"))
156 (setq arg (prefix-numeric-value arg))
170 (setq data (buffer-substring top bot))
173 (setq vals (math-read-expr data))
174 (setq vals (math-read-expr (concat "[" data "]")))
175 (and (eq (car-safe vals) 'vec)
177 (eq (car-safe (nth 1 vals)) 'vec)
178 (setq vals (nth 1 vals))))
179 (if (eq (car-safe vals) 'error)
182 (pop-to-buffer from-buffer)
184 (switch-to-buffer from-buffer))
186 (forward-char (+ (nth 1 vals) (if single 0 1)))
187 (error (nth 2 vals))))
189 (calc-enter-result 0 "grab" vals))))
192 (defun calc-do-grab-rectangle (top bot arg &optional reduce)
193 (and (memq major-mode '(calc-mode calc-trail-mode))
194 (error "This command works only in a regular text buffer"))
195 (let* ((col1 (save-excursion (goto-char top) (current-column)))
196 (col2 (save-excursion (goto-char bot) (current-column)))
197 (from-buffer (current-buffer))
198 (calc-was-started (get-buffer-window "*Calculator*"))
199 data mat vals lnum pt pos)
203 (error "Point and mark must be at beginning of line, or define a rectangle"))
205 (while (< (point) bot)
208 (setq data (cons (buffer-substring pt (1- (point))) data)))
209 (setq data (nreverse data)))
210 (setq data (extract-rectangle top bot)))
212 (setq mat (list 'vec)
215 (setq arg (if (consp arg) 0 (prefix-numeric-value arg))))
223 (let ((w (length (car data)))
228 (math-read-expr (substring (car data) pos))
229 (math-read-expr (substring (car data) pos j))))
230 (if (eq (car-safe v) 'error)
232 (setq vals (nconc vals (list v))
234 (if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
236 (setq vals (list 'vec (string-to-number (car data))))
238 (string-match "[[{][^][{}]*[]}]" (car data)))
239 (setq pos (match-beginning 0)
240 vals (math-read-expr (math-match-substring (car data) 0)))
241 (let ((s (if (string-match
242 "\\`\\([0-9]+:[ \t]\\)?\\(.*[^, \t]\\)[, \t]*\\'"
244 (math-match-substring (car data) 2)
247 vals (math-read-expr (concat "[" s "]")))
248 (if (eq (car-safe vals) 'error)
249 (let ((v2 (math-read-expr s)))
250 (unless (eq (car-safe v2) 'error)
251 (setq vals (list 'vec v2)))))))))
252 (if (eq (car-safe vals) 'error)
255 (pop-to-buffer from-buffer)
257 (switch-to-buffer from-buffer))
260 (forward-char (+ (nth 1 vals) (min col1 col2) pos))
261 (error (nth 2 vals))))
262 (unless (equal vals '(vec))
263 (setq mat (cons vals mat)))
264 (setq data (cdr data)
268 (calc-enter-result 0 "grb+" (list reduce '(var add var-add)
270 (calc-enter-result 0 "grab" (nreverse mat))))))
273 (defun calc-copy-to-buffer (nn)
274 "Copy the top of stack into an editing buffer."
276 (let ((thebuf (and (not (memq major-mode '(calc-mode calc-trail-mode)))
283 (let ((n (prefix-numeric-value nn))
284 (eat-lnums calc-line-numbering)
285 (big-offset (if (eq calc-language 'big) 1 0))
287 (setq oldbuf (current-buffer)
289 (calc-find-writable-buffer (buffer-list) 0)
290 (calc-find-writable-buffer (buffer-list) 1)
291 (error "No other buffer")))
292 (cond ((and (or (null nn)
294 (= (calc-substack-height 0)
295 (- (1- (calc-substack-height 1)) big-offset)))
296 (calc-cursor-stack-index 1)
298 (if calc-line-numbering "[0-9]+: *[^ \n]" " *[^ \n]"))
299 (goto-char (1- (match-end 0))))
302 (calc-cursor-stack-index 0)
303 (setq bot (- (1- (point)) big-offset)))
305 (calc-cursor-stack-index n)
307 (calc-cursor-stack-index 0)
308 (setq bot (- (point) big-offset)))
310 (calc-cursor-stack-index (- n))
312 (calc-cursor-stack-index (1- (- n)))
315 (goto-char (point-min))
318 (calc-cursor-stack-index 0)
323 (kill-region (region-beginning) (region-end)))
324 (push-mark (point) t)
325 (if (and overwrite-mode (not (consp nn)))
326 (calc-overwrite-string (save-excursion
328 (buffer-substring top bot))
330 (or (bolp) (setq eat-lnums nil))
331 (insert-buffer-substring oldbuf top bot)
334 (while (and (> (point) (mark))
337 (>= (point) (mark))))
342 (setq movept (point)))
343 (when (get-buffer-window (current-buffer))
344 (set-window-point (get-buffer-window (current-buffer))
348 (when (and (consp nn)
351 (switch-to-buffer newbuf))))
353 (defun calc-overwrite-string (str eat-lnums)
354 (when (string-match "\n\\'" str)
355 (setq str (substring str 0 -1)))
357 (setq str (substring str 4)))
358 (if (and (string-match "\\`[-+]?[0-9.]+\\(e-?[0-9]+\\)?\\'" str)
359 (looking-at "[-+]?[0-9.]+\\(e-?[0-9]+\\)?"))
361 (delete-region (point) (match-end 0))
364 (while (< i (length str))
365 (if (= (setq last-command-char (aref str i)) ?\n)
366 (or (= i (1- (length str)))
369 (delete-region pt (point))
373 (if eat-lnums (setq i (+ i 4)))))
374 (self-insert-command 1))
377 ;; First, require that buffer is visible and does not begin with "*"
378 ;; Second, require only that it not begin with "*Calc"
379 (defun calc-find-writable-buffer (buf mode)
381 (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
382 (buffer-name (car buf)))
384 (or (string-match "\\`\\*.*" (buffer-name (car buf)))
385 (not (get-buffer-window (car buf))))))
386 (calc-find-writable-buffer (cdr buf) mode)
394 (setq n (calc-stack-size)))
397 (list (math-showing-full-precision
399 (function (lambda (x)
400 (math-format-flat-expr x 0)))
403 (if (math-vectorp x) (setq allow-ret t))
404 (math-format-nice-expr x (frame-width)))))
407 (calc-top-list 1 (- n)))))))
408 (calc-edit-mode (list 'calc-finish-stack-edit (or flag n)) allow-ret)
410 (insert (car list) "\n")
411 (setq list (cdr list)))))
412 (calc-show-edit-buffer))
414 (defun calc-alg-edit (str)
415 (calc-edit-mode '(calc-finish-stack-edit 0))
416 (calc-show-edit-buffer)
419 (calc-set-command-flag 'do-edit))
421 (defvar calc-edit-mode-map
422 (let ((map (make-sparse-keymap)))
423 (define-key map "\n" 'calc-edit-finish)
424 (define-key map "\r" 'calc-edit-return)
425 (define-key map "\C-c\C-c" 'calc-edit-finish)
427 "Keymap for use by the calc-edit command.")
429 (defvar calc-original-buffer)
430 (defvar calc-return-buffer)
431 (defvar calc-one-window)
432 (defvar calc-edit-handler)
433 (defvar calc-restore-trail)
434 (defvar calc-allow-ret)
435 (defvar calc-edit-top)
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-mode-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)
465 (let ((inhibit-read-only t))
467 (add-hook 'kill-buffer-hook (lambda ()
468 (let ((calc-edit-handler nil))
469 (calc-edit-finish t))
470 (message "(Cancelled)")) t t)
473 (or title title "Calc Edit Mode. ")
475 (if allow-ret "" " or RET")
476 " to finish, `C-x k RET' to cancel.\n\n")
477 'font-lock-face 'italic 'read-only t 'rear-nonsticky t 'front-sticky t))
478 (make-local-variable 'calc-edit-top)
479 (setq calc-edit-top (point))))
480 (put 'calc-edit-mode 'mode-class 'special)
482 (defun calc-show-edit-buffer ()
483 (let ((buf (current-buffer)))
484 (if (and (one-window-p t) pop-up-windows)
485 (pop-to-buffer (get-buffer-create "*Calc Edit*"))
486 (and calc-embedded-info (get-buffer-window (aref calc-embedded-info 1))
487 (select-window (get-buffer-window (aref calc-embedded-info 1))))
488 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
489 (setq calc-return-buffer buf)
490 (if (and (< (window-width) (frame-width))
492 (let ((win (get-buffer-window (calc-trail-buffer))))
494 (delete-window win))))
495 (set-buffer-modified-p nil)
496 (goto-char calc-edit-top)))
498 (defun calc-edit-return ()
500 (if (and (boundp 'calc-allow-ret) calc-allow-ret)
504 ;; The variable calc-edit-disp-trail is local to calc-edit finish, but
505 ;; is used by calc-finish-selection-edit and calc-finish-stack-edit.
506 (defvar calc-edit-disp-trail)
508 (defun calc-edit-finish (&optional keep)
509 "Finish calc-edit mode. Parse buffer contents and push them on the stack."
511 (message "Working...")
512 (or (and (boundp 'calc-original-buffer)
513 (boundp 'calc-return-buffer)
514 (boundp 'calc-one-window)
515 (boundp 'calc-edit-handler)
516 (boundp 'calc-restore-trail)
517 (eq major-mode 'calc-edit-mode))
518 (error "This command is valid only in buffers created by calc-edit"))
519 (let ((buf (current-buffer))
520 (original calc-original-buffer)
521 (return calc-return-buffer)
522 (one-window calc-one-window)
523 (calc-edit-disp-trail calc-restore-trail))
525 (when (or (null (buffer-name original))
527 (set-buffer original)
528 (not (eq major-mode 'calc-mode))))
529 (error "Original calculator buffer has been corrupted")))
530 (goto-char calc-edit-top)
531 (if (buffer-modified-p)
532 (eval calc-edit-handler))
533 (if (and one-window (not (one-window-p t)))
535 (if (get-buffer-window return)
536 (select-window (get-buffer-window return))
537 (switch-to-buffer return))
541 (if calc-edit-disp-trail
543 (calc-trail-display 1 t)))
546 (defun calc-edit-cancel ()
547 "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
549 (let ((calc-edit-handler nil))
551 (message "(Cancelled)"))
553 (defun calc-finish-stack-edit (num)
554 (let ((buf (current-buffer))
555 (str (buffer-substring calc-edit-top (point-max)))
558 (if (and (integerp num) (> num 1))
559 (while (setq pos (string-match "\n." str))
561 (switch-to-buffer calc-original-buffer)
562 (let ((vals (let ((calc-language nil)
563 (math-expr-opers (math-standard-ops)))
564 (and (string-match "[^\n\t ]" str)
565 (math-read-exprs str)))))
566 (when (eq (car-safe vals) 'error)
567 (switch-to-buffer buf)
568 (goto-char (+ start (nth 1 vals)))
569 (error (nth 2 vals)))
574 (calc-refresh-evaltos num))
575 (if calc-edit-disp-trail
576 (calc-trail-display 1 t))
578 (let ((calc-simplify-mode (if (eq last-command-char ?\C-j)
580 calc-simplify-mode)))
582 (calc-enter-result num "edit" vals)
583 (calc-enter-result 1 "edit" vals (- num))))))))))
588 ;; generated-autoload-file: "calc-loaddefs.el"
591 ;; arch-tag: ca61019e-caca-4daa-b32c-b6afe372d5b5
592 ;;; calc-yank.el ends here