How to have find-file's prompt match Emacs shell's $PWD? - emacs

With Emacs, if the current buffer is one that's "visiting" a normal file (for example), whose full pathname is /path/to/somefile, and one runs find-file (C-x C-f), the prompt that appears in the mini-buffer is something like
Find file: /path/to/▮
...with the cursor placed as indicated above by ▮. IOW, the suggested path shown by default is always to the directory containing the file that the current buffer is visiting.
If, however, the current buffer is an Emacs shell process, and one runs find-file, then, AFAICT, the path shown in the prompt remains fixed at the value of $PWD when the shell process was started, irrespective of the current value of $PWD:
Find file: /pwd/at/startup/▮
This behavior is not so useful, because the $PWD at startup often becomes irrelevant later on. It would be really nice if the directory shown in find-file's prompt were instead the shell process's current $PWD.
Is there a simple way to modify find-file to behave this way whenever the current buffer is a shell process?

You want "shell directory tracking". E.g. check dirtrack-mode or shell-dirtrack-mode.
shell-dirtrack-mode tries to parse "cd" commands, (event_jr: which in my experience does not work consistently). dirtrack-mode uses the prompt regexp, which works very well.

There are a number of ways to manage this. As Stefan notes, there are a couple of built in packages that manage it.
My preferred way is to alter your prompt (when in Emacs) to have the $PWD embedded in it, and then Emacs strips it out and uses it. This has the benefit of always being up to date. I've found that dirtrack-mode sometimes gets out of sync.
Check out my solution here, which is a modification of a similar implementation on the Emacs Wiki.

Related

Shell script mode automatically at each emacs start

