Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Org-mode: trouble auto-indenting snippets that insert headings and drawers #362

novoid opened this Issue Feb 15, 2013 · 20 comments


None yet
4 participants

novoid commented Feb 15, 2013


An Org-heading (starting with an asterisk) followed by a line
with something like ":word: $1-test" does not work with yasnippet and

No change in behavior in following cases:

  • empty line after heading
  • empty line and in-between text after heading
  • replacing ":ID:" with ":test:"
  • replacing "$1-after" with "${1}-after"


  • Org-mode version 6.33x (7.9.3e.1054.g5d467) (latest git)
  • yasnippet (version 0.8.0 (beta)) (from github, 2013-02-15)

I started with «emacs -q test.org» and only activated yasnippet and Org-mode without customizations.

Although, in http://article.gmane.org/gmane.emacs.orgmode/66297 somebody could not re-produce this effect. I don't have any clue why he got different behavior especially because I did not use any customization (emacs -q).



* Test
:ID: ${1:test}-after

Expected result:

* Test
:ID: foo bar-after

-> This is what I'd like to achieve.

But unfortunately:

* Test
foo bar  :test:     test-after

-> "foo bar" is at beginning of line and "test" does not get
replaced by "foo bar" (which was the user input in every example here)
when being in Org major mode. (It works in text mode.)

Demo: omitting one colon

* Test
ID: ${1:test}-after

... results in:

* Test
  ID: foo bar-after

-> works when first colon is missing


* Test
:ID: $1-after

... results in:

* Test
foo bar  :ID:       -after


Demo: omitting the string after $1

* Test
:ID: $1

... results in:

* Test
  :ID: foo bar

-> works when "-after" is not given


* Test
:ID: ${1:test}

... results in:

* Test
foo bar

-> ERROR (second line is marked as a wole and thus overwritten by user)

Demo: missing heading

:ID: ${1:test}-after

... results in:

:test: foo bar-after

-> works when heading line is omitted

yyr commented Feb 15, 2013

I can reproduce this problem


joaotavora commented Feb 16, 2013

Nice report, I'll try to check it out soon. But where's the test.org file you speak of? And surely some initial customization lines have been used, right? Also, have you considered adding these tests to yasnippet-tests.el in a pull request? Use the existing tests there as a guide.

novoid commented Feb 16, 2013

