custom.el 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. ;;; custom.el --- tools for declaring and initializing options
  2. ;;
  3. ;; Copyright (C) 1996-1997, 1999, 2001-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
  6. ;; Maintainer: FSF
  7. ;; Keywords: help, faces
  8. ;; Package: emacs
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;;
  22. ;; This file only contains the code needed to declare and initialize
  23. ;; user options. The code to customize options is autoloaded from
  24. ;; `cus-edit.el' and is documented in the Emacs Lisp Reference manual.
  25. ;; The code implementing face declarations is in `cus-face.el'.
  26. ;;; Code:
  27. (require 'widget)
  28. (defvar custom-define-hook nil
  29. ;; Customize information for this option is in `cus-edit.el'.
  30. "Hook called after defining each customize option.")
  31. (defvar custom-dont-initialize nil
  32. "Non-nil means `defcustom' should not initialize the variable.
  33. That is used for the sake of `custom-make-dependencies'.
  34. Users should not set it.")
  35. (defvar custom-current-group-alist nil
  36. "Alist of (FILE . GROUP) indicating the current group to use for FILE.")
  37. ;;; The `defcustom' Macro.
  38. (defun custom-initialize-default (symbol value)
  39. "Initialize SYMBOL with VALUE.
  40. This will do nothing if symbol already has a default binding.
  41. Otherwise, if symbol has a `saved-value' property, it will evaluate
  42. the car of that and use it as the default binding for symbol.
  43. Otherwise, VALUE will be evaluated and used as the default binding for
  44. symbol."
  45. (eval `(defvar ,symbol ,(if (get symbol 'saved-value)
  46. (car (get symbol 'saved-value))
  47. value))))
  48. (defun custom-initialize-set (symbol value)
  49. "Initialize SYMBOL based on VALUE.
  50. If the symbol doesn't have a default binding already,
  51. then set it using its `:set' function (or `set-default' if it has none).
  52. The value is either the value in the symbol's `saved-value' property,
  53. if any, or VALUE."
  54. (unless (default-boundp symbol)
  55. (funcall (or (get symbol 'custom-set) 'set-default)
  56. symbol
  57. (eval (if (get symbol 'saved-value)
  58. (car (get symbol 'saved-value))
  59. value)))))
  60. (defun custom-initialize-reset (symbol value)
  61. "Initialize SYMBOL based on VALUE.
  62. Set the symbol, using its `:set' function (or `set-default' if it has none).
  63. The value is either the symbol's current value
  64. \(as obtained using the `:get' function), if any,
  65. or the value in the symbol's `saved-value' property if any,
  66. or (last of all) VALUE."
  67. (funcall (or (get symbol 'custom-set) 'set-default)
  68. symbol
  69. (cond ((default-boundp symbol)
  70. (funcall (or (get symbol 'custom-get) 'default-value)
  71. symbol))
  72. ((get symbol 'saved-value)
  73. (eval (car (get symbol 'saved-value))))
  74. (t
  75. (eval value)))))
  76. (defun custom-initialize-changed (symbol value)
  77. "Initialize SYMBOL with VALUE.
  78. Like `custom-initialize-reset', but only use the `:set' function if
  79. not using the standard setting.
  80. For the standard setting, use `set-default'."
  81. (cond ((default-boundp symbol)
  82. (funcall (or (get symbol 'custom-set) 'set-default)
  83. symbol
  84. (funcall (or (get symbol 'custom-get) 'default-value)
  85. symbol)))
  86. ((get symbol 'saved-value)
  87. (funcall (or (get symbol 'custom-set) 'set-default)
  88. symbol
  89. (eval (car (get symbol 'saved-value)))))
  90. (t
  91. (set-default symbol (eval value)))))
  92. (defvar custom-delayed-init-variables nil
  93. "List of variables whose initialization is pending.")
  94. (defun custom-initialize-delay (symbol _value)
  95. "Delay initialization of SYMBOL to the next Emacs start.
  96. This is used in files that are preloaded (or for autoloaded
  97. variables), so that the initialization is done in the run-time
  98. context rather than the build-time context. This also has the
  99. side-effect that the (delayed) initialization is performed with
  100. the :set function.
  101. For variables in preloaded files, you can simply use this
  102. function for the :initialize property. For autoloaded variables,
  103. you will also need to add an autoload stanza calling this
  104. function, and another one setting the standard-value property."
  105. ;; No longer true:
  106. ;; "See `send-mail-function' in sendmail.el for an example."
  107. ;; Until the var is actually initialized, it is kept unbound.
  108. ;; This seemed to be at least as good as setting it to an arbitrary
  109. ;; value like nil (evaluating `value' is not an option because it
  110. ;; may have undesirable side-effects).
  111. (push symbol custom-delayed-init-variables))
  112. (defun custom-declare-variable (symbol default doc &rest args)
  113. "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
  114. DEFAULT should be an expression to evaluate to compute the default value,
  115. not the default value itself.
  116. DEFAULT is stored as SYMBOL's standard value, in SYMBOL's property
  117. `standard-value'. At the same time, SYMBOL's property `force-value' is
  118. set to nil, as the value is no longer rogue."
  119. (put symbol 'standard-value (purecopy (list default)))
  120. ;; Maybe this option was rogue in an earlier version. It no longer is.
  121. (when (get symbol 'force-value)
  122. (put symbol 'force-value nil))
  123. (if (keywordp doc)
  124. (error "Doc string is missing"))
  125. (let ((initialize 'custom-initialize-reset)
  126. (requests nil))
  127. (unless (memq :group args)
  128. (custom-add-to-group (custom-current-group) symbol 'custom-variable))
  129. (while args
  130. (let ((arg (car args)))
  131. (setq args (cdr args))
  132. (unless (symbolp arg)
  133. (error "Junk in args %S" args))
  134. (let ((keyword arg)
  135. (value (car args)))
  136. (unless args
  137. (error "Keyword %s is missing an argument" keyword))
  138. (setq args (cdr args))
  139. (cond ((eq keyword :initialize)
  140. (setq initialize value))
  141. ((eq keyword :set)
  142. (put symbol 'custom-set value))
  143. ((eq keyword :get)
  144. (put symbol 'custom-get value))
  145. ((eq keyword :require)
  146. (push value requests))
  147. ((eq keyword :risky)
  148. (put symbol 'risky-local-variable value))
  149. ((eq keyword :safe)
  150. (put symbol 'safe-local-variable value))
  151. ((eq keyword :type)
  152. (put symbol 'custom-type (purecopy value)))
  153. ((eq keyword :options)
  154. (if (get symbol 'custom-options)
  155. ;; Slow safe code to avoid duplicates.
  156. (mapc (lambda (option)
  157. (custom-add-option symbol option))
  158. value)
  159. ;; Fast code for the common case.
  160. (put symbol 'custom-options (copy-sequence value))))
  161. (t
  162. (custom-handle-keyword symbol keyword value
  163. 'custom-variable))))))
  164. (put symbol 'custom-requests requests)
  165. ;; Do the actual initialization.
  166. (unless custom-dont-initialize
  167. (funcall initialize symbol default)))
  168. ;; Use defvar to set the docstring as well as the special-variable-p flag.
  169. ;; FIXME: We should reproduce more of `defvar's behavior, such as the warning
  170. ;; when the var is currently let-bound.
  171. (if (not (default-boundp symbol))
  172. ;; Don't use defvar to avoid setting a default-value when undesired.
  173. (when doc (put symbol 'variable-documentation doc))
  174. (eval `(defvar ,symbol nil ,@(when doc (list doc)))))
  175. (push symbol current-load-list)
  176. (run-hooks 'custom-define-hook)
  177. symbol)
  178. (defmacro defcustom (symbol standard doc &rest args)
  179. "Declare SYMBOL as a customizable variable.
  180. SYMBOL is the variable name; it should not be quoted.
  181. STANDARD is an expression specifying the variable's standard
  182. value. It should not be quoted. It is evaluated once by
  183. `defcustom', and the value is assigned to SYMBOL if the variable
  184. is unbound. The expression itself is also stored, so that
  185. Customize can re-evaluate it later to get the standard value.
  186. DOC is the variable documentation.
  187. This macro uses `defvar' as a subroutine, which also marks the
  188. variable as \"special\", so that it is always dynamically bound
  189. even when `lexical-binding' is t.
  190. The remaining arguments to `defcustom' should have the form
  191. [KEYWORD VALUE]...
  192. The following keywords are meaningful:
  193. :type VALUE should be a widget type for editing the symbol's value.
  194. :options VALUE should be a list of valid members of the widget type.
  195. :initialize
  196. VALUE should be a function used to initialize the
  197. variable. It takes two arguments, the symbol and value
  198. given in the `defcustom' call. The default is
  199. `custom-initialize-reset'.
  200. :set VALUE should be a function to set the value of the symbol
  201. when using the Customize user interface.
  202. It takes two arguments, the symbol to set and the value to
  203. give it. The default choice of function is `set-default'.
  204. :get VALUE should be a function to extract the value of symbol.
  205. The function takes one argument, a symbol, and should return
  206. the current value for that symbol. The default choice of function
  207. is `default-value'.
  208. :require
  209. VALUE should be a feature symbol. If you save a value
  210. for this option, then when your `.emacs' file loads the value,
  211. it does (require VALUE) first.
  212. :set-after VARIABLES
  213. Specifies that SYMBOL should be set after the list of variables
  214. VARIABLES when both have been customized.
  215. :risky Set SYMBOL's `risky-local-variable' property to VALUE.
  216. :safe Set SYMBOL's `safe-local-variable' property to VALUE.
  217. See Info node `(elisp) File Local Variables'.
  218. The following common keywords are also meaningful.
  219. :group VALUE should be a customization group.
  220. Add SYMBOL (or FACE with `defface') to that group.
  221. :link LINK-DATA
  222. Include an external link after the documentation string for this
  223. item. This is a sentence containing an active field which
  224. references some other documentation.
  225. There are several alternatives you can use for LINK-DATA:
  226. (custom-manual INFO-NODE)
  227. Link to an Info node; INFO-NODE is a string which specifies
  228. the node name, as in \"(emacs)Top\".
  229. (info-link INFO-NODE)
  230. Like `custom-manual' except that the link appears in the
  231. customization buffer with the Info node name.
  232. (url-link URL)
  233. Link to a web page; URL is a string which specifies the URL.
  234. (emacs-commentary-link LIBRARY)
  235. Link to the commentary section of LIBRARY.
  236. (emacs-library-link LIBRARY)
  237. Link to an Emacs Lisp LIBRARY file.
  238. (file-link FILE)
  239. Link to FILE.
  240. (function-link FUNCTION)
  241. Link to the documentation of FUNCTION.
  242. (variable-link VARIABLE)
  243. Link to the documentation of VARIABLE.
  244. (custom-group-link GROUP)
  245. Link to another customization GROUP.
  246. You can specify the text to use in the customization buffer by
  247. adding `:tag NAME' after the first element of the LINK-DATA; for
  248. example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
  249. Emacs manual which appears in the buffer as `foo'.
  250. An item can have more than one external link; however, most items
  251. have none at all.
  252. :version
  253. VALUE should be a string specifying that the variable was
  254. first introduced, or its default value was changed, in Emacs
  255. version VERSION.
  256. :package-version
  257. VALUE should be a list with the form (PACKAGE . VERSION)
  258. specifying that the variable was first introduced, or its
  259. default value was changed, in PACKAGE version VERSION. This
  260. keyword takes priority over :version. The PACKAGE and VERSION
  261. must appear in the alist `customize-package-emacs-version-alist'.
  262. Since PACKAGE must be unique and the user might see it in an
  263. error message, a good choice is the official name of the
  264. package, such as MH-E or Gnus.
  265. :tag LABEL
  266. Use LABEL, a string, instead of the item's name, to label the item
  267. in customization menus and buffers.
  268. :load FILE
  269. Load file FILE (a string) before displaying this customization
  270. item. Loading is done with `load', and only if the file is
  271. not already loaded.
  272. If SYMBOL has a local binding, then this form affects the local
  273. binding. This is normally not what you want. Thus, if you need
  274. to load a file defining variables with this form, or with
  275. `defvar' or `defconst', you should always load that file
  276. _outside_ any bindings for these variables. \(`defvar' and
  277. `defconst' behave similarly in this respect.)
  278. See Info node `(elisp) Customization' in the Emacs Lisp manual
  279. for more information."
  280. (declare (doc-string 3) (debug (name body)))
  281. ;; It is better not to use backquote in this file,
  282. ;; because that makes a bootstrapping problem
  283. ;; if you need to recompile all the Lisp files using interpreted code.
  284. `(custom-declare-variable
  285. ',symbol
  286. ,(if lexical-binding ;FIXME: This is not reliable, but is all we have.
  287. ;; The STANDARD arg should be an expression that evaluates to
  288. ;; the standard value. The use of `eval' for it is spread
  289. ;; over many different places and hence difficult to
  290. ;; eliminate, yet we want to make sure that the `standard'
  291. ;; expression is checked by the byte-compiler, and that
  292. ;; lexical-binding is obeyed, so quote the expression with
  293. ;; `lambda' rather than with `quote'.
  294. `(list (lambda () ,standard))
  295. `',standard)
  296. ,doc
  297. ,@args))
  298. ;;; The `defface' Macro.
  299. (defmacro defface (face spec doc &rest args)
  300. "Declare FACE as a customizable face that defaults to SPEC.
  301. FACE does not need to be quoted.
  302. Third argument DOC is the face documentation.
  303. If FACE has been set with `custom-set-faces', set the face attributes
  304. as specified by that function, otherwise set the face attributes
  305. according to SPEC.
  306. The remaining arguments should have the form
  307. [KEYWORD VALUE]...
  308. For a list of valid keywords, see the common keywords listed in
  309. `defcustom'.
  310. SPEC should be an alist of the form ((DISPLAY ATTS)...).
  311. In the first element, DISPLAY can be `default'. The ATTS in that
  312. element then act as defaults for all the following elements.
  313. Aside from that, DISPLAY specifies conditions to match some or
  314. all frames. For each frame, the first element of SPEC where the
  315. DISPLAY conditions are satisfied is the one that applies to that
  316. frame. The ATTRs in this element take effect, and the following
  317. elements are ignored, on that frame.
  318. In the last element, DISPLAY can be t. That element applies to a
  319. frame if none of the previous elements (except the `default' if
  320. any) did.
  321. ATTS is a list of face attributes followed by their values:
  322. (ATTR VALUE ATTR VALUE...)
  323. The possible attributes are `:family', `:width', `:height', `:weight',
  324. `:slant', `:underline', `:overline', `:strike-through', `:box',
  325. `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
  326. DISPLAY can be `default' (only in the first element), the symbol
  327. t (only in the last element) to match all frames, or an alist of
  328. conditions of the form \(REQ ITEM...). For such an alist to
  329. match a frame, each of the conditions must be satisfied, meaning
  330. that the REQ property of the frame must match one of the
  331. corresponding ITEMs. These are the defined REQ values:
  332. `type' (the value of `window-system')
  333. Under X, in addition to the values `window-system' can take,
  334. `motif', `lucid', `gtk' and `x-toolkit' are allowed, and match when
  335. the Motif toolkit, Lucid toolkit, GTK toolkit or any X toolkit is in use.
  336. `class' (the frame's color support)
  337. Should be one of `color', `grayscale', or `mono'.
  338. `background' (what color is used for the background text)
  339. Should be one of `light' or `dark'.
  340. `min-colors' (the minimum number of colors the frame should support)
  341. Should be an integer, it is compared with the result of
  342. `display-color-cells'.
  343. `supports' (only match frames that support the specified face attributes)
  344. Should be a list of face attributes. See the documentation for
  345. the function `display-supports-face-attributes-p' for more
  346. information on exactly how testing is done.
  347. See Info node `(elisp) Customization' in the Emacs Lisp manual
  348. for more information."
  349. (declare (doc-string 3))
  350. ;; It is better not to use backquote in this file,
  351. ;; because that makes a bootstrapping problem
  352. ;; if you need to recompile all the Lisp files using interpreted code.
  353. (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
  354. ;;; The `defgroup' Macro.
  355. (defun custom-current-group ()
  356. (cdr (assoc load-file-name custom-current-group-alist)))
  357. (defun custom-declare-group (symbol members doc &rest args)
  358. "Like `defgroup', but SYMBOL is evaluated as a normal argument."
  359. (while members
  360. (apply 'custom-add-to-group symbol (car members))
  361. (setq members (cdr members)))
  362. (when doc
  363. ;; This text doesn't get into DOC.
  364. (put symbol 'group-documentation (purecopy doc)))
  365. (while args
  366. (let ((arg (car args)))
  367. (setq args (cdr args))
  368. (unless (symbolp arg)
  369. (error "Junk in args %S" args))
  370. (let ((keyword arg)
  371. (value (car args)))
  372. (unless args
  373. (error "Keyword %s is missing an argument" keyword))
  374. (setq args (cdr args))
  375. (cond ((eq keyword :prefix)
  376. (put symbol 'custom-prefix (purecopy value)))
  377. (t
  378. (custom-handle-keyword symbol keyword value
  379. 'custom-group))))))
  380. ;; Record the group on the `current' list.
  381. (let ((elt (assoc load-file-name custom-current-group-alist)))
  382. (if elt (setcdr elt symbol)
  383. (push (cons (purecopy load-file-name) symbol)
  384. custom-current-group-alist)))
  385. (run-hooks 'custom-define-hook)
  386. symbol)
  387. (defmacro defgroup (symbol members doc &rest args)
  388. "Declare SYMBOL as a customization group containing MEMBERS.
  389. SYMBOL does not need to be quoted.
  390. Third argument DOC is the group documentation. This should be a short
  391. description of the group, beginning with a capital and ending with
  392. a period. Words other than the first should not be capitalized, if they
  393. are not usually written so.
  394. MEMBERS should be an alist of the form ((NAME WIDGET)...) where
  395. NAME is a symbol and WIDGET is a widget for editing that symbol.
  396. Useful widgets are `custom-variable' for editing variables,
  397. `custom-face' for edit faces, and `custom-group' for editing groups.
  398. The remaining arguments should have the form
  399. [KEYWORD VALUE]...
  400. For a list of valid keywords, see the common keywords listed in
  401. `defcustom'.
  402. See Info node `(elisp) Customization' in the Emacs Lisp manual
  403. for more information."
  404. (declare (doc-string 3))
  405. ;; It is better not to use backquote in this file,
  406. ;; because that makes a bootstrapping problem
  407. ;; if you need to recompile all the Lisp files using interpreted code.
  408. (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
  409. (defun custom-add-to-group (group option widget)
  410. "To existing GROUP add a new OPTION of type WIDGET.
  411. If there already is an entry for OPTION and WIDGET, nothing is done."
  412. (let ((members (get group 'custom-group))
  413. (entry (list option widget)))
  414. (unless (member entry members)
  415. (put group 'custom-group (nconc members (list entry))))))
  416. (defun custom-group-of-mode (mode)
  417. "Return the custom group corresponding to the major or minor MODE.
  418. If no such group is found, return nil."
  419. (or (get mode 'custom-mode-group)
  420. (if (or (get mode 'custom-group)
  421. (and (string-match "-mode\\'" (symbol-name mode))
  422. (get (setq mode (intern (substring (symbol-name mode)
  423. 0 (match-beginning 0))))
  424. 'custom-group)))
  425. mode)))
  426. ;;; Properties.
  427. (defun custom-handle-all-keywords (symbol args type)
  428. "For customization option SYMBOL, handle keyword arguments ARGS.
  429. Third argument TYPE is the custom option type."
  430. (unless (memq :group args)
  431. (custom-add-to-group (custom-current-group) symbol type))
  432. (while args
  433. (let ((arg (car args)))
  434. (setq args (cdr args))
  435. (unless (symbolp arg)
  436. (error "Junk in args %S" args))
  437. (let ((keyword arg)
  438. (value (car args)))
  439. (unless args
  440. (error "Keyword %s is missing an argument" keyword))
  441. (setq args (cdr args))
  442. (custom-handle-keyword symbol keyword value type)))))
  443. (defun custom-handle-keyword (symbol keyword value type)
  444. "For customization option SYMBOL, handle KEYWORD with VALUE.
  445. Fourth argument TYPE is the custom option type."
  446. (if purify-flag
  447. (setq value (purecopy value)))
  448. (cond ((eq keyword :group)
  449. (custom-add-to-group value symbol type))
  450. ((eq keyword :version)
  451. (custom-add-version symbol value))
  452. ((eq keyword :package-version)
  453. (custom-add-package-version symbol value))
  454. ((eq keyword :link)
  455. (custom-add-link symbol value))
  456. ((eq keyword :load)
  457. (custom-add-load symbol value))
  458. ((eq keyword :tag)
  459. (put symbol 'custom-tag value))
  460. ((eq keyword :set-after)
  461. (custom-add-dependencies symbol value))
  462. (t
  463. (error "Unknown keyword %s" keyword))))
  464. (defun custom-add-dependencies (symbol value)
  465. "To the custom option SYMBOL, add dependencies specified by VALUE.
  466. VALUE should be a list of symbols. For each symbol in that list,
  467. this specifies that SYMBOL should be set after the specified symbol, if
  468. both appear in constructs like `custom-set-variables'."
  469. (unless (listp value)
  470. (error "Invalid custom dependency `%s'" value))
  471. (let* ((deps (get symbol 'custom-dependencies))
  472. (new-deps deps))
  473. (while value
  474. (let ((dep (car value)))
  475. (unless (symbolp dep)
  476. (error "Invalid custom dependency `%s'" dep))
  477. (unless (memq dep new-deps)
  478. (setq new-deps (cons dep new-deps)))
  479. (setq value (cdr value))))
  480. (unless (eq deps new-deps)
  481. (put symbol 'custom-dependencies new-deps))))
  482. (defun custom-add-option (symbol option)
  483. "To the variable SYMBOL add OPTION.
  484. If SYMBOL's custom type is a hook, OPTION should be a hook member.
  485. If SYMBOL's custom type is an alist, OPTION specifies a symbol
  486. to offer to the user as a possible key in the alist.
  487. For other custom types, this has no effect."
  488. (let ((options (get symbol 'custom-options)))
  489. (unless (member option options)
  490. (put symbol 'custom-options (cons option options)))))
  491. (defalias 'custom-add-frequent-value 'custom-add-option)
  492. (defun custom-add-link (symbol widget)
  493. "To the custom option SYMBOL add the link WIDGET."
  494. (let ((links (get symbol 'custom-links)))
  495. (unless (member widget links)
  496. (put symbol 'custom-links (cons (purecopy widget) links)))))
  497. (defun custom-add-version (symbol version)
  498. "To the custom option SYMBOL add the version VERSION."
  499. (put symbol 'custom-version (purecopy version)))
  500. (defun custom-add-package-version (symbol version)
  501. "To the custom option SYMBOL add the package version VERSION."
  502. (put symbol 'custom-package-version (purecopy version)))
  503. (defun custom-add-load (symbol load)
  504. "To the custom option SYMBOL add the dependency LOAD.
  505. LOAD should be either a library file name, or a feature name."
  506. (let ((loads (get symbol 'custom-loads)))
  507. (unless (member load loads)
  508. (put symbol 'custom-loads (cons (purecopy load) loads)))))
  509. (defun custom-autoload (symbol load &optional noset)
  510. "Mark SYMBOL as autoloaded custom variable and add dependency LOAD.
  511. If NOSET is non-nil, don't bother autoloading LOAD when setting the variable."
  512. (put symbol 'custom-autoload (if noset 'noset t))
  513. (custom-add-load symbol load))
  514. ;; This test is also in the C code of `user-variable-p'.
  515. (defun custom-variable-p (variable)
  516. "Return non-nil if VARIABLE is a customizable variable.
  517. A customizable variable is either (i) a variable whose property
  518. list contains a non-nil `standard-value' or `custom-autoload'
  519. property, or (ii) an alias for another customizable variable."
  520. (setq variable (indirect-variable variable))
  521. (or (get variable 'standard-value)
  522. (get variable 'custom-autoload)))
  523. (defun custom-note-var-changed (variable)
  524. "Inform Custom that VARIABLE has been set (changed).
  525. VARIABLE is a symbol that names a user option.
  526. The result is that the change is treated as having been made through Custom."
  527. (put variable 'customized-value (list (custom-quote (eval variable)))))
  528. ;;; Custom Themes
  529. ;;; Loading files needed to customize a symbol.
  530. ;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
  531. (defvar custom-load-recursion nil
  532. "Hack to avoid recursive dependencies.")
  533. (defun custom-load-symbol (symbol)
  534. "Load all dependencies for SYMBOL."
  535. (unless custom-load-recursion
  536. (let ((custom-load-recursion t))
  537. ;; Load these files if not already done,
  538. ;; to make sure we know all the dependencies of SYMBOL.
  539. (condition-case nil
  540. (require 'cus-load)
  541. (error nil))
  542. (condition-case nil
  543. (require 'cus-start)
  544. (error nil))
  545. (dolist (load (get symbol 'custom-loads))
  546. (cond ((symbolp load) (condition-case nil (require load) (error nil)))
  547. ;; This is subsumed by the test below, but it's much faster.
  548. ((assoc load load-history))
  549. ;; This was just (assoc (locate-library load) load-history)
  550. ;; but has been optimized not to load locate-library
  551. ;; if not necessary.
  552. ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
  553. "\\(\\'\\|\\.\\)"))
  554. (found nil))
  555. (dolist (loaded load-history)
  556. (and (stringp (car loaded))
  557. (string-match regexp (car loaded))
  558. (setq found t)))
  559. found))
  560. ;; Without this, we would load cus-edit recursively.
  561. ;; We are still loading it when we call this,
  562. ;; and it is not in load-history yet.
  563. ((equal load "cus-edit"))
  564. (t (condition-case nil (load load) (error nil))))))))
  565. (defvar custom-local-buffer nil
  566. "Non-nil, in a Customization buffer, means customize a specific buffer.
  567. If this variable is non-nil, it should be a buffer,
  568. and it means customize the local bindings of that buffer.
  569. This variable is a permanent local, and it normally has a local binding
  570. in every Customization buffer.")
  571. (put 'custom-local-buffer 'permanent-local t)
  572. (defun custom-set-default (variable value)
  573. "Default :set function for a customizable variable.
  574. Normally, this sets the default value of VARIABLE to VALUE,
  575. but if `custom-local-buffer' is non-nil,
  576. this sets the local binding in that buffer instead."
  577. (if custom-local-buffer
  578. (with-current-buffer custom-local-buffer
  579. (set variable value))
  580. (set-default variable value)))
  581. (defun custom-set-minor-mode (variable value)
  582. ":set function for minor mode variables.
  583. Normally, this sets the default value of VARIABLE to nil if VALUE
  584. is nil and to t otherwise,
  585. but if `custom-local-buffer' is non-nil,
  586. this sets the local binding in that buffer instead."
  587. (if custom-local-buffer
  588. (with-current-buffer custom-local-buffer
  589. (funcall variable (if value 1 0)))
  590. (funcall variable (if value 1 0))))
  591. (defun custom-quote (sexp)
  592. "Quote SEXP if it is not self quoting."
  593. (if (or (memq sexp '(t nil))
  594. (keywordp sexp)
  595. (and (listp sexp)
  596. (memq (car sexp) '(lambda)))
  597. (stringp sexp)
  598. (numberp sexp)
  599. (vectorp sexp)
  600. ;;; (and (fboundp 'characterp)
  601. ;;; (characterp sexp))
  602. )
  603. sexp
  604. (list 'quote sexp)))
  605. (defun customize-mark-to-save (symbol)
  606. "Mark SYMBOL for later saving.
  607. If the default value of SYMBOL is different from the standard value,
  608. set the `saved-value' property to a list whose car evaluates to the
  609. default value. Otherwise, set it to nil.
  610. To actually save the value, call `custom-save-all'.
  611. Return non-nil if the `saved-value' property actually changed."
  612. (custom-load-symbol symbol)
  613. (let* ((get (or (get symbol 'custom-get) 'default-value))
  614. (value (funcall get symbol))
  615. (saved (get symbol 'saved-value))
  616. (standard (get symbol 'standard-value))
  617. (comment (get symbol 'customized-variable-comment)))
  618. ;; Save default value if different from standard value.
  619. (if (or (null standard)
  620. (not (equal value (condition-case nil
  621. (eval (car standard))
  622. (error nil)))))
  623. (put symbol 'saved-value (list (custom-quote value)))
  624. (put symbol 'saved-value nil))
  625. ;; Clear customized information (set, but not saved).
  626. (put symbol 'customized-value nil)
  627. ;; Save any comment that might have been set.
  628. (when comment
  629. (put symbol 'saved-variable-comment comment))
  630. (not (equal saved (get symbol 'saved-value)))))
  631. (defun customize-mark-as-set (symbol)
  632. "Mark current value of SYMBOL as being set from customize.
  633. If the default value of SYMBOL is different from the saved value if any,
  634. or else if it is different from the standard value, set the
  635. `customized-value' property to a list whose car evaluates to the
  636. default value. Otherwise, set it to nil.
  637. Return non-nil if the `customized-value' property actually changed."
  638. (custom-load-symbol symbol)
  639. (let* ((get (or (get symbol 'custom-get) 'default-value))
  640. (value (funcall get symbol))
  641. (customized (get symbol 'customized-value))
  642. (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
  643. ;; Mark default value as set if different from old value.
  644. (if (not (and old
  645. (equal value (condition-case nil
  646. (eval (car old))
  647. (error nil)))))
  648. (progn (put symbol 'customized-value (list (custom-quote value)))
  649. (custom-push-theme 'theme-value symbol 'user 'set
  650. (custom-quote value)))
  651. (put symbol 'customized-value nil))
  652. ;; Changed?
  653. (not (equal customized (get symbol 'customized-value)))))
  654. (defun custom-reevaluate-setting (symbol)
  655. "Reset the value of SYMBOL by re-evaluating its saved or standard value.
  656. Use the :set function to do so. This is useful for customizable options
  657. that are defined before their standard value can really be computed.
  658. E.g. dumped variables whose default depends on run-time information."
  659. (funcall (or (get symbol 'custom-set) 'set-default)
  660. symbol
  661. (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value))))))
  662. ;;; Custom Themes
  663. ;; Custom themes are collections of settings that can be enabled or
  664. ;; disabled as a unit.
  665. ;; Each Custom theme is defined by a symbol, called the theme name.
  666. ;; The `theme-settings' property of the theme name records the
  667. ;; variable and face settings of the theme. This property is a list
  668. ;; of elements, each of the form
  669. ;;
  670. ;; (PROP SYMBOL THEME VALUE)
  671. ;;
  672. ;; - PROP is either `theme-value' or `theme-face'
  673. ;; - SYMBOL is the face or variable name
  674. ;; - THEME is the theme name (redundant, but simplifies the code)
  675. ;; - VALUE is an expression that gives the theme's setting for SYMBOL.
  676. ;;
  677. ;; The theme name also has a `theme-feature' property, whose value is
  678. ;; specified when the theme is defined (see `custom-declare-theme').
  679. ;; Usually, this is just a symbol named THEME-theme. This lets
  680. ;; external libraries call (require 'foo-theme).
  681. ;; In addition, each symbol (either a variable or a face) affected by
  682. ;; an *enabled* theme has a `theme-value' or `theme-face' property,
  683. ;; which is a list of elements each of the form
  684. ;;
  685. ;; (THEME VALUE)
  686. ;;
  687. ;; which have the same meanings as in `theme-settings'.
  688. ;;
  689. ;; The `theme-value' and `theme-face' lists are ordered by decreasing
  690. ;; theme precedence. Thus, the first element is always the one that
  691. ;; is in effect.
  692. ;; Each theme is stored in a theme file, with filename THEME-theme.el.
  693. ;; Loading a theme basically involves calling (load "THEME-theme")
  694. ;; This is done by the function `load-theme'. Loading a theme
  695. ;; automatically enables it.
  696. ;;
  697. ;; When a theme is enabled, the `theme-value' and `theme-face'
  698. ;; properties for the affected symbols are set. When a theme is
  699. ;; disabled, its settings are removed from the `theme-value' and
  700. ;; `theme-face' properties, but the theme's own `theme-settings'
  701. ;; property remains unchanged.
  702. (defvar custom-known-themes '(user changed)
  703. "Themes that have been defined with `deftheme'.
  704. The default value is the list (user changed). The theme `changed'
  705. contains the settings before custom themes are applied. The theme
  706. `user' contains all the settings the user customized and saved.
  707. Additional themes declared with the `deftheme' macro will be added
  708. to the front of this list.")
  709. (defsubst custom-theme-p (theme)
  710. "Non-nil when THEME has been defined."
  711. (memq theme custom-known-themes))
  712. (defsubst custom-check-theme (theme)
  713. "Check whether THEME is valid, and signal an error if it is not."
  714. (unless (custom-theme-p theme)
  715. (error "Unknown theme `%s'" theme)))
  716. (defun custom-push-theme (prop symbol theme mode &optional value)
  717. "Record VALUE for face or variable SYMBOL in custom theme THEME.
  718. PROP is `theme-face' for a face, `theme-value' for a variable.
  719. MODE can be either the symbol `set' or the symbol `reset'. If it is the
  720. symbol `set', then VALUE is the value to use. If it is the symbol
  721. `reset', then SYMBOL will be removed from THEME (VALUE is ignored).
  722. See `custom-known-themes' for a list of known themes."
  723. (unless (memq prop '(theme-value theme-face))
  724. (error "Unknown theme property"))
  725. (let* ((old (get symbol prop))
  726. (setting (assq theme old)) ; '(theme value)
  727. (theme-settings ; '(prop symbol theme value)
  728. (get theme 'theme-settings)))
  729. (cond
  730. ;; Remove a setting:
  731. ((eq mode 'reset)
  732. (when setting
  733. (let (res)
  734. (dolist (theme-setting theme-settings)
  735. (if (and (eq (car theme-setting) prop)
  736. (eq (cadr theme-setting) symbol))
  737. (setq res theme-setting)))
  738. (put theme 'theme-settings (delq res theme-settings)))
  739. (put symbol prop (delq setting old))))
  740. ;; Alter an existing setting:
  741. (setting
  742. (let (res)
  743. (dolist (theme-setting theme-settings)
  744. (if (and (eq (car theme-setting) prop)
  745. (eq (cadr theme-setting) symbol))
  746. (setq res theme-setting)))
  747. (put theme 'theme-settings
  748. (cons (list prop symbol theme value)
  749. (delq res theme-settings)))
  750. (setcar (cdr setting) value)))
  751. ;; Add a new setting:
  752. (t
  753. (unless old
  754. ;; If the user changed a variable outside of Customize, save
  755. ;; the value to a fake theme, `changed'. If the theme is
  756. ;; later disabled, we use this to bring back the old value.
  757. ;;
  758. ;; For faces, we just use `face-new-frame-defaults' to
  759. ;; recompute when the theme is disabled.
  760. (when (and (eq prop 'theme-value)
  761. (boundp symbol))
  762. (let ((sv (get symbol 'standard-value))
  763. (val (symbol-value symbol)))
  764. (unless (and sv (equal (eval (car sv)) val))
  765. (setq old `((changed ,(custom-quote val))))))))
  766. (put symbol prop (cons (list theme value) old))
  767. (put theme 'theme-settings
  768. (cons (list prop symbol theme value) theme-settings))))))
  769. (defun custom-fix-face-spec (spec)
  770. "Convert face SPEC, replacing obsolete :bold and :italic attributes.
  771. Also change :reverse-video to :inverse-video."
  772. (when (listp spec)
  773. (if (or (memq :bold spec)
  774. (memq :italic spec)
  775. (memq :inverse-video spec))
  776. (let (result)
  777. (while spec
  778. (let ((key (car spec))
  779. (val (car (cdr spec))))
  780. (cond ((eq key :italic)
  781. (push :slant result)
  782. (push (if val 'italic 'normal) result))
  783. ((eq key :bold)
  784. (push :weight result)
  785. (push (if val 'bold 'normal) result))
  786. ((eq key :reverse-video)
  787. (push :inverse-video result)
  788. (push val result))
  789. (t
  790. (push key result)
  791. (push val result))))
  792. (setq spec (cddr spec)))
  793. (nreverse result))
  794. spec)))
  795. (defun custom-set-variables (&rest args)
  796. "Install user customizations of variable values specified in ARGS.
  797. These settings are registered as theme `user'.
  798. The arguments should each be a list of the form:
  799. (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
  800. This stores EXP (without evaluating it) as the saved value for SYMBOL.
  801. If NOW is present and non-nil, then also evaluate EXP and set
  802. the default value for the SYMBOL to the value of EXP.
  803. REQUEST is a list of features we must require in order to
  804. handle SYMBOL properly.
  805. COMMENT is a comment string about SYMBOL."
  806. (apply 'custom-theme-set-variables 'user args))
  807. (defun custom-theme-set-variables (theme &rest args)
  808. "Initialize variables for theme THEME according to settings in ARGS.
  809. Each of the arguments in ARGS should be a list of this form:
  810. (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
  811. SYMBOL is the variable name, and EXP is an expression which
  812. evaluates to the customized value. EXP will also be stored,
  813. without evaluating it, in SYMBOL's `saved-value' property, so
  814. that it can be restored via the Customize interface. It is also
  815. added to the alist in SYMBOL's `theme-value' property \(by
  816. calling `custom-push-theme').
  817. NOW, if present and non-nil, means to install the variable's
  818. value directly now, even if its `defcustom' declaration has not
  819. been executed. This is for internal use only.
  820. REQUEST is a list of features to `require' (which are loaded
  821. prior to evaluating EXP).
  822. COMMENT is a comment string about SYMBOL."
  823. (custom-check-theme theme)
  824. ;; Process all the needed autoloads before anything else, so that the
  825. ;; subsequent code has all the info it needs (e.g. which var corresponds
  826. ;; to a minor mode), regardless of the ordering of the variables.
  827. (dolist (entry args)
  828. (let* ((symbol (indirect-variable (nth 0 entry))))
  829. (unless (or (get symbol 'standard-value)
  830. (memq (get symbol 'custom-autoload) '(nil noset)))
  831. ;; This symbol needs to be autoloaded, even just for a `set'.
  832. (custom-load-symbol symbol))))
  833. ;; Move minor modes and variables with explicit requires to the end.
  834. (setq args
  835. (sort args
  836. (lambda (a1 a2)
  837. (let* ((sym1 (car a1))
  838. (sym2 (car a2))
  839. (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
  840. (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
  841. (cond ((and 1-then-2 2-then-1)
  842. (error "Circular custom dependency between `%s' and `%s'"
  843. sym1 sym2))
  844. (2-then-1 nil)
  845. ;; 1 is a dependency of 2, so needs to be set first.
  846. (1-then-2)
  847. ;; Put minor modes and symbols with :require last.
  848. ;; Putting minor modes last ensures that the mode
  849. ;; function will see other customized values rather
  850. ;; than default values.
  851. (t (or (nth 3 a2)
  852. (eq (get sym2 'custom-set)
  853. 'custom-set-minor-mode))))))))
  854. (dolist (entry args)
  855. (unless (listp entry)
  856. (error "Incompatible Custom theme spec"))
  857. (let* ((symbol (indirect-variable (nth 0 entry)))
  858. (value (nth 1 entry)))
  859. (custom-push-theme 'theme-value symbol theme 'set value)
  860. (unless custom--inhibit-theme-enable
  861. ;; Now set the variable.
  862. (let* ((now (nth 2 entry))
  863. (requests (nth 3 entry))
  864. (comment (nth 4 entry))
  865. set)
  866. (when requests
  867. (put symbol 'custom-requests requests)
  868. (mapc 'require requests))
  869. (setq set (or (get symbol 'custom-set) 'custom-set-default))
  870. (put symbol 'saved-value (list value))
  871. (put symbol 'saved-variable-comment comment)
  872. ;; Allow for errors in the case where the setter has
  873. ;; changed between versions, say, but let the user know.
  874. (condition-case data
  875. (cond (now
  876. ;; Rogue variable, set it now.
  877. (put symbol 'force-value t)
  878. (funcall set symbol (eval value)))
  879. ((default-boundp symbol)
  880. ;; Something already set this, overwrite it.
  881. (funcall set symbol (eval value))))
  882. (error
  883. (message "Error setting %s: %s" symbol data)))
  884. (and (or now (default-boundp symbol))
  885. (put symbol 'variable-comment comment)))))))
  886. ;;; Defining themes.
  887. ;; A theme file is named `THEME-theme.el' (where THEME is the theme
  888. ;; name) found in `custom-theme-load-path'. It has this format:
  889. ;;
  890. ;; (deftheme THEME
  891. ;; DOCSTRING)
  892. ;;
  893. ;; (custom-theme-set-variables
  894. ;; 'THEME
  895. ;; [THEME-VARIABLES])
  896. ;;
  897. ;; (custom-theme-set-faces
  898. ;; 'THEME
  899. ;; [THEME-FACES])
  900. ;;
  901. ;; (provide-theme 'THEME)
  902. ;; The IGNORED arguments to deftheme come from the XEmacs theme code, where
  903. ;; they were used to supply keyword-value pairs like `:immediate',
  904. ;; `:variable-reset-string', etc. We don't use any of these, so ignore them.
  905. (defmacro deftheme (theme &optional doc &rest ignored)
  906. "Declare THEME to be a Custom theme.
  907. The optional argument DOC is a doc string describing the theme.
  908. Any theme `foo' should be defined in a file called `foo-theme.el';
  909. see `custom-make-theme-feature' for more information."
  910. (let ((feature (custom-make-theme-feature theme)))
  911. ;; It is better not to use backquote in this file,
  912. ;; because that makes a bootstrapping problem
  913. ;; if you need to recompile all the Lisp files using interpreted code.
  914. (list 'custom-declare-theme (list 'quote theme) (list 'quote feature) doc)))
  915. (defun custom-declare-theme (theme feature &optional doc &rest ignored)
  916. "Like `deftheme', but THEME is evaluated as a normal argument.
  917. FEATURE is the feature this theme provides. Normally, this is a symbol
  918. created from THEME by `custom-make-theme-feature'."
  919. (unless (custom-theme-name-valid-p theme)
  920. (error "Custom theme cannot be named %S" theme))
  921. (add-to-list 'custom-known-themes theme)
  922. (put theme 'theme-feature feature)
  923. (when doc (put theme 'theme-documentation doc)))
  924. (defun custom-make-theme-feature (theme)
  925. "Given a symbol THEME, create a new symbol by appending \"-theme\".
  926. Store this symbol in the `theme-feature' property of THEME.
  927. Calling `provide-theme' to provide THEME actually puts `THEME-theme'
  928. into `features'.
  929. This allows for a file-name convention for autoloading themes:
  930. Every theme X has a property `provide-theme' whose value is \"X-theme\".
  931. \(load-theme X) then attempts to load the file `X-theme.el'."
  932. (intern (concat (symbol-name theme) "-theme")))
  933. ;;; Loading themes.
  934. (defcustom custom-theme-directory user-emacs-directory
  935. "Default user directory for storing custom theme files.
  936. The command `customize-create-theme' writes theme files into this
  937. directory. By default, Emacs searches for custom themes in this
  938. directory first---see `custom-theme-load-path'."
  939. :type 'string
  940. :group 'customize
  941. :version "22.1")
  942. (defcustom custom-theme-load-path (list 'custom-theme-directory t)
  943. "List of directories to search for custom theme files.
  944. When loading custom themes (e.g. in `customize-themes' and
  945. `load-theme'), Emacs searches for theme files in the specified
  946. order. Each element in the list should be one of the following:
  947. - the symbol `custom-theme-directory', meaning the value of
  948. `custom-theme-directory'.
  949. - the symbol t, meaning the built-in theme directory (a directory
  950. named \"themes\" in `data-directory').
  951. - a directory name (a string).
  952. Each theme file is named THEME-theme.el, where THEME is the theme
  953. name."
  954. :type '(repeat (choice (const :tag "custom-theme-directory"
  955. custom-theme-directory)
  956. (const :tag "Built-in theme directory" t)
  957. directory))
  958. :group 'customize
  959. :version "24.1")
  960. (defvar custom--inhibit-theme-enable nil
  961. "Whether the custom-theme-set-* functions act immediately.
  962. If nil, `custom-theme-set-variables' and `custom-theme-set-faces'
  963. change the current values of the given variable or face. If
  964. non-nil, they just make a record of the theme settings.")
  965. (defun provide-theme (theme)
  966. "Indicate that this file provides THEME.
  967. This calls `provide' to provide the feature name stored in THEME's
  968. property `theme-feature' (which is usually a symbol created by
  969. `custom-make-theme-feature')."
  970. (unless (custom-theme-name-valid-p theme)
  971. (error "Custom theme cannot be named %S" theme))
  972. (custom-check-theme theme)
  973. (provide (get theme 'theme-feature)))
  974. (defcustom custom-safe-themes '(default)
  975. "Themes that are considered safe to load.
  976. If the value is a list, each element should be either the SHA-256
  977. hash of a safe theme file, or the symbol `default', which stands
  978. for any theme in the built-in Emacs theme directory (a directory
  979. named \"themes\" in `data-directory').
  980. If the value is t, Emacs treats all themes as safe.
  981. This variable cannot be set in a Custom theme."
  982. :type '(choice (repeat :tag "List of safe themes"
  983. (choice string
  984. (const :tag "Built-in themes" default)))
  985. (const :tag "All themes" t))
  986. :group 'customize
  987. :risky t
  988. :version "24.1")
  989. (defun load-theme (theme &optional no-confirm no-enable)
  990. "Load Custom theme named THEME from its file.
  991. The theme file is named THEME-theme.el, in one of the directories
  992. specified by `custom-theme-load-path'.
  993. If the theme is not considered safe by `custom-safe-themes',
  994. prompt the user for confirmation before loading it. But if
  995. optional arg NO-CONFIRM is non-nil, load the theme without
  996. prompting.
  997. Normally, this function also enables THEME. If optional arg
  998. NO-ENABLE is non-nil, load the theme but don't enable it, unless
  999. the theme was already enabled.
  1000. This function is normally called through Customize when setting
  1001. `custom-enabled-themes'. If used directly in your init file, it
  1002. should be called with a non-nil NO-CONFIRM argument, or after
  1003. `custom-safe-themes' has been loaded.
  1004. Return t if THEME was successfully loaded, nil otherwise."
  1005. (interactive
  1006. (list
  1007. (intern (completing-read "Load custom theme: "
  1008. (mapcar 'symbol-name
  1009. (custom-available-themes))))
  1010. nil nil))
  1011. (unless (custom-theme-name-valid-p theme)
  1012. (error "Invalid theme name `%s'" theme))
  1013. ;; If THEME is already enabled, re-enable it after loading, even if
  1014. ;; NO-ENABLE is t.
  1015. (if no-enable
  1016. (setq no-enable (not (custom-theme-enabled-p theme))))
  1017. ;; If reloading, clear out the old theme settings.
  1018. (when (custom-theme-p theme)
  1019. (disable-theme theme)
  1020. (put theme 'theme-settings nil)
  1021. (put theme 'theme-feature nil)
  1022. (put theme 'theme-documentation nil))
  1023. (let ((fn (locate-file (concat (symbol-name theme) "-theme.el")
  1024. (custom-theme--load-path)
  1025. '("" "c")))
  1026. hash)
  1027. (unless fn
  1028. (error "Unable to find theme file for `%s'" theme))
  1029. (with-temp-buffer
  1030. (insert-file-contents fn)
  1031. (setq hash (secure-hash 'sha256 (current-buffer)))
  1032. ;; Check file safety with `custom-safe-themes', prompting the
  1033. ;; user if necessary.
  1034. (when (or no-confirm
  1035. (eq custom-safe-themes t)
  1036. (and (memq 'default custom-safe-themes)
  1037. (equal (file-name-directory fn)
  1038. (expand-file-name "themes/" data-directory)))
  1039. (member hash custom-safe-themes)
  1040. (custom-theme-load-confirm hash))
  1041. (let ((custom--inhibit-theme-enable t))
  1042. (eval-buffer))
  1043. ;; Optimization: if the theme changes the `default' face, put that
  1044. ;; entry first. This avoids some `frame-set-background-mode' rigmarole
  1045. ;; by assigning the new background immediately.
  1046. (let* ((settings (get theme 'theme-settings))
  1047. (tail settings)
  1048. found)
  1049. (while (and tail (not found))
  1050. (and (eq (nth 0 (car tail)) 'theme-face)
  1051. (eq (nth 1 (car tail)) 'default)
  1052. (setq found (car tail)))
  1053. (setq tail (cdr tail)))
  1054. (if found
  1055. (put theme 'theme-settings (cons found (delq found settings)))))
  1056. ;; Finally, enable the theme.
  1057. (unless no-enable
  1058. (enable-theme theme))
  1059. t))))
  1060. (defun custom-theme-load-confirm (hash)
  1061. "Query the user about loading a Custom theme that may not be safe.
  1062. The theme should be in the current buffer. If the user agrees,
  1063. query also about adding HASH to `custom-safe-themes'."
  1064. (if noninteractive
  1065. nil
  1066. (let ((exit-chars '(?y ?n ?\s))
  1067. window prompt char)
  1068. (save-window-excursion
  1069. (rename-buffer "*Custom Theme*" t)
  1070. (emacs-lisp-mode)
  1071. (setq window (display-buffer (current-buffer)))
  1072. (setq prompt
  1073. (format "Loading a theme can run Lisp code. Really load?%s"
  1074. (if (and window
  1075. (< (line-number-at-pos (point-max))
  1076. (window-body-height)))
  1077. " (y or n) "
  1078. (push ?\C-v exit-chars)
  1079. "\nType y or n, or C-v to scroll: ")))
  1080. (goto-char (point-min))
  1081. (while (null char)
  1082. (setq char (read-char-choice prompt exit-chars))
  1083. (when (eq char ?\C-v)
  1084. (if window
  1085. (with-selected-window window
  1086. (condition-case nil
  1087. (scroll-up)
  1088. (error (goto-char (point-min))))))
  1089. (setq char nil)))
  1090. (when (memq char '(?\s ?y))
  1091. ;; Offer to save to `custom-safe-themes'.
  1092. (and (or custom-file user-init-file)
  1093. (y-or-n-p "Treat this theme as safe in future sessions? ")
  1094. (customize-push-and-save 'custom-safe-themes (list hash)))
  1095. t)))))
  1096. (defun custom-theme-name-valid-p (name)
  1097. "Return t if NAME is a valid name for a Custom theme, nil otherwise.
  1098. NAME should be a symbol."
  1099. (and (symbolp name)
  1100. name
  1101. (not (or (zerop (length (symbol-name name)))
  1102. (eq name 'user)
  1103. (eq name 'changed)))))
  1104. (defun custom-available-themes ()
  1105. "Return a list of available Custom themes (symbols)."
  1106. (let (sym themes)
  1107. (dolist (dir (custom-theme--load-path))
  1108. (when (file-directory-p dir)
  1109. (dolist (file (file-expand-wildcards
  1110. (expand-file-name "*-theme.el" dir) t))
  1111. (setq file (file-name-nondirectory file))
  1112. (and (string-match "\\`\\(.+\\)-theme.el\\'" file)
  1113. (setq sym (intern (match-string 1 file)))
  1114. (custom-theme-name-valid-p sym)
  1115. (push sym themes)))))
  1116. (nreverse (delete-dups themes))))
  1117. (defun custom-theme--load-path ()
  1118. (let (lpath)
  1119. (dolist (f custom-theme-load-path)
  1120. (cond ((eq f 'custom-theme-directory)
  1121. (setq f custom-theme-directory))
  1122. ((eq f t)
  1123. (setq f (expand-file-name "themes" data-directory))))
  1124. (if (file-directory-p f)
  1125. (push f lpath)))
  1126. (nreverse lpath)))
  1127. ;;; Enabling and disabling loaded themes.
  1128. (defun enable-theme (theme)
  1129. "Reenable all variable and face settings defined by THEME.
  1130. THEME should be either `user', or a theme loaded via `load-theme'.
  1131. After this function completes, THEME will have the highest
  1132. precedence (after `user')."
  1133. (interactive (list (intern
  1134. (completing-read
  1135. "Enable custom theme: "
  1136. obarray (lambda (sym) (get sym 'theme-settings)) t))))
  1137. (if (not (custom-theme-p theme))
  1138. (error "Undefined Custom theme %s" theme))
  1139. (let ((settings (get theme 'theme-settings)))
  1140. ;; Loop through theme settings, recalculating vars/faces.
  1141. (dolist (s settings)
  1142. (let* ((prop (car s))
  1143. (symbol (cadr s))
  1144. (spec-list (get symbol prop)))
  1145. (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list)))
  1146. (cond
  1147. ((eq prop 'theme-face)
  1148. (custom-theme-recalc-face symbol))
  1149. ((eq prop 'theme-value)
  1150. ;; Ignore `custom-enabled-themes' and `custom-safe-themes'.
  1151. (unless (memq symbol '(custom-enabled-themes custom-safe-themes))
  1152. (custom-theme-recalc-variable symbol)))))))
  1153. (unless (eq theme 'user)
  1154. (setq custom-enabled-themes
  1155. (cons theme (delq theme custom-enabled-themes)))
  1156. ;; Give the `user' theme the highest priority.
  1157. (enable-theme 'user)))
  1158. (defcustom custom-enabled-themes nil
  1159. "List of enabled Custom Themes, highest precedence first.
  1160. This list does not include the `user' theme, which is set by
  1161. Customize and always takes precedence over other Custom Themes.
  1162. This variable cannot be defined inside a Custom theme; there, it
  1163. is simply ignored.
  1164. Setting this variable through Customize calls `enable-theme' or
  1165. `load-theme' for each theme in the list."
  1166. :group 'customize
  1167. :type '(repeat symbol)
  1168. :set-after '(custom-theme-directory custom-theme-load-path
  1169. custom-safe-themes)
  1170. :risky t
  1171. :set (lambda (symbol themes)
  1172. (let (failures)
  1173. (setq themes (delq 'user (delete-dups themes)))
  1174. ;; Disable all themes not in THEMES.
  1175. (if (boundp symbol)
  1176. (dolist (theme (symbol-value symbol))
  1177. (if (not (memq theme themes))
  1178. (disable-theme theme))))
  1179. ;; Call `enable-theme' or `load-theme' on each of THEMES.
  1180. (dolist (theme (reverse themes))
  1181. (condition-case nil
  1182. (if (custom-theme-p theme)
  1183. (enable-theme theme)
  1184. (load-theme theme))
  1185. (error (setq failures (cons theme failures)
  1186. themes (delq theme themes)))))
  1187. (enable-theme 'user)
  1188. (custom-set-default symbol themes)
  1189. (if failures
  1190. (message "Failed to enable theme: %s"
  1191. (mapconcat 'symbol-name failures ", "))))))
  1192. (defsubst custom-theme-enabled-p (theme)
  1193. "Return non-nil if THEME is enabled."
  1194. (memq theme custom-enabled-themes))
  1195. (defun disable-theme (theme)
  1196. "Disable all variable and face settings defined by THEME.
  1197. See `custom-enabled-themes' for a list of enabled themes."
  1198. (interactive (list (intern
  1199. (completing-read
  1200. "Disable custom theme: "
  1201. (mapcar 'symbol-name custom-enabled-themes)
  1202. nil t))))
  1203. (when (custom-theme-enabled-p theme)
  1204. (let ((settings (get theme 'theme-settings)))
  1205. (dolist (s settings)
  1206. (let* ((prop (car s))
  1207. (symbol (cadr s))
  1208. (val (assq-delete-all theme (get symbol prop))))
  1209. (put symbol prop val)
  1210. (cond
  1211. ((eq prop 'theme-value)
  1212. (custom-theme-recalc-variable symbol))
  1213. ((eq prop 'theme-face)
  1214. ;; If the face spec specified by this theme is in the
  1215. ;; saved-face property, reset that property.
  1216. (when (equal (nth 3 s) (get symbol 'saved-face))
  1217. (put symbol 'saved-face (and val (cadr (car val)))))))))
  1218. ;; Recompute faces on all frames.
  1219. (dolist (frame (frame-list))
  1220. ;; We must reset the fg and bg color frame parameters, or
  1221. ;; `face-set-after-frame-default' will use the existing
  1222. ;; parameters, which could be from the disabled theme.
  1223. (set-frame-parameter frame 'background-color
  1224. (custom--frame-color-default
  1225. frame :background "background" "Background"
  1226. "unspecified-bg" "white"))
  1227. (set-frame-parameter frame 'foreground-color
  1228. (custom--frame-color-default
  1229. frame :foreground "foreground" "Foreground"
  1230. "unspecified-fg" "black"))
  1231. (face-set-after-frame-default frame))
  1232. (setq custom-enabled-themes
  1233. (delq theme custom-enabled-themes)))))
  1234. (defun custom--frame-color-default (frame attribute resource-attr resource-class
  1235. tty-default x-default)
  1236. (let ((col (face-attribute 'default attribute t)))
  1237. (cond
  1238. ((and col (not (eq col 'unspecified))) col)
  1239. ((null (window-system frame)) tty-default)
  1240. ((setq col (x-get-resource resource-attr resource-class)) col)
  1241. (t x-default))))
  1242. (defun custom-variable-theme-value (variable)
  1243. "Return (list VALUE) indicating the custom theme value of VARIABLE.
  1244. That is to say, it specifies what the value should be according to
  1245. currently enabled custom themes.
  1246. This function returns nil if no custom theme specifies a value for VARIABLE."
  1247. (let ((theme-value (get variable 'theme-value)))
  1248. (if theme-value
  1249. (cdr (car theme-value)))))
  1250. (defun custom-theme-recalc-variable (variable)
  1251. "Set VARIABLE according to currently enabled custom themes."
  1252. (let ((valspec (custom-variable-theme-value variable)))
  1253. (if valspec
  1254. (put variable 'saved-value valspec)
  1255. (setq valspec (get variable 'standard-value)))
  1256. (if (and valspec
  1257. (or (get variable 'force-value)
  1258. (default-boundp variable)))
  1259. (funcall (or (get variable 'custom-set) 'set-default) variable
  1260. (eval (car valspec))))))
  1261. (defun custom-theme-recalc-face (face)
  1262. "Set FACE according to currently enabled custom themes."
  1263. (if (get face 'face-alias)
  1264. (setq face (get face 'face-alias)))
  1265. ;; Reset the faces for each frame.
  1266. (dolist (frame (frame-list))
  1267. (face-spec-recalc face frame)))
  1268. ;;; XEmacs compatibility functions
  1269. ;; In XEmacs, when you reset a Custom Theme, you have to specify the
  1270. ;; theme to reset it to. We just apply the next available theme, so
  1271. ;; just ignore the IGNORED arguments.
  1272. (defun custom-theme-reset-variables (theme &rest args)
  1273. "Reset some variable settings in THEME to their values in other themes.
  1274. Each of the arguments ARGS has this form:
  1275. (VARIABLE IGNORED)
  1276. This means reset VARIABLE. (The argument IGNORED is ignored)."
  1277. (custom-check-theme theme)
  1278. (dolist (arg args)
  1279. (custom-push-theme 'theme-value (car arg) theme 'reset)))
  1280. (defun custom-reset-variables (&rest args)
  1281. "Reset the specs of some variables to their values in other themes.
  1282. This creates settings in the `user' theme.
  1283. Each of the arguments ARGS has this form:
  1284. (VARIABLE IGNORED)
  1285. This means reset VARIABLE. (The argument IGNORED is ignored)."
  1286. (apply 'custom-theme-reset-variables 'user args))
  1287. ;;; The End.
  1288. ;; Process the defcustoms for variables loaded before this file.
  1289. (while custom-declare-variable-list
  1290. (apply 'custom-declare-variable (car custom-declare-variable-list))
  1291. (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
  1292. (provide 'custom)
  1293. ;;; custom.el ends here