all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Jean-Christophe Helary <jean.christophe.helary@traduction-libre.org>
To: help-gnu-emacs@gnu.org
Subject: Re: safe way to add contents to a file ?
Date: Mon, 23 Dec 2019 07:18:58 +0900	[thread overview]
Message-ID: <4F3F7D8E-870C-4C9B-8B7D-2B05CE1B87A8@traduction-libre.org> (raw)
In-Reply-To: <877e2ofmtv.fsf@telefonica.net>

Oscar, Stephan,

Thank you *so* much for your comments.

Regarding `user-error' or `error', neither the doc strings nor the reference mention that the execution is stopped, I discovered that eventually but that was confusing. Am I missing something ?

Oscar, you say that a `let' would be "nicer" than a `setq', is it because `let' is local to the body that follows, so that I don't have to "polute" my top level with locally used values ?

Stephan, regarding `save-current-buffer' vs `with-file-noselect', the former saves the buffer that is current so I need to use `set-buffer' to set the current buffer, but `with-file-noselect' sets the current buffer to the buffer associated with the file, hence `set-buffer' is redundant there, right ?

`unwind-protect' is pretty cool ! I guess it can be used to easily handle errors without bothering about error messages. The doc strings are very clear, but the reference is totally confusing:

"The ‘unwind-protect’ construct is essential whenever you temporarily put
a data structure in an inconsistent state; it permits you to make the
data consistent again in the event of an error or throw.  (Another more
specific cleanup construct that is used only for changes in buffer
contents is the atomic change group; *note Atomic Changes::.)"

I would never think of using that form for such a trivial issue. Why is the writing so obfuscated ?

Jean-Christophe 



> On Dec 22, 2019, at 23:37, Óscar Fuentes <ofv@wanadoo.es> wrote:
> 
> Jean-Christophe Helary <jean.christophe.helary@traduction-libre.org>
> writes:
> 
>> Here is my latest version...
>> 
>> 
>> (defun myInsert4 (myText myMarker myFile)
>>  (save-current-buffer
>>    (set-buffer (find-file-noselect myFile))
>>    (goto-char (point-min))
>>    (if (not (search-forward myMarker nil t))
>> 	(progn
>> 	  (user-error (format "%s was not found" myMarker))
>> 	  (kill-buffer))
>>      (progn
>> 	(goto-char (point-min))
>> 	(goto-char (- (search-forward myMarker) (length myMarker)))
>> 	(insert myText)
>> 	(indent-region (point-min) (point-max))
>> 	(save-buffer)))
>>    (kill-buffer)))
>> 
>> Really not sure if I'm going in the right direction. Plus, for some
>> reason, the buffer is not killed even after an error...
> 
> `user-error' (which is a variant of `error') stops the execution, so
> `kill-buffer' is never executed. Either put `kill-buffer' before
> `user-error' or do not use `user-error', like this:
> 
> (defun myInsert4 (myText myMarker myFile)
>  (save-current-buffer (find-file-noselect myFile))
>    (goto-char (point-min))
>    (if (not (search-forward myMarker nil t))
>        (message "%s was not found" myMarker)
>      (progn
> 	(goto-char (point-min))
> 	(goto-char (- (search-forward myMarker) (length myMarker)))
> 	(insert myText)
> 	(indent-region (point-min) (point-max))
> 	(save-buffer)))
>    (kill-buffer))
> 
> You can also cache the result of `search-forward', thus avoiding
> repeating it:
> 
> (defun myInsert4 (myText myMarker myFile)
>  (save-current-buffer (find-file-noselect myFile))
>    (goto-char (point-min))
>    (setq p (search-forward myMarker nil t))
>    (if (not p)
>        (message "%s was not found" myMarker)
>      (progn
> 	(goto-char (- p (length myMarker)))
> 	(insert myText)
> 	(indent-region (point-min) (point-max))
> 	(save-buffer)))
>    (kill-buffer))
> 
> A `let' would be nicer than a `setq'. Fixing that is left as an exercise
> for the reader.

Jean-Christophe Helary
-----------------------------------------------
http://mac4translators.blogspot.com @brandelune





  reply	other threads:[~2019-12-22 22:18 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-18  0:03 safe way to add contents to a file ? Jean-Christophe Helary
2019-12-18  0:36 ` Óscar Fuentes
2019-12-18  3:20   ` Jean-Christophe Helary
2019-12-20 13:50   ` Jean-Christophe Helary
2019-12-20 16:00     ` Stefan Monnier
2019-12-22  3:14       ` Jean-Christophe Helary
2019-12-22  4:42         ` Jean-Christophe Helary
2019-12-22 14:37           ` Óscar Fuentes
2019-12-22 22:18             ` Jean-Christophe Helary [this message]
2019-12-23  0:08               ` Óscar Fuentes
2019-12-22 18:23         ` Stefan Monnier
2019-12-18  3:59 ` Stefan Monnier
2019-12-18  9:41   ` Jean-Christophe Helary
2019-12-18 13:10     ` Óscar Fuentes
2019-12-18 22:33       ` Jean-Christophe Helary
2019-12-18 14:10     ` Stefan Monnier
2019-12-18 22:25       ` Jean-Christophe Helary
2019-12-18 22:27       ` Jean-Christophe Helary

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=4F3F7D8E-870C-4C9B-8B7D-2B05CE1B87A8@traduction-libre.org \
    --to=jean.christophe.helary@traduction-libre.org \
    --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.