unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 96f5ea6b08dda374b6414fca77ed5a7f8f898e83 10275 bytes (raw)
name: lisp/emacs-lisp/icons.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
 
;;; icons.el --- Handling icons  -*- lexical-binding:t -*-

;; Copyright (C) 2022 Free Software Foundation, Inc.

;; Author: Lars Ingebrigtsen <larsi@gnus.org>
;; Keywords: icons buttons

;; This file is part of GNU Emacs.

;; GNU Emacs 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.

;; GNU Emacs 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 GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

(require 'cl-lib)

(defface icon
  '((t :underline nil))
  "Face for buttons."
  :version "29.1"
  :group 'customize)

(defface icon-button
  '((((type x w32 ns haiku pgtk) (class color))
     :inherit icon
     :box (:line-width (3 . -1) :color "#404040" :style flat-button)
     :background "#808080"
     :foreground "black"))
  "Face for buttons."
  :version "29.1"
  :group 'customize)

(defcustom icon-preference '(image emoji symbol text)
  "List of icon types to use, in order of preference.
Emacs will choose the icon of the highest preference possible
on the current display, and \"degrade\" gracefully to an icon
type that's available."
  :version "29.1"
  :group 'customize
  :type '(repeat (choice (const :tag "Images" image)
                         (const :tag "Colorful Emojis" emoji)
                         (const :tag "Monochrome Symbols" symbol)
                         (const :tag "Text Only" text))))

