summaryrefslogtreecommitdiff
path: root/helm-ring.el
blob: 89a8fb85aae0a44f70ac2d5f2f39ceb4debf7855 (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
;;; helm-ring.el --- kill-ring, mark-ring, and register browsers for helm.

;; Copyright (C) 2012 Thierry Volpiatto <thierry.volpiatto@gmail.com>

;; 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 <http://www.gnu.org/licenses/>.

;;; Code:

(eval-when-compile (require 'cl))
(require 'helm)
(require 'helm-utils)

(declare-function undo-tree-restore-state-from-register "ext:undo-tree.el" (register))


(defgroup helm-ring nil
  "Ring related Applications and libraries for Helm."
  :group 'helm)

(defcustom helm-kill-ring-threshold 10
  "Minimum length of a candidate to be listed by `helm-c-source-kill-ring'."
  :type 'integer
  :group 'helm-ring)

(defcustom helm-c-kill-ring-max-lines-number nil
  "Max number of lines displayed per candidate in kill-ring browser.
If nil or zero, don't truncate candidate, show all."
  :type 'integer
  :group 'helm-ring)

(defcustom helm-c-register-max-offset 160
  "Max size of string register entries before truncating."
  :group 'helm-ring
  :type  'integer)


;;; Kill ring
;;
;;
(defvar helm-kill-ring-map
  (let ((map (make-sparse-keymap)))
    (set-keymap-parent map helm-map)
    (define-key map (kbd "M-y") 'helm-next-line)
    (define-key map (kbd "M-u") 'helm-previous-line)
    map)
  "Keymap for `helm-show-kill-ring'.")

(defvar helm-c-source-kill-ring
  `((name . "Kill Ring")
    (init . (lambda () (helm-attrset 'last-command last-command)))
    (candidates . helm-c-kill-ring-candidates)
    (filtered-candidate-transformer helm-c-kill-ring-transformer)
    (action . helm-c-kill-ring-action)
    (keymap . ,helm-kill-ring-map)
    (last-command)
    (migemo)
    (multiline))
  "Source for browse and insert contents of kill-ring.")

(defun helm-c-kill-ring-candidates ()
  (loop for kill in (helm-fast-remove-dups kill-ring :test 'equal)
        unless (or (< (length kill) helm-kill-ring-threshold)
                   (string-match "^\\(\\s-\\|\t\\)+$" kill))
        collect kill))

(defun helm-c-kill-ring-transformer (candidates source)
  "Display only the `helm-c-kill-ring-max-lines-number' lines of candidate."
  (loop for i in candidates
        for nlines = (with-temp-buffer (insert i) (count-lines (point-min) (point-max)))
        if (and helm-c-kill-ring-max-lines-number
                (> nlines helm-c-kill-ring-max-lines-number))
        collect (cons
                 (with-temp-buffer
                   (insert i)
                   (goto-char (point-min))
                   (concat
                    (buffer-substring
                     (point-min)
                     (save-excursion
                       (forward-line helm-c-kill-ring-max-lines-number)
                       (point)))
                    "[...]")) i)
        else collect i))

(defun helm-c-kill-ring-action (str)
  "Insert STR in `kill-ring' and set STR to the head.
If this action is executed just after `yank',
replace with STR as yanked string."
  (setq kill-ring (delete str kill-ring))
  (if (not (eq (helm-attr 'last-command) 'yank))
      (with-helm-current-buffer (insert-for-yank str))
      ;; from `yank-pop'
      (let ((inhibit-read-only t)
            (before (< (point) (mark t))))
        (if before
            (funcall (or yank-undo-function 'delete-region) (point) (mark t))
            (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
        (setq yank-undo-function nil)
        (set-marker (mark-marker) (point) helm-current-buffer)
        (with-helm-current-buffer (insert-for-yank str))
        ;; Set the window start back where it was in the yank command,
        ;; if possible.
        (set-window-start (selected-window) yank-window-start t)
        (if before
            ;; This is like exchange-point-and-mark, but doesn't activate the mark.
            ;; It is cleaner to avoid activation, even though the command
            ;; loop would deactivate the mark because we inserted text.
            (goto-char (prog1 (mark t)
                         (set-marker (mark-marker) (point) helm-current-buffer))))))
  (kill-new str))



;;;; <Mark ring>
;; DO NOT include these sources in `helm-sources' use
;; the commands `helm-mark-ring', `helm-global-mark-ring' or
;; `helm-all-mark-rings' instead.

(defun helm-mark-ring-get-marks (pos)
  (save-excursion
    (goto-char pos)
    (beginning-of-line)
    (let ((line  (car (split-string (thing-at-point 'line) "[\n\r]"))))
      (when (string= "" line)
        (setq line  "<EMPTY LINE>"))
      (format "%7d: %s" (line-number-at-pos) line))))

(defun helm-mark-ring-get-candidates ()
  (with-helm-current-buffer
    (loop with marks = (if (mark) (cons (mark-marker) mark-ring) mark-ring)
          with recip = nil
          for i in marks
          for m = (helm-mark-ring-get-marks i)
          unless (member m recip)
          collect m into recip
          finally return recip)))

(defvar helm-mark-ring-cache nil)
(defvar helm-c-source-mark-ring
  '((name . "mark-ring")
    (init . (lambda ()
              (setq helm-mark-ring-cache
                    (ignore-errors (helm-mark-ring-get-candidates)))))
    (candidates . (lambda ()
                    (helm-aif helm-mark-ring-cache
                        it)))
    (action . (("Goto line"
                . (lambda (candidate)
                    (helm-goto-line (string-to-number candidate))
                    (push-mark nil 'nomsg))))) 
    (persistent-action . (lambda (candidate)
                           (helm-goto-line (string-to-number candidate))
                           (helm-match-line-color-current-line)))
    (persistent-help . "Show this line")))


;;; Global-mark-ring
(defvar helm-c-source-global-mark-ring
  '((name . "global-mark-ring")
    (candidates . helm-global-mark-ring-get-candidates)
    (action . (("Goto line"
                . (lambda (candidate)
                    (let ((items (split-string candidate ":")))
                      (helm-c-switch-to-buffer (second items))
                      (helm-goto-line (string-to-number (car items))))))))
    (persistent-action . (lambda (candidate)
                           (let ((items (split-string candidate ":")))
                             (helm-c-switch-to-buffer (second items))
                             (helm-goto-line (string-to-number (car items)))
                             (helm-match-line-color-current-line))))
    (persistent-help . "Show this line")))

(defun helm-global-mark-ring-format-buffer (marker)
  (with-current-buffer (marker-buffer marker)
    (goto-char marker)
    (beginning-of-line)
    (let (line)
      (if (string= "" line)
          (setq line  "<EMPTY LINE>")
          (setq line (car (split-string (thing-at-point 'line)
                                        "[\n\r]"))))
      (format "%7d:%s:    %s"
              (line-number-at-pos) (marker-buffer marker) line))))

(defun helm-global-mark-ring-get-candidates ()
  (loop with marks = global-mark-ring
        with recip = nil
        for i in marks
        for gm = (unless (or (string-match
                              "^ " (format "%s" (marker-buffer i)))
                             (null (marker-buffer i)))
                   (helm-global-mark-ring-format-buffer i))
        when (and gm (not (member gm recip)))
        collect gm into recip
        finally return recip))


;;;; <Register>
;;; Insert from register
(defvar helm-c-source-register
  '((name . "Registers")
    (candidates . helm-c-register-candidates)
    (action-transformer . helm-c-register-action-transformer)
    (multiline)
    (action))
  "See (info \"(emacs)Registers\")")

(defun helm-c-register-candidates ()
  "Collecting register contents and appropriate commands."
  (loop for (char . val) in register-alist
        for key    = (single-key-description char)
        for string-actions =
        (cond
          ((numberp val)
           (list (int-to-string val)
                 'insert-register
                 'increment-register))
          ((markerp val)
           (let ((buf (marker-buffer val)))
             (if (null buf)
                 (list "a marker in no buffer")
                 (list (concat
                        "a buffer position:"
                        (buffer-name buf)
                        ", position "
                        (int-to-string (marker-position val)))
                       'jump-to-register
                       'insert-register))))
          ((and (consp val) (window-configuration-p (car val)))
           (list "window configuration."
                 'jump-to-register))
          ((and (consp val) (frame-configuration-p (car val)))
           (list "frame configuration."
                 'jump-to-register))
          ((and (consp val) (eq (car val) 'file))
           (list (concat "file:"
                         (prin1-to-string (cdr val))
                         ".")
                 'jump-to-register))
          ((and (consp val) (eq (car val) 'file-query))
           (list (concat "file:a file-query reference: file "
                         (car (cdr val))
                         ", position "
                         (int-to-string (car (cdr (cdr val))))
                         ".")
                 'jump-to-register))
          ((consp val)
           (let ((lines (format "%4d" (length val))))
             (list (format "%s: %s\n" lines
                           (truncate-string-to-width
                            (mapconcat 'identity (list (car val))
                                       "^J") (- (window-width) 15)))
                   'insert-register)))
          ((stringp val)
           (list
            ;; without properties
            (concat (substring-no-properties
                     val 0 (min (length val) helm-c-register-max-offset))
                    (if (> (length val) helm-c-register-max-offset)
                        "[...]" ""))
            'insert-register
            'append-to-register
            'prepend-to-register))
          ((vectorp val)
           (list
            "Undo-tree entry."
            'undo-tree-restore-state-from-register))
          (t
           "GARBAGE!"))
        collect (cons (format "Register %3s:\n %s" key (car string-actions))
                      (cons char (cdr string-actions)))))

(defun helm-c-register-action-transformer (actions register-and-functions)
  "Decide actions by the contents of register."
  (loop with func-actions =
        '((insert-register
           "Insert Register" .
           (lambda (c) (insert-register (car c))))
          (jump-to-register
           "Jump to Register" .
           (lambda (c) (jump-to-register (car c))))
          (append-to-register
           "Append Region to Register" .
           (lambda (c) (append-to-register
                        (car c) (region-beginning) (region-end))))
          (prepend-to-register
           "Prepend Region to Register" .
           (lambda (c) (prepend-to-register
                        (car c) (region-beginning) (region-end))))
          (increment-register
           "Increment Prefix Arg to Register" .
           (lambda (c) (increment-register
                        helm-current-prefix-arg (car c))))
          (undo-tree-restore-state-from-register
           "Restore Undo-tree register"
           (lambda (c) (and (fboundp 'undo-tree-restore-state-from-register)
                            (undo-tree-restore-state-from-register (car c))))))
        for func in (cdr register-and-functions)
        for cell = (assq func func-actions)
        when cell
        collect (cdr cell)))

;;;###autoload
(defun helm-mark-ring ()
  "Preconfigured `helm' for `helm-c-source-mark-ring'."
  (interactive)
  (helm :sources 'helm-c-source-mark-ring))

;;;###autoload
(defun helm-global-mark-ring ()
  "Preconfigured `helm' for `helm-c-source-global-mark-ring'."
  (interactive)
  (helm :sources 'helm-c-source-global-mark-ring))

;;;###autoload
(defun helm-all-mark-rings ()
  "Preconfigured `helm' for `helm-c-source-global-mark-ring' and \
`helm-c-source-mark-ring'."
  (interactive)
  (helm :sources '(helm-c-source-mark-ring
                   helm-c-source-global-mark-ring)))

;;;###autoload
(defun helm-register ()
  "Preconfigured `helm' for Emacs registers."
  (interactive)
  (helm-other-buffer 'helm-c-source-register "*helm register*"))

;;;###autoload
(defun helm-show-kill-ring ()
  "Preconfigured `helm' for `kill-ring'.
It is drop-in replacement of `yank-pop'.

First call open the kill-ring browser, next calls move to next line."
  (interactive)
  (helm :sources helm-c-source-kill-ring
        :buffer "*helm kill-ring*"
        :allow-nest t))

(provide 'helm-ring)

;; Local Variables:
;; byte-compile-warnings: (not cl-functions obsolete)
;; coding: utf-8
;; indent-tabs-mode: nil
;; End:

;;; helm-ring.el ends here