dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

all-the-icons-ibuffer.el (7809B)


      1 ;;; all-the-icons-ibuffer.el --- Display icons for all buffers in ibuffer        -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2020 Vincent Zhang
      4 
      5 ;; Author: Vincent Zhang <seagle0128@gmail.com>
      6 ;; Homepage: https://github.com/seagle0128/all-the-icons-ibuffer
      7 ;; Version: 1.3.0
      8 ;; Package-Requires: ((emacs "24.4") (all-the-icons "2.2.0"))
      9 ;; Keywords: convenience, icons, ibuffer
     10 
     11 ;; This file is not part of GNU Emacs.
     12 
     13 ;;
     14 ;; This program is free software; you can redistribute it and/or
     15 ;; modify it under the terms of the GNU General Public License as
     16 ;; published by the Free Software Foundation; either version 2, or
     17 ;; (at your option) any later version.
     18 ;;
     19 ;; This program is distributed in the hope that it will be useful,
     20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     22 ;; General Public License for more details.
     23 ;;
     24 ;; You should have received a copy of the GNU General Public License
     25 ;; along with this program; see the file COPYING.  If not, write to
     26 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
     27 ;; Floor, Boston, MA 02110-1301, USA.
     28 ;;
     29 
     30 ;;; Commentary:
     31 
     32 ;; Display icons for all buffers in ibuffer.
     33 ;;
     34 ;; Install:
     35 ;; From melpa, `M-x package-install RET all-the-icons-ibuffer RET`.
     36 ;; (all-the-icons-ibuffer-mode 1)
     37 ;; or
     38 ;; (use-package all-the-icons-ibuffer-mode
     39 ;;   :ensure t
     40 ;;   :init (all-the-icons-ibuffer-mode 1))
     41 
     42 
     43 ;;; Code:
     44 
     45 (require 'ibuffer)
     46 (require 'all-the-icons)
     47 
     48 (defgroup all-the-icons-ibuffer nil
     49   "Display icons for all buffers in ibuffer."
     50   :group 'all-the-icons
     51   :group 'ibuffer
     52   :link '(url-link :tag "Homepage" "https://github.com/seagle0128/all-the-icons-ibuffer"))
     53 
     54 (defface all-the-icons-ibuffer-icon-face
     55   '((t (:inherit default)))
     56   "Face used for the icons while `all-the-icons-ibuffer-color-icon' is nil."
     57   :group 'all-the-icons-ibuffer)
     58 
     59 (defcustom all-the-icons-ibuffer-color-icon t
     60   "Whether display the colorful icons.
     61 
     62 It respects `all-the-icons-color-icons'."
     63   :group 'all-the-icons-ibuffer
     64   :type 'boolean)
     65 
     66 (defcustom all-the-icons-ibuffer-icon-size 1.0
     67   "The default icon size in ibuffer."
     68   :group 'all-the-icons-ibuffer
     69   :type 'number)
     70 
     71 (defcustom all-the-icons-ibuffer-icon-v-adjust 0.0
     72   "The default vertical adjustment of the icon in ibuffer."
     73   :group 'all-the-icons-ibuffer
     74   :type 'number)
     75 
     76 (defcustom all-the-icons-ibuffer-human-readable-size t
     77   "Use human readable file size in ibuffer."
     78   :group 'all-the-icons-ibuffer
     79   :type 'boolean)
     80 
     81 (defcustom all-the-icons-ibuffer-formats
     82   `((mark modified read-only ,(if (>= emacs-major-version 26) 'locked "")
     83           ;; Here you may adjust by replacing :right with :center or :left
     84           ;; According to taste, if you want the icon further from the name
     85           " " (icon 2 2 :left :elide)
     86           ,(propertize " " 'display `(space :align-to 8))
     87           (name 18 18 :left :elide)
     88           " " (size-h 9 -1 :right)
     89           " " (mode+ 16 16 :left :elide)
     90           " " filename-and-process+)
     91     (mark " " (name 16 -1) " " filename))
     92   "A list of ways to display buffer lines with `all-the-icons'.
     93 
     94 See `ibuffer-formats' for details."
     95   :group 'all-the-icons-ibuffer
     96   :type '(repeat sexp))
     97 
     98 
     99 
    100 ;; For alignment, the size of the name field should be the width of an icon
    101 ;;;###autoload(autoload 'ibuffer-make-column-icon "all-the-icons-ibuffer")
    102 (define-ibuffer-column icon
    103   (:name "  " :inline t)
    104   (let ((icon (if (and (buffer-file-name) (all-the-icons-auto-mode-match?))
    105                   (all-the-icons-icon-for-file (file-name-nondirectory (buffer-file-name))
    106                                                :height all-the-icons-ibuffer-icon-size
    107                                                :v-adjust all-the-icons-ibuffer-icon-v-adjust)
    108                 (all-the-icons-icon-for-mode major-mode
    109                                              :height all-the-icons-ibuffer-icon-size
    110                                              :v-adjust all-the-icons-ibuffer-icon-v-adjust))))
    111     (if (or (null icon) (symbolp icon))
    112         (setq icon (all-the-icons-faicon "file-o"
    113                                          :face (if all-the-icons-ibuffer-color-icon
    114                                                    'all-the-icons-dsilver
    115                                                  'all-the-icons-ibuffer-icon-face)
    116                                          :height (* 0.9 all-the-icons-ibuffer-icon-size)
    117                                          :v-adjust all-the-icons-ibuffer-icon-v-adjust))
    118       (let* ((props (get-text-property 0 'face icon))
    119              (family (plist-get props :family))
    120              (face (if all-the-icons-ibuffer-color-icon
    121                        (or (plist-get props :inherit) props)
    122                      'all-the-icons-ibuffer-icon-face))
    123              (new-face `(:inherit ,face
    124                          :family ,family
    125                          :height ,all-the-icons-ibuffer-icon-size)))
    126         (propertize icon 'face new-face)))))
    127 
    128 ;; Human readable file size for ibuffer
    129 ;;;###autoload(autoload 'ibuffer-make-column-size-h "all-the-icons-ibuffer")
    130 (define-ibuffer-column size-h
    131   (:name "Size"
    132    :inline t
    133    :props ('font-lock-face 'font-lock-comment-face)
    134    :header-mouse-map ibuffer-size-header-map
    135    :summarizer
    136    (lambda (column-strings)
    137      (let ((total 0))
    138        (dolist (string column-strings)
    139 	     (setq total
    140 	           ;; like, ewww ...
    141 	           (+ (float (string-to-number string))
    142 		          total)))
    143        (format "%.0f" total))))
    144   (let ((size (buffer-size)))
    145     (if all-the-icons-ibuffer-human-readable-size
    146         (file-size-human-readable size)
    147       (format "%s" (buffer-size)))))
    148 
    149 ;;;###autoload(autoload 'ibuffer-make-column-mode+ "all-the-icons-ibuffer")
    150 (define-ibuffer-column mode+
    151   (:name "Mode"
    152    :inline t
    153    :header-mouse-map ibuffer-mode-header-map
    154    :props ('font-lock-face 'font-lock-keyword-face
    155                            'mouse-face 'highlight
    156 	                       'keymap ibuffer-mode-name-map
    157 	                       'help-echo "mouse-2: filter by this mode"))
    158   (format-mode-line mode-name nil nil (current-buffer)))
    159 
    160 ;;;###autoload(autoload 'ibuffer-make-column-filename-and-process+ "all-the-icons-ibuffer")
    161 (define-ibuffer-column filename-and-process+
    162   (:name "Filename/Process"
    163    :props ('font-lock-face 'font-lock-string-face)
    164    :header-mouse-map ibuffer-filename/process-header-map
    165    :summarizer
    166    (lambda (strings)
    167      (setq strings (delete "" strings))
    168      (let ((procs 0)
    169 	       (files 0))
    170        (dolist (string strings)
    171          (when (get-text-property 1 'ibuffer-process string)
    172            (setq procs (1+ procs)))
    173 	     (setq files (1+ files)))
    174        (concat (cond ((zerop files) "No files")
    175 		             ((= 1 files) "1 file")
    176 		             (t (format "%d files" files)))
    177 	           ", "
    178 	           (cond ((zerop procs) "no processes")
    179 		             ((= 1 procs) "1 process")
    180 		             (t (format "%d processes" procs)))))))
    181   (let ((proc (get-buffer-process buffer))
    182 	    (filename (ibuffer-make-column-filename buffer mark)))
    183     (if proc
    184 	    (concat (propertize (format "(%s %s)" proc (process-status proc))
    185 			                'font-lock-face 'italic
    186                             'ibuffer-process proc)
    187 		        (if (> (length filename) 0)
    188 		            (format " %s" filename)
    189 		          ""))
    190       filename)))
    191 
    192 (defvar all-the-icons-ibuffer-old-formats ibuffer-formats)
    193 
    194 ;;;###autoload
    195 (define-minor-mode all-the-icons-ibuffer-mode
    196   "Display icons for all buffers in ibuffer."
    197   :lighter nil
    198   :global t
    199   (if all-the-icons-ibuffer-mode
    200       (setq ibuffer-formats all-the-icons-ibuffer-formats)
    201     (setq ibuffer-formats all-the-icons-ibuffer-old-formats)))
    202 
    203 (provide 'all-the-icons-ibuffer)
    204 
    205 ;;; all-the-icons-ibuffer.el ends here