aboutsummaryrefslogtreecommitdiff
path: root/org-link-edit.el
blob: b9c7a8eff421d9c5f4111761cfec309d9af02441 (plain)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
;;; org-link-edit.el --- Slurp and barf with Org links  -*- lexical-binding: t; -*-

;; Copyright (C) 2015-2020 Kyle Meyer <kyle@kyleam.com>

;; Author:  Kyle Meyer <kyle@kyleam.com>
;; URL: https://git.kyleam.com/org-link-edit/about
;; Keywords: convenience
;; Version: 1.2.1
;; Package-Requires: ((cl-lib "0.5") (org "9.3"))

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Org Link Edit provides Paredit-inspired slurping and barfing
;; commands for Org link descriptions.
;;
;; There are four slurp and barf commands, all which operate when
;; point is on an Org link.
;;
;; - org-link-edit-forward-slurp
;; - org-link-edit-backward-slurp
;; - org-link-edit-forward-barf
;; - org-link-edit-backward-barf
;;
;; Org Link Edit doesn't bind these commands to any keys.  Finding
;; good keys for these commands is difficult because, while it's
;; convenient to be able to quickly repeat these commands, they won't
;; be used frequently enough to be worthy of a short, repeat-friendly
;; binding.  Using Hydra [1] provides a nice solution to this.  After
;; an initial key sequence, any of the commands will be repeatable
;; with a single key.  (Plus, you get a nice interface that displays
;; the key for each command.)  Below is one example of how you could
;; configure this.
;;
;;     (define-key org-mode-map YOUR-KEY
;;       (defhydra hydra-org-link-edit ()
;;         "Org Link Edit"
;;         ("j" org-link-edit-forward-slurp "forward slurp")
;;         ("k" org-link-edit-forward-barf "forward barf")
;;         ("u" org-link-edit-backward-slurp "backward slurp")
;;         ("i" org-link-edit-backward-barf "backward barf")
;;         ("q" nil "cancel")))
;;
;; In addition to the slurp and barf commands, the command
;; `org-link-edit-transport-next-link' searches for the next (or
;; previous) link and moves it to point, using the word at point or
;; the selected region as the link's description.
;;
;; [1] https://github.com/abo-abo/hydra

;;; Code:

(require 'org)
(require 'org-element)
(require 'cl-lib)

(defun org-link-edit--on-link-p (&optional element)
  (org-element-lineage (or element (org-element-context)) '(link) t))

(defun org-link-edit--link-data ()
  "Return list with information about the link at point.
The list includes
- the position at the start of the link
- the position at the end of the link
- the link text
- the link description (nil when on a plain link)"
  (let ((el (org-element-context)))
    (unless (org-link-edit--on-link-p el)
      (user-error "Point is not on a link"))
    (save-excursion
      (goto-char (org-element-property :begin el))
      (cond
       ;; Use match-{beginning,end} because match-end is consistently
       ;; positioned after ]], while the :end property is positioned
       ;; at the next word on the line, if one is present.
       ((looking-at org-link-bracket-re)
        (list (match-beginning 0)
              (match-end 0)
              (save-match-data
                (org-link-unescape (match-string-no-properties 1)))
              (or (match-string-no-properties 2) "")))
       ((looking-at org-link-plain-re)
        (list (match-beginning 0)
              (match-end 0)
              (match-string-no-properties 0)
              nil))
       (t
        (error "What am I looking at?"))))))

(defun org-link-edit--forward-blob (n &optional no-punctuation)
  "Move forward N blobs (backward if N is negative).

A block of non-whitespace characters is a blob.  If
NO-PUNCTUATION is non-nil, trailing punctuation characters are
not considered part of the blob when going in the forward
direction.

If the edge of the buffer is reached before completing the
movement, return nil.  Otherwise, return t."
  (let* ((forward-p (> n 0))
         (nblobs (abs n))
         (skip-func (if forward-p 'skip-syntax-forward 'skip-syntax-backward))
         skip-func-retval)
    (while (/= nblobs 0)
      (funcall skip-func " ")
      (setq skip-func-retval (funcall skip-func "^ "))
      (setq nblobs (1- nblobs)))
    (when (and forward-p no-punctuation)
      (let ((punc-tail-offset (save-excursion (skip-syntax-backward "."))))
        ;; Don't consider trailing punctuation as part of the blob
        ;; unless the whole blob consists of punctuation.
        (unless (= skip-func-retval (- punc-tail-offset))
          (goto-char (+ (point) punc-tail-offset)))))
    (/= skip-func-retval 0)))

;;;###autoload
(defun org-link-edit-forward-slurp (&optional n)
  "Slurp N trailing blobs into link's description.

  The \[\[https://orgmode.org/\]\[Org mode\]\] site

                        |
                        v

  The \[\[https://orgmode.org/\]\[Org mode site\]\]

A blob is a block of non-whitespace characters.  When slurping
forward, trailing punctuation characters are not considered part
of a blob.

After slurping, return the slurped text and move point to the
beginning of the link.

If N is negative, slurp leading blobs instead of trailing blobs."
  (interactive "p")
  (setq n (or n 1))
  (cond
   ((= n 0))
   ((< n 0)
    (org-link-edit-backward-slurp (- n)))
   (t
    (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data)
      (goto-char (save-excursion
                   (goto-char end)
                   (or (org-link-edit--forward-blob n 'no-punctuation)
                       (user-error "Not enough blobs after the link"))
                   (point)))
      (let ((slurped (buffer-substring-no-properties end (point))))
        (setq slurped (replace-regexp-in-string "\n+" " " slurped))
        (when (and (= (length desc) 0)
                   (string-match "^\\s-+\\(.*\\)" slurped))
          (setq slurped (match-string 1 slurped)))
        (setq desc (concat desc slurped)
              end (+ end (length slurped)))
        (delete-region beg (point))
        (insert (org-link-make-string link desc))
        (goto-char beg)
        slurped)))))

;;;###autoload
(defun org-link-edit-backward-slurp (&optional n)
  "Slurp N leading blobs into link's description.

  The \[\[https://orgmode.org/\]\[Org mode\]\] site

                        |
                        v

  \[\[https://orgmode.org/\]\[The Org mode\]\] site

A blob is a block of non-whitespace characters.

After slurping, return the slurped text and move point to the
beginning of the link.

If N is negative, slurp trailing blobs instead of leading blobs."
  (interactive "p")
  (setq n (or n 1))
  (cond
   ((= n 0))
   ((< n 0)
    (org-link-edit-forward-slurp (- n)))
   (t
    (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data)
      (goto-char (save-excursion
                   (goto-char beg)
                   (or (org-link-edit--forward-blob (- n))
                       (user-error "Not enough blobs before the link"))
                   (point)))
      (let ((slurped (buffer-substring-no-properties (point) beg)))
        (when (and (= (length desc) 0)
                   (string-match "\\(.*\\)\\s-+$" slurped))
          (setq slurped (match-string 1 slurped)))
        (setq slurped (replace-regexp-in-string "\n+" " " slurped))
        (setq desc (concat slurped desc)
              beg (- beg (length slurped)))
        (delete-region (point) end)
        (insert (org-link-make-string link desc))
        (goto-char beg)
        slurped)))))

(defun org-link-edit--split-first-blobs (string n)
  "Split STRING into (N first blobs . other) cons cell.
`N first blobs' contains all text from the start of STRING up to
the start of the N+1 blob.  `other' includes the remaining text
of STRING.  If the number of blobs in STRING is fewer than N,
`other' is nil."
  (when (< n 0) (user-error "N cannot be negative"))
  (with-temp-buffer
    (insert string)
    (goto-char (point-min))
    (with-syntax-table org-mode-syntax-table
      (let ((within-bound (org-link-edit--forward-blob n)))
        (skip-syntax-forward " ")
        (cons (buffer-substring 1 (point))
              (and within-bound
                   (buffer-substring (point) (point-max))))))))

