facemenu.el 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. ;;; facemenu.el --- create a face menu for interactively adding fonts to text
  2. ;; Copyright (C) 1994-1996, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Boris Goldowsky <boris@gnu.org>
  4. ;; Keywords: faces
  5. ;; Package: emacs
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This file defines a menu of faces (bold, italic, etc) which allows you to
  19. ;; set the face used for a region of the buffer. Some faces also have
  20. ;; keybindings, which are shown in the menu.
  21. ;;
  22. ;; The menu also contains submenus for indentation and justification-changing
  23. ;; commands.
  24. ;;; Usage:
  25. ;; Selecting a face from the menu or typing the keyboard equivalent will
  26. ;; change the region to use that face. If you use transient-mark-mode and the
  27. ;; region is not active, the face will be remembered and used for the next
  28. ;; insertion. It will be forgotten if you move point or make other
  29. ;; modifications before inserting or typing anything.
  30. ;;
  31. ;; Faces can be selected from the keyboard as well.
  32. ;; The standard keybindings are M-o (or ESC o) + letter:
  33. ;; M-o i = "set italic", M-o b = "set bold", etc.
  34. ;;; Customization:
  35. ;; An alternative set of keybindings that may be easier to type can be set up
  36. ;; using "Alt" or "Hyper" keys. This requires that you either have or create
  37. ;; an Alt or Hyper key on your keyboard. On my keyboard, there is a key
  38. ;; labeled "Alt", but to make it act as an Alt key I have to put this command
  39. ;; into my .xinitrc:
  40. ;; xmodmap -e "add Mod3 = Alt_L"
  41. ;; Or, I can make it into a Hyper key with this:
  42. ;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L"
  43. ;; Check with local X-perts for how to do it on your system.
  44. ;; Then you can define your keybindings with code like this in your .emacs:
  45. ;; (setq facemenu-keybindings
  46. ;; '((default . [?\H-d])
  47. ;; (bold . [?\H-b])
  48. ;; (italic . [?\H-i])
  49. ;; (bold-italic . [?\H-l])
  50. ;; (underline . [?\H-u])))
  51. ;; (facemenu-update)
  52. ;; (setq facemenu-keymap global-map)
  53. ;; (define-key global-map [?\H-c] 'facemenu-set-foreground) ; set fg color
  54. ;; (define-key global-map [?\H-C] 'facemenu-set-background) ; set bg color
  55. ;;
  56. ;; The order of the faces that appear in the menu and their keybindings can be
  57. ;; controlled by setting the variables `facemenu-keybindings' and
  58. ;; `facemenu-new-faces-at-end'. List faces that you want to use in documents
  59. ;; in `facemenu-listed-faces'.
  60. ;;; Known Problems:
  61. ;; Bold and Italic do not combine to create bold-italic if you select them
  62. ;; both, although most other combinations (eg bold + underline + some color)
  63. ;; do the intuitive thing.
  64. ;;
  65. ;; There is at present no way to display what the faces look like in
  66. ;; the menu itself.
  67. ;;
  68. ;; `list-faces-display' shows the faces in a different order than
  69. ;; this menu, which could be confusing. I do /not/ sort the list
  70. ;; alphabetically, because I like the default order: it puts the most
  71. ;; basic, common fonts first.
  72. ;;
  73. ;; Please send me any other problems, comments or ideas.
  74. ;;; Code:
  75. (eval-when-compile
  76. (require 'help)
  77. (require 'button))
  78. ;; Global bindings:
  79. (define-key global-map [C-down-mouse-2] 'facemenu-menu)
  80. (define-key global-map "\M-o" 'facemenu-keymap)
  81. (defgroup facemenu nil
  82. "Create a face menu for interactively adding fonts to text."
  83. :group 'faces
  84. :prefix "facemenu-")
  85. (defcustom facemenu-keybindings
  86. (mapcar 'purecopy
  87. '((default . "d")
  88. (bold . "b")
  89. (italic . "i")
  90. (bold-italic . "l") ; {bold} intersect {italic} = {l}
  91. (underline . "u")))
  92. "Alist of interesting faces and keybindings.
  93. Each element is itself a list: the car is the name of the face,
  94. the next element is the key to use as a keyboard equivalent of the menu item;
  95. the binding is made in `facemenu-keymap'.
  96. The faces specifically mentioned in this list are put at the top of
  97. the menu, in the order specified. All other faces which are defined
  98. in `facemenu-listed-faces' are listed after them, but get no
  99. keyboard equivalents.
  100. If you change this variable after loading facemenu.el, you will need to call
  101. `facemenu-update' to make it take effect."
  102. :type '(repeat (cons face string))
  103. :group 'facemenu)
  104. (defcustom facemenu-new-faces-at-end t
  105. "Where in the menu to insert newly-created faces.
  106. This should be nil to put them at the top of the menu, or t to put them
  107. just before \"Other\" at the end."
  108. :type 'boolean
  109. :group 'facemenu)
  110. (defvar facemenu-unlisted-faces
  111. `(modeline region secondary-selection highlight scratch-face
  112. ,(purecopy "^font-lock-") ,(purecopy "^gnus-") ,(purecopy "^message-")
  113. ,(purecopy "^ediff-") ,(purecopy "^term-") ,(purecopy "^vc-")
  114. ,(purecopy "^widget-") ,(purecopy "^custom-") ,(purecopy "^vm-"))
  115. "*List of faces that are of no interest to the user.")
  116. (make-obsolete-variable 'facemenu-unlisted-faces 'facemenu-listed-faces
  117. "22.1,\n and has no effect on the Face menu")
  118. (defcustom facemenu-listed-faces nil
  119. "List of faces to include in the Face menu.
  120. Each element should be a symbol, the name of a face.
  121. The \"basic \" faces in `facemenu-keybindings' are automatically
  122. added to the Face menu, and need not be in this list.
  123. This value takes effect when you load facemenu.el. If the
  124. list includes symbols which are not defined as faces, they
  125. are ignored; however, subsequently defining or creating
  126. those faces adds them to the menu then. You can call
  127. `facemenu-update' to recalculate the menu contents, such as
  128. if you change the value of this variable,
  129. If this variable is t, all faces that you apply to text
  130. using the face menu commands (even by name), and all faces
  131. that you define or create, are added to the menu. You may
  132. find it useful to set this variable to t temporarily while
  133. you define some faces, so that they will be added. However,
  134. if the value is no longer t and you call `facemenu-update',
  135. it will remove any faces not explicitly in the list."
  136. :type '(choice (const :tag "List all faces" t)
  137. (const :tag "None" nil)
  138. (repeat symbol))
  139. :group 'facemenu
  140. :version "22.1")
  141. (defvar facemenu-face-menu
  142. (let ((map (make-sparse-keymap "Face")))
  143. (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
  144. map)
  145. "Menu keymap for faces.")
  146. (defalias 'facemenu-face-menu facemenu-face-menu)
  147. (put 'facemenu-face-menu 'menu-enable '(facemenu-enable-faces-p))
  148. (defvar facemenu-foreground-menu
  149. (let ((map (make-sparse-keymap "Foreground Color")))
  150. (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-foreground))
  151. map)
  152. "Menu keymap for foreground colors.")
  153. (defalias 'facemenu-foreground-menu facemenu-foreground-menu)
  154. (put 'facemenu-foreground-menu 'menu-enable '(facemenu-enable-faces-p))
  155. (defvar facemenu-background-menu
  156. (let ((map (make-sparse-keymap "Background Color")))
  157. (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-background))
  158. map)
  159. "Menu keymap for background colors.")
  160. (defalias 'facemenu-background-menu facemenu-background-menu)
  161. (put 'facemenu-background-menu 'menu-enable '(facemenu-enable-faces-p))
  162. ;;; Condition for enabling menu items that set faces.
  163. (defun facemenu-enable-faces-p ()
  164. ;; Enable the facemenu if facemenu-add-face-function is defined
  165. ;; (e.g. in Tex-mode and SGML mode), or if font-lock is off.
  166. (or (not (and font-lock-mode font-lock-defaults))
  167. facemenu-add-face-function))
  168. (defvar facemenu-special-menu
  169. (let ((map (make-sparse-keymap "Special")))
  170. (define-key map [?s] (cons (purecopy "Remove Special")
  171. 'facemenu-remove-special))
  172. (define-key map [?t] (cons (purecopy "Intangible")
  173. 'facemenu-set-intangible))
  174. (define-key map [?v] (cons (purecopy "Invisible")
  175. 'facemenu-set-invisible))
  176. (define-key map [?r] (cons (purecopy "Read-Only")
  177. 'facemenu-set-read-only))
  178. map)
  179. "Menu keymap for non-face text-properties.")
  180. (defalias 'facemenu-special-menu facemenu-special-menu)
  181. (defvar facemenu-justification-menu
  182. (let ((map (make-sparse-keymap "Justification")))
  183. (define-key map [?c] (cons (purecopy "Center") 'set-justification-center))
  184. (define-key map [?b] (cons (purecopy "Full") 'set-justification-full))
  185. (define-key map [?r] (cons (purecopy "Right") 'set-justification-right))
  186. (define-key map [?l] (cons (purecopy "Left") 'set-justification-left))
  187. (define-key map [?u] (cons (purecopy "Unfilled") 'set-justification-none))
  188. map)
  189. "Submenu for text justification commands.")
  190. (defalias 'facemenu-justification-menu facemenu-justification-menu)
  191. (defvar facemenu-indentation-menu
  192. (let ((map (make-sparse-keymap "Indentation")))
  193. (define-key map [decrease-right-margin]
  194. (cons (purecopy "Indent Right Less") 'decrease-right-margin))
  195. (define-key map [increase-right-margin]
  196. (cons (purecopy "Indent Right More") 'increase-right-margin))
  197. (define-key map [decrease-left-margin]
  198. (cons (purecopy "Indent Less") 'decrease-left-margin))
  199. (define-key map [increase-left-margin]
  200. (cons (purecopy "Indent More") 'increase-left-margin))
  201. map)
  202. "Submenu for indentation commands.")
  203. (defalias 'facemenu-indentation-menu facemenu-indentation-menu)
  204. ;; This is split up to avoid an overlong line in loaddefs.el.
  205. (defvar facemenu-menu nil
  206. "Facemenu top-level menu keymap.")
  207. (setq facemenu-menu (make-sparse-keymap "Text Properties"))
  208. (let ((map facemenu-menu))
  209. (define-key map [dc] (cons (purecopy "Display Colors") 'list-colors-display))
  210. (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display))
  211. (define-key map [dp] (cons (purecopy "Describe Properties")
  212. 'describe-text-properties))
  213. (define-key map [ra] (list 'menu-item (purecopy "Remove Text Properties")
  214. 'facemenu-remove-all
  215. :enable 'mark-active))
  216. (define-key map [rm] (list 'menu-item (purecopy "Remove Face Properties")
  217. 'facemenu-remove-face-props
  218. :enable 'mark-active))
  219. (define-key map [s1] (list (purecopy "--"))))
  220. (let ((map facemenu-menu))
  221. (define-key map [in] (cons (purecopy "Indentation")
  222. 'facemenu-indentation-menu))
  223. (define-key map [ju] (cons (purecopy "Justification")
  224. 'facemenu-justification-menu))
  225. (define-key map [s2] (list (purecopy "--")))
  226. (define-key map [sp] (cons (purecopy "Special Properties")
  227. 'facemenu-special-menu))
  228. (define-key map [bg] (cons (purecopy "Background Color")
  229. 'facemenu-background-menu))
  230. (define-key map [fg] (cons (purecopy "Foreground Color")
  231. 'facemenu-foreground-menu))
  232. (define-key map [fc] (cons (purecopy "Face")
  233. 'facemenu-face-menu)))
  234. (defalias 'facemenu-menu facemenu-menu)
  235. (defvar facemenu-keymap
  236. (let ((map (make-sparse-keymap "Set face")))
  237. (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
  238. (define-key map "\M-o" 'font-lock-fontify-block)
  239. map)
  240. "Keymap for face-changing commands.
  241. `Facemenu-update' fills in the keymap according to the bindings
  242. requested in `facemenu-keybindings'.")
  243. (defalias 'facemenu-keymap facemenu-keymap)
  244. (defcustom facemenu-add-face-function nil
  245. "Function called at beginning of text to change or nil.
  246. This function is passed the FACE to set and END of text to change, and must
  247. return a string which is inserted. It may set `facemenu-end-add-face'."
  248. :type '(choice (const :tag "None" nil)
  249. function)
  250. :group 'facemenu)
  251. (defcustom facemenu-end-add-face nil
  252. "String to insert or function called at end of text to change or nil.
  253. This function is passed the FACE to set, and must return a string which is
  254. inserted."
  255. :type '(choice (const :tag "None" nil)
  256. string
  257. function)
  258. :group 'facemenu)
  259. (defcustom facemenu-remove-face-function nil
  260. "When non-nil, this is a function called to remove faces.
  261. This function is passed the START and END of text to change.
  262. May also be t meaning to use `facemenu-add-face-function'."
  263. :type '(choice (const :tag "None" nil)
  264. (const :tag "Use add-face" t)
  265. function)
  266. :group 'facemenu)
  267. ;;; Internal Variables
  268. (defvar facemenu-color-alist nil
  269. "Alist of colors, used for completion.
  270. If this is nil, then the value of (defined-colors) is used.")
  271. (defun facemenu-update ()
  272. "Add or update the \"Face\" menu in the menu bar.
  273. You can call this to update things if you change any of the menu configuration
  274. variables."
  275. (interactive)
  276. ;; Add each defined face to the menu.
  277. (facemenu-iterate 'facemenu-add-new-face
  278. (facemenu-complete-face-list facemenu-keybindings)))
  279. (defun facemenu-set-face (face &optional start end)
  280. "Apply FACE to the region or next character typed.
  281. If the region is active (normally true except in Transient
  282. Mark mode) and nonempty, and there is no prefix argument,
  283. this command applies FACE to the region. Otherwise, it applies FACE
  284. to the faces to use for the next character
  285. inserted. (Moving point or switching buffers before typing
  286. a character to insert cancels the specification.)
  287. If FACE is `default', to \"apply\" it means clearing
  288. the list of faces to be used. For any other value of FACE,
  289. to \"apply\" it means putting FACE at the front of the list
  290. of faces to be used, and removing any faces further
  291. along in the list that would be completely overridden by
  292. preceding faces (including FACE).
  293. This command can also add FACE to the menu of faces,
  294. if `facemenu-listed-faces' says to do that."
  295. (interactive (list (progn
  296. (barf-if-buffer-read-only)
  297. (read-face-name "Use face"))
  298. (if (and mark-active (not current-prefix-arg))
  299. (region-beginning))
  300. (if (and mark-active (not current-prefix-arg))
  301. (region-end))))
  302. (facemenu-add-new-face face)
  303. (facemenu-add-face face start end))
  304. (defun facemenu-set-foreground (color &optional start end)
  305. "Set the foreground COLOR of the region or next character typed.
  306. This command reads the color in the minibuffer.
  307. If the region is active (normally true except in Transient Mark mode)
  308. and there is no prefix argument, this command sets the region to the
  309. requested face.
  310. Otherwise, this command specifies the face for the next character
  311. inserted. Moving point or switching buffers before
  312. typing a character to insert cancels the specification."
  313. (interactive (list (progn
  314. (barf-if-buffer-read-only)
  315. (read-color "Foreground color: "))
  316. (if (and mark-active (not current-prefix-arg))
  317. (region-beginning))
  318. (if (and mark-active (not current-prefix-arg))
  319. (region-end))))
  320. (facemenu-set-face-from-menu
  321. (facemenu-add-new-color color 'facemenu-foreground-menu)
  322. start end))
  323. (defun facemenu-set-background (color &optional start end)
  324. "Set the background COLOR of the region or next character typed.
  325. This command reads the color in the minibuffer.
  326. If the region is active (normally true except in Transient Mark mode)
  327. and there is no prefix argument, this command sets the region to the
  328. requested face.
  329. Otherwise, this command specifies the face for the next character
  330. inserted. Moving point or switching buffers before
  331. typing a character to insert cancels the specification."
  332. (interactive (list (progn
  333. (barf-if-buffer-read-only)
  334. (read-color "Background color: "))
  335. (if (and mark-active (not current-prefix-arg))
  336. (region-beginning))
  337. (if (and mark-active (not current-prefix-arg))
  338. (region-end))))
  339. (facemenu-set-face-from-menu
  340. (facemenu-add-new-color color 'facemenu-background-menu)
  341. start end))
  342. (defun facemenu-set-face-from-menu (face start end)
  343. "Set the FACE of the region or next character typed.
  344. This function is designed to be called from a menu; FACE is determined
  345. using the event type of the menu entry. If FACE is a symbol whose
  346. name starts with \"fg:\" or \"bg:\", then this functions sets the
  347. foreground or background to the color specified by the rest of the
  348. symbol's name. Any other symbol is considered the name of a face.
  349. If the region is active (normally true except in Transient Mark mode)
  350. and there is no prefix argument, this command sets the region to the
  351. requested face.
  352. Otherwise, this command specifies the face for the next character
  353. inserted. Moving point or switching buffers before typing a character
  354. to insert cancels the specification."
  355. (interactive (list last-command-event
  356. (if (and mark-active (not current-prefix-arg))
  357. (region-beginning))
  358. (if (and mark-active (not current-prefix-arg))
  359. (region-end))))
  360. (barf-if-buffer-read-only)
  361. (facemenu-add-face
  362. (let ((fn (symbol-name face)))
  363. (if (string-match "\\`\\([fb]\\)g:\\(.+\\)" fn)
  364. (list (list (if (string= (match-string 1 fn) "f")
  365. :foreground
  366. :background)
  367. (match-string 2 fn)))
  368. face))
  369. start end))
  370. (defun facemenu-set-invisible (start end)
  371. "Make the region invisible.
  372. This sets the `invisible' text property; it can be undone with
  373. `facemenu-remove-special'."
  374. (interactive "r")
  375. (add-text-properties start end '(invisible t)))
  376. (defun facemenu-set-intangible (start end)
  377. "Make the region intangible: disallow moving into it.
  378. This sets the `intangible' text property; it can be undone with
  379. `facemenu-remove-special'."
  380. (interactive "r")
  381. (add-text-properties start end '(intangible t)))
  382. (defun facemenu-set-read-only (start end)
  383. "Make the region unmodifiable.
  384. This sets the `read-only' text property; it can be undone with
  385. `facemenu-remove-special'."
  386. (interactive "r")
  387. (add-text-properties start end '(read-only t)))
  388. (defun facemenu-remove-face-props (start end)
  389. "Remove `face' and `mouse-face' text properties."
  390. (interactive "*r") ; error if buffer is read-only despite the next line.
  391. (let ((inhibit-read-only t))
  392. (remove-text-properties
  393. start end '(face nil mouse-face nil))))
  394. (defun facemenu-remove-all (start end)
  395. "Remove all text properties from the region."
  396. (interactive "*r") ; error if buffer is read-only despite the next line.
  397. (let ((inhibit-read-only t))
  398. (set-text-properties start end nil)))
  399. (defun facemenu-remove-special (start end)
  400. "Remove all the \"special\" text properties from the region.
  401. These special properties include `invisible', `intangible' and `read-only'."
  402. (interactive "*r") ; error if buffer is read-only despite the next line.
  403. (let ((inhibit-read-only t))
  404. (remove-text-properties
  405. start end '(invisible nil intangible nil read-only nil))))
  406. (defalias 'facemenu-read-color 'read-color)
  407. (defcustom list-colors-sort nil
  408. "Color sort order for `list-colors-display'.
  409. `nil' means default implementation-dependent order (defined in `x-colors').
  410. `name' sorts by color name.
  411. `rgb' sorts by red, green, blue components.
  412. `(rgb-dist . COLOR)' sorts by the RGB distance to the specified color.
  413. `hsv' sorts by hue, saturation, value.
  414. `(hsv-dist . COLOR)' sorts by the HSV distance to the specified color
  415. and excludes grayscale colors."
  416. :type '(choice (const :tag "Unsorted" nil)
  417. (const :tag "Color Name" name)
  418. (const :tag "Red-Green-Blue" rgb)
  419. (cons :tag "Distance on RGB cube"
  420. (const :tag "Distance from Color" rgb-dist)
  421. (color :tag "Source Color Name"))
  422. (const :tag "Hue-Saturation-Value" hsv)
  423. (cons :tag "Distance on HSV cylinder"
  424. (const :tag "Distance from Color" hsv-dist)
  425. (color :tag "Source Color Name")))
  426. :group 'facemenu
  427. :version "24.1")
  428. (defun list-colors-sort-key (color)
  429. "Return a list of keys for sorting colors depending on `list-colors-sort'.
  430. COLOR is the name of the color. When return value is nil,
  431. filter out the color from the output."
  432. (require 'color)
  433. (cond
  434. ((null list-colors-sort) color)
  435. ((eq list-colors-sort 'name)
  436. (downcase color))
  437. ((eq list-colors-sort 'rgb)
  438. (color-values color))
  439. ((eq (car-safe list-colors-sort) 'rgb-dist)
  440. (color-distance color (cdr list-colors-sort)))
  441. ((eq list-colors-sort 'hsv)
  442. (apply 'color-rgb-to-hsv (color-name-to-rgb color)))
  443. ((eq (car-safe list-colors-sort) 'hsv-dist)
  444. (let* ((c-rgb (color-name-to-rgb color))
  445. (c-hsv (apply 'color-rgb-to-hsv c-rgb))
  446. (o-hsv (apply 'color-rgb-to-hsv
  447. (color-name-to-rgb (cdr list-colors-sort)))))
  448. (unless (and (eq (nth 0 c-rgb) (nth 1 c-rgb)) ; exclude grayscale
  449. (eq (nth 1 c-rgb) (nth 2 c-rgb)))
  450. ;; 3D Euclidean distance (sqrt is not needed for sorting)
  451. (+ (expt (- 180 (abs (- 180 (abs (- (nth 0 c-hsv) ; wrap hue
  452. (nth 0 o-hsv)))))) 2)
  453. (expt (- (nth 1 c-hsv) (nth 1 o-hsv)) 2)
  454. (expt (- (nth 2 c-hsv) (nth 2 o-hsv)) 2)))))))
  455. (defun list-colors-display (&optional list buffer-name callback)
  456. "Display names of defined colors, and show what they look like.
  457. If the optional argument LIST is non-nil, it should be a list of
  458. colors to display. Otherwise, this command computes a list of
  459. colors that the current display can handle. Customize
  460. `list-colors-sort' to change the order in which colors are shown.
  461. If the optional argument BUFFER-NAME is nil, it defaults to *Colors*.
  462. If the optional argument CALLBACK is non-nil, it should be a
  463. function to call each time the user types RET or clicks on a
  464. color. The function should accept a single argument, the color name."
  465. (interactive)
  466. (when (and (null list) (> (display-color-cells) 0))
  467. (setq list (list-colors-duplicates (defined-colors)))
  468. (when list-colors-sort
  469. ;; Schwartzian transform with `(color key1 key2 key3 ...)'.
  470. (setq list (mapcar
  471. 'car
  472. (sort (delq nil (mapcar
  473. (lambda (c)
  474. (let ((key (list-colors-sort-key
  475. (car c))))
  476. (when key
  477. (cons c (if (consp key) key
  478. (list key))))))
  479. list))
  480. (lambda (a b)
  481. (let* ((a-keys (cdr a))
  482. (b-keys (cdr b))
  483. (a-key (car a-keys))
  484. (b-key (car b-keys)))
  485. ;; Skip common keys at the beginning of key lists.
  486. (while (and a-key b-key (equal a-key b-key))
  487. (setq a-keys (cdr a-keys) a-key (car a-keys)
  488. b-keys (cdr b-keys) b-key (car b-keys)))
  489. (cond
  490. ((and (numberp a-key) (numberp b-key))
  491. (< a-key b-key))
  492. ((and (stringp a-key) (stringp b-key))
  493. (string< a-key b-key)))))))))
  494. (when (memq (display-visual-class) '(gray-scale pseudo-color direct-color))
  495. ;; Don't show more than what the display can handle.
  496. (let ((lc (nthcdr (1- (display-color-cells)) list)))
  497. (if lc
  498. (setcdr lc nil)))))
  499. (unless buffer-name
  500. (setq buffer-name "*Colors*"))
  501. (with-help-window buffer-name
  502. (with-current-buffer standard-output
  503. (erase-buffer)
  504. (list-colors-print list callback)
  505. (set-buffer-modified-p nil)
  506. (setq truncate-lines t)))
  507. (when callback
  508. (pop-to-buffer buffer-name)
  509. (message "Click on a color to select it.")))
  510. (defun list-colors-print (list &optional callback)
  511. (let ((callback-fn
  512. (if callback
  513. `(lambda (button)
  514. (funcall ,callback (button-get button 'color-name))))))
  515. (dolist (color list)
  516. (if (consp color)
  517. (if (cdr color)
  518. (setq color (sort color (lambda (a b)
  519. (string< (downcase a)
  520. (downcase b))))))
  521. (setq color (list color)))
  522. (let* ((opoint (point))
  523. (color-values (color-values (car color)))
  524. (light-p (>= (apply 'max color-values)
  525. (* (car (color-values "white")) .5))))
  526. (insert (car color))
  527. (indent-to 22)
  528. (put-text-property opoint (point) 'face `(:background ,(car color)))
  529. (put-text-property
  530. (prog1 (point)
  531. (insert " ")
  532. ;; Insert all color names.
  533. (insert (mapconcat 'identity color ",")))
  534. (point)
  535. 'face (list :foreground (car color)))
  536. (insert (propertize " " 'display '(space :align-to (- right 9))))
  537. (insert " ")
  538. (insert (propertize
  539. (apply 'format "#%02x%02x%02x"
  540. (mapcar (lambda (c) (lsh c -8))
  541. color-values))
  542. 'mouse-face 'highlight
  543. 'help-echo
  544. (let ((hsv (apply 'color-rgb-to-hsv
  545. (color-name-to-rgb (car color)))))
  546. (format "H:%d S:%d V:%d"
  547. (nth 0 hsv) (nth 1 hsv) (nth 2 hsv)))))
  548. (when callback
  549. (make-text-button
  550. opoint (point)
  551. 'follow-link t
  552. 'mouse-face (list :background (car color)
  553. :foreground (if light-p "black" "white"))
  554. 'color-name (car color)
  555. 'action callback-fn)))
  556. (insert "\n"))
  557. (goto-char (point-min))))
  558. (defun list-colors-duplicates (&optional list)
  559. "Return a list of colors with grouped duplicate colors.
  560. If a color has no duplicates, then the element of the returned list
  561. has the form '(COLOR-NAME). The element of the returned list with
  562. duplicate colors has the form '(COLOR-NAME DUPLICATE-COLOR-NAME ...).
  563. This function uses the predicate `facemenu-color-equal' to compare
  564. color names. If the optional argument LIST is non-nil, it should
  565. be a list of colors to display. Otherwise, this function uses
  566. a list of colors that the current display can handle."
  567. (let* ((list (mapcar 'list (or list (defined-colors))))
  568. (l list))
  569. (while (cdr l)
  570. (if (and (facemenu-color-equal (car (car l)) (car (car (cdr l))))
  571. ;; On MS-Windows, there are logical colors that might have
  572. ;; the same value but different names and meanings. For
  573. ;; example, `SystemMenuText' (the color w32 uses for the
  574. ;; text in menu entries) and `SystemWindowText' (the default
  575. ;; color w32 uses for the text in windows and dialogs) may
  576. ;; be the same display color and be adjacent in the list.
  577. ;; These system colors all have names prefixed with "System",
  578. ;; which is hardcoded in w32fns.c (SYSTEM_COLOR_PREFIX).
  579. ;; This makes them different to any other color. Bug#9722
  580. (not (and (eq system-type 'windows-nt)
  581. (string-match-p "^System" (car (car l))))))
  582. (progn
  583. (setcdr (car l) (cons (car (car (cdr l))) (cdr (car l))))
  584. (setcdr l (cdr (cdr l))))
  585. (setq l (cdr l))))
  586. list))
  587. (defun facemenu-color-equal (a b)
  588. "Return t if colors A and B are the same color.
  589. A and B should be strings naming colors.
  590. This function queries the display system to find out what the color
  591. names mean. It returns nil if the colors differ or if it can't
  592. determine the correct answer."
  593. (cond ((equal a b) t)
  594. ((equal (color-values a) (color-values b)))))
  595. (defvar facemenu-self-insert-data nil)
  596. (defun facemenu-post-self-insert-function ()
  597. (when (and (car facemenu-self-insert-data)
  598. (eq last-command (cdr facemenu-self-insert-data)))
  599. (put-text-property (1- (point)) (point)
  600. 'face (car facemenu-self-insert-data))
  601. (setq facemenu-self-insert-data nil))
  602. (remove-hook 'post-self-insert-hook 'facemenu-post-self-insert-function))
  603. (defun facemenu-set-self-insert-face (face)
  604. "Arrange for the next self-inserted char to have face `face'."
  605. (setq facemenu-self-insert-data (cons face this-command))
  606. (add-hook 'post-self-insert-hook 'facemenu-post-self-insert-function))
  607. (defun facemenu-add-face (face &optional start end)
  608. "Add FACE to text between START and END.
  609. If START is nil or START to END is empty, add FACE to next typed character
  610. instead. For each section of that region that has a different face property,
  611. FACE will be consed onto it, and other faces that are completely hidden by
  612. that will be removed from the list.
  613. If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil,
  614. they are used to set the face information.
  615. As a special case, if FACE is `default', then the region is left with NO face
  616. text property. Otherwise, selecting the default face would not have any
  617. effect. See `facemenu-remove-face-function'."
  618. (interactive "*xFace: \nr")
  619. (cond
  620. ((and (eq face 'default)
  621. (not (eq facemenu-remove-face-function t)))
  622. (if facemenu-remove-face-function
  623. (funcall facemenu-remove-face-function start end)
  624. (if (and start (< start end))
  625. (remove-text-properties start end '(face default))
  626. (facemenu-set-self-insert-face 'default))))
  627. (facemenu-add-face-function
  628. (save-excursion
  629. (if end (goto-char end))
  630. (save-excursion
  631. (if start (goto-char start))
  632. (insert-before-markers
  633. (funcall facemenu-add-face-function face end)))
  634. (if facemenu-end-add-face
  635. (insert (if (stringp facemenu-end-add-face)
  636. facemenu-end-add-face
  637. (funcall facemenu-end-add-face face))))))
  638. ((and start (< start end))
  639. (let ((part-start start) part-end)
  640. (while (not (= part-start end))
  641. (setq part-end (next-single-property-change part-start 'face
  642. nil end))
  643. (let ((prev (get-text-property part-start 'face)))
  644. (put-text-property part-start part-end 'face
  645. (if (null prev)
  646. face
  647. (facemenu-active-faces
  648. (cons face
  649. (if (listp prev)
  650. prev
  651. (list prev)))
  652. ;; Specify the selected frame
  653. ;; because nil would mean to use
  654. ;; the new-frame default settings,
  655. ;; and those are usually nil.
  656. (selected-frame)))))
  657. (setq part-start part-end))))
  658. (t
  659. (facemenu-set-self-insert-face
  660. (if (eq last-command (cdr facemenu-self-insert-data))
  661. (cons face (if (listp (car facemenu-self-insert-data))
  662. (car facemenu-self-insert-data)
  663. (list (car facemenu-self-insert-data))))
  664. face))))
  665. (unless (facemenu-enable-faces-p)
  666. (message "Font-lock mode will override any faces you set in this buffer")))
  667. (defun facemenu-active-faces (face-list &optional frame)
  668. "Return from FACE-LIST those faces that would be used for display.
  669. This means each face attribute is not specified in a face earlier in FACE-LIST
  670. and such a face is therefore active when used to display text.
  671. If the optional argument FRAME is given, use the faces in that frame; otherwise
  672. use the selected frame. If t, then the global, non-frame faces are used."
  673. (let* ((mask-atts (copy-sequence
  674. (if (consp (car face-list))
  675. (face-attributes-as-vector (car face-list))
  676. (or (internal-lisp-face-p (car face-list) frame)
  677. (check-face (car face-list))))))
  678. (active-list (list (car face-list)))
  679. (face-list (cdr face-list))
  680. (mask-len (length mask-atts)))
  681. (while face-list
  682. (if (let ((face-atts
  683. (if (consp (car face-list))
  684. (face-attributes-as-vector (car face-list))
  685. (or (internal-lisp-face-p (car face-list) frame)
  686. (check-face (car face-list)))))
  687. (i mask-len)
  688. (useful nil))
  689. (while (>= (setq i (1- i)) 0)
  690. (and (not (memq (aref face-atts i) '(nil unspecified)))
  691. (memq (aref mask-atts i) '(nil unspecified))
  692. (aset mask-atts i (setq useful t))))
  693. useful)
  694. (setq active-list (cons (car face-list) active-list)))
  695. (setq face-list (cdr face-list)))
  696. (nreverse active-list)))
  697. (defun facemenu-add-new-face (face)
  698. "Add FACE (a face) to the Face menu if `facemenu-listed-faces' says so.
  699. This is called whenever you create a new face, and at other times."
  700. (let* (name
  701. symbol
  702. menu docstring
  703. (key (cdr (assoc face facemenu-keybindings)))
  704. function menu-val)
  705. (if (symbolp face)
  706. (setq name (symbol-name face)
  707. symbol face)
  708. (setq name face
  709. symbol (intern name)))
  710. (setq menu 'facemenu-face-menu)
  711. (setq docstring
  712. (purecopy (format "Select face `%s' for subsequent insertion.
  713. If the mark is active and there is no prefix argument,
  714. apply face `%s' to the region instead.
  715. This command was defined by `facemenu-add-new-face'."
  716. name name)))
  717. (cond ((facemenu-iterate ; check if equivalent face is already in the menu
  718. (lambda (m) (and (listp m)
  719. (symbolp (car m))
  720. ;; Avoid error in face-equal
  721. ;; when a non-face is erroneously present.
  722. (facep (car m))
  723. (face-equal (car m) symbol)))
  724. (cdr (symbol-function menu))))
  725. ;; Faces with a keyboard equivalent. These go at the front.
  726. (key
  727. (setq function (intern (concat "facemenu-set-" name)))
  728. (fset function
  729. `(lambda ()
  730. ,docstring
  731. (interactive)
  732. (facemenu-set-face
  733. (quote ,symbol)
  734. (if (and mark-active (not current-prefix-arg))
  735. (region-beginning))
  736. (if (and mark-active (not current-prefix-arg))
  737. (region-end)))))
  738. (define-key 'facemenu-keymap key (cons name function))
  739. (define-key menu key (cons name function)))
  740. ;; Faces with no keyboard equivalent. Figure out where to put it:
  741. ((or (eq t facemenu-listed-faces)
  742. (memq symbol facemenu-listed-faces))
  743. (setq key (vector symbol)
  744. function 'facemenu-set-face-from-menu
  745. menu-val (symbol-function menu))
  746. (if (and facemenu-new-faces-at-end
  747. (> (length menu-val) 3))
  748. (define-key-after menu-val key (cons name function)
  749. (car (nth (- (length menu-val) 3) menu-val)))
  750. (define-key menu key (cons name function))))))
  751. nil) ; Return nil for facemenu-iterate
  752. (defun facemenu-add-new-color (color menu)
  753. "Add COLOR (a color name string) to the appropriate Face menu.
  754. MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'.
  755. Return the event type (a symbol) of the added menu entry.
  756. This is called whenever you use a new color."
  757. (let (symbol)
  758. (unless (color-defined-p color)
  759. (error "Color `%s' undefined" color))
  760. (cond ((eq menu 'facemenu-foreground-menu)
  761. (setq symbol (intern (concat "fg:" color))))
  762. ((eq menu 'facemenu-background-menu)
  763. (setq symbol (intern (concat "bg:" color))))
  764. (t (error "MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'")))
  765. (unless (facemenu-iterate ; Check if color is already in the menu.
  766. (lambda (m) (and (listp m)
  767. (eq (car m) symbol)))
  768. (cdr (symbol-function menu)))
  769. ;; Color is not in the menu. Figure out where to put it.
  770. (let ((key (vector symbol))
  771. (function 'facemenu-set-face-from-menu)
  772. (menu-val (symbol-function menu)))
  773. (if (and facemenu-new-faces-at-end
  774. (> (length menu-val) 3))
  775. (define-key-after menu-val key (cons color function)
  776. (car (nth (- (length menu-val) 3) menu-val)))
  777. (define-key menu key (cons color function)))))
  778. symbol))
  779. (defun facemenu-complete-face-list (&optional oldlist)
  780. "Return list of all faces that look different.
  781. Starts with given ALIST of faces, and adds elements only if they display
  782. differently from any face already on the list.
  783. The faces on ALIST will end up at the end of the returned list, in reverse
  784. order."
  785. (let ((list (nreverse (mapcar 'car oldlist))))
  786. (facemenu-iterate
  787. (lambda (new-face)
  788. (if (not (memq new-face list))
  789. (setq list (cons new-face list)))
  790. nil)
  791. (nreverse (face-list)))
  792. list))
  793. (defun facemenu-iterate (func list)
  794. "Apply FUNC to each element of LIST until one returns non-nil.
  795. Returns the non-nil value it found, or nil if all were nil."
  796. (while (and list (not (funcall func (car list))))
  797. (setq list (cdr list)))
  798. (car list))
  799. (facemenu-update)
  800. (provide 'facemenu)
  801. ;;; facemenu.el ends here