]> code.delx.au - gnu-emacs-elpa/blob - packages/ack/pcmpl-ack.el
Merge commit '199c52606dcd614cb856bbcaca13b5fada0772b6' from avy
[gnu-emacs-elpa] / packages / ack / pcmpl-ack.el
1 ;;; pcmpl-ack.el --- completion for ack -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
4
5 ;; Author: Leo Liu <sdl.web@gmail.com>
6 ;; Keywords: tools, processes, convenience
7 ;; Created: 2012-09-26
8 ;; URL: https://github.com/leoliu/ack-el
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;; Provide pcompletion support for the cli tool `ack' which can be
26 ;; downloaded from http://beyondgrep.com.
27 ;;
28 ;; Install:
29 ;; (autoload 'pcomplete/ack "pcmpl-ack")
30 ;;
31 ;; Usage:
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=
37
38 ;;; Code:
39
40 (require 'pcomplete)
41
42 (defcustom pcmpl-ack-program
43 (file-name-nondirectory (or (executable-find "ack-grep")
44 (executable-find "ack")
45 "ack"))
46 "Name of the ack program."
47 :type 'file
48 :group 'pcomplete)
49
50 (defvar pcmpl-ack-color-options
51 '("clear"
52 "reset"
53 "dark"
54 "bold"
55 "underline"
56 "underscore"
57 "blink"
58 "reverse"
59 "concealed"
60 "black"
61 "red"
62 "green"
63 "yellow"
64 "blue"
65 "magenta"
66 "on_black"
67 "on_red"
68 "on_green"
69 "on_yellow"
70 "on_blue"
71 "on_magenta"
72 "on_cyan"
73 "on_white")
74 "Color names for the `ack' command.")
75
76 (defun pcmpl-ack-run (buffer &rest args)
77 "Run ack with ARGS and send the output to BUFFER."
78 (condition-case nil
79 (apply 'call-process (or pcmpl-ack-program "ack") nil buffer nil args)
80 (file-error -1)))
81
82 (defun pcmpl-ack-short-options ()
83 "Short options for the `ack' command."
84 (with-temp-buffer
85 (let (options)
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) "")))))
91
92 (defun pcmpl-ack-long-options (&optional arg)
93 "Long options for the `ack' command."
94 (with-temp-buffer
95 (let (options)
96 (when (zerop (pcmpl-ack-run t (or arg "--help")))
97 (goto-char (point-min))
98 (while (re-search-forward
99 "\\(?: ?\\|, \\)\\(--\\(\\[no\\]\\)?\\([[:alnum:]-]+=?\\)\\)"
100 nil t)
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)))))
106
107 (defun pcmpl-ack-type-options ()
108 "A list of types for the `ack' command."
109 (pcmpl-ack-long-options "--help-types"))
110
111 ;;;###autoload
112 (defun pcomplete/ack ()
113 "Completion for the `ack' command.
114 Start an argument with '-' to complete short options and '--' for
115 long options."
116 ;; No space after =
117 (while t
118 (if (pcomplete-match "^-" 0)
119 (cond
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)))))
136
137 ;;;###autoload
138 (defalias 'pcomplete/ack-grep 'pcomplete/ack)
139
140 (provide 'pcmpl-ack)
141 ;;; pcmpl-ack.el ends here