Commit | Line | Data |
---|---|---|
7ed9159a JY |
1 | ;;;; ses.el -- Simple Emacs Spreadsheet |
2 | ||
3 | ;; Copyright (C) 2002 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Jonathan Yavner <jyavner@engineer.com> | |
6 | ;; Maintainer: Jonathan Yavner <jyavner@engineer.com> | |
7 | ;; Keywords: spreadsheet | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
25 | ||
26 | ;;; To-do list: | |
27 | ;; * Do something about control characters & octal codes in cell print | |
28 | ;; areas. Currently they distort the columnar appearance, but fixing them | |
29 | ;; seems like too much work? Use text-char-description? | |
30 | ;; * Input validation functions. How specified? | |
31 | ;; * Menubar and popup menus. | |
32 | ;; * Faces (colors & styles) in print cells. | |
33 | ;; * Move a column by dragging its letter in the header line. | |
34 | ;; * Left-margin column for row number. | |
35 | ;; * Move a row by dragging its number in the left-margin. | |
36 | ||
37 | (require 'unsafep) | |
38 | ||
39 | ||
40 | ;;;---------------------------------------------------------------------------- | |
41 | ;;;; User-customizable variables | |
42 | ;;;---------------------------------------------------------------------------- | |
43 | ||
44 | (defgroup ses nil | |
45 | "Simple Emacs Spreadsheet" | |
46 | :group 'applications | |
47 | :prefix "ses-" | |
48 | :version "21.1") | |
49 | ||
50 | (defcustom ses-initial-size '(1 . 1) | |
51 | "Initial size of a new spreadsheet, as a cons (NUMROWS . NUMCOLS)." | |
52 | :group 'ses | |
53 | :type '(cons (integer :tag "numrows") (integer :tag "numcols"))) | |
54 | ||
55 | (defcustom ses-initial-column-width 7 | |
56 | "Initial width of columns in a new spreadsheet." | |
57 | :group 'ses | |
58 | :type '(integer :match (lambda (widget value) (> value 0)))) | |
59 | ||
60 | (defcustom ses-initial-default-printer "%.7g" | |
61 | "Initial default printer for a new spreadsheet." | |
62 | :group 'ses | |
63 | :type '(choice string | |
64 | (list :tag "Parenthesized string" string) | |
65 | function)) | |
66 | ||
67 | (defcustom ses-after-entry-functions '(forward-char) | |
68 | "Things to do after entering a value into a cell. An abnormal hook that | |
69 | usually runs a cursor-movement function. Each function is called with ARG=1." | |
70 | :group 'ses | |
71 | :type 'hook | |
72 | :options '(forward-char backward-char next-line previous-line)) | |
73 | ||
74 | (defcustom ses-mode-hook nil | |
75 | "Hook functions to be run upon entering SES mode." | |
76 | :group 'ses | |
77 | :type 'hook) | |
78 | ||
79 | ||
80 | ;;;---------------------------------------------------------------------------- | |
81 | ;;;; Global variables and constants | |
82 | ;;;---------------------------------------------------------------------------- | |
83 | ||
84 | (defvar ses-read-cell-history nil | |
85 | "List of formulas that have been typed in.") | |
86 | ||
87 | (defvar ses-read-printer-history nil | |
88 | "List of printer functions that have been typed in.") | |
89 | ||
90 | (defvar ses-mode-map nil | |
91 | "Local keymap for Simple Emacs Spreadsheet.") | |
92 | ||
93 | (defvar ses-mode-print-map nil | |
94 | "Local keymap for SES print area.") | |
95 | ||
96 | (defvar ses-mode-edit-map nil | |
97 | "Local keymap for SES minibuffer cell-editing.") | |
98 | ||
99 | ;Key map used for 'x' key. | |
100 | (defalias 'ses-export-keymap | |
101 | (let ((map (make-sparse-keymap "SES export"))) | |
102 | (define-key map "T" (cons " tab-formulas" 'ses-export-tsf)) | |
103 | (define-key map "t" (cons " tab-values" 'ses-export-tsv)) | |
104 | map)) | |
105 | ||
106 | (defconst ses-print-data-boundary "\n\014\n" | |
107 | "Marker string denoting the boundary between print area and data area") | |
108 | ||
109 | (defconst ses-initial-global-parameters | |
110 | "\n( ;Global parameters (these are read first)\n 2 ;SES file-format\n 1 ;numrows\n 1 ;numcols\n)\n\n" | |
111 | "Initial contents for the three-element list at the bottom of the data area") | |
112 | ||
113 | (defconst ses-initial-file-trailer | |
114 | ";;; Local Variables:\n;;; mode: ses\n;;; End:\n" | |
115 | "Initial contents for the file-trailer area at the bottom of the file.") | |
116 | ||
117 | (defconst ses-initial-file-contents | |
118 | (concat " \n" ;One blank cell in print area | |
119 | ses-print-data-boundary | |
120 | "(ses-cell A1 nil nil nil nil)\n" ;One blank cell in data area | |
121 | "\n" ;End-of-row terminator for the one row in data area | |
122 | "(ses-column-widths [7])\n" | |
123 | "(ses-column-printers [nil])\n" | |
124 | "(ses-default-printer \"%.7g\")\n" | |
125 | "(ses-header-row 0)\n" | |
126 | ses-initial-global-parameters | |
127 | ses-initial-file-trailer) | |
128 | "The initial contents of an empty spreadsheet.") | |
129 | ||
130 | (defconst ses-cell-size 4 | |
131 | "A cell consists of a SYMBOL, a FORMULA, a PRINTER-function, and a list of | |
132 | REFERENCES.") | |
133 | ||
134 | (defconst ses-paramlines-plist | |
135 | '(column-widths 2 col-printers 3 default-printer 4 header-row 5 | |
136 | file-format 8 numrows 9 numcols 10) | |
137 | "Offsets from last cell line to various parameter lines in the data area | |
138 | of a spreadsheet.") | |
139 | ||
140 | (defconst ses-box-prop '(:box (:line-width 2 :style released-button)) | |
141 | "Display properties to create a raised box for cells in the header line.") | |
142 | ||
143 | (defconst ses-standard-printer-functions | |
144 | '(ses-center ses-center-span ses-dashfill ses-dashfill-span | |
145 | ses-tildefill-span) | |
146 | "List of print functions to be included in initial history of printer | |
147 | functions. None of these standard-printer functions is suitable for use as a | |
148 | column printer or a global-default printer because they invoke the column or | |
149 | default printer and then modify its output.") | |
150 | ||
151 | (eval-and-compile | |
152 | (defconst ses-localvars | |
153 | '(blank-line cells col-printers column-widths curcell curcell-overlay | |
154 | default-printer deferred-narrow deferred-recalc deferred-write | |
155 | file-format header-hscroll header-row header-string linewidth | |
156 | mode-line-process next-line-add-newlines numcols numrows | |
157 | symbolic-formulas transient-mark-mode) | |
158 | "Buffer-local variables used by SES.")) | |
159 | ||
160 | ;;When compiling, create all the buffer locals and give them values | |
161 | (eval-when-compile | |
162 | (dolist (x ses-localvars) | |
163 | (make-local-variable x) | |
164 | (set x nil))) | |
165 | ||
166 | ||
167 | ;;; | |
168 | ;;; "Side-effect variables". They are set in one function, altered in | |
169 | ;;; another as a side effect, then read back by the first, as a way of | |
170 | ;;; passing back more than one value. These declarations are just to make | |
171 | ;;; the compiler happy, and to conform to standard Emacs-Lisp practice (I | |
172 | ;;; think the make-local-variable trick above is cleaner). | |
173 | ;;; | |
174 | ||
175 | (defvar ses-relocate-return nil | |
176 | "Set by `ses-relocate-formula' and `ses-relocate-range', read by | |
177 | `ses-relocate-all'. Set to 'delete if a cell-reference was deleted from a | |
178 | formula--so the formula needs recalculation. Set to 'range if the size of a | |
179 | `ses-range' was changed--so both the formula's value and list of dependents | |
180 | need to be recalculated.") | |
181 | ||
182 | (defvar ses-call-printer-return nil | |
183 | "Set to t if last cell printer invoked by `ses-call-printer' requested | |
184 | left-justification of the result. Set to error-signal if ses-call-printer | |
185 | encountered an error during printing. Nil otherwise.") | |
186 | ||
187 | (defvar ses-start-time nil | |
188 | "Time when current operation started. Used by `ses-time-check' to decide | |
189 | when to emit a progress message.") | |
190 | ||
191 | ||
192 | ;;;---------------------------------------------------------------------------- | |
193 | ;;;; Macros | |
194 | ;;;---------------------------------------------------------------------------- | |
195 | ||
196 | (defmacro ses-get-cell (row col) | |
197 | "Return the cell structure that stores information about cell (ROW,COL)." | |
198 | `(aref (aref cells ,row) ,col)) | |
199 | ||
200 | (defmacro ses-cell-symbol (row &optional col) | |
201 | "From a CELL or a pair (ROW,COL), get the symbol that names the local-variable holding its value. (0,0) => A1." | |
202 | `(aref ,(if col `(ses-get-cell ,row ,col) row) 0)) | |
203 | ||
204 | (defmacro ses-cell-formula (row &optional col) | |
205 | "From a CELL or a pair (ROW,COL), get the function that computes its value." | |
206 | `(aref ,(if col `(ses-get-cell ,row ,col) row) 1)) | |
207 | ||
208 | (defmacro ses-cell-printer (row &optional col) | |
209 | "From a CELL or a pair (ROW,COL), get the function that prints its value." | |
210 | `(aref ,(if col `(ses-get-cell ,row ,col) row) 2)) | |
211 | ||
212 | (defmacro ses-cell-references (row &optional col) | |
213 | "From a CELL or a pair (ROW,COL), get the list of symbols for cells whose | |
214 | functions refer to its value." | |
215 | `(aref ,(if col `(ses-get-cell ,row ,col) row) 3)) | |
216 | ||
217 | (defmacro ses-cell-value (row &optional col) | |
218 | "From a CELL or a pair (ROW,COL), get the current value for that cell." | |
219 | `(symbol-value (ses-cell-symbol ,row ,col))) | |
220 | ||
221 | (defmacro ses-col-width (col) | |
222 | "Return the width for column COL." | |
223 | `(aref column-widths ,col)) | |
224 | ||
225 | (defmacro ses-col-printer (col) | |
226 | "Return the default printer for column COL." | |
227 | `(aref col-printers ,col)) | |
228 | ||
229 | (defmacro ses-sym-rowcol (sym) | |
230 | "From a cell-symbol SYM, gets the cons (row . col). A1 => (0 . 0). Result | |
231 | is nil if SYM is not a symbol that names a cell." | |
232 | `(and (symbolp ,sym) (get ,sym 'ses-cell))) | |
233 | ||
234 | (defmacro ses-cell (sym value formula printer references) | |
235 | "Load a cell SYM from the spreadsheet file. Does not recompute VALUE from | |
236 | FORMULA, does not reprint using PRINTER, does not check REFERENCES. This is a | |
237 | macro to prevent propagate-on-load viruses. Safety-checking for FORMULA and | |
238 | PRINTER are deferred until first use." | |
239 | (let ((rowcol (ses-sym-rowcol sym))) | |
240 | (ses-formula-record formula) | |
241 | (ses-printer-record printer) | |
242 | (or (atom formula) | |
243 | (eq safe-functions t) | |
244 | (setq formula `(ses-safe-formula ,formula))) | |
245 | (or (not printer) | |
246 | (stringp printer) | |
247 | (eq safe-functions t) | |
248 | (setq printer `(ses-safe-printer ,printer))) | |
249 | (aset (aref cells (car rowcol)) | |
250 | (cdr rowcol) | |
251 | (vector sym formula printer references))) | |
252 | (set sym value) | |
253 | sym) | |
254 | ||
255 | (defmacro ses-column-widths (widths) | |
256 | "Load the vector of column widths from the spreadsheet file. This is a | |
257 | macro to prevent propagate-on-load viruses." | |
258 | (or (and (vectorp widths) (= (length widths) numcols)) | |
259 | (error "Bad column-width vector")) | |
260 | ;;To save time later, we also calculate the total width of each line in the | |
261 | ;;print area (excluding the terminating newline) | |
262 | (setq column-widths widths | |
263 | linewidth (apply '+ -1 (mapcar '1+ widths)) | |
264 | blank-line (concat (make-string linewidth ? ) "\n")) | |
265 | t) | |
266 | ||
267 | (defmacro ses-column-printers (printers) | |
268 | "Load the vector of column printers from the spreadsheet file and checks | |
269 | them for safety. This is a macro to prevent propagate-on-load viruses." | |
270 | (or (and (vectorp printers) (= (length printers) numcols)) | |
271 | (error "Bad column-printers vector")) | |
272 | (dotimes (x numcols) | |
273 | (aset printers x (ses-safe-printer (aref printers x)))) | |
274 | (setq col-printers printers) | |
275 | (mapc 'ses-printer-record printers) | |
276 | t) | |
277 | ||
278 | (defmacro ses-default-printer (def) | |
279 | "Load the global default printer from the spreadsheet file and checks it | |
280 | for safety. This is a macro to prevent propagate-on-load viruses." | |
281 | (setq default-printer (ses-safe-printer def)) | |
282 | (ses-printer-record def) | |
283 | t) | |
284 | ||
285 | (defmacro ses-header-row (row) | |
286 | "Load the header row from the spreadsheet file and checks it | |
287 | for safety. This is a macro to prevent propagate-on-load viruses." | |
288 | (or (and (wholenump row) (< row numrows)) | |
289 | (error "Bad header-row")) | |
290 | (setq header-row row) | |
291 | t) | |
292 | ||
293 | (defmacro ses-dotimes-msg (spec msg &rest body) | |
294 | "(ses-dotimes-msg (VAR LIMIT) MSG BODY...): Like `dotimes', but | |
295 | a message is emitted using MSG every second or so during the loop." | |
296 | (let ((msgvar (make-symbol "msg")) | |
297 | (limitvar (make-symbol "limit")) | |
298 | (var (car spec)) | |
299 | (limit (cadr spec))) | |
300 | `(let ((,limitvar ,limit) | |
301 | (,msgvar ,msg)) | |
302 | (setq ses-start-time (float-time)) | |
303 | (message ,msgvar) | |
304 | (setq ,msgvar (concat ,msgvar " (%d%%)")) | |
305 | (dotimes (,var ,limitvar) | |
306 | (ses-time-check ,msgvar '(/ (* ,var 100) ,limitvar)) | |
307 | ,@body) | |
308 | (message nil)))) | |
309 | ||
310 | (put 'ses-dotimes-msg 'lisp-indent-function 2) | |
311 | (def-edebug-spec ses-dotimes-msg ((symbolp form) form body)) | |
312 | ||
313 | (defmacro ses-dorange (curcell &rest body) | |
314 | "Execute BODY repeatedly, with the variables `row' and `col' set to each | |
315 | cell in the range specified by CURCELL. The range is available in the | |
316 | variables `minrow', `maxrow', `mincol', and `maxcol'." | |
317 | (let ((cur (make-symbol "cur")) | |
318 | (min (make-symbol "min")) | |
319 | (max (make-symbol "max")) | |
320 | (r (make-symbol "r")) | |
321 | (c (make-symbol "c"))) | |
322 | `(let* ((,cur ,curcell) | |
323 | (,min (ses-sym-rowcol (if (consp ,cur) (car ,cur) ,cur))) | |
324 | (,max (ses-sym-rowcol (if (consp ,cur) (cdr ,cur) ,cur)))) | |
325 | (let ((minrow (car ,min)) | |
326 | (maxrow (car ,max)) | |
327 | (mincol (cdr ,min)) | |
328 | (maxcol (cdr ,max)) | |
329 | row col) | |
330 | (if (or (> minrow maxrow) (> mincol maxcol)) | |
331 | (error "Empty range")) | |
332 | (dotimes (,r (- maxrow minrow -1)) | |
333 | (setq row (+ ,r minrow)) | |
334 | (dotimes (,c (- maxcol mincol -1)) | |
335 | (setq col (+ ,c mincol)) | |
336 | ,@body)))))) | |
337 | ||
338 | (put 'ses-dorange 'lisp-indent-function 'defun) | |
339 | (def-edebug-spec ses-dorange (form body)) | |
340 | ||
341 | ;;Support for coverage testing. | |
342 | (defmacro 1value (form) | |
343 | "For code-coverage testing, indicate that FORM is expected to always have | |
344 | the same value." | |
345 | form) | |
346 | (defmacro noreturn (form) | |
347 | "For code-coverage testing, indicate that FORM will always signal an error." | |
348 | form) | |
349 | ||
350 | ||
351 | ;;;---------------------------------------------------------------------------- | |
352 | ;;;; Utility functions | |
353 | ;;;---------------------------------------------------------------------------- | |
354 | ||
355 | (defun ses-vector-insert (array idx new) | |
356 | "Create a new vector which is one larger than ARRAY and has NEW inserted | |
357 | before element IDX." | |
358 | (let* ((len (length array)) | |
359 | (result (make-vector (1+ len) new))) | |
360 | (dotimes (x len) | |
361 | (aset result | |
362 | (if (< x idx) x (1+ x)) | |
363 | (aref array x))) | |
364 | result)) | |
365 | ||
366 | ;;Allow ARRAY to be a symbol for use in buffer-undo-list | |
367 | (defun ses-vector-delete (array idx count) | |
368 | "Create a new vector which is a copy of ARRAY with COUNT objects removed | |
369 | starting at element IDX. ARRAY is either a vector or a symbol whose value | |
370 | is a vector--if a symbol, the new vector is assigned as the symbol's value." | |
371 | (let* ((a (if (arrayp array) array (symbol-value array))) | |
372 | (len (- (length a) count)) | |
373 | (result (make-vector len nil))) | |
374 | (dotimes (x len) | |
375 | (aset result x (aref a (if (< x idx) x (+ x count))))) | |
376 | (if (symbolp array) | |
377 | (set array result)) | |
378 | result)) | |
379 | ||
380 | (defun ses-delete-line (count) | |
381 | "Like `kill-line', but no kill ring." | |
382 | (let ((pos (point))) | |
383 | (forward-line count) | |
384 | (delete-region pos (point)))) | |
385 | ||
386 | (defun ses-printer-validate (printer) | |
387 | "Signals an error if PRINTER is not a valid SES cell printer." | |
388 | (or (not printer) | |
389 | (stringp printer) | |
390 | (functionp printer) | |
391 | (and (stringp (car-safe printer)) (not (cdr printer))) | |
392 | (error "Invalid printer function")) | |
393 | printer) | |
394 | ||
395 | (defun ses-printer-record (printer) | |
396 | "Add PRINTER to `ses-read-printer-history' if not already there, after first | |
397 | checking that it is a valid printer function." | |
398 | (ses-printer-validate printer) | |
399 | ;;To speed things up, we avoid calling prin1 for the very common "nil" case. | |
400 | (if printer | |
401 | (add-to-list 'ses-read-printer-history (prin1-to-string printer)))) | |
402 | ||
403 | (defun ses-formula-record (formula) | |
404 | "If FORMULA is of the form 'symbol, adds it to the list of symbolic formulas | |
405 | for this spreadsheet." | |
406 | (when (and (eq (car-safe formula) 'quote) | |
407 | (symbolp (cadr formula))) | |
408 | (add-to-list 'symbolic-formulas | |
409 | (list (symbol-name (cadr formula)))))) | |
410 | ||
411 | (defun ses-column-letter (col) | |
412 | "Converts a column number to A..Z or AA..ZZ" | |
413 | (if (< col 26) | |
414 | (char-to-string (+ ?A col)) | |
415 | (string (+ ?@ (/ col 26)) (+ ?A (% col 26))))) | |
416 | ||
417 | (defun ses-create-cell-symbol (row col) | |
418 | "Produce a symbol that names the cell (ROW,COL). (0,0) => 'A1." | |
419 | (intern (concat (ses-column-letter col) (number-to-string (1+ row))))) | |
420 | ||
421 | (defun ses-create-cell-variable-range (minrow maxrow mincol maxcol) | |
422 | "Create buffer-local variables for cells. This is undoable." | |
423 | (push `(ses-destroy-cell-variable-range ,minrow ,maxrow ,mincol ,maxcol) | |
424 | buffer-undo-list) | |
425 | (let (sym xrow xcol) | |
426 | (dotimes (row (1+ (- maxrow minrow))) | |
427 | (dotimes (col (1+ (- maxcol mincol))) | |
428 | (setq xrow (+ row minrow) | |
429 | xcol (+ col mincol) | |
430 | sym (ses-create-cell-symbol xrow xcol)) | |
431 | (put sym 'ses-cell (cons xrow xcol)) | |
432 | (make-local-variable sym))))) | |
433 | ||
434 | ;;;We do not delete the ses-cell properties for the cell-variables, in case a | |
435 | ;;;formula that refers to this cell is in the kill-ring and is later pasted | |
436 | ;;;back in. | |
437 | (defun ses-destroy-cell-variable-range (minrow maxrow mincol maxcol) | |
438 | "Destroy buffer-local variables for cells. This is undoable." | |
439 | (let (sym) | |
440 | (dotimes (row (1+ (- maxrow minrow))) | |
441 | (dotimes (col (1+ (- maxcol mincol))) | |
442 | (setq sym (ses-create-cell-symbol (+ row minrow) (+ col mincol))) | |
443 | (if (boundp sym) | |
444 | (push `(ses-set-with-undo ,sym ,(symbol-value sym)) | |
445 | buffer-undo-list)) | |
446 | (kill-local-variable sym)))) | |
447 | (push `(ses-create-cell-variable-range ,minrow ,maxrow ,mincol ,maxcol) | |
448 | buffer-undo-list)) | |
449 | ||
450 | (defun ses-reset-header-string () | |
451 | "Flags the header string for update. Upon undo, the header string will be | |
452 | updated again." | |
453 | (push '(ses-reset-header-string) buffer-undo-list) | |
454 | (setq header-hscroll -1)) | |
455 | ||
456 | ;;Split this code off into a function to avoid coverage-testing difficulties | |
457 | (defun ses-time-check (format arg) | |
458 | "If `ses-start-time' is more than a second ago, call `message' with FORMAT | |
459 | and (eval ARG) and reset `ses-start-time' to the current time." | |
460 | (when (> (- (float-time) ses-start-time) 1.0) | |
461 | (message format (eval arg)) | |
462 | (setq ses-start-time (float-time))) | |
463 | nil) | |
464 | ||
465 | ||
466 | ;;;---------------------------------------------------------------------------- | |
467 | ;;;; The cells | |
468 | ;;;---------------------------------------------------------------------------- | |
469 | ||
470 | (defun ses-set-cell (row col field val) | |
471 | "Install VAL as the contents for field FIELD (named by a quoted symbol) of | |
472 | cell (ROW,COL). This is undoable. The cell's data will be updated through | |
473 | `post-command-hook'." | |
474 | (let ((cell (ses-get-cell row col)) | |
475 | (elt (plist-get '(value t symbol 0 formula 1 printer 2 references 3) | |
476 | field)) | |
477 | change) | |
478 | (or elt (signal 'args-out-of-range nil)) | |
479 | (setq change (if (eq elt t) | |
480 | (ses-set-with-undo (ses-cell-symbol cell) val) | |
481 | (ses-aset-with-undo cell elt val))) | |
482 | (if change | |
483 | (add-to-list 'deferred-write (cons row col)))) | |
484 | nil) ;Make coverage-tester happy | |
485 | ||
486 | (defun ses-cell-set-formula (row col formula) | |
487 | "Store a new formula for (ROW . COL) and enqueues the cell for | |
488 | recalculation via `post-command-hook'. Updates the reference lists for the | |
489 | cells that this cell refers to. Does not update cell value or reprint the | |
490 | cell. To avoid inconsistencies, this function is not interruptible, which | |
491 | means Emacs will crash if FORMULA contains a circular list." | |
492 | (let* ((cell (ses-get-cell row col)) | |
493 | (old (ses-cell-formula cell))) | |
494 | (let ((sym (ses-cell-symbol cell)) | |
495 | (oldref (ses-formula-references old)) | |
496 | (newref (ses-formula-references formula)) | |
497 | (inhibit-quit t) | |
498 | x xrow xcol) | |
499 | (add-to-list 'deferred-recalc sym) | |
500 | ;;Delete old references from this cell. Skip the ones that are also | |
501 | ;;in the new list. | |
502 | (dolist (ref oldref) | |
503 | (unless (memq ref newref) | |
504 | (setq x (ses-sym-rowcol ref) | |
505 | xrow (car x) | |
506 | xcol (cdr x)) | |
507 | (ses-set-cell xrow xcol 'references | |
508 | (delq sym (ses-cell-references xrow xcol))))) | |
509 | ;;Add new ones. Skip ones left over from old list | |
510 | (dolist (ref newref) | |
511 | (setq x (ses-sym-rowcol ref) | |
512 | xrow (car x) | |
513 | xcol (cdr x) | |
514 | x (ses-cell-references xrow xcol)) | |
515 | (or (memq sym x) | |
516 | (ses-set-cell xrow xcol 'references (cons sym x)))) | |
517 | (ses-formula-record formula) | |
518 | (ses-set-cell row col 'formula formula)))) | |
519 | ||
520 | (defun ses-calculate-cell (row col force) | |
521 | "Calculate and print the value for cell (ROW,COL) using the cell's formula | |
522 | function and print functions, if any. Result is nil for normal operation, or | |
523 | the error signal if the formula or print function failed. The old value is | |
524 | left unchanged if it was *skip* and the new value is nil. | |
525 | Any cells that depend on this cell are queued for update after the end of | |
526 | processing for the current keystroke, unless the new value is the same as | |
527 | the old and FORCE is nil." | |
528 | (let ((cell (ses-get-cell row col)) | |
529 | formula-error printer-error) | |
530 | (let ((symbol (ses-cell-symbol cell)) | |
531 | (oldval (ses-cell-value cell)) | |
532 | (formula (ses-cell-formula cell)) | |
533 | newval) | |
534 | (if (eq (car-safe formula) 'ses-safe-formula) | |
535 | (ses-set-cell row col 'formula (ses-safe-formula (cadr formula)))) | |
536 | (condition-case sig | |
537 | (setq newval (eval formula)) | |
538 | (error | |
539 | (setq formula-error sig | |
540 | newval '*error*))) | |
541 | (if (and (not newval) (eq oldval '*skip*)) | |
542 | ;;Don't lose the *skip* - previous field spans this one | |
543 | (setq newval '*skip*)) | |
544 | (when (or force (not (eq newval oldval))) | |
545 | (add-to-list 'deferred-write (cons row col)) ;In case force=t | |
546 | (ses-set-cell row col 'value newval) | |
547 | (dolist (ref (ses-cell-references cell)) | |
548 | (add-to-list 'deferred-recalc ref)))) | |
549 | (setq printer-error (ses-print-cell row col)) | |
550 | (or formula-error printer-error))) | |
551 | ||
552 | (defun ses-clear-cell (row col) | |
553 | "Delete formula and printer for cell (ROW,COL)." | |
554 | (ses-set-cell row col 'printer nil) | |
555 | (ses-cell-set-formula row col nil)) | |
556 | ||
557 | (defun ses-update-cells (list &optional force) | |
558 | "Recalculate cells in LIST, checking for dependency loops. Prints | |
559 | progress messages every second. Dependent cells are not recalculated | |
560 | if the cell's value is unchanged if FORCE is nil." | |
561 | (let ((deferred-recalc list) | |
562 | (nextlist list) | |
563 | (pos (point)) | |
564 | curlist prevlist rowcol formula) | |
565 | (with-temp-message " " | |
566 | (while (and deferred-recalc (not (equal nextlist prevlist))) | |
567 | ;;In each loop, recalculate cells that refer only to other cells that | |
568 | ;;have already been recalculated or aren't in the recalculation | |
569 | ;;region. Repeat until all cells have been processed or until the | |
570 | ;;set of cells being worked on stops changing. | |
571 | (if prevlist | |
572 | (message "Recalculating... (%d cells left)" | |
573 | (length deferred-recalc))) | |
574 | (setq curlist deferred-recalc | |
575 | deferred-recalc nil | |
576 | prevlist nextlist) | |
577 | (while curlist | |
578 | (setq rowcol (ses-sym-rowcol (car curlist)) | |
579 | formula (ses-cell-formula (car rowcol) (cdr rowcol))) | |
580 | (or (catch 'ref | |
581 | (dolist (ref (ses-formula-references formula)) | |
582 | (when (or (memq ref curlist) | |
583 | (memq ref deferred-recalc)) | |
584 | ;;This cell refers to another that isn't done yet | |
585 | (add-to-list 'deferred-recalc (car curlist)) | |
586 | (throw 'ref t)))) | |
587 | ;;ses-update-cells is called from post-command-hook, so | |
588 | ;;inhibit-quit is implicitly bound to t. | |
589 | (when quit-flag | |
590 | ;;Abort the recalculation. User will probably undo now. | |
591 | (error "Quit")) | |
592 | (ses-calculate-cell (car rowcol) (cdr rowcol) force)) | |
593 | (setq curlist (cdr curlist))) | |
594 | (dolist (ref deferred-recalc) | |
595 | (add-to-list 'nextlist ref)) | |
596 | (setq nextlist (sort (copy-sequence nextlist) 'string<)) | |
597 | (if (equal nextlist prevlist) | |
598 | ;;We'll go around the loop one more time. | |
599 | (add-to-list 'nextlist t))) | |
600 | (when deferred-recalc | |
601 | ;;Just couldn't finish these | |
602 | (dolist (x deferred-recalc) | |
603 | (let ((rowcol (ses-sym-rowcol x))) | |
604 | (ses-set-cell (car rowcol) (cdr rowcol) 'value '*error*) | |
605 | (1value (ses-print-cell (car rowcol) (cdr rowcol))))) | |
606 | (error "Circular references: %s" deferred-recalc)) | |
607 | (message " ")) | |
608 | ;;Can't use save-excursion here: if the cell under point is | |
609 | ;;updated, save-excusion's marker will move past the cell. | |
610 | (goto-char pos))) | |
611 | ||
612 | ||
613 | ;;;---------------------------------------------------------------------------- | |
614 | ;;;; The print area | |
615 | ;;;---------------------------------------------------------------------------- | |
616 | ||
617 | ;;;We turn off point-motion-hooks and explicitly position the cursor, in case | |
618 | ;;;the intangible properties have gotten screwed up (e.g., when | |
619 | ;;;ses-goto-print is called during a recursive ses-print-cell). | |
620 | (defun ses-goto-print (row col) | |
621 | "Move point to print area for cell (ROW,COL)." | |
622 | (let ((inhibit-point-motion-hooks t)) | |
623 | (goto-char 1) | |
624 | (forward-line row) | |
625 | (dotimes (c col) | |
626 | (forward-char (1+ (ses-col-width c)))))) | |
627 | ||
628 | (defun ses-set-curcell () | |
629 | "Sets `curcell' to the current cell symbol, or a cons (BEG,END) for a | |
630 | region, or nil if cursor is not at a cell." | |
631 | (if (or (not mark-active) | |
632 | deactivate-mark | |
633 | (= (region-beginning) (region-end))) | |
634 | ;;Single cell | |
635 | (setq curcell (get-text-property (point) 'intangible)) | |
636 | ;;Range | |
637 | (let ((bcell (get-text-property (region-beginning) 'intangible)) | |
638 | (ecell (get-text-property (1- (region-end)) 'intangible))) | |
639 | (setq curcell (if (and bcell ecell) | |
640 | (cons bcell ecell) | |
641 | nil)))) | |
642 | nil) | |
643 | ||
644 | (defun ses-check-curcell (&rest args) | |
645 | "Signal an error if curcell is inappropriate. The end marker is | |
646 | appropriate if some argument is 'end. A range is appropriate if some | |
647 | argument is 'range. A single cell is appropriate unless some argument is | |
648 | 'needrange." | |
649 | (if (eq curcell t) | |
650 | ;;curcell recalculation was postponed, but user typed ahead | |
651 | (ses-set-curcell)) | |
652 | (cond | |
653 | ((not curcell) | |
654 | (or (memq 'end args) | |
655 | (error "Not at cell"))) | |
656 | ((consp curcell) | |
657 | (or (memq 'range args) | |
658 | (memq 'needrange args) | |
659 | (error "Can't use a range"))) | |
660 | ((memq 'needrange args) | |
661 | (error "Need a range")))) | |
662 | ||
663 | (defun ses-print-cell (row col) | |
664 | "Format and print the value of cell (ROW,COL) to the print area, using the | |
665 | cell's printer function. If the cell's new print form is too wide, it will | |
666 | spill over into the following cell, but will not run off the end of the row | |
667 | or overwrite the next non-nil field. Result is nil for normal operation, or | |
668 | the error signal if the printer function failed and the cell was formatted | |
669 | with \"%s\". If the cell's value is *skip*, nothing is printed because the | |
670 | preceding cell has spilled over." | |
671 | (catch 'ses-print-cell | |
672 | (let* ((cell (ses-get-cell row col)) | |
673 | (value (ses-cell-value cell)) | |
674 | (printer (ses-cell-printer cell)) | |
675 | (maxcol (1+ col)) | |
676 | text sig startpos x) | |
677 | ;;Create the string to print | |
678 | (cond | |
679 | ((eq value '*skip*) | |
680 | ;;Don't print anything | |
681 | (throw 'ses-print-cell nil)) | |
682 | ((eq value '*error*) | |
683 | (setq text (make-string (ses-col-width col) ?#))) | |
684 | (t | |
685 | ;;Deferred safety-check on printer | |
686 | (if (eq (car-safe printer) 'ses-safe-printer) | |
687 | (ses-set-cell row col 'printer | |
688 | (setq printer (ses-safe-printer (cadr printer))))) | |
689 | ;;Print the value | |
690 | (setq text (ses-call-printer (or printer | |
691 | (ses-col-printer col) | |
692 | default-printer) | |
693 | value)) | |
694 | (if (consp ses-call-printer-return) | |
695 | ;;Printer returned an error | |
696 | (setq sig ses-call-printer-return)))) | |
697 | ;;Adjust print width to match column width | |
698 | (let ((width (ses-col-width col)) | |
699 | (len (length text))) | |
700 | (cond | |
701 | ((< len width) | |
702 | ;;Fill field to length with spaces | |
703 | (setq len (make-string (- width len) ? ) | |
704 | text (if (eq ses-call-printer-return t) | |
705 | (concat text len) | |
706 | (concat len text)))) | |
707 | ((> len width) | |
708 | ;;Spill over into following cells, if possible | |
709 | (let ((maxwidth width)) | |
710 | (while (and (> len maxwidth) | |
711 | (< maxcol numcols) | |
712 | (or (not (setq x (ses-cell-value row maxcol))) | |
713 | (eq x '*skip*))) | |
714 | (unless x | |
715 | ;;Set this cell to '*skip* so it won't overwrite our spillover | |
716 | (ses-set-cell row maxcol 'value '*skip*)) | |
717 | (setq maxwidth (+ maxwidth (ses-col-width maxcol) 1) | |
718 | maxcol (1+ maxcol))) | |
719 | (if (<= len maxwidth) | |
720 | ;;Fill to complete width of all the fields spanned | |
721 | (setq text (concat text (make-string (- maxwidth len) ? ))) | |
722 | ;;Not enough room to end of line or next non-nil field. Truncate | |
723 | ;;if string; otherwise fill with error indicator | |
724 | (setq sig `(error "Too wide" ,text)) | |
725 | (if (stringp value) | |
726 | (setq text (substring text 0 maxwidth)) | |
727 | (setq text (make-string maxwidth ?#)))))))) | |
728 | ;;Substitute question marks for tabs and newlines. Newlines are | |
729 | ;;used as row-separators; tabs could confuse the reimport logic. | |
730 | (setq text (replace-regexp-in-string "[\t\n]" "?" text)) | |
731 | (ses-goto-print row col) | |
732 | (setq startpos (point)) | |
733 | ;;Install the printed result. This is not interruptible. | |
734 | (let ((inhibit-read-only t) | |
735 | (inhibit-quit t)) | |
736 | (delete-char (1+ (length text))) | |
737 | ;;We use concat instead of inserting separate strings in order to | |
738 | ;;reduce the number of cells in the undo list. | |
739 | (setq x (concat text (if (< maxcol numcols) " " "\n"))) | |
740 | ;;We use set-text-properties to prevent a wacky print function | |
741 | ;;from inserting rogue properties, and to ensure that the keymap | |
742 | ;;property is inherited (is it a bug that only unpropertied strings | |
743 | ;;actually inherit from surrounding text?) | |
744 | (set-text-properties 0 (length x) nil x) | |
745 | (insert-and-inherit x) | |
746 | (put-text-property startpos (point) 'intangible | |
747 | (ses-cell-symbol cell)) | |
748 | (when (and (zerop row) (zerop col)) | |
749 | ;;Reconstruct special beginning-of-buffer attributes | |
750 | (put-text-property 1 (point) 'keymap 'ses-mode-print-map) | |
751 | (put-text-property 1 (point) 'read-only 'ses) | |
752 | (put-text-property 1 2 'front-sticky t))) | |
753 | (if (= row (1- header-row)) | |
754 | ;;This line is part of the header - force recalc | |
755 | (ses-reset-header-string)) | |
756 | ;;If this cell (or a preceding one on the line) previously spilled over | |
757 | ;;and has gotten shorter, redraw following cells on line recursively. | |
758 | (when (and (< maxcol numcols) (eq (ses-cell-value row maxcol) '*skip*)) | |
759 | (ses-set-cell row maxcol 'value nil) | |
760 | (ses-print-cell row maxcol)) | |
761 | ;;Return to start of cell | |
762 | (goto-char startpos) | |
763 | sig))) | |
764 | ||
765 | (defun ses-call-printer (printer &optional value) | |
766 | "Invokes PRINTER (a string or parenthesized string or function-symbol or | |
767 | lambda of one argument) on VALUE. Result is the the printed cell as a | |
768 | string. The variable `ses-call-printer-return' is set to t if the printer | |
769 | used parenthesis to request left-justification, or the error-signal if the | |
770 | printer signalled one (and \"%s\" is used as the default printer), else nil." | |
771 | (setq ses-call-printer-return nil) | |
772 | (unless value | |
773 | (setq value "")) | |
774 | (condition-case signal | |
775 | (cond | |
776 | ((stringp printer) | |
777 | (format printer value)) | |
778 | ((stringp (car-safe printer)) | |
779 | (setq ses-call-printer-return t) | |
780 | (format (car printer) value)) | |
781 | (t | |
782 | (setq value (funcall printer value)) | |
783 | (if (stringp value) | |
784 | value | |
785 | (or (stringp (car-safe value)) | |
786 | (error "Printer should return \"string\" or (\"string\")")) | |
787 | (setq ses-call-printer-return t) | |
788 | (car value)))) | |
789 | (error | |
790 | (setq ses-call-printer-return signal) | |
791 | (prin1-to-string value t)))) | |
792 | ||
793 | (defun ses-adjust-print-width (col change) | |
794 | "Insert CHANGE spaces in front of column COL, or at end of line if | |
795 | COL=NUMCOLS. Deletes characters if CHANGE < 0. Caller should bind | |
796 | inhibit-quit to t." | |
797 | (let ((inhibit-read-only t) | |
798 | (blank (if (> change 0) (make-string change ? ))) | |
799 | (at-end (= col numcols))) | |
800 | (ses-set-with-undo 'linewidth (+ linewidth change)) | |
801 | ;;ses-set-with-undo always returns t for strings. | |
802 | (1value (ses-set-with-undo 'blank-line | |
803 | (concat (make-string linewidth ? ) "\n"))) | |
804 | (dotimes (row numrows) | |
805 | (ses-goto-print row col) | |
806 | (when at-end | |
807 | ;;Insert new columns before newline | |
808 | (let ((inhibit-point-motion-hooks t)) | |
809 | (backward-char 1))) | |
810 | (if blank | |
811 | (insert blank) | |
812 | (delete-char (- change)))))) | |
813 | ||
814 | (defun ses-print-cell-new-width (row col) | |
815 | "Same as ses-print-cell, except if the cell's value is *skip*, the preceding | |
816 | nonskipped cell is reprinted. This function is used when the width of | |
817 | cell (ROW,COL) has changed." | |
818 | (if (not (eq (ses-cell-value row col) '*skip*)) | |
819 | (ses-print-cell row col) | |
820 | ;;Cell was skipped over - reprint previous | |
821 | (ses-goto-print row col) | |
822 | (backward-char 1) | |
823 | (let ((rowcol (ses-sym-rowcol (get-text-property (point) 'intangible)))) | |
824 | (ses-print-cell (car rowcol) (cdr rowcol))))) | |
825 | ||
826 | ||
827 | ;;;---------------------------------------------------------------------------- | |
828 | ;;;; The data area | |
829 | ;;;---------------------------------------------------------------------------- | |
830 | ||
831 | (defun ses-goto-data (def &optional col) | |
832 | "Move point to data area for (DEF,COL). If DEF is a row number, COL is the | |
833 | column number for a data cell -- otherwise DEF is one of the symbols | |
834 | column-widths, col-printers, default-printer, numrows, or numcols." | |
835 | (if (< (point-max) (buffer-size)) | |
836 | (setq deferred-narrow t)) | |
837 | (widen) | |
838 | (let ((inhibit-point-motion-hooks t)) ;In case intangible attrs are wrong | |
839 | (goto-char 1) | |
840 | (if col | |
841 | ;;It's a cell | |
842 | (forward-line (+ numrows 2 (* def (1+ numcols)) col)) | |
843 | ;;Convert def-symbol to offset | |
844 | (setq def (plist-get ses-paramlines-plist def)) | |
845 | (or def (signal 'args-out-of-range nil)) | |
846 | (forward-line (+ (* numrows (+ numcols 2)) def))))) | |
847 | ||
848 | (defun ses-set-parameter (def value &optional elem) | |
849 | "Sets parameter DEF to VALUE (with undo) and writes the value to the data | |
850 | area. See `ses-goto-data' for meaning of DEF. Newlines in the data | |
851 | are escaped. If ELEM is specified, it is the array subscript within DEF to | |
852 | be set to VALUE." | |
853 | (save-excursion | |
854 | ;;We call ses-goto-data early, using the old values of numrows and | |
855 | ;;numcols in case one of them is being changed. | |
856 | (ses-goto-data def) | |
857 | (if elem | |
858 | (ses-aset-with-undo (symbol-value def) elem value) | |
859 | (ses-set-with-undo def value)) | |
860 | (let ((inhibit-read-only t) | |
861 | (fmt (plist-get '(column-widths "(ses-column-widths %S)" | |
862 | col-printers "(ses-column-printers %S)" | |
863 | default-printer "(ses-default-printer %S)" | |
864 | header-row "(ses-header-row %S)" | |
865 | file-format " %S ;SES file-format" | |
866 | numrows " %S ;numrows" | |
867 | numcols " %S ;numcols") | |
868 | def))) | |
869 | (delete-region (point) (line-end-position)) | |
870 | (insert (format fmt (symbol-value def)))))) | |
871 | ||
872 | (defun ses-write-cells () | |
873 | "`deferred-write' is a list of (ROW,COL) for cells to be written from | |
874 | buffer-local variables to data area. Newlines in the data are escaped." | |
875 | (let* ((inhibit-read-only t) | |
876 | (print-escape-newlines t) | |
877 | rowcol row col cell sym formula printer text) | |
878 | (setq ses-start-time (float-time)) | |
879 | (with-temp-message " " | |
880 | (save-excursion | |
881 | (while deferred-write | |
882 | (ses-time-check "Writing... (%d cells left)" | |
883 | '(length deferred-write)) | |
884 | (setq rowcol (pop deferred-write) | |
885 | row (car rowcol) | |
886 | col (cdr rowcol) | |
887 | cell (ses-get-cell row col) | |
888 | sym (ses-cell-symbol cell) | |
889 | formula (ses-cell-formula cell) | |
890 | printer (ses-cell-printer cell)) | |
891 | (if (eq (car-safe formula) 'ses-safe-formula) | |
892 | (setq formula (cadr formula))) | |
893 | (if (eq (car-safe printer) 'ses-safe-printer) | |
894 | (setq printer (cadr printer))) | |
895 | ;;This is noticably faster than (format "%S %S %S %S %S") | |
896 | (setq text (concat "(ses-cell " | |
897 | (symbol-name sym) | |
898 | " " | |
899 | (prin1-to-string (symbol-value sym)) | |
900 | " " | |
901 | (prin1-to-string formula) | |
902 | " " | |
903 | (prin1-to-string printer) | |
904 | " " | |
905 | (if (atom (ses-cell-references cell)) | |
906 | "nil" | |
907 | (concat "(" | |
908 | (mapconcat 'symbol-name | |
909 | (ses-cell-references cell) | |
910 | " ") | |
911 | ")")) | |
912 | ")")) | |
913 | (ses-goto-data row col) | |
914 | (delete-region (point) (line-end-position)) | |
915 | (insert text))) | |
916 | (message " ")))) | |
917 | ||
918 | ||
919 | ;;;---------------------------------------------------------------------------- | |
920 | ;;;; Formula relocation | |
921 | ;;;---------------------------------------------------------------------------- | |
922 | ||
923 | (defun ses-formula-references (formula &optional result-so-far) | |
924 | "Produce a list of symbols for cells that this formula's value | |
925 | refers to. For recursive calls, RESULT-SO-FAR is the list being constructed, | |
926 | or t to get a wrong-type-argument error when the first reference is found." | |
927 | (if (atom formula) | |
928 | (if (ses-sym-rowcol formula) | |
929 | ;;Entire formula is one symbol | |
930 | (add-to-list 'result-so-far formula) | |
931 | ) ;;Ignore other atoms | |
932 | (dolist (cur formula) | |
933 | (cond | |
934 | ((ses-sym-rowcol cur) | |
935 | ;;Save this reference | |
936 | (add-to-list 'result-so-far cur)) | |
937 | ((eq (car-safe cur) 'ses-range) | |
938 | ;;All symbols in range are referenced | |
939 | (dolist (x (cdr (macroexpand cur))) | |
940 | (add-to-list 'result-so-far x))) | |
941 | ((and (consp cur) (not (eq (car cur) 'quote))) | |
942 | ;;Recursive call for subformulas | |
943 | (setq result-so-far (ses-formula-references cur result-so-far))) | |
944 | (t | |
945 | ;;Ignore other stuff | |
946 | )))) | |
947 | result-so-far) | |
948 | ||
949 | (defun ses-relocate-formula (formula startrow startcol rowincr colincr) | |
950 | "Produce a copy of FORMULA where all symbols that refer to cells in row | |
951 | STARTROW or above and col STARTCOL or above are altered by adding ROWINCR | |
952 | and COLINCR. STARTROW and STARTCOL are 0-based. Example: | |
953 | (ses-relocate-formula '(+ A1 B2 D3) 1 2 1 -1) | |
954 | => (+ A1 B2 C4) | |
955 | If ROWINCR or COLINCR is negative, references to cells being deleted are | |
956 | removed. Example: | |
957 | (ses-relocate-formula '(+ A1 B2 D3) 0 1 0 -1) | |
958 | => (+ A1 C3) | |
959 | Sets `ses-relocate-return' to 'delete if cell-references were removed." | |
960 | (let (rowcol result) | |
961 | (if (or (atom formula) (eq (car formula) 'quote)) | |
962 | (if (setq rowcol (ses-sym-rowcol formula)) | |
963 | (ses-relocate-symbol formula rowcol | |
964 | startrow startcol rowincr colincr) | |
965 | formula) ;Pass through as-is | |
966 | (dolist (cur formula) | |
967 | (setq rowcol (ses-sym-rowcol cur)) | |
968 | (cond | |
969 | (rowcol | |
970 | (setq cur (ses-relocate-symbol cur rowcol | |
971 | startrow startcol rowincr colincr)) | |
972 | (if cur | |
973 | (push cur result) | |
974 | ;;Reference to a deleted cell. Set a flag in ses-relocate-return. | |
975 | ;;don't change the flag if it's already 'range, since range | |
976 | ;;implies 'delete. | |
977 | (unless ses-relocate-return | |
978 | (setq ses-relocate-return 'delete)))) | |
979 | ((eq (car-safe cur) 'ses-range) | |
980 | (setq cur (ses-relocate-range cur startrow startcol rowincr colincr)) | |
981 | (if cur | |
982 | (push cur result))) | |
983 | ((or (atom cur) (eq (car cur) 'quote)) | |
984 | ;;Constants pass through unchanged | |
985 | (push cur result)) | |
986 | (t | |
987 | ;;Recursively copy and alter subformulas | |
988 | (push (ses-relocate-formula cur startrow startcol | |
989 | rowincr colincr) | |
990 | result)))) | |
991 | (nreverse result)))) | |
992 | ||
993 | (defun ses-relocate-symbol (sym rowcol startrow startcol rowincr colincr) | |
994 | "Relocate one symbol SYM, whichs corresponds to ROWCOL (a cons of ROW and | |
995 | COL). Cells starting at (STARTROW,STARTCOL) are being shifted | |
996 | by (ROWINCR,COLINCR)." | |
997 | (let ((row (car rowcol)) | |
998 | (col (cdr rowcol))) | |
999 | (if (or (< row startrow) (< col startcol)) | |
1000 | sym | |
1001 | (setq row (+ row rowincr) | |
1002 | col (+ col colincr)) | |
1003 | (if (and (>= row startrow) (>= col startcol) | |
1004 | (< row numrows) (< col numcols)) | |
1005 | ;;Relocate this variable | |
1006 | (ses-create-cell-symbol row col) | |
1007 | ;;Delete reference to a deleted cell | |
1008 | nil)))) | |
1009 | ||
1010 | (defun ses-relocate-range (range startrow startcol rowincr colincr) | |
1011 | "Relocate one RANGE, of the form '(ses-range min max). Cells starting | |
1012 | at (STARTROW,STARTCOL) are being shifted by (ROWINCR,COLINCR). Result is the | |
1013 | new range, or nil if the entire range is deleted. If new rows are being added | |
1014 | just beyond the end of a row range, or new columns just beyond a column range, | |
1015 | the new rows/columns will be added to the range. Sets `ses-relocate-return' | |
1016 | if the range was altered." | |
1017 | (let* ((minorig (cadr range)) | |
1018 | (minrowcol (ses-sym-rowcol minorig)) | |
1019 | (min (ses-relocate-symbol minorig minrowcol | |
1020 | startrow startcol | |
1021 | rowincr colincr)) | |
1022 | (maxorig (nth 2 range)) | |
1023 | (maxrowcol (ses-sym-rowcol maxorig)) | |
1024 | (max (ses-relocate-symbol maxorig maxrowcol | |
1025 | startrow startcol | |
1026 | rowincr colincr)) | |
1027 | field) | |
1028 | (cond | |
1029 | ((and (not min) (not max)) | |
1030 | (setq range nil)) ;;The entire range is deleted | |
1031 | ((zerop colincr) | |
1032 | ;;Inserting or deleting rows | |
1033 | (setq field 'car) | |
1034 | (if (not min) | |
1035 | ;;Chopped off beginning of range | |
1036 | (setq min (ses-create-cell-symbol startrow (cdr minrowcol)) | |
1037 | ses-relocate-return 'range)) | |
1038 | (if (not max) | |
1039 | (if (> rowincr 0) | |
1040 | ;;Trying to insert a nonexistent row | |
1041 | (setq max (ses-create-cell-symbol (1- numrows) (cdr minrowcol))) | |
1042 | ;;End of range is being deleted | |
1043 | (setq max (ses-create-cell-symbol (1- startrow) (cdr minrowcol)) | |
1044 | ses-relocate-return 'range)) | |
1045 | (and (> rowincr 0) | |
1046 | (= (car maxrowcol) (1- startrow)) | |
1047 | (= (cdr minrowcol) (cdr maxrowcol)) | |
1048 | ;;Insert after ending row of vertical range - include it | |
1049 | (setq max (ses-create-cell-symbol (+ startrow rowincr -1) | |
1050 | (cdr maxrowcol)))))) | |
1051 | (t | |
1052 | ;;Inserting or deleting columns | |
1053 | (setq field 'cdr) | |
1054 | (if (not min) | |
1055 | ;;Chopped off beginning of range | |
1056 | (setq min (ses-create-cell-symbol (car minrowcol) startcol) | |
1057 | ses-relocate-return 'range)) | |
1058 | (if (not max) | |
1059 | (if (> colincr 0) | |
1060 | ;;Trying to insert a nonexistent column | |
1061 | (setq max (ses-create-cell-symbol (car maxrowcol) (1- numcols))) | |
1062 | ;;End of range is being deleted | |
1063 | (setq max (ses-create-cell-symbol (car maxrowcol) (1- startcol)) | |
1064 | ses-relocate-return 'range)) | |
1065 | (and (> colincr 0) | |
1066 | (= (cdr maxrowcol) (1- startcol)) | |
1067 | (= (car minrowcol) (car maxrowcol)) | |
1068 | ;;Insert after ending column of horizontal range - include it | |
1069 | (setq max (ses-create-cell-symbol (car maxrowcol) | |
1070 | (+ startcol colincr -1))))))) | |
1071 | (when range | |
1072 | (if (/= (- (funcall field maxrowcol) | |
1073 | (funcall field minrowcol)) | |
1074 | (- (funcall field (ses-sym-rowcol max)) | |
1075 | (funcall field (ses-sym-rowcol min)))) | |
1076 | ;;This range has changed size | |
1077 | (setq ses-relocate-return 'range)) | |
1078 | (list 'ses-range min max)))) | |
1079 | ||
1080 | (defun ses-relocate-all (minrow mincol rowincr colincr) | |
1081 | "Alter all cell values, symbols, formulas, and reference-lists to relocate | |
1082 | the rectangle (MINROW,MINCOL)..(NUMROWS,NUMCOLS) by adding ROWINCR and COLINCR | |
1083 | to each symbol." | |
1084 | (let (reform) | |
1085 | (let (mycell newval) | |
1086 | (ses-dotimes-msg (row numrows) "Relocating formulas..." | |
1087 | (dotimes (col numcols) | |
1088 | (setq ses-relocate-return nil | |
1089 | mycell (ses-get-cell row col) | |
1090 | newval (ses-relocate-formula (ses-cell-formula mycell) | |
1091 | minrow mincol rowincr colincr)) | |
1092 | (ses-set-cell row col 'formula newval) | |
1093 | (if (eq ses-relocate-return 'range) | |
1094 | ;;This cell contains a (ses-range X Y) where a cell has been | |
1095 | ;;inserted or deleted in the middle of the range. | |
1096 | (push (cons row col) reform)) | |
1097 | (if ses-relocate-return | |
1098 | ;;This cell referred to a cell that's been deleted or is no | |
1099 | ;;longer part of the range. We can't fix that now because | |
1100 | ;;reference lists cells have been partially updated. | |
1101 | (add-to-list 'deferred-recalc | |
1102 | (ses-create-cell-symbol row col))) | |
1103 | (setq newval (ses-relocate-formula (ses-cell-references mycell) | |
1104 | minrow mincol rowincr colincr)) | |
1105 | (ses-set-cell row col 'references newval) | |
1106 | (and (>= row minrow) (>= col mincol) | |
1107 | (ses-set-cell row col 'symbol | |
1108 | (ses-create-cell-symbol row col)))))) | |
1109 | ;;Relocate the cell values | |
1110 | (let (oldval myrow mycol xrow xcol) | |
1111 | (cond | |
1112 | ((and (<= rowincr 0) (<= colincr 0)) | |
1113 | ;;Deletion of rows and/or columns | |
1114 | (ses-dotimes-msg (row (- numrows minrow)) "Relocating variables..." | |
1115 | (setq myrow (+ row minrow)) | |
1116 | (dotimes (col (- numcols mincol)) | |
1117 | (setq mycol (+ col mincol) | |
1118 | xrow (- myrow rowincr) | |
1119 | xcol (- mycol colincr)) | |
1120 | (if (and (< xrow numrows) (< xcol numcols)) | |
1121 | (setq oldval (ses-cell-value xrow xcol)) | |
1122 | ;;Cell is off the end of the array | |
1123 | (setq oldval (symbol-value (ses-create-cell-symbol xrow xcol)))) | |
1124 | (ses-set-cell myrow mycol 'value oldval)))) | |
1125 | ((and (wholenump rowincr) (wholenump colincr)) | |
1126 | ;;Insertion of rows and/or columns. Run the loop backwards. | |
1127 | (let ((disty (1- numrows)) | |
1128 | (distx (1- numcols)) | |
1129 | myrow mycol) | |
1130 | (ses-dotimes-msg (row (- numrows minrow)) "Relocating variables..." | |
1131 | (setq myrow (- disty row)) | |
1132 | (dotimes (col (- numcols mincol)) | |
1133 | (setq mycol (- distx col) | |
1134 | xrow (- myrow rowincr) | |
1135 | xcol (- mycol colincr)) | |
1136 | (if (or (< xrow minrow) (< xcol mincol)) | |
1137 | ;;Newly-inserted value | |
1138 | (setq oldval nil) | |
1139 | ;;Transfer old value | |
1140 | (setq oldval (ses-cell-value xrow xcol))) | |
1141 | (ses-set-cell myrow mycol 'value oldval))) | |
1142 | t)) ;Make testcover happy by returning non-nil here | |
1143 | (t | |
1144 | (error "ROWINCR and COLINCR must have the same sign")))) | |
1145 | ;;Reconstruct reference lists for cells that contain ses-ranges that | |
1146 | ;;have changed size. | |
1147 | (when reform | |
1148 | (message "Fixing ses-ranges...") | |
1149 | (let (row col) | |
1150 | (setq ses-start-time (float-time)) | |
1151 | (while reform | |
1152 | (ses-time-check "Fixing ses-ranges... (%d left)" '(length reform)) | |
1153 | (setq row (caar reform) | |
1154 | col (cdar reform) | |
1155 | reform (cdr reform)) | |
1156 | (ses-cell-set-formula row col (ses-cell-formula row col)))) | |
1157 | (message nil)))) | |
1158 | ||
1159 | ||
1160 | ;;;---------------------------------------------------------------------------- | |
1161 | ;;;; Undo control | |
1162 | ;;;---------------------------------------------------------------------------- | |
1163 | ||
1164 | (defadvice undo-more (around ses-undo-more activate preactivate) | |
1165 | "Define a meaning for conses in buffer-undo-list whose car is a symbol | |
1166 | other than t or nil. To undo these, apply the car--a function--to the | |
1167 | cdr--its arglist." | |
1168 | (let ((ses-count (ad-get-arg 0))) | |
1169 | (catch 'undo | |
1170 | (dolist (ses-x pending-undo-list) | |
1171 | (unless ses-x | |
1172 | ;;End of undo boundary | |
1173 | (setq ses-count (1- ses-count)) | |
1174 | (if (<= ses-count 0) | |
1175 | ;;We've seen enough boundaries - stop undoing | |
1176 | (throw 'undo nil))) | |
1177 | (and (consp ses-x) (symbolp (car ses-x)) (fboundp (car ses-x)) | |
1178 | ;;Undo using apply | |
1179 | (apply (car ses-x) (cdr ses-x))))) | |
1180 | (if (not (eq major-mode 'ses-mode)) | |
1181 | ad-do-it | |
1182 | ;;Here is some extra code for SES mode. | |
1183 | (setq deferred-narrow (or deferred-narrow (< (point-max) (buffer-size)))) | |
1184 | (widen) | |
1185 | (condition-case x | |
1186 | ad-do-it | |
1187 | (error | |
1188 | ;;Restore narrow if appropriate | |
1189 | (ses-command-hook) | |
1190 | (signal (car x) (cdr x))))))) | |
1191 | ||
1192 | (defun ses-begin-change () | |
1193 | "For undo, remember current buffer-position before we start changing hidden | |
1194 | stuff." | |
1195 | (let ((inhibit-read-only t)) | |
1196 | (insert-and-inherit "X") | |
1197 | (delete-region (1- (point)) (point)))) | |
1198 | ||
1199 | (defun ses-set-with-undo (sym newval) | |
1200 | "Like set, but undoable. Result is t if value has changed." | |
1201 | ;;We avoid adding redundant entries to the undo list, but this is | |
1202 | ;;unavoidable for strings because equal ignores text properties and there's | |
1203 | ;;no easy way to get the whole property list to see if it's different! | |
1204 | (unless (and (boundp sym) | |
1205 | (equal (symbol-value sym) newval) | |
1206 | (not (stringp newval))) | |
1207 | (push (if (boundp sym) | |
1208 | `(ses-set-with-undo ,sym ,(symbol-value sym)) | |
1209 | `(ses-unset-with-undo ,sym)) | |
1210 | buffer-undo-list) | |
1211 | (set sym newval) | |
1212 | t)) | |
1213 | ||
1214 | (defun ses-unset-with-undo (sym) | |
1215 | "Set SYM to be unbound. This is undoable." | |
1216 | (when (1value (boundp sym)) ;;Always bound, except after a programming error | |
1217 | (push `(ses-set-with-undo ,sym ,(symbol-value sym)) buffer-undo-list) | |
1218 | (makunbound sym))) | |
1219 | ||
1220 | (defun ses-aset-with-undo (array idx newval) | |
1221 | "Like aset, but undoable. Result is t if element has changed" | |
1222 | (unless (equal (aref array idx) newval) | |
1223 | (push `(ses-aset-with-undo ,array ,idx ,(aref array idx)) buffer-undo-list) | |
1224 | (aset array idx newval) | |
1225 | t)) | |
1226 | ||
1227 | ||
1228 | ;;;---------------------------------------------------------------------------- | |
1229 | ;;;; Startup for major mode | |
1230 | ;;;---------------------------------------------------------------------------- | |
1231 | ||
1232 | (defun ses-build-mode-map () | |
1233 | "Set up `ses-mode-map', `ses-mode-print-map', and `ses-mode-edit-map' with | |
1234 | standard keymap bindings for SES." | |
1235 | (message "Building mode map...") | |
1236 | ;;;Define ses-mode-map | |
1237 | (let ((keys '("\C-c\M-\C-l" ses-reconstruct-all | |
1238 | "\C-c\C-l" ses-recalculate-all | |
1239 | "\C-c\C-n" ses-renarrow-buffer | |
1240 | "\C-c\C-c" ses-recalculate-cell | |
1241 | "\C-c\M-\C-s" ses-sort-column | |
1242 | "\C-c\M-\C-h" ses-read-header-row | |
1243 | "\C-c\C-t" ses-truncate-cell | |
1244 | "\C-c\C-j" ses-jump | |
1245 | "\C-c\C-p" ses-read-default-printer | |
1246 | "\M-\C-l" ses-reprint-all | |
1247 | [?\S-\C-l] ses-reprint-all | |
1248 | [header-line mouse-2] ses-sort-column-click)) | |
1249 | (newmap (make-sparse-keymap))) | |
1250 | (while keys | |
1251 | (define-key (1value newmap) (car keys) (cadr keys)) | |
1252 | (setq keys (cddr keys))) | |
1253 | (setq ses-mode-map (1value newmap))) | |
1254 | ;;;Define ses-mode-print-map | |
1255 | (let ((keys '(;;At least three ways to define shift-tab--and some PC systems | |
1256 | ;;won't generate it at all! | |
1257 | [S-tab] backward-char | |
1258 | [backtab] backward-char | |
1259 | [S-iso-backtab] backward-char | |
1260 | [S-iso-lefttab] backward-char | |
1261 | [tab] ses-forward-or-insert | |
1262 | "\C-i" ses-forward-or-insert ;Needed for ses-coverage.el? | |
1263 | "\M-o" ses-insert-column | |
1264 | "\C-o" ses-insert-row | |
1265 | "\C-m" ses-edit-cell | |
1266 | "\M-k" ses-delete-column | |
1267 | "\M-y" ses-yank-pop | |
1268 | "\C-k" ses-delete-row | |
1269 | "\C-j" ses-append-row-jump-first-column | |
1270 | "\M-h" ses-mark-row | |
1271 | "\M-H" ses-mark-column | |
1272 | "\C-d" ses-clear-cell-forward | |
1273 | "\C-?" ses-clear-cell-backward | |
1274 | "(" ses-read-cell | |
1275 | "\"" ses-read-cell | |
1276 | "'" ses-read-symbol | |
1277 | "=" ses-edit-cell | |
1278 | "j" ses-jump | |
1279 | "p" ses-read-cell-printer | |
1280 | "w" ses-set-column-width | |
1281 | "x" ses-export-keymap | |
1282 | "\M-p" ses-read-column-printer)) | |
1283 | (repl '(;;We'll replace these wherever they appear in the keymap | |
1284 | clipboard-kill-region ses-kill-override | |
1285 | end-of-line ses-end-of-line | |
1286 | kill-line ses-delete-row | |
1287 | kill-region ses-kill-override | |
1288 | open-line ses-insert-row)) | |
1289 | (numeric "0123456789.-") | |
1290 | (newmap (make-keymap))) | |
1291 | ;;Get rid of printables | |
1292 | (suppress-keymap (1value newmap) t) | |
1293 | ;;These keys insert themselves as the beginning of a numeric value | |
1294 | (dotimes (x (length (1value numeric))) | |
1295 | (define-key (1value newmap) | |
1296 | (substring (1value numeric) x (1+ x)) | |
1297 | 'ses-read-cell)) | |
1298 | ;;Override these global functions wherever they're bound | |
1299 | (while repl | |
1300 | (substitute-key-definition (car repl) (cadr repl) | |
1301 | (1value newmap) | |
1302 | (current-global-map)) | |
1303 | (setq repl (cddr repl))) | |
1304 | ;;Apparently substitute-key-definition doesn't catch this? | |
1305 | (define-key (1value newmap) [(menu-bar) edit cut] 'ses-kill-override) | |
1306 | ;;Define our other local keys | |
1307 | (while keys | |
1308 | (define-key (1value newmap) (car keys) (cadr keys)) | |
1309 | (setq keys (cddr keys))) | |
1310 | ;;Keymap property wants the map as a function, not a variable | |
1311 | (fset 'ses-mode-print-map (1value newmap)) | |
1312 | (setq ses-mode-print-map (1value newmap))) | |
1313 | ;;;Define ses-mode-edit-map | |
1314 | (let ((keys '("\C-c\C-r" ses-insert-range | |
1315 | "\C-c\C-s" ses-insert-ses-range | |
1316 | [S-mouse-3] ses-insert-range-click | |
1317 | [C-S-mouse-3] ses-insert-ses-range-click | |
1318 | "\M-\C-i" lisp-complete-symbol)) | |
1319 | (newmap (make-sparse-keymap))) | |
1320 | (1value (set-keymap-parent (1value newmap) (1value minibuffer-local-map))) | |
1321 | (while keys | |
1322 | (define-key (1value newmap) (car keys) (cadr keys)) | |
1323 | (setq keys (cddr keys))) | |
1324 | (setq ses-mode-edit-map (1value newmap))) | |
1325 | (message nil)) | |
1326 | ||
1327 | (defun ses-load () | |
1328 | "Parse the current buffer and sets up buffer-local variables. Does not | |
1329 | execute cell formulas or print functions." | |
1330 | (widen) | |
1331 | ;;Read our global parameters, which should be a 3-element list | |
1332 | (goto-char (point-max)) | |
1333 | (search-backward ";;; Local Variables:\n" nil t) | |
1334 | (backward-list 1) | |
1335 | (let ((params (condition-case nil (read (current-buffer)) (error nil))) | |
1336 | sym) | |
1337 | (or (and (= (safe-length params) 3) | |
1338 | (numberp (car params)) | |
1339 | (numberp (cadr params)) | |
1340 | (> (cadr params) 0) | |
1341 | (numberp (nth 2 params)) | |
1342 | (> (nth 2 params) 0)) | |
1343 | (error "Invalid SES file")) | |
1344 | (setq file-format (car params) | |
1345 | numrows (cadr params) | |
1346 | numcols (nth 2 params)) | |
1347 | (when (= file-format 1) | |
1348 | (let (buffer-undo-list) ;This is not undoable | |
1349 | (ses-goto-data 'header-row) | |
1350 | (insert "(ses-header-row 0)\n") | |
1351 | (ses-set-parameter 'file-format 2) | |
1352 | (message "Upgrading from SES-1 file format"))) | |
1353 | (or (= file-format 2) | |
1354 | (error "This file needs a newer version of the SES library code.")) | |
1355 | (ses-create-cell-variable-range 0 (1- numrows) 0 (1- numcols)) | |
1356 | ;;Initialize cell array | |
1357 | (setq cells (make-vector numrows nil)) | |
1358 | (dotimes (row numrows) | |
1359 | (aset cells row (make-vector numcols nil)))) | |
1360 | ;;Skip over print area, which we assume is correct | |
1361 | (goto-char 1) | |
1362 | (forward-line numrows) | |
1363 | (or (looking-at ses-print-data-boundary) | |
1364 | (error "Missing marker between print and data areas")) | |
1365 | (forward-char (length ses-print-data-boundary)) | |
1366 | ;;Initialize printer and symbol lists | |
1367 | (mapc 'ses-printer-record ses-standard-printer-functions) | |
1368 | (setq symbolic-formulas nil) | |
1369 | ;;Load cell definitions | |
1370 | (dotimes (row numrows) | |
1371 | (dotimes (col numcols) | |
1372 | (let* ((x (read (current-buffer))) | |
1373 | (rowcol (ses-sym-rowcol (car-safe (cdr-safe x))))) | |
1374 | (or (and (looking-at "\n") | |
1375 | (eq (car-safe x) 'ses-cell) | |
1376 | (eq row (car rowcol)) | |
1377 | (eq col (cdr rowcol))) | |
1378 | (error "Cell-def error")) | |
1379 | (eval x))) | |
1380 | (or (looking-at "\n\n") | |
1381 | (error "Missing blank line between rows"))) | |
1382 | ;;Load global parameters | |
1383 | (let ((widths (read (current-buffer))) | |
1384 | (n1 (char-after (point))) | |
1385 | (printers (read (current-buffer))) | |
1386 | (n2 (char-after (point))) | |
1387 | (def-printer (read (current-buffer))) | |
1388 | (n3 (char-after (point))) | |
1389 | (head-row (read (current-buffer))) | |
1390 | (n4 (char-after (point)))) | |
1391 | (or (and (eq (car-safe widths) 'ses-column-widths) | |
1392 | (= n1 ?\n) | |
1393 | (eq (car-safe printers) 'ses-column-printers) | |
1394 | (= n2 ?\n) | |
1395 | (eq (car-safe def-printer) 'ses-default-printer) | |
1396 | (= n3 ?\n) | |
1397 | (eq (car-safe head-row) 'ses-header-row) | |
1398 | (= n4 ?\n)) | |
1399 | (error "Invalid SES global parameters")) | |
1400 | (1value (eval widths)) | |
1401 | (1value (eval def-printer)) | |
1402 | (1value (eval printers)) | |
1403 | (1value (eval head-row))) | |
1404 | ;;Should be back at global-params | |
1405 | (forward-char 1) | |
1406 | (or (looking-at (replace-regexp-in-string "1" "[0-9]+" | |
1407 | ses-initial-global-parameters)) | |
1408 | (error "Problem with column-defs or global-params")) | |
1409 | ;;Check for overall newline count in definitions area | |
1410 | (forward-line 3) | |
1411 | (let ((start (point))) | |
1412 | (ses-goto-data 'numrows) | |
1413 | (or (= (point) start) | |
1414 | (error "Extraneous newlines someplace?")))) | |
1415 | ||
1416 | (defun ses-setup () | |
1417 | "Set up for display of only the printed cell values. | |
1418 | ||
1419 | Narrows the buffer to show only the print area. Gives it `read-only' and | |
1420 | `intangible' properties. Sets up highlighting for current cell." | |
1421 | (interactive) | |
1422 | (let ((end 1) | |
1423 | (inhibit-read-only t) | |
1424 | (was-modified (buffer-modified-p)) | |
1425 | pos sym) | |
1426 | (ses-goto-data 0 0) ;;Include marker between print-area and data-area | |
1427 | (set-text-properties (point) (buffer-size) nil) ;Delete garbage props | |
1428 | (mapc 'delete-overlay (overlays-in 1 (buffer-size))) | |
1429 | ;;The print area is read-only (except for our special commands) and uses a | |
1430 | ;;special keymap. | |
1431 | (put-text-property 1 (1- (point)) 'read-only 'ses) | |
1432 | (put-text-property 1 (1- (point)) 'keymap 'ses-mode-print-map) | |
1433 | ;;For the beginning of the buffer, we want the read-only and keymap | |
1434 | ;;attributes to be inherited from the first character | |
1435 | (put-text-property 1 2 'front-sticky t) | |
1436 | ;;Create intangible properties, which also indicate which cell the text | |
1437 | ;;came from. | |
1438 | (ses-dotimes-msg (row numrows) "Finding cells..." | |
1439 | (dotimes (col numcols) | |
1440 | (setq pos end | |
1441 | sym (ses-cell-symbol row col)) | |
1442 | ;;Include skipped cells following this one | |
1443 | (while (and (< col (1- numcols)) | |
1444 | (eq (ses-cell-value row (1+ col)) '*skip*)) | |
1445 | (setq end (+ end (ses-col-width col) 1) | |
1446 | col (1+ col))) | |
1447 | (setq end (+ end (ses-col-width col) 1)) | |
1448 | (put-text-property pos end 'intangible sym))) | |
1449 | ;;Adding these properties did not actually alter the text | |
1450 | (unless was-modified | |
1451 | (set-buffer-modified-p nil) | |
1452 | (buffer-disable-undo) | |
1453 | (buffer-enable-undo))) | |
1454 | ;;Create the underlining overlay. It's impossible for (point) to be 2, | |
1455 | ;;because column A must be at least 1 column wide. | |
1456 | (setq curcell-overlay (make-overlay 2 2)) | |
1457 | (overlay-put curcell-overlay 'face 'underline)) | |
1458 | ||
1459 | (defun ses-cleanup () | |
1460 | "Cleanup when changing a buffer from SES mode to something else. Delete | |
1461 | overlay, remove special text properties." | |
1462 | (widen) | |
1463 | (let ((inhibit-read-only t) | |
1464 | (was-modified (buffer-modified-p)) | |
1465 | end) | |
1466 | ;;Delete read-only, keymap, and intangible properties | |
1467 | (set-text-properties 1 (point-max) nil) | |
1468 | ;;Delete overlay | |
1469 | (mapc 'delete-overlay (overlays-in 1 (point-max))) | |
1470 | (unless was-modified | |
1471 | (set-buffer-modified-p nil)))) | |
1472 | ||
1473 | ;;;###autoload | |
1474 | (defun ses-mode () | |
1475 | "Major mode for Simple Emacs Spreadsheet. See \"ses-readme.txt\" for more info. | |
1476 | ||
1477 | Key definitions: | |
1478 | \\{ses-mode-map} | |
1479 | These key definitions are active only in the print area (the visible part): | |
1480 | \\{ses-mode-print-map} | |
1481 | These are active only in the minibuffer, when entering or editing a formula: | |
1482 | \\{ses-mode-edit-map}" | |
1483 | (interactive) | |
1484 | (unless (and (boundp 'deferred-narrow) | |
1485 | (eq deferred-narrow 'ses-mode)) | |
1486 | (kill-all-local-variables) | |
1487 | (mapc 'make-local-variable ses-localvars) | |
1488 | (setq major-mode 'ses-mode | |
1489 | mode-name "SES" | |
1490 | next-line-add-newlines nil | |
1491 | truncate-lines t | |
1492 | ;;SES deliberately puts lots of trailing whitespace in its buffer | |
1493 | show-trailing-whitespace nil | |
1494 | ;;Cell ranges do not work reasonably without this | |
1495 | transient-mark-mode t) | |
1496 | (unless (and ses-mode-map ses-mode-print-map ses-mode-edit-map) | |
1497 | (ses-build-mode-map)) | |
1498 | (1value (add-hook 'change-major-mode-hook 'ses-cleanup nil t)) | |
1499 | (1value (add-hook 'before-revert-hook 'ses-cleanup nil t)) | |
1500 | (setq curcell nil | |
1501 | deferred-recalc nil | |
1502 | deferred-write nil | |
1503 | header-hscroll -1 ;Flag for "initial recalc needed" | |
1504 | header-line-format '(:eval (progn | |
1505 | (when (/= (window-hscroll) | |
1506 | header-hscroll) | |
1507 | ;;Reset header-hscroll first, to | |
1508 | ;;avoid recursion problems when | |
1509 | ;;debugging ses-create-header-string | |
1510 | (setq header-hscroll (window-hscroll)) | |
1511 | (ses-create-header-string)) | |
1512 | header-string))) | |
1513 | (let ((was-empty (zerop (buffer-size))) | |
1514 | (was-modified (buffer-modified-p))) | |
1515 | (save-excursion | |
1516 | (if was-empty | |
1517 | ;;Initialize buffer to contain one cell, for now | |
1518 | (insert ses-initial-file-contents)) | |
1519 | (ses-load) | |
1520 | (ses-setup)) | |
1521 | (when was-empty | |
1522 | (unless (equal ses-initial-default-printer (1value default-printer)) | |
1523 | (1value (ses-read-default-printer ses-initial-default-printer))) | |
1524 | (unless (= ses-initial-column-width (1value (ses-col-width 0))) | |
1525 | (1value (ses-set-column-width 0 ses-initial-column-width))) | |
1526 | (ses-set-curcell) | |
1527 | (if (> (car ses-initial-size) (1value numrows)) | |
1528 | (1value (ses-insert-row (1- (car ses-initial-size))))) | |
1529 | (if (> (cdr ses-initial-size) (1value numcols)) | |
1530 | (1value (ses-insert-column (1- (cdr ses-initial-size))))) | |
1531 | (ses-write-cells) | |
1532 | (set-buffer-modified-p was-modified) | |
1533 | (buffer-disable-undo) | |
1534 | (buffer-enable-undo) | |
1535 | (goto-char 1))) | |
1536 | (use-local-map ses-mode-map) | |
1537 | ;;Set the deferred narrowing flag (we can't narrow until after | |
1538 | ;;after-find-file completes). If .ses is on the auto-load alist and the | |
1539 | ;;file has "mode: ses", our ses-mode function will be called twice! Use | |
1540 | ;;a special flag to detect this (will be reset by ses-command-hook). | |
1541 | ;;For find-alternate-file, post-command-hook doesn't get run for some | |
1542 | ;;reason, so use an idle timer to make sure. | |
1543 | (setq deferred-narrow 'ses-mode) | |
1544 | (1value (add-hook 'post-command-hook 'ses-command-hook nil t)) | |
1545 | (run-with-idle-timer 0.01 nil 'ses-command-hook) | |
1546 | (run-hooks 'ses-mode-hook))) | |
1547 | ||
1548 | (put 'ses-mode 'mode-class 'special) | |
1549 | ||
1550 | (defun ses-command-hook () | |
1551 | "Invoked from `post-command-hook'. If point has moved to a different cell, | |
1552 | moves the underlining overlay. Performs any recalculations or cell-data | |
1553 | writes that have been deferred. If buffer-narrowing has been deferred, | |
1554 | narrows the buffer now." | |
1555 | (condition-case err | |
1556 | (when (eq major-mode 'ses-mode) ;Otherwise, not our buffer anymore | |
1557 | (when deferred-recalc | |
1558 | ;;We reset the deferred list before starting on the recalc -- in case | |
1559 | ;;of error, we don't want to retry the recalc after every keystroke! | |
1560 | (let ((old deferred-recalc)) | |
1561 | (setq deferred-recalc nil) | |
1562 | (ses-update-cells old))) | |
1563 | (if deferred-write | |
1564 | ;;We don't reset the deferred list before starting -- the most | |
1565 | ;;likely error is keyboard-quit, and we do want to keep trying | |
1566 | ;;these writes after a quit. | |
1567 | (ses-write-cells)) | |
1568 | (when deferred-narrow | |
1569 | ;;We're not allowed to narrow the buffer until after-find-file has | |
1570 | ;;read the local variables at the end of the file. Now it's safe to | |
1571 | ;;do the narrowing. | |
1572 | (save-excursion | |
1573 | (goto-char 1) | |
1574 | (forward-line numrows) | |
1575 | (narrow-to-region 1 (point))) | |
1576 | (setq deferred-narrow nil)) | |
1577 | ;;Update the modeline | |
1578 | (let ((oldcell curcell)) | |
1579 | (ses-set-curcell) | |
1580 | (unless (eq curcell oldcell) | |
1581 | (cond | |
1582 | ((not curcell) | |
1583 | (setq mode-line-process nil)) | |
1584 | ((atom curcell) | |
1585 | (setq mode-line-process (list " cell " (symbol-name curcell)))) | |
1586 | (t | |
1587 | (setq mode-line-process (list " range " | |
1588 | (symbol-name (car curcell)) | |
1589 | "-" | |
1590 | (symbol-name (cdr curcell)))))) | |
1591 | (force-mode-line-update))) | |
1592 | ;;Use underline overlay for single-cells only, turn off otherwise | |
1593 | (if (listp curcell) | |
1594 | (move-overlay curcell-overlay 2 2) | |
1595 | (let ((next (next-single-property-change (point) 'intangible))) | |
1596 | (move-overlay curcell-overlay (point) (1- next)))) | |
1597 | (when (not (pos-visible-in-window-p)) | |
1598 | ;;Scrolling will happen later | |
1599 | (run-with-idle-timer 0.01 nil 'ses-command-hook) | |
1600 | (setq curcell t))) | |
1601 | ;;Prevent errors in this post-command-hook from silently erasing the hook! | |
1602 | (error | |
1603 | (unless executing-kbd-macro | |
1604 | (ding)) | |
1605 | (message (error-message-string err)))) | |
1606 | nil) ;Make coverage-tester happy | |
1607 | ||
1608 | (defun ses-create-header-string () | |
1609 | "Sets up `header-string' as the buffer's header line, based on the | |
1610 | current set of columns and window-scroll position." | |
1611 | (let ((totwidth (- 1 (window-hscroll))) | |
1612 | result width result x) | |
1613 | (if window-system | |
1614 | ;;Leave room for the left-side fringe | |
1615 | (push " " result)) | |
1616 | (dotimes (col numcols) | |
1617 | (setq width (ses-col-width col) | |
1618 | totwidth (+ totwidth width 1)) | |
1619 | (if (= totwidth 2) ;Scrolled so intercolumn space is leftmost | |
1620 | (push " " result)) | |
1621 | (when (> totwidth 2) | |
1622 | (if (> header-row 0) | |
1623 | (save-excursion | |
1624 | (ses-goto-print (1- header-row) col) | |
1625 | (setq x (buffer-substring-no-properties (point) | |
1626 | (+ (point) width))) | |
1627 | (if (>= width (1- totwidth)) | |
1628 | (setq x (substring x (- width totwidth -2)))) | |
1629 | (push (propertize x 'face ses-box-prop) result)) | |
1630 | (setq x (ses-column-letter col)) | |
1631 | (push (propertize x 'face ses-box-prop) result) | |
1632 | (push (propertize (make-string (- width (length x)) ?.) | |
1633 | 'display `((space :align-to ,(1- totwidth))) | |
1634 | 'face ses-box-prop) | |
1635 | result)) | |
1636 | ;;Allow the following space to be squished to make room for the 3-D box | |
1637 | ;;Coverage test ignores properties, thinks this is always a space! | |
1638 | (push (1value (propertize " " 'display `((space :align-to ,totwidth)))) | |
1639 | result))) | |
1640 | (if (> header-row 0) | |
1641 | (push (propertize (format " [row %d]" header-row) | |
1642 | 'display '((height (- 1)))) | |
1643 | result)) | |
1644 | (setq header-string (apply 'concat (nreverse result))))) | |
1645 | ||
1646 | ||
1647 | ;;;---------------------------------------------------------------------------- | |
1648 | ;;;; Redisplay and recalculation | |
1649 | ;;;---------------------------------------------------------------------------- | |
1650 | ||
1651 | (defun ses-jump (sym) | |
1652 | "Move point to cell SYM." | |
1653 | (interactive "SJump to cell: ") | |
1654 | (let ((rowcol (ses-sym-rowcol sym))) | |
1655 | (or rowcol (error "Invalid cell name")) | |
1656 | (if (eq (symbol-value sym) '*skip*) | |
1657 | (error "Cell is covered by preceding cell")) | |
1658 | (ses-goto-print (car rowcol) (cdr rowcol)))) | |
1659 | ||
1660 | (defun ses-jump-safe (cell) | |
1661 | "Like `ses-jump', but no error if invalid cell." | |
1662 | (condition-case nil | |
1663 | (ses-jump cell) | |
1664 | (error))) | |
1665 | ||
1666 | (defun ses-reprint-all (&optional nonarrow) | |
1667 | "Recreate the display area. Calls all printer functions. Narrows to | |
1668 | print area if NONARROW is nil." | |
1669 | (interactive "*P") | |
1670 | (widen) | |
1671 | (unless nonarrow | |
1672 | (setq deferred-narrow t)) | |
1673 | (let ((startcell (get-text-property (point) 'intangible)) | |
1674 | (inhibit-read-only t)) | |
1675 | (ses-begin-change) | |
1676 | (goto-char 1) | |
1677 | (search-forward ses-print-data-boundary) | |
1678 | (backward-char (length ses-print-data-boundary)) | |
1679 | (delete-region 1 (point)) | |
1680 | ;;Insert all blank lines before printing anything, so ses-print-cell can | |
1681 | ;;find the data area when inserting or deleting *skip* values for cells | |
1682 | (dotimes (row numrows) | |
1683 | (insert-and-inherit blank-line)) | |
1684 | (ses-dotimes-msg (row numrows) "Reprinting..." | |
1685 | (if (eq (ses-cell-value row 0) '*skip*) | |
1686 | ;;Column deletion left a dangling skip | |
1687 | (ses-set-cell row 0 'value nil)) | |
1688 | (dotimes (col numcols) | |
1689 | (ses-print-cell row col)) | |
1690 | (beginning-of-line 2)) | |
1691 | (ses-jump-safe startcell))) | |
1692 | ||
1693 | (defun ses-recalculate-cell () | |
1694 | "Recalculate and reprint the current cell or range. | |
1695 | ||
1696 | For an individual cell, shows the error if the formula or printer | |
1697 | signals one, or otherwise shows the cell's complete value. For a range, the | |
1698 | cells are recalculated in \"natural\" order, so cells that other cells refer | |
1699 | to are recalculated first." | |
1700 | (interactive "*") | |
1701 | (ses-check-curcell 'range) | |
1702 | (ses-begin-change) | |
1703 | (let (sig) | |
1704 | (setq ses-start-time (float-time)) | |
1705 | (if (atom curcell) | |
1706 | (setq sig (ses-sym-rowcol curcell) | |
1707 | sig (ses-calculate-cell (car sig) (cdr sig) t)) | |
1708 | ;;First, recalculate all cells that don't refer to other cells and | |
1709 | ;;produce a list of cells with references. | |
1710 | (ses-dorange curcell | |
1711 | (ses-time-check "Recalculating... %s" '(ses-cell-symbol row col)) | |
1712 | (condition-case nil | |
1713 | (progn | |
1714 | ;;The t causes an error if the cell has references. | |
1715 | ;;If no references, the t will be the result value. | |
1716 | (1value (ses-formula-references (ses-cell-formula row col) t)) | |
1717 | (setq sig (ses-calculate-cell row col t))) | |
1718 | (wrong-type-argument | |
1719 | ;;The formula contains a reference | |
1720 | (add-to-list 'deferred-recalc (ses-cell-symbol row col)))))) | |
1721 | ;;Do the update now, so we can force recalculation | |
1722 | (let ((x deferred-recalc)) | |
1723 | (setq deferred-recalc nil) | |
1724 | (condition-case hold | |
1725 | (ses-update-cells x t) | |
1726 | (error (setq sig hold)))) | |
1727 | (cond | |
1728 | (sig | |
1729 | (message (error-message-string sig))) | |
1730 | ((consp curcell) | |
1731 | (message " ")) | |
1732 | (t | |
1733 | (princ (symbol-value curcell)))))) | |
1734 | ||
1735 | (defun ses-recalculate-all () | |
1736 | "Recalculate and reprint all cells." | |
1737 | (interactive "*") | |
1738 | (let ((startcell (get-text-property (point) 'intangible)) | |
1739 | (curcell (cons 'A1 (ses-cell-symbol (1- numrows) (1- numcols))))) | |
1740 | (ses-recalculate-cell) | |
1741 | (ses-jump-safe startcell))) | |
1742 | ||
1743 | (defun ses-truncate-cell () | |
1744 | "Reprint current cell, but without spillover into any following blank | |
1745 | cells." | |
1746 | (interactive "*") | |
1747 | (ses-check-curcell) | |
1748 | (let* ((rowcol (ses-sym-rowcol curcell)) | |
1749 | (row (car rowcol)) | |
1750 | (col (cdr rowcol))) | |
1751 | (when (and (< col (1- numcols)) ;;Last column can't spill over, anyway | |
1752 | (eq (ses-cell-value row (1+ col)) '*skip*)) | |
1753 | ;;This cell has spill-over. We'll momentarily pretend the following | |
1754 | ;;cell has a `t' in it. | |
1755 | (eval `(let ((,(ses-cell-symbol row (1+ col)) t)) | |
1756 | (ses-print-cell row col))) | |
1757 | ;;Now remove the *skip*. ses-print-cell is always nil here | |
1758 | (ses-set-cell row (1+ col) 'value nil) | |
1759 | (1value (ses-print-cell row (1+ col)))))) | |
1760 | ||
1761 | (defun ses-reconstruct-all () | |
1762 | "Reconstruct buffer based on cell data stored in Emacs variables." | |
1763 | (interactive "*") | |
1764 | (ses-begin-change) | |
1765 | ;;Reconstruct reference lists. | |
1766 | (let (refs x yrow ycol) | |
1767 | ;;Delete old reference lists | |
1768 | (ses-dotimes-msg (row numrows) "Deleting references..." | |
1769 | (dotimes (col numcols) | |
1770 | (ses-set-cell row col 'references nil))) | |
1771 | ;;Create new reference lists | |
1772 | (ses-dotimes-msg (row numrows) "Computing references..." | |
1773 | (dotimes (col numcols) | |
1774 | (dolist (ref (ses-formula-references (ses-cell-formula row col))) | |
1775 | (setq x (ses-sym-rowcol ref) | |
1776 | yrow (car x) | |
1777 | ycol (cdr x)) | |
1778 | (ses-set-cell yrow ycol 'references | |
1779 | (cons (ses-cell-symbol row col) | |
1780 | (ses-cell-references yrow ycol))))))) | |
1781 | ;;Delete everything and reconstruct basic data area | |
1782 | (if (< (point-max) (buffer-size)) | |
1783 | (setq deferred-narrow t)) | |
1784 | (widen) | |
1785 | (let ((inhibit-read-only t)) | |
1786 | (goto-char (point-max)) | |
1787 | (if (search-backward ";;; Local Variables:\n" nil t) | |
1788 | (delete-region 1 (point)) | |
1789 | ;;Buffer is quite screwed up - can't even save the user-specified locals | |
1790 | (delete-region 1 (point-max)) | |
1791 | (insert ses-initial-file-trailer) | |
1792 | (goto-char 1)) | |
1793 | ;;Create a blank display area | |
1794 | (dotimes (row numrows) | |
1795 | (insert blank-line)) | |
1796 | (insert ses-print-data-boundary) | |
1797 | ;;Placeholders for cell data | |
1798 | (insert (make-string (* numrows (1+ numcols)) ?\n)) | |
1799 | ;;Placeholders for col-widths, col-printers, default-printer, header-row | |
1800 | (insert "\n\n\n\n") | |
1801 | (insert ses-initial-global-parameters)) | |
1802 | (ses-set-parameter 'column-widths column-widths) | |
1803 | (ses-set-parameter 'col-printers col-printers) | |
1804 | (ses-set-parameter 'default-printer default-printer) | |
1805 | (ses-set-parameter 'header-row header-row) | |
1806 | (ses-set-parameter 'numrows numrows) | |
1807 | (ses-set-parameter 'numcols numcols) | |
1808 | ;;Keep our old narrowing | |
1809 | (ses-setup) | |
1810 | (ses-recalculate-all) | |
1811 | (goto-char 1)) | |
1812 | ||
1813 | ||
1814 | ;;;---------------------------------------------------------------------------- | |
1815 | ;;;; Input of cell formulas | |
1816 | ;;;---------------------------------------------------------------------------- | |
1817 | ||
1818 | (defun ses-edit-cell (row col newval) | |
1819 | "Display current cell contents in minibuffer, for editing. Returns nil if | |
1820 | cell formula was unsafe and user declined confirmation." | |
1821 | (interactive | |
1822 | (progn | |
1823 | (barf-if-buffer-read-only) | |
1824 | (ses-check-curcell) | |
1825 | (let* ((rowcol (ses-sym-rowcol curcell)) | |
1826 | (row (car rowcol)) | |
1827 | (col (cdr rowcol)) | |
1828 | (formula (ses-cell-formula row col)) | |
1829 | initial) | |
1830 | (if (eq (car-safe formula) 'ses-safe-formula) | |
1831 | (setq formula (cadr formula))) | |
1832 | (if (eq (car-safe formula) 'quote) | |
1833 | (setq initial (format "'%S" (cadr formula))) | |
1834 | (setq initial (prin1-to-string formula))) | |
1835 | (if (stringp formula) | |
1836 | ;;Position cursor inside close-quote | |
1837 | (setq initial (cons initial (length initial)))) | |
1838 | (list row col | |
1839 | (read-from-minibuffer (format "Cell %s: " curcell) | |
1840 | initial | |
1841 | ses-mode-edit-map | |
1842 | t ;Convert to Lisp object | |
1843 | 'ses-read-cell-history))))) | |
1844 | (when (ses-warn-unsafe newval 'unsafep) | |
1845 | (ses-begin-change) | |
1846 | (ses-cell-set-formula row col newval) | |
1847 | t)) | |
1848 | ||
1849 | (defun ses-read-cell (row col newval) | |
1850 | "Self-insert for initial character of cell function." | |
1851 | (interactive | |
1852 | (let ((initial (this-command-keys)) | |
1853 | (rowcol (progn (ses-check-curcell) (ses-sym-rowcol curcell)))) | |
1854 | (barf-if-buffer-read-only) | |
1855 | (if (string= initial "\"") | |
1856 | (setq initial "\"\"") ;Enter a string | |
1857 | (if (string= initial "(") | |
1858 | (setq initial "()"))) ;Enter a formula list | |
1859 | (list (car rowcol) | |
1860 | (cdr rowcol) | |
1861 | (read-from-minibuffer (format "Cell %s: " curcell) | |
1862 | (cons initial 2) | |
1863 | ses-mode-edit-map | |
1864 | t ;Convert to Lisp object | |
1865 | 'ses-read-cell-history)))) | |
1866 | (when (ses-edit-cell row col newval) | |
1867 | (ses-command-hook) ;Update cell widths before movement | |
1868 | (dolist (x ses-after-entry-functions) | |
1869 | (funcall x 1)))) | |
1870 | ||
1871 | (defun ses-read-symbol (row col symb) | |
1872 | "Self-insert for a symbol as a cell formula. The set of all symbols that | |
1873 | have been used as formulas in this spreadsheet is available for completions." | |
1874 | (interactive | |
1875 | (let ((rowcol (progn (ses-check-curcell) (ses-sym-rowcol curcell))) | |
1876 | newval) | |
1877 | (barf-if-buffer-read-only) | |
1878 | (setq newval (completing-read (format "Cell %s ': " curcell) | |
1879 | symbolic-formulas)) | |
1880 | (list (car rowcol) | |
1881 | (cdr rowcol) | |
1882 | (if (string= newval "") | |
1883 | nil ;Don't create zero-length symbols! | |
1884 | (list 'quote (intern newval)))))) | |
1885 | (when (ses-edit-cell row col symb) | |
1886 | (ses-command-hook) ;Update cell widths before movement | |
1887 | (dolist (x ses-after-entry-functions) | |
1888 | (funcall x 1)))) | |
1889 | ||
1890 | (defun ses-clear-cell-forward (count) | |
1891 | "Delete formula and printer for current cell and then move to next cell. | |
1892 | With prefix, deletes several cells." | |
1893 | (interactive "*p") | |
1894 | (if (< count 0) | |
1895 | (1value (ses-clear-cell-backward (- count))) | |
1896 | (ses-check-curcell) | |
1897 | (ses-begin-change) | |
1898 | (dotimes (x count) | |
1899 | (ses-set-curcell) | |
1900 | (let ((rowcol (ses-sym-rowcol curcell))) | |
1901 | (or rowcol (signal 'end-of-buffer nil)) | |
1902 | (ses-clear-cell (car rowcol) (cdr rowcol))) | |
1903 | (forward-char 1)))) | |
1904 | ||
1905 | (defun ses-clear-cell-backward (count) | |
1906 | "Move to previous cell and then delete it. With prefix, deletes several | |
1907 | cells." | |
1908 | (interactive "*p") | |
1909 | (if (< count 0) | |
1910 | (1value (ses-clear-cell-forward (- count))) | |
1911 | (ses-check-curcell 'end) | |
1912 | (ses-begin-change) | |
1913 | (dotimes (x count) | |
1914 | (backward-char 1) ;Will signal 'beginning-of-buffer if appropriate | |
1915 | (ses-set-curcell) | |
1916 | (let ((rowcol (ses-sym-rowcol curcell))) | |
1917 | (ses-clear-cell (car rowcol) (cdr rowcol)))))) | |
1918 | ||
1919 | ||
1920 | ;;;---------------------------------------------------------------------------- | |
1921 | ;;;; Input of cell-printer functions | |
1922 | ;;;---------------------------------------------------------------------------- | |
1923 | ||
1924 | (defun ses-read-printer (prompt default) | |
1925 | "Common code for `ses-read-cell-printer', `ses-read-column-printer', and `ses-read-default-printer'. | |
1926 | PROMPT should end with \": \". Result is t if operation was cancelled." | |
1927 | (barf-if-buffer-read-only) | |
1928 | (if (eq default t) | |
1929 | (setq default "") | |
1930 | (setq prompt (format "%s [currently %S]: " | |
1931 | (substring prompt 0 -2) | |
1932 | default))) | |
1933 | (let ((new (read-from-minibuffer prompt | |
1934 | nil ;Initial contents | |
1935 | ses-mode-edit-map | |
1936 | t ;Evaluate the result | |
1937 | 'ses-read-printer-history | |
1938 | (prin1-to-string default)))) | |
1939 | (if (equal new default) | |
1940 | ;;User changed mind, decided not to change printer | |
1941 | (setq new t) | |
1942 | (ses-printer-validate new) | |
1943 | (or (not new) | |
1944 | (stringp new) | |
1945 | (stringp (car-safe new)) | |
1946 | (ses-warn-unsafe new 'unsafep-function) | |
1947 | (setq new t))) | |
1948 | new)) | |
1949 | ||
1950 | (defun ses-read-cell-printer (newval) | |
1951 | "Set the printer function for the current cell or range. | |
1952 | ||
1953 | A printer function is either a string (a format control-string with one | |
1954 | %-sequence -- result from format will be right-justified), or a list of one | |
1955 | string (result from format will be left-justified), or a lambda-expression of | |
1956 | one argument, or a symbol that names a function of one argument. In the | |
1957 | latter two cases, the function's result should be either a string (will be | |
1958 | right-justified) or a list of one string (will be left-justified)." | |
1959 | (interactive | |
1960 | (let ((default t) | |
1961 | prompt) | |
1962 | (ses-check-curcell 'range) | |
1963 | ;;Default is none if not all cells in range have same printer | |
1964 | (catch 'ses-read-cell-printer | |
1965 | (ses-dorange curcell | |
1966 | (setq x (ses-cell-printer row col)) | |
1967 | (if (eq (car-safe x) 'ses-safe-printer) | |
1968 | (setq x (cadr x))) | |
1969 | (if (eq default t) | |
1970 | (setq default x) | |
1971 | (unless (equal default x) | |
1972 | ;;Range contains differing printer functions | |
1973 | (setq default t) | |
1974 | (throw 'ses-read-cell-printer t))))) | |
1975 | (list (ses-read-printer (format "Cell %S printer: " curcell) default)))) | |
1976 | (unless (eq newval t) | |
1977 | (ses-begin-change) | |
1978 | (ses-dorange curcell | |
1979 | (ses-set-cell row col 'printer newval) | |
1980 | (ses-print-cell row col)))) | |
1981 | ||
1982 | (defun ses-read-column-printer (col newval) | |
1983 | "Set the printer function for the current column. See | |
1984 | `ses-read-cell-printer' for input forms." | |
1985 | (interactive | |
1986 | (let ((col (cdr (ses-sym-rowcol curcell)))) | |
1987 | (ses-check-curcell) | |
1988 | (list col (ses-read-printer (format "Column %s printer: " | |
1989 | (ses-column-letter col)) | |
1990 | (ses-col-printer col))))) | |
1991 | ||
1992 | (unless (eq newval t) | |
1993 | (ses-begin-change) | |
1994 | (ses-set-parameter 'col-printers newval col) | |
1995 | (save-excursion | |
1996 | (dotimes (row numrows) | |
1997 | (ses-print-cell row col))))) | |
1998 | ||
1999 | (defun ses-read-default-printer (newval) | |
2000 | "Set the default printer function for cells that have no other. See | |
2001 | `ses-read-cell-printer' for input forms." | |
2002 | (interactive | |
2003 | (list (ses-read-printer "Default printer: " default-printer))) | |
2004 | (unless (eq newval t) | |
2005 | (ses-begin-change) | |
2006 | (ses-set-parameter 'default-printer newval) | |
2007 | (ses-reprint-all t))) | |
2008 | ||
2009 | ||
2010 | ;;;---------------------------------------------------------------------------- | |
2011 | ;;;; Spreadsheet size adjustments | |
2012 | ;;;---------------------------------------------------------------------------- | |
2013 | ||
2014 | (defun ses-insert-row (count) | |
2015 | "Insert a new row before the current one. With prefix, insert COUNT rows | |
2016 | before current one." | |
2017 | (interactive "*p") | |
2018 | (ses-check-curcell 'end) | |
2019 | (or (> count 0) (signal 'args-out-of-range nil)) | |
2020 | (ses-begin-change) | |
2021 | (let ((inhibit-quit t) | |
2022 | (inhibit-read-only t) | |
2023 | (row (or (car (ses-sym-rowcol curcell)) numrows)) | |
2024 | newrow) | |
2025 | ;;Create a new set of cell-variables | |
2026 | (ses-create-cell-variable-range numrows (+ numrows count -1) | |
2027 | 0 (1- numcols)) | |
2028 | (ses-set-parameter 'numrows (+ numrows count)) | |
2029 | ;;Insert each row | |
2030 | (ses-goto-print row 0) | |
2031 | (ses-dotimes-msg (x count) "Inserting row..." | |
2032 | ;;Create a row of empty cells. The `symbol' fields will be set by | |
2033 | ;;the call to ses-relocate-all. | |
2034 | (setq newrow (make-vector numcols nil)) | |
2035 | (dotimes (col numcols) | |
2036 | (aset newrow col (make-vector ses-cell-size nil))) | |
2037 | (setq cells (ses-vector-insert cells row newrow)) | |
2038 | (push `(ses-vector-delete cells ,row 1) buffer-undo-list) | |
2039 | (insert blank-line)) | |
2040 | ;;Insert empty lines in cell data area (will be replaced by | |
2041 | ;;ses-relocate-all) | |
2042 | (ses-goto-data row 0) | |
2043 | (insert (make-string (* (1+ numcols) count) ?\n)) | |
2044 | (ses-relocate-all row 0 count 0) | |
2045 | ;;If any cell printers insert constant text, insert that text | |
2046 | ;;into the line. | |
2047 | (let ((cols (mapconcat #'ses-call-printer col-printers nil)) | |
2048 | (global (ses-call-printer default-printer))) | |
2049 | (if (or (> (length cols) 0) (> (length global) 0)) | |
2050 | (dotimes (x count) | |
2051 | (dotimes (col numcols) | |
2052 | ;;These cells are always nil, only constant formatting printed | |
2053 | (1value (ses-print-cell (+ x row) col)))))) | |
2054 | (when (> header-row row) | |
2055 | ;;Inserting before header | |
2056 | (ses-set-parameter 'header-row (+ header-row count)) | |
2057 | (ses-reset-header-string))) | |
2058 | ;;Reconstruct text attributes | |
2059 | (ses-setup) | |
2060 | ;;Return to current cell | |
2061 | (if curcell | |
2062 | (ses-jump-safe curcell) | |
2063 | (ses-goto-print (1- numrows) 0))) | |
2064 | ||
2065 | (defun ses-delete-row (count) | |
2066 | "Delete the current row. With prefix, Deletes COUNT rows starting from the | |
2067 | current one." | |
2068 | (interactive "*p") | |
2069 | (ses-check-curcell) | |
2070 | (or (> count 0) (signal 'args-out-of-range nil)) | |
2071 | (let ((inhibit-quit t) | |
2072 | (inhibit-read-only t) | |
2073 | (row (car (ses-sym-rowcol curcell))) | |
2074 | pos) | |
2075 | (setq count (min count (- numrows row))) | |
2076 | (ses-begin-change) | |
2077 | (ses-set-parameter 'numrows (- numrows count)) | |
2078 | ;;Delete lines from print area | |
2079 | (ses-goto-print row 0) | |
2080 | (ses-delete-line count) | |
2081 | ;;Delete lines from cell data area | |
2082 | (ses-goto-data row 0) | |
2083 | (ses-delete-line (* count (1+ numcols))) | |
2084 | ;;Relocate variables and formulas | |
2085 | (ses-set-with-undo 'cells (ses-vector-delete cells row count)) | |
2086 | (ses-relocate-all row 0 (- count) 0) | |
2087 | (ses-destroy-cell-variable-range numrows (+ numrows count -1) | |
2088 | 0 (1- numcols)) | |
2089 | (when (> header-row row) | |
2090 | (if (<= header-row (+ row count)) | |
2091 | ;;Deleting the header row | |
2092 | (ses-set-parameter 'header-row 0) | |
2093 | (ses-set-parameter 'header-row (- header-row count))) | |
2094 | (ses-reset-header-string))) | |
2095 | ;;Reconstruct attributes | |
2096 | (ses-setup) | |
2097 | (ses-jump-safe curcell)) | |
2098 | ||
2099 | (defun ses-insert-column (count &optional col width printer) | |
2100 | "Insert a new column before COL (default is the current one). With prefix, | |
2101 | insert COUNT columns before current one. If COL is specified, the new | |
2102 | column(s) get the specified WIDTH and PRINTER (otherwise they're taken from | |
2103 | the current column)." | |
2104 | (interactive "*p") | |
2105 | (ses-check-curcell) | |
2106 | (or (> count 0) (signal 'args-out-of-range nil)) | |
2107 | (or col | |
2108 | (setq col (cdr (ses-sym-rowcol curcell)) | |
2109 | width (ses-col-width col) | |
2110 | printer (ses-col-printer col))) | |
2111 | (ses-begin-change) | |
2112 | (let ((inhibit-quit t) | |
2113 | (inhibit-read-only t) | |
2114 | (widths column-widths) | |
2115 | (printers col-printers) | |
2116 | has-skip) | |
2117 | ;;Create a new set of cell-variables | |
2118 | (ses-create-cell-variable-range 0 (1- numrows) | |
2119 | numcols (+ numcols count -1)) | |
2120 | ;;Insert each column. | |
2121 | (ses-dotimes-msg (x count) "Inserting column..." | |
2122 | ;;Create a column of empty cells. The `symbol' fields will be set by | |
2123 | ;;the call to ses-relocate-all. | |
2124 | (ses-adjust-print-width col (1+ width)) | |
2125 | (ses-set-parameter 'numcols (1+ numcols)) | |
2126 | (dotimes (row numrows) | |
2127 | (and (< (1+ col) numcols) (eq (ses-cell-value row col) '*skip*) | |
2128 | ;;Inserting in the middle of a spill-over | |
2129 | (setq has-skip t)) | |
2130 | (ses-aset-with-undo cells row | |
2131 | (ses-vector-insert (aref cells row) | |
2132 | col | |
2133 | (make-vector ses-cell-size nil))) | |
2134 | ;;Insert empty lines in cell data area (will be replaced by | |
2135 | ;;ses-relocate-all) | |
2136 | (ses-goto-data row col) | |
2137 | (insert ?\n)) | |
2138 | ;;Insert column width and printer | |
2139 | (setq widths (ses-vector-insert widths col width) | |
2140 | printers (ses-vector-insert printers col printer))) | |
2141 | (ses-set-parameter 'column-widths widths) | |
2142 | (ses-set-parameter 'col-printers printers) | |
2143 | (ses-reset-header-string) | |
2144 | (ses-relocate-all 0 col 0 count) | |
2145 | (if has-skip | |
2146 | (ses-reprint-all t) | |
2147 | (when (or (> (length (ses-call-printer printer)) 0) | |
2148 | (> (length (ses-call-printer default-printer)) 0)) | |
2149 | ;;Either column printer or global printer inserts some constant text | |
2150 | ;;Reprint the new columns to insert that text. | |
2151 | (dotimes (x numrows) | |
2152 | (dotimes (y count) | |
2153 | ;Always nil here - this is a blank column | |
2154 | (1value (ses-print-cell-new-width x (+ y col)))))) | |
2155 | (ses-setup))) | |
2156 | (ses-jump-safe curcell)) | |
2157 | ||
2158 | (defun ses-delete-column (count) | |
2159 | "Delete the current column. With prefix, Deletes COUNT columns starting | |
2160 | from the current one." | |
2161 | (interactive "*p") | |
2162 | (ses-check-curcell) | |
2163 | (or (> count 0) (signal 'args-out-of-range nil)) | |
2164 | (let ((inhibit-quit t) | |
2165 | (inhibit-read-only t) | |
2166 | (rowcol (ses-sym-rowcol curcell)) | |
2167 | (width 0) | |
2168 | new col origrow has-skip) | |
2169 | (setq origrow (car rowcol) | |
2170 | col (cdr rowcol) | |
2171 | count (min count (- numcols col))) | |
2172 | (if (= count numcols) | |
2173 | (error "Can't delete all columns!")) | |
2174 | ;;Determine width of column(s) being deleted | |
2175 | (dotimes (x count) | |
2176 | (setq width (+ width (ses-col-width (+ col x)) 1))) | |
2177 | (ses-begin-change) | |
2178 | (ses-set-parameter 'numcols (- numcols count)) | |
2179 | (ses-adjust-print-width col (- width)) | |
2180 | (ses-dotimes-msg (row numrows) "Deleting column..." | |
2181 | ;;Delete lines from cell data area | |
2182 | (ses-goto-data row col) | |
2183 | (ses-delete-line count) | |
2184 | ;;Delete cells. Check if deletion area begins or ends with a skip. | |
2185 | (if (or (eq (ses-cell-value row col) '*skip*) | |
2186 | (and (< col numcols) | |
2187 | (eq (ses-cell-value row (+ col count)) '*skip*))) | |
2188 | (setq has-skip t)) | |
2189 | (ses-aset-with-undo cells row | |
2190 | (ses-vector-delete (aref cells row) col count))) | |
2191 | ;;Update globals | |
2192 | (ses-set-parameter 'column-widths | |
2193 | (ses-vector-delete column-widths col count)) | |
2194 | (ses-set-parameter 'col-printers | |
2195 | (ses-vector-delete col-printers col count)) | |
2196 | (ses-reset-header-string) | |
2197 | ;;Relocate variables and formulas | |
2198 | (ses-relocate-all 0 col 0 (- count)) | |
2199 | (ses-destroy-cell-variable-range 0 (1- numrows) | |
2200 | numcols (+ numcols count -1)) | |
2201 | (if has-skip | |
2202 | (ses-reprint-all t) | |
2203 | (ses-setup)) | |
2204 | (if (>= col numcols) | |
2205 | (setq col (1- col))) | |
2206 | (ses-goto-print origrow col))) | |
2207 | ||
2208 | (defun ses-forward-or-insert (&optional count) | |
2209 | "Move to next cell in row, or inserts a new cell if already in last one, or | |
2210 | inserts a new row if at bottom of print area. Repeat COUNT times." | |
2211 | (interactive "p") | |
2212 | (ses-check-curcell 'end) | |
2213 | (setq deactivate-mark t) ;Doesn't combine well with ranges | |
2214 | (dotimes (x count) | |
2215 | (ses-set-curcell) | |
2216 | (if (not curcell) | |
2217 | (progn ;At bottom of print area | |
2218 | (barf-if-buffer-read-only) | |
2219 | (ses-insert-row 1)) | |
2220 | (let ((col (cdr (ses-sym-rowcol curcell)))) | |
2221 | (when (/= 32 | |
2222 | (char-before (next-single-property-change (point) | |
2223 | 'intangible))) | |
2224 | ;;We're already in last nonskipped cell on line. Need to create a | |
2225 | ;;new column. | |
2226 | (barf-if-buffer-read-only) | |
2227 | (ses-insert-column (- count x) | |
2228 | numcols | |
2229 | (ses-col-width col) | |
2230 | (ses-col-printer col))))) | |
2231 | (forward-char))) | |
2232 | ||
2233 | (defun ses-append-row-jump-first-column () | |
2234 | "Insert a new row after current one and jumps to its first column." | |
2235 | (interactive "*") | |
2236 | (ses-check-curcell) | |
2237 | (ses-begin-change) | |
2238 | (beginning-of-line 2) | |
2239 | (ses-set-curcell) | |
2240 | (ses-insert-row 1)) | |
2241 | ||
2242 | (defun ses-set-column-width (col newwidth) | |
2243 | "Set the width of the current column." | |
2244 | (interactive | |
2245 | (let ((col (cdr (progn (ses-check-curcell) (ses-sym-rowcol curcell))))) | |
2246 | (barf-if-buffer-read-only) | |
2247 | (list col | |
2248 | (if current-prefix-arg | |
2249 | (prefix-numeric-value current-prefix-arg) | |
2250 | (read-from-minibuffer (format "Column %s width [currently %d]: " | |
2251 | (ses-column-letter col) | |
2252 | (ses-col-width col)) | |
2253 | nil ;No initial contents | |
2254 | nil ;No override keymap | |
2255 | t ;Convert to Lisp object | |
2256 | nil ;No history | |
2257 | (number-to-string | |
2258 | (ses-col-width col))))))) ;Default value | |
2259 | (if (< newwidth 1) | |
2260 | (error "Invalid column width")) | |
2261 | (ses-begin-change) | |
2262 | (ses-reset-header-string) | |
2263 | (save-excursion | |
2264 | (let ((inhibit-quit t)) | |
2265 | (ses-adjust-print-width col (- newwidth (ses-col-width col))) | |
2266 | (ses-set-parameter 'column-widths newwidth col)) | |
2267 | (dotimes (row numrows) | |
2268 | (ses-print-cell-new-width row col)))) | |
2269 | ||
2270 | ||
2271 | ;;;---------------------------------------------------------------------------- | |
2272 | ;;;; Cut and paste, import and export | |
2273 | ;;;---------------------------------------------------------------------------- | |
2274 | ||
2275 | (defadvice copy-region-as-kill (around ses-copy-region-as-kill | |
2276 | activate preactivate) | |
2277 | "It doesn't make sense to copy read-only or intangible attributes into the | |
2278 | kill ring. It probably doesn't make sense to copy keymap properties. | |
2279 | We'll assume copying front-sticky properties doesn't make sense, either. | |
2280 | ||
2281 | This advice also includes some SES-specific code because otherwise it's too | |
2282 | hard to override how mouse-1 works." | |
2283 | (when (> beg end) | |
2284 | (let ((temp beg)) | |
2285 | (setq beg end | |
2286 | end temp))) | |
2287 | (if (not (and (eq major-mode 'ses-mode) | |
2288 | (eq (get-text-property beg 'read-only) 'ses) | |
2289 | (eq (get-text-property (1- end) 'read-only) 'ses))) | |
2290 | ad-do-it ;Normal copy-region-as-kill | |
2291 | (kill-new (ses-copy-region beg end)))) | |
2292 | ||
2293 | (defun ses-copy-region (beg end) | |
2294 | "Treat the region as rectangular. Convert the intangible attributes to | |
2295 | SES attributes recording the contents of the cell as of the time of copying." | |
2296 | (let* ((inhibit-point-motion-hooks t) | |
2297 | (x (mapconcat 'ses-copy-region-helper | |
2298 | (extract-rectangle beg (1- end)) "\n"))) | |
2299 | (remove-text-properties 0 (length x) | |
2300 | '(read-only t | |
2301 | intangible t | |
2302 | keymap t | |
2303 | front-sticky t) | |
2304 | x) | |
2305 | x)) | |
2306 | ||
2307 | (defun ses-copy-region-helper (line) | |
2308 | "Converts one line (of a rectangle being extracted from a spreadsheet) to | |
2309 | external form by attaching to each print cell a 'ses attribute that records | |
2310 | the corresponding data cell." | |
2311 | (or (> (length line) 1) | |
2312 | (error "Empty range")) | |
2313 | (let ((inhibit-read-only t) | |
2314 | (pos 0) | |
2315 | mycell next sym rowcol) | |
2316 | (while pos | |
2317 | (setq sym (get-text-property pos 'intangible line) | |
2318 | next (next-single-property-change pos 'intangible line) | |
2319 | rowcol (ses-sym-rowcol sym) | |
2320 | mycell (ses-get-cell (car rowcol) (cdr rowcol))) | |
2321 | (put-text-property pos (or next (length line)) | |
2322 | 'ses | |
2323 | (list (ses-cell-symbol mycell) | |
2324 | (ses-cell-formula mycell) | |
2325 | (ses-cell-printer mycell)) | |
2326 | line) | |
2327 | (setq pos next))) | |
2328 | line) | |
2329 | ||
2330 | (defun ses-kill-override (beg end) | |
2331 | "Generic override for any commands that kill text. We clear the killed | |
2332 | cells instead of deleting them." | |
2333 | (interactive "r") | |
2334 | (ses-check-curcell 'needrange) | |
2335 | ;;For some reason, the text-read-only error is not caught by | |
2336 | ;;`delete-region', so we have to use subterfuge. | |
2337 | (let ((buffer-read-only t)) | |
2338 | (1value (condition-case x | |
2339 | (noreturn (funcall (lookup-key (current-global-map) | |
2340 | (this-command-keys)) | |
2341 | beg end)) | |
2342 | (buffer-read-only nil)))) ;The expected error | |
2343 | ;;Because the buffer was marked read-only, the kill command turned itself | |
2344 | ;;into a copy. Now we clear the cells or signal the error. First we | |
2345 | ;;check whether the buffer really is read-only. | |
2346 | (barf-if-buffer-read-only) | |
2347 | (ses-begin-change) | |
2348 | (ses-dorange curcell | |
2349 | (ses-clear-cell row col)) | |
2350 | (ses-jump (car curcell))) | |
2351 | ||
2352 | (defadvice yank (around ses-yank activate preactivate) | |
2353 | "In SES mode, the yanked text is inserted as cells. | |
2354 | ||
2355 | If the text contains 'ses attributes (meaning it went to the kill-ring from a | |
2356 | SES buffer), the formulas and print functions are restored for the cells. If | |
2357 | the text contains tabs, this is an insertion of tab-separated formulas. | |
2358 | Otherwise the text is inserted as the formula for the current cell. | |
2359 | ||
2360 | When inserting cells, the formulas are usually relocated to keep the same | |
2361 | relative references to neighboring cells. This is best if the formulas | |
2362 | generally refer to other cells within the yanked text. You can use the C-u | |
2363 | prefix to specify insertion without relocation, which is best when the | |
2364 | formulas refer to cells outsite the yanked text. | |
2365 | ||
2366 | When inserting formulas, the text is treated as a string constant if it doesn't | |
2367 | make sense as a sexp or would otherwise be considered a symbol. Use 'sym to | |
2368 | explicitly insert a symbol, or use the C-u prefix to treat all unmarked words | |
2369 | as symbols." | |
2370 | (if (not (and (eq major-mode 'ses-mode) | |
2371 | (eq (get-text-property (point) 'keymap) 'ses-mode-print-map))) | |
2372 | ad-do-it ;Normal non-SES yank | |
2373 | (ses-check-curcell 'end) | |
2374 | (push-mark (point)) | |
2375 | (let ((text (current-kill (cond | |
2376 | ((listp arg) 0) | |
2377 | ((eq arg '-) -1) | |
2378 | (t (1- arg)))))) | |
2379 | (or (ses-yank-cells text arg) | |
2380 | (ses-yank-tsf text arg) | |
2381 | (ses-yank-one (ses-yank-resize 1 1) | |
2382 | text | |
2383 | 0 | |
2384 | (if (memq (aref text (1- (length text))) '(?\t ?\n)) | |
2385 | ;;Just one cell - delete final tab or newline | |
2386 | (1- (length text))) | |
2387 | arg))) | |
2388 | (if (consp arg) | |
2389 | (exchange-point-and-mark)))) | |
2390 | ||
2391 | (defun ses-yank-pop (arg) | |
2392 | "Replace just-yanked stretch of killed text with a different stretch. | |
2393 | This command is allowed only immediately after a `yank' or a `yank-pop', when | |
2394 | the region contains a stretch of reinserted previously-killed text. We | |
2395 | replace it with a different stretch of killed text. | |
2396 | Unlike standard `yank-pop', this function uses `undo' to delete the | |
2397 | previous insertion." | |
2398 | (interactive "*p") | |
2399 | (or (eq last-command 'yank) | |
2400 | ;;Use noreturn here just to avoid a "poor-coverage" warning in its | |
2401 | ;;macro definition. | |
2402 | (noreturn (error "Previous command was not a yank"))) | |
2403 | (undo) | |
2404 | (ses-set-curcell) | |
2405 | (yank (1+ (or arg 1))) | |
2406 | (setq this-command 'yank)) | |
2407 | ||
2408 | (defun ses-yank-cells (text arg) | |
2409 | "If the TEXT has a proper set of 'ses attributes, inserts the text as | |
2410 | cells, else return nil. The cells are reprinted--the supplied text is | |
2411 | ignored because the column widths, default printer, etc. at yank time might | |
2412 | be different from those at kill-time. ARG is a list to indicate that | |
2413 | formulas are to be inserted without relocation." | |
2414 | (let ((first (get-text-property 0 'ses text)) | |
2415 | (last (get-text-property (1- (length text)) 'ses text))) | |
2416 | (when (and first last) ;;Otherwise not proper set of attributes | |
2417 | (setq first (ses-sym-rowcol (car first)) | |
2418 | last (ses-sym-rowcol (car last))) | |
2419 | (let* ((needrows (- (car last) (car first) -1)) | |
2420 | (needcols (- (cdr last) (cdr first) -1)) | |
2421 | (rowcol (ses-yank-resize needrows needcols)) | |
2422 | (rowincr (- (car rowcol) (car first))) | |
2423 | (colincr (- (cdr rowcol) (cdr first))) | |
2424 | (pos 0) | |
2425 | myrow mycol x) | |
2426 | (ses-dotimes-msg (row needrows) "Yanking..." | |
2427 | (setq myrow (+ row (car rowcol))) | |
2428 | (dotimes (col needcols) | |
2429 | (setq mycol (+ col (cdr rowcol)) | |
2430 | last (get-text-property pos 'ses text) | |
2431 | pos (next-single-property-change pos 'ses text) | |
2432 | x (ses-sym-rowcol (car last))) | |
2433 | (if (not last) | |
2434 | ;;Newline - all remaining cells on row are skipped | |
2435 | (setq x (cons (- myrow rowincr) (+ needcols colincr -1)) | |
2436 | last (list nil nil nil) | |
2437 | pos (1- pos))) | |
2438 | (if (/= (car x) (- myrow rowincr)) | |
2439 | (error "Cell row error")) | |
2440 | (if (< (- mycol colincr) (cdr x)) | |
2441 | ;;Some columns were skipped | |
2442 | (let ((oldcol mycol)) | |
2443 | (while (< (- mycol colincr) (cdr x)) | |
2444 | (ses-clear-cell myrow mycol) | |
2445 | (setq col (1+ col) | |
2446 | mycol (1+ mycol))) | |
2447 | (ses-print-cell myrow (1- oldcol)))) ;;This inserts *skip* | |
2448 | (when (car last) ;Skip this for *skip* cells | |
2449 | (setq x (nth 2 last)) | |
2450 | (unless (equal x (ses-cell-printer myrow mycol)) | |
2451 | (or (not x) | |
2452 | (stringp x) | |
2453 | (eq (car-safe x) 'ses-safe-printer) | |
2454 | (setq x `(ses-safe-printer ,x))) | |
2455 | (ses-set-cell myrow mycol 'printer x)) | |
2456 | (setq x (cadr last)) | |
2457 | (if (atom arg) | |
2458 | (setq x (ses-relocate-formula x 0 0 rowincr colincr))) | |
2459 | (or (atom x) | |
2460 | (eq (car-safe x) 'ses-safe-formula) | |
2461 | (setq x `(ses-safe-formula ,x))) | |
2462 | (ses-cell-set-formula myrow mycol x))) | |
2463 | (when pos | |
2464 | (if (get-text-property pos 'ses text) | |
2465 | (error "Missing newline between rows")) | |
2466 | (setq pos (next-single-property-change pos 'ses text)))) | |
2467 | t)))) | |
2468 | ||
2469 | (defun ses-yank-one (rowcol text from to arg) | |
2470 | "Insert the substring [FROM,TO] of TEXT as the formula for cell ROWCOL (a | |
2471 | cons of ROW and COL). Treat plain symbols as strings unless ARG is a list." | |
2472 | (let ((val (condition-case nil | |
2473 | (read-from-string text from to) | |
2474 | (error (cons nil from))))) | |
2475 | (cond | |
2476 | ((< (cdr val) (or to (length text))) | |
2477 | ;;Invalid sexp - leave it as a string | |
2478 | (setq val (substring text from to))) | |
2479 | ((and (car val) (symbolp (car val))) | |
2480 | (if (consp arg) | |
2481 | (setq val (list 'quote (car val))) ;Keep symbol | |
2482 | (setq val (substring text from to)))) ;Treat symbol as text | |
2483 | (t | |
2484 | (setq val (car val)))) | |
2485 | (let ((row (car rowcol)) | |
2486 | (col (cdr rowcol))) | |
2487 | (or (atom val) | |
2488 | (setq val `(ses-safe-formula ,val))) | |
2489 | (ses-cell-set-formula row col val)))) | |
2490 | ||
2491 | (defun ses-yank-tsf (text arg) | |
2492 | "If TEXT contains tabs and/or newlines, treats the tabs as | |
2493 | column-separators and the newlines as row-separators and inserts the text as | |
2494 | cell formulas--else return nil. Treat plain symbols as strings unless ARG | |
2495 | is a list. Ignore a final newline." | |
2496 | (if (or (not (string-match "[\t\n]" text)) | |
2497 | (= (match-end 0) (length text))) | |
2498 | ;;Not TSF format | |
2499 | nil | |
2500 | (if (/= (aref text (1- (length text))) ?\n) | |
2501 | (setq text (concat text "\n"))) | |
2502 | (let ((pos -1) | |
2503 | (spots (list -1)) | |
2504 | (cols 0) | |
2505 | (needrows 0) | |
2506 | needcols rowcol) | |
2507 | ;;Find all the tabs and newlines | |
2508 | (while (setq pos (string-match "[\t\n]" text (1+ pos))) | |
2509 | (push pos spots) | |
2510 | (setq cols (1+ cols)) | |
2511 | (when (eq (aref text pos) ?\n) | |
2512 | (if (not needcols) | |
2513 | (setq needcols cols) | |
2514 | (or (= needcols cols) | |
2515 | (error "Inconsistent row lengths"))) | |
2516 | (setq cols 0 | |
2517 | needrows (1+ needrows)))) | |
2518 | ;;Insert the formulas | |
2519 | (setq rowcol (ses-yank-resize needrows needcols)) | |
2520 | (dotimes (row needrows) | |
2521 | (dotimes (col needcols) | |
2522 | (ses-yank-one (cons (+ (car rowcol) needrows (- row) -1) | |
2523 | (+ (cdr rowcol) needcols (- col) -1)) | |
2524 | text (1+ (cadr spots)) (car spots) arg) | |
2525 | (setq spots (cdr spots)))) | |
2526 | (ses-goto-print (+ (car rowcol) needrows -1) | |
2527 | (+ (cdr rowcol) needcols -1)) | |
2528 | t))) | |
2529 | ||
2530 | (defun ses-yank-resize (needrows needcols) | |
2531 | "If this yank will require inserting rows and/or columns, asks for | |
2532 | confirmation and then inserts them. Result is (row,col) for top left of yank | |
2533 | spot, or error signal if user requests cancel." | |
2534 | (ses-begin-change) | |
2535 | (let ((rowcol (if curcell (ses-sym-rowcol curcell) (cons numrows 0))) | |
2536 | rowbool colbool) | |
2537 | (setq needrows (- (+ (car rowcol) needrows) numrows) | |
2538 | needcols (- (+ (cdr rowcol) needcols) numcols) | |
2539 | rowbool (> needrows 0) | |
2540 | colbool (> needcols 0)) | |
2541 | (when (or rowbool colbool) | |
2542 | ;;Need to insert. Get confirm | |
2543 | (or (y-or-n-p (format "Yank will insert %s%s%s. Continue " | |
2544 | (if rowbool (format "%d rows" needrows) "") | |
2545 | (if (and rowbool colbool) " and " "") | |
2546 | (if colbool (format "%d columns" needcols) ""))) | |
2547 | (error "Cancelled")) | |
2548 | (when rowbool | |
2549 | (let (curcell) | |
2550 | (save-excursion | |
2551 | (ses-goto-print numrows 0) | |
2552 | (ses-insert-row needrows)))) | |
2553 | (when colbool | |
2554 | (ses-insert-column needcols | |
2555 | numcols | |
2556 | (ses-col-width (1- numcols)) | |
2557 | (ses-col-printer (1- numcols))))) | |
2558 | rowcol)) | |
2559 | ||
2560 | (defun ses-export-tsv (beg end) | |
2561 | "Export values from the current range, with tabs between columns and | |
2562 | newlines between rows. Result is placed in kill ring." | |
2563 | (interactive "r") | |
2564 | (ses-export-tab nil)) | |
2565 | ||
2566 | (defun ses-export-tsf (beg end) | |
2567 | "Export formulas from the current range, with tabs between columns and | |
2568 | newlines between rows. Result is placed in kill ring." | |
2569 | (interactive "r") | |
2570 | (ses-export-tab t)) | |
2571 | ||
2572 | (defun ses-export-tab (want-formulas) | |
2573 | "Export the current range with tabs between columns and newlines between | |
2574 | rows. Result is placed in kill ring. The export is values unless | |
2575 | WANT-FORMULAS is non-nil. Newlines and tabs in the export text are escaped." | |
2576 | (ses-check-curcell 'needrange) | |
2577 | (let ((print-escape-newlines t) | |
2578 | result item) | |
2579 | (ses-dorange curcell | |
2580 | (setq item (if want-formulas | |
2581 | (ses-cell-formula row col) | |
2582 | (ses-cell-value row col))) | |
2583 | (if (eq (car-safe item) 'ses-safe-formula) | |
2584 | ;;Hide our deferred safety-check marker | |
2585 | (setq item (cadr item))) | |
2586 | (if (or (not item) (eq item '*skip*)) | |
2587 | (setq item "")) | |
2588 | (when (eq (car-safe item) 'quote) | |
2589 | (push "'" result) | |
2590 | (setq item (cadr item))) | |
2591 | (setq item (prin1-to-string item t)) | |
2592 | (setq item (replace-regexp-in-string "\t" "\\\\t" item)) | |
2593 | (push item result) | |
2594 | (cond | |
2595 | ((< col maxcol) | |
2596 | (push "\t" result)) | |
2597 | ((< row maxrow) | |
2598 | (push "\n" result)))) | |
2599 | (setq result (apply 'concat (nreverse result))) | |
2600 | (kill-new result))) | |
2601 | ||
2602 | ||
2603 | ;;;---------------------------------------------------------------------------- | |
2604 | ;;;; Other user commands | |
2605 | ;;;---------------------------------------------------------------------------- | |
2606 | ||
2607 | (defun ses-read-header-row (row) | |
2608 | (interactive "NHeader row: ") | |
2609 | (if (or (< row 0) (> row numrows)) | |
2610 | (error "Invalid header-row")) | |
2611 | (ses-begin-change) | |
2612 | (ses-set-parameter 'header-row row) | |
2613 | (ses-reset-header-string)) | |
2614 | ||
2615 | (defun ses-mark-row () | |
2616 | "Marks the entirety of current row as a range." | |
2617 | (interactive) | |
2618 | (ses-check-curcell 'range) | |
2619 | (let ((row (car (ses-sym-rowcol (or (car-safe curcell) curcell))))) | |
2620 | (push-mark (point)) | |
2621 | (ses-goto-print (1+ row) 0) | |
2622 | (push-mark (point) nil t) | |
2623 | (ses-goto-print row 0))) | |
2624 | ||
2625 | (defun ses-mark-column () | |
2626 | "Marks the entirety of current column as a range." | |
2627 | (interactive) | |
2628 | (ses-check-curcell 'range) | |
2629 | (let ((col (cdr (ses-sym-rowcol (or (car-safe curcell) curcell)))) | |
2630 | (row 0)) | |
2631 | (push-mark (point)) | |
2632 | (ses-goto-print (1- numrows) col) | |
2633 | (forward-char 1) | |
2634 | (push-mark (point) nil t) | |
2635 | (while (eq '*skip* (ses-cell-value row col)) | |
2636 | ;;Skip over initial cells in column that can't be selected | |
2637 | (setq row (1+ row))) | |
2638 | (ses-goto-print row col))) | |
2639 | ||
2640 | (defun ses-end-of-line () | |
2641 | "Move point to last cell on line." | |
2642 | (interactive) | |
2643 | (ses-check-curcell 'end 'range) | |
2644 | (when curcell ;Otherwise we're at the bottom row, which is empty anyway | |
2645 | (let ((col (1- numcols)) | |
2646 | row rowcol) | |
2647 | (if (symbolp curcell) | |
2648 | ;;Single cell | |
2649 | (setq row (car (ses-sym-rowcol curcell))) | |
2650 | ;;Range - use whichever end of the range the point is at | |
2651 | (setq rowcol (ses-sym-rowcol (if (< (point) (mark)) | |
2652 | (car curcell) | |
2653 | (cdr curcell)))) | |
2654 | ;;If range already includes the last cell in a row, point is actually | |
2655 | ;;in the following row | |
2656 | (if (<= (cdr rowcol) (1- col)) | |
2657 | (setq row (car rowcol)) | |
2658 | (setq row (1+ (car rowcol))) | |
2659 | (if (= row numrows) | |
2660 | ;;Already at end - can't go anywhere | |
2661 | (setq col 0)))) | |
2662 | (when (< row numrows) ;Otherwise it's a range that includes last cell | |
2663 | (while (eq (ses-cell-value row col) '*skip*) | |
2664 | ;;Back to beginning of multi-column cell | |
2665 | (setq col (1- col))) | |
2666 | (ses-goto-print row col))))) | |
2667 | ||
2668 | (defun ses-renarrow-buffer () | |
2669 | "Narrow the buffer so only the print area is visible. Use after \\[widen]." | |
2670 | (interactive) | |
2671 | (setq deferred-narrow t)) | |
2672 | ||
2673 | (defun ses-sort-column (sorter &optional reverse) | |
2674 | "Sorts the range by a specified column. With prefix, sorts in | |
2675 | REVERSE order." | |
2676 | (interactive "*sSort column: \nP") | |
2677 | (ses-check-curcell 'needrange) | |
2678 | (let ((min (ses-sym-rowcol (car curcell))) | |
2679 | (max (ses-sym-rowcol (cdr curcell)))) | |
2680 | (let ((minrow (car min)) | |
2681 | (mincol (cdr min)) | |
2682 | (maxrow (car max)) | |
2683 | (maxcol (cdr max)) | |
2684 | keys extracts end) | |
2685 | (setq sorter (cdr (ses-sym-rowcol (intern (concat sorter "1"))))) | |
2686 | (or (and sorter (>= sorter mincol) (<= sorter maxcol)) | |
2687 | (error "Invalid sort column")) | |
2688 | ;;Get key columns and sort them | |
2689 | (dotimes (x (- maxrow minrow -1)) | |
2690 | (ses-goto-print (+ minrow x) sorter) | |
2691 | (setq end (next-single-property-change (point) 'intangible)) | |
2692 | (push (cons (buffer-substring-no-properties (point) end) | |
2693 | (+ minrow x)) | |
2694 | keys)) | |
2695 | (setq keys (sort keys #'(lambda (x y) (string< (car x) (car y))))) | |
2696 | ;;Extract the lines in reverse sorted order | |
2697 | (or reverse | |
2698 | (setq keys (nreverse keys))) | |
2699 | (dolist (x keys) | |
2700 | (ses-goto-print (cdr x) (1+ maxcol)) | |
2701 | (setq end (point)) | |
2702 | (ses-goto-print (cdr x) mincol) | |
2703 | (push (ses-copy-region (point) end) extracts)) | |
2704 | (deactivate-mark) | |
2705 | ;;Paste the lines sequentially | |
2706 | (dotimes (x (- maxrow minrow -1)) | |
2707 | (ses-goto-print (+ minrow x) mincol) | |
2708 | (ses-set-curcell) | |
2709 | (ses-yank-cells (pop extracts) nil))))) | |
2710 | ||
2711 | (defun ses-sort-column-click (event reverse) | |
2712 | (interactive "*e\nP") | |
2713 | (setq event (event-end event)) | |
2714 | (select-window (posn-window event)) | |
2715 | (setq event (car (posn-col-row event))) ;Click column | |
2716 | (let ((col 0)) | |
2717 | (while (and (< col numcols) (> event (ses-col-width col))) | |
2718 | (setq event (- event (ses-col-width col) 1) | |
2719 | col (1+ col))) | |
2720 | (if (>= col numcols) | |
2721 | (ding) | |
2722 | (ses-sort-column (ses-column-letter col) reverse)))) | |
2723 | ||
2724 | (defun ses-insert-range () | |
2725 | "Inserts into minibuffer the list of cells currently highlighted in the | |
2726 | spreadsheet." | |
2727 | (interactive "*") | |
2728 | (let (x) | |
2729 | (with-current-buffer (window-buffer minibuffer-scroll-window) | |
2730 | (ses-command-hook) ;For ses-coverage | |
2731 | (ses-check-curcell 'needrange) | |
2732 | (setq x (cdr (macroexpand `(ses-range ,(car curcell) ,(cdr curcell)))))) | |
2733 | (insert (substring (prin1-to-string (nreverse x)) 1 -1)))) | |
2734 | ||
2735 | (defun ses-insert-ses-range () | |
2736 | "Inserts \"(ses-range x y)\" in the minibuffer to represent the currently | |
2737 | highlighted range in the spreadsheet." | |
2738 | (interactive "*") | |
2739 | (let (x) | |
2740 | (with-current-buffer (window-buffer minibuffer-scroll-window) | |
2741 | (ses-command-hook) ;For ses-coverage | |
2742 | (ses-check-curcell 'needrange) | |
2743 | (setq x (format "(ses-range %S %S)" (car curcell) (cdr curcell)))) | |
2744 | (insert x))) | |
2745 | ||
2746 | (defun ses-insert-range-click (event) | |
2747 | "Mouse version of `ses-insert-range'." | |
2748 | (interactive "*e") | |
2749 | (mouse-set-point event) | |
2750 | (ses-insert-range)) | |
2751 | ||
2752 | (defun ses-insert-ses-range-click (event) | |
2753 | "Mouse version of `ses-insert-ses-range'." | |
2754 | (interactive "*e") | |
2755 | (mouse-set-point event) | |
2756 | (ses-insert-ses-range)) | |
2757 | ||
2758 | ||
2759 | ;;;---------------------------------------------------------------------------- | |
2760 | ;;;; Checking formulas for safety | |
2761 | ;;;---------------------------------------------------------------------------- | |
2762 | ||
2763 | (defun ses-safe-printer (printer) | |
2764 | "Returns PRINTER if safe, or the substitute printer `ses-unsafe' otherwise." | |
2765 | (if (or (stringp printer) | |
2766 | (stringp (car-safe printer)) | |
2767 | (not printer) | |
2768 | (ses-warn-unsafe printer 'unsafep-function)) | |
2769 | printer | |
2770 | 'ses-unsafe)) | |
2771 | ||
2772 | (defun ses-safe-formula (formula) | |
2773 | "Returns FORMULA if safe, or the substitute formula *unsafe* otherwise." | |
2774 | (if (ses-warn-unsafe formula 'unsafep) | |
2775 | formula | |
2776 | `(ses-unsafe ',formula))) | |
2777 | ||
2778 | (defun ses-warn-unsafe (formula checker) | |
2779 | "Applies CHECKER to FORMULA. If result is non-nil, asks user for | |
2780 | confirmation about FORMULA, which might be unsafe. Returns t if formula | |
2781 | is safe or user allows execution anyway. Always returns t if | |
2782 | `safe-functions' is t." | |
2783 | (if (eq safe-functions t) | |
2784 | t | |
2785 | (setq checker (funcall checker formula)) | |
2786 | (if (not checker) | |
2787 | t | |
2788 | (y-or-n-p (format "Formula %S\nmight be unsafe %S. Process it? " | |
2789 | formula checker))))) | |
2790 | ||
2791 | ||
2792 | ;;;---------------------------------------------------------------------------- | |
2793 | ;;;; Standard formulas | |
2794 | ;;;---------------------------------------------------------------------------- | |
2795 | ||
2796 | (defmacro ses-range (from to) | |
2797 | "Expands to a list of cell-symbols for the range. The range automatically | |
2798 | expands to include any new row or column inserted into its middle. The SES | |
2799 | library code specifically looks for the symbol `ses-range', so don't create an | |
2800 | alias for this macro!" | |
2801 | (let (result) | |
2802 | (ses-dorange (cons from to) | |
2803 | (push (ses-cell-symbol row col) result)) | |
2804 | (cons 'list result))) | |
2805 | ||
2806 | (defun ses-delete-blanks (&rest args) | |
2807 | "Return ARGS reversed, with the blank elements (nil and *skip*) removed." | |
2808 | (let (result) | |
2809 | (dolist (cur args) | |
2810 | (and cur (not (eq cur '*skip*)) | |
2811 | (push cur result))) | |
2812 | result)) | |
2813 | ||
2814 | (defun ses+ (&rest args) | |
2815 | "Compute the sum of the arguments, ignoring blanks." | |
2816 | (apply '+ (apply 'ses-delete-blanks args))) | |
2817 | ||
2818 | (defun ses-average (list) | |
2819 | "Computes the sum of the numbers in LIST, divided by their length. Blanks | |
2820 | are ignored. Result is always floating-point, even if all args are integers." | |
2821 | (setq list (apply 'ses-delete-blanks list)) | |
2822 | (/ (float (apply '+ list)) (length list))) | |
2823 | ||
2824 | (defmacro ses-select (fromrange test torange) | |
2825 | "Select cells in FROMRANGE that are `equal' to TEST. For each match, return | |
2826 | the corresponding cell from TORANGE. The ranges are macroexpanded but not | |
2827 | evaluated so they should be either (ses-range BEG END) or (list ...). The | |
2828 | TEST is evaluated." | |
2829 | (setq fromrange (cdr (macroexpand fromrange)) | |
2830 | torange (cdr (macroexpand torange)) | |
2831 | test (eval test)) | |
2832 | (or (= (length fromrange) (length torange)) | |
2833 | (error "ses-select: Ranges not same length")) | |
2834 | (let (result) | |
2835 | (dolist (x fromrange) | |
2836 | (if (equal test (symbol-value x)) | |
2837 | (push (car torange) result)) | |
2838 | (setq torange (cdr torange))) | |
2839 | (cons 'list result))) | |
2840 | ||
2841 | ;;All standard formulas are safe | |
2842 | (dolist (x '(ses-range ses-delete-blanks ses+ ses-average ses-select)) | |
2843 | (put x 'side-effect-free t)) | |
2844 | ||
2845 | ||
2846 | ;;;---------------------------------------------------------------------------- | |
2847 | ;;;; Standard print functions | |
2848 | ;;;---------------------------------------------------------------------------- | |
2849 | ||
2850 | ;;These functions use the variables 'row' and 'col' that are | |
2851 | ;;dynamically bound by ses-print-cell. We define these varables at | |
2852 | ;;compile-time to make the compiler happy. | |
2853 | (eval-when-compile | |
2854 | (make-local-variable 'row) | |
2855 | (make-local-variable 'col) | |
2856 | ;;Don't use setq -- that gives a "free variable" compiler warning | |
2857 | (set 'row nil) | |
2858 | (set 'col nil)) | |
2859 | ||
2860 | (defun ses-center (value &optional span fill) | |
2861 | "Print VALUE, centered within column. FILL is the fill character for | |
2862 | centering (default = space). SPAN indicates how many additional rightward | |
2863 | columns to include in width (default = 0)." | |
2864 | (let ((printer (or (ses-col-printer col) default-printer)) | |
2865 | (width (ses-col-width col)) | |
2866 | half) | |
2867 | (or fill (setq fill ? )) | |
2868 | (or span (setq span 0)) | |
2869 | (setq value (ses-call-printer printer value)) | |
2870 | (dotimes (x span) | |
2871 | (setq width (+ width 1 (ses-col-width (+ col span (- x)))))) | |
2872 | (setq width (- width (length value))) | |
2873 | (if (<= width 0) | |
2874 | value ;Too large for field, anyway | |
2875 | (setq half (make-string (/ width 2) fill)) | |
2876 | (concat half value half | |
2877 | (if (> (% width 2) 0) (char-to-string fill)))))) | |
2878 | ||
2879 | (defun ses-center-span (value &optional fill) | |
2880 | "Print VALUE, centered within the span that starts in the current column | |
2881 | and continues until the next nonblank column. FILL specifies the fill | |
2882 | character (default = space)." | |
2883 | (let ((end (1+ col))) | |
2884 | (while (and (< end numcols) | |
2885 | (memq (ses-cell-value row end) '(nil *skip*))) | |
2886 | (setq end (1+ end))) | |
2887 | (ses-center value (- end col 1) fill))) | |
2888 | ||
2889 | (defun ses-dashfill (value &optional span) | |
2890 | "Print VALUE centered using dashes. SPAN indicates how many rightward | |
2891 | columns to include in width (default = 0)." | |
2892 | (ses-center value span ?-)) | |
2893 | ||
2894 | (defun ses-dashfill-span (value) | |
2895 | "Print VALUE, centered using dashes within the span that starts in the | |
2896 | current column and continues until the next nonblank column." | |
2897 | (ses-center-span value ?-)) | |
2898 | ||
2899 | (defun ses-tildefill-span (value) | |
2900 | "Print VALUE, centered using tildes within the span that starts in the | |
2901 | current column and continues until the next nonblank column." | |
2902 | (ses-center-span value ?~)) | |
2903 | ||
2904 | (defun ses-unsafe (value) | |
2905 | "Substitute for an unsafe formula or printer" | |
2906 | (error "Unsafe formula or printer")) | |
2907 | ||
2908 | ;;All standard printers are safe, including ses-unsafe! | |
2909 | (dolist (x (cons 'ses-unsafe ses-standard-printer-functions)) | |
2910 | (put x 'side-effect-free t)) | |
2911 | ||
2912 | (provide 'ses) | |
2913 | ||
2914 | ;; ses.el ends here. |