help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: safe way to add contents to a file ?


From: Jean-Christophe Helary
Subject: Re: safe way to add contents to a file ?
Date: Mon, 23 Dec 2019 07:18:58 +0900

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 via email to

[Prev in Thread] Current Thread [Next in Thread]