all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* compile-command customisation
@ 2005-02-23 12:51 Daniel Wright
  0 siblings, 0 replies; 12+ messages in thread
From: Daniel Wright @ 2005-02-23 12:51 UTC (permalink / raw)


Hi all,
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):

(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 :)

Cheers,
Daniel

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: compile-command customisation
       [not found] <mailman.1248.1109165001.32256.help-gnu-emacs@gnu.org>
@ 2005-02-23 14:51 ` Stefan Monnier
  2005-02-26 22:13   ` Steinar Børmer
  2005-02-26 22:23   ` Steinar Børmer
  2005-02-23 15:44 ` Thien-Thi Nguyen
  2005-02-23 15:48 ` Kevin Rodgers
  2 siblings, 2 replies; 12+ messages in thread
From: Stefan Monnier @ 2005-02-23 14:51 UTC (permalink / raw)


> 	      (if (file-exists-p "Makefile")
> 		  (progn
> 		    (find-file "Makefile")

Never call `find-file' from elisp.
Instead, use (with-current-buffer (find-file-noselect "Makefile") ...).


        Stefan

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: compile-command customisation
       [not found] <mailman.1248.1109165001.32256.help-gnu-emacs@gnu.org>
  2005-02-23 14:51 ` Stefan Monnier
@ 2005-02-23 15:44 ` Thien-Thi Nguyen
  2005-02-23 15:48 ` Kevin Rodgers
  2 siblings, 0 replies; 12+ messages in thread
From: Thien-Thi Nguyen @ 2005-02-23 15:44 UTC (permalink / raw)


Daniel Wright <daniel.d.wright@gmail.com> writes:

> how it could be done better :)

some standard tips:

(if CONDITION (progn EXP ...))
=> (when CONDITION EXP ...)

(unless CONDITION (progn EXP ...))
=> (unless CONDITION EXP ...)

you can lift `file-name-sans-extension', like so:

(... (file-name-sans-extension file)
 ... (file-name-sans-extension file) ...)
=> (let ((fn-noext (file-name-sans-extension file)))
     (... fn-noext
      ... fn-noext ...))

generally, these transforms do not change your algorithm,
so it is only better in the sense that it is more easy to
read.  improving the algorithm is your sack of joy to haul.

thi

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: compile-command customisation
       [not found] <mailman.1248.1109165001.32256.help-gnu-emacs@gnu.org>
  2005-02-23 14:51 ` Stefan Monnier
  2005-02-23 15:44 ` Thien-Thi Nguyen
@ 2005-02-23 15:48 ` Kevin Rodgers
  2 siblings, 0 replies; 12+ messages in thread
From: Kevin Rodgers @ 2005-02-23 15:48 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: compile-command customisation
@ 2005-02-24  8:59 Daniel Wright
  0 siblings, 0 replies; 12+ messages in thread
From: Daniel Wright @ 2005-02-24  8:59 UTC (permalink / raw)


Stefan Monnier <monnier@iro.umontreal.ca> writes:
>Never call `find-file' from elisp.
>Instead, use (with-current-buffer (find-file-noselect "Makefile") ...).
>
>      Stefan

Thanks a lot for the tip! It did seem a bit over zealous to hop around
from file to file. But the way you use "never" makes me think, that it
is more than just common sense - is there another reason too?

The emacs lisp manual (i finally looked at it) suggests copying the
the contents of the file to an empty buffer (with
"insert-file-contents"), it seems a good idea in this case - i could
then just search that. What do you think?

Thanks again!
Daniel

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: compile-command customisation
@ 2005-02-24  9:19 Daniel Wright
  0 siblings, 0 replies; 12+ messages in thread
From: Daniel Wright @ 2005-02-24  9:19 UTC (permalink / raw)


