org-element: Small optimization

* lisp/org-element.el (org-element--current-element): Only look after
  planning lines right after parsing a section.
(org-element--next-mode): New function.
(org-element--parse-elements, org-element--parse-to): Use new
function.
This commit is contained in:
Nicolas Goaziou 2014-08-31 16:31:59 +02:00
parent 539eac9211
commit 21258060ad
1 changed files with 36 additions and 29 deletions

View File

@ -3610,8 +3610,7 @@ CONTENTS is nil."
;; `section'). Special modes are: `first-section', `item', ;; `section'). Special modes are: `first-section', `item',
;; `node-property', `section' and `table-row'. ;; `node-property', `section' and `table-row'.
(defun org-element--current-element (defun org-element--current-element (limit &optional granularity mode structure)
(limit &optional granularity special structure)
"Parse the element starting at point. "Parse the element starting at point.
Return value is a list like (TYPE PROPS) where TYPE is the type Return value is a list like (TYPE PROPS) where TYPE is the type
@ -3628,12 +3627,12 @@ recursion. Allowed values are `headline', `greater-element',
nil), secondary values will not be parsed, since they only nil), secondary values will not be parsed, since they only
contain objects. contain objects.
Optional argument SPECIAL, when non-nil, can be either Optional argument MODE, when non-nil, can be either
`first-section', `item', `node-property', `section', and `first-section', `section', `planning', `item', `node-property'
`table-row'. and `table-row'.
If STRUCTURE isn't provided but SPECIAL is set to `item', it will If STRUCTURE isn't provided but MODE is set to `item', it will be
be computed. computed.
This function assumes point is always at the beginning of the This function assumes point is always at the beginning of the
element it has to parse." element it has to parse."
@ -3645,26 +3644,28 @@ element it has to parse."
(raw-secondary-p (and granularity (not (eq granularity 'object))))) (raw-secondary-p (and granularity (not (eq granularity 'object)))))
(cond (cond
;; Item. ;; Item.
((eq special 'item) ((eq mode 'item)
(org-element-item-parser limit structure raw-secondary-p)) (org-element-item-parser limit structure raw-secondary-p))
;; Table Row. ;; Table Row.
((eq special 'table-row) (org-element-table-row-parser limit)) ((eq mode 'table-row) (org-element-table-row-parser limit))
;; Node Property. ;; Node Property.
((eq special 'node-property) (org-element-node-property-parser limit)) ((eq mode 'node-property) (org-element-node-property-parser limit))
;; Headline. ;; Headline.
((org-with-limited-levels (org-at-heading-p)) ((org-with-limited-levels (org-at-heading-p))
(org-element-headline-parser limit raw-secondary-p)) (org-element-headline-parser limit raw-secondary-p))
;; Sections (must be checked after headline). ;; Sections (must be checked after headline).
((eq special 'section) (org-element-section-parser limit)) ((eq mode 'section) (org-element-section-parser limit))
((eq special 'first-section) ((eq mode 'first-section)
(org-element-section-parser (org-element-section-parser
(or (save-excursion (org-with-limited-levels (outline-next-heading))) (or (save-excursion (org-with-limited-levels (outline-next-heading)))
limit))) limit)))
;; Planning.
((and (eq mode 'planning) (looking-at org-planning-line-re))
(org-element-planning-parser limit))
;; When not at bol, point is at the beginning of an item or ;; When not at bol, point is at the beginning of an item or
;; a footnote definition: next item is always a paragraph. ;; a footnote definition: next item is always a paragraph.
((not (bolp)) (org-element-paragraph-parser limit (list (point)))) ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
;; Planning and Clock. ;; Clock.
((looking-at org-planning-line-re) (org-element-planning-parser limit))
((looking-at org-clock-line-re) (org-element-clock-parser limit)) ((looking-at org-clock-line-re) (org-element-clock-parser limit))
;; Inlinetask. ;; Inlinetask.
((org-at-heading-p) ((org-at-heading-p)
@ -4078,6 +4079,18 @@ looking into captions:
;; object is searched only once at top level (but sometimes more for ;; object is searched only once at top level (but sometimes more for
;; nested types). ;; nested types).
(defsubst org-element--next-mode (type)
"Return next special mode according to TYPE, or nil.
TYPE is a symbol representing the type of an element or object.
Modes can be either `first-section', `section', `planning',
`item', `node-property' and `table-row'."
(case type
(headline 'section)
(section 'planning)
(plain-list 'item)
(property-drawer 'node-property)
(table 'table-row)))
(defun org-element--parse-elements (defun org-element--parse-elements
(beg end special structure granularity visible-only acc) (beg end special structure granularity visible-only acc)
"Parse elements between BEG and END positions. "Parse elements between BEG and END positions.
@ -4132,11 +4145,7 @@ Elements are accumulated into ACC."
(org-element--parse-elements (org-element--parse-elements
cbeg (org-element-property :contents-end element) cbeg (org-element-property :contents-end element)
;; Possibly switch to a special mode. ;; Possibly switch to a special mode.
(case type (org-element--next-mode type)
(headline 'section)
(plain-list 'item)
(property-drawer 'node-property)
(table 'table-row))
(and (memq type '(item plain-list)) (and (memq type '(item plain-list))
(org-element-property :structure element)) (org-element-property :structure element))
granularity visible-only element)) granularity visible-only element))
@ -5220,7 +5229,7 @@ the process stopped before finding the expected result."
(let* ((cached (and (org-element--cache-active-p) (let* ((cached (and (org-element--cache-active-p)
(org-element--cache-find pos nil))) (org-element--cache-find pos nil)))
(begin (org-element-property :begin cached)) (begin (org-element-property :begin cached))
element next) element next mode)
(cond (cond
;; Nothing in cache before point: start parsing from first ;; Nothing in cache before point: start parsing from first
;; element following headline above, or first element in ;; element following headline above, or first element in
@ -5229,7 +5238,8 @@ the process stopped before finding the expected result."
(when (org-with-limited-levels (outline-previous-heading)) (when (org-with-limited-levels (outline-previous-heading))
(forward-line)) (forward-line))
(skip-chars-forward " \r\t\n") (skip-chars-forward " \r\t\n")
(beginning-of-line)) (beginning-of-line)
(setq mode 'planning))
;; Cache returned exact match: return it. ;; Cache returned exact match: return it.
((= pos begin) ((= pos begin)
(throw 'exit (if syncp (org-element-property :parent cached) cached))) (throw 'exit (if syncp (org-element-property :parent cached) cached)))
@ -5240,7 +5250,8 @@ the process stopped before finding the expected result."
(org-with-limited-levels org-outline-regexp-bol) begin t) (org-with-limited-levels org-outline-regexp-bol) begin t)
(forward-line) (forward-line)
(skip-chars-forward " \r\t\n") (skip-chars-forward " \r\t\n")
(beginning-of-line)) (beginning-of-line)
(setq mode 'planning))
;; Check if CACHED or any of its ancestors contain point. ;; Check if CACHED or any of its ancestors contain point.
;; ;;
;; If there is such an element, we inspect it in order to know ;; If there is such an element, we inspect it in order to know
@ -5274,8 +5285,7 @@ the process stopped before finding the expected result."
(save-excursion (save-excursion
(org-with-limited-levels (outline-next-heading)) (org-with-limited-levels (outline-next-heading))
(point)))) (point))))
(parent element) (parent element))
special-flag)
(while t (while t
(when syncp (when syncp
(cond ((= (point) pos) (throw 'exit parent)) (cond ((= (point) pos) (throw 'exit parent))
@ -5283,7 +5293,7 @@ the process stopped before finding the expected result."
(throw 'interrupt nil)))) (throw 'interrupt nil))))
(unless element (unless element
(setq element (org-element--current-element (setq element (org-element--current-element
end 'element special-flag end 'element mode
(org-element-property :structure parent))) (org-element-property :structure parent)))
(org-element-put-property element :parent parent) (org-element-put-property element :parent parent)
(org-element--cache-put element)) (org-element--cache-put element))
@ -5323,10 +5333,7 @@ the process stopped before finding the expected result."
(and (= cend pos) (= (point-max) pos))))) (and (= cend pos) (= (point-max) pos)))))
(goto-char (or next cbeg)) (goto-char (or next cbeg))
(setq next nil (setq next nil
special-flag (case type mode (org-element--next-mode type)
(plain-list 'item)
(property-drawer 'node-property)
(table 'table-row))
parent element parent element
end cend)))) end cend))))
;; Otherwise, return ELEMENT as it is the smallest ;; Otherwise, return ELEMENT as it is the smallest