Paredit and transpose code blocks - emacs

If I have the following s-expression:
(if true (this) (that))
And I want:
(if true (that) (this))
How does paredit helps to transpose (this) and (that)?

I don't know what "paredit" is, but in stock Emacs you can place the
point between (this) and (that) and hit C-M-t which runs
the command transpose-sexps:
Like C-t (transpose-chars), but applies to sexps.
Unlike transpose-words, point must be between the two sexps and not
in the middle of a sexp to be transposed.
With non-zero prefix arg ARG, effect is to take the sexp before point
and drag it forward past ARG other sexps (backward if ARG is negative).
If ARG is zero, the sexps ending at or after point and at or after mark
are interchanged.

Related

Compute the number of universal-arguments before digit argument

How can I find the number of universal-arguments used to invoke a command prior to a numeric-argument.
For example, how could I differentiate between arg1 and arg2 in the following (wrong) function?
(defun my-func (&optional arg1 arg2)
(interactive "P\nP")
(message "arg1: %S, arg2: %S, value: %S"
arg1 arg2 (prefix-numeric-value current-prefix-arg)))
Use case, transparently passing the numeric prefix value to an advised command depending on the number of prior universal-arguments, eg. dired rename following 2 files in current dired buffer with C-u 2 or to other dired buffer with C-u C-u 2.
Something along the lines of the following advice, but that actually passes the 2 properly to dired-do-rename instead of either 1 or 4.
(define-advice dired-do-rename (:around (fun &rest args) "defer-dwim")
(let ((dired-dwim-target (equal '(4) current-prefix-arg)))
(apply fun (list (max 1 (prefix-numeric-value current-prefix-arg))))))
I see there is a prefix-command-preserve-state-hook, but it seems like significant additional effort to make that work.
Sorry, but your question is quite unclear to me - so this might not answer it.
You say "How can I find the number of universal-arguments used to invoke a command prior to a numeric-argument." I'll answer that.
It doesn't matter how many times you write "P" in an interactive string spec. The value is the same each time, and multiple such occurrences do NOT correspond to multiple uses of C-u.
So (interactive "P\nP\nP") just produces three arguments that all have the same value - the value that comes from a single use of a prefix argument key sequence, whether that sequence is C-u, C-9, M--3, C-u 360, or C-u C-u C-u.
If you want your function to distinguish, say, C-u, C-u C-u, and C-u C-u C-u then it needs to test the raw prefix argument (what "P" specifies for interactive).
C-u corresponds to the raw prefix arg (4), that is, a list with just the number 4.
C-u C-u corresponds to the raw prefix arg (16).
C-u C-u C-u corresponds to the raw prefix arg (64).
(prefix-numeric-value '(4)) returns 4; (prefix-numeric-value '(16)) returns 16; and (prefix-numeric-value '(64)) returns 64.
See the Elisp manual, node Prefix Command Arguments.
You can practice with something like this command, to see what the resulting raw and numeric prefix arguments are when you type different prefix-arg key sequences:
(defun foo (arg narg)
(interactive "P\np")
(message "ARG: %S, NARG: %S" arg narg))
The rest of your question is completely unclear to me. I don't understand what you're trying to do with "dired-do-rename. If I'm right, please consider posting two separate questions: one asking how to distinguish C-u from C-u C-u and the other asking something (what?) about dired-do-rename.

Elisp: is this an expected behavior of save-excursion?

The starting condition is this text in *scratch*:
(
form
30
)
(
baz
41
)
The whitespace is the only important thing here.
Now, with point on o in form, evaluating with M-:(eval-expression)
this code:
(save-excursion
(up-list)
(backward-list)
(indent-sexp))
Gives
(
form
30
)
with point on o. Same (expected) behavior repeats for point on r,m,0,a,z,1.
However for starting point on f, 3, b, 4, the resulting point is one character before
the expected position.
Is this the expected behavior or a bug?
If it's expected, I'd like to know more about rules and edge cases.
UPD: Add intuitive save-excursion variant
Just in case someone will want it,
here's the behavior that's more intuitive to me (i.e. saves point on f, 3 etc.):
(defmacro save-excursion-ex (&rest body)
"More intuitive (`save-excursion' BODY)."
`(let ((ext (save-match-data
(looking-back "^\\s-*")))
(out (save-excursion
,#body)))
(if ext
(if (or (bolp)
(= (point)
(save-excursion
(back-to-indentation)
(point))))
(back-to-indentation)
(error "Unexpected")))
out))
In Emacs, the point is considered to be "before" (point).
So, when indent-sexp inserts whitespace, it inserts it after the point.
E.g., when you say that the point is on f, it is actually at the beginning of line right before f and indent-sexp inserts spaces after it and before f.
The general rule is that if the point is between the beginning of line and the first non-blank character in the line, it will end up at the beginning of the line, otherwise it will preserve its position relative the surrounding text.
Think of it this way: we indent the code by
delete all leading blanks
insert leading blanks as necessary
So, the answer is: this is the expected behavior.
More to the implied gist of your question, there is no reason to rely on the specific position of the point after indentation if the point was adjacent to whitespace which was normalized.
If you need to get your code working, add a (goto-char (line-beginning-position)) (or equivalent) after the indentation call and then skip to the position you want to operate on.

flyspell correct the previous to previous mistake

suppose I have buffer contents as follows
teh msot |
curser is at |. generally I can correct msot to most with single C-; press (flyspell-auto-correct-previous-word). What I want is to correct teh to the, i.e previous to previous mistake. (or in general nth spell)
It seems flyspell-auto-correct-previous-word is taking numerical argument but not yielding intended result.
what am I missing.?
UPDATE:
Why I need this., when I write research notes, flyspell mistakenly marks some scientific words wrong. So need to skip the one or two false marks.
C-h f flyspell-auto-correct-previous-word tells me that the numerical argument is called "position". That does not look like what you are looking for. Position is likely to refer to a position in the buffer. Looking at the flyspell sourcecode reveals that the parameter is not used in the intendet way (cant tell what an overlay is thoug...)
;*---------------------------------------------------------------------*/
;* flyspell-auto-correct-previous-word ... */
;*---------------------------------------------------------------------*/
(defun flyspell-auto-correct-previous-word (position)
"*Auto correct the first mispelled word that occurs before point."
(interactive "d")
(add-hook 'pre-command-hook
(function flyspell-auto-correct-previous-hook) t t)
(save-excursion
(unless flyspell-auto-correct-previous-pos
;; only reset if a new overlay exists
(setq flyspell-auto-correct-previous-pos nil)
(let ((overlay-list (overlays-in (point-min) position))
(new-overlay 'dummy-value))
[SNIP]
also the (interactive "d") shows that the current position of point is assigned to position in case of an interactive call.
matthias

emacs equivalent of ct

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.

How can you modify two matching delimiters at once with Emacs?

While this question concerns the formatting of LaTeX within Emacs (and maybe Auctex), I believe this can be applied to more general situations in Emacs concerning delimiters like parentheses, brackets, and braces.
I am looking to be able to do the following with Emacs (and elisp), and don't know where to begin. Say I have:
(This is in parentheses)
With some keybinding in Emacs, I want Emacs to find the matching delimiter to whichever one is by my cursor (something I know Emacs can do since it can highlight matching delimiters in various modes) and be able to change both of them to
\left( This is in parentheses \right)
The delimiters I would like this to work with are: (...), [...], \lvert ... \rvert, \langle ... \rangle, \{ ... \}. What elisp would I need to accomplish this task?
More general ways to handle matching delimiters are welcome.
Evaluate the command below in Emacs. After reloading you can put the point (text cursor) immediately after a closing paren. Then do M-x replace-matching-parens to replace the closing ) with \right) and the matching start paren ( with \left(.
(defun replace-matching-parens ()
(interactive)
(save-excursion
(let ((end-point (point)))
(backward-list)
(let ((start-point (point)))
(goto-char end-point)
(re-search-backward ")" nil t)
(replace-match " \\\\right)" nil nil)
(goto-char start-point)
(re-search-forward "(" nil t)
(replace-match "\\\\left( " nil nil)))))
The interactive bit indicates that I want a "command", so it can be executed using M-x. To avoid the cursor ending up in a strange place after execution I'm wrapping the logic in save-excursion. The point jumps back to the opening paren using backward-list and holds on to the start and end positions of the paren-matched region. Lastly, starting at the end and working backwards I replace the strings. By replacing backwards rather than forwards I avoid invalidating end-point before I need it.
Generalizing this to handle different kinds of delimiters shouldn't be too bad. backward-list ought to work with any pair of strings emacs recognizes as analogues of ( and ). To add more parenthesis-like string pairs, check out set-syntax-table in this Parenthesis Matching article.
Use global-set-key to setup a key binding to replace-matching-parens.
Fair warning: replace-matching-parens is the first elisp command I've implemented, so it may not align with best practices. To all the gurus out there, I'm open to constructive criticism.