test.org is an empty file. I only activated yasnippet minor mode and Org-mode (http://orgmode.org/org.html#Activation)

No special initialization was used.

Sorry, being an elisp-newbie, adding this issue to yasnippet-tests.el seems to be "too much" for my current knowledge :-(

novoid commented Jun 9, 2013


Issue still exists with:

  • GNU Emacs 23.4.1 (x86_64-pc-linux-gnu, GTK+ Version 2.24.10) of 2012-09-08 on trouble, modified by Debian
  • Org-mode version 8.0.2 (release_8.0.2-121-g86fab4 @ /home/vk/.emacs.d/contrib/org-mode/lisp/)
  • yasnippet (version 0.8.0beta) -- pluskid pluskid@gmail.com/joaotavora joaotavora@gmail.com

I can not make yasnippet-tests.el run. Therefore, I can not test the following elisp test which should demonstrate my issue:

  (ert-deftest my-org-mode-bug ()
      (yas-minor-mode 1)
      (yas-expand-snippet "** Test ${1:test}\n:PROPERTIES:\n:ID: ${1:test}-after\n:END:")
      (should (string= (yas--buffer-contents)
                       "** Test test\n:PROPERTIES:\n:ID: test-after\n:END:"))
      (ert-simulate-command `(yas-mock-insert "bla"))
      (should (string= (yas--buffer-contents)
                       "** Test bla\n:PROPERTIES:\n:ID: bla-after\n:END:")))

novoid commented Nov 21, 2013

Does yasnippet-tests.el run at your side? Can you re-produce the issue?

It basically prevents me from using yasnippet with Org-mode :-(


joaotavora commented Nov 21, 2013

First of all, I'm sorry for the enormous delay in replying to this.

  • To run the tests in batch

    emacs -Q -L . -l yasnippet-tests.el -nw --batch -e yas-batch-run-tests

    This is with a very recent emacs, but I'm pretty sure it should work with your. What exactly doesn't work?

  • There are indeed problems with expanding auto-indenting snippets in org-mode, probably due to the way drawers are implemented in org mode. Try the same snippet with fixed indentation (see variable yas-indent-line). In other words, the following tests passes

    (ert-deftest my-org-mode-bug ()
        (yas-minor-mode 1)
        (let ((yas-indent-line 'fixed))
          (yas-expand-snippet "** Test ${1:test}\n   :PROPERTIES:\n   :ID: $1-after\n   :END:"))
        (should (string= (yas--buffer-contents)
                         "** Test test\n   :PROPERTIES:\n   :ID: test-after\n   :END:"))
        (ert-simulate-command `(yas-mock-insert "bla"))
        (should (string= (yas--buffer-contents)
                         "** Test bla\n   :PROPERTIES:\n   :ID: bla-after\n   :END:"))))

    Notice that in your particular case, auto-indentation is useless anyway since ** Test will always be indented to the leftmost column, right?

  • This is a confirmed problem (org-mode + auto-indenting snippets) in the latest yasnippet, and in my investigations, it seems to also happen with the new upcoming version with a new snippet expansion engine. However, it should be easier to fix and debug there.

novoid commented Nov 25, 2013


I still can not run the test suite. I resolved the missing "ert" with:

curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el

After this, I ran into "Cannot open load file: cl-lib" which I could
not resolve yet.

However, I made a progress in the issue itself: you mentioned
"yas-indent-line". I set it to "fixed" and with this, my Org-mode
snippet gets expanded without the issues mentioned.

It seems that this might be a viable work-around to me. Since the issue is already known, I consider this a duplicate and close it.

Thanks for your help and I am looking forward to the new upcoming version where the bug can be fixed more easily! :-)

@novoid novoid closed this Nov 25, 2013


npostavs commented Nov 25, 2013

After this, I ran into "Cannot open load file: cl-lib" which I could
not resolve yet.

You can get cl-lib from http://elpa.gnu.org/packages/cl-lib-0.3.el (or else upgrade emacs to 24.3).

novoid commented Nov 25, 2013


I downloaded cl-lib.el from the URL provided above. Now I get this:

vk@powerplant ~/.emacs.d/contrib/yasnippet % emacs -Q -L . -l yasnippet-tests.el -nw --batch -e yas-batch-run-tests
Symbol's function definition is void: define-error
255 vk@powerplant ~/.emacs.d/contrib/yasnippet %

joaotavora commented Nov 26, 2013

This has to be debugged further, but I wonder if it's worth the trouble or if it's yasnippet's fault. Can't you upgrade to emacs 24?

novoid commented Nov 26, 2013

I don't want to migrate to Emacs 24 for now. Maybe I will find time in the holiday season ...


npostavs commented Nov 29, 2013

I downloaded emacs 23.4 to try this out. The define-error is from trunk ert.el, the version from before it was merged to emacs works:

curl -L -O https://github.com/ohler/ert/raw/c619b56c5bc6a866e33787489545b87d79973205/lisp/emacs-lisp/ert-x.el
curl -L -O https://github.com/ohler/ert/raw/c619b56c5bc6a866e33787489545b87d79973205/lisp/emacs-lisp/ert.el

Then it turns that cl-lib doesn't define cl-flet(!), it can simply be changed to flet:

diff --git i/yasnippet-tests.el w/yasnippet-tests.el
index 1d770b4..25185ab 100644
--- i/yasnippet-tests.el
+++ w/yasnippet-tests.el
@@ -325,7 +325,7 @@ TODO: correct this bug!"
     ;; saving all definitions before overriding anything ensures FDEFINITION
     ;; errors don't cause accidental permanent redefinitions.
