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: Fri, 20 Dec 2019 22:50:29 +0900 [thread overview]
Message-ID: <2C8281A0-290F-4505-8495-4992E8E0B82B@traduction-libre.org> (raw)
In-Reply-To: <87k16ua2qm.fsf@telefonica.net>
Ok, here is my attempt at this:
(setq myText "<item>bla</item>")
(setq myMarker "<!-- place new items before this comment -->")
(setq myFile "/path/to/test.xml")
(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)
(save-buffer)
(kill-buffer)))
(myInsert myText myMarker myFile)
I've used find-file-noselect and insert, as suggested.
Are there things I can do to make that code more idiomatic ? Is there anything I am seemingly not understanding ?
Thank you in advance.
Jean-Christophe
> On Dec 18, 2019, at 9:36, Óscar Fuentes <ofv@wanadoo.es> wrote:
>
> Jean-Christophe Helary <jean.christophe.helary@traduction-libre.org>
> writes:
>
>> I'm struggling with what is the ideal "elispy" way of adding contents
>> to a file at a given position.
>>
>> What I want it:
>> - create some content
>> - put that content at a specific location in a file saved on disk
>> (current use case: add an xml block to an RSS file)
>>
>> I thought write-region would do the trick but there are 2 issues with
>> it
>>
>> 1) if APPEND is a number, that's the position from which my contents
>> will *overwrite* the rest of the file (I want to *insert* my contents)
>>
>> 2) there are weird things like the whole code where I call this
>> function is actually copied to the target file buffer and I have no
>> idea how to prevent that.
>>
>> So, I thought of something a bit convoluted:
>>
>> use a temporary buffer
>> insert the contents of the file there
>
> find-file-noselect takes care of creating the buffer and reading the
> contents of the file. Later you must get rid of the buffer.
>
>> find the point where I want to insert my contents
>
> use `insert' or `insert-buffer-substring'
>
>> write all that to the original file
>>
>> But I thought, it's not like such things don't happen all the time in
>> emacs, so there must be a better workflow.
>>
>> But the reference is incredibly cryptic regarding that and the Intro
>> does not address any file i/o at all...
>
> The sequence of operations above is actually quite simple (although not
> very efficient if the target file is large and your new content is near
> the end).
>
>
Jean-Christophe Helary
-----------------------------------------------
http://mac4translators.blogspot.com @brandelune
next prev parent reply other threads:[~2019-12-20 13:50 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 [this message]
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
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=2C8281A0-290F-4505-8495-4992E8E0B82B@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.
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).