Fix some declarations.
[emacs.git] / lisp / calc / calc-yank.el
blobed70666eb4554a6f1367c0c03fd6177c0e89983c
1 ;;; calc-yank.el --- kill-ring functionality for Calc
3 ;; Copyright (C) 1990-1993, 2001-2016 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;;; Code:
26 ;; This file is autoloaded from calc-ext.el.
28 (require 'calc-ext)
29 (require 'calc-macs)
31 ;;; Kill ring commands.
33 (defun calc-kill (nn &optional no-delete)
34 (interactive "P")
35 (if (eq major-mode 'calc-mode)
36 (calc-wrapper
37 (calc-force-refresh)
38 (calc-set-command-flag 'no-align)
39 (let ((num (max (calc-locate-cursor-element (point)) 1))
40 (n (prefix-numeric-value nn)))
41 (if (< n 0)
42 (progn
43 (if (eobp)
44 (setq num (1- num)))
45 (setq num (- num n)
46 n (- n))))
47 (calc-check-stack num)
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)))))
58 (kill-line nn)))
60 (defun calc-force-refresh ()
61 (if (or calc-executing-macro calc-display-dirty)
62 (let ((calc-executing-macro nil))
63 (calc-refresh))))
65 (defun calc-locate-cursor-element (pt)
66 (save-excursion
67 (goto-char (point-max))
68 (calc-locate-cursor-scan (- calc-stack-top) calc-stack pt)))
70 (defun calc-locate-cursor-scan (n stack pt)
71 (if (or (<= (point) pt)
72 (null stack))
74 (forward-line (- (nth 1 (car stack))))
75 (calc-locate-cursor-scan (1+ n) (cdr stack) pt)))
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 (top-pos (save-excursion
85 (calc-cursor-stack-index top-num)
86 (point)))
87 (bot-num (calc-locate-cursor-element (1- bot)))
88 (bot-pos (save-excursion
89 (calc-cursor-stack-index (max 0 (1- bot-num)))
90 (point)))
91 (num (- top-num bot-num -1)))
92 (copy-region-as-kill top-pos bot-pos)
93 (setq calc-last-kill (cons (car kill-ring)
94 (calc-top-list num bot-num)))
95 (if (not no-delete)
96 (calc-pop-stack num bot-num))))
97 (if no-delete
98 (copy-region-as-kill top bot)
99 (kill-region top bot))))
101 (defun calc-copy-as-kill (n)
102 (interactive "P")
103 (calc-kill n t))
105 (defun calc-copy-region-as-kill (top bot)
106 (interactive "r")
107 (calc-kill-region top bot t))
109 (defun math-number-regexp (radix-num)
110 "Return a regexp which will match a Calc number base RADIX-NUM."
111 (let* ((digit-range
112 (cond
113 ;; radix 2 to 10
114 ((and (<= 2 radix-num)
115 (>= 10 radix-num))
116 (concat "[0-"
117 (number-to-string (1- radix-num))
118 "]"))
119 ;; radix 11
120 ((= 11 radix-num) "[0-9aA]")
121 ;; radix 12+
123 (concat "[0-9"
124 "a-" (format "%c" (+ (- ?a 11) radix-num))
125 "A-" (format "%c" (+ (- ?A 11) radix-num))
126 "]"))))
127 (integer-regexp (concat digit-range "+"))
128 (decimal-regexp (concat digit-range "+\\." digit-range "*")))
129 (concat
130 " *\\("
131 ;; "e" notation
132 "[-_+]?" decimal-regexp "[eE][-+]?[0-9]+"
133 "\\|"
134 "[-_+]?" integer-regexp "[eE][-+]?[0-9]+"
135 "\\|"
136 ;; Integer+fractions
137 "[-_+]?" integer-regexp "*[:/]" integer-regexp "[:/]" integer-regexp
138 "\\|"
139 ;; Fractions
140 "[-_+]?" integer-regexp "[:/]" integer-regexp
141 "\\|"
142 ;; Decimal point
143 "[-_+]?" decimal-regexp
144 "\\|"
145 ;; Integers
146 "[-_+]?" integer-regexp
147 "\\) *\\(\n\\|\\'\\)")))
149 ;; This function uses calc-last-kill if possible to get an exact result,
150 ;; otherwise it just parses the yanked string.
151 ;; Modified to use Emacs 19 extended concept of kill-ring. -- daveg 12/15/96
152 ;;;###autoload
153 (defun calc-yank (radix)
154 "Yank a value into the Calculator buffer.
156 Valid numeric prefixes for RADIX: 0, 2, 6, 8
157 No radix notation is prepended for any other numeric prefix.
159 If RADIX is 2, prepend \"2#\" - Binary.
160 If RADIX is 8, prepend \"8#\" - Octal.
161 If RADIX is 0, prepend \"10#\" - Decimal.
162 If RADIX is 6, prepend \"16#\" - Hexadecimal.
164 If RADIX is a non-nil list (created using \\[universal-argument]), the user
165 will be prompted to enter the radix in the minibuffer.
167 If RADIX is nil or if the yanked string already has a calc radix prefix, the
168 yanked string will be passed on directly to the Calculator buffer without any
169 alteration."
170 (interactive "P")
171 (calc-wrapper
172 (calc-pop-push-record-list
173 0 "yank"
174 (let* (radix-num
175 radix-notation
176 valid-num-regexp
177 (thing-raw
178 (if (fboundp 'current-kill)
179 (current-kill 0 t)
180 (car kill-ring-yank-pointer)))
181 (thing
182 (if (or (null radix)
183 ;; Match examples: -2#10, 10\n(10#10,01)
184 (string-match-p "^[-(]*[0-9]\\{1,2\\}#" thing-raw))
185 thing-raw
186 (progn
187 (if (listp radix)
188 (progn
189 (setq radix-num
190 (read-number
191 "Set radix for yanked content (2-36): "))
192 (when (not (and (integerp radix-num)
193 (<= 2 radix-num)
194 (>= 36 radix-num)))
195 (error (concat "The radix has to be an "
196 "integer between 2 and 36."))))
197 (setq radix-num
198 (cond ((eq radix 2) 2)
199 ((eq radix 8) 8)
200 ((eq radix 0) 10)
201 ((eq radix 6) 16)
202 (t (message
203 (concat "No radix prepended "
204 "for invalid *numeric* "
205 "prefix %0d.")
206 radix)
207 nil))))
208 (if radix-num
209 (progn
210 (setq radix-notation
211 (concat (number-to-string radix-num) "#"))
212 (setq valid-num-regexp
213 (math-number-regexp radix-num))
214 ;; Ensure that the radix-notation is prefixed
215 ;; correctly even for multi-line yanks like below,
216 ;; 111
217 ;; 1111
218 (replace-regexp-in-string
219 valid-num-regexp
220 (concat radix-notation "\\&")
221 thing-raw))
222 thing-raw)))))
223 (if (eq (car-safe calc-last-kill) thing-raw)
224 (cdr calc-last-kill)
225 (if (stringp thing)
226 (let ((val (math-read-exprs (calc-clean-newlines thing))))
227 (if (eq (car-safe val) 'error)
228 (progn
229 (setq val (math-read-exprs thing))
230 (if (eq (car-safe val) 'error)
231 (error "Bad format in yanked data")
232 val))
233 val))))))))
235 ;;; The Calc set- and get-register commands are modified versions of functions
236 ;;; in register.el
238 (defvar calc-register-alist nil
239 "Alist of elements (NAME . (TEXT . CALCVAL)).
240 NAME is a character (a number).
241 TEXT and CALCVAL are the TEXT and internal structure of stack entries.")
243 (defun calc-set-register (register text calcval)
244 "Set the contents of the Calc register REGISTER to (TEXT . CALCVAL),
245 as well as set the contents of the Emacs register REGISTER to TEXT."
246 (set-register register text)
247 (setf (alist-get register calc-register-alist) (cons text calcval)))
249 (defun calc-get-register (reg)
250 "Return the CALCVAL portion of the contents of the Calc register REG,
251 unless the TEXT portion doesn't match the contents of the Emacs register REG,
252 in which case either return the contents of the Emacs register (if it is
253 text) or nil."
254 (let ((cval (cdr (assq reg calc-register-alist)))
255 (val (cdr (assq reg register-alist))))
256 (if (stringp val)
257 (if (and (stringp (car cval))
258 (string= (car cval) val))
259 (cdr cval)
260 val))))
262 (defun calc-copy-to-register (register start end &optional delete-flag)
263 "Copy the lines in the region into register REGISTER.
264 With prefix arg, delete as well.
266 Interactively, reads the register using `register-read-with-preview'."
267 (interactive (list (register-read-with-preview "Copy to register: ")
268 (region-beginning) (region-end)
269 current-prefix-arg))
270 (if (eq major-mode 'calc-mode)
271 (let* ((top-num (calc-locate-cursor-element start))
272 (top-pos (save-excursion
273 (calc-cursor-stack-index top-num)
274 (point)))
275 (bot-num (calc-locate-cursor-element (1- end)))
276 (bot-pos (save-excursion
277 (calc-cursor-stack-index (max 0 (1- bot-num)))
278 (point)))
279 (num (- top-num bot-num -1))
280 (str (buffer-substring top-pos bot-pos)))
281 (calc-set-register register str (calc-top-list num bot-num))
282 (if delete-flag
283 (calc-wrapper
284 (calc-pop-stack num bot-num))))
285 (copy-to-register register start end delete-flag)))
287 (defun calc-insert-register (register)
288 "Insert the contents of register REGISTER.
290 Interactively, reads the register using `register-read-with-preview'."
291 (interactive (list (register-read-with-preview "Insert register: ")))
292 (if (eq major-mode 'calc-mode)
293 (let ((val (calc-get-register register)))
294 (calc-wrapper
295 (calc-pop-push-record-list
296 0 "insr"
297 (if (not val)
298 (error "Bad format in register data")
299 (if (consp val)
301 (let ((nval (math-read-exprs (calc-clean-newlines val))))
302 (if (eq (car-safe nval) 'error)
303 (progn
304 (setq nval (math-read-exprs val))
305 (if (eq (car-safe nval) 'error)
306 (error "Bad format in register data")
307 nval))
308 nval)))))))
309 (insert-register register)))
311 (defun calc-add-to-register (register start end prepend delete-flag)
312 "Add the lines in the region to register REGISTER.
313 If PREPEND is non-nil, add them to the beginning of the register,
314 otherwise the end. If DELETE-FLAG is non-nil, also delete the region."
315 (let* ((top-num (calc-locate-cursor-element start))
316 (top-pos (save-excursion
317 (calc-cursor-stack-index top-num)
318 (point)))
319 (bot-num (calc-locate-cursor-element (1- end)))
320 (bot-pos (save-excursion
321 (calc-cursor-stack-index (max 0 (1- bot-num)))
322 (point)))
323 (num (- top-num bot-num -1))
324 (str (buffer-substring top-pos bot-pos))
325 (calcval (calc-top-list num bot-num))
326 (cval (cdr (assq register calc-register-alist))))
327 (if (not cval)
328 (calc-set-register register str calcval)
329 (if prepend
330 (calc-set-register
331 register
332 (concat str (car cval))
333 (append calcval (cdr cval)))
334 (calc-set-register
335 register
336 (concat (car cval) str)
337 (append (cdr cval) calcval))))
338 (if delete-flag
339 (calc-wrapper
340 (calc-pop-stack num bot-num)))))
342 (defun calc-append-to-register (register start end &optional delete-flag)
343 "Copy the lines in the region to the end of register REGISTER.
344 With prefix arg, also delete the region.
346 Interactively, reads the register using `register-read-with-preview'."
347 (interactive (list (register-read-with-preview "Append to register: ")
348 (region-beginning) (region-end)
349 current-prefix-arg))
350 (if (eq major-mode 'calc-mode)
351 (calc-add-to-register register start end nil delete-flag)
352 (append-to-register register start end delete-flag)))
354 (defun calc-prepend-to-register (register start end &optional delete-flag)
355 "Copy the lines in the region to the beginning of register REGISTER.
356 With prefix arg, also delete the region.
358 Interactively, reads the register using `register-read-with-preview'."
359 (interactive (list (register-read-with-preview "Prepend to register: ")
360 (region-beginning) (region-end)
361 current-prefix-arg))
362 (if (eq major-mode 'calc-mode)
363 (calc-add-to-register register start end t delete-flag)
364 (prepend-to-register register start end delete-flag)))
368 (defun calc-clean-newlines (s)
369 (cond
371 ;; Omit leading/trailing whitespace
372 ((or (string-match "\\`[ \n\r]+\\([^\001]*\\)\\'" s)
373 (string-match "\\`\\([^\001]*\\)[ \n\r]+\\'" s))
374 (calc-clean-newlines (math-match-substring s 1)))
376 ;; Convert newlines to commas
377 ((string-match "\\`\\(.*\\)[\n\r]+\\([^\001]*\\)\\'" s)
378 (calc-clean-newlines (concat (math-match-substring s 1) ","
379 (math-match-substring s 2))))
381 (t s)))
384 (defun calc-do-grab-region (top bot arg)
385 (when (memq major-mode '(calc-mode calc-trail-mode))
386 (error "This command works only in a regular text buffer"))
387 (let* ((from-buffer (current-buffer))
388 (calc-was-started (get-buffer-window "*Calculator*"))
389 (single nil)
390 data vals pos)
391 (if arg
392 (if (consp arg)
393 (setq single t)
394 (setq arg (prefix-numeric-value arg))
395 (if (= arg 0)
396 (setq top (point-at-bol)
397 bot (point-at-eol))
398 (save-excursion
399 (setq top (point))
400 (forward-line arg)
401 (if (> arg 0)
402 (setq bot (point))
403 (setq bot top
404 top (point)))))))
405 (setq data (buffer-substring top bot))
406 (calc)
407 (if single
408 (setq vals (math-read-expr data))
409 (setq vals (math-read-expr (concat "[" data "]")))
410 (and (eq (car-safe vals) 'vec)
411 (= (length vals) 2)
412 (eq (car-safe (nth 1 vals)) 'vec)
413 (setq vals (nth 1 vals))))
414 (if (eq (car-safe vals) 'error)
415 (progn
416 (if calc-was-started
417 (pop-to-buffer from-buffer)
418 (calc-quit t)
419 (switch-to-buffer from-buffer))
420 (goto-char top)
421 (forward-char (+ (nth 1 vals) (if single 0 1)))
422 (error (nth 2 vals))))
423 (calc-slow-wrapper
424 (calc-enter-result 0 "grab" vals))))
427 (defun calc-do-grab-rectangle (top bot arg &optional reduce)
428 (and (memq major-mode '(calc-mode calc-trail-mode))
429 (error "This command works only in a regular text buffer"))
430 (let* ((col1 (save-excursion (goto-char top) (current-column)))
431 (col2 (save-excursion (goto-char bot) (current-column)))
432 (from-buffer (current-buffer))
433 (calc-was-started (get-buffer-window "*Calculator*"))
434 data mat vals lnum pt pos)
435 (if (= col1 col2)
436 (save-excursion
437 (unless (= col1 0)
438 (error "Point and mark must be at beginning of line, or define a rectangle"))
439 (goto-char top)
440 (while (< (point) bot)
441 (setq pt (point))
442 (forward-line 1)
443 (setq data (cons (buffer-substring pt (1- (point))) data)))
444 (setq data (nreverse data)))
445 (setq data (extract-rectangle top bot)))
446 (calc)
447 (setq mat (list 'vec)
448 lnum 0)
449 (when arg
450 (setq arg (if (consp arg) 0 (prefix-numeric-value arg))))
451 (while data
452 (if (natnump arg)
453 (progn
454 (if (= arg 0)
455 (setq arg 1000000))
456 (setq pos 0
457 vals (list 'vec))
458 (let ((w (length (car data)))
459 j v)
460 (while (< pos w)
461 (setq j (+ pos arg)
462 v (if (>= j w)
463 (math-read-expr (substring (car data) pos))
464 (math-read-expr (substring (car data) pos j))))
465 (if (eq (car-safe v) 'error)
466 (setq vals v w 0)
467 (setq vals (nconc vals (list v))
468 pos j)))))
469 (if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
470 (car data))
471 (setq vals (list 'vec (string-to-number (car data))))
472 (if (and (null arg)
473 (string-match "[[{][^][{}]*[]}]" (car data)))
474 (setq pos (match-beginning 0)
475 vals (math-read-expr (math-match-substring (car data) 0)))
476 (let ((s (if (string-match
477 "\\`\\([0-9]+:[ \t]\\)?\\(.*[^, \t]\\)[, \t]*\\'"
478 (car data))
479 (math-match-substring (car data) 2)
480 (car data))))
481 (setq pos -1
482 vals (math-read-expr (concat "[" s "]")))
483 (if (eq (car-safe vals) 'error)
484 (let ((v2 (math-read-expr s)))
485 (unless (eq (car-safe v2) 'error)
486 (setq vals (list 'vec v2)))))))))
487 (if (eq (car-safe vals) 'error)
488 (progn
489 (if calc-was-started
490 (pop-to-buffer from-buffer)
491 (calc-quit t)
492 (switch-to-buffer from-buffer))
493 (goto-char top)
494 (forward-line lnum)
495 (forward-char (+ (nth 1 vals) (min col1 col2) pos))
496 (error (nth 2 vals))))
497 (unless (equal vals '(vec))
498 (setq mat (cons vals mat)))
499 (setq data (cdr data)
500 lnum (1+ lnum)))
501 (calc-slow-wrapper
502 (if reduce
503 (calc-enter-result 0 "grb+" (list reduce '(var add var-add)
504 (nreverse mat)))
505 (calc-enter-result 0 "grab" (nreverse mat))))))
508 (defun calc-copy-to-buffer (nn)
509 "Copy the top of stack into an editing buffer."
510 (interactive "P")
511 (let ((thebuf (and (not (memq major-mode '(calc-mode calc-trail-mode)))
512 (current-buffer)))
513 (movept nil)
514 oldbuf newbuf)
515 (calc-wrapper
516 (save-excursion
517 (calc-force-refresh)
518 (let ((n (prefix-numeric-value nn))
519 (eat-lnums calc-line-numbering)
520 (big-offset (if (eq calc-language 'big) 1 0))
521 top bot)
522 (setq oldbuf (current-buffer)
523 newbuf (or thebuf
524 (calc-find-writable-buffer (buffer-list) 0)
525 (calc-find-writable-buffer (buffer-list) 1)
526 (error "No other buffer")))
527 (cond ((and (or (null nn)
528 (consp nn))
529 (= (calc-substack-height 0)
530 (- (1- (calc-substack-height 1)) big-offset)))
531 (calc-cursor-stack-index 1)
532 (if (looking-at
533 (if calc-line-numbering "[0-9]+: *[^ \n]" " *[^ \n]"))
534 (goto-char (1- (match-end 0))))
535 (setq eat-lnums nil
536 top (point))
537 (calc-cursor-stack-index 0)
538 (setq bot (- (1- (point)) big-offset)))
539 ((> n 0)
540 (calc-cursor-stack-index n)
541 (setq top (point))
542 (calc-cursor-stack-index 0)
543 (setq bot (- (point) big-offset)))
544 ((< n 0)
545 (calc-cursor-stack-index (- n))
546 (setq top (point))
547 (calc-cursor-stack-index (1- (- n)))
548 (setq bot (point)))
550 (goto-char (point-min))
551 (forward-line 1)
552 (setq top (point))
553 (calc-cursor-stack-index 0)
554 (setq bot (point))))
555 (with-current-buffer newbuf
556 (if (consp nn)
557 (kill-region (region-beginning) (region-end)))
558 (push-mark (point) t)
559 (if (and overwrite-mode (not (consp nn)))
560 (calc-overwrite-string (with-current-buffer oldbuf
561 (buffer-substring top bot))
562 eat-lnums)
563 (or (bolp) (setq eat-lnums nil))
564 (insert-buffer-substring oldbuf top bot)
565 (and eat-lnums
566 (let ((n 1))
567 (while (and (> (point) (mark))
568 (progn
569 (forward-line -1)
570 (>= (point) (mark))))
571 (delete-char 4)
572 (setq n (1+ n)))
573 (forward-line n))))
574 (when thebuf
575 (setq movept (point)))
576 (when (get-buffer-window (current-buffer))
577 (set-window-point (get-buffer-window (current-buffer))
578 (point)))))))
579 (when movept
580 (goto-char movept))
581 (when (and (consp nn)
582 (not thebuf))
583 (calc-quit t)
584 (switch-to-buffer newbuf))))
586 (defun calc-overwrite-string (str eat-lnums)
587 (when (string-match "\n\\'" str)
588 (setq str (substring str 0 -1)))
589 (when eat-lnums
590 (setq str (substring str 4)))
591 (if (and (string-match "\\`[-+]?[0-9.]+\\(e-?[0-9]+\\)?\\'" str)
592 (looking-at "[-+]?[0-9.]+\\(e-?[0-9]+\\)?"))
593 (progn
594 (delete-region (point) (match-end 0))
595 (insert str))
596 (let ((i 0))
597 (while (< i (length str))
598 (if (= (setq last-command-event (aref str i)) ?\n)
599 (or (= i (1- (length str)))
600 (let ((pt (point)))
601 (end-of-line)
602 (delete-region pt (point))
603 (if (eobp)
604 (insert "\n")
605 (forward-char 1))
606 (if eat-lnums (setq i (+ i 4)))))
607 (self-insert-command 1))
608 (setq i (1+ i))))))
610 ;; First, require that buffer is visible and does not begin with "*"
611 ;; Second, require only that it not begin with "*Calc"
612 (defun calc-find-writable-buffer (buf mode)
613 (and buf
614 (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
615 (buffer-name (car buf)))
616 (and (= mode 0)
617 (or (string-match "\\`\\*.*" (buffer-name (car buf)))
618 (not (get-buffer-window (car buf))))))
619 (calc-find-writable-buffer (cdr buf) mode)
620 (car buf))))
623 (defun calc-edit (n)
624 (interactive "p")
625 (calc-slow-wrapper
626 (when (eq n 0)
627 (setq n (calc-stack-size)))
628 (let* ((flag nil)
629 (allow-ret (> n 1))
630 (list (math-showing-full-precision
631 (mapcar (if (> n 1)
632 (function (lambda (x)
633 (math-format-flat-expr x 0)))
634 (function
635 (lambda (x)
636 (if (math-vectorp x) (setq allow-ret t))
637 (math-format-nice-expr x (frame-width)))))
638 (if (> n 0)
639 (calc-top-list n)
640 (calc-top-list 1 (- n)))))))
641 (calc-edit-mode (list 'calc-finish-stack-edit (or flag n)) allow-ret)
642 (while list
643 (insert (car list) "\n")
644 (setq list (cdr list)))))
645 (calc-show-edit-buffer))
647 (defun calc-alg-edit (str)
648 (calc-edit-mode '(calc-finish-stack-edit 0))
649 (calc-show-edit-buffer)
650 (insert str "\n")
651 (backward-char 1)
652 (calc-set-command-flag 'do-edit))
654 (defvar calc-edit-mode-map
655 (let ((map (make-sparse-keymap)))
656 (define-key map "\n" 'calc-edit-finish)
657 (define-key map "\r" 'calc-edit-return)
658 (define-key map "\C-c\C-c" 'calc-edit-finish)
659 map)
660 "Keymap for use by the calc-edit command.")
662 (defvar calc-original-buffer)
663 (defvar calc-return-buffer)
664 (defvar calc-one-window)
665 (defvar calc-edit-handler)
666 (defvar calc-restore-trail)
667 (defvar calc-allow-ret)
668 (defvar calc-edit-top)
670 (defun calc-edit-mode (&optional handler allow-ret title)
671 "Calculator editing mode. Press RET, LFD, or C-c C-c to finish.
672 To cancel the edit, simply kill the *Calc Edit* buffer."
673 (interactive)
674 (unless handler
675 (error "This command can be used only indirectly through calc-edit"))
676 (let ((oldbuf (current-buffer))
677 (buf (get-buffer-create "*Calc Edit*")))
678 (set-buffer buf)
679 (kill-all-local-variables)
680 (use-local-map calc-edit-mode-map)
681 (setq buffer-read-only nil)
682 (setq truncate-lines nil)
683 (setq major-mode 'calc-edit-mode)
684 (setq mode-name "Calc Edit")
685 (run-mode-hooks 'calc-edit-mode-hook)
686 (make-local-variable 'calc-original-buffer)
687 (setq calc-original-buffer oldbuf)
688 (make-local-variable 'calc-return-buffer)
689 (setq calc-return-buffer oldbuf)
690 (make-local-variable 'calc-one-window)
691 (setq calc-one-window (and (one-window-p t) pop-up-windows))
692 (make-local-variable 'calc-edit-handler)
693 (setq calc-edit-handler handler)
694 (make-local-variable 'calc-restore-trail)
695 (setq calc-restore-trail (get-buffer-window (calc-trail-buffer)))
696 (make-local-variable 'calc-allow-ret)
697 (setq calc-allow-ret allow-ret)
698 (let ((inhibit-read-only t))
699 (erase-buffer))
700 (add-hook 'kill-buffer-hook (lambda ()
701 (let ((calc-edit-handler nil))
702 (calc-edit-finish t))
703 (message "(Canceled)")) t t)
704 (insert (propertize
705 (concat
706 (or title title "Calc Edit Mode. ")
707 (format-message "Press `C-c C-c'")
708 (if allow-ret "" " or RET")
709 (format-message " to finish, `C-x k RET' to cancel.\n\n"))
710 'font-lock-face 'italic 'read-only t 'rear-nonsticky t 'front-sticky t))
711 (make-local-variable 'calc-edit-top)
712 (setq calc-edit-top (point))))
713 (put 'calc-edit-mode 'mode-class 'special)
715 (defun calc-show-edit-buffer ()
716 (let ((buf (current-buffer)))
717 (if (and (one-window-p t) pop-up-windows)
718 (pop-to-buffer (get-buffer-create "*Calc Edit*"))
719 (and calc-embedded-info (get-buffer-window (aref calc-embedded-info 1))
720 (select-window (get-buffer-window (aref calc-embedded-info 1))))
721 (switch-to-buffer (get-buffer-create "*Calc Edit*")))
722 (setq calc-return-buffer buf)
723 (if (and (< (window-width) (frame-width))
724 calc-display-trail)
725 (let ((win (get-buffer-window (calc-trail-buffer))))
726 (if win
727 (delete-window win))))
728 (set-buffer-modified-p nil)
729 (goto-char calc-edit-top)))
731 (defun calc-edit-return ()
732 (interactive)
733 (if (and (boundp 'calc-allow-ret) calc-allow-ret)
734 (newline)
735 (calc-edit-finish)))
737 ;; The variable calc-edit-disp-trail is local to calc-edit finish, but
738 ;; is used by calc-finish-selection-edit and calc-finish-stack-edit.
739 (defvar calc-edit-disp-trail)
741 (defun calc-edit-finish (&optional keep)
742 "Finish calc-edit mode. Parse buffer contents and push them on the stack."
743 (interactive "P")
744 (message "Working...")
745 (or (and (boundp 'calc-original-buffer)
746 (boundp 'calc-return-buffer)
747 (boundp 'calc-one-window)
748 (boundp 'calc-edit-handler)
749 (boundp 'calc-restore-trail)
750 (eq major-mode 'calc-edit-mode))
751 (error "This command is valid only in buffers created by calc-edit"))
752 (let ((buf (current-buffer))
753 (original calc-original-buffer)
754 (return calc-return-buffer)
755 (one-window calc-one-window)
756 (calc-edit-disp-trail calc-restore-trail))
757 (save-excursion
758 (when (or (null (buffer-name original))
759 (progn
760 (set-buffer original)
761 (not (eq major-mode 'calc-mode))))
762 (error "Original calculator buffer has been corrupted")))
763 (goto-char calc-edit-top)
764 (if (buffer-modified-p)
765 (eval calc-edit-handler))
766 (if (and one-window (not (one-window-p t)))
767 (delete-window))
768 (if (get-buffer-window return)
769 (select-window (get-buffer-window return))
770 (switch-to-buffer return))
771 (if keep
772 (bury-buffer buf)
773 (kill-buffer buf))
774 (if calc-edit-disp-trail
775 (calc-wrapper
776 (calc-trail-display 1 t)))
777 (message "")))
779 (defun calc-edit-cancel ()
780 "Cancel calc-edit mode. Ignore the Calc Edit buffer and don't change stack."
781 (interactive)
782 (let ((calc-edit-handler nil))
783 (calc-edit-finish))
784 (message "(Canceled)"))
786 (defun calc-finish-stack-edit (num)
787 (let ((buf (current-buffer))
788 (str (buffer-substring calc-edit-top (point-max)))
789 (start (point))
790 pos)
791 (if (and (integerp num) (> num 1))
792 (while (setq pos (string-match "\n." str))
793 (aset str pos ?\,)))
794 (switch-to-buffer calc-original-buffer)
795 (let ((vals (let ((calc-language nil)
796 (math-expr-opers (math-standard-ops)))
797 (and (string-match "[^\n\t ]" str)
798 (math-read-exprs str)))))
799 (when (eq (car-safe vals) 'error)
800 (switch-to-buffer buf)
801 (goto-char (+ start (nth 1 vals)))
802 (error (nth 2 vals)))
803 (calc-wrapper
804 (if (symbolp num)
805 (progn
806 (set num (car vals))
807 (calc-refresh-evaltos num))
808 (if calc-edit-disp-trail
809 (calc-trail-display 1 t))
810 (and vals
811 (let ((calc-simplify-mode (if (eq last-command-event ?\C-j)
812 'none
813 calc-simplify-mode)))
814 (if (>= num 0)
815 (calc-enter-result num "edit" vals)
816 (calc-enter-result 1 "edit" vals (- num))))))))))
818 (provide 'calc-yank)
820 ;; Local variables:
821 ;; generated-autoload-file: "calc-loaddefs.el"
822 ;; End:
824 ;;; calc-yank.el ends here