From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jason Riedy Subject: [PATCH] Refactor orgtbl-to-generic; explicitly separate heading from body. Date: Sun, 02 Mar 2008 16:35:55 -0800 Message-ID: <87mypgiqz8.fsf@sparse.yi.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1JVyf8-0008Jk-EF for emacs-orgmode@gnu.org; Sun, 02 Mar 2008 19:36:14 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1JVyf6-0008Gb-4x for emacs-orgmode@gnu.org; Sun, 02 Mar 2008 19:36:13 -0500 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JVyf5-0008GD-Sh for emacs-orgmode@gnu.org; Sun, 02 Mar 2008 19:36:11 -0500 Received: from main.gmane.org ([80.91.229.2] helo=ciao.gmane.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1JVyf5-0005Jk-Ae for emacs-orgmode@gnu.org; Sun, 02 Mar 2008 19:36:11 -0500 Received: from list by ciao.gmane.org with local (Exim 4.43) id 1JVyex-0006sE-Ml for emacs-orgmode@gnu.org; Mon, 03 Mar 2008 00:36:03 +0000 Received: from 209-204-163-7.vpn.sonic.net ([209.204.163.7]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 03 Mar 2008 00:36:03 +0000 Received: from jason by 209-204-163-7.vpn.sonic.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 03 Mar 2008 00:36:03 +0000 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org 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 --- org.el | 100 +++++++++++++++++++++++++++++++++------------------------------ 1 files changed, 52 insertions(+), 48 deletions(-) diff --git a/org.el b/org.el index 4e27ecc..64121c9 100644 --- a/org.el +++ b/org.el @@ -11770,15 +11770,31 @@ 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))) +(defun orgtbl-format-line (line) + "Format LINE as a table row." + (if (eq line 'hline) (if hline (push hline rtn)) + (let* ((i 0) + (line + (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)) + (f (if (and efmt (string-match orgtbl-exp-regexp f)) + (format efmt (match-string 1 f) + (match-string 2 f)) + f))) + (if fmt (format fmt f) f))) + line))) + (push (if lfmt (apply 'format lfmt line) + (concat lstart (mapconcat 'identity line sep) lend)) + rtn)))) + +(defun orgtbl-format-section (section-stopper) + "Format lines until the first occurrence of SECTION-STOPPER." + (progn + (while (not (eq (car table) section-stopper)) + (orgtbl-format-line (pop table))))) (defun orgtbl-to-generic (table params) "Convert the orgtbl-mode TABLE to some other format. @@ -11812,7 +11828,7 @@ Valid parameters are 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 :hlend :hsep :hlfmt :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. @@ -11826,49 +11842,37 @@ 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)) + (sep (plist-get params :sep)) + (efmt (plist-get params :efmt)) + (lstart (plist-get params :lstart)) + (lend (plist-get params :lend)) + (lfmt (plist-get params :lfmt)) + (fmt (plist-get params :fmt)) + 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") rtn)) + + ;; Do we have a heading section? If so, format it and handle the + ;; trailing hline. + (if (and (not splicep) (listp (car table)) (memq 'hline table)) + (progn + (let* ((lstart (or (plist-get params :hlstart) lstart)) + (lend (or (plist-get params :hlend) lend)) + (lfmt (or (plist-get params :hlfmt) lfmt)) + (fmt (or (plist-get params :hfmt) fmt))) + (orgtbl-format-section 'hline)) + (if hline (push hline rtn)) + (pop 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") rtn)) (mapconcat 'identity (nreverse rtn) "\n"))) -- 1.5.4.3