From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: pjb@informatimago.com (Pascal J. Bourguignon) Newsgroups: gmane.emacs.help Subject: Re: Adding `#' at each new line with text until the end of the file Date: Tue, 18 May 2010 14:06:03 +0200 Organization: Informatimago Message-ID: References: <87k4r24iq3.fsf@merciadriluca-station.MERCIADRILUCA> <87bpcel6zj.fsf@kuiper.lan.informatimago.com> <878w7is2yy.fsf@merciadriluca-station.MERCIADRILUCA> <87zkzyjkcb.fsf@kuiper.lan.informatimago.com> <87aarx4j3i.fsf@merciadriluca-station.MERCIADRILUCA> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1291832417 14645 80.91.229.12 (8 Dec 2010 18:20:17 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 8 Dec 2010 18:20:17 +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 Dec 08 19:20:11 2010 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PQOcd-0006pD-3n for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 19:20:11 +0100 Original-Received: from localhost ([127.0.0.1]:58866 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQOcc-0005a6-B9 for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 13:20:10 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 100 Original-X-Trace: individual.net Ov50yAwnEZ1RLYV6YigqHgwMdTvrIBOdKxeKwNcDmBbcsLKQSI Cancel-Lock: sha1:YWU0YTZlZTFiYjBjMDZkYzU0Y2I3YTc3NTgyNmY3YWZmOWJlMWFiOA== sha1:XjMCuk5VeTGE/6SxkZ7QiB3S9jw= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.101 (Gnus v5.10.10) Emacs/23.1 (darwin) Original-Xref: usenet.stanford.edu gnu.emacs.help:178235 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:75779 Archived-At: Merciadri Luca writes: > pjb@informatimago.com (Pascal J. Bourguignon) writes: > >> Merciadri Luca writes: >> >>> pjb@informatimago.com (Pascal J. Bourguignon) writes: >>> >>>> >>>> So: >>>> (while (< (point) (point-max)) >>>> (when (looking-at ".") >>>> (insert "#")) >>>> (forward-line)) >>>> >>> >>> After extensive testing, >>> >>> == >>> (defun fildi () >>> (find-file "~/Sayings") >>> (goto-char (point-min)) >>> (while (< (point) (point-max)) >>> (when (looking-at ".") >>> (insert "#")) >>> (forward-line)) >>> ) >>> == >>> >>> does not work. When I evaluate it using C-x C-e (in a buffer), it only >>> says `fildi', which does not look very interesting. What am I doing >>> wrong? It finds the file; it goes to the beginning of it; while it has >>> not reached the end of the file, it looks for a line containing a dot >>> (as every saying ends by a dot); >> >> No. Did you read the documentation of looking-at? > No. I should have done it. >> Because that's >> what you should do for every new function you see! > Nice reflex. > >> What does the >> documentation of looking-at says about the arguments of the function? >> >> (looking-at ".") tests whether the cursor is at position where there >> is any character, but a newline. That is, it tests whether the line >> contains something, since we should be at the beginning of the line, >> because this is what point-min should be at (actually, unless you >> narrow the region from the middle of a line), and this is ensured by >> forward-line. > Okay, thanks. >> >>> while this works, it inserts a "#" >>> and, as the cursor is at the beginning of the line, it inserts a "#" >>> at the beginning of the line. It also goes to the next line. And? >> >> Wasn't it what you asked for? Otherwise I didn't understand what you >> wanted. > Sure, but, as I explained in my previous message, it does not even > modify the Sayings file. Why? Because you didn't instruct the program to modify the file. Read the documentation of insert, for example. Does it mention files? What does insert modify? Here is a macro that could be useful: (require 'cl) (defmacro* with-file (file-and-options &body body) "Processes BODY with a buffer on the given file. DO: find-file or find-file-literally, process body, and optionally save the buffer and kill it. save is not done if body exits exceptionnaly. kill is always done as specified. FILE-AND-OPTION: either an atom evaluated to a path, or (path &key (save t) (kill t) (literal nil)) " (if (atom file-and-options) `(with-file (,file-and-options) ,@body) ;; destructuring-bind is broken, we cannot give anything else than nil ;; as default values: (destructuring-bind (path &key (save nil savep) (kill nil killp) (literal nil literalp)) file-and-options (unless savep (setf save t)) (unless killp (setf kill t)) `(unwind-protect (progn (,(if literal 'find-file-literally 'find-file) ,path) (prog1 (save-excursion ,@body) ,(when save `(save-buffer 1)))) ,(when kill `(kill-buffer (current-buffer))))))) -- __Pascal Bourguignon__ http://www.informatimago.com