Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 792 lines (703 sloc) 30.82 kB
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
1 ;;; helm-utils.el --- Utilities Functions for helm.
2
3 ;; Copyright (C) 2012 Thierry Volpiatto <thierry.volpiatto@gmail.com>
4
3b91dac re #6 - cleaned up headers and added helm-pkg.el for package.el compa…
Bozhidar Batsov authored
5 ;; This program is free software; you can redistribute it and/or modify
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
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)
66550b2 @thierryvolpiatto * helm-buffers.el: require cl, helm, helm-vars, helm-utils.
thierryvolpiatto authored
21 (require 'helm)
22 (require 'compile) ; Fixme: Is this needed?
23 (require 'dired)
24
1145cdd @thierryvolpiatto * helm-utils.el Remove eval-defun hack.
thierryvolpiatto authored
25 (declare-function helm-find-files-1 "helm-files.el" (fname &optional preselect))
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
26
a4d6d28 @thierryvolpiatto * helm-config.el: Remove users variables and move to helm-vars.el.
thierryvolpiatto authored
27
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
28 (defgroup helm-utils nil
29 "Utilities routines for Helm."
30 :group 'helm)
31
32 (defcustom helm-su-or-sudo "sudo"
33 "What command to use for root access."
34 :type 'string
35 :group 'helm-utils)
36
8c4e289 add `helm-yank-symbol-first' option
Le Wang authored
37 (defcustom helm-yank-symbol-first nil
38 "`helm-yank-text-at-point' yanks symbol at point on first
39 invocation if this is non-nil."
40 :type 'boolean
41 :group 'helm-utils)
42
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
43 (defcustom helm-default-kbsize 1024.0
44 "Default Kbsize to use for showing files size.
45 It is a float, usually 1024.0 but could be 1000.0 on some systems."
46 :group 'helm-utils
47 :type 'float)
48
ce3f7a4 @thierryvolpiatto * helm-utils.el (helm-selection-line): Renamed from helm-overlay-line…
thierryvolpiatto authored
49 (defface helm-selection-line
34755a4 @thierryvolpiatto * helm.el (helm-selection): Use a default face visible on marked cand…
thierryvolpiatto authored
50 '((t (:background "IndianRed4" :underline t)))
ce3f7a4 @thierryvolpiatto * helm-utils.el (helm-selection-line): Renamed from helm-overlay-line…
thierryvolpiatto authored
51 "Face used in the `helm-current-buffer' when jumping to candidate."
34755a4 @thierryvolpiatto * helm.el (helm-selection): Use a default face visible on marked cand…
thierryvolpiatto authored
52 :group 'helm-utils)
53
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
54
a4d6d28 @thierryvolpiatto * helm-config.el: Remove users variables and move to helm-vars.el.
thierryvolpiatto authored
55 ;;; compatibility
56 ;;
57 ;;
58 (unless (fboundp 'window-system)
59 (defun window-system (&optional arg)
60 window-system))
61
62 (unless (fboundp 'make-composed-keymap)
63 (defun make-composed-keymap (maps &optional parent)
64 "Construct a new keymap composed of MAPS and inheriting from PARENT.
65 When looking up a key in the returned map, the key is looked in each
66 keymap of MAPS in turn until a binding is found.
67 If no binding is found in MAPS, the lookup continues in PARENT, if non-nil.
68 As always with keymap inheritance, a nil binding in MAPS overrides
69 any corresponding binding in PARENT, but it does not override corresponding
70 bindings in other keymaps of MAPS.
71 MAPS can be a list of keymaps or a single keymap.
72 PARENT if non-nil should be a keymap."
73 `(keymap
74 ,@(if (keymapp maps) (list maps) maps)
75 ,@parent)))
76
77 (unless (fboundp 'apply-partially)
78 (defun apply-partially (fun &rest args)
79 "Return a function that is a partial application of FUN to ARGS.
80 ARGS is a list of the first N arguments to pass to FUN.
81 The result is a new function which does the same as FUN, except that
82 the first N arguments are fixed at the values with which this function
83 was called."
84 (lexical-let ((fun fun) (args1 args))
85 (lambda (&rest args2) (apply fun (append args1 args2))))))
06cc23e @thierryvolpiatto * helm.el: Move help related things to helm-help.el.
thierryvolpiatto authored
86
87 (unless (fboundp 'assoc-default)
88 (defun assoc-default (key alist &optional test default)
89 "Find object KEY in a pseudo-alist ALIST.
90 ALIST is a list of conses or objects. Each element (or the element's car,
91 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
92 If that is non-nil, the element matches;
93 then `assoc-default' returns the element's cdr, if it is a cons,
94 or DEFAULT if the element is not a cons.
95
96 If no element matches, the value is nil.
97 If TEST is omitted or nil, `equal' is used."
98 (let (found (tail alist) value)
99 (while (and tail (not found))
100 (let ((elt (car tail)))
101 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
102 (setq found t value (if (consp elt) (cdr elt) default))))
103 (setq tail (cdr tail)))
104 value)))
105
106 ;; Function not available in XEmacs,
107 (unless (fboundp 'minibuffer-contents)
108 (defun minibuffer-contents ()
109 "Return the user input in a minbuffer as a string.
110 The current buffer must be a minibuffer."
111 (field-string (point-max)))
112
113 (defun delete-minibuffer-contents ()
114 "Delete all user input in a minibuffer.
115 The current buffer must be a minibuffer."
116 (delete-field (point-max))))
117
118 ;; Function not available in older Emacs (<= 22.1).
119 (unless (fboundp 'buffer-chars-modified-tick)
120 (defun buffer-chars-modified-tick (&optional buffer)
121 "Return BUFFER's character-change tick counter.
122 Each buffer has a character-change tick counter, which is set to the
123 value of the buffer's tick counter (see `buffer-modified-tick'), each
124 time text in that buffer is inserted or deleted. By comparing the
125 values returned by two individual calls of `buffer-chars-modified-tick',
126 you can tell whether a character change occurred in that buffer in
127 between these calls. No argument or nil as argument means use current
128 buffer as BUFFER."
129 (with-current-buffer (or buffer (current-buffer))
130 (if (listp buffer-undo-list)
131 (length buffer-undo-list)
132 (buffer-modified-tick)))))
133
134
135 ;; CUA workaround
136 (defadvice cua-delete-region (around helm-avoid-cua activate)
137 (ignore-errors ad-do-it))
138
139 (defadvice copy-region-as-kill (around helm-avoid-cua activate)
140 (if cua-mode
141 (ignore-errors ad-do-it)
142 ad-do-it))
143
a4d6d28 @thierryvolpiatto * helm-config.el: Remove users variables and move to helm-vars.el.
thierryvolpiatto authored
144
145 ;;; Utils functions
146 ;;
147 ;;
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
148 (defun helm-ff-find-printers ()
149 "Return a list of available printers on Unix systems."
150 (when (executable-find "lpstat")
151 (let ((printer-list (with-temp-buffer
152 (call-process "lpstat" nil t nil "-a")
153 (split-string (buffer-string) "\n"))))
154 (loop for p in printer-list
155 for printer = (car (split-string p))
156 when printer
157 collect printer))))
158
159 ;; Shut up byte compiler in emacs24*.
160 (defun helm-c-switch-to-buffer (buffer-or-name)
161 "Same as `switch-to-buffer' whithout warnings at compile time."
162 (with-no-warnings
163 (switch-to-buffer buffer-or-name)))
164
165 (defun* helm-c-position (item seq &key (test 'eq) all)
166 "A simple and faster replacement of CL `position'.
167 Return position of first occurence of ITEM found in SEQ.
168 Argument SEQ can be a string, in this case ITEM have to be a char.
169 Argument ALL, if non--nil specify to return a list of positions of
170 all ITEM found in SEQ."
171 (let ((key (if (stringp seq) 'across 'in)))
172 (eval
173 `(loop for c ,key seq
174 for index from 0
175 when (funcall test c item)
176 if all collect index into ls
177 else return index
178 finally return ls))))
179
180 (defun helm-c-get-pid-from-process-name (process-name)
181 "Get pid from running process PROCESS-NAME."
182 (loop with process-list = (list-system-processes)
183 for pid in process-list
184 for process = (assoc-default 'comm (process-attributes pid))
185 when (and process (string-match process-name process))
186 return pid))
187
188 (defun* helm-current-buffer-narrowed-p (&optional
189 (buffer helm-current-buffer))
190 "Check if BUFFER is narrowed.
191 Default is `helm-current-buffer'."
192 (with-current-buffer buffer
193 (let ((beg (point-min))
194 (end (point-max))
195 (total (buffer-size)))
196 (or (/= beg 1) (/= end (1+ total))))))
197
198 (defun helm-region-active-p ()
199 (and transient-mark-mode mark-active (/= (mark) (point))))
200
201 (defun helm-goto-char (loc)
202 "Go to char, revealing if necessary."
203 (goto-char loc)
204 (when (or (eq major-mode 'org-mode)
205 (and (boundp 'outline-minor-mode)
206 outline-minor-mode))
207 (require 'org) ; On some old Emacs versions org may not be loaded.
208 (org-reveal)))
209
210 (defun helm-goto-line (lineno &optional noanim)
211 "Goto LINENO opening only outline headline if needed.
212 Animation is used unless NOANIM is non--nil."
213 (goto-char (point-min))
214 (helm-goto-char (point-at-bol lineno))
215 (unless noanim
216 (helm-match-line-color-current-line)
217 (sit-for 0.3)
218 (helm-match-line-cleanup)))
219
220 ;;;###autoload
cd1aa7e @thierryvolpiatto New command to show all and some arrangements.
thierryvolpiatto authored
221 (defun helm-show-all-in-this-source-only (arg)
222 "Show only current source of this helm session with all its candidates.
223 With a numeric prefix arg show only the ARG number of candidates."
224 (interactive "p")
225 (with-helm-window
226 (let ((helm-candidate-number-limit (and (> arg 1) arg)))
227 (save-window-excursion
228 (helm-set-source-filter
229 (list (assoc-default 'name (helm-get-current-source))))))))
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
230
231 (defun helm-displaying-source-names ()
cd1aa7e @thierryvolpiatto New command to show all and some arrangements.
thierryvolpiatto authored
232 "Return the list of sources name for this helm session."
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
233 (with-current-buffer helm-buffer
234 (goto-char (point-min))
235 (loop with pos
236 while (setq pos (next-single-property-change (point) 'helm-header))
237 do (goto-char pos)
238 collect (buffer-substring-no-properties (point-at-bol)(point-at-eol))
239 do (forward-line 1))))
240
241 (defun helm-c-match-on-file-name (candidate)
242 "Return non-nil if `helm-pattern' match basename of filename CANDIDATE."
243 (string-match helm-pattern (file-name-nondirectory candidate)))
244
245 (defun helm-c-match-on-directory-name (candidate)
246 "Return non-nil if `helm-pattern' match directory part of CANDIDATE."
247 (helm-aif (file-name-directory candidate)
248 (string-match helm-pattern it)))
249
250 (defun helm-c-match-on-basename (candidate)
251 "Return non-nil if `helm-pattern' match basename of filename CANDIDATE."
252 (string-match helm-pattern (helm-c-basename candidate)))
253
254 (defun helm-c-string-match (candidate)
255 "Return non-nil if `helm-pattern' match CANDIDATE.
256 The match is done with `string-match'."
257 (string-match helm-pattern candidate))
258
de6cb44 @thierryvolpiatto Fix shadow/skip boring files/buffers.
thierryvolpiatto authored
259 (defun helm-skip-entries (seq regexp-list)
260 "Remove entries which matches one of REGEXP-LIST from SEQ."
261 (loop for i in seq
262 unless (loop for regexp in regexp-list
263 thereis (and (stringp i)
264 (string-match regexp i)))
265 collect i))
266
267 (defun helm-shadow-entries (seq regexp-list)
268 "Put shadow property on entries in SEQ matching a regexp in REGEXP-LIST."
269 (let ((face 'italic))
270 (loop for i in seq
271 if (loop for regexp in regexp-list
272 thereis (and (stringp i)
273 (string-match regexp i)))
274 collect (propertize i 'face face)
275 else collect i)))
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
276
277 (defun helm-c-stringify (str-or-sym)
278 "Get string of STR-OR-SYM."
279 (if (stringp str-or-sym)
280 str-or-sym
281 (symbol-name str-or-sym)))
282
283 (defun helm-c-symbolify (str-or-sym)
284 "Get symbol of STR-OR-SYM."
285 (if (symbolp str-or-sym)
286 str-or-sym
287 (intern str-or-sym)))
288
289 (defun helm-c-describe-function (func)
290 "FUNC is symbol or string."
291 (describe-function (helm-c-symbolify func)))
292
293 (defun helm-c-describe-variable (var)
294 "VAR is symbol or string."
295 (describe-variable (helm-c-symbolify var)))
296
297 (defun helm-c-find-function (func)
298 "FUNC is symbol or string."
299 (find-function (helm-c-symbolify func)))
300
301 (defun helm-c-find-variable (var)
302 "VAR is symbol or string."
303 (find-variable (helm-c-symbolify var)))
304
305 (defun helm-c-kill-new (candidate &optional replace)
306 "CANDIDATE is symbol or string.
307 See `kill-new' for argument REPLACE."
308 (kill-new (helm-c-stringify candidate) replace))
309
310 (defun* helm-fast-remove-dups (seq &key (test 'eq))
311 "Remove duplicates elements in list SEQ.
312 This is same as `remove-duplicates' but with memoisation.
313 It is much faster, especially in large lists.
314 A test function can be provided with TEST argument key.
315 Default is `eq'."
316 (loop with cont = (make-hash-table :test test)
317 for elm in seq
318 unless (gethash elm cont)
319 do (puthash elm elm cont)
320 finally return
321 (loop for i being the hash-values in cont collect i)))
322
cd1aa7e @thierryvolpiatto New command to show all and some arrangements.
thierryvolpiatto authored
323 ;;;###autoload
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
324 (defun helm-quit-and-find-file ()
325 "Drop into `helm-find-files' from `helm'.
326 If current selection is a buffer or a file, `helm-find-files'
327 from its directory."
328 (interactive)
329 (helm-run-after-quit
330 (lambda (f)
331 (if (file-exists-p f)
332 (helm-find-files-1 (file-name-directory f)
333 (if helm-ff-transformer-show-only-basename
334 (helm-c-basename f) f))
335 (helm-find-files-1 f)))
336 (helm-aif (get-buffer (helm-get-selection))
337 (or (buffer-file-name it)
338 (car (rassoc it dired-buffers))
339 (and (with-current-buffer it
340 (eq major-mode 'org-agenda-mode))
341 org-directory
342 (expand-file-name org-directory))
343 default-directory)
344 (let ((sel (helm-get-selection)))
345 (cond ((or (file-remote-p sel)
346 (file-exists-p sel))
347 (expand-file-name sel))
348 ((string-match ffap-url-regexp sel)
349 sel)
350 (t default-directory))))))
351
352 (defmacro* helm-c-walk-directory (directory &key path (directories t) match)
353 "Walk through DIRECTORY tree.
ad8835e @thierryvolpiatto * helm-utils.el (helm-c-walk-directory): Improve, match argument can …
thierryvolpiatto authored
354 Argument PATH can be one of basename, relative, or full, default to basename.
355 Argument DIRECTORIES when non--nil (default) return also directories names,
356 otherwise skip directories names.
357 Argument MATCH can be a predicate or a regexp."
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
358 `(let (result
359 (fn (case ,path
360 (basename 'file-name-nondirectory)
361 (relative 'file-relative-name)
362 (full 'identity)
363 (t 'file-name-nondirectory))))
364 (labels ((ls-R (dir)
365 (loop with ls = (directory-files
366 dir t directory-files-no-dot-files-regexp)
367 for f in ls
368 if (file-directory-p f)
369 do (progn (when ,directories
370 (push (funcall fn f) result))
371 ;; Don't recurse in directory symlink.
372 (unless (file-symlink-p f)
373 (ls-R f)))
ad8835e @thierryvolpiatto * helm-utils.el (helm-c-walk-directory): Improve, match argument can …
thierryvolpiatto authored
374 else when
375 (and ,match
376 (if (functionp ,match)
377 (funcall ,match f)
378 (and (stringp ,match)
379 (string-match
380 ,match (file-name-nondirectory f)))))
381 do (push (funcall fn f) result))))
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
382 (ls-R ,directory)
383 (nreverse result))))
384
385 (defun helm-c-basename (fname &optional ext)
386 "Print FNAME with any leading directory components removed.
387 If specified, also remove filename extension EXT."
388 (if (and ext (or (string= (file-name-extension fname) ext)
389 (string= (file-name-extension fname t) ext))
390 (not (file-directory-p fname)))
391 (file-name-sans-extension (file-name-nondirectory fname))
392 (file-name-nondirectory (directory-file-name fname))))
393
394 (defun helm-file-human-size (size)
395 "Return a string showing SIZE of a file in human readable form.
396 SIZE can be an integer or a float depending it's value.
397 `file-attributes' will take care of that to avoid overflow error.
398 KBSIZE if a floating point number, default value is 1024.0."
ba17f09 @thierryvolpiatto Move all defcustoms and faces in their respectives groups and files.
thierryvolpiatto authored
399 (let ((M (cons "M" (/ size (expt helm-default-kbsize 2))))
400 (G (cons "G" (/ size (expt helm-default-kbsize 3))))
401 (K (cons "K" (/ size helm-default-kbsize)))
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
402 (B (cons "B" size)))
403 (loop with result = B
404 for (a . b) in
405 (loop for (x . y) in (list M G K B)
406 unless (< y 1) collect (cons x y))
407 when (< b (cdr result)) do (setq result (cons a b))
408 finally return (if (string= (car result) "B")
409 (format "%s" size)
410 (format "%.1f%s" (cdr result) (car result))))))
411
412 (defun* helm-file-attributes
413 (file &key type links uid gid access-time modif-time
ab191f6 @thierryvolpiatto * helm-files.el (helm-ff-properties): Split infos.
thierryvolpiatto authored
414 status size mode gid-change inode device-num dired human-size
74343d7 @thierryvolpiatto * helm-utils.el (helm-file-attributes): Improve.
thierryvolpiatto authored
415 mode-type mode-owner mode-group mode-other (string t))
416 "Return `file-attributes' elements of FILE separately according to key value.
417 Availables keys are:
418 - TYPE: Same as nth 0 `files-attributes' if STRING is nil
419 otherwise return either symlink, directory or file (default).
420 - LINKS: See nth 1 `files-attributes'.
421 - UID: See nth 2 `files-attributes'.
422 - GID: See nth 3 `files-attributes'.
423 - ACCESS-TIME: See nth 4 `files-attributes', however format time
424 when STRING is non--nil (the default).
425 - MODIF-TIME: See nth 5 `files-attributes', same as above.
426 - STATUS: See nth 6 `files-attributes', same as above.
427 - SIZE: See nth 7 `files-attributes'.
428 - MODE: See nth 8 `files-attributes'.
429 - GID-CHANGE: See nth 9 `files-attributes'.
430 - INODE: See nth 10 `files-attributes'.
431 - DEVICE-NUM: See nth 11 `files-attributes'.
432 - DIRED: A line similar to what 'ls -l' return.
433 - HUMAN-SIZE: The size in human form, see `helm-file-human-size'.
434 - MODE-TYPE, mode-owner,mode-group, mode-other: Split what
435 nth 7 `files-attributes' return in four categories.
436 - STRING: When non--nil (default) `helm-file-attributes' return
437 more friendly values.
438 If you want the same behavior as `files-attributes' ,
439 \(but with return values in proplist\) use a nil value for STRING.
440 However when STRING is non--nil, time and type value are different from what
441 you have in `file-attributes'."
ab191f6 @thierryvolpiatto * helm-files.el (helm-ff-properties): Split infos.
thierryvolpiatto authored
442 (let* ((all (destructuring-bind
443 (type links uid gid access-time modif-time
444 status size mode gid-change inode device-num)
74343d7 @thierryvolpiatto * helm-utils.el (helm-file-attributes): Improve.
thierryvolpiatto authored
445 (file-attributes file string)
446 (list :type (if string
447 (cond ((stringp type) "symlink") ; fname
448 (type "directory") ; t
449 (t "file")) ; nil
450 type)
ab191f6 @thierryvolpiatto * helm-files.el (helm-ff-properties): Split infos.
thierryvolpiatto authored
451 :links links
452 :uid uid
453 :gid gid
74343d7 @thierryvolpiatto * helm-utils.el (helm-file-attributes): Improve.
thierryvolpiatto authored
454 :access-time (if string
455 (format-time-string
456 "%Y-%m-%d %R" access-time)
457 access-time)
458 :modif-time (if string
459 (format-time-string
460 "%Y-%m-%d %R" modif-time)
461 modif-time)
462 :status (if string
463 (format-time-string
464 "%Y-%m-%d %R" status)
465 status)
ab191f6 @thierryvolpiatto * helm-files.el (helm-ff-properties): Split infos.
thierryvolpiatto authored
466 :size size
467 :mode mode
468 :gid-change gid-change
469 :inode inode
470 :device-num device-num)))
471 (modes (helm-split-mode-file-attributes (getf all :mode))))
eba1d84 @thierryvolpiatto * helm-utils.el (helm-file-attributes): Always return time under stri…
thierryvolpiatto authored
472 (cond (type (getf all :type))
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
473 (links (getf all :links))
474 (uid (getf all :uid))
475 (gid (getf all :gid))
eba1d84 @thierryvolpiatto * helm-utils.el (helm-file-attributes): Always return time under stri…
thierryvolpiatto authored
476 (access-time (getf all :access-time))
477 (modif-time (getf all :modif-time))
478 (status (getf all :status))
c10a32b @thierryvolpiatto * helm-utils.el (helm-file-attributes): Allow using human-size separa…
thierryvolpiatto authored
479 (size (getf all :size))
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
480 (mode (getf all :mode))
481 (gid-change (getf all :gid-change))
482 (inode (getf all :inode))
483 (device-num (getf all :device-num))
484 (dired
485 (concat
eba1d84 @thierryvolpiatto * helm-utils.el (helm-file-attributes): Always return time under stri…
thierryvolpiatto authored
486 (helm-split-mode-file-attributes (getf all :mode) t) " "
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
487 (number-to-string (getf all :links)) " "
488 (getf all :uid) ":"
489 (getf all :gid) " "
ab191f6 @thierryvolpiatto * helm-files.el (helm-ff-properties): Split infos.
thierryvolpiatto authored
490 (if human-size
491 (helm-file-human-size (getf all :size))
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
492 (int-to-string (getf all :size))) " "
eba1d84 @thierryvolpiatto * helm-utils.el (helm-file-attributes): Always return time under stri…
thierryvolpiatto authored
493 (getf all :modif-time)))
c10a32b @thierryvolpiatto * helm-utils.el (helm-file-attributes): Allow using human-size separa…
thierryvolpiatto authored
494 (human-size (helm-file-human-size (getf all :size)))
ab191f6 @thierryvolpiatto * helm-files.el (helm-ff-properties): Split infos.
thierryvolpiatto authored
495 (mode-type (getf modes :mode-type))
496 (mode-owner (getf modes :user))
497 (mode-group (getf modes :group))
498 (mode-other (getf modes :other))
499 (t (append all modes)))))
500
501 (defun helm-split-mode-file-attributes (str &optional string)
502 "Split mode file attributes STR into a proplist.
503 If STRING is non--nil return instead a space separated string."
504 (loop with type = (substring str 0 1)
505 with cdr = (substring str 1)
506 for i across cdr
507 for count from 1
508 if (<= count 3)
509 concat (string i) into user
510 if (and (> count 3) (<= count 6))
511 concat (string i) into group
512 if (and (> count 6) (<= count 9))
513 concat (string i) into other
514 finally return
515 (if string
516 (mapconcat 'identity (list type user group other) " ")
517 (list :mode-type type :user user :group group :other other))))
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
518
519 (defun helm-c-current-directory ()
520 "Return current-directory name at point.
521 Useful in dired buffers when there is inserted subdirs."
522 (if (eq major-mode 'dired-mode)
523 (dired-current-directory)
524 default-directory))
525
66550b2 @thierryvolpiatto * helm-buffers.el: require cl, helm, helm-vars, helm-utils.
thierryvolpiatto authored
526 ;;; Persistent Action Helpers
527 ;;
528 ;;
34755a4 @thierryvolpiatto * helm.el (helm-selection): Use a default face visible on marked cand…
thierryvolpiatto authored
529 ;; Internal
66550b2 @thierryvolpiatto * helm-buffers.el: require cl, helm, helm-vars, helm-utils.
thierryvolpiatto authored
530 (defvar helm-match-line-overlay nil)
531
dcdb597 make recenter useful for `helm-match-line-color-current-line`
Le Wang authored
532 (defun helm-match-line-color-current-line (&optional start end buf face)
66550b2 @thierryvolpiatto * helm-buffers.el: require cl, helm, helm-vars, helm-utils.
thierryvolpiatto authored
533 "Highlight and underline current position"
dcdb597 make recenter useful for `helm-match-line-color-current-line`
Le Wang authored
534 (let* ((start (or start (line-beginning-position)))
535 (end (or end (1+ (line-end-position))))
536 (args (list start end buf)))
66550b2 @thierryvolpiatto * helm-buffers.el: require cl, helm, helm-vars, helm-utils.
thierryvolpiatto authored
537 (if (not helm-match-line-overlay)
538 (setq helm-match-line-overlay (apply 'make-overlay args))
dcdb597 make recenter useful for `helm-match-line-color-current-line`
Le Wang authored
539 (apply 'move-overlay helm-match-line-overlay args))
540 (overlay-put helm-match-line-overlay
541 'face (or face 'helm-selection-line))
542 (recenter-top-bottom)))
66550b2 @thierryvolpiatto * helm-buffers.el: require cl, helm, helm-vars, helm-utils.
thierryvolpiatto authored
543
544 (defalias 'helm-persistent-highlight-point 'helm-match-line-color-current-line)
545
546 (defun helm-match-line-cleanup ()
547 (when helm-match-line-overlay
548 (delete-overlay helm-match-line-overlay)
549 (setq helm-match-line-overlay nil)))
550
551 (defun helm-match-line-update ()
552 (when helm-match-line-overlay
553 (delete-overlay helm-match-line-overlay)
554 (helm-match-line-color-current-line)))
555
556 (add-hook 'helm-cleanup-hook 'helm-match-line-cleanup)
557 (add-hook 'helm-after-persistent-action-hook 'helm-match-line-update)
558
559 (defun helm-w32-prepare-filename (file)
560 "Convert filename FILE to something usable by external w32 executables."
561 (replace-regexp-in-string ; For UNC paths
562 "/" "\\"
563 (replace-regexp-in-string ; Strip cygdrive paths
564 "/cygdrive/\\(.\\)" "\\1:"
565 file nil nil) nil t))
566
567 ;;;###autoload
568 (defun helm-w32-shell-execute-open-file (file)
569 (interactive "fOpen file:")
570 (with-no-warnings
571 (w32-shell-execute "open" (helm-w32-prepare-filename file))))
572
573 (defun helm-c-open-file-with-default-tool (file)
574 "Open FILE with the default tool on this platform."
575 (if (eq system-type 'windows-nt)
576 (helm-w32-shell-execute-open-file file)
577 (start-process "helm-c-open-file-with-default-tool"
578 nil
579 (cond ((eq system-type 'gnu/linux)
580 "xdg-open")
581 ((or (eq system-type 'darwin) ;; Mac OS X
582 (eq system-type 'macos)) ;; Mac OS 9
583 "open"))
584 file)))
585
586 (defun helm-c-open-dired (file)
587 "Opens a dired buffer in FILE's directory. If FILE is a
588 directory, open this directory."
589 (if (file-directory-p file)
590 (dired file)
591 (dired (file-name-directory file))
592 (dired-goto-file file)))
593
594 (defun helm-c-display-to-real-line (candidate)
595 (if (string-match "^ *\\([0-9]+\\):\\(.*\\)$" candidate)
596 (list (string-to-number (match-string 1 candidate))
597 (match-string 2 candidate))
598 (error "Line number not found")))
599
600 (defun helm-c-action-line-goto (lineno-and-content)
601 (apply #'helm-goto-file-line
602 (helm-interpret-value (helm-attr 'target-file))
603 (append lineno-and-content
604 (list (if (and (helm-attr-defined 'target-file)
605 (not helm-in-persistent-action))
606 'find-file-other-window
607 'find-file)))))
608
609 (defun* helm-c-action-file-line-goto (file-line-content
610 &optional
611 (find-file-function #'find-file))
612 (apply #'helm-goto-file-line
613 (if (stringp file-line-content)
614 ;; Case: filtered-candidate-transformer is skipped
615 (cdr (helm-c-filtered-candidate-transformer-file-line-1
616 file-line-content))
617 file-line-content)))
618
619 (defun helm-require-or-error (feature function)
620 (or (require feature nil t)
621 (error "Need %s to use `%s'." feature function)))
622
623 (defun helm-c-filtered-candidate-transformer-file-line (candidates source)
624 (delq nil (mapcar 'helm-c-filtered-candidate-transformer-file-line-1
625 candidates)))
626
627 (defun helm-c-filtered-candidate-transformer-file-line-1 (candidate)
628 (when (string-match "^\\(.+?\\):\\([0-9]+\\):\\(.*\\)$" candidate)
629 (let ((filename (match-string 1 candidate))
630 (lineno (match-string 2 candidate))
631 (content (match-string 3 candidate)))
632 (cons (format "%s:%s\n %s"
633 (propertize filename 'face compilation-info-face)
634 (propertize lineno 'face compilation-line-face)
635 content)
636 (list (expand-file-name
637 filename
638 (or (helm-interpret-value (helm-attr 'default-directory))
639 (and (helm-candidate-buffer)
640 (buffer-local-value
641 'default-directory (helm-candidate-buffer)))))
642 (string-to-number lineno) content)))))
643
644 (defun* helm-goto-file-line (file lineno content
645 &optional (find-file-function #'find-file))
646 (helm-aif (helm-attr 'before-jump-hook)
647 (funcall it))
648 (when file (funcall find-file-function file))
649 (if (helm-attr-defined 'adjust)
650 (helm-c-goto-line-with-adjustment lineno content)
651 (helm-goto-line lineno))
652 (unless (helm-attr-defined 'recenter)
653 (set-window-start (get-buffer-window helm-current-buffer) (point)))
654 (helm-aif (helm-attr 'after-jump-hook)
655 (funcall it))
656 (when helm-in-persistent-action
657 (helm-match-line-color-current-line)))
658
659 (defun helm-find-file-as-root (candidate)
b224784 @thierryvolpiatto * helm-utils.el (helm-find-file-as-root): Use `find-alternate-file'.
thierryvolpiatto authored
660 (let ((buf (helm-c-basename candidate)))
661 (if (buffer-live-p (get-buffer buf))
662 (progn
663 (set-buffer buf)
664 (find-alternate-file (concat "/" helm-su-or-sudo
665 "::" (expand-file-name candidate))))
666 (find-file (concat "/" helm-su-or-sudo
667 "::" (expand-file-name candidate))))))
66550b2 @thierryvolpiatto * helm-buffers.el: require cl, helm, helm-vars, helm-utils.
thierryvolpiatto authored
668
669 (defun helm-find-many-files (ignore)
670 (mapc 'find-file (helm-marked-candidates)))
671
672 (defun helm-c-goto-line-with-adjustment (line line-content)
673 (let ((startpos)
674 offset found pat)
675 ;; This constant is 1/2 the initial search window.
676 ;; There is no sense in making it too small,
677 ;; since just going around the loop once probably
678 ;; costs about as much as searching 2000 chars.
679 (setq offset 1000
680 found nil
681 pat (concat (if (eq selective-display t)
682 "\\(^\\|\^m\\) *" "^ *") ;allow indent
683 (regexp-quote line-content)))
684 ;; If no char pos was given, try the given line number.
685 (setq startpos (progn (helm-goto-line line) (point)))
686 (or startpos (setq startpos (point-min)))
687 ;; First see if the tag is right at the specified location.
688 (goto-char startpos)
689 (setq found (looking-at pat))
690 (while (and (not found)
691 (progn
692 (goto-char (- startpos offset))
693 (not (bobp))))
694 (setq found
695 (re-search-forward pat (+ startpos offset) t)
696 offset (* 3 offset))) ; expand search window
697 (or found
698 (re-search-forward pat nil t)
699 (error "not found")))
700 ;; Position point at the right place
701 ;; if the search string matched an extra Ctrl-m at the beginning.
702 (and (eq selective-display t)
703 (looking-at "\^m")
704 (forward-char 1))
705 (beginning-of-line))
706
707 (defun helm-c-quit-and-execute-action (action)
708 "Quit current helm session and execute ACTION."
709 (setq helm-saved-action action)
710 (helm-exit-minibuffer))
711
f512dc1 @thierryvolpiatto * helm-config.el: Move yank-text-at-point in *utils.el.
thierryvolpiatto authored
712 ;; Yank text at point.
713 ;;
714 ;;
715 ;; Internal
716 (defvar helm-yank-point nil)
717
bd87f58 refactor out `insert-in-minibuffer` into top level function to avoid …
Le Wang authored
718 (defun helm-insert-in-minibuffer (word &optional replace follow)
9085560 @thierryvolpiatto * helm-utils.el (helm-insert-in-minibuffer): Add docstring, no code c…
thierryvolpiatto authored
719 "Insert WORD in minibuffer.
720 If REPLACE is non--nil, remove the actual content of minibuffer
721 and replace it with WORD, otherwise WORD is appended.
722 Argument FOLLOW is used to notify if we are in `helm-follow-mode'.
723 If it is the case (i.e FOLLOW non--nil) function have no effect
724 and return nil.
725 See `helm-find-files-persistent-action' for usage."
bd87f58 refactor out `insert-in-minibuffer` into top level function to avoid …
Le Wang authored
726 (unless follow
8c4e289 add `helm-yank-symbol-first' option
Le Wang authored
727 (with-current-buffer (window-buffer (minibuffer-window))
bd87f58 refactor out `insert-in-minibuffer` into top level function to avoid …
Le Wang authored
728 (delete-minibuffer-contents)
729 (set-text-properties 0 (length word) nil word)
730 (insert (concat (if replace "" helm-pattern) word)))))
731
f512dc1 @thierryvolpiatto * helm-config.el: Move yank-text-at-point in *utils.el.
thierryvolpiatto authored
732 ;;;###autoload
733 (defun helm-yank-text-at-point ()
8c4e289 add `helm-yank-symbol-first' option
Le Wang authored
734 "Yank text at point in invocation buffer into minibuffer.
735
736 `helm-yank-symbol-first' controls whether the first yank grabs
737 the entire symbol.
738 "
f512dc1 @thierryvolpiatto * helm-config.el: Move yank-text-at-point in *utils.el.
thierryvolpiatto authored
739 (interactive)
bd87f58 refactor out `insert-in-minibuffer` into top level function to avoid …
Le Wang authored
740 (with-helm-current-buffer
741 ;; Start to initial point if C-w have never been hit.
8c4e289 add `helm-yank-symbol-first' option
Le Wang authored
742 (if (or helm-yank-point
743 (not helm-yank-symbol-first))
744 (progn
745 (unless helm-yank-point (setq helm-yank-point (point)))
746 (goto-char helm-yank-point)
747 (forward-word 1)
748 (helm-insert-in-minibuffer (buffer-substring-no-properties helm-yank-point (point)))
749 (setq helm-yank-point (point)))
750 (let* ((sym (symbol-at-point))
751 (str (and sym
752 (symbol-name sym))))
753 (if str
754 (progn
755 (helm-insert-in-minibuffer str)
756 (setq helm-yank-point (cdr (bounds-of-thing-at-point 'symbol)))
757 (goto-char helm-yank-point))
758 (setq helm-yank-point (point))
759 (helm-yank-text-at-point))))))
f512dc1 @thierryvolpiatto * helm-config.el: Move yank-text-at-point in *utils.el.
thierryvolpiatto authored
760
761 (defun helm-reset-yank-point ()
762 (setq helm-yank-point nil))
763
764 (add-hook 'helm-after-persistent-action-hook 'helm-reset-yank-point)
765 (add-hook 'helm-cleanup-hook 'helm-reset-yank-point)
66550b2 @thierryvolpiatto * helm-buffers.el: require cl, helm, helm-vars, helm-utils.
thierryvolpiatto authored
766
c16dfab @thierryvolpiatto * helm-firefox.el: new file.
thierryvolpiatto authored
767 (defun helm-html-bookmarks-to-alist (file url-regexp bmk-regexp)
768 "Parse html bookmark FILE and return an alist with (title . url) as elements."
769 (let (bookmarks-alist url title)
770 (with-temp-buffer
771 (insert-file-contents file)
772 (goto-char (point-min))
773 (while (re-search-forward "href=\\|^ *<DT><A HREF=" nil t)
774 (forward-line 0)
775 (when (re-search-forward url-regexp nil t)
776 (setq url (match-string 0)))
777 (when (re-search-forward bmk-regexp nil t)
778 (setq title (match-string 1)))
779 (push (cons title url) bookmarks-alist)
780 (forward-line)))
781 (nreverse bookmarks-alist)))
782
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
783 (provide 'helm-utils)
784
37b4201 @thierryvolpiatto Fix dependencies and autoloads. Reorder.
thierryvolpiatto authored
785 ;; Local Variables:
786 ;; coding: utf-8
787 ;; indent-tabs-mode: nil
788 ;; byte-compile-dynamic: t
789 ;; End:
790
2797374 @thierryvolpiatto * helm-config.el: Start splitting. Remove all utils functions and mov…
thierryvolpiatto authored
791 ;;; helm-utils.el ends here
Something went wrong with that request. Please try again.