forked from mirrors/org-mode
org-element: Fix `org-element-normalize-contents'
* lisp/org-element.el (org-element-normalize-contents): Fix return value when any line after the first has no indentation. * testing/lisp/test-org-element.el (test-org-element/normalize-contents): Add test.
This commit is contained in:
parent
1d57c5670a
commit
5ada73aef6
|
@ -4677,17 +4677,16 @@ indentation removed from its contents."
|
||||||
;; the beginnings of the contents or right after a line
|
;; the beginnings of the contents or right after a line
|
||||||
;; break.
|
;; break.
|
||||||
(lambda (blob first-flag min-ind)
|
(lambda (blob first-flag min-ind)
|
||||||
(catch 'zero
|
|
||||||
(dolist (datum (org-element-contents blob) min-ind)
|
(dolist (datum (org-element-contents blob) min-ind)
|
||||||
(when first-flag
|
(when first-flag
|
||||||
(setq first-flag nil)
|
(setq first-flag nil)
|
||||||
(cond
|
(cond
|
||||||
;; Objects cannot start with spaces: in this
|
;; Objects cannot start with spaces: in this
|
||||||
;; case, indentation is 0.
|
;; case, indentation is 0.
|
||||||
((not (stringp datum)) (throw 'zero 0))
|
((not (stringp datum)) (throw :zero 0))
|
||||||
((not (string-match
|
((not (string-match
|
||||||
"\\`\\([ \t]+\\)\\([^ \t\n]\\|\n\\|\\'\\)" datum))
|
"\\`\\([ \t]+\\)\\([^ \t\n]\\|\n\\|\\'\\)" datum))
|
||||||
(throw 'zero 0))
|
(throw :zero 0))
|
||||||
((equal (match-string 2 datum) "\n")
|
((equal (match-string 2 datum) "\n")
|
||||||
(put-text-property
|
(put-text-property
|
||||||
(match-beginning 1) (match-end 1) 'org-ind 'empty datum))
|
(match-beginning 1) (match-end 1) 'org-ind 'empty datum))
|
||||||
|
@ -4700,24 +4699,29 @@ indentation removed from its contents."
|
||||||
((stringp datum)
|
((stringp datum)
|
||||||
(let ((s 0))
|
(let ((s 0))
|
||||||
(while (string-match
|
(while (string-match
|
||||||
"\n\\([ \t]+\\)\\([^ \t\n]\\|\n\\|\\'\\)" datum s)
|
"\n\\([ \t]*\\)\\([^ \t\n]\\|\n\\|\\'\\)" datum s)
|
||||||
(setq s (match-end 1))
|
(setq s (match-end 1))
|
||||||
(if (equal (match-string 2 datum) "\n")
|
(cond
|
||||||
(put-text-property
|
((equal (match-string 1 datum) "")
|
||||||
(match-beginning 1) (match-end 1)
|
(unless (member (match-string 2 datum) '("" "\n"))
|
||||||
'org-ind 'empty
|
(throw :zero 0)))
|
||||||
datum)
|
((equal (match-string 2 datum) "\n")
|
||||||
|
(put-text-property (match-beginning 1) (match-end 1)
|
||||||
|
'org-ind 'empty datum))
|
||||||
|
(t
|
||||||
(let ((i (string-width (match-string 1 datum))))
|
(let ((i (string-width (match-string 1 datum))))
|
||||||
(put-text-property
|
(put-text-property (match-beginning 1) (match-end 1)
|
||||||
(match-beginning 1) (match-end 1) 'org-ind i datum)
|
'org-ind i datum)
|
||||||
(setq min-ind (min i min-ind)))))))
|
(setq min-ind (min i min-ind))))))))
|
||||||
((eq (org-element-type datum) 'line-break)
|
((eq (org-element-type datum) 'line-break)
|
||||||
(setq first-flag t))
|
(setq first-flag t))
|
||||||
((memq (org-element-type datum) org-element-recursive-objects)
|
((memq (org-element-type datum) org-element-recursive-objects)
|
||||||
(setq min-ind
|
(setq min-ind
|
||||||
(funcall find-min-ind datum first-flag min-ind))))))))
|
(funcall find-min-ind datum first-flag min-ind)))))))
|
||||||
(min-ind (funcall find-min-ind
|
(min-ind
|
||||||
element (not ignore-first) most-positive-fixnum)))
|
(catch :zero
|
||||||
|
(funcall find-min-ind
|
||||||
|
element (not ignore-first) most-positive-fixnum))))
|
||||||
(if (or (zerop min-ind) (= min-ind most-positive-fixnum)) element
|
(if (or (zerop min-ind) (= min-ind most-positive-fixnum)) element
|
||||||
;; Build ELEMENT back, replacing each string with the same
|
;; Build ELEMENT back, replacing each string with the same
|
||||||
;; string minus common indentation.
|
;; string minus common indentation.
|
||||||
|
|
|
@ -3272,6 +3272,11 @@ Text
|
||||||
(org-element-normalize-contents
|
(org-element-normalize-contents
|
||||||
'(paragraph nil " Two spaces\n Three spaces"))
|
'(paragraph nil " Two spaces\n Three spaces"))
|
||||||
'(paragraph nil "Two spaces\n Three spaces")))
|
'(paragraph nil "Two spaces\n Three spaces")))
|
||||||
|
(should
|
||||||
|
(equal
|
||||||
|
(org-element-normalize-contents
|
||||||
|
'(paragraph nil " Two spaces\nNo space"))
|
||||||
|
'(paragraph nil " Two spaces\nNo space")))
|
||||||
;; Ignore objects within contents when computing maximum common
|
;; Ignore objects within contents when computing maximum common
|
||||||
;; indentation. However, if contents start with an object, common
|
;; indentation. However, if contents start with an object, common
|
||||||
;; indentation is 0.
|
;; indentation is 0.
|
||||||
|
|
Loading…
Reference in a new issue