From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Arni Magnusson Newsgroups: gmane.emacs.bugs Subject: bug#2887: Suggestions for simple.el Date: Sun, 19 Apr 2009 13:41:39 +0000 (GMT) Message-ID: References: <26172.194.144.135.59.1238851923.squirrel@www.hafro.is> <11531.194.144.135.59.1238888128.squirrel@www.hafro.is> <13654.194.144.135.59.1238962672.squirrel@www.hafro.is> <16717.194.144.135.59.1239072410.squirrel@www.hafro.is> Reply-To: Arni Magnusson , 2887@emacsbugs.donarmstrong.com NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Trace: ger.gmane.org 1240149885 29220 80.91.229.12 (19 Apr 2009 14:04:45 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 19 Apr 2009 14:04:45 +0000 (UTC) Cc: 2887@emacsbugs.donarmstrong.com, bug-lisp-manual@gnu.org To: Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Apr 19 16:06:02 2009 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1LvXeG-0005rj-Ja for geb-bug-gnu-emacs@m.gmane.org; Sun, 19 Apr 2009 16:05:53 +0200 Original-Received: from localhost ([127.0.0.1]:37065 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LvXcr-00016t-HN for geb-bug-gnu-emacs@m.gmane.org; Sun, 19 Apr 2009 10:04:05 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LvXcm-00015p-UH for bug-gnu-emacs@gnu.org; Sun, 19 Apr 2009 10:04:00 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LvXcj-000155-3j for bug-gnu-emacs@gnu.org; Sun, 19 Apr 2009 10:04:00 -0400 Original-Received: from [199.232.76.173] (port=53890 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LvXci-00014z-LO for bug-gnu-emacs@gnu.org; Sun, 19 Apr 2009 10:03:56 -0400 Original-Received: from rzlab.ucr.edu ([138.23.92.77]:50640) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1LvXch-0004m8-Ru for bug-gnu-emacs@gnu.org; Sun, 19 Apr 2009 10:03:56 -0400 Original-Received: from rzlab.ucr.edu (rzlab.ucr.edu [127.0.0.1]) by rzlab.ucr.edu (8.13.8/8.13.8/Debian-3) with ESMTP id n3JE3p4A027722; Sun, 19 Apr 2009 07:03:53 -0700 Original-Received: (from debbugs@localhost) by rzlab.ucr.edu (8.13.8/8.13.8/Submit) id n3JDo3JS023706; Sun, 19 Apr 2009 06:50:03 -0700 X-Loop: owner@emacsbugs.donarmstrong.com Resent-From: Arni Magnusson Resent-To: bug-submit-list@donarmstrong.com Resent-CC: Emacs Bugs Resent-Date: Sun, 19 Apr 2009 13:50:03 +0000 Resent-Message-ID: Resent-Sender: owner@emacsbugs.donarmstrong.com X-Emacs-PR-Message: followup 2887 X-Emacs-PR-Package: emacs X-Emacs-PR-Keywords: Original-Received: via spool by 2887-submit@emacsbugs.donarmstrong.com id=B2887.124014851322182 (code B ref 2887); Sun, 19 Apr 2009 13:50:03 +0000 Original-Received: (at 2887) by emacsbugs.donarmstrong.com; 19 Apr 2009 13:41:53 +0000 X-Spam-Bayes: score:0.5 Bayes not run. spammytokens:Tokens not available. hammytokens:Tokens not available. Original-Received: from hafgarpur.hafro.is (hafgarpur.hafro.is [130.208.64.48]) by rzlab.ucr.edu (8.13.8/8.13.8/Debian-3) with ESMTP id n3JDfmZb022162 for <2887@emacsbugs.donarmstrong.com>; Sun, 19 Apr 2009 06:41:50 -0700 X-Virus-Scanned: amavisd-new at hafro.is Original-Received: from localhost.localdomain (hafstormur.hafro.is [130.208.66.52]) by hafgarpur.hafro.is (8.14.2/8.14.2/hafro-2.45) with ESMTP id n3JDff3I021593; Sun, 19 Apr 2009 13:41:41 GMT Original-Received: from localhost.localdomain (localhost [127.0.0.1]) by localhost.localdomain (8.14.2/8.14.2/hafro-1.6) with ESMTP id n3JDfeE9012561; Sun, 19 Apr 2009 13:41:40 GMT Original-Received: from localhost (arnima@localhost) by localhost.localdomain (8.14.2/8.14.2/hafro-0.3) with ESMTP id n3JDfdVu012558; Sun, 19 Apr 2009 13:41:40 GMT X-Authentication-Warning: localhost.localdomain: arnima owned process doing -bs X-X-Sender: arnima@localhost.localdomain In-Reply-To: X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Resent-Date: Sun, 19 Apr 2009 10:04:00 -0400 X-BeenThere: bug-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:27350 Archived-At: [Arni Magnusson wrote:] >> (defun pos-at-beginning-of-line (N) >> "Return the position at beginning of line N." >> (save-excursion >> (goto-char (point-min))(line-beginning-position N))) >> >> (defun pos-at-end-of-line (N) >> "Return the position at end of line N." >> (save-excursion >> (goto-char (point-min))(line-end-position N))) >> >> (defun comment-line-or-region () >> "Comment line or region." >> (interactive) >> (require 'newcomment) >> (if (and mark-active transient-mark-mode) >> (comment-region >> (pos-at-beginning-of-line >> (line-number-at-pos (region-beginning))) >> (pos-at-end-of-line >> (line-number-at-pos (region-end)))) >> (comment-region >> (line-beginning-position)(line-end-position)))) [Stefan Monnier wrote:] > A perfect example of the kind of performance bug that comes up when you > think in terms of lines, as encouraged by pos-at-beginning/end-of-line. > The above should be: > > (defun comment-line-or-region () > "Comment line or region." > (interactive) > (require 'newcomment) > (if (and mark-active transient-mark-mode) > (comment-region > (save-excursion > (goto-char (region-beginning))(line-beginning-position)) > (save-excursion > (goto-char (region-end))(line-end-position))) > (comment-region > (line-beginning-position)(line-end-position)))) > > line-number-at-pos is also a "function to avoid", just as bad as > goto-line. Your code will walk over the whole buffer 4 times (twice to > compute the line-number at region beg and end, then twice to find the > beg/end of those 2 lines). --- Aha, now I see what you mean. One should use relative motion in Emacs Lisp programs and avoid referring to absolute line numbers (`goto-line', `line-beginning-position', `line-end-position', `line-number-at-pos'). Thank you Stefan, for explaining this to me - now I would like to help others to avoid using these functions in Emacs Lisp programs, when possible. Couldn't this be mentioned in the docstrings and in the Emacs Lisp Manual? They had already helped me by tagging a warning sign on functions like: `next-line', `previous-line' `beginning-of-buffer', `end-of-buffer' `replace-string', `replace-regexp' `insert-file', `insert-buffer' In my notes, I have also written that (goto-char (point-min)) is better than (goto-line 1), but now I can't see where this is documented. Besides the docstrings and the function entries in the manual, there is a section in the manual called "Emacs Programming Tips" where the pitfalls of *-line-* functions could be mentioned. Thanks, Arni