-    (cl-flet ((set-fdefinitions (names functions)
+    (flet ((set-fdefinitions (names functions)
                                 (loop for name in names
                                       for fn in functions
                                       do (fset name fn))))

One unexpected failure, I think it's just because emacs 23.4 doesn't define prog-mode:

src/emacs -Q --batch -L ~/src/yasnippet -l cl-lib -l yasnippet-tests -nw -f yas-batch-run-tests
Wrote /tmp/yasnippet-fixture26865GEl/.emacs.d/snippets/c-mode/foo-group-b/strcmp
Wrote /tmp/yasnippet-fixture26865GEl/.emacs.d/snippets/lisp-interaction-mode/ert-deftest
Selector: (not (tag :external))
Passed: 32
Failed: 3 (1 unexpected)
Total:  35/35

Started at:   2013-11-28 20:35:48-0500
Finished at:  2013-11-28 20:35:53-0500


F loading-with-cyclic-parenthood
    Test loading when cyclic parenthood is setup.
    (cl-set-exclusive-or expected observed)))
      :value nil))

joaotavora commented Dec 2, 2013

I'm reopening and renaming this since it is still a bug: yasnippet should at least try to auto-indent in modes with funny indentation functions such as Org.

@joaotavora joaotavora reopened this Dec 2, 2013


npostavs commented May 4, 2016

Problematic indenting code seems to be at org.el:22202-22206

novoid commented Dec 2, 2016 edited

Issue still persists:

  • GNU Emacs 24.5.1 (x86_64-pc-linux-gnu, GTK+ Version 3.14.5) of 2016-03-19 on trouble, modified by Debian
  • Org-mode version 8.2.10 (release_8.2.10 @ mixed installation! /usr/share/emacs/24.5/lisp/org/ and /home/vk/.emacs.d/elpa/org-30000101/)
  • elpa/yasnippet-20160410.833

Emacs started with -q and following configuration:

(setq package-user-dir "~/.emacs.d/elpa")
(add-to-list 'package-archives '("melpa" . "http://stable.melpa.org/packages/"))
(require 'use-package)
(use-package yasnippet
  :demand t
  :mode ("/\\.emacs\\.d/snippets/" . snippet-mode)
  :diminish yas-minor-mode
  :defer 15
  (yas-load-directory "~/.emacs.d/snippets/")
  (yas-global-mode 1)

Snippet used:

# name : Testing yasnippet issue
# --

** Test ${1:test}
:ID: $1-myid

Gave up on ert/cl-lib.


npostavs commented Dec 3, 2016

Can you try out the fix in #755, please?

novoid commented Dec 4, 2016 edited

I failed.

I replaced the patched yasnippet.el with the one from the commit mentioned.

Then I started emacs -q and did a eval-buffer on the yasnippet.el file.

To load my test snippet, I opened the test-orgissue snippet file and invoked yas-load-snippet-buffer. This is asking me Choose or enter a table (yas guesses text-mode): but org-mode in not a possible option. Therefore I also failed on invoking the snippet in a test.org file.

Could you please give me a hint how to test this properly? Thanks!


npostavs commented Dec 4, 2016 edited

Assuming yasnippet.el is this file: https://raw.githubusercontent.com/npostavs/yasnippet/indent-re-markers/yasnippet.el, and you're in its directory.

Run emacs -Q -L . -l yasnippet.el -f yas-global-mode -f org-mode
Then C-u M-x yas-new-snippet RET.
Copy this snippet into the *new snippet* buffer that pops up:

# name : Testing yasnippet issue
# key: test
# --

** Test ${1:test}
:ID: $1-myid

do C-c C-l, hit RET at the prompt (it should say Choose or enter a table (yas guesses org-mode):).

Now you should be able to invoke your snippet in org-mode, by typing test and hitting tab.

novoid commented Dec 4, 2016 edited

Awesome instructions, thanks.

I can confirm that the patch did fix the issue.

Do you have an idea when the patch will find its way to the master branch?


npostavs commented Dec 4, 2016

I can confirm that the patch did fix the issue.

Thanks for testing.

Do you have an idea when the patch will find its way to the master branch?

Probably a week or two.

@npostavs npostavs closed this in #755 Dec 12, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment