From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Eric Abrahamsen Newsgroups: gmane.emacs.help Subject: Re: search across linebreaks Date: Tue, 19 Feb 2013 09:22:18 +0800 Message-ID: <87bobhhzcl.fsf@ericabrahamsen.net> References: <878v6nbd1i.fsf@ericabrahamsen.net> <87txp9py3i.fsf@yahoo.fr> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1361236644 16358 80.91.229.3 (19 Feb 2013 01:17:24 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 19 Feb 2013 01:17:24 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Feb 19 02:17:47 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 1U7bq4-0000NM-Qu for geh-help-gnu-emacs@m.gmane.org; Tue, 19 Feb 2013 02:17:44 +0100 Original-Received: from localhost ([::1]:39525 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U7bpk-0002qj-Gz for geh-help-gnu-emacs@m.gmane.org; Mon, 18 Feb 2013 20:17:24 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:59133) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U7bpc-0002qY-LZ for help-gnu-emacs@gnu.org; Mon, 18 Feb 2013 20:17:20 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1U7bpZ-0004o5-Nt for help-gnu-emacs@gnu.org; Mon, 18 Feb 2013 20:17:16 -0500 Original-Received: from plane.gmane.org ([80.91.229.3]:48090) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U7bpZ-0004nz-Gs for help-gnu-emacs@gnu.org; Mon, 18 Feb 2013 20:17:13 -0500 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1U7bpq-0000FK-HH for help-gnu-emacs@gnu.org; Tue, 19 Feb 2013 02:17:30 +0100 Original-Received: from 114.250.114.226 ([114.250.114.226]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 19 Feb 2013 02:17:30 +0100 Original-Received: from eric by 114.250.114.226 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 19 Feb 2013 02:17:30 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 42 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 114.250.114.226 User-Agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.2 (gnu/linux) Cancel-Lock: sha1:MlLaYohEFsErkovh2QgluJMs9a8= 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:89160 Archived-At: "Nicolas Richard" writes: > 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")))) 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