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