unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "Óscar Fuentes" <ofv@wanadoo.es>
To: help-gnu-emacs@gnu.org
Subject: Re: safe way to add contents to a file ?
Date: Sun, 22 Dec 2019 15:37:32 +0100	[thread overview]
Message-ID: <877e2ofmtv.fsf@telefonica.net> (raw)
In-Reply-To: 55F7CE36-37C4-49BC-95F2-E91DE6C4628F@traduction-libre.org

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.




  reply	other threads:[~2019-12-22 14:37 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 [this message]
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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=877e2ofmtv.fsf@telefonica.net \
    --to=ofv@wanadoo.es \
    --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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).