【问题标题】:Elisp internal variable scoping for a minor mode (setq/make-local-variable/let)Elisp 用于次要模式的内部变量范围(setq/make-local-variable/let)
【发布时间】:2011-11-27 00:44:56
【问题描述】:

我写了这个小模式,让 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)

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:

    这是你想要的吗?看,没有变量!

    (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 效率略高。

    【讨论】:

    • 这很聪明 :) 没有测试过,但阅读逻辑似乎很合理。让我们来测试一下吧!
    • 爱它。完美运行。我永远不会这么想。感谢您的大开眼界。
    • 我注意到你是个 Ruby 人。你学 lisp 越多,你就越了解 ruby​​ 是从哪里来的。 ;) 我相信正确地学习 elisp 会让你成为一个更好的程序员。
    • 是的,我最初是因为这个原因想学习 scheme/guile,但最近转而使用 emacs,部分原因是它是必须学习一些 lisp/函数式编程概念的借口。我听说人们在 ruby​​ 聚会上大谈函数式编程。
    • 是的,显然scheme/guile/clojure 通常更有用。但 elisp 绝对足以让你开始朝那个方向思考。
    猜你喜欢
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 2016-11-13
    • 2013-12-27
    • 2013-10-04
    • 1970-01-01
    相关资源
    最近更新 更多