From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: =?utf-8?Q?=C3=93scar_Fuentes?= Newsgroups: gmane.emacs.help Subject: Re: safe way to add contents to a file ? Date: Sun, 22 Dec 2019 15:37:32 +0100 Message-ID: <877e2ofmtv.fsf@telefonica.net> 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> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="63435"; 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 15:37:56 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 1ij2Ml-000GMc-8v for geh-help-gnu-emacs@m.gmane.org; Sun, 22 Dec 2019 15:37:55 +0100 Original-Received: from localhost ([::1]:48128 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ij2Mk-00035s-5X for geh-help-gnu-emacs@m.gmane.org; Sun, 22 Dec 2019 09:37:54 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55532) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ij2Ma-00035l-4A for help-gnu-emacs@gnu.org; Sun, 22 Dec 2019 09:37:45 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ij2MY-0002Qw-US for help-gnu-emacs@gnu.org; Sun, 22 Dec 2019 09:37:43 -0500 Original-Received: from 195-159-176-226.customer.powertech.no ([195.159.176.226]:40164 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ij2MY-0002GC-Kk for help-gnu-emacs@gnu.org; Sun, 22 Dec 2019 09:37:42 -0500 Original-Received: from list by blaine.gmane.org with local (Exim 4.89) (envelope-from ) id 1ij2MV-000G62-M8 for help-gnu-emacs@gnu.org; Sun, 22 Dec 2019 15:37:39 +0100 X-Injected-Via-Gmane: http://gmane.org/ Cancel-Lock: sha1:BVjqF5higmJVZf4qT7RjUmFXmFA= 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:122082 Archived-At: Jean-Christophe Helary writes: > 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... `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: (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)) You can also cache the result of `search-forward', thus avoiding repeating it: (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)) A `let' would be nicer than a `setq'. Fixing that is left as an exercise for the reader.