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

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

Custom query-replace.


From: R. Clayton
Subject: Custom query-replace.
Date: Sun, 12 Jul 2015 16:47:56 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

I'm trying to write a query-replace command to deal with hyphens in text.  For
each match, I want to perform one of three changes:

  delete hyphen; go from "for- got" to "forgot"
  unspace hyphen; go from "red- headed" to "red-headed"
  space hyphen; go from "stop- it" to "stop - it"

as well as the usual query-replace options (quit, skip, and so on).  For
example, typing 'D' at the query-replace prompt would delete the hyphen.

I had a hazy idea I could do this by flogging some keymap.  However, once I got
this far

  (defun rehyphenate ()

    (interactive)

    (let (f r)

      (fset 'r (lambda (data count)
                 (concat (match-string 1) "-" (match-string 2))))

      (fset 'f (lambda ()
                 (perform-replace
                   "\\([a-z]\\)- +\\([a-z]\\)"    ; from-string
                   (cons 'r "")                   ; replacements
                   t                              ; query-flag
                   t                              ; regexp-flag
                   nil                            ; delimited-flag
                   nil                            ; repeat-count
                   nil                            ; keymap
                   (point-min)                    ; start
                   (point-max)                    ; end
                   )))
      (while (f)
        )))

I realized my hazy idea was hazier than I though.  It seems to me I have two
paths I can follow:

  Continue to flog the keymap by including commands in the keymap that in turn
  flog the replacement text, or otherwise communicate the change choice to the
  replacement function.

  Move the query from the match to the replacement function, which would forego
  standard query-replace behavior, such as quitting and skipping, which is an
  ok trade-off.

Both of these paths seem unattractive to me.  What alternatives are there
available for me to do what I want?  Is there some similarly-behaving code
around I can steal from?




reply via email to

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