Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 338 lines (288 sloc) 12.95 kb
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
1 ;;; helm-plugin.el --- Helm plugins
2
3 ;; Copyright (C) 2012 Thierry Volpiatto <thierry.volpiatto@gmail.com>
4
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
9
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 ;;; Code:
19
20 (require 'cl)
21 (require 'helm)
22
7d8197a @thierryvolpiatto Fix warnings at compilation.
thierryvolpiatto authored
23 (declare-function Info-index-nodes "info" (&optional file))
24 (declare-function Info-goto-node "info" (&optional fork))
25 (declare-function Info-find-node "info.el" (filename nodename &optional no-going-back))
26
8fa7f6d @thierryvolpiatto * helm-plugin.el: Add comments, reorder.
thierryvolpiatto authored
27 ;;; Plug-in: `info-index'
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
28 ;;
29 ;;
30 (defun* helm-c-info-init (&optional (file (helm-attr 'info-file)))
31 (let (result)
32 (unless (helm-candidate-buffer)
33 (save-window-excursion
34 (info file)
35 (let (Info-history
36 (tobuf (helm-candidate-buffer 'global))
37 (infobuf (current-buffer))
38 s e)
39 (dolist (node (or (helm-attr 'index-nodes) (Info-index-nodes)))
40 (Info-goto-node node)
41 (goto-char (point-min))
42 (while (search-forward "\n* " nil t)
43 (unless (search-forward "Menu:\n" (1+ (point-at-eol)) t)
44 '(save-current-buffer (buffer-substring-no-properties (point-at-bol) (point-at-eol)) result)
45 (setq s (point-at-bol)
46 e (point-at-eol))
47 (with-current-buffer tobuf
48 (insert-buffer-substring infobuf s e)
49 (insert "\n"))))))))))
50
51 (defun helm-c-info-goto (node-line)
52 (Info-goto-node (car node-line))
53 (helm-goto-line (cdr node-line)))
54
55 (defun helm-c-info-display-to-real (line)
56 (and (string-match
57 ;; This regexp is stolen from Info-apropos-matches
58 "\\* +\\([^\n]*.+[^\n]*\\):[ \t]+\\([^\n]*\\)\\.\\(?:[ \t\n]*(line +\\([0-9]+\\))\\)?" line)
59 (cons (format "(%s)%s" (helm-attr 'info-file) (match-string 2 line))
60 (string-to-number (or (match-string 3 line) "1")))))
61
62 (defun helm-c-make-info-source (source file)
63 `(,@source
64 (name . ,(concat "Info Index: " file))
65 (info-file . ,file)
66 (init . helm-c-info-init)
67 (display-to-real . helm-c-info-display-to-real)
68 (get-line . buffer-substring)
69 (candidates-in-buffer)
70 (action ("Goto node" . helm-c-info-goto))))
71
72 (defun helm-compile-source--info-index (source)
73 (helm-aif (helm-interpret-value (assoc-default 'info-index source))
74 (helm-c-make-info-source source it)
75 source))
8fa7f6d @thierryvolpiatto * helm-plugin.el: Add comments, reorder.
thierryvolpiatto authored
76
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
77 (add-to-list 'helm-compile-source-functions 'helm-compile-source--info-index)
78
79 (helm-document-attribute 'info-index "info-index plugin"
80 "Create a source of info index very easily.
81
82 ex. (defvar helm-c-source-info-wget '((info-index . \"wget\"))")
83
84 (helm-document-attribute 'index-nodes "info-index plugin (optional)"
85 "Index nodes of info file.
86
87 If it is omitted, `Info-index-nodes' is used to collect index nodes.
88 Some info files are missing index specification.
89
90 ex. See `helm-c-source-info-screen'.")
91
8fa7f6d @thierryvolpiatto * helm-plugin.el: Add comments, reorder.
thierryvolpiatto authored
92 ;;; Plug-in: `candidates-file'
93 ;;
94 ;; List all lines in a file.
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
95 (defun helm-compile-source--candidates-file (source)
96 (if (assoc-default 'candidates-file source)
8fa7f6d @thierryvolpiatto * helm-plugin.el: Add comments, reorder.
thierryvolpiatto authored
97 `((init helm-p-candidates-file-init
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
98 ,@(let ((orig-init (assoc-default 'init source)))
99 (cond ((null orig-init) nil)
100 ((functionp orig-init) (list orig-init))
101 (t orig-init))))
102 (candidates-in-buffer)
103 ,@source)
104 source))
105 (add-to-list 'helm-compile-source-functions 'helm-compile-source--candidates-file)
106
8fa7f6d @thierryvolpiatto * helm-plugin.el: Add comments, reorder.
thierryvolpiatto authored
107 (defun helm-p-candidates-file-init ()
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
108 (destructuring-bind (file &optional updating)
109 (helm-mklist (helm-attr 'candidates-file))
110 (setq file (helm-interpret-value file))
111 (with-current-buffer (helm-candidate-buffer (find-file-noselect file))
112 (when updating
113 (buffer-disable-undo)
114 (font-lock-mode -1)
115 (auto-revert-mode 1)))))
116
117 (helm-document-attribute 'candidates-file "candidates-file plugin"
118 "Use a file as the candidates buffer.
119
120 1st argument is a filename, string or function name or variable name.
8fa7f6d @thierryvolpiatto * helm-plugin.el: Add comments, reorder.
thierryvolpiatto authored
121 If optional 2nd argument is non-nil, the file is opened with
122 `auto-revert-mode' enabled.
123
124 e.g
125
126 \(defvar helm-c-source-test-file
127 '((name . \"test1\")
128 (candidates-file \"~/.emacs.el\" t)))
129
130 Will list all lines in .emacs.el.")
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
131
8fa7f6d @thierryvolpiatto * helm-plugin.el: Add comments, reorder.
thierryvolpiatto authored
132 ;;; Plug-in: `headline'
133 ;;
134 ;;
135 ;; Le Wang: Note on how `helm-head-line-get-candidates' works with a list
136 ;; of regexps.
137 ;;
138 ;; 1. Create list of ((title . start-of-match) . hiearchy)
139 ;; 2. Sort this list by start-of-match.
140 ;; 3. Go through sorted list and return titles that reflect full hiearchy.
141 ;;
142 ;; It's quite brilliantly written.
143 ;;
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
144 (defun helm-compile-source--helm-headline (source)
145 (if (assoc-default 'headline source)
146 (append '((init . helm-headline-init)
147 (get-line . buffer-substring)
148 (type . line))
149 source
150 '((candidates-in-buffer)
151 (persistent-help . "Show this line")))
152 source))
153 (add-to-list 'helm-compile-source-functions 'helm-compile-source--helm-headline)
154
155 (defun helm-headline-init ()
156 (when (and (helm-current-buffer-is-modified)
157 (with-helm-current-buffer
158 (eval (or (helm-attr 'condition) t))))
159 (helm-headline-make-candidate-buffer
160 (helm-interpret-value (helm-attr 'headline))
161 (helm-interpret-value (helm-attr 'subexp)))))
162
163 (helm-document-attribute 'headline "Headline plug-in"
164 "Regexp string for helm-headline to scan.")
165 (helm-document-attribute 'condition "Headline plug-in"
166 "A sexp representing the condition to use helm-headline.")
167 (helm-document-attribute 'subexp "Headline plug-in"
168 "Display (match-string-no-properties subexp).")
169
170 (defun helm-headline-get-candidates (regexp subexp)
171 (with-helm-current-buffer
172 (save-excursion
173 (goto-char (point-min))
174 (if (functionp regexp) (setq regexp (funcall regexp)))
175 (let (hierarchy curhead)
176 (flet ((matched ()
177 (if (numberp subexp)
178 (cons (match-string-no-properties subexp) (match-beginning subexp))
179 (cons (buffer-substring (point-at-bol) (point-at-eol))
180 (point-at-bol))))
181 (hierarchies (headlines)
182 (1+ (loop for (_ . hierarchy) in headlines
183 maximize hierarchy)))
184 (vector-0-n (v n)
185 (loop for i from 0 to hierarchy
186 collecting (aref curhead i)))
187 (arrange (headlines)
188 (unless (null headlines) ; FIX headlines empty bug!
189 (loop with curhead = (make-vector (hierarchies headlines) "")
190 for ((str . pt) . hierarchy) in headlines
191 do (aset curhead hierarchy str)
192 collecting
193 (cons
194 (format "H%d:%s" (1+ hierarchy)
195 (mapconcat 'identity (vector-0-n curhead hierarchy) " / "))
196 pt)))))
197 (if (listp regexp)
198 (arrange
199 (sort
200 (loop for re in regexp
201 for hierarchy from 0
202 do (goto-char (point-min))
203 appending
204 (loop
205 while (re-search-forward re nil t)
206 collect (cons (matched) hierarchy)))
207 (lambda (a b) (> (cdar b) (cdar a)))))
208 (loop while (re-search-forward regexp nil t)
209 collect (matched))))))))
210
211
212 (defun helm-headline-make-candidate-buffer (regexp subexp)
213 (with-current-buffer (helm-candidate-buffer 'local)
214 (loop for (content . pos) in (helm-headline-get-candidates regexp subexp)
215 do (insert
216 (format "%5d:%s\n"
217 (with-helm-current-buffer
218 (line-number-at-pos pos))
219 content)))))
220
221 (defun helm-headline-goto-position (pos recenter)
222 (goto-char pos)
223 (unless recenter
224 (set-window-start (get-buffer-window helm-current-buffer) (point))))
225
226
8fa7f6d @thierryvolpiatto * helm-plugin.el: Add comments, reorder.
thierryvolpiatto authored
227 ;;; Plug-in: `persistent-help'
228 ;;
229 ;; Add help about persistent action in `helm-buffer' header.
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
230 (defun helm-compile-source--persistent-help (source)
231 (append source '((header-line . helm-persistent-help-string))))
232 (add-to-list 'helm-compile-source-functions 'helm-compile-source--persistent-help)
233
234 (defun helm-persistent-help-string ()
5a765e6 @thierryvolpiatto * helm.el (helm-display-mode-line): Fix header-line-format.
thierryvolpiatto authored
235 (substitute-command-keys
236 (concat "\\<helm-map>\\[helm-execute-persistent-action]: "
237 (or (helm-interpret-value (helm-attr 'persistent-help))
238 (helm-aif (or (assoc-default
239 'persistent-action
240 (helm-get-current-source))
241 (assoc-default
242 'action (helm-get-current-source)))
243 (cond ((symbolp it)
244 (symbol-name it))
245 ((listp it)
246 (or (ignore-errors (caar it)) ""))))
247 "")
248 " (keeping session)")))
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
249
250 (helm-document-attribute 'persistent-help "persistent-help plug-in"
251 "A string to explain persistent-action of this source.
252 It also accepts a function or a variable name.")
253
254
8fa7f6d @thierryvolpiatto * helm-plugin.el: Add comments, reorder.
thierryvolpiatto authored
255 ;;; Plug-in: Type `customize'
256 ;;
257 ;;
258 (defvar helm-additional-type-attributes nil)
259
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
260 (defun helm-c-uniq-list (lst)
261 "Like `remove-duplicates' in CL.
262 But cut deeper duplicates and test by `equal'. "
263 (reverse (remove-duplicates (reverse lst) :test 'equal)))
8fa7f6d @thierryvolpiatto * helm-plugin.el: Add comments, reorder.
thierryvolpiatto authored
264
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
265 (defun helm-c-arrange-type-attribute (type spec)
266 "Override type attributes by `define-helm-type-attribute'.
267
268 The SPEC is like source. The symbol `REST' is replaced
269 with original attribute value.
270
271 Example: Set `play-sound-file' as default action
272 (helm-c-arrange-type-attribute 'file
273 '((action (\"Play sound\" . play-sound-file)
274 REST ;; Rest of actions (find-file, find-file-other-window, etc...)."
275 (add-to-list 'helm-additional-type-attributes
276 (cons type
277 (loop with typeattr = (assoc-default
278 type helm-type-attributes)
279 for (attr . value) in spec
280 if (listp value)
281 collect (cons attr
282 (helm-c-uniq-list
283 (loop for v in value
284 if (eq v 'REST)
285 append
286 (assoc-default attr typeattr)
287 else
288 collect v)))
289 else
290 collect (cons attr value)))))
291 (put 'helm-c-arrange-type-attribute 'lisp-indent-function 1)
292
293 (defun helm-compile-source--type-customize (source)
294 (helm-aif (assoc-default (assoc-default 'type source)
295 helm-additional-type-attributes)
296 (append it source)
297 source))
8fa7f6d @thierryvolpiatto * helm-plugin.el: Add comments, reorder.
thierryvolpiatto authored
298
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
299 (add-to-list 'helm-compile-source-functions
300 'helm-compile-source--type-customize t)
301
8fa7f6d @thierryvolpiatto * helm-plugin.el: Add comments, reorder.
thierryvolpiatto authored
302 ;;; Plug-in: `default-action'
303 ;;
304 ;;
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
305 (defun helm-compile-source--default-action (source)
306 (helm-aif (assoc-default 'default-action source)
307 (append `((action ,it ,@(remove it (assoc-default 'action source))))
308 source)
309 source))
310 (add-to-list 'helm-compile-source-functions
311 'helm-compile-source--default-action t)
a1b8c7b @thierryvolpiatto Finish merging and removing obsolete stuff from helm-config.el
thierryvolpiatto authored
312
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
313 (helm-document-attribute 'default-action "default-action plug-in"
314 "Default action.")
a1b8c7b @thierryvolpiatto Finish merging and removing obsolete stuff from helm-config.el
thierryvolpiatto authored
315 (helm-document-attribute 'default-directory "type . file-line"
316 "`default-directory' to interpret file.")
317 (helm-document-attribute 'before-jump-hook "type . file-line / line"
318 "Function to call before jumping to the target location.")
319 (helm-document-attribute 'after-jump-hook "type . file-line / line"
320 "Function to call after jumping to the target location.")
321 (helm-document-attribute 'adjust "type . file-line"
322 "Search around line matching line contents.")
323 (helm-document-attribute 'recenter "type . file-line / line"
324 "`recenter' after jumping.")
325 (helm-document-attribute 'target-file "type . line"
326 "Goto line of target-file.")
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
327
328 (provide 'helm-plugin)
329
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
330 ;; Local Variables:
a7ee65b @thierryvolpiatto Disable compile warnings and add new keyword to helm.
thierryvolpiatto authored
331 ;; byte-compile-warnings: (not cl-functions obsolete)
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
332 ;; coding: utf-8
333 ;; indent-tabs-mode: nil
334 ;; byte-compile-dynamic: t
335 ;; End:
336
6cbdbb4 @thierryvolpiatto * helm-plugin.el: new
thierryvolpiatto authored
337 ;;; helm-plugin ends here
Something went wrong with that request. Please try again.