Mercurial > emacs
annotate lisp/calc/calc-yank.el @ 54280:bfa320f698d6
(cvs-fileinfo->backup-file): Use a more constraining
regexp to distinguish .#ChangeLog.9.1.400 and .#ChangeLog.1.400.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Thu, 04 Mar 2004 22:44:23 +0000 |
parents | 695cf19ef79e |
children | 4e7e96bc6b95 375f2633d815 |
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> |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49263
diff
changeset
|
6 ;; Maintainers: D. Goel <deego@gnufans.org> |
49263
f4d68f97221e
Add new maintainer (deego).
Deepak Goel <deego@gnufans.org>
parents:
41271
diff
changeset
|
7 ;; Colin Walters <walters@debian.org> |
40785 | 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 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
26 ;;; Commentary: |
40785 | 27 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
28 ;;; Code: |
40785 | 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))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
64 (kill-line nn))) |
40785 | 65 |
66 (defun calc-force-refresh () | |
67 (if (or calc-executing-macro calc-display-dirty) | |
68 (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
|
69 (calc-refresh)))) |
40785 | 70 |
71 (defun calc-locate-cursor-element (pt) | |
72 (save-excursion | |
73 (goto-char (point-max)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
74 (calc-locate-cursor-scan (- calc-stack-top) calc-stack pt))) |
40785 | 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)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
81 (calc-locate-cursor-scan (1+ n) (cdr stack) pt))) |
40785 | 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) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
99 (kill-region top bot)))) |
40785 | 100 |
101 (defun calc-copy-as-kill (n) | |
102 (interactive "P") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
103 (calc-kill n t)) |
40785 | 104 |
105 (defun calc-copy-region-as-kill (top bot) | |
106 (interactive "r") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
107 (calc-kill-region top bot t)) |
40785 | 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)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
130 val)))))))) |
40785 | 131 |
132 (defun calc-clean-newlines (s) | |
133 (cond | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49263
diff
changeset
|
134 |
40785 | 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)))) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49263
diff
changeset
|
144 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
145 (t s))) |
40785 | 146 |
147 | |
148 (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
|
149 (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
|
150 (error "This command works only in a regular text buffer")) |
40785 | 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 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
191 (calc-enter-result 0 "grab" vals)))) |
40785 | 192 |
193 | |
194 (defun calc-do-grab-rectangle (top bot arg &optional reduce) | |
195 (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
|
196 (error "This command works only in a regular text buffer")) |
40785 | 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 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
204 (unless (= col1 0) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
205 (error "Point and mark must be at beginning of line, or define a rectangle")) |
40785 | 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) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
216 (when arg |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
217 (setq arg (if (consp arg) 0 (prefix-numeric-value arg)))) |
40785 | 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))) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
252 (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
|
253 (setq vals (list 'vec v2))))))))) |
40785 | 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)))) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
264 (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
|
265 (setq mat (cons vals mat))) |
40785 | 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))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
272 (calc-enter-result 0 "grab" (nreverse mat)))))) |
40785 | 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)))) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
343 (when thebuf |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
344 (setq movept (point))) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
345 (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
|
346 (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
|
347 (point))))))) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
348 (when movept |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
349 (goto-char movept)) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
350 (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
|
351 (not thebuf)) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
352 (calc-quit t) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
353 (switch-to-buffer newbuf)))) |
40785 | 354 |
355 (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
|
356 (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
|
357 (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
|
358 (when eat-lnums |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
359 (setq str (substring str 4))) |
40785 | 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)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
377 (setq i (1+ i)))))) |
40785 | 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) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
389 (car buf)))) |
40785 | 390 |
391 | |
392 (defun calc-edit (n) | |
393 (interactive "p") | |
394 (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
|
395 (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
|
396 (setq n (calc-stack-size))) |
40785 | 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)) | |
40998
ee9c2872370b
Use `frame-width' instead of `screen-width',
Eli Zaretskii <eliz@gnu.org>
parents:
40785
diff
changeset
|
406 (math-format-nice-expr x (frame-width))))) |
40785 | 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))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
414 (calc-show-edit-buffer)) |
40785 | 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) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
421 (calc-set-command-flag 'do-edit)) |
40785 | 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) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
429 (define-key calc-edit-mode-map "\C-c\C-c" 'calc-edit-finish)) |
40785 | 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) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
435 (unless handler |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
436 (error "This command can be used only indirectly through calc-edit")) |
40785 | 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") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
469 " to cancel.\n"))) |
40785 | 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) | |
40998
ee9c2872370b
Use `frame-width' instead of `screen-width',
Eli Zaretskii <eliz@gnu.org>
parents:
40785
diff
changeset
|
480 (if (and (< (window-width) (frame-width)) |
40785 | 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)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
487 (forward-line 1))) |
40785 | 488 |
489 (defun calc-edit-return () | |
490 (interactive) | |
491 (if (and (boundp 'calc-allow-ret) calc-allow-ret) | |
492 (newline) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
493 (calc-edit-finish))) |
40785 | 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)) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
505 (error "This command is valid only in buffers created by calc-edit")) |
40785 | 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 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
512 (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
|
513 (progn |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
514 (set-buffer original) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
515 (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
|
516 (error "Original calculator buffer has been corrupted"))) |
40785 | 517 (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
|
518 (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
|
519 (forward-line 1)) |
40785 | 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))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
533 (message ""))) |
40785 | 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)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
540 (message "(Cancelled)")) |
40785 | 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))))) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
555 (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
|
556 (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
|
557 (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
|
558 (error (nth 2 vals))) |
40785 | 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) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
572 (calc-enter-result 1 "edit" vals (- num)))))))))) |
40785 | 573 |
52401 | 574 ;;; arch-tag: ca61019e-caca-4daa-b32c-b6afe372d5b5 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40998
diff
changeset
|
575 ;;; calc-yank.el ends here |