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