[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: search across linebreaks
From: |
Eric Abrahamsen |
Subject: |
Re: search across linebreaks |
Date: |
Tue, 19 Feb 2013 09:22:18 +0800 |
User-agent: |
Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.2 (gnu/linux) |
"Nicolas Richard" <theonewiththeevillook@yahoo.fr> writes:
> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>> The two solutions I can think of are: 1) break up the characters in the
>> search string and insert "\n?" between each one to create regexps to
>> search on, and 2) unfill the whole file at the start of the procedure
>> and then refill it afterwards. Neither of these seems like a great
>> idea -- does anyone have any brighter ideas?
>
> Not bright by any means, but slightly different from your solutions. The idea
> is : save newlines as markers (except if two or more consecutive), and
> restore afterwards.
>
> (defun yf/test nil ""
> (let* (lom marker
> (dict '(("foo bar" "foo barred")
> ("foo baz" "foo bazzed")
> ("foo foo" "foo fooed")))
> (regexp (regexp-opt (mapcar 'car dict))))
> ;; replace single newlines by markers (recorded in a list of markers)
> (while (search-forward "\n" nil t)
> (if (looking-at "\n")
> (skip-chars-forward "\n")
> (replace-match " ")
> (add-to-list 'lom (set-marker (make-marker) (point)))))
> (goto-char (point-min))
> ;; replace matches according to dict
> (while (re-search-forward regexp nil t)
> (replace-match (cadr (assoc (match-string 0) dict)) t t))
> ;; transform markers into newline again
> (while (setq marker (pop lom))
> (goto-char marker)
> (when (looking-at " ")
> (replace-match ""))
> (insert "\n"))))
That's a pretty interesting solution, thank you! I don't use markers
much, but the basic idea of the marker -- a spot that remains immobile
relative to the text around it -- seems pretty applicable to my problem.
Thanks!
Eric