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.
This commit is contained in:
Nicolas Goaziou 2013-01-09 15:33:29 +01:00
parent 1dd1b10d5a
commit 2fd88bfd56
2 changed files with 15 additions and 4 deletions

View File

@ -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

View File

@ -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)