all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Adding `#' at each new line with text until the end of the file
@ 2010-05-17 16:05 Merciadri Luca
  2010-05-17 18:26 ` Pascal J. Bourguignon
  0 siblings, 1 reply; 11+ messages in thread
From: Merciadri Luca @ 2010-05-17 16:05 UTC (permalink / raw
  To: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I have some personally-defined function:

==
(defun psig ()
  "Insert the signature for a personal posting on a newsgroup, including a saying."
  (interactive)
  (goto-char (point-max))
  (or (= (char-after (- (point) 1)) ?\n)
      (newline 1))
  (insert "-- \n")
  (let ((saying ""))
    (save-window-excursion
      (find-file "~/Sayings")
      (goto-char (point-min))
      (search-forward "#")
      (delete-char -1)
      (setq fill-prefix nil)
      (mark-paragraph)
      (setq saying (buffer-substring (region-beginning) (region-end)))
      (exchange-point-and-mark)
      (if (eobp) (goto-char (1+ (point-min))))
      ;(insert "#")
      (save-buffer))
    (insert saying)
    ))
==

The file ~/Sayings contains sayings, and, at the beginning, each line
with some saying was beginning with a #.

So, my sayings' file looked like:

==

#saying1

#saying2

#...

#sayingn

==

Nice, but my psig() function takes the `#' to tell me that the given
saying has already been used. As it looks for the following `#', it
takes the first line with `#' beginning it, that is, the first saying
which has not been already used before.

The problem is that, as I do not have *a lot* of sayings, I would like
Gnus to modify the sayings' file so it looks like at its beginning,
that is, with a `#' beginning each saying.

I tried

==
(defun fildi ()
       (find-file "/~Sayings")
       (goto-char (point-min))
       ; go to the first saying, and do the following until the end of the
       ; file is reached, for each line with some text on it:
       (insert "#")
       (save-buffer))
==

But I don't know how to translate my two lines of comments into real
ELisp code. Any idea about how I could achieve this?

Thanks.

- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

Act today only, tomorrow is too late.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAkvxaTQACgkQM0LLzLt8MhzYBQCgoZsPqIFxOkVJjMnpdh41rUMw
0AMAoKILCLVRtsastH6moo0tjpbqU7Gg
=XnME
-----END PGP SIGNATURE-----


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Adding `#' at each new line with text until the end of the file
  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
  0 siblings, 1 reply; 11+ messages in thread
From: Pascal J. Bourguignon @ 2010-05-17 18:26 UTC (permalink / raw
  To: help-gnu-emacs

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

> The problem is that, as I do not have *a lot* of sayings, I would like
> Gnus to modify the sayings' file so it looks like at its beginning,
> that is, with a `#' beginning each saying.
>
> I tried
>
> ==
> (defun fildi ()
>        (find-file "/~Sayings")
>        (goto-char (point-min))
>        ; go to the first saying, and do the following until the end of the
>        ; file is reached, for each line with some text on it:
>        (insert "#")
>        (save-buffer))
> ==
>
> But I don't know how to translate my two lines of comments into real
> ELisp code. Any idea about how I could achieve this?

A line that has some text on it is a line that has at least one character right?

So:
    (while (< (point) (point-max))
        (when (looking-at ".")
            (insert "#"))
        (forward-line))

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Adding `#' at each new line with text until the end of the file
  2010-05-17 18:26 ` Pascal J. Bourguignon
@ 2010-05-17 20:11   ` Merciadri Luca
       [not found]     ` <87zkzyjkcb.fsf@kuiper.lan.informatimago.com>
  0 siblings, 1 reply; 11+ messages in thread
From: Merciadri Luca @ 2010-05-17 20:11 UTC (permalink / raw
  To: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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); 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?

- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

When making your choices in life, do not forget to live. (Samuel Johnson)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAkvxovUACgkQM0LLzLt8Mhy/UwCfZVeNrnTF0Ygl0XwS54iTVvEa
qRwAn1oBeyo6ORYEJ1D/YZ2zVu6uCvyZ
=0fJA
-----END PGP SIGNATURE-----


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Adding `#' at each new line with text until the end of the file
       [not found]     ` <87zkzyjkcb.fsf@kuiper.lan.informatimago.com>
@ 2010-05-18 10:09       ` Merciadri Luca
  2010-05-18 12:06         ` Pascal J. Bourguignon
  0 siblings, 1 reply; 11+ messages in thread
From: Merciadri Luca @ 2010-05-18 10:09 UTC (permalink / raw
  To: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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?
- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

All flowers are not in one garden.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAkvyZ1EACgkQM0LLzLt8MhxBqQCfTSIZs8NH6I+BWy9zXViSHlDc
65IAnie+few8wvSEwVuQMNtnmNao6tdq
=uGJQ
-----END PGP SIGNATURE-----


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Adding `#' at each new line with text until the end of the file
  2010-05-18 10:09       ` Merciadri Luca
@ 2010-05-18 12:06         ` Pascal J. Bourguignon
  2010-05-18 16:47           ` Merciadri Luca
  0 siblings, 1 reply; 11+ messages in thread
From: Pascal J. Bourguignon @ 2010-05-18 12:06 UTC (permalink / raw
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Adding `#' at each new line with text until the end of the file
  2010-05-18 12:06         ` Pascal J. Bourguignon
@ 2010-05-18 16:47           ` Merciadri Luca
  2010-05-18 20:27             ` Pascal J. Bourguignon
  0 siblings, 1 reply; 11+ messages in thread
From: Merciadri Luca @ 2010-05-18 16:47 UTC (permalink / raw
  To: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pjb@informatimago.com (Pascal J. Bourguignon) writes:

>> 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?
Insert modifies the current buffer, according to the manual. Or
find-file loads its arg into a buffer. So, if find-file loads its arg
into the *current* buffer, insert should modify the current buffer,
that is, `Sayings'.

> 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)))))))
Thanks. I'll keep it.

- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

The whole dignity of man lies in the power of thought. 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAkvyxLcACgkQM0LLzLt8MhyKawCfXMN2zho0p+/bBEc1gQAG3YmD
MO4AoK6a8STV9snV2hmw0the3TF9GnIy
=CMWS
-----END PGP SIGNATURE-----


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Adding `#' at each new line with text until the end of the file
  2010-05-18 16:47           ` Merciadri Luca
@ 2010-05-18 20:27             ` Pascal J. Bourguignon
  2010-05-19 16:08               ` Merciadri Luca
  0 siblings, 1 reply; 11+ messages in thread
From: Pascal J. Bourguignon @ 2010-05-18 20:27 UTC (permalink / raw
  To: help-gnu-emacs

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

> pjb@informatimago.com (Pascal J. Bourguignon) writes:
>
>>> 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?
> Insert modifies the current buffer, according to the manual. Or
> find-file loads its arg into a buffer. So, if find-file loads its arg
> into the *current* buffer, insert should modify the current buffer,
> that is, `Sayings'.

(list
    (progn (find-file "/tmp/Sayings")
           (buffer-name (current-buffer)))
    (progn (find-file "/mnt/Sayings")
           (buffer-name (current-buffer))))
--> ("Sayings" "Sayings<2>")

Here you have two files, named "/tmp/Sayings" and "/mnt/Sayings", and
when opening them at the same time,  we get "Sayings" and "Sayings<2>"
as buffer names.

Clearly, the buffer names are not entirely independant from the file
names, but it should be obvious from the example, that there's two
name spaces and two different kind of entities.  A buffer named "X" is
not a file name "X".

So to repeat what you wrote above:

- find-file (or find-file-noselect) loads the contents of a  _file_
  into a _buffer_.

- insert modifies the contents of a _buffer_.

When  you do only these to action what happens to the _file_?


>> Here is a macro that could be useful:

Here with find-file-noselect as it has been advised (not tested):

(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
                (find-file-noselect ,path t ,literal)
                (prog1 (save-excursion ,@body)
                  ,(when save `(save-buffer 1))))
           ,(when kill
                  `(kill-buffer (current-buffer)))))))

> Thanks. I'll keep it.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Adding `#' at each new line with text until the end of the file
  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 19:05                 ` Andreas Politz
  0 siblings, 2 replies; 11+ messages in thread
From: Merciadri Luca @ 2010-05-19 16:08 UTC (permalink / raw
  To: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pjb@informatimago.com (Pascal J. Bourguignon) writes:

> Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> writes:
>
>> pjb@informatimago.com (Pascal J. Bourguignon) writes:
>>
>>>> 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?
>> Insert modifies the current buffer, according to the manual. Or
>> find-file loads its arg into a buffer. So, if find-file loads its arg
>> into the *current* buffer, insert should modify the current buffer,
>> that is, `Sayings'.
>
> (list
>     (progn (find-file "/tmp/Sayings")
>            (buffer-name (current-buffer)))
>     (progn (find-file "/mnt/Sayings")
>            (buffer-name (current-buffer))))
> --> ("Sayings" "Sayings<2>")
>
> Here you have two files, named "/tmp/Sayings" and "/mnt/Sayings", and
> when opening them at the same time,  we get "Sayings" and "Sayings<2>"
> as buffer names.
>
> Clearly, the buffer names are not entirely independant from the file
> names, but it should be obvious from the example, that there's two
> name spaces and two different kind of entities.  A buffer named "X" is
> not a file name "X".
>
> So to repeat what you wrote above:
>
> - find-file (or find-file-noselect) loads the contents of a  _file_
>   into a _buffer_.
>
> - insert modifies the contents of a _buffer_.
>
> When  you do only these to action what happens to the _file_?
Nothing, actually. That's the problem.

>>> Here is a macro that could be useful:
>
> Here with find-file-noselect as it has been advised (not tested):
>
> (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
>                 (find-file-noselect ,path t ,literal)
>                 (prog1 (save-excursion ,@body)
>                   ,(when save `(save-buffer 1))))
>            ,(when kill
>                   `(kill-buffer (current-buffer)))))))
>
>> Thanks. I'll keep it.
Thanks.

- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

Nobody leaves us, we only leave others.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAkv0DPwACgkQM0LLzLt8Mhyt3gCgpC8sYQPznCu0gI19a9TszeXK
+bQAn2rfXklxI3EmG6Pjs6LBAAVnmmTU
=2zNs
-----END PGP SIGNATURE-----


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Adding `#' at each new line with text until the end of the file
  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
  1 sibling, 1 reply; 11+ messages in thread
From: Pascal J. Bourguignon @ 2010-05-20 13:18 UTC (permalink / raw
  To: help-gnu-emacs

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:
>>>
>>>>> 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?
>>> Insert modifies the current buffer, according to the manual. Or
>>> find-file loads its arg into a buffer. So, if find-file loads its arg
>>> into the *current* buffer, insert should modify the current buffer,
>>> that is, `Sayings'.
>>
>> (list
>>     (progn (find-file "/tmp/Sayings")
>>            (buffer-name (current-buffer)))
>>     (progn (find-file "/mnt/Sayings")
>>            (buffer-name (current-buffer))))
>> --> ("Sayings" "Sayings<2>")
>>
>> Here you have two files, named "/tmp/Sayings" and "/mnt/Sayings", and
>> when opening them at the same time,  we get "Sayings" and "Sayings<2>"
>> as buffer names.
>>
>> Clearly, the buffer names are not entirely independant from the file
>> names, but it should be obvious from the example, that there's two
>> name spaces and two different kind of entities.  A buffer named "X" is
>> not a file name "X".
>>
>> So to repeat what you wrote above:
>>
>> - find-file (or find-file-noselect) loads the contents of a  _file_
>>   into a _buffer_.
>>
>> - insert modifies the contents of a _buffer_.
>>
>> When  you do only these to action what happens to the _file_?
> Nothing, actually. That's the problem.

