/
misc.el
223 lines (213 loc) · 9.31 KB
/
misc.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
;;; custom/git/autoload/misc.el -*- lexical-binding: t; -*-
;; Shared by eleven_cupfuls on Reddit
;; https://www.reddit.com/r/emacs/comments/w9p2oo/resetting_dirlocalsel/ii2kn47/
;;;###autoload
(defun +reload-dir-locals (proj)
"Read values from the current project's .dir-locals file and
apply them in all project file buffers as if opening those files
for the first time.
Signals an error if there is no current project."
(interactive (list (project-current)))
(unless proj
(user-error "There doesn't seem to be a project here"))
;; Load the variables; they are stored buffer-locally, so...
(hack-dir-local-variables)
;; Hold onto them...
(let ((locals dir-local-variables-alist))
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(when (and (equal proj (project-current))
buffer-file-name)
;; transfer the loaded values to this buffer...
(setq-local dir-local-variables-alist locals)
;; and apply them.
(hack-local-variables-apply))))))
;;;###autoload
(defun bisect-github-url (url)
"Returns plist with parts of GitHub URL."
;; different kinds of GH links, for future reference:
;;
;; plain: https://github.com/fniessen/refcard-org-mode
;; issues: https://github.com/fniessen/refcard-org-mode/issues
;; PRs: https://github.com/fniessen/refcard-org-mode/pulls
;; an issue: https://github.com/fniessen/refcard-org-mode/issues/7
;; a PR: https://github.com/fniessen/refcard-org-mode/pull/5
;; a file: https://github.com/fniessen/refcard-org-mode/blob/master/images/org-mode-unicorn.png
;; a diff: https://github.com/advthreat/iroh/pull/7317/files#diff-3edf99653c7afaf324e036f0df597c2322e4fdfbb0f266bc6370f747e1e51bb4
;; comparison: https://github.com/advthreat/iroh/compare/master...1.78-proposal
;;
(let* ((seg "\\([A-z,0-9,._~!$&'()*+,;=:@%-]+\\)")
(bare-rx (concat "\\(https\\:\\/\\/github.com\\)\\/" seg "\\/" seg))
(file-rx (concat bare-rx "\\/blob\\/" seg "\\/\\(.*\\)"))
(issue-rx (concat "\\(https\\:\\/\\/github.com\\)\\/" seg
"\\/" seg "\\/issues\\/\\([0-9]+\\)" ))
(pr-rx (concat "\\(https\\:\\/\\/github.com\\)\\/" seg
"\\/" seg "\\/pull\\/\\([0-9]+\\)"))
(type (if (not (string-match-p bare-rx url))
(error "Is that a GitHub url?\n%s" url)
(cond ((string-match file-rx url) 'file)
((string-match issue-rx url) 'issue)
((string-match pr-rx url) 'pull)
((string-match bare-rx url) 'bare)))))
(list
:forge (match-string 1 url)
:org (match-string 2 url)
:repo (match-string 3 url)
:ref (when (eq type 'file)
(match-string 4 url))
:issue (when (eq type 'issue)
(match-string 4 url))
:pull (when (eq type 'pull)
(match-string 4 url))
:path (match-string 5 url)
:ext (ignore-errors
(replace-regexp-in-string
".*\\.\\(.*\\)#.*" "\\1"
(match-string 5 url)))
:line (ignore-errors
(let* ((fname (match-string 5 url))
(re ".*#L\\([0-9]+\\).*"))
(when (string-match re fname)
(replace-regexp-in-string
re "\\1" fname)))))))
;;;###autoload
(defun +fetch-github-raw-file (url)
"Open the raw file of a GitHub URL.
If URL is a link to a file, it extracts its raw form and tries to open in a buffer."
(interactive)
(let* ((parts (bisect-github-url url))
(raw-url (thread-last
url
(replace-regexp-in-string
"https://github.com/"
"https://raw.githubusercontent.com/")
(replace-regexp-in-string "blob/" "")))
(path (plist-get parts :path))
(bufname (format
"%s/%s/%s | %s"
(plist-get parts :org)
(plist-get parts :repo)
(plist-get parts :ref)
(plist-get parts :path)))
(mode (thread-first
(concat "." (plist-get parts :ext))
(assoc auto-mode-alist #'string-match-p)
(cdr))))
(when path
(request raw-url
:sync t
:headers `(("Authorization"
. ,(format
"Token %s"
(auth-source-pick-first-password
:host "api.github.com"))))
:parser 'buffer-string
:complete (cl-function
(lambda (&key data &allow-other-keys)
(when data
(with-current-buffer (get-buffer-create bufname)
(erase-buffer)
(insert data)
(funcall mode)
(when-let* ((line (plist-get parts :line))
(line-num (1- (string-to-number line))))
(goto-char (point-min))
(forward-line line-num))
(switch-to-buffer-other-window
(current-buffer))))))))))
;;;###autoload
(defun forge-visit-topic-via-url (&optional url)
"Opens Forge Topic buffer or the raw file, based on GitHub URL."
(interactive)
(let* ((url (or url
;; basic
(thing-at-point-url-at-point)
;; org-link
(thread-last
(org-element-lineage (org-element-context) '(link) t)
(org-element-property :raw-link))
;; markdown link
(nth 3 (markdown-link-at-pos (point)))
;; bug-reference
(when-let* ((o (car (overlays-at (point)))))
(overlay-get o 'bug-reference-url))))
(parts (bisect-github-url url))
(issue (plist-get parts :issue))
(pr (plist-get parts :pull))
(owner (plist-get parts :org))
(repo-name (plist-get parts :repo))
(ext (plist-get parts :ext)))
(cond
((or issue pr)
(let* ((topic-num (string-to-number (or issue pr "")))
(repo-url (format "%s/%s/%s" (plist-get parts :forge) owner repo-name))
(repo (or (ignore-errors (forge-get-repository repo-url t t))
(forge-get-repository repo-url nil 'create)))
(topic
;; This one is tricky:
;; - first, it tries to find the topic in forge db
;; - if that fails, it fetches the topic and updates the db
;; - and then grabs the topic from the db
;;
;; Since fetching fns are async with a callback, have to wrap it into a deferred
(or
(forge-get-topic repo topic-num)
(deferred:sync!
(deferred:$
(deferred:next
(lambda ()
(let* ((d (deferred:new #'identity))
(fetch-fn (if issue 'ghub-fetch-issue
'ghub-fetch-pullreq))
(update-fn (if issue 'forge--update-issue
'forge--update-pullreq)))
(funcall
fetch-fn
owner repo-name topic-num
(lambda (data)
(funcall update-fn repo data nil)
(deferred:callback-post
d
(forge-get-topic repo topic-num))))
d))))))))
;; otherwise it complains for not running inside a git repo
(cl-letf (((symbol-function #'magit-toplevel)
(lambda () default-directory))
(magit-display-buffer-function
(lambda (buf)
(get-buffer-window
(pop-to-buffer buf)))))
(forge-topic-setup-buffer topic))))
(ext (+fetch-github-raw-file url)))))
;;;###autoload
(defun magit-transient-unblock-global-keys ()
"Enable/unblock <M-x>, <M-:>, <C-h k>, etc. keys in Magit transients."
(dolist (sfx (transient-suffixes 'magit-dispatch))
(when-let ((cmd (transient--suffix-command sfx)))
(ignore-errors
(transient-append-suffix cmd '(0)
[:hide always
:setup-children
(lambda (_)
(list
(transient-parse-suffix
'magit-remote
[("M-x" "M-x" execute-extended-command :transient t)
("M-:" "M-:" eval-expression :transient t)
("C-h k" "describe key"
(lambda ()
(interactive)
(let ((transient-hide-during-minibuffer-read nil))
(call-interactively (keymap-global-lookup "C-h k"))))
:transient t)])))])))))
;;;###autoload
(defun transient-export-content ()
"Open content of the current transient in a buffer."
(interactive)
(when-let ((trans (get-buffer transient--buffer-name))
(b (generate-new-buffer
(format "%S transient"
transient-current-command))))
(with-current-buffer b
(insert-buffer-substring-no-properties trans)
(switch-to-buffer-other-window b))))