在文本文件中的后续行重复TAB(但禁用代码禁用TAB) [英] Repeating TABs on subsequent lines in text files (but keeping TABs disabled for code)

查看:166
本文介绍了在文本文件中的后续行重复TAB(但禁用代码禁用TAB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我按 Cq TAB 在行的开头插入 TAB 字符,然后按几个字符。



一旦我按ENTER, emacs在以下行中插入八个空格。



如何在我的.emacs中指定我希望TAB在后面的行与TAB重复?



重要的是,我不喜欢程序代码中的TAB字符,所以我有(setq-default indent-tabs-mode nil),以确保只有当我明确要求时才插入TAB。

解决方案

真正的答案是:不,没有办法通过emacs中的一些简单的配置设置来做到这一点。 indent-tabs-mode 是打开还是关闭,缩进将按照这样做。



但是,只是因为这个功能不在,并不意味着你不能添加它!



这实际上不是我发现的一个简单的问题。是否使用选项卡或空格由C中的 indent-tabs-mode 决定。假设您正在运行最新版本的emacs,自动缩进来自电动缩进模式,它使用 indent-to-mode post-self-insert-hook 中进行缩进。



为此做的是定义缓冲区局部小模式,当此模式处于活动状态时 indent-tabs-mode 将根据最后一行的第一个字符临时设置,而运行 indent-upon-to-mode



所以当 smart-electric-indent-tabs-mode 处于活动状态时,最后一行从标签开始,下一行也会缩进一个选项卡,否则它只会使用通常设置的任何 indent-tabs-mode



您可以将以下内容添加到您的配置中以激活它。 add-hook 子句放在你的方便之后,你可以像普通的小模式一样在飞行中激活它,如果你愿意的话。

 (define-minor-mode smart-electric-indent-tabs-mode 
打开时,缩进将
:init-value nil
:打开smart-tabs
:keymap nil
:全局零)

(依赖于模式的defadvice indent(可能在使用选项卡激活)
跟随`smart-electric-indent tabs-mode'。
(let((indent-tabs-mode
(或者(和smart-electric-indent-tabs-mode
(save-excursion
保存限制
(加宽)
(行首0)
(查看\t))))
indent-tabs-mode)) )
ad-do-it))

;;如果需要,添加一个文本模式钩子
(add-hook'text-mode-hook'smart-electric-indent-tabs-mode)

这只是在电压缩期间才被测试


I am editing a text file foo.txt using emacs.

I press C-q TAB to insert a TAB character at the beginning of a line and then follow with a few characters.

Once I press ENTER, emacs inserts eight spaces on the following line.

How do I specify in my .emacs that I would like TABs to be repeated on subsequent lines with TABs?

Importantly, I dislike TAB characters in program code, and so I have (setq-default indent-tabs-mode nil) to make sure that TABs are inserted only when I explicitly ask for them.

解决方案

The real answer is: No, there is no way to do this by some simple configuration setting in emacs. indent-tabs-mode is either on or off, and indentation will behave according to that.

But, just because this feature is not there, doesn't mean you can't add it!

This is actually not a simple problem from what I found. Whether or not to use tabs or spaces is determined by indent-tabs-mode in C mostly. Assuming that you are running a recent version of emacs, the auto indentation is coming from electric-indent-mode which uses indent-according-to-mode in a post-self-insert-hook to do the indentation.

What I did for this was define a buffer local minor mode, when this mode is active indent-tabs-mode will be temporarily set depending on the first character in the last line while running indent-according-to-mode.

So when smart-electric-indent-tabs-mode is active, and your last line started with the tab, the next line will indent with a tab too, else it will just use whatever indent-tabs-mode would normally be set to.

You could add the following to your config to activate it. The add-hook clause is put in there for your convenience, you can activate it on the fly like a normal minor mode if you'd like.

(define-minor-mode smart-electric-indent-tabs-mode
  "When on, indenting will use tabs if the current line does,
    else it will indent according to `indent-tabs-mode'."
  :init-value nil
  :lighter " smart-tabs"
  :keymap nil
  :global nil)

 (defadvice indent-according-to-mode (around maybe-use-tabs activate)
  "Follow `smart-electric-indent-tabs-mode'."
  (let ((indent-tabs-mode
         (or (and smart-electric-indent-tabs-mode
                  (save-excursion
                    (save-restriction
                      (widen)
                      (beginning-of-line 0)
                      (looking-at "\t"))))
             indent-tabs-mode)))
    ad-do-it))

;; if you want, add a text mode hook
(add-hook 'text-mode-hook 'smart-electric-indent-tabs-mode)

This has only been tested to work during electric indentation

这篇关于在文本文件中的后续行重复TAB(但禁用代码禁用TAB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