diff options
| author | Vincent Belaïche | 2016-07-28 17:41:21 +0200 |
|---|---|---|
| committer | Vincent Belaïche | 2016-07-28 17:41:21 +0200 |
| commit | 41b28dea8587c13b0bc59c1ec70b65afab3aeeca (patch) | |
| tree | df194b2e078802337f57ad8af0e709bd994d84ee /doc | |
| parent | 3f4f21b406a56d504c03a9bab3ac0949d8774c17 (diff) | |
| download | emacs-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.texi | 189 |
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 | ||
| 388 | Printer functions convert binary cell values into the print forms that | 378 | Printer functions convert binary cell values into the print forms that |
| 389 | Emacs will display on the screen. | 379 | Emacs will display on the screen. |
| 390 | 380 | ||
| 391 | A 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 | |||
| 392 | When configuring what printer function applies (@pxref{Configuring | ||
| 393 | what printer function applies}), you can enter a printer function as | ||
| 394 | one of the following: | ||
| 395 | |||
| 396 | @itemize | ||
| 397 | @item | ||
| 398 | A format string, like @samp{"$%.2f"}. The result | ||
| 392 | string is right-aligned within the print cell. To get left-alignment, | 399 | string is right-aligned within the print cell. To get left-alignment, |
| 393 | use parentheses: @samp{("$%.2f")}. A printer can also be a | 400 | use parentheses: @samp{("$%.2f")}. |
| 394 | one-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 | 402 | A printer can also be a one-argument function, the result of which is |
| 396 | a lambda, you can use @kbd{M-@key{TAB}} to complete the names of symbols. | 403 | a string (right-aligned) or list of one string (left-aligned). Such a |
| 404 | function can be in turn configured as: | ||
| 405 | @itemize | ||
| 406 | @item | ||
| 407 | A 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 | |||
| 417 | While typing in a lambda, you can use @kbd{M-@key{TAB}} to complete | ||
| 418 | the names of symbols. | ||
| 419 | @item | ||
| 420 | A symbol referring to a standard printer function (@pxref{Standard | ||
| 421 | printer functions}). | ||
| 422 | @item | ||
| 423 | A symbol referring to a local printer function (@pxref{Local printer | ||
| 424 | functions}). | ||
| 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 | ||
| 398 | Each cell has a printer. If @code{nil}, the column-printer for the cell's | 434 | Each cell has a printer. If @code{nil}, the column-printer for the cell's |
| 399 | column is used. If that is also @code{nil}, the default-printer for the | 435 | column 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 | ||
| 404 | Enter a printer for current cell or range (@code{ses-read-cell-printer}). | 441 | Enter 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 | ||
| 407 | Enter a printer for the current column (@code{ses-read-column-printer}). | 445 | Enter 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 | ||
| 410 | Enter the default printer for the spreadsheet | 449 | Enter 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 | ||
| 414 | The @code{ses-read-@r{XXX}-printer} commands have their own minibuffer | 453 | The @code{ses-read-@var{xxx}-printer} commands have their own |
| 415 | history, which is preloaded with the set of all printers used in this | 454 | minibuffer history, which is preloaded with the set of all printers |
| 416 | spreadsheet, plus the standard printers. | 455 | used in this spreadsheet, plus the standard printers (@pxref{Standard |
| 456 | printer functions}) and the local printers (@pxref{Local printer | ||
| 457 | functions}). | ||
| 417 | 458 | ||
| 418 | The standard printers are suitable only for cells, not columns or | 459 | @node Standard printer functions |
| 419 | default, because they format the value using the column-printer (or | 460 | @subsection Standard printer functions |
| 420 | default-printer if @code{nil}) and then center the result: | ||
| 421 | 461 | ||
| 422 | @table @code | 462 | |
| 463 | Except for @code{ses-prin1}, the other standard printers are suitable | ||
| 464 | only for cells, not columns or default, because they format the value | ||
| 465 | using the column-printer (or default-printer if @code{nil}) and then | ||
| 466 | center the result: | ||
| 467 | |||
| 468 | @ftable @code | ||
| 423 | @item ses-center | 469 | @item ses-center |
| 424 | Just centering. | 470 | Just 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 |
| 436 | Centering with tildes (~) and spill-over. | 482 | Centering with tildes (~) and spill-over. |
| 437 | @end table | ||
| 438 | 483 | ||
| 484 | @item ses-prin1 | ||
| 485 | This is the fallback printer, used when calling the configured printer | ||
| 486 | throws some error. | ||
| 487 | @end ftable | ||
| 488 | |||
| 489 | @node Local printer functions | ||
| 490 | @subsection Local printer functions | ||
| 491 | |||
| 492 | @findex ses-define-local-printer | ||
| 439 | You can define printer function local to a sheet with the command | 493 | You 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 |
| 445 | reprinted accordingly. | 499 | reprinted accordingly. |
| 446 | 500 | ||
| 447 | When you define a printer function with a lambda expression taking one | 501 | Sometimes there are local printers that you want to define or |
| 448 | argument, please take care that the returned value is a string, or a | 502 | re-define automatically every time you open a sheet. For instance |
| 449 | list containing a string, even when the input argument has an | 503 | imagine that you want to define/re-define automatically a local |
| 504 | printer @code{euro} to display a number like an amount of euros, that | ||
| 505 | is 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, | ||
| 507 | you 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 | |||
| 522 | If 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 | ||
| 525 | the definition will occur only if a local printer with the same name | ||
| 526 | in not already defined. | ||
| 527 | |||
| 528 | |||
| 529 | @node Writing a lambda printer function | ||
| 530 | @subsection Writing a lambda printer function | ||
| 531 | |||
| 532 | You can write a printer function with a lambda expression taking one | ||
| 533 | argument in two cases: | ||
| 534 | |||
| 535 | @itemize | ||
| 536 | @item | ||
| 537 | when you configure the printer function applying to a cell or column, or | ||
| 538 | @item | ||
| 539 | when you define a local printer function with command | ||
| 540 | @code{ses-define-local-printer}. | ||
| 541 | @end itemize | ||
| 542 | |||
| 543 | When doing so, please take care that the returned value is a string, | ||
| 544 | or a list containing a string, even when the input argument has an | ||
| 450 | unexpected value. Here is an example: | 545 | unexpected 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 | ||
| 460 | This example will: | 555 | This example will: |
| 556 | |||
| 461 | @itemize | 557 | @itemize |
| 462 | @item | 558 | @item |
| 463 | When the cell is empty (ie.@: when @code{val} is @code{nil}), print an | 559 | When 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 | |||
| 467 | fixed-point notation with one decimal after point | 563 | fixed-point notation with one decimal after point |
| 468 | @item | 564 | @item |
| 469 | Otherwise, handle the value as erroneous by printing it as an | 565 | Otherwise, handle the value as erroneous by printing it as an |
| 470 | s-expression (using @code{prin1}), centered and surrounded by @code{#} | 566 | s-expression (using @code{ses-prin1}), centered and surrounded by |
| 471 | filling. | 567 | @code{#} filling. |
| 472 | @end itemize | 568 | @end itemize |
| 473 | 569 | ||
| 570 | Another precaution to take is to avoid stack-overflow (due to a | ||
| 571 | printer function indefintely recursively re-calling itself). This can | ||
| 572 | happen mistakenly when you use a local printer as a column printer, | ||
| 573 | and this local printer implicitely call the current column printer, so | ||
| 574 | will call itself recursively. Imagine for instance that you want to | ||
| 575 | create some local printer @code{=fill} that would center the content | ||
| 576 | of a cell and surround it by equal signs @code{=}, and you do it this | ||
| 577 | way: | ||
| 578 | |||
| 579 | @lisp | ||
| 580 | (lambda (x) | ||
| 581 | (cond | ||
| 582 | ((null x) "") | ||
| 583 | (t (ses-center x 0 ?=)))) | ||
| 584 | @end lisp | ||
| 474 | 585 | ||
| 586 | Because @code{=fill} uses standard printer @code{ses-center} without | ||
| 587 | passing explicitely any printer to it, @code{ses-center} will call the | ||
| 588 | current column printer if any or the spreadsheet default printer | ||
| 589 | otherwise. So using @code{=fill} as a column printer will result in a | ||
| 590 | stack overflow in this column. SES does not make any check for that, | ||
| 591 | you just have to be careful. For instance re-write @code{=fill} like | ||
| 592 | this: | ||
| 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 | ||
| 602 | The code above applies the @code{=} filling only to strings, it also | ||
| 603 | surrounds the string by one space on each side before filling with | ||
| 604 | @code{=} signs. So string @samp{Foo} will be displayed like @samp{@w{=== | ||
| 605 | Foo ===}} in an 11 character wide column. Anything else than empty cell | ||
| 606 | or 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 |