gpt4 book ai didi

emacs - 次要模式的 Elisp 内部变量作用域 (setq/make-local-variable/let)

转载 作者:行者123 更新时间:2023-12-01 22:53:46 25 4
gpt4 key购买 nike

我编写了这个小的次要模式,让 TAB 键在主要模式执行初始缩进行为后继续缩进和/或在主要模式认为不需要缩进时强制缩进。

有人向我指出 setqmake-local-variable 的组合可能可以简化为 let 作用域。鉴于这需要同时跨多个缓冲区工作,如何将其更改为使用 let 而不是 make-local-variable

;;; dwim-tab.el --- minor mode to force indentation when TAB would otherwise stall

; internal tracking variables
(setq dwim-tab-point-before nil)
(setq dwim-tab-point-after nil)

(defun dwim-tab ()
"Indents normally once, then switches to tab-to-tab-stop if invoked again.
Always performs tab-to-tab-stop if the first TAB press does not cause the
point to move."
(interactive)
(setq dwim-tab-point-before (point))
(if (eq dwim-tab-point-before dwim-tab-point-after) ; pressed TAB again
(tab-to-tab-stop)
(indent-for-tab-command))
(if (eq (point) dwim-tab-point-before) ; point didn't move
(tab-to-tab-stop))
(setq dwim-tab-point-after (point)))

(define-minor-mode dwim-tab-mode
"Toggle dwim-tab-mode.
With a non-nil argument, turns on dwim-tab-mode. With a nil argument, turns it
off.

When dwim-tab-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 dwim-tab 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 '(("\t" . dwim-tab))
(make-local-variable 'dwim-tab-point-before)
(make-local-variable 'dwim-tab-point-after))

(provide 'dwim-tab)

最佳答案

这是否符合您的要求?看,没有变数!

(defun tab-dwim ()
(interactive)
(when (or (eq last-command this-command)
(= (point) (progn
(indent-for-tab-command)
(point))))
(tab-to-tab-stop)))

如果 indent-for-tab-command 不会奇迹般地再次开始缩进,那么最后一个命令检查并不是绝对必要的。但它的 CPU 效率略高。

关于emacs - 次要模式的 Elisp 内部变量作用域 (setq/make-local-variable/let),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7586331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com