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