org-link-edit.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. ;;; org-link-edit.el --- Slurp and barf with Org links -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015-2020 Kyle Meyer <kyle@kyleam.com>
  3. ;; Author: Kyle Meyer <kyle@kyleam.com>
  4. ;; URL: https://git.kyleam.com/org-link-edit/about
  5. ;; Keywords: convenience
  6. ;; Version: 1.2.1
  7. ;; Package-Requires: ((cl-lib "0.5") (org "9.3"))
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; This program is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org Link Edit provides Paredit-inspired slurping and barfing
  20. ;; commands for Org link descriptions.
  21. ;;
  22. ;; There are four slurp and barf commands, all which operate when
  23. ;; point is on an Org link.
  24. ;;
  25. ;; - org-link-edit-forward-slurp
  26. ;; - org-link-edit-backward-slurp
  27. ;; - org-link-edit-forward-barf
  28. ;; - org-link-edit-backward-barf
  29. ;;
  30. ;; Org Link Edit doesn't bind these commands to any keys. Finding
  31. ;; good keys for these commands is difficult because, while it's
  32. ;; convenient to be able to quickly repeat these commands, they won't
  33. ;; be used frequently enough to be worthy of a short, repeat-friendly
  34. ;; binding. Using Hydra [1] provides a nice solution to this. After
  35. ;; an initial key sequence, any of the commands will be repeatable
  36. ;; with a single key. (Plus, you get a nice interface that displays
  37. ;; the key for each command.) Below is one example of how you could
  38. ;; configure this.
  39. ;;
  40. ;; (define-key org-mode-map YOUR-KEY
  41. ;; (defhydra hydra-org-link-edit ()
  42. ;; "Org Link Edit"
  43. ;; ("j" org-link-edit-forward-slurp "forward slurp")
  44. ;; ("k" org-link-edit-forward-barf "forward barf")
  45. ;; ("u" org-link-edit-backward-slurp "backward slurp")
  46. ;; ("i" org-link-edit-backward-barf "backward barf")
  47. ;; ("q" nil "cancel")))
  48. ;;
  49. ;; In addition to the slurp and barf commands, the command
  50. ;; `org-link-edit-transport-next-link' searches for the next (or
  51. ;; previous) link and moves it to point, using the word at point or
  52. ;; the selected region as the link's description.
  53. ;;
  54. ;; [1] https://github.com/abo-abo/hydra
  55. ;;; Code:
  56. (require 'org)
  57. (require 'org-element)
  58. (require 'cl-lib)
  59. (defun org-link-edit--on-link-p (&optional element)
  60. (org-element-lineage (or element (org-element-context)) '(link) t))
  61. (defun org-link-edit--link-data ()
  62. "Return list with information about the link at point.
  63. The list includes
  64. - the position at the start of the link
  65. - the position at the end of the link
  66. - the link text
  67. - the link description (nil when on a plain link)"
  68. (let ((el (org-element-context)))
  69. (unless (org-link-edit--on-link-p el)
  70. (user-error "Point is not on a link"))
  71. (save-excursion
  72. (goto-char (org-element-property :begin el))
  73. (cond
  74. ;; Use match-{beginning,end} because match-end is consistently
  75. ;; positioned after ]], while the :end property is positioned
  76. ;; at the next word on the line, if one is present.
  77. ((looking-at org-link-bracket-re)
  78. (list (match-beginning 0)
  79. (match-end 0)
  80. (save-match-data
  81. (org-link-unescape (match-string-no-properties 1)))
  82. (or (match-string-no-properties 2) "")))
  83. ((looking-at org-link-plain-re)
  84. (list (match-beginning 0)
  85. (match-end 0)
  86. (match-string-no-properties 0)
  87. nil))
  88. (t
  89. (error "What am I looking at?"))))))
  90. (defun org-link-edit--forward-blob (n &optional no-punctuation)
  91. "Move forward N blobs (backward if N is negative).
  92. A block of non-whitespace characters is a blob. If
  93. NO-PUNCTUATION is non-nil, trailing punctuation characters are
  94. not considered part of the blob when going in the forward
  95. direction.
  96. If the edge of the buffer is reached before completing the
  97. movement, return nil. Otherwise, return t."
  98. (let* ((forward-p (> n 0))
  99. (nblobs (abs n))
  100. (skip-func (if forward-p 'skip-syntax-forward 'skip-syntax-backward))
  101. skip-func-retval)
  102. (while (/= nblobs 0)
  103. (funcall skip-func " ")
  104. (setq skip-func-retval (funcall skip-func "^ "))
  105. (setq nblobs (1- nblobs)))
  106. (when (and forward-p no-punctuation)
  107. (let ((punc-tail-offset (save-excursion (skip-syntax-backward "."))))
  108. ;; Don't consider trailing punctuation as part of the blob
  109. ;; unless the whole blob consists of punctuation.
  110. (unless (= skip-func-retval (- punc-tail-offset))
  111. (goto-char (+ (point) punc-tail-offset)))))
  112. (/= skip-func-retval 0)))
  113. ;;;###autoload
  114. (defun org-link-edit-forward-slurp (&optional n)
  115. "Slurp N trailing blobs into link's description.
  116. The \[\[https://orgmode.org/\]\[Org mode\]\] site
  117. |
  118. v
  119. The \[\[https://orgmode.org/\]\[Org mode site\]\]
  120. A blob is a block of non-whitespace characters. When slurping
  121. forward, trailing punctuation characters are not considered part
  122. of a blob.
  123. After slurping, return the slurped text and move point to the
  124. beginning of the link.
  125. If N is negative, slurp leading blobs instead of trailing blobs."
  126. (interactive "p")
  127. (setq n (or n 1))
  128. (cond
  129. ((= n 0))
  130. ((< n 0)
  131. (org-link-edit-backward-slurp (- n)))
  132. (t
  133. (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data)
  134. (goto-char (save-excursion
  135. (goto-char end)
  136. (or (org-link-edit--forward-blob n 'no-punctuation)
  137. (user-error "Not enough blobs after the link"))
  138. (point)))
  139. (let ((slurped (buffer-substring-no-properties end (point))))
  140. (setq slurped (replace-regexp-in-string "\n+" " " slurped))
  141. (when (and (= (length desc) 0)
  142. (string-match "^\\s-+\\(.*\\)" slurped))
  143. (setq slurped (match-string 1 slurped)))
  144. (setq desc (concat desc slurped)
  145. end (+ end (length slurped)))
  146. (delete-region beg (point))
  147. (insert (org-link-make-string link desc))
  148. (goto-char beg)
  149. slurped)))))
  150. ;;;###autoload
  151. (defun org-link-edit-backward-slurp (&optional n)
  152. "Slurp N leading blobs into link's description.
  153. The \[\[https://orgmode.org/\]\[Org mode\]\] site
  154. |
  155. v
  156. \[\[https://orgmode.org/\]\[The Org mode\]\] site
  157. A blob is a block of non-whitespace characters.
  158. After slurping, return the slurped text and move point to the
  159. beginning of the link.
  160. If N is negative, slurp trailing blobs instead of leading blobs."
  161. (interactive "p")
  162. (setq n (or n 1))
  163. (cond
  164. ((= n 0))
  165. ((< n 0)
  166. (org-link-edit-forward-slurp (- n)))
  167. (t
  168. (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data)
  169. (goto-char (save-excursion
  170. (goto-char beg)
  171. (or (org-link-edit--forward-blob (- n))
  172. (user-error "Not enough blobs before the link"))
  173. (point)))
  174. (let ((slurped (buffer-substring-no-properties (point) beg)))
  175. (when (and (= (length desc) 0)
  176. (string-match "\\(.*\\)\\s-+$" slurped))
  177. (setq slurped (match-string 1 slurped)))
  178. (setq slurped (replace-regexp-in-string "\n+" " " slurped))
  179. (setq desc (concat slurped desc)
  180. beg (- beg (length slurped)))
  181. (delete-region (point) end)
  182. (insert (org-link-make-string link desc))
  183. (goto-char beg)
  184. slurped)))))
  185. (defun org-link-edit--split-first-blobs (string n)
  186. "Split STRING into (N first blobs . other) cons cell.
  187. 'N first blobs' contains all text from the start of STRING up to
  188. the start of the N+1 blob. 'other' includes the remaining text
  189. of STRING. If the number of blobs in STRING is fewer than N,
  190. 'other' is nil."
  191. (when (< n 0) (user-error "N cannot be negative"))
  192. (with-temp-buffer
  193. (insert string)
  194. (goto-char (point-min))
  195. (with-syntax-table org-mode-syntax-table
  196. (let ((within-bound (org-link-edit--forward-blob n)))
  197. (skip-syntax-forward " ")
  198. (cons (buffer-substring 1 (point))
  199. (and within-bound
  200. (buffer-substring (point) (point-max))))))))
  201. (defun org-link-edit--split-last-blobs (string n)
  202. "Split STRING into (other . N last blobs) cons cell.
  203. 'N last blobs' contains all text from the end of STRING back to
  204. the end of the N+1 last blob. 'other' includes the remaining
  205. text of STRING. If the number of blobs in STRING is fewer than
  206. N, 'other' is nil."
  207. (when (< n 0) (user-error "N cannot be negative"))
  208. (with-temp-buffer
  209. (insert string)
  210. (goto-char (point-max))
  211. (with-syntax-table org-mode-syntax-table
  212. (let ((within-bound (org-link-edit--forward-blob (- n))))
  213. (skip-syntax-backward " ")
  214. (cons (and within-bound
  215. (buffer-substring 1 (point)))
  216. (buffer-substring (point) (point-max)))))))
  217. ;;;###autoload
  218. (defun org-link-edit-forward-barf (&optional n)
  219. "Barf N trailing blobs from link's description.
  220. The \[\[https://orgmode.org/\]\[Org mode\]\] site
  221. |
  222. v
  223. The \[\[https://orgmode.org/\]\[Org\]\] mode site
  224. A blob is a block of non-whitespace characters.
  225. After barfing, return the barfed text and move point to the
  226. beginning of the link.
  227. If N is negative, barf leading blobs instead of trailing blobs."
  228. (interactive "p")
  229. (setq n (or n 1))
  230. (cond
  231. ((= n 0))
  232. ((< n 0)
  233. (org-link-edit-backward-barf (- n)))
  234. (t
  235. (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data)
  236. (when (= (length desc) 0)
  237. (user-error "Link has no description"))
  238. (pcase-let ((`(,new-desc . ,barfed) (org-link-edit--split-last-blobs
  239. desc n)))
  240. (unless new-desc (user-error "Not enough blobs in description"))
  241. (goto-char beg)
  242. (delete-region beg end)
  243. (insert (org-link-make-string link new-desc))
  244. (when (string= new-desc "")
  245. (setq barfed (concat " " barfed)))
  246. (insert barfed)
  247. (goto-char beg)
  248. barfed)))))
  249. ;;;###autoload
  250. (defun org-link-edit-backward-barf (&optional n)
  251. "Barf N leading blobs from link's description.
  252. The \[\[https://orgmode.org/\]\[Org mode\]\] site
  253. |
  254. v
  255. The Org \[\[https://orgmode.org/\]\[mode\]\] site
  256. A blob is a block of non-whitespace characters.
  257. After barfing, return the barfed text and move point to the
  258. beginning of the link.
  259. If N is negative, barf trailing blobs instead of leading blobs."
  260. (interactive "p")
  261. (setq n (or n 1))
  262. (cond
  263. ((= n 0))
  264. ((< n 0)
  265. (org-link-edit-forward-barf (- n)))
  266. (t
  267. (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data)
  268. (when (= (length desc) 0)
  269. (user-error "Link has no description"))
  270. (pcase-let ((`(,barfed . ,new-desc) (org-link-edit--split-first-blobs
  271. desc n)))
  272. (unless new-desc (user-error "Not enough blobs in description"))
  273. (goto-char beg)
  274. (delete-region beg end)
  275. (insert (org-link-make-string link new-desc))
  276. (when (string= new-desc "")
  277. (setq barfed (concat barfed " ")))
  278. (goto-char beg)
  279. (insert barfed)
  280. barfed)))))
  281. (defun org-link-edit--next-link-data (&optional previous)
  282. (save-excursion
  283. (if (funcall (if previous #'re-search-backward #'re-search-forward)
  284. org-link-any-re nil t)
  285. (org-link-edit--link-data)
  286. (user-error "No %s link found" (if previous "previous" "next")))))
  287. ;;;###autoload
  288. (defun org-link-edit-transport-next-link (&optional previous beg end overwrite)
  289. "Move the next link to point.
  290. If the region is active, use the selected text as the link's
  291. description. Otherwise, use the word at point.
  292. With prefix argument PREVIOUS, move the previous link instead of
  293. the next link.
  294. Non-interactively, use the text between BEG and END as the
  295. description, moving the next (or previous) link relative to BEG
  296. and END. By default, refuse to overwrite an existing
  297. description. If OVERWRITE is `ask', prompt for confirmation
  298. before overwriting; for any other non-nil value, overwrite
  299. without asking."
  300. (interactive `(,current-prefix-arg
  301. ,@(if (use-region-p)
  302. (list (region-beginning) (region-end))
  303. (list nil nil))
  304. ask))
  305. (let ((pt (point))
  306. (desc-bounds (cond
  307. ((and beg end)
  308. (cons (progn (goto-char beg)
  309. (point-marker))
  310. (progn (goto-char end)
  311. (point-marker))))
  312. ((not (looking-at-p "\\s-"))
  313. (progn (skip-syntax-backward "w")
  314. (let ((beg (point-marker)))
  315. (skip-syntax-forward "w")
  316. (cons beg (point-marker))))))))
  317. (when (or (and desc-bounds
  318. (or (progn (goto-char (car desc-bounds))
  319. (org-link-edit--on-link-p))
  320. (progn (goto-char (cdr desc-bounds))
  321. (org-link-edit--on-link-p))))
  322. (progn (goto-char pt)
  323. (org-link-edit--on-link-p)))
  324. (user-error "Cannot transport next link with point on a link"))
  325. (goto-char (or (car desc-bounds) pt))
  326. (cl-multiple-value-bind (link-beg link-end link orig-desc)
  327. (org-link-edit--next-link-data previous)
  328. (unless (or (not desc-bounds)
  329. (= (length orig-desc) 0)
  330. (if (eq overwrite 'ask)
  331. (y-or-n-p "Overwrite existing description?")
  332. overwrite))
  333. (user-error "Link already has a description"))
  334. (delete-region link-beg link-end)
  335. (insert (org-link-make-string
  336. link
  337. (if desc-bounds
  338. (delete-and-extract-region (car desc-bounds)
  339. (cdr desc-bounds))
  340. orig-desc))))))
  341. (provide 'org-link-edit)
  342. ;;; org-link-edit.el ends here