From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Nicolas Richard" Newsgroups: gmane.emacs.help Subject: Re: search across linebreaks Date: Mon, 18 Feb 2013 14:09:53 +0100 Message-ID: <87txp9py3i.fsf@yahoo.fr> References: <878v6nbd1i.fsf@ericabrahamsen.net> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1361193001 26774 80.91.229.3 (18 Feb 2013 13:10:01 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 18 Feb 2013 13:10:01 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Feb 18 14:10:23 2013 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1U7QUA-0001W6-FB for geh-help-gnu-emacs@m.gmane.org; Mon, 18 Feb 2013 14:10:22 +0100 Original-Received: from localhost ([::1]:42925 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U7QTp-0006i5-MM for geh-help-gnu-emacs@m.gmane.org; Mon, 18 Feb 2013 08:10:01 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:44106) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U7QTU-0005xz-PX for help-gnu-emacs@gnu.org; Mon, 18 Feb 2013 08:09:46 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1U7QTM-0001pP-Kv for help-gnu-emacs@gnu.org; Mon, 18 Feb 2013 08:09:40 -0500 Original-Received: from plane.gmane.org ([80.91.229.3]:51822) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U7QTM-0001p7-Em for help-gnu-emacs@gnu.org; Mon, 18 Feb 2013 08:09:32 -0500 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1U7QTd-0001ID-JW for help-gnu-emacs@gnu.org; Mon, 18 Feb 2013 14:09:49 +0100 Original-Received: from geodiff-mac3.ulb.ac.be ([164.15.131.113]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 18 Feb 2013 14:09:49 +0100 Original-Received: from theonewiththeevillook by geodiff-mac3.ulb.ac.be with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 18 Feb 2013 14:09:49 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 56 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: geodiff-mac3.ulb.ac.be User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2.92 (gnu/linux) Cancel-Lock: sha1:8PaAuwaThNEdGMPY7aVZfZc/lLU= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:89140 Archived-At: Eric Abrahamsen 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.