1 ;;; major-mode-icons.el --- display icon for major-mode on mode-line.
3 ;; Authors: stardiviner <numbchild@gmail.com>
4 ;; Package-Requires: ((emacs "24.3") (powerline "2.4") (all-the-icons "2.3.0"))
6 ;; Keywords: frames multimedia
7 ;; homepage: https://repo.or.cz/major-mode-icons.git
9 ;; You should have received a copy of the GNU General Public License
10 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
14 ;; If you want to use `major-mode-icons--major-mode-extra' extra info, you
15 ;; should install corresponding packages.
17 ;; - clojure-mode <-> cider
18 ;; - ruby-mode <-> rbenv
19 ;; - python-mode <-> pyvenv
23 ;;; ----------------------------------------------------------------------------
28 (defgroup major-mode-icons nil
29 "Show icon for current buffer's major-mode."
32 (defconst major-mode-icons--icons-default-path
34 (file-name-directory (or load-file-name
37 "Default icons path of major-mode-icons.")
39 (defcustom major-mode-icons-icons-path major-mode-icons--icons-default-path
41 :group
'major-mode-icons
44 (defcustom major-mode-icons-mode-name-font
"Segoe Print"
45 "The font family used for major mode name."
46 :group
'major-mode-icons
50 (defcustom major-mode-icons-icons-style
54 "Use `all-the-icons' package to show major mode icons.
56 If set to symbol `all-the-icons' then use `all-the-icons'.
57 Otherwise symbol `xpm' to use built-in xpm image files."
58 :group
'major-mode-icons
61 (require (pcase major-mode-icons-icons-style
62 (`all-the-icons
'all-the-icons
)
65 ;; major mode with icon
66 (defvar major-mode-icons--major-mode-list
68 inferior-emacs-lisp-mode
72 slime-repl-mode sly-mrepl-mode
) .
"Common-Lisp")
73 ((scheme-mode) .
"Scheme")
75 cider-repl-mode
) .
"Clojure")
76 ((clojurescript-mode) .
"ClojureScript")
77 ((python-mode) .
"Python")
78 ((enh-ruby-mode ruby-mode
) .
"Ruby")
79 ((inf-ruby-mode) .
"inf-ruby")
82 ((csharp-mode) .
"C#")
84 ((swift-mode) .
"Swift")
85 ((rust-mode) .
"Rust")
86 ((java-mode) .
"Java")
88 ((web-mode html-mode
) .
"HTML")
91 js-mode js2-mode js3-mode inferior-js-mode
) .
"JavaScript")
92 ((coffee-mode) .
"CoffeeScript")
93 ((org-mode org-agenda-mode
) .
"Org-mode")
94 ((tex-mode latex-mode TeX-mode LaTeX-mode
) .
"TeX")
95 ((bibtex-mode) .
"BibTeX")
96 ((markdown-mode) .
"Markdown")
97 ((yaml-mode) .
"YAML")
98 ((rst-mode) .
"reStructuredText")
99 ((eshell-mode) .
"Command-Line")
100 ((sh-mode shell-mode
) .
"Shell")
101 ((term-mode) .
"term")
102 ((powershell-mode) .
"powershell")
103 ((ess-mode R-mode
) .
"R")
104 ((julia-mode ess-julia-mode
) .
"Julia")
105 ((gnuplot-mode) .
"gnuplot")
106 ((octave-mode) .
"Octave")
107 ((matlab-mode) .
"Matlab")
108 ((haskell-mode) .
"Haskell")
109 ((scala-mode) .
"Scala")
110 ((erlang-mode) .
"Erlang")
111 ((prolog-mode) .
"Prolog")
112 ((ocaml-mode) .
"OCaml")
114 ((xml-mode nxml-mode
) .
"XML")
115 ((json-mode) .
"JSON")
116 ((diff-mode ediff-mode magit-diff-mode
) .
"diff")
117 ((asm-mode nasm-mode
) .
"Assembly")
118 ((android-mode) .
"Android")
120 ((arduino-mode) .
"Arduino")
121 ((systemd-mode) .
"Systemd")
122 ((docker-mode) .
"Docker")
123 ((projectile-rails-mode) .
"Rails")
124 ((slim-mode) .
"Slim")
125 ((sass-mode) .
"Sass")
126 ((spice-mode) .
"Electric")
128 "Pairs: ([mode-list] . [icon-name])."
132 (defun major-mode-icons--major-mode-list-match ()
133 "Return the matched item in `major-mode-list'."
135 (cl-some ; or use (remove nil '(nil nil (clojure-mode) nil nil ...))
137 (when (not (null elem
))
141 (member major-mode element
))
142 (mapcar 'car major-mode-icons--major-mode-list
)))
143 major-mode-icons--major-mode-list
))
145 (defun major-mode-icons--major-mode-icon (&optional extra
)
146 "Display icon for current buffer's `major-mode' and `EXTRA' info."
147 (let* ((match (major-mode-icons--major-mode-list-match))
149 (icon-path (concat major-mode-icons-icons-path icon
".xpm")))
151 (if (and (file-exists-p icon-path
) (image-type-available-p 'xpm
))
154 'display
(if (and (file-exists-p icon-path
) (image-type-available-p 'xpm
))
155 (create-image icon-path
'xpm nil
:ascent
'center
))
156 'mouse-face
'mode-line-highlight
157 'help-echo
"Major-mode\n\ mouse-1: Display major mode menu\n\ mouse2: Show help for major mode\n\ mouse-3: Toggle minor modes"
158 'local-map
(let ((map (make-sparse-keymap)))
159 (define-key map
[mode-line down-mouse-1
]
160 `(menu-item ,(purecopy "Menu Bar") ignore
161 :filter
(lambda (_) (mouse-menu-major-mode-map))))
162 (define-key map
[mode-line mouse-2
] 'describe-mode
)
163 (define-key map
[mode-line down-mouse-3
] mode-line-mode-menu
)
166 (propertize (format-mode-line mode-name
))
170 (propertize (format " %s" (or extra
"")))
174 ;;; auto show extra info
175 (defun major-mode-icons--major-mode-extra ()
176 "Extend function `major-mode-icon' with extra info."
180 (if (and (featurep 'cider
)
181 (not (equal (cider--modeline-info) "not connected")))
182 (cider--project-name nrepl-project-dir
)))
184 (if (and (featurep 'rbenv
) global-rbenv-mode
)
185 (rbenv--active-ruby-version) ; `rbenv--modestring'
188 (if (and (featurep 'pyvenv
) pyvenv-mode
)
189 ;; `pyvenv-mode-line-indicator' -> `pyvenv-virtual-env-name'
190 pyvenv-virtual-env-name
191 ;; conda: `conda-env-current-name'
196 (defun major-mode-icons-show ()
197 "Show icon on mode-line."
198 (cl-case major-mode-icons-icons-style
200 (all-the-icons-icon-for-buffer))
202 (major-mode-icons--major-mode-icon (major-mode-icons--major-mode-extra)))))
205 (defpowerline powerline-major-mode-icons
206 (cl-case major-mode-icons-icons-style
208 (all-the-icons-icon-for-buffer))
210 (let* ((match (major-mode-icons--major-mode-list-match))
212 (icon-path (concat major-mode-icons-icons-path icon
".xpm")))
213 (propertize (format-mode-line mode-name
) ; display `mode-name' text.
214 'display
; display icon
215 (if (and (image-type-available-p 'xpm
)
216 (file-exists-p icon-path
))
217 (create-image icon-path
'xpm nil
:ascent
'center
))
218 'mouse-face
'mode-line-highlight
219 'help-echo
"Major-mode\n\ mouse-1: Display major mode menu\n\ mouse2: Show help for major mode\n\ mouse-3: Toggle minor modes"
220 'local-map
(let ((map (make-sparse-keymap)))
221 (define-key map
[mode-line down-mouse-1
]
222 `(menu-item ,(purecopy "Menu Bar") ignore
223 :filter
(lambda (_) (mouse-menu-major-mode-map))))
224 (define-key map
[mode-line mouse-2
] 'describe-mode
)
225 (define-key map
[mode-line down-mouse-3
] mode-line-mode-menu
)
229 (defvar major-mode-icons-lighter
230 (cl-case major-mode-icons-icons-style
232 ;; - `all-the-icons-icon-for-buffer'
233 ;; - `all-the-icons-icon-for-file'
234 ;; - `all-the-icons-icon-for-mode'
235 (all-the-icons-icon-for-buffer)
238 (let* ((match (major-mode-icons--major-mode-list-match))
240 (propertize (format-mode-line mode-name
) ; display `mode-name' text.
241 'display
; display icon
243 (concat major-mode-icons-icons-path icon
".xpm")))
244 (if (and (image-type-available-p 'xpm
)
245 (file-exists-p icon-path
))
246 (create-image icon-path
'xpm nil
:ascent
'center
)
248 'mouse-face
'mode-line-highlight
249 'help-echo
"Major-mode\n\ mouse-1: Display major mode menu\n\ mouse2: Show help for major mode\n\ mouse-3: Toggle minor modes"
250 'local-map
(let ((map (make-sparse-keymap)))
251 (define-key map
[mode-line down-mouse-1
]
252 `(menu-item ,(purecopy "Menu Bar") ignore
253 :filter
(lambda (_) (mouse-menu-major-mode-map))))
254 (define-key map
[mode-line mouse-2
] 'describe-mode
)
255 (define-key map
[mode-line down-mouse-3
] mode-line-mode-menu
)
258 "Lighter for minor mode `major-mode-icons'.")
260 (put 'major-mode-icons-lighter
'risky-local-variable t
)
263 (define-minor-mode major-mode-icons-mode
264 "A minor mode of showing icon for major-mode of current buffer."
266 :lighter major-mode-icons-lighter
269 ;;; ----------------------------------------------------------------------------
271 (provide 'major-mode-icons
)
273 ;;; major-mode-icons.el ends here