Marcin Borkowski 改进 Emacs 复制当前位置的代码
来源标题:Marcin Borkowski: Copying the current location improved again
Marcin Borkowski 再次改进了他的 Emacs 代码,使复制当前定位到 kill ring 时,在 region 激活的情况下会复制行范围,而不是单个位置。他借鉴 count-words-region 的处理方式:当 region-end 位于行首时,不把这一行计入范围,同时顺带修复了自己此前引入的几个 bug。
以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。
去年我改进了复制当前位置到 kill ring 的代码。最近我意识到,那还不是最终的版本。事实上,我挺惊讶自己之前居然没想到——事后看这几乎是显而易见的。思路是:当 region 处于激活状态时,应该复制行范围。
一个朴素的做法是:如果 region 处于激活状态,就用 region-beginning 和 region-end 所在的行号。不过这并不像看起来那么简单。问题在于,如果 region-end 位于行首,实际上最好不要把那一行算进去。例如,如果缓冲区内容是这样(point 和 mark 都用星号表示):
Lorem ipsum *dolor sit amet, consectetur *adipiscing elit
范围应该是 2–3,而不是 2–4,因为实际高亮的只有第 2 行和第 3 行。count-words-region 就是这么做的。查看 count-lines(它被 count-words-region 调用)可以发现这段代码:
(if (bolp)
(1- (line-number-at-pos))
(line-number-at-pos))
所以我想在我的函数里也这么做。
顺便,我想修掉自己在这个命令里引入的几个 bug。问题是它假设自己运行在文件缓冲区或 Dired 缓冲区中,并且位于某个项目内(比如一个 Git 仓库)。我经常不小心在比如显示文件旧版本的 Magit 缓冲区里运行它,甚至在没有对应文件的临时缓冲区里运行。(我原本以为前一种情况下把 commit hash 包含进去会很有用。)另外,它在版本控制仓库之外、或者(一般来说)项目之外就无法工作。
(defun mbork-copy-current-location (no-line-number)
"Show the current location and put it into the kill ring.
Here, \"location\" means the filename and line number (after a colon) or
a line number range (if the region is active). Use the filename
relative to the parent of the current project root directory, so it
starts with the main project dir. With \\[universal-argument], the line
number is omitted."
(interactive "P")
(let* ((current-project (project-current))
(project-files (if current-project
(project-files current-project)
'()))
(file-name (if current-project
(file-relative-name
(or buffer-file-name
dired-directory
(when (bound-and-true-p magit-buffer-file-name)
(concat magit-buffer-file-name ":" magit-buffer-revision))
(buffer-name))
(file-name-concat (project-root current-project)
".."))
(abbreviate-file-name (or buffer-file-name (buffer-name)))))
(file-base-name (file-name-nondirectory file-name))
(count (length (cl-remove-if-not
(lambda (filename)
(string= file-base-name
(file-name-nondirectory filename)))
project-files)))
(location (if (use-region-p)
(format "%s-%s"
(line-number-at-pos (region-beginning) t)
(save-excursion
(goto-char (region-end))
(if (bolp)
(1- (line-number-at-pos nil t))
(line-number-at-pos nil t))))
(line-number-at-pos nil t)))
(location-full
(format (if (or no-line-number (eq major-mode 'dired-mode)) "%s" "%s:%s")
(if (= count 1)
file-base-name
file-name)
location)))
(kill-new location-full)
(message location-full)))
现在我得承认,我在这里确实用了 LLM。声明一下:它没有写任何代码,只是我请它审阅我写的内容。(如果你是怀疑 LLM 的那一派,别担心;顺便说一句,我博客上的所有代码都是手写的,除非特别说明。)我得承认,它的审阅比我自己的要细致得多。比如,我完全没想到它在“合成”的 Dired 缓冲区里(也就是你不给它列目录内容,而是给 dired 函数一个文件名列表的情况)无法正常工作;也没想到如果我把一个非文件缓冲区命名为以斜杠开头,它会把那当成一个(可能不存在的)路径,然后做出奇怪的事情(你可以自己试试!);还有如果 region 处于激活状态但完全位于一行之内,我会得到类似 file:1337-1337 这样的结果。但这些是我并不太在意的极端边界情况——在一个交互式的便利命令里,为它们做修复其实不太值得。即使偶尔遇到报错或者荒谬的值,也无所谓。
那么,这里的教训是什么?我想有几个。首先,写交互式命令时,最好考虑基本的情况,比如带前缀参数调用时它会做什么,或者 region 处于激活状态时它会做什么。第二点是,在你真正遇到之前,你大概不会在意那些边角情况——Emacs 是一台庞大而复杂的机器,你的命令在某个你甚至不知道存在的状态下求值时,可能会出错。
好了,今天就到这里——和往常一样,happy hacking!
CategoryEnglish, CategoryBlog, CategoryEmacs
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。