-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstandoff-mark.el
592 lines (532 loc) · 20.8 KB
/
standoff-mark.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
;;; standoff-mark.el --- Marking and operating on items.
;; Copyright (C) 2015 Christian Lück
;; Author: Christian Lück <[email protected]>
;; URL: https://github.com/lueck/standoff-mode
;; This file is not part of GNU Emacs.
;; 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 standoff-mode. If not, see
;; <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This file defines some commands for marking and operating on marks
;; that can be reused in different buffers listing annotations one by
;; line like files in a Dired buffer.
;; Adapted from dired.el from GNU Emacs.
;; While adapting from dired.el, the word 'files' was subsituted with
;; 'items', which was the most general term. We could also have said
;; 'annotations'. We always say *items* buffer in doc strings, but
;; there will be a *Relations* buffer, a *Literals* buffer etc.
;;; Code:
(require 'standoff-util)
(require 'standoff-log)
;;;; Marking
(defcustom standoff-mark/no-confirm nil
"A list of symbols for commands operate should not confirm, or t.
Command symbols are `delete'.
If t, confirmation is never needed."
:group 'standoff
:type '(choice (const :tag "Confirmation never needed" t)
(set (const delete))))
;; These regexps must be tested at beginning-of-line, but are also
;; used to search for next matches, so neither omitting "^" nor
;; replacing "^" by "\n" (to make it slightly faster) will work.
(defvar standoff-mark/re-mark "^[^ \n]")
;; "Regexp matching a marked line.
;; Important: the match ends just after the marker."
(defvar standoff-mark/re-maybe-mark "^. ")
(defvar standoff-mark/marker-char ?*
"In the *items* buffer, the current mark character.
This is what the do-commands look for, and what the mark-commands store.")
(defvar standoff-mark/del-marker ?D
"Character used to flag items for deletion.")
(defun standoff-mark/marker-regexp ()
"Return a regexp, matching the marker character."
(concat "^" (regexp-quote (char-to-string standoff-mark/marker-char))))
;;;; Commands for marking, moving etc.
(defun standoff-mark (arg &optional interactive)
"Mark the item at point in the *items* buffer.
If the region is active, mark all items in the region.
Otherwise, with a prefix arg, mark items on the next ARG lines.
INTERACTIVE should be t, if called interactively.
Use \\[standoff-unmark-all] to remove all marks and
\\[standoff-unmark] to remove a single mark."
(interactive (list current-prefix-arg t))
(if (and interactive (use-region-p))
;; Mark items in the active region.
(save-excursion
(let ((beg (region-beginning))
(end (region-end)))
(standoff-mark/mark-items-in-region
(progn (goto-char beg) (line-beginning-position))
(progn (goto-char end) (line-beginning-position)))))
;; Mark the current (or next ARG) items.
(let ((inhibit-read-only t))
(standoff-mark/repeat-over-lines
(prefix-numeric-value arg)
#'(lambda ()
(delete-char 1)
(insert standoff-mark/marker-char))))))
(defun standoff-unmark (arg &optional interactive)
"Unmark the item at point in the *items* buffer.
If the region is active, unmark all items in the region.
Otherwise, with a prefix arg, unmark items on the next ARG
lines. INTERACTIVE should be t, if called interactively."
(interactive (list current-prefix-arg t))
(let ((standoff-mark/marker-char ?\040))
(standoff-mark arg interactive)))
(defun standoff-unmark-all-marks ()
"Remove all marks from all items in the *items* buffer."
(interactive)
(standoff-unmark-all-items ?\r))
(defun standoff-unmark-all-items (mark &optional arg)
"Remove a specific MARK (or any mark) from every item.
After this command, type the mark character to remove,
or type RET to remove all marks.
With prefix ARG, query for each marked item.
Type \\[help-command] at that time for help."
(interactive "cRemove marks (RET means all): \nP")
(save-excursion
(let* ((count 0)
(inhibit-read-only t) case-fold-search
(string (format "\n%c" mark))
(help-form "\
Type SPC or `y' to unmark one item, DEL or `n' to skip to next,
`!' to unmark all remaining items with no more questions."))
(goto-char (point-min))
(while (if (eq mark ?\r)
(re-search-forward standoff-mark/re-mark nil t)
(search-forward string nil t))
(if (or (not arg)
(let ((item (standoff-mark/get-item-id)))
(and item
(standoff-mark/query 'query "Unmark `%s'? "
item))))
(progn (subst-char-in-region (1- (point)) (point)
(preceding-char) ?\s)
(setq count (1+ count)))))
(message (if (= count 1) "1 mark removed"
"%d marks removed")
count))))
(defun standoff-toggle-marks ()
"Toggle marks: marked items become unmarked, and vice versa.
Items marked with other flags (such as `D') are not affected.
As always, hidden items are not affected."
(interactive)
(save-excursion
(goto-char (point-min))
(let ((inhibit-read-only t))
(while (not (eobp))
;; use subst instead of insdel because it does not move
;; the gap and thus should be faster and because
;; other characters are left alone automatically
(apply 'subst-char-in-region
(point) (1+ (point))
(if (eq ?\040 (following-char)) ; SPC
(list ?\040 standoff-mark/marker-char)
(list standoff-mark/marker-char ?\040)))
(forward-line 1)))))
(defun standoff-flag-delete (arg &optional interactive)
"In the *items* buffer, flag the current line's item for deletion.
If the region is active, flag all items in the region.
Otherwise, with a prefix arg, flag items on the next ARG
lines. INTERACTIVE should be t, if called interactively."
(interactive (list current-prefix-arg t))
(let ((standoff-mark/marker-char standoff-mark/del-marker))
(standoff-mark arg interactive)))
(defun standoff-next-item (arg)
"Move down lines then position at item.
Optional prefix ARG says how many lines to move; default is one line."
(interactive "p")
(let ((line-move-visual)
(goal-column))
(line-move arg t))
;; We never want to move point into an invisible line.
(while (and (invisible-p (point))
(not (if (and arg (< arg 0)) (bobp) (eobp))))
(forward-char (if (and arg (< arg 0)) -1 1)))
(standoff-mark/move-to-item))
(defun standoff-previous-item (arg)
"Move up lines then position at item.
Optional prefix ARG says how many lines to move; default is one line."
(interactive "p")
(standoff-next-item (- (or arg 1))))
;;;; helper function
(defun standoff-mark/mark-items-in-region (start end)
"Mark items in region given by START and END."
(let ((inhibit-read-only t))
(if (> start end)
(error "start > end"))
(goto-char start) ; assumed at beginning of line
(while (< (point) end)
(delete-char 1)
(insert standoff-mark/marker-char)
(forward-line 1))))
(defun standoff-mark/repeat-over-lines (arg function)
"If ARG is numerical, FUNCTION is called on ARG lines."
(let ((pos (make-marker)))
(beginning-of-line)
(while (and (> arg 0) (not (eobp)))
(setq arg (1- arg))
(beginning-of-line)
(save-excursion
(forward-line 1)
(move-marker pos (1+ (point))))
(save-excursion (funcall function))
;; Advance to the next line--actually, to the line that *was* next.
;; (If FUNCTION inserted some new lines in between, skip them.)
(goto-char pos))
(while (and (< arg 0) (not (bobp)))
(setq arg (1+ arg))
(forward-line -1)
(beginning-of-line)
(save-excursion (funcall function)))
(move-marker pos nil)
(standoff-mark/move-to-item)))
(defun standoff-mark/move-to-item ()
"Move to the item in the current line."
;; TODO
(beginning-of-line))
;;;; Operate
(defun standoff-mark/get-item-id ()
"Return the id of the item in this line."
(save-excursion
(let ((limit (end-of-line))
rel-id)
(beginning-of-line)
(if (re-search-forward standoff-util/re-uuid limit t)
(setq rel-id (match-string 0)) ; returns item id
(error "No Id found in this line")))))
(defun standoff-mark/map-over-marks (body arg &optional show-progress
distinguish-one-marked)
"Call function BODY with point on each marked line.
Return a list of BODY's results. If no marked item could be
found, execute BODY on the current line. ARG, if non-nil,
specifies the items to use instead of the marked items.
If ARG is an integer, use the next ARG (or previous -ARG, if
ARG<0) items. In that case, point is dragged along. This is
so that commands on the next ARG (instead of the marked) items
can be chained easily. For any other non-nil value of ARG, use
the current item.
If optional third arg SHOW-PROGRESS evaluates to non-nil,
redisplay the *items* buffer after each item is
processed.
No guarantee is made about the position on the marked line.
BODY must ensure this itself if it depends on this.
Search starts at the beginning of the buffer, thus the car of the
list corresponds to the line nearest to the buffer's bottom.
This is also true for (positive and negative) integer values of
ARG.
BODY should not be too long as it is called four times.
If DISTINGUISH-ONE-MARKED is non-nil, then if we find just one
marked item, return (t ITEM-ID) instead of (ITEM-ID)."
(prog1
(let ((inhibit-read-only t)
case-fold-search
found
results)
(if arg
(if (integerp arg)
(progn ;; no save-excursion, want to move point.
(standoff-mark/repeat-over-lines
arg
(function (lambda ()
(if show-progress (sit-for 0))
(setq results (cons (funcall body) results)))))
(if (< arg 0)
(nreverse results)
results))
;; non-nil, non-integer ARG means use current item:
(list (funcall body)))
;; arg is nil
(let ((regexp (standoff-mark/marker-regexp))
next-position)
(save-excursion
(goto-char (point-min))
;; remember position of next marked item before BODY
;; can insert lines before the just found item,
;; confusing us by finding the same marked item again
;; and again and...
(setq next-position (and (re-search-forward regexp nil t)
(point-marker))
found (not (null next-position)))
(while next-position
(goto-char next-position)
(if show-progress (sit-for 0))
(setq results (cons (funcall body) results))
;; move after last match
(goto-char next-position)
(forward-line 1)
(set-marker next-position nil)
(setq next-position (and (re-search-forward regexp nil t)
(point-marker)))))
(if (and distinguish-one-marked (= (length results) 1))
(setq results (cons t results)))
(if found
results
(list (funcall body))))))
;; save-excursion loses, again
(standoff-mark/move-to-item)))
(defun standoff-mark/map-over-marks-off (body arg &optional show-progress
distinguish-one-marked)
;; This version does not map over marks
(prog1
(let ((inhibit-read-only t)
case-fold-search
found
results)
(if arg
(if (integerp arg)
(progn ;; no save-excursion, want to move point.
(standoff-mark/repeat-over-lines
arg
(function (lambda ()
(if show-progress (sit-for 0))
(setq results (cons body results)))))
(if (< arg 0)
(nreverse results)
results))
;; non-nil, non-integer ARG means use current item:
(list body))
(let ((regexp (standoff-mark/marker-regexp))
next-position)
(save-excursion
(goto-char (point-min))
;; remember position of next marked item before BODY
;; can insert lines before the just found item,
;; confusing us by finding the same marked item again
;; and again and...
(setq next-position (and (re-search-forward regexp nil t)
(point-marker))
found (not (null next-position)))
(while next-position
(goto-char next-position)
(if show-progress (sit-for 0))
(setq results (cons body results))
;; move after last match
(goto-char next-position)
(forward-line 1)
(set-marker next-position nil)
(setq next-position (and (re-search-forward regexp nil t)
(point-marker)))))
(if (and distinguish-one-marked (= (length results) 1))
(setq results (cons t results)))
(if found
results
(list body)))))
;; save-excursion loses, again
(standoff-mark/move-to-item)))
(defun standoff-mark/map-over-marks-quoted (body arg &optional show-progress
distinguish-one-marked)
"Original implementation adapted from dired."
`(prog1
(let ((inhibit-read-only t)
case-fold-search
found
results)
(if ,arg
(if (integerp ,arg)
(progn ;; no save-excursion, want to move point.
(standoff-mark/repeat-over-lines
,arg
(function (lambda ()
(if ,show-progress (sit-for 0))
(setq results (cons ,body results)))))
(if (< ,arg 0)
(nreverse results)
results))
;; non-nil, non-integer ARG means use current item:
(list ,body))
(let ((regexp (standoff-mark/marker-regexp))
next-position)
(save-excursion
(goto-char (point-min))
;; remember position of next marked item before BODY
;; can insert lines before the just found item,
;; confusing us by finding the same marked item again
;; and again and...
(setq next-position (and (re-search-forward regexp nil t)
(point-marker))
found (not (null next-position)))
(while next-position
(goto-char next-position)
(if ,show-progress (sit-for 0))
(setq results (cons ,body results))
;; move after last match
(goto-char next-position)
(forward-line 1)
(set-marker next-position nil)
(setq next-position (and (re-search-forward regexp nil t)
(point-marker)))))
(if (and ,distinguish-one-marked (= (length results) 1))
(setq results (cons t results)))
(if found
results
(list ,body)))))
;; save-excursion loses, again
(standoff-mark/move-to-item)))
(defvar standoff-mark/deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
(defun standoff-mark/query (symbol query-string &rest subst)
"Query the user QUERY-STRING formatted with SUBST.
SYMBOL is ignored."
;; FIXME: Adapt from dired.
(y-or-n-p (format query-string subst)))
(defun standoff-mark/internal-do-deletions (l arg delete-function &optional trash)
"L is an alist of items to delete, with their buffer positions.
ARG is the prefix arg. Itemnames are absolute. (car L) *must*
be the *last* (bottommost) item in the *items* buffer. That way
as changes are made in the buffer they do not shift the lines
still to be changed, so the (point) values in L stay valid. The
DELETE-FUNCTION is called with the id of the item to be deleted
as argument. If TRASH is non-nil, the deleted item will be moved
to trash."
(let* ((items (mapcar (function car) l))
(count (length l))
(succ 0)
(trashing (and trash delete-by-moving-to-trash)))
;; canonicalize items list for pop up
(if (standoff-mark/mark-pop-up
" *Deletions*" 'delete items standoff-mark/deletion-confirmer
(format "%s %s "
(if trashing "Trash" "Delete")
(standoff-mark/mark-prompt arg items)))
(save-excursion
(let ((progress-reporter
(make-progress-reporter
(if trashing "Trashing..." "Deleting...")
succ count))
failures) ;; items better be in reverse order for this loop!
(while l
(goto-char (cdr (car l)))
(let ((inhibit-read-only t))
(condition-case err
(let ((rel-id (car (car l)))
(del-start (progn (beginning-of-line) (point)))
(del-end (progn (forward-line 1) (point))))
(goto-char del-start)
(funcall delete-function rel-id)
;; if we get here, removing worked
(setq succ (1+ succ))
(progress-reporter-update progress-reporter succ)
(delete-region del-start del-end))
(error ;; catch errors from failed deletions
(standoff-log "%s\n" err)
(setq failures (cons (car (car l)) failures)))))
(setq l (cdr l)))
(if (not failures)
(progress-reporter-done progress-reporter)
(standoff-log-summary
(format "%d of %d deletion%s failed"
(length failures) count
(standoff-util/plural-s count))
failures))))
(message "(No deletions performed)")))
(standoff-mark/move-to-item))
(defun standoff-mark/mark-prompt (arg items)
"Return a string suitable for use in a operation prompt.
ARG is normally the prefix argument for the calling command.
ITEMS should be a list of ITEM-IDs.
The return value has a form like \"<UUID>\", \"[next 3 items]\",
or \"* [3 items]\"."
;; distinguish-one-marked can cause the first element to be just t.
(if (eq (car items) t) (setq items (cdr items)))
(let ((count (length items)))
(if (= count 1)
(car items)
;; more than 1 item:
(if (integerp arg)
;; abs(arg) = count
;; Perhaps this is nicer, but it also takes more screen space:
;;(format "[%s %d items]" (if (> arg 0) "next" "previous")
;; count)
(format "[next %d items]" arg)
(format "%c [%d items]" standoff-mark/marker-char count)))))
(defun standoff-mark/mark-pop-up (buffer-or-name op-symbol items function &rest args)
"Return FUNCTION's result on ARGS after showing which items are marked.
Displays the items in a window showing a buffer named
BUFFER-OR-NAME; the default name being \" *Marked Items*\". The
window is not shown if there is just one item,
`standoff-mark/no-confirm' is t, or OP-SYMBOL is a member of
the list in `standoff-mark/no-confirm'.
By default, the display buffer is shrinked to fit the marked
items. To disable this, use the Customization interface to
add a new rule to `display-buffer-alist' where condition regexp
is \"^ \\*Marked Items\\*$\", action argument symbol is
`window-height' and its value is nil.
ITEMS is the list of marked items. It can also be (t ITEM)
in the case of one marked item, to distinguish that from using
just the current item.
FUNCTION should not manipulate items, just read input (an
argument or confirmation)."
(if (or (eq standoff-mark/no-confirm t)
(memq op-symbol standoff-mark/no-confirm)
;; If ITEMS defaulted to the current line's item.
(= (length items) 1))
(apply function args)
(let ((buffer (get-buffer-create (or buffer-or-name " *Marked Items*")))
;; Mark *Marked Items* window as softly-dedicated, to prevent
;; other buffers e.g. *Completions* from reusing it (bug#17554).
(display-buffer-mark-dedicated 'soft))
(with-current-buffer buffer
(with-current-buffer-window
buffer
(cons 'display-buffer-below-selected
'((window-height . fit-window-to-buffer)))
#'(lambda (window _value)
(with-selected-window window
(unwind-protect
(apply function args)
(when (window-live-p window)
(quit-restore-window window 'kill)))))
;; Handle (t ITEM) just like (ITEM), here. That value is
;; used (only in some cases), to mean just one item that was
;; marked, rather than the current line item.
(standoff-mark/format-columns-of-items
(if (eq (car items) t) (cdr items) items))
(remove-text-properties (point-min) (point-max)
'(mouse-face nil help-echo nil)))))))
;;;; formatting
(defun standoff-mark/format-columns-of-items (items)
"Do nothing at the moment with ITEMS."
;; FIXME
)
;;;; mode maps
(defvar standoff-mark/mark-map
(let ((map (make-sparse-keymap)))
;; Commands for marking and unmarking
(define-key map "*" nil)
(define-key map "*m" 'standoff-mark)
(define-key map "*?" 'standoff-unmark-all-items)
(define-key map "*!" 'standoff-unmark-all-marks)
(define-key map "U" 'standoff-unmark-all-marks)
(define-key map "*t" 'standoff-toggle-marks)
;; Lower keys for commands not operating on all the marked files
(define-key map "d" 'standoff-flag-delete)
(define-key map "m" 'standoff-mark)
(define-key map "n" 'standoff-next-item)
(define-key map "p" 'standoff-previous-item)
(define-key map "t" 'standoff-toggle-marks)
(define-key map "u" 'standoff-unmark)
;; moving
(define-key map " " 'standoff-next-item)
(define-key map [remap next-line] 'standoff-next-item)
(define-key map [remap previous-line] 'standoff-previous-item)
map))
(easy-menu-define standoff-mark/mark-menu standoff-mark/mark-map
"Menu for marking items in standoff-mode."
'("Mark"
["Toggle marks" standoff-toggle-marks]
["Mark" standoff-mark]
["Unmark" standoff-unmark]
["Unmark all" standoff-unmark-all-marks]
["Unmark query all" standoff-unmark-all-items]
["Flag" standoff-flag-delete]
))
(provide 'standoff-mark)
;;; standoff-mark.el ends here