mgalgs
mgalgs

Reputation: 16779

Conflicts between org-mode and yasnippet

EDIT My issue was related to snippet syntax all along... The configuration below totally works.


I'm trying to use org-mode and yasnippet together and it's not working even with some of the workarounds on the org-mode FAQ. Whenever I hit TAB on a snippet abbreviation the word gets deleted. TAB behaves normally if I'm not over a snippet word, so there's something going on...

I'm using Org-mode version 7.7, yasnippet (version 0.7.0), and GNU Emacs 23.4.1.

Here's my setup:

(setq load-path
      (append (list nil
                    "~/.emacs.d/site-lisp/yasnippet"
                    "~/.emacs.d/site-lisp/org-7.7/lisp")
              load-path))

;; set up yasnippet
(require 'yasnippet)
(yas/initialize)
(setq yas/snippet-dirs '("~/.emacs.d/mysnippets"
                         "~/.emacs.d/site-lisp/yasnippet/snippets"))
(mapc 'yas/load-directory yas/snippet-dirs)

;; set up org mode
(require 'org-install)
;; fix some org-mode + yasnippet conflicts:
(defun yas/org-very-safe-expand ()
  (let ((yas/fallback-behavior 'return-nil)) (yas/expand)))

(add-hook 'org-mode-hook
          (lambda ()
            (make-variable-buffer-local 'yas/trigger-key)
            (setq yas/trigger-key [tab])
            (add-to-list 'org-tab-first-hook 'yas/org-very-safe-expand)
            (define-key yas/keymap [tab] 'yas/next-field)))

And I'm pretty sure the hook is running as expected because of the following output of C-h v org-tab-first-hook in an org buffer:

org-tab-first-hook is a variable defined in `org.el'.
Its value is
(yas/org-very-safe-expand org-hide-block-toggle-maybe org-src-native-tab-command-maybe org-babel-hide-result-toggle-maybe)

And here's C-h k TAB in an org buffer:

<tab> runs the command org-cycle, which is an interactive Lisp
function in `org.el'.

EDIT

After doing a edebug-defun on my yas/org-very-safe-expand function I'm seeing the following message

Result: "[yas] elisp error! Symbol's value as variable is void: err"

So yas is error'ing out somewhere... My edebug foo is not quite up to par but if I get some time I'll try to single step through and see where the error is. My full emacs configuration is on github here.

Upvotes: 2

Views: 4351

Answers (2)

mgalgs
mgalgs

Reputation: 16779

Turns out my issue was related to snippet syntax, not setup. Silly silly me...

In other words, this totally works:

;; fix some org-mode + yasnippet conflicts:
(defun yas/org-very-safe-expand ()
  (let ((yas/fallback-behavior 'return-nil)) (yas/expand)))

(add-hook 'org-mode-hook
          (lambda ()
            (make-variable-buffer-local 'yas/trigger-key)
            (setq yas/trigger-key [tab])
            (add-to-list 'org-tab-first-hook 'yas/org-very-safe-expand)
            (define-key yas/keymap [tab] 'yas/next-field)))

Upvotes: 4

cm2
cm2

Reputation: 1815

This took me some time to get worked out as well. I'm using Org-mode version 7.7, yasnippet (version 0.6.1c), GNU Emacs 22.1.1. Here are the relevant portions of my .emacs file (there is some flyspell stuff that is irrelevant):

;;                                                                              
;; org-mode stuff                                                               
;;                                                                              
(add-to-list 'load-path "/Users/cmalone/install/org-mode/org-mode/lisp")
(add-to-list 'load-path "/Users/cmalone/install/org-mode/org-mode/contrib/lisp")
(require 'org-install)

;;                                                                              
;; for YASnippet                                                                
;;                                                                              
(add-to-list 'load-path "/Users/cmalone/.emacs.d/plugins/yasnippet-0.6.1c")
(require 'yasnippet)
(yas/initialize)
(yas/load-directory "/Users/cmalone/.emacs.d/plugins/yasnippet-0.6.1c/snippets")
;; Make TAB the yas trigger key in the org-mode-hook and enable flyspell mode and autofill                                                                     
(add-hook 'org-mode-hook
          (lambda ()
            ;; yasnippet                                                        
            (make-variable-buffer-local 'yas/trigger-key)
            (org-set-local 'yas/trigger-key [tab])
            (define-key yas/keymap [tab] 'yas/next-field-group)
            ;; flyspell mode for spell checking everywhere                      
;;            (flyspell-mode 1)                                                 
            ;; auto-fill mode on                                                
            (auto-fill-mode 1)))

C-h v org-tab-first-hook is the same as yours except of the yas/org-very-safe-expand of course. C-h k TAB shows:

TAB runs the command yas/expand
  which is an interactive Lisp function in `yasnippet.el'.
It is bound to TAB, <menu-bar> <YASnippet> <Expand trigger>.
(yas/expand)

Expand a snippet before point.

If no snippet expansion is possible, fall back to the behaviour
defined in `yas/fallback-behavior'

Upvotes: 2

Related Questions