all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: Adding `#' at each new line with text until the end of the file
Date: Tue, 18 May 2010 14:06:03 +0200	[thread overview]
Message-ID: <lzsk5pifdg.fsf@informatimago.com> (raw)
In-Reply-To: 87aarx4j3i.fsf@merciadriluca-station.MERCIADRILUCA

Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> writes:

> pjb@informatimago.com (Pascal J. Bourguignon) writes:
>
>> Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> 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?


\f
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


  reply	other threads:[~2010-05-18 12:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-17 16:05 Adding `#' at each new line with text until the end of the file Merciadri Luca
2010-05-17 18:26 ` Pascal J. Bourguignon
2010-05-17 20:11   ` Merciadri Luca
     [not found]     ` <87zkzyjkcb.fsf@kuiper.lan.informatimago.com>
2010-05-18 10:09       ` Merciadri Luca
2010-05-18 12:06         ` Pascal J. Bourguignon [this message]
2010-05-18 16:47           ` Merciadri Luca
2010-05-18 20:27             ` Pascal J. Bourguignon
2010-05-19 16:08               ` Merciadri Luca
2010-05-20 13:18                 ` Pascal J. Bourguignon
2010-05-20 18:09                   ` Merciadri Luca
2010-05-20 19:05                 ` Andreas Politz

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=lzsk5pifdg.fsf@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.
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.