Browse Source

ob: Remove indentation information from Babel properties

* lisp/ob-core.el (org-babel-get-src-block-info): Do not return block
  indentation.
(org-babel-parse-src-block-match): Ignore block indentation.
(org-babel-demarcate-block): Find appropriate indentation instead of
using the one from the original block.
(org-babel-where-is-src-block-result): Change signature.  Indent
according to context instead of relying on indentation from original
block.
(org-babel-insert-result): Change signature.
(org-babel-check-confirm-evaluate):
(org-babel-execute-src-block):
(org-babel-insert-header-arg): Apply change to src-block info.

* lisp/ob-exp.el (org-babel-exp-process-buffer): Apply change to
  src-block info.

* lisp/ob-lob.el (org-babel-lob-get-info): Do not return indentation.
(org-babel-lob-execute): Apply change to lob-info.

Indentation from source block is often wrong at the block and its
results may not be in the same context.
Nicolas Goaziou 9 năm trước cách đây
mục cha
commit
8865920879
3 tập tin đã thay đổi với 51 bổ sung48 xóa
  1. 34 25
      lisp/ob-core.el
  2. 3 3
      lisp/ob-exp.el
  3. 14 20
      lisp/ob-lob.el

+ 34 - 25
lisp/ob-core.el

@@ -53,6 +53,7 @@
 (declare-function with-parsed-tramp-file-name "tramp" (filename var &rest body))
 (declare-function with-parsed-tramp-file-name "tramp" (filename var &rest body))
 (declare-function org-edit-src-code "org-src" (&optional code edit-buffer-name))
 (declare-function org-edit-src-code "org-src" (&optional code edit-buffer-name))
 (declare-function org-edit-src-exit "org-src"  ())
 (declare-function org-edit-src-exit "org-src"  ())
+(declare-function org-get-indentation "org" (&optional line))
 (declare-function org-in-regexp "org" (regexp &optional nlines visually))
 (declare-function org-in-regexp "org" (regexp &optional nlines visually))
 (declare-function org-open-at-point "org" (&optional in-emacs reference-buffer))
 (declare-function org-open-at-point "org" (&optional in-emacs reference-buffer))
 (declare-function org-save-outline-visibility "org-macs" (use-markers &rest body))
 (declare-function org-save-outline-visibility "org-macs" (use-markers &rest body))
@@ -62,6 +63,7 @@
 (declare-function org-split-string "org" (string &optional separators))
 (declare-function org-split-string "org" (string &optional separators))
 (declare-function org-entry-get "org"
 (declare-function org-entry-get "org"
 		  (pom property &optional inherit literal-nil))
 		  (pom property &optional inherit literal-nil))
+(declare-function org-indent-line "org" ())
 (declare-function org-make-options-regexp "org" (kwds &optional extra))
 (declare-function org-make-options-regexp "org" (kwds &optional extra))
 (declare-function org-do-remove-indentation "org" (&optional n))
 (declare-function org-do-remove-indentation "org" (&optional n))
 (declare-function org-next-block "org" (arg &optional backward block-regexp))
 (declare-function org-next-block "org" (arg &optional backward block-regexp))
@@ -252,15 +254,13 @@ references; a process which could likely result in the execution
 of other code blocks.
 of other code blocks.
 
 
 Returns a list
 Returns a list