Yes.  That's because you haven't said something.  What should you say
to have the file modified in any way?

\f
Something like: 

  - save the buffer to the file, keeping a backup of the old file.

or, in emacs lisp: 

   (save-buffer 1)

Remember: Computers only do what you tell them to do!  ;-)

-- 
__Pascal Bourguignon__
http://www.informatimago.com


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Adding `#' at each new line with text until the end of the file
  2010-05-20 13:18                 ` Pascal J. Bourguignon
@ 2010-05-20 18:09                   ` Merciadri Luca
  0 siblings, 0 replies; 11+ messages in thread
From: Merciadri Luca @ 2010-05-20 18:09 UTC (permalink / raw
  To: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pjb@informatimago.com (Pascal J. Bourguignon) writes:

> 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:
>>>>
>>>>>> 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?
>>>> Insert modifies the current buffer, according to the manual. Or
>>>> find-file loads its arg into a buffer. So, if find-file loads its arg
>>>> into the *current* buffer, insert should modify the current buffer,
>>>> that is, `Sayings'.
>>>
>>> (list
>>>     (progn (find-file "/tmp/Sayings")
>>>            (buffer-name (current-buffer)))
>>>     (progn (find-file "/mnt/Sayings")
>>>            (buffer-name (current-buffer))))
>>> --> ("Sayings" "Sayings<2>")
>>>
>>> Here you have two files, named "/tmp/Sayings" and "/mnt/Sayings", and
>>> when opening them at the same time,  we get "Sayings" and "Sayings<2>"
>>> as buffer names.
>>>
>>> Clearly, the buffer names are not entirely independant from the file
>>> names, but it should be obvious from the example, that there's two
>>> name spaces and two different kind of entities.  A buffer named "X" is
>>> not a file name "X".
>>>
>>> So to repeat what you wrote above:
>>>
>>> - find-file (or find-file-noselect) loads the contents of a  _file_
>>>   into a _buffer_.
>>>
>>> - insert modifies the contents of a _buffer_.
>>>
>>> When  you do only these to action what happens to the _file_?
>> Nothing, actually. That's the problem.
>
> Yes.  That's because you haven't said something.  What should you say
> to have the file modified in any way?
>
> \f
> Something like: 
>
>   - save the buffer to the file, keeping a backup of the old file.
>
> or, in emacs lisp: 
>
>    (save-buffer 1)
>
> Remember: Computers only do what you tell them to do!  ;-)
Yes.

