Commit | Line | Data |
---|---|---|
3132f345 CW |
1 | ;;; calc-undo.el --- undo functions for Calc |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 1990-1993, 2001-2011 Free Software Foundation, Inc. |
3132f345 CW |
4 | |
5 | ;; Author: David Gillespie <daveg@synaptics.com> | |
e8fff8ed | 6 | ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com> |
136211a9 EZ |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
662c9c64 | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
7c671b23 | 11 | ;; it under the terms of the GNU General Public License as published by |
662c9c64 GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
7c671b23 | 14 | |
136211a9 | 15 | ;; GNU Emacs is distributed in the hope that it will be useful, |
7c671b23 GM |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
662c9c64 | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
136211a9 | 22 | |
3132f345 | 23 | ;;; Commentary: |
136211a9 | 24 | |
3132f345 | 25 | ;;; Code: |
136211a9 EZ |
26 | |
27 | ;; This file is autoloaded from calc-ext.el. | |
136211a9 | 28 | |
2c2198cd | 29 | (require 'calc-ext) |
136211a9 EZ |
30 | (require 'calc-macs) |
31 | ||
136211a9 EZ |
32 | ;;; Undo. |
33 | ||
c310ff4f | 34 | ;;;###autoload |
136211a9 EZ |
35 | (defun calc-undo (n) |
36 | (interactive "p") | |
3132f345 CW |
37 | (when calc-executing-macro |
38 | (error "Use C-x e, not X, to run a keyboard macro that uses Undo")) | |
136211a9 EZ |
39 | (if (<= n 0) |
40 | (if (< n 0) | |
41 | (calc-redo (- n)) | |
42 | (calc-last-args 1)) | |
43 | (calc-wrapper | |
3132f345 CW |
44 | (when (null (nthcdr (1- n) calc-undo-list)) |
45 | (error "No further undo information available")) | |
136211a9 EZ |
46 | (setq calc-undo-list |
47 | (prog1 | |
48 | (nthcdr n calc-undo-list) | |
49 | (let ((saved-stack-top calc-stack-top)) | |
50 | (let ((calc-stack-top 0)) | |
51 | (calc-handle-undos calc-undo-list n)) | |
52 | (setq calc-stack-top saved-stack-top)))) | |
bf77c646 | 53 | (message "Undo!")))) |
136211a9 EZ |
54 | |
55 | (defun calc-handle-undos (cl n) | |
3132f345 CW |
56 | (when (> n 0) |
57 | (let ((old-redo calc-redo-list)) | |
58 | (setq calc-undo-list nil) | |
59 | (calc-handle-undo (car cl)) | |
60 | (setq calc-redo-list (append calc-undo-list old-redo))) | |
61 | (calc-handle-undos (cdr cl) (1- n)))) | |
136211a9 EZ |
62 | |
63 | (defun calc-handle-undo (list) | |
3132f345 | 64 | (when list |
136211a9 EZ |
65 | (let ((action (car list))) |
66 | (cond | |
67 | ((eq (car action) 'push) | |
68 | (calc-pop-stack 1 (nth 1 action) t)) | |
69 | ((eq (car action) 'pop) | |
70 | (calc-push-list (nth 2 action) (nth 1 action))) | |
71 | ((eq (car action) 'set) | |
72 | (calc-record-undo (list 'set (nth 1 action) | |
73 | (symbol-value (nth 1 action)))) | |
74 | (set (nth 1 action) (nth 2 action))) | |
75 | ((eq (car action) 'store) | |
76 | (let ((v (intern (nth 1 action)))) | |
77 | (calc-record-undo (list 'store (nth 1 action) | |
78 | (and (boundp v) (symbol-value v)))) | |
6735a29b JB |
79 | (if (y-or-n-p (format "Un-store variable %s? " |
80 | (calc-var-name (nth 1 action)))) | |
136211a9 EZ |
81 | (progn |
82 | (if (nth 2 action) | |
83 | (set v (nth 2 action)) | |
84 | (makunbound v)) | |
85 | (calc-refresh-evaltos v))))) | |
86 | ((eq (car action) 'eval) | |
87 | (calc-record-undo (append (list 'eval (nth 2 action) (nth 1 action)) | |
88 | (cdr (cdr (cdr action))))) | |
89 | (apply (nth 1 action) (cdr (cdr (cdr action)))))) | |
bf77c646 | 90 | (calc-handle-undo (cdr list))))) |
136211a9 EZ |
91 | |
92 | (defun calc-redo (n) | |
93 | (interactive "p") | |
3132f345 CW |
94 | (when calc-executing-macro |
95 | (error "Use C-x e, not X, to run a keyboard macro that uses Redo")) | |
136211a9 EZ |
96 | (if (<= n 0) |
97 | (calc-undo (- n)) | |
98 | (calc-wrapper | |
3132f345 CW |
99 | (when (null (nthcdr (1- n) calc-redo-list)) |
100 | (error "Unable to redo")) | |
136211a9 EZ |
101 | (setq calc-redo-list |
102 | (prog1 | |
103 | (nthcdr n calc-redo-list) | |
104 | (let ((saved-stack-top calc-stack-top)) | |
105 | (let ((calc-stack-top 0)) | |
106 | (calc-handle-redos calc-redo-list n)) | |
107 | (setq calc-stack-top saved-stack-top)))) | |
bf77c646 | 108 | (message "Redo!")))) |
136211a9 EZ |
109 | |
110 | (defun calc-handle-redos (cl n) | |
3132f345 CW |
111 | (when (> n 0) |
112 | (let ((old-undo calc-undo-list)) | |
113 | (setq calc-undo-list nil) | |
114 | (calc-handle-undo (car cl)) | |
115 | (setq calc-undo-list (append calc-undo-list old-undo))) | |
116 | (calc-handle-redos (cdr cl) (1- n)))) | |
136211a9 EZ |
117 | |
118 | (defun calc-last-args (n) | |
119 | (interactive "p") | |
3132f345 CW |
120 | (when calc-executing-macro |
121 | (error "Use C-x e, not X, to run a keyboard macro that uses last-args")) | |
136211a9 EZ |
122 | (calc-wrapper |
123 | (let ((urec (calc-find-last-x calc-undo-list n))) | |
124 | (if urec | |
125 | (calc-handle-last-x urec) | |
bf77c646 | 126 | (error "Not enough undo information available"))))) |
136211a9 EZ |
127 | |
128 | (defun calc-handle-last-x (list) | |
3132f345 CW |
129 | (when list |
130 | (let ((action (car list))) | |
131 | (if (eq (car action) 'pop) | |
132 | (calc-pop-push-record-list 0 "larg" | |
133 | (delq 'top-of-stack (nth 2 action)))) | |
134 | (calc-handle-last-x (cdr list))))) | |
136211a9 EZ |
135 | |
136 | (defun calc-find-last-x (ul n) | |
3132f345 CW |
137 | (when ul |
138 | (if (calc-undo-does-pushes (car ul)) | |
139 | (if (<= n 1) | |
140 | (car ul) | |
141 | (calc-find-last-x (cdr ul) (1- n))) | |
142 | (calc-find-last-x (cdr ul) n)))) | |
136211a9 EZ |
143 | |
144 | (defun calc-undo-does-pushes (list) | |
145 | (and list | |
146 | (or (eq (car (car list)) 'pop) | |
bf77c646 | 147 | (calc-undo-does-pushes (cdr list))))) |
136211a9 | 148 | |
2c2198cd JB |
149 | (provide 'calc-undo) |
150 | ||
bf77c646 | 151 | ;;; calc-undo.el ends here |