gpt4 book ai didi

EMACS:扩展/扩展区域,使其包含整行

转载 作者:行者123 更新时间:2023-12-04 15:26:44 25 4
gpt4 key购买 nike

我的初始化文件中有这个简洁的函数:

(defun comment-or-uncomment-region-or-line()
"Comments or uncomments the region or the current line if there's no active region."
(interactive)
(let (beg end)
(if (region-active-p)
(setq beg (region-beginning) end (region-end))
(setq beg (line-beginning-position) end (line-end-position))
)
(comment-or-uncomment-region beg end)
(next-line)
)
)

但是,我不喜欢的是以下情况:

Lorem ipsum dolor sit amet, consec|tetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proi|dent, sunt in culpa qui officia deserunt
mollit anim id est laborum.

注意: |表示区域的点或标记。

评论此区域将导致:

Lorem ipsum dolor sit amet, consec// tetur adipisicing elit, sed do eiusmod tempor
// incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
// exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
// dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
// Excepteur sint occaecat cupidatat non proi
// dent, sunt in culpa qui officia deserunt
// mollit anim id est laborum.

相反,我希望它简单:

// Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
// incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
// exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
// dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
// Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
// mollit anim id est laborum.

换句话说,我希望该区域扩展/扩展以包含整条线(第一条和最后一条),而不管标记和点位于第一条和最后一条线的中间。

有什么方法可以修改此函数以使其行为相应吗?

最佳答案

在将点实际移动到所需位置后调用line-beginning-positionline-end-position:

(defun comment-or-uncomment-region-or-line()
"Comments or uncomments the region or the current line if there's no active region."
(interactive)
(let (beg end)
(if (region-active-p)
(progn
(setq beg (region-beginning) end (region-end))
(save-excursion
(setq beg (progn (goto-char beg) (line-beginning-position))
end (progn (goto-char end) (line-end-position)))))
(setq beg (line-beginning-position)
end (line-end-position)))
(comment-or-uncomment-region beg end)
(next-line)))

关于EMACS:扩展/扩展区域,使其包含整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13186811/

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