本文介绍了Elisp用于次模式的内部变量范围(setq / make-local-variable / let)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了这个小小的模式,以便在主模式执行初始缩进行为和/或在主模式认为没有缩进是必要的时候强制缩进,以使TAB键继续缩进。



已经指出,组合 setq make-local-variable 可以简化为 let 范围。鉴于这需要同时跨多个缓冲区进行工作,如何使用 let 而不是 make-local-variable

  ;;; dwim-tab.el ---轻微模式,当TAB否则停止

时强制缩进;内部跟踪变量
(setq dwim-tab-point-before nil)
(setq dwim-tab-point-after nil)

(defun dwim-tab()
一般缩进一次,然后如果再次调用则切换到tab-to-tab-stop
如果第一个TAB按钮不会导致
点移动,则始终执行tab-to-tab-stop
(interactive)
(setq dwim-tab-point-before(point))
(if(eq dwim-tab-point-before dwim-tab-point-after);再次按TAB
(tab-to-tab-stop)
(indent-for-tab-command))
(if(eq(point)dwim-tab-point-before);点没有移动
(tab-to-tab-stop))
(setq dwim-tab-point-after(point)))

(define-minor-模式dwim-tab-mode
切换dwim-tab模式
使用非零参数,打开dwim-tab模式,使用nil参数,将
关闭。

当启用dwim-tab模式时,按TAB键一次会按照正常的方式执行,
,然后按下它将继续缩进,使用
片 - 制表位。

如果dwim-tab确定第一个TAB键按压导致
的移动没有​​移动,它将根据Tab-to-tab-stop而缩进。
:init-value nil
:lightDWIM
:keymap'((\t.dwim-tab))
(make-local-variable'dwim-tab-point -before)
(make-local-variable'dwim-tab-point-after))

(提供'dwim-tab)
/ pre>

解决方案

这样做你想要什么吗?看ma,没有变量!



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
=(point)(progn
(indent-for-tab-command)
(point))))
(tab-to-tab-stop))

假设 indent-for-tab-command 不会再神奇地开始缩进,但CPU效率略高一些ent。


I've written this little minor mode to get the TAB key to continue indenting after the major mode has performed the initial indent behaviour and/or to force indentation when the major mode thinks no indent is necessary.

It has been pointed out to me that the combination setq and make-local-variable can probably be simplified into a let scope. Given that this needs to work across multiple buffers concurrently, how would one change this to use let instead of 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)
解决方案

Does this do what you want? Look ma, no variables!

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

The last-command check isn't strictly necessary assuming indent-for-tab-command won't magically start indenting again. But it's slightly more CPU efficient.

这篇关于Elisp用于次模式的内部变量范围(setq / make-local-variable / let)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 10:40