我决定尝试一下 lisp,因为我想让 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 点文件中。这正如预期的那样。
然后我决定我所做的基本上是次要模式,所以我尝试将键绑定(bind)移动到次要模式。由于某种原因,即使启用了次要模式(如模式行中所示,并且只是通过打开和关闭它),当我点击TAB 键。我仍然可以按预期使用 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)
干杯,
克里斯
最佳答案
我认为你们非常接近。
尝试一下你的键盘映射:
'(("\t" . tab-dwim)))
关于emacs - 第一次 elisp 尝试 - Tab 键未在选项卡上调用的次要模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7584991/