如何在Emacs中安全地覆盖Tab键以仅用于缩进

3

我是一个Emacs的新用户,我特别不喜欢Emacs模式处理缩进的方式,特别是当混合使用模式时(比如ASP和perl)。我编写了以下函数来按照“经典”编辑器的方式进行缩进:

(defun classic-indent (width)
  "Tab the current line or block the 'classic' way"
  (save-excursion
  (if (not (use-region-p)) (select-current-line))
  (indent-rigidly (mark) (point) width)))

(defun indent-forward ()
  "tab two space forward"
  (interactive)
  (classic-indent 2))

(defun indent-back ()
  "tab two spaces back"
  (interactive)
  (classic-indent -2))


(defun select-current-line ()
  "Select the current line"
  (interactive)
  (end-of-line) ; move to end of line
  (set-mark (line-beginning-position)))

这里的想法是将 <backtab> 绑定到 indent-back 函数,将 <tab> 绑定到 indent-forward 函数。使用 M-x 调用这些函数时效果很好, <backtab> 绑定也没问题,但是如果我直接绑定 <tab> ,它会干扰所有酷炫的功能,比如自动补全。我尝试使用以下代码设置 indent-line-function:

(setq indent-line-function 'indent-forward)

并使用以下代码设置我的主模式缩进功能:

(setq cperl-indent-command 'indent-forward)

但是,它们都没有任何作用。我不确定是我设置有误还是这根本就不是正确的方法。

总之,如何在使用tab键时覆盖缩进而不会破坏其他“tab”行为(如自动完成)?


你是在全局更改 <tab> 绑定(例如使用 global-set-key)吗?如果不是,你是为哪些模式更改它的? - Tikhon Jelvis
此外,你应该尝试适应Emacs的缩进方式——与你喜欢的方案相比,它为我节省了很多工作。一旦我习惯了它,旧的方式就不再有意义了。 - Tikhon Jelvis
2个回答

0

0

当你加载一个缓冲区时,主模式默认设置可能会覆盖你的设置。确保你的indent-line-function是使用缓冲区本地的,就可以解决这个问题。

(add-hook 'elixir-mode-hook
          (lambda ()
            (electric-indent-mode -1)
            (setq indent-line-function 'indent-forward)))

参考:https://www.emacswiki.org/emacs/BufferLocalVariable


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接