Thien-Thi Nguyen <ttn@glug.org> writes:
>Daniel Wright <daniel.d.wright@gmail.com> writes:
>
>> how it could be done better :)
>
>some standard tips:
>
>(if CONDITION (progn EXP ...))
>=> (when CONDITION EXP ...)
>
>(unless CONDITION (progn EXP ...))
>=> (unless CONDITION EXP ...)
>
>you can lift `file-name-sans-extension', like so:
>
>(... (file-name-sans-extension file)
>... (file-name-sans-extension file) ...)
>=> (let ((fn-noext (file-name-sans-extension file)))
>    (... fn-noext
>     ... fn-noext ...))
>
>generally, these transforms do not change your algorithm,
>so it is only better in the sense that it is more easy to
>read.  improving the algorithm is your sack of joy to haul.

Thanks for the help! It really does make it a lot easier to read. It
seemed necessary to use "let*" so that file-noext gets evaluated
correctly:

...
(let* ((count 0) (file (file-name-nondirectory buffer-file-name))
     (file-noext (file-name-sans-extension file)))
...

After reading the reply from Kevin Rodgers, I'll leave the improvement
of this algorithm: I'm sure there are plenty of others to improve.. ;)

Thanks again!
Daniel

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: compile-command customisation
       [not found] <421ce346.7d020262.679d.ffff9321SMTPIN_ADDED@mx.gmail.com>
@ 2005-02-24  9:41 ` Daniel Wright
  0 siblings, 0 replies; 12+ messages in thread
From: Daniel Wright @ 2005-02-24  9:41 UTC (permalink / raw)


Kevin Rodgers <ihs_4664@yahoo.com> writes:

> 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))))))
> I think that less is more.
> --
> Kevin Rodgers

Thanks! This is a much better and more elegant solution. I'm still
unfamiliar with make, but you've really helped my understanding.
All the best,
Daniel.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: compile-command customisation
       [not found] <mailman.1420.1109237398.32256.help-gnu-emacs@gnu.org>
@ 2005-02-25 13:49 ` Stefan Monnier
  2005-02-26 12:02   ` Daniel Wright
  0 siblings, 1 reply; 12+ messages in thread
From: Stefan Monnier @ 2005-02-25 13:49 UTC (permalink / raw)


> Thanks a lot for the tip! It did seem a bit over zealous to hop around
> from file to file. But the way you use "never" makes me think, that it
> is more than just common sense - is there another reason too?

`find-file' may fail in the case where the current window does not allow
switching buffer (e.g. it's a minibuffer window, or a dedicated window).

> The emacs lisp manual (i finally looked at it) suggests copying the
> the contents of the file to an empty buffer (with
> "insert-file-contents"), it seems a good idea in this case - i could
> then just search that. What do you think?

It all depends on whether you expect that the files might already be loaded
in a buffer and whether the user would find it convenient or annoying that
the Makefile file is automatically loaded.


        Stefan

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: compile-command customisation
  2005-02-25 13:49 ` compile-command customisation Stefan Monnier
@ 2005-02-26 12:02   ` Daniel Wright
  0 siblings, 0 replies; 12+ messages in thread
From: Daniel Wright @ 2005-02-26 12:02 UTC (permalink / raw)


Stefan Monnier wrote:
>>Thanks a lot for the tip! It did seem a bit over zealous to hop around
>>from file to file. But the way you use "never" makes me think, that it
>>is more than just common sense - is there another reason too?
> 
> 
> `find-file' may fail in the case where the current window does not allow
> switching buffer (e.g. it's a minibuffer window, or a dedicated window).
> 
> 
>>The emacs lisp manual (i finally looked at it) suggests copying the
>>the contents of the file to an empty buffer (with
>>"insert-file-contents"), it seems a good idea in this case - i could
>>then just search that. What do you think?
> 
> 
> It all depends on whether you expect that the files might already be loaded
> in a buffer and whether the user would find it convenient or annoying that
> the Makefile file is automatically loaded.
> 
> 
>         Stefan
Thanks again for your help, it's most appreciated.
Daniel.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: compile-command customisation
  2005-02-23 14:51 ` Stefan Monnier
@ 2005-02-26 22:13   ` Steinar Børmer
  2005-02-26 22:23   ` Steinar Børmer
  1 sibling, 0 replies; 12+ messages in thread
From: Steinar Børmer @ 2005-02-26 22:13 UTC (permalink / raw)


Stefan Monnier wrote:

| > 	      (if (file-exists-p "Makefile")
| > 		  (progn
| > 		    (find-file "Makefile")
| 
| Never call `find-file' from elisp.
| Instead, use (with-current-buffer (find-file-noselect "Makefile") ...).

I assume the reason is because find-file is meant to be used
interactively?

I use find-file in my .emacs.el, and realized that I need a generic
defun if find-file is not "good".

This is what I've been using:

(global-set-key (kbd "C-<f6>") #'(lambda ()
                                  (interactive)
                                  (find-file "~/.emacs.el")))

