Giving a prefix to current location to add line number

Should the `SPC-u` prefix add the line number or keep it off? Not
sure, but since my copy-code-dwim adds the line number, I'm assuming
that I normally don't want to bother with the line number.

Thanks to http://mbork.pl/2022-08-08_Copying_the_current_location_revisited
for giving me more to this idea.
This commit is contained in:
Howard Abrams 2022-08-09 09:49:27 -07:00
parent 3398edc33b
commit 515ef3401f

View file

@ -31,21 +31,26 @@ Sure, I try to keep my text editing world /inside/ the internal consistency and
* Into the Clipboard
[[http://mbork.pl/2022-06-20_Copying_the_current_location][This essay]] from =mbork= has an interesting idea of being able to select code, and copy it to the clipboard with /extra information/ about the location. The location is the filename (relative to the project), as well as the project name and line number, but since every buffer may not have all this information, well make some best guesses:
#+begin_src emacs-lisp
(defun current-location (&optional start-line)
(defun current-location (start-line)
"Show the current location and put it into the kill ring.
Use the filename relative to the current projectile root directory.
If called non-interactively, return the location as a string."
(interactive)
(interactive "P")
(let* ((project-name (projectile-project-name))
(file-name (when (and buffer-file-name (projectile-project-root))
(file-relative-name buffer-file-name (projectile-project-root))))
(line-number (or start-line (line-number-at-pos nil t)))
(line-number (if (and (called-interactively-p) start-line)
(line-number-at-pos nil t)
start-line))
(location (cond
((and project-name file-name)
((and project-name file-name line-number)
(format "%s :: %s : %s" project-name file-name line-number))
(file-name
((and project-name file-name)
(format "%s :: %s" project-name file-name))
((and file-name line-number)
(format "%s : %d" file-name line-number))
(file-name file-name)
(project-name
(format "project: %s" project-name))
(t ""))))
@ -60,9 +65,10 @@ Use the =current-location= function, along with the /region/ or /function/ and c
"Copy the active region along with a location header to kill ring.
Calls `current-location' to get the header."
(interactive "r")
(let ((location (current-location start)))
(kill-new
(format "From %s …\n%s" (current-location start) (buffer-substring-no-properties start end)))
(message "Copied code to clipboard, %s" (current-location start)))
(format "From %s …\n%s" location (buffer-substring-no-properties start end)))
(message "Copied code to clipboard, %s" location)))
#+end_src
And if there is no active region, lets attempt to copy the /function/ (whatever that may mean), that we get with the [[help:which-function-mode][which-function-mode]] (see [[https://www.emacswiki.org/emacs/WhichFuncMode][the Emacs wiki]] for details):
#+begin_src emacs-lisp