Browse Source

Deprecate `org-babel-trim' in favor of `org-trim'

* lisp/org.el (org-trim): Add optional argument.

* contrib/lisp/ob-mathematica.el (org-babel-execute:mathematica):
* contrib/lisp/org-bibtex-extras.el (obe-html-export-citations):
* contrib/lisp/org-mime.el (org-mime-compose):
* lisp/ob-C.el (org-babel-C-execute):
* lisp/ob-calc.el (org-babel-execute:calc):
* lisp/ob-clojure.el (org-babel-expand-body:clojure):
* lisp/ob-core.el (org-babel-initiate-session):
(org-babel-parse-multiple-vars):
(org-babel-read-element):
(org-babel-insert-result):
(org-babel-expand-noweb-references):
(org-babel-trim): Mark obsolete.
* lisp/ob-forth.el (org-babel-forth-session-execute):
* lisp/ob-fortran.el (org-babel-execute:fortran):
* lisp/ob-haskell.el (org-babel-execute:haskell):
* lisp/ob-latex.el (org-babel-expand-body:latex):
* lisp/ob-lisp.el (org-babel-expand-body:lisp):
* lisp/ob-ocaml.el (org-babel-execute:ocaml):
* lisp/ob-octave.el (org-babel-octave-evaluate-session):
* lisp/ob-python.el (org-babel-python-evaluate-external-process):
(org-babel-python-evaluate-session):
* lisp/ob-ref.el (org-babel-ref-split-args):
* lisp/ob-ruby.el (org-babel-ruby-evaluate):
* lisp/ob-shell.el (org-babel-sh-evaluate):
* lisp/ob-table.el (org-sbe):
* lisp/ob-tangle.el (org-babel-spec-to-string):
* lisp/org-bibtex.el (org-bibtex-get):
* testing/lisp/test-ob.el (test-ob/parse-header-args): Use `org-trim'
  instead of `org-babel-trim'.
Nicolas Goaziou 3 years ago
parent
commit
4f63cfabb3

+ 4 - 1
contrib/lisp/ob-mathematica.el

