From a8fdd4a448d0abb221ae59c49bbe321499ebdd55 Mon Sep 17 00:00:00 2001 From: Mohsin Kaleem Date: Sun, 14 May 2023 17:22:06 +0100 Subject: [PATCH 1/2] feat: Make all icons customizeable Add customizeable variables for all the icons inserted by all-the-icons that previously were hardcoded into the functions that used them. --- all-the-icons.el | 112 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 81 insertions(+), 31 deletions(-) diff --git a/all-the-icons.el b/all-the-icons.el index 93f8bc892..264d307fb 100644 --- a/all-the-icons.el +++ b/all-the-icons.el @@ -877,6 +877,7 @@ for performance sake.") ("\\.pdf$" all-the-icons-octicon "file-pdf" :v-adjust 0.0 :face all-the-icons-dred) ("google" all-the-icons-faicon "google") ("\\.rss" all-the-icons-faicon "rss") + (t all-the-icons-faicon "globe") )) ;; ==================== @@ -904,6 +905,18 @@ for performance sake.") (insert-file-contents modules-file) (search-forward-regexp module-search (point-max) t))))) +(defcustom all-the-icons-chevron-icon-alist + '((up all-the-icons-nerd-oct "chevron-up" :height 0.8 :v-adjust -0.1) + (down all-the-icons-nerd-oct "chevron-down" :height 0.8 :v-adjust -0.1) + (left all-the-icons-nerd-oct "chevron-left" :height 0.8 :v-adjust -0.1) + (right all-the-icons-nerd-oct "chevron-right" :height 0.8 :v-adjust -0.1)) + "Alist associating chevron types with icons." + :type '(alist :key-type (choice (const up) + (const down) + (const left) + (const right)) + :value-type list)) + ;; Icon functions (defun all-the-icons-icon-for-dir-with-chevron (dir &optional chevron padding) "Format an icon for DIR with CHEVRON similar to tree based directories. @@ -915,7 +928,14 @@ Produces different symbols by inspecting DIR to distinguish symlinks and git repositories which do not depend on the directory contents" (let ((icon (all-the-icons-icon-for-dir dir)) - (chevron (if chevron (all-the-icons-octicon (format "chevron-%s" chevron) :height 0.8 :v-adjust -0.1) "")) + (chevron + (or + (when-let ((chevron-icon + (alist-get + (if (symbolp chevron) chevron (intern chevron)) + all-the-icons-chevron-icon-alist))) + (apply (car chevron-icon) (cdr chevron-icon))) + "")) (padding (or padding "\t"))) (format "%s%s%s%s%s" padding chevron padding icon padding))) @@ -932,28 +952,55 @@ icon." "Get the icon font family for the current buffer." (all-the-icons--icon-info-for-buffer "family")) +(defcustom all-the-icons-web-mode-icon-alist + '(("jsx" all-the-icons-fileicon "jsx-2") + ("javascript" all-the-icons-alltheicon "javascript") + ("json" all-the-icons-alltheicon "less") + ("xml" all-the-icons-faicon "file-code-o") + ("css" all-the-icons-alltheicon "css3") + (t all-the-icons-alltheicon "html5")) + "Association list between a `web-mode' buffer and the icon for it. +This is used by `all-the-icons--web-mode'." + :type '(alist :key-type (choice + (string :tag "Value of `web-mode-content-type'") + (const :tag "Default icon" t)) + :value-type list)) + (defun all-the-icons--web-mode-icon (&rest arg-overrides) "Get icon for a `web-mode' buffer with ARG-OVERRIDES." (all-the-icons--web-mode nil arg-overrides)) (defun all-the-icons--web-mode-icon-family () "Get icon family for a `web-mode' buffer." (all-the-icons--web-mode t)) (defun all-the-icons--web-mode (&optional family arg-overrides) "Return icon or FAMILY for `web-mode' based on `web-mode-content-type'. Providing ARG-OVERRIDES will modify the creation of the icon." - (let ((non-nil-args (cl-reduce (lambda (acc it) (if it (append acc (list it)) acc)) arg-overrides :initial-value '()))) - (cond - ((equal web-mode-content-type "jsx") - (if family (all-the-icons-fileicon-family) (apply 'all-the-icons-fileicon (append '("jsx-2") non-nil-args)))) - ((equal web-mode-content-type "javascript") - (if family (all-the-icons-alltheicon-family) (apply 'all-the-icons-alltheicon (append '("javascript") non-nil-args)))) - ((equal web-mode-content-type "json") - (if family (all-the-icons-alltheicon-family) (apply 'all-the-icons-alltheicon (append '("less") non-nil-args)))) - ((equal web-mode-content-type "xml") - (if family (all-the-icons-faicon-family) (apply 'all-the-icons-faicon (append '("file-code-o") non-nil-args)))) - ((equal web-mode-content-type "css") - (if family (all-the-icons-alltheicon-family) (apply 'all-the-icons-alltheicon (append '("css3") non-nil-args)))) - (t - (if family (all-the-icons-alltheicon-family) (apply 'all-the-icons-alltheicon (append '("html5") non-nil-args))))))) + (let* ((non-nil-args (cl-reduce (lambda (acc it) (if it (append acc (list it)) acc)) arg-overrides :initial-value '())) + (icon (cdr + (or + (when (boundp 'web-mode-content-type) + (assoc web-mode-content-type all-the-icons-web-mode-icon-alist #'equal)) + (assoc t all-the-icons-web-mode-icon-alist)))) + (args (cdr icon))) + (when icon + (if family + (funcall (intern (format "%s-family" (car icon)))) + (when non-nil-args + (setq args (append `(,(car args)) non-nil-args (cdr args)))) + (apply (car icon) args))))) ;; Icon Functions +(defcustom all-the-icons-dir-icon-overrides + '((remote all-the-icons-octicon "terminal") + (symlink all-the-icons-octicon "file-symlink-directory") + (submodule all-the-icons-octicon "file-submodule") + (project all-the-icons-octicon "repo")) + "Directory icon overrides for `all-the-icons-icon-for-dir'. +If a directory matches the key of any entry in this variable then replace +the icon that would be used for it with the cdr of that entry." + :type '(alist :key-type (choice (const :tag "Directory is on a remote host" remote) + (const :tag "Directory is a symlink" symlink) + (const :tag "Directory is a submodule" submodule) + (const :tag "Directory is a project" project)) + :value-type list)) + ;;;###autoload (defun all-the-icons-icon-for-dir (dir &rest arg-overrides) "Get the formatted icon for DIR. @@ -965,19 +1012,23 @@ Note: You want chevron, please use `all-the-icons-icon-for-dir-with-chevron'." (let* ((dirname (file-name-base (directory-file-name dir))) (icon (all-the-icons-match-to-alist dirname all-the-icons-dir-icon-alist)) (args (cdr icon))) + (when-let ((icon-override + (cdr + (if (file-remote-p dir) + (assoc 'remote all-the-icons-dir-icon-overrides) + (let ((path (expand-file-name dir))) + (cond + ((file-symlink-p path) + (assoc 'symlink all-the-icons-dir-icon-overrides)) + ((all-the-icons-dir-is-submodule path) + (assoc 'submodule all-the-icons-dir-icon-overrides)) + ((file-exists-p (format "%s/.git" path)) + (assoc 'project all-the-icons-dir-icon-overrides)))))))) + (setq icon (list (car icon-override)) + args (append (cdr icon-override) (cdr args)))) + (when arg-overrides (setq args (append `(,(car args)) arg-overrides (cdr args)))) - (if (file-remote-p dir) ;; don't call expand-file-name on a remote dir as this can make emacs hang - (apply #'all-the-icons-octicon "terminal" (cdr args)) - (let - ((path (expand-file-name dir))) - (cond - ((file-symlink-p path) - (apply #'all-the-icons-octicon "file-symlink-directory" (cdr args))) - ((all-the-icons-dir-is-submodule path) - (apply #'all-the-icons-octicon "file-submodule" (cdr args))) - ((file-exists-p (format "%s/.git" path)) - (apply #'all-the-icons-octicon "repo" (cdr args))) - (t (apply (car icon) args))))))) + (apply (car icon) args))) ;;;###autoload (defun all-the-icons-icon-for-file (file &rest arg-overrides) @@ -1014,11 +1065,10 @@ If an icon for URL isn't found in `all-the-icons-url-alist', a globe is used. ARG-OVERRIDES should be a plist containining `:height', `:v-adjust' or `:face' properties like in the normal icon inserting functions." - (let* ((icon (all-the-icons-match-to-alist url all-the-icons-url-alist)) + (let* ((icon + (or (all-the-icons-match-to-alist url all-the-icons-url-alist) + (alist-get t all-the-icons-url-alist))) (args (cdr icon))) - (unless icon - (setq icon '(all-the-icons-faicon "globe")) - (setq args (cdr icon))) (when arg-overrides (setq args (append `(,(car args)) arg-overrides (cdr args)))) (apply (car icon) args))) From 606236190ed1505dbcf82b55fbb132a7d8d2013f Mon Sep 17 00:00:00 2001 From: Mohsin Kaleem Date: Sun, 14 May 2023 17:31:23 +0100 Subject: [PATCH 2/2] refactor: Add helper for overriding icon args --- all-the-icons.el | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/all-the-icons.el b/all-the-icons.el index 264d307fb..8da5a0430 100644 --- a/all-the-icons.el +++ b/all-the-icons.el @@ -884,6 +884,17 @@ for performance sake.") ;; Functions Start ;; ==================== +(cl-defsubst all-the-icons--override-args (args overrides) + "Override an `all-the-icons' icon arg list ARGS with OVERRIDES. +ARGS should be a list of arguments supplied to an all-the-icons icon +function such as `all-the-icons-octicon'. The first entry in ARGS +should be the icon name, the remaining entries should be overrides +for that icon. OVERRIDES should be a new set of OVERRIDES that will +take precedence over (cdr args)." + (when overrides + (setq args (append (list (car args)) overrides (cdr args)))) + args) + (defun all-the-icons-auto-mode-match? (&optional file) "Whether or not FILE's `major-mode' match against its `auto-mode-alist'." (let* ((file (or file (buffer-file-name) (buffer-name))) @@ -971,7 +982,9 @@ This is used by `all-the-icons--web-mode'." (defun all-the-icons--web-mode (&optional family arg-overrides) "Return icon or FAMILY for `web-mode' based on `web-mode-content-type'. Providing ARG-OVERRIDES will modify the creation of the icon." - (let* ((non-nil-args (cl-reduce (lambda (acc it) (if it (append acc (list it)) acc)) arg-overrides :initial-value '())) + (let* ((non-nil-args (cl-reduce (lambda (acc it) + (if it (append acc (list it)) acc)) + arg-overrides :initial-value '())) (icon (cdr (or (when (boundp 'web-mode-content-type) @@ -982,7 +995,7 @@ Providing ARG-OVERRIDES will modify the creation of the icon." (if family (funcall (intern (format "%s-family" (car icon)))) (when non-nil-args - (setq args (append `(,(car args)) non-nil-args (cdr args)))) + (setq args (all-the-icons--override-args args non-nil-args))) (apply (car icon) args))))) ;; Icon Functions @@ -1027,7 +1040,8 @@ Note: You want chevron, please use `all-the-icons-icon-for-dir-with-chevron'." (setq icon (list (car icon-override)) args (append (cdr icon-override) (cdr args)))) - (when arg-overrides (setq args (append `(,(car args)) arg-overrides (cdr args)))) + (when arg-overrides + (setq args (all-the-icons--override-args args arg-overrides))) (apply (car icon) args))) ;;;###autoload @@ -1043,7 +1057,8 @@ inserting functions." all-the-icons-extension-icon-alist))) all-the-icons-default-file-icon)) (args (cdr icon))) - (when arg-overrides (setq args (append `(,(car args)) arg-overrides (cdr args)))) + (when arg-overrides + (setq args (all-the-icons--override-args args arg-overrides))) (apply (car icon) args))) ;;;###autoload @@ -1055,7 +1070,8 @@ inserting functions." (let* ((icon (cdr (or (assoc mode all-the-icons-mode-icon-alist) (assoc (get mode 'derived-mode-parent) all-the-icons-mode-icon-alist)))) (args (cdr icon))) - (when arg-overrides (setq args (append `(,(car args)) arg-overrides (cdr args)))) + (when arg-overrides + (setq args (all-the-icons--override-args args arg-overrides))) (if icon (apply (car icon) args) mode))) ;;;###autoload @@ -1069,7 +1085,8 @@ inserting functions." (or (all-the-icons-match-to-alist url all-the-icons-url-alist) (alist-get t all-the-icons-url-alist))) (args (cdr icon))) - (when arg-overrides (setq args (append `(,(car args)) arg-overrides (cdr args)))) + (when arg-overrides + (setq args (all-the-icons--override-args args arg-overrides))) (apply (car icon) args))) (defcustom all-the-icons--cache-limit 2048