如何安全地覆盖 emacs 中的缩进 Tab 键

发布于 2024-12-20 03:12:11 字数 1198 浏览 1 评论 0原文

我是 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)))

这里的想法是将 indent-back 绑定到 并将 indent-forward 绑定到 ;。当使用 Mx 调用它们时,这些函数工作得很好,并且 绑定工作正常,但如果我尝试绑定 > 直接地,它会干扰各种很酷的事情,例如自动完成。我尝试将 indent-line-function 设置为:

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

并设置我的主要模式缩进函数为:

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

但两者都没有任何效果。我不确定我是否设置错误,或者这是否是正确的方法。

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

I'm a new user to emacs, and I don't particularly like the way emacs modes handle indentation, especially when mixing modes (say ASP and perl). I have written the following functions to indent things the way a "classic" editor would:

(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)))

The idea here being to bind indent-back to <backtab> and indent-forward to <tab>. The functions work great when calling them with M-x, and the <backtab> binding works fine, but if I try to bind <tab> directly, it interfere's with all sorts of cool things like auto-completion. I tried setting indent-line-function with:

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

and setting my major modes indentation function with:

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

but neither has any effect. I'm not sure if I'm setting them wrong or if this is even the right approach.

to sum up, How can I override indentation with the tab key without clobbering other 'tab' behavior like auto-completion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是有多爱你 2024-12-27 03:12:11

Emacs wiki 有一个关于缩进和 TAB 的完整类别页面: http://www.emacswiki.org/emacs/CategoryIndentation 。请参阅有关为您提供各种 TAB DWIM(“智能”TAB)行为的库或片段的其他 wiki 页面。这里有几个:

Emacs wiki has a whole category page about indentation and TAB: http://www.emacswiki.org/emacs/CategoryIndentation. See that for other wiki pages about libraries or snippets that give you various kinds of TAB DWIM ("smart" TAB) behavior. Here are a couple:

撩心不撩汉 2024-12-27 03:12:11

当您加载缓冲区时,主模式默认值可能会覆盖您的设置。做这样的事情确保您的缩进行函数使用缓冲区本地并且它将起作用。

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

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

It's likely the major-mode default is overriding your setting when you load a buffer. Do something like this ensure your indent-line-function is used buffer-local and it will work.

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

ref: https://www.emacswiki.org/emacs/BufferLocalVariable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文