org-link-edit.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. ;;; org-link-edit.el --- Slurp and barf with Org links -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015-2017 Kyle Meyer <kyle@kyleam.com>
  3. ;; Author: Kyle Meyer <kyle@kyleam.com>
  4. ;; URL: https://gitlab.com/kyleam/org-link-edit
  5. ;; Keywords: convenience
  6. ;; Version: 1.1.1
  7. ;; Package-Requires: ((cl-lib "0.5") (org "8.2.10"))
  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 <http://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. (let ((el (or element (org-element-context))))
  61. ;; Don't use `org-element-lineage' because it isn't available
  62. ;; until Org version 8.3.
  63. (while (and el (not (memq (car el) '(link))))
  64. (setq el (org-element-property :parent el)))
  65. (eq (car el) 'link)))
  66. (defun org-link-edit--link-data ()
  67. "Return list with information about the link at point.
  68. The list includes
  69. - the position at the start of the link
  70. - the position at the end of the link
  71. - the link text
  72. - the link description (nil when on a plain link)"
  73. (let ((el (org-element-context)))
  74. (unless (org-link-edit--on-link-p el)
  75. (user-error "Point is not on a link"))
  76. (save-excursion
  77. (goto-char (org-element-property :begin el))
  78. (cond
  79. ;; Use match-{beginning,end} because match-end is consistently
  80. ;; positioned after ]], while the :end property is positioned
  81. ;; at the next word on the line, if one is present.
  82. ((looking-at org-bracket-link-regexp)
  83. (list (match-beginning 0)
  84. (match-end 0)
  85. (save-match-data
  86. (org-link-unescape (match-string-no-properties 1)))
  87. (or (match-string-no-properties 2) "")))
  88. ((looking-at org-plain-link-re)
  89. (list (match-beginning 0)
  90. (match-end 0)
  91. (match-string-no-properties 0)
  92. nil))
  93. (t
  94. (error "What am I looking at?"))))))
  95. (defun org-link-edit--forward-blob (n &optional no-punctuation)
  96. "Move forward N blobs (backward if N is negative).
  97. A block of non-whitespace characters is a blob. If
  98. NO-PUNCTUATION is non-nil, trailing punctuation characters are
  99. not considered part of the blob when going in the forward
  100. direction.
  101. If the edge of the buffer is reached before completing the
  102. movement, return nil. Otherwise, return t."
  103. (let* ((forward-p (> n 0))
  104. (nblobs (abs n))
  105. (skip-func (if forward-p 'skip-syntax-forward 'skip-syntax-backward))
  106. skip-func-retval)
  107. (while (/= nblobs 0)
  108. (funcall skip-func " ")
  109. (setq skip-func-retval (funcall skip-func "^ "))
  110. (setq nblobs (1- nblobs)))
  111. (when (and forward-p no-punctuation)
  112. (let ((punc-tail-offset (save-excursion (skip-syntax-backward "."))))
  113. ;; Don't consider trailing punctuation as part of the blob
  114. ;; unless the whole blob consists of punctuation.
  115. (unless (= skip-func-retval (- punc-tail-offset))
  116. (goto-char (+ (point) punc-tail-offset)))))
  117. (/= skip-func-retval 0)))
  118. ;;;###autoload
  119. (defun org-link-edit-forward-slurp (&optional n)
  120. "Slurp N trailing blobs into link's description.
  121. The \[\[https://orgmode.org/\]\[Org mode\]\] site
  122. |
  123. v
  124. The \[\[https://orgmode.org/\]\[Org mode site\]\]
  125. A blob is a block of non-whitespace characters. When slurping
  126. forward, trailing punctuation characters are not considered part
  127. of a blob.
  128. After slurping, return the slurped text and move point to the
  129. beginning of the link.
  130. If N is negative, slurp leading blobs instead of trailing blobs."
  131. (interactive "p")
  132. (setq n (or n 1))
  133. (cond
  134. ((= n 0))
  135. ((< n 0)
  136. (org-link-edit-backward-slurp (- n)))
  137. (t
  138. (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data)
  139. (goto-char (save-excursion
  140. (goto-char end)
  141. (or (org-link-edit--forward-blob n 'no-punctuation)
  142. (user-error "Not enough blobs after the link"))
  143. (point)))
  144. (let ((slurped (buffer-substring-no-properties end (point))))
  145. (setq slurped (replace-regexp-in-string "\n+" " " slurped))
  146. (when (and (= (length desc) 0)
  147. (string-match "^\\s-+\\(.*\\)" slurped))
  148. (setq slurped (match-string 1 slurped)))
  149. (setq desc (concat desc slurped)
  150. end (+ end (length slurped)))
  151. (delete-region beg (point))
  152. (insert (org-make-link-string link desc))
  153. (goto-char beg)
  154. slurped)))))
  155. ;;;###autoload
  156. (defun org-link-edit-backward-slurp (&optional n)
  157. "Slurp N leading blobs into link's description.
  158. The \[\[https://orgmode.org/\]\[Org mode\]\] site
  159. |
  160. v
  161. \[\[https://orgmode.org/\]\[The Org mode\]\] site
  162. A blob is a block of non-whitespace characters.
  163. After slurping, return the slurped text and move point to the
  164. beginning of the link.
  165. If N is negative, slurp trailing blobs instead of leading blobs."
  166. (interactive "p")
  167. (setq n (or n 1))
  168. (cond
  169. ((= n 0))
  170. ((< n 0)
  171. (org-link-edit-forward-slurp (- n)))
  172. (t
  173. (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data)
  174. (goto-char (save-excursion
  175. (goto-char beg)
  176. (or (org-link-edit--forward-blob (- n))
  177. (user-error "Not enough blobs before the link"))
  178. (point)))
  179. (let ((slurped (buffer-substring-no-properties (point) beg)))
  180. (when (and (= (length desc) 0)
  181. (string-match "\\(.*\\)\\s-+$" slurped))
  182. (setq slurped (match-string 1 slurped)))
  183. (setq slurped (replace-regexp-in-string "\n+" " " slurped))
  184. (setq desc (concat slurped desc)
  185. beg (- beg (length slurped)))
  186. (delete-region (point) end)
  187. (insert (org-make-link-string link desc))
  188. (goto-char beg)
  189. slurped)))))
  190. (defun org-link-edit--split-first-blobs (string n)
  191. "Split STRING into (N first blobs . other) cons cell.
  192. 'N first blobs' contains all text from the start of STRING up to
  193. the start of the N+1 blob. 'other' includes the remaining text
  194. of STRING. If the number of blobs in STRING is fewer than N,
  195. 'other' is nil."
  196. (when (< n 0) (user-error "N cannot be negative"))
  197. (with-temp-buffer
  198. (insert string)
  199. (goto-char (point-min))
  200. (with-syntax-table org-mode-syntax-table
  201. (let ((within-bound (org-link-edit--forward-blob n)))
  202. (skip-syntax-forward " ")
  203. (cons (buffer-substring 1 (point))
  204. (and within-bound
  205. (buffer-substring (point) (point-max))))))))
  206. (defun org-link-edit--split-last-blobs (string n)
  207. "Split STRING into (other . N last blobs) cons cell.
  208. 'N last blobs' contains all text from the end of STRING back to
  209. the end of the N+1 last blob. 'other' includes the remaining
  210. text of STRING. If the number of blobs in STRING is fewer than
  211. N, 'other' is nil."
  212. (when (< n 0) (user-error "N cannot be negative"))
  213. (with-temp-buffer
  214. (insert string)
  215. (goto-char (point-max))
  216. (with-syntax-table org-mode-syntax-table
  217. (let ((within-bound (org-link-edit--forward-blob (- n))))
  218. (skip-syntax-backward " ")
  219. (cons (and within-bound
  220. (buffer-substring 1 (point)))
  221. (buffer-substring (point) (point-max)))))))
  222. ;;;###autoload
  223. (defun org-link-edit-forward-barf (&optional n)
  224. "Barf N trailing blobs from link's description.
  225. The \[\[https://orgmode.org/\]\[Org mode\]\] site
  226. |
  227. v
  228. The \[\[https://orgmode.org/\]\[Org\]\] mode site
  229. A blob is a block of non-whitespace characters.
  230. After barfing, return the barfed text and move point to the
  231. beginning of the link.
  232. If N is negative, barf leading blobs instead of trailing blobs."
  233. (interactive "p")
  234. (setq n (or n 1))
  235. (cond
  236. ((= n 0))
  237. ((< n 0)
  238. (org-link-edit-backward-barf (- n)))
  239. (t
  240. (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data)
  241. (when (= (length desc) 0)
  242. (user-error "Link has no description"))
  243. (pcase-let ((`(,new-desc . ,barfed) (org-link-edit--split-last-blobs
  244. desc n)))
  245. (unless new-desc (user-error "Not enough blobs in description"))
  246. (goto-char beg)
  247. (delete-region beg end)
  248. (insert (org-make-link-string link new-desc))
  249. (when (string= new-desc "")
  250. (setq barfed (concat " " barfed)))
  251. (insert barfed)
  252. (goto-char beg)
  253. barfed)))))
  254. ;;;###autoload
  255. (defun org-link-edit-backward-barf (&optional n)
  256. "Barf N leading blobs from link's description.
  257. The \[\[https://orgmode.org/\]\[Org mode\]\] site
  258. |
  259. v
  260. The Org \[\[https://orgmode.org/\]\[mode\]\] site
  261. A blob is a block of non-whitespace characters.
  262. After barfing, return the barfed text and move point to the
  263. beginning of the link.
  264. If N is negative, barf trailing blobs instead of leading blobs."
  265. (interactive "p")
  266. (setq n (or n 1))
  267. (cond
  268. ((= n 0))
  269. ((< n 0)
  270. (org-link-edit-forward-barf (- n)))
  271. (t
  272. (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data)
  273. (when (= (length desc) 0)
  274. (user-error "Link has no description"))
  275. (pcase-let ((`(,barfed . ,new-desc) (org-link-edit--split-first-blobs
  276. desc n)))
  277. (unless new-desc (user-error "Not enough blobs in description"))
  278. (goto-char beg)
  279. (delete-region beg end)
  280. (insert (org-make-link-string link new-desc))
  281. (when (string= new-desc "")
  282. (setq barfed (concat barfed " ")))
  283. (goto-char beg)
  284. (insert barfed)
  285. barfed)))))
  286. (defun org-link-edit--next-link-data (&optional previous)
  287. (save-excursion
  288. (if (funcall (if previous #'re-search-backward #'re-search-forward)
  289. org-any-link-re nil t)
  290. (org-link-edit--link-data)
  291. (user-error "No %s link found" (if previous "previous" "next")))))
  292. ;;;###autoload
  293. (defun org-link-edit-transport-next-link (&optional previous beg end)
  294. "Move the next link to point.
  295. If the region is active, use the selected text as the link's
  296. description. Otherwise, use the word at point.
  297. With prefix argument PREVIOUS, move the previous link instead of
  298. the next link.
  299. Non-interactively, use the text between BEG and END as the
  300. description, moving the next (or previous) link relative BEG and
  301. END."
  302. (interactive (cons current-prefix-arg
  303. (and (use-region-p)
  304. (list (region-beginning) (region-end)))))
  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) (= (length orig-desc) 0))
  329. (user-error "Link already has a description"))
  330. (delete-region link-beg link-end)
  331. (insert (org-make-link-string
  332. link
  333. (if desc-bounds
  334. (delete-and-extract-region (car desc-bounds)
  335. (cdr desc-bounds))
  336. orig-desc))))))
  337. (provide 'org-link-edit)
  338. ;;; org-link-edit.el ends here