Cleanup code

- Enable `lexical-binding'.

- Remove `mode-icons--load-file-name' and add `mode-icons--directory'.
  The directory is what is needed and when developing inside Emacs the
  `load-file-name' variable isn't available. So now this variable
  defaults to `default-directory' when being evaluated in a buffer.

- Add/improve docstrings.

- Simplify `mode-icons'. Don't specify the entire image specification,
  but just enough for the rest of the code to figure out how to create
  this specification. `get-icon-display' is then used to create the
  image specification.

- Shorten the `set-mode-icon' function.
This commit is contained in:
Tom Willemse 2013-06-02 13:56:07 +02:00
parent efefc5ee58
commit 68fa015bf7

View file

@ -1,4 +1,4 @@
;;; mode-icons.el --- Show icons for modes ;;; mode-icons.el --- Show icons for modes -*- lexical-binding: t; -*-
;; Copyright (C) 2012 Tom Willemsen ;; Copyright (C) 2012 Tom Willemsen
@ -25,45 +25,66 @@
;;; Code: ;;; Code:
(defconst mode-icons--load-file-name load-file-name (defconst mode-icons--directory
(if load-file-name
(file-name-directory load-file-name)
default-directory)
"Where mode-icons was loaded from.") "Where mode-icons was loaded from.")
(defun mode-icons-get-icon-file (name) (defun mode-icons-get-icon-file (icon)
(concat (file-name-directory mode-icons--load-file-name) "Get the location of ICON.
"/icons/" name))
ICON should be a file name with extension. The result is the
absolute path to ICON."
(concat mode-icons--directory "/icons/" icon))
(defvar mode-icons (defvar mode-icons
`(("Emacs-Lisp" . (image :type xpm `(("Emacs-Lisp" "emacs" xpm)
:file ,(mode-icons-get-icon-file "emacs.xpm") ("Python" "python" xpm)
:ascent center)) ("Scheme" "scheme" xpm)
("Python" . (image :type xpm ("Lisp" "cl" xpm)
:file ,(mode-icons-get-icon-file "python.xpm") ("PHP" "php" xpm)
:ascent center)) ("HTML" "html" xpm)
("Scheme" . (image :type xpm ("Org" "org" xpm))
:file ,(mode-icons-get-icon-file "scheme.xpm") "Icons for major modes.
:ascent center))
("Lisp" . (image :type xpm Each specification is a list with the first element being the
:file ,(mode-icons-get-icon-file "cl.xpm") name of the major mode. The second the name of the icon file,
:ascent center)) without the extension. And the third being the type of icon.")
("PHP" . (image :type xpm
:file ,(mode-icons-get-icon-file "php.xpm") (defun get-icon-display (icon type)
:ascent center)) "Get the value for the display property of ICON having TYPE.
("HTML" . (image :type xpm
:file ,(mode-icons-get-icon-file "html.xpm") ICON should be a string naming the file of the icon, without its
:ascent center)) extension. Type should be a symbol designating the file type for
("Org" . (image :type xpm the icon."
:file ,(mode-icons-get-icon-file "org.xpm") (let ((icon-path (mode-icons-get-icon-file
:ascent center))) (concat icon "." (symbol-name type)))))
"Icons for major modes.") `(image :type ,type :file ,icon-path :ascent center)))
(defun propertize-mode (mode icon-spec)
"Propertize MODE with ICON-SPEC.
MODE should be a string, the name of the mode to propertize.
ICON-SPEC should be a specification from `mode-icons'."
(propertize
mode 'display (get-icon-display (nth 1 icon-spec) (nth 2 icon-spec))))
(defun get-mode-icon (mode)
"Get the icon for MODE, if there is one."
(let* ((mode-name (format-mode-line mode))
(icon-spec (assoc mode-name mode-icons)))
(if icon-spec
(propertize-mode mode-name icon-spec)
mode-name)))
(defun set-mode-icon (mode) (defun set-mode-icon (mode)
(setq mode (format-mode-line mode)) "Set the icon for MODE."
(let ((icon-spec (assoc mode mode-icons))) (setq mode-name (get-mode-icon mode)))
(when icon-spec
(setq mode-name (propertize mode 'display (cdr icon-spec))))))
;;;###autoload ;;;###autoload
(defun set-current-mode-icon () (defun set-current-mode-icon ()
"Set the icon for the current major mode."
(set-mode-icon mode-name)) (set-mode-icon mode-name))
(provide 'mode-icons) (provide 'mode-icons)