1 ;;; pcmpl-ack.el --- completion for ack -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
5 ;; Author: Leo Liu <sdl.web@gmail.com>
6 ;; Keywords: tools, processes, convenience
8 ;; URL: https://github.com/leoliu/ack-el
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;; Provide pcompletion support for the cli tool `ack' which can be
26 ;; downloaded from http://beyondgrep.com.
29 ;; (autoload 'pcomplete/ack "pcmpl-ack")
32 ;; - To complete short options type '-' first
33 ;; - To complete long options type '--' first
34 ;; - Color name completion is supported following
35 ;; --color-filename=, --color-match= and --color-lineno=
36 ;; - Type completion is supported following --type=
42 (defcustom pcmpl-ack-program
43 (file-name-nondirectory (or (executable-find "ack-grep")
44 (executable-find "ack")
46 "Name of the ack program."
50 (defvar pcmpl-ack-color-options
74 "Color names for the `ack' command.")
76 (defun pcmpl-ack-run (buffer &rest args)
77 "Run ack with ARGS and send the output to BUFFER."
79 (apply 'call-process (or pcmpl-ack-program "ack") nil buffer nil args)
82 (defun pcmpl-ack-short-options ()
83 "Short options for the `ack' command."
86 (when (zerop (pcmpl-ack-run t "--help"))
87 (goto-char (point-min))
88 (while (re-search-forward "^ -\\([^-]\\)" nil t)
89 (push (match-string 1) options))
90 (mapconcat 'identity (nreverse options) "")))))
92 (defun pcmpl-ack-long-options (&optional arg)
93 "Long options for the `ack' command."
96 (when (zerop (pcmpl-ack-run t (or arg "--help")))
97 (goto-char (point-min))
98 (while (re-search-forward
99 "\\(?: ?\\|, \\)\\(--\\(\\[no\\]\\)?\\([[:alnum:]-]+=?\\)\\)"
101 (if (not (match-string 2))
102 (push (match-string 1) options)
103 (push (concat "--" (match-string 3)) options)
104 (push (concat "--no" (match-string 3)) options)))
105 (nreverse options)))))
107 (defun pcmpl-ack-type-options ()
108 "A list of types for the `ack' command."
109 (pcmpl-ack-long-options "--help-types"))
112 (defun pcomplete/ack ()
113 "Completion for the `ack' command.
114 Start an argument with '-' to complete short options and '--' for
118 (if (pcomplete-match "^-" 0)
120 ((pcomplete-match "^--color-\\w+=\\(\\S-*\\)" 0)
121 (pcomplete-here* pcmpl-ack-color-options
122 (pcomplete-match-string 1 0) t))
123 ((pcomplete-match "^--\\(?:no\\)?ignore-dir=\\(\\S-*\\)" 0)
124 (pcomplete-here* (pcomplete-dirs)
125 (pcomplete-match-string 1 0) t))
126 ((pcomplete-match "^--type=\\(\\S-*\\)" 0)
127 (pcomplete-here* (mapcar (lambda (type-option)
128 (substring type-option 2))
129 (pcmpl-ack-type-options))
130 (pcomplete-match-string 1 0) t))
131 ((pcomplete-match "^--" 0)
132 (pcomplete-here* (append (pcmpl-ack-long-options)
133 (pcmpl-ack-type-options))))
134 (t (pcomplete-opt (pcmpl-ack-short-options))))
135 (pcomplete-here* (pcomplete-dirs-or-entries)))))
138 (defalias 'pcomplete/ack-grep 'pcomplete/ack)
141 ;;; pcmpl-ack.el ends here