Skip to content

Commit d7d8f1e

Browse files
committed
[emacs] github-url: use start and end if region active
1 parent 8631b0a commit d7d8f1e

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

  • emacs/.config/emacs/lisp

emacs/.config/emacs/lisp/web.el

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
(line-number-at-pos))))))
3535

3636
(defun meain/github-url (&optional use-master)
37-
"Link to the currently selected code in GitHub. Pass `USE-MASTER' to use master branch."
37+
"Link to the currently selected code in GitHub. Pass `USE-MASTER' to use master branch.
38+
If region is active, link covers the region."
3839
(interactive "P")
3940
(save-restriction
4041
(widen)
@@ -49,7 +50,6 @@
4950
1)
5051
(user-error "Current latest commit not available upstream")))
5152

52-
5353
(let* ((git-url (replace-regexp-in-string
5454
"^git@github.com:\\(.*\\)\\.git$" "https://github.com/\\1"
5555
(meain/cmd-head "git config --get remote.origin.url")))
@@ -60,8 +60,15 @@
6060
(meain/cmd-head "git log --format='%H' -n 1")))
6161
(file-path (file-relative-name (or buffer-file-name default-directory)
6262
(car (project-roots (project-current)))))
63-
(line-frag (unless (equal major-mode 'dired-mode)
64-
(format "#L%s" (line-number-at-pos))))
63+
(line-frag
64+
(unless (equal major-mode 'dired-mode)
65+
(if (use-region-p)
66+
(let* ((start (line-number-at-pos (region-beginning)))
67+
(end (1- (line-number-at-pos (region-end)))))
68+
(if (= start end)
69+
(format "#L%s" start)
70+
(format "#L%s-L%s" start end)))
71+
(format "#L%s" (line-number-at-pos)))))
6572
(web-url (concat git-url "/blob/" git-ref "/" file-path (or line-frag ""))))
6673
(message "%s copied to clipboard." web-url)
6774
(meain/copy-to-clipboard web-url))))

0 commit comments

Comments
 (0)