summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2017-01-12 14:33:17 +0100
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2017-01-12 14:33:17 +0100
commit5ada73aef659442fa8cd8c226ad26194a4f25129 (patch)
tree8fc6af674a34bb5809317786c20a9b33a376c997
parent1d57c5670aab85162cf3589ad90624d2dbc7a52f (diff)
downloadorg-mode-5ada73aef659442fa8cd8c226ad26194a4f25129.tar.gz
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.
-rw-r--r--lisp/org-element.el84
-rw-r--r--testing/lisp/test-org-element.el5
2 files changed, 49 insertions, 40 deletions
diff --git a/lisp/org-element.el b/lisp/org-element.el
index 75f1d04..537f3fe 100644
--- a/lisp/org-element.el
+++ b/lisp/org-element.el
@@ -4677,47 +4677,51 @@ indentation removed from its contents."
;; the beginnings of the contents or right after a line
;; break.
(lambda (blob first-flag min-ind)
- (catch 'zero
- (dolist (datum (org-element-contents blob) min-ind)
- (when first-flag
- (setq first-flag nil)
- (cond
- ;; Objects cannot start with spaces: in this
- ;; case, indentation is 0.
- ((not (stringp datum)) (throw 'zero 0))
- ((not (string-match
- "\\`\\([ \t]+\\)\\([^ \t\n]\\|\n\\|\\'\\)" datum))
- (throw 'zero 0))
- ((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))))
- (put-text-property
- (match-beginning 1) (match-end 1) 'org-ind i datum)
- (setq min-ind (min i min-ind))))))
+ (dolist (datum (org-element-contents blob) min-ind)
+ (when first-flag
+ (setq first-flag nil)
(cond
- ((stringp datum)
- (let ((s 0))
- (while (string-match
- "\n\\([ \t]+\\)\\([^ \t\n]\\|\n\\|\\'\\)" datum s)
- (setq s (match-end 1))
- (if (equal (match-string 2 datum) "\n")
- (put-text-property
- (match-beginning 1) (match-end 1)
- 'org-ind 'empty
- datum)
- (let ((i (string-width (match-string 1 datum))))
- (put-text-property
- (match-beginning 1) (match-end 1) 'org-ind i datum)
- (setq min-ind (min i min-ind)))))))
- ((eq (org-element-type datum) 'line-break)
- (setq first-flag t))
- ((memq (org-element-type datum) org-element-recursive-objects)
- (setq min-ind
- (funcall find-min-ind datum first-flag min-ind))))))))
- (min-ind (funcall find-min-ind
- element (not ignore-first) most-positive-fixnum)))
+ ;; Objects cannot start with spaces: in this
+ ;; case, indentation is 0.
+ ((not (stringp datum)) (throw :zero 0))
+ ((not (string-match
+ "\\`\\([ \t]+\\)\\([^ \t\n]\\|\n\\|\\'\\)" datum))
+ (throw :zero 0))
+ ((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))))
+ (put-text-property
+ (match-beginning 1) (match-end 1) 'org-ind i datum)
+ (setq min-ind (min i min-ind))))))
+ (cond
+ ((stringp datum)
+ (let ((s 0))
+ (while (string-match
+ "\n\\([ \t]*\\)\\([^ \t\n]\\|\n\\|\\'\\)" datum s)
+ (setq s (match-end 1))
+ (cond
+ ((equal (match-string 1 datum) "")
+ (unless (member (match-string 2 datum) '("" "\n"))
+ (throw :zero 0)))
+ ((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))))
+ (put-text-property (match-beginning 1) (match-end 1)
+ 'org-ind i datum)
+ (setq min-ind (min i min-ind))))))))
+ ((eq (org-element-type datum) 'line-break)
+ (setq first-flag t))
+ ((memq (org-element-type datum) org-element-recursive-objects)
+ (setq min-ind
+ (funcall find-min-ind datum first-flag min-ind)))))))
+ (min-ind
+ (catch :zero
+ (funcall find-min-ind
+ element (not ignore-first) most-positive-fixnum))))
(if (or (zerop min-ind) (= min-ind most-positive-fixnum)) element
;; Build ELEMENT back, replacing each string with the same
;; string minus common indentation.
diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el
index 2a507f8..dc33162 100644
--- a/testing/lisp/test-org-element.el
+++ b/testing/lisp/test-org-element.el
@@ -3272,6 +3272,11 @@ Text
(org-element-normalize-contents
'(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
;; indentation. However, if contents start with an object, common
;; indentation is 0.