Browse Source

org-element: Small optimization to `org-element-context'

* lisp/org-element.el (org-element-context): Add an optional argument
  so (org-element-context)
  and (org-element-context (org-element-at-point)) are equivalent.
* testing/lisp/test-org-element.el: Add test.
Nicolas Goaziou 5 years ago
parent
commit
2fd88bfd56
2 changed files with 15 additions and 4 deletions
  1. 7 3
      lisp/org-element.el
  2. 8 1
      testing/lisp/test-org-element.el

+ 7 - 3
lisp/org-element.el

@@ -4614,7 +4614,7 @@ first element of current section."
 		 (goto-char cbeg)))))))))))
 
 ;;;###autoload
-(defun org-element-context ()
+(defun org-element-context (&optional element)
   "Return closest element or object around point.
 
 Return value is a list like (TYPE PROPS) where TYPE is the type
@@ -4624,10 +4624,14 @@ associated to it.
 Possible types are defined in `org-element-all-elements' and
 `org-element-all-objects'.  Properties depend on element or
 object type, but always include :begin, :end, :parent
-and :post-blank properties."
+and :post-blank properties.
+
+Optional argument ELEMENT, when non-nil, is the closest element
+containing point, as returned by `org-element-at-point'.
+Providing it allows for quicker computation."
   (org-with-wide-buffer
    (let* ((origin (point))
-	  (element (org-element-at-point))
+	  (element (or element (org-element-at-point)))
 	  (type (car element))
 	  end)
      ;; Check if point is inside an element containing objects or at

+ 8 - 1
testing/lisp/test-org-element.el

@@ -2713,7 +2713,14 @@ Paragraph \\alpha."
        (org-test-with-temp-text "<<target>>{{{test}}}"
 	 (progn (search-forward "{")
 		(backward-char)
-		(org-element-type (org-element-context)))))))
+		(org-element-type (org-element-context))))))
+  ;; Test optional argument.
+  (should
+   (eq 'underline
+       (org-test-with-temp-text "Some *text with _underline_ text*"
+	 (progn
+	   (search-forward "under")
+	   (org-element-type (org-element-context (org-element-at-point))))))))
 
 
 (provide 'test-org-element)