Last year I improved my code for copying the current location to the kill ring. Recently it occurred to me that this was not the last word. In fact, I’m pretty surprised I didn’t think of it earlier – it’s sort of obvious in hindsight. The idea is that it should copy the range of lines when the region is active.
A naive way is to use the line number of region-beginning and region-end if the region is active. This is not as simple as it looks, though. The problem is that if region-end is at the beginning of line, it’s actually better not to count that last line. For example, if the buffer looks like this (both point and mark are denoted by an asterisk):
Lorem ipsum *dolor sit amet, consectetur *adipiscing elit
the range should be 2–3, not 2–4, since only lines 2 and 3 are actually highlighted. This is how count-words-region works. Looking at count-lines (which is called by count-words-region) reveals this snippet:
(if (bolp)
(1- (line-number-at-pos))
(line-number-at-pos))
so I’d like to do the same in my function.
While at that, I thought that I could fix a few bugs I introduced in my command. The problem was that it assumed that it was ran in either a file buffer or a Dired buffer, and within a project (for example, a Git repo). I often accidentally run it in e.g. Magit buffers showing an old version of a file or even temporary buffers with no backing files. (I thought that including the commit hash in the former case would be useful.) Also, it didn’t work outside a version control repo or (in general) a project.
(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)))
Now I have to admit that I did use an LLM here. Disclaimer: it didn’t write any code, but I asked it to review what I have written. (If you are in the LLM-skeptical camp, don’t worry; all code on my blog is hand-written unless explicitly stated, by the way.) I have to admit that its review was much more thorough than what I would do. For example, it never occurred to me that it won’t work correctly in a “synthetic” Dired buffer (where instead of listing the contents of a directory you give the dired function a list of file names), or that if I named a non-file buffer with a slash at the beginning, it would treat that as a (possibly non-existent) path and do weird things (try it for yourself!), or that if a region is active but contained within one line, I’d get something like file:1337-1337. But those are extreme edge cases I don’t really care about that much – it’s not really worth fixing them in a function which is an interactive, convenience command. Even if I get an error or an absurd value once in a while, it just doesn’t matter.
So, what’s the lesson here? I guess there are a few. First of all, when you write interactive commands, it’s good to think about basic cases, like what it is going to do when invoked with a prefix argument or what it is going to do when the region is active. The second thing is that you probably don’t care about corner cases until you really run into them – Emacs is a huge, complicated machine, and your command might error out when evaluated in a state you never knew even existed.
Anyway, that’s it for today – happy hacking as usual!