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