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