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