如何模拟 Vim 的“softtabstop”在 Emacs 中?

发布于 2024-08-05 16:51:21 字数 897 浏览 2 评论 0原文

我最近一直在尝试使用 emacs,我需要做的事情之一就是缩进。

示例 1:

sub foo {
    my $bar = 'quux';
    |

示例 2:

sub foo {
    my $bar = 'quux';       |# foo

假设上面示例中的竖线字符表示光标位置。现在,我对每个缩进级别使用 (4) 个空格(无制表符),并且我有 emacs 设置来自动缩进我的代码,并牢记这一点。那里没有问题。但在上面的示例中,如果我要在指示的光标位置按退格键,我希望 emacs 一直退格到下一个缩进级别(第 / 4 列)。也就是说,我希望它将前面的空格视为由制表符组成。相反,它总是只删除一个空格字符。

在 vim 中,我打开“expandtab”以使其插入空格而不是制表符,以及“softtabstop”,这使其(除其他外)退格到下一个“软制表符”,如上所述。

在 emacs 中,我想我可以(如果我更好地了解 emacs/elisp)将退格键绑定到执行以下操作的函数:

if indent-tabs-mode is nil
    if the cursor position is preceded by whitespace
        calculate the position of the previous "soft tabstop"
        if there's enough whitespace
            backspace all the way to that point
        else
            backspace by one character

我想知道的是,是否有更简单的方法可以做到这一点,和/或有人知道吗现有的解决方案?

I've been trying to get into emacs lately, and one of the things I need to get right is indentation.

Example 1:

sub foo {
    my $bar = 'quux';
    |

Example 2:

sub foo {
    my $bar = 'quux';       |# foo

Imagine that the pipe character in the above examples indicates the cursor position. Now, I use (4) spaces for every indent level (no tabs), and I have emacs setup to indent my code automatically with that in mind. No problems there. But in the examples above, if I were to hit backspace at the indicated cursor positions, I want emacs to backspace all the way back to the next indent level (column / 4). That is, I want it to treat the preceding whitespace as if it were made up of tabs. Instead, it always just erases a single space character.

In vim, I turn on 'expandtab' to make it insert spaces instead of tabs, and 'softtabstop', which makes it (among other things) backspace to the next "soft tabstop" as described above.

In emacs, I suppose I could (if I knew emacs/elisp better) bind backspace to a function which does something like the following:

if indent-tabs-mode is nil
    if the cursor position is preceded by whitespace
        calculate the position of the previous "soft tabstop"
        if there's enough whitespace
            backspace all the way to that point
        else
            backspace by one character

What I want to know is, is there a simpler way to do this, and/or does anyone know of an existing solution?

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

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

发布评论

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

评论(1

紫南 2024-08-12 16:51:21

这对我有用,其中 'tab-width 用作列的宽度。在适当的键盘映射中设置键...

(local-set-key (kbd "DEL") 'backward-delete-whitespace-to-column)
(defun backward-delete-whitespace-to-column ()
  "delete back to the previous column of whitespace, or as much whitespace as possible,
or just one char if that's not possible"
  (interactive)
  (if indent-tabs-mode
      (call-interactively 'backward-delete-char-untabify)
    (let ((movement (% (current-column) tab-width))
          (p (point)))
      (when (= movement 0) (setq movement tab-width))
      (save-match-data
        (if (string-match "\\w*\\(\\s-+\\)$" (buffer-substring-no-properties (- p movement) p))
            (backward-delete-char-untabify (- (match-end 1) (match-beginning 1)))
        (call-interactively 'backward-delete-char-untabify))))))

This works for me, where the 'tab-width is used as the width of the columns. Set the key in the appropriate keymaps...

(local-set-key (kbd "DEL") 'backward-delete-whitespace-to-column)
(defun backward-delete-whitespace-to-column ()
  "delete back to the previous column of whitespace, or as much whitespace as possible,
or just one char if that's not possible"
  (interactive)
  (if indent-tabs-mode
      (call-interactively 'backward-delete-char-untabify)
    (let ((movement (% (current-column) tab-width))
          (p (point)))
      (when (= movement 0) (setq movement tab-width))
      (save-match-data
        (if (string-match "\\w*\\(\\s-+\\)$" (buffer-substring-no-properties (- p movement) p))
            (backward-delete-char-untabify (- (match-end 1) (match-beginning 1)))
        (call-interactively 'backward-delete-char-untabify))))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文