>From 7c08eed000387e2e52d21403970e177ae114db20 Mon Sep 17 00:00:00 2001
From: Rasmus
Date: Thu, 21 Dec 2017 12:59:36 +0100
Subject: [PATCH 3/4] org-tempo: Various improvements
* lisp/org-tempo.el (org-tempo-keywords-alist): Improve docstring.
(org-tempo--update-maybe):
(org-tempo--keys):
(org-tempo-complete-tag): New functions.
(org-tempo-setup):
(org-tempo-add-templates): Use new functions.
(org-tempo-add-block): Smarter position of point.
* testing/lisp/test-org-tempo.el (test-org-tempo/cursor-placement):
(test-org-tempo/space-first-line): New tests.
---
lisp/org-tempo.el | 34 ++++++++++++++++++++++++----------
testing/lisp/test-org-tempo.el | 39 +++++++++++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+), 10 deletions(-)
diff --git a/lisp/org-tempo.el b/lisp/org-tempo.el
index 92a97c752..76e781adc 100644
--- a/lisp/org-tempo.el
+++ b/lisp/org-tempo.el
@@ -62,7 +62,9 @@ and KEYWORD. The tempo snippet \" (length keys) (length (delete-dups keys)))
(warn
"Duplicated keys in `org-structure-template-alist' and `org-tempo-keywords-alist'"))
-
;; Remove any keys already defined in case they have been updated.
(mapc (lambda (key)
(if (assoc-string key org-tempo-tags)
@@ -104,9 +116,11 @@ Goes through `org-structure-template-alist' and
(defun org-tempo-add-block (entry)
"Add block entry from `org-structure-template-alist'."
(let* ((key (format "<%s" (car entry)))
- (name (cdr entry)))
+ (name (cdr entry))
+ (special (member name '("src" "export"))))
(tempo-define-template (format "org-%s" (replace-regexp-in-string " " "-" name))
- `(,(format "#+begin_%s " name) p '> n n
+ `(,(format "#+begin_%s%s" name (if special " " ""))
+ ,(when special 'p) '> n '> ,(unless special 'p) n
,(format "#+end_%s" (car (split-string name " ")))
>)
key
@@ -155,7 +169,7 @@ Goes through `org-structure-template-alist' and
(add-hook 'org-mode-hook 'org-tempo-setup)
(add-hook 'org-tab-before-tab-emulation-hook
- 'tempo-complete-tag)
+ 'org-tempo-complete-tag)
;; Enable Org Tempo in all open Org buffers.
(dolist (b (org-buffer-list))
diff --git a/testing/lisp/test-org-tempo.el b/testing/lisp/test-org-tempo.el
index 060a7da88..d48cf6ca2 100644
--- a/testing/lisp/test-org-tempo.el
+++ b/testing/lisp/test-org-tempo.el
@@ -59,6 +59,45 @@
(buffer-string))
""
+ (org-tempo-setup)
+ (tempo-complete-tag)
+ (goto-char (point-min))
+ (end-of-line)
+ (skip-chars-backward " "))))
+ ;; src blocks, export blocks and keywords should have one space at
+ ;; the end of the first line.
+ (should (cl-every (apply-partially 'eq 1)
+ (mapcar (lambda (s)
+ (org-test-with-temp-text (format "<%s" s)
+ (org-tempo-setup)
+ (tempo-complete-tag)
+ (goto-char (point-min))
+ (end-of-line)
+ (abs (skip-chars-backward " "))))
+ '("s" "E" "L")))))
+
+(ert-deftest test-org-tempo/cursor-placement ()
+ "Test the placement of the cursor after tempo expand"
+ ;; Normal blocks place point "inside" block.
+ (should
+ (eq (org-test-with-temp-text ""
+ (org-tempo-setup)
+ (tempo-complete-tag)
+ (point))
+ (length "#\\+begin_export latex\n")))
+ ;; Special block stop at end of #+begin line.
+ (should
+ (eq (org-test-with-temp-text ""
+ (org-tempo-setup)
+ (tempo-complete-tag)
+ (point))
+ (length "#\\+begin_src "))))
+
(ert-deftest test-org-tempo/add-new-templates ()
"Test that new structures and keywords are added correctly."
;; Check that deleted keys are not kept
--
2.15.1