summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2018-06-12 09:03:33 +0200
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2018-06-12 09:03:33 +0200
commit3745c0af9f7bbd8a3285f8df4a5c933d71a7740e (patch)
tree89d44cca7c818e8e2951a8ce7756b64692025a80
parenta0dacdc495c5e50a00dafe67550e34db7fe6fffb (diff)
downloadorg-mode-3745c0af9f7bbd8a3285f8df4a5c933d71a7740e.tar.gz
Fix indentation when wrapping a block around a region
* lisp/org.el (org-insert-structure-template): Indent like the first line, not the line the point happens to be on. Reported-by: stardiviner <numbchild@gmail.com> <http://lists.gnu.org/r/emacs-orgmode/2018-06/msg00107.html>
-rw-r--r--lisp/org.el52
-rw-r--r--testing/lisp/test-org.el6
2 files changed, 32 insertions, 26 deletions
diff --git a/lisp/org.el b/lisp/org.el
index d2b4c26..ee8a171 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -11849,36 +11849,36 @@ block."
(list (pcase (org--insert-structure-template-mks)
(`("\t" . ,_) (read-string "Structure type: "))
(`(,_ ,choice . ,_) choice))))
- (let* ((column (current-indentation))
- (region? (use-region-p))
+ (let* ((region? (use-region-p))
(region-start (and region? (region-beginning)))
(region-end (and region? (copy-marker (region-end))))
- (special? (string-match-p "\\`\\(src\\|export\\)\\'" type)))
+ (extended? (string-match-p "\\`\\(src\\|export\\)\\'" type))
+ (verbatim? (string-match-p
+ (concat "\\`" (regexp-opt '("example" "export" "src")))
+ type)))
(when region? (goto-char region-start))
- (if (save-excursion (skip-chars-backward " \t") (bolp))
- (beginning-of-line)
- (insert "\n"))
- (save-excursion
- (indent-to column)
- (insert (format "#+begin_%s%s\n" type (if special? " " "")))
- (when region?
- (when (string-match-p (concat "\\`"
- (regexp-opt '("example" "export" "src")))
- type)
- (org-escape-code-in-region (point) region-end))
- (goto-char region-end)
- ;; Ignore empty lines at the end of the region.
- (skip-chars-backward " \r\t\n")
- (end-of-line))
- (unless (bolp) (insert "\n"))
- (indent-to column)
- (insert (format "#+end_%s" (car (split-string type))))
- (if (looking-at "[ \t]*$") (replace-match "")
+ (let ((column (current-indentation)))
+ (if (save-excursion (skip-chars-backward " \t") (bolp))
+ (beginning-of-line)
(insert "\n"))
- (when (and (eobp) (not (bolp))) (insert "\n")))
- (if special? (end-of-line)
- (forward-line)
- (skip-chars-forward " \t"))))
+ (save-excursion
+ (indent-to column)
+ (insert (format "#+begin_%s%s\n" type (if extended? " " "")))
+ (when region?
+ (when verbatim? (org-escape-code-in-region (point) region-end))
+ (goto-char region-end)
+ ;; Ignore empty lines at the end of the region.
+ (skip-chars-backward " \r\t\n")
+ (end-of-line))
+ (unless (bolp) (insert "\n"))
+ (indent-to column)
+ (insert (format "#+end_%s" (car (split-string type))))
+ (if (looking-at "[ \t]*$") (replace-match "")
+ (insert "\n"))
+ (when (and (eobp) (not (bolp))) (insert "\n")))
+ (if extended? (end-of-line)
+ (forward-line)
+ (skip-chars-forward " \t")))))
;;;; TODO, DEADLINE, Comments
diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index ed9da2f..9390694 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -4123,6 +4123,12 @@ Text.
(org-mark-element)
(org-insert-structure-template "foo")
(buffer-string))))
+ (should
+ (string= " #+begin_foo\n Line 1\n Line2\n #+end_foo\n"
+ (org-test-with-temp-text " Line 1\n Line2"
+ (org-mark-element)
+ (org-insert-structure-template "foo")
+ (buffer-string))))
;; Test point location.
(should
(string= "#+begin_foo\n"