Invalid function warning in Emacs - emacs

I wanted to get rid of that automatic "splash screen" that Emacs visits (called GNU Emacs). I added the following line to my .emacs file:
(add-hook 'after-init-hook '(kill-buffer "GNU Emacs"))
Well, it works, but I get the following warning message in the echo area:
"Invalid function: (kill-buffer "GNU Emacs")
I don't see what's invalid. Anyone know?
Thanks,
P.S. I'm sure a better approach would be to get Emacs to just not visit the GNU Emacs in the first place, but I haven't figured out how to do that (maybe something in the default.el file?)

Take a look at the variable inhibit-startup-screen.
(setq inhibit-startup-screen t)
The function add-hook expects a function as its second argument; '(kill-buffer ...) evaluates to a list, which is not a function. One way to turn it into a function is to use the lambda operator:
(add-hook 'after-init-hook (lambda () (kill-buffer "GNU Emacs")))

(setq inhibit-default-init 1) is one way to do it. Didn't it work for you?

Related

How does the following statement is interpreted by 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.

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.

wrong type argument: stringp, nil

Before now I've just been cutting and pasting code into my .emacs file, but then I decided to add some maven functionality to emacs. Now, I don't see how I was able to mess this up, but last night I kept getting the error I put in the title when I run M-x jarl-mvn-exec. I slept on it, and came back the next day but I'm still not getting anywhere.
(defun jarl-get-pom ()
(concat (locate-dominating-file
(buffer-file-name
(current-buffer))
"pom.xml")
"pom.xml"))
(defun jarl-visit-pom ()
(interactive)
(find-file (jarl-get-pom)))
(defun jarl-mvn-exec ()
(interactive)
(switch-to-buffer (get-buffer-create "maven"))
(start-process-shell-command "mvn-exec" "maven" "mvn" "-f" (jarl-get-pom) "compile")
(start-process-shell-command "mvn-exec" "maven" "mvn" "-f" (jarl-get-pom) "exec:exec"))
You'll need to provide more information to be sure. Try setting
(setq debug-on-error t)
which will give you a stack trace showing what function is complaining about the string being nil.
My guess is that buffer-file-name is returning nil, and that's where the problem lies (not all buffers have file names). Check out the debugging section of An Introduction To Programming in Emacs Lisp, or the debugging section of the Emacs Lisp manual.
The secret to finding a problem in your init file is not a secret: binary search.
Use comment-region to comment out half your init file, then 3/4, 7/8,... It is very quick to identify the problem. comment-region also uncomments: C-h f comment-region RET.

Run sgml-pretty-print when opening an xml file in emacs?

I can currently use sgml-pretty-print to pretty print an xml file in emacs, but it's a manual process:
M-<
C-space
M->
M-x sgml-pretty-print
I'd like this to happen automatically (or at least have some option to do so). I'm new to emacs/elisp, and do not understand how:
emacs knows what code to run when you open a file (does this start in files.el?)
If you wanted to override that code with your own, how to do that
This should do the trick for you:
(add-hook 'find-file-hook 'my-sgml-find-file-hook)
(defun my-sgml-find-file-hook ()
"run sgml pretty-print on the file when it's opened (if it's sgml)"
(when (eq major-mode 'sgml-mode)
(sgml-pretty-print (point-min) (point-max))))
The key pieces of information are the find-file-hook, point-min (-max), and major-mode.
If you want to learn more about elisp, you can take a look at this question, which gives some pointers on how to figure things out.
A slightly simpler alternative to Trey Jackson's answer. Just add this to your ~/.emacs file:
(add-hook 'sgml-mode-hook #'(lambda ()
(sgml-pretty-print (point-min) (point-max))))

How do I make Emacs start without so much fanfare?

Every time I start Emacs I see a page of help text and a bunch of messages suggesting that I try the tutorial. How do I stop this from happening?
Emacs has a couple of variables which inhibit these actions. If you edit your emacs control file (.emacs) and insert the following:
;; inhibit-startup-echo-area-message MUST be set to a hardcoded
;; string of your login name
(setq inhibit-startup-echo-area-message "USERNAME")
(setq inhibit-startup-message t)
that should solve your problem. They basically set the inhibit parameters to true to prevent the behavior you want to get rid of.
Put the following in your .emacs:
(setq inhibit-startup-message t)
(setq inhibit-startup-echo-area-message t)
You can customize message in minibuffer therefore remove fanfare:
;; Hide advertisement from minibuffer
(defun display-startup-echo-area-message ()
(message ""))
Put the following in your personal init file (ususally ~/.emacs.el):
(setq inhibit-startup-message t)
(Or (setq inhibit-startup-screen t) in with older Emacs versions.)
You can also turn off the message "For information about GNU Emacs and the GNU system, type C-h C-a." in the echo with the variable inhibit-startup-echo-area-message, but it is not enough to set it to t; you must set it to your username. See the documentation for inhibit-startup-echo-area-message.
If your init file is byte-compiled, use the following form instead:
(eval '(setq inhibit-startup-echo-area-message "YOUR-USER-NAME"))
Add the below to your init file
(setq inhibit-startup-message t
inhibit-startup-echo-area-message t)