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 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
30 ;; This file is autoloaded from calc-ext.el.
35 (defun calc-Need-calc-yank () nil
)
38 ;;; Kill ring commands.
40 (defun calc-kill (nn &optional no-delete
)
42 (if (eq major-mode
'calc-mode
)
45 (calc-set-command-flag 'no-align
)
46 (let ((num (max (calc-locate-cursor-element (point)) 1))
47 (n (prefix-numeric-value nn
)))
54 (let ((stuff (calc-top-list n
(- num n -
1))))
55 (calc-cursor-stack-index num
)
56 (let ((first (point)))
57 (calc-cursor-stack-index (- num n
))
59 (backward-char 1)) ; don't include newline for raw C-k
60 (copy-region-as-kill first
(point))
62 (calc-pop-stack n
(- num n -
1))))
63 (setq calc-last-kill
(cons (car kill-ring
) stuff
)))))
66 (defun calc-force-refresh ()
67 (if (or calc-executing-macro calc-display-dirty
)
68 (let ((calc-executing-macro nil
))
71 (defun calc-locate-cursor-element (pt)
73 (goto-char (point-max))
74 (calc-locate-cursor-scan (- calc-stack-top
) calc-stack pt
)))
76 (defun calc-locate-cursor-scan (n stack pt
)
77 (if (or (<= (point) pt
)
80 (forward-line (- (nth 1 (car stack
))))
81 (calc-locate-cursor-scan (1+ n
) (cdr stack
) pt
)))
83 (defun calc-kill-region (top bot
&optional no-delete
)
85 (if (eq major-mode
'calc-mode
)
88 (calc-set-command-flag 'no-align
)
89 (let* ((top-num (calc-locate-cursor-element top
))
90 (bot-num (calc-locate-cursor-element (1- bot
)))
91 (num (- top-num bot-num -
1)))
92 (copy-region-as-kill top bot
)
93 (setq calc-last-kill
(cons (car kill-ring
)
94 (calc-top-list num bot-num
)))
96 (calc-pop-stack num bot-num
))))
98 (copy-region-as-kill top bot
)
99 (kill-region top bot
))))
101 (defun calc-copy-as-kill (n)
105 (defun calc-copy-region-as-kill (top bot
)
107 (calc-kill-region top bot t
))
109 ;;; This function uses calc-last-kill if possible to get an exact result,
110 ;;; otherwise it just parses the yanked string.
111 ;;; Modified to use Emacs 19 extended concept of kill-ring. -- daveg 12/15/96
115 (calc-pop-push-record-list
117 (let ((thing (if (fboundp 'current-kill
)
119 (car kill-ring-yank-pointer
))))
120 (if (eq (car-safe calc-last-kill
) thing
)
123 (let ((val (math-read-exprs (calc-clean-newlines thing
))))
124 (if (eq (car-safe val
) 'error
)
126 (setq val
(math-read-exprs thing
))
127 (if (eq (car-safe val
) 'error
)
128 (error "Bad format in yanked data")
132 (defun calc-clean-newlines (s)
135 ;; Omit leading/trailing whitespace
136 ((or (string-match "\\`[ \n\r]+\\([^\001]*\\)\\'" s
)
137 (string-match "\\`\\([^\001]*\\)[ \n\r]+\\'" s
))
138 (calc-clean-newlines (math-match-substring s
1)))
140 ;; Convert newlines to commas
141 ((string-match "\\`\\(.*\\)[\n\r]+\\([^\001]*\\)\\'" s
)
142 (calc-clean-newlines (concat (math-match-substring s
1) ","
143 (math-match-substring s
2))))
148 (defun calc-do-grab-region (top bot arg
)
149 (when (memq major-mode
'(calc-mode calc-trail-mode
))
150 (error "This command works only in a regular text buffer"))
151 (let* ((from-buffer (current-buffer))
152 (calc-was-started (get-buffer-window "*Calculator*"))
158 (setq arg
(prefix-numeric-value arg
))
172 (setq data
(buffer-substring top bot
))
175 (setq vals
(math-read-expr data
))
176 (setq vals
(math-read-expr (concat "[" data
"]")))
177 (and (eq (car-safe vals
) 'vec
)
179 (eq (car-safe (nth 1 vals
)) 'vec
)
180 (setq vals
(nth 1 vals
))))
181 (if (eq (car-safe vals
) 'error
)
184 (pop-to-buffer from-buffer
)
186 (switch-to-buffer from-buffer
))
188 (forward-char (+ (nth 1 vals
) (if single
0 1)))
189 (error (nth 2 vals
))))
191 (calc-enter-result 0 "grab" vals
))))
194 (defun calc-do-grab-rectangle (top bot arg
&optional reduce
)
195 (and (memq major-mode
'(calc-mode calc-trail-mode
))
196 (error "This command works only in a regular text buffer"))
197 (let* ((col1 (save-excursion (goto-char top
) (current-column)))
198 (col2 (save-excursion (goto-char bot
) (current-column)))
199 (from-buffer (current-buffer))
200 (calc-was-started (get-buffer-window "*Calculator*"))
201 data mat vals lnum pt pos
)
205 (error "Point and mark must be at beginning of line, or define a rectangle"))
207 (while (< (point) bot
)
210 (setq data
(cons (buffer-substring pt
(1- (point))) data
)))
211 (setq data
(nreverse data
)))
212 (setq data
(extract-rectangle top bot
)))
214 (setq mat
(list 'vec
)
217 (setq arg
(if (consp arg
) 0 (prefix-numeric-value arg
))))
225 (let ((w (length (car data
)))
230 (math-read-expr (substring (car data
) pos
))
231 (math-read-expr (substring (car data
) pos j
))))
232 (if (eq (car-safe v
) 'error
)
234 (setq vals
(nconc vals
(list v
))
236 (if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
238 (setq vals
(list 'vec
(string-to-int (car data
))))
240 (string-match "[[{][^][{}]*[]}]" (car data
)))
241 (setq pos
(match-beginning 0)
242 vals
(math-read-expr (math-match-substring (car data
) 0)))
243 (let ((s (if (string-match
244 "\\`\\([0-9]+:[ \t]\\)?\\(.*[^, \t]\\)[, \t]*\\'"
246 (math-match-substring (car data
) 2)
249 vals
(math-read-expr (concat "[" s
"]")))
250 (if (eq (car-safe vals
) 'error
)
251 (let ((v2 (math-read-expr s
)))
252 (unless (eq (car-safe v2
) 'error
)
253 (setq vals
(list 'vec v2
)))))))))
254 (if (eq (car-safe vals
) 'error
)
257 (pop-to-buffer from-buffer
)
259 (switch-to-buffer from-buffer
))
262 (forward-char (+ (nth 1 vals
) (min col1 col2
) pos
))
263 (error (nth 2 vals
))))
264 (unless (equal vals
'(vec))
265 (setq mat
(cons vals mat
)))
266 (setq data
(cdr data
)
270 (calc-enter-result 0 "grb+" (list reduce
'(var add var-add
)
272 (calc-enter-result 0 "grab" (nreverse mat
))))))
275 (defun calc-copy-to-buffer (nn)
276 "Copy the top of stack into an editing buffer."
278 (let ((thebuf (and (not (memq major-mode
'(calc-mode calc-trail-mode
)))
285 (let ((n (prefix-numeric-value nn
))
286 (eat-lnums calc-line-numbering
)
287 (big-offset (if (eq calc-language
'big
) 1 0))
289 (setq oldbuf
(current-buffer)
291 (calc-find-writable-buffer (buffer-list) 0)
292 (calc-find-writable-buffer (buffer-list) 1)
293 (error "No other buffer")))
294 (cond ((and (or (null nn
)
296 (= (calc-substack-height 0)
297 (- (1- (calc-substack-height 1)) big-offset
)))
298 (calc-cursor-stack-index 1)
300 (if calc-line-numbering
"[0-9]+: *[^ \n]" " *[^ \n]"))
301 (goto-char (1- (match-end 0))))
304 (calc-cursor-stack-index 0)
305 (setq bot
(- (1- (point)) big-offset
)))
307 (calc-cursor-stack-index n
)
309 (calc-cursor-stack-index 0)
310 (setq bot
(- (point) big-offset
)))
312 (calc-cursor-stack-index (- n
))
314 (calc-cursor-stack-index (1- (- n
)))
317 (goto-char (point-min))
320 (calc-cursor-stack-index 0)
325 (kill-region (region-beginning) (region-end)))
326 (push-mark (point) t
)
327 (if (and overwrite-mode
(not (consp nn
)))
328 (calc-overwrite-string (save-excursion
330 (buffer-substring top bot
))
332 (or (bolp) (setq eat-lnums nil
))
333 (insert-buffer-substring oldbuf top bot
)
336 (while (and (> (point) (mark))
339 (>= (point) (mark))))
344 (setq movept
(point)))
345 (when (get-buffer-window (current-buffer))
346 (set-window-point (get-buffer-window (current-buffer))
350 (when (and (consp nn
)
353 (switch-to-buffer newbuf
))))
355 (defun calc-overwrite-string (str eat-lnums
)
356 (when (string-match "\n\\'" str
)
357 (setq str
(substring str
0 -
1)))
359 (setq str
(substring str
4)))
360 (if (and (string-match "\\`[-+]?[0-9.]+\\(e-?[0-9]+\\)?\\'" str
)
361 (looking-at "[-+]?[0-9.]+\\(e-?[0-9]+\\)?"))
363 (delete-region (point) (match-end 0))
366 (while (< i
(length str
))
367 (if (= (setq last-command-char
(aref str i
)) ?
\n)
368 (or (= i
(1- (length str
)))
371 (delete-region pt
(point))
375 (if eat-lnums
(setq i
(+ i
4)))))
376 (self-insert-command 1))
379 ;;; First, require that buffer is visible and does not begin with "*"
380 ;;; Second, require only that it not begin with "*Calc"
381 (defun calc-find-writable-buffer (buf mode
)
383 (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
384 (buffer-name (car buf
)))
386 (or (string-match "\\`\\*.*" (buffer-name (car buf
)))
387 (not (get-buffer-window (car buf
))))))
388 (calc-find-writable-buffer (cdr buf
) mode
)
396 (setq n
(calc-stack-size)))
399 (list (math-showing-full-precision
401 (function (lambda (x)
402 (math-format-flat-expr x
0)))
405 (if (math-vectorp x
) (setq allow-ret t
))
406 (math-format-nice-expr x
(frame-width)))))
409 (calc-top-list 1 (- n
)))))))
410 (calc-edit-mode (list 'calc-finish-stack-edit
(or flag n
)) allow-ret
)
412 (insert (car list
) "\n")
413 (setq list
(cdr list
)))))
414 (calc-show-edit-buffer))
416 (defun calc-alg-edit (str)
417 (calc-edit-mode '(calc-finish-stack-edit 0))
418 (calc-show-edit-buffer)
421 (calc-set-command-flag 'do-edit
))
423 (defvar calc-edit-mode-map nil
"Keymap for use by the calc-edit command.")
424 (if calc-edit-mode-map
426 (setq calc-edit-mode-map
(make-sparse-keymap))
427 (define-key calc-edit-mode-map
"\n" 'calc-edit-finish
)
428 (define-key calc-edit-mode-map
"\r" 'calc-edit-return
)
429 (define-key calc-edit-mode-map
"\C-c\C-c" 'calc-edit-finish
))
431 (defun calc-edit-mode (&optional handler allow-ret title
)
432 "Calculator editing mode. Press RET, LFD, or C-c C-c to finish.
433 To cancel the edit, simply kill the *Calc Edit* buffer."
436 (error "This command can be used only indirectly through calc-edit"))
437 (let ((oldbuf (current-buffer))
438 (buf (get-buffer-create "*Calc Edit*")))
440 (kill-all-local-variables)
441 (use-local-map calc-edit-mode-map
)
442 (setq buffer-read-only nil
)
443 (setq truncate-lines nil
)
444 (setq major-mode
'calc-edit-mode
)
445 (setq mode-name
"Calc Edit")
446 (run-hooks 'calc-edit-mode-hook
)
447 (make-local-variable 'calc-original-buffer
)
448 (setq calc-original-buffer oldbuf
)
449 (make-local-variable 'calc-return-buffer
)
450 (setq calc-return-buffer oldbuf
)
451 (make-local-variable 'calc-one-window
)
452 (setq calc-one-window
(and (one-window-p t
) pop-up-windows
))
453 (make-local-variable 'calc-edit-handler
)
454 (setq calc-edit-handler handler
)
455 (make-local-variable 'calc-restore-trail
)
456 (setq calc-restore-trail
(get-buffer-window (calc-trail-buffer)))
457 (make-local-variable 'calc-allow-ret
)
458 (setq calc-allow-ret allow-ret
)
460 (insert (or title title
"Calc Edit Mode")
462 (if (eq (lookup-key (current-global-map) "\e#") 'calc-dispatch
)
464 (if allow-ret
"C-c C-c" "RET"))
466 (if (eq (lookup-key (current-global-map) "\e#") 'calc-dispatch
)
470 (put 'calc-edit-mode
'mode-class
'special
)
472 (defun calc-show-edit-buffer ()
473 (let ((buf (current-buffer)))
474 (if (and (one-window-p t
) pop-up-windows
)
475 (pop-to-buffer (get-buffer-create "*Calc Edit*"))
476 (and calc-embedded-info
(get-buffer-window (aref calc-embedded-info
1))
477 (select-window (get-buffer-window (aref calc-embedded-info
1))))
478 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
479 (setq calc-return-buffer buf
)
480 (if (and (< (window-width) (frame-width))
482 (let ((win (get-buffer-window (calc-trail-buffer))))
484 (delete-window win
))))
485 (set-buffer-modified-p nil
)
486 (goto-char (point-min))
489 (defun calc-edit-return ()
491 (if (and (boundp 'calc-allow-ret
) calc-allow-ret
)
495 (defun calc-edit-finish (&optional keep
)
496 "Finish calc-edit mode. Parse buffer contents and push them on the stack."
498 (message "Working...")
499 (or (and (boundp 'calc-original-buffer
)
500 (boundp 'calc-return-buffer
)
501 (boundp 'calc-one-window
)
502 (boundp 'calc-edit-handler
)
503 (boundp 'calc-restore-trail
)
504 (eq major-mode
'calc-edit-mode
))
505 (error "This command is valid only in buffers created by calc-edit"))
506 (let ((buf (current-buffer))
507 (original calc-original-buffer
)
508 (return calc-return-buffer
)
509 (one-window calc-one-window
)
510 (disp-trail calc-restore-trail
))
512 (when (or (null (buffer-name original
))
514 (set-buffer original
)
515 (not (eq major-mode
'calc-mode
))))
516 (error "Original calculator buffer has been corrupted")))
517 (goto-char (point-min))
518 (when (looking-at "Calc Edit\\|Editing ")
520 (if (buffer-modified-p)
521 (eval calc-edit-handler
))
524 (if (get-buffer-window return
)
525 (select-window (get-buffer-window return
))
526 (switch-to-buffer return
))
532 (calc-trail-display 1 t
)))
535 (defun calc-edit-cancel ()
536 "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
538 (let ((calc-edit-handler nil
))
540 (message "(Cancelled)"))
542 (defun calc-finish-stack-edit (num)
543 (let ((buf (current-buffer))
544 (str (buffer-substring (point) (point-max)))
547 (if (and (integerp num
) (> num
1))
548 (while (setq pos
(string-match "\n." str
))
550 (switch-to-buffer calc-original-buffer
)
551 (let ((vals (let ((calc-language nil
)
552 (math-expr-opers math-standard-opers
))
553 (and (string-match "[^\n\t ]" str
)
554 (math-read-exprs str
)))))
555 (when (eq (car-safe vals
) 'error
)
556 (switch-to-buffer buf
)
557 (goto-char (+ start
(nth 1 vals
)))
558 (error (nth 2 vals
)))
563 (calc-refresh-evaltos num
))
565 (calc-trail-display 1 t
))
567 (let ((calc-simplify-mode (if (eq last-command-char ?\C-j
)
569 calc-simplify-mode
)))
571 (calc-enter-result num
"edit" vals
)
572 (calc-enter-result 1 "edit" vals
(- num
))))))))))
574 ;;; calc-yank.el ends here