emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: James TD Smith <ahktenzero@dsl.pipex.com>
To: emacs-orgmode@gnu.org
Subject: [PATCH 3/5] Some improvements to org-table-export
Date: Sun, 16 Mar 2008 16:29:50 +0000	[thread overview]
Message-ID: <20080316162920.23004.97374.stgit@nyarlathotep.internal> (raw)
In-Reply-To: <20080316162417.23004.16993.stgit@nyarlathotep.internal>

From: James TD Smith <ahktenzero@usa.net>

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.

  parent reply	other threads:[~2008-03-16 17:06 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-16 16:27 [PATCH 0/5] Various patches James TD Smith
2008-03-16 16:28 ` [PATCH 1/5] Hide drawers after displaying an entry using org-clock-goto James TD Smith
2008-03-16 16:29 ` [PATCH 2/5] Add a way to set a user-defined function to generate descriptions for links James TD Smith
2008-04-09 15:15   ` Carsten Dominik
2008-03-16 16:29 ` James TD Smith [this message]
2008-04-09 15:59   ` [PATCH 3/5] Some improvements to org-table-export Carsten Dominik
2008-03-16 16:30 ` [PATCH 4/5] Add a new sort option, which sorts items by todo keyword James TD Smith, James TD Smith
2008-04-09 15:40   ` Carsten Dominik
2008-03-16 16:31 ` [PATCH 5/5] clipboard handling in remember templats James TD Smith
2008-04-09 15:32   ` Carsten Dominik
2008-04-09 17:35     ` James TD Smith
2008-03-16 18:29 ` [PATCH 4/5] Various patches James TD Smith

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=20080316162920.23004.97374.stgit@nyarlathotep.internal \
    --to=ahktenzero@dsl.pipex.com \
    --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).