diff options
| author | Daniel Mendler <mail@daniel-mendler.de> | 2023-03-14 17:45:32 +0100 |
|---|---|---|
| committer | Daniel Mendler <mail@daniel-mendler.de> | 2023-03-14 17:45:32 +0100 |
| commit | 684ada93bb2e55366bbb48278d344025cfb69b75 (patch) | |
| tree | acf5844cfe82159e5f29cb01bcb27bb8f491efe9 | |
| parent | 32e737b145fef05105e2bad4ca34ca41072e9569 (diff) | |
compat-29: Add directory-abbrev-apply and directory-abbrev-make-regexp
| -rw-r--r-- | NEWS.org | 5 | ||||
| -rw-r--r-- | compat-29.el | 25 | ||||
| -rw-r--r-- | compat-tests.el | 11 | ||||
| -rw-r--r-- | compat.texi | 13 |
4 files changed, 54 insertions, 0 deletions
@@ -2,6 +2,11 @@ #+link: compat-gh https://github.com/emacs-compat/compat/issues/ #+options: toc:nil num:nil author:nil +* Development + +- compat-29: Add ~directory-abbrev-apply~. +- compat-29: Add ~directory-abbrev-make-regexp~. + * Release of "Compat" Version 29.1.4.0 - compat-27: Drop obsolete ~compat-call dired-get-marked-files~. diff --git a/compat-29.el b/compat-29.el index fee1f5c..c74a866 100644 --- a/compat-29.el +++ b/compat-29.el @@ -623,6 +623,31 @@ The variable list SPEC is the same as in `if-let*'." ;;;; Defined in files.el +(compat-defun directory-abbrev-make-regexp (directory) ;; <compat-tests:directory-abbrev-make-regexp> + "Create a regexp to match DIRECTORY for `directory-abbrev-alist'." + (let ((regexp + ;; We include a slash at the end, to avoid spurious + ;; matches such as `/usr/foobar' when the home dir is + ;; `/usr/foo'. + (concat "\\`" (regexp-quote directory) "\\(/\\|\\'\\)"))) + ;; The value of regexp could be multibyte or unibyte. In the + ;; latter case, we need to decode it. + (if (multibyte-string-p regexp) + regexp + (decode-coding-string regexp + (if (eq system-type 'windows-nt) + 'utf-8 + locale-coding-system))))) + +(compat-defun directory-abbrev-apply (filename) ;; <compat-tests:directory-abbrev-apply> + "Apply the abbreviations in `directory-abbrev-alist' to FILENAME. +Note that when calling this, you should set `case-fold-search' as +appropriate for the filesystem used for FILENAME." + (dolist (dir-abbrev directory-abbrev-alist filename) + (when (string-match (car dir-abbrev) filename) + (setq filename (concat (cdr dir-abbrev) + (substring filename (match-end 0))))))) + (compat-defun file-name-split (filename) ;; <compat-tests:file-name-split> "Return a list of all the components of FILENAME. On most systems, this will be true: diff --git a/compat-tests.el b/compat-tests.el index d67365d..8b93c70 100644 --- a/compat-tests.el +++ b/compat-tests.el @@ -1500,6 +1500,17 @@ (delete-file (file-name-concat dir "file")) (should (directory-empty-p dir)))) +(ert-deftest directory-abbrev-apply () + (let ((directory-abbrev-alist + (list + (cons (directory-abbrev-make-regexp "/long/path/to/foo") "foo:") + (cons (directory-abbrev-make-regexp "/long/path/to/bar") "bar:")))) + (should-equal (directory-abbrev-apply "/long/path/to/foo/file") "foo:file") + (should-equal (directory-abbrev-apply "/long/path/to/bar/file") "bar:file"))) + +(ert-deftest directory-abbrev-make-regexp () + (should-equal (directory-abbrev-make-regexp "/home/user/") "\\`/home/user/\\(/\\|\\'\\)")) + (ert-deftest make-empty-file () (ert-with-temp-directory dir (let ((file (file-name-concat dir "file"))) diff --git a/compat.texi b/compat.texi index dbb90b6..5fe7a92 100644 --- a/compat.texi +++ b/compat.texi @@ -2737,6 +2737,19 @@ time comparisons are limited to calls with the same tag. @xref{File Attributes,,,elisp}. @end defun +@c based on lisp/files.el +@defun directory-abbrev-make-regexp directory +Create a regexp to match @var{directory} for +@code{directory-abbrev-alist}. +@end defun + +@c based on lisp/files.el +@defun directory-abbrev-apply filename +Apply the abbreviations in @code{directory-abbrev-alist} to @var{filename}. +Note that when calling this, you should set @code{case-fold-search} as +appropriate for the filesystem used for @var{filename}. +@end defun + @c based on lisp/keymap.el @defun key-valid-p keys Say whether @var{keys} is a valid key. A key is a string consisting of |