(defun org-link-edit--split-last-blobs (string n)
  "Split STRING into (other . N last blobs) cons cell.
`N last blobs' contains all text from the end of STRING back to
the end of the N+1 last blob.  `other' includes the remaining
text of STRING.  If the number of blobs in STRING is fewer than
N, `other' is nil."
  (when (< n 0) (user-error "N cannot be negative"))
  (with-temp-buffer
    (insert string)
    (goto-char (point-max))
    (with-syntax-table org-mode-syntax-table
      (let ((within-bound (org-link-edit--forward-blob (- n))))
        (skip-syntax-backward " ")
        (cons (and within-bound
                   (buffer-substring 1 (point)))
              (buffer-substring (point) (point-max)))))))

;;;###autoload
(defun org-link-edit-forward-barf (&optional n)
  "Barf N trailing blobs from link's description.

  The \[\[https://orgmode.org/\]\[Org mode\]\] site

                        |
                        v

  The \[\[https://orgmode.org/\]\[Org\]\] mode site

A blob is a block of non-whitespace characters.

After barfing, return the barfed text and move point to the
beginning of the link.

If N is negative, barf leading blobs instead of trailing blobs."
  (interactive "p")
  (setq n (or n 1))
  (cond
   ((= n 0))
   ((< n 0)
    (org-link-edit-backward-barf (- n)))
   (t
    (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data)
      (when (= (length desc) 0)
        (user-error "Link has no description"))
      (pcase-let ((`(,new-desc . ,barfed) (org-link-edit--split-last-blobs
                                           desc n)))
        (unless new-desc (user-error "Not enough blobs in description"))
        (goto-char beg)
        (delete-region beg end)
        (insert (org-link-make-string link new-desc))
        (when (string= new-desc "")
          (setq barfed (concat " " barfed)))
        (insert barfed)
        (goto-char beg)
        barfed)))))

