1 ;;;_ fileset-whole.el --- Commands and data applying to filesets as a whole
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)
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.
36 ;;;_ . fileset-whole-alist
37 (defcustom fileset-whole-alist
39 "Alist associating fileset names to keyed data"
43 (string :tag
"Name" :value
"")
48 (put 'fileset-whole-alist
'risky-local-variable t
)
50 ;;;_ . fileset-whole-commands
51 (defcustom fileset-whole-commands
53 gdb
((gud-query-cmdline 'gdb
)))
55 magit-status
(default-directory))
59 fileset-whole-apply-compile
60 ("make -C" default-directory
))
65 "List of commands that apply to whole filesets.
67 Before the command is applied, each arg is evalled with the
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."
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
("")
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
91 (put 'fileset-whole-commands
'risky-local-variable t
)
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 (put 'fileset-whole-name-here
'permanent-local t
)
99 ;;;_ , Setting up menus
100 ;;;_ . fileset-whole-setup-menu
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
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
]
113 ;;;_ . fileset-whole-cmds-to-menu
114 (defun fileset-whole-cmds-to-menu ()
115 "Add fileset-whole commands to filesets command menu."
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
125 (fileset-whole-read-fileset))])))
126 fileset-whole-commands
)
129 ;;;_ . fileset-whole-add-buffer
131 ;;Add a "created" buffer such as gdb, eshell, or magit make to the
132 ;;menus, in a section paralleling the list of files.
134 ;;;_ . fileset-whole-setup-files
136 (defun fileset-whole-setup-files ()
137 "Run a hook in each currently open buffer that any fileset applies to."
139 ;;$$FACTOR ME Several parts: Running something in all relevant
140 ;;buffers (parms: fileset-name). The hook. Running the hook this
142 ;;$$DESIGN ABOUT ME How and when to trigger this. There are no
143 ;;obvious hooks in fileset.
149 ;;For each one that's already open, with that buffer current,
150 ;;run hooks. Default hook will just set `fileset-whole-name-here'.
152 ;;;_ . fileset-whole-init
154 (defun fileset-whole-init ()
155 "Set up fileset-whole on top of filesets"
157 (fileset-whole-setup-menu)
158 (fileset-whole-cmds-to-menu))
160 ;;;_ . fileset-whole-cmd->fn
161 (defun fileset-whole-cmd->fn
(cmd)
162 "Return the function field of CMD"
164 ;;;_ . fileset-whole-cmd->args
165 (defun fileset-whole-cmd->args
(cmd)
166 "Return the args field of CMD"
168 ;;;_ . fileset-whole-entry->alist
169 (defun fileset-whole-entry->alist
(entry)
170 "Return the alist of ENTRY"
172 ;;;_ . filesets-whole-data-get
173 (defun filesets-whole-data-get (entry key
&optional default carp
)
174 "Extract the value for KEY in the data part of fileset ENTRY.
175 Return DEFAULT if not found. Return (car VALUE) if CARP is non-nil."
177 (fileset-whole-entry->alist entry
) key default carp
))
179 ;;;_ , Running commands
180 ;;;_ . fileset-whole-read-cmd
181 (defun fileset-whole-read-cmd ()
182 "Interactively get the name of a fileset command.
183 Either a whole command or a command on individual files."
185 (completing-read "Select command: "
186 (append filesets-commands fileset-whole-commands
)
188 ;;;_ . fileset-whole-read-fileset
189 (defun fileset-whole-read-fileset (&optional ask
)
190 "Interactively get the name of a fileset"
192 (unless ask fileset-whole-name-here
)
195 (completing-read "Select fileset: "
197 nil nil fileset-whole-name-here
)))
198 ;;Record it so we won't have to ask again wrt this buffer
199 ;;(`fileset-whole-name-here' is buffer-local)
201 ;;$$IMPROVE ME Allow some way(s) to not set it:
202 ;;User preference, and a buffer-local variable.
203 (unless (eq name fileset-whole-name-here
)
204 (setq fileset-whole-name-here name
))
205 ;;$$IMPROVE ME If buffer is not associated with
206 ;;any file, ask to add it (consult user preference)
207 ;;`filesets-add-buffer'
209 ;;;_ . fileset-whole-get-dir
210 (defun fileset-whole-get-dir (fileset-whole fileset
)
211 "Return the root dir of FILESET-WHOLE if it exists."
212 ;;$$IMPROVE ME Store a :root-dir property if we made one.
214 (filesets-whole-data-get fileset-whole
:root-dir nil t
)
215 (fileset-whole-root-dir fileset
)))
218 ;;;_ . fileset-whole-run-cmd
220 (defun fileset-whole-run-cmd (cmd-name &optional fileset-name
)
221 "Run command CMD-NAME on whole fileset FILESET-NAME."
224 (fileset-whole-read-cmd)
225 (fileset-whole-read-fileset)))
227 ((cmd-on-whole (assoc cmd-name fileset-whole-commands
)))
233 (filesets-get-fileset-from-name fileset-name
))
235 (assoc fileset-name fileset-whole-alist
))
237 (fileset-whole-get-dir fileset-whole fileset
))
239 (fileset-whole-cmd->fn cmd-on-whole
))
241 (fileset-whole-cmd->args cmd-on-whole
))
243 (mapcar #'eval args-spec
)))
245 ;;$$IMPROVE ME Maybe open the fileset first, as
246 ;;filesets-run-cmd does. Do this according to some
247 ;;property of the command. `filesets-open'.
249 ;;$$IMPROVE ME When command makes another buffer
250 ;;(if we can tell it did), associate that buffer
251 ;;with FILESET. Set `fileset-whole-name-here' in
252 ;;it. Add it as an associated buffer (Possibly
253 ;;in another menu area).
256 ;;If not, call the filesets command (which presumably
257 ;;exists since we found it earlier)
259 (filesets-run-cmd cmd-name fileset-name
)))))
261 ;;;_ , Fileset extras
262 ;;;_ . filesets-whole->fileset
263 (defun filesets-whole->fileset
(fileset)
264 "Return a fileset object corresponding to FILESET.
265 If FILESET is a string, return the fileset of that name."
269 (filesets-get-fileset-from-name fileset
))
273 (error "Can't convert to fileset: %s" fileset
))))
274 ;;;_ . fileset-whole-pick-file
275 ;;This is suitable as a destination-file function for
276 ;;`org-remember-templates'
277 (defun fileset-whole-pick-file (&optional fileset-name filter
)
278 "Interactively pick a single file name from a fileset.
279 If FILESET-NAME if not given, prompt for it.
281 If FILTER is given, it must be a function of 1 arg. Only present
282 files that satisfy it."
289 (fileset-whole-read-fileset t
)))
291 (filesets-get-fileset-from-name fileset-name
))
293 (filesets-get-filelist fileset nil nil
))
295 ;;$$IMPROVE ME Support filtering by extension, by
296 ;;mode-symbol, or by regexp.
298 (filesets-filter-list files filter
)
301 (completing-read "File: " files nil t
)))
303 ;;;_ . fileset-whole-root-dir
304 (defun fileset-whole-root-dir (fileset)
305 "Return a fileset's smallest enclosing directory."
308 ((fileset (filesets-whole->fileset fileset
))
309 (files (filesets-get-filelist fileset nil nil
)))
311 #'fill-common-string-prefix
315 ;;;_ , Maintaining fileset-whole data
316 ;;;_ . fileset-whole-edit
317 (defun fileset-whole-edit ()
318 "Customize `fileset-whole-alist'."
320 ;;$$IMPROVE ME Populate it first from fileset-data
321 (customize-variable 'fileset-whole-alist
))
323 ;;;_ . fileset-whole-populate-alist
325 ;;Populate `fileset-whole-alist' with fileset names and whatever data
326 ;;can be deduced about them, such as :root-dir
328 ;;;_ . fileset-whole-apply-compile
329 (defun fileset-whole-apply-compile (&rest args
)
330 "Like `compile', but applied to the concatenation of ARGS"
332 (mapconcat #'identity args
" ")))
333 ;;;_ , To set up bindings
334 ;;;###autoload (global-set-key "\C-cp" #'fileset-whole-run-cmd)
335 ;;;###autoload (fileset-whole-init)
339 (provide 'fileset-whole
)
341 ;;;_ * Local emacs vars.
342 ;;;_ + Local variables:
347 ;;; fileset-whole.el ends here