Add hook to default mode when using emacs -q -l - emacs

I have been loading emacs with emacs -q -l "init.el" quite a bit and was trying to enable auto-complete in my scratch buffer. I was struggling to figure out why it wasn't working but realized it must have to do with the order of operations when emacs is loaded like this - a quick test with the following init file:
(package-initialize)
(require 'auto-complete)
(ac-config-default)
(add-hook 'lisp-interaction-mode-hook
'(lambda ()
(auto-complete-mode t)))
shows completion working as I would like when calling emacs as normal from the command line. But if I call it as emacs -q -l init.el there is no dropdown completion.
Question: How can I get this hook to run?
I've tried variations on after-init-hook but none seem to work.

The following analysis is based on startup.el of the master branch: https://github.com/emacs-mirror/emacs/blob/master/lisp/startup.el
As I understand the question, it seeks an answer as to when the command line option -l aka --load FILE is run as compared to when the *scratch* buffer is initialized with the initial-major-mode that by default is lisp-interaction-mode.
Based on the sequence of events defined within startup.el, the -l or --load options are taken into consideration at line 2381 of the function command-line-1.
The function command-line-1 runs at line 1366 of startup.el, which is subsequent to the after-init-hook at line 1344 and subsequent to the *scratch* buffer being initialized with the initial-major-mode at line 1350.
To the extent that the original poster would like to rely upon loading a file manually using the -l or --load option, then functions being assigned to the lisp-interaction-mode-hook will not be seen at line 1350 because they do not exist until command-line-1 runs at line 1366. One option the original poster may wish to consider would be the following: (with-current-buffer "*scratch*" (lisp-interaction-mode)) after the auto-complete-mode has been added to the lisp-interaction-mode-hook.

If starting Emacs from the command line, you could invoke the function to run near the end of the loading sequence, as follows:
emacs -f lisp-interaction-mode
See Action Arguments in the manual:
‘-f function’
‘--funcall=function’
Call Lisp function function. If it is an interactive function (a command), it reads the arguments interactively just as if you had called the same function with a key sequence. Otherwise, it calls the function with no arguments.

Related

emacs init file on MS Windows

