all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Nicolas Richard" <theonewiththeevillook@yahoo.fr>
To: help-gnu-emacs@gnu.org
Subject: Re: search across linebreaks
Date: Mon, 18 Feb 2013 14:09:53 +0100	[thread overview]
Message-ID: <87txp9py3i.fsf@yahoo.fr> (raw)
In-Reply-To: 878v6nbd1i.fsf@ericabrahamsen.net

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"))))

There are many "areas for improvement" (aka bugs), e.g. it might be
necessary to allow more than just "\n" to be deleted/restored (I
imagine you could make `lom' into an alist of (marker . deleted-text)
and restore deleted-text instead of just inserting \n).

Test it with smth like:
(progn
  (insert
   "One two foo
bar three do
bar baz foo
baz for foo

bar baz foo
bar foo bar
foo bar foo
bar foo bar")
  (goto-char (point-min))
  (yf/test))

-- 
N.




  parent reply	other threads:[~2013-02-18 13:09 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 [this message]
2013-02-19  1:22   ` Eric Abrahamsen

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=87txp9py3i.fsf@yahoo.fr \
    --to=theonewiththeevillook@yahoo.fr \
    --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.