declare smobs in alloc.c
[bpt/emacs.git] / doc / misc / ses.texi
1 \input texinfo @c -*- mode: texinfo; coding: utf-8; -*-
2 @c %**start of header
3 @setfilename ../../info/ses.info
4 @settitle @acronym{SES}: Simple Emacs Spreadsheet
5 @setchapternewpage off
6 @syncodeindex fn cp
7 @syncodeindex vr cp
8 @syncodeindex ky cp
9 @documentencoding UTF-8
10 @c %**end of header
11
12 @copying
13 This file documents @acronym{SES}: the Simple Emacs Spreadsheet.
14
15 Copyright @copyright{} 2002--2014 Free Software Foundation, Inc.
16
17 @quotation
18 Permission is granted to copy, distribute and/or modify this document
19 under the terms of the GNU Free Documentation License, Version 1.3 or
20 any later version published by the Free Software Foundation; with no
21 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
22 and with the Back-Cover Texts as in (a) below. A copy of the license
23 is included in the section entitled ``GNU Free Documentation License.''
24
25 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
26 modify this GNU manual.''
27 @end quotation
28 @end copying
29
30 @dircategory Emacs misc features
31 @direntry
32 * @acronym{SES}: (ses). Simple Emacs Spreadsheet.
33 @end direntry
34
35 @finalout
36
37 @titlepage
38 @title @acronym{SES}
39 @subtitle Simple Emacs Spreadsheet
40 @author Jonathan A. Yavner
41 @author @email{jyavner@@member.fsf.org}
42
43 @page
44 @vskip 0pt plus 1filll
45 @insertcopying
46 @end titlepage
47
48 @contents
49
50 @c ===================================================================
51
52 @ifnottex
53 @node Top
54 @comment node-name, next, previous, up
55 @top @acronym{SES}: Simple Emacs Spreadsheet
56
57 @display
58 @acronym{SES} is a major mode for GNU Emacs to edit spreadsheet files, which
59 contain a rectangular grid of cells. The cells' values are specified
60 by formulas that can refer to the values of other cells.
61 @end display
62 @end ifnottex
63
64 To report bugs, use @kbd{M-x report-emacs-bug}.
65
66 @insertcopying
67
68 @menu
69 * Sales Pitch:: Why use @acronym{SES}?
70 * Quick Tutorial:: A quick introduction
71 * The Basics:: Basic spreadsheet commands
72 * Advanced Features:: Want to know more?
73 * For Gurus:: Want to know @emph{even more}?
74 * Index:: Concept, Function and Variable Index
75 * Acknowledgments:: Acknowledgments
76 * GNU Free Documentation License:: The license for this documentation.
77 @end menu
78
79 @c ===================================================================
80
81 @node Sales Pitch
82 @comment node-name, next, previous, up
83 @chapter Sales Pitch
84 @cindex features
85
86 @itemize @bullet
87 @item Create and edit simple spreadsheets with a minimum of fuss.
88 @item Full undo/redo/autosave.
89 @item Immune to viruses in spreadsheet files.
90 @item Cell formulas are straight Emacs Lisp.
91 @item Printer functions for control of cell appearance.
92 @item Intuitive keystroke commands: C-o = insert row, M-o = insert column, etc.
93 @item ``Spillover'' of lengthy cell values into following blank cells.
94 @item Header line shows column letters or a selected row.
95 @item Completing-read for entering symbols as cell values.
96 @item Cut, copy, and paste can transfer formulas and printer functions.
97 @item Import and export of tab-separated values or tab-separated formulas.
98 @item Plaintext, easily-hacked file format.
99 @end itemize
100
101 @c ===================================================================
102
103 @node Quick Tutorial
104 @chapter Quick Tutorial
105 @cindex introduction
106 @cindex tutorial
107
108 If you want to get started quickly and think that you know what to
109 expect from a simple spreadsheet, this chapter may be all that you
110 need.
111
112 First, visit a new file with the @file{.ses} extension.
113 Emacs presents you with an empty spreadsheet containing a single cell.
114
115 Begin by inserting a headline: @kbd{"Income"@key{RET}}. The double
116 quotes indicate that this is a text cell. (Notice that Emacs
117 automatically inserts the closing quotation mark.)
118
119 To insert your first income value, you must first resize the
120 spreadsheet. Press @key{TAB} to add a new cell and navigate back up
121 to it. Enter a number, such as @samp{2.23}. Then proceed to add a
122 few more income entries, e.g.:
123
124 @example
125 @group
126 A
127 Income
128 2.23
129 0.02
130 15.76
131 -4.00
132 @end group
133 @end example
134
135 To add up the values, enter a Lisp expression:
136
137 @example
138 (+ A2 A3 A4 A5)
139 @end example
140
141 Perhaps you want to add a cell to the right of cell A4 to explain
142 why you have a negative entry. Pressing @kbd{TAB} in that cell
143 adds an entire new column @samp{B}, where you can add such a note.
144
145 The column is fairly narrow by default, but pressing @kbd{w} allows
146 you to resize it as needed. Make it 20 characters wide. You can
147 now add descriptive legends for all the entries, e.g.:
148
149 @example
150 @group
151 A B
152 Income
153 2.23 Consulting fee
154 0.02 Informed opinion
155 15.76 Lemonade stand
156 -4 Loan to Joe
157 14.01 Total
158 @end group
159 @end example
160
161 By default, the labels in column B are right-justified. To change
162 that, you can enter a printer function for the whole column, using
163 e.g., @kbd{M-p ("%s")}. You can override a column's printer function
164 in any individual cell using @kbd{p}.
165
166 If Joe pays back his loan, you might blank that entry; e.g., by
167 positioning the cursor in cell A5 and pressing @kbd{C-d} twice.
168 If you do that, the total cell will display @samp{######}. That is
169 because the regular @code{+} operator does not handle a range that
170 contains some empty cells. Instead of emptying the cell, you could
171 enter a literal @samp{0}, or delete the entire row using @kbd{C-k}.
172 An alternative is to use the special function @code{ses+} instead of
173 the regular @code{+}:
174
175 @example
176 (ses+ A2 A3 A4 A5)
177 @end example
178
179 To make a formula robust against changes in the spreadsheet geometry,
180 you can use the @code{ses-range} macro to refer to a range of cells by
181 the end-points, e.g.:
182
183 @example
184 (apply 'ses+ (ses-range A2 A5))
185 @end example
186
187 (The @code{apply} is necessary because @code{ses-range} produces a
188 @emph{list} of values. This allows for more complex possibilities.)
189
190 @c ===================================================================
191
192 @node The Basics
193 @comment node-name, next, previous, up
194 @chapter The Basics
195 @cindex basic commands
196 @findex ses-jump
197 @findex ses-mark-row
198 @findex ses-mark-column
199 @findex ses-mark-whole-buffer
200 @findex set-mark-command
201 @findex keyboard-quit
202
203 To create a new spreadsheet, visit a nonexistent file whose name ends
204 with ".ses". For example, @kbd{C-x C-f test.ses RET}.
205
206
207 A @dfn{cell identifier} is a symbol with a column letter and a row
208 number. Cell B7 is the 2nd column of the 7th row. For very wide
209 spreadsheets, there are two column letters: cell AB7 is the 28th
210 column of the 7th row. Super wide spreadsheets get AAA1, etc.
211 You move around with the regular Emacs movement commands.
212
213 @table @kbd
214 @item j
215 Moves point to cell, specified by identifier (@code{ses-jump}).
216 @end table
217
218 Point is always at the left edge of a cell, or at the empty endline.
219 When mark is inactive, the current cell is underlined. When mark is
220 active, the range is the highlighted rectangle of cells (@acronym{SES} always
221 uses transient mark mode). Drag the mouse from A1 to A3 to create the
222 range A1-A2. Many @acronym{SES} commands operate only on single cells, not
223 ranges.
224
225 @table @kbd
226 @item C-@key{SPC}
227 @itemx C-@@
228 Set mark at point (@code{set-mark-command}).
229
230 @item C-g
231 Turn off the mark (@code{keyboard-quit}).
232
233 @item M-h
234 Highlight current row (@code{ses-mark-row}).
235
236 @item S-M-h
237 Highlight current column (@code{ses-mark-column}).
238
239 @item C-x h
240 Highlight all cells (@code{mark-whole-buffer}).
241 @end table
242
243 @menu
244 * Formulas::
245 * Resizing::
246 * Printer functions::
247 * Clearing cells::
248 * Copy/cut/paste::
249 * Customizing @acronym{SES}::
250 @end menu
251
252 @node Formulas
253 @section Cell formulas
254 @cindex formulas
255 @cindex formulas, entering
256 @cindex values
257 @cindex cell values
258 @cindex editing cells
259 @findex ses-read-cell
260 @findex ses-read-symbol
261 @findex ses-edit-cell
262 @findex ses-recalculate-cell
263 @findex ses-recalculate-all
264
265 To insert a value into a cell, simply type a numeric expression,
266 @samp{"double-quoted text"}, or a Lisp expression.
267
268 @table @kbd
269 @item 0..9
270 Self-insert a digit (@code{ses-read-cell}).
271
272 @item -
273 Self-insert a negative number (@code{ses-read-cell}).
274
275 @item .
276 Self-insert a fractional number (@code{ses-read-cell}).
277
278 @item "
279 Self-insert a quoted string. The ending double-quote
280 is inserted for you (@code{ses-read-cell}).
281
282 @item (
283 Self-insert an expression. The right-parenthesis is inserted for you
284 (@code{ses-read-cell}). To access another cell's value, just use its
285 identifier in your expression. Whenever the other cell is changed,
286 this cell's formula will be reevaluated. While typing in the
287 expression, you can use @kbd{M-@key{TAB}} to complete symbol names.
288
289 @item ' @r{(apostrophe)}
290 Enter a symbol (ses-read-symbol). @acronym{SES} remembers all symbols that have
291 been used as formulas, so you can type just the beginning of a symbol
292 and use @kbd{@key{SPC}}, @kbd{@key{TAB}}, and @kbd{?} to complete it.
293 @end table
294
295 To enter something else (e.g., a vector), begin with a digit, then
296 erase the digit and type whatever you want.
297
298 @table @kbd
299 @item RET
300 Edit the existing formula in the current cell (@code{ses-edit-cell}).
301
302 @item C-c C-c
303 Force recalculation of the current cell or range (@code{ses-recalculate-cell}).
304
305 @item C-c C-l
306 Recalculate the entire spreadsheet (@code{ses-recalculate-all}).
307 @end table
308
309 @node Resizing
310 @section Resizing the spreadsheet
311 @cindex resizing spreadsheets
312 @cindex dimensions
313 @cindex row, adding or removing
314 @cindex column, adding or removing
315 @cindex adding rows or columns
316 @cindex inserting rows or columns
317 @cindex removing rows or columns
318 @cindex deleting rows or columns
319 @findex ses-insert-row
320 @findex ses-insert-column
321 @findex ses-delete-row
322 @findex ses-delete-column
323 @findex ses-set-column-width
324 @findex ses-forward-or-insert
325 @findex ses-append-row-jump-first-column
326
327
328 Basic commands:
329
330 @table @kbd
331 @item C-o
332 (@code{ses-insert-row})
333
334 @item M-o
335 (@code{ses-insert-column})
336
337 @item C-k
338 (@code{ses-delete-row})
339
340 @item M-k
341 (@code{ses-delete-column})
342
343 @item w
344 (@code{ses-set-column-width})
345
346 @item TAB
347 Moves point to the next rightward cell, or inserts a new column if
348 already at last cell on line, or inserts a new row if at endline
349 (@code{ses-forward-or-insert}).
350
351 @item C-j
352 Linefeed inserts below the current row and moves to column A
353 (@code{ses-append-row-jump-first-column}).
354 @end table
355
356 Resizing the spreadsheet (unless you're just changing a column width)
357 relocates all the cell-references in formulas so they still refer to
358 the same cells. If a formula mentioned B1 and you insert a new first
359 row, the formula will now mention B2.
360
361 If you delete a cell that a formula refers to, the cell-symbol is
362 deleted from the formula, so @code{(+ A1 B1 C1)} after deleting the third
363 column becomes @code{(+ A1 B1)}. In case this is not what you wanted:
364
365 @table @kbd
366 @item C-_
367 @itemx C-x u
368 Undo previous action (@code{(undo)}).
369 @end table
370
371
372 @node Printer functions
373 @section Printer functions
374 @cindex printer functions
375 @cindex cell formatting
376 @cindex formatting cells
377 @findex ses-read-cell-printer
378 @findex ses-read-column-printer
379 @findex ses-read-default-printer
380 @findex ses-center
381 @findex ses-center-span
382 @findex ses-dashfill
383 @findex ses-dashfill-span
384 @findex ses-tildefill-span
385
386
387 Printer functions convert binary cell values into the print forms that
388 Emacs will display on the screen.
389
390 A printer can be a format string, like @samp{"$%.2f"}. The result
391 string is right-aligned within the print cell. To get left-alignment,
392 use parentheses: @samp{("$%.2f")}. A printer can also be a
393 one-argument function (a symbol or a lambda), whose result is a string
394 (right-aligned) or list of one string (left-aligned). While typing in
395 a lambda, you can use @kbd{M-@key{TAB}} to complete the names of symbols.
396
397 Each cell has a printer. If @code{nil}, the column-printer for the cell's
398 column is used. If that is also @code{nil}, the default-printer for the
399 spreadsheet is used.
400
401 @table @kbd
402 @item p
403 Enter a printer for current cell or range (@code{ses-read-cell-printer}).
404
405 @item M-p
406 Enter a printer for the current column (@code{ses-read-column-printer}).
407
408 @item C-c C-p
409 Enter the default printer for the spreadsheet
410 (@code{ses-read-default-printer}).
411 @end table
412
413 The @code{ses-read-@r{XXX}-printer} commands have their own minibuffer
414 history, which is preloaded with the set of all printers used in this
415 spreadsheet, plus the standard printers.
416
417 The standard printers are suitable only for cells, not columns or
418 default, because they format the value using the column-printer (or
419 default-printer if @code{nil}) and then center the result:
420
421 @table @code
422 @item ses-center
423 Just centering.
424
425 @item ses-center-span
426 Centering with spill-over to following blank cells.
427
428 @item ses-dashfill
429 Centering using dashes (-) instead of spaces.
430
431 @item ses-dashfill-span
432 Centering with dashes and spill-over.
433
434 @item ses-tildefill-span
435 Centering with tildes (~) and spill-over.
436 @end table
437
438 You can define printer function local to a sheet with command
439 @code{ses-define-local-printer}. For instance define printer
440 @samp{foo} to @code{"%.2f"} and then use symbol @samp{foo} as a
441 printer function. Then, if you call again
442 @code{ses-define-local-printer} on @samp{foo} to redefine it as
443 @code{"%.3f"} all the cells using printer @samp{foo} will be reprinted
444 accordingly.
445
446 @node Clearing cells
447 @section Clearing cells
448 @cindex clearing commands
449 @findex ses-clear-cell-backward
450 @findex ses-clear-cell-forward
451
452 These commands set both formula and printer to @code{nil}:
453
454 @table @kbd
455 @item DEL
456 Clear cell and move left (@code{ses-clear-cell-backward}).
457
458 @item C-d
459 Clear cell and move right (@code{ses-clear-cell-forward}).
460 @end table
461
462
463 @node Copy/cut/paste
464 @section Copy, cut, and paste
465 @cindex copy
466 @cindex cut
467 @cindex paste
468 @findex kill-ring-save
469 @findex mouse-set-region
470 @findex mouse-set-secondary
471 @findex ses-kill-override
472 @findex yank
473 @findex clipboard-yank
474 @findex mouse-yank-at-click
475 @findex mouse-yank-at-secondary
476 @findex ses-yank-pop
477
478 The copy functions work on rectangular regions of cells. You can paste the
479 copies into non-@acronym{SES} buffers to export the print text.
480
481 @table @kbd
482 @item M-w
483 @itemx [copy]
484 @itemx [C-insert]
485 Copy the highlighted cells to kill ring and primary clipboard
486 (@code{kill-ring-save}).
487
488 @item [drag-mouse-1]
489 Mark a region and copy it to kill ring and primary clipboard
490 (@code{mouse-set-region}).
491
492 @item [M-drag-mouse-1]
493 Mark a region and copy it to kill ring and secondary clipboard
494 (@code{mouse-set-secondary}).
495
496 @item C-w
497 @itemx [cut]
498 @itemx [S-delete]
499 The cut functions do not actually delete rows or columns---they copy
500 and then clear (@code{ses-kill-override}).
501
502 @item C-y
503 @itemx [S-insert]
504 Paste from kill ring (@code{yank}). The paste functions behave
505 differently depending on the format of the text being inserted:
506 @itemize @bullet
507 @item
508 When pasting cells that were cut from a @acronym{SES} buffer, the print text is
509 ignored and only the attached formula and printer are inserted; cell
510 references in the formula are relocated unless you use @kbd{C-u}.
511 @item
512 The pasted text overwrites a rectangle of cells whose top left corner
513 is the current cell. If part of the rectangle is beyond the edges of
514 the spreadsheet, you must confirm the increase in spreadsheet size.
515 @item
516 Non-@acronym{SES} text is usually inserted as a replacement formula for the
517 current cell. If the formula would be a symbol, it's treated as a
518 string unless you use @kbd{C-u}. Pasted formulas with syntax errors
519 are always treated as strings.
520 @end itemize
521
522 @item [paste]
523 Paste from primary clipboard or kill ring (@code{clipboard-yank}).
524
525 @item [mouse-2]
526 Set point and paste from primary clipboard (@code{mouse-yank-at-click}).
527
528 @item [M-mouse-2]
529 Set point and paste from secondary clipboard (@code{mouse-yank-secondary}).
530
531 @item M-y
532 Immediately after a paste, you can replace the text with a preceding
533 element from the kill ring (@code{ses-yank-pop}). Unlike the standard
534 Emacs yank-pop, the @acronym{SES} version uses @code{undo} to delete the old
535 yank. This doesn't make any difference?
536 @end table
537
538 @node Customizing @acronym{SES}
539 @section Customizing @acronym{SES}
540 @cindex customizing
541 @vindex enable-local-eval
542 @vindex ses-mode-hook
543 @vindex safe-functions
544 @vindex enable-local-eval
545
546
547 By default, a newly-created spreadsheet has 1 row and 1 column. The
548 column width is 7 and the default printer is @samp{"%.7g"}. Each of these
549 can be customized. Look in group ``ses''.
550
551 After entering a cell value, point normally moves right to the next
552 cell. You can customize @code{ses-after-entry-functions} to move left or
553 up or down. For diagonal movement, select two functions from the
554 list.
555
556 @code{ses-mode-hook} is a normal mode hook (list of functions to
557 execute when starting @acronym{SES} mode for a buffer).
558
559 The variable @code{safe-functions} is a list of possibly-unsafe
560 functions to be treated as safe when analyzing formulas and printers.
561 @xref{Virus protection}. Before customizing @code{safe-functions},
562 think about how much you trust the person who's suggesting this
563 change. The value @code{t} turns off all anti-virus protection. A
564 list-of-functions value might enable a ``gee whiz'' spreadsheet, but it
565 also creates trapdoors in your anti-virus armor. In order for virus
566 protection to work, you must always press @kbd{n} when presented with
567 a virus warning, unless you understand what the questionable code is
568 trying to do. Do not listen to those who tell you to customize
569 @code{enable-local-eval}---this variable is for people who don't wear
570 safety belts!
571
572
573 @c ===================================================================
574
575 @node Advanced Features
576 @chapter Advanced Features
577 @cindex advanced features
578 @findex ses-read-header-row
579
580
581 @table @kbd
582 @item C-c M-C-h
583 (@code{ses-set-header-row}).
584 @findex ses-set-header-row
585 @kindex C-c M-C-h
586 The header line at the top of the @acronym{SES}
587 window normally shows the column letter for each column. You can set
588 it to show a copy of some row, such as a row of column titles, so that
589 row will always be visible. Default is to set the current row as the
590 header; use C-u to prompt for header row. Set the header to row 0 to
591 show column letters again.
592 @item [header-line mouse-3]
593 Pops up a menu to set the current row as the header, or revert to
594 column letters.
595 @item M-x ses-rename-cell
596 @findex ses-rename-cell
597 Rename a cell from a standard A1-like name to any
598 string.
599 @item M-x ses-repair-cell-reference-all
600 @findex ses-repair-cell-reference-all
601 When you interrupt a cell formula update by clicking @kbd{C-g}, then
602 the cell reference link may be broken, which will jeopardize automatic
603 cell update when any other cell on which it depends is changed. To
604 repair that use function @code{ses-repair-cell-reference-all}
605 @end table
606
607 @menu
608 * The print area::
609 * Ranges in formulas::
610 * Sorting by column::
611 * Standard formula functions::
612 * More on cell printing::
613 * Import and export::
614 * Virus protection::
615 * Spreadsheets with details and summary::
616 @end menu
617
618 @node The print area
619 @section The print area
620 @cindex print area
621 @findex widen
622 @findex ses-renarrow-buffer
623 @findex ses-reprint-all
624
625 A @acronym{SES} file consists of a print area and a data area. Normally the
626 buffer is narrowed to show only the print area. The print area is
627 read-only except for special @acronym{SES} commands; it contains cell values
628 formatted by printer functions. The data area records the formula and
629 printer functions, etc.
630
631 @table @kbd
632 @item C-x n w
633 Show print and data areas (@code{widen}).
634
635 @item C-c C-n
636 Show only print area (@code{ses-renarrow-buffer}).
637
638 @item S-C-l
639 @itemx M-C-l
640 Recreate print area by reevaluating printer functions for all cells
641 (@code{ses-reprint-all}).
642 @end table
643
644 @node Ranges in formulas
645 @section Ranges in formulas
646 @cindex ranges
647 @findex ses-insert-range-click
648 @findex ses-insert-range
649 @findex ses-insert-ses-range-click
650 @findex ses-insert-ses-range
651 @vindex from
652 @vindex to
653
654 A formula like
655 @lisp
656 (+ A1 A2 A3)
657 @end lisp
658 is the sum of three specific cells. If you insert a new second row,
659 the formula becomes
660 @lisp
661 (+ A1 A3 A4)
662 @end lisp
663 and the new row is not included in the sum.
664
665 The macro @code{(ses-range @var{from} @var{to})} evaluates to a list of
666 the values in a rectangle of cells. If your formula is
667 @lisp
668 (apply '+ (ses-range A1 A3))
669 @end lisp
670 and you insert a new second row, it becomes
671 @lisp
672 (apply '+ (ses-range A1 A4))
673 @end lisp
674 and the new row is included in the sum.
675
676 While entering or editing a formula in the minibuffer, you can select
677 a range in the spreadsheet (using mouse or keyboard), then paste a
678 representation of that range into your formula. Suppose you select
679 A1-C1:
680
681 @table @kbd
682 @item [S-mouse-3]
683 Inserts "A1 B1 C1" @code{(ses-insert-range-click})
684
685 @item C-c C-r
686 Keyboard version (@code{ses-insert-range}).
687
688 @item [C-S-mouse-3]
689 Inserts "(ses-range A1 C1)" (@code{ses-insert-ses-range-click}).
690
691 @item C-c C-s
692 Keyboard version (@code{ses-insert-ses-range}).
693 @end table
694
695 If you delete the @var{from} or @var{to} cell for a range, the nearest
696 still-existing cell is used instead. If you delete the entire range,
697 the formula relocator will delete the ses-range from the formula.
698
699 If you insert a new row just beyond the end of a one-column range, or
700 a new column just beyond a one-row range, the new cell is included in
701 the range. New cells inserted just before a range are not included.
702
703 Flags can be added to @code{ses-range} immediately after the @var{to}
704 cell.
705 @table @code
706 @item !
707 Empty cells in range can be removed by adding the @code{!} flag. An
708 empty cell is a cell the value of which is one of symbols @code{nil}
709 or @code{*skip*}. For instance @code{(ses-range A1 A4 !)} will do the
710 same as @code{(list A1 A3)} when cells @code{A2} and @code{A4} are
711 empty.
712 @item _
713 Empty cell values are replaced by the argument following flag
714 @code{_}, or @code{0} when flag @code{_} is last in argument list. For
715 instance @code{(ses-range A1 A4 _ "empty")} will do the same as
716 @code{(list A1 "empty" A3 "empty")} when cells @code{A2} and @code{A4}
717 are empty. Similarly, @code{(ses-range A1 A4 _ )} will do the same as
718 @code{(list A1 0 A3 0)}.
719 @item >v
720 When order matters, list cells by reading cells row-wise from top left
721 to bottom right. This flag is provided for completeness only as it is
722 the default reading order.
723 @item <v
724 List cells by reading cells row-wise from top right to bottom left.
725 @item v>
726 List cells by reading cells column-wise from top left to bottom right.
727 @item v<
728 List cells by reading cells column-wise from top right to bottom left.
729 @item v
730 A short hand for @code{v>}.
731 @item ^
732 A short hand for @code{^>}.
733 @item >
734 A short hand for @code{>v}.
735 @item <
736 A short hand for @code{>^}.
737 @item *
738 Instead of listing cells, it makes a Calc vector or matrix of it
739 (@pxref{Top,,,calc,GNU Emacs Calc Manual}). If the range contains only
740 one row or one column a vector is made, otherwise a matrix is made.
741 @item *2
742 Same as @code{*} except that a matrix is always made even when there
743 is only one row or column in the range.
744 @item *1
745 Same as @code{*} except that a vector is always made even when there
746 is only one row or column in the range, that is to say the
747 corresponding matrix is flattened.
748 @end table
749
750 @node Sorting by column
751 @section Sorting by column
752 @cindex sorting
753 @findex ses-sort-column
754 @findex ses-sort-column-click
755
756 @table @kbd
757 @item C-c M-C-s
758 Sort the cells of a range using one of the columns
759 (@code{ses-sort-column}). The rows (or partial rows if the range
760 doesn't include all columns) are rearranged so the chosen column will
761 be in order.
762
763 @item [header-line mouse-2]
764 The easiest way to sort is to click mouse-2 on the chosen column's header row
765 (@code{ses-sort-column-click}).
766 @end table
767
768 The sort comparison uses @code{string<}, which works well for
769 right-justified numbers and left-justified strings.
770
771 With prefix arg, sort is in descending order.
772
773 Rows are moved one at a time, with relocation of formulas. This works
774 well if formulas refer to other cells in their row, not so well for
775 formulas that refer to other rows in the range or to cells outside the
776 range.
777
778
779 @node Standard formula functions
780 @section Standard formula functions
781 @cindex standard formula functions
782 @cindex *skip*
783 @cindex *error*
784 @findex ses-delete-blanks
785 @findex ses-average
786 @findex ses+
787
788 Oftentimes you want a calculation to exclude the blank cells. Here
789 are some useful functions to call from your formulas:
790
791 @table @code
792 @item (ses-delete-blanks &rest @var{args})
793 Returns a list from which all blank cells (value is either @code{nil} or
794 '*skip*) have been deleted.
795
796 @item (ses+ &rest @var{args})
797 Sum of non-blank arguments.
798
799 @item (ses-average @var{list})
800 Average of non-blank elements in @var{list}. Here the list is passed
801 as a single argument, since you'll probably use it with @code{ses-range}.
802 @end table
803
804 @node More on cell printing
805 @section More on cell printing
806 @cindex cell printing, more
807 @findex ses-truncate-cell
808 @findex ses-recalculate-cell
809
810 Special cell values:
811 @itemize
812 @item nil prints the same as "", but allows previous cell to spill over.
813 @item '*skip* replaces nil when the previous cell actually does spill over;
814 nothing is printed for it.
815 @item '*error* indicates that the formula signaled an error instead of
816 producing a value: the print cell is filled with hash marks (#).
817 @end itemize
818
819 If the result from the printer function is too wide for the cell and
820 the following cell is @code{nil}, the result will spill over into the
821 following cell. Very wide results can spill over several cells. If
822 the result is too wide for the available space (up to the end of the
823 row or the next non-@code{nil} cell), the result is truncated if the cell's
824 value is a string, or replaced with hash marks otherwise.
825
826 @acronym{SES} could get confused by printer results that contain newlines or
827 tabs, so these are replaced with question marks.
828
829 @table @kbd
830 @item t
831 Confine a cell to its own column (@code{ses-truncate-cell}). This
832 allows you to move point to a rightward cell that would otherwise be
833 covered by a spill-over. If you don't change the rightward cell, the
834 confined cell will spill over again the next time it is reprinted.
835
836 @item c
837 When applied to a single cell, this command displays in the echo area
838 any formula error or printer error that occurred during
839 recalculation/reprinting (@code{ses-recalculate-cell}). You can use
840 this to undo the effect of @kbd{t}.
841 @end table
842
843 When a printer function signals an error, the fallback printer
844 @samp{"%s"} is substituted. This is useful when your column printer
845 is numeric-only and you use a string as a cell value. Note that the
846 standard default printer is ``%.7g'' which is numeric-only, so cells
847 that are empty of contain strings will use the fallback printer.
848 @kbd{c} on such cells will display ``Format specifier doesn't match
849 argument type''.
850
851
852 @node Import and export
853 @section Import and export
854 @cindex import and export
855 @cindex export, and import
856 @findex ses-export-tsv
857 @findex ses-export-tsf
858
859 @table @kbd
860 @item x t
861 Export a range of cells as tab-separated values (@code{ses-export-tsv}).
862 @item x T
863 Export a range of cells as tab-separated formulas (@code{ses-export-tsf}).
864 @end table
865
866 The exported text goes to the kill ring; you can paste it into
867 another buffer. Columns are separated by tabs, rows by newlines.
868
869 To import text, use any of the yank commands where the text to paste
870 contains tabs and/or newlines. Imported formulas are not relocated.
871
872 @node Virus protection
873 @section Virus protection
874 @cindex virus protection
875
876 Whenever a formula or printer is read from a file or is pasted into
877 the spreadsheet, it receives a ``needs safety check'' marking. Later,
878 when the formula or printer is evaluated for the first time, it is
879 checked for safety using the @code{unsafep} predicate; if found to be
880 ``possibly unsafe'', the questionable formula or printer is displayed
881 and you must press Y to approve it or N to use a substitute. The
882 substitute always signals an error.
883
884 Formulas or printers that you type in are checked immediately for
885 safety. If found to be possibly unsafe and you press N to disapprove,
886 the action is canceled and the old formula or printer will remain.
887
888 Besides viruses (which try to copy themselves to other files),
889 @code{unsafep} can also detect all other kinds of Trojan horses, such as
890 spreadsheets that delete files, send email, flood Web sites, alter
891 your Emacs settings, etc.
892
893 Generally, spreadsheet formulas and printers are simple things that
894 don't need to do any fancy computing, so all potentially-dangerous
895 parts of the Emacs Lisp environment can be excluded without cramping
896 your style as a formula-writer. See the documentation in @file{unsafep.el}
897 for more info on how Lisp forms are classified as safe or unsafe.
898
899 @node Spreadsheets with details and summary
900 @section Spreadsheets with details and summary
901 @cindex details and summary
902 @cindex summary, and details
903
904 A common organization for spreadsheets is to have a bunch of ``detail''
905 rows, each perhaps describing a transaction, and then a set of
906 ``summary'' rows that each show reduced data for some subset of the
907 details. @acronym{SES} supports this organization via the @code{ses-select}
908 function.
909
910 @table @code
911 @item (ses-select @var{fromrange} @var{test} @var{torange})
912 Returns a subset of @var{torange}. For each member in @var{fromrange}
913 that is equal to @var{test}, the corresponding member of @var{torange}
914 is included in the result.
915 @end table
916
917 Example of use:
918 @lisp
919 (ses-average (ses-select (ses-range A1 A5) 'Smith (ses-range B1 B5)))
920 @end lisp
921 This computes the average of the B column values for those rows whose
922 A column value is the symbol 'Smith.
923
924 Arguably one could specify only @var{fromrange} plus
925 @var{to-row-offset} and @var{to-column-offset}. The @var{torange} is
926 stated explicitly to ensure that the formula will be recalculated if
927 any cell in either range is changed.
928
929 File @file{etc/ses-example.el} in the Emacs distribution is an example of a
930 details-and-summary spreadsheet.
931
932
933 @c ===================================================================
934
935 @node For Gurus
936 @chapter For Gurus
937 @cindex advanced features
938
939 @menu
940 * Deferred updates::
941 * Nonrelocatable references::
942 * The data area::
943 * Buffer-local variables in spreadsheets::
944 * Uses of defadvice in @acronym{SES}::
945 @end menu
946
947 @node Deferred updates
948 @section Deferred updates
949 @cindex deferred updates
950 @cindex updates, deferred
951 @vindex run-with-idle-timer
952
953 To save time by avoiding redundant computations, cells that need
954 recalculation due to changes in other cells are added to a set. At
955 the end of the command, each cell in the set is recalculated once.
956 This can create a new set of cells that need recalculation. The
957 process is repeated until either the set is empty or it stops changing
958 (due to circular references among the cells). In extreme cases, you
959 might see progress messages of the form ``Recalculating... (@var{nnn}
960 cells left)''. If you interrupt the calculation using @kbd{C-g}, the
961 spreadsheet will be left in an inconsistent state, so use @kbd{C-_} or
962 @kbd{C-c C-l} to fix it.
963
964 To save even more time by avoiding redundant writes, cells that have
965 changes are added to a set instead of being written immediately to the
966 data area. Each cell in the set is written once, at the end of the
967 command. If you change vast quantities of cells, you might see a
968 progress message of the form ``Writing... (@var{nnn} cells left)''.
969 These deferred cell-writes cannot be interrupted by @kbd{C-g}, so
970 you'll just have to wait.
971
972 @acronym{SES} uses @code{run-with-idle-timer} to move the cell underline when
973 Emacs will be scrolling the buffer after the end of a command, and
974 also to narrow and underline after @kbd{C-x C-v}. This is visible as
975 a momentary glitch after C-x C-v and certain scrolling commands. You
976 can type ahead without worrying about the glitch.
977
978
979 @node Nonrelocatable references
980 @section Nonrelocatable references
981 @cindex nonrelocatable references
982 @cindex references, nonrelocatable
983
984 @kbd{C-y} relocates all cell-references in a pasted formula, while
985 @kbd{C-u C-y} relocates none of the cell-references. What about mixed
986 cases?
987
988 You can use
989 @lisp
990 (symbol-value 'B3)
991 @end lisp
992 to make an @dfn{absolute reference}. The formula relocator skips over
993 quoted things, so this will not be relocated when pasted or when
994 rows/columns are inserted/deleted. However, B3 will not be recorded
995 as a dependency of this cell, so this cell will not be updated
996 automatically when B3 is changed.
997
998 The variables @code{row} and @code{col} are dynamically bound while a
999 cell formula is being evaluated. You can use
1000 @lisp
1001 (ses-cell-value row 0)
1002 @end lisp
1003 to get the value from the leftmost column in the current row. This
1004 kind of dependency is also not recorded.
1005
1006
1007 @node The data area
1008 @section The data area
1009 @cindex data area
1010 @findex ses-reconstruct-all
1011
1012 Begins with an 014 character, followed by sets of cell-definition
1013 macros for each row, followed by column-widths, column-printers,
1014 default-printer, and header-row. Then there's the global parameters
1015 (file-format ID, numrows, numcols) and the local variables (specifying
1016 @acronym{SES} mode for the buffer, etc.).
1017
1018 When a @acronym{SES} file is loaded, first the numrows and numcols values are
1019 loaded, then the entire data area is @code{eval}ed, and finally the local
1020 variables are processed.
1021
1022 You can edit the data area, but don't insert or delete any newlines
1023 except in the local-variables part, since @acronym{SES} locates things by
1024 counting newlines. Use @kbd{C-x C-e} at the end of a line to install
1025 your edits into the spreadsheet data structures (this does not update
1026 the print area, use, e.g., @kbd{C-c C-l} for that).
1027
1028 The data area is maintained as an image of spreadsheet data
1029 structures that area stored in buffer-local variables. If the data
1030 area gets messed up, you can try reconstructing the data area from the
1031 data structures:
1032
1033 @table @kbd
1034 @item C-c M-C-l
1035 (@code{ses-reconstruct-all}).
1036 @end table
1037
1038
1039 @node Buffer-local variables in spreadsheets
1040 @section Buffer-local variables in spreadsheets
1041 @cindex buffer-local variables
1042 @cindex variables, buffer-local
1043
1044 You can add additional local variables to the list at the bottom of
1045 the data area, such as hidden constants you want to refer to in your
1046 formulas.
1047
1048 You can override the variable @code{ses--symbolic-formulas} to be a list of
1049 symbols (as parenthesized strings) to show as completions for the '
1050 command. This initial completions list is used instead of the actual
1051 set of symbols-as-formulas in the spreadsheet.
1052
1053 For an example of this, see file @file{etc/ses-example.ses}.
1054
1055 If (for some reason) you want your formulas or printers to save data
1056 into variables, you must declare these variables as buffer-locals in
1057 order to avoid a virus warning.
1058
1059 You can define functions by making them values for the fake local
1060 variable @code{eval}. Such functions can then be used in your
1061 formulas and printers, but usually each @code{eval} is presented to
1062 the user during file loading as a potential virus. This can get
1063 annoying.
1064
1065 You can define functions in your @file{.emacs} file. Other people can
1066 still read the print area of your spreadsheet, but they won't be able
1067 to recalculate or reprint anything that depends on your functions. To
1068 avoid virus warnings, each function used in a formula needs
1069 @lisp
1070 (put 'your-function-name 'safe-function t)
1071 @end lisp
1072
1073 @node Uses of defadvice in @acronym{SES}
1074 @section Uses of defadvice in @acronym{SES}
1075 @cindex defadvice
1076 @cindex undo-more
1077 @cindex copy-region-as-kill
1078 @cindex yank
1079
1080 @table @code
1081 @item undo-more
1082 Defines a new undo element format (@var{fun} . @var{args}), which
1083 means ``undo by applying @var{fun} to @var{args}''. For spreadsheet
1084 buffers, it allows undos in the data area even though that's outside
1085 the narrowing.
1086
1087 @item copy-region-as-kill
1088 When copying from the print area of a spreadsheet, treat the region as
1089 a rectangle and attach each cell's formula and printer as 'ses
1090 properties.
1091
1092 @item yank
1093 When yanking into the print area of a spreadsheet, first try to yank
1094 as cells (if the yank text has 'ses properties), then as tab-separated
1095 formulas, then (if all else fails) as a single formula for the current
1096 cell.
1097 @end table
1098
1099 @c ===================================================================
1100 @node Index
1101 @unnumbered Index
1102
1103 @printindex cp
1104
1105 @c ===================================================================
1106
1107 @node Acknowledgments
1108 @unnumbered Acknowledgments
1109
1110 Coding by:
1111 @quotation
1112 @c jyavner@@member.fsf.org
1113 Jonathan Yavner,
1114 @c monnier@@gnu.org
1115 Stefan Monnier,
1116 @c shigeru.fukaya@@gmail.com
1117 Shigeru Fukaya
1118 @end quotation
1119
1120 @noindent
1121 Texinfo manual by:
1122 @quotation
1123 @c jyavner@@member.fsf.org
1124 Jonathan Yavner,
1125 @c brad@@chenla.org
1126 Brad Collins
1127 @end quotation
1128
1129 @noindent
1130 Ideas from:
1131 @quotation
1132 @c christoph.conrad@@gmx.de
1133 Christoph Conrad,
1134 @c cyberbob@@redneck.gacracker.org
1135 CyberBob,
1136 @c syver-en@@online.no
1137 Syver Enstad,
1138 @c fischman@@zion.bpnetworks.com
1139 Ami Fischman,
1140 @c Thomas.Gehrlein@@t-online.de
1141 Thomas Gehrlein,
1142 @c c.f.a.johnson@@rogers.com
1143 Chris F.A. Johnson,
1144 @c lyusong@@hotmail.com
1145 Yusong Li,
1146 @c juri@@jurta.org
1147 Juri Linkov,
1148 @c maierh@@myself.com
1149 Harald Maier,
1150 @c anash@@san.rr.com
1151 Alan Nash,
1152 @c pinard@@iro.umontreal.ca
1153 François Pinard,
1154 @c ppinto@@cs.cmu.edu
1155 Pedro Pinto,
1156 @c xsteve@@riic.at
1157 Stefan Reichör,
1158 @c epameinondas@@gmx.de
1159 Oliver Scholz,
1160 @c rms@@gnu.org
1161 Richard M. Stallman,
1162 @c teirllm@@dms.auburn.edu
1163 Luc Teirlinck,
1164 @c jotto@@pobox.com
1165 J. Otto Tennant,
1166 @c jphil@@acs.pagesjaunes.fr
1167 Jean-Philippe Theberge
1168 @end quotation
1169
1170 @c ===================================================================
1171
1172 @node GNU Free Documentation License
1173 @appendix GNU Free Documentation License
1174 @include doclicense.texi
1175
1176 @bye