Ver Fonte

Correctly interpret CVS tables with quoted fields

The csv parser was very primitive, ignoring quoted fields.  This is
now fixed.
Carsten Dominik há 15 anos atrás
pai
commit
59c9c4cdd4
2 ficheiros alterados com 24 adições e 9 exclusões
  1. 3 0
      lisp/ChangeLog
  2. 21 9
      lisp/org-table.el

+ 3 - 0
lisp/ChangeLog

@@ -1,5 +1,8 @@
 2009-10-26  Carsten Dominik  <carsten.dominik@gmail.com>
 
+	* org-table.el (org-table-convert-region): Correctly interpret
+	quoting in csv import.
+
 	* org.el (org-icompleting-read): Make iswitchb completion work
 	with lists and tables.
 

+ 21 - 9
lisp/org-table.el

@@ -424,17 +424,29 @@ nil      When nil, the command tries to be smart and figure out the
 	     ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
 	     ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
 	     (t 1))))
-    (setq re (cond
-	      ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
-	      ((equal separator '(16)) "^\\|\t")
-	      ((integerp separator)
-	       (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
-	      (t (error "This should not happen"))))
     (goto-char beg)
-    (while (re-search-forward re end t)
-      (replace-match "| " t t))
+    (if (equal separator '(4))
+	(while (<= (point) end)
+	  ;; parse the csv stuff
+	  (cond
+	   ((looking-at "^") (insert "|"))
+	   ((looking-at "[ \t]*$") (replace-match "|") (beginning-of-line 2))
+	   ((looking-at "[ \t]*\"\\([^\"\n]*\\)\"")
+	    (replace-match "\\1")
+	    (if (looking-at "\"") (insert "\"")))
+	   ((looking-at "[^,\n]+") (goto-char (match-end 0)))
+	   ((looking-at "[ \t]*,") (replace-match " | "))
+	   (t (beginning-of-line 2)
+	      (if (< (point) end) (insert "|")))))
+      (setq re (cond
+		((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
+		((equal separator '(16)) "^\\|\t")
+		((integerp separator)
+		 (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
+		(t (error "This should not happen"))))
+      (while (re-search-forward re end t)
+	(replace-match "| " t t)))
     (goto-char beg)
-    (insert " ")
     (org-table-align)))
 
 (defun org-table-import (file arg)