emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/compat 684ada93bb: compat-29: Add directory-abbrev-appl


From: ELPA Syncer
Subject: [elpa] externals/compat 684ada93bb: compat-29: Add directory-abbrev-apply and directory-abbrev-make-regexp
Date: Tue, 14 Mar 2023 12:57:30 -0400 (EDT)

branch: externals/compat
commit 684ada93bb2e55366bbb48278d344025cfb69b75
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>

    compat-29: Add directory-abbrev-apply and directory-abbrev-make-regexp
---
 NEWS.org        |  5 +++++
 compat-29.el    | 25 +++++++++++++++++++++++++
 compat-tests.el | 11 +++++++++++
 compat.texi     | 13 +++++++++++++
 4 files changed, 54 insertions(+)

diff --git a/NEWS.org b/NEWS.org
index 1cbc7796ac..84c89b6d6b 100644
--- a/NEWS.org
+++ b/NEWS.org
@@ -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 fee1f5c5cc..c74a866ced 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 d67365d35c..8b93c704b4 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 dbb90b6d4c..5fe7a92022 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]