all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: help-gnu-emacs@gnu.org
Subject: Re: search across linebreaks
Date: Tue, 19 Feb 2013 09:22:18 +0800	[thread overview]
Message-ID: <87bobhhzcl.fsf@ericabrahamsen.net> (raw)
In-Reply-To: 87txp9py3i.fsf@yahoo.fr

"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




      reply	other threads:[~2013-02-19  1:22 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-17  7:43 search across linebreaks Eric Abrahamsen
2013-02-17 13:13 ` Jude DaShiell
     [not found] ` <mailman.20189.1361106838.855.help-gnu-emacs@gnu.org>
2013-02-17 14:43   ` J G Miller
2013-02-17 15:52 ` Drew Adams
2013-02-18  3:52   ` Eric Abrahamsen
2013-02-18  4:01     ` Jambunathan K
2013-02-18  6:09       ` Eric Abrahamsen
2013-02-17 17:05 ` Andreas Röhler
2013-02-18 13:09 ` Nicolas Richard
2013-02-19  1:22   ` Eric Abrahamsen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bobhhzcl.fsf@ericabrahamsen.net \
    --to=eric@ericabrahamsen.net \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.