Is this "wrong", and why?


I assume something like the following is a good way to avoid find-file,
but it seems overly elaborate:

(defun quick-find (file)
  "Find the file FILE."
  (let ((buffer (with-current-buffer (find-file-noselect file))))
    (switch-to-buffer buffer)))

It also appears to do pretty much the same as find-file itself.

Comments?

-- 
SB

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: compile-command customisation
  2005-02-23 14:51 ` Stefan Monnier
  2005-02-26 22:13   ` Steinar Børmer
@ 2005-02-26 22:23   ` Steinar Børmer
  2005-02-27  2:43     ` Stefan Monnier
  1 sibling, 1 reply; 12+ messages in thread
From: Steinar Børmer @ 2005-02-26 22:23 UTC (permalink / raw)


Stefan Monnier wrote:

| > 	      (if (file-exists-p "Makefile")
| > 		  (progn
| > 		    (find-file "Makefile")
| 
| Never call `find-file' from elisp.
| Instead, use (with-current-buffer (find-file-noselect "Makefile") ...).

I assume the reason is because find-file is meant to be used
interactively?

I use find-file in my .emacs.el, and realized that I need a generic
defun if find-file is not "good".

This is what I've been using:

(global-set-key (kbd "C-<f6>") #'(lambda ()
                                  (interactive)
                                  (find-file "~/.emacs.el")))

Is this "wrong", and why?


I assume something like the following is a good way to avoid find-file,
but it seems overly elaborate:

(defun quick-find (file)
  "Find the file FILE."
  (let ((buffer (find-file-noselect file)))
    (switch-to-buffer buffer)))

It also appears to do pretty much the same as find-file itself.

Comments?

-- 
SB

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: compile-command customisation
  2005-02-26 22:23   ` Steinar Børmer
@ 2005-02-27  2:43     ` Stefan Monnier
  0 siblings, 0 replies; 12+ messages in thread
From: Stefan Monnier @ 2005-02-27  2:43 UTC (permalink / raw)


> This is what I've been using:

> (global-set-key (kbd "C-<f6>") #'(lambda ()
>                                   (interactive)
>                                   (find-file "~/.emacs.el")))

> Is this "wrong", and why?

No, it's not wrong.  while strictly speaking it's calling find-file from
elisp, it's not what I meant by "calling find-file from elisp".
The difference is that the purpose of your elisp function is to call
find-file, so of course it should do that.

> I assume something like the following is a good way to avoid find-file,
> but it seems overly elaborate:

> (defun quick-find (file)
>   "Find the file FILE."
>   (let ((buffer (find-file-noselect file)))
>     (switch-to-buffer buffer)))

> It also appears to do pretty much the same as find-file itself.

Indeed, it's no better.  Because `switch-to-buffer' should also usually be
avoided (unless it's indeed exactly what you want to do).
See C-h f switch-to-buffer RET for example (or the fact that
switch-to-buffer signals an error if you try to use it in a dedicated
window or in a minibuffer window).


        Stefan

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2005-02-27  2:43 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1420.1109237398.32256.help-gnu-emacs@gnu.org>
2005-02-25 13:49 ` compile-command customisation Stefan Monnier
2005-02-26 12:02   ` Daniel Wright
     [not found] <421ce346.7d020262.679d.ffff9321SMTPIN_ADDED@mx.gmail.com>
2005-02-24  9:41 ` Daniel Wright
2005-02-24  9:19 Daniel Wright
  -- strict thread matches above, loose matches on Subject: below --
2005-02-24  8:59 Daniel Wright
     [not found] <mailman.1248.1109165001.32256.help-gnu-emacs@gnu.org>
2005-02-23 14:51 ` Stefan Monnier
2005-02-26 22:13   ` Steinar Børmer
2005-02-26 22:23   ` Steinar Børmer
2005-02-27  2:43     ` Stefan Monnier
2005-02-23 15:44 ` Thien-Thi Nguyen
2005-02-23 15:48 ` Kevin Rodgers
2005-02-23 12:51 Daniel Wright

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.