copyright date update
[eproject.git] / eproject.el
blob0a31ecdfe103b8eca094711fdd1759b05798b893
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; eproject.el --- project workspaces for emacs
4 ;;
5 ;; Copyright (C) 2008,2009 grischka
6 ;;
7 ;; Author: grischka -- grischka@users.sourceforge.net
8 ;; Created: 24 Jan 2008
9 ;; Version: 0.2
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 ;; There is a global file
24 (defun prj-globalfile ()
25 (expand-file-name (concat user-emacs-directory "eproject.lst"))
28 ;; with the list of all projects
29 (defvar prj-list)
31 ;; and the project that was open in the last session (if any)
32 (defvar prj-last-open nil)
34 ;; and the frame coords from last session
35 (defvar prj-frame-pos nil)
37 ;; eproject version that created the config file
38 (defvar prj-version nil)
40 ;; Here is a function to reset these
41 (defun prj-init ()
42 (setq prj-version nil)
43 (setq prj-list nil)
44 (setq prj-last-open nil)
45 (setq prj-frame-pos nil)
48 ;; Each project has a directory
49 (defvar prj-directory)
51 ;; with a configuration files in it
52 (defun prj-localfile ()
53 (expand-file-name "eproject.cfg" prj-directory)
56 ;; This file defines:
58 ;; the list of files
59 (defvar prj-files)
61 ;; the current file
62 (defvar prj-curfile)
64 ;; an alist of settings
65 (defvar prj-config)
67 ;; a list of tools
68 (defvar prj-tools)
70 ;; a list of utility functions (feature incomplete)
71 (defvar prj-functions nil)
73 ;; directory to run commands, default to prj-directory
74 (defvar prj-directory-run)
76 ;; Here are some default tools for new projects,
77 ;; (which you might want to adjust to your needs)
79 (defun prj-default-config ()
80 (setq prj-tools (copy-tree '(
81 ("Make" "make" "f9")
82 ("Clean" "make clean" "C-f9")
83 ("Run" "echo run what" "f8")
84 ("Stop" "-e eproject-killtool" "C-f8")
85 ("---")
86 ("Configure" "./configure")
87 ("---")
88 ("Explore Project" "nautilus --browser `pwd` &")
89 ("XTerm In Project" "xterm &")
90 )))
93 ;; This defines the current project
94 (defvar prj-current)
96 ;; There is an internal list with generated functions
97 ;; for each tool
98 (defvar prj-tools-fns)
100 ;; and a list with files removed from the project
101 (defvar prj-removed-files)
103 ;; Here is a function to reset/close the project
104 (defun prj-reset ()
105 (setq prj-version nil)
106 (setq prj-current nil)
107 (setq prj-directory nil)
108 (setq prj-directory-run nil)
109 (setq prj-files nil)
110 (setq prj-removed-files nil)
111 (setq prj-curfile nil)
112 (setq prj-config nil)
113 (setq prj-tools nil)
114 (setq prj-tools-fns nil)
115 (prj-reset-functions)
116 (prj-default-config)
119 (defun prj-reset-functions ()
120 (dolist (l prj-functions)
121 (if (eq (car l) 'setq)
122 (makunbound (cadr l))
123 (fmakunbound (cadr l))
125 (setq prj-functions nil)
128 (defun prj-set-functions (s)
129 (prj-reset-functions)
130 (setq prj-functions s)
131 (dolist (l s) (eval l))
134 ;; Some more variables
136 ;; the frame that exists on startup
137 (defvar prj-initial-frame nil)
139 ;; this is put into minor-mode-alist
140 (defvar eproject-mode t)
142 ;; where this file is in
143 (defvar eproject-directory)
145 ;; eproject version that created the files
146 (defvar eproject-version "0.2")
148 ;; Configuration UI
149 (eval-and-compile
150 (defun eproject-setup-toggle () (interactive))
151 (defun eproject-setup-quit () (interactive))
152 (defun prj-config-get-result (s))
153 (defun prj-config-reset ())
154 (defun prj-config-print ())
155 (defun prj-config-parse ())
158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 ;; Small functions
161 (defun prj-del-list (l e)
162 (let ((a (assoc (car e) l)))
163 (if a
164 (delq a l)
165 l)))
167 (defun prj-add-list (l e)
168 (nconc (prj-del-list l e) (list e))
171 (defun prj-next-file (l e)
172 (let ((a (assoc (car e) l)))
173 (when a
174 (setq l (memq a l))
175 (if (cdr l) (cadr l) a)
178 (defun prj-prev-file (l e)
179 (let ((a (assoc (car e) l)) (p l))
180 (when a
181 (while (and l (null (eq (car l) a)))
182 (setq p l l (cdr l))
184 (car p)
187 ;; replace a closed file, either by the previous or the next.
188 (defun prj-otherfile (l f)
189 (let ((n (prj-prev-file l f)))
190 (when (equal f n)
191 (setq n (prj-next-file l f))
192 (when (equal f n)
193 (setq n nil)
197 (defun caddr (l) (car (cddr l)))
199 ;; make relative path, but only up to the second level of ..
200 (defun prj-relative-path (f)
201 (let ((r (file-relative-name f prj-directory)))
202 (if (string-match "^\\.\\.[/\\]\\.\\.[/\\]\\.\\.[/\\]" r)
207 ;; friendly truncate filename
208 (defun prj-shortname (s)
209 (let ((l (length s)) (x 30) n)
210 (cond ((>= x l) s)
211 ((progn
212 (setq x (- x 3))
213 (setq n (length (file-name-nondirectory s)))
214 (if (< n l) (setq n (1+ n)))
215 (>= x n)
217 (concat (substring s 0 (- x n)) "..." (substring s (- n)))
219 ((= n l)
220 (concat (substring s 0 x) "...")
223 (concat "..." (substring s (- n) (- (- x 3) n)) "...")
224 ))))
226 (defun prj-settitle ()
227 (modify-frame-parameters
229 (list (cons 'title
230 (and prj-current
231 (format "emacs - %s" (car prj-current))
232 )))))
234 (defun eproject-addon (f)
235 (concat eproject-directory f)
238 (defun prj-goto-line (n)
239 (goto-char 1)
240 (beginning-of-line n)
243 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
244 ;; Write configuration to file
246 (defun prj-print-list (s fp)
247 (let ((v (eval s)))
248 (setq v (list 'setq s
249 (if (and (atom v) (null (and (symbolp v) v)))
251 (list 'quote v)
253 ;;(print v fp)
254 (pp v fp) (princ "\n" fp)
257 (defun prj-create-file (filename)
258 (let ((fp (generate-new-buffer filename)))
259 (princ ";; -*- mode: Lisp; -*-\n\n" fp)
260 fp))
262 (defun prj-close-file (fp)
263 (with-current-buffer fp
264 (condition-case nil
265 (write-region nil nil (buffer-name fp) nil 0)
266 (error nil)
268 (kill-buffer fp)
271 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
272 ;; Load/Save global project list and initial frame sizes
274 (defun prj-loadlist ()
275 (prj-init)
276 (load (prj-globalfile) t t)
277 (setq prj-version eproject-version)
280 (defun prj-get-frame-pos (f)
281 (mapcar
282 (lambda (parm) (cons parm (frame-parameter f parm)))
283 '(top left width height)
286 (defun prj-savelist ()
287 (let ((g (prj-globalfile))
290 (unless (file-exists-p g)
291 (make-directory (file-name-directory g) t)
293 (setq prj-last-open (car prj-current))
294 (when (frame-live-p prj-initial-frame)
295 (setq prj-frame-pos (prj-get-frame-pos prj-initial-frame))
297 (setq fp (prj-create-file g))
298 (when fp
299 (prj-print-list 'prj-version fp)
300 (prj-print-list 'prj-list fp)
301 (prj-print-list 'prj-last-open fp)
302 (prj-print-list 'prj-frame-pos fp)
303 (prj-close-file fp)
306 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
307 ;; Load/Save local per-project configuration file
309 (defun prj-update-config ()
310 (setq prj-directory-run
311 (file-name-as-directory
312 (expand-file-name
313 (or (prj-getconfig "run-directory") ".")
314 prj-directory
318 (defun prj-loadconfig (a)
319 (let (lf e)
320 (prj-reset)
321 (setq prj-current a)
322 (setq prj-directory
323 (file-name-as-directory
324 (expand-file-name (cadr a))
327 (when (file-exists-p (setq lf (prj-localfile)))
328 (load lf nil t)
329 (setq prj-curfile
330 (or (assoc prj-curfile prj-files)
331 (car prj-files)
334 (if (setq e (prj-getconfig "project-name"))
335 (setcar a e)
336 (prj-setconfig "project-name" (car a))
338 (prj-update-config)
339 (prj-set-functions prj-functions)
340 (setq prj-version eproject-version)
343 (defun prj-saveconfig ()
344 (when prj-current
345 (let (w c b files)
346 (prj-removehooks)
347 (setq w (selected-window))
348 (setq c (window-buffer w))
349 (dolist (f prj-files)
350 (cond ((setq b (get-buffer (car f)))
351 (set-window-buffer w b t)
352 (with-current-buffer b
353 (let ((s (line-number-at-pos (window-start w)))
354 (p (line-number-at-pos (window-point w)))
356 (push (list (car f) s p) files)
358 ((consp (cdr f))
359 (push f files)
361 (set-window-buffer w c t)
362 (prj-addhooks)
363 (let ((fp (prj-create-file (prj-localfile)))
364 (prj-curfile (car prj-curfile))
365 (prj-files (nreverse files))
367 (when fp
368 (prj-print-list 'prj-version fp)
369 (prj-print-list 'prj-config fp)
370 (prj-print-list 'prj-tools fp)
371 (prj-print-list 'prj-files fp)
372 (prj-print-list 'prj-curfile fp)
373 (prj-print-list 'prj-functions fp)
374 (prj-close-file fp)
378 (defun prj-saveall ()
379 (prj-saveconfig)
380 (prj-savelist)
383 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
384 ;; The core functions: Open / Close / Add / Remove Project
386 (defun eproject-open (a)
387 "Open another project."
388 (interactive
389 (list
390 (or (prj-config-get-result 'p)
391 (completing-read "Open Project: " (mapcar 'car prj-list))
393 (unless (consp a)
394 (let ((b (assoc a prj-list)))
395 (unless b
396 (error "No such project: %s" a)
398 (setq a b)
400 (setq a (or (car (member a prj-list)) a))
401 (unless (eq a prj-current)
402 (unless (file-directory-p (cadr a))
403 (error "Error: No such directory: %s" (cadr a))
405 (setq prj-list (cons a (delq a prj-list)))
406 (eproject-close)
407 (prj-loadconfig a)
409 (prj-addhooks)
410 (prj-setup-all)
411 (cd prj-directory)
412 (unless (prj-edit-file prj-curfile)
413 (eproject-dired)
416 (defun eproject-close ()
417 "Close the current project."
418 (interactive)
419 (when prj-current
420 (prj-saveconfig)
421 (prj-removehooks)
422 (let (f)
423 (unwind-protect
424 (progn
425 (save-some-buffers nil)
426 (eproject-killbuffers t)
427 (setq f t)
429 (or f (prj-addhooks))
431 (prj-reset)
432 (prj-config-reset)
433 (prj-setup-all)
436 (defun eproject-killbuffers (&optional from-project)
437 "If called interactively kills all buffers that
438 do not belong to project files"
439 (interactive)
440 (let (a b)
441 (dolist (f prj-files)
442 (setq b (get-buffer (car f)))
443 (if b
444 (setq a (cons (list b) a))
446 (dolist (b (buffer-list))
447 (when (eq (consp (assoc b a)) from-project)
448 (kill-buffer b)
449 ))))
451 (defun eproject-add (d)
452 "Add a new or existing project to the list."
453 (interactive
454 (list
455 (read-directory-name "Add project in directory: " prj-directory nil t)
457 (when d
458 (setq d (directory-file-name d))
460 (when (= 0 (length d))
461 (error "Error: Empty directory name.")
463 (let (n a)
464 (setq n (file-name-nondirectory d))
465 (setq a (list n d))
466 (push a prj-list)
467 (prj-setup-all)
470 (defun eproject-remove (a)
471 "Remove a project from the list."
472 (interactive
473 (list
474 (or (prj-config-get-result 'p)
475 (completing-read "Remove project: " (mapcar 'car prj-list))
477 (unless (consp a)
478 (let ((b (assoc a prj-list)))
479 (unless b
480 (error "No such project: %s" a)
482 (setq a b)
484 (when (progn
485 (beep)
486 (prog1 (y-or-n-p (format "Remove \"%s\"? " (car a)))
487 (message "")
489 (setq prj-list (prj-del-list prj-list a))
490 (prj-setup-all)
493 (defun eproject-save ()
494 "Save the project configuration to file."
495 (interactive)
496 (prj-config-parse)
497 (prj-config-print)
498 (prj-saveall)
501 (defun eproject-revert ()
502 "Reload the project configuration from file."
503 (interactive)
504 (prj-loadlist)
505 (if prj-current
506 (prj-loadconfig prj-current)
508 (prj-setup-all)
511 (defun eproject-addfile (f)
512 "Add a file to the current project."
513 (interactive
514 (and prj-current
515 (list
516 (read-file-name "Add file to project: " nil nil t nil)
518 (unless prj-current (error "No project open"))
519 (let ((a (prj-insert-file f (prj-config-get-result 'f))))
520 (unless (cdr a)
521 (message "Added to project: %s" (car a))
523 (prj-config-print)
524 (prj-setmenu)
527 (defun eproject-removefile (a)
528 "Remove a file from the current project."
529 (interactive (prj-get-existing-file-1 "Remove file from project: "))
530 (setq a (prj-get-existing-file-2 a))
531 (prj-remove-file a)
534 (defun eproject-visitfile (a)
535 "Visit a file from the current project."
536 (interactive (prj-get-existing-file-1 "Visit file: "))
537 (setq a (prj-get-existing-file-2 a))
538 (prj-edit-file a)
541 (defun prj-get-existing-file-1 (msg)
542 (and prj-current
543 (list
544 (or (prj-config-get-result 'f)
545 (completing-read msg (mapcar 'car prj-files))
546 ))))
548 (defun prj-get-existing-file-2 (a)
549 (unless prj-current (error "No project open"))
550 (if (consp a)
552 (let ((b (assoc (prj-relative-path a) prj-files)))
553 (unless b (error "No such file in project: %s" a))
557 (defun eproject-help ()
558 "Show the eproject README."
559 (interactive)
560 (view-file (eproject-addon "eproject.txt"))
563 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
564 ;; Hook functions to track opening/closing files from emacs
566 (defun prj-addhooks ()
567 (add-hook 'kill-buffer-hook 'prj-kill-buffer-hook)
568 (add-hook 'find-file-hook 'prj-find-file-hook)
569 (add-hook 'window-configuration-change-hook 'prj-wcc-hook)
572 (defun prj-removehooks ()
573 (remove-hook 'window-configuration-change-hook 'prj-wcc-hook)
574 (remove-hook 'find-file-hook 'prj-find-file-hook)
575 (remove-hook 'kill-buffer-hook 'prj-kill-buffer-hook)
578 (defun prj-wcc-hook ()
579 (let ((w (selected-window)) (b (window-buffer (selected-window))))
580 ;; (message "wcc-hook: %s" (prin1-to-string (list wcc-count w b n)))
581 (prj-register-buffer b)
584 (defun prj-find-file-hook ()
585 (run-with-idle-timer
588 `(lambda () (prj-register-buffer ,(current-buffer)))
591 (defun prj-kill-buffer-hook ()
592 (let ((b (current-buffer)) a)
593 (if (setq a (rassq b prj-files))
594 (prj-remove-file a t)
595 (if (setq a (rassq b prj-removed-files))
596 (setq prj-removed-files (delq a prj-removed-files))
597 ))))
599 (defun prj-register-buffer (b)
600 (let (f a i)
601 (setq f (buffer-file-name b))
602 (when f
603 (setq a (rassq b prj-files))
604 (unless a
605 (setq a (prj-insert-file f nil t))
606 (when a
607 (unless (cdr a)
608 (message "Added to project: %s" (car a))
610 (setcdr a b)
611 (with-current-buffer b
612 (rename-buffer (car a) t)
614 (when (and a (null (eq a prj-curfile)))
615 (setq prj-curfile a)
616 (prj-setmenu)
620 (defun prj-insert-file (f &optional after on-the-fly)
621 (let ((r (prj-relative-path f)) a m)
622 (setq a (assoc r prj-files))
623 (unless (or a (and on-the-fly (assoc r prj-removed-files)))
624 (setq a (list r))
625 (setq m (memq (or after prj-curfile) prj-files))
626 (if m
627 (setcdr m (cons a (cdr m)))
628 (setq prj-files (prj-add-list prj-files a))
630 (setq prj-removed-files (prj-del-list prj-removed-files a))
634 (defun prj-remove-file (a &optional on-the-fly)
635 (let ((n (prj-otherfile prj-files a)) b)
636 (setq prj-files (prj-del-list prj-files a))
637 (when (eq prj-curfile a)
638 (setq prj-curfile n)
640 (unless on-the-fly
641 (setq prj-removed-files (prj-add-list prj-removed-files a))
643 (unless (prj-config-print)
644 (prj-edit-file prj-curfile)
646 (prj-setmenu)
647 (message "Removed from project: %s" (car a))
650 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
651 ;; Edit another file
653 (defun prj-edit-file (a)
654 (when a
655 (let ((n (car a)) f b pos)
656 (setq f (expand-file-name n prj-directory))
657 (setq b (get-file-buffer f))
658 (unless b
659 (prj-removehooks)
660 (setq b (find-file-noselect f))
661 (prj-addhooks)
662 (when b
663 (with-current-buffer b
664 (rename-buffer n t)
666 (setq pos (cdr a))
668 (when b
669 (setcdr a b)
670 (eproject-setup-quit)
671 (switch-to-buffer b)
672 (prj-restore-edit-pos pos (selected-window))
673 (prj-setmenu)
675 (setq prj-curfile a)
678 (defun prj-restore-edit-pos (pos w)
679 (when (consp pos)
680 (let ((b (current-buffer)) (top (car pos)) (line (cadr pos)))
681 (when (and (numberp top) (numberp line))
682 (prj-goto-line top)
683 (set-window-start w (point))
684 (prj-goto-line line)
685 ))))
687 (defun prj-select-window (w)
688 (let (focus-follows-mouse)
689 (select-window w)
690 (select-frame-set-input-focus (window-frame w))
693 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
694 ;; choose next/previous file
696 (defun eproject-nextfile ()
697 "Switch to the next file that belongs to the current project."
698 (interactive)
699 (prj-switch-file 'prj-next-file 'next-buffer)
702 (defun eproject-prevfile ()
703 "Switch to the previous file that belongs to the current project."
704 (interactive)
705 (prj-switch-file 'prj-prev-file 'previous-buffer)
708 (defun prj-switch-file (fn1 fn2)
709 (let ((a (rassoc (current-buffer) prj-files)))
710 (cond (a
711 (prj-edit-file (funcall fn1 prj-files a))
713 (prj-curfile
714 (prj-edit-file prj-curfile)
717 (funcall fn2)
718 ))))
720 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
721 ;; Set key shortcuts
723 (defun prj-setkeys ()
724 (let ((f (consp prj-current))
725 (a (assoc 'eproject-mode minor-mode-map-alist))
726 (map (make-sparse-keymap))
728 (if a
729 (setcdr a map)
730 (push (cons 'eproject-mode map) minor-mode-map-alist)
732 (when f
733 (define-key map [M-right] 'eproject-nextfile)
734 (define-key map [M-left] 'eproject-prevfile)
735 (define-key map [C-f5] 'eproject-dired)
736 (let ((n 0) fn s)
737 (dolist (a prj-tools)
738 (unless (setq fn (nth n prj-tools-fns))
739 (setq fn (list 'lambda))
740 (setq prj-tools-fns (nconc prj-tools-fns (list fn)))
742 (setcdr fn `(() (interactive) (prj-run-tool ',a)))
743 (setq n (1+ n))
744 (when (setq s (caddr a))
745 (define-key map (prj-parse-key s) (and f fn))
746 ))))
747 (define-key map [f5] 'eproject-setup-toggle)
750 (defun prj-parse-key (s)
751 (read
752 (if (string-match "[a-z][a-z0-9]+$" s)
753 (concat "[" s "]")
754 (concat "\"\\" s "\""))))
756 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
757 ;; Set menus
759 (defun prj-list-sorted ()
760 (sort (append prj-list nil)
761 '(lambda (a b) (string-lessp (car a) (car b)))
764 (defun prj-setmenu ()
765 (let ((f (consp prj-current)) m1 m2 m3)
767 (setq m1
768 `(("Open" open ,@(prj-menulist-maker prj-list prj-current 'prj-menu-open))
769 ("Add/Remove" other
770 ("Add ..." "Add new or existing project to the list" . eproject-add)
771 ("Remove ..." "Remove project from the list" . eproject-remove)
772 ,@(and f '(("Close" "Close current project" . eproject-close)))
773 ("--")
774 ("Setup" "Enter the project setup area." . eproject-setup-toggle)
775 ("Help" "View eproject.txt" . eproject-help)
778 (when f
779 (nconc m1 (cons '("--") (prj-menulist-maker prj-tools nil prj-tools-fns)))
780 (setq m2
781 `(("Dired" "Browse project directory in Dired - Use 'a' to add file(s) to the project" . eproject-dired)
782 ("--")
783 ,@(prj-menulist-maker prj-files prj-curfile 'prj-menu-edit)
786 (prj-menu-maker
787 global-map
788 `((buffer "Project" project ,@m1)
789 (file "List" list ,@m2)
791 '(menu-bar)
794 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
796 (defun prj-menu-edit ()
797 (interactive)
798 (let ((a (nth last-command-event prj-files)))
799 (if a (prj-edit-file a))
802 (defun prj-menu-open ()
803 (interactive)
804 (let ((a (nth last-command-event prj-list)))
805 (if a (eproject-open (car a)))
808 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
810 (defun prj-menu-maker (map l v)
811 (let ((e (list nil)))
812 (setq v (append v e))
813 (dolist (k (reverse l))
814 (let (s a)
815 (when (symbolp (car k))
816 (setq a (pop k))
818 (cond
819 ((numberp (car k))
820 (setcar e (pop k))
822 ((and (consp (cdr k)) (symbolp (cadr k)))
823 (setcar e (cadr k))
824 (setq s (cddr k))
825 (setq k (and s (cons (car k) (make-sparse-keymap (car k)))))
828 (setcar e (intern (downcase (car k))))
830 (if a
831 (define-key-after map (vconcat v) k a)
832 (define-key map (vconcat v) k)
834 (if s (prj-menu-maker map s v))
835 ))))
837 (defun prj-copy-head (l n)
838 (let (r)
839 (while (and l (> n 0))
840 (push (pop l) r)
841 (setq n (1- n))
843 (nreverse r)
846 (defun prj-split-list (l n)
847 (let (r)
848 (while l
849 (push (prj-copy-head l n) r)
850 (setq l (nthcdr n l))
852 (nreverse r)
855 (defun prj-menulist-maker (l act fns)
856 (let (r (w 30) s (m 0) (n 0) k)
857 (cond
858 ((< (length l) w)
859 (prj-menulist-maker-1 (list l fns n) act)
862 ;; menu too long; split into submenus
863 (setq s (prj-split-list l w))
864 (setq k (prj-menulist-maker-1 (list (append (pop s) '(("--"))) fns n) act))
865 (setq r (nreverse k))
866 (dolist (l s)
867 (when (consp fns)
868 (setq fns (nthcdr w fns))
870 (setq n (+ n w))
871 (setq k (prj-menulist-maker-1 (list l fns n) act))
872 (push (cons (concat (prj-shortname (caar l)) " ...")
873 (cons (intern (format "m_%d" (setq m (1+ m))))
874 k)) r)
876 (nreverse r)
877 ))))
879 (defun prj-menulist-maker-1 (l act)
880 (let (r e f s i n a)
881 (while (car l)
882 (setq a (caar l))
883 (setcar l (cdar l))
884 (setq n (caddr l))
885 (setcar (cddr l) (1+ n))
886 (setq f (if (consp (cadr l))
887 (prog1 (car (cadr l)) (setcar (cdr l) (cdr (cadr l))))
888 (cadr l)))
890 (setq i (car a))
891 (unless (string-match "^ *#" i)
892 (setq s (if (and (consp (cdr a)) (stringp (cadr a))) (cadr a) i))
893 (cond ((equal ">" i)
894 (setq e (cons s (cons (intern s) (prj-menulist-maker-1 l act))))
895 (setq r (cons e r))
897 ((equal "<" i)
898 (setq l nil)
901 (setq i (prj-shortname i))
902 (setq e (cons n (if (eq a act)
903 `(menu-item ,i ,f :button (:toggle . t) :help ,s)
904 (cons i (cons s f)))))
905 (setq r (cons e r))
908 (nreverse r)
911 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
912 ;; Run make and other commands
914 (defun prj-setup-tool-window ()
915 (let ((bn "*compilation*") w h b c f)
916 (unless (get-buffer-window bn t)
917 (setq b (get-buffer-create bn))
918 (setq f (frame-list))
919 (cond ((cdr f)
920 (setq w (frame-first-window (car f)))
921 (delete-other-windows w)
924 (setq h (/ (* 70 (frame-height)) 100))
925 (delete-other-windows w)
926 (setq w (split-window w h))
928 (set-window-buffer w b)
931 (defun prj-run (cmd)
932 (let (dir)
933 (when (string-match "^-in +\\([^[:space:]]+\\) +" cmd)
934 (setq dir (match-string-no-properties 1 cmd))
935 (setq cmd (substring cmd (match-end 0)))
937 (when prj-directory-run
938 (setq dir (expand-file-name (or dir ".") prj-directory-run))
940 (if dir (cd dir))
941 (cond ((string-match "^-e +" cmd)
942 (setq cmd (read (substring cmd (match-end 0))))
943 (unless (commandp cmd)
944 (setq cmd `(lambda () (interactive) ,cmd))
946 (command-execute cmd)
948 ((string-match "\\(.+\\)& *$" cmd)
949 (start-process-shell-command "eproject-async" nil (match-string 1 cmd))
950 (message (match-string 1 cmd))
953 (unless (or (fboundp 'ecb-activate) (fboundp 'ewm-init))
954 (prj-setup-tool-window)
956 (let ((display-buffer-reuse-frames t))
957 (compile cmd)
958 )))))
960 (defun prj-run-tool (a)
961 (unless (string-match "^--+$" (car a))
962 (prj-run (or (cadr a) (car a)))
965 (defun eproject-killtool ()
966 (interactive)
967 (let ((bn "*compilation*") w0 w1)
968 (when (setq w1 (get-buffer-window bn t))
969 (when (fboundp 'kill-compilation)
970 (setq w0 (selected-window))
971 (select-window w1)
972 (kill-compilation)
973 (select-window w0)
974 ))))
976 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
977 ;; run grep on project files
979 (require 'grep)
981 (defun eproject-grep (command-args)
982 "Run the grep command on all the project files."
983 (interactive
984 (progn
985 (grep-compute-defaults)
986 (let ((default (grep-default-command)))
987 (list (read-from-minibuffer
988 "Run grep on project files: "
989 (if current-prefix-arg default grep-command)
992 'grep-history
993 (if current-prefix-arg nil default)
994 )))))
995 (let ((default-directory prj-directory))
996 (dolist (f (mapcar 'car prj-files))
997 (setq command-args (concat command-args " " f))
999 (grep command-args)
1002 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1003 ;; add files to the project with dired
1005 (require 'dired)
1007 (defun prj-dired-addfiles ()
1008 (interactive)
1009 (when prj-current
1010 (let ((n 0) a)
1011 (dolist (f (dired-get-marked-files))
1012 (setq a (prj-insert-file f))
1013 (unless (cdr a)
1014 (setq n (1+ n))
1015 (setq prj-curfile a)
1017 (message "Added to project: %d file(s)" n)
1018 (prj-setmenu)
1021 (defun prj-dired-run ()
1022 (interactive)
1023 (let ((f (dired-get-marked-files)) c)
1024 (and (setq c (pop f))
1025 (null f)
1026 (let ((prj-directory (file-name-directory c)))
1027 (prj-run c)))))
1029 (defun eproject-dired ()
1030 "Start a dired window with the project directory."
1031 (interactive)
1032 (when prj-directory-run
1033 (eproject-setup-quit)
1034 ;;(message "Use 'a' to add marked or single files to the project.")
1035 (dired prj-directory-run)
1036 (let ((map dired-mode-map))
1037 (define-key map [mouse-2] 'dired-find-file)
1038 (define-key map "a" 'prj-dired-addfiles)
1039 (define-key map "r" 'prj-dired-run)
1040 (define-key map [menu-bar operate command] '("Add to Project"
1041 "Add current or marked file(s) to project" . prj-dired-addfiles))
1044 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1046 (defun prj-setup-all ()
1047 (prj-setkeys)
1048 (prj-setmenu)
1049 (prj-settitle)
1050 (prj-config-print)
1053 (defun prj-getconfig (n)
1054 (let ((a (cdr (assoc n prj-config))))
1055 (and (stringp a) a)
1058 (defun prj-setconfig (n v)
1059 (let ((a (assoc n prj-config)))
1060 (unless a
1061 (setq a (list n))
1062 (setq prj-config (nconc prj-config (list a)))
1064 (setcdr a v)
1067 (defun prj-on-kill ()
1068 (save-some-buffers t)
1069 (prj-saveall)
1072 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1073 ;; Initialize
1075 (defun prj-startup-delayed ()
1076 ;; where is this file
1077 (setq eproject-directory
1078 (file-name-directory (symbol-file 'eproject-startup)))
1080 ;; load UI support
1081 (load (eproject-addon "eproject-config") nil t)
1083 ;; When no projects are specified yet, load the eproject project itself.
1084 (unless prj-list
1085 (load (eproject-addon "eproject.cfg"))
1088 ;; no project so far
1089 (prj-reset)
1090 (prj-setup-all)
1091 (add-hook 'kill-emacs-hook 'prj-on-kill)
1093 ;; inhibit open last project when a file was on the commandline
1094 (unless (buffer-file-name (window-buffer))
1095 (when prj-last-open
1097 ;; open last project
1098 (eproject-open prj-last-open)
1100 ;; restore frame position
1101 (unless (fboundp 'ewm-init)
1102 (when (and prj-frame-pos prj-initial-frame)
1103 (modify-frame-parameters prj-initial-frame prj-frame-pos)
1104 ;; emacs bug: when it's too busy it doesn't set frames correctly.
1105 (sit-for 0.2)
1106 ))))
1108 (when (fboundp 'ecb-activate)
1109 (ecb-activate)
1113 (defun prj-command-line-switch (option)
1114 (setq prj-last-open (pop argv))
1115 (setq inhibit-startup-screen t)
1118 (defun eproject-startup ()
1119 (if (boundp 'prj-list)
1120 (progn
1121 (load (eproject-addon "eproject-config"))
1122 (prj-setup-all))
1123 (progn
1124 (prj-loadlist)
1125 (when prj-last-open (setq inhibit-startup-screen t))
1126 (when (display-graphic-p) (setq prj-initial-frame (selected-frame)))
1127 (push '("project" . prj-command-line-switch) command-switch-alist)
1128 (run-with-idle-timer 0.1 nil 'prj-startup-delayed)
1131 ;;;###autoload(require 'eproject)
1132 (provide 'eproject)
1133 (eproject-startup)
1135 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1136 ;; eproject.el ends here