1 ;;; pcmpl-x.el --- completion for miscellaneous tools -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
5 ;; Author: Leo Liu <sdl.web@gmail.com>
6 ;; Keywords: processes, tools, convenience
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24 (eval-when-compile (require 'cl-lib))
28 ;;;; tlmgr - http://www.tug.org/texlive/tlmgr.html
30 (defcustom pcmpl-x-tlmgr-program "tlmgr"
31 "Name of the tlmgr program."
35 (defvar pcmpl-x-tlmgr-common-options
42 "--persistent-downloads"
43 "--no-persistent-downloads"
44 "--no-execute-actions"
49 (defvar pcmpl-x-tlmgr-actions
58 ("repository" ("list" "add" "remove" "set"))
75 ("conf" ("texmf" "tlmgr"))
77 ("a4" "letter" "xdvi" "pdftex" "dvips" "dvipdfmx" "dvipdfm" "context")
79 (unless (member (pcomplete-arg 1) '("a4" "letter"))
80 (pcomplete-here* '("paper"))
81 (pcomplete-here* '("a4" "letter")))))
82 ("platform" ("list" "add" "remove"))
83 ("print-platform" ("collections" "schemes"))
84 ("arch" ("list" "add" "remove"))
85 ("print-arch" ("collections" "schemes"))
86 ("info" ("collections" "schemes"))
89 ("check" ("files" "depends" "executes" "runfiles" "all"))
90 ("path" ("add" "remove"))
91 ("postaction" ("install" "remove") ("shortcut" "fileassoc" "script"))
93 ("generate" ("language"
99 (defvar pcmpl-x-tlmgr-options-cache (make-hash-table :size 31 :test 'equal))
101 (defun pcmpl-x-tlmgr-action-options (action)
102 "Get the list of long options for ACTION."
103 (if (eq (gethash action pcmpl-x-tlmgr-options-cache 'missing) 'missing)
106 (call-process pcmpl-x-tlmgr-program nil t nil action "-h"))
107 (goto-char (point-min))
110 (cl-loop while (re-search-forward
111 "^[ \t]+\\(--[[:alnum:]-]+=?\\)"
113 collect (match-string 1)))
114 pcmpl-x-tlmgr-options-cache)
115 (pcmpl-x-tlmgr-action-options action)))
116 (gethash action pcmpl-x-tlmgr-options-cache)))
119 (defun pcomplete/tlmgr ()
120 "Completion for the `tlmgr' command."
121 (while (pcomplete-match "^--" 0)
122 (pcomplete-here* pcmpl-x-tlmgr-common-options)
123 (unless (or (pcomplete-match "^--" 0)
124 (all-completions (pcomplete-arg 0) pcmpl-x-tlmgr-actions))
125 (pcomplete-here* (pcomplete-dirs-or-entries))))
126 (pcomplete-here* pcmpl-x-tlmgr-actions)
127 (let ((action (substring-no-properties (pcomplete-arg 1))))
129 (if (pcomplete-match "^--" 0)
130 (pcomplete-here* (pcmpl-x-tlmgr-action-options action))
131 (dolist (completions (cdr (assoc action pcmpl-x-tlmgr-actions)))
132 (cond ((functionp completions)
133 (funcall completions))
134 ((all-completions (pcomplete-arg 0) completions)
135 (pcomplete-here* completions))
136 (t (pcomplete-here* (pcomplete-dirs-or-entries)))))
137 (unless (pcomplete-match "^--" 0)
138 (pcomplete-here* (pcomplete-dirs-or-entries)))))))
141 ;;;; ack - http://betterthangrep.com
144 ;; - To complete short options type '-' first
145 ;; - To complete long options type '--' first
146 ;; - Color name completion is supported following
147 ;; --color-filename=, --color-match= and --color-lineno=
148 ;; - Type completion is supported following --type=
150 (defcustom pcmpl-x-ack-program
151 (file-name-nondirectory (or (executable-find "ack-grep")
152 (executable-find "ack")
154 "Name of the ack program."
158 (defvar pcmpl-x-ack-color-options
182 "Color names for the `ack' command.")
184 (defun pcmpl-x-ack-run (buffer &rest args)
185 "Run ack with ARGS and send the output to BUFFER."
187 (apply 'call-process (or pcmpl-x-ack-program "ack") nil buffer nil args)
190 (defun pcmpl-x-ack-short-options ()
191 "Short options for the `ack' command."
194 (when (zerop (pcmpl-x-ack-run t "--help"))
195 (goto-char (point-min))
196 (while (re-search-forward "^ -\\([^-]\\)" nil t)
197 (push (match-string 1) options))
198 (mapconcat 'identity (nreverse options) "")))))
200 (defun pcmpl-x-ack-long-options (&optional arg)
201 "Long options for the `ack' command."
204 (when (zerop (pcmpl-x-ack-run t (or arg "--help")))
205 (goto-char (point-min))
206 (while (re-search-forward
207 "\\(?: ?\\|, \\)\\(--\\(\\[no\\]\\)?\\([[:alnum:]-]+=?\\)\\)"
209 (if (not (match-string 2))
210 (push (match-string 1) options)
211 (push (concat "--" (match-string 3)) options)
212 (push (concat "--no" (match-string 3)) options)))
213 (nreverse options)))))
215 (defun pcmpl-x-ack-type-options ()
216 "A list of types for the `ack' command."
217 (pcmpl-x-ack-long-options "--help-types"))
220 (defun pcomplete/ack ()
221 "Completion for the `ack' command.
222 Start an argument with '-' to complete short options and '--' for
226 (if (pcomplete-match "^-" 0)
228 ((pcomplete-match "^--color-\\w+=\\(\\S-*\\)" 0)
229 (pcomplete-here* pcmpl-x-ack-color-options
230 (pcomplete-match-string 1 0) t))
231 ((pcomplete-match "^--\\(?:no\\)?ignore-dir=\\(\\S-*\\)" 0)
232 (pcomplete-here* (pcomplete-dirs)
233 (pcomplete-match-string 1 0) t))
234 ((pcomplete-match "^--type=\\(\\S-*\\)" 0)
235 (pcomplete-here* (mapcar (lambda (type-option)
236 (substring type-option 2))
237 (pcmpl-x-ack-type-options))
238 (pcomplete-match-string 1 0) t))
239 ((pcomplete-match "^--" 0)
240 (pcomplete-here* (append (pcmpl-x-ack-long-options)
241 (pcmpl-x-ack-type-options))))
242 (t (pcomplete-opt (pcmpl-x-ack-short-options))))
243 (pcomplete-here* (pcomplete-dirs-or-entries)))))
246 (defalias 'pcomplete/ack-grep 'pcomplete/ack)
249 ;;; pcmpl-x.el ends here