From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stephen Berman Newsgroups: gmane.emacs.devel Subject: Re: uniq Date: Sat, 04 Dec 2010 13:08:28 +0100 Message-ID: <87ipz9hhab.fsf@escher.home> References: <20101203.184112.37434497.Takaaki.Ota@am.sony.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1291464523 28079 80.91.229.12 (4 Dec 2010 12:08:43 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sat, 4 Dec 2010 12:08:43 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Dec 04 13:08:39 2010 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1POqus-00068R-4l for ged-emacs-devel@m.gmane.org; Sat, 04 Dec 2010 13:08:38 +0100 Original-Received: from localhost ([127.0.0.1]:51486 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1POqur-0004GT-HQ for ged-emacs-devel@m.gmane.org; Sat, 04 Dec 2010 07:08:37 -0500 Original-Received: from [140.186.70.92] (port=60977 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1POqul-0004GN-Qc for emacs-devel@gnu.org; Sat, 04 Dec 2010 07:08:32 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1POquk-0002bu-JQ for emacs-devel@gnu.org; Sat, 04 Dec 2010 07:08:31 -0500 Original-Received: from lo.gmane.org ([80.91.229.12]:37235) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1POquk-0002bX-8d for emacs-devel@gnu.org; Sat, 04 Dec 2010 07:08:30 -0500 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1POquj-00065D-7c for emacs-devel@gnu.org; Sat, 04 Dec 2010 13:08:29 +0100 Original-Received: from i59f573f4.versanet.de ([89.245.115.244]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 04 Dec 2010 13:08:29 +0100 Original-Received: from stephen.berman by i59f573f4.versanet.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 04 Dec 2010 13:08:29 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 44 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: i59f573f4.versanet.de User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:133400 Archived-At: On Fri, 3 Dec 2010 18:41:12 -0800 Tak Ota wrote: > Do we have something equivalent to the next command? I know the name > is bad as it is not same as UNIQ(1). It works better because sorting > is not required. > > -Tak > > ;; > ;; uniq > ;; > (defun uniq () > "Omit duplicated lines." > (interactive) > (save-excursion > (goto-char (point-min)) > (while (not (= (point) (point-max))) > (let* ((start (point)) > (str (format "^%s" > (regexp-quote > (buffer-substring start > (progn (forward-line 1) (point))))))) > (save-excursion > (while (re-search-forward str nil t) > (delete-region (match-beginning 0) (match-end 0)))))))) Would it be faster to avoid nested while-loops? (defun uniq () "Omit duplicated lines." (interactive) (let (lines) (save-excursion (goto-char (point-min)) (while (not (eobp)) (let* ((beg (line-beginning-position)) (end (line-end-position)) (line (buffer-substring beg end))) (if (member line lines) (delete-region beg (1+ end)) (push line lines) (forward-line))))))) Steve Berman