Upload library homepage
[major-mode-icons.git] / major-mode-icons.el
blobea9a05c469b00180e09b32c5c707b30bb5012afe
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: 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/>.
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
51 (if (featurep 'xpm)
52 'xpm
53 'all-the-icons)
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
59 :type 'string)
61 (require (pcase major-mode-icons-icons-style
62 (`all-the-icons 'all-the-icons)
63 (`xpm 'xpm)))
65 ;; major mode with icon
66 (defvar major-mode-icons--major-mode-list
67 '(((emacs-lisp-mode
68 inferior-emacs-lisp-mode
69 ielm-mode) . "Emacs")
70 ((lisp-mode
71 inferior-lisp-mode
72 slime-repl-mode sly-mrepl-mode) . "Common-Lisp")
73 ((scheme-mode) . "Scheme")
74 ((clojure-mode
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")
80 ((c-mode) . "C")
81 ((c++-mode) . "C++")
82 ((csharp-mode) . "C#")
83 ((go-mode) . "Go")
84 ((swift-mode) . "Swift")
85 ((rust-mode) . "Rust")
86 ((java-mode) . "Java")
87 ((php-mode) . "PHP")
88 ((web-mode html-mode) . "HTML")
89 ((css-mode) . "CSS")
90 ((javascript-mode
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")
113 ((sql-mode) . "SQL")
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")
119 ((qt-mode) . "Qt")
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])."
131 ;;;###autoload
132 (defun major-mode-icons--major-mode-list-match ()
133 "Return the matched item in `major-mode-list'."
134 (assoc
135 (cl-some ; or use (remove nil '(nil nil (clojure-mode) nil nil ...))
136 (lambda (elem)
137 (when (not (null elem))
138 elem))
139 (mapcar
140 (lambda (element)
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))
148 (icon (cdr match))
149 (icon-path (concat major-mode-icons-icons-path icon ".xpm")))
150 (concat
151 (if (and (file-exists-p icon-path) (image-type-available-p 'xpm))
152 (propertize
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)
164 map)
166 (propertize (format-mode-line mode-name))
168 ;;; extra
169 (if extra
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."
177 (let ((extra
178 (cl-case major-mode
179 ('clojure-mode
180 (if (and (featurep 'cider)
181 (not (equal (cider--modeline-info) "not connected")))
182 (cider--project-name nrepl-project-dir)))
183 ('enh-ruby-mode
184 (if (and (featurep 'rbenv) global-rbenv-mode)
185 (rbenv--active-ruby-version) ; `rbenv--modestring'
187 ('python-mode
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'
193 )))))
195 ;;;###autoload
196 (defun major-mode-icons-show ()
197 "Show icon on mode-line."
198 (cl-case major-mode-icons-icons-style
199 (`all-the-icons
200 (all-the-icons-icon-for-buffer))
201 (`xpm
202 (major-mode-icons--major-mode-icon (major-mode-icons--major-mode-extra)))))
204 ;;;###autoload
205 (defpowerline powerline-major-mode-icons
206 (cl-case major-mode-icons-icons-style
207 (`all-the-icons
208 (all-the-icons-icon-for-buffer))
209 (`xpm
210 (let* ((match (major-mode-icons--major-mode-list-match))
211 (icon (cdr 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)
226 map))))))
228 ;;;###autoload
229 (defvar major-mode-icons-lighter
230 (cl-case major-mode-icons-icons-style
231 (`all-the-icons
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)
237 (`xpm
238 (let* ((match (major-mode-icons--major-mode-list-match))
239 (icon (cdr match)))
240 (propertize (format-mode-line mode-name) ; display `mode-name' text.
241 'display ; display icon
242 (let ((icon-path
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)
256 map))))
258 "Lighter for minor mode `major-mode-icons'.")
260 (put 'major-mode-icons-lighter 'risky-local-variable t)
262 ;;;###autoload
263 (define-minor-mode major-mode-icons-mode
264 "A minor mode of showing icon for major-mode of current buffer."
265 :init-value t
266 :lighter major-mode-icons-lighter
267 :global t)
269 ;;; ----------------------------------------------------------------------------
271 (provide 'major-mode-icons)
273 ;;; major-mode-icons.el ends here