From: Arni Magnusson <arnima@hafro.is>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 2887@emacsbugs.donarmstrong.com, bug-lisp-manual@gnu.org
Subject: bug#2887: Suggestions for simple.el
Date: Sun, 19 Apr 2009 13:41:39 +0000 (GMT) [thread overview]
Message-ID: <alpine.LFD.0.9999.0904191206110.11783@localhost.localdomain> (raw)
In-Reply-To: <jwvzledxs6x.fsf-monnier+emacsbugreports@gnu.org>
[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
next prev parent reply other threads:[~2009-04-19 13:41 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-04-04 13:32 bug#2887: Suggestions for simple.el Arni Magnusson
2009-04-04 14:21 ` Stefan Monnier
2009-04-04 23:35 ` Arni Magnusson
2009-04-05 3:27 ` Stefan Monnier
2009-04-05 14:26 ` Leo
2009-04-05 20:17 ` Arni Magnusson
2009-04-05 21:59 ` Lennart Borgman
2009-04-06 2:14 ` Stefan Monnier
2009-04-06 3:02 ` Drew Adams
2009-04-07 2:46 ` Arni Magnusson
2009-04-07 14:02 ` Stefan Monnier
2009-04-07 16:09 ` Drew Adams
2009-04-07 16:09 ` Drew Adams
2009-04-07 17:18 ` Stefan Monnier
2009-04-07 17:18 ` Stefan Monnier
2009-04-07 17:22 ` Chong Yidong
2009-04-07 17:26 ` Drew Adams
2009-04-07 17:26 ` Drew Adams
2009-04-07 21:43 ` Stefan Monnier
2009-04-07 17:22 ` Chong Yidong
2009-04-18 0:08 ` Arni Magnusson
2009-04-18 19:32 ` Stefan Monnier
2009-04-19 1:13 ` Arni Magnusson
2009-04-19 1:40 ` Arni Magnusson
2009-04-19 3:14 ` Stefan Monnier
2009-04-19 13:41 ` Arni Magnusson [this message]
2020-09-19 21:50 ` Lars Ingebrigtsen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alpine.LFD.0.9999.0904191206110.11783@localhost.localdomain \
--to=arnima@hafro.is \
--cc=2887@emacsbugs.donarmstrong.com \
--cc=bug-lisp-manual@gnu.org \
--cc=monnier@iro.umontreal.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.