@@ -14,6 +14,9 @@
 (require 'ob-ref)
 (require 'ob-comint)
 (require 'ob-eval)
+
+(declare-function org-trim "org" (s &optional keep-lead))
+
 ;; Optionally require mma.el for font lock, etc
 (require 'mma nil 'noerror)
 (add-to-list 'org-src-lang-modes '("mathematica" . "mma"))
@@ -56,7 +59,7 @@ called by `org-babel-execute-src-block'"
 	       (and (member "output" result-params)
 		    (not (member "table" result-params))))
 	   raw
-	 (org-babel-script-escape (org-babel-trim raw))))
+	 (org-babel-script-escape (org-trim raw))))
     (org-babel-eval (concat cmd " " tmp-script-file) ""))))
 
 (defun org-babel-prep-session:mathematica (session params)

+ 3 - 1
contrib/lisp/org-bibtex-extras.el

@@ -61,6 +61,8 @@
 ;;; Code:
 (require 'org-bibtex)
 
+(declare-function org-trim "org" (s &optional keep-lead))
+
 (defcustom obe-bibtex-file nil "File holding bibtex entries.")
 
 (defcustom obe-html-link-base nil
@@ -91,7 +93,7 @@ For example, to point to your `obe-bibtex-file' use the following.
       (replace-match
        (save-match-data
 	 (mapconcat (lambda (c) (format "[[%s#%s][%s]]" obe-html-link-base c c))
-		    (mapcar #'org-babel-trim
+		    (mapcar #'org-trim
 			    (split-string (match-string 1) ",")) ", "))))))
 
 (defun obe-meta-to-json (meta &optional fields)

+ 3 - 2
contrib/lisp/org-mime.el

@@ -57,6 +57,7 @@
 
 (declare-function org-export-string-as "ox"
 		  (string backend &optional body-only ext-plist))
+(declare-function org-trim "org" (s &optional keep-lead))
 
 (defcustom org-mime-use-property-inheritance nil
   "Non-nil means al MAIL_ properties apply also for sublevels."
@@ -302,7 +303,7 @@ otherwise export the entire body."
      ((eq fmt 'org)
       (require 'ox-org)
       (insert (org-export-string-as
-	       (org-babel-trim (funcall bhook body 'org)) 'org t)))
+	       (org-trim (funcall bhook body 'org)) 'org t)))
      ((eq fmt 'ascii)
       (require 'ox-ascii)
       (insert (org-export-string-as
@@ -320,7 +321,7 @@ otherwise export the entire body."
 	     (html (org-mime-apply-html-hook (car html-and-images))))
 	(insert (org-mime-multipart
 		 (org-export-string-as
-		  (org-babel-trim
+		  (org-trim
 		   (funcall bhook body (if (eq fmt 'html) 'org 'ascii)))
 		  (if (eq fmt 'html) 'org 'ascii) t)
 		 html)

+ 7 - 1
etc/ORG-NEWS

@@ -337,7 +337,8 @@ Use ~org-agenda-filter-by-tag~ instead.
 Use ~org-agenda-today-p~ instead.
 *** ~org-babel-get-header~ is removed.
 Use ~org-babel--get-vars~ or ~assq~ instead, as applicable.
-
+*** ~org-babel-trim~ is deprecated.
+Use ~org-trim~ instead.
 *** ~org-element-remove-indentation~ is deprecated.
 Use ~org-remove-indentation~ instead.
 *** ~org-image-file-name-regexp~ is deprecated
@@ -424,6 +425,11 @@ called with 6 parameters.
 *** =ITEM= special property returns headline without stars
 *** Rename ~org-insert-columns-dblock~ into ~org-columns-insert-dblock~
 The previous name is, for the time being, kept as an obsolete alias.
+*** ~org-trim~ can preserve leading indentation.
+When setting a new optional argument to a non-nil value, ~org-trim~
+preserves leading indentation while removing blank lines at the
+beginning of the string.  The behavior is identical for white space at
+the end of the string.
 *** Function ~org-info-export~ changes.
 HTML links created from certain info links now point to =gnu.org= URL's rather
 than just to local files. For example info links such as =info:emacs#List

+ 3 - 3
lisp/ob-C.el

@@ -34,9 +34,9 @@
 (require 'ob)
 (require 'cc-mode)
 
-(declare-function org-entry-get "org"
-		  (pom property &optional inherit literal-nil))
+(declare-function org-entry-get "org" (pom property &optional inherit literal-nil))
 (declare-function org-remove-indentation "org" (code &optional n))
+(declare-function org-trim "org" (s &optional keep-lead))
 
 (defvar org-babel-tangle-lang-exts)
 (add-to-list 'org-babel-tangle-lang-exts '("C++" . "cpp"))
@@ -169,7 +169,7 @@ or `org-babel-execute:C++' or `org-babel-execute:D'."
 		       cmdline)))
 	    "")))
       (when results
-	(setq results (org-babel-trim (org-remove-indentation results)))
+	(setq results (org-trim (org-remove-indentation results)))
 	(org-babel-reassemble-table
 	 (org-babel-result-cond (cdr (assoc :result-params params))
 	   (org-babel-read results t)

+ 1 - 1
lisp/ob-J.el

@@ -31,7 +31,7 @@
 ;;; Code:
 (require 'ob)
 
-(declare-function org-trim "org" (S))
+(declare-function org-trim "org" (s &optional keep-lead))
 (declare-function j-console-ensure-session "ext:j-console" ())
 
 (defun org-babel-expand-body:J (body _params &optional _processed-params)

+ 2 - 1
lisp/ob-calc.el

@@ -34,6 +34,7 @@
 (declare-function calc-store-into    "calc-store" (&optional var))
 (declare-function calc-recall        "calc-store" (&optional var))
 (declare-function math-evaluate-expr "calc-ext"   (x))
+(declare-function org-trim "org" (s &optional keep-lead))
 
 (defvar org-babel-default-header-args:calc nil
   "Default arguments for evaluating an calc source block.")
@@ -84,7 +85,7 @@
                                    ;; parse line into calc objects
                                    (car (math-read-exprs line)))))))))
                   ))))))
-     (mapcar #'org-babel-trim
+     (mapcar #'org-trim
 	     (split-string (org-babel-expand-body:calc body params) "[\n\r]"))))
   (save-excursion
     (with-current-buffer (get-buffer "*Calculator*")

+ 9 - 9
lisp/ob-clojure.el

@@ -48,6 +48,7 @@
 (declare-function nrepl-dict-get "ext:nrepl-client" (dict key))
 (declare-function nrepl-sync-request:eval "ext:nrepl-client"
 		  (input connection session &optional ns))
+(declare-function org-trim "org" (s &optional keep-lead))
 (declare-function slime-eval "ext:slime" (sexp &optional package))
 
 (defvar org-babel-tangle-lang-exts)
@@ -70,15 +71,14 @@
   (let* ((vars (org-babel--get-vars params))
 	 (result-params (cdr (assoc :result-params params)))
 	 (print-level nil) (print-length nil)
-	 (body (org-babel-trim
-		(if (> (length vars) 0)
-		    (concat "(let ["
-			    (mapconcat
-			     (lambda (var)
-			       (format "%S (quote %S)" (car var) (cdr var)))
-			     vars "\n      ")
-			    "]\n" body ")")
-		  body))))
+	 (body (org-trim
+		(if (null vars) (org-trim body)
+		  (concat "(let ["
+			  (mapconcat
+			   (lambda (var)
+			     (format "%S (quote %S)" (car var) (cdr var)))
+			   vars "\n      ")
+			  "]\n" body ")")))))
     (if (or (member "code" result-params)
 	    (member "pp" result-params))
 	(format "(clojure.pprint/pprint (do %s))" body)

+ 7 - 12
lisp/ob-core.el

@@ -73,6 +73,7 @@
 (declare-function org-table-import "org-table" (file arg))
 (declare-function org-table-align "org-table" ())
 (declare-function org-table-end "org-table" (&optional table-type))
+(declare-function org-trim "org" (s &optional keep-lead))
 (declare-function orgtbl-to-generic "org-table" (table params))
 (declare-function orgtbl-to-orgtbl "org-table" (table params))
 (declare-function org-babel-tangle-comment-links "ob-tangle" (&optional info))
@@ -925,7 +926,7 @@ the session.  Copy the body of the code block to the kill ring."
 	(error "This block is not using a session!"))
     (unless (fboundp init-cmd)
       (error "No org-babel-initiate-session function for %s!" lang))
-    (with-temp-buffer (insert (org-babel-trim body))
+    (with-temp-buffer (insert (org-trim body))
                       (copy-region-as-kill (point-min) (point-max)))
     (when arg
       (unless (fboundp prep-cmd)
@@ -1509,7 +1510,7 @@ shown below.
   (let (results)
     (mapc (lambda (pair)
 	    (if (eq (car pair) :var)
-		(mapcar (lambda (v) (push (cons :var (org-babel-trim v)) results))
+		(mapcar (lambda (v) (push (cons :var (org-trim v)) results))
 			(org-babel-join-splits-near-ch
 			 61 (org-babel-balanced-split (cdr pair) 32)))
 	      (push pair results)))
@@ -2008,7 +2009,7 @@ Return nil if ELEMENT cannot be read."
    (goto-char (org-element-property :post-affiliated element))
    (pcase (org-element-type element)
      (`fixed-width
-      (let ((v (org-babel-trim (org-element-property :value element))))
+      (let ((v (org-trim (org-element-property :value element))))
 	(or (org-babel-number-p v) v)))
      (`table (org-babel-read-table))
      (`plain-list (org-babel-read-list))
@@ -2261,7 +2262,7 @@ INFO may provide the values of these header arguments (in the
 		   ;; Insert a list if preferred.
 		   ((member "list" result-params)
 		    (insert
-		     (org-babel-trim
+		     (org-trim
 		      (org-list-to-generic
 		       (cons 'unordered
 			     (mapcar
@@ -2666,7 +2667,7 @@ block but are passed literally to the \"example-block\"."
 		   (with-temp-buffer
 		     (funcall (intern (concat lang "-mode")))
 		     (comment-region (point) (progn (insert text) (point)))
-		     (org-babel-trim (buffer-string)))))
+		     (org-trim (buffer-string)))))
 	 index source-name evaluate prefix)
     (with-temp-buffer
       (setq-local org-babel-noweb-wrap-start ob-nww-start)
@@ -2912,13 +2913,7 @@ can be specified as the REGEXP argument."
       (setq string (substring string 0 -1)))
     string))
 
-(defun org-babel-trim (string &optional regexp)
-  "Strip a leading and trailing space or carriage return from STRING.
-Like `org-babel-chomp', but run on both the first and last
-character of the string."
-  (org-babel-chomp
-   (org-reverse-string
-    (org-babel-chomp (org-reverse-string string) regexp)) regexp))
+(define-obsolete-function-alias 'org-babel-trim 'org-trim "Org 9.0")
 
 (defun org-babel-local-file-name (file)
   "Return the local name component of FILE."

+ 5 - 4
lisp/ob-forth.el

@@ -35,6 +35,7 @@
 (require 'ob)
 
 (declare-function forth-proc "ext:gforth" ())
+(declare-function org-trim "org" (s &optional keep-lead))
 
 (defvar org-babel-default-header-args:forth '((:session . "yes"))
   "Default header arguments for forth code blocks.")
@@ -76,10 +77,10 @@ This function is called by `org-babel-execute-src-block'"
 		    (org-babel-eval-error-notify 1
 		     (buffer-substring
 		      (+ (match-beginning 0) 1) (point-max))) nil))))
-	      (split-string (org-babel-trim
-			     (org-babel-expand-body:generic
-			      body params))
-			    "\n" 'omit-nulls)))))
+	      (split-string (org-trim
+			     (org-babel-expand-body:generic body params))
+			    "\n"
+			    'omit-nulls)))))
 
 (provide 'ob-forth)
 

+ 2 - 1
lisp/ob-fortran.el

@@ -34,6 +34,7 @@
 (declare-function org-entry-get "org"
 		  (pom property &optional inherit literal-nil))
 (declare-function org-remove-indentation "org" (code &optional n))
+(declare-function org-trim "org" (s &optional keep-lead))
 
 (defvar org-babel-tangle-lang-exts)
 (add-to-list 'org-babel-tangle-lang-exts '("fortran" . "F90"))
@@ -60,7 +61,7 @@
 			(if (listp flags) flags (list flags)) " ")
 	     (org-babel-process-file-name tmp-src-file)) "")
     (let ((results
-           (org-babel-trim
+           (org-trim
             (org-remove-indentation
 	     (org-babel-eval
 	      (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))))

+ 3 - 2
lisp/ob-haskell.el

@@ -44,6 +44,7 @@
 (eval-when-compile (require 'cl))
 
 (declare-function org-remove-indentation "org" (code &optional n))
+(declare-function org-trim "org" (s &optional keep-lead))
 (declare-function haskell-mode "ext:haskell-mode" ())
 (declare-function run-haskell "ext:inf-haskell" (&optional arg))
 (declare-function inferior-haskell-load-file
@@ -69,14 +70,14 @@
          (session (org-babel-haskell-initiate-session session params))
          (raw (org-babel-comint-with-output
 		  (session org-babel-haskell-eoe t full-body)
-                (insert (org-babel-trim full-body))
+                (insert (org-trim full-body))
                 (comint-send-input nil t)
                 (insert org-babel-haskell-eoe)
                 (comint-send-input nil t)))
          (results (mapcar
                    #'org-babel-haskell-read-string
                    (cdr (member org-babel-haskell-eoe
-                                (reverse (mapcar #'org-babel-trim raw)))))))
+                                (reverse (mapcar #'org-trim raw)))))))
     (org-babel-reassemble-table
      (let ((result
             (case result-type

+ 5 - 6
lisp/ob-latex.el

@@ -32,12 +32,11 @@
 ;;; Code:
 (require 'ob)
 
-(declare-function org-create-formula-image "org"
-                  (string tofile options buffer &optional type))
-(declare-function org-splice-latex-header "org"
-		  (tpl def-pkg pkg snippets-p &optional extra))
-(declare-function org-latex-guess-inputenc "ox-latex" (header))
+(declare-function org-create-formula-image "org" (string tofile options buffer &optional type))
 (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
+(declare-function org-latex-guess-inputenc "ox-latex" (header))
+(declare-function org-splice-latex-header "org" (tpl def-pkg pkg snippets-p &optional extra))
+(declare-function org-trim "org" (s &optional keep-lead))
 
 (defvar org-babel-tangle-lang-exts)
 (add-to-list 'org-babel-tangle-lang-exts '("latex" . "tex"))
@@ -86,7 +85,7 @@
                  (if (stringp (cdr pair))
                      (cdr pair) (format "%S" (cdr pair)))
                  body))) (org-babel--get-vars params))
-  (org-babel-trim body))
+  (org-trim body))
 
 (defun org-babel-execute:latex (body params)
   "Execute a block of Latex code with Babel.

+ 8 - 9
lisp/ob-lisp.el

@@ -40,6 +40,7 @@
 
 (declare-function sly-eval "ext:sly" (sexp &optional package))
 (declare-function slime-eval "ext:slime" (sexp &optional package))
+(declare-function org-trim "org" (s &optional keep-lead))
 
 (defvar org-babel-tangle-lang-exts)
 (add-to-list 'org-babel-tangle-lang-exts '("lisp" . "lisp"))
@@ -69,15 +70,13 @@ current directory string."
   (let* ((vars (org-babel--get-vars params))
 	 (result-params (cdr (assoc :result-params params)))
 	 (print-level nil) (print-length nil)
-	 (body (org-babel-trim
-		(if (> (length vars) 0)
-		    (concat "(let ("
-			    (mapconcat
-			     (lambda (var)
-			       (format "(%S (quote %S))" (car var) (cdr var)))
-			     vars "\n      ")
-			    ")\n" body ")")
-		  body))))
+	 (body (if (null vars) (org-trim body)
+		 (concat "(let ("
+			 (mapconcat
+			  (lambda (var)
+			    (format "(%S (quote %S))" (car var) (cdr var)))
+			  vars "\n      ")
+			 ")\n" body ")"))))
     (if (or (member "code" result-params)
 	    (member "pp" result-params))
 	(format "(pprint %s)" body)

+ 3 - 2
lisp/ob-ocaml.el

@@ -42,6 +42,7 @@
 (declare-function tuareg-run-caml "ext:tuareg" ())
 (declare-function tuareg-run-ocaml "ext:tuareg" ())
 (declare-function tuareg-interactive-send-input "ext:tuareg" ())
+(declare-function org-trim "org" (s &optional keep-lead))
 
 (defvar org-babel-tangle-lang-exts)
 (add-to-list 'org-babel-tangle-lang-exts '("ocaml" . "ml"))
@@ -79,8 +80,8 @@
 					 (progn (setq out nil) line)
 				       (when (string-match re line)
 					 (progn (setq out t) nil))))
-				   (mapcar #'org-babel-trim (reverse raw)))))))
-	 (raw (org-babel-trim clean))
+				   (mapcar #'org-trim (reverse raw)))))))
+	 (raw (org-trim clean))
 	 (result-params (cdr (assoc :result-params params))))
     (string-match
      "\\(\\(.*\n\\)*\\)[^:\n]+ : \\([^=\n]+\\) =\\(\n\\| \\)\\(.+\\)$"

+ 3 - 2
lisp/ob-octave.el

@@ -34,6 +34,7 @@
 
 (declare-function matlab-shell "ext:matlab-mode")
 (declare-function matlab-shell-run-region "ext:matlab-mode")
+(declare-function org-trim "org" (s &optional keep-lead))
 
 (defvar org-babel-default-header-args:matlab '())
 (defvar org-babel-default-header-args:octave '())
@@ -240,11 +241,11 @@ value of the last statement in BODY, as elisp."
 	       (if matlabp
 		   (cdr (reverse (delq "" (mapcar
 					   #'org-babel-octave-read-string
-					   (mapcar #'org-babel-trim raw)))))
+					   (mapcar #'org-trim raw)))))
 		 (cdr (member org-babel-octave-eoe-output
 			      (reverse (mapcar
 					#'org-babel-octave-read-string
-					(mapcar #'org-babel-trim raw)))))))
+					(mapcar #'org-trim raw)))))))
 	 (mapconcat #'identity (reverse results) "\n"))))))
 
 (defun org-babel-octave-import-elisp-from-file (file-name)

+ 7 - 7
lisp/ob-python.el

@@ -31,6 +31,7 @@
 (eval-when-compile (require 'cl))
 
 (declare-function org-remove-indentation "org" )
+(declare-function org-trim "org" (s &optional keep-lead))
 (declare-function py-shell "ext:python-mode" (&optional argprompt))
 (declare-function py-toggle-shells "ext:python-mode" (arg))
 (declare-function run-python "ext:python" (&optional cmd dedicated show))
@@ -248,7 +249,7 @@ open('%s', 'w').write( pprint.pformat(main()) )")
      body result-type result-params preamble)))
 
 (defun org-babel-python-evaluate-external-process
-  (body &optional result-type result-params preamble)
+    (body &optional result-type result-params preamble)
   "Evaluate BODY in external python process.
 If RESULT-TYPE equals `output' then return standard output as a
 string.  If RESULT-TYPE equals `value' then return the value of the
@@ -269,15 +270,14 @@ last statement in BODY, as elisp."
                          org-babel-python-wrapper-method)
                        (mapconcat
                         (lambda (line) (format "\t%s" line))
-                        (split-string
-                         (org-remove-indentation
-                          (org-babel-trim body))
-                         "[\r\n]") "\n")
+                        (split-string (org-remove-indentation (org-trim body))
+				      "[\r\n]")
+			"\n")
                        (org-babel-process-file-name tmp-file 'noquote))))
                     (org-babel-eval-read-file tmp-file))))))
     (org-babel-result-cond result-params
       raw
-      (org-babel-python-table-or-string (org-babel-trim raw)))))
+      (org-babel-python-table-or-string (org-trim raw)))))
 
 (defun org-babel-python-evaluate-session
     (session body &optional result-type result-params)
@@ -307,7 +307,7 @@ last statement in BODY, as elisp."
           (case result-type
             (output
              (mapconcat
-              #'org-babel-trim
+              #'org-trim
               (butlast
                (org-babel-comint-with-output
                    (session org-babel-python-eoe-indicator t body)

+ 2 - 2
lisp/ob-ref.el

@@ -65,7 +65,7 @@
 (declare-function org-pop-to-buffer-same-window "org-compat"
 		  (&optional buffer-or-name norecord label))
 (declare-function org-show-context "org" (&optional key))
-
+(declare-function org-trim "org" (s &optional keep-lead))
 
 (defvar org-babel-ref-split-regexp
   "[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*")
@@ -242,7 +242,7 @@ to \"0:-1\"."
 
 (defun org-babel-ref-split-args (arg-string)
   "Split ARG-STRING into top-level arguments of balanced parenthesis."
-  (mapcar #'org-babel-trim (org-babel-balanced-split arg-string 44)))
+  (mapcar #'org-trim (org-babel-balanced-split arg-string 44)))
 
 
 (provide 'ob-ref)

+ 2 - 1
lisp/ob-ruby.el

@@ -39,6 +39,7 @@
 (require 'ob)
 (eval-when-compile (require 'cl))
 
+(declare-function org-trim "org" (s &optional keep-lead))
 (declare-function run-ruby "ext:inf-ruby" (&optional command name))
 (declare-function xmp "ext:rcodetools" (&optional option))
 
@@ -220,7 +221,7 @@ return the value of the last statement in BODY, as elisp."
 	  (butlast
 	   (split-string
 	    (mapconcat
-	     #'org-babel-trim
+	     #'org-trim
 	     (org-babel-comint-with-output
 		 (buffer org-babel-ruby-eoe-indicator t body)
 	       (mapc

+ 4 - 3
lisp/ob-shell.el

@@ -36,6 +36,7 @@
 (declare-function org-babel-comint-buffer-livep "ob-comint" (buffer))
 (declare-function org-babel-comint-with-output "ob-comint" (meta &rest body)
 		  t)
+(declare-function org-trim "org" (s &optional keep-lead))
 (declare-function orgtbl-to-generic "org-table" (table params))
 
 (defvar org-babel-default-header-args:shell '())
@@ -213,7 +214,7 @@ return the value of the last statement in BODY."
            (mapconcat
             #'org-babel-sh-strip-weird-long-prompt
             (mapcar
-             #'org-babel-trim
+             #'org-trim
              (butlast
               (org-babel-comint-with-output
                   (session org-babel-sh-eoe-output t body)
@@ -228,7 +229,7 @@ return the value of the last statement in BODY."
                      (accept-process-output
                       (get-buffer-process (current-buffer)))))
                  (append
-                  (split-string (org-babel-trim body) "\n")
+                  (split-string (org-trim body) "\n")
                   (list org-babel-sh-eoe-indicator))))
               2)) "\n"))
           ('otherwise                   ; external shell script
@@ -243,7 +244,7 @@ return the value of the last statement in BODY."
                    (insert body))
                  (set-file-modes script-file #o755)
                  (org-babel-eval script-file ""))
-             (org-babel-eval shell-file-name (org-babel-trim body)))))))
+             (org-babel-eval shell-file-name (org-trim body)))))))
     (when results
       (let ((result-params (cdr (assoc :result-params params))))
         (org-babel-result-cond result-params

+ 3 - 1
lisp/ob-table.el

@@ -55,6 +55,8 @@
 ;;; Code:
 (require 'ob-core)
 
+(declare-function org-trim "org" (s &optional keep-lead))
+
 (defun org-babel-table-truncate-at-newline (string)
   "Replace newline character with ellipses.
 If STRING ends in a newline character, then remove the newline
@@ -142,7 +144,7 @@ as shown in the example below.
                     nil (list "emacs-lisp" "results" params)
                     '((:results . "silent"))))
                "")))
-        (org-babel-trim (if (stringp result) result (format "%S" result)))))))
+        (org-trim (if (stringp result) result (format "%S" result)))))))
 
 (provide 'ob-table)
 

+ 6 - 4
lisp/ob-tangle.el

@@ -41,8 +41,10 @@
 (declare-function org-in-commented-heading-p "org" (&optional no-inheritance))
 (declare-function org-link-escape "org" (text &optional table merge))
 (declare-function org-open-link-from-string "org" (s &optional arg reference-buffer))
+(declare-function org-remove-indentation "org" (code &optional n))
 (declare-function org-store-link "org" (arg))
 (declare-function org-string-nw-p "org-macs" (s))
+(declare-function org-trim "org" (s &optional keep-lead))
 (declare-function outline-previous-heading "outline" ())
 
 (defvar org-link-types-re)
@@ -387,10 +389,10 @@ that the appropriate major-mode is set.  SPEC has the form:
        insert-comment
        (org-fill-template org-babel-tangle-comment-format-beg link-data)))
     (insert
-     (format
-      "%s\n"
-      (org-unescape-code-in-string
-       (org-babel-trim body (if org-src-preserve-indentation "[\f\n\r\v]")))))
+     (org-unescape-code-in-string
+      (if org-src-preserve-indentation (org-trim body t)
+	(org-trim (org-remove-indentation body))))
+     "\n")
     (when link-p
       (funcall
        insert-comment

+ 1 - 2
lisp/org-bibtex.el

@@ -121,7 +121,6 @@
 (declare-function bibtex-generate-autokey "bibtex" ())
 (declare-function bibtex-parse-entry "bibtex" (&optional content))
 (declare-function bibtex-url "bibtex" (&optional pos no-browse))
-(declare-function org-babel-trim "ob-core" (string &optional regexp))
 
 
 ;;; Bibtex data
@@ -313,7 +312,7 @@ and `org-exclude-tags-from-inheritence'."
                (org-entry-get (point) (upcase property))
                (org-entry-get (point) (concat org-bibtex-prefix
                                               (upcase property)))))))
-    (when it (org-babel-trim it))))
+    (when it (org-trim it))))
 
 (defun org-bibtex-put (property value)
   (let ((prop (upcase (if (keywordp property)

+ 1 - 1
lisp/org-footnote.el

@@ -52,7 +52,7 @@
 (declare-function org-inside-latex-macro-p "org" ())
 (declare-function org-mark-ring-push "org" (&optional pos buffer))
 (declare-function org-show-context "org" (&optional key))
-(declare-function org-trim "org" (s))
+(declare-function org-trim "org" (s &optional keep-lead))
 (declare-function outline-next-heading "outline")
 
 (defvar electric-indent-mode)

+ 1 - 1
lisp/org-list.el

@@ -147,7 +147,7 @@
 (declare-function org-time-string-to-seconds "org" (s))
 (declare-function org-timer-hms-to-secs "org-timer" (hms))
 (declare-function org-timer-item "org-timer" (&optional arg))
-(declare-function org-trim "org" (s))
+(declare-function org-trim "org" (s &optional keep-lead))
 (declare-function org-uniquify "org" (list))
 (declare-function outline-flag-region "outline" (from to flag))
 (declare-function outline-invisible-p "outline" (&optional pos))

+ 1 - 1
lisp/org-src.el

@@ -51,7 +51,7 @@
 (declare-function org-pop-to-buffer-same-window "org-compat"
 		  (&optional buffer-or-name norecord label))
 (declare-function org-switch-to-buffer-other-window "org" (&rest args))
-(declare-function org-trim "org" (s))
+(declare-function org-trim "org" (s &optional keep-lead))
 
 (defvar org-element-all-elements)
 (defvar org-inhibit-startup)

+ 5 - 3
lisp/org.el

@@ -178,10 +178,12 @@ Stars are put in group 1 and the trimmed body in group 2.")
   "Get text property PROPERTY at the beginning of line."
   (get-text-property (point-at-bol) property))
 
-(defsubst org-trim (s)
-  "Remove whitespace at the beginning and the end of string S."
+(defsubst org-trim (s &optional keep-lead)
+  "Remove whitespace at the beginning and the end of string S.
+When optional argument KEEP-LEAD is non-nil, removing blank lines
+at the beginning of the string does not affect leading indentation."
   (replace-regexp-in-string
-   "\\`[ \t\n\r]+" ""
+   (if keep-lead "\\`\\([ \t]*\n\\)+" "\\`[ \t\n\r]+") ""
    (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
 
 ;; load languages based on value of `org-babel-load-languages'

+ 1 - 1
testing/lisp/test-ob.el

@@ -207,7 +207,7 @@ should still return the link."
 	   (params (nth 2 info)))
       (message "%S" params)
       (should (equal "example-lang" (nth 0 info)))
-      (should (string= "the body" (org-babel-trim (nth 1 info))))
+      (should (string= "the body" (org-trim (nth 1 info))))
       (should-not (member '(:session\ \ \ \ ) params))
       (should (equal '(:session) (assoc :session params)))
       (should (equal '(:result-type . output) (assoc :result-type params)))