- (language body header-arguments-alist switches name indent block-head)."
+ (language body header-arguments-alist switches name block-head)."
   (let ((case-fold-search t) head info name indent)
   (let ((case-fold-search t) head info name indent)
     ;; full code block
     ;; full code block
     (if (setq head (org-babel-where-is-src-block-head))
     (if (setq head (org-babel-where-is-src-block-head))
 	(save-excursion
 	(save-excursion
 	  (goto-char head)
 	  (goto-char head)
 	  (setq info (org-babel-parse-src-block-match))
 	  (setq info (org-babel-parse-src-block-match))
-	  (setq indent (car (last info)))
-	  (setq info (butlast info))
 	  (while (and (= 0 (forward-line -1))
 	  (while (and (= 0 (forward-line -1))
 		      (looking-at org-babel-multi-line-header-regexp))
 		      (looking-at org-babel-multi-line-header-regexp))
 	    (setf (nth 2 info)
 	    (setf (nth 2 info)
@@ -278,7 +278,7 @@ Returns a list
       (setf (nth 2 info) (org-babel-process-params (nth 2 info))))
       (setf (nth 2 info) (org-babel-process-params (nth 2 info))))
     (when info
     (when info
       (setf (nth 2 info) (org-babel-generate-file-param name (nth 2 info))))
       (setf (nth 2 info) (org-babel-generate-file-param name (nth 2 info))))
-    (when info (append info (list name indent head)))))
+    (when info (append info (list name head)))))
 
 
 (defvar org-babel-exp-reference-buffer nil
 (defvar org-babel-exp-reference-buffer nil
   "Buffer containing original contents of the exported buffer.
   "Buffer containing original contents of the exported buffer.
@@ -302,7 +302,7 @@ should be asked whether to allow evaluation."
 		    (and export (equal eval "query-export"))
 		    (and export (equal eval "query-export"))
 		    (if (functionp org-confirm-babel-evaluate)
 		    (if (functionp org-confirm-babel-evaluate)
 			(save-excursion
 			(save-excursion
-			  (goto-char (nth 6 info))
+			  (goto-char (nth 5 info))
 			  (funcall org-confirm-babel-evaluate
 			  (funcall org-confirm-babel-evaluate
 				   ;; language, code block body
 				   ;; language, code block body
 				   (nth 0 info) (nth 1 info)))
 				   (nth 0 info) (nth 1 info)))
@@ -640,7 +640,7 @@ block."
   (interactive)
   (interactive)
   (let* ((org-babel-current-src-block-location
   (let* ((org-babel-current-src-block-location
 	  (or org-babel-current-src-block-location
 	  (or org-babel-current-src-block-location
-	      (nth 6 info)
+	      (nth 5 info)
 	      (org-babel-where-is-src-block-head)
 	      (org-babel-where-is-src-block-head)
 	      ;; inline src block
 	      ;; inline src block
 	      (and (org-babel-get-inline-src-block-matches)
 	      (and (org-babel-get-inline-src-block-matches)
@@ -683,7 +683,6 @@ block."
 		  (or (org-bound-and-true-p
 		  (or (org-bound-and-true-p
 		       org-babel-call-process-region-original)
 		       org-babel-call-process-region-original)
 		      (symbol-function 'call-process-region)))
 		      (symbol-function 'call-process-region)))
-		 (indent (nth 5 info))
 		 result cmd)
 		 result cmd)
 	    (unwind-protect
 	    (unwind-protect
 		(let ((call-process-region
 		(let ((call-process-region
@@ -740,7 +739,7 @@ block."
 			  (setq result-params
 			  (setq result-params
 				(remove "file" result-params)))))
 				(remove "file" result-params)))))
 		    (org-babel-insert-result
 		    (org-babel-insert-result
-		     result result-params info new-hash indent lang))
+		     result result-params info new-hash lang))
                   (run-hooks 'org-babel-after-execute-hook)
                   (run-hooks 'org-babel-after-execute-hook)
 		  result)
 		  result)
 	      (setq call-process-region
 	      (setq call-process-region
@@ -850,7 +849,7 @@ arguments and pop open the results in a preview buffer."
   (interactive)
   (interactive)
   (let* ((info (org-babel-get-src-block-info 'light))
   (let* ((info (org-babel-get-src-block-info 'light))
 	 (lang (car info))
 	 (lang (car info))
-	 (begin (nth 6 info))
+	 (begin (nth 5 info))
 	 (lang-headers (intern (concat "org-babel-header-args:" lang)))
 	 (lang-headers (intern (concat "org-babel-header-args:" lang)))
 	 (headers (org-babel-combine-header-arg-lists
 	 (headers (org-babel-combine-header-arg-lists
 		   org-babel-common-header-args-w-values
 		   org-babel-common-header-args-w-values
@@ -1453,8 +1452,7 @@ specified in the properties of the current outline entry."
 (defvar org-src-preserve-indentation) ;; declare defcustom from org-src
 (defvar org-src-preserve-indentation) ;; declare defcustom from org-src
 (defun org-babel-parse-src-block-match ()
 (defun org-babel-parse-src-block-match ()
   "Parse the results from a match of the `org-babel-src-block-regexp'."
   "Parse the results from a match of the `org-babel-src-block-regexp'."
-  (let* ((block-indentation (string-width (match-string 1)))
-	 (lang (org-match-string-no-properties 2))
+  (let* ((lang (org-match-string-no-properties 2))
          (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
          (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
 	 (switches (match-string 3))
 	 (switches (match-string 3))
          (body (let* ((body (org-match-string-no-properties 5))
          (body (let* ((body (org-match-string-no-properties 5))
@@ -1480,8 +1478,7 @@ specified in the properties of the current outline entry."
 		  (org-babel-params-from-properties lang)
 		  (org-babel-params-from-properties lang)
 		  (list (org-babel-parse-header-arguments
 		  (list (org-babel-parse-header-arguments
 			 (org-no-properties (or (match-string 4) ""))))))
 			 (org-no-properties (or (match-string 4) ""))))))
-	  switches
-	  block-indentation)))
+	  switches)))
 
 
 (defun org-babel-parse-inline-src-block-match ()
 (defun org-babel-parse-inline-src-block-match ()
   "Parse the results from a match of the `org-babel-inline-src-block-regexp'."
   "Parse the results from a match of the `org-babel-inline-src-block-regexp'."
@@ -1877,7 +1874,7 @@ region is not active then the point is demarcated."
            (save-excursion
            (save-excursion
              (goto-char place)
              (goto-char place)
              (let ((lang (nth 0 info))
              (let ((lang (nth 0 info))
-                   (indent (make-string (nth 5 info) ? )))
+                   (indent (make-string (org-get-indentation) ?\s)))
 	       (when (string-match "^[[:space:]]*$"
 	       (when (string-match "^[[:space:]]*$"
 				   (buffer-substring (point-at-bol)
 				   (buffer-substring (point-at-bol)
 						     (point-at-eol)))
 						     (point-at-eol)))
@@ -1916,7 +1913,7 @@ region is not active then the point is demarcated."
 	(goto-char start) (move-end-of-line 1)))))
 	(goto-char start) (move-end-of-line 1)))))
 
 
 (defvar org-babel-lob-one-liner-regexp)
 (defvar org-babel-lob-one-liner-regexp)
-(defun org-babel-where-is-src-block-result (&optional insert info hash indent)
+(defun org-babel-where-is-src-block-result (&optional insert info hash)
   "Find where the current source block results begin.
   "Find where the current source block results begin.
 Return the point at the beginning of the result of the current
 Return the point at the beginning of the result of the current
 source block.  Specifically at the beginning of the results line.
 source block.  Specifically at the beginning of the results line.
@@ -1931,7 +1928,7 @@ following the source block."
 		      (match-end 0)))
 		      (match-end 0)))
 	   (name (nth 4 (or info (org-babel-get-src-block-info 'light))))
 	   (name (nth 4 (or info (org-babel-get-src-block-info 'light))))
 	   (head (unless on-lob-line (org-babel-where-is-src-block-head)))
 	   (head (unless on-lob-line (org-babel-where-is-src-block-head)))
-	   found beg end)
+	   found beg end ind)
       (when head (goto-char head))
       (when head (goto-char head))
       (org-with-wide-buffer
       (org-with-wide-buffer
        (setq
        (setq
@@ -1945,10 +1942,18 @@ following the source block."
 	  ;; - if it does need to be rebuilt then do set end
 	  ;; - if it does need to be rebuilt then do set end
 	  name (setq beg (org-babel-find-named-result name))
 	  name (setq beg (org-babel-find-named-result name))
 	  (prog1 beg
 	  (prog1 beg
-	    (when (and hash (not (string= hash (match-string 5))))
-	      (goto-char beg) (setq end beg) ;; beginning of result
-	      (forward-line 1)
-	      (delete-region end (org-babel-result-end)) nil)))
+	    (goto-char beg)
+	    (setq ind (org-get-indentation))
+	    (when hash
+	      (looking-at org-babel-result-regexp)
+	      (unless (string= (match-string 5) hash)
+		(setq end beg)
+		(let ((element (org-element-at-point)))
+		  (delete-region
+		   (org-element-property :begin element)
+		   (progn (goto-char (org-element-property :end element))
+			  (skip-chars-backward " \t\n")
+			  (line-beginning-position 2))))))))
 	 (and
 	 (and
 	  ;; unnamed results:
 	  ;; unnamed results:
 	  ;; - return t if it is found, else return nil
 	  ;; - return t if it is found, else return nil
@@ -1965,6 +1970,7 @@ following the source block."
 			(beginning-of-line 1)
 			(beginning-of-line 1)
 			(cond
 			(cond
 			 ((looking-at (concat org-babel-result-regexp "\n"))
 			 ((looking-at (concat org-babel-result-regexp "\n"))
+			  (setq ind (org-get-indentation))
 			  (throw 'non-comment t))
 			  (throw 'non-comment t))
 			 ((and (looking-at "^[ \t]*#")
 			 ((and (looking-at "^[ \t]*#")
 			       (not (looking-at
 			       (not (looking-at
@@ -1985,7 +1991,11 @@ following the source block."
 	(goto-char end)
 	(goto-char end)
 	(unless beg
 	(unless beg
 	  (if (looking-at "[\n\r]") (forward-char 1) (insert "\n")))
 	  (if (looking-at "[\n\r]") (forward-char 1) (insert "\n")))
-	(when (wholenump indent) (indent-to indent))
+	(if ind (indent-to ind)
+	  ;; Open line to properly indent.
+	  (save-excursion (insert "\n"))
+	  (org-indent-line)
+	  (delete-char 1))
 	(insert (concat
 	(insert (concat
 		 "#+" org-babel-results-keyword
 		 "#+" org-babel-results-keyword
 		 (when hash
 		 (when hash
@@ -2097,8 +2107,7 @@ If the path of the link is a file path it is expanded using
       ;; scalar result
       ;; scalar result
       (funcall echo-res result))))
       (funcall echo-res result))))
 
 
-(defun org-babel-insert-result
-    (result &optional result-params info hash indent lang)
+(defun org-babel-insert-result (result &optional result-params info hash lang)
   "Insert RESULT into the current buffer.
   "Insert RESULT into the current buffer.
 
 
 By default RESULT is inserted after the end of the current source
 By default RESULT is inserted after the end of the current source
@@ -2197,7 +2206,7 @@ INFO may provide the values of these header arguments (in the
 		  (point))))
 		  (point))))
 	     (existing-result
 	     (existing-result
 	      (unless inlinep
 	      (unless inlinep
-		(org-babel-where-is-src-block-result t info hash indent)))
+		(org-babel-where-is-src-block-result t info hash)))
 	     (bad-inline-p
 	     (bad-inline-p
 	      (when inlinep
 	      (when inlinep
 		(or
 		(or
@@ -2215,7 +2224,7 @@ INFO may provide the values of these header arguments (in the
 	     (outside-scope-p (and existing-result
 	     (outside-scope-p (and existing-result
 				   (or (> visible-beg existing-result)
 				   (or (> visible-beg existing-result)
 				       (<= visible-end existing-result))))
 				       (<= visible-end existing-result))))
-	     beg end)
+	     beg end indent)
 	(when (and (stringp result)  ; ensure results end in a newline
 	(when (and (stringp result)  ; ensure results end in a newline
 		   (not inlinep)
 		   (not inlinep)
 		   (> (length result) 0)
 		   (> (length result) 0)

+ 3 - 3
lisp/ob-exp.el

@@ -228,10 +228,10 @@ may make them unreachable."
 					  (org-no-properties
 					  (org-no-properties
 					   (concat
 					   (concat
 					    ":var results="
 					    ":var results="
-					    (mapconcat 'identity
-						       (butlast lob-info 2)
+					    (mapconcat #'identity
+						       (butlast lob-info)
 						       " ")))))))
 						       " ")))))))
-				"" (nth 3 lob-info) (nth 2 lob-info))
+				"" (nth 2 lob-info))
 			  'lob))
 			  'lob))
 			(rep (org-fill-template
 			(rep (org-fill-template
 			      org-babel-exp-call-line-template
 			      org-babel-exp-call-line-template

+ 14 - 20
lisp/ob-lob.el

@@ -108,32 +108,26 @@ from `inline-babel-call' or `babel-call' DATUM, when provided."
 		      (if in (format "[%s]" in) ""))
 		      (if in (format "[%s]" in) ""))
 		    (or (org-element-property :arguments context) ""))
 		    (or (org-element-property :arguments context) ""))
 	    (org-element-property :end-header context)
 	    (org-element-property :end-header context)
-	    (string-width		;Indentation.
-	     (org-with-wide-buffer
-	      (goto-char (org-element-property :begin context))
-	      (buffer-substring-no-properties
-	       (line-beginning-position) (point))))
 	    (org-element-property :name context)))))
 	    (org-element-property :name context)))))
 
 
 (defvar org-babel-default-header-args:emacs-lisp) ; Defined in ob-emacs-lisp.el
 (defvar org-babel-default-header-args:emacs-lisp) ; Defined in ob-emacs-lisp.el
 (defun org-babel-lob-execute (info)
 (defun org-babel-lob-execute (info)
   "Execute the lob call specified by INFO."
   "Execute the lob call specified by INFO."
   (let* ((mkinfo (lambda (p)
   (let* ((mkinfo (lambda (p)
-		   (list "emacs-lisp" "results" p nil
-			 (nth 3 info) ;; name
-			 (nth 2 info))))
-	 (pre-params (apply #'org-babel-merge-params
-			    org-babel-default-header-args
-			    org-babel-default-header-args:emacs-lisp
-			    (append
-			     (org-babel-params-from-properties)
-			     (list
-			      (org-babel-parse-header-arguments
-			       (org-no-properties
-				(concat
-				 ":var results="
-				 (mapconcat #'identity (butlast info 2)
-					    " "))))))))
+		   ;; Make plist P compatible with
+		   ;; `org-babel-get-src-block-info'.
+		   (list "emacs-lisp" "results" p nil (nth 2 info))))
+	 (pre-params
+	  (apply #'org-babel-merge-params
+		 org-babel-default-header-args
+		 org-babel-default-header-args:emacs-lisp
+		 (append
+		  (org-babel-params-from-properties)
+		  (list
+		   (org-babel-parse-header-arguments
+		    (org-no-properties
+		     (concat ":var results="
+			     (mapconcat #'identity (butlast info) " "))))))))
 	 (pre-info (funcall mkinfo pre-params))
 	 (pre-info (funcall mkinfo pre-params))
 	 (cache-p (and (cdr (assoc :cache pre-params))
 	 (cache-p (and (cdr (assoc :cache pre-params))
 		       (string= "yes" (cdr (assoc :cache pre-params)))))
 		       (string= "yes" (cdr (assoc :cache pre-params)))))