mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-18 76bbd07de7add0f9d13c6914f158d19630fe2f62
commit | author | age
5cb5f7 1 ;;; helm-color.el --- colors and faces -*- lexical-binding: t -*-
C 2
3 ;; Copyright (C) 2012 ~ 2018 Thierry Volpiatto <thierry.volpiatto@gmail.com>
4
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
9
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 ;;; Code:
19 (require 'cl-lib)
20 (require 'helm)
21 (require 'helm-help)
22 (require 'helm-elisp)
23
24 ;;; Customize Face
25 ;;
26 ;;
27 (defun helm-custom-faces-init ()
28   "Initialize buffer for `helm-source-customize-face'."
29   (unless (helm-candidate-buffer)
30     (save-selected-window
31       (list-faces-display)
32       (message nil))
33     (helm-init-candidates-in-buffer
34         'global
35       (with-current-buffer (get-buffer "*Faces*")
36         (buffer-substring
37          (next-single-char-property-change (point-min) 'face)
38          (point-max))))
39     (kill-buffer "*Faces*")))
40
41 (defvar helm-source-customize-face
42   (helm-build-in-buffer-source "Customize Face"
43     :init 'helm-custom-faces-init
44     :get-line 'buffer-substring
45     :persistent-action (lambda (candidate)
46                          (helm-elisp--persistent-help
47                           (intern (car (split-string candidate)))
48                           'helm-describe-face))
49     :persistent-help "Describe face"
50     :action '(("Customize"
51                . (lambda (line)
52                    (customize-face (intern (car (split-string line))))))
53               ("Copy name"
54                . (lambda (line)
55                    (kill-new (car (split-string line " " t)))))))
56   "See (info \"(emacs)Faces\")")
57
58 ;;; Colors browser
59 ;;
60 ;;
61 (defun helm-colors-init ()
62   (unless (helm-candidate-buffer)
63     (save-selected-window
64       (list-colors-display)
65       (message nil))
66     (helm-init-candidates-in-buffer
67         'global
68       (with-current-buffer (get-buffer "*Colors*")
69         (buffer-string)))
70     (kill-buffer "*Colors*")))
71
72 (defun helm-color-insert-name (candidate)
73   (with-helm-current-buffer
74     (insert (helm-colors-get-name candidate))))
75
76 (defun helm-color-kill-name (candidate)
77   (kill-new (helm-colors-get-name candidate)))
78
79 (defun helm-color-insert-rgb (candidate)
80   (with-helm-current-buffer
81     (insert (helm-colors-get-rgb candidate))))
82
83 (defun helm-color-kill-rgb (candidate)
84   (kill-new (helm-colors-get-rgb candidate)))
85
86 (defun helm-color-run-insert-name ()
87   "Insert name of color from `helm-source-colors'"
88   (interactive)
89   (with-helm-alive-p (helm-exit-and-execute-action 'helm-color-insert-name)))
90 (put 'helm-color-run-insert-name 'helm-only t)
91
92 (defun helm-color-run-kill-name ()
93   "Kill name of color from `helm-source-colors'"
94   (interactive)
95   (with-helm-alive-p (helm-exit-and-execute-action 'helm-color-kill-name)))
96 (put 'helm-color-run-kill-name 'helm-only t)
97
98 (defun helm-color-run-insert-rgb ()
99   "Insert RGB of color from `helm-source-colors'"
100   (interactive)
101   (with-helm-alive-p (helm-exit-and-execute-action 'helm-color-insert-rgb)))
102 (put 'helm-color-run-insert-rgb 'helm-only t)
103
104 (defun helm-color-run-kill-rgb ()
105   "Kill RGB of color from `helm-source-colors'"
106   (interactive)
107   (with-helm-alive-p (helm-exit-and-execute-action 'helm-color-kill-rgb)))
108 (put 'helm-color-run-kill-rgb 'helm-only t)
109
110 (defvar helm-color-map
111   (let ((map (make-sparse-keymap)))
112     (set-keymap-parent map helm-map)
113     (define-key map (kbd "C-c n") 'helm-color-run-insert-name)
114     (define-key map (kbd "C-c N") 'helm-color-run-kill-name)
115     (define-key map (kbd "C-c r") 'helm-color-run-insert-rgb)
116     (define-key map (kbd "C-c R") 'helm-color-run-kill-rgb)
117     map))
118
119 (defvar helm-source-colors
120   (helm-build-in-buffer-source "Colors"
121     :init 'helm-colors-init
122     :get-line 'buffer-substring
123     :keymap helm-color-map
124     :persistent-help "Kill entry in RGB format."
125     :persistent-action 'helm-color-kill-rgb
126     :help-message 'helm-colors-help-message
127     :action
128     '(("Copy Name (C-c N)" . helm-color-kill-name)
129       ("Copy RGB (C-c R)" . helm-color-kill-rgb)
130       ("Insert Name (C-c n)" . helm-color-insert-name)
131       ("Insert RGB (C-c r)" . helm-color-insert-rgb))))
132
133 (defun helm-colors-get-name (candidate)
134   "Get color name."
135   (replace-regexp-in-string
136    " " ""
137    (with-temp-buffer
138      (insert (capitalize candidate))
139      (goto-char (point-min))
140      (search-forward-regexp "\\s-\\{2,\\}")
141      (delete-region (point) (point-max))
142      (buffer-string))))
143
144 (defun helm-colors-get-rgb (candidate)
145   "Get color RGB."
146   (replace-regexp-in-string
147    " " ""
148    (with-temp-buffer
149      (insert (capitalize candidate))
150      (goto-char (point-max))
151      (search-backward-regexp "\\s-\\{2,\\}")
152      (delete-region (point) (point-min))
153      (buffer-string))))
154
155 ;;;###autoload
156 (defun helm-colors ()
157   "Preconfigured `helm' for color."
158   (interactive)
159   (helm :sources '(helm-source-colors helm-source-customize-face)
160         :buffer "*helm colors*"))
161
162 (provide 'helm-color)
163
164 ;; Local Variables:
165 ;; byte-compile-warnings: (not obsolete)
166 ;; coding: utf-8
167 ;; indent-tabs-mode: nil
168 ;; End:
169
170 ;;; helm-color.el ends here