help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Electric punctuation replacement for editing (correcting grammar)


From: Thien-Thi Nguyen
Subject: Re: Electric punctuation replacement for editing (correcting grammar)
Date: Tue, 13 Dec 2011 22:30:08 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

() "Ludwig, Mark" <ludwig.mark@siemens.com>
() Tue, 13 Dec 2011 19:32:31 +0000

   I don't know how to handle situations like:

       I thought so (I was not sure, but suspected.    [before]

   This should be:

       I thought so (I was not sure), but suspected.   [after]

   I want to be able to type the closing parenthesis (")") just as I might
   type semi-colon (";") in the above example, and have it correctly place the
   parenthesis before the comma.

Perhaps a stateful approach is indicated?

(defun ttn-electric-close-paren ()
  "Insert a close-paren, or bump one just inserted backwards."
  (interactive)
  (save-excursion
    ;; futz only for subsequent invocations
    (when (eq 'ttn-electric-close-paren last-command)
      (search-backward ")")
      (delete-char 1)
      (forward-word -1))
    ;; keep the riff-raff out
    (while (memq (char-before) '(?\s ?, ?\n ?! ?? ?.))
      (forward-char -1))
    (insert ")")))

To test, you can bind this command to ‘s-q’.  With point at end
of sentence, to go from [before] to [after] thus requires typing
‘s-q’ three times.

Of course, this kind of approach can be tedious if the correction
occurs far way from end-of-sentence.  One could modify the command
to take an optional numeric arg specifying how many words to skip
backwards initially on first invocation, for that.

Another code improvement opportunity is the loop to find the end
of word.  One could test against character syntax, instead, or
perhaps simply do ‘(forward-word -1) (forward-word 1)’, or
[insert emacs var tweak here whereby (forward-word -1) DTRT].

Maybe best would be to encourage the original authors to use
Emacs and its balanc{ing,ed} facilities, to thwart the error
grievous and grating, arranging for mischievous mating...



reply via email to

[Prev in Thread] Current Thread [Next in Thread]