How does the following statement is interpreted by emacs - emacs

https://stackoverflow.com/a/663636/391104
(defun my-c++-mode-hook ()
(setq c-basic-offset 4)
(c-set-offset 'substatement-open 0))
(add-hook 'c++-mode-hook 'my-c++-mode-hook)
Based on my investigation, I just need to add the above code into my .emacs and then it works magically.
Q1> What does defun my-c++-mode-hook () mean? a function definition in lisp?
Q2> What is the usage of following line? where should I trigger it or it is run automatically by emacs
(add-hook 'c++-mode-hook 'my-c++-mode-hook)
Thank you

Q1: Yes, this is a function definition (hence defun). The second symbol is the name, which has the suffix '-hook' to indicate to humans that it is intended to be used as a hook. It could be given (almost) any arbitrary name without changing its behaviour. The empty () indicates the function takes no arguments. Everything else is the body of the function.
Q2: Basically, this adds a pointer to the previous function to the list of functions that are called when ever c++-mode is started. Whenever you start a mode, the Emacs looks for the mode hook, running all the functions in it. Both the function definition and the add-hook line need to go in your .emacs, and they will be run automatically when you start emacs.
To wrap your head around elisp, the introduction is highly recommended. It ships with emacs, and can be accessed from the info system: C-h i, then look for Elisp Introduction.

Related

Strange Emacs behavior (produces garbage on the screen)

I'm trying to learn Emacs and eLisp by writing some simple macros. Here is one of them:
(global-set-key (kbd "C-c d") 'local-delete-line)
(defun local-delete-line ()
"deletes 1 line"
(interactive)
(beginning-of-line)
(set-mark-command)
(next-line)
(delete-region))
Unfortunately, after triggering C-c d (or any other hotkey that I set up by global-set-key), Emacs responds with this:
Any ideas what can cause this?
To troubleshoot, I've removed my whole .emacs file, created an empty one, and I've put only the definition of local-delete-line function, together with global-set-key command. Emacs still produces garbage when invoking the function.
Start by reading the doc of set-mark-command (C-h f set-mark-command). See what it says about not setting the mark in code you write. See how many arguments it requires.
Read the doc of delete-region: it requires two arguments.
Do M-: (setq debug-on-error t), and then try your recipe. The backtrace buffer will tell you what you have done wrong.
State what it is that you are trying to do. So far, it seems like you just want to delete or kill a line. If so, what's wrong with C-k?

emacs only delete-trailing-whitespace while saving in programming mode

following line removes all training white space while saving.
(add-hook 'write-file-hooks 'delete-trailing-whitespace)
but I want to hook this feature only when i'm in programming mode, so i did
(defun nuke_traling ()
(add-hook 'write-file-hooks 'delete-trailing-whitespace)
)
(add-hook 'prog-mode-hook 'nuke_traling)
which doesn't is not stopping which are not in programming mode.
Making the hook variable buffer-local has been mentioned. Don't do that. Or rather, don't do it using make-local-variable.
The normal hook mechanisms have buffer-local support built in -- that's the purpose of the LOCAL argument to add-hook. When the hook is run, it runs both the global and the buffer-local values.
So taking the example code in the question, you could change it to use:
(add-hook 'write-file-hooks 'delete-trailing-whitespace nil t)
And then delete-trailing-whitespace would be called whenever write-file-hooks was run, but only in the buffers in which prog-mode-hook had run.
However there are better ways to achieve this.
I agree with Drew that you are better to test whether your mode is derived from prog-mode, and with juanleon that before-save-hook is a better hook to use. So you might do something like:
(add-hook 'before-save-hook 'my-prog-nuke-trailing-whitespace)
(defun my-prog-nuke-trailing-whitespace ()
(when (derived-mode-p 'prog-mode)
(delete-trailing-whitespace)))
But what I actually recommend is using either ws-trim or ws-butler to take care of this in a smarter way.
Blindly removing all trailing whitespace from a file is a great way to wind up committing loads of unrelated lines to a version-control repository. Both of the libraries mentioned will ensure that your own commits are free of trailing whitespace, without also introducing unwanted modifications elsewhere in the file.
write-file-hooks is obsolete since Emacs-22, replaced by write-file-functions. But this hook is a bit delicate to use (because it can also be used to perform the write), so I recommend you use before-save-hook instead. And to make it apply only to the current buffer, just pass a non-nil value for the local argument of add-hook, as in:
(defun nuke_traling ()
(add-hook 'before-save-hook #'delete-trailing-whitespace nil t))
(add-hook 'prog-mode-hook #'nuke_traling)
Yes, because as soon as you enter a prog-mode mode, you add the function to write-file-hooks, where it remains. And that hook applies to writing any file, regardless of the mode of its buffer.
Instead of putting that simple function on the hook, you can add a function that tests the mode and only does the whitespace deletion when it is a mode where you want to do that.
Or else you would need to make write-file-hooks buffer-local (which I doubt you would want want to do --- the hook is used more generally).
Bad way:
(add-to-list 'write-file-functions 'delete-trailing-whitespace)
Better way is using ws-butler:
(straight-use-package 'ws-butler)
(add-hook 'prog-mode-hook #'ws-butler-mode)
ws-butler-mode remove spaces only on changed lines.
You would need to make the variable buffer local:
(defun nuke_traling ()
(make-variable-buffer-local 'write-file-hooks)
(add-hook 'write-file-hooks 'delete-trailing-whitespace))
But I would recommend using before-save-hook instead:
(defun nuke_traling ()
(add-to-list 'before-save-hook 'delete-trailing-whitespace))
write-file-hooks may be risky if used as a file-local variable, and documentation recomends using before-save-hook instead for thing like you want to do.
In emacs 21 or later you can add this hook to a perticular mode like this:
(add-hook 'prog-mode-hook
(lambda () (add-to-list 'write-file-functions 'delete-trailing-whitespace)))

add a function with parameters to a hook

I want to run a function according to the programming language when I open a source code file using the following. I have to pass the language-specific str to the foo function. How can do it in the add-hook statement?
(defun foo (str)
(blahblah...))
(add-hook 'prog-mode-hook 'foo)
Use the built-in apply-partially:
(add-hook 'prog-mode-hook (apply-partially #'foo "spam with eggs"))
This is described in the manual (first hit on Google for "emacs add-hook"):
(add-hook 'prog-mode-hook (lambda () (foo "foobarbaz")))
The bad news is that mode hooks usually don't have any arguments. The good news is that here's what (emacs) Hooks section of manual has to say about your situation:
Most major modes run one or more "mode hooks" as the last step of initialization.
So, I didn't test it myself, but I'm pretty sure you can write a generic hook that will inspect major-mode variable and do the mode-specific actions instead.

emacs autoload modes

I am a newbie.
Now I want to
auto start yasnippet auto-complete gtags flymake modes
when I opening a (php/java/el/...) file.
What Should I put in my emacs config file ?
As liu says, you use add-hook to specify that an action be run when a particular mode is started. Documentation here. Hooks are analogous to events in C#, or the observer pattern in OOP.
Every mode should define a hook that is run when that mode is started, eg. for a mode named java-mode there is a corresponding hook called java-mode-hook. add-hook lets you wire an action to this hook:
(add-hook 'java-mode-hook 'my-action)
You can use anonymous lambda functions to define the action in-place, like so:
(add-hook 'java-mode-hook (lambda () (message "hello from java-mode")))
This will print a message whenever you start up java mode.
It is usually better to define a named function for the purpose. If you use named functions, add-hook will ensure the same function is not called multiple times.
(defun hello ()
(message "hello from java-mode"))
(add-hook 'java-mode-hook 'hello)
Language editing modes are derived from prog-mode. If you want to run an action when you start up any programming language mode, you add your function to prog-mode-hook.
You need to manually configure autocomplete mode for each mode you want to use it in. For each mode, add that mode to the ac-modes list:
(add-to-list 'ac-modes 'java-mode)
As a side note, you will want to use flyspell-prog-mode for programming language modes, so you only receive spelling suggestions in comments and string literals.
(defun on-prog-mode ()
(flyspell-prog-mode t))
(add-hook 'prog-mode-hook 'on-prog-mode)
And you will probably want to use flycheck instead of the older flymake. Flycheck is under active development and has checkers for php.
you can add just like below:
(global-auto-complete-mode t)
(yas-global-mode 1)
then update below accordingly:
(add-hook 'php-mode-hook (lambda () (flyspell-mode 1)))
(add-hook 'php-mode-hook (lambda()(gtags-mode 1)))

before defadvice not executing before the function?

[I apologize for the poor title, but couldn't come up with a better one.]
bin chen asked on Google+:
How to input relative path of (buffer-file-name) in minibuffer after M-! in #emacs?
I thought if the buffer-file-name is saved in a register, it should be accessible by invoking insert-register (C-x r i) while at the shell-command prompt.
(defun save-buffer-file-name-in-register ()
(set-register ?F (buffer-file-name))
(set-register ?D (file-name-directory buffer-file-name)))
(defadvice shell-command (before save-buffer-file-name)
"Save buffer-file-name to register F before running shell-command"
(save-buffer-file-name-in-register))
(ad-activate 'shell-command)
When I invoke shell-command (M-!) followed by insert-register (C-x r i), I get the error message: Register does not contain any text.
But when I run list-registers I do see that the registers F and D are set with the appropriate values. If I run the shell-command again, I can access the values from the registers previously saved.
Is it possible that the registers are being set too late for the first time? How can I fix the code to do what I want?
Edit: Changed around to before (Thanks to #phils)
n.b. You have defined around advice, not before advice.
Around advice acts as a wrapper, and must include the token ad-do-it to execute the code of the function it is wrapping.
You have effectively replaced the body of the shell-command function with a call to save-buffer-file-name-in-register
As to your main question, I'd need to check the documentation, but I suspect that because the arguments to the advised function are available to advice, the original function's interactive declaration probably executes before the advice does, which would explain why your register values are not visible at the interactive shell-command prompt.
(If the around in the above code is indeed what you were using, the fact that you were still being prompted for a shell command would seem to verify this sequence.)
When the interactive form runs, your advice hasn't executed yet. See: this question
You need to specify an interactive form in your advice that redefines the original if you want to stick with this approach. However, this approach is a little fancy-pants for the sake of fancy-pants-ness.
Just define your own interactive function which does what you want without registers.
(defun insert-cur-dir ()
(interactive)
(let ((dir-name (file-name-directory (buffer-file-name (window-buffer (minibuffer-selected-window))))))
(insert (or dir-name ""))))
(define-key minibuffer-local-map (kbd "C-c i") 'insert-cur-dir)
An alternative, but way awesomer approach is to use yasnippet.