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: Colin Walters <walters@debian.org>
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 (defun calc-edit-mode (&optional handler allow-ret title
)
431 "Calculator editing mode. Press RET, LFD, or C-c C-c to finish.
432 To cancel the edit, simply kill the *Calc Edit* buffer."
435 (error "This command can be used only indirectly through calc-edit"))
436 (let ((oldbuf (current-buffer))
437 (buf (get-buffer-create "*Calc Edit*")))
439 (kill-all-local-variables)
440 (use-local-map calc-edit-mode-map
)
441 (setq buffer-read-only nil
)
442 (setq truncate-lines nil
)
443 (setq major-mode
'calc-edit-mode
)
444 (setq mode-name
"Calc Edit")
445 (run-hooks 'calc-edit-mode-hook
)
446 (make-local-variable 'calc-original-buffer
)
447 (setq calc-original-buffer oldbuf
)
448 (make-local-variable 'calc-return-buffer
)
449 (setq calc-return-buffer oldbuf
)
450 (make-local-variable 'calc-one-window
)
451 (setq calc-one-window
(and (one-window-p t
) pop-up-windows
))
452 (make-local-variable 'calc-edit-handler
)
453 (setq calc-edit-handler handler
)
454 (make-local-variable 'calc-restore-trail
)
455 (setq calc-restore-trail
(get-buffer-window (calc-trail-buffer)))
456 (make-local-variable 'calc-allow-ret
)
457 (setq calc-allow-ret allow-ret
)
459 (insert (or title title
"Calc Edit Mode")
461 (if (eq (lookup-key (current-global-map) "\e#") 'calc-dispatch
)
463 (if allow-ret
"C-c C-c" "RET"))
465 (if (eq (lookup-key (current-global-map) "\e#") 'calc-dispatch
)
469 (put 'calc-edit-mode
'mode-class
'special
)
471 (defun calc-show-edit-buffer ()
472 (let ((buf (current-buffer)))
473 (if (and (one-window-p t
) pop-up-windows
)
474 (pop-to-buffer (get-buffer-create "*Calc Edit*"))
475 (and calc-embedded-info
(get-buffer-window (aref calc-embedded-info
1))
476 (select-window (get-buffer-window (aref calc-embedded-info
1))))
477 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
478 (setq calc-return-buffer buf
)
479 (if (and (< (window-width) (frame-width))
481 (let ((win (get-buffer-window (calc-trail-buffer))))
483 (delete-window win
))))
484 (set-buffer-modified-p nil
)
485 (goto-char (point-min))
488 (defun calc-edit-return ()
490 (if (and (boundp 'calc-allow-ret
) calc-allow-ret
)
494 (defun calc-edit-finish (&optional keep
)
495 "Finish calc-edit mode. Parse buffer contents and push them on the stack."
497 (message "Working...")
498 (or (and (boundp 'calc-original-buffer
)
499 (boundp 'calc-return-buffer
)
500 (boundp 'calc-one-window
)
501 (boundp 'calc-edit-handler
)
502 (boundp 'calc-restore-trail
)
503 (eq major-mode
'calc-edit-mode
))
504 (error "This command is valid only in buffers created by calc-edit"))
505 (let ((buf (current-buffer))
506 (original calc-original-buffer
)
507 (return calc-return-buffer
)
508 (one-window calc-one-window
)
509 (disp-trail calc-restore-trail
))
511 (when (or (null (buffer-name original
))
513 (set-buffer original
)
514 (not (eq major-mode
'calc-mode
))))
515 (error "Original calculator buffer has been corrupted")))
516 (goto-char (point-min))
517 (when (looking-at "Calc Edit\\|Editing ")
519 (if (buffer-modified-p)
520 (eval calc-edit-handler
))
523 (if (get-buffer-window return
)
524 (select-window (get-buffer-window return
))
525 (switch-to-buffer return
))
531 (calc-trail-display 1 t
)))
534 (defun calc-edit-cancel ()
535 "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
537 (let ((calc-edit-handler nil
))
539 (message "(Cancelled)"))
541 (defun calc-finish-stack-edit (num)
542 (let ((buf (current-buffer))
543 (str (buffer-substring (point) (point-max)))
546 (if (and (integerp num
) (> num
1))
547 (while (setq pos
(string-match "\n." str
))
549 (switch-to-buffer calc-original-buffer
)
550 (let ((vals (let ((calc-language nil
)
551 (math-expr-opers math-standard-opers
))
552 (and (string-match "[^\n\t ]" str
)
553 (math-read-exprs str
)))))
554 (when (eq (car-safe vals
) 'error
)
555 (switch-to-buffer buf
)
556 (goto-char (+ start
(nth 1 vals
)))
557 (error (nth 2 vals
)))
562 (calc-refresh-evaltos num
))
564 (calc-trail-display 1 t
))
566 (let ((calc-simplify-mode (if (eq last-command-char ?\C-j
)
568 calc-simplify-mode
)))
570 (calc-enter-result num
"edit" vals
)
571 (calc-enter-result 1 "edit" vals
(- num
))))))))))
573 ;;; calc-yank.el ends here