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