summaryrefslogtreecommitdiff
path: root/helm-id-utils.el
blob: 05673582f5790fcc87bb3f5357c92f69499aaa62 (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
;;; helm-id-utils.el --- Helm interface for id-utils. -*- lexical-binding: t -*-

;; Copyright (C) 2015 ~ 2020 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:

(require 'helm-grep)
(require 'helm-help)

(defgroup helm-id-utils nil
  "ID-Utils related Applications and libraries for Helm."
  :group 'helm)

(defcustom helm-gid-program "gid"
  "Name of gid command (usually `gid').
For Mac OS X users, if you install GNU coreutils, the name `gid'
might be occupied by `id' from GNU coreutils, and you should set
it to correct name (or absolute path).  For example, if using
MacPorts to install id-utils, it should be `gid32'."
  :group 'helm-id-utils
  :type 'file)

(defcustom helm-gid-db-file-name "ID"
  "Name of a database file created by `mkid' command from `ID-utils'."
  :group 'helm-id-utils
  :type 'string)

(defun helm-gid-candidates-process ()
  (let* ((patterns (helm-mm-split-pattern helm-pattern))
         (default-com (format "%s -r %s" helm-gid-program
                              (shell-quote-argument (car patterns))))
         (cmd (helm-aif (cdr patterns)
                  (concat default-com
                          (cl-loop for p in it
                                   concat (format " | grep --color=always %s"
                                                  (shell-quote-argument p))))
                default-com))
         (proc (start-process-shell-command
                "gid" helm-buffer cmd)))
    (set (make-local-variable 'helm-grep-last-cmd-line) cmd)
    (prog1 proc
      (set-process-sentinel
       proc (lambda (_process event)
              (when (string= event "finished\n")
                (helm-maybe-show-help-echo)
                (with-helm-window
                  (setq mode-line-format
                        '(" " mode-line-buffer-identification " "
                          (:eval (format "L%s" (helm-candidate-number-at-point))) " "
                          (:eval (propertize
                                  (format "[Helm Gid process finished - (%s results)]"
                                          (max (1- (count-lines
                                                    (point-min) (point-max)))
                                               0))
                                  'face 'helm-locate-finish))))
                  (force-mode-line-update))
                (helm-log "Error: Gid %s"
                          (replace-regexp-in-string "\n" "" event))))))))

(defun helm-gid-filtered-candidate-transformer (candidates _source)
  ;; "gid -r" may add dups in some rare cases.
  (cl-loop for c in (helm-fast-remove-dups candidates :test 'equal)
           collect (helm-grep--filter-candidate-1 c)))

(defclass helm-gid-source (helm-source-async)
  ((header-name
    :initform
    (lambda (name)
      (concat name " [" (helm-get-attr 'db-dir) "]")))
   (db-dir :initarg :db-dir
           :initform nil
           :custom string
           :documentation " Location of ID file.")
   (candidates-process :initform #'helm-gid-candidates-process)
   (filtered-candidate-transformer
    :initform #'helm-gid-filtered-candidate-transformer)
   (candidate-number-limit :initform 99999)
   (action :initform (helm-make-actions
                      "Find File" 'helm-grep-action
                      "Find file other frame" 'helm-grep-other-frame
                      "Save results in grep buffer" 'helm-grep-save-results
                      "Find file other window" 'helm-grep-other-window))
   (persistent-action :initform 'helm-grep-persistent-action)
   (history :initform 'helm-grep-history)
   (nohighlight :initform t)
   (help-message :initform 'helm-grep-help-message)
   (requires-pattern :initform 2)))

;;;###autoload
(defun helm-gid ()
  "Preconfigured `helm' for `gid' command line of `ID-Utils'.
Need A database created with the command `mkid' above
`default-directory'.
Need id-utils as dependency which provide `mkid', `gid' etc..
See <https://www.gnu.org/software/idutils/>."
  (interactive)
  (let* ((db (locate-dominating-file
              default-directory
              helm-gid-db-file-name))
         (helm-grep-default-directory-fn
          (lambda () default-directory))
         (helm-maybe-use-default-as-input t))
    (cl-assert db nil "No DataBase found, create one with `mkid'")
    (helm :sources (helm-make-source "Gid" 'helm-gid-source
                     :db-dir db)
          :buffer "*helm gid*"
          :keymap helm-grep-map
          :truncate-lines helm-grep-truncate-lines)))

(provide 'helm-id-utils)

;;; helm-id-utils ends here