12

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

一位朋友建议我在这里想要的是一个 yasnippet-can-fire-p,但我仍然不确定执行此操作的最佳方法。实现这一点的最干净的途径是什么,不会让我的系统陷入停顿?

4

1 回答 1

14

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

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

而且,令人惊讶的是,它实际上并没有放慢速度。

;; 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. cf问题

于 2013-01-10T18:56:17.630 回答