gpt4 book ai didi

function - emacs 中有 apply-function-to-region-lines 吗?

转载 作者:行者123 更新时间:2023-12-02 10:23:36 25 4
gpt4 key购买 nike

我的很多工作都涉及搜索和删除不必要的代码行。因此,我创建一个宏,然后选择所有行 (C-x h),然后运行命令 (apply-macro-to-region-lines)。我设法保存该命令并将其放入我的 .emacs 文件中;我将其命名为 cut_it_now。但现在我的函数不再是宏,所以我不能再使用 (apply-macro-to-region-lines) 函数。你知道是否有(apply-function-to-region-lines)在某处实现?

非常感谢,

D

最佳答案

以下函数应该可以完成您想要的操作:

(defun apply-function-to-region-lines (fn)
(interactive "aFunction to apply to lines in region: ")
(save-excursion
(goto-char (region-end))
(let ((end-marker (copy-marker (point-marker)))
next-line-marker)
(goto-char (region-beginning))
(if (not (bolp))
(forward-line 1))
(setq next-line-marker (point-marker))
(while (< next-line-marker end-marker)
(let ((start nil)
(end nil))
(goto-char next-line-marker)
(save-excursion
(setq start (point))
(forward-line 1)
(set-marker next-line-marker (point))
(setq end (point)))
(save-excursion
(let ((mark-active nil))
(narrow-to-region start end)
(funcall fn)
(widen)))))
(set-marker end-marker nil)
(set-marker next-line-marker nil))))

因此,如果您想要将以下函数应用于缓冲区中的行:

(defun test()
(insert "> "))

并且,如果您的缓冲区包含以下内容:

Line 1: blah, blah
Line 2: blah, blah
Line 3: blah, blah
Line 4: blah, blah

如果您选择仅包含第 2 行和第 3 行的区域,输入“M-x apply-function-to-region-lines”,并在提示时输入“test”作为函数名称,您将在缓冲区中得到以下结果:

Line 1: blah, blah
> Line 2: blah, blah
> Line 3: blah, blah
Line 4: blah, blah

关于function - emacs 中有 apply-function-to-region-lines 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6532898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com