Browse Source

New helper functions in org-table.el

Carsten Dominik 15 years ago
parent
commit
d70fb6fdba
2 changed files with 62 additions and 16 deletions
  1. 7 0
      lisp/ChangeLog
  2. 55 16
      lisp/org-table.el

+ 7 - 0
lisp/ChangeLog

@@ -1,3 +1,10 @@
+2010-03-21  Carsten Dominik  <carsten.dominik@gmail.com>
+
+	* org-table.el (org-table-goto-column): Fix forcing a non-existing
+	column.
+	(org-table-get, org-table-put, org-table-goto-line)
+	(org-table-current-line): New functions.
+
 2010-03-21  Jan Böcker  <jan.boecker@jboecker.de>
 
 	* org.el (org-open-file): Allow regular expressions in

+ 55 - 16
lisp/org-table.el

@@ -1005,6 +1005,47 @@ This actually throws an error, so it aborts the current command."
 (defvar org-table-clip nil
   "Clipboard for table regions.")
 
+(defun org-table-get (line column)
+  "Get the field in table line LINE, column COLUMN.
+If LINE is larger than the number of data lines in the table, the function
+returns nil.  However, if COLUMN is too large, we will simply return an
+empty string.
+If LINE is nil, use the current line.
+If column is nil, use the current column."
+  (setq column (or column (org-table-current-column)))
+  (save-excursion
+    (and (or (not line) (org-table-goto-line line))
+	 (org-trim (org-table-get-field column)))))
+
+(defun org-table-put (line column value &optional align)
+  "Put VALUE into line LINE, column COLUMN.
+When ALIGN is set, als realign the table."
+  (setq column (or column (org-table-current-column)))
+  (prog1 (save-excursion
+	   (and (or (not line) (org-table-goto-line line))
+		(progn (org-table-goto-column column nil 'force) t)
+		(org-table-get-field column value)))
+    (and align (org-table-align))))
+  
+(defun org-table-current-line ()
+  "Return the index of the current data line."
+  (let ((pos (point)) (end (org-table-end)) (cnt 0)) 
+    (save-excursion
+      (goto-char (org-table-begin))
+      (while (and (re-search-forward org-table-dataline-regexp end t)
+		  (setq cnt (1+ cnt))
+		  (< (point-at-eol) pos))))
+    cnt))
+
+(defun org-table-goto-line (N)
+  "Go to the Nth data line in the current table.
+Return t when the line exists, nil if it does not exist."
+  (goto-char (org-table-begin))
+  (let ((end (org-table-end)) (cnt 0))
+    (while (and (re-search-forward org-table-dataline-regexp end t)
+		(< (setq cnt (1+ cnt)) line)))
+    (= cnt line)))
+
 (defun org-table-blank-field ()
   "Blank the current table field or active region."
   (interactive)
@@ -1104,22 +1145,20 @@ of the field.
 If there are less than N fields, just go to after the last delimiter.
 However, when FORCE is non-nil, create new columns if necessary."
   (interactive "p")
-  (let ((pos (point-at-eol)))
-    (beginning-of-line 1)
-    (when (> n 0)
-      (while (and (> (setq n (1- n)) -1)
-		  (or (search-forward "|" pos t)
-		      (and force
-			   (progn (end-of-line 1)
-				  (skip-chars-backward "^|")
-				  (insert " | "))))))
-;                                  (backward-char 2) t)))))
-      (when (and force (not (looking-at ".*|")))
-	(save-excursion (end-of-line 1) (insert " | ")))
-      (if on-delim
-	  (backward-char 1)
-	(if (looking-at " ") (forward-char 1))))))
-
+  (beginning-of-line 1)
+  (when (> n 0)
+    (while (and (> (setq n (1- n)) -1)
+		(or (search-forward "|" (point-at-eol) t)
+		    (and force
+			 (progn (end-of-line 1)
+				(skip-chars-backward "^|")
+				(insert " | ")
+				t)))))
+    (when (and force (not (looking-at ".*|")))
+      (save-excursion (end-of-line 1) (insert " | ")))
+    (if on-delim
+	(backward-char 1)
+      (if (looking-at " ") (forward-char 1)))))
 
 (defun org-table-insert-column ()
   "Insert a new column into the table."