use autoload cookie to load void variable major-mode-icons-icons-style.
[major-mode-icons.git] / major-mode-icons.el
blob0e224167d6f8c04575d1a7814018af6f30ceda43
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"))
5 ;; Version: 0.2
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/>.
12 ;;; Commentary:
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
22 ;;; Code:
23 ;;; ----------------------------------------------------------------------------
25 (require 'cl-lib)
26 (require 'powerline)
28 (defgroup major-mode-icons nil
29 "Show icon for current buffer's major-mode."
30 :group 'mode-line)
32 (defconst major-mode-icons--icons-default-path
33 (concat
34 (file-name-directory (or load-file-name
35 (buffer-file-name)))
36 "icons/")
37 "Default icons path of major-mode-icons.")
39 (defcustom major-mode-icons-icons-path major-mode-icons--icons-default-path
40 "Path to icons."
41 :group 'major-mode-icons
42 :type 'string)
44 (defcustom major-mode-icons-mode-name-font "Segoe Print"
45 "The font family used for major mode name."
46 :group 'major-mode-icons
47 :type 'string)
49 ;;;###autoload
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
56 :type 'string)
58 (require (pcase major-mode-icons-icons-style
59 (`all-the-icons 'all-the-icons)
60 (`xpm 'xpm)))
62 ;; major mode with icon
63 (defvar major-mode-icons--major-mode-list
64 '(((emacs-lisp-mode
65 inferior-emacs-lisp-mode
66 ielm-mode) . "Emacs")
67 ((lisp-mode
68 inferior-lisp-mode
69 slime-repl-mode sly-mrepl-mode) . "Common-Lisp")
70 ((scheme-mode) . "Scheme")
71 ((clojure-mode
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")
77 ((c-mode) . "C")
78 ((c++-mode) . "C++")
79 ((csharp-mode) . "C#")
80 ((go-mode) . "Go")
81 ((swift-mode) . "Swift")
82 ((rust-mode) . "Rust")
83 ((java-mode) . "Java")
84 ((php-mode) . "PHP")
85 ((web-mode html-mode) . "HTML")
86 ((css-mode) . "CSS")
87 ((javascript-mode
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")
110 ((sql-mode) . "SQL")
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")
116 ((qt-mode) . "Qt")
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])."
128 ;;;###autoload
129 (defun major-mode-icons--major-mode-list-match ()
130 "Return the matched item in `major-mode-list'."
131 (assoc
132 (cl-some ; or use (remove nil '(nil nil (clojure-mode) nil nil ...))
133 (lambda (elem)
134 (when (not (null elem))
135 elem))
136 (mapcar
137 (lambda (element)
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))
145 (icon (cdr match))
146 (icon-path (concat major-mode-icons-icons-path icon ".xpm")))
147 (concat
148 (if (and (file-exists-p icon-path) (image-type-available-p 'xpm))
149 (propertize
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)
161 map)
163 (propertize (format-mode-line mode-name))
165 ;;; extra
166 (if extra
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."
174 (let ((extra
175 (cl-case major-mode
176 ('clojure-mode
177 (if (and (featurep 'cider)
178 (not (equal (cider--modeline-info) "not connected")))
179 (cider--project-name nrepl-project-dir)))
180 ('enh-ruby-mode
181 (if (and (featurep 'rbenv) global-rbenv-mode)
182 (rbenv--active-ruby-version) ; `rbenv--modestring'
184 ('python-mode
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'
190 )))))
192 ;;;###autoload
193 (defun major-mode-icons-show ()
194 "Show icon on mode-line."
195 (cl-case major-mode-icons-icons-style
196 (`all-the-icons
197 (all-the-icons-icon-for-buffer))
198 (`xpm
199 (major-mode-icons--major-mode-icon (major-mode-icons--major-mode-extra)))))
201 ;;;###autoload
202 (defpowerline powerline-major-mode-icons
203 (cl-case major-mode-icons-icons-style
204 (`all-the-icons
205 (all-the-icons-icon-for-buffer))
206 (`xpm
207 (let* ((match (major-mode-icons--major-mode-list-match))
208 (icon (cdr 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)
223 map))))))
225 ;;;###autoload
226 (defvar major-mode-icons-lighter
227 (cl-case major-mode-icons-icons-style
228 (`all-the-icons
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)
234 (`xpm
235 (let* ((match (major-mode-icons--major-mode-list-match))
236 (icon (cdr match)))
237 (propertize (format-mode-line mode-name) ; display `mode-name' text.
238 'display ; display icon
239 (let ((icon-path
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)
253 map))))
255 "Lighter for minor mode `major-mode-icons'.")
257 (put 'major-mode-icons-lighter 'risky-local-variable t)
259 ;;;###autoload
260 (define-minor-mode major-mode-icons-mode
261 "A minor mode of showing icon for major-mode of current buffer."
262 :init-value t
263 :lighter major-mode-icons-lighter
264 :global t)
266 ;;; ----------------------------------------------------------------------------
268 (provide 'major-mode-icons)
270 ;;; major-mode-icons.el ends here