1 ;; Calculator for GNU Emacs, part II [calc-yank.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
24 ;; This file is autoloaded from calc-ext.el.
29 (defun calc-Need-calc-yank () nil)
32 ;;; Kill ring commands.
34 (defun calc-kill (nn &optional no-delete)
36 (if (eq major-mode 'calc-mode)
39 (calc-set-command-flag 'no-align)
40 (let ((num (max (calc-locate-cursor-element (point)) 1))
41 (n (prefix-numeric-value nn)))
48 (let ((stuff (calc-top-list n (- num n -1))))
49 (calc-cursor-stack-index num)
50 (let ((first (point)))
51 (calc-cursor-stack-index (- num n))
53 (backward-char 1)) ; don't include newline for raw C-k
54 (copy-region-as-kill first (point))
56 (calc-pop-stack n (- num n -1))))
57 (setq calc-last-kill (cons (car kill-ring) stuff)))))
61 (defun calc-force-refresh ()
62 (if (or calc-executing-macro calc-display-dirty)
63 (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))
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))
81 (defun calc-kill-region (top bot &optional no-delete)
83 (if (eq major-mode 'calc-mode)
86 (calc-set-command-flag 'no-align)
87 (let* ((top-num (calc-locate-cursor-element top))
88 (bot-num (calc-locate-cursor-element (1- bot)))
89 (num (- top-num bot-num -1)))
90 (copy-region-as-kill top bot)
91 (setq calc-last-kill (cons (car kill-ring)
92 (calc-top-list num bot-num)))
94 (calc-pop-stack num bot-num))))
96 (copy-region-as-kill top bot)
97 (kill-region top bot)))
100 (defun calc-copy-as-kill (n)
105 (defun calc-copy-region-as-kill (top bot)
107 (calc-kill-region top bot t)
110 ;;; This function uses calc-last-kill if possible to get an exact result,
111 ;;; otherwise it just parses the yanked string.
112 ;;; Modified to use Emacs 19 extended concept of kill-ring. -- daveg 12/15/96
116 (calc-pop-push-record-list
118 (let ((thing (if (fboundp 'current-kill)
120 (car kill-ring-yank-pointer))))
121 (if (eq (car-safe calc-last-kill) thing)
124 (let ((val (math-read-exprs (calc-clean-newlines thing))))
125 (if (eq (car-safe val) 'error)
127 (setq val (math-read-exprs thing))
128 (if (eq (car-safe val) 'error)
129 (error "Bad format in yanked data")
134 (defun calc-clean-newlines (s)
137 ;; Omit leading/trailing whitespace
138 ((or (string-match "\\`[ \n\r]+\\([^\001]*\\)\\'" s)
139 (string-match "\\`\\([^\001]*\\)[ \n\r]+\\'" s))
140 (calc-clean-newlines (math-match-substring s 1)))
142 ;; Convert newlines to commas
143 ((string-match "\\`\\(.*\\)[\n\r]+\\([^\001]*\\)\\'" s)
144 (calc-clean-newlines (concat (math-match-substring s 1) ","
145 (math-match-substring s 2))))
151 (defun calc-do-grab-region (top bot arg)
152 (and (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)))
198 (defun calc-do-grab-rectangle (top bot arg &optional reduce)
199 (and (memq major-mode '(calc-mode calc-trail-mode))
200 (error "This command works only in a regular text buffer."))
201 (let* ((col1 (save-excursion (goto-char top) (current-column)))
202 (col2 (save-excursion (goto-char bot) (current-column)))
203 (from-buffer (current-buffer))
204 (calc-was-started (get-buffer-window "*Calculator*"))
205 data mat vals lnum pt pos)
209 (error "Point and mark must be at beginning of line, or define a rectangle"))
211 (while (< (point) bot)
214 (setq data (cons (buffer-substring pt (1- (point))) data)))
215 (setq data (nreverse data)))
216 (setq data (extract-rectangle top bot)))
218 (setq mat (list 'vec)
221 (setq arg (if (consp arg) 0 (prefix-numeric-value arg))))
229 (let ((w (length (car data)))
234 (math-read-expr (substring (car data) pos))
235 (math-read-expr (substring (car data) pos j))))
236 (if (eq (car-safe v) 'error)
238 (setq vals (nconc vals (list v))
240 (if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
242 (setq vals (list 'vec (string-to-int (car data))))
244 (string-match "[[{][^][{}]*[]}]" (car data)))
245 (setq pos (match-beginning 0)
246 vals (math-read-expr (math-match-substring (car data) 0)))
247 (let ((s (if (string-match
248 "\\`\\([0-9]+:[ \t]\\)?\\(.*[^, \t]\\)[, \t]*\\'"
250 (math-match-substring (car data) 2)
253 vals (math-read-expr (concat "[" s "]")))
254 (if (eq (car-safe vals) 'error)
255 (let ((v2 (math-read-expr s)))
256 (or (eq (car-safe v2) 'error)
257 (setq vals (list 'vec v2)))))))))
258 (if (eq (car-safe vals) 'error)
261 (pop-to-buffer from-buffer)
263 (switch-to-buffer from-buffer))
266 (forward-char (+ (nth 1 vals) (min col1 col2) pos))
267 (error (nth 2 vals))))
268 (or (equal vals '(vec))
269 (setq mat (cons vals mat)))
270 (setq data (cdr data)
274 (calc-enter-result 0 "grb+" (list reduce '(var add var-add)
276 (calc-enter-result 0 "grab" (nreverse mat)))))
280 (defun calc-copy-to-buffer (nn)
281 "Copy the top of stack into an editing buffer."
283 (let ((thebuf (and (not (memq major-mode '(calc-mode calc-trail-mode)))
290 (let ((n (prefix-numeric-value nn))
291 (eat-lnums calc-line-numbering)
292 (big-offset (if (eq calc-language 'big) 1 0))
294 (setq oldbuf (current-buffer)
296 (calc-find-writable-buffer (buffer-list) 0)
297 (calc-find-writable-buffer (buffer-list) 1)
298 (error "No other buffer")))
299 (cond ((and (or (null nn)
301 (= (calc-substack-height 0)
302 (- (1- (calc-substack-height 1)) big-offset)))
303 (calc-cursor-stack-index 1)
305 (if calc-line-numbering "[0-9]+: *[^ \n]" " *[^ \n]"))
306 (goto-char (1- (match-end 0))))
309 (calc-cursor-stack-index 0)
310 (setq bot (- (1- (point)) big-offset)))
312 (calc-cursor-stack-index n)
314 (calc-cursor-stack-index 0)
315 (setq bot (- (point) big-offset)))
317 (calc-cursor-stack-index (- n))
319 (calc-cursor-stack-index (1- (- n)))
322 (goto-char (point-min))
325 (calc-cursor-stack-index 0)
330 (kill-region (region-beginning) (region-end)))
331 (push-mark (point) t)
332 (if (and overwrite-mode (not (consp nn)))
333 (calc-overwrite-string (save-excursion
335 (buffer-substring top bot))
337 (or (bolp) (setq eat-lnums nil))
338 (insert-buffer-substring oldbuf top bot)
341 (while (and (> (point) (mark))
344 (>= (point) (mark))))
348 (if thebuf (setq movept (point)))
349 (if (get-buffer-window (current-buffer))
350 (set-window-point (get-buffer-window (current-buffer))
352 (if movept (goto-char movept))
357 (switch-to-buffer newbuf))))
360 (defun calc-overwrite-string (str eat-lnums)
361 (if (string-match "\n\\'" str)
362 (setq str (substring str 0 -1)))
364 (setq str (substring str 4)))
365 (if (and (string-match "\\`[-+]?[0-9.]+\\(e-?[0-9]+\\)?\\'" str)
366 (looking-at "[-+]?[0-9.]+\\(e-?[0-9]+\\)?"))
368 (delete-region (point) (match-end 0))
371 (while (< i (length str))
372 (if (= (setq last-command-char (aref str i)) ?\n)
373 (or (= i (1- (length str)))
376 (delete-region pt (point))
380 (if eat-lnums (setq i (+ i 4)))))
381 (self-insert-command 1))
385 ;;; First, require that buffer is visible and does not begin with "*"
386 ;;; Second, require only that it not begin with "*Calc"
387 (defun calc-find-writable-buffer (buf mode)
389 (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
390 (buffer-name (car buf)))
392 (or (string-match "\\`\\*.*" (buffer-name (car buf)))
393 (not (get-buffer-window (car buf))))))
394 (calc-find-writable-buffer (cdr buf) mode)
403 (setq n (calc-stack-size)))
406 (list (math-showing-full-precision
408 (function (lambda (x)
409 (math-format-flat-expr x 0)))
412 (if (math-vectorp x) (setq allow-ret t))
413 (math-format-nice-expr x (frame-width)))))
416 (calc-top-list 1 (- n)))))))
417 (calc-edit-mode (list 'calc-finish-stack-edit (or flag n)) allow-ret)
419 (insert (car list) "\n")
420 (setq list (cdr list)))))
421 (calc-show-edit-buffer)
424 (defun calc-alg-edit (str)
425 (calc-edit-mode '(calc-finish-stack-edit 0))
426 (calc-show-edit-buffer)
429 (calc-set-command-flag 'do-edit)
432 (defvar calc-edit-mode-map nil "Keymap for use by the calc-edit command.")
433 (if calc-edit-mode-map
435 (setq calc-edit-mode-map (make-sparse-keymap))
436 (define-key calc-edit-mode-map "\n" 'calc-edit-finish)
437 (define-key calc-edit-mode-map "\r" 'calc-edit-return)
438 (define-key calc-edit-mode-map "\C-c\C-c" 'calc-edit-finish)
441 (defun calc-edit-mode (&optional handler allow-ret title)
442 "Calculator editing mode. Press RET, LFD, or C-c C-c to finish.
443 To cancel the edit, simply kill the *Calc Edit* buffer."
446 (error "This command can be used only indirectly through calc-edit."))
447 (let ((oldbuf (current-buffer))
448 (buf (get-buffer-create "*Calc Edit*")))
450 (kill-all-local-variables)
451 (use-local-map calc-edit-mode-map)
452 (setq buffer-read-only nil)
453 (setq truncate-lines nil)
454 (setq major-mode 'calc-edit-mode)
455 (setq mode-name "Calc Edit")
456 (run-hooks 'calc-edit-mode-hook)
457 (make-local-variable 'calc-original-buffer)
458 (setq calc-original-buffer oldbuf)
459 (make-local-variable 'calc-return-buffer)
460 (setq calc-return-buffer oldbuf)
461 (make-local-variable 'calc-one-window)
462 (setq calc-one-window (and (one-window-p t) pop-up-windows))
463 (make-local-variable 'calc-edit-handler)
464 (setq calc-edit-handler handler)
465 (make-local-variable 'calc-restore-trail)
466 (setq calc-restore-trail (get-buffer-window (calc-trail-buffer)))
467 (make-local-variable 'calc-allow-ret)
468 (setq calc-allow-ret allow-ret)
470 (insert (or title title "Calc Edit Mode")
472 (if (eq (lookup-key (current-global-map) "\e#") 'calc-dispatch)
474 (if allow-ret "C-c C-c" "RET"))
476 (if (eq (lookup-key (current-global-map) "\e#") 'calc-dispatch)
481 (put 'calc-edit-mode 'mode-class 'special)
483 (defun calc-show-edit-buffer ()
484 (let ((buf (current-buffer)))
485 (if (and (one-window-p t) pop-up-windows)
486 (pop-to-buffer (get-buffer-create "*Calc Edit*"))
487 (and calc-embedded-info (get-buffer-window (aref calc-embedded-info 1))
488 (select-window (get-buffer-window (aref calc-embedded-info 1))))
489 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
490 (setq calc-return-buffer buf)
491 (if (and (< (window-width) (frame-width))
493 (let ((win (get-buffer-window (calc-trail-buffer))))
495 (delete-window win))))
496 (set-buffer-modified-p nil)
497 (goto-char (point-min))
501 (defun calc-edit-return ()
503 (if (and (boundp 'calc-allow-ret) calc-allow-ret)
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 (disp-trail calc-restore-trail))
525 (if (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 (point-min))
531 (if (looking-at "Calc Edit\\|Editing ")
533 (if (buffer-modified-p)
534 (eval calc-edit-handler))
537 (if (get-buffer-window return)
538 (select-window (get-buffer-window return))
539 (switch-to-buffer return))
545 (calc-trail-display 1 t)))
549 (defun calc-edit-cancel ()
550 "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
552 (let ((calc-edit-handler nil))
554 (message "(Cancelled)")
557 (defun calc-finish-stack-edit (num)
558 (let ((buf (current-buffer))
559 (str (buffer-substring (point) (point-max)))
562 (if (and (integerp num) (> num 1))
563 (while (setq pos (string-match "\n." str))
565 (switch-to-buffer calc-original-buffer)
566 (let ((vals (let ((calc-language nil)
567 (math-expr-opers math-standard-opers))
568 (and (string-match "[^\n\t ]" str)
569 (math-read-exprs str)))))
570 (if (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))
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)))))))))