(calc-record-compilation-date): Remove.
[bpt/emacs.git] / lisp / calc / calc-yank.el
CommitLineData
136211a9 1;; Calculator for GNU Emacs, part II [calc-yank.el]
8f66f479 2;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
136211a9
EZ
3;; Written by Dave Gillespie, daveg@synaptics.com.
4
5;; This file is part of GNU Emacs.
6
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.
13
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.
21
22
23
24;; This file is autoloaded from calc-ext.el.
25(require 'calc-ext)
26
27(require 'calc-macs)
28
29(defun calc-Need-calc-yank () nil)
30
31
32;;; Kill ring commands.
33
34(defun calc-kill (nn &optional no-delete)
35 (interactive "P")
36 (if (eq major-mode 'calc-mode)
37 (calc-wrapper
38 (calc-force-refresh)
39 (calc-set-command-flag 'no-align)
40 (let ((num (max (calc-locate-cursor-element (point)) 1))
41 (n (prefix-numeric-value nn)))
42 (if (< n 0)
43 (progn
44 (if (eobp)
45 (setq num (1- num)))
46 (setq num (- num n)
47 n (- n))))
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))
52 (if (null nn)
53 (backward-char 1)) ; don't include newline for raw C-k
54 (copy-region-as-kill first (point))
55 (if (not no-delete)
56 (calc-pop-stack n (- num n -1))))
57 (setq calc-last-kill (cons (car kill-ring) stuff)))))
bf77c646 58 (kill-line nn)))
136211a9
EZ
59
60(defun calc-force-refresh ()
61 (if (or calc-executing-macro calc-display-dirty)
62 (let ((calc-executing-macro nil))
bf77c646 63 (calc-refresh))))
136211a9
EZ
64
65(defun calc-locate-cursor-element (pt)
66 (save-excursion
67 (goto-char (point-max))
bf77c646 68 (calc-locate-cursor-scan (- calc-stack-top) calc-stack pt)))
136211a9
EZ
69
70(defun calc-locate-cursor-scan (n stack pt)
71 (if (or (<= (point) pt)
72 (null stack))
73 n
74 (forward-line (- (nth 1 (car stack))))
bf77c646 75 (calc-locate-cursor-scan (1+ n) (cdr stack) pt)))
136211a9
EZ
76
77(defun calc-kill-region (top bot &optional no-delete)
78 (interactive "r")
79 (if (eq major-mode 'calc-mode)
80 (calc-wrapper
81 (calc-force-refresh)
82 (calc-set-command-flag 'no-align)
83 (let* ((top-num (calc-locate-cursor-element top))
84 (bot-num (calc-locate-cursor-element (1- bot)))
85 (num (- top-num bot-num -1)))
86 (copy-region-as-kill top bot)
87 (setq calc-last-kill (cons (car kill-ring)
88 (calc-top-list num bot-num)))
89 (if (not no-delete)
90 (calc-pop-stack num bot-num))))
91 (if no-delete
92 (copy-region-as-kill top bot)
bf77c646 93 (kill-region top bot))))
136211a9
EZ
94
95(defun calc-copy-as-kill (n)
96 (interactive "P")
bf77c646 97 (calc-kill n t))
136211a9
EZ
98
99(defun calc-copy-region-as-kill (top bot)
100 (interactive "r")
bf77c646 101 (calc-kill-region top bot t))
136211a9
EZ
102
103;;; This function uses calc-last-kill if possible to get an exact result,
104;;; otherwise it just parses the yanked string.
105;;; Modified to use Emacs 19 extended concept of kill-ring. -- daveg 12/15/96
106(defun calc-yank ()
107 (interactive)
108 (calc-wrapper
109 (calc-pop-push-record-list
110 0 "yank"
111 (let ((thing (if (fboundp 'current-kill)
112 (current-kill 0 t)
113 (car kill-ring-yank-pointer))))
114 (if (eq (car-safe calc-last-kill) thing)
115 (cdr calc-last-kill)
116 (if (stringp thing)
117 (let ((val (math-read-exprs (calc-clean-newlines thing))))
118 (if (eq (car-safe val) 'error)
119 (progn
120 (setq val (math-read-exprs thing))
121 (if (eq (car-safe val) 'error)
122 (error "Bad format in yanked data")
123 val))
bf77c646 124 val))))))))
136211a9
EZ
125
126(defun calc-clean-newlines (s)
127 (cond
128
129 ;; Omit leading/trailing whitespace
130 ((or (string-match "\\`[ \n\r]+\\([^\001]*\\)\\'" s)
131 (string-match "\\`\\([^\001]*\\)[ \n\r]+\\'" s))
132 (calc-clean-newlines (math-match-substring s 1)))
133
134 ;; Convert newlines to commas
135 ((string-match "\\`\\(.*\\)[\n\r]+\\([^\001]*\\)\\'" s)
136 (calc-clean-newlines (concat (math-match-substring s 1) ","
137 (math-match-substring s 2))))
138
bf77c646 139 (t s)))
136211a9
EZ
140
141
142(defun calc-do-grab-region (top bot arg)
143 (and (memq major-mode '(calc-mode calc-trail-mode))
144 (error "This command works only in a regular text buffer."))
145 (let* ((from-buffer (current-buffer))
146 (calc-was-started (get-buffer-window "*Calculator*"))
147 (single nil)
148 data vals pos)
149 (if arg
150 (if (consp arg)
151 (setq single t)
152 (setq arg (prefix-numeric-value arg))
153 (if (= arg 0)
154 (save-excursion
155 (beginning-of-line)
156 (setq top (point))
157 (end-of-line)
158 (setq bot (point)))
159 (save-excursion
160 (setq top (point))
161 (forward-line arg)
162 (if (> arg 0)
163 (setq bot (point))
164 (setq bot top
165 top (point)))))))
166 (setq data (buffer-substring top bot))
167 (calc)
168 (if single
169 (setq vals (math-read-expr data))
170 (setq vals (math-read-expr (concat "[" data "]")))
171 (and (eq (car-safe vals) 'vec)
172 (= (length vals) 2)
173 (eq (car-safe (nth 1 vals)) 'vec)
174 (setq vals (nth 1 vals))))
175 (if (eq (car-safe vals) 'error)
176 (progn
177 (if calc-was-started
178 (pop-to-buffer from-buffer)
179 (calc-quit t)
180 (switch-to-buffer from-buffer))
181 (goto-char top)
182 (forward-char (+ (nth 1 vals) (if single 0 1)))
183 (error (nth 2 vals))))
184 (calc-slow-wrapper
bf77c646 185 (calc-enter-result 0 "grab" vals))))
136211a9
EZ
186
187
188(defun calc-do-grab-rectangle (top bot arg &optional reduce)
189 (and (memq major-mode '(calc-mode calc-trail-mode))
190 (error "This command works only in a regular text buffer."))
191 (let* ((col1 (save-excursion (goto-char top) (current-column)))
192 (col2 (save-excursion (goto-char bot) (current-column)))
193 (from-buffer (current-buffer))
194 (calc-was-started (get-buffer-window "*Calculator*"))
195 data mat vals lnum pt pos)
196 (if (= col1 col2)
197 (save-excursion
198 (or (= col1 0)
199 (error "Point and mark must be at beginning of line, or define a rectangle"))
200 (goto-char top)
201 (while (< (point) bot)
202 (setq pt (point))
203 (forward-line 1)
204 (setq data (cons (buffer-substring pt (1- (point))) data)))
205 (setq data (nreverse data)))
206 (setq data (extract-rectangle top bot)))
207 (calc)
208 (setq mat (list 'vec)
209 lnum 0)
210 (and arg
211 (setq arg (if (consp arg) 0 (prefix-numeric-value arg))))
212 (while data
213 (if (natnump arg)
214 (progn
215 (if (= arg 0)
216 (setq arg 1000000))
217 (setq pos 0
218 vals (list 'vec))
219 (let ((w (length (car data)))
220 j v)
221 (while (< pos w)
222 (setq j (+ pos arg)
223 v (if (>= j w)
224 (math-read-expr (substring (car data) pos))
225 (math-read-expr (substring (car data) pos j))))
226 (if (eq (car-safe v) 'error)
227 (setq vals v w 0)
228 (setq vals (nconc vals (list v))
229 pos j)))))
230 (if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
231 (car data))
232 (setq vals (list 'vec (string-to-int (car data))))
233 (if (and (null arg)
234 (string-match "[[{][^][{}]*[]}]" (car data)))
235 (setq pos (match-beginning 0)
236 vals (math-read-expr (math-match-substring (car data) 0)))
237 (let ((s (if (string-match
238 "\\`\\([0-9]+:[ \t]\\)?\\(.*[^, \t]\\)[, \t]*\\'"
239 (car data))
240 (math-match-substring (car data) 2)
241 (car data))))
242 (setq pos -1
243 vals (math-read-expr (concat "[" s "]")))
244 (if (eq (car-safe vals) 'error)
245 (let ((v2 (math-read-expr s)))
246 (or (eq (car-safe v2) 'error)
247 (setq vals (list 'vec v2)))))))))
248 (if (eq (car-safe vals) 'error)
249 (progn
250 (if calc-was-started
251 (pop-to-buffer from-buffer)
252 (calc-quit t)
253 (switch-to-buffer from-buffer))
254 (goto-char top)
255 (forward-line lnum)
256 (forward-char (+ (nth 1 vals) (min col1 col2) pos))
257 (error (nth 2 vals))))
258 (or (equal vals '(vec))
259 (setq mat (cons vals mat)))
260 (setq data (cdr data)
261 lnum (1+ lnum)))
262 (calc-slow-wrapper
263 (if reduce
264 (calc-enter-result 0 "grb+" (list reduce '(var add var-add)
265 (nreverse mat)))
bf77c646 266 (calc-enter-result 0 "grab" (nreverse mat))))))
136211a9
EZ
267
268
269(defun calc-copy-to-buffer (nn)
270 "Copy the top of stack into an editing buffer."
271 (interactive "P")
272 (let ((thebuf (and (not (memq major-mode '(calc-mode calc-trail-mode)))
273 (current-buffer)))
274 (movept nil)
275 oldbuf newbuf)
276 (calc-wrapper
277 (save-excursion
278 (calc-force-refresh)
279 (let ((n (prefix-numeric-value nn))
280 (eat-lnums calc-line-numbering)
281 (big-offset (if (eq calc-language 'big) 1 0))
282 top bot)
283 (setq oldbuf (current-buffer)
284 newbuf (or thebuf
285 (calc-find-writable-buffer (buffer-list) 0)
286 (calc-find-writable-buffer (buffer-list) 1)
287 (error "No other buffer")))
288 (cond ((and (or (null nn)
289 (consp nn))
290 (= (calc-substack-height 0)
291 (- (1- (calc-substack-height 1)) big-offset)))
292 (calc-cursor-stack-index 1)
293 (if (looking-at
294 (if calc-line-numbering "[0-9]+: *[^ \n]" " *[^ \n]"))
295 (goto-char (1- (match-end 0))))
296 (setq eat-lnums nil
297 top (point))
298 (calc-cursor-stack-index 0)
299 (setq bot (- (1- (point)) big-offset)))
300 ((> n 0)
301 (calc-cursor-stack-index n)
302 (setq top (point))
303 (calc-cursor-stack-index 0)
304 (setq bot (- (point) big-offset)))
305 ((< n 0)
306 (calc-cursor-stack-index (- n))
307 (setq top (point))
308 (calc-cursor-stack-index (1- (- n)))
309 (setq bot (point)))
310 (t
311 (goto-char (point-min))
312 (forward-line 1)
313 (setq top (point))
314 (calc-cursor-stack-index 0)
315 (setq bot (point))))
316 (save-excursion
317 (set-buffer newbuf)
318 (if (consp nn)
319 (kill-region (region-beginning) (region-end)))
320 (push-mark (point) t)
321 (if (and overwrite-mode (not (consp nn)))
322 (calc-overwrite-string (save-excursion
323 (set-buffer oldbuf)
324 (buffer-substring top bot))
325 eat-lnums)
326 (or (bolp) (setq eat-lnums nil))
327 (insert-buffer-substring oldbuf top bot)
328 (and eat-lnums
329 (let ((n 1))
330 (while (and (> (point) (mark))
331 (progn
332 (forward-line -1)
333 (>= (point) (mark))))
334 (delete-char 4)
335 (setq n (1+ n)))
336 (forward-line n))))
337 (if thebuf (setq movept (point)))
338 (if (get-buffer-window (current-buffer))
339 (set-window-point (get-buffer-window (current-buffer))
340 (point)))))))
341 (if movept (goto-char movept))
342 (and (consp nn)
343 (not thebuf)
344 (progn
345 (calc-quit t)
bf77c646 346 (switch-to-buffer newbuf)))))
136211a9
EZ
347
348(defun calc-overwrite-string (str eat-lnums)
349 (if (string-match "\n\\'" str)
350 (setq str (substring str 0 -1)))
351 (if eat-lnums
352 (setq str (substring str 4)))
353 (if (and (string-match "\\`[-+]?[0-9.]+\\(e-?[0-9]+\\)?\\'" str)
354 (looking-at "[-+]?[0-9.]+\\(e-?[0-9]+\\)?"))
355 (progn
356 (delete-region (point) (match-end 0))
357 (insert str))
358 (let ((i 0))
359 (while (< i (length str))
360 (if (= (setq last-command-char (aref str i)) ?\n)
361 (or (= i (1- (length str)))
362 (let ((pt (point)))
363 (end-of-line)
364 (delete-region pt (point))
365 (if (eobp)
366 (insert "\n")
367 (forward-char 1))
368 (if eat-lnums (setq i (+ i 4)))))
369 (self-insert-command 1))
bf77c646 370 (setq i (1+ i))))))
136211a9
EZ
371
372;;; First, require that buffer is visible and does not begin with "*"
373;;; Second, require only that it not begin with "*Calc"
374(defun calc-find-writable-buffer (buf mode)
375 (and buf
376 (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
377 (buffer-name (car buf)))
378 (and (= mode 0)
379 (or (string-match "\\`\\*.*" (buffer-name (car buf)))
380 (not (get-buffer-window (car buf))))))
381 (calc-find-writable-buffer (cdr buf) mode)
bf77c646 382 (car buf))))
136211a9
EZ
383
384
385(defun calc-edit (n)
386 (interactive "p")
387 (calc-slow-wrapper
388 (if (eq n 0)
389 (setq n (calc-stack-size)))
390 (let* ((flag nil)
391 (allow-ret (> n 1))
392 (list (math-showing-full-precision
393 (mapcar (if (> n 1)
394 (function (lambda (x)
395 (math-format-flat-expr x 0)))
396 (function
397 (lambda (x)
398 (if (math-vectorp x) (setq allow-ret t))
8f66f479 399 (math-format-nice-expr x (frame-width)))))
136211a9
EZ
400 (if (> n 0)
401 (calc-top-list n)
402 (calc-top-list 1 (- n)))))))
403 (calc-edit-mode (list 'calc-finish-stack-edit (or flag n)) allow-ret)
404 (while list
405 (insert (car list) "\n")
406 (setq list (cdr list)))))
bf77c646 407 (calc-show-edit-buffer))
136211a9
EZ
408
409(defun calc-alg-edit (str)
410 (calc-edit-mode '(calc-finish-stack-edit 0))
411 (calc-show-edit-buffer)
412 (insert str "\n")
413 (backward-char 1)
bf77c646 414 (calc-set-command-flag 'do-edit))
136211a9
EZ
415
416(defvar calc-edit-mode-map nil "Keymap for use by the calc-edit command.")
417(if calc-edit-mode-map
418 ()
419 (setq calc-edit-mode-map (make-sparse-keymap))
420 (define-key calc-edit-mode-map "\n" 'calc-edit-finish)
421 (define-key calc-edit-mode-map "\r" 'calc-edit-return)
bf77c646 422 (define-key calc-edit-mode-map "\C-c\C-c" 'calc-edit-finish))
136211a9
EZ
423
424(defun calc-edit-mode (&optional handler allow-ret title)
425 "Calculator editing mode. Press RET, LFD, or C-c C-c to finish.
426To cancel the edit, simply kill the *Calc Edit* buffer."
427 (interactive)
428 (or handler
429 (error "This command can be used only indirectly through calc-edit."))
430 (let ((oldbuf (current-buffer))
431 (buf (get-buffer-create "*Calc Edit*")))
432 (set-buffer buf)
433 (kill-all-local-variables)
434 (use-local-map calc-edit-mode-map)
435 (setq buffer-read-only nil)
436 (setq truncate-lines nil)
437 (setq major-mode 'calc-edit-mode)
438 (setq mode-name "Calc Edit")
439 (run-hooks 'calc-edit-mode-hook)
440 (make-local-variable 'calc-original-buffer)
441 (setq calc-original-buffer oldbuf)
442 (make-local-variable 'calc-return-buffer)
443 (setq calc-return-buffer oldbuf)
444 (make-local-variable 'calc-one-window)
445 (setq calc-one-window (and (one-window-p t) pop-up-windows))
446 (make-local-variable 'calc-edit-handler)
447 (setq calc-edit-handler handler)
448 (make-local-variable 'calc-restore-trail)
449 (setq calc-restore-trail (get-buffer-window (calc-trail-buffer)))
450 (make-local-variable 'calc-allow-ret)
451 (setq calc-allow-ret allow-ret)
452 (erase-buffer)
453 (insert (or title title "Calc Edit Mode")
454 ". Press "
455 (if (eq (lookup-key (current-global-map) "\e#") 'calc-dispatch)
456 "M-# M-# or C-c C-c"
457 (if allow-ret "C-c C-c" "RET"))
458 " to finish, "
459 (if (eq (lookup-key (current-global-map) "\e#") 'calc-dispatch)
460 "M-# x"
461 "C-x k RET")
bf77c646 462 " to cancel.\n")))
136211a9
EZ
463(put 'calc-edit-mode 'mode-class 'special)
464
465(defun calc-show-edit-buffer ()
466 (let ((buf (current-buffer)))
467 (if (and (one-window-p t) pop-up-windows)
468 (pop-to-buffer (get-buffer-create "*Calc Edit*"))
469 (and calc-embedded-info (get-buffer-window (aref calc-embedded-info 1))
470 (select-window (get-buffer-window (aref calc-embedded-info 1))))
471 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
472 (setq calc-return-buffer buf)
8f66f479 473 (if (and (< (window-width) (frame-width))
136211a9
EZ
474 calc-display-trail)
475 (let ((win (get-buffer-window (calc-trail-buffer))))
476 (if win
477 (delete-window win))))
478 (set-buffer-modified-p nil)
479 (goto-char (point-min))
bf77c646 480 (forward-line 1)))
136211a9
EZ
481
482(defun calc-edit-return ()
483 (interactive)
484 (if (and (boundp 'calc-allow-ret) calc-allow-ret)
485 (newline)
bf77c646 486 (calc-edit-finish)))
136211a9
EZ
487
488(defun calc-edit-finish (&optional keep)
489 "Finish calc-edit mode. Parse buffer contents and push them on the stack."
490 (interactive "P")
491 (message "Working...")
492 (or (and (boundp 'calc-original-buffer)
493 (boundp 'calc-return-buffer)
494 (boundp 'calc-one-window)
495 (boundp 'calc-edit-handler)
496 (boundp 'calc-restore-trail)
497 (eq major-mode 'calc-edit-mode))
498 (error "This command is valid only in buffers created by calc-edit."))
499 (let ((buf (current-buffer))
500 (original calc-original-buffer)
501 (return calc-return-buffer)
502 (one-window calc-one-window)
503 (disp-trail calc-restore-trail))
504 (save-excursion
505 (if (or (null (buffer-name original))
506 (progn
507 (set-buffer original)
508 (not (eq major-mode 'calc-mode))))
509 (error "Original calculator buffer has been corrupted.")))
510 (goto-char (point-min))
511 (if (looking-at "Calc Edit\\|Editing ")
512 (forward-line 1))
513 (if (buffer-modified-p)
514 (eval calc-edit-handler))
515 (if one-window
516 (delete-window))
517 (if (get-buffer-window return)
518 (select-window (get-buffer-window return))
519 (switch-to-buffer return))
520 (if keep
521 (bury-buffer buf)
522 (kill-buffer buf))
523 (if disp-trail
524 (calc-wrapper
525 (calc-trail-display 1 t)))
bf77c646 526 (message "")))
136211a9
EZ
527
528(defun calc-edit-cancel ()
529 "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
530 (interactive)
531 (let ((calc-edit-handler nil))
532 (calc-edit-finish))
bf77c646 533 (message "(Cancelled)"))
136211a9
EZ
534
535(defun calc-finish-stack-edit (num)
536 (let ((buf (current-buffer))
537 (str (buffer-substring (point) (point-max)))
538 (start (point))
539 pos)
540 (if (and (integerp num) (> num 1))
541 (while (setq pos (string-match "\n." str))
542 (aset str pos ?\,)))
543 (switch-to-buffer calc-original-buffer)
544 (let ((vals (let ((calc-language nil)
545 (math-expr-opers math-standard-opers))
546 (and (string-match "[^\n\t ]" str)
547 (math-read-exprs str)))))
548 (if (eq (car-safe vals) 'error)
549 (progn
550 (switch-to-buffer buf)
551 (goto-char (+ start (nth 1 vals)))
552 (error (nth 2 vals))))
553 (calc-wrapper
554 (if (symbolp num)
555 (progn
556 (set num (car vals))
557 (calc-refresh-evaltos num))
558 (if disp-trail
559 (calc-trail-display 1 t))
560 (and vals
561 (let ((calc-simplify-mode (if (eq last-command-char ?\C-j)
562 'none
563 calc-simplify-mode)))
564 (if (>= num 0)
565 (calc-enter-result num "edit" vals)
bf77c646 566 (calc-enter-result 1 "edit" vals (- num))))))))))
136211a9 567
bf77c646 568;;; calc-yank.el ends here