Emacs functions to read user input - emacs

I need functions to ask for user input both in: 1) Graphical and 2) non-Graphical environment in emacs.
found that I can do something like this for the non-graphical case:
(defun query-friends-phone (name)
"…"
(interactive "sEnter friend's name: ")
(message "Name: %s" name)
)
I need an input box for the Graphical case. Something like an input box to ask for something like other languages. Does Emacs have such a thing?
(to avoid confusion something like this image: http://eleganceit.com/blog/wp-content/uploads/input-form.png, this is just the idea and I know it won't be like this!)

Emacs stays away from that approach. All the text is made to be entered in the main frame. You can have dialog boxes for questions that can be answered by clicking with a mouse (see http://www.gnu.org/software/emacs/manual/html_node/elisp/Frames.html#Frames).
The GPG interface in emacs is using the approach you are suggesting for security purposes only. Having a separate window that captures all the keys no matter what makes sure that you do not accidentaly type your password in a file and save it to disk.

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).

Buffer menu to select a set of filenames in Emacs

I have a directory "a" with a set of templates, for instance
$ ls a
b bcc cc ccdd
I would like to implement a keyboard shortcut in Emacs that will show a buffer with the template names, similar to dired or buffer-menu and then be able to select a template name by using arrow keys or mouse. Then insert the selected template into the current buffer at point.
How can this be done?
To augment Chris' answer with a little code, here is a small wrapper around ido-insert-file:
(require 'ido)
(defvar so/template-directory "/tmp/templates"
"Directory where template files are stored")
(defun so/insert-template ()
(interactive)
(let ((default-directory so/template-directory))
(ido-insert-file)))
This allows you to run (or bind a key to) so/insert-template no matter what directory you are currently in. Obviously set so/template-directory to your preferred directory.
insert-file, bound to C-x i by default, can insert a file into your buffer at point, but it doesn't give you a nice menu. Both helm and ido enhance this behaviour.
helm does not come with Emacs, but it can be installed via MELPA. When helm-mode is active, insert-file uses Helm's narrowing features. Once you're in the a directory, the up and down keys may be used to select a file, and Enter will insert it.
ido is shipped with Emacs. When ido-mode is active, C-x i is rebound to ido-insert-file. Once you're in the a directory, the left and right keys may be used to select a file, and Enter will insert it.
Both tools are excellent, both can be used in many other situations, and both offer effective filtering and navigation. Try one or both and use whichever you prefer.
Everything #Chris said about Helm and Ido is true also for Icicles, and with better "narrowing" features and on-the-fly sorting in different orders.
There is nothing extra to do --- just load Icicles and turn on Icicle minor mode. Whenever you use standard command insert-file (bound to C-x i) you get the behavior you requested for free. This behavior is in fact available for all completion in Emacs. In Icicle mode, standard commands become menus you can use the arrow keys on, etc.
In addition, your question title asks to be able to "select a set" of files. You can do that easily in Icicles, but not otherwise. IOW, selection is also multi-selection.
(However, I suspect that your question is mistitled, since the text describes something different, and I doubt that you want to insert a set of files. You probably meant that you want to select one file name from a set of file names. Consider retitling the question, if so.)

Add content to Emacs minibuffer as the user types in it?

I'm probably going down the wrong path here, so let me know if I am. I'm trying to build a similar user interface to that which Vim's ctrlp and other plugins use, whereby the user is given a prompt, and as they type, results are shown above the minibuffer prompt line.
I've gotten the minibuffer command handling part working fine with minibuffer-with-setup-hook and a local post-command-hook (easy) and can get the results I want to display (verified by just (message)ing them for now).
If I want to show, say, 10 lines of results above that minibuffer prompt line, should I be somehow prepending text to the minibuffer, or using a separate buffer that I'll close once the command finishes? Any pointers to parts of the manual I should be reading to be on the right track with this?
The "minibuffer" is a normal buffer, so you can modify it by inserting/deleting text into it in the normal way. This said, adding text "before" means basically modifying the prompt which might lead to problems down the line. You'll probably be much better off doing something like
(let ((ol (make-overlay (point-min) (point-min))))
(overlay-put ol 'before-string (format "%s\n" myresults)))
I started to write this as a comment, but it got a bit too long ...
TBH, I feel there is room for a lighter weight version of helm. But the reality is helm is good enough, and someone else has already written it.
Neither I nor anyone else (so far) is motivated enough to rewrite it. What you describe as "God" aspect of it is indeed unappealing. But it is possible to load it (huge as it is, with modern computers, it really isn't an issue), and change settings so it is minimalistic.
ido is a simpler alternative, but the style of UI is not exactly how you described.

minibuffer input: word completion with custom dictionary

I am trying to accept user input for a command-line utility in emacs. I've got a handful of words that I can use in this command-line (something like the possible target list of a make invocation), and I want to be able to auto-complete words that I know about, allow user to input more than one entry in my dictionary, and also allow the user to write things not in my dictionary. Some library that allows word completion in minibuffer using a custom dictionary would be just the thing.
I do not require a full solution, but a few pointers on where to start looking would be much appreciated. Also, I'd rather avoid using intrusive libraries such as icicles or ido if at all possible - I don't want the users of this package to be limited in how they configure the rest of their setup.
My best solution so far is to use completing-read multiple times for each target until the user enters the empty string.
Solution
event_jr's answer below did the trick. The final code I've used looks like:
(require 'crm)
(let ((crm-separator " ")
(crm-local-completion-map (copy-keymap crm-local-completion-map)))
(define-key crm-local-completion-map " " 'self-insert-command)
(completing-read-multiple "prompt: " '("foo" "foobar" "baz"))))
How about this:
(completing-read-multiple ": " '("foo" "foo2" "foobar"))

The equivalent of a GUI "Quick Open" in Emacs on CLI?

The title probably doesn't describe this question well enough. Many GUI-based editors allow you to press a key combination and then start typing a file name (anywhere, in any directory) and as you're typing, a list is filtered down of all the files matching that pattern, in realtime. If you hit enter, the currently highlighted file will be opened, or if you hit the UP or DOWN arrows you can change the selection. For example, in TextMate and Sublime Text 2 (on OS X) this is achieved by hitting CMD+T.
Now I know about find-dired and find-grep in Emacs, but is there anything else available that's a little more instant? The annoying thing with dired is that you have to hit enter and perform the search in order to see if the filename was correct. This is ok if you know the filename, but not so good when you're going based on educated guesses. It seems like something emacs could be suited to. Set the directory to index (just once) then when searching filter the index using a Radix tree search or some such, using a split window to show files and responding to the UP and DOWN arrows to adjust the selection.
Anything out there? :)
ido-mode is perfect for this. It has countless useful applications, including fuzzy filename matching as you demonstrated in your picture.
Some things to get started:
What You Can Learn From ido-mode
EmacsWiki article
Introduction to Ido Mode
Anything.el does this with files and it can do much more. Here's a good introduction on how to use it.
ido-mode is great on its own, but you may find the likes of Find File In Project a better option for matching filename patterns anywhere within a directory tree.
M-x find-file-in-project RET chooses the root of the directory tree automatically (based on the directory-local variables file).
I'm just now noticing that the M-x ffip-find-file-in-dirtree RET which I also have available (and which works on arbitrary specified directory trees, irrespective of project files) is actually provided by the ffip.el included with nxhtml.
The latter also looks like it supports ido when it's enabled. (Actually, there's a bug, but change (if (memq ido-mode '(file 'both)) to (if (memq ido-mode (list 'file 'both)) in (defun ffip-find-file-in-project (file) ...)
There will doubtless be a variety of similar options available.
peepopen also provides this functionality
I found it where I didn't want to :( ...
A plugin, called "Control-P" for vim - provides the exact same functionality the author of the question is looking for.
For example - on this video - http://www.youtube.com/watch?v=YhqsjUUHj6g - go to 26:55, and there you'll find a quick and very clean example of it. So, with one command you point to the doc-root, and after that - you're good to go :) ...
Unfortunately, none of these work the same way :D ...
I've used "Sublime text 2" for a long time TOO ... and yesterday decided to try using "emacs" ("vim" is very uncomfortable). So, I wanted this functionality:
You point to the root folder of a project.
And voila - you have "quick-find" functionality.
But ... there is no such functionality :( ...
(for example - "find-file-in-project" - it requires you to set-up "a file" in the root directory of the project, so it will know that it's the root of your project ... but what if I have to work on several projects simultaneously? ... some of them - use git, some of them - SVN, and some of them - none of these ... :D ... "Sublime text" handle this situation on-the-fly ... but emacs and vim - no :( ...)
Still waiting for the answer though ...