(defmacro define-icon (name parent specification documentation &rest keywords)
  "Define an icon identified by NAME.
If non-nil, inherit the specification from PARENT.  Entries from
SPECIFICATION will override inherited specifications.

SPECIFICATION is an alist of entries where the first element is
the type, and the rest are icons of that type.  Valid types are
`image', `emoji', `symbol' and `text'.

KEYWORDS specify additional information.  Valid keywords are:

`:version': The first Emacs version to include this icon; this is
mandatory.

`:group': The customization group the icon belongs in; this is
inferred if not present.

`:help-echo': Informational text that explains what happens if
the icon is used as a button and you click it."
  (declare (indent 2))
  (unless (symbolp name)
    (error "NAME must be a symbol: %S" name))
  (unless (plist-get keywords :version)
    (error "There must be a :version keyword in `define-icon'"))
  `(icons--register ',name ',parent ,specification ,documentation
                    ',keywords))

(defun icons--register (name parent spec doc keywords)
  (put name 'icon--properties (list parent spec doc keywords))
  (custom-add-to-group
   (or (plist-get keywords :group)
       (custom-current-group))
   name 'custom-icon))

(defun icon-spec-keywords (spec)
  (seq-drop-while (lambda (e) (not (keywordp e))) (cdr spec)))

(defun icon-spec-values (spec)
  (seq-take-while (lambda (e) (not (keywordp e))) (cdr spec)))

(defun iconp (object)
  "Return nil if OBJECT is not an icon.
If OBJECT is an icon, return the icon properties."
  (get object 'icon--properties))

(defun icon-documentation (icon)
  "Return the documentation for ICON."
  (let ((props (iconp icon)))
    (unless props
      (user-error "%s is not a valid icon" icon))
    (nth 2 props)))

(defun icons--spec (icon)
  (nth 1 (iconp icon)))

(defun icons--copy-spec (spec)
  (mapcar #'copy-sequence spec))

(defun icon-complete-spec (icon &optional inhibit-theme inhibit-inheritance)
  "Return the merged spec for ICON."
  (pcase-let ((`(,parent ,spec _ _) (iconp icon)))
    ;; We destructively modify `spec' when merging, so copy it.
    (setq spec (icons--copy-spec spec))
    ;; Let the Customize theme override.
    (unless inhibit-theme
      (when-let ((theme-spec (cadr (car (get icon 'theme-icon)))))
        (setq spec (icons--merge-spec (icons--copy-spec theme-spec) spec))))
    ;; Inherit from the parent spec (recursively).
    (unless inhibit-inheritance
      (while parent
        (let ((parent-props (get parent 'icon--properties)))
          (when parent-props
            (setq spec (icons--merge-spec spec (cadr parent-props))))
          (setq parent (car parent-props)))))
    spec))

(defun icon-string (name)
  "Return a string suitable for display in the current buffer for icon NAME."
  (let ((props (iconp name)))
    (unless props
      (user-error "%s is not a valid icon" name))
    (pcase-let ((`(_ ,spec _ ,keywords) props))
      (setq spec (icon-complete-spec name))
      ;; We now have a full spec, so check the intersection of what
      ;; the user wants and what this Emacs is capable of showing.
      (let ((icon-string
             (catch 'found
               (dolist (type icon-preference)
                 (let* ((type-spec (assq type spec))
                        ;; Find the keywords at the end of the section
                        ;; (if any).
                        (type-keywords (icon-spec-keywords type-spec)))
                   ;; Go through all the variations in this section
                   ;; and return the first one we can display.
                   (dolist (icon (icon-spec-values type-spec))
                     (when-let ((result
                                 (icons--create type icon type-keywords)))
                       (throw 'found
                              (if-let ((face (plist-get type-keywords :face)))
                                  (propertize result 'face face)
                                result)))))))))
        (unless icon-string
          (error "Couldn't find any way to display the %s icon" name))
        (when-let ((help (plist-get keywords :help-echo)))
          (setq icon-string (propertize icon-string 'help-echo help)))
        (propertize icon-string 'rear-nonsticky t)))))

(defun icon-elements (name)
  "Return the elements of icon NAME.
The elements are represented as a plist where the keys are
`string', `face' and `display'.  The `image' element is only
present if the icon is represented by an image."
  (let ((string (icon-string name)))
    (list 'face (get-text-property 0 'face string)
          'image (get-text-property 0 'display string)
          'string (substring-no-properties string))))

(defun icons--merge-spec (merged parent-spec)
  (dolist (elem parent-spec)
    (let ((current (assq (car elem) merged)))
      (if (not current)
          ;; Just add the entry.
          (push elem merged)
        ;; See if there are any keywords to inherit.
        (let ((parent-keywords (icon-spec-keywords elem))
              (current-keywords (icon-spec-keywords current)))
          (while parent-keywords
            (unless (plist-get (car parent-keywords) current-keywords)
              (nconc current (take 2 parent-keywords))
              (setq parent-keywords (cddr parent-keywords))))))))
  merged)

(cl-defmethod icons--create ((_type (eql 'image)) icon keywords)
  (let ((file (if (file-name-absolute-p icon)
                  icon
                (and (fboundp 'image-search-load-path)
                     (image-search-load-path icon)))))
    (and (display-images-p)
         (fboundp 'image-supported-file-p)
         (image-supported-file-p file)
         (propertize
          " " 'display
          (if-let ((height (plist-get keywords :height)))
              (create-image file
                            nil nil
                            :height (if (eq height 'line)
                                        (window-default-line-height)
                                      height)
                            :scale 1
                            :rotation (plist-get keywords :rotation)
                            :ascent (or (plist-get keywords :ascent) 'center))
            (create-image file))))))

(cl-defmethod icons--create ((_type (eql 'emoji)) icon _keywords)
  (when-let ((font (and (display-multi-font-p)
                        ;; FIXME: This is not enough for ensuring
                        ;; display of color Emoji.
                        (car (internal-char-font nil ?🟠)))))
    (and (font-has-char-p font (aref icon 0))
         icon)))

(cl-defmethod icons--create ((_type (eql 'symbol)) icon _keywords)
  (and (cl-every #'char-displayable-p icon)
       icon))

(cl-defmethod icons--create ((_type (eql 'text)) icon _keywords)
  icon)

(define-icon button nil
  '((image :face icon-button)
    (emoji "🔵" :face icon)
    (symbol "●" :face icon-button)
    (text "button" :face icon-button))
  "Base icon for buttons."
  :version "29.1")

;;;###autoload
(defun describe-icon (icon)
  "Pop to a buffer to describe ICON."
  (interactive
   (list (intern (completing-read "Describe icon: " obarray 'iconp t))))
  (let ((help-buffer-under-preparation t))
    (help-setup-xref (list #'describe-icon icon)
		     (called-interactively-p 'interactive))
    (with-help-window (help-buffer)
      (with-current-buffer standard-output
        (insert "Icon: " (symbol-name icon) "\n\n")
        (insert "Documentation:\n"
                (substitute-command-keys (icon-documentation icon)))
        (ensure-empty-lines)
        (let ((spec (icon-complete-spec icon))
              (plain (icon-complete-spec icon t t)))
          (insert "Specification including inheritance and theming:\n")
          (icons--describe-spec spec)
          (unless (equal spec plain)
            (insert "\nSpecification not including inheritance and theming:\n")
            (icons--describe-spec plain)))))))

(defun icons--describe-spec (spec)
  (dolist (elem spec)
    (let ((type (car elem))
          (values (icon-spec-values elem))
          (keywords (icon-spec-keywords elem)))
      (when (or values keywords)
        (insert (format "\nType: %s\n" type))
        (dolist (value values)
          (insert (format "  %s\n" value)))
        (while keywords
          (insert (format "    %s: %s\n" (pop keywords) (pop keywords))))))))

(provide 'icons)

;;; icons.el ends here

debug log:

solving 96f5ea6b08 ...
found 96f5ea6b08 in https://yhetil.org/emacs-bugs/86bkrcijsf.fsf@mail.linkov.net/
found ff4f20c207 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 ff4f20c2071694eeb3c4b95fd7a8bf2c5b7e5108	lisp/emacs-lisp/icons.el

applying [1/1] https://yhetil.org/emacs-bugs/86bkrcijsf.fsf@mail.linkov.net/
diff --git a/lisp/emacs-lisp/icons.el b/lisp/emacs-lisp/icons.el
index ff4f20c207..96f5ea6b08 100644

Checking patch lisp/emacs-lisp/icons.el...
Applied patch lisp/emacs-lisp/icons.el cleanly.

index at:
100644 96f5ea6b08dda374b6414fca77ed5a7f8f898e83	lisp/emacs-lisp/icons.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).