修改缓冲区时如何运行Emacs钩子?

时间:2022-10-05 23:53:00

Building on Getting Emacs to untabify when saving certain file types (and only those file types) , I'd like to run a hook to untabify my C++ files when I start modifying the buffer. I tried adding hooks to untabify the buffer on load, but then it untabifies all my writable files that are autoloaded when emacs starts.

建立在保存某些文件类型(并且只有那些文件类型)时让Emacs解压缩时,我想在我开始修改缓冲区时运行一个钩子来解压缩我的C ++文件。我尝试添加钩子来加载缓冲区,但是当emacs启动时它会解除所有可自动加载的可写文件。

(For those that wonder why I'm doing this, it's because where I work enforces the use of tabs in files, which I'm happy to comply with. The problem is that I mark up my files to tell me when lines are too long, but the regexp matches the number of characters in the line, not how much space the line takes up. 4 tabs in a line can push it far over my 132 character limit, but the line won't be marked appropriately. Thus, I need a way to tabify and untabify automatically.)

(对于那些想知道我为什么这样做的人来说,这是因为我工作的地方强制使用文件中的标签,我很乐意遵守。问题是我标记了我的文件,告诉我线条何时也是如此但是,正则表达式匹配行中字符的数量,而不是行占用的空间。一行中的4个制表符可以将它推到我的132个字符限制之上,但该行不会被正确标记。因此,我需要一种自动标记和解压缩的方法。)

4 个解决方案

#1


4  

Take a look at the variable "before-change-functions".

看一下变量“before-change-functions”。

Perhaps something along this line (warning: code not tested):

也许沿着这条线(警告:代码没有测试):

(add-hook 'before-change-functions 
          (lambda (&rest args) 
            (if (not (buffer-modified-p))
                (untabify (point-min) (point-max)))))

#2


1  

Here is what I added to my emacs file to untabify on load:

这是我添加到我的emacs文件中以加载时取消更改:

(defun untabify-buffer ()
  "Untabify current buffer"
  (interactive)
  (untabify (point-min) (point-max)))

(defun untabify-hook ()
  (untabify-buffer))

; Add the untabify hook to any modes you want untabified on load
(add-hook 'nxml-mode-hook 'untabify-hook)

#3


0  

This answer is tangential, but may be of use.

这个答案是切向的,但可能有用。

The package wide-column.el link text changes the cursor color when the cursor is past a given column - and actually the cursor colors can vary depending on the settings. This sounds like a less intrusive a solution than your regular expression code, but it may not suit your needs.

当光标经过给定列时,包wide-column.el链接文本会更改光标颜色 - 实际上光标颜色可能会因设置而异。这听起来像是一个比正则表达式代码更少侵入性的解决方案,但它可能不适合您的需求。

#4


0  

And a different, tangential answer.

一个不同的,切向的答案。

You mentioned that your regexp wasn't good enough to tell when the 132 character limit was met. Perhaps a better regexp...

你提到你的正则表达式不足以判断何时满足132个字符的限制。或许更好的正则表达式......

This regexp will match a line when it has more than 132 characters, assuming a tabs width is 4. (I think I got the math right)

假设标签宽度为4,此正则表达式将匹配一行超过132个字符的行。(我认为我的数学正确)

"^\\(?: \\|[^   \n]\\{4\\}\\)\\{33\\}\\(.+\\)$"

The last parenthesized expression is the set of characters that are over the limit. The first parenthesized expression is shy.

最后一个带括号的表达式是超出限制的字符集。第一个带括号的表达式很害羞。

#1


4  

Take a look at the variable "before-change-functions".

看一下变量“before-change-functions”。

Perhaps something along this line (warning: code not tested):

也许沿着这条线(警告:代码没有测试):

(add-hook 'before-change-functions 
          (lambda (&rest args) 
            (if (not (buffer-modified-p))
                (untabify (point-min) (point-max)))))

#2


1  

Here is what I added to my emacs file to untabify on load:

这是我添加到我的emacs文件中以加载时取消更改:

(defun untabify-buffer ()
  "Untabify current buffer"
  (interactive)
  (untabify (point-min) (point-max)))

(defun untabify-hook ()
  (untabify-buffer))

; Add the untabify hook to any modes you want untabified on load
(add-hook 'nxml-mode-hook 'untabify-hook)

#3


0  

This answer is tangential, but may be of use.

这个答案是切向的,但可能有用。

The package wide-column.el link text changes the cursor color when the cursor is past a given column - and actually the cursor colors can vary depending on the settings. This sounds like a less intrusive a solution than your regular expression code, but it may not suit your needs.

当光标经过给定列时,包wide-column.el链接文本会更改光标颜色 - 实际上光标颜色可能会因设置而异。这听起来像是一个比正则表达式代码更少侵入性的解决方案,但它可能不适合您的需求。

#4


0  

And a different, tangential answer.

一个不同的,切向的答案。

You mentioned that your regexp wasn't good enough to tell when the 132 character limit was met. Perhaps a better regexp...

你提到你的正则表达式不足以判断何时满足132个字符的限制。或许更好的正则表达式......

This regexp will match a line when it has more than 132 characters, assuming a tabs width is 4. (I think I got the math right)

假设标签宽度为4,此正则表达式将匹配一行超过132个字符的行。(我认为我的数学正确)

"^\\(?: \\|[^   \n]\\{4\\}\\)\\{33\\}\\(.+\\)$"

The last parenthesized expression is the set of characters that are over the limit. The first parenthesized expression is shy.

最后一个带括号的表达式是超出限制的字符集。第一个带括号的表达式很害羞。