Browse Source

Prevent spurious newline character in fixed-width area value

* lisp/org-element.el (org-element-fixed-width-parser): Prevent
  spurious newline character at the end of the value.
* lisp/ox-latex.el (org-latex-fixed-width):
* lisp/ox-man.el (org-man-fixed-width):
* lisp/ox-texinfo.el (org-texinfo-fixed-width):
* contrib/lisp/ox-groff.el (org-groff-fixed-width): Adapt to change.
* testing/lisp/test-org-element.el (test-org-element/fixed-width-parser):
  Update tests.

Reported-by: 孙振祥 <sunznx@icloud.com>
<http://lists.gnu.org/archive/html/emacs-orgmode/2018-07/msg00003.html>
Nicolas Goaziou 1 year ago
parent
commit
7d9e4da447
6 changed files with 25 additions and 38 deletions
  1. 1 1
      contrib/lisp/ox-groff.el
  2. 6 13
      lisp/org-element.el
  3. 1 1
      lisp/ox-latex.el
  4. 1 1
      lisp/ox-man.el
  5. 1 1
      lisp/ox-texinfo.el
  6. 15 21
      testing/lisp/test-org-element.el

+ 1 - 1
contrib/lisp/ox-groff.el

@@ -902,7 +902,7 @@ CONTENTS is nil.  INFO is a plist holding contextual information."
 CONTENTS is nil.  INFO is a plist holding contextual information."
   (org-groff--wrap-label
    fixed-width
-   (format "\\fC\n%s\\fP"
+   (format "\\fC\n%s\n\\fP"
            (org-remove-indentation
             (org-element-property :value fixed-width)))))
 

+ 6 - 13
lisp/org-element.el

@@ -2072,26 +2072,22 @@ Assume point is at the beginning of the fixed-width area."
   (save-excursion
     (let* ((begin (car affiliated))
 	   (post-affiliated (point))
-	   value
 	   (end-area
 	    (progn
 	      (while (and (< (point) limit)
 			  (looking-at "[ \t]*:\\( \\|$\\)"))
-		;; Accumulate text without starting colons.
-		(setq value
-		      (concat value
-			      (buffer-substring-no-properties
-			       (match-end 0) (point-at-eol))
-			      "\n"))
 		(forward-line))
-	      (point)))
+	      (if (bolp) (line-end-position 0) (point))))
 	   (end (progn (skip-chars-forward " \r\t\n" limit)
 		       (if (eobp) (point) (line-beginning-position)))))
       (list 'fixed-width
 	    (nconc
 	     (list :begin begin
 		   :end end
-		   :value value
+		   :value (replace-regexp-in-string
+			   "^[ \t]*: ?" ""
+			   (buffer-substring-no-properties post-affiliated
+							   end-area))
 		   :post-blank (count-lines end-area end)
 		   :post-affiliated post-affiliated)
 	     (cdr affiliated))))))
@@ -2099,10 +2095,7 @@ Assume point is at the beginning of the fixed-width area."
 (defun org-element-fixed-width-interpreter (fixed-width _)
   "Interpret FIXED-WIDTH element as Org syntax."
   (let ((value (org-element-property :value fixed-width)))
-    (and value
-	 (replace-regexp-in-string
-	  "^" ": "
-	  (if (string-match "\n\\'" value) (substring value 0 -1) value)))))
+    (and value (replace-regexp-in-string "^" ": " value))))
 
 
 ;;;; Horizontal Rule

+ 1 - 1
lisp/ox-latex.el

@@ -1852,7 +1852,7 @@ CONTENTS is nil.  INFO is a plist holding contextual information."
 CONTENTS is nil.  INFO is a plist holding contextual information."
   (org-latex--wrap-label
    fixed-width
-   (format "\\begin{verbatim}\n%s\\end{verbatim}"
+   (format "\\begin{verbatim}\n%s\n\\end{verbatim}"
 	   (org-remove-indentation
 	    (org-element-property :value fixed-width)))
    info))

+ 1 - 1
lisp/ox-man.el

@@ -424,7 +424,7 @@ CONTENTS is nil.  INFO is a plist holding contextual information."
 CONTENTS is nil.  INFO is a plist holding contextual information."
   (org-man--wrap-label
    fixed-width
-   (format "\\fC\n%s\\fP"
+   (format "\\fC\n%s\n\\fP"
            (org-remove-indentation
             (org-element-property :value fixed-width)))))
 

+ 1 - 1
lisp/ox-texinfo.el

@@ -825,7 +825,7 @@ CONTENTS is nil.  INFO is a plist holding contextual information."
 (defun org-texinfo-fixed-width (fixed-width _contents _info)
   "Transcode a FIXED-WIDTH element from Org to Texinfo.
 CONTENTS is nil.  INFO is a plist holding contextual information."
-  (format "@example\n%s@end example"
+  (format "@example\n%s\n@end example"
 	  (org-remove-indentation
 	   (org-texinfo--sanitize-content
 	    (org-element-property :value fixed-width)))))

+ 15 - 21
testing/lisp/test-org-element.el

@@ -900,29 +900,32 @@ Some other text
   "Test fixed-width area parsing."
   ;; Preserve indentation.
   (should
-   (org-test-with-temp-text ": no blank\n:  one blank"
-     (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
+   (equal "no blank\n one blank"
+	  (org-test-with-temp-text ": no blank\n:  one blank"
+	    (org-element-property :value (org-element-at-point)))))
   ;; Fixed-width with empty lines.
   (should
-   (org-test-with-temp-text ": first part\n:\n: \n: second part"
-     (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
+   (equal "first part\n\n\nsecond part"
+	  (org-test-with-temp-text ": first part\n:\n: \n: second part"
+	    (org-element-property :value (org-element-at-point)))))
   ;; Parse indented fixed-width markers.
   (should
-   (org-test-with-temp-text "Text\n  : no blank\n  :  one blank"
-     (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
+   (eq 'fixed-width
+       (org-test-with-temp-text "Text\n<point>  : no blank\n  :  one blank"
+	 (org-element-type (org-element-at-point)))))
   ;; Distinguish fixed-width areas within a list and outside of it.
   (should
-   (= 2
-      (length
-       (org-test-with-temp-text "
+   (org-test-with-temp-text "
 - Item
-  : fixed-width inside
+  : fixed-width inside<point>
 : fixed-width outside"
-	 (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))))
+     (= (org-element-property :end (org-element-at-point))
+	(line-beginning-position 2))))
   ;; Handle non-empty blank line at the end of buffer.
   (should
    (org-test-with-temp-text ": A\n "
-     (= (org-element-property :end (org-element-at-point)) (point-max)))))
+     (= (org-element-property :end (org-element-at-point))
+	(point-max)))))
 
 
 ;;;; Footnote Definition
@@ -2859,15 +2862,6 @@ CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] =>  0:01"))))
   ;; Preserve indentation.
   (should (equal (org-test-parse-and-interpret ":  2 blanks\n: 1 blank")
 		 ":  2 blanks\n: 1 blank\n"))
-  ;; Remove last newline character
-  (should
-   (equal (org-element-fixed-width-interpreter
-	   '(fixed-width (:value "Test\n")) nil)
-	  ": Test"))
-  (should
-   (equal (org-element-fixed-width-interpreter
-	   '(fixed-width (:value "Test")) nil)
-	  ": Test"))
   ;; Handle empty string.
   (should
    (equal (org-element-fixed-width-interpreter