Each time when I edit bash script I type a command M-x shell-script-mode. And then I get nice shell code higlighting. How to get it automatically each time I start emacs so I do not have to type the command. When I added (shell-script-mode) to init.el it did not help.
You can set the default major mode to be whatever you want by adding
(setq-default major-mode 'shell-script-mode)
to your init file. That will ensure that any newly created buffer will be in shell-script-mode unless its mode is specified otherwise (e.g. through auto-mode-alist). Whether it's a good idea or not, I don't know: I probably would not want that to be my default setting - but to each her/his own.
One of the simplest ways to have Emacs set the desired mode for a buffer editing a file is to include a special comment in the first line of that file, e.g. for a shell script your first line might be:
# -*-sh-*-
For scripts it is also common, or and often even required, to have an interpreter file comment on the very first line of the file, which of course would preclude having an Emacs mode comment, so Emacs also looks for interpreter file comments and associates those with a major mode, so the first line of your shell script might be:
#!/bin/sh
There are a number of other ways to tell Emacs how to set the buffer mode when visiting a file. See, for example, Emacs Manual: Choosing File Modes

What "local rules" have been set in Emacs?

I have been trying to solve a sh-mode indentation issue in Emacs (I get a double indent after a then) and found that I can set the indentation by hand and then run C-c > to automatically configure the indentation.
However, this configuration only applies to my current session and I can't seem to find any variables that have been modified as a result of the auto-configuration. (It worked though: the indentation is consistently what I want.) When it has run, it says "Local rules set" but doesn't tell me what local rules have been set.
And of course, when I restart Emacs, the local configuration is lost and I have to run the auto-config command again.
I've found a number of questions on this site about listing variables and their values, but I can't find out how to list ones that have only been changed for the current session. I even used this post to dump variables before and after running the C-c > command and comparing the output; nothing obviously different.
So I suppose my question is twofold:
If session variables are what's meant by "local rules" then how do I find out which ones were set?
If "local rules" means something else, then what is that and how can I somehow transfer those changes to my ~/.emacs file?
I think the result depends on your version of Emacs, but with a recent version, this is handled by SMIE, so the personal settings are kept in the smie-config variable.
The local settings in use in the current buffer are kept in an internal variable (smie-config--buffer-local). You can move them over to smie-config and save them into your ~/.emacs by calling smie-config-save.
One more detail: smie-config-save doesn't in itself save the settings to ~/.emacs. It only transfers them from the transient buffer-local variable smie-config--buffer-local to the global smie-config variable which is under the control of Customize and can have be saved to your ~/.emacs via something like M-x customize-save-customized. This should arguably be improved in smie-config-save. I suggest M-x report-emacs-bug to ask for this improvement.
This is more of an incomplete workaround than an answer, but it's a start!
The idea is to trigger sh-learn-buffer-indent as soon as a shell script is loaded. Add the following to your ~/.emacs file:
(add-hook 'sh-set-shell-hook 'sh-learn-buffer-indent)
A couple of important notes:
It assumes that your shell script is already correctly formatted when it's loaded.
It can be slow for large files.
(This is apparently mentioned in sh-script.el and was brought to my attention by Noam Postavsky via the bug report I filed.)

telling Icicles to ignore matches when creating a file

I use Icicles for auto-completion when for example finding a file in emacs. However sometimes I need to create a file with a particular name filename.tex in a directory and the autcomplete automatically finds a file with a similar name filanem_another.tex in another directory (I'm guessing from history).
This is annoying as it prevents making new files using C-x C-f and instead finds a similar file.
How can I ignore Icicles's suggestions?
Please try to provide a step-by-step recipe of what you do. So far, I don't recognize the behavior you describe. What do you mean by "autocompletion" and "Icicles's suggestions", for example?
Also mention whether you have any
Icicles customizations. Best is a recipe that starts from emacs -Q (no init file), saying exactly what to do to reproduce the problem. And please mention your emacs-version.
By default, in Icicle mode C-x C-f is bound to icicle-file. You should be able to enter any file name you like at the prompt; you need not choose any of the completion candidates, and you need not even complete (TAB or S-TAB). (And completion does not complete against the history.) IOW, in these respects C-x C-f should behave the same as in vanilla Emacs.
[To those tempted to complain that this is a comment and not a real answer: I intend to answer the question here, when I get some more info about it.]

Emacs Comint History: Search Rather Than Navigate One By One

I would like to, after switching to the buffer where I usually run commands, navigate the history by searching it, rather than navigate one-command-at-a-time at the end of the buffer (e.g. C-p).
Basically I would like to "Reverse I-search" the command history at the end of the buffer, rather than search the buffer.
Did anyone code a working solution? Note that I noticed there is a Command History buffer available, but here it is just a bunch of text and it is not grouped well enough I think to use.
As in a terminal, you can use M-r to search backward. It works in comint-mode, but it also work elsewhere, like in M-x (M-xM-rpatternRET).
Yes, with Icicles.
In Icicle mode, command icicle-comint-search is bound to C-c ` in shell buffers. It gives you the behavior you are looking for. It is described here.
It uses only stuff that is in the currently visible history as candidates, however. If you want to access stuff from your history from previous sessions then use command comint-input-ring, bound to C-c TAB, instead. (This is explained in the same doc.)

Emacs: Can't activate input method

My default input method has always been configured this way:
(setq default-input-method "russian-computer")
Something broke, and all I get is this message (on top of the execution stack, if I'm interpreting it right) as soon as I press C-\:
activate-input-method: Can't activate input method `russian-computer'
It used to work without a hitch. There has been no intervention that I'm aware of; I'd like to get some clues.
Emacs loads the input-methods it knows from a directory called "leim" (Library of Emacs Input Methods). The location of the code to load should be something like
/usr/share/emacs/<version>/leim/leim-list.el
(For the exact location, search for an entry containing the word "leim" in your load-path variable by typing C-h v load-path.)
If you inspect that file, you should see an entry
(register-input-method
"russian-computer" "Russian" 'quail-use-package
"RU" "ЙЦУКЕН Russian computer layout"
"quail/cyrillic")
which refers to a subdirectory of the "leim" directory called "quail".
Make sure that both the file leim-list.el and the sub-directory quail exist on your system. The quail directory is distributed separately from the Emacs source code, so if you installed from source, make sure you included the leim-list package. See here (scroll to bottom):
http://ftp.gnu.org/pub/gnu/emacs/
If the leim directory is empty, you might consider re-installing emacs.