don't require library 'map' to don't need Emacs version 25.1
[major-mode-icons.git] / major-mode-icons.el
blobdca5e73f39d412726c7c1da4c1aeb438f6969dc1
1 ;;; major-mode-icons.el --- display icon for major-mode on mode-line.
3 ;; Authors: stardiviner <numbchild@gmail.com>
4 ;; Package-Requires: ((emacs "25.1"))
5 ;; Version: 0.1
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)
27 (defgroup major-mode-icons nil
28 "Show icon for current buffer's major-mode."
29 :group 'mode-line)
31 (defconst major-mode-icons--icons-default-path
32 (concat
33 (file-name-directory (or load-file-name
34 (buffer-file-name)))
35 "icons/")
36 "Default icons path of major-mode-icons.")
38 (defcustom major-mode-icons-icons-path major-mode-icons--icons-default-path
39 "Path to icons."
40 :group 'major-mode-icons
41 :type 'string)
43 (defcustom major-mode-icons-mode-name-font "Segoe Print"
44 "The font family used for major mode name."
45 :group 'major-mode-icons
46 :type 'string)
48 ;; major mode with icon
49 (defvar major-mode-icons--major-mode-list
50 '(((emacs-lisp-mode
51 inferior-emacs-lisp-mode
52 ielm-mode) . "Emacs")
53 ((lisp-mode
54 inferior-lisp-mode
55 slime-repl-mode sly-mrepl-mode) . "Common-Lisp")
56 ((scheme-mode) . "Scheme")
57 ((clojure-mode
58 cider-repl-mode) . "Clojure")
59 ((clojurescript-mode) . "ClojureScript")
60 ((python-mode) . "Python")
61 ((enh-ruby-mode ruby-mode) . "Ruby")
62 ((inf-ruby-mode) . "inf-ruby")
63 ((c-mode) . "C")
64 ((c++-mode) . "C++")
65 ((csharp-mode) . "C#")
66 ((go-mode) . "Go")
67 ((swift-mode) . "Swift")
68 ((rust-mode) . "Rust")
69 ((java-mode) . "Java")
70 ((php-mode) . "PHP")
71 ((web-mode html-mode) . "HTML")
72 ((css-mode) . "CSS")
73 ((javascript-mode
74 js-mode js2-mode js3-mode inferior-js-mode) . "JavaScript")
75 ((coffee-mode) . "CoffeeScript")
76 ((org-mode org-agenda-mode) . "Org-mode")
77 ((tex-mode latex-mode TeX-mode LaTeX-mode) . "TeX")
78 ((bibtex-mode) . "BibTeX")
79 ((markdown-mode) . "Markdown")
80 ((yaml-mode) . "YAML")
81 ((rst-mode) . "reStructuredText")
82 ((eshell-mode) . "Command-Line")
83 ((sh-mode shell-mode) . "Shell")
84 ((term-mode) . "term")
85 ((powershell-mode) . "powershell")
86 ((ess-mode R-mode) . "R")
87 ((julia-mode ess-julia-mode) . "Julia")
88 ((gnuplot-mode) . "gnuplot")
89 ((octave-mode) . "Octave")
90 ((matlab-mode) . "Matlab")
91 ((haskell-mode) . "Haskell")
92 ((scala-mode) . "Scala")
93 ((erlang-mode) . "Erlang")
94 ((prolog-mode) . "Prolog")
95 ((ocaml-mode) . "OCaml")
96 ((sql-mode) . "SQL")
97 ((xml-mode nxml-mode) . "XML")
98 ((json-mode) . "JSON")
99 ((diff-mode ediff-mode magit-diff-mode) . "diff")
100 ((asm-mode nasm-mode) . "Assembly")
101 ((android-mode) . "Android")
102 ((qt-mode) . "Qt")
103 ((arduino-mode) . "Arduino")
104 ((systemd-mode) . "Systemd")
105 ((docker-mode) . "Docker")
106 ((projectile-rails-mode) . "Rails")
107 ((slim-mode) . "Slim")
108 ((sass-mode) . "Sass")
109 ((spice-mode) . "Electric")
111 "Pairs: ([mode-list] . [icon-name])."
114 (defun major-mode-icons--major-mode-list-match ()
115 "Return the matched item in `major-mode-list'."
116 (assoc
117 (cl-some ; or use (remove nil '(nil nil (clojure-mode) nil nil ...))
118 (lambda (elem)
119 (when (not (null elem))
120 elem))
121 (mapcar
122 (lambda (element)
123 (member major-mode element))
124 (mapcar 'car major-mode-icons--major-mode-list)))
125 major-mode-icons--major-mode-list))
127 (defun major-mode-icons--major-mode-icon (&optional extra)
128 "Display icon for current buffer's `major-mode' and `EXTRA' info."
129 (let* ((match (major-mode-icons--major-mode-list-match))
130 (icon (cdr match))
131 (icon-path (concat major-mode-icons-icons-path icon ".xpm")))
132 (concat
133 (if (and (file-exists-p icon-path) (image-type-available-p 'xpm))
134 (propertize
136 'display (if (and (file-exists-p icon-path) (image-type-available-p 'xpm))
137 (create-image icon-path 'xpm nil :ascent 'center)))
138 (propertize (format "%s" mode-name))
140 ;;; extra
141 (if extra
142 (propertize (format " %s" (or extra "")))
146 ;;; auto show extra info
147 (defun major-mode-icons--major-mode-extra ()
148 "Extend function `major-mode-icon' with extra info."
149 (let ((extra
150 (cl-case major-mode
151 ('clojure-mode
152 (if (and (featurep 'cider)
153 (not (equal (cider--modeline-info) "not connected")))
154 (cider--project-name nrepl-project-dir)))
155 ('enh-ruby-mode
156 (if (and (featurep 'rbenv) global-rbenv-mode)
157 (rbenv--active-ruby-version) ; `rbenv--modestring'
159 ('python-mode
160 (if (and (featurep 'pyvenv) pyvenv-mode)
161 ;; `pyvenv-mode-line-indicator' -> `pyvenv-virtual-env-name'
162 pyvenv-virtual-env-name
163 ;; conda: `conda-env-current-name'
165 )))))
167 ;;;###autoload
168 (defun major-mode-icons-show ()
169 "Show icon on mode-line."
170 (major-mode-icons--major-mode-icon (major-mode-icons--major-mode-extra)))
172 (defvar major-mode-icons-lighter
173 (let* ((match (major-mode-icons--major-mode-list-match))
174 (icon (cdr match)))
175 (propertize (format " %s " mode-name) ; display `mode-name' text.
176 'display
177 (let ((icon-path
178 (concat major-mode-icons-icons-path icon ".xpm")))
179 (if (and (image-type-available-p 'xpm)
180 (file-exists-p icon-path))
181 (create-image icon-path 'xpm nil :ascent 'center)
182 ))))
183 "Lighter for minor mode `major-mode-icons'.")
185 (put 'major-mode-icons-lighter 'risky-local-variable t)
187 ;;;###autoload
188 (define-minor-mode major-mode-icons-mode
189 "A minor mode of showing icon for major-mode of current buffer."
190 :init-value t
191 :lighter major-mode-icons-lighter
192 :global t)
194 ;;; ----------------------------------------------------------------------------
196 (provide 'major-mode-icons)
198 ;;; major-mode-icons.el ends here