New function fileset-whole-cmds-to-menu
[fileset-whole.git] / fileset-whole.el
blob625bde7e4c59ac5487f83cd47dfabb617046e055
1 ;;;_ fileset-whole.el --- Commands and data applying to filesets as a whole
3 ;;;_. Headers
4 ;;;_ , License
5 ;; Copyright (C) 2011 Tom Breton (Tehom)
7 ;; Author: Tom Breton (Tehom) <tehom@panix.com>
8 ;; Keywords: convenience
10 ;; This file 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 2, or (at your option)
13 ;; any later version.
15 ;; This file 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 GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;;_ , Commentary:
27 ;;
30 ;;;_ , Requires
32 (require 'filesets)
34 ;;;_. Body
35 ;;;_ , Customizations
36 ;;;_ . fileset-whole-alist
37 (defcustom fileset-whole-alist
38 '()
39 "Alist associating fileset names to keyed data"
40 :group 'filesets
41 :type '(repeat
42 (cons :tag "Fileset"
43 (string :tag "Name" :value "")
44 (repeat :tag "Data"
45 (list
46 symbol
47 sexp)))))
48 (put 'fileset-whole-alist 'risky-local-variable t)
50 ;;;_ . fileset-whole-commands
51 (defcustom fileset-whole-commands
52 '(("gdb"
53 gdb ((gud-query-cmdline 'gdb)))
54 ("git"
55 magit-status (default-directory))
56 ("eshell"
57 eshell nil)
58 ("Build"
59 fileset-whole-apply-compile
60 ("make -C" default-directory))
61 ("test"
62 emt:fileset
63 (fileset-name)))
65 "List of commands that apply to whole filesets.
67 Before the command is applied, each arg is evalled with the
68 following bindings:
70 `fileset' - the fileset.
71 `fileset-name' - the name of the fileset
72 `fileset-whole' - other data associated with the fileset, if any.
73 `default-directory' the root directory of the project, if any.
75 Then the command is applied to the list of args."
76 :group 'filesets
77 ;;$$IMPROVE ME Add a hotkey and put it into some keymap
78 ;;$$IMPROVE ME Add a field saying how to find the buffers it
79 ;;creates, given its return value.
80 :type '(repeat :tag "Commands"
81 (list :tag "Definition" :value ("")
82 (string "Name")
83 (choice :tag "Command"
84 (function :tag "Function"))
85 (repeat :tag "Argument List"
86 (choice :tag "Arguments"
87 ;;$$IMPROVE ME List the bound symbols as options
88 (sexp :tag "Sexp"
89 :value nil))))))
91 (put 'fileset-whole-commands 'risky-local-variable t)
92 ;;;_ , Variables
93 ;;For now, we don't contemplate multiple filesets applying to one
94 ;;buffer. Could treat a list of them differently.
95 (defvar fileset-whole-name-here nil
96 "The fileset that contains this buffer, if any." )
97 (make-variable-buffer-local 'fileset-whole-name-here)
98 ;;;_ , Setting up fileset stuff
99 ;;;_ . fileset-whole-setup-menu
100 ;;;###autoload
101 (defun fileset-whole-setup-menu ()
102 "Add the commands in fileset-whole-commands to fileset menu."
103 ;;When is this triggered?
104 ;;And see `filesets-get-cmd-menu'. Either add to it or add another
105 ;;menu.
107 (easy-menu-add-item
108 nil ;;Should this be `filesets-menu-in-menu'?
109 (append filesets-menu-path (list filesets-menu-name "# Filesets"))
110 ["Edit Filesets' Data" fileset-whole-edit]
111 "Save Filesets"))
113 ;;;_ . fileset-whole-cmds-to-menu
114 (defun fileset-whole-cmds-to-menu ()
115 "Add fileset-whole commands to filesets command menu."
116 (mapcar
117 #'(lambda (this)
118 (let
119 ((name (car this)))
120 (easy-menu-add-item
121 nil ;;Should this be `filesets-menu-in-menu'?
122 (append filesets-menu-path (list filesets-menu-name "+ Commands"))
123 `[,name (fileset-whole-run-cmd
124 ,name
125 (fileset-whole-read-fileset))])))
126 fileset-whole-commands))
128 ;;;_ . fileset-whole-edit
129 ;;$$MOVE ME
130 (defun fileset-whole-edit ()
131 "Customize `fileset-whole-alist'."
132 (interactive)
133 ;;$$IMPROVE ME Populate it first from fileset-data
134 (customize-variable 'fileset-whole-alist))
136 ;;;_ . fileset-whole-add-buffer
138 ;;Add a "created" buffer such as gdb, eshell, or magit make to the
139 ;;menus, in a section paralleling the list of files.
141 ;;;_ . fileset-whole-setup-files
142 ;;;###autoload
143 (defun fileset-whole-setup-files ()
144 "Run a hook in each currently open buffer that any fileset applies to."
145 ;;$$WRITE ME
146 ;;$$FACTOR ME Several parts: Running something in all relevant
147 ;;buffers (parms: fileset-name). The hook. Running the hook this
148 ;;way.
149 ;;$$DESIGN ABOUT ME How and when to trigger this. There are no
150 ;;obvious hooks in fileset.
151 (interactive)
152 (let*
154 ;;For each fileset
155 ;;Get list of files
156 ;;For each one that's already open, with that buffer current,
157 ;;run hooks. Default hook will just set `fileset-whole-name-here'.
160 ;;;_ , Structuring
161 ;;;_ . fileset-whole-cmd->fn
162 (defun fileset-whole-cmd->fn (cmd)
163 "Return the function field of CMD"
164 (nth 1 cmd))
165 ;;;_ . fileset-whole-cmd->args
166 (defun fileset-whole-cmd->args (cmd)
167 "Return the args field of CMD"
168 (nth 2 cmd))
169 ;;;_ . fileset-whole-entry->alist
170 (defun fileset-whole-entry->alist (entry)
171 "Return the alist of ENTRY"
172 (cdr entry))
173 ;;;_ . filesets-whole-data-get
174 (defun filesets-whole-data-get (entry key &optional default carp)
175 "Extract the value for KEY in the data part of fileset ENTRY.
176 Return DEFAULT if not found. Return (car VALUE) if CARP is non-nil."
177 (filesets-alist-get
178 (fileset-whole-entry->alist entry) key default carp))
180 ;;;_ , Running commands
181 ;;;_ . fileset-whole-read-cmd
182 (defun fileset-whole-read-cmd ()
183 "Interactively get the name of a fileset command.
184 Either a whole command or a command on individual files."
186 (completing-read "Select command: "
187 (append filesets-commands fileset-whole-commands)
188 nil t))
189 ;;;_ . fileset-whole-read-fileset
190 (defun fileset-whole-read-fileset (&optional ask)
191 "Interactively get the name of a fileset"
193 (unless ask fileset-whole-name-here)
194 (let
195 ((name
196 (completing-read "Select fileset: "
197 filesets-data nil t
198 nil nil fileset-whole-name-here)))
199 ;;Record it so we won't have to ask again wrt this buffer
200 ;;(`fileset-whole-name-here' is buffer-local)
202 ;;$$IMPROVE ME Allow some way(s) to not set it:
203 ;;User preference, and a buffer-local variable.
204 (unless (eq name fileset-whole-name-here)
205 (setq fileset-whole-name-here name))
206 ;;$$IMPROVE ME If buffer is not associated with
207 ;;any file, ask to add it (consult user preference)
208 ;;`filesets-add-buffer'
209 name)))
210 ;;;_ . fileset-whole-get-dir
211 (defun fileset-whole-get-dir (fileset-whole fileset)
212 "Return the root dir of FILESET-WHOLE if it exists."
213 ;;$$IMPROVE ME Store a :root-dir property if we made one.
215 (filesets-whole-data-get fileset-whole :root-dir nil t)
216 (fileset-whole-root-dir fileset)))
219 ;;;_ . fileset-whole-run-cmd
220 ;;;###autoload
221 (defun fileset-whole-run-cmd (cmd-name &optional fileset-name)
222 "Run command CMD-NAME on whole fileset FILESET-NAME."
223 (interactive
224 (list
225 (fileset-whole-read-cmd)
226 (fileset-whole-read-fileset)))
227 (let
228 ((cmd-on-whole (assoc cmd-name fileset-whole-commands)))
229 (cond
230 (cmd-on-whole
231 (let*
233 (fileset
234 (filesets-get-fileset-from-name fileset-name))
235 (fileset-whole
236 (assoc fileset-name fileset-whole-alist))
237 (default-directory
238 (fileset-whole-get-dir fileset-whole fileset))
240 (fileset-whole-cmd->fn cmd-on-whole))
241 (args-spec
242 (fileset-whole-cmd->args cmd-on-whole))
243 (args
244 (mapcar #'eval args-spec)))
246 ;;$$IMPROVE ME Maybe open the fileset first, as
247 ;;filesets-run-cmd does. Do this according to some
248 ;;property of the command. `filesets-open'.
250 ;;$$IMPROVE ME When command makes another buffer
251 ;;(if we can tell it did), associate that buffer
252 ;;with FILESET. Set `fileset-whole-name-here' in
253 ;;it. Add it as an associated buffer (Possibly
254 ;;in another menu area).
255 (apply fn args)))
257 ;;If not, call the filesets command (which presumably
258 ;;exists since we found it earlier)
260 (filesets-run-cmd cmd-name fileset-name)))))
262 ;;;_ , Fileset extras
263 ;;;_ . filesets-whole->fileset
264 (defun filesets-whole->fileset (fileset)
265 "Return a fileset object corresponding to FILESET.
266 If FILESET is a string, return the fileset of that name."
268 (cond
269 ((stringp fileset)
270 (filesets-get-fileset-from-name fileset))
271 ((listp fileset)
272 fileset)
274 (error "Can't convert to fileset: %s" fileset))))
275 ;;;_ . fileset-whole-pick-file
276 ;;This is suitable as a destination-file function for
277 ;;`org-remember-templates'
278 (defun fileset-whole-pick-file (&optional fileset-name filter)
279 "Interactively pick a single file name from a fileset.
280 If FILESET-NAME if not given, prompt for it.
282 If FILTER is given, it must be a function of 1 arg. Only present
283 files that satisfy it."
285 (interactive)
286 (let*
287 ((fileset-name
288 (or
289 fileset-name
290 (fileset-whole-read-fileset t)))
291 (fileset
292 (filesets-get-fileset-from-name fileset-name))
293 (files
294 (filesets-get-filelist fileset nil nil))
295 (files
296 ;;$$IMPROVE ME Support filtering by extension, by
297 ;;mode-symbol, or by regexp.
298 (if filter
299 (filesets-filter-list files filter)
300 files)))
302 (completing-read "File: " files nil t)))
304 ;;;_ . fileset-whole-root-dir
305 (defun fileset-whole-root-dir (fileset)
306 "Return a fileset's smallest enclosing directory."
308 (let*
309 ((fileset (filesets-whole->fileset fileset))
310 (files (filesets-get-filelist fileset nil nil)))
311 (reduce
312 #'fill-common-string-prefix
313 files)))
315 ;;;_ , Populating the alist
317 ;;;_ . fileset-whole-populate-alist
318 ;;YAGNI
319 ;;Populate `fileset-whole-alist' with fileset names and whatever data
320 ;;can be deduced about them, such as :root-dir
321 ;;;_ , Support
322 ;;;_ . fileset-whole-apply-compile
323 (defun fileset-whole-apply-compile (&rest args)
324 "Like `compile', but applied to the concatenation of ARGS"
325 (compile
326 (mapconcat #'identity args " ")))
327 ;;;_ , To set up bindings
328 ;;;###autoload (global-set-key "\C-cp" #'fileset-whole-run-cmd)
330 ;;;_. Footers
331 ;;;_ , Provides
333 (provide 'fileset-whole)
335 ;;;_ * Local emacs vars.
336 ;;;_ + Local variables:
337 ;;;_ + mode: allout
338 ;;;_ + End:
340 ;;;_ , End
341 ;;; fileset-whole.el ends here