calc-yank.el 23.2 KB
Newer Older
1 2
;;; calc-yank.el --- kill-ring functionality for Calc

3
;; Copyright (C) 1990-1993, 2001-2014 Free Software Foundation, Inc.
4 5

;; Author: David Gillespie <daveg@synaptics.com>
Jay Belanger's avatar
Jay Belanger committed
6
;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
Eli Zaretskii's avatar
Eli Zaretskii committed
7 8 9

;; This file is part of GNU Emacs.

10
;; GNU Emacs is free software: you can redistribute it and/or modify
11
;; it under the terms of the GNU General Public License as published by
12 13
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
14

Eli Zaretskii's avatar
Eli Zaretskii committed
15
;; GNU Emacs is distributed in the hope that it will be useful,
16 17 18 19 20
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
21
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Eli Zaretskii's avatar
Eli Zaretskii committed
22

23
;;; Commentary:
Eli Zaretskii's avatar
Eli Zaretskii committed
24

25
;;; Code:
Eli Zaretskii's avatar
Eli Zaretskii committed
26 27 28

;; This file is autoloaded from calc-ext.el.

Jay Belanger's avatar
Jay Belanger committed
29
(require 'calc-ext)
Eli Zaretskii's avatar
Eli Zaretskii committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
(require 'calc-macs)

;;; Kill ring commands.

(defun calc-kill (nn &optional no-delete)
  (interactive "P")
  (if (eq major-mode 'calc-mode)
      (calc-wrapper
       (calc-force-refresh)
       (calc-set-command-flag 'no-align)
       (let ((num (max (calc-locate-cursor-element (point)) 1))
	     (n (prefix-numeric-value nn)))
	 (if (< n 0)
	     (progn
	       (if (eobp)
		   (setq num (1- num)))
	       (setq num (- num n)
		     n (- n))))
48
         (calc-check-stack num)
Eli Zaretskii's avatar
Eli Zaretskii committed
49 50 51 52 53 54 55 56 57 58
	 (let ((stuff (calc-top-list n (- num n -1))))
	   (calc-cursor-stack-index num)
	   (let ((first (point)))
	     (calc-cursor-stack-index (- num n))
	     (if (null nn)
		 (backward-char 1))   ; don't include newline for raw C-k
	     (copy-region-as-kill first (point))
	     (if (not no-delete)
		 (calc-pop-stack n (- num n -1))))
	   (setq calc-last-kill (cons (car kill-ring) stuff)))))
59
    (kill-line nn)))
Eli Zaretskii's avatar
Eli Zaretskii committed
60 61 62 63

(defun calc-force-refresh ()
  (if (or calc-executing-macro calc-display-dirty)
      (let ((calc-executing-macro nil))
64
	(calc-refresh))))
Eli Zaretskii's avatar
Eli Zaretskii committed
65 66 67 68

(defun calc-locate-cursor-element (pt)
  (save-excursion
    (goto-char (point-max))
69
    (calc-locate-cursor-scan (- calc-stack-top) calc-stack pt)))
Eli Zaretskii's avatar
Eli Zaretskii committed
70 71 72 73 74 75

(defun calc-locate-cursor-scan (n stack pt)
  (if (or (<= (point) pt)
	  (null stack))
      n
    (forward-line (- (nth 1 (car stack))))
76
    (calc-locate-cursor-scan (1+ n) (cdr stack) pt)))
Eli Zaretskii's avatar
Eli Zaretskii committed
77 78 79 80 81 82 83 84

(defun calc-kill-region (top bot &optional no-delete)
  (interactive "r")
  (if (eq major-mode 'calc-mode)
      (calc-wrapper
       (calc-force-refresh)
       (calc-set-command-flag 'no-align)
       (let* ((top-num (calc-locate-cursor-element top))
85 86 87
              (top-pos (save-excursion
                         (calc-cursor-stack-index top-num)
                         (point)))
Eli Zaretskii's avatar
Eli Zaretskii committed
88
	      (bot-num (calc-locate-cursor-element (1- bot)))
89 90 91
              (bot-pos (save-excursion
                         (calc-cursor-stack-index (max 0 (1- bot-num)))
                         (point)))
Eli Zaretskii's avatar
Eli Zaretskii committed
92
	      (num (- top-num bot-num -1)))
93
	 (copy-region-as-kill top-pos bot-pos)
Eli Zaretskii's avatar
Eli Zaretskii committed
94 95 96 97 98 99
	 (setq calc-last-kill (cons (car kill-ring)
				    (calc-top-list num bot-num)))
	 (if (not no-delete)
	     (calc-pop-stack num bot-num))))
    (if no-delete
	(copy-region-as-kill top bot)
100
      (kill-region top bot))))
Eli Zaretskii's avatar
Eli Zaretskii committed
101 102 103

(defun calc-copy-as-kill (n)
  (interactive "P")
104
  (calc-kill n t))
Eli Zaretskii's avatar
Eli Zaretskii committed
105 106 107

(defun calc-copy-region-as-kill (top bot)
  (interactive "r")
108
  (calc-kill-region top bot t))
Eli Zaretskii's avatar
Eli Zaretskii committed
109

110 111 112 113
;; This function uses calc-last-kill if possible to get an exact result,
;; otherwise it just parses the yanked string.
;; Modified to use Emacs 19 extended concept of kill-ring. -- daveg 12/15/96
;;;###autoload
Eli Zaretskii's avatar
Eli Zaretskii committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
(defun calc-yank ()
  (interactive)
  (calc-wrapper
   (calc-pop-push-record-list
    0 "yank"
    (let ((thing (if (fboundp 'current-kill)
		     (current-kill 0 t)
		   (car kill-ring-yank-pointer))))
      (if (eq (car-safe calc-last-kill) thing)
	  (cdr calc-last-kill)
	(if (stringp thing)
	    (let ((val (math-read-exprs (calc-clean-newlines thing))))
	      (if (eq (car-safe val) 'error)
		  (progn
		    (setq val (math-read-exprs thing))
		    (if (eq (car-safe val) 'error)
			(error "Bad format in yanked data")
		      val))
132
		val))))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
133

Paul Eggert's avatar
Paul Eggert committed
134
;;; The Calc set- and get-register commands are modified versions of functions
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
;;; in register.el

(defvar calc-register-alist nil
  "Alist of elements (NAME . (TEXT . CALCVAL)).
NAME is a character (a number).
TEXT and CALCVAL are the TEXT and internal structure of stack entries.")

(defun calc-set-register (register text calcval)
  "Set the contents of the Calc register REGISTER to (TEXT . CALCVAL),
as well as set the contents of the Emacs register REGISTER to TEXT."
  (set-register register text)
  (let ((aelt (assq register calc-register-alist)))
    (if aelt
        (setcdr aelt (cons text calcval))
      (push (cons register (cons text calcval)) calc-register-alist))))

(defun calc-get-register (reg)
  "Return the CALCVAL portion of the contents of the Calc register REG,
unless the TEXT portion doesn't match the contents of the Emacs register REG,
in which case either return the contents of the Emacs register (if it is
text) or `nil'."
  (let ((cval (cdr (assq reg calc-register-alist)))
        (val (cdr (assq reg register-alist))))
158 159 160
    (if (stringp val)
        (if (and (stringp (car cval))
                 (string= (car cval) val))
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            (cdr cval)
          val))))

(defun calc-copy-to-register (register start end &optional delete-flag)
  "Copy the lines in the region into register REGISTER.
With prefix arg, delete as well."
  (interactive "cCopy to register: \nr\nP")
  (if (eq major-mode 'calc-mode)
      (let* ((top-num (calc-locate-cursor-element start))
             (top-pos (save-excursion
                        (calc-cursor-stack-index top-num)
                        (point)))
             (bot-num (calc-locate-cursor-element (1- end)))
             (bot-pos (save-excursion
                        (calc-cursor-stack-index (max 0 (1- bot-num)))
                        (point)))
             (num (- top-num bot-num -1))
             (str (buffer-substring top-pos bot-pos)))
        (calc-set-register register str (calc-top-list num bot-num))
        (if delete-flag
            (calc-wrapper
             (calc-pop-stack num bot-num))))
    (copy-to-register register start end delete-flag)))

(defun calc-insert-register (register)
  "Insert the contents of register REGISTER."
  (interactive "cInsert register: ")
  (if (eq major-mode 'calc-mode)
      (let ((val (calc-get-register register)))
        (calc-wrapper
         (calc-pop-push-record-list
          0 "insr"
          (if (not val)
              (error "Bad format in register data")
            (if (consp val)
                val
              (let ((nval (math-read-exprs (calc-clean-newlines val))))
                (if (eq (car-safe nval) 'error)
                    (progn
                      (setq nval (math-read-exprs val))
                      (if (eq (car-safe nval) 'error)
                          (error "Bad format in register data")
                        nval))
                  nval)))))))
    (insert-register register)))

(defun calc-add-to-register (register start end prepend delete-flag)
  "Add the lines in the region to register REGISTER.
Paul Eggert's avatar
Paul Eggert committed
209
If PREPEND is non-nil, add them to the beginning of the register,
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
otherwise the end.  If DELETE-FLAG is non-nil, also delete the region."
  (let* ((top-num (calc-locate-cursor-element start))
         (top-pos (save-excursion
                    (calc-cursor-stack-index top-num)
                    (point)))
         (bot-num (calc-locate-cursor-element (1- end)))
         (bot-pos (save-excursion
                    (calc-cursor-stack-index (max 0 (1- bot-num)))
                    (point)))
         (num (- top-num bot-num -1))
         (str (buffer-substring top-pos bot-pos))
         (calcval (calc-top-list num bot-num))
         (cval (cdr (assq register calc-register-alist))))
    (if (not cval)
        (calc-set-register register str calcval)
      (if prepend
          (calc-set-register
           register
           (concat str (car cval))
           (append calcval (cdr cval)))
        (calc-set-register
         register
         (concat (car cval) str)
         (append (cdr cval) calcval))))
    (if delete-flag
        (calc-wrapper
         (calc-pop-stack num bot-num)))))

(defun calc-append-to-register (register start end &optional delete-flag)
  "Copy the lines in the region to the end of register REGISTER.
With prefix arg, also delete the region."
  (interactive "cAppend to register: \nr\nP")
  (if (eq major-mode 'calc-mode)
      (calc-add-to-register register start end nil delete-flag)
    (append-to-register register start end delete-flag)))
Paul Eggert's avatar
Paul Eggert committed
245

246 247 248 249 250 251 252
(defun calc-prepend-to-register (register start end &optional delete-flag)
  "Copy the lines in the region to the beginning of register REGISTER.
With prefix arg, also delete the region."
  (interactive "cPrepend to register: \nr\nP")
  (if (eq major-mode 'calc-mode)
      (calc-add-to-register register start end t delete-flag)
    (prepend-to-register register start end delete-flag)))
Paul Eggert's avatar
Paul Eggert committed
253

254 255


Eli Zaretskii's avatar
Eli Zaretskii committed
256 257
(defun calc-clean-newlines (s)
  (cond
258

Eli Zaretskii's avatar
Eli Zaretskii committed
259 260 261 262 263 264 265 266 267
   ;; Omit leading/trailing whitespace
   ((or (string-match "\\`[ \n\r]+\\([^\001]*\\)\\'" s)
	(string-match "\\`\\([^\001]*\\)[ \n\r]+\\'" s))
    (calc-clean-newlines (math-match-substring s 1)))

   ;; Convert newlines to commas
   ((string-match "\\`\\(.*\\)[\n\r]+\\([^\001]*\\)\\'" s)
    (calc-clean-newlines (concat (math-match-substring s 1) ","
				 (math-match-substring s 2))))
268

269
   (t s)))
Eli Zaretskii's avatar
Eli Zaretskii committed
270 271 272


(defun calc-do-grab-region (top bot arg)
273 274
  (when (memq major-mode '(calc-mode calc-trail-mode))
    (error "This command works only in a regular text buffer"))
Eli Zaretskii's avatar
Eli Zaretskii committed
275 276 277 278 279 280 281 282 283
  (let* ((from-buffer (current-buffer))
	 (calc-was-started (get-buffer-window "*Calculator*"))
	 (single nil)
	 data vals pos)
    (if arg
	(if (consp arg)
	    (setq single t)
	  (setq arg (prefix-numeric-value arg))
	  (if (= arg 0)
284 285
	      (setq top (point-at-bol)
		    bot (point-at-eol))
Eli Zaretskii's avatar
Eli Zaretskii committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	    (save-excursion
	      (setq top (point))
	      (forward-line arg)
	      (if (> arg 0)
		  (setq bot (point))
		(setq bot top
		      top (point)))))))
    (setq data (buffer-substring top bot))
    (calc)
    (if single
	(setq vals (math-read-expr data))
      (setq vals (math-read-expr (concat "[" data "]")))
      (and (eq (car-safe vals) 'vec)
	   (= (length vals) 2)
	   (eq (car-safe (nth 1 vals)) 'vec)
	   (setq vals (nth 1 vals))))
    (if (eq (car-safe vals) 'error)
	(progn
	  (if calc-was-started
	      (pop-to-buffer from-buffer)
	    (calc-quit t)
	    (switch-to-buffer from-buffer))
	  (goto-char top)
	  (forward-char (+ (nth 1 vals) (if single 0 1)))
	  (error (nth 2 vals))))
    (calc-slow-wrapper
312
     (calc-enter-result 0 "grab" vals))))
Eli Zaretskii's avatar
Eli Zaretskii committed
313 314 315 316


(defun calc-do-grab-rectangle (top bot arg &optional reduce)
  (and (memq major-mode '(calc-mode calc-trail-mode))
317
       (error "This command works only in a regular text buffer"))
Eli Zaretskii's avatar
Eli Zaretskii committed
318 319 320 321 322 323 324
  (let* ((col1 (save-excursion (goto-char top) (current-column)))
	 (col2 (save-excursion (goto-char bot) (current-column)))
	 (from-buffer (current-buffer))
	 (calc-was-started (get-buffer-window "*Calculator*"))
	 data mat vals lnum pt pos)
    (if (= col1 col2)
	(save-excursion
325 326
	  (unless (= col1 0)
	    (error "Point and mark must be at beginning of line, or define a rectangle"))
Eli Zaretskii's avatar
Eli Zaretskii committed
327 328 329 330 331 332 333 334 335 336
	  (goto-char top)
	  (while (< (point) bot)
	    (setq pt (point))
	    (forward-line 1)
	    (setq data (cons (buffer-substring pt (1- (point))) data)))
	  (setq data (nreverse data)))
      (setq data (extract-rectangle top bot)))
    (calc)
    (setq mat (list 'vec)
	  lnum 0)
337 338
    (when arg
      (setq arg (if (consp arg) 0 (prefix-numeric-value arg))))
Eli Zaretskii's avatar
Eli Zaretskii committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    (while data
      (if (natnump arg)
	  (progn
	    (if (= arg 0)
		(setq arg 1000000))
	    (setq pos 0
		  vals (list 'vec))
	    (let ((w (length (car data)))
		  j v)
	      (while (< pos w)
		(setq j (+ pos arg)
		      v (if (>= j w)
			    (math-read-expr (substring (car data) pos))
			  (math-read-expr (substring (car data) pos j))))
		(if (eq (car-safe v) 'error)
		    (setq vals v w 0)
		  (setq vals (nconc vals (list v))
			pos j)))))
	(if (string-match "\\` *-?[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]? *\\'"
			  (car data))
359
	    (setq vals (list 'vec (string-to-number (car data))))
Eli Zaretskii's avatar
Eli Zaretskii committed
360 361 362 363 364 365 366 367 368 369 370 371 372
	  (if (and (null arg)
		   (string-match "[[{][^][{}]*[]}]" (car data)))
	      (setq pos (match-beginning 0)
		    vals (math-read-expr (math-match-substring (car data) 0)))
	    (let ((s (if (string-match
			  "\\`\\([0-9]+:[ \t]\\)?\\(.*[^, \t]\\)[, \t]*\\'"
			  (car data))
			 (math-match-substring (car data) 2)
		       (car data))))
	      (setq pos -1
		    vals (math-read-expr (concat "[" s "]")))
	      (if (eq (car-safe vals) 'error)
		  (let ((v2 (math-read-expr s)))
373 374
		    (unless (eq (car-safe v2) 'error)
		      (setq vals (list 'vec v2)))))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
375 376 377 378 379 380 381 382 383 384
      (if (eq (car-safe vals) 'error)
	  (progn
	    (if calc-was-started
		(pop-to-buffer from-buffer)
	      (calc-quit t)
	      (switch-to-buffer from-buffer))
	    (goto-char top)
	    (forward-line lnum)
	    (forward-char (+ (nth 1 vals) (min col1 col2) pos))
	    (error (nth 2 vals))))
385 386
      (unless (equal vals '(vec))
	(setq mat (cons vals mat)))
Eli Zaretskii's avatar
Eli Zaretskii committed
387 388 389 390 391 392
      (setq data (cdr data)
	    lnum (1+ lnum)))
    (calc-slow-wrapper
     (if reduce
	 (calc-enter-result 0 "grb+" (list reduce '(var add var-add)
					   (nreverse mat)))
393
       (calc-enter-result 0 "grab" (nreverse mat))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442


(defun calc-copy-to-buffer (nn)
  "Copy the top of stack into an editing buffer."
  (interactive "P")
  (let ((thebuf (and (not (memq major-mode '(calc-mode calc-trail-mode)))
		     (current-buffer)))
	(movept nil)
	oldbuf newbuf)
    (calc-wrapper
     (save-excursion
       (calc-force-refresh)
       (let ((n (prefix-numeric-value nn))
	     (eat-lnums calc-line-numbering)
	     (big-offset (if (eq calc-language 'big) 1 0))
	     top bot)
	 (setq oldbuf (current-buffer)
	       newbuf (or thebuf
			  (calc-find-writable-buffer (buffer-list) 0)
			  (calc-find-writable-buffer (buffer-list) 1)
			  (error "No other buffer")))
	 (cond ((and (or (null nn)
			 (consp nn))
		     (= (calc-substack-height 0)
			(- (1- (calc-substack-height 1)) big-offset)))
		(calc-cursor-stack-index 1)
		(if (looking-at
		     (if calc-line-numbering "[0-9]+: *[^ \n]" " *[^ \n]"))
		    (goto-char (1- (match-end 0))))
		(setq eat-lnums nil
		      top (point))
		(calc-cursor-stack-index 0)
		(setq bot (- (1- (point)) big-offset)))
	       ((> n 0)
		(calc-cursor-stack-index n)
		(setq top (point))
		(calc-cursor-stack-index 0)
		(setq bot (- (point) big-offset)))
	       ((< n 0)
		(calc-cursor-stack-index (- n))
		(setq top (point))
		(calc-cursor-stack-index (1- (- n)))
		(setq bot (point)))
	       (t
		(goto-char (point-min))
		(forward-line 1)
		(setq top (point))
		(calc-cursor-stack-index 0)
		(setq bot (point))))
443
	 (with-current-buffer newbuf
Eli Zaretskii's avatar
Eli Zaretskii committed
444 445 446 447
	   (if (consp nn)
	       (kill-region (region-beginning) (region-end)))
	   (push-mark (point) t)
	   (if (and overwrite-mode (not (consp nn)))
448
	       (calc-overwrite-string (with-current-buffer oldbuf
Eli Zaretskii's avatar
Eli Zaretskii committed
449 450 451 452 453 454 455 456 457 458 459 460 461
					(buffer-substring top bot))
				      eat-lnums)
	     (or (bolp) (setq eat-lnums nil))
	     (insert-buffer-substring oldbuf top bot)
	     (and eat-lnums
		  (let ((n 1))
		    (while (and (> (point) (mark))
				(progn
				  (forward-line -1)
				  (>= (point) (mark))))
		      (delete-char 4)
		      (setq n (1+ n)))
		    (forward-line n))))
462 463 464 465 466 467 468 469 470 471 472
	   (when thebuf
	     (setq movept (point)))
	   (when (get-buffer-window (current-buffer))
	     (set-window-point (get-buffer-window (current-buffer))
			       (point)))))))
    (when movept
      (goto-char movept))
    (when (and (consp nn)
	       (not thebuf))
      (calc-quit t)
      (switch-to-buffer newbuf))))
Eli Zaretskii's avatar
Eli Zaretskii committed
473 474

(defun calc-overwrite-string (str eat-lnums)
475 476 477 478
  (when (string-match "\n\\'" str)
    (setq str (substring str 0 -1)))
  (when eat-lnums
    (setq str (substring str 4)))
Eli Zaretskii's avatar
Eli Zaretskii committed
479 480 481 482 483 484 485
  (if (and (string-match "\\`[-+]?[0-9.]+\\(e-?[0-9]+\\)?\\'" str)
	   (looking-at "[-+]?[0-9.]+\\(e-?[0-9]+\\)?"))
      (progn
	(delete-region (point) (match-end 0))
	(insert str))
    (let ((i 0))
      (while (< i (length str))
486
	(if (= (setq last-command-event (aref str i)) ?\n)
Eli Zaretskii's avatar
Eli Zaretskii committed
487 488 489 490 491 492 493 494 495
	    (or (= i (1- (length str)))
		(let ((pt (point)))
		  (end-of-line)
		  (delete-region pt (point))
		  (if (eobp)
		      (insert "\n")
		    (forward-char 1))
		  (if eat-lnums (setq i (+ i 4)))))
	  (self-insert-command 1))
496
	(setq i (1+ i))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
497

498 499
;; First, require that buffer is visible and does not begin with "*"
;; Second, require only that it not begin with "*Calc"
Eli Zaretskii's avatar
Eli Zaretskii committed
500 501 502 503 504 505 506 507
(defun calc-find-writable-buffer (buf mode)
  (and buf
       (if (or (string-match "\\`\\( .*\\|\\*Calc.*\\)"
			     (buffer-name (car buf)))
	       (and (= mode 0)
		    (or (string-match "\\`\\*.*" (buffer-name (car buf)))
			(not (get-buffer-window (car buf))))))
	   (calc-find-writable-buffer (cdr buf) mode)
508
	 (car buf))))
Eli Zaretskii's avatar
Eli Zaretskii committed
509 510 511 512 513


(defun calc-edit (n)
  (interactive "p")
  (calc-slow-wrapper
514 515
   (when (eq n 0)
     (setq n (calc-stack-size)))
Eli Zaretskii's avatar
Eli Zaretskii committed
516 517 518 519 520 521 522 523 524
   (let* ((flag nil)
	  (allow-ret (> n 1))
	  (list (math-showing-full-precision
		 (mapcar (if (> n 1)
			     (function (lambda (x)
					 (math-format-flat-expr x 0)))
			   (function
			    (lambda (x)
			      (if (math-vectorp x) (setq allow-ret t))
525
			      (math-format-nice-expr x (frame-width)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
526 527 528 529 530 531 532
			 (if (> n 0)
			     (calc-top-list n)
			   (calc-top-list 1 (- n)))))))
     (calc-edit-mode (list 'calc-finish-stack-edit (or flag n)) allow-ret)
     (while list
       (insert (car list) "\n")
       (setq list (cdr list)))))
533
  (calc-show-edit-buffer))
Eli Zaretskii's avatar
Eli Zaretskii committed
534 535 536 537 538 539

(defun calc-alg-edit (str)
  (calc-edit-mode '(calc-finish-stack-edit 0))
  (calc-show-edit-buffer)
  (insert str "\n")
  (backward-char 1)
540
  (calc-set-command-flag 'do-edit))
Eli Zaretskii's avatar
Eli Zaretskii committed
541

542 543 544 545 546 547 548
(defvar calc-edit-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "\n" 'calc-edit-finish)
    (define-key map "\r" 'calc-edit-return)
    (define-key map "\C-c\C-c" 'calc-edit-finish)
    map)
  "Keymap for use by the calc-edit command.")
Eli Zaretskii's avatar
Eli Zaretskii committed
549

550 551 552 553 554 555
(defvar calc-original-buffer)
(defvar calc-return-buffer)
(defvar calc-one-window)
(defvar calc-edit-handler)
(defvar calc-restore-trail)
(defvar calc-allow-ret)
Jay Belanger's avatar
Jay Belanger committed
556
(defvar calc-edit-top)
557

Eli Zaretskii's avatar
Eli Zaretskii committed
558 559 560 561
(defun calc-edit-mode (&optional handler allow-ret title)
  "Calculator editing mode.  Press RET, LFD, or C-c C-c to finish.
To cancel the edit, simply kill the *Calc Edit* buffer."
  (interactive)
562 563
  (unless handler
    (error "This command can be used only indirectly through calc-edit"))
Eli Zaretskii's avatar
Eli Zaretskii committed
564 565 566 567 568 569 570 571 572
  (let ((oldbuf (current-buffer))
	(buf (get-buffer-create "*Calc Edit*")))
    (set-buffer buf)
    (kill-all-local-variables)
    (use-local-map calc-edit-mode-map)
    (setq buffer-read-only nil)
    (setq truncate-lines nil)
    (setq major-mode 'calc-edit-mode)
    (setq mode-name "Calc Edit")
573
    (run-mode-hooks 'calc-edit-mode-hook)
Eli Zaretskii's avatar
Eli Zaretskii committed
574 575 576 577 578 579 580 581 582 583 584 585
    (make-local-variable 'calc-original-buffer)
    (setq calc-original-buffer oldbuf)
    (make-local-variable 'calc-return-buffer)
    (setq calc-return-buffer oldbuf)
    (make-local-variable 'calc-one-window)
    (setq calc-one-window (and (one-window-p t) pop-up-windows))
    (make-local-variable 'calc-edit-handler)
    (setq calc-edit-handler handler)
    (make-local-variable 'calc-restore-trail)
    (setq calc-restore-trail (get-buffer-window (calc-trail-buffer)))
    (make-local-variable 'calc-allow-ret)
    (setq calc-allow-ret allow-ret)
586 587
    (let ((inhibit-read-only t))
      (erase-buffer))
Paul Eggert's avatar
Paul Eggert committed
588
    (add-hook 'kill-buffer-hook (lambda ()
589 590
                                  (let ((calc-edit-handler nil))
                                    (calc-edit-finish t))
Paul Eggert's avatar
Paul Eggert committed
591
                                  (message "(Canceled)")) t t)
592
    (insert (propertize
Paul Eggert's avatar
Paul Eggert committed
593
             (concat
594 595 596 597
              (or title title "Calc Edit Mode. ")
              "Press `C-c C-c'"
              (if allow-ret "" " or RET")
              " to finish, `C-x k RET' to cancel.\n\n")
Jay Belanger's avatar
Jay Belanger committed
598 599 600
             'font-lock-face 'italic 'read-only t 'rear-nonsticky t 'front-sticky t))
    (make-local-variable 'calc-edit-top)
    (setq calc-edit-top (point))))
Eli Zaretskii's avatar
Eli Zaretskii committed
601 602 603 604 605 606 607 608 609 610
(put 'calc-edit-mode 'mode-class 'special)

(defun calc-show-edit-buffer ()
  (let ((buf (current-buffer)))
    (if (and (one-window-p t) pop-up-windows)
	(pop-to-buffer (get-buffer-create "*Calc Edit*"))
      (and calc-embedded-info (get-buffer-window (aref calc-embedded-info 1))
	   (select-window (get-buffer-window (aref calc-embedded-info 1))))
      (switch-to-buffer (get-buffer-create "*Calc Edit*")))
    (setq calc-return-buffer buf)
611
    (if (and (< (window-width) (frame-width))
Eli Zaretskii's avatar
Eli Zaretskii committed
612 613 614 615 616
	     calc-display-trail)
	(let ((win (get-buffer-window (calc-trail-buffer))))
	  (if win
	      (delete-window win))))
    (set-buffer-modified-p nil)
Jay Belanger's avatar
Jay Belanger committed
617
    (goto-char calc-edit-top)))
Eli Zaretskii's avatar
Eli Zaretskii committed
618 619 620 621 622

(defun calc-edit-return ()
  (interactive)
  (if (and (boundp 'calc-allow-ret) calc-allow-ret)
      (newline)
623
    (calc-edit-finish)))
Eli Zaretskii's avatar
Eli Zaretskii committed
624

625 626 627 628
;; The variable calc-edit-disp-trail is local to calc-edit finish, but
;; is used by calc-finish-selection-edit and calc-finish-stack-edit.
(defvar calc-edit-disp-trail)

Eli Zaretskii's avatar
Eli Zaretskii committed
629 630 631 632 633 634 635 636 637 638
(defun calc-edit-finish (&optional keep)
  "Finish calc-edit mode.  Parse buffer contents and push them on the stack."
  (interactive "P")
  (message "Working...")
  (or (and (boundp 'calc-original-buffer)
	   (boundp 'calc-return-buffer)
	   (boundp 'calc-one-window)
	   (boundp 'calc-edit-handler)
	   (boundp 'calc-restore-trail)
	   (eq major-mode 'calc-edit-mode))
639
      (error "This command is valid only in buffers created by calc-edit"))
Eli Zaretskii's avatar
Eli Zaretskii committed
640 641 642 643
  (let ((buf (current-buffer))
	(original calc-original-buffer)
	(return calc-return-buffer)
	(one-window calc-one-window)
644
	(calc-edit-disp-trail calc-restore-trail))
Eli Zaretskii's avatar
Eli Zaretskii committed
645
    (save-excursion
646 647 648 649 650
      (when (or (null (buffer-name original))
		(progn
		  (set-buffer original)
		  (not (eq major-mode 'calc-mode))))
	(error "Original calculator buffer has been corrupted")))
Jay Belanger's avatar
Jay Belanger committed
651
    (goto-char calc-edit-top)
Eli Zaretskii's avatar
Eli Zaretskii committed
652 653
    (if (buffer-modified-p)
	(eval calc-edit-handler))
654
    (if (and one-window (not (one-window-p t)))
Eli Zaretskii's avatar
Eli Zaretskii committed
655 656 657 658 659 660 661
	(delete-window))
    (if (get-buffer-window return)
	(select-window (get-buffer-window return))
      (switch-to-buffer return))
    (if keep
	(bury-buffer buf)
      (kill-buffer buf))
662
    (if calc-edit-disp-trail
Eli Zaretskii's avatar
Eli Zaretskii committed
663 664
	(calc-wrapper
	 (calc-trail-display 1 t)))
665
    (message "")))
Eli Zaretskii's avatar
Eli Zaretskii committed
666 667 668 669 670 671

(defun calc-edit-cancel ()
  "Cancel calc-edit mode.  Ignore the Calc Edit buffer and don't change stack."
  (interactive)
  (let ((calc-edit-handler nil))
    (calc-edit-finish))
Paul Eggert's avatar
Paul Eggert committed
672
  (message "(Canceled)"))
Eli Zaretskii's avatar
Eli Zaretskii committed
673 674 675

(defun calc-finish-stack-edit (num)
  (let ((buf (current-buffer))
Jay Belanger's avatar
Jay Belanger committed
676
	(str (buffer-substring calc-edit-top (point-max)))
Eli Zaretskii's avatar
Eli Zaretskii committed
677 678 679 680 681 682 683
	(start (point))
	pos)
    (if (and (integerp num) (> num 1))
	(while (setq pos (string-match "\n." str))
	  (aset str pos ?\,)))
    (switch-to-buffer calc-original-buffer)
    (let ((vals (let ((calc-language nil)
684
		      (math-expr-opers (math-standard-ops)))
Eli Zaretskii's avatar
Eli Zaretskii committed
685 686
		  (and (string-match "[^\n\t ]" str)
		       (math-read-exprs str)))))
687 688 689 690
      (when (eq (car-safe vals) 'error)
	(switch-to-buffer buf)
	(goto-char (+ start (nth 1 vals)))
	(error (nth 2 vals)))
Eli Zaretskii's avatar
Eli Zaretskii committed
691 692 693 694 695
      (calc-wrapper
       (if (symbolp num)
	   (progn
	     (set num (car vals))
	     (calc-refresh-evaltos num))
696
	 (if calc-edit-disp-trail
Eli Zaretskii's avatar
Eli Zaretskii committed
697 698
	     (calc-trail-display 1 t))
	 (and vals
699
	      (let ((calc-simplify-mode (if (eq last-command-event ?\C-j)
Eli Zaretskii's avatar
Eli Zaretskii committed
700 701 702 703
					    'none
					  calc-simplify-mode)))
		(if (>= num 0)
		    (calc-enter-result num "edit" vals)
704
		  (calc-enter-result 1 "edit" vals (- num))))))))))
Eli Zaretskii's avatar
Eli Zaretskii committed
705

Jay Belanger's avatar
Jay Belanger committed
706 707
(provide 'calc-yank)

708 709 710 711
;; Local variables:
;; generated-autoload-file: "calc-loaddefs.el"
;; End:

712
;;; calc-yank.el ends here