Merge from emacs--rel--22
[bpt/emacs.git] / lisp / calc / calc-yank.el
1 ;;; calc-yank.el --- kill-ring functionality for Calc
2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 ;; This file is autoloaded from calc-ext.el.
31
32 (require 'calc-ext)
33 (require 'calc-macs)
34
35 ;;; Kill ring commands.
36
37 (defun calc-kill (nn &optional no-delete)
38 (interactive "P")
39 (if (eq major-mode 'calc-mode)
40 (calc-wrapper
41 (calc-force-refresh)
42 (calc-set-command-flag 'no-align)
43 (let ((num (max (calc-locate-cursor-element (point)) 1))
44 (n (prefix-numeric-value nn)))
45 (if (< n 0)
46 (progn
47 (if (eobp)
48 (setq num (1- num)))
49 (setq num (- num n)
50 n (- n))))
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))
55 (if (null nn)
56 (backward-char 1)) ; don't include newline for raw C-k
57 (copy-region-as-kill first (point))
58 (if (not no-delete)
59 (calc-pop-stack n (- num n -1))))
60 (setq calc-last-kill (cons (car kill-ring) stuff)))))
61 (kill-line nn)))
62
63 (defun calc-force-refresh ()
64 (if (or calc-executing-macro calc-display-dirty)
65 (let ((calc-executing-macro nil))
66 (calc-refresh))))
67
68 (defun calc-locate-cursor-element (pt)
69 (save-excursion
70 (goto-char (point-max))
71 (calc-locate-cursor-scan (- calc-stack-top) calc-stack pt)))
72
73 (defun calc-locate-cursor-scan (n stack pt)
74 (if (or (<= (point) pt)
75 (null stack))
76 n
77 (forward-line (- (nth 1 (car stack))))
78 (calc-locate-cursor-scan (1+ n) (cdr stack) pt)))
79
80 (defun calc-kill-region (top bot &optional no-delete)
81 (interactive "r")
82 (if (eq major-mode 'calc-mode)
83 (calc-wrapper
84 (calc-force-refresh)
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)))
92 (if (not no-delete)
93 (calc-pop-stack num bot-num))))
94 (if no-delete
95 (copy-region-as-kill top bot)
96 (kill-region top bot))))
97
98 (defun calc-copy-as-kill (n)
99 (interactive "P")
100 (calc-kill n t))
101
102 (defun calc-copy-region-as-kill (top bot)
103 (interactive "r")
104 (calc-kill-region top bot t))
105
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
109 ;;;###autoload
110 (defun calc-yank ()
111 (interactive)
112 (calc-wrapper
113 (calc-pop-push-record-list
114 0 "yank"
115 (let ((thing (if (fboundp 'current-kill)
116 (current-kill 0 t)
117 (car kill-ring-yank-pointer))))
118 (if (eq (car-safe calc-last-kill) thing)
119 (cdr calc-last-kill)
120 (if (stringp thing)
121 (let ((val (math-read-exprs (calc-clean-newlines thing))))
122 (if (eq (car-safe val) 'error)
123 (progn
124 (setq val (math-read-exprs thing))
125 (if (eq (car-safe val) 'error)
126 (error "Bad format in yanked data")
127 val))
128 val))))))))
129
130 (defun calc-clean-newlines (s)
131 (cond
132
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)))
137
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))))
142
143 (t s)))
144
145
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*"))
151 (single nil)
152 data vals pos)
153 (if arg
154 (if (consp arg)
155 (setq single t)
156 (setq arg (prefix-numeric-value arg))
157 (if (= arg 0)
158 (save-excursion
159 (beginning-of-line)
160 (setq top (point))
161 (end-of-line)
162 (setq bot (point)))
163 (save-excursion
164 (setq top (point))
165 (forward-line arg)
166 (if (> arg 0)
167 (setq bot (point))
168 (setq bot top
169 top (point)))))))
170 (setq data (buffer-substring top bot))
171 (calc)
172 (if single
173 (setq vals (math-read-expr data))
174 (setq vals (math-read-expr (concat "[" data "]")))
175 (and (eq (car-safe vals) 'vec)
176 (= (length vals) 2)
177 (eq (car-safe (nth 1 vals)) 'vec)
178 (setq vals (nth 1 vals))))
179 (if (eq (car-safe vals) 'error)
180 (progn
181 (if calc-was-started
182 (pop-to-buffer from-buffer)
183 (calc-quit t)
184 (switch-to-buffer from-buffer))
185 (goto-char top)
186 (forward-char (+ (nth 1 vals) (if single 0 1)))
187 (error (nth 2 vals))))
188 (calc-slow-wrapper
189 (calc-enter-result 0 "grab" vals))))
190
191
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)
200 (if (= col1 col2)
201 (save-excursion
202 (unless (= col1 0)
203 (error "Point and mark must be at beginning of line, or define a rectangle"))
204 (goto-char top)
205 (while (< (point) bot)
206 (setq pt (point))
207 (forward-line 1)
208 (setq data (cons (buffer-substring pt (1- (point))) data)))
209 (setq data (nreverse data)))
210 (setq data (extract-rectangle top bot)))
211 (calc)
212 (setq mat (list 'vec)
213 lnum 0)
214 (when arg
215 (setq arg (if (consp arg) 0 (prefix-numeric-value arg))))
216 (while data
217 (if (natnump arg)
218 (progn
219 (if (= arg 0)
220 (setq arg 1000000))
221 (setq pos 0
222 vals (list 'vec))
223 (let ((w (length (car data)))
224 j v)
225 (while (< pos w)
226 (setq j (+ pos arg)
227 v (if (>= j w)
228 (math-read-expr (substring (car data) pos))
229 (math-read-expr (substring (car data) pos j))))
230 (if (eq (car-safe v) 'error)
231 (setq vals v w 0)
232 (setq vals (nconc vals (list v))
233 pos j)))))
234 (if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
235 (car data))
236 (setq vals (list 'vec (string-to-number (car data))))
237 (if (and (null arg)
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]*\\'"
243 (car data))
244 (math-match-substring (car data) 2)
245 (car data))))
246 (setq pos -1
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)
253 (progn
254 (if calc-was-started
255 (pop-to-buffer from-buffer)
256 (calc-quit t)
257 (switch-to-buffer from-buffer))
258 (goto-char top)
259 (forward-line lnum)
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)
265 lnum (1+ lnum)))
266 (calc-slow-wrapper
267 (if reduce
268 (calc-enter-result 0 "grb+" (list reduce '(var add var-add)
269 (nreverse mat)))
270 (calc-enter-result 0 "grab" (nreverse mat))))))
271
272
273 (defun calc-copy-to-buffer (nn)
274 "Copy the top of stack into an editing buffer."
275 (interactive "P")
276 (let ((thebuf (and (not (memq major-mode '(calc-mode calc-trail-mode)))
277 (current-buffer)))
278 (movept nil)
279 oldbuf newbuf)
280 (calc-wrapper
281 (save-excursion
282 (calc-force-refresh)
283 (let ((n (prefix-numeric-value nn))
284 (eat-lnums calc-line-numbering)
285 (big-offset (if (eq calc-language 'big) 1 0))
286 top bot)
287 (setq oldbuf (current-buffer)
288 newbuf (or thebuf
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)
293 (consp nn))
294 (= (calc-substack-height 0)
295 (- (1- (calc-substack-height 1)) big-offset)))
296 (calc-cursor-stack-index 1)
297 (if (looking-at
298 (if calc-line-numbering "[0-9]+: *[^ \n]" " *[^ \n]"))
299 (goto-char (1- (match-end 0))))
300 (setq eat-lnums nil
301 top (point))
302 (calc-cursor-stack-index 0)
303 (setq bot (- (1- (point)) big-offset)))
304 ((> n 0)
305 (calc-cursor-stack-index n)
306 (setq top (point))
307 (calc-cursor-stack-index 0)
308 (setq bot (- (point) big-offset)))
309 ((< n 0)
310 (calc-cursor-stack-index (- n))
311 (setq top (point))
312 (calc-cursor-stack-index (1- (- n)))
313 (setq bot (point)))
314 (t
315 (goto-char (point-min))
316 (forward-line 1)
317 (setq top (point))
318 (calc-cursor-stack-index 0)
319 (setq bot (point))))
320 (save-excursion
321 (set-buffer newbuf)
322 (if (consp nn)
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
327 (set-buffer oldbuf)
328 (buffer-substring top bot))
329 eat-lnums)
330 (or (bolp) (setq eat-lnums nil))
331 (insert-buffer-substring oldbuf top bot)
332 (and eat-lnums
333 (let ((n 1))
334 (while (and (> (point) (mark))
335 (progn
336 (forward-line -1)
337 (>= (point) (mark))))
338 (delete-char 4)
339 (setq n (1+ n)))
340 (forward-line n))))
341 (when thebuf
342 (setq movept (point)))
343 (when (get-buffer-window (current-buffer))
344 (set-window-point (get-buffer-window (current-buffer))
345 (point)))))))
346 (when movept
347 (goto-char movept))
348 (when (and (consp nn)
349 (not thebuf))
350 (calc-quit t)
351 (switch-to-buffer newbuf))))
352
353 (defun calc-overwrite-string (str eat-lnums)
354 (when (string-match "\n\\'" str)
355 (setq str (substring str 0 -1)))
356 (when eat-lnums
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]+\\)?"))
360 (progn
361 (delete-region (point) (match-end 0))
362 (insert str))
363 (let ((i 0))
364 (while (< i (length str))
365 (if (= (setq last-command-char (aref str i)) ?\n)
366 (or (= i (1- (length str)))
367 (let ((pt (point)))
368 (end-of-line)
369 (delete-region pt (point))
370 (if (eobp)
371 (insert "\n")
372 (forward-char 1))
373 (if eat-lnums (setq i (+ i 4)))))
374 (self-insert-command 1))
375 (setq i (1+ i))))))
376
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)
380 (and buf
381 (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
382 (buffer-name (car buf)))
383 (and (= mode 0)
384 (or (string-match "\\`\\*.*" (buffer-name (car buf)))
385 (not (get-buffer-window (car buf))))))
386 (calc-find-writable-buffer (cdr buf) mode)
387 (car buf))))
388
389
390 (defun calc-edit (n)
391 (interactive "p")
392 (calc-slow-wrapper
393 (when (eq n 0)
394 (setq n (calc-stack-size)))
395 (let* ((flag nil)
396 (allow-ret (> n 1))
397 (list (math-showing-full-precision
398 (mapcar (if (> n 1)
399 (function (lambda (x)
400 (math-format-flat-expr x 0)))
401 (function
402 (lambda (x)
403 (if (math-vectorp x) (setq allow-ret t))
404 (math-format-nice-expr x (frame-width)))))
405 (if (> n 0)
406 (calc-top-list n)
407 (calc-top-list 1 (- n)))))))
408 (calc-edit-mode (list 'calc-finish-stack-edit (or flag n)) allow-ret)
409 (while list
410 (insert (car list) "\n")
411 (setq list (cdr list)))))
412 (calc-show-edit-buffer))
413
414 (defun calc-alg-edit (str)
415 (calc-edit-mode '(calc-finish-stack-edit 0))
416 (calc-show-edit-buffer)
417 (insert str "\n")
418 (backward-char 1)
419 (calc-set-command-flag 'do-edit))
420
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)
426 map)
427 "Keymap for use by the calc-edit command.")
428
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)
436
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."
440 (interactive)
441 (unless handler
442 (error "This command can be used only indirectly through calc-edit"))
443 (let ((oldbuf (current-buffer))
444 (buf (get-buffer-create "*Calc Edit*")))
445 (set-buffer buf)
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))
466 (erase-buffer))
467 (add-hook 'kill-buffer-hook (lambda ()
468 (let ((calc-edit-handler nil))
469 (calc-edit-finish t))
470 (message "(Cancelled)")) t t)
471 (insert (propertize
472 (concat
473 (or title title "Calc Edit Mode. ")
474 "Press `C-c C-c'"
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)
481
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))
491 calc-display-trail)
492 (let ((win (get-buffer-window (calc-trail-buffer))))
493 (if win
494 (delete-window win))))
495 (set-buffer-modified-p nil)
496 (goto-char calc-edit-top)))
497
498 (defun calc-edit-return ()
499 (interactive)
500 (if (and (boundp 'calc-allow-ret) calc-allow-ret)
501 (newline)
502 (calc-edit-finish)))
503
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)
507
508 (defun calc-edit-finish (&optional keep)
509 "Finish calc-edit mode. Parse buffer contents and push them on the stack."
510 (interactive "P")
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))
524 (save-excursion
525 (when (or (null (buffer-name original))
526 (progn
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)))
534 (delete-window))
535 (if (get-buffer-window return)
536 (select-window (get-buffer-window return))
537 (switch-to-buffer return))
538 (if keep
539 (bury-buffer buf)
540 (kill-buffer buf))
541 (if calc-edit-disp-trail
542 (calc-wrapper
543 (calc-trail-display 1 t)))
544 (message "")))
545
546 (defun calc-edit-cancel ()
547 "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
548 (interactive)
549 (let ((calc-edit-handler nil))
550 (calc-edit-finish))
551 (message "(Cancelled)"))
552
553 (defun calc-finish-stack-edit (num)
554 (let ((buf (current-buffer))
555 (str (buffer-substring calc-edit-top (point-max)))
556 (start (point))
557 pos)
558 (if (and (integerp num) (> num 1))
559 (while (setq pos (string-match "\n." str))
560 (aset str pos ?\,)))
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)))
570 (calc-wrapper
571 (if (symbolp num)
572 (progn
573 (set num (car vals))
574 (calc-refresh-evaltos num))
575 (if calc-edit-disp-trail
576 (calc-trail-display 1 t))
577 (and vals
578 (let ((calc-simplify-mode (if (eq last-command-char ?\C-j)
579 'none
580 calc-simplify-mode)))
581 (if (>= num 0)
582 (calc-enter-result num "edit" vals)
583 (calc-enter-result 1 "edit" vals (- num))))))))))
584
585 (provide 'calc-yank)
586
587 ;; Local variables:
588 ;; generated-autoload-file: "calc-loaddefs.el"
589 ;; End:
590
591 ;; arch-tag: ca61019e-caca-4daa-b32c-b6afe372d5b5
592 ;;; calc-yank.el ends here