emacs - 当 Yasnippet 宏可以触发时,如何触发事件?

标签 emacs elisp yasnippet

我喜欢 yasnippet,但它需要时间来内存。我想要做的是在可以扩展宏的时候更改光标颜色(当没有宏时再返回)。但是,根据我对 yasnippet 的工作方式的内存,这可能并不完全有效。

一位 friend 建议我在这里想要的是 yasnippet-can-fire-p,但我仍然不确定这样做的最佳方法。实现这一点的最干净的方法是什么,不会使我的系统陷入停顿?

最佳答案

花了一些时间来找到检查它是否可以扩展的函数,但最终“幸运”地找到了它。

关键是这个函数通常会扩展,或者以其他方式执行回退行为。我克隆了这个函数并在这些地方设置了光标颜色。

而且,令人惊讶的是,它实际上根本没有减速。

;; It will test whether it can expand, if yes, cursor color -> green.
(defun yasnippet-can-fire-p (&optional field)
  (interactive)
  (setq yas--condition-cache-timestamp (current-time))
  (let (templates-and-pos)
    (unless (and yas-expand-only-for-last-commands
                 (not (member last-command yas-expand-only-for-last-commands)))
      (setq templates-and-pos (if field
                                  (save-restriction
                                    (narrow-to-region (yas--field-start field)
                                                      (yas--field-end field))
                                    (yas--current-key))
                                (yas--current-key))))

  (set-cursor-color (if (and templates-and-pos (first templates-and-pos)) 
                        "green" "red"))))

; As pointed out by Dmitri, this will make sure it will update color when needed.
(add-hook 'post-command-hook 'yasnippet-can-fire-p)

将此添加到我的 lisp 集合中(我实际上认为这也很有用)。

更新:在最新版本的 yasnippet [从 2014 年 8 月开始,来自 0.8.1 ], yas--current-key函数已重命名为 yas--templates-for-key-at-point .参见 Issue

关于emacs - 当 Yasnippet 宏可以触发时,如何触发事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14264228/

相关文章:

Emacs 包版本

emacs - 如何在 Spacemacs shell 中自定义 Oh My ZSH?

emacs - Emacs 中每种模式的不同颜色主题

emacs - YASnippet -- 如何创建右键单击上下文菜单 -- popup-menu

string - Emacs 口齿不清 : evaluating list of strings

emacs - yasnippet 如何调试?

emacs - 使用带有 EmacsW32 和 cygwin 的tramp,可能吗?

eclipse - Emacs 中的 TODO 工具窗口?

emacs - 在 emacs lisp 中使用侧滑手势

emacs - 将字体设置为部分模式行