forked from mirrors/org-mode
org-src-preserve-indentation: Refactor handling src block flags
* lisp/org-src.el (org-src-preserve-indentation-p): New function checking whether block should preserve indentation. This function abstracts away the check for block type, indentation flag, and customized `org-src-preserve-indentation' value. (org-src--edit-element): * lisp/ob-core.el (org-babel--normalize-body): (org-babel-read-element): (org-babel-update-block-body): * lisp/ob-exp.el (org-babel-exp-process-buffer): * lisp/org-element.el (org-element-example-block-interpreter): (org-element-src-block-interpreter): * lisp/org.el (org-fixup-indentation): (org-indent-region): * lisp/ox.el (org-export-unravel-code): Use the new function instead of duplicating code. * lisp/ob-haskell.el (org-babel-haskell-export-to-lhs): Add FIXME. We do not have access to the block element here and cannot easily check the flag. * lisp/ob-tangle.el (org-babel-tangle-single-block): * lisp/org-src.el (org-src-font-lock-fontify-block): * lisp/org.el (org-indent-line): Check block flag in addition to `org-src-preserve-indentation'. This commit unifies logic deciding whether to preserve block indentation into a single place to avoid confusion.
This commit is contained in:
parent
c74c4ab18d
commit
1967aa43e5
|
@ -43,7 +43,6 @@
|
||||||
(defvar org-edit-src-content-indentation)
|
(defvar org-edit-src-content-indentation)
|
||||||
(defvar org-link-file-path-type)
|
(defvar org-link-file-path-type)
|
||||||
(defvar org-src-lang-modes)
|
(defvar org-src-lang-modes)
|
||||||
(defvar org-src-preserve-indentation)
|
|
||||||
(defvar org-babel-tangle-uncomment-comments)
|
(defvar org-babel-tangle-uncomment-comments)
|
||||||
|
|
||||||
(declare-function org-attach-dir "org-attach" (&optional create-if-not-exists-p no-fs-check))
|
(declare-function org-attach-dir "org-attach" (&optional create-if-not-exists-p no-fs-check))
|
||||||
|
@ -60,6 +59,7 @@
|
||||||
(declare-function org-cycle "org-cycle" (&optional arg))
|
(declare-function org-cycle "org-cycle" (&optional arg))
|
||||||
(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-src-preserve-indentation-p "org-src" (node))
|
||||||
(declare-function org-element-at-point "org-element" (&optional pom cached-only))
|
(declare-function org-element-at-point "org-element" (&optional pom cached-only))
|
||||||
(declare-function org-element-at-point-no-context "org-element" (&optional pom))
|
(declare-function org-element-at-point-no-context "org-element" (&optional pom))
|
||||||
(declare-function org-element-context "org-element" (&optional element))
|
(declare-function org-element-context "org-element" (&optional element))
|
||||||
|
@ -661,9 +661,7 @@ Remove final newline character and spurious indentation."
|
||||||
;; src-block are not meaningful, since they could come from
|
;; src-block are not meaningful, since they could come from
|
||||||
;; some paragraph filling. Treat them as a white space.
|
;; some paragraph filling. Treat them as a white space.
|
||||||
(replace-regexp-in-string "\n[ \t]*" " " body))
|
(replace-regexp-in-string "\n[ \t]*" " " body))
|
||||||
((or org-src-preserve-indentation
|
((org-src-preserve-indentation-p datum) body)
|
||||||
(org-element-property :preserve-indent datum))
|
|
||||||
body)
|
|
||||||
(t (org-remove-indentation body)))))
|
(t (org-remove-indentation body)))))
|
||||||
|
|
||||||
;;; functions
|
;;; functions
|
||||||
|
@ -2245,9 +2243,7 @@ Return nil if ELEMENT cannot be read."
|
||||||
(`plain-list (org-babel-read-list))
|
(`plain-list (org-babel-read-list))
|
||||||
((or `example-block `src-block)
|
((or `example-block `src-block)
|
||||||
(let ((v (org-element-property :value element)))
|
(let ((v (org-element-property :value element)))
|
||||||
(if (or org-src-preserve-indentation
|
(if (org-src-preserve-indentation-p element) v
|
||||||
(org-element-property :preserve-indent element))
|
|
||||||
v
|
|
||||||
(org-remove-indentation v))))
|
(org-remove-indentation v))))
|
||||||
(`export-block
|
(`export-block
|
||||||
(org-remove-indentation (org-element-property :value element)))
|
(org-remove-indentation (org-element-property :value element)))
|
||||||
|
@ -2820,9 +2816,7 @@ specified as an an \"attachment:\" style link."
|
||||||
(let* ((ind (org-current-text-indentation))
|
(let* ((ind (org-current-text-indentation))
|
||||||
(body-start (line-beginning-position 2))
|
(body-start (line-beginning-position 2))
|
||||||
(body (org-element-normalize-string
|
(body (org-element-normalize-string
|
||||||
(if (or org-src-preserve-indentation
|
(if (org-src-preserve-indentation-p element) new-body
|
||||||
(org-element-property :preserve-indent element))
|
|
||||||
new-body
|
|
||||||
(with-temp-buffer
|
(with-temp-buffer
|
||||||
(insert (org-remove-indentation new-body))
|
(insert (org-remove-indentation new-body))
|
||||||
(indent-rigidly
|
(indent-rigidly
|
||||||
|
|
|
@ -43,8 +43,7 @@
|
||||||
drop-locals))
|
drop-locals))
|
||||||
(declare-function org-in-commented-heading-p "org" (&optional no-inheritance element))
|
(declare-function org-in-commented-heading-p "org" (&optional no-inheritance element))
|
||||||
(declare-function org-in-archived-heading-p "org" (&optional no-inheritance element))
|
(declare-function org-in-archived-heading-p "org" (&optional no-inheritance element))
|
||||||
|
(declare-function org-src-preserve-indentation-p "org-src" (node))
|
||||||
(defvar org-src-preserve-indentation)
|
|
||||||
|
|
||||||
(defcustom org-export-use-babel t
|
(defcustom org-export-use-babel t
|
||||||
"Switch controlling code evaluation and header processing during export.
|
"Switch controlling code evaluation and header processing during export.
|
||||||
|
@ -282,25 +281,23 @@ this template."
|
||||||
(forward-line 0)
|
(forward-line 0)
|
||||||
(delete-region begin (point)))
|
(delete-region begin (point)))
|
||||||
(t
|
(t
|
||||||
(if (or org-src-preserve-indentation
|
(if (org-src-preserve-indentation-p element)
|
||||||
(org-element-property
|
|
||||||
:preserve-indent element))
|
|
||||||
;; Indent only code block
|
;; Indent only code block
|
||||||
;; markers.
|
;; markers.
|
||||||
(with-temp-buffer
|
(with-temp-buffer
|
||||||
;; Do not use tabs for block
|
;; Do not use tabs for block
|
||||||
;; indentation.
|
;; indentation.
|
||||||
(when (fboundp 'indent-tabs-mode)
|
(when (fboundp 'indent-tabs-mode)
|
||||||
(indent-tabs-mode -1)
|
(indent-tabs-mode -1)
|
||||||
;; FIXME: Emacs 26
|
;; FIXME: Emacs 26
|
||||||
;; compatibility.
|
;; compatibility.
|
||||||
(setq-local indent-tabs-mode nil))
|
(setq-local indent-tabs-mode nil))
|
||||||
(insert replacement)
|
(insert replacement)
|
||||||
(skip-chars-backward " \r\t\n")
|
(skip-chars-backward " \r\t\n")
|
||||||
(indent-line-to ind)
|
(indent-line-to ind)
|
||||||
(goto-char 1)
|
(goto-char 1)
|
||||||
(indent-line-to ind)
|
(indent-line-to ind)
|
||||||
(setq replacement (buffer-string)))
|
(setq replacement (buffer-string)))
|
||||||
;; Indent everything.
|
;; Indent everything.
|
||||||
(with-temp-buffer
|
(with-temp-buffer
|
||||||
;; Do not use tabs for block
|
;; Do not use tabs for block
|
||||||
|
|
|
@ -236,6 +236,7 @@ constructs (header arguments, no-web syntax etc...) are ignored."
|
||||||
(command (concat org-babel-haskell-lhs2tex-command
|
(command (concat org-babel-haskell-lhs2tex-command
|
||||||
" " (org-babel-process-file-name lhs-file)
|
" " (org-babel-process-file-name lhs-file)
|
||||||
" > " (org-babel-process-file-name tex-file)))
|
" > " (org-babel-process-file-name tex-file)))
|
||||||
|
;; FIXME: What if src block has :preserve-indentation flag?
|
||||||
(preserve-indentp org-src-preserve-indentation)
|
(preserve-indentp org-src-preserve-indentation)
|
||||||
indentation)
|
indentation)
|
||||||
;; escape haskell source-code blocks
|
;; escape haskell source-code blocks
|
||||||
|
|
|
@ -590,8 +590,7 @@ non-nil, return the full association list to be used by
|
||||||
link
|
link
|
||||||
source-name
|
source-name
|
||||||
params
|
params
|
||||||
(if org-src-preserve-indentation
|
(if (org-src-preserve-indentation-p) (org-trim body t)
|
||||||
(org-trim body t)
|
|
||||||
(org-trim (org-remove-indentation body)))
|
(org-trim (org-remove-indentation body)))
|
||||||
comment)))
|
comment)))
|
||||||
(if only-this-block
|
(if only-this-block
|
||||||
|
|
|
@ -77,6 +77,7 @@
|
||||||
|
|
||||||
(declare-function org-at-heading-p "org" (&optional _))
|
(declare-function org-at-heading-p "org" (&optional _))
|
||||||
(declare-function org-escape-code-in-string "org-src" (s))
|
(declare-function org-escape-code-in-string "org-src" (s))
|
||||||
|
(declare-function org-src-preserve-indentation-p "org-src" (node))
|
||||||
(declare-function org-macro-escape-arguments "org-macro" (&rest args))
|
(declare-function org-macro-escape-arguments "org-macro" (&rest args))
|
||||||
(declare-function org-macro-extract-arguments "org-macro" (s))
|
(declare-function org-macro-extract-arguments "org-macro" (s))
|
||||||
(declare-function org-reduced-level "org" (l))
|
(declare-function org-reduced-level "org" (l))
|
||||||
|
@ -93,7 +94,6 @@
|
||||||
(defvar org-property-drawer-re)
|
(defvar org-property-drawer-re)
|
||||||
(defvar org-property-format)
|
(defvar org-property-format)
|
||||||
(defvar org-property-re)
|
(defvar org-property-re)
|
||||||
(defvar org-src-preserve-indentation)
|
|
||||||
(defvar org-tags-column)
|
(defvar org-tags-column)
|
||||||
(defvar org-todo-regexp)
|
(defvar org-todo-regexp)
|
||||||
(defvar org-ts-regexp-both)
|
(defvar org-ts-regexp-both)
|
||||||
|
@ -2332,9 +2332,7 @@ Return a new syntax node of `example-block' type containing `:begin',
|
||||||
(value
|
(value
|
||||||
(let ((val (org-element-property :value example-block)))
|
(let ((val (org-element-property :value example-block)))
|
||||||
(cond
|
(cond
|
||||||
((or org-src-preserve-indentation
|
((org-src-preserve-indentation-p example-block) val)
|
||||||
(org-element-property :preserve-indent example-block))
|
|
||||||
val)
|
|
||||||
((= 0 org-edit-src-content-indentation)
|
((= 0 org-edit-src-content-indentation)
|
||||||
(org-remove-indentation val))
|
(org-remove-indentation val))
|
||||||
(t
|
(t
|
||||||
|
@ -2867,9 +2865,7 @@ Assume point is at the beginning of the block."
|
||||||
(value
|
(value
|
||||||
(let ((val (org-element-property :value src-block)))
|
(let ((val (org-element-property :value src-block)))
|
||||||
(cond
|
(cond
|
||||||
((or org-src-preserve-indentation
|
((org-src-preserve-indentation-p src-block) val)
|
||||||
(org-element-property :preserve-indent src-block))
|
|
||||||
val)
|
|
||||||
((zerop org-edit-src-content-indentation)
|
((zerop org-edit-src-content-indentation)
|
||||||
(org-remove-indentation val))
|
(org-remove-indentation val))
|
||||||
(t
|
(t
|
||||||
|
|
|
@ -454,12 +454,20 @@ DATUM is an element or an object. Consider blank lines or white
|
||||||
spaces after it as being outside."
|
spaces after it as being outside."
|
||||||
(and (>= (point) (org-element-begin datum))
|
(and (>= (point) (org-element-begin datum))
|
||||||
(<= (point)
|
(<= (point)
|
||||||
(org-with-wide-buffer
|
(org-with-wide-buffer
|
||||||
(goto-char (org-element-end datum))
|
(goto-char (org-element-end datum))
|
||||||
(skip-chars-backward " \r\t\n")
|
(skip-chars-backward " \r\t\n")
|
||||||
(if (eq (org-element-class datum) 'element)
|
(if (eq (org-element-class datum) 'element)
|
||||||
(line-end-position)
|
(line-end-position)
|
||||||
(point))))))
|
(point))))))
|
||||||
|
|
||||||
|
(defun org-src-preserve-indentation-p (&optional node)
|
||||||
|
"Non-nil when indentation should be preserved within NODE.
|
||||||
|
When NODE is not passed, assume element at point."
|
||||||
|
(let ((node (or node (org-element-at-point))))
|
||||||
|
(and (org-element-type-p node '(example-block src-block))
|
||||||
|
(or (org-element-property :preserve-indent node)
|
||||||
|
org-src-preserve-indentation))))
|
||||||
|
|
||||||
(defun org-src--contents-for-write-back (write-back-buf)
|
(defun org-src--contents-for-write-back (write-back-buf)
|
||||||
"Populate WRITE-BACK-BUF with contents in the appropriate format.
|
"Populate WRITE-BACK-BUF with contents in the appropriate format.
|
||||||
|
@ -558,10 +566,7 @@ Leave point in edit buffer."
|
||||||
(org-element-parent datum) nil))
|
(org-element-parent datum) nil))
|
||||||
(t (org-current-text-indentation)))))
|
(t (org-current-text-indentation)))))
|
||||||
(content-ind org-edit-src-content-indentation)
|
(content-ind org-edit-src-content-indentation)
|
||||||
(preserve-ind
|
(preserve-ind (org-src-preserve-indentation-p datum))
|
||||||
(and (memq type '(example-block src-block))
|
|
||||||
(or (org-element-property :preserve-indent datum)
|
|
||||||
org-src-preserve-indentation)))
|
|
||||||
;; Store relative positions of mark (if any) and point
|
;; Store relative positions of mark (if any) and point
|
||||||
;; within the edited area.
|
;; within the edited area.
|
||||||
(point-coordinates (and (not remote)
|
(point-coordinates (and (not remote)
|
||||||
|
@ -716,7 +721,7 @@ as `org-src-fontify-natively' is non-nil."
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(goto-char start)
|
(goto-char start)
|
||||||
(let ((indent-offset
|
(let ((indent-offset
|
||||||
(if org-src-preserve-indentation 0
|
(if (org-src-preserve-indentation-p) 0
|
||||||
(+ (progn (backward-char)
|
(+ (progn (backward-char)
|
||||||
(org-current-text-indentation))
|
(org-current-text-indentation))
|
||||||
org-edit-src-content-indentation))))
|
org-edit-src-content-indentation))))
|
||||||
|
|
24
lisp/org.el
24
lisp/org.el
|
@ -6886,14 +6886,11 @@ Assume point is at a heading or an inlinetask beginning."
|
||||||
(forward-line 0)
|
(forward-line 0)
|
||||||
(or (and (looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
|
(or (and (looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
|
||||||
(let ((e (org-element-at-point)))
|
(let ((e (org-element-at-point)))
|
||||||
(and (org-element-type-p
|
(and (org-src-preserve-indentation-p e)
|
||||||
e '(example-block src-block))
|
(goto-char (org-element-end e))
|
||||||
(or org-src-preserve-indentation
|
(progn (skip-chars-backward " \r\t\n")
|
||||||
(org-element-property :preserve-indent e))
|
(forward-line 0)
|
||||||
(goto-char (org-element-end e))
|
t))))
|
||||||
(progn (skip-chars-backward " \r\t\n")
|
|
||||||
(forward-line 0)
|
|
||||||
t))))
|
|
||||||
(forward-line))))))))
|
(forward-line))))))))
|
||||||
;; Shift lines but footnote definitions, inlinetasks boundaries
|
;; Shift lines but footnote definitions, inlinetasks boundaries
|
||||||
;; by DIFF. Also skip contents of source or example blocks
|
;; by DIFF. Also skip contents of source or example blocks
|
||||||
|
@ -6911,10 +6908,7 @@ Assume point is at a heading or an inlinetask beginning."
|
||||||
(forward-line 0)
|
(forward-line 0)
|
||||||
(or (and (looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
|
(or (and (looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
|
||||||
(let ((e (org-element-at-point)))
|
(let ((e (org-element-at-point)))
|
||||||
(and (org-element-type-p
|
(and (org-src-preserve-indentation-p e)
|
||||||
e '(example-block src-block))
|
|
||||||
(or org-src-preserve-indentation
|
|
||||||
(org-element-property :preserve-indent e))
|
|
||||||
(goto-char (org-element-end e))
|
(goto-char (org-element-end e))
|
||||||
(progn (skip-chars-backward " \r\t\n")
|
(progn (skip-chars-backward " \r\t\n")
|
||||||
(forward-line 0)
|
(forward-line 0)
|
||||||
|
@ -19154,7 +19148,7 @@ Also align node properties according to `org-property-format'."
|
||||||
(skip-chars-backward " \t\n")
|
(skip-chars-backward " \t\n")
|
||||||
(line-beginning-position))))
|
(line-beginning-position))))
|
||||||
(let ((block-content-ind
|
(let ((block-content-ind
|
||||||
(when (not org-src-preserve-indentation)
|
(when (not (org-src-preserve-indentation-p element))
|
||||||
(org-with-point-at (org-element-property :begin element)
|
(org-with-point-at (org-element-property :begin element)
|
||||||
(+ (org-current-text-indentation)
|
(+ (org-current-text-indentation)
|
||||||
org-edit-src-content-indentation)))))
|
org-edit-src-content-indentation)))))
|
||||||
|
@ -19208,9 +19202,7 @@ assumed to be significant there."
|
||||||
;; boundaries can.
|
;; boundaries can.
|
||||||
((or (memq type '(export-block latex-environment))
|
((or (memq type '(export-block latex-environment))
|
||||||
(and (eq type 'example-block)
|
(and (eq type 'example-block)
|
||||||
(not
|
(not (org-src-preserve-indentation-p element))))
|
||||||
(or org-src-preserve-indentation
|
|
||||||
(org-element-property :preserve-indent element)))))
|
|
||||||
(let ((offset (- ind (current-indentation))))
|
(let ((offset (- ind (current-indentation))))
|
||||||
(unless (zerop offset)
|
(unless (zerop offset)
|
||||||
(indent-rigidly (org-element-begin element)
|
(indent-rigidly (org-element-begin element)
|
||||||
|
|
|
@ -4989,9 +4989,7 @@ reference on that line (string)."
|
||||||
;; to the code proper.
|
;; to the code proper.
|
||||||
(code (replace-regexp-in-string
|
(code (replace-regexp-in-string
|
||||||
"\n\\'" ""
|
"\n\\'" ""
|
||||||
(if (or org-src-preserve-indentation
|
(if (org-src-preserve-indentation-p element) value
|
||||||
(org-element-property :preserve-indent element))
|
|
||||||
value
|
|
||||||
(org-remove-indentation value))))
|
(org-remove-indentation value))))
|
||||||
;; Build a regexp matching a loc with a reference.
|
;; Build a regexp matching a loc with a reference.
|
||||||
(ref-re (org-src-coderef-regexp (org-src-coderef-format element))))
|
(ref-re (org-src-coderef-regexp (org-src-coderef-format element))))
|
||||||
|
|
Loading…
Reference in New Issue