unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Björn Bidar via Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Stefan Kangas <stefankangas@gmail.com>
Cc: Eli Zaretskii <eliz@gnu.org>, acorallo@gnu.org, 74946@debbugs.gnu.org
Subject: bug#74946: [PATCH] * lisp/files.el (auto-mode-alist): Include gdbinit too
Date: Fri, 20 Dec 2024 03:33:18 +0200	[thread overview]
Message-ID: <46947.5245032058$1734658468@news.gmane.org> (raw)
In-Reply-To: <CADwFkmm-erfm5LW9cx1AACceGF7UF8_FUtE=sv6ELF4q_54fvw@mail.gmail.com> (Stefan Kangas's message of "Thu, 19 Dec 2024 17:55:22 -0500")

Stefan Kangas <stefankangas@gmail.com> writes:

> Eli Zaretskii <eliz@gnu.org> writes:
>
>> Btw, if we want to fix this entry, we should perhaps do a more
>> thorough job.  For example, on my system I have files with the
>> following base names:
>>
>>   .gdbinit.in
>>   .gdbinit
>>   _gdbinit (for MS-DOS)
>>   gdb.ini (likewise)
>>   gdbinit
>>   gdbinit.in
>>   SOMETHING-gdbinit
>>   .gdbinit.loader
>>   gdbinit-history.exp (not a GDB init file)
>>   gdbinit.5 (likewise)
>>   gdbinit.c (likewise)
>>   .gdbinit.py.in (likewise)
>>
>> Should we improve the regexp to DTRT for those additional files, but
>> without false positives?
>
> Maybe something like the below patch?
>
> BTW, should the .gdbinit.py.in file be in python-mode perhaps?
> And what about gdbinit.5?

That's a man page file.

> From a5a5dcd0452cadfee988a0256c10877835eefcef Mon Sep 17 00:00:00 2001
> From: Stefan Kangas <stefankangas@gmail.com>
> Date: Thu, 19 Dec 2024 23:51:38 +0100
> Subject: [PATCH] Match more gdbinit files
>
> * lisp/files.el (auto-mode-alist): Match more gdbinit files,
> including XDG, and MS-Windows.  Avoid false positives.
> (Bug#74946)
> (set-auto-mode--find-matching-alist-entry): Break out function...
> (set-auto-mode--apply-alist): ...from here.
> * test/lisp/files-tests.el (files-tests-auto-mode-alist): New
> test.
> ---
>  lisp/files.el            | 56 +++++++++++++++++++++++-----------------
>  test/lisp/files-tests.el | 17 ++++++++++++
>  2 files changed, 49 insertions(+), 24 deletions(-)
>
> diff --git a/lisp/files.el b/lisp/files.el
> index 9f13804540b..68ce2e0c273 100644
> --- a/lisp/files.el
> +++ b/lisp/files.el
> @@ -3005,7 +3005,7 @@ auto-mode-alist
>       ;; files, cross-debuggers can use something like
>       ;; .PROCESSORNAME-gdbinit so that the host and target gdbinit files
>       ;; don't interfere with each other.
> -     ("/\\.[a-z0-9-]*gdbinit" . gdb-script-mode)
> +     ("/[._]?[A-Za-z0-9-]*\\(?:gdbinit\\(?:\\.\\(?:ini?\\|loader\\)\\)?\\|gdb\\.ini\\)\\'" . gdb-script-mode)

.gdbinit.loader seems to be Mozilla thing. It might be better to either
let users manually set the mode for these files or allow for text to be
after .gdbinit. 

>       ;; GDB 7.5 introduced OBJFILE-gdb.gdb script files; e.g. a file
>       ;; named 'emacs-gdb.gdb', if it exists, will be automatically
>       ;; loaded when GDB reads an objfile called 'emacs'.
> @@ -3404,6 +3404,35 @@ magic-mode-regexp-match-limit
>    "Upper limit on `magic-mode-alist' regexp matches.
>  Also applies to `magic-fallback-mode-alist'.")
>  
> +(defun set-auto-mode--find-matching-alist-entry (alist name case-insensitive)
> +  "Find first matching entry in ALIST for file NAME.
> +
> +If CASE-INSENSITIVE, the file system of file NAME is case-insensitive."
> +  (let (mode)
> +    (while name
> +      (setq mode
> +            (if case-insensitive
> +                ;; Filesystem is case-insensitive.
> +                (let ((case-fold-search t))
> +                  (assoc-default name alist 'string-match))
> +              ;; Filesystem is case-sensitive.
> +              (or
> +               ;; First match case-sensitively.
> +               (let ((case-fold-search nil))
> +                 (assoc-default name alist 'string-match))
> +               ;; Fallback to case-insensitive match.
> +               (and auto-mode-case-fold
> +                    (let ((case-fold-search t))
> +                      (assoc-default name alist 'string-match))))))
> +      (if (and mode
> +               (not (functionp mode))
> +               (consp mode)
> +               (cadr mode))
> +          (setq mode (car mode)
> +                name (substring name 0 (match-beginning 0)))
> +        (setq name nil)))
> +    mode))
> +

This function looks very useful for applications with preferential
orders of files as in e.g. a .dotfile and .dotconfig/file.
It could also be a generic function.

>  (defun set-auto-mode--apply-alist (alist keep-mode-if-same dir-local)
>    "Helper function for `set-auto-mode'.
>  This function takes an alist of the same form as
> @@ -3425,29 +3454,8 @@ set-auto-mode--apply-alist
>          (when (and (stringp remote-id)
>                     (string-match (regexp-quote remote-id) name))
>            (setq name (substring name (match-end 0))))
> -        (while name
> -          ;; Find first matching alist entry.
> -          (setq mode
> -                (if case-insensitive-p
> -                    ;; Filesystem is case-insensitive.
> -                    (let ((case-fold-search t))
> -                      (assoc-default name alist 'string-match))
> -                  ;; Filesystem is case-sensitive.
> -                  (or
> -                   ;; First match case-sensitively.
> -                   (let ((case-fold-search nil))
> -                     (assoc-default name alist 'string-match))
> -                   ;; Fallback to case-insensitive match.
> -                   (and auto-mode-case-fold
> -                        (let ((case-fold-search t))
> -                          (assoc-default name alist 'string-match))))))
> -          (if (and mode
> -                   (not (functionp mode))
> -                   (consp mode)
> -                   (cadr mode))
> -              (setq mode (car mode)
> -                    name (substring name 0 (match-beginning 0)))
> -            (setq name nil)))
> +        (setq mode (set-auto-mode--find-matching-alist-entry
> +                    alist name case-insensitive-p))
>          (when (and dir-local mode
>                     (not (set-auto-mode--dir-local-valid-p mode)))
>            (message "Ignoring invalid mode `%s'" mode)
> diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el
> index ad54addf06b..0edc78d01e5 100644
> --- a/test/lisp/files-tests.el
> +++ b/test/lisp/files-tests.el
> @@ -1680,6 +1680,23 @@ files-tests--check-shebang
>        (when (eq expected-mode 'sh-base-mode)
>          (should (eq sh-shell expected-dialect))))))
>  
> +(ert-deftest files-tests-auto-mode-alist ()
> +  (cl-flet ((find (file mode &optional case-insensitive)
> +              (eq mode (set-auto-mode--find-matching-alist-entry
> +                        auto-mode-alist (concat "/home/stefank/" file) case-insensitive))))
> +    (should (find ".gdbinit.in" #'gdb-script-mode))
> +    (should (find ".gdbinit" #'gdb-script-mode))
> +    (should (find "_gdbinit" #'gdb-script-mode)) ; for MS-DOS
> +    (should (find "gdb.ini" #'gdb-script-mode))  ; likewise
> +    (should (find "gdbinit" #'gdb-script-mode))
> +    (should (find "gdbinit.in" #'gdb-script-mode))
> +    (should (find "SOMETHING-gdbinit" #'gdb-script-mode))
> +    (should (find ".gdbinit.loader" #'gdb-script-mode))
> +    (should-not (find "gdbinit-history.exp" #'gdb-script-mode)) ; not a GDB init file
> +    (should-not (find "gdbinit.5" #'gdb-script-mode))        ; likewise
> +    (should-not (find "gdbinit.c" #'gdb-script-mode))        ; likewise
> +    (should-not (find ".gdbinit.py.in" #'gdb-script-mode))))       ; likewise
> +
>  (ert-deftest files-tests-auto-mode-interpreter ()
>    "Test that `set-auto-mode' deduces correct modes from shebangs."
>    ;; Straightforward interpreter invocation.





  reply	other threads:[~2024-12-20  1:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-18 14:19 bug#74946: [PATCH] * lisp/files.el (auto-mode-alist): Include gdbinit too Björn Bidar via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-12-19  1:00 ` Stefan Kangas
2024-12-19  8:14   ` Eli Zaretskii
2024-12-19 21:29     ` Björn Bidar via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-12-19 22:55     ` Stefan Kangas
2024-12-20  1:33       ` Björn Bidar via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2024-12-20  7:05       ` Eli Zaretskii
     [not found]     ` <87frmjtl9f.fsf@>
2024-12-20  6:46       ` Eli Zaretskii
2024-12-20  8:07         ` Björn Bidar via Bug reports for GNU Emacs, the Swiss army knife of text editors

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='46947.5245032058$1734658468@news.gmane.org' \
    --to=bug-gnu-emacs@gnu.org \
    --cc=74946@debbugs.gnu.org \
    --cc=acorallo@gnu.org \
    --cc=bjorn.bidar@thaodan.de \
    --cc=eliz@gnu.org \
    --cc=stefankangas@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).