From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: =?utf-8?Q?=C3=93scar_Fuentes?= Newsgroups: gmane.emacs.help Subject: Re: safe way to add contents to a file ? Date: Wed, 18 Dec 2019 01:36:49 +0100 Message-ID: <87k16ua2qm.fsf@telefonica.net> References: <0FE61EAF-672C-4348-8107-F4C3D176FCF4@traduction-libre.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="97540"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Dec 18 01:37:25 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 1ihNLB-000PH8-7B for geh-help-gnu-emacs@m.gmane.org; Wed, 18 Dec 2019 01:37:25 +0100 Original-Received: from localhost ([::1]:47730 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihNLA-0004JW-2q for geh-help-gnu-emacs@m.gmane.org; Tue, 17 Dec 2019 19:37:24 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:52403) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihNKn-0004IQ-GJ for help-gnu-emacs@gnu.org; Tue, 17 Dec 2019 19:37:03 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihNKm-0005kO-GT for help-gnu-emacs@gnu.org; Tue, 17 Dec 2019 19:37:01 -0500 Original-Received: from 195-159-176-226.customer.powertech.no ([195.159.176.226]:49796 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ihNKm-0005iv-6w for help-gnu-emacs@gnu.org; Tue, 17 Dec 2019 19:37:00 -0500 Original-Received: from list by blaine.gmane.org with local (Exim 4.89) (envelope-from ) id 1ihNKj-000OmO-Og for help-gnu-emacs@gnu.org; Wed, 18 Dec 2019 01:36:57 +0100 X-Injected-Via-Gmane: http://gmane.org/ Cancel-Lock: sha1:D6E1OnQ58vkvD7CqC9bnFiKPpg8= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 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:122057 Archived-At: Jean-Christophe Helary 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).