From d70fb6fdbadded2191c5cf207c2053ee1e8840aa Mon Sep 17 00:00:00 2001 From: Carsten Dominik Date: Sun, 21 Mar 2010 20:08:01 +0100 Subject: [PATCH] New helper functions in org-table.el --- lisp/ChangeLog | 7 ++++++ lisp/org-table.el | 71 ++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 82768cbd0..b9f353d93 100755 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,10 @@ +2010-03-21 Carsten Dominik + + * 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 * org.el (org-open-file): Allow regular expressions in diff --git a/lisp/org-table.el b/lisp/org-table.el index df4bf3f29..670edcbe5 100644 --- a/lisp/org-table.el +++ b/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." -- 2.11.4.GIT