From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Charalampos Mitrodimas Newsgroups: gmane.emacs.bugs Subject: bug#70806: [PATCH] Add rename-file-and-open function Date: Mon, 06 May 2024 18:43:40 +0000 Message-ID: <87cypyn5lv.fsf@posteo.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29854"; mail-complaints-to="usenet@ciao.gmane.io" To: 70806@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon May 06 20:45:14 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1s43Kf-0006zt-Ne for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 06 May 2024 20:45:03 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s43KS-0007Bv-2R; Mon, 06 May 2024 14:44:48 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s43KK-0007B9-Kn for bug-gnu-emacs@gnu.org; Mon, 06 May 2024 14:44:41 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s43KI-0004qn-1m for bug-gnu-emacs@gnu.org; Mon, 06 May 2024 14:44:38 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1s43Kg-0005jc-B5 for bug-gnu-emacs@gnu.org; Mon, 06 May 2024 14:45:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Charalampos Mitrodimas Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 06 May 2024 18:45:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 70806 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.171502106321945 (code B ref -1); Mon, 06 May 2024 18:45:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 6 May 2024 18:44:23 +0000 Original-Received: from localhost ([127.0.0.1]:39601 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s43K2-0005hq-N6 for submit@debbugs.gnu.org; Mon, 06 May 2024 14:44:23 -0400 Original-Received: from lists.gnu.org ([2001:470:142::17]:54580) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s43Jz-0005hh-Lf for submit@debbugs.gnu.org; Mon, 06 May 2024 14:44:21 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s43JV-0006qU-K7 for bug-gnu-emacs@gnu.org; Mon, 06 May 2024 14:43:49 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s43JR-0003jn-S2 for bug-gnu-emacs@gnu.org; Mon, 06 May 2024 14:43:48 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 9195B240027 for ; Mon, 6 May 2024 20:43:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1715021021; bh=RRwDT7sowPUqD56B5yoZ5+oRvYCKgi3+Ew5MLlrJpnU=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=GUJgyvxlVybkAKAVUtOXWCXnEuJeydYXeo3BQizsOwi7vH9iXkqmRVtWIzLuGoOD4 5yk3djgBC7aAu0NnX2KlFXfZQpDS0RcNBBYMu9mHVm0JtGhysn43ENnbrP681VaYmA IjVQ3f21gRP5GkUyLrHzaroSH3B5RSs3uhnbwAyD/b+FupjaGu+mmnwI/6n3RllIXR S09hW3lIhd8RPJnDFTIJveW7RgpEVK6GX6kCLbItdR0zOE22e5hddS04oCVRUAIt2p aiJahpOaSjzP2IJYA3PHpKB86n537hZ1Jzr1Ok+w+4ILMTfsOXyNOC2Z80zyTEUG1W ePG4G1+JMZeoQ== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4VY9KS6zMtz6tw4 for ; Mon, 6 May 2024 20:43:40 +0200 (CEST) Received-SPF: pass client-ip=185.67.36.65; envelope-from=charmitro@posteo.net; helo=mout01.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:284605 Archived-At: --=-=-= Content-Type: text/plain Tags: patch Hi, The new function is similar to "rename-file", but it automatically opens the file once it's renamed (or throws a relevant error). I'm not sure if this addition is necessary, but I've had it for so many years that I thought it might be useful to have it in Emacs by default. I'm also not sure if it's in the correct position within lisp/files.el. In any case, I'm submitting this patch for consideration. -- C. Mitrodimas In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.38, cairo version 1.16.0) of 2024-05-06 built on Debian-bookworm-latest-amd64-base Repository revision: 71395b7888f4d7b64be36a8cedb5b2b8c4819259 Repository branch: master System Description: Debian GNU/Linux 12 (bookworm) Configured using: 'configure --with-native-compilation' --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Add-rename-file-and-open-function.patch >From 6c91170beb549240325e04ed1f08de833c6bfd88 Mon Sep 17 00:00:00 2001 From: Charalampos Mitrodimas Date: Mon, 6 May 2024 21:31:08 +0300 Subject: [PATCH] Add rename-file-and-open function Introduce rename-file-and-open, a new function that renames a file and opens the renamed file immediately. This function was developed to make the renaming of a file more automated: when using rename-file, the renamed file is not automatically opened, requiring an additional step to open it manually. The rename-file-and-open function takes two required arguments: - file: the source file name (a string) - newname: the destination file name (a string) - ok-if-already-exists: control the behavior when the destination file already exists It calls rename-file to perform the actual renaming, handling any file-error that may occur by signaling a user-error with an appropriate error message. * lisp/files.el (rename-file-and-open): New function to rename a file and open it, since `rename-file' doesn't open the renamed file. --- lisp/files.el | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lisp/files.el b/lisp/files.el index c24e48e3db2..ded6e1b130f 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -7380,6 +7380,25 @@ rename-auto-save-file (recent-auto-save-p)) (rename-file osave buffer-auto-save-file-name t)))) +(defun rename-file-and-open (file newname &optional ok-if-already-exists) + "Rename FILE as NEWNAME and open the renamed file. +Both args must be strings. + +If file has names other than FILE, it continues to have those names. +If NEWNAME is a directory name, rename FILE to a like-named file under +NEWNAME. For NEWNAME to be recognized as a directory name, it should +end in a slash. + +Signal a `file-already-exists' error if a file NEWNAME already exists +unless optional third argument OK-IF-ALREADY-EXISTS is non-nil. +An integer third arg means request confirmation if NEWNAME already exists." + (interactive "fRename file: \nGRename to file: \np") + (condition-case err + (rename-file file newname ok-if-already-exists) + (file-error + (user-error "Failed to rename file: %s" (error-message-string err)))) + (find-file newname)) + (defun make-auto-save-file-name () "Return file name to use for auto-saves of current buffer. Does not consider `auto-save-visited-file-name' as that variable is checked -- 2.39.3 (Apple Git-146) --=-=-=--