org-mode和yasnippet之间的冲突

时间:2012-02-23 17:35:07

标签: emacs org-mode yasnippet

编辑我的问题一直与代码段语法有关...以下配置完全有效。


我正在尝试将org-mode和yasnippet一起使用,即使在org-mode FAQ上的某些workarounds也无法正常工作。每当我在片段缩写上点击 TAB 时,该词就会被删除。 TAB 如果我没有翻过一个片段字就行为正常,所以有一些事情发生......

我正在使用Org-mode version 7.7yasnippet (version 0.7.0)GNU Emacs 23.4.1

这是我的设置:

(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)))

我非常确定钩子是按预期运行的,因为组织缓冲区中的 C-h v org-tab-first-hook 的输出如下:

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)

这是组织缓冲区中的 C-h k TAB

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

修改

edebug-defun函数上执行yas/org-very-safe-expand后,我看到以下消息

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

所以,某些地方的错误......我的edebug foo并不完全可以达到标准,但如果我有一些时间,我会尝试单步执行并查看错误的位置。我的完整emacs配置在github上here

2 个答案:

答案 0 :(得分:3)

原来我的问题与片段语法有关,而不是设置。傻傻的我......

换句话说,这完全奏效:

;; 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)))

答案 1 :(得分:1)

这花了我一些时间来解决问题。我正在使用Org-mode version 7.7yasnippet (version 0.6.1c)GNU Emacs 22.1.1。以下是我的 .emacs 文件的相关部分(有些flyspell内容无关紧要):

;;                                                                              
;; 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之外,

yas/org-very-safe-expand与您的相同。 C-h k TAB显示:

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'