* lisp/svg.el (svg--encode-text): Fix off-by-one error in previous patch.
[emacs.git] / doc / misc / ses.texi
blob5f9a0f597219f39737e6caf1a5c9140c05a77cf0
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 @include docstyle.texi
6 @setchapternewpage off
7 @syncodeindex fn cp
8 @syncodeindex vr cp
9 @syncodeindex ky cp
10 @c %**end of header
12 @copying
13 This file documents @acronym{SES}: the Simple Emacs Spreadsheet.
15 Copyright @copyright{} 2002--2017 Free Software Foundation, Inc.
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.''
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
30 @dircategory Emacs misc features
31 @direntry
32 * @acronym{SES}: (ses).                   Simple Emacs Spreadsheet.
33 @end direntry
35 @finalout
37 @titlepage
38 @title @acronym{SES}
39 @subtitle Simple Emacs Spreadsheet
40 @author Jonathan A. Yavner
41 @author @email{jyavner@@member.fsf.org}
43 @page
44 @vskip 0pt plus 1filll
45 @insertcopying
46 @end titlepage
48 @contents
50 @c ===================================================================
52 @ifnottex
53 @node Top
54 @comment  node-name,  next,  previous,  up
55 @top @acronym{SES}: Simple Emacs Spreadsheet
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
64 To report bugs, use @kbd{M-x report-emacs-bug}.
66 @insertcopying
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
79 @c ===================================================================
81 @node Sales Pitch
82 @comment  node-name,  next,  previous,  up
83 @chapter Sales Pitch
84 @cindex features
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
101 @c ===================================================================
103 @node Quick Tutorial
104 @chapter Quick Tutorial
105 @cindex introduction
106 @cindex tutorial
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.
112 First, visit a new file with the @file{.ses} extension.
113 Emacs presents you with an empty spreadsheet containing a single cell.
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.)
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.:
124 @example
125 @group
127  Income
128    2.23
129    0.02
130   15.76
131   -4.00
132 @end group
133 @end example
135 To add up the values, enter a Lisp expression:
137 @example
138 (+ A2 A3 A4 A5)
139 @end example
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.
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.:
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
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}.
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{+}:
175 @example
176 (ses+ A2 A3 A4 A5)
177 @end example
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.:
183 @example
184 (apply 'ses+ (ses-range A2 A5))
185 @end example
187 (The @code{apply} is necessary because @code{ses-range} produces a
188 @emph{list} of values.  This allows for more complex possibilities.)
190 Alternatively you can use the @code{!} modifier of @code{ses-range} to
191 remove blank cells from the returned list, which allows to use
192 @code{+} instead of @code{ses+}:
194 @lisp
195 (apply '+ (ses-range A2 A5 !))
196 @end lisp
198 @c ===================================================================
200 @node The Basics
201 @comment  node-name,  next,  previous,  up
202 @chapter The Basics
203 @cindex basic commands
204 @findex ses-jump
205 @findex ses-mark-row
206 @findex ses-mark-column
207 @findex ses-mark-whole-buffer
208 @findex set-mark-command
209 @findex keyboard-quit
211 To create a new spreadsheet, visit a nonexistent file whose name ends
212   with ".ses".  For example, @kbd{C-x C-f test.ses RET}.
215 A @dfn{cell identifier} is a symbol with a column letter and a row
216 number.  Cell B7 is the 2nd column of the 7th row.  For very wide
217 spreadsheets, there are two column letters: cell AB7 is the 28th
218 column of the 7th row. Super wide spreadsheets get AAA1, etc.
219 You move around with the regular Emacs movement commands.
221 @table @kbd
222 @item j
223 Moves point to cell, specified by identifier (@code{ses-jump}).
224 @end table
226 Point is always at the left edge of a cell, or at the empty endline.
227 When mark is inactive, the current cell is underlined.  When mark is
228 active, the range is the highlighted rectangle of cells (@acronym{SES} always
229 uses transient mark mode).  Drag the mouse from A1 to A3 to create the
230 range A1-A2.  Many @acronym{SES} commands operate only on single cells, not
231 ranges.
233 @table @kbd
234 @item C-@key{SPC}
235 @itemx C-@@
236 Set mark at point (@code{set-mark-command}).
238 @item C-g
239 Turn off the mark (@code{keyboard-quit}).
241 @item M-h
242 Highlight current row (@code{ses-mark-row}).
244 @item S-M-h
245 Highlight current column (@code{ses-mark-column}).
247 @item C-x h
248 Highlight all cells (@code{mark-whole-buffer}).
249 @end table
251 @menu
252 * Formulas::
253 * Resizing::
254 * Printer functions::
255 * Clearing cells::
256 * Copy/cut/paste::
257 * Customizing @acronym{SES}::
258 @end menu
260 @node Formulas
261 @section Cell formulas
262 @cindex formulas
263 @cindex formulas, entering
264 @cindex values
265 @cindex cell values
266 @cindex editing cells
267 @findex ses-read-cell
268 @findex ses-read-symbol
269 @findex ses-edit-cell
270 @findex ses-recalculate-cell
271 @findex ses-recalculate-all
273 To insert a value into a cell, simply type a numeric expression,
274 @samp{"double-quoted text"}, or a Lisp expression.
276 @table @kbd
277 @item 0..9
278 Self-insert a digit (@code{ses-read-cell}).
280 @item -
281 Self-insert a negative number (@code{ses-read-cell}).
283 @item .
284 Self-insert a fractional number (@code{ses-read-cell}).
286 @item "
287 Self-insert a quoted string.  The ending double-quote
288 is inserted for you (@code{ses-read-cell}).
290 @item (
291 Self-insert an expression.  The right-parenthesis is inserted for you
292 (@code{ses-read-cell}).  To access another cell's value, just use its
293 identifier in your expression.  Whenever the other cell is changed,
294 this cell's formula will be reevaluated.  While typing in the
295 expression, you can use the following keys:
296 @table @kbd
297 @item M-@key{TAB}
298 to complete symbol names, and
299 @item  C-h C-n
300 to list the named cells symbols in a help buffer.
301 @end table
303 @item ' @r{(apostrophe)}
304 Enter a symbol (ses-read-symbol).  @acronym{SES} remembers all symbols that have
305 been used as formulas, so you can type just the beginning of a symbol
306 and use @kbd{@key{SPC}}, @kbd{@key{TAB}}, and @kbd{?} to complete it.
307 @end table
309 To enter something else (e.g., a vector), begin with a digit, then
310 erase the digit and type whatever you want.
312 @table @kbd
313 @item RET
314 Edit the existing formula in the current cell (@code{ses-edit-cell}).
316 @item C-c C-c
317 Force recalculation of the current cell or range (@code{ses-recalculate-cell}).
319 @item C-c C-l
320 Recalculate the entire spreadsheet (@code{ses-recalculate-all}).
321 @end table
323 @node Resizing
324 @section Resizing the spreadsheet
325 @cindex resizing spreadsheets
326 @cindex dimensions
327 @cindex row, adding or removing
328 @cindex column, adding or removing
329 @cindex adding rows or columns
330 @cindex inserting rows or columns
331 @cindex removing rows or columns
332 @cindex deleting rows or columns
333 @findex ses-insert-row
334 @findex ses-insert-column
335 @findex ses-delete-row
336 @findex ses-delete-column
337 @findex ses-set-column-width
338 @findex ses-forward-or-insert
339 @findex ses-append-row-jump-first-column
342 Basic commands:
344 @table @kbd
345 @item C-o
346 (@code{ses-insert-row})
348 @item M-o
349 (@code{ses-insert-column})
351 @item C-k
352 (@code{ses-delete-row})
354 @item M-k
355 (@code{ses-delete-column})
357 @item w
358 (@code{ses-set-column-width})
360 @item TAB
361 Moves point to the next rightward cell, or inserts a new column if
362 already at last cell on line, or inserts a new row if at endline
363 (@code{ses-forward-or-insert}).
365 @item C-j
366 Linefeed inserts below the current row and moves to column A
367 (@code{ses-append-row-jump-first-column}).
368 @end table
370 Resizing the spreadsheet (unless you're just changing a column width)
371 relocates all the cell-references in formulas so they still refer to
372 the same cells.  If a formula mentioned B1 and you insert a new first
373 row, the formula will now mention B2.
375 If you delete a cell that a formula refers to, the cell-symbol is
376 deleted from the formula, so @code{(+ A1 B1 C1)} after deleting the third
377 column becomes @code{(+ A1 B1)}.  In case this is not what you wanted:
379 @table @kbd
380 @item C-_
381 @itemx C-x u
382 Undo previous action (@code{(undo)}).
383 @end table
386 @node Printer functions
387 @section Printer functions
388 @cindex printer functions
389 @cindex cell formatting
390 @cindex formatting cells
392 Printer functions convert binary cell values into the print forms that
393 Emacs will display on the screen.
395 @menu
396 * Various kinds of printer functions::
397 * Configuring what printer function applies::
398 * Standard printer functions::
399 * Local printer functions::
400 * Writing a lambda printer function::
401 @end menu
403 @node Various kinds of printer functions
404 @subsection Various kinds of printer functions
406 When configuring what printer function applies (@pxref{Configuring
407 what printer function applies}), you can enter a printer function as
408 one of the following:
410 @itemize
411 @item
412 A format string, like @samp{"$%.2f"}.  The result
413 string is right-aligned within the print cell.  To get left-alignment,
414 use parentheses: @samp{("$%.2f")}.
415 @item
416 A printer can also be a one-argument function, the result of which is
417 a string (right-aligned) or list of one string (left-aligned). Such a
418 function can be in turn configured as:
419 @itemize
420 @item
421 A lambda expression, for instance:
423 @lisp
424 (lambda (x)
425   (cond
426      ((null x) "")
427      ((numberp x) (format "%.2f" x))
428      (t (ses-center-span x ?# 'ses-prin1))))
429 @end lisp
431 While typing in a lambda, you can use @kbd{M-@key{TAB}} to complete
432 the names of symbols.
433 @item
434 A symbol referring to a standard printer function (@pxref{Standard
435 printer functions}).
436 @item
437 A symbol referring to a local printer function (@pxref{Local printer
438 functions}).
439 @end itemize
442 @end itemize
445 @node Configuring what printer function applies
446 @subsection Configuring what printer function applies
448 Each cell has a printer.  If @code{nil}, the column-printer for the cell's
449 column is used.  If that is also @code{nil}, the default-printer for the
450 spreadsheet is used.
452 @table @kbd
453 @item p
454 @findex ses-read-cell-printer
455 Enter a printer for current cell or range (@code{ses-read-cell-printer}).
457 @item M-p
458 @findex ses-read-column-printer
459 Enter a printer for the current column (@code{ses-read-column-printer}).
461 @item C-c C-p
462 @findex ses-read-default-printer
463 Enter the default printer for the spreadsheet
464 (@code{ses-read-default-printer}).
465 @end table
467 The @code{ses-read-@var{xxx}-printer} allows the following commands during editing:
469 @table @kbd
470 @item @key{arrow-up}
471 @itemx @key{arrow-down}
472 To browse history: the @code{ses-read-@var{xxx}-printer} commands have
473 their own minibuffer history, which is preloaded with the set of all
474 printers used in this spreadsheet, plus the standard printers
475 (@pxref{Standard printer functions}) and the local printers
476 (@pxref{Local printer functions}).
477 @item @key{TAB}
478 To complete the local printer symbols, and
479 @item C-h C-p
480 To list the local printers in a help buffer.
481 @end table
484 @node Standard printer functions
485 @subsection Standard printer functions
488 Except for @code{ses-prin1}, the other standard printers are suitable
489 only for cells, not columns or default, because they format the value
490 using the column-printer (or default-printer if @code{nil}) and then
491 center the result:
493 @ftable @code
494 @item ses-center
495 Just centering.
497 @item ses-center-span
498 Centering with spill-over to following blank cells.
500 @item ses-dashfill
501 Centering using dashes (-) instead of spaces.
503 @item ses-dashfill-span
504 Centering with dashes and spill-over.
506 @item ses-tildefill-span
507 Centering with tildes (~) and spill-over.
509 @item ses-prin1
510 This is the fallback printer, used when calling the configured printer
511 throws some error.
512 @end ftable
514 @node Local printer functions
515 @subsection Local printer functions
517 @findex ses-define-local-printer
518 You can define printer function local to a sheet with the command
519 @code{ses-define-local-printer}.  For instance, define a printer
520 @samp{foo} to @code{"%.2f"}, and then use symbol @samp{foo} as a
521 printer function.  Then, if you call again
522 @code{ses-define-local-printer} on @samp{foo} to redefine it as
523 @code{"%.3f"}, all the cells using printer @samp{foo} will be
524 reprinted accordingly.
526 Sometimes there are local printers that you want to define or
527 re-define automatically every time you open a sheet. For instance
528 imagine that you want to define/re-define automatically a local
529 printer @code{euro} to display a number like an amount of euros, that
530 is to say number @code{3.1} would be displayed as
531 @code{3.10@dmn{}@euro{}}. To do so in any non read-only SES buffer,
532 you can add some code like this to your @file{.emacs} init file:
534 @lisp
535 (defun my-ses-mode-hook ()
536   (unless buffer-read-only
537     (ses-define-local-printer
538      'euro
539      (lambda (x)
540        (cond
541         ((null x) "")
542         ((numberp x) (format "%.2f€" x))
543         (t (ses-center-span x ?# 'ses-prin1)))))))
544 (add-hook 'ses-mode-hook 'my-ses-mode-hook)
545 @end lisp
547 If you replace command @code{ses-define-local-printer} by function
548 @code{ses-define-if-new-local-printer}
549 @findex ses-define-if-new-local-printer
550 the definition will occur only if a local printer with the same name
551 in not already defined.
554 @node Writing a lambda printer function
555 @subsection Writing a lambda printer function
557 You can write a printer function with a lambda expression taking one
558 argument in two cases:
560 @itemize
561 @item
562 when you configure the printer function applying to a cell or column, or
563 @item
564 when you define a local printer function with command
565 @code{ses-define-local-printer}.
566 @end itemize
568 When doing so, please take care that the returned value is a string,
569 or a list containing a string, even when the input argument has an
570 unexpected value. Here is an example:
572 @example
573 (lambda (val)
574    (cond
575       ((null val) "")
576       ((and (numberp val) (>= val 0)) (format "%.1f" val))
577       (t (ses-center-span val ?# 'ses-prin1))))
578 @end example
580 This example will:
582 @itemize
583 @item
584 When the cell is empty (ie.@: when @code{val} is @code{nil}), print an
585 empty string @code{""}
586 @item
587 When the cell value is a non negative number, format the value in
588 fixed-point notation with one decimal after point
589 @item
590 Otherwise, handle the value as erroneous by printing it as an
591 s-expression (using @code{ses-prin1}), centered and surrounded by
592 @code{#} filling.
593 @end itemize
595 Another precaution to take is to avoid stack overflow due to a
596 printer function calling itself indefinitely.  This mistake can
597 happen when you use a local printer as a column printer,
598 and this local printer implicitly calls the current column printer, so it
599 will call itself recursively.  Imagine for instance that you want to
600 create some local printer @code{=fill} that would center the content
601 of a cell and surround it by equal signs @code{=}, and you do it this
602 way:
604 @lisp
605 (lambda (x)
606   (cond
607    ((null x) "")
608    (t (ses-center x 0 ?=))))
609 @end lisp
611 Because @code{=fill} uses the standard printer @code{ses-center} without
612 explicitly passing any printer to it, @code{ses-center} will call the
613 current column printer if any, or the spreadsheet default printer
614 otherwise.  So using @code{=fill} as a column printer will result in a
615 stack overflow in this column.  SES does not check for that;
616 you just have to be careful.  For instance, re-write @code{=fill} like
617 this:
619 @lisp
620 (lambda (x)
621   (cond
622    ((null x) "")
623    ((stringp x) (ses-center x 0 ?= " %s "))
624    (t (ses-center-span x ?# 'ses-prin1))))
625 @end lisp
627 The code above applies the @code{=} filling only to strings; it also
628 surrounds the string by one space on each side before filling with
629 @code{=} signs.  So the string @samp{Foo} will be displayed like @samp{@w{===
630 Foo ===}} in an 11 character wide column.  Anything other than an empty cell
631 or a non-string is displayed as an error by using @code{#} filling.
633 @node Clearing cells
634 @section Clearing cells
635 @cindex clearing commands
636 @findex ses-clear-cell-backward
637 @findex ses-clear-cell-forward
639 These commands set both formula and printer to @code{nil}:
641 @table @kbd
642 @item DEL
643 Clear cell and move left (@code{ses-clear-cell-backward}).
645 @item C-d
646 Clear cell and move right (@code{ses-clear-cell-forward}).
647 @end table
650 @node Copy/cut/paste
651 @section Copy, cut, and paste
652 @cindex copy
653 @cindex cut
654 @cindex paste
655 @findex kill-ring-save
656 @findex mouse-set-region
657 @findex mouse-set-secondary
658 @findex ses-kill-override
659 @findex yank
660 @findex clipboard-yank
661 @findex mouse-yank-at-click
662 @findex mouse-yank-at-secondary
663 @findex ses-yank-pop
665 The copy functions work on rectangular regions of cells.  You can paste the
666 copies into non-@acronym{SES} buffers to export the print text.
668 @table @kbd
669 @item M-w
670 @itemx [copy]
671 @itemx [C-insert]
672 Copy the highlighted cells to kill ring and primary clipboard
673 (@code{kill-ring-save}).
675 @item [drag-mouse-1]
676 Mark a region and copy it to kill ring and primary clipboard
677 (@code{mouse-set-region}).
679 @item [M-drag-mouse-1]
680 Mark a region and copy it to kill ring and secondary clipboard
681 (@code{mouse-set-secondary}).
683 @item C-w
684 @itemx [cut]
685 @itemx [S-delete]
686 The cut functions do not actually delete rows or columns---they copy
687 and then clear (@code{ses-kill-override}).
689 @item C-y
690 @itemx [S-insert]
691 Paste from kill ring (@code{yank}).  The paste functions behave
692 differently depending on the format of the text being inserted:
693 @itemize @bullet
694 @item
695 When pasting cells that were cut from a @acronym{SES} buffer, the print text is
696 ignored and only the attached formula and printer are inserted; cell
697 references in the formula are relocated unless you use @kbd{C-u}.
698 @item
699 The pasted text overwrites a rectangle of cells whose top left corner
700 is the current cell.  If part of the rectangle is beyond the edges of
701 the spreadsheet, you must confirm the increase in spreadsheet size.
702 @item
703 Non-@acronym{SES} text is usually inserted as a replacement formula for the
704 current cell.  If the formula would be a symbol, it's treated as a
705 string unless you use @kbd{C-u}.  Pasted formulas with syntax errors
706 are always treated as strings.
707 @end itemize
709 @item [paste]
710 Paste from primary clipboard or kill ring (@code{clipboard-yank}).
712 @item [mouse-2]
713 Set point and paste from primary clipboard (@code{mouse-yank-at-click}).
715 @item [M-mouse-2]
716 Set point and paste from secondary clipboard (@code{mouse-yank-secondary}).
718 @item M-y
719 Immediately after a paste, you can replace the text with a preceding
720 element from the kill ring (@code{ses-yank-pop}).  Unlike the standard
721 Emacs yank-pop, the @acronym{SES} version uses @code{undo} to delete the old
722 yank.  This doesn't make any difference?
723 @end table
725 @node Customizing @acronym{SES}
726 @section Customizing @acronym{SES}
727 @cindex customizing
728 @vindex enable-local-eval
729 @vindex ses-mode-hook
730 @vindex safe-functions
731 @vindex enable-local-eval
734 By default, a newly-created spreadsheet has 1 row and 1 column.  The
735 column width is 7 and the default printer is @samp{"%.7g"}.  Each of these
736 can be customized.  Look in group ``ses''.
738 After entering a cell value, point normally moves right to the next
739 cell.  You can customize @code{ses-after-entry-functions} to move left or
740 up or down.  For diagonal movement, select two functions from the
741 list.
743 @code{ses-mode-hook} is a normal mode hook (list of functions to
744 execute when starting @acronym{SES} mode for a buffer).
746 The variable @code{safe-functions} is a list of possibly-unsafe
747 functions to be treated as safe when analyzing formulas and printers.
748 @xref{Virus protection}.  Before customizing @code{safe-functions},
749 think about how much you trust the person who's suggesting this
750 change.  The value @code{t} turns off all anti-virus protection.  A
751 list-of-functions value might enable a ``gee whiz'' spreadsheet, but it
752 also creates trapdoors in your anti-virus armor.  In order for virus
753 protection to work, you must always press @kbd{n} when presented with
754 a virus warning, unless you understand what the questionable code is
755 trying to do.  Do not listen to those who tell you to customize
756 @code{enable-local-eval}---this variable is for people who don't wear
757 safety belts!
760 @c ===================================================================
762 @node Advanced Features
763 @chapter Advanced Features
764 @cindex advanced features
765 @findex ses-read-header-row
768 @table @kbd
769 @item C-c M-C-h
770 (@code{ses-set-header-row}).
771 @findex ses-set-header-row
772 @kindex C-c M-C-h
773 The header line at the top of the @acronym{SES}
774 window normally shows the column letter for each column.  You can set
775 it to show a copy of some row, such as a row of column titles, so that
776 row will always be visible.  Default is to set the current row as the
777 header; use C-u to prompt for header row.  Set the header to row 0 to
778 show column letters again.
779 @item [header-line mouse-3]
780 Pops up a menu to set the current row as the header, or revert to
781 column letters.
782 @item M-x ses-rename-cell
783 @findex ses-rename-cell
784 Rename a cell from a standard A1-like name to any string that can be a
785 valid local variable name (See also @ref{Nonrelocatable references}).
786 @item M-x ses-repair-cell-reference-all
787 @findex ses-repair-cell-reference-all
788 When you interrupt a cell formula update by clicking @kbd{C-g}, then
789 the cell reference link may be broken, which will jeopardize automatic
790 cell update when any other cell on which it depends is changed. To
791 repair that use function @code{ses-repair-cell-reference-all}
792 @end table
794 @menu
795 * The print area::
796 * Ranges in formulas::
797 * Sorting by column::
798 * Standard formula functions::
799 * More on cell printing::
800 * Import and export::
801 * Virus protection::
802 * Spreadsheets with details and summary::
803 @end menu
805 @node The print area
806 @section The print area
807 @cindex print area
808 @findex widen
809 @findex ses-renarrow-buffer
810 @findex ses-reprint-all
812 A @acronym{SES} file consists of a print area and a data area.  Normally the
813 buffer is narrowed to show only the print area.  The print area is
814 read-only except for special @acronym{SES} commands; it contains cell values
815 formatted by printer functions.  The data area records the formula and
816 printer functions, etc.
818 @table @kbd
819 @item C-x n w
820 Show print and data areas (@code{widen}).
822 @item C-c C-n
823 Show only print area (@code{ses-renarrow-buffer}).
825 @item S-C-l
826 @itemx M-C-l
827 Recreate print area by reevaluating printer functions for all cells
828 (@code{ses-reprint-all}).
829 @end table
831 @node Ranges in formulas
832 @section Ranges in formulas
833 @cindex ranges
834 @findex ses-insert-range-click
835 @findex ses-insert-range
836 @findex ses-insert-ses-range-click
837 @findex ses-insert-ses-range
838 @vindex from
839 @vindex to
841 A formula like
842 @lisp
843 (+ A1 A2 A3)
844 @end lisp
845 is the sum of three specific cells.  If you insert a new second row,
846 the formula becomes
847 @lisp
848 (+ A1 A3 A4)
849 @end lisp
850 and the new row is not included in the sum.
852 The macro @code{(ses-range @var{from} @var{to})} evaluates to a list of
853 the values in a rectangle of cells.  If your formula is
854 @lisp
855 (apply '+ (ses-range A1 A3))
856 @end lisp
857 and you insert a new second row, it becomes
858 @lisp
859 (apply '+ (ses-range A1 A4))
860 @end lisp
861 and the new row is included in the sum.
863 While entering or editing a formula in the minibuffer, you can select
864 a range in the spreadsheet (using mouse or keyboard), then paste a
865 representation of that range into your formula.  Suppose you select
866 A1-C1:
868 @table @kbd
869 @item [S-mouse-3]
870 Inserts "A1 B1 C1" @code{(ses-insert-range-click})
872 @item C-c C-r
873 Keyboard version (@code{ses-insert-range}).
875 @item [C-S-mouse-3]
876 Inserts "(ses-range A1 C1)" (@code{ses-insert-ses-range-click}).
878 @item C-c C-s
879 Keyboard version (@code{ses-insert-ses-range}).
880 @end table
882 If you delete the @var{from} or @var{to} cell for a range, the nearest
883 still-existing cell is used instead.  If you delete the entire range,
884 the formula relocator will delete the ses-range from the formula.
886 If you insert a new row just beyond the end of a one-column range, or
887 a new column just beyond a one-row range, the new cell is included in
888 the range.  New cells inserted just before a range are not included.
890 Flags can be added to @code{ses-range} immediately after the @var{to}
891 cell.
892 @table @code
893 @item !
894 Empty cells in range can be removed by adding the @code{!} flag. An
895 empty cell is a cell the value of which is one of symbols @code{nil}
896 or @code{*skip*}. For instance @code{(ses-range A1 A4 !)} will do the
897 same as @code{(list A1 A3)} when cells @code{A2} and @code{A4} are
898 empty.
899 @item _
900 Empty cell values are replaced by the argument following flag
901 @code{_}, or @code{0} when flag @code{_} is last in argument list. For
902 instance @code{(ses-range A1 A4 _ "empty")} will do the same as
903 @code{(list A1 "empty" A3 "empty")} when cells @code{A2} and @code{A4}
904 are empty. Similarly, @code{(ses-range A1 A4 _ )} will do the same as
905 @code{(list A1 0 A3 0)}.
906 @item >v
907 When order matters, list cells by reading cells row-wise from top left
908 to bottom right. This flag is provided for completeness only as it is
909 the default reading order.
910 @item <v
911 List cells by reading cells row-wise from top right to bottom left.
912 @item v>
913 List cells by reading cells column-wise from top left to bottom right.
914 @item v<
915 List cells by reading cells column-wise from top right to bottom left.
916 @item v
917 A short hand for @code{v>}.
918 @item ^
919 A short hand for @code{^>}.
920 @item >
921 A short hand for @code{>v}.
922 @item <
923 A short hand for @code{>^}.
924 @item *
925 Instead of listing cells, it makes a Calc vector or matrix of it
926 (@pxref{Top,,,calc,GNU Emacs Calc Manual}). If the range contains only
927 one row or one column a vector is made, otherwise a matrix is made.
928 @item *2
929 Same as @code{*} except that a matrix is always made even when there
930 is only one row or column in the range.
931 @item *1
932 Same as @code{*} except that a vector is always made even when there
933 is only one row or column in the range, that is to say the
934 corresponding matrix is flattened.
935 @end table
937 @node Sorting by column
938 @section Sorting by column
939 @cindex sorting
940 @findex ses-sort-column
941 @findex ses-sort-column-click
943 @table @kbd
944 @item C-c M-C-s
945 Sort the cells of a range using one of the columns
946 (@code{ses-sort-column}).  The rows (or partial rows if the range
947 doesn't include all columns) are rearranged so the chosen column will
948 be in order.
950 @item [header-line mouse-2]
951 The easiest way to sort is to click mouse-2 on the chosen column's header row
952 (@code{ses-sort-column-click}).
953 @end table
955 The sort comparison uses @code{string<}, which works well for
956 right-justified numbers and left-justified strings.
958 With prefix arg, sort is in descending order.
960 Rows are moved one at a time, with relocation of formulas.  This works
961 well if formulas refer to other cells in their row, not so well for
962 formulas that refer to other rows in the range or to cells outside the
963 range.
966 @node Standard formula functions
967 @section Standard formula functions
968 @cindex standard formula functions
969 @cindex *skip*
970 @cindex *error*
971 @findex ses-delete-blanks
972 @findex ses-average
973 @findex ses+
975 Oftentimes you want a calculation to exclude the blank cells.  Here
976 are some useful functions to call from your formulas:
978 @table @code
979 @item (ses-delete-blanks &rest @var{args})
980 Returns a list from which all blank cells (value is either @code{nil}
981 or '*skip*) have been deleted. Order of args is reverted. Please note
982 that @code{ses-range} has a @code{!} modifier that allows to remove
983 blanks, so it is possible to write:
984 @lisp
985 (ses-range A1 A5 !)
986 @end lisp
987 instead of
988 @lisp
989 (apply 'ses-delete-blanks (ses-range A1 A5 <))
990 @end lisp
992 @item (ses+ &rest @var{args})
993 Sum of non-blank arguments.
995 @item (ses-average @var{list})
996 Average of non-blank elements in @var{list}.  Here the list is passed
997 as a single argument, since you'll probably use it with @code{ses-range}.
998 @end table
1000 @node More on cell printing
1001 @section More on cell printing
1002 @cindex cell printing, more
1003 @findex ses-truncate-cell
1004 @findex ses-recalculate-cell
1006 Special cell values:
1007 @itemize
1008 @item nil prints the same as "", but allows previous cell to spill over.
1009 @item '*skip* replaces nil when the previous cell actually does spill over;
1010 nothing is printed for it.
1011 @item '*error* indicates that the formula signaled an error instead of
1012 producing a value: the print cell is filled with hash marks (#).
1013 @end itemize
1015 If the result from the printer function is too wide for the cell and
1016 the following cell is @code{nil}, the result will spill over into the
1017 following cell.  Very wide results can spill over several cells.  If
1018 the result is too wide for the available space (up to the end of the
1019 row or the next non-@code{nil} cell), the result is truncated if the cell's
1020 value is a string, or replaced with hash marks otherwise.
1022 @acronym{SES} could get confused by printer results that contain newlines or
1023 tabs, so these are replaced with question marks.
1025 @table @kbd
1026 @item t
1027 Confine a cell to its own column (@code{ses-truncate-cell}).  This
1028 allows you to move point to a rightward cell that would otherwise be
1029 covered by a spill-over.  If you don't change the rightward cell, the
1030 confined cell will spill over again the next time it is reprinted.
1032 @item c
1033 When applied to a single cell, this command displays in the echo area
1034 any formula error or printer error that occurred during
1035 recalculation/reprinting (@code{ses-recalculate-cell}).  You can use
1036 this to undo the effect of @kbd{t}.
1037 @end table
1039 When a printer function signals an error, the fallback printer
1040 @findex ses-prin1
1041 @code{ses-prin1} is substituted.  This is useful when your column printer
1042 is numeric-only and you use a string as a cell value.  Note that the
1043 standard default printer is @samp{"%.7g"} which is numeric-only, so cells
1044 that are empty of contain strings will use the fallback printer.
1045 @kbd{c} on such cells will display ``Format specifier doesn't match
1046 argument type''.
1049 @node Import and export
1050 @section Import and export
1051 @cindex import and export
1052 @cindex export, and import
1053 @findex ses-export-tsv
1054 @findex ses-export-tsf
1056 @table @kbd
1057 @item x t
1058 Export a range of cells as tab-separated values (@code{ses-export-tsv}).
1059 @item x T
1060 Export a range of cells as tab-separated formulas (@code{ses-export-tsf}).
1061 @end table
1063 The exported text goes to the kill ring; you can paste it into
1064 another buffer.  Columns are separated by tabs, rows by newlines.
1066 To import text, use any of the yank commands where the text to paste
1067 contains tabs and/or newlines.  Imported formulas are not relocated.
1069 @node Virus protection
1070 @section Virus protection
1071 @cindex virus protection
1073 Whenever a formula or printer is read from a file or is pasted into
1074 the spreadsheet, it receives a ``needs safety check'' marking.  Later,
1075 when the formula or printer is evaluated for the first time, it is
1076 checked for safety using the @code{unsafep} predicate; if found to be
1077 ``possibly unsafe'', the questionable formula or printer is displayed
1078 and you must press Y to approve it or N to use a substitute.  The
1079 substitute always signals an error.
1081 Formulas or printers that you type in are checked immediately for
1082 safety.  If found to be possibly unsafe and you press N to disapprove,
1083 the action is canceled and the old formula or printer will remain.
1085 Besides viruses (which try to copy themselves to other files),
1086 @code{unsafep} can also detect all other kinds of Trojan horses, such as
1087 spreadsheets that delete files, send email, flood Web sites, alter
1088 your Emacs settings, etc.
1090 Generally, spreadsheet formulas and printers are simple things that
1091 don't need to do any fancy computing, so all potentially-dangerous
1092 parts of the Emacs Lisp environment can be excluded without cramping
1093 your style as a formula-writer.  See the documentation in @file{unsafep.el}
1094 for more info on how Lisp forms are classified as safe or unsafe.
1096 @node Spreadsheets with details and summary
1097 @section Spreadsheets with details and summary
1098 @cindex details and summary
1099 @cindex summary, and details
1101 A common organization for spreadsheets is to have a bunch of ``detail''
1102 rows, each perhaps describing a transaction, and then a set of
1103 ``summary'' rows that each show reduced data for some subset of the
1104 details.  @acronym{SES} supports this organization via the @code{ses-select}
1105 function.
1107 @table @code
1108 @item (ses-select @var{fromrange} @var{test} @var{torange})
1109 Returns a subset of @var{torange}.  For each member in @var{fromrange}
1110 that is equal to @var{test}, the corresponding member of @var{torange}
1111 is included in the result.
1112 @end table
1114 Example of use:
1115 @lisp
1116 (ses-average (ses-select (ses-range A1 A5) 'Smith (ses-range B1 B5)))
1117 @end lisp
1118 This computes the average of the B column values for those rows whose
1119 A column value is the symbol 'Smith.
1121 Arguably one could specify only @var{fromrange} plus
1122 @var{to-row-offset} and @var{to-column-offset}.  The @var{torange} is
1123 stated explicitly to ensure that the formula will be recalculated if
1124 any cell in either range is changed.
1126 File @file{etc/ses-example.el} in the Emacs distribution is an example of a
1127 details-and-summary spreadsheet.
1130 @c ===================================================================
1132 @node For Gurus
1133 @chapter For Gurus
1134 @cindex advanced features
1136 @menu
1137 * Deferred updates::
1138 * Nonrelocatable references::
1139 * The data area::
1140 * Buffer-local variables in spreadsheets::
1141 * Uses of defadvice in @acronym{SES}::
1142 @end menu
1144 @node Deferred updates
1145 @section Deferred updates
1146 @cindex deferred updates
1147 @cindex updates, deferred
1148 @vindex run-with-idle-timer
1150 To save time by avoiding redundant computations, cells that need
1151 recalculation due to changes in other cells are added to a set.  At
1152 the end of the command, each cell in the set is recalculated once.
1153 This can create a new set of cells that need recalculation.  The
1154 process is repeated until either the set is empty or it stops changing
1155 (due to circular references among the cells).  In extreme cases, you
1156 might see progress messages of the form ``Recalculating... (@var{nnn}
1157 cells left)''.  If you interrupt the calculation using @kbd{C-g}, the
1158 spreadsheet will be left in an inconsistent state, so use @kbd{C-_} or
1159 @kbd{C-c C-l} to fix it.
1161 To save even more time by avoiding redundant writes, cells that have
1162 changes are added to a set instead of being written immediately to the
1163 data area.  Each cell in the set is written once, at the end of the
1164 command.  If you change vast quantities of cells, you might see a
1165 progress message of the form ``Writing... (@var{nnn} cells left)''.
1166 These deferred cell-writes cannot be interrupted by @kbd{C-g}, so
1167 you'll just have to wait.
1169 @acronym{SES} uses @code{run-with-idle-timer} to move the cell underline when
1170 Emacs will be scrolling the buffer after the end of a command, and
1171 also to narrow and underline after @kbd{C-x C-v}.  This is visible as
1172 a momentary glitch after C-x C-v and certain scrolling commands.  You
1173 can type ahead without worrying about the glitch.
1176 @node Nonrelocatable references
1177 @section Nonrelocatable references
1178 @cindex nonrelocatable references
1179 @cindex references, nonrelocatable
1181 @kbd{C-y} relocates all cell-references in a pasted formula, while
1182 @kbd{C-u C-y} relocates none of the cell-references.  What about mixed
1183 cases?
1185 The best way is to rename cells that you do not want to be relocatable
1186 by using @code{ses-rename-cell}.
1187 @findex ses-rename-cell
1188 Cells that do not have an A1-like name style are not relocated on
1189 yank. Using this method, the concerned cells won't be relocated
1190 whatever formula they appear in. Please note however that when a
1191 formula contains some range @code{(ses-range @var{cell1} @var{cell2})}
1192 then in the yanked formula each range bound @var{cell1} and
1193 @var{cell2} are relocated, or not, independently, depending on whether
1194 they are A1-like or renamed.
1196 An alternative method is to use
1197 @lisp
1198 (symbol-value 'B3)
1199 @end lisp
1200 to make an @dfn{absolute reference}.  The formula relocator skips over
1201 quoted things, so this will not be relocated when pasted or when
1202 rows/columns are inserted/deleted.  However, B3 will not be recorded
1203 as a dependency of this cell, so this cell will not be updated
1204 automatically when B3 is changed, this is why using
1205 @code{ses-rename-cell} is most of the time preferable.
1207 The variables @code{row} and @code{col} are dynamically bound while a
1208 cell formula is being evaluated.  You can use
1209 @lisp
1210 (ses-cell-value row 0)
1211 @end lisp
1212 to get the value from the leftmost column in the current row.  This
1213 kind of dependency is also not recorded.
1216 @node The data area
1217 @section The data area
1218 @cindex data area
1219 @findex ses-reconstruct-all
1221 Begins with an 014 character, followed by sets of cell-definition
1222 macros for each row, followed by the set of local printer
1223 definitions, followed by column-widths, column-printers,
1224 default-printer, and header-row.  Then there's the global parameters
1225 (file-format ID, row count, column count, local printer count) and the
1226 local variables (specifying @acronym{SES} mode for the buffer, etc.).
1228 When a @acronym{SES} file is loaded, first the global parameters are
1229 loaded, then the entire data area is @code{eval}ed, and finally the local
1230 variables are processed.
1232 You can edit the data area, but don't insert or delete any newlines
1233 except in the local-variables part, since @acronym{SES} locates things by
1234 counting newlines.  Use @kbd{C-x C-e} at the end of a line to install
1235 your edits into the spreadsheet data structures (this does not update
1236 the print area, use, e.g., @kbd{C-c C-l} for that).
1238 The data area is maintained as an image of spreadsheet data
1239 structures that area stored in buffer-local variables.  If the data
1240 area gets messed up, you can try reconstructing the data area from the
1241 data structures:
1243 @table @kbd
1244 @item C-c M-C-l
1245 (@code{ses-reconstruct-all}).
1246 @end table
1249 @node Buffer-local variables in spreadsheets
1250 @section Buffer-local variables in spreadsheets
1251 @cindex buffer-local variables
1252 @cindex variables, buffer-local
1254 You can add additional local variables to the list at the bottom of
1255 the data area, such as hidden constants you want to refer to in your
1256 formulas.
1258 You can override the variable @code{ses--symbolic-formulas} to be a list of
1259 symbols (as parenthesized strings) to show as completions for the @kbd{'}
1260 command.  This initial completions list is used instead of the actual
1261 set of symbols-as-formulas in the spreadsheet.
1263 For an example of this, see file @file{etc/ses-example.ses}.
1265 If (for some reason) you want your formulas or printers to save data
1266 into variables, you must declare these variables as buffer-locals in
1267 order to avoid a virus warning.
1269 You can define functions by making them values for the fake local
1270 variable @code{eval}.  Such functions can then be used in your
1271 formulas and printers, but usually each @code{eval} is presented to
1272 the user during file loading as a potential virus.  This can get
1273 annoying.
1275 You can define functions in your @file{.emacs} file.  Other people can
1276 still read the print area of your spreadsheet, but they won't be able
1277 to recalculate or reprint anything that depends on your functions.  To
1278 avoid virus warnings, each function used in a formula needs
1279 @lisp
1280 (put 'your-function-name 'safe-function t)
1281 @end lisp
1283 @node Uses of defadvice in @acronym{SES}
1284 @section Uses of defadvice in @acronym{SES}
1285 @cindex defadvice
1286 @cindex undo-more
1287 @cindex copy-region-as-kill
1288 @cindex yank
1290 @table @code
1291 @item undo-more
1292 Defines a new undo element format (@var{fun} . @var{args}), which
1293 means ``undo by applying @var{fun} to @var{args}''.  For spreadsheet
1294 buffers, it allows undos in the data area even though that's outside
1295 the narrowing.
1297 @item copy-region-as-kill
1298 When copying from the print area of a spreadsheet, treat the region as
1299 a rectangle and attach each cell's formula and printer as 'ses
1300 properties.
1302 @item yank
1303 When yanking into the print area of a spreadsheet, first try to yank
1304 as cells (if the yank text has 'ses properties), then as tab-separated
1305 formulas, then (if all else fails) as a single formula for the current
1306 cell.
1307 @end table
1309 @c ===================================================================
1310 @node Index
1311 @unnumbered Index
1313 @printindex cp
1315 @c ===================================================================
1317 @node Acknowledgments
1318 @unnumbered Acknowledgments
1320 Coding by:
1321 @quotation
1322 @c jyavner@@member.fsf.org
1323 Jonathan Yavner,
1324 @c monnier@@gnu.org
1325 Stefan Monnier,
1326 @c shigeru.fukaya@@gmail.com
1327 Shigeru Fukaya
1328 @end quotation
1330 @noindent
1331 Texinfo manual by:
1332 @quotation
1333 @c jyavner@@member.fsf.org
1334 Jonathan Yavner,
1335 @c brad@@chenla.org
1336 Brad Collins
1337 @end quotation
1339 @noindent
1340 Ideas from:
1341 @quotation
1342 @c christoph.conrad@@gmx.de
1343 Christoph Conrad,
1344 @c cyberbob@@redneck.gacracker.org
1345 CyberBob,
1346 @c syver-en@@online.no
1347 Syver Enstad,
1348 @c fischman@@zion.bpnetworks.com
1349 Ami Fischman,
1350 @c Thomas.Gehrlein@@t-online.de
1351 Thomas Gehrlein,
1352 @c c.f.a.johnson@@rogers.com
1353 Chris F.A. Johnson,
1354 @c lyusong@@hotmail.com
1355 Yusong Li,
1356 @c juri@@jurta.org
1357 Juri Linkov,
1358 @c maierh@@myself.com
1359 Harald Maier,
1360 @c anash@@san.rr.com
1361 Alan Nash,
1362 @c pinard@@iro.umontreal.ca
1363 François Pinard,
1364 @c ppinto@@cs.cmu.edu
1365 Pedro Pinto,
1366 @c xsteve@@riic.at
1367 Stefan Reichör,
1368 @c epameinondas@@gmx.de
1369 Oliver Scholz,
1370 @c rms@@gnu.org
1371 Richard M. Stallman,
1372 @c teirllm@@dms.auburn.edu
1373 Luc Teirlinck,
1374 @c jotto@@pobox.com
1375 J. Otto Tennant,
1376 @c jphil@@acs.pagesjaunes.fr
1377 Jean-Philippe Theberge
1378 @end quotation
1380 @c ===================================================================
1382 @node GNU Free Documentation License
1383 @appendix GNU Free Documentation License
1384 @include doclicense.texi
1386 @bye