How can I use M-x in minibuffer? - emacs

Say I want to create a file. Then I was asked to enter the name. I want to use the date as the name of file. I want to use M-x insert date (I define myself) . How can I do that?

Set (or bind, depending on the context) variable enable-recursive-minibuffers to non-nil (e.g. t), before the first entry into the minibuffer.
That lets you use actions, such as M-x which themselves activate the minibuffer, from within a minibuffer. The second activation takes place in a recursive minibuffer. When it ends (e.g. you hit RET after replying to the M-x prompt), things continue as they were, with the initial minibuffer still active.

Related

Create an Emacs semi-interactive macro

I would like to create a quick Emacs macro (with C-x () which takes some user input from the minibuffer each time it's executed.
The usage scenario is this:
You have a complex text to change where the regexp query substitution just doesn't cut it, and you want to create a macro which:
does some complex changes in the current line of text
position the cursor at a specific point
asks the user for some text in the minibuffer
possibly executes some other action, then exits
I have tried using
M-x eval-expression
and
(insert-text (read-from-minibuffer ">"))
but after creation, the macro obviously (in retrospect... :) ) repeats the same keystrokes and therefore doesn't give me a chance to insert some new text.
Can this be done?
Thank you for the help!
Use kbd-macro-query, normally bound to C-x q. Specifically, you should use C-u C-x q, which enters a recursive edit mode when you're defining the query and lets you enter whatever text you like to a minibuffer prompt, but note that the text you enter here will not be part of the keyboard macro. While you're defining the macro, you'll need to hit <enter> once to end the recursive edit and then hit it again to send the text to the minibuffer prompt so you can continue defining the macro. Once you finishing defining the macro, you can then execute it and it will stop at the point you called kbd-macro-query, prompt for the text, and then hitting <enter> will work as expected.

How do I access the result of eval-buffer?

Two questions:
Where does the result of eval-buffer get stored? How do I access it?
For example:
(+ 2 2)
Functions like (forward-word) are executed by eval-buffer, but do not move the cursor? Why is that?
eval-buffer is usually used for side-effects, rather than return value. For example, in your .emacs file, eval-buffer will re-load all our config settings. By default, and when used interactively, it will always return nil. If you want to get the return value of code in a buffer, this is the wrong way to do it. eval-last-sexp, bound to C-x C-e, is one way to do so. Calling it with a prefix, C-u C-x C-e will insert the return value into the current buffer.
eval-buffer preserves the value of point. So functions like forward-word will have no visible effect.

Emacs minibuffer message when typing C-x

Well. When I type some first keys of the key series, emacs write those keys in minibuffer after some interval of time. Like that: Typing C-x 4 will make C-x 4- visible in minibuffer.
The question is: can this be modified? I was thinking about making something like combining part of key-help (generated by C-h when type some keys) with this string.
Can interval for waiting this message be shorten too?
Is it subroutine?
Edited, new question
There is a message when I quit emacs with C-x C-c and have modified buffers, that ask me if I want to save them. How can I know that this message is here? I tried to look in (minibuffer-prompt) (minibuffer-contents) (buffer-substring (point-min) (point-max)), selecting (select-window (minibuffer-window)). Nothing gives me results.
Yes, the user option echo-keystrokes controls how much time elapses before the prefix key is shown in the minibuffer. From (emacs) Echo Area Customization:
User Option: echo-keystrokes
This variable determines how much time should elapse before command
characters echo. Its value must be an integer or floating point
number, which specifies the number of seconds to wait before
echoing. If the user types a prefix key (such as `C-x') and then
delays this many seconds before continuing, the prefix key is
echoed in the echo area. (Once echoing begins in a key sequence,
all subsequent characters in the same key sequence are echoed
immediately.)
If the value is zero, then command input is not echoed.
You can control the timing of this help message by setting suggest-key-bindings to a larger/smaller number.
(setq suggest-key-bindings 5) ; wait 5 seconds
There is no easy way to customize the behavior, you'd have to edit the C code for execute-extended-command, or use a replacement for it which also provides the help. One possibility for a replacement is the anything-complete library which has a replacement for execute-extended-command (note: I haven't tried it). It builds on top of the package anything, which is a different experience than the standard Emacs.
I wrote working version of what I wanted to implement.
To use, (require 'keylist), copy one or two last lines to .emacs and uncomment them.
As you can see through this code, I used this
(not cursor-in-echo-area)
(not (minibufferp))
(not (= 13 (aref (this-command-keys-vector) 0)))
to find out, if my minibuffer, or echo area is in use.
The difference between them is that minibuffer is used to read, and echo area is used to message something.
When you type C-x C-c cursor is placed in echo area, and value of cursor-in-echo-area is changed.
The last string (= 13 (aref (this-command-keys-vector) 0)) is the most funny. It is used to catch things like query-replace. When making raplacements, (this-command-keys-vector) shows that RET is the first key pressed, then keys of your choise(y,n). As far as I don't have key-sequences starting with RET, i am okay with this.

What is the key binding to enter input string in C-s or C-r in Emacs?

In finding string in the current buffer, I know that we can use C-s or C-r, then you'll have the minibuffer prompt of what the string to be search. I know that we can use M-p or M-n to go to that prompt, but unfortunately emacs will display the previous search query.
I don't like using backspace to delete it, is there any key in which the prompt can be cleared from previous searches?
Thanks
Are you saying you want to change your search term while you are already in the process of searching?
A couple of simple options are:
Break out of the search first. I usually just do something which moves point, such as C-a. Then when you C-s again, the prompt will be blank (unless you type it twice, in which case it will search for the previous pattern again).
RET is isearch-exit, but that has a different effect with no pattern, and I prefer the consistency. You could also use C-g (isearch-abort), but you may need to type that repeatedly, depending on what happened up to that point.
ESCESCESC runs isearch-cancel which will reliably "Terminate the search and go back to the starting point", which may sometimes be preferable to C-a which will leave you on the line where you typed it.
You can edit the pattern on the fly with M-e, and then delete the whole thing with normal editing commands. After editing, type RET to continue searching for your newly-edited pattern.
Type C-sC-hC-h for the built-in help.
Try binding a key to the M-x search-forward command.
(global-set-key (kbd "C-s") 'search-forward)
This will automatically focus on the minibuffer and clear previous input.

Emacs Lisp: How to, an interactive buffer with REPL?

Imagine the dunnet.el game, the eliza doctor or the IELM mode...; This is, evaluating answers and prompting new questions on the main buffer, then making some actions according to their elisp rules.
I would to create something like an interactive assistant, BUT NOT prompting from minibuffer as (interactive) does (not doing M-x)... I need a kind of REPL loop. So the question i how to get interaction on the working buffer (the main screen), like any text conversational/adventure, but for call my defun'ed functions on this way.
So i need a basic elisp skeleton, to make a custom REPL. The target is to make an assistant with a natural language, then parsing the anwsers, and making some actions according the rules.
Thank you for your time,
Steve,
Check out ielm, which is an elisp repl included with emacs.
Depending on what can go on in your buffer, can you just give it a major mode with an appropriate re-binding of return? (This answer vaguely inspired by Lisp-Interaction-Mode's C-j)
You should bind all keys that use for moving in buffer like C-p C-n, Up Down Arrows/ page up/down just bind them to (lambda ()). change function for C-b and C-f (Left Right Arrows) if cursor position is in beginning or end of the line. And bind Return key to function that read current line from end to optional prompt (you can use Regex to remove the prompt) do whatever you want with that string, and then go to the next line in the buffer and print your result.