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: Mon, 23 Dec 2019 07:18:58 +0900 Message-ID: <4F3F7D8E-870C-4C9B-8B7D-2B05CE1B87A8@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> <55F7CE36-37C4-49BC-95F2-E91DE6C4628F@traduction-libre.org> <877e2ofmtv.fsf@telefonica.net> Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3608.40.2.2.4\)) 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="537"; 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 23:19:27 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 1ij9ZN-0018EP-Lb for geh-help-gnu-emacs@m.gmane.org; Sun, 22 Dec 2019 23:19:26 +0100 Original-Received: from localhost ([::1]:51130 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ij9ZL-00078J-VE for geh-help-gnu-emacs@m.gmane.org; Sun, 22 Dec 2019 17:19:23 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:46871) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ij9Z9-000787-Eb for help-gnu-emacs@gnu.org; Sun, 22 Dec 2019 17:19:12 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ij9Z5-00084t-M6 for help-gnu-emacs@gnu.org; Sun, 22 Dec 2019 17:19:10 -0500 Original-Received: from relay11.mail.gandi.net ([217.70.178.231]:36391) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ij9Z4-0007vV-U2 for help-gnu-emacs@gnu.org; Sun, 22 Dec 2019 17:19:07 -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 88453100006 for ; Sun, 22 Dec 2019 22:19:02 +0000 (UTC) In-Reply-To: <877e2ofmtv.fsf@telefonica.net> 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:122085 Archived-At: Oscar, Stephan, Thank you *so* much for your comments. Regarding `user-error' or `error', neither the doc strings nor the = reference mention that the execution is stopped, I discovered that = eventually but that was confusing. Am I missing something ? Oscar, you say that a `let' would be "nicer" than a `setq', is it = because `let' is local to the body that follows, so that I don't have to = "polute" my top level with locally used values ? Stephan, regarding `save-current-buffer' vs `with-file-noselect', the = former saves the buffer that is current so I need to use `set-buffer' to = set the current buffer, but `with-file-noselect' sets the current buffer = to the buffer associated with the file, hence `set-buffer' is redundant = there, right ? `unwind-protect' is pretty cool ! I guess it can be used to easily = handle errors without bothering about error messages. The doc strings = are very clear, but the reference is totally confusing: "The =E2=80=98unwind-protect=E2=80=99 construct is essential whenever = you temporarily put a data structure in an inconsistent state; it permits you to make the data consistent again in the event of an error or throw. (Another more specific cleanup construct that is used only for changes in buffer contents is the atomic change group; *note Atomic Changes::.)" I would never think of using that form for such a trivial issue. Why is = the writing so obfuscated ? Jean-Christophe=20 > On Dec 22, 2019, at 23:37, =C3=93scar Fuentes wrote: >=20 > Jean-Christophe Helary > writes: >=20 >> Here is my latest version... >>=20 >>=20 >> (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))) >>=20 >> Really not sure if I'm going in the right direction. Plus, for some >> reason, the buffer is not killed even after an error... >=20 > `user-error' (which is a variant of `error') stops the execution, so > `kill-buffer' is never executed. Either put `kill-buffer' before > `user-error' or do not use `user-error', like this: >=20 > (defun myInsert4 (myText myMarker myFile) > (save-current-buffer (find-file-noselect myFile)) > (goto-char (point-min)) > (if (not (search-forward myMarker nil t)) > (message "%s was not found" myMarker) > (progn > (goto-char (point-min)) > (goto-char (- (search-forward myMarker) (length myMarker))) > (insert myText) > (indent-region (point-min) (point-max)) > (save-buffer))) > (kill-buffer)) >=20 > You can also cache the result of `search-forward', thus avoiding > repeating it: >=20 > (defun myInsert4 (myText myMarker myFile) > (save-current-buffer (find-file-noselect myFile)) > (goto-char (point-min)) > (setq p (search-forward myMarker nil t)) > (if (not p) > (message "%s was not found" myMarker) > (progn > (goto-char (- p (length myMarker))) > (insert myText) > (indent-region (point-min) (point-max)) > (save-buffer))) > (kill-buffer)) >=20 > A `let' would be nicer than a `setq'. Fixing that is left as an = exercise > for the reader. Jean-Christophe Helary ----------------------------------------------- http://mac4translators.blogspot.com @brandelune