Next: , Previous: Formula syntax for Calc, Up: The spreadsheet


3.4.3 Emacs Lisp forms as formulas

It is also possible to write a formula in Emacs Lisp; this can be useful for string manipulation and control structures. If a formula starts with a single quote followed by an opening parenthesis, then it is evaluated as a lisp form. The evaluation should return either a string or a number. Just as with calc formulas, you can specify modes and a printf format after a semicolon. A reference will be replaced with a string (in double quotes) containing the field. If you provide the `N' mode switch, all referenced elements will be numbers. Ranges are inserted as space-separated fields, so you can embed them in list or vector syntax. A few examples, note how the `N' mode is used when we do computations in lisp.

     Swap the first two characters of the content of column 1
       '(concat (substring $1 1 2) (substring $1 0 1) (substring $1 2))
     Add columns 1 and 2, equivalent to the Calc's $1+$2
       '(+ $1 $2);N
     Compute the sum of columns 1-4, like Calc's vsum($1..$4)
       '(apply '+ '($1..$4));N