Smart-tab in minibuffer - emacs

I've just found out that Hippie-expand works in the mini-buffer (via M=/), however I use smart-tab (via TAB) for completion (and indentation) during standard editing. Hitting TAB in the minibuffer only inserts 4 spaces - which is something likely I'd never actually want. Is there a way to do directly Hippie-expand with smart-tab in the mini-buffer? I have completion for paths, buffers and extended commands (through ido/smex), but would like to have it for everything (e.g. for replace, search, flush/keep-line etc.) that needs input.
At the moment I use simply (Emacs 23.3):
(setq tab-always-indent 'complete)
although in the past I had more elaborate smart-tab settings and it also didn't work in the minibuffer...
Thank you!

I don't think you need smart-tab at all to get tab minibuffer completion. You might try icomplete-mode instead:
(icomplete-mode +1)
Since smart-tab just indents or completes and in the minubuffer you never want to indent I guess it's basically the same.

Icicles gives you (TAB) completion pretty much whenever the minibuffer is used, the only exceptions being when the calling code explicitly uses the minibuffer in a way that does not allow completion.
(But when constructing Lisp sexps (e.g. with M-:), you can use M-TAB to complete individual symbols.)

Related

Emacs - suppress Completion Buffer

In Emacs, I don't want the *Completions* buffer to pop up, even when I press tab to autocomplete in the minibuffer and there are multiple results.
It's distracting and jarring.
How can I do this?
Even better, I would like an alternative that isn't distracting or jarring -- such as requiring one tab for autocomplete if available, but requiring two tabs to open a Completions buffer. This way, I don't get the Completions buffer when I'm expecting an autocomplete. This is what the OS X terminal does to show tab completion possibilities.
I think the cause is the minibuffer-completion-help command, which is run automatically, described here: https://www.gnu.org/software/emacs/manual/html_node/elisp/Completion-Commands.html
I use ido and smex, but the problem occurs in a vanilla Emacs too.
EDIT: I found a hack to fix this. Using M-x find-function, I found and copied the function definition of minibuffer-completion-help into my .emacs.d/init.el file. Then, I renamed the version I copied my-minibuffer-completion-help and changed (with-displayed-buffer-window *Completions* to '(with-displayed-buffer-window *Completions* (putting a quote in front so it is just interpreted as a string. Finally, I overrode the call to minibuffer-completion-help by putting
(advice-add 'minibuffer-completion-help
:override #'my-minibuffer-completion-help)
after the my-minibuffer-completion-help function in my .emacs.d/init.el file. There must be a better way.
EDIT 2: quoting out (message "Making completion list...") in my-minibuffer-completion-help has the added benefit of getting rid of the flicker in autocomplete that is caused by flashing another message during autocomplete. Is it possible to do this another way?
I believe you just want to set completion-auto-help to nil:
(setq completion-auto-help nil)
I believe what you're looking for is either temporarily modifying display-buffer-alist, probably setting it to use display-buffer-no-window for *Completions* or M-x customize-group completion and setting Completion Show Help to nil

How do I determine why emacs indented a certain amount?

In Emacs I'm editing some source code, and I hit <tab>. Emacs indents the line to n spaces. I'd like to change the amount that indents for that kind of line. How do I figure out what rule emacs applied to indent that line by n spaces?
I want to change n, but I need to figure out which of the many indentation-related variables Emacs just used.
A generic answer is difficult. Some modes will make this more apparent than others, but in the general case (as they are free to implement indentation however they wish) I don't think you'll get away from needing to read some elisp.
Starting with the binding for TAB will work, but might be slightly time-consuming depending on how many layers of indirection are involved.
If you know that the major mode in question implements its own indentation, then one (non-rigorous, but fast) approach that you could try to help track down the functions being called is to use ELP, the built in elisp profiler. elp-instrument-package will instrument for profiling all functions with names matching the prefix string argument you specify. Therefore you might do something like the following in a PHP file (noting that php-mode tells you that it is derived from c-mode)
M-x elp-instrument-package RET php- RET
M-x elp-instrument-package RET c- RET
M-x elp-instrument-package RET indent RET
Now type TAB in your source code, and run M-x elp-results to see which of those instrumented functions were called.
At this point you're on your own -- look for the likely suspects, and see what the code is doing -- but it can be a handy way to filter the search.
Once you've finished, use M-x elp-restore-all to prevent any further profiling.
If you're using a mode based on cc-mode (e.g. c-mode, c++-mode, java-mode, etc.), you can hit C-c C-s and it'll tell you what syntactic category the line is. If you want to change it, hit C-c C-o and you'll be guided through the process. Check out the cc-mode docs on customization for more details: https://www.gnu.org/s/emacs/manual/html_node/ccmode/Customizing-Indentation.html
If you happen to enjoy getting your hands really dirty, there's always the elisp debugger to tell you just what Emacs is up to.
If you hit C-h k TAB you'll find the function that Emacs is running (e.g. indent-for-tab-command) then you can do M-x debug-on-entry RET indent-for-tab-command RET. Now whenever you hit TAB you'll pop up a debugger and can watch the execution step by step.
Depending on your taste for debugging, it's either a maddening or enlightening experience. Either way, don't forget to M-x cancel-debug-on-entry when you're done.

Invoke single normal-mode command with Ctrl-o in Vimpulse?

The usual behavior in vim is for C-o in insert-mode to allow the user to use one normal-mode command and then return to insert-mode.
Following the suggestions in this comment, I've set
(vimpulse-imap "\C-o" 'viper-escape-to-vi)
But when I press C-o in insert-mode, the characters "^O" are inserted in the buffer and I am unable to switch to normal-mode for one command as I expect. I understand viper-escape-to-vi is bound to C-z by default but I am also unable to switch to normal-mode using this key combination either ("^Z" is printed in the buffer instead). What am I doing wrong?
Also, second question: I would like for C-z to not be bound by viper-mode (or vimpulse) as I use it for elscreen. How can I tell it to leave C-z alone?
Edit: Found this bit of code in vimpulse-misc-keybindings.el:
;; temporarily escape to vi state
(define-key viper-insert-basic-map "\C-o" 'viper-escape-to-vi)
So should be defined already? Not sure why it doesn't work.
Ad 1) It works for me, maybe something went wrong with your config?
But the output is strange: The only way to get it is a quoted-insert (or similar).
viper-escape-to-vi in insert state is bound to viper-toggle-key (but well that defaults to C-z), so see below. Again your output is strange.
Ad 2) You have to set viper-toggle-key to a sexp before viper is loaded, alternatively customize it.
For the former (using the Pause key for it)
(setq viper-toggle-key [pause])
(require 'viper)
You should try evil, being the successor to vimpulse. It's coming along well and gets rid of viper.
Setting the toggle key there (to pause again) is a (evil-set-toggle-key "<pause>") away, it's a kbd sequence not a sexp.
C-o works fine, too.

Changing highlight time when searching

When I perform search in emacs, the available matches are highlighted for around 1 second.
How can I change this?
Is there any customize-group for setting search highlight time options? (in isearch there is no such entry).
I'd say the customize group is lazy-highlight
(which is one of the child groups listed at the bottom of the isearch group's buffer, incidentally.)
edit: Although it sounds like you're saying that the highlighting disappears again after a second with no intervention on your part, which would seem like a conflict with something else, as that's not the default behaviour. (You can run emacs -Q to test the defaults.)
Assuming that lazy-highlight-cleanup is being called unexpectedly, you could call debug-on-entry on that function to check the stack trace and see where that call is coming from. You'll have to continue past some expected calls when initiating the search. Use cancel-debug-on-entry when you're done.
See also:
Standard debugger commands:
M-: (info "(elisp) Debugger Commands") RET
Main manual entry for debugging elisp:
M-: (info "(elisp) Debugging") RET
Try setting lazy-highlight-interval variable, for example:
(setq lazy-highlight-interval 0)

Does Emacs has word and line completion (like Vim's insert mode completion)?

Vim completes words and lines with CTRL-X P and CTRL-L. There's a Emacs plugin called Company mode but this plugin interfere and cause conflicts with lots of things within Emacs (with global linum and yasnippets). I know that I can complete words with CTRL-/ in Emacs. But it is possible to take previously written lines to complete code?
Maybe you're looking for hippie-expand? From that web page (as of this writing, anyway):
HippieExpand looks at the word before
point and tries to expand it in
various ways including expanding from
a fixed list (like expand-abbrev),
expanding from matching text found in
a buffer (like dabbrev-expand) or
expanding in ways defined by your own
functions. Which of these it tries and
in what order is controlled by a
configurable list of functions.
For a comprehensive list of completion options visit the emacs wiki page on completion.
There are a gazillion ways to do completion in Emacs. Some are mode specific, some inline, some configurable and what not. Here is a list of modes that might help you.
Use numberic argument to complete by line, say M-5 M-/ will complete by line, while M-/ alone still complete the normal way.
hippe-expend function has a very useful feature which is :
With a positive numeric argument, jumps directly to the ARG next function in this list. With a negative argument or just C-u, undoes the expansion.
Customize the expansion functions in hippie-expand-try-functions-list and put the function try-expand-line as 5th list element, then you could use M-5 M-/ to complete by line.
This tip is very handy and useful and I highly recommend it.
Also worth noting: if your window manager does not steal Alt-tab, emacs will auto-complete with Alt-tab (I set up my window manager to user the "windows key" instead of alt for this very reason).
If you are using evil, this is the most vim-like solution I use:
(defun my-expand-lines ()
(interactive)
(let ((hippie-expand-try-functions-list
'(try-expand-line-all-buffers)))
(call-interactively 'hippie-expand)))
(define-key evil-insert-state-map (kbd "C-x C-l") 'my-expand-lines)
This way you can use our old friend C-x C-l in insert mode for whole line all buffers completion.
Thanks #ymln for the suggestion of using try-expand-line-all-buffers.