Thanks for your implication in this.

I am now trying with

==
(defun fildi ()
  (find-file "~/Sayings")
  ;(goto-char (point-min))
  (while (< (point) (point-max))
    (when (looking-at ".")
      (insert "#"))
    (forward-line)) 
  (save-buffer 1) 
  )
==
which `should' do the trick, as the file is found, opened in the
buffer, the buffer is modified as I want it to be, and the buffer is
then saved. Why is it still erroneous?

Thanks.
- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

A real friend is someone who walks in when the rest of the world walks out.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAkv1etkACgkQM0LLzLt8MhzstwCdG6pMCOXZSljbXxz6Snp5/c/N
wK4Anj2S5SO1H5wExmyvS3lCUHpLfbeZ
=Ck8d
-----END PGP SIGNATURE-----


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Adding `#' at each new line with text until the end of the file
  2010-05-19 16:08               ` Merciadri Luca
  2010-05-20 13:18                 ` Pascal J. Bourguignon
@ 2010-05-20 19:05                 ` Andreas Politz
  1 sibling, 0 replies; 11+ messages in thread
From: Andreas Politz @ 2010-05-20 19:05 UTC (permalink / raw
  To: help-gnu-emacs

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

>> Here with find-file-noselect as it has been advised (not tested):
>>
>> (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
>>                 (find-file-noselect ,path t ,literal)
>>                 (prog1 (save-excursion ,@body)
>>                   ,(when save `(save-buffer 1))))
>>            ,(when kill
>>                   `(kill-buffer (current-buffer)))))))
>>

This macro may kill buffers at random, because `find-file-noselect' does
not make the files buffer current.

But even with `find-file', this may go wrong, in case the function does
not succeed or body changes the current buffer.

-ap


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2010-05-20 19:05 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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.