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: Wed, 18 Dec 2019 18:41:09 +0900	[thread overview]
Message-ID: <08057AA1-387E-489F-9DB9-CF1768172251@traduction-libre.org> (raw)
In-Reply-To: <jwvsglifg3x.fsf-monnier+emacs@gnu.org>



> On Dec 18, 2019, at 12:59, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> 
>> 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)
> 
> This is the way life works, it's not specific to Emacs: given the way
> files are typically represented on disk, there is simply no efficient
> way to *insert* into a file.

Maybe a note about that in the function reference would be a good reminder that disks are not like "shelves" or other places where you put "files" and "folders" IRL.

>> 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.
> 
> I don't understand what you're describing, really, but from where
> I stand it sounds like you just had a weird bug in your code which made
> it do something you did not intend.

The code is a most simple one liner variation on the following:

(write-region "stuff" nil "path/to/my/file/test.txt" nil t nil t).

Sometimes the whole *line* above (contained in file "test.el") would be copied to the file "test.txt" and I have no idea why.

>> But I thought, it's not like such things don't happen all the time in emacs,
>> so there must be a better workflow.
> 
> The normal workflow in Emacs is:
> - read the file into a buffer.
> - modify the buffer as you please (here, insertion is available and
>  reasonably efficient).
> - save the buffer back into the file.
> 
> The case you describe seems to fit this workflow perfectly and it'll be
> pretty close to optimal compared to what could theoretically be obtained
> from the POSIX API (my guess is that in the very worst case you'll be
> performing twice the minimum number of disk accesses).

Good to know. Thank you.

>> But the reference is incredibly cryptic regarding that and the Intro does
>> not address any file i/o at all...
> 
> Which part of which reference?

The elisp reference. I'm not aware that there is any other reference material regarding elisp. Is there ?

> What have you looked for?

The files chapter, the buffers chapter. It's all descriptions of function after function and overall very confusing when you don't know the basics of what's necessary.

> Given the structure of your problem, you have to read the file in order
> to find the precise place where you want to insert the new content and
> once you've read the file into a buffer, the rest seems to follow
> quite naturally.

It would certainly follow naturally if I were an elisp native. But thank you for the general confirmation that I'm going in the right direction.


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





  reply	other threads:[~2019-12-18  9:41 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
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 [this message]
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=08057AA1-387E-489F-9DB9-CF1768172251@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.