Flyspell without a mouse - emacs

How can I add a word to the dictionary in flyspell without using a mouse? I know, I can use the regular ispell.el binding M-$ and and go through the menu for that, however, this needs multiple keystrokes.
Is it possible to bind the "Save Word" functionality from the flyspell pop-up window to to a single key?

When you get to choose options - press i that should add it into your local dictionary (well, does for me anyway :)).
Thorough examination of ispell.el shows that there's no special function to do it, but you could have one of you own:
(defun save-ispell-word (word)
(interactive "sA word you want to add to dictionary ")
(ispell-send-string (concat "*" word "\n"))
(setq ispell-pdict-modified-p '(t)))
But it will work only in ispell minor mode.

Related

Search selected text and show all occurrence of it in current (all other buffers) in emacs?

So in notepad++ I can select text, hit C-f, then, if I need to look for occurrence of selection in all opened files, i hit M-o and get nice clickable list with navigating to occurrence option. Or if I need list only for current file I point mouse to “Find all in current document” button do a click and get same nice clickable list only for currently active file. So is it possible to do exact thing in emac?
You can implement that functionality with the following lisp function:
(defun occur-selection ()
(interactive)
(when (region-active-p)
(let (deactivate-mark)
(occur (regexp-quote (buffer-substring (region-beginning) (region-end)))))))
If you put that code in your ~/.emacs file together with the follwing line:
(global-set-key [(meta o)] 'occur-selection)
you should be able to select some text, hit M-o and get a list of all occurrences of the selected text displayed in a separate buffer.
User M-g n and M-g p do cycle through the matching lines in the original buffer.
Note, however, that multiple occurrences in a single line are not distinguished.
By default, Emacs has M-x occur which work similar but slightly differently. It allows you to specify a regular expression, all matches of which in the current buffer will be displayed and hyperlinked.
If your focus is more on navigation than on highlighting all matches of a search term, there might be an external alternative that could help you.
Emacs' original philosophy is not built around user interface metaphors such as clicking with a mouse, it comes from a keyboard only background. If you're interested in this approach, you might want to have a look at the Avy package for Emacs. It lets you quickly jump to one of multiple occurrences of a word.
Check out the excellent Emacs Rocks episode "Jumping Around" to see a precursor of Avy (called ace-jump-mode) in action: http://emacsrocks.com/e10.html
You can do the same thing with the helm package.
Emacs will search the word the cursor/"point" is on (you
don't need to highlight it).
To make helm search in all open files/buffers, use:
M-x helm-multi-swoop-all
To make helm search only in file/buffer you're currently in, use:
M-x helm-swoop
Press the ENTER key to drop into the selected file at the
selected line.
To bind these functions to the same key-comboes, you'd need
this in your .emacs:
(global-set-key (kbd "M-o") 'helm-multi-swoop-all)
(global-set-key (kbd "C-f") 'helm-swoop)
NB
Helm is hosted in the MELPA repository.
HTH,
Michael

How do I get a list of all the currently available commands and hotkeys?

To help me learn to work with various emacs modes, I would like to have a second monitor with a little HTML page that is used for showing me what sorts of things I can type or key-chord on whatever I'm currently looking at in emacs.
So how can I get a list of all the commands or key-chords available to me in my current mode?
Someone else will no doubt tell you how to get a cheatsheet such as you request (well, here is info about that too).
But if you want something that tells you dynamically what keys are available in the current context, no matter what it is, then this is what I have to offer:
C-h m tells you about the current (major) mode. C-h b tells you about currently available keys.
The Icicles feature key completion gives you access to all of the currently available key sequences, via key S-TAB. If you use a prefix key first, then S-TAB, then you see all the completions of that prefix key. You can move up and down the key hierarchy, including even menu items, to see all possible keys. You can use C-M-RET to get help (info about) any given key that is available. Here is some more about this feature of showing you all currently possible key bindings.
I would very much like to know good answer to this question myself! At present I am using this simple function to display key bindings for the current major mode in *Help on keys* buffer:
(defun describe-current-bindings (mode)
"Show key bindings for the current major mode in *Help on keys* buffer."
(interactive)
(with-current-buffer (get-buffer-create "*Help on keys*")
(erase-buffer)
(insert (documentation mode))))
And then use defadvice to call the function automatically whenever I switch buffers or windows:
(defadvice switch-to-buffer (after display-keys-buffer activate)
(describe-current-bindings major-mode))
(defadvice select-window (after display-keys-window activate)
(describe-current-bindings major-mode))
Now I can open *Help on keys* buffer in another frame and move that frame to my second monitor.
If you use other functions to switch windows (from windmove package, etc) you may need to add defadvice for them as well.
Try the pacakge help-fns+.el, there are some useful functions: describe-mode - "Display documentation of current major mode and minor modes.", describe-keymap - "Describe bindings in KEYMAP, a variable whose value is a keymap.", etc. For example,
(describe-keymap 'global-map) ;; global bindings
(describe-keymap 'emacs-lisp-mode-map) ;; major mode bindings
(describe-keymap 'smartparens-mode-map) ;; minor mode bindings

Change the format of a word in Emacs-AUCTeX without actually selecting it

One nice feature of modern word processors is that one can change the format (say, from roman to italic) of a word without actually selecting it; one just needs to place the text cursor within the word and tell the word processor (via a keyboard shortcut) to change its format. (Smart editing, I believe it is sometimes called.)
Is there a way of doing that in Emacs-AUCTeX? (The usual way to change the format---that is, insert a format command---is to select the word [mark its region] and then press the key combination for the command [e.g. C-c C-f C-i to insert \textit{}].)
The shortcut C-c C-f calls TeX-font. Then it emphasizes/italicizes/whatever,
based on the last key chord. So the solution is to advice this function:
(defvar TeX-font-current-word t)
(defadvice TeX-font (before TeX-font-word (replace what))
"If nothing is selected and `TeX-font-current-word' is not nil,
mark current word before calling `TeX-font'."
(when (and TeX-font-current-word
(not replace)
(not (region-active-p))
(not (looking-at "\\s-")))
(unless (looking-back "\\s-") (backward-word))
(mark-word)))
(ad-activate 'TeX-font)
Now, when no region is selected, TeX-font will work as if the current word
was selected. You can turn this behavior on/off by setting TeX-font-current-word to t/nil.
In case there is no solution right from the spot, Emacs offers two ways basically once the succession of commands/keys is known:
either store them into a keyboard-macro, which be might called with just one key - or put all the commands into a function, make it a command, assign a key.

How to prompt the user for a block of text in elisp?

read-from-minibuffer is a great way to prompt a user for a single line of text. How do I prompt a user for a large block of multi-line text in elisp?
This is what I'm thinking, but I don't know if it's the smoothest approach:
create a temporary buffer (via with-temporary-buffer?)
seed the buffer with some default text
display the buffer
tell the user, "edit the text as you see fit, then hit <some key sequence> to indicate that you are done" (perhaps via header-line-format)
wait for the user to hit the key sequence
collect the buffer text and put it in a variable (via buffer-string)
destroy the temporary buffer and restore the window layout as it was before
do stuff with the text
(defun my-read-mb-lines (prompt some-keyseq)
(let ((keymap (copy-keymap minibuffer-local-map)))
(define-key keymap (kbd "RET") 'newline)
(define-key keymap some-keyseq 'exit-minibuffer)
(read-from-minibuffer prompt nil keymap)))
Calling example:
(my-read-mb-lines "Insert text (C-s to submit): " (kbd "C-s"))
The 'let' block creates a local copy of the minibuffer's default keymap. The next two calls to "define-key" modify the keymap copy. Afterward, "read-from-minibuffer" passes the modified keymap for the minibuffer to use while prompting the user (instead of its default keymap, "minibuffer-local-map").
FWIW, C-j is mapped to "exit-minibuffer" by default and a simplified version can be written:
(defun my-simplified-read-mb-lines (prompt)
(let ((keymap (copy-keymap minibuffer-local-map)))
(define-key keymap (kbd "RET") 'newline)
(read-from-minibuffer prompt nil keymap)))
Calling example:
(my-simplified-read-mb-lines "Insert text (C-j to submit): ")
I suppose it depends on the exact use-case?
There are no shortage of examples of your proposed approach working very nicely (e.g. writing a VCS commit message), so there's certainly nothing wrong with that -- it's tried and true. In addition, if it really is a large (or simply not small) block of text, then I suspect that providing a normal buffer to edit in may provide the nicest experience for the user.
If you're talking about collecting multiple input fields including a multi-line field, then the widget-based approach (as suggested by wvxvw) would enable you to put everything in a single buffer, which might also be desirable.
Or you could use the mail-like approach of using a single buffer for multiple fields, and then parse the results afterwards.
First of all Emacs 23.4 is very old. You should upgrade.
The work-flow you describe is what org-mode uses to edit source blocks.
Org-mode is included in Emacs 24.
See the source of org-edit-special for how it works.
It does a bit more than you need.
Basically, you want to set up a minor-mode for the created buffer that has a
binding to gather the text and restore window configuration.
I've written ges to edit arbitrary blocks in a new buffer using org-mode
machinery, but it's more complex than what you need.

Emacs copy with regex

I have a text file. Can Emacs select text based on regex and put it in kill-ring, so I can copy it somewhere else? Something like regex-kill-ring-save?
inspired by the already given comments (the Charles answer doesn't work as I would want it), I added a new function to the isearch/isearch-regexp mode map which puts only the matching string into the kill ring (whereas Charles proposal kills from current point to end of matching string):
(defun hack-isearch-kill ()
"Push current matching string into kill ring."
(interactive)
(kill-new (buffer-substring (point) isearch-other-end))
(isearch-done))
(define-key isearch-mode-map (kbd "M-w") 'hack-isearch-kill)
The nice thing about the isearch/isearch-regexp approach (which you can enable with C-s and C-M-s respectively) is that you can see your search string growing and you can copy it with M-w as soon as you are satisfied (and go back to where you have been before with C-u C-Space).
This works for me with Emacs 23.1. Don't know if it will work in all situations. Anyway I hope you find it useful :)
UPDATE: going through the emacswiki I stumbled over KillISearchMatch which suggests more or less the same (plus some more tips ...).
Cheers,
Daniel
I'm not sure if there is such a function already, but what you can do it with a keyboard macro:
Start recording a kbd macro: C-x (
Search for your regexp with search-forward-regexp
Move to the beginning of your match (the text you want to kill) with the various emacs navigation commands, e.g. search or backward-word etc.
Mark: C-spc
Move to the end of your match
Kill the text: C-w
You can then name the keyboard macro with M-x name-last-kbd-macro so that you can execute the macro with a name rather than with C-x e.
If you want to save the macro for future sessions, you can open your .emacs and insert the macro into the buffer with M-x insert-kbd-macro. After than you can bind a key to the macro just like you bind keys to normal emacs functions, e.g. (global-set-key "\C-c m" 'funky-macro-macro).
More about emacs keyboard macros
Isearch+ does this already. It optionally sets the region around the search target. You can use C-SPC C-SPC or M-= C-SPC at any time during Isearch to toggle this.
isearchp-deactivate-region-flag is a variable defined in isearch+.el.
Its value is t
Documentation:
Non-nil means isearching deactivates the region.
See also option isearchp-restrict-to-region-flag.
You can toggle this option using M-= C-SPC during Isearch.
You can customize this variable.