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: Fri, 20 Dec 2019 22:50:29 +0900 Message-ID: <2C8281A0-290F-4505-8495-4992E8E0B82B@traduction-libre.org> 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="198990"; mail-complaints-to="usenet@blaine.gmane.org" To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Dec 20 15:09:18 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 1iiIxy-000pe8-9F for geh-help-gnu-emacs@m.gmane.org; Fri, 20 Dec 2019 15:09:18 +0100 Original-Received: from localhost ([::1]:56514 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iiIxw-0004Dc-JG for geh-help-gnu-emacs@m.gmane.org; Fri, 20 Dec 2019 09:09:16 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36824) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iiIgR-0001Zj-CK for help-gnu-emacs@gnu.org; Fri, 20 Dec 2019 08:51:12 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iiIgP-0003QC-Ir for help-gnu-emacs@gnu.org; Fri, 20 Dec 2019 08:51:10 -0500 Original-Received: from relay3-d.mail.gandi.net ([217.70.183.195]:46751) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iiIgP-0003At-8N for help-gnu-emacs@gnu.org; Fri, 20 Dec 2019 08:51:09 -0500 X-Originating-IP: 182.251.132.170 Original-Received: from [172.20.10.2] (KD182251132170.au-net.ne.jp [182.251.132.170]) (Authenticated sender: jean.christophe.helary@traduction-libre.org) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id C42DA60008 for ; Fri, 20 Dec 2019 13:51:04 +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.195 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:122075 Archived-At: Ok, here is my attempt at this: (setq myText "bla") (setq myMarker "") (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=20 > 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). >=20 >=20 Jean-Christophe Helary ----------------------------------------------- http://mac4translators.blogspot.com @brandelune