emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH 0/4] Generalize orgtbl formatting with functions
@ 2008-04-16 21:39 Jason Riedy
  2008-04-16 21:39 ` [PATCH 1/4] Refactor orgtbl-to-generic; explicitly separate heading from body Jason Riedy
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Jason Riedy @ 2008-04-16 21:39 UTC (permalink / raw)
  To: emacs-orgmode

This is lead-up to a contrib function that generates SQL insertions
from a table.  Using functions for some of the orgtbl parameters opens
up many possibilities.

Jason Riedy (4):
  Refactor orgtbl-to-generic; explicitly separate heading from body.
  Support last-line specializers.
  Allow functions for some orgtbl parameters.
  Add a :remove-nil-lines parameter to orgtbl-to-generic.

 ChangeLog         |   45 +++++++++++++
 doc/org.texi      |    5 +-
 lisp/org-table.el |  190 ++++++++++++++++++++++++++++++++++++++---------------
 3 files changed, 187 insertions(+), 53 deletions(-)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] Refactor orgtbl-to-generic; explicitly separate heading from body.
  2008-04-16 21:39 [PATCH 0/4] Generalize orgtbl formatting with functions Jason Riedy
@ 2008-04-16 21:39 ` Jason Riedy
  2008-04-16 21:39 ` [PATCH 2/4] Support last-line specializers Jason Riedy
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Jason Riedy @ 2008-04-16 21:39 UTC (permalink / raw)
  To: emacs-orgmode

Parameters are fluidly bound as early as possible.  Added one
helper function, orgtbl-format-section, and removed one,
org-get-param.  Also cleaned org-format-line.

Signed-off-by: Jason Riedy <jason@acm.org>
---
 ChangeLog         |   16 +++++++
 lisp/org-table.el |  124 ++++++++++++++++++++++++++++++++--------------------
 2 files changed, 92 insertions(+), 48 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 9ae71a9..4569d3a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2008-04-15  Jason Riedy  <jason@acm.org>
+
+	* lisp/org-table.el (*orgtbl-table*, *orgtbl-rtn*): Dynamically
+	bound variables to hold the input collection of lines and output
+	formatted text.
+	(*orgtbl-hline*, *orgtbl-sep*, *orgtbl-fmt*, *orgtbl-efmt*,
+	(*orgtbl-lfmt*, *orgtbl-lstart*, *orgtbl-lend*): Dynamically bound
+	format parameters.
+	(orgtbl-format-line): New function encapsulating formatting for a
+	single line.
+	(orgtbl-format-section): Similar for each section.  Rebinding the
+	dynamic vars customizes the formatting for each section.
+	(orgtbl-to-generic): Use orgtbl-format-line and
+	orgtbl-format-section.
+	(org-get-param): Now unused, so delete.
+
 2008-04-15  Carsten Dominik  <dominik@science.uva.nl>
 
 	* lisp/org-agenda.el (org-agenda-columns-show-summaries)
diff --git a/lisp/org-table.el b/lisp/org-table.el
index 1e1bd85..3cc70c1 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -3602,15 +3602,51 @@ First element has index 0, or I0 if given."
     (insert txt)
     (goto-char pos)))
 
-(defun org-get-param (params header i sym &optional hsym)
-  "Get parameter value for symbol SYM.
-If this is a header line, actually get the value for the symbol with an
-additional \"h\" inserted after the colon.
-If the value is a protperty list, get the element for the current column.
-Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
-  (let ((val (plist-get params sym)))
-    (and hsym header (setq val (or (plist-get params hsym) val)))
-    (if (consp val) (plist-get val i) val)))
+;; Dynamically bound input and output for table formatting.
+(defvar *orgtbl-table* nil
+  "Carries the current table through formatting routines.")
+(defvar *orgtbl-rtn* nil
+  "Formatting routines push the output lines here.")
+;; Formatting parameters for the current table section.
+(defvar *orgtbl-hline* nil "Text used for horizontal lines")
+(defvar *orgtbl-sep* nil "Text used as a column separator")
+(defvar *orgtbl-fmt* nil "Format for each entry")
+(defvar *orgtbl-efmt* nil "Format for numbers")
+(defvar *orgtbl-lfmt* nil "Format for an entire line, overrides fmt")
+(defvar *orgtbl-lstart* nil "Text starting a row")
+(defvar *orgtbl-lend* nil "Text ending a row")
+
+(defun orgtbl-format-line (line)
+  "Format LINE as a table row."
+  (if (eq line 'hline) (if *orgtbl-hline* (push *orgtbl-hline* *orgtbl-rtn*))
+    (let* ((i 0)
+	   (line
+	    (mapcar
+	     (lambda (f)
+	       (setq i (1+ i))
+	       (let* ((*orgtbl-fmt* (if (consp *orgtbl-fmt*)
+				     (plist-get *orgtbl-fmt* i)
+				     *orgtbl-fmt*))
+		      (*orgtbl-efmt* (if (consp *orgtbl-efmt*)
+				      (plist-get *orgtbl-efmt* i)
+				      *orgtbl-efmt*))
+		      (f (if (and *orgtbl-efmt*
+				  (string-match orgtbl-exp-regexp f))
+			     (format *orgtbl-efmt* (match-string 1 f)
+				     (match-string 2 f))
+			   f)))
+		 (if *orgtbl-fmt* (format *orgtbl-fmt* f) f)))
+	     line)))
+      (push (if *orgtbl-lfmt* (apply 'format *orgtbl-lfmt* line)
+	      (concat *orgtbl-lstart* (mapconcat 'identity line *orgtbl-sep*)
+		      *orgtbl-lend*))
+	    *orgtbl-rtn*))))
+
+(defun orgtbl-format-section (section-stopper)
+  "Format lines until the first occurrence of SECTION-STOPPER."
+  (progn
+    (while (not (eq (car *orgtbl-table*) section-stopper))
+      (orgtbl-format-line (pop *orgtbl-table*)))))
 
 (defun orgtbl-to-generic (table params)
   "Convert the orgtbl-mode TABLE to some other format.
@@ -3658,51 +3694,43 @@ Valid parameters are
 In addition to this, the parameters :skip and :skipcols are always handled
 directly by `orgtbl-send-table'.  See manual."
   (interactive)
