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