From 03eb88661b7b270b19c00597ffc70a59f53f3a10 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Sun, 28 Jun 2015 14:42:07 +0200 Subject: [PATCH] Use parser for `org-at-table-p' and al. * lisp/org-table.el (org-table-begin): (org-table-end): Refactor using parser. * lisp/org.el (org-at-table-p): (org-at-table.el-p): Refactor using parser. (org-meta-return): Use `org-at-table-p'. (org-at-table-hline-p): (org-table-recognize-table.el): Small refactoring. --- lisp/org-table.el | 26 +++++++--------- lisp/org.el | 90 +++++++++++++++++++++++-------------------------------- 2 files changed, 47 insertions(+), 69 deletions(-) diff --git a/lisp/org-table.el b/lisp/org-table.el index 9b8c3e52f..7b7d5f555 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -912,27 +912,21 @@ When nil, simply write \"#ERROR\" in corrupted fields.") (defun org-table-begin (&optional table-type) "Find the beginning of the table and return its position. With argument TABLE-TYPE, go to the beginning of a table.el-type table." - (save-excursion - (if (not (re-search-backward - (if table-type org-table-any-border-regexp - org-table-border-regexp) - nil t)) - (progn (goto-char (point-min)) (point)) - (goto-char (match-beginning 0)) - (beginning-of-line 2) - (point)))) + (let ((table (org-element-lineage (org-element-at-point) '(table) t))) + (and table (org-element-property :post-affiliated table)))) ;;;###autoload (defun org-table-end (&optional table-type) "Find the end of the table and return its position. With argument TABLE-TYPE, go to the end of a table.el-type table." - (if (save-excursion - (re-search-forward - (if table-type org-table-any-border-regexp - org-table-border-regexp) - nil t)) - (match-beginning 0) - (point-max))) + (let ((table (org-element-lineage (org-element-at-point) '(table) t))) + (and table + (let ((type (org-element-property :type table))) + (if (eq type 'org) (org-element-property :contents-end table) + (save-excursion + (goto-char (org-element-property :end table)) + (skip-chars-backward " \t\n") + (line-beginning-position 2))))))) ;;;###autoload (defun org-table-justify-field-maybe (&optional new) diff --git a/lisp/org.el b/lisp/org.el index 5d922b4a5..d5b76965e 100755 --- a/lisp/org.el +++ b/lisp/org.el @@ -4494,56 +4494,49 @@ Normal means, no org-mode-specific context." (orgtbl-mode 1)) (defun org-at-table-p (&optional table-type) - "Return t if the cursor is inside an org-type table. -If TABLE-TYPE is non-nil, also check for table.el-type tables." - (if org-enable-table-editor - (save-excursion - (beginning-of-line 1) - (looking-at (if table-type org-table-any-line-regexp - org-table-line-regexp))) - nil)) + "Non-nil if the cursor is inside an Org table. +If TABLE-TYPE is non-nil, also check for table.el-type tables. +If `org-enable-table-editor' is nil, return nil unconditionally." + (and org-enable-table-editor + (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]")) + (let ((element (org-element-lineage (org-element-at-point) '(table) t))) + (and element + (or table-type (eq (org-element-property :type element) 'org)))))) (defsubst org-table-p () (org-at-table-p)) (defun org-at-table.el-p () - "Return t if and only if we are at a table.el table." - (and (org-at-table-p 'any) - (save-excursion - (goto-char (org-table-begin 'any)) - (looking-at org-table1-hline-regexp)))) + "Non-nil when point is at a table.el table." + (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]")) + (let ((element (org-element-at-point))) + (and (eq (org-element-type element) 'table) + (eq (org-element-property :type element) 'table.el))))) (defun org-table-recognize-table.el () "If there is a table.el table nearby, recognize it and move into it." - (if org-table-tab-recognizes-table.el - (if (org-at-table.el-p) - (progn - (beginning-of-line 1) - (if (looking-at org-table-dataline-regexp) - nil - (if (looking-at org-table1-hline-regexp) - (progn - (beginning-of-line 2) - (if (looking-at org-table-any-border-regexp) - (beginning-of-line -1))))) - (if (re-search-forward "|" (org-table-end t) t) - (progn - (require 'table) - (if (table--at-cell-p (point)) - t - (message "recognizing table.el table...") - (table-recognize-table) - (message "recognizing table.el table...done"))) - (error "This should not happen")) - t) - nil) - nil)) + (when (and org-table-tab-recognizes-table.el (org-at-table.el-p)) + (beginning-of-line) + (unless (or (looking-at org-table-dataline-regexp) + (not (looking-at org-table1-hline-regexp))) + (forward-line) + (when (looking-at org-table-any-border-regexp) + (forward-line -2))) + (if (re-search-forward "|" (org-table-end t) t) + (progn + (require 'table) + (if (table--at-cell-p (point)) t + (message "recognizing table.el table...") + (table-recognize-table) + (message "recognizing table.el table...done"))) + (error "This should not happen")))) (defun org-at-table-hline-p () - "Return t if the cursor is inside a hline in a table." - (if org-enable-table-editor - (save-excursion - (beginning-of-line 1) - (looking-at org-table-hline-regexp)) - nil)) + "Non-nil when point is inside a hline in a table. +Assume point is already in a table. If `org-enable-table-editor' +is nil, return nil unconditionally." + (and org-enable-table-editor + (save-excursion + (beginning-of-line) + (looking-at org-table-hline-regexp)))) (defun org-table-map-tables (function &optional quietly) "Apply FUNCTION to the start of all tables in the buffer." @@ -21535,17 +21528,8 @@ on context. See the individual commands for more information." (interactive "P") (org-check-before-invisible-edit 'insert) (or (run-hook-with-args-until-success 'org-metareturn-hook) - (let* ((element (org-element-at-point)) - (type (org-element-type element))) - (when (eq type 'table-row) - (setq element (org-element-property :parent element)) - (setq type 'table)) - (if (and (eq type 'table) - (eq (org-element-property :type element) 'org) - (>= (point) (org-element-property :contents-begin element)) - (< (point) (org-element-property :contents-end element))) - (call-interactively 'org-table-wrap-region) - (call-interactively 'org-insert-heading))))) + (call-interactively (if (org-at-table-p) #'org-table-wrap-region + #'org-insert-heading)))) ;;; Menu entries -- 2.11.4.GIT