2013-06-02 13:56:07 +02:00
|
|
|
;;; mode-icons.el --- Show icons for modes -*- lexical-binding: t; -*-
|
2012-11-10 23:12:36 +01:00
|
|
|
|
2013-06-02 14:48:00 +02:00
|
|
|
;; Copyright (C) 2013 Tom Willemse
|
2012-11-10 23:12:36 +01:00
|
|
|
|
2013-06-02 14:45:06 +02:00
|
|
|
;; Author: Tom Willemse <tom@ryuslash.org>
|
2012-11-10 23:12:36 +01:00
|
|
|
;; Keywords: multimedia
|
2012-11-10 23:21:46 +01:00
|
|
|
;; Version: 0.1.0
|
2013-06-02 14:45:06 +02:00
|
|
|
;; URL: http://ryuslash.org/projects/mode-icons.html
|
2012-11-10 23:12:36 +01:00
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Code:
|
2016-01-22 16:31:03 +01:00
|
|
|
(defgroup mode-icons nil
|
|
|
|
"Provide icons for major modes."
|
|
|
|
:group 'editing-basics
|
|
|
|
:group 'convenience)
|
2012-11-10 23:12:36 +01:00
|
|
|
|
2013-06-02 13:56:07 +02:00
|
|
|
(defconst mode-icons--directory
|
|
|
|
(if load-file-name
|
|
|
|
(file-name-directory load-file-name)
|
|
|
|
default-directory)
|
2012-11-10 23:17:42 +01:00
|
|
|
"Where mode-icons was loaded from.")
|
|
|
|
|
2013-06-02 13:56:07 +02:00
|
|
|
(defun mode-icons-get-icon-file (icon)
|
|
|
|
"Get the location of ICON.
|
|
|
|
|
|
|
|
ICON should be a file name with extension. The result is the
|
|
|
|
absolute path to ICON."
|
|
|
|
(concat mode-icons--directory "/icons/" icon))
|
2012-11-10 23:12:36 +01:00
|
|
|
|
2016-01-22 16:31:03 +01:00
|
|
|
(defcustom mode-icons
|
|
|
|
'(("CSS" "css" xpm)
|
2015-12-24 06:39:38 +01:00
|
|
|
("Coffee" "coffee" xpm)
|
2016-01-06 06:44:36 +01:00
|
|
|
("Compilation" "compile" xpm)
|
2015-12-24 06:39:38 +01:00
|
|
|
("Emacs-Lisp" "emacs" xpm)
|
2013-06-02 13:56:07 +02:00
|
|
|
("HTML" "html" xpm)
|
2015-12-24 06:39:38 +01:00
|
|
|
("Haml" "haml" xpm)
|
|
|
|
("Image[imagemagick]" "svg" xpm)
|
2016-01-18 08:22:24 +01:00
|
|
|
("Inf-Ruby" "infruby" xpm)
|
2015-12-24 06:39:38 +01:00
|
|
|
("JavaScript" "js" xpm)
|
|
|
|
("Lisp" "cl" xpm)
|
2016-01-14 09:21:22 +01:00
|
|
|
("nXML" "xml" xpm)
|
2015-12-23 07:12:18 +01:00
|
|
|
("Org" "org" xpm)
|
2015-12-24 06:39:38 +01:00
|
|
|
("PHP" "php" xpm)
|
2016-01-18 08:22:24 +01:00
|
|
|
("Projectile Rails Server" "rails" xpm)
|
2015-12-24 06:39:38 +01:00
|
|
|
("Python" "python" xpm)
|
2015-12-23 10:56:44 +01:00
|
|
|
("Ruby" "ruby" xpm)
|
2016-01-20 22:21:05 +01:00
|
|
|
("ESS[S]" "R" xpm)
|
|
|
|
("iESS" "R" xpm)
|
2015-12-23 11:20:22 +01:00
|
|
|
("SCSS" "sass" xpm)
|
2015-12-24 06:39:38 +01:00
|
|
|
("Sass" "sass" xpm)
|
|
|
|
("Scheme" "scheme" xpm)
|
|
|
|
("Shell-script" "bash" xpm)
|
|
|
|
("Slim" "slim" xpm)
|
2016-01-14 10:33:19 +01:00
|
|
|
("Term" "term" xpm)
|
2016-01-14 09:21:22 +01:00
|
|
|
("Web" "html" xpm)
|
|
|
|
("XML" "xml" xpm)
|
2015-12-23 10:56:44 +01:00
|
|
|
("YAML" "yaml" xpm)
|
2015-12-24 06:39:38 +01:00
|
|
|
("YASnippet" "yas" xpm)
|
2016-01-22 05:50:31 +01:00
|
|
|
(" yas" "yas" xpm)
|
2016-01-23 00:31:53 +01:00
|
|
|
(" hs" "hs" xpm)
|
|
|
|
("\\(ElDoc\\|Anzu\\|SP\\|Guide\\|PgLn\\|Golden\\|Undo-Tree\\|Ergo.*\\|,\\)" nil nil))
|
2016-01-22 05:50:31 +01:00
|
|
|
"Icons for major and minor modes.
|
2013-06-02 13:56:07 +02:00
|
|
|
|
2016-01-22 16:31:03 +01:00
|
|
|
Each specificatioun is a list with the first element being the
|
2013-06-02 13:56:07 +02:00
|
|
|
name of the major mode. The second the name of the icon file,
|
2016-01-22 16:31:03 +01:00
|
|
|
without the extension. And the third being the type of icon."
|
|
|
|
:type '(repeat
|
2016-01-22 19:09:51 +01:00
|
|
|
(list (string :tag "Regular Expression")
|
|
|
|
(choice
|
|
|
|
(string :tag "Icon Name")
|
|
|
|
(const :tag "Suppress" nil))
|
|
|
|
(choice
|
2016-01-23 00:31:53 +01:00
|
|
|
(const :tag "text" nil)
|
2016-01-22 19:09:51 +01:00
|
|
|
(const :tag "png" png)
|
|
|
|
(const :tag "gif" gif)
|
|
|
|
(const :tag "jpeg" jpeg)
|
|
|
|
(const :tag "xbm" xbm)
|
|
|
|
(const :tag "xpm" xpm))))
|
2016-01-22 16:31:03 +01:00
|
|
|
:group 'mode-icons)
|
2013-06-02 13:56:07 +02:00
|
|
|
|
2016-01-22 05:50:31 +01:00
|
|
|
(defun mode-icons-get-icon-display (icon type)
|
2013-06-02 13:56:07 +02:00
|
|
|
"Get the value for the display property of ICON having TYPE.
|
|
|
|
|
|
|
|
ICON should be a string naming the file of the icon, without its
|
|
|
|
extension. Type should be a symbol designating the file type for
|
|
|
|
the icon."
|
|
|
|
(let ((icon-path (mode-icons-get-icon-file
|
|
|
|
(concat icon "." (symbol-name type)))))
|
2016-01-22 22:54:31 +01:00
|
|
|
`(image :type ,type :file ,icon-path :ascent center)))
|
2013-06-02 13:56:07 +02:00
|
|
|
|
2016-01-22 22:54:31 +01:00
|
|
|
(defcustom mode-icons-minor-mode-base-text-properties
|
|
|
|
'('help-echo 'rm--help-echo
|
|
|
|
'mouse-face 'mode-line-highlight
|
|
|
|
'local-map mode-line-minor-mode-keymap)
|
|
|
|
"List of text propeties to apply to every minor mode."
|
|
|
|
:type '(repeat sexp)
|
|
|
|
:group 'mode-icons)
|
|
|
|
|
|
|
|
(defvar mode-icons-powerline-p nil)
|
|
|
|
(defun mode-icons-need-update-p ()
|
|
|
|
"Determine if the mode-icons need an update"
|
|
|
|
(not (or (and (boundp 'rich-minority-mode) rich-minority-mode)
|
|
|
|
(member 'sml/pos-id-separator mode-line-format)
|
|
|
|
(string-match-p "powerline" (prin1-to-string mode-line-format)))))
|
|
|
|
|
2016-01-23 00:31:53 +01:00
|
|
|
(defun mode-icons-propertize-mode (mode icon-spec)
|
2013-06-02 13:56:07 +02:00
|
|
|
"Propertize MODE with ICON-SPEC.
|
|
|
|
|
|
|
|
MODE should be a string, the name of the mode to propertize.
|
2016-01-23 00:31:53 +01:00
|
|
|
ICON-SPEC should be a specification from `mode-icons'."
|
2016-01-22 16:31:03 +01:00
|
|
|
(cond
|
|
|
|
((not (nth 1 icon-spec)) "")
|
2016-01-22 22:54:31 +01:00
|
|
|
(t (propertize mode 'display (mode-icons-get-icon-display (nth 1 icon-spec) (nth 2 icon-spec))))))
|
2013-06-02 13:56:07 +02:00
|
|
|
|
2016-01-22 16:44:27 +01:00
|
|
|
(defun mode-icons-get-icon-spec (mode)
|
|
|
|
"Get icon spec based on regular expression."
|
|
|
|
(catch 'found-mode
|
|
|
|
(dolist (item mode-icons)
|
|
|
|
(when (string-match-p (car item) mode)
|
2016-01-22 19:09:51 +01:00
|
|
|
(throw 'found-mode item)))
|
2016-01-22 16:44:27 +01:00
|
|
|
nil))
|
|
|
|
|
2016-01-22 05:50:31 +01:00
|
|
|
(defun mode-icons-get-mode-icon (mode)
|
2013-06-02 13:56:07 +02:00
|
|
|
"Get the icon for MODE, if there is one."
|
|
|
|
(let* ((mode-name (format-mode-line mode))
|
2016-01-22 16:44:27 +01:00
|
|
|
(icon-spec (mode-icons-get-icon-spec mode-name)))
|
2013-06-02 13:56:07 +02:00
|
|
|
(if icon-spec
|
2016-01-22 05:50:31 +01:00
|
|
|
(mode-icons-propertize-mode mode-name icon-spec)
|
2013-06-02 13:56:07 +02:00
|
|
|
mode-name)))
|
2012-11-10 23:12:36 +01:00
|
|
|
|
2016-01-22 17:47:32 +01:00
|
|
|
(defvar mode-icons-cached-mode-name nil
|
|
|
|
"Cached mode name to restore when disabling mode-icons.")
|
|
|
|
|
2016-01-22 05:50:31 +01:00
|
|
|
(defun mode-icons-set-mode-icon (mode)
|
2013-06-02 13:56:07 +02:00
|
|
|
"Set the icon for MODE."
|
2016-01-22 17:47:32 +01:00
|
|
|
(unless mode-icons-cached-mode-name
|
|
|
|
(set (make-local-variable 'mode-icons-cached-mode-name)
|
2016-01-22 19:09:51 +01:00
|
|
|
mode-name)
|
2016-01-22 17:47:32 +01:00
|
|
|
(setq mode-name (mode-icons-get-mode-icon mode))))
|
|
|
|
|
|
|
|
(defun mode-icons-major-mode-icons-undo ()
|
|
|
|
"Undo the mode-name changes"
|
|
|
|
(dolist (b (buffer-list))
|
|
|
|
(with-current-buffer b
|
|
|
|
(when mode-icons-cached-mode-name
|
2016-01-22 19:09:51 +01:00
|
|
|
(setq mode-name mode-icons-cached-mode-name
|
|
|
|
mode-icons-cached-mode-name nil)))))
|
2016-01-22 17:47:32 +01:00
|
|
|
|
|
|
|
(defun mode-icons-major-mode-icons ()
|
|
|
|
"Apply mode name changes on all buffers."
|
|
|
|
(dolist (b (buffer-list))
|
|
|
|
(with-current-buffer b
|
|
|
|
(mode-icons-set-current-mode-icon))))
|
2012-11-10 23:12:36 +01:00
|
|
|
|
2016-01-22 05:50:31 +01:00
|
|
|
(defun mode-icons-set-current-mode-icon ()
|
2013-06-02 13:56:07 +02:00
|
|
|
"Set the icon for the current major mode."
|
2016-01-22 05:50:31 +01:00
|
|
|
(mode-icons-set-mode-icon mode-name))
|
2012-11-10 23:12:36 +01:00
|
|
|
|
2016-01-22 07:23:45 +01:00
|
|
|
(defvar mode-icons-set-minor-mode-icon-alist nil)
|
|
|
|
|
|
|
|
(defun mode-icons-set-minor-mode-icon-undo ()
|
|
|
|
(let (minor)
|
|
|
|
(dolist (mode mode-icons-set-minor-mode-icon-alist)
|
|
|
|
(setq minor (assq (car mode) minor-mode-alist))
|
|
|
|
(when minor
|
2016-01-22 19:09:51 +01:00
|
|
|
(setcdr minor (cdr mode)))))
|
2016-01-22 22:54:31 +01:00
|
|
|
(setq mode-icons-set-minor-mode-icon-alist nil)
|
|
|
|
(force-mode-line-update))
|
2016-01-22 07:23:45 +01:00
|
|
|
|
2016-01-22 16:31:03 +01:00
|
|
|
(defcustom mode-icons-separate-images-with-spaces t
|
|
|
|
"Separate minor-mode icons with spaces."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'mode-icons)
|
|
|
|
|
2016-01-22 07:23:45 +01:00
|
|
|
(defun mode-icons-set-minor-mode-icon ()
|
|
|
|
"Set the icon for the minor modes."
|
|
|
|
(let (icon-spec mode-name minor)
|
|
|
|
(dolist (mode minor-mode-alist)
|
|
|
|
(unless (assq (car mode) mode-icons-set-minor-mode-icon-alist)
|
2016-01-22 19:09:51 +01:00
|
|
|
(setq mode-name (format-mode-line mode)
|
|
|
|
icon-spec (mode-icons-get-icon-spec mode-name))
|
|
|
|
(when icon-spec
|
|
|
|
(setq minor (assq (car mode) minor-mode-alist))
|
|
|
|
(when minor
|
|
|
|
(or (assq (car mode) mode-icons-set-minor-mode-icon-alist)
|
|
|
|
(push (copy-sequence minor) mode-icons-set-minor-mode-icon-alist))
|
|
|
|
(setq mode-name (replace-regexp-in-string "^ " "" mode-name))
|
|
|
|
(setcdr minor (list (concat (or (and mode-icons-separate-images-with-spaces " ") "")
|
2016-01-23 00:31:53 +01:00
|
|
|
(mode-icons-propertize-mode mode-name icon-spec)))))))))
|
2016-01-22 19:05:08 +01:00
|
|
|
(force-mode-line-update))
|
2016-01-22 07:23:45 +01:00
|
|
|
|
2016-01-22 22:54:31 +01:00
|
|
|
(defun mode-icons--generate-minor-mode-list ()
|
|
|
|
"Extracts all rich strings necessary for the minor mode list."
|
2016-01-23 00:31:53 +01:00
|
|
|
(delete "" (mapcar (lambda(mode)
|
|
|
|
(let ((pm (eval `(propertize ,mode ,@mode-icons-minor-mode-base-text-properties))))
|
|
|
|
(unless (string= pm "")
|
|
|
|
(setq pm (concat " " pm)))
|
|
|
|
pm))
|
|
|
|
(split-string (format-mode-line minor-mode-alist)))))
|
2016-01-22 22:54:31 +01:00
|
|
|
|
|
|
|
;; Based on rich-minority by Artur Malabarba
|
|
|
|
(defvar mode-icons--backup-construct nil)
|
|
|
|
(defvar mode-icons--mode-line-construct
|
|
|
|
'(:eval (mode-icons--generate-minor-mode-list))
|
|
|
|
"Construct used to replace `minor-mode-alist'.")
|
|
|
|
|
|
|
|
(defun mode-icons-fix (&optional enable)
|
|
|
|
"Fix mode-icons."
|
|
|
|
(if enable
|
|
|
|
(let ((place (or (member 'minor-mode-alist mode-line-modes)
|
|
|
|
(cl-member-if
|
|
|
|
(lambda (x) (and (listp x)
|
|
|
|
(equal (car x) :propertize)
|
|
|
|
(equal (cadr x) '("" minor-mode-alist))))
|
|
|
|
mode-line-modes))))
|
|
|
|
(when place
|
|
|
|
(setq mode-icons--backup-construct (car place))
|
|
|
|
(setcar place mode-icons--mode-line-construct)))
|
|
|
|
(let ((place (member mode-icons--mode-line-construct mode-line-modes)))
|
|
|
|
(when place
|
|
|
|
(setcar place mode-icons--backup-construct)))))
|
|
|
|
|
2013-06-02 14:27:32 +02:00
|
|
|
;;;###autoload
|
2013-06-02 14:09:49 +02:00
|
|
|
(define-minor-mode mode-icons-mode
|
|
|
|
"Replace the name of the current major mode with an icon."
|
|
|
|
:global t
|
|
|
|
(if mode-icons-mode
|
2013-06-02 14:29:47 +02:00
|
|
|
(progn
|
2016-01-22 05:50:31 +01:00
|
|
|
(add-hook 'after-change-major-mode-hook 'mode-icons-set-current-mode-icon)
|
2016-01-22 19:09:51 +01:00
|
|
|
(add-hook 'after-change-major-mode-hook 'mode-icons-set-minor-mode-icon)
|
2016-01-22 22:54:31 +01:00
|
|
|
(mode-icons-fix t)
|
2016-01-22 19:09:51 +01:00
|
|
|
(mode-icons-set-minor-mode-icon)
|
|
|
|
(mode-icons-major-mode-icons))
|
2016-01-22 07:23:45 +01:00
|
|
|
(remove-hook 'after-change-major-mode-hook 'mode-icons-set-minor-mode-icon)
|
|
|
|
(remove-hook 'after-change-major-mode-hook 'mode-icons-set-current-mode-icon)
|
2016-01-22 17:47:32 +01:00
|
|
|
(mode-icons-set-minor-mode-icon-undo)
|
2016-01-22 22:54:31 +01:00
|
|
|
(mode-icons-major-mode-icons-undo)
|
|
|
|
(mode-icons-fix)))
|
2013-06-02 14:09:49 +02:00
|
|
|
|
2012-11-10 23:12:36 +01:00
|
|
|
(provide 'mode-icons)
|
|
|
|
;;; mode-icons.el ends here
|
2016-01-22 19:09:51 +01:00
|
|
|
;; Local Variables:
|
|
|
|
;; indent-tabs-mode: nil
|
|
|
|
;; End:
|