corfu 的官配是 kind-icon, 但是他需要 svg-lib,还要去下载一堆 svg,不是很喜欢,然后呢,kind-icon 官网那堆图标看起来总感觉大小不一,也不美观…然后想起来本来就已经安装了 all-the-icons 了,在以前使用 company-box 时候也是使用 all-the-icons 来显示图标的。所以仿照 kind-icon, 借鉴以前使用 company-box 时候的图标(也抄了点 centaur emacs 里面对于图标的配置,鸣谢一下),一起拼凑了一个 kind-all-the-icons.el 文件,可以用于显示 corfu 补全菜单的 icons。使用很简单,在 corfu 载入后 加上下面两句:
(require 'kind-all-the-icons)
(add-to-list 'corfu-margin-formatters
#'kind-all-the-icons-margin-formatter)
效果如下:
代码如下:
;;; kind-all-the-icons.el -*- lexical-binding: t; -*-
(require 'all-the-icons)
(defvar kind-all-the-icons--cache nil
"The cache of styled and padded label (text or icon).
An alist.")
(defun kind-all-the-icons-reset-cache ()
"Remove all cached icons from `kind-all-the-icons-mapping'."
(interactive)
(setq kind-all-the-icons--cache nil))
(defun kind-all-the-icons--set-default-clear-cache (&rest args)
(kind-all-the-icons-reset-cache)
(apply #'set-default args))
(defvar kind-all-the-icons--icons
`((unknown . ,(all-the-icons-material "find_in_page" :height 0.8 :v-adjust -0.15))
(text . ,(all-the-icons-faicon "text-width" :height 0.8 :v-adjust -0.02))
(method . ,(all-the-icons-faicon "cube" :height 0.8 :v-adjust -0.02 :face 'all-the-icons-purple))
(function . ,(all-the-icons-faicon "cube" :height 0.8 :v-adjust -0.02 :face 'all-the-icons-purple))
(fun . ,(all-the-icons-faicon "cube" :height 0.8 :v-adjust -0.02 :face 'all-the-icons-purple))
(constructor . ,(all-the-icons-faicon "cube" :height 0.8 :v-adjust -0.02 :face 'all-the-icons-purple))
(ctor . ,(all-the-icons-faicon "cube" :height 0.8 :v-adjust -0.02 :face 'all-the-icons-purple))
(field . ,(all-the-icons-octicon "tag" :height 0.85 :v-adjust 0 :face 'all-the-icons-lblue))
(variable . ,(all-the-icons-octicon "tag" :height 0.85 :v-adjust 0 :face 'all-the-icons-lblue))
(var . ,(all-the-icons-octicon "tag" :height 0.85 :v-adjust 0 :face 'all-the-icons-lblue))
(class . ,(all-the-icons-material "settings_input_component" :height 0.8 :v-adjust -0.15 :face 'all-the-icons-orange))
(interface . ,(all-the-icons-material "share" :height 0.8 :v-adjust -0.15 :face 'all-the-icons-lblue))
(i/f . ,(all-the-icons-material "share" :height 0.8 :v-adjust -0.15 :face 'all-the-icons-lblue))
(module . ,(all-the-icons-material "view_module" :height 0.8 :v-adjust -0.15 :face 'all-the-icons-lblue))
(mod . ,(all-the-icons-material "view_module" :height 0.8 :v-adjust -0.15 :face 'all-the-icons-lblue))
(property . ,(all-the-icons-faicon "wrench" :height 0.8 :v-adjust -0.02))
(prop . ,(all-the-icons-faicon "wrench" :height 0.8 :v-adjust -0.02))
(unit . ,(all-the-icons-material "settings_system_daydream" :height 0.8 :v-adjust -0.15))
(value . ,(all-the-icons-material "format_align_right" :height 0.8 :v-adjust -0.15 :face 'all-the-icons-lblue))
(enum . ,(all-the-icons-material "storage" :height 0.8 :v-adjust -0.15 :face 'all-the-icons-orange))
(keyword . ,(all-the-icons-material "filter_center_focus" :height 0.8 :v-adjust -0.15))
(k/w . ,(all-the-icons-material "filter_center_focus" :height 0.8 :v-adjust -0.15))
(snippet . ,(all-the-icons-material "format_align_center" :height 0.8 :v-adjust -0.15))
(sn . ,(all-the-icons-material "format_align_center" :height 0.8 :v-adjust -0.15))
(color . ,(all-the-icons-material "palette" :height 0.8 :v-adjust -0.15))
(file . ,(all-the-icons-faicon "file-o" :height 0.8 :v-adjust -0.02))
(reference . ,(all-the-icons-material "collections_bookmark" :height 0.8 :v-adjust -0.15))
(ref . ,(all-the-icons-material "collections_bookmark" :height 0.8 :v-adjust -0.15))
(folder . ,(all-the-icons-faicon "folder-open" :height 0.8 :v-adjust -0.02))
(dir . ,(all-the-icons-faicon "folder-open" :height 0.8 :v-adjust -0.02))
(enum-member . ,(all-the-icons-material "format_align_right" :height 0.8 :v-adjust -0.15))
(enummember . ,(all-the-icons-material "format_align_right" :height 0.8 :v-adjust -0.15))
(member . ,(all-the-icons-material "format_align_right" :height 0.8 :v-adjust -0.15))
(constant . ,(all-the-icons-faicon "square-o" :height 0.8 :v-adjust -0.1))
(const . ,(all-the-icons-faicon "square-o" :height 0.8 :v-adjust -0.1))
(struct . ,(all-the-icons-material "settings_input_component" :height 0.8 :v-adjust -0.15 :face 'all-the-icons-orange))
(event . ,(all-the-icons-octicon "zap" :height 0.8 :v-adjust 0 :face 'all-the-icons-orange))
(operator . ,(all-the-icons-material "control_point" :height 0.8 :v-adjust -0.15))
(op . ,(all-the-icons-material "control_point" :height 0.8 :v-adjust -0.15))
(type-parameter . ,(all-the-icons-faicon "arrows" :height 0.8 :v-adjust -0.02))
(param . ,(all-the-icons-faicon "arrows" :height 0.8 :v-adjust -0.02))
(template . ,(all-the-icons-material "format_align_left" :height 0.8 :v-adjust -0.15))
(t . ,(all-the-icons-material "find_in_page" :height 0.8 :v-adjust -0.15))))
(defsubst kind-all-the-icons--metadata-get (metadata type-name)
(or
(plist-get completion-extra-properties (intern (format ":%s" type-name)))
(cdr (assq (intern type-name) metadata))))
(defun kind-all-the-icons-formatted (kind)
"Format icon kind with all-the-icons"
(or (alist-get kind kind-all-the-icons--cache)
(let ((map (assq kind kind-all-the-icons--icons)))
(let* ((icon (if map
(cdr map)
(cdr (assq t kind-all-the-icons--icons))))
(half (/ (default-font-width) 2))
(pad (propertize " " 'display `(space :width (,half))))
(disp (concat pad icon pad)))
(setf (alist-get kind kind-all-the-icons--cache) disp)
disp))))
(defun kind-all-the-icons-margin-formatter (metadata)
"Return a margin-formatter function which produces kind icons.
METADATA is the completion metadata supplied by the caller (see
info node `(elisp)Programmed Completion'). To use, add this
function to the relevant margin-formatters list."
(if-let ((kind-func (kind-all-the-icons--metadata-get metadata "company-kind")))
(lambda (cand)
(if-let ((kind (funcall kind-func cand)))
(kind-all-the-icons-formatted kind)
(kind-all-the-icons-formatted t))))) ;; as a backup
(provide 'kind-all-the-icons)
;;; kind-all-the-icons.el ends here