mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-18 76bbd07de7add0f9d13c6914f158d19630fe2f62
commit | author | age
5cb5f7 1 ;;; helm-id-utils.el --- Helm interface for id-utils. -*- lexical-binding: t -*-
C 2
3 ;; Copyright (C) 2015 ~ 2018 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 'helm-grep)
21 (require 'helm-help)
22
23 (defgroup helm-id-utils nil
24   "ID-Utils related Applications and libraries for Helm."
25   :group 'helm)
26
27 (defcustom helm-gid-program "gid"
28   "Name of gid command (usually `gid').
29 For Mac OS X users, if you install GNU coreutils, the name `gid'
30 might be occupied by `id' from GNU coreutils, and you should set
31 it to correct name (or absolute path), for example, if using
32 MacPorts to install id-utils, it should be `gid32'."
33   :group 'helm-id-utils
34   :type 'file)
35
36 (defcustom helm-gid-db-file-name "ID"
37   "Name of a database file created by `mkid' command from `ID-utils'."
38   :group 'helm-id-utils
39   :type 'string)
40
41 (defun helm-gid-candidates-process ()
42   (let* ((patterns (helm-mm-split-pattern helm-pattern))
43          (default-com (format "%s -r %s" helm-gid-program
44                               (shell-quote-argument (car patterns))))
45          (cmd (helm-aif (cdr patterns)
46                   (concat default-com
47                           (cl-loop for p in it
48                                    concat (format " | grep --color=always %s"
49                                                   (shell-quote-argument p))))
50                 default-com))
51          (proc (start-process-shell-command
52                 "gid" helm-buffer cmd)))
53     (set (make-local-variable 'helm-grep-last-cmd-line) cmd)
54     (prog1 proc
55       (set-process-sentinel
56        proc (lambda (_process event)
57               (when (string= event "finished\n")
58                 (helm-maybe-show-help-echo)
59                 (with-helm-window
60                   (setq mode-line-format
61                         '(" " mode-line-buffer-identification " "
62                           (:eval (format "L%s" (helm-candidate-number-at-point))) " "
63                           (:eval (propertize
64                                   (format "[Helm Gid process finished - (%s results)]" 
65                                           (max (1- (count-lines
66                                                     (point-min) (point-max)))
67                                                0))
68                                   'face 'helm-locate-finish))))
69                   (force-mode-line-update))
70                 (helm-log "Error: Gid %s"
71                           (replace-regexp-in-string "\n" "" event))))))))
72
73 (defun helm-gid-filtered-candidate-transformer (candidates _source)
74   ;; "gid -r" may add dups in some rare cases.
75   (cl-loop for c in (helm-fast-remove-dups candidates :test 'equal)
76            collect (helm-grep--filter-candidate-1 c)))
77
78 (defclass helm-gid-source (helm-source-async)
79   ((header-name
80     :initform
81     (lambda (name)
82       (concat name " [" (helm-attr 'db-dir) "]")))
83    (db-dir :initarg :db-dir
84            :initform nil
85            :custom string
86            :documentation " Location of ID file.")
87    (candidates-process :initform #'helm-gid-candidates-process)
88    (filtered-candidate-transformer
89     :initform #'helm-gid-filtered-candidate-transformer)
90    (candidate-number-limit :initform 99999)
91    (action :initform (helm-make-actions
92                       "Find File" 'helm-grep-action
93                       "Find file other frame" 'helm-grep-other-frame
94                       "Save results in grep buffer" 'helm-grep-save-results
95                       "Find file other window" 'helm-grep-other-window))
96    (persistent-action :initform 'helm-grep-persistent-action)
97    (history :initform 'helm-grep-history)
98    (nohighlight :initform t)
99    (help-message :initform 'helm-grep-help-message)
100    (requires-pattern :initform 2)))
101
102 ;;;###autoload
103 (defun helm-gid ()
104   "Preconfigured helm for `gid' command line of `ID-Utils'.
105 Need A database created with the command `mkid'
106 above `default-directory'.
107 Need id-utils as dependency which provide `mkid', `gid' etc...
108 See <https://www.gnu.org/software/idutils/>."
109   (interactive)
110   (let* ((db (locate-dominating-file
111               default-directory
112               helm-gid-db-file-name))
113          (helm-grep-default-directory-fn
114           (lambda () default-directory))
115          (helm--maybe-use-default-as-input t))
116     (cl-assert db nil "No DataBase found, create one with `mkid'")
117     (helm :sources (helm-make-source "Gid" 'helm-gid-source
118                      :db-dir db)
119           :buffer "*helm gid*"
120           :keymap helm-grep-map
121           :truncate-lines helm-grep-truncate-lines)))
122
123 (provide 'helm-id-utils)
124
125 ;; Local Variables:
126 ;; byte-compile-warnings: (not obsolete)
127 ;; coding: utf-8
128 ;; indent-tabs-mode: nil
129 ;; End:
130
131 ;;; helm-id-utils ends here