org-link-edit.el 12 KB

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