Basic functionality.
[ShellArchive.git] / anything-make.el
blob72244573be22454346bd64a43314c488d30bfba4
1 ;;; anything-make.el --- Add targets of nearest makefile to anything sources
3 ;; Copyright (C) 2008 Philip Jackson
5 ;; Author: Philip Jackson <phil@shellarchive.co.uk>
7 ;; This file is not currently part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or
10 ;; modify it under the terms of the GNU General Public License as
11 ;; published by the Free Software Foundation; either version 2, or (at
12 ;; your option) any later version.
14 ;; This program is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program ; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 ;;; Commentary:
26 ;; Evaling (require 'anything-make) will make the targets for the
27 ;; nearest makefile available as anything sources. It searches up the
28 ;; file system tree for the closest makefile (the name of which is
29 ;; defined by `anything-make-makefile-name').
31 (defvar anything-make-makefile-name "Makefile"
32 "Name of the Makefile file to look for.")
34 (defun anything-make-targets (makefile-path)
35 "Grab the targets from the makefile at MAKEFILE-PATH"
36 (let ((hits '())
37 (buf (find-file-noselect makefile-path)))
38 (with-current-buffer buf
39 (goto-char (point-min))
40 (while (re-search-forward "^\\([[:alnum:]].+?\\):\\(:?$\\| \\)" nil t)
41 (mapcar '(lambda (s)
42 (add-to-list 'hits s t))
43 (split-string (match-string-no-properties 1) " " t))))
44 hits))
46 (defun anything-make-find-makefile (filename &optional startdir)
47 "Search up the file tree for the next instance of FILENAME. If
48 STARTDIR is non-nil then start from there, otherwise start from
49 cwd."
50 (let ((startdir (expand-file-name (or startdir "."))))
51 (catch 'filename
52 (if (file-exists-p (expand-file-name filename startdir))
53 (throw 'filename startdir)
54 (if (string= startdir "/")
55 (throw 'filename nil)
56 (anything-make-find-makefile
57 filename (expand-file-name ".." startdir)))))))
59 (defvar anything-c-source-make-targets
60 '((name . "Make targets")
61 (init . (lambda ()
62 (let ((path
63 (anything-make-find-makefile anything-make-makefile-name)))
64 (setq anything-default-directory path))))
65 (candidates . (lambda ()
66 (anything-make-targets anything-make-makefile-name)))
67 (action .
68 (("Make target" . (lambda (c)
69 (compile (concat "cd " anything-default-directory
70 " && make " c))))))
71 (volatile)))
73 (add-to-list 'anything-sources anything-c-source-make-targets)
75 (provide 'anything-make)