From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: Operate on each line in region Date: Wed, 23 Apr 2014 20:15:19 +0200 Organization: Informatimago Message-ID: <87ppk7x688.fsf@kuiper.lan.informatimago.com> References: <787a28ce-2ab1-4b11-92fd-ca4506447baa@googlegroups.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1398277227 32246 80.91.229.3 (23 Apr 2014 18:20:27 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 23 Apr 2014 18:20:27 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Apr 23 20:20:23 2014 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 1Wd1mN-0004xL-A5 for geh-help-gnu-emacs@m.gmane.org; Wed, 23 Apr 2014 20:20:19 +0200 Original-Received: from localhost ([::1]:34203 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wd1mM-0007Fg-Ra for geh-help-gnu-emacs@m.gmane.org; Wed, 23 Apr 2014 14:20:18 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 87 Original-X-Trace: individual.net x1/p4vS1E10fPjjVJFlcKAHPS0+40gA7LohyG6hRGsBZW2UkfS Cancel-Lock: sha1:NzkyOTQ2YjM2YzJkNDEzMDk3OTJiZWEzZDFmNzI4MjY1N2QzZTMwMw== sha1:mtLIInWa4EljqyLdGistITC+DvY= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:205070 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:97336 Archived-At: Jacob Gerlach 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 !"