emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Orgmode] [PATCH 3/5] Some improvements to org-table-export


From: James TD Smith
Subject: [Orgmode] [PATCH 3/5] Some improvements to org-table-export
Date: Sun, 16 Mar 2008 16:29:50 +0000
User-agent: StGIT/0.14.1

From: James TD Smith <address@hidden>

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.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]