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: Sun, 22 Dec 2019 13:42:23 +0900 Message-ID: <55F7CE36-37C4-49BC-95F2-E91DE6C4628F@traduction-libre.org> References: <0FE61EAF-672C-4348-8107-F4C3D176FCF4@traduction-libre.org> <87k16ua2qm.fsf@telefonica.net> <2C8281A0-290F-4505-8495-4992E8E0B82B@traduction-libre.org> <3C9F1A1A-551E-411B-91B2-CB31B51094B4@traduction-libre.org> Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3608.40.2.2.4\)) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="197991"; 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 Sun Dec 22 05:42:49 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 1iit4q-000pND-Bi for geh-help-gnu-emacs@m.gmane.org; Sun, 22 Dec 2019 05:42:48 +0100 Original-Received: from localhost ([::1]:44362 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iit4p-0008BR-5c for geh-help-gnu-emacs@m.gmane.org; Sat, 21 Dec 2019 23:42:47 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55272) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iit4c-0008BL-Do for help-gnu-emacs@gnu.org; Sat, 21 Dec 2019 23:42:39 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iit4b-0006uo-6r for help-gnu-emacs@gnu.org; Sat, 21 Dec 2019 23:42:34 -0500 Original-Received: from relay11.mail.gandi.net ([217.70.178.231]:52751) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iit4a-0006rl-Tv for help-gnu-emacs@gnu.org; Sat, 21 Dec 2019 23:42:33 -0500 Original-Received: from [172.20.10.2] (KD182251133189.au-net.ne.jp [182.251.133.189]) (Authenticated sender: jean.christophe.helary@traduction-libre.org) by relay11.mail.gandi.net (Postfix) with ESMTPSA id B9492100003 for ; Sun, 22 Dec 2019 04:42:28 +0000 (UTC) In-Reply-To: <3C9F1A1A-551E-411B-91B2-CB31B51094B4@traduction-libre.org> X-Mailer: Apple Mail (2.3608.40.2.2.4) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 217.70.178.231 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:122081 Archived-At: > On Dec 22, 2019, at 12:14, Jean-Christophe Helary = wrote: >=20 >=20 >=20 >> On Dec 21, 2019, at 1:00, Stefan Monnier = wrote: >>=20 >>> (setq myText "bla") >>> (setq myMarker "") >>> (setq myFile "/path/to/test.xml") >>=20 >> Being at top-level these aren't just setting the vars but defining = them, >> so should use `defvar` or `defconst`. >>=20 >>> (defun myInsert (myText myMarker myFile) >>> (save-current-buffer >>> (set-buffer (find-file-noselect myFile)) >>=20 >> `with-current-buffer` does the same, but shorter ;-) >=20 > How is it supposed to be shorter ? >=20 > (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) > (indent-region (point-min) (point-max)) > (save-buffer) > (kill-buffer))) >=20 > (defun myInsert2 (myText myMarker myFile) > (with-current-buffer > (set-buffer (find-file-noselect myFile)) > (goto-char (point-min)) > (goto-char (- (search-forward myMarker) (length myMarker))) > (insert myText) > (indent-region (point-min) (point-max)) > (save-buffer) > (kill-buffer))) >=20 >=20 > Honestly, as I read the respective doc strings and reference parts = again, I'm not even sure why I chose save-current-buffer here anymore, = but *because* there is no explanatory document on how to do proper file = i/o in the Elisp Reference it is hard to make informed decisions, or = even progress without a lot of searching outside the reference. >=20 >>> (goto-char (point-min)) >>> (goto-char (- (search-forward myMarker) (length myMarker))) >>=20 >> If the search fails, this will signal a "low-level" error, and it's >> often useful to replace it with some other behavior (e.g. an error >> message which the user is more likely to understand, or some other >> behavior), so it's more idiomatic to do something like: >>=20 >> (goto-char (point-min)) >> (if (not (search-forward myMarker nil t)) >> (user-error "Can't find foo bar in your fine file") >> (goto-char (match-beginning 0)) >=20 > But here, the code would go on inserting the text in a position that's = not correct, right ? >=20 > Isn't it possible to catch the error around the search ? The only = language I know, AppleScript, has a >=20 > try > body > on error > do something > end try >=20 > block which seems equivalent to "condition-case" but it seems that I'd = have to use a progn to have the equivalent: >=20 > (defun myInsert3 (myText myMarker myFile) > (save-current-buffer > (set-buffer (find-file-noselect myFile)) > (goto-char (point-min)) > (condition-case nil > (progn > (goto-char (- (search-forward myMarker) (length myMarker))) > (insert myText) > (indent-region (point-min) (point-max)) > (save-buffer)) > (error (format "%s was not found" myMarker))) > (kill-buffer))) >=20 > And then the message is not displayed. I just get a "t" which I guess = corresponds to the successful (kill-buffer)... >=20 > And if I put the kill-buffer inside the progn, then I'm left with an = open buffer that's not relevant anymore... Here is my latest version... (defun myInsert4 (myText myMarker myFile) (save-current-buffer (set-buffer (find-file-noselect myFile)) (goto-char (point-min)) (if (not (search-forward myMarker nil t)) (progn (user-error (format "%s was not found" myMarker)) (kill-buffer)) (progn (goto-char (point-min)) (goto-char (- (search-forward myMarker) (length myMarker))) (insert myText) (indent-region (point-min) (point-max)) (save-buffer))) (kill-buffer))) Really not sure if I'm going in the right direction. Plus, for some = reason, the buffer is not killed even after an error... Jean-Christophe Helary ----------------------------------------------- http://mac4translators.blogspot.com @brandelune