help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: compile-command customisation


From: Kevin Rodgers
Subject: Re: compile-command customisation
Date: Wed, 23 Feb 2005 08:48:27 -0700
User-agent: Mozilla Thunderbird 0.9 (X11/20041105)

Daniel Wright wrote:
> Sorry to flog a dead horse: this subject has been discussed recently.
> But I found the compile-command customisation so fantastic, it took me
> to a new level of emacs power, (from the emacs wiki):
>
> (add-hook 'c-mode-common-hook
>      (lambda ()
>        (unless (file-exists-p "Makefile")
>          (set (make-local-variable 'compile-command)
>               (let ((file (file-name-nondirectory buffer-file-name)))
>                 (concat "gcc -g -Wall -W -o " (file-name-sans-extension file)
>                         " " file))))))
>
> But let's say you want to write a little test c/c++ program that you
> don't want to include in your Makefile, but still have in the
> project's directory (i.e. there is a Makefile there). Then the above
> version of the function won't work, so I've playing with the following
> (which searches through the Makefile):

It doesn't work because of the presence of a Makefile, right?  But make
has many built-in default rules, including rules that allow you to build
an executable from its C or C++ source.  In fact, there is a similar
code snippet in compile-command's doc string, and just last week I was
considering submitting a bug report that the Makefile test is not useful
in the mode hook.

Note also that make's default rules are parameterized by macros,
e.g. CC, CPPFLAGS, CFLAGS, CXXFLAGS, and LDFLAGS, which in turn get
their default values from your environment.  So all you should really
need is CC=gcc and CFLAGS="-g -W -Wall", plus this:

(add-hook 'c-mode-common-hook
          (lambda ()
            (set (make-local-variable 'compile-command)
                 (format "make %s"
                         (file-name-sans-extension
                          (file-name-nondirectory buffer-file-name))))))

> (add-hook 'c-mode-common-hook
>      (lambda ()
>        (let ((count 0) (file (file-name-nondirectory buffer-file-name)))
>          (if (file-exists-p "Makefile")
>              (progn
>                (find-file "Makefile")
>                (while (search-forward (file-name-sans-extension file) nil t 
count)
>                  (setq count (1+ count)))
>                (find-file file)))
>          (unless (> count 1)
>            (progn
>              (set (make-local-variable 'compile-command)
>                   (concat "gcc -g -Wall -W -o " (file-name-sans-extension 
file)
>                           " " file)))))))
>
> I was wondering what you all think. I started with emacs lisp this
> week, so I don't mind if you tell me how bad it is, as long as you
> tell us how it could be done better :)

I think that less is more.

--
Kevin Rodgers

reply via email to

[Prev in Thread] Current Thread [Next in Thread]