如何在Emacs之前执行更改行的操作? [英] How to perform operations on changed lines in Emacs before-save?

查看:155
本文介绍了如何在Emacs之前执行更改行的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个先前保存的钩子,在这里我可以做一些操作,只是自上次保存以来发生变化的行。例如,删除尾随空格,检查缩进等。我不想为整个文件执行此操作,我知道每个选项都有单独的选项(仅为了删除所有更改的行的尾随空格,等等),但我想要一些通用的东西,以便我可以添加更多的东西。我想象有一些东西可以让我们改变行号列表,或者是get-first-change-line(),get-next-changed-line()类型的函数。

解决方案

使用高亮变化模式机器



您可以使用更改模式,然后迭代使用下一个单一属性更改设置的文本属性 hilit-chg



例如,

  with-current-buffermy-buffer-name
(let((beg(point-min))end)
(while(setq end(next-single-property-change beg'hilit- chg))
(setq beg(next-single-property-change end'hilit-chg))
(message[[%s]](buffer-substring-no-properties end beg) ))))

将产生以下内容:

  [[
这些是我的更改

]]
[[和这里]]
[[
这里有更多的变化
]]










实施示例

ws-butler 使用这个机制在EOL修剪空格,保存修改
行。


I'd like to add a before-save-hook where I can do some operations for just the lines that have changed since the last save. For e.g., remove trailing whitespace, check indentation, etc.. I don't want to do this for an entire file, and I am aware that there are individual options for each of these(just to remove trailing whitespace for all changed lines, etc..), but I'd like something generic so that I can add more stuff to it. I imagine there is something where I can either get the list of line numbers changed, or a get-first-changed-line(), get-next-changed-line() type of functions.

解决方案

use highlight-changes-mode machinery

You could use highlight-changes-mode and then iterate over the text property hilit-chg set by it using next-single-property-change.

E.g.,

(with-current-buffer "my-buffer-name"
  (let ((beg (point-min)) end)
    (while (setq end (next-single-property-change beg 'hilit-chg))
      (setq beg (next-single-property-change end 'hilit-chg))
      (message "[[%s]]" (buffer-substring-no-properties end beg)))))

will produce the following:

[[
these are my changes

]]
[[ and here]]
[[
here are more changes
]]

in the *Messages* buffer (and in the echo area).

full implementation example

ws-butler uses this mechanism to trim spaces at EOL on save for modified lines.

这篇关于如何在Emacs之前执行更改行的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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