【发布时间】:2011-09-28 14:39:27
【问题描述】:
我决定用一点口齿不清来弄湿我的脚趾,因为我想让 emacs 在我点击 TAB 时表现得更好一些。我的命令工作正常。它只是执行indent-for-tab-command,如果什么也没发生,它执行tab-to-tab-stop,假设我不太可能点击 TAB 只是为了让我在一个多用途中拒绝让步线串或类似的东西。在第一次按 TAB 之后,它会继续执行tab-to-tab-stop,直到继续编辑,或者将点移到其他位置。 AFAIK,我的逻辑没问题,虽然我的 lisp 代码可能不是!
最初我只是通过(local-set-key (kbd "TAB") 'tab-dwim) 将它侵入到我的 emacs 点文件中,用于我想要这种行为的主要模式。这按预期工作。
然后我决定我所做的基本上是次要模式,所以我尝试将键绑定移动到次要模式。出于某种原因,即使启用了次要模式(如模式行中所示,并且只是通过打开和关闭它),当我点击 TAB时,我的 tab-dwim 函数没有被调用> 键。我仍然可以按预期使用 M-x 调用它。
我的次要模式的:keymap 做错了什么?
;;;
;; TAB DWIM
; buffer-local before/after point tracking
(setq point-before-tab nil)
(setq point-after-tab nil)
(make-local-variable 'point-before-tab)
(make-local-variable 'point-after-tab)
(defun tab-dwim ()
"Indents normally once, then switches to tab-to-tab-stop if invoked again.
tab-dwim will always perform tab-to-tab-stop if the first TAB press does not
cause the point to move."
(interactive)
(print "in tab-dwim now") ; THIS LINE IS NEVER INVOKED ON TAB?
(setq point-before-tab (point))
(if (eq point-before-tab point-after-tab) ; pressed TAB again
(tab-to-tab-stop)
(indent-for-tab-command))
(if (eq (point) point-before-tab) ; point didn't move
(tab-to-tab-stop))
(setq point-after-tab (point)))
(define-minor-mode tab-dwim-mode
"Toggle tab-dwim-mode.
With a non-nil argument, turns on tab-dwim-mode. With a nil argument, turns it
off.
When tab-dwim-mode is enabled, pressing the TAB key once will behave as normal,
but pressing it subsequent times, will continue to indent, using
tab-to-tab-stop.
If tab-dwim determines that the first TAB key press resulted in no movement of
the point, it will indent according to tab-to-tab-stop instead."
:init-value nil
:lighter " DWIM"
:keymap
'(([TAB] . tab-dwim)))
(provide 'tab-dwim)
干杯,
克里斯
【问题讨论】: