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: Wed, 18 Dec 2019 12:20:36 +0900

Thank you Oscar.

> 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).

The problem I have is that nothing in the reference or in the doc strings or 
anywhere else I looked gives a clear path to discovering how to do that 
seemingly frequent action.

Or maybe there is something I missed.

Can you give me an indication on how to discover the process you described ?

Also, I don't understand the reason why write-region behaves the way I 
described:

>> 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.

Do you have an idea ?


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





reply via email to

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