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: http://github.com/stardiviner/major-mode-icons
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
'xpm
51 "Use `all-the-icons' package to show major mode icons.
53 If set to symbol `all-the-icons' then use `all-the-icons'.
54 Otherwise symbol `xpm' to use built-in xpm image files."
55 :group
'major-mode-icons
58 (require (pcase major-mode-icons-icons-style
59 (`all-the-icons
'all-the-icons
)
62 ;; major mode with icon
63 (defvar major-mode-icons--major-mode-list
65 inferior-emacs-lisp-mode
69 slime-repl-mode sly-mrepl-mode
) .
"Common-Lisp")
70 ((scheme-mode) .
"Scheme")
72 cider-repl-mode
) .
"Clojure")
73 ((clojurescript-mode) .
"ClojureScript")
74 ((python-mode) .
"Python")
75 ((enh-ruby-mode ruby-mode
) .
"Ruby")
76 ((inf-ruby-mode) .
"inf-ruby")
79 ((csharp-mode) .
"C#")
81 ((swift-mode) .
"Swift")
82 ((rust-mode) .
"Rust")
83 ((java-mode) .
"Java")
85 ((web-mode html-mode
) .
"HTML")
88 js-mode js2-mode js3-mode inferior-js-mode
) .
"JavaScript")
89 ((coffee-mode) .
"CoffeeScript")
90 ((org-mode org-agenda-mode
) .
"Org-mode")
91 ((tex-mode latex-mode TeX-mode LaTeX-mode
) .
"TeX")
92 ((bibtex-mode) .
"BibTeX")
93 ((markdown-mode) .
"Markdown")
94 ((yaml-mode) .
"YAML")
95 ((rst-mode) .
"reStructuredText")
96 ((eshell-mode) .
"Command-Line")
97 ((sh-mode shell-mode
) .
"Shell")
98 ((term-mode) .
"term")
99 ((powershell-mode) .
"powershell")
100 ((ess-mode R-mode
) .
"R")
101 ((julia-mode ess-julia-mode
) .
"Julia")
102 ((gnuplot-mode) .
"gnuplot")
103 ((octave-mode) .
"Octave")
104 ((matlab-mode) .
"Matlab")
105 ((haskell-mode) .
"Haskell")
106 ((scala-mode) .
"Scala")
107 ((erlang-mode) .
"Erlang")
108 ((prolog-mode) .
"Prolog")
109 ((ocaml-mode) .
"OCaml")
111 ((xml-mode nxml-mode
) .
"XML")
112 ((json-mode) .
"JSON")
113 ((diff-mode ediff-mode magit-diff-mode
) .
"diff")
114 ((asm-mode nasm-mode
) .
"Assembly")
115 ((android-mode) .
"Android")
117 ((arduino-mode) .
"Arduino")
118 ((systemd-mode) .
"Systemd")
119 ((docker-mode) .
"Docker")
120 ((projectile-rails-mode) .
"Rails")
121 ((slim-mode) .
"Slim")
122 ((sass-mode) .
"Sass")
123 ((spice-mode) .
"Electric")
125 "Pairs: ([mode-list] . [icon-name])."
129 (defun major-mode-icons--major-mode-list-match ()
130 "Return the matched item in `major-mode-list'."
132 (cl-some ; or use (remove nil '(nil nil (clojure-mode) nil nil ...))
134 (when (not (null elem
))
138 (member major-mode element
))
139 (mapcar 'car major-mode-icons--major-mode-list
)))
140 major-mode-icons--major-mode-list
))
142 (defun major-mode-icons--major-mode-icon (&optional extra
)
143 "Display icon for current buffer's `major-mode' and `EXTRA' info."
144 (let* ((match (major-mode-icons--major-mode-list-match))
146 (icon-path (concat major-mode-icons-icons-path icon
".xpm")))
148 (if (and (file-exists-p icon-path
) (image-type-available-p 'xpm
))
151 'display
(if (and (file-exists-p icon-path
) (image-type-available-p 'xpm
))
152 (create-image icon-path
'xpm nil
:ascent
'center
))
153 'mouse-face
'mode-line-highlight
154 'help-echo
"Major-mode\n\ mouse-1: Display major mode menu\n\ mouse2: Show help for major mode\n\ mouse-3: Toggle minor modes"
155 'local-map
(let ((map (make-sparse-keymap)))
156 (define-key map
[mode-line down-mouse-1
]
157 `(menu-item ,(purecopy "Menu Bar") ignore
158 :filter
(lambda (_) (mouse-menu-major-mode-map))))
159 (define-key map
[mode-line mouse-2
] 'describe-mode
)
160 (define-key map
[mode-line down-mouse-3
] mode-line-mode-menu
)
163 (propertize (format-mode-line mode-name
))
167 (propertize (format " %s" (or extra
"")))
171 ;;; auto show extra info
172 (defun major-mode-icons--major-mode-extra ()
173 "Extend function `major-mode-icon' with extra info."
177 (if (and (featurep 'cider
)
178 (not (equal (cider--modeline-info) "not connected")))
179 (cider--project-name nrepl-project-dir
)))
181 (if (and (featurep 'rbenv
) global-rbenv-mode
)
182 (rbenv--active-ruby-version) ; `rbenv--modestring'
185 (if (and (featurep 'pyvenv
) pyvenv-mode
)
186 ;; `pyvenv-mode-line-indicator' -> `pyvenv-virtual-env-name'
187 pyvenv-virtual-env-name
188 ;; conda: `conda-env-current-name'
193 (defun major-mode-icons-show ()
194 "Show icon on mode-line."
195 (cl-case major-mode-icons-icons-style
197 (all-the-icons-icon-for-buffer))
199 (major-mode-icons--major-mode-icon (major-mode-icons--major-mode-extra)))))
202 (defpowerline powerline-major-mode-icons
203 (cl-case major-mode-icons-icons-style
205 (all-the-icons-icon-for-buffer))
207 (let* ((match (major-mode-icons--major-mode-list-match))
209 (icon-path (concat major-mode-icons-icons-path icon
".xpm")))
210 (propertize (format-mode-line mode-name
) ; display `mode-name' text.
211 'display
; display icon
212 (if (and (image-type-available-p 'xpm
)
213 (file-exists-p icon-path
))
214 (create-image icon-path
'xpm nil
:ascent
'center
))
215 'mouse-face
'mode-line-highlight
216 'help-echo
"Major-mode\n\ mouse-1: Display major mode menu\n\ mouse2: Show help for major mode\n\ mouse-3: Toggle minor modes"
217 'local-map
(let ((map (make-sparse-keymap)))
218 (define-key map
[mode-line down-mouse-1
]
219 `(menu-item ,(purecopy "Menu Bar") ignore
220 :filter
(lambda (_) (mouse-menu-major-mode-map))))
221 (define-key map
[mode-line mouse-2
] 'describe-mode
)
222 (define-key map
[mode-line down-mouse-3
] mode-line-mode-menu
)
226 (defvar major-mode-icons-lighter
227 (cl-case major-mode-icons-icons-style
229 ;; - `all-the-icons-icon-for-buffer'
230 ;; - `all-the-icons-icon-for-file'
231 ;; - `all-the-icons-icon-for-mode'
232 (all-the-icons-icon-for-buffer)
235 (let* ((match (major-mode-icons--major-mode-list-match))
237 (propertize (format-mode-line mode-name
) ; display `mode-name' text.
238 'display
; display icon
240 (concat major-mode-icons-icons-path icon
".xpm")))
241 (if (and (image-type-available-p 'xpm
)
242 (file-exists-p icon-path
))
243 (create-image icon-path
'xpm nil
:ascent
'center
)
245 'mouse-face
'mode-line-highlight
246 'help-echo
"Major-mode\n\ mouse-1: Display major mode menu\n\ mouse2: Show help for major mode\n\ mouse-3: Toggle minor modes"
247 'local-map
(let ((map (make-sparse-keymap)))
248 (define-key map
[mode-line down-mouse-1
]
249 `(menu-item ,(purecopy "Menu Bar") ignore
250 :filter
(lambda (_) (mouse-menu-major-mode-map))))
251 (define-key map
[mode-line mouse-2
] 'describe-mode
)
252 (define-key map
[mode-line down-mouse-3
] mode-line-mode-menu
)
255 "Lighter for minor mode `major-mode-icons'.")
257 (put 'major-mode-icons-lighter
'risky-local-variable t
)
260 (define-minor-mode major-mode-icons-mode
261 "A minor mode of showing icon for major-mode of current buffer."
263 :lighter major-mode-icons-lighter
266 ;;; ----------------------------------------------------------------------------
268 (provide 'major-mode-icons
)
270 ;;; major-mode-icons.el ends here