Call a command with arguments in Emacs minibuffer - emacs

I know that M-x (execute-extended-command) allows one to call a command in Emacs by typing its name. That however doesn't allow me to call command with arguments, e.g "backward-word 5"
I am aware that C-5 M-b produces the desired result but I am looking for a general method.
Does anyone know how to do this?
Thanks,
Danny

Eval it: M-: (backward-word 5) RET.

#abo-abo provided the general answer: use M-:.
However, if you are interested not only in obtaining the side effects of the evaluation, and not only in a cursory overview of the return value, but in the full return value (regardless of its size and complexity), then vanilla Emacs M-: is not what you want.
For that, I substitute pp-eval-expression for eval-expression wrt its key bindings (including M-:), and I recommend this practice for others too:
(substitute-key-definition 'eval-expression 'pp-eval-expression global-map)
That pretty-prints the return value, and you can make it print the complete value (no ellipses: ...).
In addition, I offer a modified version of pp-eval-expression in library pp+.el. It has these advantages:
Reads with completion, using pp-read-expression-map.
Emacs-Lisp mode indentation and completion key bindings are available during input.
With a prefix arg, inserts the resulting value into the current buffer at point.
With a negative prefix arg, if the return value is a string, inserts it into the buffer without double-quotes (").
Font-locks the return value (syntax highlighting) for Emacs-Lisp mode. (And during display emacs-lisp-mode-hook and change-major-mode-hook are inhibited.)
Respects new user options pp-eval-expression-print-length,
pp-eval-expression-print-level, and standard option eval-expression-debug-on-error. The former are separate from the similar, standard options eval-expression-print-length and
eval-expression-print-level because the use cases are typically different.
If you use library Icicles then you get the same advantages as library pp+.el -- no need to load pp+.el also.

Related

Run a command after every input to minibuffer

I want to set up a sort of ISearch mode replacement in Emacs, where I can run a command after every input to the minibuffer. Is it possible?
EDIT 1:
The idea is to completely replace ISearch with my own mode. Ideally, I'd like it to have most of the functionality that ISearch has (like highlighting results as you type). To implement some, I'd need to run a command after every key is input. Is there a way to trigger a function when the minibuffer change, or should I use something that isn't the minibuffer?
EDIT 2:
To be more specific, basically I am looking to take a string from the minibuffer and highlight all matches in the buffer, much like in ISearch mode. So essentially, after each letter, symbol, or number inputted into the minibuffer, I'd like to be able to recognize that change and run some arbitrary elisp. Similar to how helm recognizes input and updates search results.
You wan to use minibuffer-with-setup-hook and in the setup hook, you'll want to either use post-command-hook or after-change-functions.
E.g.
(defun my-update-function (beg end len)
(let ((str (minibuffer-contents)))
<update-search-result>))
..(minibuffer-with-setup-hook
(lambda ()
(add-hook 'after-change-functions #'my-update-function))
...(read-string ...) ...)
See variable minibuffer-exit-hook.
In general, for this kind of question, look for a variable whose name ends in hook (or function or functions). Use commands such as apropos to find a variable (or function) whose name contains various substrings (e.g. -hook, minibuf).
Your question is, however, a bit unclear. Will you actually be using Isearch, or are you going to replace Isearch with something that uses the minibuffer? Please specify more precisely what you are doing.
FYI - Isearch does not use the minibuffer (except when you use M-e to edit the search string).
Update after your Edit 1:
Now I'm afraid your question is too broad, and risks being closed for that reason. You are essentially vaguely asking how to implement some kind of a replacement for Isearch.
If you want to do something after each command, see post-command-hook. But beware that nearly every key press is a command invocation.
Update after your Edit 2:
In that case, consider looking at what other libraries do in this regard. For example, highlight.el (e.g. hlt-highlight-symbol), highlight-symbol.el, color-moccur.el, Icicles (e.g. icicle-occur), and Helm (e.g. helm-swoop). Some of those, such as Icicles and Helm, provide incremental highlighting update as you describe. Others highlight a given name that is entered in the minibuffer (i.e., using RET, not just characters typed in the minibuffer).
But you might want to specify what you want that is different from what such libraries already do. Your question still seems overly broad, to me.
In general, things such as Isearch that do incremental updating read key presses and respond accordingly. They may do this from the minibuffer or at top level, but the point is that they generally do not require a user to enter something in the minibuffer, using RET. They respond after each key press (or each command invocation, e.g. via post-command-hook).

Slime/Emacs : Keyboard shortcut for Go to function ( Not M-.)

How do I directly navigate to a function definition in a given file in Slime/Emacs using keyboard shortcuts? I know about M-. but that is not I want. I am already in the file and know the function name. Search by text won't directly take me to the function definition as it will take me to call sites of that function as well.
For those of you who also know Eclipse, I am looking for the equivalent of using Ctrl-O to open the outline view and then as I type the method name, it will filter(elide) to the function I want, I then just press enter and it takes me there.
If there is an alternative that you use, I would be happy to try that too.
It sounds like you're looking for M-x imenu. It doesn't have a keyboard shortcut by default; I like to bind it to s-i:
(global-set-key [(super ?i)] 'imenu)
As #legoscia said, Imenu is the answer. As additional info I'll mention how Icicles can enhance your use of Imenu.
The obvious enhancement is better completion (substring, regexp,...), including narrowing choices with multiple patterns.
Unobvious is the Icicles multi-commands that are specialized for Imenu navigation, giving you, in effect, an Imenu browser. This is described here.
There are different commands to navigate to/among different kinds of Emacs Lisp definitions: commands, non-command functions, faces, keymaps of different kinds, user options, and other variables.
While navigating, you can sort the candidates that match your input, and cycle among any subset of them in the sort order.
There are "full" versions of the commands, which provide as candidates not just what matches the Imenu regexps (e.g. (defun foobar () and your current input, but the complete definitions (e.g., full function definition).
These navigation commands are also for searching. In particular, the "full" versions provide the full definitions that match your current minibuffer input as candidates. As you change your input incrementally, the full definitions are searched, narrowing the choices. You can then navigate among any of those.
You can also do it with lispy.
It's a mixture of Paredit, vi and IDE features for Elisp, Clojure,
Common Lisp and Scheme.
The feature that you want is provided by lispy-goto, bound to
g. It uses CEDET to parse the whole source directory,
allowing you to jump to a tag in all files in current directory.
There's also lispy-goto-local bound to G, that
looks for tags in just the current file.
helm completion is used for both commands so it's really fast.
Have a look at
Navigating a directory of Common Lisp code with lispy.el
for a screencast.
You can see that it's much more advanced than imenu:
it recognizes tag types such as in-package, defparameter,
defconstant, defclass etc. This can also be extended to arbitrary
tags, such as SLIME's define-pattern-substitution.
Also, lispy uses SLIME to provide inline arguments (alternative to eldoc) and
eval bindings.

In Emacs, how can I exclude '-' from the common delimiters?

When coding in elisp, I find that I'm stopping at hyphens when moving by words, and would prefer to ignore them.
What's the simplest way to do this?
M-x modify-syntax-entry RET - RET w RET should do it. Or if you prefer an elisp snippet that you can add into a hook, (modify-syntax-entry ?- "w")
The syntax table for a mode contains information on what constitutes various syntactic classes (e.g. words, spaces etc.). These are used to determine the operation of commands such as forward-word etc. Modifying it change the behaviour of these commands.
Instead of changing Emacs' notion of words, it might be preferably to navigate by s-expressions (C-M-f, C-M-b) to skip whole identifiers. That way, you keep the convenience to be able to navigate by the partial words if you want to change an identifier.
You can use interactive regex search. Pressing just C-M-s SPACE should search for any whitespace (you might need to configure search-whitespace-regexp).

Emacs: Enter commands like in gedit

in gedit it's possible to define so-called "snippets" for simpler input.
For example, there is a snippet while. This means: If you type while -> (-> stands for tab key). And gedit automatically converts it to the following (including correct indentation):
while (condition){
}
In vim (in conjunction with latex-suite) I saw the following: If you type (, vim inserts just a (. If you type ( a second time, vim automatically converts it to \left( \right).
I found abbrev-mode but this mode doesn't place the cursor properly (i.e. between parentheses or inside the while loop).
I managed to create custom emacs keybindings/macros that do just the same (without having to press the tab key), so I know it's possible.
However, is there already and package where you can define such "snippets" without much effort? Or are there even any serious reasons not to use such things?
See yasnippet. It provides snippets for most major languages, and it is easy to add new ones or modify the old ones.
Yes, yasnippet is probably the way to go. But make sure you learn the major mode you're using for your editing - when writing in LaTeX, learn auctex. Major modes can contain functionality that makes some snippets pointless, and do the same thing even better. So instead of using a begin/end-snippet in a LaTeX buffer, try C-c C-e in auctex. Etc :)
Don't forget abbrev-mode.

How can I filter compilation output only for a specific mode or buffer in Emacs?

I have a HTML page, with html-mode enabled. I call function sgml-validate to check for any markup errors. It's based on compilation-mode. I want to remove some warnings from the compilation output, so I wrote a function and hooked it to compilation-filter-hook (this variable is not documented, but compilation-filter invokes it). Everything works. My problem is that how can I ensure my filter function only gets called when I started the compilation process on a HTML page (via sgml-validate)?
I see two methods, but none of them worked:
First, I can check the value of major-mode. But it always returns compilation-mode, since that is enabled on the *compilation* buffer. (I found a filter function in the source code of grep+, and they did check the value of major-mode. I can't figure out how can it work correctly.)
The other idea was than to only hook my filter function to the HTML file's buffer, but for similar reasons it couldn't work as the output of compilation process goes to a seperate buffer.
It sounds like you can advise smgl-validate so that it performs the filtering before it performs all it's other operations. For example:
(defadvice sgml-validate (around fix-filtering command activate)
(let ((return-value ad-do-it))
(filter-function return-value))))
Meanwhile, I found that compilation-start accepts an optional argument mode, which will be the major mode for the compilation buffer. So I can create a major mode derived from compilation-mode, and define there my filter function now hooked to the proper buffer.
The only problem is now that sgml-validate does not allow me to set the mode argument on compilation-start, but that's another question.
(I don't consider this the ultimate solution, of course.)