branch: elpa/magit
commit 223461b52c35b0f426c053f4c6e7e7637c4a9b73
Author: Jonas Bernoulli <jo...@bernoul.li>
Commit: Jonas Bernoulli <jo...@bernoul.li>

    magit-format-file-function: New option
    
    Closes #5308.
---
 CHANGELOG            |  4 ++++
 docs/magit.org       |  8 ++++++++
 docs/magit.texi      |  8 ++++++++
 lisp/magit-diff.el   | 56 +++++++++++++++++++++++++++++++++++++++++++++-------
 lisp/magit-status.el |  3 ++-
 5 files changed, 71 insertions(+), 8 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 57a2ab052d..0369e9ca5f 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,10 @@
 # -*- mode: org -*-
 * v4.3.1    UNRELEASED
 
+- Added new option ~magit-format-file-function,~ and two functions to
+  optionally prefix file names with icons, with the help of either
+  ~all-the-icons~ or ~nerd-icons~.  #5308
+  
 Bugfixes:
 
 - ~magit-commit-revise~ failed if no arguments were used.  #5306
diff --git a/docs/magit.org b/docs/magit.org
index 4441ca1bba..23d5373620 100644
--- a/docs/magit.org
+++ b/docs/magit.org
@@ -3476,6 +3476,14 @@ that they are available here too.
   allowed here: ~--stat-width~, ~--stat-name-width~,
   ~--stat-graph-width~ and ~--compact-summary~.  Also see [[man:git-diff]]
 
+- User Option: magit-format-file-function ::
+
+  This function is used to format lines representing a file.  It is
+  used for file headings in diffs, in diffstats and for lists of files
+  (such as the untracked files).  Depending on the caller, it receives
+  either three or five arguments; the signature has to be ~(kind file
+  face &optional status orig)~.  KIND is one of ~diff~, ~stat~ and ~list~.
+
 *** Revision Buffer
 
 - User Option: magit-revision-insert-related-refs ::
diff --git a/docs/magit.texi b/docs/magit.texi
index 24f0a425ef..4a547d6c65 100644
--- a/docs/magit.texi
+++ b/docs/magit.texi
@@ -4094,6 +4094,14 @@ the git-diff(1) manpage.
 @end iftex
 @end defopt
 
+@defopt magit-format-file-function
+This function is used to format lines representing a file.  It is
+used for file headings in diffs, in diffstats and for lists of files
+(such as the untracked files).  Depending on the caller, it receives
+either three or five arguments; the signature has to be @code{(kind file
+  face &optional status orig)}.  KIND is one of @code{diff}, @code{stat} and 
@code{list}.
+@end defopt
+
 @anchor{Revision Buffer}
 @subsection Revision Buffer
 
diff --git a/lisp/magit-diff.el b/lisp/magit-diff.el
index 8dc934ac68..5004502972 100644
--- a/lisp/magit-diff.el
+++ b/lisp/magit-diff.el
@@ -85,6 +85,9 @@
 (declare-function magit-smerge-keep-base "magit-apply" ())
 (declare-function magit-smerge-keep-lower "magit-apply" ())
 
+(declare-function all-the-icons-icon-for-file "ext:all-the-icons")
+(declare-function nerd-icons-icon-for-file "ext:nerd-icons")
+
 (eval-when-compile
   (cl-pushnew 'orig-rev eieio--known-slot-names)
   (cl-pushnew 'action-type eieio--known-slot-names)
@@ -322,6 +325,21 @@ and `--compact-summary'.  See the git-diff(1) manpage."
                 (list string)
                 (const :tag "None" nil)))
 
+(defcustom magit-format-file-function #'magit-format-file-default
+  "Function used to format lines representing a file.
+
+This function is used for file headings in diffs, in diffstats and for
+lists of files (such as the untracked files).  Depending on the caller,
+it receives either three or five arguments; the signature has to be
+(kind file face &optional status orig).  KIND is one of `diff', `stat'
+and `list'."
+  :package-version '(magit . "4.3.1")
+  :group 'magit-diff
+  :type `(choice (function-item ,#'magit-format-file-default)
+                 (function-item ,#'magit-format-file-all-the-icons)
+                 (function-item ,#'magit-format-file-nerd-icons)
+                 function))
+
 ;;;; File Diff
 
 (defcustom magit-diff-buffer-file-locked t
@@ -2310,8 +2328,9 @@ section or a child thereof."
                 (when (> le ld)
                   (setq sep (concat (make-string (- le ld) ?\s) sep))))
               (magit-insert-section (file (pop files))
-                (insert (propertize file 'font-lock-face 'magit-filename)
-                        sep cnt " ")
+                (insert (funcall magit-format-file-function
+                                 'stat file 'magit-filename))
+                (insert sep cnt " ")
                 (when add
                   (insert (propertize add 'font-lock-face
                                       'magit-diffstat-added)))
@@ -2462,11 +2481,9 @@ section or a child thereof."
         :source (and (not (equal orig file)) orig)
         :header header
         :binary binary)
-    (insert (propertize (format "%-10s %s" status
-                                (if (or (not orig) (equal orig file))
-                                    file
-                                  (format "%s -> %s" orig file)))
-                        'font-lock-face 'magit-diff-file-heading))
+    (insert (funcall magit-format-file-function
+                     'diff file 'magit-diff-file-heading status
+                     (and (not (equal orig file)) orig)))
     (cond ((and binary long-status)
            (insert (format " (%s, binary)" long-status)))
           ((or binary long-status)
@@ -2482,6 +2499,31 @@ section or a child thereof."
         (magit-insert-heading)))
     (magit-wash-sequence #'magit-diff-wash-hunk)))
 
+(defun magit-format-file-default (_kind file face &optional status orig)
+  (propertize (concat (and status (format "%-11s" status))
+                      (if orig (format "%s -> %s" orig file) file))
+              'font-lock-face face))
+
+(defun magit-format-file-all-the-icons (_kind file face &optional status orig)
+  (propertize
+   (concat (and status (format "%-11s" status))
+           (if orig
+               (format "%s %s -> %s %s"
+                       (all-the-icons-icon-for-file orig) orig
+                       (all-the-icons-icon-for-file file) file)
+             (format "%s %s" (all-the-icons-icon-for-file file) file)))
+   'font-lock-face face))
+
+(defun magit-format-file-nerd-icons (_kind file face &optional status orig)
+  (propertize
+   (concat (and status (format "%-11s" status))
+           (if orig
+               (format "%s %s -> %s %s"
+                       (nerd-icons-icon-for-file orig) orig
+                       (nerd-icons-icon-for-file file) file)
+             (format "%s %s" (nerd-icons-icon-for-file file) file)))
+   'font-lock-face face))
+
 (defun magit-diff-wash-submodule ()
   ;; See `show_submodule_summary' in submodule.c and "this" commit.
   (when (looking-at "^Submodule \\([^ ]+\\)")
diff --git a/lisp/magit-status.el b/lisp/magit-status.el
index 182130e520..494c940b26 100644
--- a/lisp/magit-status.el
+++ b/lisp/magit-status.el
@@ -812,7 +812,8 @@ Honor the buffer's file filter, which can be set using \"D 
- -\"."
             (cl-decf limit)
             (let ((file (pop files)))
               (magit-insert-section (file file)
-                (insert (propertize file 'font-lock-face 'magit-filename))
+                (insert (funcall magit-format-file-function
+                                 'list file 'magit-filename))
                 (insert ?\n))))
           (when files
             (magit-insert-section (info)

Reply via email to