From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.help Subject: Re: `looking-back' strange warning Date: Fri, 02 Oct 2015 22:59:41 +0200 Message-ID: <87lhblrnpe.fsf@gnu.org> References: <560B9C7F.2060301@easy-emacs.de> <560CD7CE.4010404@yandex.ru> <6524c81a-949c-40d1-b990-d214d6ee5b60@default> <560D73DF.8040403@yandex.ru> <560D7A68.8040404@easy-emacs.de> <560D8289.6010409@yandex.ru> <560D8F96.5040200@easy-emacs.de> <877fn66pag.fsf@web.de> <190e27a5-9099-45ce-a8db-a314b9e32b35@default> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1443819798 7035 80.91.229.3 (2 Oct 2015 21:03:18 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 2 Oct 2015 21:03:18 +0000 (UTC) Cc: Michael Heerdegen , help-gnu-emacs@gnu.org To: Drew Adams Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Oct 02 23:03:08 2015 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 1Zi7Tq-00040C-Cm for geh-help-gnu-emacs@m.gmane.org; Fri, 02 Oct 2015 23:03:02 +0200 Original-Received: from localhost ([::1]:35180 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zi7Tp-0008NF-Of for geh-help-gnu-emacs@m.gmane.org; Fri, 02 Oct 2015 17:03:01 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:42377) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zi7Qi-00084S-Fj for help-gnu-emacs@gnu.org; Fri, 02 Oct 2015 16:59:49 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zi7Qf-0002oK-94 for help-gnu-emacs@gnu.org; Fri, 02 Oct 2015 16:59:48 -0400 Original-Received: from out4-smtp.messagingengine.com ([66.111.4.28]:42951) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zi7Qf-0002nx-5A for help-gnu-emacs@gnu.org; Fri, 02 Oct 2015 16:59:45 -0400 Original-Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 9B07520669 for ; Fri, 2 Oct 2015 16:59:44 -0400 (EDT) Original-Received: from frontend2 ([10.202.2.161]) by compute5.internal (MEProxy); Fri, 02 Oct 2015 16:59:44 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=smtpout; bh=WUnGqXSkMo5Bj8Uau+U0Ad6zfrI=; b=Cyi7Q LNN1Nzv5rl/yTR4K7FChw2S91se/q3Wcx67sIR1sZs9Y7dCKxRhL/pJgYzk9F7rL dN3eACP0ZQcHjjISzyw7279ZzBd+XcRmeyJeVia0YIwW+9/vAkaCi416aHHfUhwP URtH/tGeFR6Bqlw88tro742FmK+4WEtq064kww= X-Sasl-enc: xb2papgIsXwr7p8BnudbL4g5ZZkEC/xT23aG5/8+0HQz 1443819584 Original-Received: from thinkpad-t440p (unknown [2.161.209.103]) by mail.messagingengine.com (Postfix) with ESMTPA id 4BDE5680120; Fri, 2 Oct 2015 16:59:43 -0400 (EDT) Mail-Followup-To: Drew Adams , Michael Heerdegen , help-gnu-emacs@gnu.org In-Reply-To: <190e27a5-9099-45ce-a8db-a314b9e32b35@default> (Drew Adams's message of "Fri, 2 Oct 2015 13:28:15 -0700 (PDT)") User-Agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/25.0.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.28 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:107486 Archived-At: Drew Adams writes: >> > My two cents: Even if you specify an optimal limit, `looking-back' can >> > still be unnecessarily slow. For example, >> > (looking-back "xy" 2) >> > is much slower than >> > (and (> (point) 2) >> > (save-excursion (goto-char (- (point) 2)) >> > (looking-at "xy"))) >> > where "xy" stands for any plain string. >> >> Definitely. And this is in fact a typical case of the >> misuse: the string to check is often a literal, so its >> length is known. >> >> This is the first thing to mention about `looking-back', >> in terms of performance: avoid it altogether, if you can. > > BTW/FWIW - > I use this function in such cases, which can be pretty common: > > (defun chars-before (chars) > "Return non-nil if the literal string CHARS is right before point. > This is more efficient that `looking-back' for this use case." > (let* ((len (length chars)) > (idx (1- len)) > (pt (point))) > (catch 'chars-before > (dolist (char (append chars ())) > (unless (condition-case nil > (eq char (char-before (- pt idx))) > (error nil)) ; e.g. `bobp' > (throw 'chars-before nil)) > (setq idx (1- idx))) > t))) This version is about 6 times faster in the t case and a still a bit faster in the nil case where the char before point is already different (which is the best case for your function). (defun chars-before (chars) "Return non-nil if the literal string CHARS is right before point. This is more efficient that `looking-back' for this use case." (let ((beg (- (point) (length chars)))) (unless (< beg 0) (save-excursion (goto-char beg) (looking-at (regexp-quote chars)))))) Bye, Tassilo