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: Sun, 22 Dec 2019 12:14:04 +0900	[thread overview]
Message-ID: <3C9F1A1A-551E-411B-91B2-CB31B51094B4@traduction-libre.org> (raw)
In-Reply-To: <jwvzhfnng8w.fsf-monnier+emacs@gnu.org>



> On Dec 21, 2019, at 1:00, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> 
>> (setq myText "<item>bla</item>")
>> (setq myMarker "<!-- place new items before this comment -->")
>> (setq myFile "/path/to/test.xml")
> 
> Being at top-level these aren't just setting the vars but defining them,
> so should use `defvar` or `defconst`.
> 
>> (defun myInsert (myText myMarker myFile)
>>  (save-current-buffer
>>    (set-buffer (find-file-noselect myFile))
> 
> `with-current-buffer` does the same, but shorter ;-)

How is it supposed to be shorter ?

(defun myInsert (myText myMarker myFile)
  (save-current-buffer
    (set-buffer (find-file-noselect myFile))
    (goto-char (point-min))
    (goto-char (- (search-forward myMarker) (length myMarker)))
    (insert myText)
    (indent-region (point-min) (point-max))
    (save-buffer)
    (kill-buffer)))

(defun myInsert2 (myText myMarker myFile)
  (with-current-buffer
      (set-buffer (find-file-noselect myFile))
    (goto-char (point-min))
    (goto-char (- (search-forward myMarker) (length myMarker)))
    (insert myText)
    (indent-region (point-min) (point-max))
    (save-buffer)
    (kill-buffer)))


Honestly, as I read the respective doc strings and reference parts again, I'm not even sure why I chose save-current-buffer here anymore, but *because* there is no explanatory document on how to do proper file i/o in the Elisp Reference it is hard to make informed decisions, or even progress without a lot of searching outside the reference.

>>    (goto-char (point-min))
>>    (goto-char (- (search-forward myMarker) (length myMarker)))
> 
> If the search fails, this will signal a "low-level" error, and it's
> often useful to replace it with some other behavior (e.g. an error
> message which the user is more likely to understand, or some other
> behavior), so it's more idiomatic to do something like:
> 
>    (goto-char (point-min))
>    (if (not (search-forward myMarker nil t))
>        (user-error "Can't find foo bar in your fine file")
>      (goto-char (match-beginning 0))

But here, the code would go on inserting the text in a position that's not correct, right ?

Isn't it possible to catch the error around the search ? The only language I know, AppleScript, has a

try
  body
on error
  do something
end try

block which seems equivalent to "condition-case" but it seems that I'd have to use a progn to have the equivalent:

(defun myInsert3 (myText myMarker myFile)
  (save-current-buffer
    (set-buffer (find-file-noselect myFile))
    (goto-char (point-min))
    (condition-case nil
	(progn
	  (goto-char (- (search-forward myMarker) (length myMarker)))
	  (insert myText)
	  (indent-region (point-min) (point-max))
	  (save-buffer))
      (error (format "%s was not found" myMarker)))
    (kill-buffer)))

And then the message is not displayed. I just get a "t" which I guess corresponds to the successful (kill-buffer)...

And if I put the kill-buffer inside the progn, then I'm left with an open buffer that's not relevant anymore...


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





  reply	other threads:[~2019-12-22  3:14 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 [this message]
2019-12-22  4:42         ` Jean-Christophe Helary
2019-12-22 14:37           ` Óscar Fuentes
2019-12-22 22:18             ` Jean-Christophe Helary
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=3C9F1A1A-551E-411B-91B2-CB31B51094B4@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.