faces.el 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558
  1. ;;; faces.el --- Lisp faces
  2. ;; Copyright (C) 1992-1996, 1998-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: internal
  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. ;;; Code:
  19. (eval-when-compile
  20. (require 'cl))
  21. (declare-function xw-defined-colors "term/common-win" (&optional frame))
  22. (defvar help-xref-stack-item)
  23. (defvar face-name-history nil
  24. "History list for some commands that read face names.
  25. Maximum length of the history list is determined by the value
  26. of `history-length', which see.")
  27. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  28. ;;; Font selection.
  29. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  30. (defgroup font-selection nil
  31. "Influencing face font selection."
  32. :group 'faces)
  33. (defcustom face-font-selection-order
  34. '(:width :height :weight :slant)
  35. "A list specifying how face font selection chooses fonts.
  36. Each of the four symbols `:width', `:height', `:weight', and `:slant'
  37. must appear once in the list, and the list must not contain any other
  38. elements. Font selection first tries to find a best matching font
  39. for those face attributes that appear before in the list. For
  40. example, if `:slant' appears before `:height', font selection first
  41. tries to find a font with a suitable slant, even if this results in
  42. a font height that isn't optimal."
  43. :tag "Font selection order"
  44. :type '(list symbol symbol symbol symbol)
  45. :group 'font-selection
  46. :set #'(lambda (symbol value)
  47. (set-default symbol value)
  48. (internal-set-font-selection-order value)))
  49. ;; In the absence of Fontconfig support, Monospace and Sans Serif are
  50. ;; unavailable, and we fall back on the courier and helv families,
  51. ;; which are generally available.
  52. (defcustom face-font-family-alternatives
  53. (mapcar (lambda (arg) (mapcar 'purecopy arg))
  54. '(("Monospace" "courier" "fixed")
  55. ("courier" "CMU Typewriter Text" "fixed")
  56. ("Sans Serif" "helv" "helvetica" "arial" "fixed")
  57. ("helv" "helvetica" "arial" "fixed")))
  58. "Alist of alternative font family names.
  59. Each element has the form (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...).
  60. If fonts of family FAMILY can't be loaded, try ALTERNATIVE1, then
  61. ALTERNATIVE2 etc."
  62. :tag "Alternative font families to try"
  63. :type '(repeat (repeat string))
  64. :group 'font-selection
  65. :set #'(lambda (symbol value)
  66. (set-default symbol value)
  67. (internal-set-alternative-font-family-alist value)))
  68. ;; This is defined originally in xfaces.c.
  69. (defcustom face-font-registry-alternatives
  70. (mapcar (lambda (arg) (mapcar 'purecopy arg))
  71. (if (eq system-type 'windows-nt)
  72. '(("iso8859-1" "ms-oemlatin")
  73. ("gb2312.1980" "gb2312" "gbk" "gb18030")
  74. ("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
  75. ("ksc5601.1989" "ksx1001.1992" "ksc5601.1987")
  76. ("muletibetan-2" "muletibetan-0"))
  77. '(("gb2312.1980" "gb2312.80&gb8565.88" "gbk" "gb18030")
  78. ("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
  79. ("ksc5601.1989" "ksx1001.1992" "ksc5601.1987")
  80. ("muletibetan-2" "muletibetan-0"))))
  81. "Alist of alternative font registry names.
  82. Each element has the form (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...).
  83. If fonts of registry REGISTRY can be loaded, font selection
  84. tries to find a best matching font among all fonts of registry
  85. REGISTRY, ALTERNATIVE1, ALTERNATIVE2, and etc."
  86. :tag "Alternative font registries to try"
  87. :type '(repeat (repeat string))
  88. :version "21.1"
  89. :group 'font-selection
  90. :set #'(lambda (symbol value)
  91. (set-default symbol value)
  92. (internal-set-alternative-font-registry-alist value)))
  93. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  94. ;;; Creation, copying.
  95. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  96. (defun face-list ()
  97. "Return a list of all defined faces."
  98. (mapcar #'car face-new-frame-defaults))
  99. (defun make-face (face &optional no-init-from-resources)
  100. "Define a new face with name FACE, a symbol.
  101. Do not call this directly from Lisp code; use `defface' instead.
  102. If NO-INIT-FROM-RESOURCES is non-nil, don't initialize face
  103. attributes from X resources. If FACE is already known as a face,
  104. leave it unmodified. Return FACE."
  105. (interactive (list (read-from-minibuffer
  106. "Make face: " nil nil t 'face-name-history)))
  107. (unless (facep face)
  108. ;; Make frame-local faces (this also makes the global one).
  109. (dolist (frame (frame-list))
  110. (internal-make-lisp-face face frame))
  111. ;; Add the face to the face menu.
  112. (when (fboundp 'facemenu-add-new-face)
  113. (facemenu-add-new-face face))
  114. ;; Define frame-local faces for all frames from X resources.
  115. (unless no-init-from-resources
  116. (make-face-x-resource-internal face)))
  117. face)
  118. (defun make-empty-face (face)
  119. "Define a new, empty face with name FACE.
  120. Do not call this directly from Lisp code; use `defface' instead."
  121. (interactive (list (read-from-minibuffer
  122. "Make empty face: " nil nil t 'face-name-history)))
  123. (make-face face 'no-init-from-resources))
  124. (defun copy-face (old-face new-face &optional frame new-frame)
  125. "Define a face named NEW-FACE, which is a copy of OLD-FACE.
  126. This function does not copy face customization data, so NEW-FACE
  127. will not be made customizable. Most Lisp code should not call
  128. this function; use `defface' with :inherit instead.
  129. If NEW-FACE already exists as a face, modify it to be like
  130. OLD-FACE. If NEW-FACE doesn't already exist, create it.
  131. If the optional argument FRAME is a frame, change NEW-FACE on
  132. FRAME only. If FRAME is t, copy the frame-independent default
  133. specification for OLD-FACE to NEW-FACE. If FRAME is nil, copy
  134. the defaults as well as the faces on each existing frame.
  135. If the optional fourth argument NEW-FRAME is given, copy the
  136. information from face OLD-FACE on frame FRAME to NEW-FACE on
  137. frame NEW-FRAME. In this case, FRAME must not be nil."
  138. (let ((inhibit-quit t))
  139. (if (null frame)
  140. (progn
  141. (when new-frame
  142. (error "Copying face %s from all frames to one frame"
  143. old-face))
  144. (make-empty-face new-face)
  145. (dolist (frame (frame-list))
  146. (copy-face old-face new-face frame))
  147. (copy-face old-face new-face t))
  148. (make-empty-face new-face)
  149. (internal-copy-lisp-face old-face new-face frame new-frame))
  150. new-face))
  151. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  152. ;;; Predicates, type checks.
  153. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  154. (defun facep (face)
  155. "Return non-nil if FACE is a face name; nil otherwise.
  156. A face name can be a string or a symbol."
  157. (internal-lisp-face-p face))
  158. (defun check-face (face)
  159. "Signal an error if FACE doesn't name a face.
  160. Value is FACE."
  161. (unless (facep face)
  162. (error "Not a face: %s" face))
  163. face)
  164. ;; The ID returned is not to be confused with the internally used IDs
  165. ;; of realized faces. The ID assigned to Lisp faces is used to
  166. ;; support faces in display table entries.
  167. (defun face-id (face &optional _frame)
  168. "Return the internal ID of face with name FACE.
  169. If FACE is a face-alias, return the ID of the target face.
  170. The optional argument FRAME is ignored, since the internal face ID
  171. of a face name is the same for all frames."
  172. (check-face face)
  173. (or (get face 'face)
  174. (face-id (get face 'face-alias))))
  175. (defun face-equal (face1 face2 &optional frame)
  176. "Non-nil if faces FACE1 and FACE2 are equal.
  177. Faces are considered equal if all their attributes are equal.
  178. If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
  179. If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
  180. If FRAME is omitted or nil, use the selected frame."
  181. (internal-lisp-face-equal-p face1 face2 frame))
  182. (defun face-differs-from-default-p (face &optional frame)
  183. "Return non-nil if FACE displays differently from the default face.
  184. If the optional argument FRAME is given, report on face FACE in that frame.
  185. If FRAME is t, report on the defaults for face FACE (for new frames).
  186. If FRAME is omitted or nil, use the selected frame."
  187. (let ((attrs
  188. (delq :inherit (mapcar 'car face-attribute-name-alist)))
  189. (differs nil))
  190. (while (and attrs (not differs))
  191. (let* ((attr (pop attrs))
  192. (attr-val (face-attribute face attr frame t)))
  193. (when (and
  194. (not (eq attr-val 'unspecified))
  195. (display-supports-face-attributes-p (list attr attr-val)
  196. frame))
  197. (setq differs attr))))
  198. differs))
  199. (defun face-nontrivial-p (face &optional frame)
  200. "True if face FACE has some non-nil attribute.
  201. If the optional argument FRAME is given, report on face FACE in that frame.
  202. If FRAME is t, report on the defaults for face FACE (for new frames).
  203. If FRAME is omitted or nil, use the selected frame."
  204. (not (internal-lisp-face-empty-p face frame)))
  205. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  206. ;;; Setting face attributes from X resources.
  207. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  208. (defcustom face-x-resources
  209. (mapcar
  210. (lambda (arg)
  211. ;; FIXME; can we purecopy some of the conses too?
  212. (cons (car arg)
  213. (cons (purecopy (car (cdr arg))) (purecopy (cdr (cdr arg))))))
  214. '((:family (".attributeFamily" . "Face.AttributeFamily"))
  215. (:foundry (".attributeFoundry" . "Face.AttributeFoundry"))
  216. (:width (".attributeWidth" . "Face.AttributeWidth"))
  217. (:height (".attributeHeight" . "Face.AttributeHeight"))
  218. (:weight (".attributeWeight" . "Face.AttributeWeight"))
  219. (:slant (".attributeSlant" . "Face.AttributeSlant"))
  220. (:foreground (".attributeForeground" . "Face.AttributeForeground"))
  221. (:background (".attributeBackground" . "Face.AttributeBackground"))
  222. (:overline (".attributeOverline" . "Face.AttributeOverline"))
  223. (:strike-through (".attributeStrikeThrough" . "Face.AttributeStrikeThrough"))
  224. (:box (".attributeBox" . "Face.AttributeBox"))
  225. (:underline (".attributeUnderline" . "Face.AttributeUnderline"))
  226. (:inverse-video (".attributeInverse" . "Face.AttributeInverse"))
  227. (:stipple
  228. (".attributeStipple" . "Face.AttributeStipple")
  229. (".attributeBackgroundPixmap" . "Face.AttributeBackgroundPixmap"))
  230. (:bold (".attributeBold" . "Face.AttributeBold"))
  231. (:italic (".attributeItalic" . "Face.AttributeItalic"))
  232. (:font (".attributeFont" . "Face.AttributeFont"))
  233. (:inherit (".attributeInherit" . "Face.AttributeInherit"))))
  234. "List of X resources and classes for face attributes.
  235. Each element has the form (ATTRIBUTE ENTRY1 ENTRY2...) where ATTRIBUTE is
  236. the name of a face attribute, and each ENTRY is a cons of the form
  237. \(RESOURCE . CLASS) with RESOURCE being the resource and CLASS being the
  238. X resource class for the attribute."
  239. :type '(repeat (cons symbol (repeat (cons string string))))
  240. :group 'faces)
  241. (declare-function internal-face-x-get-resource "xfaces.c"
  242. (resource class frame))
  243. (declare-function internal-set-lisp-face-attribute-from-resource "xfaces.c"
  244. (face attr value &optional frame))
  245. (defun set-face-attribute-from-resource (face attribute resource class frame)
  246. "Set FACE's ATTRIBUTE from X resource RESOURCE, class CLASS on FRAME.
  247. Value is the attribute value specified by the resource, or nil
  248. if not present. This function displays a message if the resource
  249. specifies an invalid attribute."
  250. (let* ((face-name (face-name face))
  251. (value (internal-face-x-get-resource (concat face-name resource)
  252. class frame)))
  253. (when value
  254. (condition-case ()
  255. (internal-set-lisp-face-attribute-from-resource
  256. face attribute (downcase value) frame)
  257. (error
  258. (message "Face %s, frame %s: invalid attribute %s %s from X resource"
  259. face-name frame attribute value))))
  260. value))
  261. (defun set-face-attributes-from-resources (face frame)
  262. "Set attributes of FACE from X resources for FRAME."
  263. (when (memq (framep frame) '(x w32))
  264. (dolist (definition face-x-resources)
  265. (let ((attribute (car definition)))
  266. (dolist (entry (cdr definition))
  267. (set-face-attribute-from-resource face attribute (car entry)
  268. (cdr entry) frame))))))
  269. (defun make-face-x-resource-internal (face &optional frame)
  270. "Fill frame-local FACE on FRAME from X resources.
  271. FRAME nil or not specified means do it for all frames."
  272. (if (null frame)
  273. (dolist (frame (frame-list))
  274. (set-face-attributes-from-resources face frame))
  275. (set-face-attributes-from-resources face frame)))
  276. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  277. ;;; Retrieving face attributes.
  278. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  279. (defun face-name (face)
  280. "Return the name of face FACE."
  281. (symbol-name (check-face face)))
  282. (defun face-all-attributes (face &optional frame)
  283. "Return an alist stating the attributes of FACE.
  284. Each element of the result has the form (ATTR-NAME . ATTR-VALUE).
  285. If FRAME is omitted or nil the value describes the default attributes,
  286. but if you specify FRAME, the value describes the attributes
  287. of FACE on FRAME."
  288. (mapcar (lambda (pair)
  289. (let ((attr (car pair)))
  290. (cons attr (face-attribute face attr (or frame t)))))
  291. face-attribute-name-alist))
  292. (defun face-attribute (face attribute &optional frame inherit)
  293. "Return the value of FACE's ATTRIBUTE on FRAME.
  294. If the optional argument FRAME is given, report on face FACE in that frame.
  295. If FRAME is t, report on the defaults for face FACE (for new frames).
  296. If FRAME is omitted or nil, use the selected frame.
  297. If INHERIT is nil, only attributes directly defined by FACE are considered,
  298. so the return value may be `unspecified', or a relative value.
  299. If INHERIT is non-nil, FACE's definition of ATTRIBUTE is merged with the
  300. faces specified by its `:inherit' attribute; however the return value
  301. may still be `unspecified' or relative.
  302. If INHERIT is a face or a list of faces, then the result is further merged
  303. with that face (or faces), until it becomes specified and absolute.
  304. To ensure that the return value is always specified and absolute, use a
  305. value of `default' for INHERIT; this will resolve any unspecified or
  306. relative values by merging with the `default' face (which is always
  307. completely specified)."
  308. (let ((value (internal-get-lisp-face-attribute face attribute frame)))
  309. (when (and inherit (face-attribute-relative-p attribute value))
  310. ;; VALUE is relative, so merge with inherited faces
  311. (let ((inh-from (face-attribute face :inherit frame)))
  312. (unless (or (null inh-from) (eq inh-from 'unspecified))
  313. (condition-case nil
  314. (setq value
  315. (face-attribute-merged-with attribute value inh-from frame))
  316. ;; The `inherit' attribute may point to non existent faces.
  317. (error nil)))))
  318. (when (and inherit
  319. (not (eq inherit t))
  320. (face-attribute-relative-p attribute value))
  321. ;; We should merge with INHERIT as well
  322. (setq value (face-attribute-merged-with attribute value inherit frame)))
  323. value))
  324. (defun face-attribute-merged-with (attribute value faces &optional frame)
  325. "Merges ATTRIBUTE, initially VALUE, with faces from FACES until absolute.
  326. FACES may be either a single face or a list of faces.
  327. \[This is an internal function.]"
  328. (cond ((not (face-attribute-relative-p attribute value))
  329. value)
  330. ((null faces)
  331. value)
  332. ((consp faces)
  333. (face-attribute-merged-with
  334. attribute
  335. (face-attribute-merged-with attribute value (car faces) frame)
  336. (cdr faces)
  337. frame))
  338. (t
  339. (merge-face-attribute attribute
  340. value
  341. (face-attribute faces attribute frame t)))))
  342. (defmacro face-attribute-specified-or (value &rest body)
  343. "Return VALUE, unless it's `unspecified', in which case evaluate BODY and return the result."
  344. (let ((temp (make-symbol "value")))
  345. `(let ((,temp ,value))
  346. (if (not (eq ,temp 'unspecified))
  347. ,temp
  348. ,@body))))
  349. (defun face-foreground (face &optional frame inherit)
  350. "Return the foreground color name of FACE, or nil if unspecified.
  351. If the optional argument FRAME is given, report on face FACE in that frame.
  352. If FRAME is t, report on the defaults for face FACE (for new frames).
  353. If FRAME is omitted or nil, use the selected frame.
  354. If INHERIT is nil, only a foreground color directly defined by FACE is
  355. considered, so the return value may be nil.
  356. If INHERIT is t, and FACE doesn't define a foreground color, then any
  357. foreground color that FACE inherits through its `:inherit' attribute
  358. is considered as well; however the return value may still be nil.
  359. If INHERIT is a face or a list of faces, then it is used to try to
  360. resolve an unspecified foreground color.
  361. To ensure that a valid color is always returned, use a value of
  362. `default' for INHERIT; this will resolve any unspecified values by
  363. merging with the `default' face (which is always completely specified)."
  364. (face-attribute-specified-or (face-attribute face :foreground frame inherit)
  365. nil))
  366. (defun face-background (face &optional frame inherit)
  367. "Return the background color name of FACE, or nil if unspecified.
  368. If the optional argument FRAME is given, report on face FACE in that frame.
  369. If FRAME is t, report on the defaults for face FACE (for new frames).
  370. If FRAME is omitted or nil, use the selected frame.
  371. If INHERIT is nil, only a background color directly defined by FACE is
  372. considered, so the return value may be nil.
  373. If INHERIT is t, and FACE doesn't define a background color, then any
  374. background color that FACE inherits through its `:inherit' attribute
  375. is considered as well; however the return value may still be nil.
  376. If INHERIT is a face or a list of faces, then it is used to try to
  377. resolve an unspecified background color.
  378. To ensure that a valid color is always returned, use a value of
  379. `default' for INHERIT; this will resolve any unspecified values by
  380. merging with the `default' face (which is always completely specified)."
  381. (face-attribute-specified-or (face-attribute face :background frame inherit)
  382. nil))
  383. (defun face-stipple (face &optional frame inherit)
  384. "Return the stipple pixmap name of FACE, or nil if unspecified.
  385. If the optional argument FRAME is given, report on face FACE in that frame.
  386. If FRAME is t, report on the defaults for face FACE (for new frames).
  387. If FRAME is omitted or nil, use the selected frame.
  388. If INHERIT is nil, only a stipple directly defined by FACE is
  389. considered, so the return value may be nil.
  390. If INHERIT is t, and FACE doesn't define a stipple, then any stipple
  391. that FACE inherits through its `:inherit' attribute is considered as
  392. well; however the return value may still be nil.
  393. If INHERIT is a face or a list of faces, then it is used to try to
  394. resolve an unspecified stipple.
  395. To ensure that a valid stipple or nil is always returned, use a value of
  396. `default' for INHERIT; this will resolve any unspecified values by merging
  397. with the `default' face (which is always completely specified)."
  398. (face-attribute-specified-or (face-attribute face :stipple frame inherit)
  399. nil))
  400. (defalias 'face-background-pixmap 'face-stipple)
  401. (defun face-underline-p (face &optional frame)
  402. "Return non-nil if FACE is underlined.
  403. If the optional argument FRAME is given, report on face FACE in that frame.
  404. If FRAME is t, report on the defaults for face FACE (for new frames).
  405. If FRAME is omitted or nil, use the selected frame."
  406. (eq (face-attribute face :underline frame) t))
  407. (defun face-inverse-video-p (face &optional frame)
  408. "Return non-nil if FACE is in inverse video on FRAME.
  409. If the optional argument FRAME is given, report on face FACE in that frame.
  410. If FRAME is t, report on the defaults for face FACE (for new frames).
  411. If FRAME is omitted or nil, use the selected frame."
  412. (eq (face-attribute face :inverse-video frame) t))
  413. (defun face-bold-p (face &optional frame)
  414. "Return non-nil if the font of FACE is bold on FRAME.
  415. If the optional argument FRAME is given, report on face FACE in that frame.
  416. If FRAME is t, report on the defaults for face FACE (for new frames).
  417. If FRAME is omitted or nil, use the selected frame.
  418. Use `face-attribute' for finer control."
  419. (let ((bold (face-attribute face :weight frame)))
  420. (memq bold '(semi-bold bold extra-bold ultra-bold))))
  421. (defun face-italic-p (face &optional frame)
  422. "Return non-nil if the font of FACE is italic on FRAME.
  423. If the optional argument FRAME is given, report on face FACE in that frame.
  424. If FRAME is t, report on the defaults for face FACE (for new frames).
  425. If FRAME is omitted or nil, use the selected frame.
  426. Use `face-attribute' for finer control."
  427. (let ((italic (face-attribute face :slant frame)))
  428. (memq italic '(italic oblique))))
  429. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  430. ;;; Face documentation.
  431. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  432. (defun face-documentation (face)
  433. "Get the documentation string for FACE.
  434. If FACE is a face-alias, get the documentation for the target face."
  435. (let ((alias (get face 'face-alias))
  436. doc)
  437. (if alias
  438. (progn
  439. (setq doc (get alias 'face-documentation))
  440. (format "%s is an alias for the face `%s'.%s" face alias
  441. (if doc (format "\n%s" doc)
  442. "")))
  443. (get face 'face-documentation))))
  444. (defun set-face-documentation (face string)
  445. "Set the documentation string for FACE to STRING."
  446. ;; Perhaps the text should go in DOC.
  447. (put face 'face-documentation (purecopy string)))
  448. (defalias 'face-doc-string 'face-documentation)
  449. (defalias 'set-face-doc-string 'set-face-documentation)
  450. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  451. ;; Setting face attributes.
  452. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  453. (defun set-face-attribute (face frame &rest args)
  454. "Set attributes of FACE on FRAME from ARGS.
  455. If FRAME is nil this function sets the attributes for all
  456. existing frames, and the default for new frames. If FRAME is t,
  457. change the default for new frames (this is done automatically
  458. each time an attribute is changed on all frames).
  459. ARGS must come in pairs ATTRIBUTE VALUE. ATTRIBUTE must be a valid
  460. face attribute name. All attributes can be set to `unspecified';
  461. this fact is not further mentioned below.
  462. The following attributes are recognized:
  463. `:family'
  464. VALUE must be a string specifying the font family, e.g. ``monospace'',
  465. or a fontset alias name. If a font family is specified, wild-cards `*'
  466. and `?' are allowed.
  467. `:foundry'
  468. VALUE must be a string specifying the font foundry,
  469. e.g. ``adobe''. If a font foundry is specified, wild-cards `*'
  470. and `?' are allowed.
  471. `:width'
  472. VALUE specifies the relative proportionate width of the font to use.
  473. It must be one of the symbols `ultra-condensed', `extra-condensed',
  474. `condensed', `semi-condensed', `normal', `semi-expanded', `expanded',
  475. `extra-expanded', or `ultra-expanded'.
  476. `:height'
  477. VALUE specifies the height of the font, in either absolute or relative
  478. terms. An absolute height is an integer, and specifies font height in
  479. units of 1/10 pt. A relative height is either a floating point number,
  480. which specifies a scaling factor for the underlying face height;
  481. or a function that takes a single argument (the underlying face height)
  482. and returns the new height. Note that for the `default' face,
  483. you can only specify an absolute height (since there is nothing
  484. for it to be relative to).
  485. `:weight'
  486. VALUE specifies the weight of the font to use. It must be one of the
  487. symbols `ultra-bold', `extra-bold', `bold', `semi-bold', `normal',
  488. `semi-light', `light', `extra-light', `ultra-light'.
  489. `:slant'
  490. VALUE specifies the slant of the font to use. It must be one of the
  491. symbols `italic', `oblique', `normal', `reverse-italic', or
  492. `reverse-oblique'.
  493. `:foreground', `:background'
  494. VALUE must be a color name, a string.
  495. `:underline'
  496. VALUE specifies whether characters in FACE should be underlined. If
  497. VALUE is t, underline with foreground color of the face. If VALUE is
  498. a string, underline with that color. If VALUE is nil, explicitly
  499. don't underline.
  500. `:overline'
  501. VALUE specifies whether characters in FACE should be overlined. If
  502. VALUE is t, overline with foreground color of the face. If VALUE is a
  503. string, overline with that color. If VALUE is nil, explicitly don't
  504. overline.
  505. `:strike-through'
  506. VALUE specifies whether characters in FACE should be drawn with a line
  507. striking through them. If VALUE is t, use the foreground color of the
  508. face. If VALUE is a string, strike-through with that color. If VALUE
  509. is nil, explicitly don't strike through.
  510. `:box'
  511. VALUE specifies whether characters in FACE should have a box drawn
  512. around them. If VALUE is nil, explicitly don't draw boxes. If
  513. VALUE is t, draw a box with lines of width 1 in the foreground color
  514. of the face. If VALUE is a string, the string must be a color name,
  515. and the box is drawn in that color with a line width of 1. Otherwise,
  516. VALUE must be a property list of the form `(:line-width WIDTH
  517. :color COLOR :style STYLE)'. If a keyword/value pair is missing from
  518. the property list, a default value will be used for the value, as
  519. specified below. WIDTH specifies the width of the lines to draw; it
  520. defaults to 1. If WIDTH is negative, the absolute value is the width
  521. of the lines, and draw top/bottom lines inside the characters area,
  522. not around it. COLOR is the name of the color to draw in, default is
  523. the foreground color of the face for simple boxes, and the background
  524. color of the face for 3D boxes. STYLE specifies whether a 3D box
  525. should be draw. If STYLE is `released-button', draw a box looking
  526. like a released 3D button. If STYLE is `pressed-button' draw a box
  527. that appears like a pressed button. If STYLE is nil, the default if
  528. the property list doesn't contain a style specification, draw a 2D
  529. box.
  530. `:inverse-video'
  531. VALUE specifies whether characters in FACE should be displayed in
  532. inverse video. VALUE must be one of t or nil.
  533. `:stipple'
  534. If VALUE is a string, it must be the name of a file of pixmap data.
  535. The directories listed in the `x-bitmap-file-path' variable are
  536. searched. Alternatively, VALUE may be a list of the form (WIDTH
  537. HEIGHT DATA) where WIDTH and HEIGHT are the size in pixels, and DATA
  538. is a string containing the raw bits of the bitmap. VALUE nil means
  539. explicitly don't use a stipple pattern.
  540. For convenience, attributes `:family', `:foundry', `:width',
  541. `:height', `:weight', and `:slant' may also be set in one step
  542. from an X font name:
  543. `:font'
  544. Set font-related face attributes from VALUE. VALUE must be a valid
  545. XLFD font name. If it is a font name pattern, the first matching font
  546. will be used.
  547. For compatibility with Emacs 20, keywords `:bold' and `:italic' can
  548. be used to specify that a bold or italic font should be used. VALUE
  549. must be t or nil in that case. A value of `unspecified' is not allowed.
  550. `:inherit'
  551. VALUE is the name of a face from which to inherit attributes, or a list
  552. of face names. Attributes from inherited faces are merged into the face
  553. like an underlying face would be, with higher priority than underlying faces."
  554. (setq args (purecopy args))
  555. (let ((where (if (null frame) 0 frame))
  556. (spec args)
  557. family foundry)
  558. ;; If we set the new-frame defaults, this face is modified outside Custom.
  559. (if (memq where '(0 t))
  560. (put (or (get face 'face-alias) face) 'face-modified t))
  561. ;; If family and/or foundry are specified, set it first. Certain
  562. ;; face attributes, e.g. :weight semi-condensed, are not supported
  563. ;; in every font. See bug#1127.
  564. (while spec
  565. (cond ((eq (car spec) :family)
  566. (setq family (cadr spec)))
  567. ((eq (car spec) :foundry)
  568. (setq foundry (cadr spec))))
  569. (setq spec (cddr spec)))
  570. (when (or family foundry)
  571. (when (and (stringp family)
  572. (string-match "\\([^-]*\\)-\\([^-]*\\)" family))
  573. (unless foundry
  574. (setq foundry (match-string 1 family)))
  575. (setq family (match-string 2 family)))
  576. (when (or (stringp family) (eq family 'unspecified))
  577. (internal-set-lisp-face-attribute face :family (purecopy family)
  578. where))
  579. (when (or (stringp foundry) (eq foundry 'unspecified))
  580. (internal-set-lisp-face-attribute face :foundry (purecopy foundry)
  581. where)))
  582. (while args
  583. (unless (memq (car args) '(:family :foundry))
  584. (internal-set-lisp-face-attribute face (car args)
  585. (purecopy (cadr args))
  586. where))
  587. (setq args (cddr args)))))
  588. (defun make-face-bold (face &optional frame _noerror)
  589. "Make the font of FACE be bold, if possible.
  590. FRAME nil or not specified means change face on all frames.
  591. Argument NOERROR is ignored and retained for compatibility.
  592. Use `set-face-attribute' for finer control of the font weight."
  593. (interactive (list (read-face-name "Make which face bold")))
  594. (set-face-attribute face frame :weight 'bold))
  595. (defun make-face-unbold (face &optional frame _noerror)
  596. "Make the font of FACE be non-bold, if possible.
  597. FRAME nil or not specified means change face on all frames.
  598. Argument NOERROR is ignored and retained for compatibility."
  599. (interactive (list (read-face-name "Make which face non-bold")))
  600. (set-face-attribute face frame :weight 'normal))
  601. (defun make-face-italic (face &optional frame _noerror)
  602. "Make the font of FACE be italic, if possible.
  603. FRAME nil or not specified means change face on all frames.
  604. Argument NOERROR is ignored and retained for compatibility.
  605. Use `set-face-attribute' for finer control of the font slant."
  606. (interactive (list (read-face-name "Make which face italic")))
  607. (set-face-attribute face frame :slant 'italic))
  608. (defun make-face-unitalic (face &optional frame _noerror)
  609. "Make the font of FACE be non-italic, if possible.
  610. FRAME nil or not specified means change face on all frames.
  611. Argument NOERROR is ignored and retained for compatibility."
  612. (interactive (list (read-face-name "Make which face non-italic")))
  613. (set-face-attribute face frame :slant 'normal))
  614. (defun make-face-bold-italic (face &optional frame _noerror)
  615. "Make the font of FACE be bold and italic, if possible.
  616. FRAME nil or not specified means change face on all frames.
  617. Argument NOERROR is ignored and retained for compatibility.
  618. Use `set-face-attribute' for finer control of font weight and slant."
  619. (interactive (list (read-face-name "Make which face bold-italic")))
  620. (set-face-attribute face frame :weight 'bold :slant 'italic))
  621. (defun set-face-font (face font &optional frame)
  622. "Change font-related attributes of FACE to those of FONT (a string).
  623. FRAME nil or not specified means change face on all frames.
  624. This sets the attributes `:family', `:foundry', `:width',
  625. `:height', `:weight', and `:slant'. When called interactively,
  626. prompt for the face and font."
  627. (interactive (read-face-and-attribute :font))
  628. (set-face-attribute face frame :font font))
  629. ;; Implementation note: Emulating gray background colors with a
  630. ;; stipple pattern is now part of the face realization process, and is
  631. ;; done in C depending on the frame on which the face is realized.
  632. (defun set-face-background (face color &optional frame)
  633. "Change the background color of face FACE to COLOR (a string).
  634. FRAME nil or not specified means change face on all frames.
  635. COLOR can be a system-defined color name (see `list-colors-display')
  636. or a hex spec of the form #RRGGBB.
  637. When called interactively, prompts for the face and color."
  638. (interactive (read-face-and-attribute :background))
  639. (set-face-attribute face frame :background (or color 'unspecified)))
  640. (defun set-face-foreground (face color &optional frame)
  641. "Change the foreground color of face FACE to COLOR (a string).
  642. FRAME nil or not specified means change face on all frames.
  643. COLOR can be a system-defined color name (see `list-colors-display')
  644. or a hex spec of the form #RRGGBB.
  645. When called interactively, prompts for the face and color."
  646. (interactive (read-face-and-attribute :foreground))
  647. (set-face-attribute face frame :foreground (or color 'unspecified)))
  648. (defun set-face-stipple (face stipple &optional frame)
  649. "Change the stipple pixmap of face FACE to STIPPLE.
  650. FRAME nil or not specified means change face on all frames.
  651. STIPPLE should be a string, the name of a file of pixmap data.
  652. The directories listed in the `x-bitmap-file-path' variable are searched.
  653. Alternatively, STIPPLE may be a list of the form (WIDTH HEIGHT DATA)
  654. where WIDTH and HEIGHT are the size in pixels,
  655. and DATA is a string, containing the raw bits of the bitmap."
  656. (interactive (read-face-and-attribute :stipple))
  657. (set-face-attribute face frame :stipple (or stipple 'unspecified)))
  658. (defun set-face-underline-p (face underline &optional frame)
  659. "Specify whether face FACE is underlined.
  660. UNDERLINE nil means FACE explicitly doesn't underline.
  661. UNDERLINE non-nil means FACE explicitly does underlining
  662. with the same of the foreground color.
  663. If UNDERLINE is a string, underline with the color named UNDERLINE.
  664. FRAME nil or not specified means change face on all frames.
  665. Use `set-face-attribute' to ``unspecify'' underlining."
  666. (interactive
  667. (let ((list (read-face-and-attribute :underline)))
  668. (list (car list) (eq (car (cdr list)) t))))
  669. (set-face-attribute face frame :underline underline))
  670. (define-obsolete-function-alias 'set-face-underline
  671. 'set-face-underline-p "22.1")
  672. (defun set-face-inverse-video-p (face inverse-video-p &optional frame)
  673. "Specify whether face FACE is in inverse video.
  674. INVERSE-VIDEO-P non-nil means FACE displays explicitly in inverse video.
  675. INVERSE-VIDEO-P nil means FACE explicitly is not in inverse video.
  676. FRAME nil or not specified means change face on all frames.
  677. Use `set-face-attribute' to ``unspecify'' the inverse video attribute."
  678. (interactive
  679. (let ((list (read-face-and-attribute :inverse-video)))
  680. (list (car list) (eq (car (cdr list)) t))))
  681. (set-face-attribute face frame :inverse-video inverse-video-p))
  682. (defun set-face-bold-p (face bold-p &optional frame)
  683. "Specify whether face FACE is bold.
  684. BOLD-P non-nil means FACE should explicitly display bold.
  685. BOLD-P nil means FACE should explicitly display non-bold.
  686. FRAME nil or not specified means change face on all frames.
  687. Use `set-face-attribute' or `modify-face' for finer control."
  688. (if (null bold-p)
  689. (make-face-unbold face frame)
  690. (make-face-bold face frame)))
  691. (defun set-face-italic-p (face italic-p &optional frame)
  692. "Specify whether face FACE is italic.
  693. ITALIC-P non-nil means FACE should explicitly display italic.
  694. ITALIC-P nil means FACE should explicitly display non-italic.
  695. FRAME nil or not specified means change face on all frames.
  696. Use `set-face-attribute' or `modify-face' for finer control."
  697. (if (null italic-p)
  698. (make-face-unitalic face frame)
  699. (make-face-italic face frame)))
  700. (defalias 'set-face-background-pixmap 'set-face-stipple)
  701. (defun invert-face (face &optional frame)
  702. "Swap the foreground and background colors of FACE.
  703. If FRAME is omitted or nil, it means change face on all frames.
  704. If FACE specifies neither foreground nor background color,
  705. set its foreground and background to the background and foreground
  706. of the default face. Value is FACE."
  707. (interactive (list (read-face-name "Invert face")))
  708. (let ((fg (face-attribute face :foreground frame))
  709. (bg (face-attribute face :background frame)))
  710. (if (not (and (eq fg 'unspecified) (eq bg 'unspecified)))
  711. (set-face-attribute face frame :foreground bg :background fg)
  712. (set-face-attribute face frame
  713. :foreground
  714. (face-attribute 'default :background frame)
  715. :background
  716. (face-attribute 'default :foreground frame))))
  717. face)
  718. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  719. ;;; Interactively modifying faces.
  720. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  721. (defun read-face-name (prompt &optional default multiple)
  722. "Read a face, defaulting to the face or faces on the char after point.
  723. If it has the property `read-face-name', that overrides the `face' property.
  724. PROMPT should be a string that describes what the caller will do with the face;
  725. it should not end in a space.
  726. The optional argument DEFAULT provides the value to display in the
  727. minibuffer prompt that is returned if the user just types RET
  728. unless DEFAULT is a string (in which case nil is returned).
  729. If MULTIPLE is non-nil, return a list of faces (possibly only one).
  730. Otherwise, return a single face."
  731. (let ((faceprop (or (get-char-property (point) 'read-face-name)
  732. (get-char-property (point) 'face)))
  733. (aliasfaces nil)
  734. (nonaliasfaces nil)
  735. faces)
  736. ;; Try to get a face name from the buffer.
  737. (if (memq (intern-soft (thing-at-point 'symbol)) (face-list))
  738. (setq faces (list (intern-soft (thing-at-point 'symbol)))))
  739. ;; Add the named faces that the `face' property uses.
  740. (if (and (listp faceprop)
  741. ;; Don't treat an attribute spec as a list of faces.
  742. (not (keywordp (car faceprop)))
  743. (not (memq (car faceprop) '(foreground-color background-color))))
  744. (dolist (f faceprop)
  745. (if (symbolp f)
  746. (push f faces)))
  747. (if (symbolp faceprop)
  748. (push faceprop faces)))
  749. (delete-dups faces)
  750. ;; Build up the completion tables.
  751. (mapatoms (lambda (s)
  752. (if (custom-facep s)
  753. (if (get s 'face-alias)
  754. (push (symbol-name s) aliasfaces)
  755. (push (symbol-name s) nonaliasfaces)))))
  756. ;; If we only want one, and the default is more than one,
  757. ;; discard the unwanted ones now.
  758. (unless multiple
  759. (if faces
  760. (setq faces (list (car faces)))))
  761. (require 'crm)
  762. (let* ((input
  763. ;; Read the input.
  764. (completing-read-multiple
  765. (if (or faces default)
  766. (format "%s (default `%s'): " prompt
  767. (if faces (mapconcat 'symbol-name faces ",")
  768. default))
  769. (format "%s: " prompt))
  770. (completion-table-in-turn nonaliasfaces aliasfaces)
  771. nil t nil 'face-name-history
  772. (if faces (mapconcat 'symbol-name faces ","))))
  773. ;; Canonicalize the output.
  774. (output
  775. (cond ((or (equal input "") (equal input '("")))
  776. (or faces (unless (stringp default) default)))
  777. ((stringp input)
  778. (mapcar 'intern (split-string input ", *" t)))
  779. ((listp input)
  780. (mapcar 'intern input))
  781. (input))))
  782. ;; Return either a list of faces or just one face.
  783. (if multiple
  784. output
  785. (car output)))))
  786. ;; Not defined without X, but behind window-system test.
  787. (defvar x-bitmap-file-path)
  788. (defun face-valid-attribute-values (attribute &optional frame)
  789. "Return valid values for face attribute ATTRIBUTE.
  790. The optional argument FRAME is used to determine available fonts
  791. and colors. If it is nil or not specified, the selected frame is used.
  792. Value is an alist of (NAME . VALUE) if ATTRIBUTE expects a value out
  793. of a set of discrete values. Value is `integerp' if ATTRIBUTE expects
  794. an integer value."
  795. (let ((valid
  796. (case attribute
  797. (:family
  798. (if (window-system frame)
  799. (mapcar (lambda (x) (cons x x))
  800. (font-family-list))
  801. ;; Only one font on TTYs.
  802. (list (cons "default" "default"))))
  803. (:foundry
  804. (list nil))
  805. (:width
  806. (mapcar #'(lambda (x) (cons (symbol-name (aref x 1)) (aref x 1)))
  807. font-width-table))
  808. (:weight
  809. (mapcar #'(lambda (x) (cons (symbol-name (aref x 1)) (aref x 1)))
  810. font-weight-table))
  811. (:slant
  812. (mapcar #'(lambda (x) (cons (symbol-name (aref x 1)) (aref x 1)))
  813. font-slant-table))
  814. (:inverse-video
  815. (mapcar #'(lambda (x) (cons (symbol-name x) x))
  816. (internal-lisp-face-attribute-values attribute)))
  817. ((:underline :overline :strike-through :box)
  818. (if (window-system frame)
  819. (nconc (mapcar #'(lambda (x) (cons (symbol-name x) x))
  820. (internal-lisp-face-attribute-values attribute))
  821. (mapcar #'(lambda (c) (cons c c))
  822. (defined-colors frame)))
  823. (mapcar #'(lambda (x) (cons (symbol-name x) x))
  824. (internal-lisp-face-attribute-values attribute))))
  825. ((:foreground :background)
  826. (mapcar #'(lambda (c) (cons c c))
  827. (defined-colors frame)))
  828. ((:height)
  829. 'integerp)
  830. (:stipple
  831. (and (memq (window-system frame) '(x ns)) ; No stipple on w32
  832. (mapcar #'list
  833. (apply #'nconc
  834. (mapcar (lambda (dir)
  835. (and (file-readable-p dir)
  836. (file-directory-p dir)
  837. (directory-files dir)))
  838. x-bitmap-file-path)))))
  839. (:inherit
  840. (cons '("none" . nil)
  841. (mapcar #'(lambda (c) (cons (symbol-name c) c))
  842. (face-list))))
  843. (t
  844. (error "Internal error")))))
  845. (if (and (listp valid) (not (memq attribute '(:inherit))))
  846. (nconc (list (cons "unspecified" 'unspecified)) valid)
  847. valid)))
  848. (defconst face-attribute-name-alist
  849. '((:family . "font family")
  850. (:foundry . "font foundry")
  851. (:width . "character set width")
  852. (:height . "height in 1/10 pt")
  853. (:weight . "weight")
  854. (:slant . "slant")
  855. (:underline . "underline")
  856. (:overline . "overline")
  857. (:strike-through . "strike-through")
  858. (:box . "box")
  859. (:inverse-video . "inverse-video display")
  860. (:foreground . "foreground color")
  861. (:background . "background color")
  862. (:stipple . "background stipple")
  863. (:inherit . "inheritance"))
  864. "An alist of descriptive names for face attributes.
  865. Each element has the form (ATTRIBUTE-NAME . DESCRIPTION) where
  866. ATTRIBUTE-NAME is a face attribute name (a keyword symbol), and
  867. DESCRIPTION is a descriptive name for ATTRIBUTE-NAME.")
  868. (defun face-descriptive-attribute-name (attribute)
  869. "Return a descriptive name for ATTRIBUTE."
  870. (cdr (assq attribute face-attribute-name-alist)))
  871. (defun face-read-string (face default name &optional completion-alist)
  872. "Interactively read a face attribute string value.
  873. FACE is the face whose attribute is read. If non-nil, DEFAULT is the
  874. default string to return if no new value is entered. NAME is a
  875. descriptive name of the attribute for prompting. COMPLETION-ALIST is an
  876. alist of valid values, if non-nil.
  877. Entering nothing accepts the default string DEFAULT.
  878. Value is the new attribute value."
  879. ;; Capitalize NAME (we don't use `capitalize' because that capitalizes
  880. ;; each word in a string separately).
  881. (setq name (concat (upcase (substring name 0 1)) (substring name 1)))
  882. (let* ((completion-ignore-case t)
  883. (value (completing-read
  884. (if default
  885. (format "%s for face `%s' (default %s): "
  886. name face default)
  887. (format "%s for face `%s': " name face))
  888. completion-alist nil nil nil nil default)))
  889. (if (equal value "") default value)))
  890. (defun face-read-integer (face default name)
  891. "Interactively read an integer face attribute value.
  892. FACE is the face whose attribute is read. DEFAULT is the default
  893. value to return if no new value is entered. NAME is a descriptive
  894. name of the attribute for prompting. Value is the new attribute value."
  895. (let ((new-value
  896. (face-read-string face
  897. (format "%s" default)
  898. name
  899. (list (cons "unspecified" 'unspecified)))))
  900. (cond ((equal new-value "unspecified")
  901. 'unspecified)
  902. ((member new-value '("unspecified-fg" "unspecified-bg"))
  903. new-value)
  904. (t
  905. (string-to-number new-value)))))
  906. (defun read-face-attribute (face attribute &optional frame)
  907. "Interactively read a new value for FACE's ATTRIBUTE.
  908. Optional argument FRAME nil or unspecified means read an attribute value
  909. of a global face. Value is the new attribute value."
  910. (let* ((old-value (face-attribute face attribute frame))
  911. (attribute-name (face-descriptive-attribute-name attribute))
  912. (valid (face-valid-attribute-values attribute frame))
  913. new-value)
  914. ;; Represent complex attribute values as strings by printing them
  915. ;; out. Stipple can be a vector; (WIDTH HEIGHT DATA). Box can be
  916. ;; a list `(:width WIDTH :color COLOR)' or `(:width WIDTH :shadow
  917. ;; SHADOW)'.
  918. (when (and (or (eq attribute :stipple)
  919. (eq attribute :box))
  920. (or (consp old-value)
  921. (vectorp old-value)))
  922. (setq old-value (prin1-to-string old-value)))
  923. (cond ((listp valid)
  924. (let ((default
  925. (or (car (rassoc old-value valid))
  926. (format "%s" old-value))))
  927. (setq new-value
  928. (face-read-string face default attribute-name valid))
  929. (if (equal new-value default)
  930. ;; Nothing changed, so don't bother with all the stuff
  931. ;; below. In particular, this avoids a non-tty color
  932. ;; from being canonicalized for a tty when the user
  933. ;; just uses the default.
  934. (setq new-value old-value)
  935. ;; Terminal frames can support colors that don't appear
  936. ;; explicitly in VALID, using color approximation code
  937. ;; in tty-colors.el.
  938. (when (and (memq attribute '(:foreground :background))
  939. (not (memq (window-system frame) '(x w32 ns)))
  940. (not (member new-value
  941. '("unspecified"
  942. "unspecified-fg" "unspecified-bg"))))
  943. (setq new-value (car (tty-color-desc new-value frame))))
  944. (when (assoc new-value valid)
  945. (setq new-value (cdr (assoc new-value valid)))))))
  946. ((eq valid 'integerp)
  947. (setq new-value (face-read-integer face old-value attribute-name)))
  948. (t (error "Internal error")))
  949. ;; Convert stipple and box value text we read back to a list or
  950. ;; vector if it looks like one. This makes the assumption that a
  951. ;; pixmap file name won't start with an open-paren.
  952. (when (and (or (eq attribute :stipple)
  953. (eq attribute :box))
  954. (stringp new-value)
  955. (string-match "^[[(]" new-value))
  956. (setq new-value (read new-value)))
  957. new-value))
  958. (declare-function fontset-list "fontset.c" ())
  959. (declare-function x-list-fonts "xfaces.c"
  960. (pattern &optional face frame maximum width))
  961. (defun read-face-font (face &optional frame)
  962. "Read the name of a font for FACE on FRAME.
  963. If optional argument FRAME is nil or omitted, use the selected frame."
  964. (let ((completion-ignore-case t))
  965. (completing-read (format "Set font attributes of face `%s' from font: " face)
  966. (append (fontset-list) (x-list-fonts "*" nil frame)))))
  967. (defun read-all-face-attributes (face &optional frame)
  968. "Interactively read all attributes for FACE.
  969. If optional argument FRAME is nil or omitted, use the selected frame.
  970. Value is a property list of attribute names and new values."
  971. (let (result)
  972. (dolist (attribute face-attribute-name-alist result)
  973. (setq result (cons (car attribute)
  974. (cons (read-face-attribute face (car attribute) frame)
  975. result))))))
  976. (defun modify-face (&optional face foreground background stipple
  977. bold-p italic-p underline inverse-p frame)
  978. "Modify attributes of faces interactively.
  979. If optional argument FRAME is nil or omitted, modify the face used
  980. for newly created frame, i.e. the global face.
  981. For non-interactive use, `set-face-attribute' is preferred.
  982. When called from Lisp, if FACE is nil, all arguments but FRAME are ignored
  983. and the face and its settings are obtained by querying the user."
  984. (interactive)
  985. (if face
  986. (set-face-attribute face frame
  987. :foreground (or foreground 'unspecified)
  988. :background (or background 'unspecified)
  989. :stipple stipple
  990. :bold bold-p
  991. :italic italic-p
  992. :underline underline
  993. :inverse-video inverse-p)
  994. (setq face (read-face-name "Modify face"))
  995. (apply #'set-face-attribute face frame
  996. (read-all-face-attributes face frame))))
  997. (defun read-face-and-attribute (attribute &optional frame)
  998. "Read face name and face attribute value.
  999. ATTRIBUTE is the attribute whose new value is read.
  1000. FRAME nil or unspecified means read attribute value of global face.
  1001. Value is a list (FACE NEW-VALUE) where FACE is the face read
  1002. \(a symbol), and NEW-VALUE is value read."
  1003. (cond ((eq attribute :font)
  1004. (let* ((prompt "Set font-related attributes of face")
  1005. (face (read-face-name prompt))
  1006. (font (read-face-font face frame)))
  1007. (list face font)))
  1008. (t
  1009. (let* ((attribute-name (face-descriptive-attribute-name attribute))
  1010. (prompt (format "Set %s of face" attribute-name))
  1011. (face (read-face-name prompt))
  1012. (new-value (read-face-attribute face attribute frame)))
  1013. (list face new-value)))))
  1014. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1015. ;;; Listing faces.
  1016. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1017. (defconst list-faces-sample-text
  1018. "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1019. "*Text string to display as the sample text for `list-faces-display'.")
  1020. ;; The name list-faces would be more consistent, but let's avoid a
  1021. ;; conflict with Lucid, which uses that name differently.
  1022. (defvar help-xref-stack)
  1023. (defun list-faces-display (&optional regexp)
  1024. "List all faces, using the same sample text in each.
  1025. The sample text is a string that comes from the variable
  1026. `list-faces-sample-text'.
  1027. If REGEXP is non-nil, list only those faces with names matching
  1028. this regular expression. When called interactively with a prefix
  1029. arg, prompt for a regular expression."
  1030. (interactive (list (and current-prefix-arg
  1031. (read-regexp "List faces matching regexp"))))
  1032. (let ((all-faces (zerop (length regexp)))
  1033. (frame (selected-frame))
  1034. (max-length 0)
  1035. faces line-format
  1036. disp-frame window face-name)
  1037. ;; We filter and take the max length in one pass
  1038. (setq faces
  1039. (delq nil
  1040. (mapcar (lambda (f)
  1041. (let ((s (symbol-name f)))
  1042. (when (or all-faces (string-match regexp s))
  1043. (setq max-length (max (length s) max-length))
  1044. f)))
  1045. (sort (face-list) #'string-lessp))))
  1046. (unless faces
  1047. (error "No faces matching \"%s\"" regexp))
  1048. (setq max-length (1+ max-length)
  1049. line-format (format "%%-%ds" max-length))
  1050. (with-help-window "*Faces*"
  1051. (with-current-buffer standard-output
  1052. (setq truncate-lines t)
  1053. (insert
  1054. (substitute-command-keys
  1055. (concat
  1056. "\\<help-mode-map>Use "
  1057. (if (display-mouse-p) "\\[help-follow-mouse] or ")
  1058. "\\[help-follow] on a face name to customize it\n"
  1059. "or on its sample text for a description of the face.\n\n")))
  1060. (setq help-xref-stack nil)
  1061. (dolist (face faces)
  1062. (setq face-name (symbol-name face))
  1063. (insert (format line-format face-name))
  1064. ;; Hyperlink to a customization buffer for the face. Using
  1065. ;; the help xref mechanism may not be the best way.
  1066. (save-excursion
  1067. (save-match-data
  1068. (search-backward face-name)
  1069. (setq help-xref-stack-item `(list-faces-display ,regexp))
  1070. (help-xref-button 0 'help-customize-face face)))
  1071. (let ((beg (point))
  1072. (line-beg (line-beginning-position)))
  1073. (insert list-faces-sample-text)
  1074. ;; Hyperlink to a help buffer for the face.
  1075. (save-excursion
  1076. (save-match-data
  1077. (search-backward list-faces-sample-text)
  1078. (help-xref-button 0 'help-face face)))
  1079. (insert "\n")
  1080. (put-text-property beg (1- (point)) 'face face)
  1081. ;; Make all face commands default to the proper face
  1082. ;; anywhere in the line.
  1083. (put-text-property line-beg (1- (point)) 'read-face-name face)
  1084. ;; If the sample text has multiple lines, line up all of them.
  1085. (goto-char beg)
  1086. (forward-line 1)
  1087. (while (not (eobp))
  1088. (insert-char ?\s max-length)
  1089. (forward-line 1))))
  1090. (goto-char (point-min))))
  1091. ;; If the *Faces* buffer appears in a different frame,
  1092. ;; copy all the face definitions from FRAME,
  1093. ;; so that the display will reflect the frame that was selected.
  1094. (setq window (get-buffer-window (get-buffer "*Faces*") t))
  1095. (setq disp-frame (if window (window-frame window)
  1096. (car (frame-list))))
  1097. (or (eq frame disp-frame)
  1098. (let ((faces (face-list)))
  1099. (while faces
  1100. (copy-face (car faces) (car faces) frame disp-frame)
  1101. (setq faces (cdr faces)))))))
  1102. (defun describe-face (face &optional frame)
  1103. "Display the properties of face FACE on FRAME.
  1104. Interactively, FACE defaults to the faces of the character after point
  1105. and FRAME defaults to the selected frame.
  1106. If the optional argument FRAME is given, report on face FACE in that frame.
  1107. If FRAME is t, report on the defaults for face FACE (for new frames).
  1108. If FRAME is omitted or nil, use the selected frame."
  1109. (interactive (list (read-face-name "Describe face" 'default t)))
  1110. (let* ((attrs '((:family . "Family")
  1111. (:foundry . "Foundry")
  1112. (:width . "Width")
  1113. (:height . "Height")
  1114. (:weight . "Weight")
  1115. (:slant . "Slant")
  1116. (:foreground . "Foreground")
  1117. (:background . "Background")
  1118. (:underline . "Underline")
  1119. (:overline . "Overline")
  1120. (:strike-through . "Strike-through")
  1121. (:box . "Box")
  1122. (:inverse-video . "Inverse")
  1123. (:stipple . "Stipple")
  1124. (:font . "Font")
  1125. (:fontset . "Fontset")
  1126. (:inherit . "Inherit")))
  1127. (max-width (apply #'max (mapcar #'(lambda (x) (length (cdr x)))
  1128. attrs))))
  1129. (help-setup-xref (list #'describe-face face)
  1130. (called-interactively-p 'interactive))
  1131. (unless face
  1132. (setq face 'default))
  1133. (if (not (listp face))
  1134. (setq face (list face)))
  1135. (with-help-window (help-buffer)
  1136. (with-current-buffer standard-output
  1137. (dolist (f face)
  1138. (if (stringp f) (setq f (intern f)))
  1139. ;; We may get called for anonymous faces (i.e., faces
  1140. ;; expressed using prop-value plists). Those can't be
  1141. ;; usefully customized, so ignore them.
  1142. (when (symbolp f)
  1143. (insert "Face: " (symbol-name f))
  1144. (if (not (facep f))
  1145. (insert " undefined face.\n")
  1146. (let ((customize-label "customize this face")
  1147. file-name)
  1148. (insert (concat " (" (propertize "sample" 'font-lock-face f) ")"))
  1149. (princ (concat " (" customize-label ")\n"))
  1150. ;; FIXME not sure how much of this belongs here, and
  1151. ;; how much in `face-documentation'. The latter is
  1152. ;; not used much, but needs to return nil for
  1153. ;; undocumented faces.
  1154. (let ((alias (get f 'face-alias))
  1155. (face f)
  1156. obsolete)
  1157. (when alias
  1158. (setq face alias)
  1159. (insert
  1160. (format "\n %s is an alias for the face `%s'.\n%s"
  1161. f alias
  1162. (if (setq obsolete (get f 'obsolete-face))
  1163. (format " This face is obsolete%s; use `%s' instead.\n"
  1164. (if (stringp obsolete)
  1165. (format " since %s" obsolete)
  1166. "")
  1167. alias)
  1168. ""))))
  1169. (insert "\nDocumentation:\n"
  1170. (or (face-documentation face)
  1171. "Not documented as a face.")
  1172. "\n\n"))
  1173. (with-current-buffer standard-output
  1174. (save-excursion
  1175. (re-search-backward
  1176. (concat "\\(" customize-label "\\)") nil t)
  1177. (help-xref-button 1 'help-customize-face f)))
  1178. (setq file-name (find-lisp-object-file-name f 'defface))
  1179. (when file-name
  1180. (princ "Defined in `")
  1181. (princ (file-name-nondirectory file-name))
  1182. (princ "'")
  1183. ;; Make a hyperlink to the library.
  1184. (save-excursion
  1185. (re-search-backward "`\\([^`']+\\)'" nil t)
  1186. (help-xref-button 1 'help-face-def f file-name))
  1187. (princ ".")
  1188. (terpri)
  1189. (terpri))
  1190. (dolist (a attrs)
  1191. (let ((attr (face-attribute f (car a) frame)))
  1192. (insert (make-string (- max-width (length (cdr a))) ?\s)
  1193. (cdr a) ": " (format "%s" attr))
  1194. (if (and (eq (car a) :inherit)
  1195. (not (eq attr 'unspecified)))
  1196. ;; Make a hyperlink to the parent face.
  1197. (save-excursion
  1198. (re-search-backward ": \\([^:]+\\)" nil t)
  1199. (help-xref-button 1 'help-face attr)))
  1200. (insert "\n")))))
  1201. (terpri)))))))
  1202. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1203. ;;; Face specifications (defface).
  1204. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1205. ;; Parameter FRAME Is kept for call compatibility to with previous
  1206. ;; face implementation.
  1207. (defun face-attr-construct (face &optional _frame)
  1208. "Return a `defface'-style attribute list for FACE.
  1209. Value is a property list of pairs ATTRIBUTE VALUE for all specified
  1210. face attributes of FACE where ATTRIBUTE is the attribute name and
  1211. VALUE is the specified value of that attribute.
  1212. Argument FRAME is ignored and retained for compatibility."
  1213. (let (result)
  1214. (dolist (entry face-attribute-name-alist result)
  1215. (let* ((attribute (car entry))
  1216. (value (face-attribute face attribute)))
  1217. (unless (eq value 'unspecified)
  1218. (setq result (nconc (list attribute value) result)))))))
  1219. (defun face-spec-set-match-display (display frame)
  1220. "Non-nil if DISPLAY matches FRAME.
  1221. DISPLAY is part of a spec such as can be used in `defface'.
  1222. If FRAME is nil, the current FRAME is used."
  1223. (let* ((conjuncts display)
  1224. conjunct req options
  1225. ;; t means we have succeeded against all the conjuncts in
  1226. ;; DISPLAY that have been tested so far.
  1227. (match t))
  1228. (if (eq conjuncts t)
  1229. (setq conjuncts nil))
  1230. (while (and conjuncts match)
  1231. (setq conjunct (car conjuncts)
  1232. conjuncts (cdr conjuncts)
  1233. req (car conjunct)
  1234. options (cdr conjunct)
  1235. match (cond ((eq req 'type)
  1236. (or (memq (window-system frame) options)
  1237. (and (memq 'graphic options)
  1238. (memq (window-system frame) '(x w32 ns)))
  1239. ;; FIXME: This should be revisited to use
  1240. ;; display-graphic-p, provided that the
  1241. ;; color selection depends on the number
  1242. ;; of supported colors, and all defface's
  1243. ;; are changed to look at number of colors
  1244. ;; instead of (type graphic) etc.
  1245. (if (null (window-system frame))
  1246. (memq 'tty options)
  1247. (or (and (memq 'motif options)
  1248. (featurep 'motif))
  1249. (and (memq 'gtk options)
  1250. (featurep 'gtk))
  1251. (and (memq 'lucid options)
  1252. (featurep 'x-toolkit)
  1253. (not (featurep 'motif))
  1254. (not (featurep 'gtk)))
  1255. (and (memq 'x-toolkit options)
  1256. (featurep 'x-toolkit))))))
  1257. ((eq req 'min-colors)
  1258. (>= (display-color-cells frame) (car options)))
  1259. ((eq req 'class)
  1260. (memq (frame-parameter frame 'display-type) options))
  1261. ((eq req 'background)
  1262. (memq (frame-parameter frame 'background-mode)
  1263. options))
  1264. ((eq req 'supports)
  1265. (display-supports-face-attributes-p options frame))
  1266. (t (error "Unknown req `%S' with options `%S'"
  1267. req options)))))
  1268. match))
  1269. (defun face-spec-choose (spec &optional frame)
  1270. "Choose the proper attributes for FRAME, out of SPEC.
  1271. If SPEC is nil, return nil."
  1272. (unless frame
  1273. (setq frame (selected-frame)))
  1274. (let ((tail spec)
  1275. result defaults)
  1276. (while tail
  1277. (let* ((entry (pop tail))
  1278. (display (car entry))
  1279. (attrs (cdr entry))
  1280. thisval)
  1281. ;; Get the attributes as actually specified by this alternative.
  1282. (setq thisval
  1283. (if (null (cdr attrs)) ;; was (listp (car attrs))
  1284. ;; Old-style entry, the attribute list is the
  1285. ;; first element.
  1286. (car attrs)
  1287. attrs))
  1288. ;; If the condition is `default', that sets the default
  1289. ;; for following conditions.
  1290. (if (eq display 'default)
  1291. (setq defaults thisval)
  1292. ;; Otherwise, if it matches, use it.
  1293. (when (face-spec-set-match-display display frame)
  1294. (setq result thisval)
  1295. (setq tail nil)))))
  1296. (if defaults (append result defaults) result)))
  1297. (defun face-spec-reset-face (face &optional frame)
  1298. "Reset all attributes of FACE on FRAME to unspecified."
  1299. (apply 'set-face-attribute face frame
  1300. (if (eq face 'default)
  1301. ;; For the default face, avoid making any attribute
  1302. ;; unspecified. Instead, set attributes to default values
  1303. ;; (see also realize_default_face in xfaces.c).
  1304. (append
  1305. '(:underline nil :overline nil :strike-through nil
  1306. :box nil :inverse-video nil :stipple nil :inherit nil)
  1307. ;; `display-graphic-p' is unavailable when running
  1308. ;; temacs, prior to loading frame.el.
  1309. (unless (and (fboundp 'display-graphic-p)
  1310. (display-graphic-p frame))
  1311. '(:family "default" :foundry "default" :width normal
  1312. :height 1 :weight normal :slant normal
  1313. :foreground "unspecified-fg"
  1314. :background "unspecified-bg")))
  1315. ;; For all other faces, unspecify all attributes.
  1316. (apply 'append
  1317. (mapcar (lambda (x) (list (car x) 'unspecified))
  1318. face-attribute-name-alist)))))
  1319. (defun face-spec-set (face spec &optional for-defface)
  1320. "Set FACE's face spec, which controls its appearance, to SPEC.
  1321. If FOR-DEFFACE is t, set the base spec, the one that `defface'
  1322. and Custom set. (In that case, the caller must put it in the
  1323. appropriate property, because that depends on the caller.)
  1324. If FOR-DEFFACE is nil, set the overriding spec (and store it
  1325. in the `face-override-spec' property of FACE).
  1326. The appearance of FACE is controlled by the base spec,
  1327. by any custom theme specs on top of that, and by the
  1328. overriding spec on top of all the rest.
  1329. FOR-DEFFACE can also be a frame, in which case we set the
  1330. frame-specific attributes of FACE for that frame based on SPEC.
  1331. That usage is deprecated.
  1332. See `defface' for information about the format and meaning of SPEC."
  1333. (if (framep for-defface)
  1334. ;; Handle the deprecated case where third arg is a frame.
  1335. (face-spec-set-2 face for-defface spec)
  1336. (if for-defface
  1337. ;; When we reset the face based on its custom spec, then it is
  1338. ;; unmodified as far as Custom is concerned.
  1339. (put (or (get face 'face-alias) face) 'face-modified nil)
  1340. ;; When we change a face based on a spec from outside custom,
  1341. ;; record it for future frames.
  1342. (put (or (get face 'face-alias) face) 'face-override-spec spec))
  1343. ;; Reset each frame according to the rules implied by all its specs.
  1344. (dolist (frame (frame-list))
  1345. (face-spec-recalc face frame))))
  1346. (defun face-spec-recalc (face frame)
  1347. "Reset the face attributes of FACE on FRAME according to its specs.
  1348. This applies the defface/custom spec first, then the custom theme specs,
  1349. then the override spec."
  1350. (face-spec-reset-face face frame)
  1351. (let ((face-sym (or (get face 'face-alias) face)))
  1352. (or (get face 'customized-face)
  1353. (get face 'saved-face)
  1354. (face-spec-set-2 face frame (face-default-spec face)))
  1355. (let ((theme-faces (reverse (get face-sym 'theme-face))))
  1356. (dolist (spec theme-faces)
  1357. (face-spec-set-2 face frame (cadr spec))))
  1358. (face-spec-set-2 face frame (get face-sym 'face-override-spec))))
  1359. (defun face-spec-set-2 (face frame spec)
  1360. "Set the face attributes of FACE on FRAME according to SPEC."
  1361. (let* ((spec (face-spec-choose spec frame))
  1362. attrs)
  1363. (while spec
  1364. (when (assq (car spec) face-x-resources)
  1365. (push (car spec) attrs)
  1366. (push (cadr spec) attrs))
  1367. (setq spec (cddr spec)))
  1368. (apply 'set-face-attribute face frame (nreverse attrs))))
  1369. (defun face-attr-match-p (face attrs &optional frame)
  1370. "Return t if attributes of FACE match values in plist ATTRS.
  1371. Optional parameter FRAME is the frame whose definition of FACE
  1372. is used. If nil or omitted, use the selected frame."
  1373. (unless frame
  1374. (setq frame (selected-frame)))
  1375. (let* ((list face-attribute-name-alist)
  1376. (match t)
  1377. (bold (and (plist-member attrs :bold)
  1378. (not (plist-member attrs :weight))))
  1379. (italic (and (plist-member attrs :italic)
  1380. (not (plist-member attrs :slant))))
  1381. (plist (if (or bold italic)
  1382. (copy-sequence attrs)
  1383. attrs)))
  1384. ;; Handle the Emacs 20 :bold and :italic properties.
  1385. (if bold
  1386. (plist-put plist :weight (if bold 'bold 'normal)))
  1387. (if italic
  1388. (plist-put plist :slant (if italic 'italic 'normal)))
  1389. (while (and match list)
  1390. (let* ((attr (caar list))
  1391. (specified-value
  1392. (if (plist-member plist attr)
  1393. (plist-get plist attr)
  1394. 'unspecified))
  1395. (value-now (face-attribute face attr frame)))
  1396. (setq match (equal specified-value value-now))
  1397. (setq list (cdr list))))
  1398. match))
  1399. (defsubst face-spec-match-p (face spec &optional frame)
  1400. "Return t if FACE, on FRAME, matches what SPEC says it should look like."
  1401. (face-attr-match-p face (face-spec-choose spec frame) frame))
  1402. (defsubst face-default-spec (face)
  1403. "Return the default face-spec for FACE, ignoring any user customization.
  1404. If there is no default for FACE, return nil."
  1405. (get face 'face-defface-spec))
  1406. (defsubst face-user-default-spec (face)
  1407. "Return the user's customized face-spec for FACE, or the default if none.
  1408. If there is neither a user setting nor a default for FACE, return nil."
  1409. (or (get face 'customized-face)
  1410. (get face 'saved-face)
  1411. (face-default-spec face)))
  1412. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1413. ;;; Frame-type independent color support.
  1414. ;;; We keep the old x-* names as aliases for back-compatibility.
  1415. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1416. (defun defined-colors (&optional frame)
  1417. "Return a list of colors supported for a particular frame.
  1418. The argument FRAME specifies which frame to try.
  1419. The value may be different for frames on different display types.
  1420. If FRAME doesn't support colors, the value is nil.
  1421. If FRAME is nil, that stands for the selected frame."
  1422. (if (memq (framep (or frame (selected-frame))) '(x w32 ns))
  1423. (xw-defined-colors frame)
  1424. (mapcar 'car (tty-color-alist frame))))
  1425. (defalias 'x-defined-colors 'defined-colors)
  1426. (declare-function xw-color-defined-p "xfns.c" (color &optional frame))
  1427. (defun color-defined-p (color &optional frame)
  1428. "Return non-nil if color COLOR is supported on frame FRAME.
  1429. If FRAME is omitted or nil, use the selected frame.
  1430. If COLOR is the symbol `unspecified' or one of the strings
  1431. \"unspecified-fg\" or \"unspecified-bg\", the value is nil."
  1432. (if (member color '(unspecified "unspecified-bg" "unspecified-fg"))
  1433. nil
  1434. (if (member (framep (or frame (selected-frame))) '(x w32 ns))
  1435. (xw-color-defined-p color frame)
  1436. (numberp (tty-color-translate color frame)))))
  1437. (defalias 'x-color-defined-p 'color-defined-p)
  1438. (declare-function xw-color-values "xfns.c" (color &optional frame))
  1439. (defun color-values (color &optional frame)
  1440. "Return a description of the color named COLOR on frame FRAME.
  1441. COLOR should be a string naming a color (e.g. \"white\"), or a
  1442. string specifying a color's RGB components (e.g. \"#ff12ec\").
  1443. Return a list of three integers, (RED GREEN BLUE), each between 0
  1444. and either 65280 or 65535 (the maximum depends on the system).
  1445. Use `color-name-to-rgb' if you want RGB floating-point values
  1446. normalized to 1.0.
  1447. If FRAME is omitted or nil, use the selected frame.
  1448. If FRAME cannot display COLOR, the value is nil.
  1449. COLOR can also be the symbol `unspecified' or one of the strings
  1450. \"unspecified-fg\" or \"unspecified-bg\", in which case the
  1451. return value is nil."
  1452. (cond
  1453. ((member color '(unspecified "unspecified-fg" "unspecified-bg"))
  1454. nil)
  1455. ((memq (framep (or frame (selected-frame))) '(x w32 ns))
  1456. (xw-color-values color frame))
  1457. (t
  1458. (tty-color-values color frame))))
  1459. (defalias 'x-color-values 'color-values)
  1460. (declare-function xw-display-color-p "xfns.c" (&optional terminal))
  1461. (defun display-color-p (&optional display)
  1462. "Return t if DISPLAY supports color.
  1463. The optional argument DISPLAY specifies which display to ask about.
  1464. DISPLAY should be either a frame or a display name (a string).
  1465. If omitted or nil, that stands for the selected frame's display."
  1466. (if (memq (framep-on-display display) '(x w32 ns))
  1467. (xw-display-color-p display)
  1468. (tty-display-color-p display)))
  1469. (defalias 'x-display-color-p 'display-color-p)
  1470. (declare-function x-display-grayscale-p "xfns.c" (&optional terminal))
  1471. (defun display-grayscale-p (&optional display)
  1472. "Return non-nil if frames on DISPLAY can display shades of gray."
  1473. (let ((frame-type (framep-on-display display)))
  1474. (cond
  1475. ((memq frame-type '(x w32 ns))
  1476. (x-display-grayscale-p display))
  1477. (t
  1478. (> (tty-color-gray-shades display) 2)))))
  1479. (defun read-color (&optional prompt convert-to-RGB allow-empty-name msg)
  1480. "Read a color name or RGB triplet.
  1481. Completion is available for color names, but not for RGB triplets.
  1482. RGB triplets have the form \"#RRGGBB\". Each of the R, G, and B
  1483. components can have one to four digits, but all three components
  1484. must have the same number of digits. Each digit is a hex value
  1485. between 0 and F; either upper case or lower case for A through F
  1486. are acceptable.
  1487. In addition to standard color names and RGB hex values, the
  1488. following are available as color candidates. In each case, the
  1489. corresponding color is used.
  1490. * `foreground at point' - foreground under the cursor
  1491. * `background at point' - background under the cursor
  1492. Optional arg PROMPT is the prompt; if nil, use a default prompt.
  1493. Interactively, or with optional arg CONVERT-TO-RGB-P non-nil,
  1494. convert an input color name to an RGB hex string. Return the RGB
  1495. hex string.
  1496. If optional arg ALLOW-EMPTY-NAME is non-nil, the user is allowed
  1497. to enter an empty color name (the empty string).
  1498. Interactively, or with optional arg MSG non-nil, print the
  1499. resulting color name in the echo area."
  1500. (interactive "i\np\ni\np") ; Always convert to RGB interactively.
  1501. (let* ((completion-ignore-case t)
  1502. (colors (or facemenu-color-alist
  1503. (append '("foreground at point" "background at point")
  1504. (if allow-empty-name '(""))
  1505. (defined-colors))))
  1506. (color (completing-read
  1507. (or prompt "Color (name or #RGB triplet): ")
  1508. ;; Completing function for reading colors, accepting
  1509. ;; both color names and RGB triplets.
  1510. (lambda (string pred flag)
  1511. (cond
  1512. ((null flag) ; Try completion.
  1513. (or (try-completion string colors pred)
  1514. (if (color-defined-p string)
  1515. string)))
  1516. ((eq flag t) ; List all completions.
  1517. (or (all-completions string colors pred)
  1518. (if (color-defined-p string)
  1519. (list string))))
  1520. ((eq flag 'lambda) ; Test completion.
  1521. (or (memq string colors)
  1522. (color-defined-p string)))))
  1523. nil t)))
  1524. ;; Process named colors.
  1525. (when (member color colors)
  1526. (cond ((string-equal color "foreground at point")
  1527. (setq color (foreground-color-at-point)))
  1528. ((string-equal color "background at point")
  1529. (setq color (background-color-at-point))))
  1530. (when (and convert-to-RGB
  1531. (not (string-equal color "")))
  1532. (let ((components (x-color-values color)))
  1533. (unless (string-match "^#\\([a-fA-F0-9][a-fA-F0-9][a-fA-F0-9]\\)+$" color)
  1534. (setq color (format "#%04X%04X%04X"
  1535. (logand 65535 (nth 0 components))
  1536. (logand 65535 (nth 1 components))
  1537. (logand 65535 (nth 2 components))))))))
  1538. (when msg (message "Color: `%s'" color))
  1539. color))
  1540. (defun face-at-point ()
  1541. "Return the face of the character after point.
  1542. If it has more than one face, return the first one.
  1543. Return nil if it has no specified face."
  1544. (let* ((faceprop (or (get-char-property (point) 'read-face-name)
  1545. (get-char-property (point) 'face)
  1546. 'default))
  1547. (face (cond ((symbolp faceprop) faceprop)
  1548. ;; List of faces (don't treat an attribute spec).
  1549. ;; Just use the first face.
  1550. ((and (consp faceprop) (not (keywordp (car faceprop)))
  1551. (not (memq (car faceprop)
  1552. '(foreground-color background-color))))
  1553. (car faceprop))
  1554. (t nil)))) ; Invalid face value.
  1555. (if (facep face) face nil)))
  1556. (defun foreground-color-at-point ()
  1557. "Return the foreground color of the character after point."
  1558. ;; `face-at-point' alone is not sufficient. It only gets named faces.
  1559. ;; Need also pick up any face properties that are not associated with named faces.
  1560. (let ((face (or (face-at-point)
  1561. (get-char-property (point) 'read-face-name)
  1562. (get-char-property (point) 'face))))
  1563. (cond ((and face (symbolp face))
  1564. (let ((value (face-foreground face nil 'default)))
  1565. (if (member value '("unspecified-fg" "unspecified-bg"))
  1566. nil
  1567. value)))
  1568. ((consp face)
  1569. (cond ((memq 'foreground-color face) (cdr (memq 'foreground-color face)))
  1570. ((memq ':foreground face) (cadr (memq ':foreground face)))))
  1571. (t nil)))) ; Invalid face value.
  1572. (defun background-color-at-point ()
  1573. "Return the background color of the character after point."
  1574. ;; `face-at-point' alone is not sufficient. It only gets named faces.
  1575. ;; Need also pick up any face properties that are not associated with named faces.
  1576. (let ((face (or (face-at-point)
  1577. (get-char-property (point) 'read-face-name)
  1578. (get-char-property (point) 'face))))
  1579. (cond ((and face (symbolp face))
  1580. (let ((value (face-background face nil 'default)))
  1581. (if (member value '("unspecified-fg" "unspecified-bg"))
  1582. nil
  1583. value)))
  1584. ((consp face)
  1585. (cond ((memq 'background-color face) (cdr (memq 'background-color face)))
  1586. ((memq ':background face) (cadr (memq ':background face)))))
  1587. (t nil)))) ; Invalid face value.
  1588. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1589. ;;; Frame creation.
  1590. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1591. (declare-function x-parse-geometry "frame.c" (string))
  1592. (defun x-handle-named-frame-geometry (parameters)
  1593. "Add geometry parameters for a named frame to parameter list PARAMETERS.
  1594. Value is the new parameter list."
  1595. ;; Note that `x-resource-name' has a global meaning.
  1596. (let ((x-resource-name (cdr (assq 'name parameters))))
  1597. (when x-resource-name
  1598. ;; Before checking X resources, we must have an X connection.
  1599. (or (window-system)
  1600. (x-display-list)
  1601. (x-open-connection (or (cdr (assq 'display parameters))
  1602. x-display-name)))
  1603. (let (res-geometry parsed)
  1604. (and (setq res-geometry (x-get-resource "geometry" "Geometry"))
  1605. (setq parsed (x-parse-geometry res-geometry))
  1606. (setq parameters
  1607. (append parameters parsed
  1608. ;; If the resource specifies a position,
  1609. ;; take note of that.
  1610. (if (or (assq 'top parsed) (assq 'left parsed))
  1611. '((user-position . t) (user-size . t)))))))))
  1612. parameters)
  1613. (defun x-handle-reverse-video (frame parameters)
  1614. "Handle the reverse-video frame parameter and X resource.
  1615. `x-create-frame' does not handle this one."
  1616. (when (cdr (or (assq 'reverse parameters)
  1617. (let ((resource (x-get-resource "reverseVideo"
  1618. "ReverseVideo")))
  1619. (if resource
  1620. (cons nil (member (downcase resource)
  1621. '("on" "true")))))))
  1622. (let* ((params (frame-parameters frame))
  1623. (bg (cdr (assq 'foreground-color params)))
  1624. (fg (cdr (assq 'background-color params))))
  1625. (modify-frame-parameters frame
  1626. (list (cons 'foreground-color fg)
  1627. (cons 'background-color bg)))
  1628. (if (equal bg (cdr (assq 'border-color params)))
  1629. (modify-frame-parameters frame
  1630. (list (cons 'border-color fg))))
  1631. (if (equal bg (cdr (assq 'mouse-color params)))
  1632. (modify-frame-parameters frame
  1633. (list (cons 'mouse-color fg))))
  1634. (if (equal bg (cdr (assq 'cursor-color params)))
  1635. (modify-frame-parameters frame
  1636. (list (cons 'cursor-color fg)))))))
  1637. (declare-function x-create-frame "xfns.c" (parms))
  1638. (declare-function x-setup-function-keys "term/common-win" (frame))
  1639. (defun x-create-frame-with-faces (&optional parameters)
  1640. "Create and return a frame with frame parameters PARAMETERS.
  1641. If PARAMETERS specify a frame name, handle X geometry resources
  1642. for that name. If PARAMETERS includes a `reverse' parameter, or
  1643. the X resource ``reverseVideo'' is present, handle that."
  1644. (setq parameters (x-handle-named-frame-geometry parameters))
  1645. (let* ((params (copy-tree parameters))
  1646. (visibility-spec (assq 'visibility parameters))
  1647. (delayed-params '(foreground-color background-color font
  1648. border-color cursor-color mouse-color
  1649. visibility scroll-bar-foreground
  1650. scroll-bar-background))
  1651. frame success)
  1652. (dolist (param delayed-params)
  1653. (setq params (assq-delete-all param params)))
  1654. (setq frame (x-create-frame `((visibility . nil) . ,params)))
  1655. (unwind-protect
  1656. (progn
  1657. (x-setup-function-keys frame)
  1658. (x-handle-reverse-video frame parameters)
  1659. (frame-set-background-mode frame t)
  1660. (face-set-after-frame-default frame parameters)
  1661. (if (null visibility-spec)
  1662. (make-frame-visible frame)
  1663. (modify-frame-parameters frame (list visibility-spec)))
  1664. (setq success t))
  1665. (unless success
  1666. (delete-frame frame)))
  1667. frame))
  1668. (defun face-set-after-frame-default (frame &optional parameters)
  1669. "Initialize the frame-local faces of FRAME.
  1670. Calculate the face definitions using the face specs, custom theme
  1671. settings, X resources, and `face-new-frame-defaults'.
  1672. Finally, apply any relevant face attributes found amongst the
  1673. frame parameters in PARAMETERS."
  1674. (let ((window-system-p (memq (window-system frame) '(x w32))))
  1675. ;; The `reverse' is so that `default' goes first.
  1676. (dolist (face (nreverse (face-list)))
  1677. (condition-case ()
  1678. (progn
  1679. ;; Initialize faces from face spec and custom theme.
  1680. (face-spec-recalc face frame)
  1681. ;; X resources for the default face are applied during
  1682. ;; `x-create-frame'.
  1683. (and (not (eq face 'default)) window-system-p
  1684. (make-face-x-resource-internal face frame))
  1685. ;; Apply attributes specified by face-new-frame-defaults
  1686. (internal-merge-in-global-face face frame))
  1687. ;; Don't let invalid specs prevent frame creation.
  1688. (error nil))))
  1689. ;; Apply attributes specified by frame parameters.
  1690. (let ((face-params '((foreground-color default :foreground)
  1691. (background-color default :background)
  1692. (font default :font)
  1693. (border-color border :background)
  1694. (cursor-color cursor :background)
  1695. (scroll-bar-foreground scroll-bar :foreground)
  1696. (scroll-bar-background scroll-bar :background)
  1697. (mouse-color mouse :background))))
  1698. (dolist (param face-params)
  1699. (let* ((param-name (nth 0 param))
  1700. (value (cdr (assq param-name parameters))))
  1701. (if value
  1702. (set-face-attribute (nth 1 param) frame
  1703. (nth 2 param) value))))))
  1704. (defun tty-handle-reverse-video (frame parameters)
  1705. "Handle the reverse-video frame parameter for terminal frames."
  1706. (when (cdr (assq 'reverse parameters))
  1707. (let* ((params (frame-parameters frame))
  1708. (bg (cdr (assq 'foreground-color params)))
  1709. (fg (cdr (assq 'background-color params))))
  1710. (modify-frame-parameters frame
  1711. (list (cons 'foreground-color fg)
  1712. (cons 'background-color bg)))
  1713. (if (equal bg (cdr (assq 'mouse-color params)))
  1714. (modify-frame-parameters frame
  1715. (list (cons 'mouse-color fg))))
  1716. (if (equal bg (cdr (assq 'cursor-color params)))
  1717. (modify-frame-parameters frame
  1718. (list (cons 'cursor-color fg)))))))
  1719. (defun tty-create-frame-with-faces (&optional parameters)
  1720. "Create and return a frame from optional frame parameters PARAMETERS.
  1721. If PARAMETERS contains a `reverse' parameter, handle that."
  1722. (let ((frame (make-terminal-frame parameters))
  1723. success)
  1724. (unwind-protect
  1725. (with-selected-frame frame
  1726. (tty-handle-reverse-video frame (frame-parameters frame))
  1727. (unless (terminal-parameter frame 'terminal-initted)
  1728. (set-terminal-parameter frame 'terminal-initted t)
  1729. (set-locale-environment nil frame)
  1730. (tty-run-terminal-initialization frame))
  1731. (frame-set-background-mode frame t)
  1732. (face-set-after-frame-default frame parameters)
  1733. (setq success t))
  1734. (unless success
  1735. (delete-frame frame)))
  1736. frame))
  1737. (defun tty-find-type (pred type)
  1738. "Return the longest prefix of TYPE to which PRED returns non-nil.
  1739. TYPE should be a tty type name such as \"xterm-16color\".
  1740. The function tries only those prefixes that are followed by a
  1741. dash or underscore in the original type name, like \"xterm\" in
  1742. the above example."
  1743. (let (hyphend)
  1744. (while (and type
  1745. (not (funcall pred type)))
  1746. ;; Strip off last hyphen and what follows, then try again
  1747. (setq type
  1748. (if (setq hyphend (string-match "[-_][^-_]+$" type))
  1749. (substring type 0 hyphend)
  1750. nil))))
  1751. type)
  1752. (defun tty-run-terminal-initialization (frame &optional type)
  1753. "Run the special initialization code for the terminal type of FRAME.
  1754. The optional TYPE parameter may be used to override the autodetected
  1755. terminal type to a different value."
  1756. (setq type (or type (tty-type frame)))
  1757. ;; Load library for our terminal type.
  1758. ;; User init file can set term-file-prefix to nil to prevent this.
  1759. (with-selected-frame frame
  1760. (unless (null term-file-prefix)
  1761. (let* (term-init-func)
  1762. ;; First, load the terminal initialization file, if it is
  1763. ;; available and it hasn't been loaded already.
  1764. (tty-find-type #'(lambda (type)
  1765. (let ((file (locate-library (concat term-file-prefix type))))
  1766. (and file
  1767. (or (assoc file load-history)
  1768. (load file t t)))))
  1769. type)
  1770. ;; Next, try to find a matching initialization function, and call it.
  1771. (tty-find-type #'(lambda (type)
  1772. (fboundp (setq term-init-func
  1773. (intern (concat "terminal-init-" type)))))
  1774. type)
  1775. (when (fboundp term-init-func)
  1776. (funcall term-init-func))
  1777. (set-terminal-parameter frame 'terminal-initted term-init-func)))))
  1778. ;; Called from C function init_display to initialize faces of the
  1779. ;; dumped terminal frame on startup.
  1780. (defun tty-set-up-initial-frame-faces ()
  1781. (let ((frame (selected-frame)))
  1782. (frame-set-background-mode frame t)
  1783. (face-set-after-frame-default frame)))
  1784. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1785. ;;; Standard faces.
  1786. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1787. (defgroup basic-faces nil
  1788. "The standard faces of Emacs."
  1789. :group 'faces)
  1790. (defface default
  1791. '((t nil)) ; If this were nil, face-defface-spec would not be set.
  1792. "Basic default face."
  1793. :group 'basic-faces)
  1794. (defface bold
  1795. '((t :weight bold))
  1796. "Basic bold face."
  1797. :group 'basic-faces)
  1798. (defface italic
  1799. '((((supports :slant italic))
  1800. :slant italic)
  1801. (((supports :underline t))
  1802. :underline t)
  1803. (t
  1804. ;; Default to italic, even if it doesn't appear to be supported,
  1805. ;; because in some cases the display engine will do its own
  1806. ;; workaround (to `dim' on ttys).
  1807. :slant italic))
  1808. "Basic italic face."
  1809. :group 'basic-faces)
  1810. (defface bold-italic
  1811. '((t :weight bold :slant italic))
  1812. "Basic bold-italic face."
  1813. :group 'basic-faces)
  1814. (defface underline
  1815. '((((supports :underline t))
  1816. :underline t)
  1817. (((supports :weight bold))
  1818. :weight bold)
  1819. (t :underline t))
  1820. "Basic underlined face."
  1821. :group 'basic-faces)
  1822. (defface fixed-pitch
  1823. '((t :family "Monospace"))
  1824. "The basic fixed-pitch face."
  1825. :group 'basic-faces)
  1826. (defface variable-pitch
  1827. '((t :family "Sans Serif"))
  1828. "The basic variable-pitch face."
  1829. :group 'basic-faces)
  1830. (defface shadow
  1831. '((((class color grayscale) (min-colors 88) (background light))
  1832. :foreground "grey50")
  1833. (((class color grayscale) (min-colors 88) (background dark))
  1834. :foreground "grey70")
  1835. (((class color) (min-colors 8) (background light))
  1836. :foreground "green")
  1837. (((class color) (min-colors 8) (background dark))
  1838. :foreground "yellow"))
  1839. "Basic face for shadowed text."
  1840. :group 'basic-faces
  1841. :version "22.1")
  1842. (defface link
  1843. '((((class color) (min-colors 88) (background light))
  1844. :foreground "RoyalBlue3" :underline t)
  1845. (((class color) (background light))
  1846. :foreground "blue" :underline t)
  1847. (((class color) (min-colors 88) (background dark))
  1848. :foreground "cyan1" :underline t)
  1849. (((class color) (background dark))
  1850. :foreground "cyan" :underline t)
  1851. (t :inherit underline))
  1852. "Basic face for unvisited links."
  1853. :group 'basic-faces
  1854. :version "22.1")
  1855. (defface link-visited
  1856. '((default :inherit link)
  1857. (((class color) (background light)) :foreground "magenta4")
  1858. (((class color) (background dark)) :foreground "violet"))
  1859. "Basic face for visited links."
  1860. :group 'basic-faces
  1861. :version "22.1")
  1862. (defface highlight
  1863. '((((class color) (min-colors 88) (background light))
  1864. :background "darkseagreen2")
  1865. (((class color) (min-colors 88) (background dark))
  1866. :background "darkolivegreen")
  1867. (((class color) (min-colors 16) (background light))
  1868. :background "darkseagreen2")
  1869. (((class color) (min-colors 16) (background dark))
  1870. :background "darkolivegreen")
  1871. (((class color) (min-colors 8))
  1872. :background "green" :foreground "black")
  1873. (t :inverse-video t))
  1874. "Basic face for highlighting."
  1875. :group 'basic-faces)
  1876. ;; Region face: under NS, default to the system-defined selection
  1877. ;; color (optimized for the fixed white background of other apps),
  1878. ;; if background is light.
  1879. (defface region
  1880. '((((class color) (min-colors 88) (background dark))
  1881. :background "blue3")
  1882. (((class color) (min-colors 88) (background light) (type gtk))
  1883. :foreground "gtk_selection_fg_color"
  1884. :background "gtk_selection_bg_color")
  1885. (((class color) (min-colors 88) (background light) (type ns))
  1886. :background "ns_selection_color")
  1887. (((class color) (min-colors 88) (background light))
  1888. :background "lightgoldenrod2")
  1889. (((class color) (min-colors 16) (background dark))
  1890. :background "blue3")
  1891. (((class color) (min-colors 16) (background light))
  1892. :background "lightgoldenrod2")
  1893. (((class color) (min-colors 8))
  1894. :background "blue" :foreground "white")
  1895. (((type tty) (class mono))
  1896. :inverse-video t)
  1897. (t :background "gray"))
  1898. "Basic face for highlighting the region."
  1899. :version "21.1"
  1900. :group 'basic-faces)
  1901. (defface secondary-selection
  1902. '((((class color) (min-colors 88) (background light))
  1903. :background "yellow1")
  1904. (((class color) (min-colors 88) (background dark))
  1905. :background "SkyBlue4")
  1906. (((class color) (min-colors 16) (background light))
  1907. :background "yellow")
  1908. (((class color) (min-colors 16) (background dark))
  1909. :background "SkyBlue4")
  1910. (((class color) (min-colors 8))
  1911. :background "cyan" :foreground "black")
  1912. (t :inverse-video t))
  1913. "Basic face for displaying the secondary selection."
  1914. :group 'basic-faces)
  1915. (defface trailing-whitespace
  1916. '((((class color) (background light))
  1917. :background "red1")
  1918. (((class color) (background dark))
  1919. :background "red1")
  1920. (t :inverse-video t))
  1921. "Basic face for highlighting trailing whitespace."
  1922. :version "21.1"
  1923. :group 'whitespace-faces ; like `show-trailing-whitespace'
  1924. :group 'basic-faces)
  1925. (defface escape-glyph
  1926. '((((background dark)) :foreground "cyan")
  1927. ;; See the comment in minibuffer-prompt for
  1928. ;; the reason not to use blue on MS-DOS.
  1929. (((type pc)) :foreground "magenta")
  1930. ;; red4 is too dark, but some say blue is too loud.
  1931. ;; brown seems to work ok. -- rms.
  1932. (t :foreground "brown"))
  1933. "Face for characters displayed as sequences using `^' or `\\'."
  1934. :group 'basic-faces
  1935. :version "22.1")
  1936. (defface nobreak-space
  1937. '((((class color) (min-colors 88)) :inherit escape-glyph :underline t)
  1938. (((class color) (min-colors 8)) :background "magenta")
  1939. (t :inverse-video t))
  1940. "Face for displaying nobreak space."
  1941. :group 'basic-faces
  1942. :version "22.1")
  1943. (defgroup mode-line-faces nil
  1944. "Faces used in the mode line."
  1945. :group 'mode-line
  1946. :group 'faces
  1947. :version "22.1")
  1948. (defface mode-line
  1949. '((((class color) (min-colors 88))
  1950. :box (:line-width -1 :style released-button)
  1951. :background "grey75" :foreground "black")
  1952. (t
  1953. :inverse-video t))
  1954. "Basic mode line face for selected window."
  1955. :version "21.1"
  1956. :group 'mode-line-faces
  1957. :group 'basic-faces)
  1958. ;; No need to define aliases of this form for new faces.
  1959. (define-obsolete-face-alias 'modeline 'mode-line "21.1")
  1960. (defface mode-line-inactive
  1961. '((default
  1962. :inherit mode-line)
  1963. (((class color) (min-colors 88) (background light))
  1964. :weight light
  1965. :box (:line-width -1 :color "grey75" :style nil)
  1966. :foreground "grey20" :background "grey90")
  1967. (((class color) (min-colors 88) (background dark) )
  1968. :weight light
  1969. :box (:line-width -1 :color "grey40" :style nil)
  1970. :foreground "grey80" :background "grey30"))
  1971. "Basic mode line face for non-selected windows."
  1972. :version "22.1"
  1973. :group 'mode-line-faces
  1974. :group 'basic-faces)
  1975. (define-obsolete-face-alias 'modeline-inactive 'mode-line-inactive "22.1")
  1976. (defface mode-line-highlight
  1977. '((((class color) (min-colors 88))
  1978. :box (:line-width 2 :color "grey40" :style released-button))
  1979. (t
  1980. :inherit highlight))
  1981. "Basic mode line face for highlighting."
  1982. :version "22.1"
  1983. :group 'mode-line-faces
  1984. :group 'basic-faces)
  1985. (define-obsolete-face-alias 'modeline-highlight 'mode-line-highlight "22.1")
  1986. (defface mode-line-emphasis
  1987. '((t (:weight bold)))
  1988. "Face used to emphasize certain mode line features.
  1989. Use the face `mode-line-highlight' for features that can be selected."
  1990. :version "23.1"
  1991. :group 'mode-line-faces
  1992. :group 'basic-faces)
  1993. (defface mode-line-buffer-id
  1994. '((t (:weight bold)))
  1995. "Face used for buffer identification parts of the mode line."
  1996. :version "22.1"
  1997. :group 'mode-line-faces
  1998. :group 'basic-faces)
  1999. (define-obsolete-face-alias 'modeline-buffer-id 'mode-line-buffer-id "22.1")
  2000. (defface header-line
  2001. '((default
  2002. :inherit mode-line)
  2003. (((type tty))
  2004. ;; This used to be `:inverse-video t', but that doesn't look very
  2005. ;; good when combined with inverse-video mode-lines and multiple
  2006. ;; windows. Underlining looks better, and is more consistent with
  2007. ;; the window-system face variants, which deemphasize the
  2008. ;; header-line in relation to the mode-line face. If a terminal
  2009. ;; can't underline, then the header-line will end up without any
  2010. ;; highlighting; this may be too confusing in general, although it
  2011. ;; happens to look good with the only current use of header-lines,
  2012. ;; the info browser. XXX
  2013. :inverse-video nil ;Override the value inherited from mode-line.
  2014. :underline t)
  2015. (((class color grayscale) (background light))
  2016. :background "grey90" :foreground "grey20"
  2017. :box nil)
  2018. (((class color grayscale) (background dark))
  2019. :background "grey20" :foreground "grey90"
  2020. :box nil)
  2021. (((class mono) (background light))
  2022. :background "white" :foreground "black"
  2023. :inverse-video nil
  2024. :box nil
  2025. :underline t)
  2026. (((class mono) (background dark))
  2027. :background "black" :foreground "white"
  2028. :inverse-video nil
  2029. :box nil
  2030. :underline t))
  2031. "Basic header-line face."
  2032. :version "21.1"
  2033. :group 'basic-faces)
  2034. (defface vertical-border
  2035. '((((type tty)) :inherit mode-line-inactive))
  2036. "Face used for vertical window dividers on ttys."
  2037. :version "22.1"
  2038. :group 'basic-faces)
  2039. (defface minibuffer-prompt
  2040. '((((background dark)) :foreground "cyan")
  2041. ;; Don't use blue because many users of the MS-DOS port customize
  2042. ;; their foreground color to be blue.
  2043. (((type pc)) :foreground "magenta")
  2044. (t :foreground "medium blue"))
  2045. "Face for minibuffer prompts.
  2046. By default, Emacs automatically adds this face to the value of
  2047. `minibuffer-prompt-properties', which is a list of text properties
  2048. used to display the prompt text."
  2049. :version "22.1"
  2050. :group 'basic-faces)
  2051. (setq minibuffer-prompt-properties
  2052. (append minibuffer-prompt-properties (list 'face 'minibuffer-prompt)))
  2053. (defface fringe
  2054. '((((class color) (background light))
  2055. :background "grey95")
  2056. (((class color) (background dark))
  2057. :background "grey10")
  2058. (t
  2059. :background "gray"))
  2060. "Basic face for the fringes to the left and right of windows under X."
  2061. :version "21.1"
  2062. :group 'frames
  2063. :group 'basic-faces)
  2064. (defface scroll-bar '((t nil))
  2065. "Basic face for the scroll bar colors under X."
  2066. :version "21.1"
  2067. :group 'frames
  2068. :group 'basic-faces)
  2069. (defface border '((t nil))
  2070. "Basic face for the frame border under X."
  2071. :version "21.1"
  2072. :group 'frames
  2073. :group 'basic-faces)
  2074. (defface cursor
  2075. '((((background light)) :background "black")
  2076. (((background dark)) :background "white"))
  2077. "Basic face for the cursor color under X.
  2078. Currently, only the `:background' attribute is meaningful; all
  2079. other attributes are ignored. The cursor foreground color is
  2080. taken from the background color of the underlying text.
  2081. Note: Other faces cannot inherit from the cursor face."
  2082. :version "21.1"
  2083. :group 'cursor
  2084. :group 'basic-faces)
  2085. (put 'cursor 'face-no-inherit t)
  2086. (defface mouse '((t nil))
  2087. "Basic face for the mouse color under X."
  2088. :version "21.1"
  2089. :group 'mouse
  2090. :group 'basic-faces)
  2091. (defface tool-bar
  2092. '((default
  2093. :box (:line-width 1 :style released-button)
  2094. :foreground "black")
  2095. (((type x w32 ns) (class color))
  2096. :background "grey75")
  2097. (((type x) (class mono))
  2098. :background "grey"))
  2099. "Basic tool-bar face."
  2100. :version "21.1"
  2101. :group 'basic-faces)
  2102. (defface menu
  2103. '((((type tty))
  2104. :inverse-video t)
  2105. (((type x-toolkit))
  2106. )
  2107. (t
  2108. :inverse-video t))
  2109. "Basic face for the font and colors of the menu bar and popup menus."
  2110. :version "21.1"
  2111. :group 'menu
  2112. :group 'basic-faces)
  2113. (defface help-argument-name '((((supports :slant italic)) :inherit italic))
  2114. "Face to highlight argument names in *Help* buffers."
  2115. :group 'help)
  2116. (defface glyphless-char
  2117. '((((type tty)) :inherit underline)
  2118. (((type pc)) :inherit escape-glyph)
  2119. (t :height 0.6))
  2120. "Face for displaying non-graphic characters (e.g. U+202A (LRE)).
  2121. It is used for characters of no fonts too."
  2122. :version "24.1"
  2123. :group 'basic-faces)
  2124. (defface error
  2125. '((((class color) (min-colors 88) (background light)) (:foreground "Red1" :weight bold))
  2126. (((class color) (min-colors 88) (background dark)) (:foreground "Pink" :weight bold))
  2127. (((class color) (min-colors 16) (background light)) (:foreground "Red1" :weight bold))
  2128. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :weight bold))
  2129. (((class color) (min-colors 8)) (:foreground "red"))
  2130. (t (:inverse-video t :weight bold)))
  2131. "Basic face used to highlight errors and to denote failure."
  2132. :version "24.1"
  2133. :group 'basic-faces)
  2134. (defface warning
  2135. '((((class color) (min-colors 16)) (:foreground "DarkOrange" :weight bold))
  2136. (((class color)) (:foreground "yellow" :weight bold))
  2137. (t (:weight bold)))
  2138. "Basic face used to highlight warnings."
  2139. :version "24.1"
  2140. :group 'basic-faces)
  2141. (defface success
  2142. '((((class color) (min-colors 16) (background light))
  2143. (:foreground "ForestGreen" :weight bold))
  2144. (((class color) (min-colors 88) (background dark))
  2145. (:foreground "Green1" :weight bold))
  2146. (((class color) (min-colors 16) (background dark))
  2147. (:foreground "Green" :weight bold))
  2148. (((class color)) (:foreground "green" :weight bold))
  2149. (t (:weight bold)))
  2150. "Basic face used to indicate successful operation."
  2151. :version "24.1"
  2152. :group 'basic-faces)
  2153. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2154. ;;; Manipulating font names.
  2155. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2156. ;; This is here for compatibility with Emacs 20.2. For example,
  2157. ;; international/fontset.el uses x-resolve-font-name. The following
  2158. ;; functions are not used in the face implementation itself.
  2159. (defvar x-font-regexp nil)
  2160. (defvar x-font-regexp-head nil)
  2161. (defvar x-font-regexp-weight nil)
  2162. (defvar x-font-regexp-slant nil)
  2163. (defconst x-font-regexp-weight-subnum 1)
  2164. (defconst x-font-regexp-slant-subnum 2)
  2165. (defconst x-font-regexp-swidth-subnum 3)
  2166. (defconst x-font-regexp-adstyle-subnum 4)
  2167. ;;; Regexps matching font names in "Host Portable Character Representation."
  2168. ;;;
  2169. (let ((- "[-?]")
  2170. (foundry "[^-]+")
  2171. (family "[^-]+")
  2172. (weight "\\(bold\\|demibold\\|medium\\)") ; 1
  2173. ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
  2174. (weight\? "\\([^-]*\\)") ; 1
  2175. (slant "\\([ior]\\)") ; 2
  2176. ; (slant\? "\\([ior?*]?\\)") ; 2
  2177. (slant\? "\\([^-]?\\)") ; 2
  2178. ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
  2179. (swidth "\\([^-]*\\)") ; 3
  2180. ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
  2181. (adstyle "\\([^-]*\\)") ; 4
  2182. (pixelsize "[0-9]+")
  2183. (pointsize "[0-9][0-9]+")
  2184. (resx "[0-9][0-9]+")
  2185. (resy "[0-9][0-9]+")
  2186. (spacing "[cmp?*]")
  2187. (avgwidth "[0-9]+")
  2188. (registry "[^-]+")
  2189. (encoding "[^-]+")
  2190. )
  2191. (setq x-font-regexp
  2192. (purecopy (concat "\\`\\*?[-?*]"
  2193. foundry - family - weight\? - slant\? - swidth - adstyle -
  2194. pixelsize - pointsize - resx - resy - spacing - avgwidth -
  2195. registry - encoding "\\*?\\'"
  2196. )))
  2197. (setq x-font-regexp-head
  2198. (purecopy (concat "\\`[-?*]" foundry - family - weight\? - slant\?
  2199. "\\([-*?]\\|\\'\\)")))
  2200. (setq x-font-regexp-slant (purecopy (concat - slant -)))
  2201. (setq x-font-regexp-weight (purecopy (concat - weight -)))
  2202. nil)
  2203. (defun x-resolve-font-name (pattern &optional face frame)
  2204. "Return a font name matching PATTERN.
  2205. All wildcards in PATTERN are instantiated.
  2206. If PATTERN is nil, return the name of the frame's base font, which never
  2207. contains wildcards.
  2208. Given optional arguments FACE and FRAME, return a font which is
  2209. also the same size as FACE on FRAME, or fail."
  2210. (or (symbolp face)
  2211. (setq face (face-name face)))
  2212. (and (eq frame t)
  2213. (setq frame nil))
  2214. (if pattern
  2215. ;; Note that x-list-fonts has code to handle a face with nil as its font.
  2216. (let ((fonts (x-list-fonts pattern face frame 1)))
  2217. (or fonts
  2218. (if face
  2219. (if (string-match "\\*" pattern)
  2220. (if (null (face-font face))
  2221. (error "No matching fonts are the same height as the frame default font")
  2222. (error "No matching fonts are the same height as face `%s'" face))
  2223. (if (null (face-font face))
  2224. (error "Height of font `%s' doesn't match the frame default font"
  2225. pattern)
  2226. (error "Height of font `%s' doesn't match face `%s'"
  2227. pattern face)))
  2228. (error "No fonts match `%s'" pattern)))
  2229. (car fonts))
  2230. (cdr (assq 'font (frame-parameters (selected-frame))))))
  2231. (provide 'faces)
  2232. ;;; faces.el ends here