I want to use abbrev or something similar in term-mode instead of creating shell aliases. Reason being is it's nice to ssh somewhere and have the aliases available.
An annoyance is that I need to hit space for the abbrev to expand. For example if I have a short alias named l5 that's used often, I don't want to hit space every time for a two char thing.
How can I have it expand without space? Perhaps on hitting enter?
The function expand-abbrev will expand the abbrev at point. Expansion in abbrev mode is handled at a low level inside the internal functions of self-insert-command so you cannot just specify a new key.
You can however find a way to run expand-abbrev before sending the shell your input.
Whether you're using shell or term, it may be tricky. You can likely advise comint-send-input or term-send-raw respectively to deterministically run expand-abbrev before sending the text off.
Here is an example to support space and return abbrev expansion in term.
(defadvice term-send-raw (before maybe-expand-in-term activate)
(when (called-interactively-p)
(let ((keys (this-command-keys)))
;; if key was enter or space, expand abbrev at point
(when (or (equal keys (string 13))
(equal keys " "))
(expand-abbrev)))))
Related
I am not an emacs lisp hacker, but a very frequent user of emacs keyboard macros, especially for manipulating long data files. about 1-in-20 times, I screw up. my macro edits when it really should not. what I think I would need is something like
M-x check-regex-on-point
which would abort (ring the door bell) when the cursor is not on something specific that I would think that it should be on at this moment (e.g., a number, an alpha string, a comma, etc.)
is there a package or function for such tasks?
(defun check-regex-on-point (x)
"abort keyboard macro if not at the right spot"
(interactive "sregex: ")
(unless (looking-at-p x) (keyboard-quit))
)
based on Drew's points in his comments above.
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.
I've defined the \ character to behave as a word constituent in latex-mode, and I'm pretty happy with the results. The only thing bothering me is that a sequence like \alpha\beta gets treated as a single word (which is the expected behavior, of course).
Is there a way to make emacs interpret a specific character as a word "starter"? This way it would always be considered part of the word following it, but never part of the word preceding it.
For clarity, here's an example:
\alpha\beta
^ ^
1 2
If the point is at 1 and I press M-d, the string "\alpha" should be killed.
If the point is at 2 and I press M-<backspace>, the string "\beta" should be killed.
How can I achieve this?
Another thought:
Your requirement is very like what subword-mode provides for camelCase.
You can't customize subword-mode's behaviour -- the regexps are hard-coded -- but you could certainly copy that library and modify it for your purposes.
M-x find-library RET subword RET
That would presumably be a pretty robust solution.
Edit: updated from the comments, as suggested:
For the record, changing every instance of [[:upper:]] to [\\\\[:upper:]] in the functions subword-forward-internal and subword-backward-internal inside subword.el works great =) (as long as "\" is defined as "w" syntax).
Personally I would be more inclined to make a copy of the library than edit it directly, unless for the purpose of making the existing library a little more general-purpose, for which the simplest solution would seem to be to move those regexps into variables -- after which it would be trivial to have buffer-local modified versions for this kind of purpose.
Edit 2: As of Emacs 24.3 (currently a release candidate), subword-mode facilitates this with the new subword-forward-regexp and subword-backward-regexp variables (for simple modifications), and the subword-forward-function and subword-backward-function variables (for more complex modifications).
By making those regexp variables buffer-local in latex-mode with the desired values, you can just use subword-mode directly.
You should be able to implement this using syntax text properties:
M-: (info "(elisp) Syntax Properties") RET
Edit: Actually, I'm not sure if you can do precisely this?
The following (which is just experimentation) is close, but M-<backspace> at 2 will only delete "beta" and not the preceding "\".
I suppose you could remap backward-kill-word to a function which checked for that preceding "\" and killed that as well. Fairly hacky, but it would probably do the trick if there's not a cleaner solution.
I haven't played with this functionality before; perhaps someone else can clarify.
(modify-syntax-entry ?\\ "w")
(setq parse-sexp-lookup-properties t)
(setq syntax-propertize-function 'my-propertize-syntax)
(defun my-propertize-syntax (start end)
"Set custom syntax properties."
(save-excursion
(goto-char start)
(while (re-search-forward "\\w\\\\" end t)
(put-text-property
(1- (point)) (point) 'syntax-table (cons "." ?\\)))))
looking for an equivalent cut and paste strategy that would replicate vim's 'cut til'. I'm sure this is googleable if I actually knew what it was called in vim, but heres what i'm looking for:
if i have a block of text like so:
foo bar (baz)
and I was at the beginning of the line and i wanted to cut until the first paren, in visual mode, I'd do:
ct (
I think there is probably a way to look back and i think you can pass more specific regular expressions. But anyway, looking for some emacs equivalents to doing this kind of text replacement. Thanks.
Here are three ways:
Just type M-dM-d to delete two words. This will leave the final space, so you'll have to delete it yourself and then add it back if you paste the two words back elsewhere.
M-z is zap-to-char, which deletes text from the cursor up to and including a character you specify. In this case you'd have to do something like M-2M-zSPC to zap up to and including the second space character.
Type C-SPC to set the mark, then go into incremental search with C-s, type a space to jump to the first space, then C-s to search forward for the next space, RET to terminate the search, and finally C-w to kill the text you selected.
Personally I'd generally go with #1.
as ataylor said zap-to-char is the way to go, The following modification to the zap-to-char is what exactly you want
(defun zap-up-to-char (arg char)
"Like standard zap-to-char, but stops just before the given character."
(interactive "p\ncZap up to char: ")
(kill-region (point)
(progn
(search-forward (char-to-string char) nil nil arg)
(forward-char (if (>= arg 0) -1 1))
(point))))
(define-key global-map [(meta ?z)] 'zap-up-to-char) ; Rebind M-z to our version
BTW don't forget that it has the ability to go backward with a negative prefix
That sounds like zap-to-char in emacs, bound to M-z by default. Note that zap-to-char will cut all the characters up to and including the one you've selected.
I'm using Emacs in conjunction with AucTeX (running Ubuntu 10.04, if that matters).
Does anyone know if there is a way to automatically enable LaTeX-math-mode (a minor mode of AucTeX) if the point is in any maths environment (i.e. in a $...$, a $$...$$, begin{equation}...\end{equation}, and so on)?
I suppose there is a relatively easy answer, since syntax highlighting uses the same criterion for coloring math stuff, but I could not find anything.
If andre-r's answer doesn't satisfy you, here's some code that sets up ` to self-insert in text mode and act as a math mode prefix in math mode. LaTeX-math-mode must be off.
(defun LaTeX-maybe-math ()
"If in math mode, act as a prefix key for `LaTeX-math-keymap'.
Otherwise act as `self-insert-command'."
(interactive)
(if (texmathp)
(let* ((events (let ((overriding-local-map LaTeX-math-keymap))
(read-key-sequence "math: ")))
(binding (lookup-key LaTeX-math-keymap events)))
(call-interactively binding))
(call-interactively 'self-insert-command)))
(define-key LaTeX-mode-map "`" 'LaTeX-maybe-math)
The following improvements are left as exercises:
Make it a minor mode.
Make it more robust towards unexpected input (I've only tested basic operation).
Show a better error message if the user presses an unbound key sequence.
Show help if the user presses C-h or f1.
LaTeX-math-mode is "a special minor mode for entering text with many mathematical symbols." (For those who don't know how, you press e.g. `A and get \forall.) So I guess it doesn't hurt to leave it on, also if you're not entering maths.
The info page therefore suggests:
(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)
IMHO the only downside would be that you have to press the prefix twice: `` to get `, at least that works with the standard prefix ` customized in LaTeX-math-abbrev-prefix.