Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good way to do Emacs project?

Tags:

emacs

project

I use emacs to do some coding, and text editing. When I create a new coding project, I simply create a new folder, and add source code into it.

The problem is, with multi-folders, it is hard to change back to the top, and run the makefile.

Is there any good method to do project management like eclipse or other IDEs?

like image 930
linjunhalida Avatar asked Apr 15 '09 00:04

linjunhalida


2 Answers

I know your problem. If you have a Makefile in the same folder as your source, and you are in a source buffer, then 'compile' will build correctly.

But if your source is in a different folder then emacs can't find the Makefile.

One solution is to specify the Makefile's location by setting the 'default-directory' variable as a file variable in each source file.

You do this by adding a line like this at the top of the file (and reload it).

// -*- mode: C++; default-directory: "c:/somewhere/yourmakefiledirectory/" -*-
like image 80
justinhj Avatar answered Oct 10 '22 14:10

justinhj


Below is the ;; compilation section of my .emacs file. I use CTRL+F7 for make, and F7 for make clean. It will search in the current directory and then in .. and so on for a file called "Makefile" to run make on.

Also not that F8 jumps the source window to the first error and CTRL+F8 takes you to the previous error. (BTW, if you think this is awesome, you should see what I've done for GDB integration)... :)

;; Compilation
(setq compilation-scroll-output 1) ;; automatically scroll the compilation windo
w
(setq compilation-window-height 10) ;; Set the compilation window height...
(setq compilation-finish-function ;; Auto-dismiss compilation buffer...
      (lambda (buf str)
        (if (string-match "exited abnormally" str)
            (message "compilation errors, press F6 to visit")
          ; no errors, make the compilation window go away after 2.5 sec
          (run-at-time 2.5 nil 'delete-windows-on buf)
          (message "No compilation errors!"))))

(require 'cl) ; If you don't have it already
(defun* get-closest-pathname (&optional (file "Makefile"))
  "This function walks up the current path until it finds Makefile and then retu
rns the path to it."
  (let ((root (expand-file-name "/")))
    (expand-file-name file
              (loop
            for d = default-directory then (expand-file-name ".." d)
            if (file-exists-p (expand-file-name file d))
            return d
            if (equal d root)
           return nil))))

(defun my-compile-func ()
  "This function does a compile."
  (interactive)
  (compile (format "make -C %s" (file-name-directory (get-closest-pathname)))))

(defun my-compile-clean-func ()
  "This function does a clean compile."
  (interactive)
  (compile (format "make -C %s clean" (file-name-directory (get-closest-pathname
)))))

(defun my-compile-package-func ()
  "This function builds an Endura package."
  (interactive)
  (compile (format "make -C %s package" (file-name-directory (get-closest-pathna
me)))))

(global-set-key [f7] 'my-compile-clean-func)
(global-set-key [C-f7] 'my-compile-func)
(global-set-key [S-f7] 'my-compile-package-func)
(global-set-key [f8] 'next-error)
(global-set-key [C-f8] 'previous-error)
like image 28
dicroce Avatar answered Oct 10 '22 15:10

dicroce