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 12:14:04 +0900 Message-ID: <3C9F1A1A-551E-411B-91B2-CB31B51094B4@traduction-libre.org> References: <0FE61EAF-672C-4348-8107-F4C3D176FCF4@traduction-libre.org> <87k16ua2qm.fsf@telefonica.net> <2C8281A0-290F-4505-8495-4992E8E0B82B@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="133460"; 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 04:14:34 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 1iirhR-000YbO-K0 for geh-help-gnu-emacs@m.gmane.org; Sun, 22 Dec 2019 04:14:33 +0100 Original-Received: from localhost ([::1]:43906 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iirhP-0003N0-Pr for geh-help-gnu-emacs@m.gmane.org; Sat, 21 Dec 2019 22:14:31 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:57910) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iirhA-0003Mr-JI for help-gnu-emacs@gnu.org; Sat, 21 Dec 2019 22:14:17 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iirh8-0001Q6-V6 for help-gnu-emacs@gnu.org; Sat, 21 Dec 2019 22:14:16 -0500 Original-Received: from relay4-d.mail.gandi.net ([217.70.183.196]:42797) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iirh8-0001Mv-Kr for help-gnu-emacs@gnu.org; Sat, 21 Dec 2019 22:14:14 -0500 X-Originating-IP: 182.251.133.189 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 relay4-d.mail.gandi.net (Postfix) with ESMTPSA id 4BBDBE0002 for ; Sun, 22 Dec 2019 03:14:10 +0000 (UTC) In-Reply-To: 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.183.196 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:122080 Archived-At: > 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 ;-) How is it supposed to be shorter ? (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))) (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))) 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. >> (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)) But here, the code would go on inserting the text in a position that's = not correct, right ? Isn't it possible to catch the error around the search ? The only = language I know, AppleScript, has a try body on error do something end try block which seems equivalent to "condition-case" but it seems that I'd = have to use a progn to have the equivalent: (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))) And then the message is not displayed. I just get a "t" which I guess = corresponds to the successful (kill-buffer)... And if I put the kill-buffer inside the progn, then I'm left with an = open buffer that's not relevant anymore... Jean-Christophe Helary ----------------------------------------------- http://mac4translators.blogspot.com @brandelune