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:
parent
3398edc33b
commit
515ef3401f
1 changed files with 15 additions and 9 deletions
|
@ -31,21 +31,26 @@ Sure, I try to keep my text editing world /inside/ the internal consistency and
|
||||||
* Into the Clipboard
|
* 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, we’ll make some best guesses:
|
[[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, we’ll make some best guesses:
|
||||||
#+begin_src emacs-lisp
|
#+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.
|
"Show the current location and put it into the kill ring.
|
||||||
Use the filename relative to the current projectile root directory.
|
Use the filename relative to the current projectile root directory.
|
||||||
If called non-interactively, return the location as a string."
|
If called non-interactively, return the location as a string."
|
||||||
(interactive)
|
(interactive "P")
|
||||||
(let* ((project-name (projectile-project-name))
|
(let* ((project-name (projectile-project-name))
|
||||||
(file-name (when (and buffer-file-name (projectile-project-root))
|
(file-name (when (and buffer-file-name (projectile-project-root))
|
||||||
(file-relative-name 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
|
(location (cond
|
||||||
((and project-name file-name)
|
((and project-name file-name line-number)
|
||||||
(format "%s :: %s : %s" 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))
|
(format "%s : %d" file-name line-number))
|
||||||
|
(file-name file-name)
|
||||||
(project-name
|
(project-name
|
||||||
(format "project: %s" project-name))
|
(format "project: %s" project-name))
|
||||||
(t ""))))
|
(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.
|
"Copy the active region along with a location header to kill ring.
|
||||||
Calls `current-location' to get the header."
|
Calls `current-location' to get the header."
|
||||||
(interactive "r")
|
(interactive "r")
|
||||||
|
(let ((location (current-location start)))
|
||||||
(kill-new
|
(kill-new
|
||||||
(format "From %s …\n%s" (current-location start) (buffer-substring-no-properties start end)))
|
(format "From %s …\n%s" location (buffer-substring-no-properties start end)))
|
||||||
(message "Copied code to clipboard, %s" (current-location start)))
|
(message "Copied code to clipboard, %s" location)))
|
||||||
#+end_src
|
#+end_src
|
||||||
And if there is no active region, let’s 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):
|
And if there is no active region, let’s 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
|
#+begin_src emacs-lisp
|
||||||
|
|
Loading…
Reference in a new issue