From mboxrd@z Thu Jan 1 00:00:00 1970 From: James TD Smith Subject: [PATCH 3/5] Some improvements to org-table-export Date: Sun, 16 Mar 2008 16:29:50 +0000 Message-ID: <20080316162920.23004.97374.stgit@nyarlathotep.internal> References: <20080316162417.23004.16993.stgit@nyarlathotep.internal> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Return-path: Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1JawJe-0001X2-LC for emacs-orgmode@gnu.org; Sun, 16 Mar 2008 13:06:34 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1JawJd-0001WA-Pg for emacs-orgmode@gnu.org; Sun, 16 Mar 2008 13:06:34 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JawJd-0001Vz-Lg for emacs-orgmode@gnu.org; Sun, 16 Mar 2008 13:06:33 -0400 Received: from 81-86-40-42.dsl.pipex.com ([81.86.40.42] helo=yog-sothoth.internal) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1JawJc-0007u7-51 for emacs-orgmode@gnu.org; Sun, 16 Mar 2008 13:06:33 -0400 Received: from nyarlathotep.internal (Debian-exim@nyarlathotep.internal [10.0.0.5]) by yog-sothoth.internal (8.13.4/8.13.4) with ESMTP id m2GGToxP015273 for ; Sun, 16 Mar 2008 16:29:50 GMT (envelope-from ahktenzero@dsl.pipex.com) Received: from [127.0.0.1] (helo=nyarlathotep.internal ident=ahktenzero) by nyarlathotep.internal with esmtp (Exim 4.69) (envelope-from ) id 1Javk6-0006Fu-H7 for emacs-orgmode@gnu.org; Sun, 16 Mar 2008 16:29:50 +0000 In-Reply-To: <20080316162417.23004.16993.stgit@nyarlathotep.internal> 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 From: James TD Smith Specify the file to export to as parameter or property Use the export mechanisms from orgtbl instead of the simple export. Specify the table output format in property. --- org.el | 91 +++++++++++++++++++++++++++++++++++++++++++++------------------- 1 files changed, 64 insertions(+), 27 deletions(-) diff --git a/org.el b/org.el index 021bd59..dee8471 100644 --- a/org.el +++ b/org.el @@ -965,6 +965,13 @@ table, obtained by prompting the user." (list (symbol :tag "Major mode") (string :tag "Format")))) +(defcustom org-table-export-default "orgtbl-to-generic :splice t :sep \"\t\"" + "Default export parameters for org-table-export. These can be + overridden on for a specific table by setting the + TABLE_EXPORT_FORMAT parameter. See orgtbl-export for the + different export transforms and available parameters." + :group 'org-table) + (defgroup org-table-settings nil "Settings for tables in Org-mode." :tag "Org Table Settings" @@ -975,6 +982,7 @@ table, obtained by prompting the user." :group 'org-table-settings :type 'string) + (defcustom org-table-number-regexp "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$" "Regular expression for recognizing numbers in table columns. @@ -8568,41 +8576,70 @@ are found, lines will be split on whitespace into fields." (insert-file-contents file) (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg))) -(defun org-table-export () +(defun org-table-export (&optional target) "Export table as a tab-separated file. -Such a file can be imported into a spreadsheet program like Excel." +Such a file can be imported into a spreadsheet program like +Excel. If TARGET is set, the table is exported to that file. If +the property TABLE_EXPORT_FILE is set on the entry the table is +in, the table will be exported to that file. Otherwise the user +is prompted for a file to write the table to. + +If the TABLE_EXPORT_FORMAT property is set, the contents of this +property will control export format in the same way as radio +tables in OrgTbl mode. +" (interactive) (let* ((beg (org-table-begin)) (end (org-table-end)) - (table (buffer-substring beg end)) - (file (read-file-name "Export table to: ")) + (txt (buffer-substring-no-properties beg end)) + (file (or target (org-entry-get beg "TABLE_EXPORT_FILE") + (read-file-name "Export table to: "))) + (format (or (org-entry-get beg "TABLE_EXPORT_FORMAT") + org-table-export-default)) buf) (unless (or (not (file-exists-p file)) (y-or-n-p (format "Overwrite file %s? " file))) (error "Abort")) - (with-current-buffer (find-file-noselect file) - (setq buf (current-buffer)) - (erase-buffer) - (fundamental-mode) - (insert table) - (goto-char (point-min)) - (while (re-search-forward "^[ \t]*|[ \t]*" nil t) - (replace-match "" t t) - (end-of-line 1)) - (goto-char (point-min)) - (while (re-search-forward "[ \t]*|[ \t]*$" nil t) - (replace-match "" t t) - (goto-char (min (1+ (point)) (point-max)))) - (goto-char (point-min)) - (while (re-search-forward "^-[-+]*$" nil t) - (replace-match "") - (if (looking-at "\n") - (delete-char 1))) - (goto-char (point-min)) - (while (re-search-forward "[ \t]*|[ \t]*" nil t) - (replace-match "\t" t t)) - (save-buffer)) - (kill-buffer buf))) + (message format) + + (if (string-match "\\([^ \t\r\n]+\\)\\( +.*\\)?" format) + (let* ((transform (intern (match-string 1 format))) + (params (if (match-end 2) + (read (concat "(" (match-string 2 format) ")")))) + (skip (plist-get params :skip)) + (skipcols (plist-get params :skipcols)) + (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*"))) + (lines (org-table-clean-before-export lines)) + (i0 (if org-table-clean-did-remove-column 2 1)) + (table (mapcar + (lambda (x) + (if (string-match org-table-hline-regexp x) + 'hline + (org-remove-by-index + (org-split-string (org-trim x) "\\s-*|\\s-*") + skipcols i0))) + lines)) + (fun (if (= i0 2) 'cdr 'identity)) + (org-table-last-alignment + (org-remove-by-index (funcall fun org-table-last-alignment) + skipcols i0)) + (org-table-last-column-widths + (org-remove-by-index (funcall fun org-table-last-column-widths) + skipcols i0))) + + (unless (fboundp transform) + (error "No such transformation function %s" transform)) + (setq txt (funcall transform table params)) + + (with-current-buffer (find-file-noselect file) + (setq buf (current-buffer)) + (erase-buffer) + (fundamental-mode) + (insert txt "\n") + (save-buffer)) + (kill-buffer buf) + (message "Export done.")) + (error "TABLE_EXPORT_FORMAT invalid")))) (defvar org-table-aligned-begin-marker (make-marker) "Marker at the beginning of the table last aligned.