-  (let* ((p params)
-	 (splicep (plist-get p :splice))
-	 (hline (plist-get p :hline))
-	 rtn line i fm efm lfmt h)
-
-    ;; Do we have a header?
-    (if (and (not splicep) (listp (car table)) (memq 'hline table))
-	(setq h t))
+  (let* ((splicep (plist-get params :splice))
+	 (hline (plist-get params :hline))
+	 (*orgtbl-table* table)
+	 (*orgtbl-sep* (plist-get params :sep))
+	 (*orgtbl-efmt* (plist-get params :efmt))
+	 (*orgtbl-lstart* (plist-get params :lstart))
+	 (*orgtbl-lend* (plist-get params :lend))
+	 (*orgtbl-lfmt* (plist-get params :lfmt))
+	 (*orgtbl-fmt* (plist-get params :fmt))
+	 *orgtbl-rtn*)
 
     ;; Put header
     (unless splicep
-      (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
-
-    ;; Now loop over all lines
-    (while (setq line (pop table))
-      (if (eq line 'hline)
-	  ;; A horizontal separator line
-	  (progn (if hline (push hline rtn))
-		 (setq h nil))               ; no longer in header
-	;; A normal line.  Convert the fields, push line onto the result list
-	(setq i 0)
-	(setq line
-	      (mapcar
-	       (lambda (f)
-		 (setq i (1+ i)
-		       fm (org-get-param p h i :fmt :hfmt)
-		       efm (org-get-param p h i :efmt))
-		 (if (and efm (string-match orgtbl-exp-regexp f))
-		     (setq f (format
-			      efm (match-string 1 f) (match-string 2 f))))
-		 (if fm (setq f (format fm f)))
-		 f)
-	       line))
-	(if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
-	    (push (apply 'format lfmt line) rtn)
-	  (push (concat
-		 (org-get-param p h i :lstart :hlstart)
-		 (mapconcat 'identity line (org-get-param p h i :sep :hsep))
-		 (org-get-param p h i :lend :hlend))
-		rtn))))
+      (push (or (plist-get params :tstart) "ERROR: no :tstart") *orgtbl-rtn*))
+
+    ;; Do we have a heading section?  If so, format it and handle the
+    ;; trailing hline.
+    (if (and (not splicep) (listp (car *orgtbl-table*))
+	     (memq 'hline *orgtbl-table*))
+	(progn
+	  (let* ((*orgtbl-lstart* (or (plist-get params :hlstart)
+				      *orgtbl-lstart*))
+		 (*orgtbl-lend* (or (plist-get params :hlend) *orgtbl-lend*))
+		 (*orgtbl-lfmt* (or (plist-get params :hlfmt) *orgtbl-lfmt*))
+		 (*orgtbl-sep* (or (plist-get params :hlsep) *orgtbl-sep*))
+		 (*orgtbl-fmt* (or (plist-get params :hfmt) *orgtbl-fmt*)))
+	    (orgtbl-format-section 'hline))
+	  (if hline (push hline *orgtbl-rtn*))
+	  (pop *orgtbl-table*)))
+
+    ;; Now format the main section.
+    (orgtbl-format-section nil)
 
     (unless splicep
-      (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
+      (push (or (plist-get params :tend) "ERROR: no :tend") *orgtbl-rtn*))
 
-    (mapconcat 'identity (nreverse rtn) "\n")))
+    (mapconcat 'identity (nreverse *orgtbl-rtn*) "\n")))
 
 (defun orgtbl-to-latex (table params)
   "Convert the orgtbl-mode TABLE to LaTeX.
-- 
1.5.5.rc1.121.g1594

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/4] Support last-line specializers.
  2008-04-16 21:39 [PATCH 0/4] Generalize orgtbl formatting with functions Jason Riedy
  2008-04-16 21:39 ` [PATCH 1/4] Refactor orgtbl-to-generic; explicitly separate heading from body Jason Riedy
@ 2008-04-16 21:39 ` Jason Riedy
  2008-04-16 21:39 ` [PATCH 3/4] Allow functions for some orgtbl parameters Jason Riedy
  2008-04-16 21:39 ` [PATCH 4/4] Add a :remove-nil-lines parameter to orgtbl-to-generic Jason Riedy
  3 siblings, 0 replies; 5+ messages in thread
From: Jason Riedy @ 2008-04-16 21:39 UTC (permalink / raw)
  To: emacs-orgmode

Each of lstart, lend, and lfmt permits a last-line specialization
called llstart, etc. with corresponding heading versions.

Signed-off-by: Jason Riedy <jason@acm.org>
---
 ChangeLog         |   10 ++++++++++
 lisp/org-table.el |   32 ++++++++++++++++++++++++++++----
 2 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 4569d3a..13980bf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2008-04-15  Jason Riedy  <jason@acm.org>
 
+	* lisp/org-table.el (*orgtbl-llfmt*, *orgtbl-llstart*)
+	(*orgtbl-llend*): Dynamic variables for last-line formatting.
+	(orgtbl-format-section): Shift formatting to support detecting the
+	last line and formatting it specially.
+	(orgtbl-to-generic): Document :ll* formats.  Set to the non-ll
+	formats unless overridden.
+	(orgtbl-to-latex): Suggest using :llend to suppress the final \\.
+
+2008-04-15  Jason Riedy  <jason@acm.org>
+
 	* lisp/org-table.el (*orgtbl-table*, *orgtbl-rtn*): Dynamically
 	bound variables to hold the input collection of lines and output
 	formatted text.
diff --git a/lisp/org-table.el b/lisp/org-table.el
index 3cc70c1..2eb9938 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -3613,8 +3613,11 @@ First element has index 0, or I0 if given."
 (defvar *orgtbl-fmt* nil "Format for each entry")
 (defvar *orgtbl-efmt* nil "Format for numbers")
 (defvar *orgtbl-lfmt* nil "Format for an entire line, overrides fmt")
+(defvar *orgtbl-llfmt* nil "Specializes lfmt for the last row")
 (defvar *orgtbl-lstart* nil "Text starting a row")
+(defvar *orgtbl-llstart* nil "Specializes lstart for the last row")
 (defvar *orgtbl-lend* nil "Text ending a row")
+(defvar *orgtbl-llend* nil "Specializes lend for the last row")
 
 (defun orgtbl-format-line (line)
   "Format LINE as a table row."
@@ -3644,9 +3647,15 @@ First element has index 0, or I0 if given."
 
 (defun orgtbl-format-section (section-stopper)
   "Format lines until the first occurrence of SECTION-STOPPER."
-  (progn
-    (while (not (eq (car *orgtbl-table*) section-stopper))
-      (orgtbl-format-line (pop *orgtbl-table*)))))
+  (let (prevline)
+    (progn
+      (while (not (eq (car *orgtbl-table*) section-stopper))
+	(if prevline (orgtbl-format-line prevline))
+	(setq prevline (pop *orgtbl-table*)))
+      (if prevline (let ((*orgtbl-lstart* *orgtbl-llstart*)
+			 (*orgtbl-lend* *orgtbl-llend*)
+			 (*orgtbl-lfmt* *orgtbl-llfmt*))
+		     (orgtbl-format-line prevline))))))
 
 (defun orgtbl-to-generic (table params)
   "Convert the orgtbl-mode TABLE to some other format.
@@ -3670,17 +3679,20 @@ Valid parameters are
             May be nil to ignore hlines.
 
 :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
 :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.
 :fmt        A format to be used to wrap the field, should contain
             %s for the original field value.  For example, to wrap
             everything in dollars, you could use :fmt \"$%s$\".
             This may also be a property list with column numbers and
             formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
 
-:hlstart :hlend :hlsep :hlfmt :hfmt
+:hlstart :hllstart :hlend :hllend :hlsep :hlfmt :hllfmt :hfmt
             Same as above, specific for the header lines in the table.
             All lines before the first hline are treated as header.
             If any of these is not present, the data line value is used.
@@ -3700,8 +3712,11 @@ directly by `orgtbl-send-table'.  See manual."
 	 (*orgtbl-sep* (plist-get params :sep))
 	 (*orgtbl-efmt* (plist-get params :efmt))
 	 (*orgtbl-lstart* (plist-get params :lstart))
+	 (*orgtbl-llstart* (or (plist-get params :llstart) *orgtbl-lstart*))
 	 (*orgtbl-lend* (plist-get params :lend))
+	 (*orgtbl-llend* (or (plist-get params :llend) *orgtbl-lend*))
 	 (*orgtbl-lfmt* (plist-get params :lfmt))
+	 (*orgtbl-llfmt* (or (plist-get params :llfmt) *orgtbl-lfmt*))
 	 (*orgtbl-fmt* (plist-get params :fmt))
 	 *orgtbl-rtn*)
 
@@ -3716,8 +3731,14 @@ directly by `orgtbl-send-table'.  See manual."
 	(progn
 	  (let* ((*orgtbl-lstart* (or (plist-get params :hlstart)
 				      *orgtbl-lstart*))
+		 (*orgtbl-llstart* (or (plist-get params :hllstart)
+				       *orgtbl-llstart*))
 		 (*orgtbl-lend* (or (plist-get params :hlend) *orgtbl-lend*))
+		 (*orgtbl-llend* (or (plist-get params :hllend)
+				     (plist-get params :hlend) *orgtbl-llend*))
 		 (*orgtbl-lfmt* (or (plist-get params :hlfmt) *orgtbl-lfmt*))
+		 (*orgtbl-llfmt* (or (plist-get params :hllfmt)
+				     (plist-get params :hlfmt) *orgtbl-llfmt*))
 		 (*orgtbl-sep* (or (plist-get params :hlsep) *orgtbl-sep*))
 		 (*orgtbl-fmt* (or (plist-get params :hfmt) *orgtbl-fmt*)))
 	    (orgtbl-format-section 'hline))
@@ -3753,6 +3774,9 @@ LaTeX are:
            example \"%s\\\\times10^{%s}\".  LaTeX default is \"%s\\\\,(%s)\".
            This may also be a property list with column numbers and formats.
 
+:llend     If you find too much space below the last line of a table,
+           pass a value of \"\" for :llend to suppress the final \\\\.
+
 The general parameters :skip and :skipcols have already been applied when
 this function is called."
   (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
-- 
1.5.5.rc1.121.g1594

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/4] Allow functions for some orgtbl parameters.
  2008-04-16 21:39 [PATCH 0/4] Generalize orgtbl formatting with functions Jason Riedy
  2008-04-16 21:39 ` [PATCH 1/4] Refactor orgtbl-to-generic; explicitly separate heading from body Jason Riedy
  2008-04-16 21:39 ` [PATCH 2/4] Support last-line specializers Jason Riedy
@ 2008-04-16 21:39 ` Jason Riedy
  2008-04-16 21:39 ` [PATCH 4/4] Add a :remove-nil-lines parameter to orgtbl-to-generic Jason Riedy
  3 siblings, 0 replies; 5+ messages in thread
From: Jason Riedy @ 2008-04-16 21:39 UTC (permalink / raw)
  To: emacs-orgmode

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>
---
 ChangeLog         |   14 +++++++++++
 doc/org.texi      |    5 +++-
 lisp/org-table.el |   67 ++++++++++++++++++++++++++++++++++++++---------------
 3 files changed, 66 insertions(+), 20 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 13980bf..1347715 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,19 @@
 2008-04-15  Jason Riedy  <jason@acm.org>
 
+	* lisp/org-table.el (orgtbl-get-fmt): New inline function for
+	picking apart formats that may be lists.
+	(orgtbl-apply-fmt): New inline function for applying formats that
+	may be functions.
+	(orgtbl-eval-str): New inline function for strings that may be
+	functions.
+	(orgtbl-format-line, orgtbl-to-generic): Use and document.
+	(orgtbl-to-latex, orgtbl-to-texinfo): Document.
+
+	* doc/org.texi (A LaTeX example): Note that fmt may be a
+	one-argument function, and efmt may be a two-argument function.
+
+2008-04-15  Jason Riedy  <jason@acm.org>
+
 	* lisp/org-table.el (*orgtbl-llfmt*, *orgtbl-llstart*)
 	(*orgtbl-llend*): Dynamic variables for last-line formatting.
 	(orgtbl-format-section): Shift formatting to support detecting the
diff --git a/doc/org.texi b/doc/org.texi
index c9eaab9..9c9b081 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -8528,6 +8528,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
@@ -8536,7 +8538,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
diff --git a/lisp/org-table.el b/lisp/org-table.el
index 2eb9938..4ae90e3 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -3619,6 +3619,25 @@ First element has index 0, or I0 if given."
 (defvar *orgtbl-lend* nil "Text ending a row")
 (defvar *orgtbl-llend* nil "Specializes lend for the last row")
 
+(defsubst orgtbl-get-fmt (fmt i)
+  "Retrieve the format from FMT corresponding to the Ith column."
+  (if (and (not (functionp fmt)) (consp fmt))
+      (plist-get fmt i)
+    fmt))
+
+(defsubst orgtbl-apply-fmt (fmt &rest args)
+  "Apply format FMT to the arguments.  NIL FMTs return the first argument."
+  (cond ((functionp fmt) (apply fmt args))
+	(fmt (apply 'format fmt args))
+	(args (car args))
+	(t args)))
+
+(defsubst orgtbl-eval-str (str)
+  "If STR is a function, evaluate it with no arguments."
+  (if (functionp str)
+      (funcall str)
+    str))
+
 (defun orgtbl-format-line (line)
   "Format LINE as a table row."
   (if (eq line 'hline) (if *orgtbl-hline* (push *orgtbl-hline* *orgtbl-rtn*))
@@ -3627,22 +3646,18 @@ First element has index 0, or I0 if given."
 	    (mapcar
 	     (lambda (f)
 	       (setq i (1+ i))
-	       (let* ((*orgtbl-fmt* (if (consp *orgtbl-fmt*)
-				     (plist-get *orgtbl-fmt* i)
-				     *orgtbl-fmt*))
-		      (*orgtbl-efmt* (if (consp *orgtbl-efmt*)
-				      (plist-get *orgtbl-efmt* i)
-				      *orgtbl-efmt*))
-		      (f (if (and *orgtbl-efmt*
-				  (string-match orgtbl-exp-regexp f))
-			     (format *orgtbl-efmt* (match-string 1 f)
-				     (match-string 2 f))
+	       (let* ((efmt (orgtbl-get-fmt *orgtbl-efmt* i))
+		      (f (if (and efmt (string-match orgtbl-exp-regexp f))
+			     (orgtbl-apply-fmt efmt (match-string 1 f)
+					       (match-string 2 f))
 			   f)))
-		 (if *orgtbl-fmt* (format *orgtbl-fmt* f) f)))
+		 (orgtbl-apply-fmt (orgtbl-get-fmt *orgtbl-fmt* i) f)))
 	     line)))
-      (push (if *orgtbl-lfmt* (apply 'format *orgtbl-lfmt* line)
-	      (concat *orgtbl-lstart* (mapconcat 'identity line *orgtbl-sep*)
-		      *orgtbl-lend*))
+      (push (if *orgtbl-lfmt*
+		(orgtbl-apply-fmt *orgtbl-lfmt* line)
+	      (concat (orgtbl-eval-str *orgtbl-lstart*)
+		      (mapconcat 'identity line *orgtbl-sep*)
+		      (orgtbl-eval-str *orgtbl-lend*)))
 	    *orgtbl-rtn*))))
 
 (defun orgtbl-format-section (section-stopper)
@@ -3669,20 +3684,27 @@ specify either :lfmt, or all of (:lstart :lend :sep).  If you do not use
 
 Valid parameters are
 
-:tstart     String to start the table.  Ignored when :splice is t.
-:tend       String to end the table.  Ignored when :splice is t.
-
 :splice     When set to t, return only table body lines, don't wrap
             them into :tstart and :tend.  Default is nil.
 
 :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:
+:tstart     String to start the table.  Ignored when :splice is t.
+:tend       String to end the table.  Ignored when :splice is t.
 :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.
@@ -3697,6 +3719,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
@@ -3722,7 +3745,8 @@ directly by `orgtbl-send-table'.  See manual."
 
     ;; Put header
     (unless splicep
-      (push (or (plist-get params :tstart) "ERROR: no :tstart") *orgtbl-rtn*))
+      (push (or (orgtbl-eval-str (plist-get params :tstart))
+		"ERROR: no :tstart") *orgtbl-rtn*))
 
     ;; Do we have a heading section?  If so, format it and handle the
     ;; trailing hline.
@@ -3749,7 +3773,8 @@ directly by `orgtbl-send-table'.  See manual."
     (orgtbl-format-section nil)
 
     (unless splicep
-      (push (or (plist-get params :tend) "ERROR: no :tend") *orgtbl-rtn*))
+      (push (or (orgtbl-eval-str (plist-get params :tend))
+		"ERROR: no :tend") *orgtbl-rtn*))
 
     (mapconcat 'identity (nreverse *orgtbl-rtn*) "\n")))
 
@@ -3768,11 +3793,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 \\\\.
@@ -3834,6 +3861,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
-- 
1.5.5.rc1.121.g1594

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 4/4] Add a :remove-nil-lines parameter to orgtbl-to-generic.
  2008-04-16 21:39 [PATCH 0/4] Generalize orgtbl formatting with functions Jason Riedy
                   ` (2 preceding siblings ...)
  2008-04-16 21:39 ` [PATCH 3/4] Allow functions for some orgtbl parameters Jason Riedy
@ 2008-04-16 21:39 ` Jason Riedy
  3 siblings, 0 replies; 5+ messages in thread
From: Jason Riedy @ 2008-04-16 21:39 UTC (permalink / raw)
  To: emacs-orgmode

Useful if the header formatting is used purely for side-effects.

Signed-off-by: Jason Riedy <jason@acm.org>
---
 ChangeLog         |    5 +++++
 lisp/org-table.el |    7 ++++++-
 2 files changed, 11 insertions(+), 1 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 1347715..15937cc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2008-04-16  Jason Riedy  <jason@acm.org>
+
+	* lisp/org-table.el (orgtbl-to-generic): Add a :remove-nil-lines
+	parameter that supresses lines that evaluate to NIL.
+
 2008-04-15  Jason Riedy  <jason@acm.org>
 
 	* lisp/org-table.el (orgtbl-get-fmt): New inline function for
diff --git a/lisp/org-table.el b/lisp/org-table.el
index 4ae90e3..9b4297b 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -3692,6 +3692,8 @@ Valid parameters are
 
 :sep        Separator between two fields
 
+:remove-nil-lines Do not include lines that evaluate to nil.
+
   Each in the following group may be either a string or a function
   of no arguments returning a string:
 :tstart     String to start the table.  Ignored when :splice is t.
@@ -3731,6 +3733,7 @@ directly by `orgtbl-send-table'.  See manual."
   (interactive)
   (let* ((splicep (plist-get params :splice))
 	 (hline (plist-get params :hline))
+	 (remove-nil-linesp (plist-get params :remove-nil-lines))
 	 (*orgtbl-table* table)
 	 (*orgtbl-sep* (plist-get params :sep))
 	 (*orgtbl-efmt* (plist-get params :efmt))
@@ -3776,7 +3779,9 @@ directly by `orgtbl-send-table'.  See manual."
       (push (or (orgtbl-eval-str (plist-get params :tend))
 		"ERROR: no :tend") *orgtbl-rtn*))
 
-    (mapconcat 'identity (nreverse *orgtbl-rtn*) "\n")))
+    (mapconcat 'identity (nreverse (if remove-nil-linesp
+				       (remq nil *orgtbl-rtn*)
+				     *orgtbl-rtn*)) "\n")))
 
 (defun orgtbl-to-latex (table params)
   "Convert the orgtbl-mode TABLE to LaTeX.
-- 
1.5.5.rc1.121.g1594

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-04-16 21:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-16 21:39 [PATCH 0/4] Generalize orgtbl formatting with functions Jason Riedy
2008-04-16 21:39 ` [PATCH 1/4] Refactor orgtbl-to-generic; explicitly separate heading from body Jason Riedy
2008-04-16 21:39 ` [PATCH 2/4] Support last-line specializers Jason Riedy
2008-04-16 21:39 ` [PATCH 3/4] Allow functions for some orgtbl parameters Jason Riedy
2008-04-16 21:39 ` [PATCH 4/4] Add a :remove-nil-lines parameter to orgtbl-to-generic Jason Riedy

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).