From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Aaron Zeng Newsgroups: gmane.emacs.bugs Subject: bug#64210: Allow ff-other-file-alist to specify which file to create Date: Wed, 21 Jun 2023 11:50:46 -0400 Message-ID: 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="22711"; mail-complaints-to="usenet@ciao.gmane.io" Cc: app-emacs-dev@janestreet.com To: 64210@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Jun 21 17:51:26 2023 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 1qC07C-0005aZ-Dq for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 21 Jun 2023 17:51:26 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qC06t-0006xu-7U; Wed, 21 Jun 2023 11:51:07 -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 1qC06p-0006xS-D5 for bug-gnu-emacs@gnu.org; Wed, 21 Jun 2023 11:51:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qC06o-0004Jk-O2 for bug-gnu-emacs@gnu.org; Wed, 21 Jun 2023 11:51:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qC06o-0002QT-JU for bug-gnu-emacs@gnu.org; Wed, 21 Jun 2023 11:51:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Aaron Zeng Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 21 Jun 2023 15:51:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 64210 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.16873626589313 (code B ref -1); Wed, 21 Jun 2023 15:51:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 21 Jun 2023 15:50:58 +0000 Original-Received: from localhost ([127.0.0.1]:34099 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qC06j-0002Q9-D8 for submit@debbugs.gnu.org; Wed, 21 Jun 2023 11:50:58 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:43280) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qC06g-0002Q0-NF for submit@debbugs.gnu.org; Wed, 21 Jun 2023 11:50:55 -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 1qC06d-0006wO-Nm for bug-gnu-emacs@gnu.org; Wed, 21 Jun 2023 11:50:52 -0400 Original-Received: from mxout5.mail.janestreet.com ([64.215.233.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qC06b-0004H5-Qx for bug-gnu-emacs@gnu.org; Wed, 21 Jun 2023 11:50:51 -0400 Received-SPF: pass client-ip=64.215.233.18; envelope-from=azeng@janestreet.com; helo=mxout5.mail.janestreet.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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:263834 Archived-At: --=-=-= Content-Type: text/plain Tags: patch Tags: patch This patch adds an optional third element to the lists that make ff-other-file-alist. If this third element is present, it is used as the extension for a new file, when none of the other options were found. Previously, this was forced to be the first extension in the list, which unnecessarily coupled search order and file-to-create. The specific use case I have in mind is for the following scenario in OCaml source files: it is common to have the following three files for a module "Foo": foo.ml, foo.mli, and foo_intf.ml. I would like to make ff-find-other-file cycle between these files if all of them exist, or to create a new file if they do not. However, it is also normal for only foo.ml and foo.mli to exist, and foo_intf.ml should not be created if it is not necessary. At the moment, if the user is looking at foo.mli, there is no way to program ff-other-file-alist to say, "go to foo_intf.ml if it exists, otherwise foo.ml, but if neither exist, create foo.ml". This patch makes it possible to specify what file to create, if it is not the first file searched for. The manual has not yet been updated for this change, since I wanted to get some feedback on the patch itself first. Happy to make the changes in the appropriate places if the patch looks reasonable. In GNU Emacs 28.2 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo version 1.15.12, Xaw scroll bars) of 2023-02-09 built on igm-qws-u12685a Repository revision: 739b5d0e52d83ec567bd61a5a49ac0e93e0eb469 Repository branch: HEAD Windowing system distributor 'The X.Org Foundation', version 11.0.12011000 System Description: CentOS Linux 7 (Core) Configured using: 'configure --with-x-toolkit=lucid --without-gpm --without-gconf --without-selinux --without-imagemagick --with-modules --with-gif=no --with-cairo --with-rsvg --prefix=/j/office/app/emacs/builds/28.2-20230209-135825' --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Allow-ff-other-file-alist-functions-to-be-lambdas.patch >From 6aba92d369183035cd86cc56bea5104dfc1816d4 Mon Sep 17 00:00:00 2001 From: "Aaron L. Zeng" Date: Mon, 12 Jun 2023 18:43:38 -0400 Subject: [PATCH 1/2] Allow ff-other-file-alist functions to be lambdas * lisp/find-file.el (ff-other-file-alist, ff-find-the-other-file): allow functions in the alist to be lambdas, by just checking `functionp'. Also make a minor docstring correction to `ff-other-file-alist'. --- lisp/find-file.el | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lisp/find-file.el b/lisp/find-file.el index 05459c3643d..e4c86ecdfee 100644 --- a/lisp/find-file.el +++ b/lisp/find-file.el @@ -215,10 +215,10 @@ matching extension or name (e.g., `.cc' yields `.hh'). This alist should be set by the major mode. -Note: if an element of the alist names a FUNCTION as its cdr, that -function must return a non-nil list of file-names. It cannot -return nil, nor can it signal in any way a failure to find a suitable -list of file names." +Note: if an element of the alist names a FUNCTION as its cadr, +that function must return a non-nil list of file-names. It +cannot return nil, nor can it signal in any way a failure to find +a suitable list of file names." :type '(choice (repeat (list regexp (choice (repeat string) function))) symbol)) @@ -469,7 +469,7 @@ If optional IN-OTHER-WINDOW is non-nil, find the file in another window." ;; if we have a function to generate new names, ;; invoke it with the name of the current file - (if (and (atom action) (fboundp action)) + (if (functionp action) (setq suffixes (funcall action (ff-buffer-file-name)) match (cons (car match) (list suffixes)) stub nil -- 2.39.3 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0002-Allow-ff-other-file-alist-to-specify-the-extension-f.patch >From 13d1881e03ea838c5448da6b57678c7ba4c48704 Mon Sep 17 00:00:00 2001 From: "Aaron L. Zeng" Date: Tue, 13 Jun 2023 17:07:40 -0400 Subject: [PATCH 2/2] Allow ff-other-file-alist to specify the extension for a new file * lisp/find-file.el (ff-other-file-alist, ff-find-the-other-file): Add an optional third element to the lists that make ff-other-file-alist. If this third element is present, it is used as the extension for a new file, when none of the other options were found. Previously, this was forced to be the first extension in the list, which unnecessarily coupled search order and file-to-create. --- lisp/find-file.el | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lisp/find-file.el b/lisp/find-file.el index e4c86ecdfee..807f46e637c 100644 --- a/lisp/find-file.el +++ b/lisp/find-file.el @@ -192,7 +192,7 @@ filename that EXTRACT returned." The value could be an alist or a symbol whose value is an alist. Each element of the alist has the form - (REGEXP (EXTENSION...)) + (REGEXP (EXTENSION...) [CREATE-EXTENSION]) where REGEXP is the regular expression matching a file's extension, and EXTENSIONs is the list of literal file-name extensions to search @@ -202,7 +202,7 @@ through each directory specified in `ff-search-directories'. Alist elements can also be of the form - (REGEXP FUNCTION) + (REGEXP FUNCTION [CREATE-EXTENSION]) where FUNCTION is a function of one argument, the current file's name, that returns the list of possible names of the corresponding files, with @@ -210,8 +210,11 @@ or without leading directories. Note the difference: FUNCTION returns the list of file names, not their extensions. This is for the case when REGEXP is not enough to determine the file name of the other file. -If a file is not found, a new one is created with the first -matching extension or name (e.g., `.cc' yields `.hh'). +If a file is not found, a new one is created with the first matching +extension or name (e.g., `.cc' yields `.hh'). If [CREATE-EXTENSION] +is present, it is used instead of the first extension. If +[CREATE-EXTENSION] is a function, it is called with a single argument, +the current file's name, and the name it returns is used instead. This alist should be set by the major mode. @@ -219,7 +222,13 @@ Note: if an element of the alist names a FUNCTION as its cadr, that function must return a non-nil list of file-names. It cannot return nil, nor can it signal in any way a failure to find a suitable list of file names." - :type '(choice (repeat (list regexp (choice (repeat string) function))) + :type '(choice (repeat (list regexp + (choice :tag "Extensions to try" + (repeat string) + function) + (choice :tag "Extension to create" + (list :inline t :tag "string" string) + (list :inline t :tag "function" function)))) symbol)) (defcustom ff-search-directories 'cc-search-directories @@ -465,7 +474,11 @@ If optional IN-OTHER-WINDOW is non-nil, find the file in another window." ;; otherwise, suffixes contains what we need (setq suffixes (car (cdr match)) action (car (cdr match)) - found nil) + found nil + default-name (caddr match)) + + (when (functionp default-name) + (setq default-name (funcall default-name (ff-buffer-file-name)))) ;; if we have a function to generate new names, ;; invoke it with the name of the current file @@ -473,7 +486,7 @@ If optional IN-OTHER-WINDOW is non-nil, find the file in another window." (setq suffixes (funcall action (ff-buffer-file-name)) match (cons (car match) (list suffixes)) stub nil - default-name (car suffixes)) + default-name (or default-name (car suffixes))) ;; otherwise build our filename stub (cond @@ -493,7 +506,7 @@ If optional IN-OTHER-WINDOW is non-nil, find the file in another window." ;; if we find nothing, we should try to get a file like this one (setq default-name - (concat stub (car (car (cdr match)))))) + (concat stub (or default-name (car (car (cdr match))))))) ;; do the real work - find the file (setq found -- 2.39.3 --=-=-=--