From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Operate on each line in region
Date: Wed, 23 Apr 2014 20:15:19 +0200 [thread overview]
Message-ID: <87ppk7x688.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: 787a28ce-2ab1-4b11-92fd-ca4506447baa@googlegroups.com
Jacob Gerlach <jacobgerlach@gmail.com> writes:
> I am writing a function that will indent each line in a region to justify equals signs:
>
> foo = 1
> longerfoo = bar
>
> Would become
>
> foo = 1
> longerfoo = bar
>
> I believe I understand how to iterate through the lines in a region using either of two methods:
>
> condition on the buffer positions for the start and end of the region:
>
> (defun my-justify (start end)
> (interactive 'r')
> (while (< (point) end))
> ...body...
> (forward-line 1)))
>
> Narrow the buffer to region and use similar conditioning with (eobp).
>
> I'm wondering if there is a more straightforward way to accomplish this.
Good question.
If there's not, you should do it; it's trivial:
(defun call-with-region-narrowed-to-each-line-in-region (start end thunk)
(save-excursion
(save-restriction
(narrow-to-region start end)
(dolines (lstart lend)
(save-restriction
(narrow-to-region lstart lend)
(funcall thunk))))))
(defmacro with-region-narrowed-to-each-line-in-region (start end &rest body)
`(call-with-region-narrowed-to-each-line-in-region ,start ,end (lambda () ,@body)))
(with-temp-buffer
(let ((data '("deux" "trois" "quatre")))
(insert "1 one
2 two
3 three
4 four
5 five
")
(goto-char (point-min))
(forward-line 1)
(block nil
(let ((start (point)))
(goto-char (point-max))
(forward-line -1)
(let ((end (point)))
(with-region-narrowed-to-each-line-in-region start end
(let ((old (split-string (buffer-substring (point-min) (point-max)) " ")))
(message "%S" (list (point-min) (point-max) old))
(delete-region (point-min) (point-max))
(insert (format ":%s:%s" (first old) (pop data))))))))
(buffer-substring (point-min) (point-max))))
"1 one
:2:deux
:3:trois
:4:quatre
5 five
"
You can find dolines in:
https://gitorious.org/com-informatimago/emacs/source/b58a0a336b46f3523700931117b409307b13d9b0:pjb-emacs.el#L2262
Notice however, that the implementation of dolines doesn't allow you to
insert new lines in the processed lines, since this will lead to an
infinite loop. It could be modified to skip to the marker to the end of
the old line.
--
__Pascal Bourguignon__
http://www.informatimago.com/
"Le mercure monte ? C'est le moment d'acheter !"
next prev parent reply other threads:[~2014-04-23 18:15 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-04-23 17:45 Operate on each line in region Jacob Gerlach
2014-04-23 18:03 ` Doug Lewan
2014-04-23 18:05 ` Stefan Monnier
2014-04-23 18:15 ` Pascal J. Bourguignon [this message]
[not found] ` <mailman.20129.1398276348.10748.help-gnu-emacs@gnu.org>
2014-04-23 19:06 ` Barry Margolin
2014-04-23 19:18 ` Jacob Gerlach
2014-04-23 23:23 ` Jacob Gerlach
2014-04-23 23:44 ` Barry Margolin
2014-04-26 3:02 ` Jacob Gerlach
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87ppk7x688.fsf@kuiper.lan.informatimago.com \
--to=pjb@informatimago.com \
--cc=help-gnu-emacs@gnu.org \
/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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).