Merge branch 'maint'

This commit is contained in:
Nicolas Goaziou 2020-01-11 20:18:47 +01:00
commit 3999320aa4
4 changed files with 81 additions and 63 deletions

View File

@ -72,7 +72,6 @@
(declare-function org-at-heading-p "org" (&optional _)) (declare-function org-at-heading-p "org" (&optional _))
(declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading)) (declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading))
(declare-function org-escape-code-in-string "org-src" (s)) (declare-function org-escape-code-in-string "org-src" (s))
(declare-function org-find-visible "org" ())
(declare-function org-macro-escape-arguments "org-macro" (&rest args)) (declare-function org-macro-escape-arguments "org-macro" (&rest args))
(declare-function org-macro-extract-arguments "org-macro" (s)) (declare-function org-macro-extract-arguments "org-macro" (s))
(declare-function org-reduced-level "org" (l)) (declare-function org-reduced-level "org" (l))
@ -4361,54 +4360,52 @@ elements.
Elements are accumulated into ACC." Elements are accumulated into ACC."
(save-excursion (save-excursion
(goto-char beg) (goto-char beg)
;; Visible only: skip invisible parts at the beginning of the
;; element.
(when (and visible-only (org-invisible-p2))
(goto-char (min (1+ (org-find-visible)) end)))
;; When parsing only headlines, skip any text before first one. ;; When parsing only headlines, skip any text before first one.
(when (and (eq granularity 'headline) (not (org-at-heading-p))) (when (and (eq granularity 'headline) (not (org-at-heading-p)))
(org-with-limited-levels (outline-next-heading))) (org-with-limited-levels (outline-next-heading)))
(let (elements) (let (elements)
(while (< (point) end) (while (< (point) end)
;; Find current element's type and parse it accordingly to ;; Visible only: skip invisible parts due to folding.
;; its category. (if (and visible-only (org-invisible-p nil t))
(let* ((element (org-element--current-element (progn
end granularity mode structure)) (goto-char (org-find-visible))
(type (org-element-type element)) (when (and (eolp) (not (eobp))) (forward-char)))
(cbeg (org-element-property :contents-begin element))) ;; Find current element's type and parse it accordingly to
(goto-char (org-element-property :end element)) ;; its category.
;; Visible only: skip invisible parts between siblings. (let* ((element (org-element--current-element
(when (and visible-only (org-invisible-p2)) end granularity mode structure))
(goto-char (min (1+ (org-find-visible)) end))) (type (org-element-type element))
;; Fill ELEMENT contents by side-effect. (cbeg (org-element-property :contents-begin element)))
(cond (goto-char (org-element-property :end element))
;; If element has no contents, don't modify it. ;; Fill ELEMENT contents by side-effect.
((not cbeg)) (cond
;; Greater element: parse it between `contents-begin' and ;; If element has no contents, don't modify it.
;; `contents-end'. Make sure GRANULARITY allows the ((not cbeg))
;; recursion, or ELEMENT is a headline, in which case going ;; Greater element: parse it between `contents-begin' and
;; inside is mandatory, in order to get sub-level headings. ;; `contents-end'. Ensure GRANULARITY allows recursion,
((and (memq type org-element-greater-elements) ;; or ELEMENT is a headline, in which case going inside
(or (memq granularity '(element object nil)) ;; is mandatory, in order to get sub-level headings.
(and (eq granularity 'greater-element) ((and (memq type org-element-greater-elements)
(eq type 'section)) (or (memq granularity '(element object nil))
(eq type 'headline))) (and (eq granularity 'greater-element)
(org-element--parse-elements (eq type 'section))
cbeg (org-element-property :contents-end element) (eq type 'headline)))
;; Possibly switch to a special mode. (org-element--parse-elements
(org-element--next-mode mode type t) cbeg (org-element-property :contents-end element)
(and (memq type '(item plain-list)) ;; Possibly switch to a special mode.
(org-element-property :structure element)) (org-element--next-mode mode type t)
granularity visible-only element)) (and (memq type '(item plain-list))
;; ELEMENT has contents. Parse objects inside, if (org-element-property :structure element))
;; GRANULARITY allows it. granularity visible-only element))
((memq granularity '(object nil)) ;; ELEMENT has contents. Parse objects inside, if
(org-element--parse-objects ;; GRANULARITY allows it.
cbeg (org-element-property :contents-end element) element ((memq granularity '(object nil))
(org-element-restriction type)))) (org-element--parse-objects
(push (org-element-put-property element :parent acc) elements) cbeg (org-element-property :contents-end element) element
;; Update mode. (org-element-restriction type))))
(setq mode (org-element--next-mode mode type nil)))) (push (org-element-put-property element :parent acc) elements)
;; Update mode.
(setq mode (org-element--next-mode mode type nil)))))
;; Return result. ;; Return result.
(apply #'org-element-set-contents acc (nreverse elements))))) (apply #'org-element-set-contents acc (nreverse elements)))))

View File

@ -1065,10 +1065,16 @@ the value in cdr."
(get-text-property (or (next-single-property-change 0 prop s) 0) (get-text-property (or (next-single-property-change 0 prop s) 0)
prop s))) prop s)))
(defun org-invisible-p (&optional pos) (defun org-invisible-p (&optional pos folding-only)
"Non-nil if the character after POS is invisible. "Non-nil if the character after POS is invisible.
If POS is nil, use `point' instead." If POS is nil, use `point' instead. When optional argument
(get-char-property (or pos (point)) 'invisible)) FOLDING-ONLY is non-nil, only consider invisible parts due to
folding of a headline, a block or a drawer, i.e., not because of
fontification."
(let ((value (get-char-property (or pos (point)) 'invisible)))
(cond ((not value) nil)
(folding-only (memq value '(org-hide-block org-hide-drawer outline)))
(t value))))
(defun org-truely-invisible-p () (defun org-truely-invisible-p ()
"Check if point is at a character currently not visible. "Check if point is at a character currently not visible.
@ -1086,6 +1092,18 @@ move it back by one char before doing this check."
(backward-char 1)) (backward-char 1))
(org-invisible-p))) (org-invisible-p)))
(defun org-find-visible ()
"Return closest visible buffer position, or `point-max'"
(if (org-invisible-p)
(next-single-char-property-change (point) 'invisible)
(point)))
(defun org-find-invisible ()
"Return closest invisible buffer position, or `point-max'"
(if (org-invisible-p)
(point)
(next-single-char-property-change (point) 'invisible)))
;;; Time ;;; Time

View File

@ -17694,17 +17694,6 @@ this numeric value."
((org-at-table-p) (call-interactively 'org-table-hline-and-move)) ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
(t (call-interactively 'org-insert-heading)))) (t (call-interactively 'org-insert-heading))))
(defun org-find-visible ()
(let ((s (point)))
(while (and (not (= (point-max) (setq s (next-overlay-change s))))
(get-char-property s 'invisible)))
s))
(defun org-find-invisible ()
(let ((s (point)))
(while (and (not (= (point-max) (setq s (next-overlay-change s))))
(not (get-char-property s 'invisible))))
s))
(defun org-copy-visible (beg end) (defun org-copy-visible (beg end)
"Copy the visible parts of the region." "Copy the visible parts of the region."
(interactive "r") (interactive "r")

View File

@ -3372,11 +3372,25 @@ Text
"Test `org-element-parse-buffer' with visible only argument." "Test `org-element-parse-buffer' with visible only argument."
(should (should
(equal '("H1" "H3" "H5") (equal '("H1" "H3" "H5")
(org-test-with-temp-text (org-test-with-temp-text
"* H1\n** H2\n** H3 :visible:\n** H4\n** H5 :visible:" "* H1\n** H2\n** H3 :visible:\n** H4\n** H5 :visible:"
(org-occur ":visible:") (org-occur ":visible:")
(org-element-map (org-element-parse-buffer nil t) 'headline (org-element-map (org-element-parse-buffer nil t) 'headline
(lambda (hl) (org-element-property :raw-value hl))))))) (lambda (hl) (org-element-property :raw-value hl))))))
(should
(equal "Test"
(let ((contents "Test"))
(org-test-with-temp-text contents
(add-text-properties 0 1 '(invisible t) contents)
(org-element-map (org-element-parse-buffer nil t) 'plain-text
#'org-no-properties nil t)))))
(should
(equal "Test"
(let ((contents "Test"))
(org-test-with-temp-text (concat "- " contents)
(add-text-properties 0 1 '(invisible t) contents)
(org-element-map (org-element-parse-buffer nil t) 'plain-text
#'org-no-properties nil t))))))