aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorVincent Belaïche2016-07-28 17:41:21 +0200
committerVincent Belaïche2016-07-28 17:41:21 +0200
commit41b28dea8587c13b0bc59c1ec70b65afab3aeeca (patch)
treedf194b2e078802337f57ad8af0e709bd994d84ee /doc
parent3f4f21b406a56d504c03a9bab3ac0949d8774c17 (diff)
downloademacs-41b28dea8587c13b0bc59c1ec70b65afab3aeeca.tar.gz
emacs-41b28dea8587c13b0bc59c1ec70b65afab3aeeca.zip
Enable addition of local printers from a mode hook.
* doc/misc/ses.texi (Printer functions): Split the node into 5 sub-nodes + add some extra documentation. (Various kinds of printer functions): Make an itemisation to disintguish better the 3 types of printers, give an example of lambda printer definition. (Standard printer functions): Add documentation for ses-prin1 printer function. (Local printer functions): Add documentation for creating local printers programmatically from a hook. (Writing a lambda printer function): Add documentation about anti-stackoverflow precautions to take when you call the standard printer functions from inside a local printer. * lisp/ses.el (ses-standard-printer-functions): Add ses-prin1 among standard printer function, and update docstring accordingly. (ses-call-printer, ses-export-tab): Call `ses-prin1' instead of prin1-to-string. (ses-define-local-printer): Add definition to arguments so that a local printer can be defined programmatically from a mode hook. Make docstring more substantial. Use completing read for local printer name input. Plus some minor optimization. (ses-define-if-new-local-printer): New defsubst. (ses-center, ses-center-span, ses-dashfill) (ses-dashfill-span, ses-tildefill-span): Allow to pass printer as an optional argument to superseed column printer/default spreadsheet printer. (ses-prin1): New defun.
Diffstat (limited to 'doc')
-rw-r--r--doc/misc/ses.texi189
1 files changed, 160 insertions, 29 deletions
diff --git a/doc/misc/ses.texi b/doc/misc/ses.texi
index 8b0bb82f174..1c5070b38a9 100644
--- a/doc/misc/ses.texi
+++ b/doc/misc/ses.texi
@@ -374,26 +374,62 @@ Undo previous action (@code{(undo)}).
374@cindex printer functions 374@cindex printer functions
375@cindex cell formatting 375@cindex cell formatting
376@cindex formatting cells 376@cindex formatting cells
377@findex ses-read-cell-printer
378@findex ses-read-column-printer
379@findex ses-read-default-printer
380@findex ses-define-local-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 377
388Printer functions convert binary cell values into the print forms that 378Printer functions convert binary cell values into the print forms that
389Emacs will display on the screen. 379Emacs will display on the screen.
390 380
391A printer can be a format string, like @samp{"$%.2f"}. The result 381@menu
382* Various kinds of printer functions::
383* Configuring what printer function applies::
384* Standard printer functions::
385* Local printer functions::
386* Writing a lambda printer function::
387@end menu
388
389@node Various kinds of printer functions
390@subsection Various kinds of printer functions
391
392When configuring what printer function applies (@pxref{Configuring
393what printer function applies}), you can enter a printer function as
394one of the following:
395
396@itemize
397@item
398A format string, like @samp{"$%.2f"}. The result
392string is right-aligned within the print cell. To get left-alignment, 399string is right-aligned within the print cell. To get left-alignment,
393use parentheses: @samp{("$%.2f")}. A printer can also be a 400use parentheses: @samp{("$%.2f")}.
394one-argument function (a symbol or a lambda), whose result is a string 401@item
395(right-aligned) or list of one string (left-aligned). While typing in 402A printer can also be a one-argument function, the result of which is
396a lambda, you can use @kbd{M-@key{TAB}} to complete the names of symbols. 403a string (right-aligned) or list of one string (left-aligned). Such a
404function can be in turn configured as:
405@itemize
406@item
407A lambda expression, for instance:
408
409@lisp
410(lambda (x)
411 (cond
412 ((null x) "")
413 ((numberp x) (format "%.2f" x))
414 (t (ses-center-span x ?# 'ses-prin1))))
415@end lisp
416
417While typing in a lambda, you can use @kbd{M-@key{TAB}} to complete
418the names of symbols.
419@item
420A symbol referring to a standard printer function (@pxref{Standard
421printer functions}).
422@item
423A symbol referring to a local printer function (@pxref{Local printer
424functions}).
425@end itemize
426
427
428@end itemize
429
430
431@node Configuring what printer function applies
432@subsection Configuring what printer function applies
397 433
398Each cell has a printer. If @code{nil}, the column-printer for the cell's 434Each 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 435column is used. If that is also @code{nil}, the default-printer for the
@@ -401,25 +437,35 @@ spreadsheet is used.
401 437
402@table @kbd 438@table @kbd
403@item p 439@item p
440@findex ses-read-cell-printer
404Enter a printer for current cell or range (@code{ses-read-cell-printer}). 441Enter a printer for current cell or range (@code{ses-read-cell-printer}).
405 442
406@item M-p 443@item M-p
444@findex ses-read-column-printer
407Enter a printer for the current column (@code{ses-read-column-printer}). 445Enter a printer for the current column (@code{ses-read-column-printer}).
408 446
409@item C-c C-p 447@item C-c C-p
448@findex ses-read-default-printer
410Enter the default printer for the spreadsheet 449Enter the default printer for the spreadsheet
411(@code{ses-read-default-printer}). 450(@code{ses-read-default-printer}).
412@end table 451@end table
413 452
414The @code{ses-read-@r{XXX}-printer} commands have their own minibuffer 453The @code{ses-read-@var{xxx}-printer} commands have their own
415history, which is preloaded with the set of all printers used in this 454minibuffer history, which is preloaded with the set of all printers
416spreadsheet, plus the standard printers. 455used in this spreadsheet, plus the standard printers (@pxref{Standard
456printer functions}) and the local printers (@pxref{Local printer
457functions}).
417 458
418The standard printers are suitable only for cells, not columns or 459@node Standard printer functions
419default, because they format the value using the column-printer (or 460@subsection Standard printer functions
420default-printer if @code{nil}) and then center the result:
421 461
422@table @code 462
463Except for @code{ses-prin1}, the other standard printers are suitable
464only for cells, not columns or default, because they format the value
465using the column-printer (or default-printer if @code{nil}) and then
466center the result:
467
468@ftable @code
423@item ses-center 469@item ses-center
424Just centering. 470Just centering.
425 471
@@ -434,8 +480,16 @@ Centering with dashes and spill-over.
434 480
435@item ses-tildefill-span 481@item ses-tildefill-span
436Centering with tildes (~) and spill-over. 482Centering with tildes (~) and spill-over.
437@end table
438 483
484@item ses-prin1
485This is the fallback printer, used when calling the configured printer
486throws some error.
487@end ftable
488
489@node Local printer functions
490@subsection Local printer functions
491
492@findex ses-define-local-printer
439You can define printer function local to a sheet with the command 493You can define printer function local to a sheet with the command
440@code{ses-define-local-printer}. For instance, define a printer 494@code{ses-define-local-printer}. For instance, define a printer
441@samp{foo} to @code{"%.2f"}, and then use symbol @samp{foo} as a 495@samp{foo} to @code{"%.2f"}, and then use symbol @samp{foo} as a
@@ -444,9 +498,50 @@ printer function. Then, if you call again
444@code{"%.3f"}, all the cells using printer @samp{foo} will be 498@code{"%.3f"}, all the cells using printer @samp{foo} will be
445reprinted accordingly. 499reprinted accordingly.
446 500
447When you define a printer function with a lambda expression taking one 501Sometimes there are local printers that you want to define or
448argument, please take care that the returned value is a string, or a 502re-define automatically every time you open a sheet. For instance
449list containing a string, even when the input argument has an 503imagine that you want to define/re-define automatically a local
504printer @code{euro} to display a number like an amount of euros, that
505is to say number @code{3.1} would be displayed as
506@code{3.10@dmn{}@euro{}}. To do so in any non read-only SES buffer,
507you can add some code like this to your @file{.emacs} init file:
508
509@lisp
510(defun my-ses-mode-hook ()
511 (unless buffer-read-only
512 (ses-define-local-printer
513 'euro
514 (lambda (x)
515 (cond
516 ((null x) "")
517 ((numberp x) (format "%.2f€" x))
518 (t (ses-center-span x ?# 'ses-prin1)))))))
519(add-hook 'ses-mode-hook 'my-ses-mode-hook)
520@end lisp
521
522If you replace command @code{ses-define-local-printer} by function
523@code{ses-define-if-new-local-printer}
524@findex ses-define-if-new-local-printer
525the definition will occur only if a local printer with the same name
526in not already defined.
527
528
529@node Writing a lambda printer function
530@subsection Writing a lambda printer function
531
532You can write a printer function with a lambda expression taking one
533argument in two cases:
534
535@itemize
536@item
537when you configure the printer function applying to a cell or column, or
538@item
539when you define a local printer function with command
540@code{ses-define-local-printer}.
541@end itemize
542
543When doing so, please take care that the returned value is a string,
544or a list containing a string, even when the input argument has an
450unexpected value. Here is an example: 545unexpected value. Here is an example:
451 546
452@example 547@example
@@ -454,10 +549,11 @@ unexpected value. Here is an example:
454 (cond 549 (cond
455 ((null val) "") 550 ((null val) "")
456 ((and (numberp val) (>= val 0)) (format "%.1f" val)) 551 ((and (numberp val) (>= val 0)) (format "%.1f" val))
457 (t (ses-center-span (format "%S" val) ?#)))) 552 (t (ses-center-span val ?# 'ses-prin1))))
458@end example 553@end example
459 554
460This example will: 555This example will:
556
461@itemize 557@itemize
462@item 558@item
463When the cell is empty (ie.@: when @code{val} is @code{nil}), print an 559When the cell is empty (ie.@: when @code{val} is @code{nil}), print an
@@ -467,12 +563,47 @@ When the cell value is a non negative number, format the the value in
467fixed-point notation with one decimal after point 563fixed-point notation with one decimal after point
468@item 564@item
469Otherwise, handle the value as erroneous by printing it as an 565Otherwise, handle the value as erroneous by printing it as an
470s-expression (using @code{prin1}), centered and surrounded by @code{#} 566s-expression (using @code{ses-prin1}), centered and surrounded by
471filling. 567@code{#} filling.
472@end itemize 568@end itemize
473 569
570Another precaution to take is to avoid stack-overflow (due to a
571printer function indefintely recursively re-calling itself). This can
572happen mistakenly when you use a local printer as a column printer,
573and this local printer implicitely call the current column printer, so
574will call itself recursively. Imagine for instance that you want to
575create some local printer @code{=fill} that would center the content
576of a cell and surround it by equal signs @code{=}, and you do it this
577way:
578
579@lisp
580(lambda (x)
581 (cond
582 ((null x) "")
583 (t (ses-center x 0 ?=))))
584@end lisp
474 585
586Because @code{=fill} uses standard printer @code{ses-center} without
587passing explicitely any printer to it, @code{ses-center} will call the
588current column printer if any or the spreadsheet default printer
589otherwise. So using @code{=fill} as a column printer will result in a
590stack overflow in this column. SES does not make any check for that,
591you just have to be careful. For instance re-write @code{=fill} like
592this:
593
594@lisp
595(lambda (x)
596 (cond
597 ((null x) "")
598 ((stringp x) (ses-center x 0 ?= " %s "))
599 (t (ses-center-span x ?# 'ses-prin1))))
600@end lisp
475 601
602The code above applies the @code{=} filling only to strings, it also
603surrounds the string by one space on each side before filling with
604@code{=} signs. So string @samp{Foo} will be displayed like @samp{@w{===
605Foo ===}} in an 11 character wide column. Anything else than empty cell
606or non string is displayed like errouneous by using @code{#} filling.
476 607
477@node Clearing cells 608@node Clearing cells
478@section Clearing cells 609@section Clearing cells