emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Jason Riedy <jason@acm.org>
To: emacs-orgmode@gnu.org
Subject: [PATCH 3/3] Allow functions for some orgtbl parameters.
Date: Sun, 02 Mar 2008 21:51:48 -0800	[thread overview]
Message-ID: <87d4qc8idn.fsf_-_@sparse.yi.org> (raw)
In-Reply-To: <87hcfo8j2l.fsf@sparse.yi.org> (jason@acm.org's message of "Sun, 02 Mar 2008 21:36:50 -0800")

Functions and dynamic binding permit some fun uses, including
gathering up header names for use in SQL insert statements.

Signed-off-by: Jason Riedy <jason@acm.org>
---
 org.el   |   38 ++++++++++++++++++++++++++++++--------
 org.texi |    5 ++++-
 2 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/org.el b/org.el
index f38f6ae..6f17f1e 100644
--- a/org.el
+++ b/org.el
@@ -11778,16 +11778,26 @@ First element has index 0, or I0 if given."
 	    (mapcar
 	     (lambda (f)
 	       (setq i (1+ i))
-	       (let* ((fmt (if (consp fmt) (plist-get fmt i) fmt))
-		      (efmt (if (consp efmt) (plist-get efmt i) efmt))
+	       (let* ((fmt (if (and (not (functionp fmt)) (consp fmt))
+			       (plist-get fmt i) fmt))
+		      (efmt (if (and (not (functionp efmt)) (consp efmt))
+				(plist-get efmt i) efmt))
 		      (f (if (and efmt (string-match orgtbl-exp-regexp f))
-			     (format efmt (match-string 1 f)
-					  (match-string 2 f))
+			     (if (functionp efmt) (funcall efmt
+							   (match-string 1 f)
+							   (match-string 2 f))
+			       (format efmt (match-string 1 f)
+				       (match-string 2 f)))
 			   f)))
-		 (if fmt (format fmt f) f)))
+		 (cond ((functionp fmt) (funcall fmt f))
+		       (fmt (format fmt f))
+		       (t f))))
 	     line)))
-      (push (if lfmt (apply 'format lfmt line)
-	      (concat lstart (mapconcat 'identity line sep) lend))
+      (push (cond ((functionp lfmt) (funcall lfmt line))
+		  (lfmt (apply 'format lfmt line))
+		  (t (concat (if (functionp lstart) (funcall lstart) lstart)
+			     (mapconcat 'identity line sep)
+			     (if (functionp lend) (funcall lend) lend))))
 	    rtn))))
 
 (defun orgtbl-format-section (section-stopper)
@@ -11821,11 +11831,18 @@ Valid parameters are
 :hline      String to be inserted on horizontal separation lines.
             May be nil to ignore hlines.
 
+:sep        Separator between two fields
+
+  Each in the following group may be either a string or a function
+  of no arguments returning a string:
 :lstart     String to start a new table line.
 :llstart    String to start the last table line, defaults to :lstart.
 :lend       String to end a table line
 :llend      String to end the last table line, defaults to :lend.
-:sep        Separator between two fields
+
+  Each in the following group may be a string, a function of one
+  argument (the field or line) returning a string, or a plist
+  mapping columns to either of the above:
 :lfmt       Format for entire line, with enough %s to capture all fields.
             If this is present, :lstart, :lend, and :sep are ignored.
 :llfmt      Format for the entire last line, defaults to :lfmt.
@@ -11840,6 +11857,7 @@ Valid parameters are
             All lines before the first hline are treated as header.
             If any of these is not present, the data line value is used.
 
+  This may be either a string or a function of two arguments:
 :efmt       Use this format to print numbers with exponentials.
             The format should have %s twice for inserting mantissa
             and exponent, for example \"%s\\\\times10^{%s}\".  This
@@ -11906,11 +11924,13 @@ LaTeX are:
            original field value.  For example, to wrap everything in dollars,
            use :fmt \"$%s$\".  This may also be a property list with column
            numbers and formats.  For example :fmt (2 \"$%s$\" 4 \"%s%%\")
+           The format may also be a function that formats its one argument.
 
 :efmt      Format for transforming numbers with exponentials.  The format
            should have %s twice for inserting mantissa and exponent, for
            example \"%s\\\\times10^{%s}\".  LaTeX default is \"%s\\\\,(%s)\".
            This may also be a property list with column numbers and formats.
+           The format may also be a function that formats its two arguments.
 
 :llend     If you find too much space below the last line of a table,
            pass a value of \"\" for :llend to suppress the final \\\\.
@@ -11972,6 +11992,8 @@ TeXInfo are:
                    everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
                    This may also be a property list with column numbers and
                    formats.  For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
+                   Each format also may be a function that formats its one
+                   argument.
 
 :cf \"f1 f2..\"    The column fractions for the table.  By default these
                    are computed automatically from the width of the columns
diff --git a/org.texi b/org.texi
index eae2db0..2ab0dda 100644
--- a/org.texi
+++ b/org.texi
@@ -8255,6 +8255,8 @@ A format to be used to wrap each field, should contain @code{%s} for the
 original field value.  For example, to wrap each field value in dollars,
 you could use @code{:fmt "$%s$"}.  This may also be a property list with
 column numbers and formats. for example @code{:fmt (2 "$%s$" 4 "%s\\%%")}.
+A function of one argument can be used in place of the strings; the
+function must return a formatted string.
 
 @item :efmt efmt
 Use this format to print numbers with exponentials.  The format should
@@ -8263,7 +8265,8 @@ have @code{%s} twice for inserting mantissa and exponent, for example
 may also be a property list with column numbers and formats, for example
 @code{:efmt (2 "$%s\\times10^@{%s@}$" 4 "$%s\\cdot10^@{%s@}$")}.  After
 @code{efmt} has been applied to a value, @code{fmt} will also be
-applied.
+applied.  Similar to @code{fmt}, functions of two arguments can be
+supplied instead of strings.
 @end table
 
 @node Translator functions, Radio lists, A LaTeX example, Tables in arbitrary syntax
-- 
1.5.4.3

  reply	other threads:[~2008-03-03  5:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-03  0:37 [PATCH] Support last-line specializers Jason Riedy
2008-03-03  5:36 ` [respun PATCH 2/3] " jason
2008-03-03  5:51   ` Jason Riedy [this message]
2008-03-03  6:06     ` How I'm using these: Generating SQL insert statements Jason Riedy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87d4qc8idn.fsf_-_@sparse.yi.org \
    --to=jason@acm.org \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).