summaryrefslogtreecommitdiff
path: root/common.el
blob: 95ac933cf7d133be7f09fe363e48b7693dfb1760 (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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
;;; common.el --- Some common functions -*- lexical-binding: t; -*-

;;;###autoload
(defun center-string-in-width (str width)
  "Add spaces to STR so that it is centered in a box which is WIDTH wide."
  (let ((len (length str)))
    (cond
     ((> len width)
      (error "String %s longer than %d" str width))
     (t (format "%s%s"
                (make-string (round (- width len) 2)
                             32)
                str)))))

;;; A better face-remap-remove-relative

(defun durand-face-remap-remove-relative (cookie)
  "Remove a face remapping previously added by `face-remap-add-relative'.
COOKIE should be the return value from that function.

Modified by Durand to use `equal' as the comparison function, so
that we are not required to record the objects returned
previously by `face-remap-add-relative', at 2021-06-05."
  (let ((remapping (assq (car cookie) face-remapping-alist)))
    (when remapping
      (let ((updated-entries (remove (cdr cookie) (cdr remapping))))
	(unless (eq updated-entries (cdr remapping))
	  (setcdr remapping updated-entries)
	  (when (or (null updated-entries)
		    (and (eq (car-safe updated-entries) (car cookie))
			 (null (cdr updated-entries))))
	    (setq face-remapping-alist
		  (remove remapping face-remapping-alist))
	    ;; Force redisplay of this buffer.
	    (force-mode-line-update))
	  (cdr cookie))))))

(advice-add 'face-remap-remove-relative :override 'durand-face-remap-remove-relative)

;;; jump between the completions buffer and the minibuffer

;;;###autoload
(defvar durand-completion-buffer-names (list "Completions"
                                             "Embark Collect"
                                             "comb")
  "The list of names that match the names of \"completion buffers\".")

;;;###autoload
(defun durand-focus-completion-or-minibuffer (&optional arg)
  "Jump between the completions buffer and the minibuffer."
  (interactive "P")
  (let* ((completion-windows
          (delq nil
                (mapcar
                 (lambda (w)
                   (and (consp
                         (delq nil
                               (mapcar (lambda (name)
                                         (string-match-p
                                          name
                                          (buffer-name (window-buffer w))))
                                       durand-completion-buffer-names)))
                        w))
                 (window-list nil 'nomini))))
         (in-completion-p (consp
                           (delq nil
                                 (mapcar
                                  (lambda (name)
                                    (string-match-p name (buffer-name)))
                                  durand-completion-buffer-names))))
         (minibuffer-active-p (active-minibuffer-window))
         (in-minibuffer-p (minibuffer-window-active-p (selected-window))))
    (cond
     ((and minibuffer-active-p
           (not in-minibuffer-p))
      (select-window (active-minibuffer-window)))
     ((and (consp completion-windows)
           (not in-completion-p))
      (select-window (car completion-windows)))
     (t
      (other-window 1)))))

;;;###autoload
(defun durand-embark-scroll-down-or-go-to-completions (&optional arg)
  "Scroll down; if this is not feasible, go to the completions buffer.
ARG means do this command ARG times."
  (interactive "p")
  (let ((original-point (point))
        (left (abs (forward-line arg)))
        (completion-windows
         (delq nil
               (mapcar
                (lambda (w)
                  (and (consp
                        (delq nil
                              (mapcar (lambda (name)
                                        (string-match-p
                                         name
                                         (buffer-name (window-buffer w))))
                                      durand-completion-buffer-names)))
                       w))
                (window-list nil 'nomini))))
        (in-completion-p (consp
                          (delq nil
                                (mapcar
                                 (lambda (name)
                                   (string-match-p name (buffer-name)))
                                 durand-completion-buffer-names)))))
    (cond ((= (point) (point-max))
           (setq left (1+ left)))
          ((get-text-property (point) 'field)
           (goto-char original-point)))
    (cond ((and (> left 0)
                (consp completion-windows)
                (not in-completion-p))
           (select-window (car completion-windows))
           (cond
            ((> arg 0)
             (goto-char (point-min))
             (cond
              ((derived-mode-p 'completion-list-mode)
               (next-completion 1))))
            ((< arg 0)
             (goto-char (point-max))
             (cond
              ((derived-mode-p 'completion-list-mode)
               (next-completion -1)))))))))

;;;###autoload
(defun durand-embark-scroll-up-or-go-to-completions (&optional arg)
  "Scroll up; if this is not feasible, go to the completions buffer.
ARG means do this command ARG times."
  (interactive "p")
  (durand-embark-scroll-down-or-go-to-completions (- arg)))

;;;###autoload
(defun durand-completion-scroll-down-or-go-to-minibuffer (&optional arg)
  "Scroll down; if this is not feasible, go to the mini-buffer.
ARG means do this command ARG times."
  (interactive "p")
  (let ((original-point (point))
        (left (abs (forward-line arg)))
        (minibuffer-active-p (active-minibuffer-window))
        (in-minibuffer-p (minibuffer-window-active-p (selected-window))))
    (cond ((= (point) (point-max))
           (setq left (1+ left)))
          ((get-text-property (point) 'field)
           (goto-char original-point)))
    (cond ((and (> left 0)
                minibuffer-active-p
                (not in-minibuffer-p))
           (select-window (active-minibuffer-window))
           (cond
            ((> arg 0)
             (goto-char (point-min)))
            ((< arg 0)
             (goto-char (point-max))))))))

(defun durand-completion-goto-minibuffer ()
  "Go to the minibuffer."
  (interactive)
  (let ((active-minibuffer-window (active-minibuffer-window))
        (completion-windows
         (delq nil
               (mapcar
                (lambda (w)
                  (and (consp
                        (delq nil
                              (mapcar
                               (lambda (name)
                                 (string-match-p
                                  name
                                  (buffer-name (window-buffer w))))
                               durand-completion-buffer-names)))
                       w))
                (window-list nil 'nomini))))
        (in-completion-p (consp
                          (delq nil
                                (mapcar
                                 (lambda (name)
                                   (string-equal name (buffer-name)))
                                 durand-completion-buffer-names))))
        (in-minibuffer-p
         (minibuffer-window-active-p (selected-window))))
    (cond
     ((and active-minibuffer-window
           (not in-minibuffer-p))
      (select-window active-minibuffer-window))
     ((and (consp completion-windows)
           (not in-completion-p))
      (select-window (car completion-windows))))))

;; This can be optimized by not duplicating an element on the stack.

(defun durand-display-in-one-window (buffer _alist)
  "Display BUFFER in one window.
ALIST is an association list of action symbols and values. See
Info node `(elisp) Buffer Display Action Alists' for details of
such alists.

This function pops up a new window and then deletes all other
windows. And ALIST is completely ignored.

\(fn BUFFER ALIST)"
  (let ((window (split-window (frame-root-window (selected-frame)))))
    (set-window-buffer window buffer)
    (select-window window)
    (delete-other-windows window)))

;; (defalias 'durand-display-in-one-window
;;   (make-byte-code
;;    #x202
;;    (unibyte-string 192 193 194 32 33 33 195 1 4 34 136 196 1 33 136 197 32 136 135)
;;    (vector 'split-window 'frame-root-window 'selected-frame
;;            'set-window-buffer 'select-window 'delete-other-windows)
;;    9
;;    "Display BUFFER in one window.
;; ALIST is an association list of action symbols and values. See
;; Info node `(elisp) Buffer Display Action Alists' for details of
;; such alists. 

;; This function pops up a new window and then deletes all other
;; windows. And ALIST is completely ignored.

;; \(fn BUFFER ALIST)"))

;;;###autoload
(defvar durand-window-max-height 0.3
  "Maximal height of the bottom window.
The window is placed at the bottom.")

;;;###autoload
(defun durand-fit-window-to-buffer-with-max (window)
  "Fit WINDOW to its buffer.
This is a thin wrapper around `fit-window-to-buffer'. It passes
the window to the function with max-height equal to
`durand-window-max-height'."
  (fit-window-to-buffer window
                        (floor
                         (* durand-window-max-height
                            (frame-height (window-frame window))))))

;;; Rebinding a key in Info mode

(with-eval-after-load 'info
  (define-key Info-mode-map (vector 41) #'Info-forward-node))

;; (defun durand-display-in-one-window (buffer _alist)
;;   "Display BUFFER in one window.
;; ALIST is an association list of action symbols and values. See
;; Info node `(elisp) Buffer Display Action Alists' for details of
;; such alists. 

;; This function pops up a new window and then deletes all other
;; windows. And ALIST is completely ignored."
;;   (let ((window (split-window (frame-root-window (selected-frame)))))
;;     (set-window-buffer window buffer)
;;     (select-window window)
;;     (delete-other-windows)
;;     window))

;; This short function is a suitable function for byte codes.

;;;###autoload
(defalias 'durand-completion-scroll-up-or-go-to-minibuffer
  (make-byte-code
   #x100
   (unibyte-string 137 132 7 0 192 178 1 193 1 91 33 135)
   (vector 1 #'durand-completion-scroll-down-or-go-to-minibuffer)
   5
   "Scroll up; if this is not feasible, go to the mini-buffer.
ARG means do this command ARG times."
   "p"))

;; (defun durand-completion-scroll-up-or-go-to-minibuffer (&optional arg)
;;   "Scroll up; if this is not feasible, go to the mini-buffer.
;; ARG means do this command ARG times."
;;   (interactive "p")
;;   (durand-completion-scroll-down-or-go-to-minibuffer (- arg)))

;;; Use completion to select text that was killed before.

;;;###autoload
(defun yank-complete-rotated-kill-ring ()
  "Return the rotated kill ring so that the current kill is at the top."
  (let* ((tail kill-ring-yank-pointer)
         (len-tail (length tail))
         (len (length kill-ring))
         (copied (copy-tree kill-ring))
         (head (progn (setcdr (nthcdr (mod (- -1 len-tail)
                                           len)
                                      copied)
                              nil)
                      copied))
         ;; Judged from `current-kill', this can be a list for some
         ;; reason.
         (interprogram-paste (and interprogram-paste-function
				  (funcall interprogram-paste-function))))
    (append (cond ((listp interprogram-paste) interprogram-paste)
                  ((stringp interprogram-paste) (list interprogram-paste)))
            tail head)))

;;;###autoload
(defvar yank-complete-history nil
  "The history of `yank-complete'.")

;;;###autoload
(defun yank-complete (&optional arg)
  "Use completion to select text that was killed before.
If ARG is a cons cell, then put the point at the beginning, the
mark at the end for the first yank."
  ;; This is largely copied from `yank'.
  (interactive)
  (let* ((inhibit-read-only t)
         (kills (yank-complete-rotated-kill-ring))
         (choice (completing-read "Yank: " kills nil
                                    t nil 'yank-complete-history)))
    (setq yank-window-start (window-start))
    (setq this-command t)
    (push-mark)
    (insert-for-yank choice)
    (cond ((consp arg)
           (goto-char (prog1 (mark t)
		        (set-marker (mark-marker) (point) (current-buffer)))))))
  (cond ((eq this-command t)
         (setq this-command 'yank))))

;;; Don't ask me if I want to visit the file again.

;; Also add a bookmark type to registers.

(require 'register)

;;;###autoload
(cl-defmethod register-val-jump-to ((val cons) delete)
  (cond
   ((frame-configuration-p (car val))
    (set-frame-configuration (car val) (not delete))
    (goto-char (cadr val)))
   ((window-configuration-p (car val))
    (set-window-configuration (car val))
    (goto-char (cadr val)))
   ((eq (car val) 'file)
    (find-file (cdr val)))
   ((eq (car val) 'bookmark)
    (require 'bookmark)
    (bookmark-jump (cdr val)))
   ((eq (car val) 'file-query)
    (find-buffer-visiting (nth 1 val))
    (find-file (nth 1 val))
    (goto-char (nth 2 val)))
   (t (cl-call-next-method val delete))))

(cl-defmethod register-val-describe ((val cons) verbose)
  (cond
   ((window-configuration-p (car val))
    (let* ((stored-window-config (car val))
           (window-config-frame
            (window-configuration-frame stored-window-config))
           (current-frame (selected-frame)))
      (princ (format
              "a window configuration: %s."
              (if (frame-live-p window-config-frame)
                  (with-selected-frame window-config-frame
                    (save-window-excursion
                      (set-window-configuration
                       stored-window-config)
                      (concat
                       (mapconcat
                        (lambda (w)
                          (buffer-name (window-buffer w)))
                        (window-list (selected-frame)) ", ")
                       (unless (eq current-frame window-config-frame)
                         " in another frame"))))
                "dead frame")))))

   ((frame-configuration-p (car val))
    (princ "a frame configuration."))

   ((eq (car val) 'bookmark)
    (princ "a bookmark:\n    location ")
    (require 'bookmark)
    (princ
     (or (bookmark-get-filename (cdr val))
         (bookmark-prop-get (cdr val) 'location)))
    (let ((position (bookmark-get-position (cdr val))))
      (cond
       (position
        (princ ",\n    position ")
        (prin1 position))))
    (cond
     (verbose
      (let ((front-string
             (bookmark-get-front-context-string (cdr val)))
            (rear-string
             (bookmark-get-rear-context-string (cdr val))))
        (cond
         (front-string
          (princ ",\n    front: ")
          (princ (replace-regexp-in-string
                  "\n\\|\r" "\\n" front-string
                  nil t))))
        (cond
         (rear-string
          (princ ",\n    rear: ")
          (princ (replace-regexp-in-string
                  "\n\\|\r" "\\n" rear-string
                  nil t))))))))

   ((eq (car val) 'file)
    (princ "the file ")
    (prin1 (cdr val))
    (princ "."))

   ((eq (car val) 'buffer)
    (princ "the buffer ")
    (prin1 (cdr val))
    (princ "."))

   ((eq (car val) 'file-query)
    (princ "a file-query reference:\n    file ")
    (prin1 (car (cdr val)))
    (princ ",\n    position ")
    (princ (car (cdr (cdr val))))
    (princ "."))

   (t
    (if verbose
	(progn
	  (princ "the rectangle:\n")
	  (while val
	    (princ "    ")
	    (princ (car val))
	    (terpri)
	    (setq val (cdr val))))
      (princ "a rectangle starting with ")
      (princ (car val))))))

;;; Bind point-to-register to a more convenient key

(define-key ctl-x-r-map (vector ?p) #'point-to-register)

;;; Save anonymous bookmark in a register

(load-after-function bookmark-make-record "bookmark-conf.el"
  "Fake function that loads bookmarks." nil)

(defun durand-bookmark-to-register (register)
  "Store an anonymous bookmark in register REGISTER.
Use \\[jump-to-register] to jump to the bookmark."
  (interactive
   (list (register-read-with-preview "Bookmark to register: ")))
  (set-register register (cons 'bookmark (bookmark-make-record))))

(define-key ctl-x-r-map (vector ?B) #'durand-bookmark-to-register)

;;; Intentionally disable some key-bindings.

;; Using byte-code is fun.

;;;###autoload
(defalias 'intentionally-disabled-bind
  (make-byte-code
   0 (unibyte-string 194 195 193 192 32 33 34 135)
   (vector 'this-command-keys-vector 'key-description 'user-error
           "You pressend an intentionally disabled key-binding: %s")
   4 "Warn the user that this key-binding is intentionally disabled."
   nil))

;; (defun intentionally-disabled-bind (arg)
;;   "Warn the user that this key-binding is intentionally disabled."
;;   (interactive)
;;   (user-error "You pressed an intentionally disabled key-binding: %s, %s"
;;               (key-description (this-command-keys-vector))
;;               arg))

;;; Hide some minor mode from the mode line display.

;;;###autoload
(defmacro durand-hide-minor-mode (minor &optional to-require light)
  "Hide MINOR from the mode line.
Require TO-REQUIRE so that we don't have errors.

Optional LIGHT means to use the lighter name instead of
completely hiding it."
  ;; (cond (to-require (require to-require)))
  (cond
   (to-require `(eval-after-load ',to-require
                 (quote
                  (setcdr
                   (assq ',minor minor-mode-alist)
                   (list (or ,light ""))))))
   (`(setcdr (assq ',minor minor-mode-alist) (list (or ,light ""))))))

;;;###autoload
(defun durand-delete-dups (sequence &rest args)
  "Delete duplicate elements in SEQUENCE.
If the keyword argument TEST is non-nil, it should be a function
with two arguments which tests for equality of elements in the
sequence. The default is the function `equal'.

If the keyword argument KEY is non-nil, it should be a function
with one argument which returns the key of the element in the
sequence to be compared by the test function. The default is the
function `identity'.

Note that this function is not supposed to change global state,
including match data, so the functions in TEST and KEY are
supposed to leave the global state alone as well.

\(fn SEQUENCE &key TEST KEY)"
  (declare (pure t) (side-effect-free t))
  (let* ((len (length sequence))
         (temp-obarray (obarray-make len))
         (valid-key-num (+ (cond ((plist-member args :key) 1) (0))
                           (cond ((plist-member args :test) 1) (0))))
         (key (cond ((cadr (plist-member args :key)))
                    (#'identity)))
         (test-fn (cond ((cadr (plist-member args :test)))
                        (#'equal)))
         found-table result)
    (cond ((or (= (mod (length args) 2) 1)
               (> (length args) (* 2 valid-key-num)))
           (user-error "Invalid keyword arguments. Only :key and :test are allowed, but got %S"
                       args)))
    ;; Note: This just puts a property to the symbol.
    (define-hash-table-test 'durand-delete-dups-test
      test-fn (function (lambda (obj) (intern (format "%S" obj) temp-obarray))))
    (setq found-table (make-hash-table :test 'durand-delete-dups-test :size len))
    (mapc
     (function
      (lambda (element)
        (cond ((gethash (funcall key element) found-table))
              ;; Abuse the fact that `puthash' always returns VALUE.
              ((puthash (funcall key element) t found-table)
               (setq result (cons element result))))))
     sequence)
    (nreverse result)))

;;;###autoload
(defun durand-member (element ls &optional test)
  "Return the tail of LS whose car is ELT.
Comparison is done by TEST if that is non-nil, else by `equal'."
  (let ((temp (copy-tree ls))
        (not-found t))
    (while (and not-found (consp temp))
      (cond
       ((and test
             (funcall test element (car temp)))
        (setq not-found nil))
       (test (setq temp (cdr temp)))
       ((equal element (car temp))
        (setq not-found nil))
       ((setq temp (cdr temp)))))
    temp))

;;;###autoload
(defun durand-copy-line-dwim (&optional arg)
  "Copy line or duplicate line, according to ARG.
If ARG is nil, then duplicate line and put cursor in the line
below.

If ARG is '(4), just copy line.

If ARG is '(16), copy and duplicate line at the same time.

If ARG is a number strictly greater than 0, then copy and
duplicate the line ARG times.

In any other case, copy line and use minibuffer to read a number,
and duplicate the line at the line that is ARG away from the
current line; negative ARG means to put the line upwards."
  (interactive "P")
  (cond
   ((null arg)
    (let ((line (buffer-substring (line-beginning-position)
                                  (line-end-position))))
      (goto-char (line-end-position))
      (insert "\n" line)))
   ((equal arg (list 4))
    (let ((line (concat (buffer-substring (line-beginning-position)
                                          (line-end-position))
                        "\n")))
      (kill-new line)
      (message "Copied \"%s\"" (substring-no-properties line))))
   ((equal arg (list 16))
    (let ((line (buffer-substring (line-beginning-position)
                                  (line-end-position))))
      (kill-new (concat line "\n"))
      (message "Copied \"%s\n\"" (substring-no-properties line))
      (goto-char (line-end-position))
      (insert "\n")
      (insert line)))
   ((and (integerp arg)
         (> arg 0))
    (let* ((line (buffer-substring (line-beginning-position)
                                   (line-end-position)))
           (lines (mapconcat (lambda (_arg) line)
                             (number-sequence 1 arg)
                             "\n")))
      (kill-new (concat line "\n"))
      (message "Copied \"%s\n\"" (substring-no-properties line))
      (insert "\n" lines)))
   (t
    (let ((line (buffer-substring (line-beginning-position)
                                  (line-end-position)))
          (num (read-number "Duplicate to which line? ")))
      (kill-new (concat line "\n"))
      (message "Copied \"%s\n\"" (substring-no-properties line))
      (goto-char (line-end-position (cond ((/= num 0) (1+ num))
                                          (num))))
      (insert "\n" line)))))

;;; A minor helper function

;;;###autoload
(defun durand-take (n ls)
  "Return the first N items of LS.
If the length of LS is less than N, then return the whole LS."
  (declare (pure t) (side-effect-free t))
  (cond
   ((< (length ls) n) ls)
   ((let ((i 0) result)
      (while (< i n)
        (cond
         (ls
          (setq result (cons (car ls) result))
          (setq ls (cdr ls))
          (setq i (1+ i)))
         ((setq i n))))
      (reverse result)))))

;;; A helper function to remove the last non-cons-cell `cdr'.

(defun remove-last-non-nil-cdr (arg)
  "Remove the last `cdr' that is not a cons cell."
  (let ((temp arg))
    (while (consp temp)
      (cond
       ((consp (cdr temp)))
       ((setcdr temp nil)))
      (setq temp (cdr temp))))
  arg)

(provide 'common)
;;; common.el ends here.



;;; Archives

;; ;;;###autoload
;; (defvar yank-pop-completion-index 0
;;   "The index of the current kill.")

;; ;;;###autoload
;; (defun yank-pop-cycle-candidate (&optional arg)
;;   "Cycle the candidates by the offset ARG.
;; This is intended to be bound in the minibuffer-local-completion-map."
;;   (interactive "p")
;;   (cond ((minibufferp))
;;         ((listp minibuffer-completion-table))
;;         ((error "Function `yank-pop-cycle-candidate' called in a weird place")))
;;   (delete-minibuffer-contents)
;;   (let ((len (length minibuffer-completion-table)))
;;     (setq yank-pop-completion-index (+ arg yank-pop-completion-index))
;;     (insert (nth (mod yank-pop-completion-index len)
;;                  minibuffer-completion-table))))

;; ;;;###autoload
;; (defun yank-pop-completion-previous-candidate (&optional arg)
;;   "Go to the previous ARG candidate.
;; This is intended to be bound in the minibuffer-local-completion-map."
;;   (interactive "p")
;;   (yank-pop-cycle-candidate (- arg)))

;; ;;;###autoload
;; (defun yank-pop-completion-set-minibuffer ()
;;   "Set up minibuffer for use of `yank-pop-complete'."
;;   (setq yank-pop-completion-index 0)
;;   (define-key minibuffer-local-completion-map
;;     (vector ?\M-p) #'yank-pop-completion-previous-candidate)
;;   (define-key minibuffer-local-completion-map
;;     (vector ?\M-n) #'yank-pop-cycle-candidate)
;;   (add-hook 'minibuffer-exit-hook #'yank-pop-completion-clear))

;; ;;;###autoload
;; (defun yank-pop-completion-clear ()
;;   "Clear the traces of `yank-pop-complete'."
;;   (setq yank-pop-completion-index 0)
;;   (define-key minibuffer-local-completion-map
;;     (vector ?\M-p) #'previous-history-element)
;;   (define-key minibuffer-local-completion-map
;;     (vector ?\M-n) #'next-history-element)
;;   (remove-hook 'minibuffer-exit-hook #'yank-pop-completion-clear))