From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.help Subject: Re: safe way to add contents to a file ? Date: Sun, 22 Dec 2019 13:23:29 -0500 Message-ID: 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 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="146894"; 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 Sun Dec 22 19:23:54 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 1ij5tS-000c4s-A3 for geh-help-gnu-emacs@m.gmane.org; Sun, 22 Dec 2019 19:23:54 +0100 Original-Received: from localhost ([::1]:49876 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ij5tR-0004zk-3W for geh-help-gnu-emacs@m.gmane.org; Sun, 22 Dec 2019 13:23:53 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59245) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ij5tE-0004zU-IF for help-gnu-emacs@gnu.org; Sun, 22 Dec 2019 13:23:41 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ij5tD-0004sn-H6 for help-gnu-emacs@gnu.org; Sun, 22 Dec 2019 13:23:40 -0500 Original-Received: from 195-159-176-226.customer.powertech.no ([195.159.176.226]:39224 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ij5tD-0004lg-8C for help-gnu-emacs@gnu.org; Sun, 22 Dec 2019 13:23:39 -0500 Original-Received: from list by blaine.gmane.org with local (Exim 4.89) (envelope-from ) id 1ij5tB-000bjr-54 for help-gnu-emacs@gnu.org; Sun, 22 Dec 2019 19:23:37 +0100 X-Injected-Via-Gmane: http://gmane.org/ Cancel-Lock: sha1:/OVCz18/ed0v3i3kVQkH5oxjeG0= 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:122083 Archived-At: > How is it supposed to be shorter ? [...] > (defun myInsert2 (myText myMarker myFile) > (with-current-buffer > (set-buffer (find-file-noselect myFile)) This `set-buffer` is redundant: (defun myInsert2 (myText myMarker myFile) (with-current-buffer (find-file-noselect myFile) >> (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 ? No (for 2 reasons: the insertion would only in the `else` part of the `if` and the `user-error` immediately terminates the execution of this function). > And if I put the kill-buffer inside the progn, then I'm left with an > open buffer that's not relevant anymore... Cleanup code like this `kill-buffer` should be placed in an `unwind-protect`, so it's executed both for normal exits and for non-local exits: (with-current-buffer (find-file-noselect myFile) (unwind-protect (progn ... do the insertion and stuff) (kill-buffer))) Tho I'd change that code to pass the buffer explicitly to `kill-buffer`, just to make sure there's no risk of killing some unrelated buffer in fringe circumstances: (with-current-buffer (find-file-noselect myFile) (let ((buf (current-buffer))) (unwind-protect (progn ... do the insertion and stuff) (kill-buffer buf)))) -- Stefan