From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Jean-Christophe Helary Newsgroups: gmane.emacs.help Subject: Re: safe way to add contents to a file ? Date: Wed, 18 Dec 2019 12:20:36 +0900 Message-ID: References: <0FE61EAF-672C-4348-8107-F4C3D176FCF4@traduction-libre.org> <87k16ua2qm.fsf@telefonica.net> Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3601.0.10\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="220334"; mail-complaints-to="usenet@blaine.gmane.org" Cc: help-gnu-emacs@gnu.org To: =?utf-8?Q?=C3=93scar_Fuentes?= Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Dec 18 04:25:45 2019 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1ihPy4-000v8f-5p for geh-help-gnu-emacs@m.gmane.org; Wed, 18 Dec 2019 04:25:44 +0100 Original-Received: from localhost ([::1]:49030 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihPy2-0002zB-De for geh-help-gnu-emacs@m.gmane.org; Tue, 17 Dec 2019 22:25:42 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48439) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihPtH-0005ql-Ho for help-gnu-emacs@gnu.org; Tue, 17 Dec 2019 22:20:48 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihPtG-0004bT-0w for help-gnu-emacs@gnu.org; Tue, 17 Dec 2019 22:20:47 -0500 Original-Received: from relay5-d.mail.gandi.net ([217.70.183.197]:35401) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ihPtF-0004Y5-Qk for help-gnu-emacs@gnu.org; Tue, 17 Dec 2019 22:20:45 -0500 X-Originating-IP: 210.160.37.43 Original-Received: from [10.254.2.136] (unknown [210.160.37.43]) (Authenticated sender: jean.christophe.helary@traduction-libre.org) by relay5-d.mail.gandi.net (Postfix) with ESMTPSA id 1ED381C0004; Wed, 18 Dec 2019 03:20:41 +0000 (UTC) In-Reply-To: <87k16ua2qm.fsf@telefonica.net> X-Mailer: Apple Mail (2.3601.0.10) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 217.70.183.197 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:122059 Archived-At: Thank you Oscar. > On Dec 18, 2019, at 9:36, =C3=93scar Fuentes wrote: >=20 > Jean-Christophe Helary > writes: >=20 >> I'm struggling with what is the ideal "elispy" way of adding contents >> to a file at a given position. >>=20 >> 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) >>=20 >> I thought write-region would do the trick but there are 2 issues with >> it >>=20 >> 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) >>=20 >> 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. >>=20 >> So, I thought of something a bit convoluted: >>=20 >> use a temporary buffer >> insert the contents of the file there >=20 > find-file-noselect takes care of creating the buffer and reading the > contents of the file. Later you must get rid of the buffer. >=20 >> find the point where I want to insert my contents >=20 > use `insert' or `insert-buffer-substring' >=20 >> write all that to the original file >>=20 >> But I thought, it's not like such things don't happen all the time in >> emacs, so there must be a better workflow. >>=20 >> But the reference is incredibly cryptic regarding that and the Intro >> does not address any file i/o at all... >=20 > 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