556381b059d0bc56122bb3b7903a523dda9cc9c9
[eproject.git] / eproject.el
blob556381b059d0bc56122bb3b7903a523dda9cc9c9
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; eproject.el --- project workspaces for emacs
4 ;;
5 ;; Copyright (C) 2008-2010 grischka
6 ;;
7 ;; Author: grischka -- grischka@users.sourceforge.net
8 ;; Created: 24 Jan 2008
9 ;; Version: 0.4
11 ;; This program is free software, released under the GNU General
12 ;; Public License (GPL, version 2). For details see:
14 ;; http://www.fsf.org/licenses/gpl.html
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24 ;; User-configurable items:
26 (defvar prj-default-config '(
27 ("Make" "make" "f9")
28 ("Clean" "make clean" "C-f9")
29 ("Run" "echo run what" "f8")
30 ("Stop" "-e eproject-killtool" "C-f8")
31 ("---")
32 ("Configure" "./configure")
33 ("---")
34 ("Explore Project" "nautilus --browser `pwd` &")
35 ("XTerm In Project" "xterm &")
37 "*The default tools menu for new projects in eproject."
40 (defvar prj-set-default-directory nil
41 "*Should eproject set the project directory as default-directory
42 for all project files (nil/t).")
44 (defvar prj-set-framepos nil
45 "*Should eproject restore the last frame position/size (nil/t).")
47 (defvar prj-set-compilation-frame nil
48 "*Should eproject show compilation output in the other frame (nil/t).")
50 (defvar prj-set-multi-isearch nil
51 "*Should eproject setup multi-isearch in the project files (nil/t).")
53 ;; End of user-configurable items
54 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
56 ;; There is a global file (~/.emacs.d/eproject.lst)
57 (defun prj-globalfile ()
58 (expand-file-name "eproject.lst"
59 (if (boundp 'user-emacs-directory) user-emacs-directory
60 "~/.emacs.d/")
63 ;; with the list of all projects
64 (defvar prj-list)
66 ;; and the project that was open in the last session (if any)
67 (defvar prj-last-open nil)
69 ;; and the frame coords from last session
70 (defvar prj-frame-pos nil)
72 ;; eproject version that created the config file
73 (defvar prj-version nil)
75 ;; Here is a function to reset these
76 (defun prj-init ()
77 (setq prj-version nil)
78 (setq prj-list nil)
79 (setq prj-last-open nil)
80 (setq prj-frame-pos nil)
83 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 ;; Each project has a directory
86 (defvar prj-directory)
88 ;; with a configuration files in it
89 (defvar prj-default-cfg "eproject.cfg")
91 ;; This file defines:
93 ;; the list of files
94 (defvar prj-files)
96 ;; the current file
97 (defvar prj-curfile)
99 ;; an alist of settings
100 (defvar prj-config)
102 ;; a list of tools
103 (defvar prj-tools)
105 ;; a list of utility functions (feature incomplete)
106 (defvar prj-functions nil)
108 ;; directory to run commands, default to prj-directory
109 (defvar prj-exec-directory)
111 ;; The current project
112 (defvar prj-current)
114 ;; A list with generated functions for each tool
115 (defvar prj-tools-fns)
117 ;; A list with files removed from the project
118 (defvar prj-removed-files)
120 ;; Here is a function to reset/close the project
121 (defun prj-reset ()
122 (setq prj-version nil)
123 (setq prj-current nil)
124 (setq prj-directory nil)
125 (setq prj-exec-directory nil)
126 (setq prj-files nil)
127 (setq prj-removed-files nil)
128 (setq prj-curfile nil)
129 (setq prj-config nil)
130 (setq prj-tools-fns nil)
131 (setq prj-tools (copy-tree prj-default-config))
132 (prj-reset-functions)
135 (defun prj-reset-functions ()
136 (dolist (l prj-functions)
137 (if (eq (car l) 'setq)
138 (makunbound (cadr l))
139 (fmakunbound (cadr l))
141 (setq prj-functions nil)
144 (defun prj-set-functions (s)
145 (prj-reset-functions)
146 (setq prj-functions s)
147 (dolist (l s) (eval l))
150 ;; Some more variables:
152 ;; the frame that exists on startup
153 (defvar prj-initial-frame nil)
155 ;; this is put into minor-mode-alist
156 (defvar eproject-mode t)
158 ;; where this file is in
159 (defvar eproject-directory)
161 ;; eproject version that created the files
162 (defvar eproject-version "0.4")
164 ;; Configuration UI
165 (eval-and-compile
166 (defun eproject-setup-toggle () (interactive))
167 (defun eproject-setup-quit () (interactive))
168 (defun prj-config-get-result (s))
169 (defun prj-config-reset ())
170 (defun prj-config-print ())
171 (defun prj-config-parse ())
174 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
175 ;; Small functions
177 (defun caddr (l) (car (cddr l)))
179 (defun prj-del-list (l e)
180 (let ((a (assoc (car e) l)))
181 (if a
182 (delq a l)
183 l)))
185 (defun prj-add-list (l e)
186 (nconc (prj-del-list l e) (list e))
189 (defun prj-next-file (l e)
190 (and (setq e (assoc (car e) l))
191 (cadr (memq e l))
194 (defun prj-prev-file (l e)
195 (prj-next-file (reverse l) e)
198 ; replace a closed file, either by the previous or the next.
199 (defun prj-otherfile (l f)
200 (or (prj-prev-file l f)
201 (prj-next-file l f)
204 ;; make relative path, but only up to the second level of ..
205 (defun prj-relative-path (f)
206 (let ((r (file-relative-name f prj-directory)))
207 (if (string-match "^\\.\\.[/\\]\\.\\.[/\\]\\.\\.[/\\]" r)
212 ;; friendly truncate filename
213 (defun prj-shortname (s)
214 (let ((l (length s)) (x 30) n)
215 (cond ((>= x l) s)
216 ((progn
217 (setq x (- x 3))
218 (setq n (length (file-name-nondirectory s)))
219 (if (< n l) (setq n (1+ n)))
220 (>= x n)
222 (concat (substring s 0 (- x n)) "..." (substring s (- n)))
224 ((= n l)
225 (concat (substring s 0 x) "...")
228 (concat "..." (substring s (- n) (- (- x 3) n)) "...")
229 ))))
231 (defun prj-settitle ()
232 (modify-frame-parameters
234 (list (cons 'title
235 (and prj-current
236 (format "emacs - %s" (car prj-current))
237 )))))
239 (defun eproject-addon (f)
240 (concat eproject-directory f)
243 (defun prj-goto-line (n)
244 (goto-char 1)
245 (beginning-of-line n)
248 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
249 ;; Write configuration to file
251 (defun prj-print-list (s fp)
252 (let ((v (eval s)))
253 (setq v (list 'setq s
254 (if (and (atom v) (null (and (symbolp v) v)))
256 (list 'quote v)
258 ;;(print v fp)
259 (pp v fp) (princ "\n" fp)
262 (defun prj-create-file (filename)
263 (let ((fp (generate-new-buffer filename)))
264 (princ ";; -*- mode: Lisp; -*-\n\n" fp)
265 fp))
267 (defun prj-close-file (fp)
268 (with-current-buffer fp
269 (condition-case nil
270 (and t (write-region nil nil (buffer-name fp) nil 0))
271 (error nil)
273 (kill-buffer fp)
276 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
277 ;; Load/Save global project list and initial frame sizes
279 (defun prj-loadlist ()
280 (prj-init)
281 (load (prj-globalfile) t t)
282 (setq prj-version eproject-version)
285 (defun prj-get-frame-pos (f)
286 (mapcar
287 (lambda (parm) (cons parm (frame-parameter f parm)))
288 '(top left width height)
291 (defun prj-savelist ()
292 (let ((g (prj-globalfile)) fp)
293 (unless (file-exists-p g)
294 (make-directory (file-name-directory g) t)
296 (setq prj-last-open (car prj-current))
297 (when (frame-live-p prj-initial-frame)
298 (setq prj-frame-pos (prj-get-frame-pos prj-initial-frame))
300 (setq fp (prj-create-file g))
301 (when fp
302 (prj-print-list 'prj-version fp)
303 (prj-print-list 'prj-list fp)
304 (prj-print-list 'prj-last-open fp)
305 (prj-print-list 'prj-frame-pos fp)
306 (prj-close-file fp)
309 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
310 ;; Load/Save local per-project configuration file
312 (defun prj-update-config ()
313 (let ((d (prj-get-directory prj-current))
314 (e (prj-getconfig "exec-root"))
316 (if e (setq d (expand-file-name e d)))
317 (setq prj-exec-directory (file-name-as-directory d))
320 (defun prj-get-directory (a)
321 (file-name-as-directory (expand-file-name (cadr a)))
324 (defun prj-get-cfg ()
325 (expand-file-name (or (caddr prj-current) prj-default-cfg) prj-directory)
328 (defun prj-loadconfig (a)
329 (let (lf e)
330 (prj-reset)
331 (setq prj-current a)
332 (setq prj-directory (prj-get-directory a))
333 (when (file-regular-p (setq lf (prj-get-cfg)))
334 (load lf nil t)
335 (setq prj-curfile
336 (or (assoc prj-curfile prj-files)
337 (car prj-files)
340 (if (setq e (prj-getconfig "project-name"))
341 (setcar a e)
342 (prj-setconfig "project-name" (car a))
344 (prj-update-config)
345 (prj-set-functions prj-functions)
346 (setq prj-version eproject-version)
349 (defun prj-saveconfig ()
350 (when prj-current
351 (let (w c b files)
352 (prj-removehooks)
353 (setq w (selected-window))
354 (setq c (window-buffer w))
355 (dolist (f prj-files)
356 (cond ((setq b (get-buffer (car f)))
357 (set-window-buffer w b t)
358 (with-current-buffer b
359 (let ((s (line-number-at-pos (window-start w)))
360 (p (line-number-at-pos (window-point w)))
362 (push (list (car f) s p) files)
364 (t ;;(consp (cdr f))
365 (push f files)
367 (set-window-buffer w c t)
368 (prj-addhooks)
369 (let ((fp (prj-create-file (prj-get-cfg)))
370 (prj-curfile (car prj-curfile))
371 (prj-files (nreverse files))
373 (when fp
374 (prj-print-list 'prj-version fp)
375 (prj-print-list 'prj-config fp)
376 (prj-print-list 'prj-tools fp)
377 (prj-print-list 'prj-files fp)
378 (prj-print-list 'prj-curfile fp)
379 (prj-print-list 'prj-functions fp)
380 (prj-close-file fp)
384 (defun prj-saveall ()
385 (prj-saveconfig)
386 (prj-savelist)
389 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
390 ;; The core functions: Open / Close / Add / Remove Project
392 (defun eproject-open (a)
393 "Open another project."
394 (interactive
395 (list
396 (or (prj-config-get-result 'p)
397 (completing-read "Open Project: " (mapcar 'car prj-list))
399 (unless (consp a)
400 (let ((b (assoc a prj-list)))
401 (unless b
402 (error "No such project: %s" a)
404 (setq a b)
406 (setq a (or (car (member a prj-list)) a))
407 (unless (eq a prj-current)
408 (unless (file-directory-p (prj-get-directory a))
409 (error "No such directory: %s" (cadr a))
411 (setq prj-list (cons a (delq a prj-list)))
412 (eproject-close)
413 (prj-loadconfig a)
415 (prj-addhooks)
416 (prj-setup-all)
417 (prj-isearch-setup)
418 (unless (prj-edit-file prj-curfile)
419 (eproject-dired)
422 (defun eproject-close ()
423 "Close the current project."
424 (interactive)
425 (when prj-current
426 (prj-saveconfig)
427 (prj-removehooks)
428 (let (f)
429 (unwind-protect
430 (progn
431 (save-some-buffers nil)
432 (eproject-killbuffers t)
433 (setq f t)
435 (or f (prj-addhooks))
437 (prj-reset)
438 (prj-config-reset)
439 (prj-setup-all)
440 (prj-isearch-setup)
443 (defun eproject-killbuffers (&optional from-project)
444 "If called interactively kills all buffers that
445 do not belong to project files"
446 (interactive)
447 (let (a b)
448 (dolist (f prj-files)
449 (setq b (get-buffer (car f)))
450 (if b
451 (setq a (cons (list b) a))
453 (dolist (b (buffer-list))
454 (when (eq (consp (assoc b a)) from-project)
455 (kill-buffer b)
456 ))))
458 (defun eproject-add (dir &optional name cfg)
459 "Add a new or existing project to the list."
460 (interactive
461 (let (d n f)
462 (setq d (read-directory-name "Add project in directory: " prj-directory nil t))
463 (setq n (file-name-nondirectory (directory-file-name d)))
464 (setq n (read-string "Project name: " n))
465 (setq f (read-string "Project file: " prj-default-cfg))
466 (list d n f)
468 (when dir
469 (setq dir (directory-file-name dir))
470 (unless name
471 (setq name (file-name-nondirectory dir))
473 (when (and cfg (string-equal cfg prj-default-cfg))
474 (setq cfg nil)
476 (let ((a (if cfg (list name dir cfg) (list name dir))))
477 (push a prj-list)
478 (eproject-open a)
481 (defun eproject-remove (a)
482 "Remove a project from the list."
483 (interactive
484 (list
485 (or (prj-config-get-result 'p)
486 (completing-read "Remove project: " (mapcar 'car prj-list))
488 (unless (consp a)
489 (let ((b (assoc a prj-list)))
490 (unless b
491 (error "No such project: %s" a)
493 (setq a b)
495 (when (progn
496 (beep)
497 (prog1 (y-or-n-p (format "Remove \"%s\"? " (car a)))
498 (message "")
500 (setq prj-list (prj-del-list prj-list a))
501 (prj-setup-all)
504 (defun eproject-save ()
505 "Save the project configuration to file."
506 (interactive)
507 (prj-config-parse)
508 (prj-config-print)
509 (prj-saveall)
512 (defun eproject-revert ()
513 "Reload the project configuration from file."
514 (interactive)
515 (prj-loadlist)
516 (if prj-current
517 (prj-loadconfig prj-current)
519 (prj-setup-all)
522 (defun eproject-addfile (f)
523 "Add a file to the current project."
524 (interactive
525 (and prj-current
526 (list
527 (read-file-name "Add file to project: " nil nil t nil)
529 (unless prj-current (error "No project open"))
530 (let ((a (prj-insert-file f (prj-config-get-result 'f))))
531 (unless (cdr a)
532 (message "Added to project: %s" (car a))
534 (prj-config-print)
535 (prj-setmenu)
538 (defun eproject-removefile (a)
539 "Remove a file from the current project."
540 (interactive (prj-get-existing-file-1 "Remove file from project: "))
541 (setq a (prj-get-existing-file-2 a))
542 (prj-remove-file a)
545 (defun eproject-visitfile (a)
546 "Visit a file from the current project."
547 (interactive (prj-get-existing-file-1 "Visit file: "))
548 (setq a (prj-get-existing-file-2 a))
549 (prj-edit-file a)
552 (defun prj-get-existing-file-1 (msg)
553 (and prj-current
554 (list
555 (or (prj-config-get-result 'f)
556 (completing-read msg (mapcar 'car prj-files))
557 ))))
559 (defun prj-get-existing-file-2 (a)
560 (unless prj-current (error "No project open"))
561 (if (consp a)
563 (let ((b (assoc (prj-relative-path a) prj-files)))
564 (unless b (error "No such file in project: %s" a))
568 (defun eproject-help ()
569 "Show the eproject README."
570 (interactive)
571 (view-file (eproject-addon "eproject.txt"))
574 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
575 ;; Hook functions to track opening/closing files from emacs
577 (defun prj-addhooks ()
578 (add-hook 'kill-buffer-hook 'prj-kill-buffer-hook)
579 (add-hook 'find-file-hook 'prj-find-file-hook)
580 (add-hook 'window-configuration-change-hook 'prj-wcc-hook)
583 (defun prj-removehooks ()
584 (remove-hook 'window-configuration-change-hook 'prj-wcc-hook)
585 (remove-hook 'find-file-hook 'prj-find-file-hook)
586 (remove-hook 'kill-buffer-hook 'prj-kill-buffer-hook)
589 (defun prj-wcc-hook ()
590 (dolist (w (window-list))
591 (prj-register-buffer (window-buffer w))
594 (defun prj-find-file-hook ()
595 (run-with-idle-timer 0.2 nil 'prj-wcc-hook)
598 (defun prj-kill-buffer-hook ()
599 (let ((b (current-buffer)) a)
600 (if (setq a (rassq b prj-files))
601 (prj-remove-file a t)
602 (if (setq a (rassq b prj-removed-files))
603 (setq prj-removed-files (delq a prj-removed-files))
604 ))))
606 (defun prj-register-buffer (b)
607 (let (f a)
608 (setq f (buffer-file-name b))
609 (when (and f t) ;;(not (string-match "^\\." (file-name-nondirectory f))))
610 (setq a (rassq b prj-files))
611 (unless a
612 (setq a (prj-insert-file f nil t))
613 (when a
614 (unless (cdr a)
615 (message "Added to project: %s" (car a))
617 (prj-init-buffer a b)
619 (when (and a (null (eq a prj-curfile)))
620 (setq prj-curfile a)
621 (prj-setmenu)
625 (defun prj-insert-file (f &optional after on-the-fly)
626 (let ((r (prj-relative-path f)) a m)
627 (setq a (assoc r prj-files))
628 (unless (or a (and on-the-fly (assoc r prj-removed-files)))
629 (setq a (list r))
630 (setq m (memq (or after prj-curfile) prj-files))
631 (if m
632 (setcdr m (cons a (cdr m)))
633 (setq prj-files (prj-add-list prj-files a))
635 (setq prj-removed-files (prj-del-list prj-removed-files a))
639 (defun prj-remove-file (a &optional on-the-fly)
640 (let ((n (prj-otherfile prj-files a)) b)
641 (setq prj-files (prj-del-list prj-files a))
642 (when (eq prj-curfile a)
643 (setq prj-curfile n)
645 (unless on-the-fly
646 (setq prj-removed-files (prj-add-list prj-removed-files a))
648 (unless (prj-config-print)
649 (prj-edit-file prj-curfile)
651 (prj-setmenu)
652 (message "Removed from project: %s" (car a))
655 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
656 ;; Edit another file
658 (defun prj-init-buffer (a b)
659 (with-current-buffer b
660 (rename-buffer (car a) t)
661 (when prj-set-default-directory
662 (cd prj-directory)
664 (setcdr a b)
667 (defun prj-find-file (a)
668 (when a
669 (let (f b pos)
670 (setq b (cdr a))
671 (setq f (expand-file-name (car a) prj-directory))
672 (setq b (get-file-buffer f))
673 (unless b
674 (prj-removehooks)
675 (setq b (find-file-noselect f))
676 (prj-addhooks)
677 (when (and b (consp (cdr a)))
678 (setq pos (cdr a))
680 (when b
681 (prj-init-buffer a b)
682 (cons b pos)
683 ))))
685 (defun prj-edit-file (a)
686 (let ((f (prj-find-file a)))
687 (when f
688 (eproject-setup-quit)
689 (switch-to-buffer (car f))
690 (prj-restore-edit-pos (cdr f) (selected-window))
691 (prj-setmenu)
692 ;;(message "dir: %s" default-directory)
694 (setq prj-curfile a)
697 (defun prj-restore-edit-pos (pos w)
698 (let ((top (car pos)) (line (cadr pos)))
699 (when (and (numberp top) (numberp line))
700 (prj-goto-line top)
701 (set-window-start w (point))
702 (prj-goto-line line)
705 (defun prj-select-window (w)
706 (let (focus-follows-mouse)
707 (select-window w)
708 (select-frame-set-input-focus (window-frame w))
711 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
712 ;; choose next/previous file
714 (defun eproject-nextfile ()
715 "Switch to the next file that belongs to the current project."
716 (interactive)
717 (prj-switch-file 'prj-next-file 'next-buffer)
720 (defun eproject-prevfile ()
721 "Switch to the previous file that belongs to the current project."
722 (interactive)
723 (prj-switch-file 'prj-prev-file 'previous-buffer)
726 (defun prj-switch-file (fn1 fn2)
727 (let ((a (rassoc (current-buffer) prj-files)))
728 (cond (a
729 (prj-edit-file (or (funcall fn1 prj-files a) a))
731 (prj-curfile
732 (prj-edit-file prj-curfile)
735 (funcall fn2)
736 ))))
738 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
739 ;; Set key shortcuts
741 (defun prj-setkeys ()
742 (let ((f (consp prj-current))
743 (a (assoc 'eproject-mode minor-mode-map-alist))
744 (map (make-sparse-keymap))
746 (if a
747 (setcdr a map)
748 (push (cons 'eproject-mode map) minor-mode-map-alist)
750 (when f
751 (define-key map [M-right] 'eproject-nextfile)
752 (define-key map [M-left] 'eproject-prevfile)
753 (define-key map [C-f5] 'eproject-dired)
754 (let ((n 0) fn s)
755 (dolist (a prj-tools)
756 (unless (setq fn (nth n prj-tools-fns))
757 (setq fn (list 'lambda))
758 (setq prj-tools-fns (nconc prj-tools-fns (list fn)))
760 (setcdr fn `(() (interactive) (prj-run-tool ',a)))
761 (setq n (1+ n))
762 (when (setq s (caddr a))
763 (define-key map (prj-parse-key s) (and f fn))
764 ))))
765 (define-key map [f5] 'eproject-setup-toggle)
768 (defun prj-parse-key (s)
769 (read
770 (if (string-match "[a-z][a-z0-9]+$" s)
771 (concat "[" s "]")
772 (concat "\"\\" s "\""))))
774 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
775 ;; Set menus
777 (defun prj-list-sorted ()
778 (sort (append prj-list nil)
779 '(lambda (a b) (string-lessp (car a) (car b)))
782 (defun prj-setmenu ()
783 (let ((f (consp prj-current)) m1 m2 m3)
785 (setq m1
786 `(("Open" open ,@(prj-menulist-maker prj-list prj-current 'prj-menu-open))
787 ("Add/Remove" other
788 ("Add ..." "Add new or existing project to the list" . eproject-add)
789 ("Remove ..." "Remove project from the list" . eproject-remove)
790 ,@(and f '(("Close" "Close current project" . eproject-close)))
791 ("--")
792 ("Setup" "Enter the project setup area." . eproject-setup-toggle)
793 ("Help" "View eproject.txt" . eproject-help)
796 (when f
797 (nconc m1 (cons '("--") (prj-menulist-maker prj-tools nil prj-tools-fns)))
798 (setq m2
799 `(("Dired" "Browse project directory in Dired - Use 'a' to add file(s) to the project" . eproject-dired)
800 ("--")
801 ,@(prj-menulist-maker prj-files prj-curfile 'prj-menu-edit)
804 (prj-menu-maker
805 global-map
806 `((buffer "Project" project ,@m1)
807 (file "List" list ,@m2)
809 '(menu-bar)
812 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
814 (defun prj-menu-edit ()
815 (interactive)
816 (let ((a (nth last-command-event prj-files)))
817 (if a (prj-edit-file a))
820 (defun prj-menu-open ()
821 (interactive)
822 (let ((a (nth last-command-event prj-list)))
823 (if a (eproject-open (car a)))
826 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
828 (defun prj-menu-maker (map l v)
829 (let ((e (list nil)))
830 (setq v (append v e))
831 (dolist (k (reverse l))
832 (let (s a)
833 (when (symbolp (car k))
834 (setq a (pop k))
836 (cond
837 ((numberp (car k))
838 (setcar e (pop k))
840 ((and (consp (cdr k)) (symbolp (cadr k)))
841 (setcar e (cadr k))
842 (setq s (cddr k))
843 (setq k (and s (cons (car k) (make-sparse-keymap (car k)))))
846 (setcar e (intern (downcase (car k))))
848 (if a
849 (define-key-after map (vconcat v) k a)
850 (define-key map (vconcat v) k)
852 (if s (prj-menu-maker map s v))
853 ))))
855 (defun prj-copy-head (l n)
856 (let (r)
857 (while (and l (> n 0))
858 (push (pop l) r)
859 (setq n (1- n))
861 (nreverse r)
864 (defun prj-split-list (l n)
865 (let (r)
866 (while l
867 (push (prj-copy-head l n) r)
868 (setq l (nthcdr n l))
870 (nreverse r)
873 (defun prj-menulist-maker (l act fns)
874 (let (r (w 30) s (m 0) (n 0) k)
875 (cond
876 ((< (length l) w)
877 (prj-menulist-maker-1 (list l fns n) act)
880 ;; menu too long; split into submenus
881 (setq s (prj-split-list l w))
882 (setq k (prj-menulist-maker-1 (list (append (pop s) '(("--"))) fns n) act))
883 (setq r (nreverse k))
884 (dolist (l s)
885 (when (consp fns)
886 (setq fns (nthcdr w fns))
888 (setq n (+ n w))
889 (setq k (prj-menulist-maker-1 (list l fns n) act))
890 (push (cons (concat (prj-shortname (caar l)) " ...")
891 (cons (intern (format "m_%d" (setq m (1+ m))))
892 k)) r)
894 (nreverse r)
895 ))))
897 (defun prj-menulist-maker-1 (l act)
898 (let (r e f s i n a)
899 (while (car l)
900 (setq a (caar l))
901 (setcar l (cdar l))
902 (setq n (caddr l))
903 (setcar (cddr l) (1+ n))
904 (setq f (if (consp (cadr l))
905 (prog1 (car (cadr l)) (setcar (cdr l) (cdr (cadr l))))
906 (cadr l)))
908 (setq i (car a))
909 (unless (string-match "^ *#" i)
910 (setq s (if (and (consp (cdr a)) (stringp (cadr a))) (cadr a) i))
911 (cond ((equal ">" i)
912 (setq e (cons s (cons (intern s) (prj-menulist-maker-1 l act))))
913 (setq r (cons e r))
915 ((equal "<" i)
916 (setq l nil)
919 (setq i (prj-shortname i))
920 (setq e (cons n (if (eq a act)
921 `(menu-item ,i ,f :button (:toggle . t) :help ,s)
922 (cons i (cons s f)))))
923 (setq r (cons e r))
926 (nreverse r)
929 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
930 ;; Run make and other commands
932 (defun prj-compilation-in-frame (cmd)
933 (let ((bn "*compilation*") w h b c f)
934 (unless (get-buffer-window bn t)
935 (setq b (get-buffer-create bn))
936 (setq f (frame-list))
937 (cond ((cdr f)
938 (setq w (frame-first-window (car f)))
939 (delete-other-windows w)
942 (setq h (/ (* 70 (frame-height)) 100))
943 (delete-other-windows w)
944 (setq w (split-window w h))
946 (set-window-buffer w b)
948 (let ((display-buffer-reuse-frames t) (f (selected-frame)))
949 (compile cmd)
950 (select-frame-set-input-focus f)
953 (defun prj-run (cmd)
954 (cond ((string-match "^-e +" cmd)
955 (setq cmd (read (substring cmd (match-end 0))))
956 (unless (commandp cmd)
957 (setq cmd `(lambda () (interactive) ,cmd))
959 (command-execute cmd)
961 ((let ((b (current-buffer))
962 (old-dir default-directory)
963 (new-dir ".")
965 (when (string-match "^-in +\\([^[:space:]]+\\) +" cmd)
966 (setq new-dir (match-string-no-properties 1 cmd))
967 (setq cmd (substring cmd (match-end 0)))
969 (when prj-exec-directory
970 (setq new-dir (expand-file-name new-dir prj-exec-directory))
972 (cd new-dir)
973 (cond ((string-match "\\(.+\\)& *$" cmd)
974 (start-process-shell-command
975 "eproject-async" nil (match-string 1 cmd))
976 (message (match-string 1 cmd))
978 (prj-set-compilation-frame
979 (prj-compilation-in-frame cmd)
982 (compile cmd)
984 (with-current-buffer b (cd old-dir))
985 ))))
987 (defun prj-run-tool (a)
988 (unless (string-match "^--+$" (car a))
989 (prj-run (or (cadr a) (car a)))
992 (defun eproject-killtool ()
993 (interactive)
994 (let ((bn "*compilation*") w0 w1)
995 (when (setq w1 (get-buffer-window bn t))
996 (when (fboundp 'kill-compilation)
997 (setq w0 (selected-window))
998 (select-window w1)
999 (kill-compilation)
1000 (select-window w0)
1001 ))))
1003 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1004 ;; run grep on project files
1006 (defun eproject-grep (command-args)
1007 "Run the grep command on all the project files."
1008 (interactive
1009 (progn
1010 (require 'grep)
1011 (grep-compute-defaults)
1012 (let ((default (grep-default-command)))
1013 (list (read-from-minibuffer
1014 "Run grep on project files: "
1015 (if current-prefix-arg default grep-command)
1018 'grep-history
1019 (if current-prefix-arg nil default)
1020 )))))
1021 (let ((b (current-buffer)) (old-dir default-directory))
1022 (dolist (f (mapcar 'car prj-files))
1023 (setq command-args (concat command-args " " f))
1025 (when prj-directory (cd prj-directory))
1026 (grep command-args)
1027 (with-current-buffer b (cd old-dir))
1030 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1031 ;; add files to the project with dired
1033 (require 'dired)
1035 (defun prj-dired-addfiles ()
1036 (interactive)
1037 (when prj-current
1038 (let ((n 0) a)
1039 (dolist (f (dired-get-marked-files))
1040 (setq a (prj-insert-file f))
1041 (unless (cdr a)
1042 (setq n (1+ n))
1043 (setq prj-curfile a)
1045 (message "Added to project: %d file(s)" n)
1046 (prj-setmenu)
1049 (defun prj-dired-run ()
1050 (interactive)
1051 (let ((f (dired-get-marked-files)) c)
1052 (and (setq c (pop f))
1053 (null f)
1054 (let ((prj-directory (file-name-directory c)))
1055 (prj-run c)))))
1057 (defun eproject-dired ()
1058 "Start a dired window with the project directory."
1059 (interactive)
1060 (when prj-directory
1061 (eproject-setup-quit)
1062 ;;(message "Use 'a' to add marked or single files to the project.")
1063 (dired prj-directory)
1064 (let ((map dired-mode-map))
1065 (define-key map [mouse-2] 'dired-find-file)
1066 (define-key map "a" 'prj-dired-addfiles)
1067 (define-key map "r" 'prj-dired-run)
1068 (define-key map [menu-bar operate command] '("Add to Project"
1069 "Add current or marked file(s) to project" . prj-dired-addfiles))
1072 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1074 (defun prj-setup-all ()
1075 (prj-setkeys)
1076 (prj-setmenu)
1077 (prj-settitle)
1078 (prj-config-print)
1081 (defun prj-getconfig (n)
1082 (let ((a (cdr (assoc n prj-config))))
1083 (and (stringp a) a)
1086 (defun prj-setconfig (n v)
1087 (let ((a (assoc n prj-config)))
1088 (unless a
1089 (setq a (list n))
1090 (setq prj-config (nconc prj-config (list a)))
1092 (setcdr a v)
1095 (defun prj-on-kill ()
1096 (prj-saveall)
1099 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1100 ;; isearch in all project files
1102 (defun prj-isearch-function (b wrap)
1103 (let (a d)
1104 (or b (setq b (current-buffer)))
1105 (cond (wrap
1106 (if isearch-forward
1107 (setq a (car prj-files))
1108 (setq a (car (last prj-files)))
1110 ((setq a (rassoc b prj-files))
1111 (if isearch-forward
1112 (setq a (prj-next-file prj-files a))
1113 (setq a (prj-prev-file prj-files a))
1116 (when a
1117 (if (buffer-live-p (cdr a))
1118 (setq d (cdr a))
1119 (setq d (car (prj-find-file a)))
1121 ;; (print `(prj-isearch (wrap . ,wrap) ,b ,d) (get-buffer "*Messages*"))
1125 (defun prj-isearch-setup ()
1126 (cond ((and prj-set-multi-isearch prj-current)
1127 (setq multi-isearch-next-buffer-function 'prj-isearch-function)
1128 (setq multi-isearch-pause 'initial)
1129 (add-hook 'isearch-mode-hook 'multi-isearch-setup)
1132 (remove-hook 'isearch-mode-hook 'multi-isearch-setup)
1135 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1136 ;; Initialize
1138 (defun prj-startup-delayed ()
1139 ;; load UI support
1140 (load (eproject-addon "eproject-config") nil t)
1142 ;; When no projects are specified yet, load the eproject project itself.
1143 (unless prj-list
1144 (load (eproject-addon prj-default-cfg))
1147 ;; no project so far
1148 (prj-reset)
1149 (prj-setup-all)
1150 (add-hook 'kill-emacs-hook 'prj-on-kill)
1152 ;; inhibit open last project when a file was on the commandline
1153 (unless (buffer-file-name (window-buffer))
1154 (when prj-last-open
1156 ;; open last project
1157 (eproject-open prj-last-open)
1159 ;; restore frame position
1160 (when (and prj-set-framepos prj-frame-pos prj-initial-frame)
1161 (modify-frame-parameters prj-initial-frame prj-frame-pos)
1162 ;; emacs bug: when it's too busy it doesn't set frames correctly.
1163 (sit-for 0.2)
1164 ))))
1166 (defun prj-command-line-switch (option)
1167 (setq prj-last-open (pop argv))
1168 (setq inhibit-startup-screen t)
1171 (defun eproject-startup ()
1172 ;; where is this file
1173 (if load-file-name
1174 (setq eproject-directory (file-name-directory load-file-name)))
1175 (if (boundp 'prj-list)
1176 (progn
1177 (load (eproject-addon "eproject-config"))
1178 (prj-setup-all))
1179 (progn
1180 (prj-loadlist)
1181 (when prj-last-open (setq inhibit-startup-screen t))
1182 (when (display-graphic-p) (setq prj-initial-frame (selected-frame)))
1183 (push '("project" . prj-command-line-switch) command-switch-alist)
1184 (run-with-idle-timer 0.1 nil 'prj-startup-delayed)
1187 ;;;###autoload(require 'eproject)
1188 (provide 'eproject)
1189 (eproject-startup)
1191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1192 ;; eproject.el ends here