2

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


我正在尝试将 org-mode 和 yasnippet 一起使用,即使使用 org-mode FAQ 中的一些解决方法,它也无法正常工作。每当我点击TAB一个片段缩写时,这个词就会被删除。TAB如果我没有超过一个片段词,则表现正常,所以发生了一些事情......

我正在使用Org-mode version 7.7,yasnippet (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-hookorg 缓冲区中的以下输出:

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"

所以 yas 在某个地方出错了……我的edebugfoo 没有达到标准,但是如果我有时间,我会尝试单步执行并查看错误在哪里。我完整的 emacs 配置在github 上。

4

2 回答 2

4

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

换句话说,这完全有效:

;; 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)))
于 2012-03-03T22:12:39.807 回答
2

这也花了我一些时间来解决。我正在使用Org-mode version 7.7, yasnippet (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-expandC-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'
于 2012-02-23T23:32:19.447 回答