-2

It's probably a very basic question, but I am having a hard time finding an answer to it:

I am trying to make emacs look for a makefile somewhere in the parent directories and use it for compilation.

I found this function:

(defun desperately-compile ()
  "Traveling up the path, find a Makefile and `compile'."
  (interactive)
  (when (locate-dominating-file default-directory "Makefile")
  (with-temp-buffer
    (cd (locate-dominating-file default-directory "Makefile"))
    (compile "make -k"))))

and put it in my .emacs file, but I can't figure out how to call it. replacing the compile command doesn't seem to work and I'm not sure how to bind a key to it.

It would be best to call that function using "M-x compile" or "M-x desperate-compile", but I am open for suggestions.

thanks.

7
  • Does M-x desperately-compile work?
    – PythonNut
    Commented Aug 15, 2015 at 20:28
  • it completes as an option but does nothing...
    – NivPgir
    Commented Aug 15, 2015 at 20:35
  • Then the problem is with the function itself, and binding it to a key won't help.
    – Dan
    Commented Aug 15, 2015 at 20:43
  • There is no problem with your function desperately-compile. It seems straight-forward and it does what I think it was intended to do: it runs make in the directory closest in the file tree to the currently opened file. If you need to bind it to a key, then, definitely, global-set-key is the way to go. PS. I'll be voting on closing this question because I believe that it shows a lack of effort (the info you are asking for is very easy to find).
    – wvxvw
    Commented Aug 15, 2015 at 23:04
  • 1
    Since this is your first question on this site, it's probably worth looking at meta.stackexchange.com/questions/10582/… this answer if you want to know about closing questions. That is to say this is not the end of the road, and is not a deliberate attack on a novice user. It is done in good faith that it will encourage you and other site users to better the quality of questions asked here.
    – wvxvw
    Commented Aug 15, 2015 at 23:07

1 Answer 1

3

Your question: "I'm not sure how to bind a key to it", IOW, how do you bind a command to a key?

What did you try? Did you look in the Emacs manual for how to bind a command to a key? For example: C-h r to get to the Emacs manual, then i bind TAB and choose binding-keys, to get to node Rebinding. It helps us help you if you tell us what you have already tried. If you tried using some code to bind the key, show that code, etc.

Anyway, here's an example of how to bind command desperately-compile to key C-o:

(global-set-key (kbd "C-o") 'desperately-compile)

If you want to see whether your command actually gets invoked when you hit the key you think you've bound to it, try M-x debug-on-entry RET desperately-compile RET.

If the command is bound to your key then when you hit your key the debugger will open. Use M-x cancel-debug-on-entry RET to cancel this.

(You can alternatively add a call to function message at the beginning of your command code. But as you said that you "found" that code somewhere maybe you don't want to fiddle with it yet.)

Anyway, this is an answer to your question, which is how to bind a key to a command. As to whether your command "works", and if not, why not: please post that as a separate question.


(Suggestion: Read some of the Emacs manual. Or follow the Emacs tutorial. You will be glad to have some introductory understanding of things. And if you are going to be writing Lisp code then check out the manual Emacs Lisp Intro, which you can get to by C-h i.)

1
  • Whoever: care to say why you downvoted the answer?
    – Drew
    Commented Aug 16, 2015 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.