;;;###autoload
(defun org-link-edit-backward-barf (&optional n)
  "Barf N leading blobs from link's description.

  The \[\[https://orgmode.org/\]\[Org mode\]\] site

                        |
                        v

  The Org \[\[https://orgmode.org/\]\[mode\]\] site

A blob is a block of non-whitespace characters.

After barfing, return the barfed text and move point to the
beginning of the link.

If N is negative, barf trailing blobs instead of leading blobs."
  (interactive "p")
  (setq n (or n 1))
  (cond
   ((= n 0))
   ((< n 0)
    (org-link-edit-forward-barf (- n)))
   (t
    (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data)
      (when (= (length desc) 0)
        (user-error "Link has no description"))
      (pcase-let ((`(,barfed . ,new-desc) (org-link-edit--split-first-blobs
                                           desc n)))
        (unless new-desc (user-error "Not enough blobs in description"))
        (goto-char beg)
        (delete-region beg end)
        (insert (org-link-make-string link new-desc))
        (when (string= new-desc "")
          (setq barfed (concat barfed " ")))
        (goto-char beg)
        (insert barfed)
        barfed)))))

(defun org-link-edit--next-link-data (&optional previous)
  (save-excursion
    (if (funcall (if previous #'re-search-backward #'re-search-forward)
                 org-link-any-re nil t)
        (org-link-edit--link-data)
      (user-error "No %s link found" (if previous "previous" "next")))))

;;;###autoload
(defun org-link-edit-transport-next-link (&optional previous beg end overwrite)
  "Move the next link to point.

If the region is active, use the selected text as the link's
description.  Otherwise, use the word at point.

With prefix argument PREVIOUS, move the previous link instead of
the next link.

Non-interactively, use the text between BEG and END as the
description, moving the next (or previous) link relative to BEG
and END.  By default, refuse to overwrite an existing
description.  If OVERWRITE is `ask', prompt for confirmation
before overwriting; for any other non-nil value, overwrite
without asking."
  (interactive `(,current-prefix-arg
                 ,@(if (use-region-p)
                       (list (region-beginning) (region-end))
                     (list nil nil))
                 ask))
  (let ((pt (point))
        (desc-bounds (cond
                      ((and beg end)
                       (cons (progn (goto-char beg)
                                    (point-marker))
                             (progn (goto-char end)
                                    (point-marker))))
                      ((not (looking-at-p "\\s-"))
                       (progn (skip-syntax-backward "w")
                              (let ((beg (point-marker)))
                                (skip-syntax-forward "w")
                                (cons beg (point-marker))))))))
    (when (or (and desc-bounds
                   (or (progn (goto-char (car desc-bounds))
                              (org-link-edit--on-link-p))
                       (progn (goto-char (cdr desc-bounds))
                              (org-link-edit--on-link-p))))
              (progn (goto-char pt)
                     (org-link-edit--on-link-p)))
      (user-error "Cannot transport next link with point on a link"))
    (goto-char (or (car desc-bounds) pt))
    (cl-multiple-value-bind (link-beg link-end link orig-desc)
        (org-link-edit--next-link-data previous)
      (unless (or (not desc-bounds)
                  (= (length orig-desc) 0)
                  (if (eq overwrite 'ask)
                      (y-or-n-p "Overwrite existing description?")
                    overwrite))
        (user-error "Link already has a description"))
      (delete-region link-beg link-end)
      (insert (org-link-make-string
               link
               (if desc-bounds
                   (delete-and-extract-region (car desc-bounds)
                                              (cdr desc-bounds))
                 orig-desc))))))

(provide 'org-link-edit)
;;; org-link-edit.el ends here