There's a huge literature on the topic, but, nevertheless, I cannot get this done.
My ultimate goal is to work with fstar on Microsoft Windows.
C-x C-f resolves ~ as C:/Users/myname which is in line with my HOME environment variable in the Environmental variables Windows section.
(expand-file-name user-emacs-directory), as described here yields, C:/Users/myname/.emacs.d/
In C:/Users/myname/.emacs.d/ I have placed .emacs.el and init.el with the suggested script:
(require 'package) (add-to-list 'package-archives '("melpa" .
"http://melpa.org/packages/") t) (package-initialize)
However M-x returns undefined, no matter if I start Emacs with or without the -q flag (see here). My "Messages" buffer is empty".
A couple of things you could try:
Check the value of the variable user-init-file (use C-h v). That should tell you if Emacs loads the file you want it to load. If you started Emacs with the -q option, the value of this variable should be nil.
The error M-x is undefined can be caused by rebinding the Escape key. (That's because pressing a key while holding down the "Meta" key is equivalent to first pressing Escape and then the key in question.) Is there something in the init file that might cause this to happen?
Try starting Emacs with -Q instead of -q. This makes Emacs skip "site-wide" init files. I can't really think of a reason why your system would have any of those, but it might be worth ruling this out.
You could edit your question and include your entire init file (surround it with ``` on a line by itself), so we could have a look.

How to start in the middle of command input

How can I have emacs start and be in the middle of a command input? Particularly, I want emacs to start in the middle of a command input find-file with a message in the small buffer saying:
Find file: ~/
and the cursor at the last character of it so that I can continue typing the remaining path to open the file I want.
You can execute one of the following commands on the command prompt or make a shell script containing it appropriately:
$ emacs -f find-file # if you want to start Emacs in the current direcoty
$ (cd ~; emacs -f find-file) # if you want to start Emacs in your home diretory
From the emacs(1) man page:
-f function, --funcall function
Excute the lisp function function
I have to admit that my lisp is a bit rusty, but this works for me. Drop it in your ~/.emacs file (or whatever init file you are using):
(add-hook 'emacs-startup-hook
(lambda ()
(if (= (length command-line-args) 1)
(call-interactively 'find-file))))
If you call emacs with no arguments, like this:
sawa#localhost:~$ emacs
It will invoke find-file for you. If, on the other hand, you invoke emacs with an argument, such as a filename, like this:
sawa#localhost:~$ emacs somefile.txt
It will default to just visiting somefile.txt

How can the *shell command output* buffer be kept in the background?

How can I tell emacs not to pop up the *Shell Command Output* buffer when calling a shell command like this?
(shell-command MY_COMMAND)
Currently emacs splits the current window into two, showing the (mostly irrelevant) output buffer. To me it would be completely sufficient if I could look it up later if I feel like it.
Maybe using shell-command was the root of the problem. I think I found a solution with call-process which works, although there may be a more elegant way:
(call-process-shell-command
"cat ~/.emacs.d/init.el"
nil "*Shell Command Output*" t
)
shell-command takes an optional argument OUTPUT-BUFFER where you can specify the buffer to output to. If it is t (actually not a buffer-name and not nil) it will be output in the current buffer. So we wrap this into a with-temp-buffer and will never have to bother with it:
(with-temp-buffer
(shell-command "cat ~/.emacs.d/init.el" t))
In my experience, if the shell command itself produces no output, then the emacs *Shell Command Output* buffer won't pop open.
Therefore, to avoid the output buffer, silence the output of the command.
One easy way is:
add " > /dev/null 2>&1" to the end of any shell command.
(Caveat: I'm unsure if /dev/null exists on 100% of platforms where one can run emacs, but on every Linux distro it should be fine.)
If the call to elisp function shell-command is in an elisp script, then you could change this:
(shell-command cmd)
to this:
(shell-command (concat cmd " > /dev/null 2>&1"))
If you occasionally do want to monitor the output, then you could create one wrapper function that suppresses the output via /dev/null, and one wrapper function with no suppression, and toggle between them as you wish.
The above advice was tested on: GNU Emacs 24.5.1 (x86_64-pc-linux-gnu, GTK+ Version 3.18.9) of 2017-09-20 on lcy01-07, modified by Debian
This utility function might help. It returns the actual value of the shell command
(defun shell-command-as-string (cmd)
(with-temp-buffer
(shell-command-on-region (point-min) (point-max)
cmd t)
(buffer-string)))
What's even better, is to use
(shell-command (concat cmd " 1>&2") t t)
This way, the output is saved in the error buffer, should you want to look at it. But it does not pop up automatically.

Emacs compilation mode won't see bash alias

Emacs M-x compile does not see any aliases set in .bashrc. If I use M-x shell then type the alias, it is fine. I tried sourcing .bashrc from /etc/profile, from ~/.profile, ~/bash_env, anything I can think of to no avail.
I am on Emacs 23 and Ubuntu 11. I start emacs using /usr/bin/emacs %F, from a desktop button.
Emacs inherits its environment from the parent process. How are you invoking Emacs - from the command line, or some other way?
What happens if you:
M-x compile RET C-a C-k bash -i -c your_alias RET
Invoking bash as an interactive shell (-i option) should read your .bashrc aliases.
Edit: I think both M-x shell-command and M-x compile execute commands in an inferior shell via call-process. Try the following in your .emacs (or just evaluate):
(setq shell-file-name "bash")
(setq shell-command-switch "-ic")
I notice that after evaluation of the above, .bashrc aliases are picked up for use by both M-x shell-command and M-x compile, i.e
M-x compile RET your_alias RET
should then work.
My environment: Emacs 24.1 (pretest rc1), OSX 10.7.3
Keith Flower's answer works but can result in some slowdowns due to .bashrc being unnecessarily loaded in other places (presumably many many times, my computer is not exactly under-powered but emacs was almost unusable when trying to use autocomplete.el).
An alternative way is to locally modify shell-command-switch only for the functions where it is needed. This can be done using emacs' "advice" feature to create a wrapper around those functions. Here's an example that modifies compile:
;; Define + active modification to compile that locally sets
;; shell-command-switch to "-ic".
(defadvice compile (around use-bashrc activate)
"Load .bashrc in any calls to bash (e.g. so we can use aliases)"
(let ((shell-command-switch "-ic"))
ad-do-it))
You need to write similar "advice" for each function that you want to use .bashrc (e.g. I also needed to define the same advice for recompile), just copy the above and replace compile in the above with another function name.
You may like emac's bash-completion :
https://github.com/szermatt/emacs-bash-completion
You'll be able to use tab completion of your aliases in the compilation minibuffer and in shell-mode.
Enjoy !
(they speak about it here Bash autocompletion in Emacs shell-mode )
I think compilation commands are not interpreted through a shell: they are juste exec'ed by emacs (which means aliases, shell functions and other shell-specific things are not taken into account).
Try to wrap you compilation command into a shell-script which would source the correct environment.
You can do this either with a full-fledged shell-script in the form
#!/bin/bash
source "~/.bashrc"
my_command
or directly in emacs with a compilation command of the form
bash -c "source ~/.bashrc; my_command"
See Is there a way to get my emacs to recognize my bash aliases and custom functions when I run a shell command? for a fix which doesn't run all your .bashrc and doesn't create these error messages:
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell

How can I set styling and positioning of a newly created emacsclient?

I've recently switched to emacsclient for most text editing. I am trying to migrate some of my settings to the new (and slightly different) environment.
In particular, in my .emacs file I have a function that sets the window size, and prepares some themes. However code in the .emacs file is not executed on each invocation of emacsclient, so the settings do not apply to these windows. Based on the question here, I added a hook to 'server-visit-hook that called a function which executed my settings. However, the settings are not being applied when I restart the server and invoke emacsclient.
How can set the styling and positioning of new emacsclient windows? Relevant portions of my .emacs are included below:
(defun gui-mode()
(set-face-attribute 'default nil :font "Monospace-8")
(require 'theme-wombat)
(set-frame-size-according-to-resolution))
(add-hook 'server-visit-hook 'gui-mode)
(when window-system
(gui-mode))
Start emacsclient with the -e option, and use that to tell it to load your configs:
emacsclient -c -e '(load "~/.emacsclient")'
where ~/.emacsclient contains your configuration code. You probably want to define an alias or menu option so that you don't actually type that in every time you call emacsclient.
(add-to-list 'default-frame-alist '(fullscreen . fullboth))
in .emacs does the job.