wid-edit.el 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748
  1. ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t; lexical-binding:t -*-
  2. ;;
  3. ;; Copyright (C) 1996-1997, 1999-2017 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
  6. ;; Maintainer: emacs-devel@gnu.org
  7. ;; Keywords: extensions
  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. ;;; Wishlist items (from widget.texi):
  21. ;; * The `menu-choice' tag should be prettier, something like the
  22. ;; abbreviated menus in Open Look.
  23. ;; * Finish `:tab-order'.
  24. ;; * Make indentation work with glyphs and proportional fonts.
  25. ;; * Add commands to show overview of object and class hierarchies to
  26. ;; the browser.
  27. ;; * Find a way to disable mouse highlight for inactive widgets.
  28. ;; * Find a way to make glyphs look inactive.
  29. ;; * Add `key-binding' widget.
  30. ;; * Add `widget' widget for editing widget specifications.
  31. ;; * Find clean way to implement variable length list. See
  32. ;; `TeX-printer-list' for an explanation.
  33. ;; * `C-h' in `widget-prompt-value' should give type specific help.
  34. ;; * A mailto widget. [This should work OK as a url-link if with
  35. ;; browse-url-browser-function' set up appropriately.]
  36. ;;; Commentary:
  37. ;;
  38. ;; See `widget.el'.
  39. ;;; Code:
  40. (require 'cl-lib)
  41. ;;; Compatibility.
  42. (defun widget-event-point (event)
  43. "Character position of the end of event if that exists, or nil."
  44. (posn-point (event-end event)))
  45. (defun widget-button-release-event-p (event)
  46. "Non-nil if EVENT is a mouse-button-release event object."
  47. (and (eventp event)
  48. (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3))
  49. (or (memq 'click (event-modifiers event))
  50. (memq 'drag (event-modifiers event)))))
  51. ;;; Customization.
  52. (defgroup widgets nil
  53. "Customization support for the Widget Library."
  54. :link '(custom-manual "(widget)Top")
  55. :link '(emacs-library-link :tag "Lisp File" "widget.el")
  56. :prefix "widget-"
  57. :group 'extensions)
  58. (defgroup widget-documentation nil
  59. "Options controlling the display of documentation strings."
  60. :group 'widgets)
  61. (defgroup widget-faces nil
  62. "Faces used by the widget library."
  63. :group 'widgets
  64. :group 'faces)
  65. (defvar widget-documentation-face 'widget-documentation
  66. "Face used for documentation strings in widgets.
  67. This exists as a variable so it can be set locally in certain buffers.")
  68. (defface widget-documentation '((((class color)
  69. (background dark))
  70. (:foreground "lime green"))
  71. (((class color)
  72. (background light))
  73. (:foreground "dark green"))
  74. (t nil))
  75. "Face used for documentation text."
  76. :group 'widget-documentation
  77. :group 'widget-faces)
  78. (defvar widget-button-face 'widget-button
  79. "Face used for buttons in widgets.
  80. This exists as a variable so it can be set locally in certain buffers.")
  81. (defface widget-button '((t (:weight bold)))
  82. "Face used for widget buttons."
  83. :group 'widget-faces)
  84. (defcustom widget-mouse-face 'highlight
  85. "Face used for widget buttons when the mouse is above them."
  86. :type 'face
  87. :group 'widget-faces)
  88. ;; TTY gets special definitions here and in the next defface, because
  89. ;; the gray colors defined for other displays cause black text on a black
  90. ;; background, at least on light-background TTYs.
  91. (defface widget-field '((((type tty))
  92. :background "yellow3"
  93. :foreground "black")
  94. (((class grayscale color)
  95. (background light))
  96. :background "gray85")
  97. (((class grayscale color)
  98. (background dark))
  99. :background "dim gray")
  100. (t
  101. :slant italic))
  102. "Face used for editable fields."
  103. :group 'widget-faces)
  104. (defface widget-single-line-field '((((type tty))
  105. :background "green3"
  106. :foreground "black")
  107. (((class grayscale color)
  108. (background light))
  109. :background "gray85")
  110. (((class grayscale color)
  111. (background dark))
  112. :background "dim gray")
  113. (t
  114. :slant italic))
  115. "Face used for editable fields spanning only a single line."
  116. :group 'widget-faces)
  117. ;;; This causes display-table to be loaded, and not usefully.
  118. ;;;(defvar widget-single-line-display-table
  119. ;;; (let ((table (make-display-table)))
  120. ;;; (aset table 9 "^I")
  121. ;;; (aset table 10 "^J")
  122. ;;; table)
  123. ;;; "Display table used for single-line editable fields.")
  124. ;;;(when (fboundp 'set-face-display-table)
  125. ;;; (set-face-display-table 'widget-single-line-field-face
  126. ;;; widget-single-line-display-table))
  127. ;;; Utility functions.
  128. ;;
  129. ;; These are not really widget specific.
  130. (defun widget-princ-to-string (object)
  131. "Return string representation of OBJECT, any Lisp object.
  132. No quoting characters are used; no delimiters are printed around
  133. the contents of strings."
  134. (with-output-to-string
  135. (princ object)))
  136. (defun widget-clear-undo ()
  137. "Clear all undo information."
  138. (buffer-disable-undo (current-buffer))
  139. (buffer-enable-undo))
  140. (defcustom widget-menu-max-size 40
  141. "Largest number of items allowed in a popup-menu.
  142. Larger menus are read through the minibuffer."
  143. :group 'widgets
  144. :type 'integer)
  145. (defcustom widget-menu-max-shortcuts 40
  146. "Largest number of items for which it works to choose one with a character.
  147. For a larger number of items, the minibuffer is used."
  148. :group 'widgets
  149. :type 'integer)
  150. (defcustom widget-menu-minibuffer-flag nil
  151. "Control how to ask for a choice from the keyboard.
  152. Non-nil means use the minibuffer;
  153. nil means read a single character."
  154. :group 'widgets
  155. :type 'boolean)
  156. (defun widget-choose (title items &optional event)
  157. "Choose an item from a list.
  158. First argument TITLE is the name of the list.
  159. Second argument ITEMS is a list whose members are either
  160. (NAME . VALUE), to indicate selectable items, or just strings to
  161. indicate unselectable items.
  162. Optional third argument EVENT is an input event.
  163. The user is asked to choose between each NAME from the items alist,
  164. and the VALUE of the chosen element will be returned. If EVENT is a
  165. mouse event, and the number of elements in items is less than
  166. `widget-menu-max-size', a popup menu will be used, otherwise the
  167. minibuffer."
  168. (cond ((and (< (length items) widget-menu-max-size)
  169. event (display-popup-menus-p))
  170. ;; Mouse click.
  171. (x-popup-menu event
  172. (list title (cons "" items))))
  173. ((or widget-menu-minibuffer-flag
  174. (> (length items) widget-menu-max-shortcuts))
  175. ;; Read the choice of name from the minibuffer.
  176. (setq items (cl-remove-if 'stringp items))
  177. (let ((val (completing-read (concat title ": ") items nil t)))
  178. (if (stringp val)
  179. (let ((try (try-completion val items)))
  180. (when (stringp try)
  181. (setq val try))
  182. (cdr (assoc val items))))))
  183. (t
  184. ;; Construct a menu of the choices
  185. ;; and then use it for prompting for a single character.
  186. (let* ((next-digit ?0)
  187. (map (make-sparse-keymap))
  188. choice some-choice-enabled value)
  189. (with-current-buffer (get-buffer-create " widget-choose")
  190. (erase-buffer)
  191. (insert "Available choices:\n\n")
  192. (while items
  193. (setq choice (pop items))
  194. (when (consp choice)
  195. (let* ((name (substitute-command-keys (car choice)))
  196. (function (cdr choice)))
  197. (insert (format "%c = %s\n" next-digit name))
  198. (define-key map (vector next-digit) function)
  199. (setq some-choice-enabled t)))
  200. ;; Allocate digits to disabled alternatives
  201. ;; so that the digit of a given alternative never varies.
  202. (setq next-digit (1+ next-digit)))
  203. (insert "\nC-g = Quit")
  204. (goto-char (point-min))
  205. (forward-line))
  206. (or some-choice-enabled
  207. (error "None of the choices is currently meaningful"))
  208. (define-key map [?\M-\C-v] 'scroll-other-window)
  209. (define-key map [?\M--] 'negative-argument)
  210. (save-window-excursion
  211. (let ((buf (get-buffer " widget-choose")))
  212. (fit-window-to-buffer (display-buffer buf))
  213. (let ((cursor-in-echo-area t)
  214. (arg 1))
  215. (while (not value)
  216. (setq value (lookup-key map (read-key-sequence (format "%s: " title))))
  217. (unless value
  218. (user-error "Canceled"))
  219. (when
  220. (cond ((eq value 'scroll-other-window)
  221. (let ((minibuffer-scroll-window
  222. (get-buffer-window buf)))
  223. (if (> 0 arg)
  224. (scroll-other-window-down
  225. (window-height minibuffer-scroll-window))
  226. (scroll-other-window))
  227. (setq arg 1)))
  228. ((eq value 'negative-argument)
  229. (setq arg -1)))
  230. (setq value nil))))))
  231. value))))
  232. ;;; Widget text specifications.
  233. ;;
  234. ;; These functions are for specifying text properties.
  235. ;; We can set it to nil now that get_local_map uses get_pos_property.
  236. (defconst widget-field-add-space nil
  237. "Non-nil means add extra space at the end of editable text fields.
  238. If you don't add the space, it will become impossible to edit a zero
  239. size field.")
  240. (defvar widget-field-use-before-change t
  241. "Non-nil means use `before-change-functions' to track editable fields.
  242. This enables the use of undo. Using before hooks also means that
  243. the :notify function can't know the new value.")
  244. (defun widget-specify-field (widget from to)
  245. "Specify editable button for WIDGET between FROM and TO."
  246. ;; Terminating space is not part of the field, but necessary in
  247. ;; order for local-map to work. Remove next sexp if local-map works
  248. ;; at the end of the overlay.
  249. (save-excursion
  250. (goto-char to)
  251. (cond ((null (widget-get widget :size))
  252. (forward-char 1))
  253. (widget-field-add-space
  254. (insert-and-inherit " ")))
  255. (setq to (point)))
  256. (let ((keymap (widget-get widget :keymap))
  257. (face (or (widget-get widget :value-face) 'widget-field))
  258. (help-echo (widget-get widget :help-echo))
  259. (follow-link (widget-get widget :follow-link))
  260. (rear-sticky
  261. (or (not widget-field-add-space) (widget-get widget :size))))
  262. (if (functionp help-echo)
  263. (setq help-echo 'widget-mouse-help))
  264. (when (= (char-before to) ?\n)
  265. ;; When the last character in the field is a newline, we want to
  266. ;; give it a `field' char-property of `boundary', which helps the
  267. ;; C-n/C-p act more naturally when entering/leaving the field. We
  268. ;; do this by making a small secondary overlay to contain just that
  269. ;; one character.
  270. (let ((overlay (make-overlay (1- to) to nil t nil)))
  271. (overlay-put overlay 'field 'boundary)
  272. ;; We need the real field for tabbing.
  273. (overlay-put overlay 'real-field widget)
  274. ;; Use `local-map' here, not `keymap', so that normal editing
  275. ;; works in the field when, say, Custom uses `suppress-keymap'.
  276. (overlay-put overlay 'local-map keymap)
  277. (overlay-put overlay 'face face)
  278. (overlay-put overlay 'follow-link follow-link)
  279. (overlay-put overlay 'help-echo help-echo))
  280. (setq to (1- to))
  281. (setq rear-sticky t))
  282. (let ((overlay (make-overlay from to nil nil rear-sticky)))
  283. (widget-put widget :field-overlay overlay)
  284. ;;(overlay-put overlay 'detachable nil)
  285. (overlay-put overlay 'field widget)
  286. (overlay-put overlay 'local-map keymap)
  287. (overlay-put overlay 'face face)
  288. (overlay-put overlay 'follow-link follow-link)
  289. (overlay-put overlay 'help-echo help-echo)))
  290. (widget-specify-secret widget))
  291. (defun widget-specify-secret (field)
  292. "Replace text in FIELD with value of `:secret', if non-nil."
  293. (let ((secret (widget-get field :secret))
  294. (size (widget-get field :size)))
  295. (when secret
  296. (let ((begin (widget-field-start field))
  297. (end (widget-field-end field)))
  298. (when size
  299. (while (and (> end begin)
  300. (eq (char-after (1- end)) ?\s))
  301. (setq end (1- end))))
  302. (while (< begin end)
  303. (let ((old (char-after begin)))
  304. (unless (eq old secret)
  305. (subst-char-in-region begin (1+ begin) old secret)
  306. (put-text-property begin (1+ begin) 'secret old))
  307. (setq begin (1+ begin))))))))
  308. (defun widget-specify-button (widget from to)
  309. "Specify button for WIDGET between FROM and TO."
  310. (let ((overlay (make-overlay from to nil t nil))
  311. (follow-link (widget-get widget :follow-link))
  312. (help-echo (widget-get widget :help-echo)))
  313. (widget-put widget :button-overlay overlay)
  314. (if (functionp help-echo)
  315. (setq help-echo 'widget-mouse-help))
  316. (overlay-put overlay 'button widget)
  317. (overlay-put overlay 'keymap (widget-get widget :keymap))
  318. (overlay-put overlay 'evaporate t)
  319. ;; We want to avoid the face with image buttons.
  320. (unless (widget-get widget :suppress-face)
  321. (overlay-put overlay 'face (widget-apply widget :button-face-get))
  322. (overlay-put overlay 'mouse-face
  323. ;; Make new list structure for the mouse-face value
  324. ;; so that different widgets will have
  325. ;; different `mouse-face' property values
  326. ;; and will highlight separately.
  327. (let ((mouse-face-value
  328. (widget-apply widget :mouse-face-get)))
  329. ;; If it's a list, copy it.
  330. (if (listp mouse-face-value)
  331. (copy-sequence mouse-face-value)
  332. ;; If it's a symbol, put it in a list.
  333. (list mouse-face-value)))))
  334. (overlay-put overlay 'pointer 'hand)
  335. (overlay-put overlay 'follow-link follow-link)
  336. (overlay-put overlay 'help-echo help-echo)))
  337. (defun widget-mouse-help (_window overlay _point)
  338. "Help-echo callback for widgets whose :help-echo is a function."
  339. (with-current-buffer (overlay-buffer overlay)
  340. (let* ((widget (widget-at (overlay-start overlay)))
  341. (help-echo (if widget (widget-get widget :help-echo))))
  342. (if (functionp help-echo)
  343. (funcall help-echo widget)
  344. help-echo))))
  345. (defun widget-specify-sample (widget from to)
  346. "Specify sample for WIDGET between FROM and TO."
  347. (let ((overlay (make-overlay from to nil t nil)))
  348. (overlay-put overlay 'face (widget-apply widget :sample-face-get))
  349. (overlay-put overlay 'evaporate t)
  350. (widget-put widget :sample-overlay overlay)))
  351. (defun widget-specify-doc (widget from to)
  352. "Specify documentation for WIDGET between FROM and TO."
  353. (let ((overlay (make-overlay from to nil t nil)))
  354. (overlay-put overlay 'widget-doc widget)
  355. (overlay-put overlay 'face widget-documentation-face)
  356. (overlay-put overlay 'evaporate t)
  357. (widget-put widget :doc-overlay overlay)))
  358. (defmacro widget-specify-insert (&rest form)
  359. "Execute FORM without inheriting any text properties."
  360. `(save-restriction
  361. (let ((inhibit-read-only t)
  362. (inhibit-modification-hooks t))
  363. (narrow-to-region (point) (point))
  364. (prog1 (progn ,@form)
  365. (goto-char (point-max))))))
  366. (defface widget-inactive
  367. '((t :inherit shadow))
  368. "Face used for inactive widgets."
  369. :group 'widget-faces)
  370. (defun widget-specify-inactive (widget from to)
  371. "Make WIDGET inactive for user modifications."
  372. (unless (widget-get widget :inactive)
  373. (let ((overlay (make-overlay from to nil t nil)))
  374. (overlay-put overlay 'face 'widget-inactive)
  375. ;; This is disabled, as it makes the mouse cursor change shape.
  376. ;; (overlay-put overlay 'mouse-face 'widget-inactive)
  377. (overlay-put overlay 'evaporate t)
  378. (overlay-put overlay 'priority 100)
  379. (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
  380. (widget-put widget :inactive overlay))))
  381. (defun widget-overlay-inactive (&rest _junk)
  382. "Ignoring the arguments, signal an error."
  383. (unless inhibit-read-only
  384. (error "The widget here is not active")))
  385. (defun widget-specify-active (widget)
  386. "Make WIDGET active for user modifications."
  387. (let ((inactive (widget-get widget :inactive)))
  388. (when inactive
  389. (delete-overlay inactive)
  390. (widget-put widget :inactive nil))))
  391. ;;; Widget Properties.
  392. (defsubst widget-type (widget)
  393. "Return the type of WIDGET. The type is a symbol."
  394. (car widget))
  395. ;;;###autoload
  396. (defun widgetp (widget)
  397. "Return non-nil if WIDGET is a widget."
  398. (if (symbolp widget)
  399. (get widget 'widget-type)
  400. (and (consp widget)
  401. (symbolp (car widget))
  402. (get (car widget) 'widget-type))))
  403. (defun widget-get-indirect (widget property)
  404. "In WIDGET, get the value of PROPERTY.
  405. If the value is a symbol, return its binding.
  406. Otherwise, just return the value."
  407. (let ((value (widget-get widget property)))
  408. (if (symbolp value)
  409. (symbol-value value)
  410. value)))
  411. (defun widget-member (widget property)
  412. "Non-nil if there is a definition in WIDGET for PROPERTY."
  413. (cond ((plist-member (cdr widget) property)
  414. t)
  415. ((car widget)
  416. (widget-member (get (car widget) 'widget-type) property))
  417. (t nil)))
  418. (defun widget-value (widget)
  419. "Extract the current value of WIDGET."
  420. (widget-apply widget
  421. :value-to-external (widget-apply widget :value-get)))
  422. (defun widget-value-set (widget value)
  423. "Set the current value of WIDGET to VALUE."
  424. (widget-apply widget
  425. :value-set (widget-apply widget
  426. :value-to-internal value)))
  427. (defun widget-default-get (widget)
  428. "Extract the default external value of WIDGET."
  429. (widget-apply widget :value-to-external
  430. (or (widget-get widget :value)
  431. (progn
  432. (when (widget-get widget :args)
  433. (setq widget (widget-copy widget))
  434. (let (args)
  435. (dolist (arg (widget-get widget :args))
  436. (setq args (append args
  437. (if (widget-get arg :inline)
  438. (widget-get arg :args)
  439. (list arg)))))
  440. (widget-put widget :args args)))
  441. (widget-apply widget :default-get)))))
  442. (defun widget-match-inline (widget vals)
  443. "In WIDGET, match the start of VALS."
  444. (cond ((widget-get widget :inline)
  445. (widget-apply widget :match-inline vals))
  446. ((and (listp vals)
  447. (widget-apply widget :match (car vals)))
  448. (cons (list (car vals)) (cdr vals)))
  449. (t nil)))
  450. (defun widget-apply-action (widget &optional event)
  451. "Apply :action in WIDGET in response to EVENT."
  452. (if (widget-apply widget :active)
  453. (widget-apply widget :action event)
  454. (error "Attempt to perform action on inactive widget")))
  455. ;;; Helper functions.
  456. ;;
  457. ;; These are widget specific.
  458. ;;;###autoload
  459. (defun widget-prompt-value (widget prompt &optional value unbound)
  460. "Prompt for a value matching WIDGET, using PROMPT.
  461. The current value is assumed to be VALUE, unless UNBOUND is non-nil."
  462. (unless (listp widget)
  463. (setq widget (list widget)))
  464. (setq prompt (format "[%s] %s" (widget-type widget) prompt))
  465. (setq widget (widget-convert widget))
  466. (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
  467. (unless (widget-apply widget :match answer)
  468. (error "Value does not match %S type" (car widget)))
  469. answer))
  470. (defun widget-get-sibling (widget)
  471. "Get the item WIDGET is assumed to toggle.
  472. This is only meaningful for radio buttons or checkboxes in a list."
  473. (let* ((children (widget-get (widget-get widget :parent) :children))
  474. child)
  475. (catch 'child
  476. (while children
  477. (setq child (car children)
  478. children (cdr children))
  479. (when (eq (widget-get child :button) widget)
  480. (throw 'child child)))
  481. nil)))
  482. (defun widget-map-buttons (function &optional buffer maparg)
  483. "Map FUNCTION over the buttons in BUFFER.
  484. FUNCTION is called with the arguments WIDGET and MAPARG.
  485. If FUNCTION returns non-nil, the walk is canceled.
  486. The arguments MAPARG, and BUFFER default to nil and (current-buffer),
  487. respectively."
  488. (let ((cur (point-min))
  489. (widget nil)
  490. (overlays (if buffer
  491. (with-current-buffer buffer (overlay-lists))
  492. (overlay-lists))))
  493. (setq overlays (append (car overlays) (cdr overlays)))
  494. (while (setq cur (pop overlays))
  495. (setq widget (overlay-get cur 'button))
  496. (if (and widget (funcall function widget maparg))
  497. (setq overlays nil)))))
  498. ;;; Images.
  499. (defcustom widget-image-directory (file-name-as-directory
  500. (expand-file-name "images/custom" data-directory))
  501. "Where widget button images are located.
  502. If this variable is nil, widget will try to locate the directory
  503. automatically."
  504. :group 'widgets
  505. :type 'directory)
  506. (defcustom widget-image-enable t
  507. "If non-nil, use image buttons in widgets when available."
  508. :version "21.1"
  509. :group 'widgets
  510. :type 'boolean)
  511. (defcustom widget-image-conversion
  512. '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
  513. (xbm ".xbm"))
  514. "Conversion alist from image formats to file name suffixes."
  515. :group 'widgets
  516. :type '(repeat (cons :format "%v"
  517. (symbol :tag "Image Format" unknown)
  518. (repeat :tag "Suffixes"
  519. (string :format "%v")))))
  520. (defun widget-image-find (image)
  521. "Create a graphical button from IMAGE.
  522. IMAGE should either already be an image, or be a file name sans
  523. extension (xpm, xbm, gif, jpg, or png) located in
  524. `widget-image-directory' or otherwise where `find-image' will find it."
  525. (cond ((not (and image widget-image-enable (display-graphic-p)))
  526. ;; We don't want or can't use images.
  527. nil)
  528. ((and (consp image)
  529. (eq 'image (car image)))
  530. ;; Already an image spec. Use it.
  531. image)
  532. ((stringp image)
  533. ;; A string. Look it up in relevant directories.
  534. (let* ((load-path (cons widget-image-directory load-path))
  535. specs)
  536. (dolist (elt widget-image-conversion)
  537. (dolist (ext (cdr elt))
  538. (push (list :type (car elt) :file (concat image ext))
  539. specs)))
  540. (find-image (nreverse specs))))
  541. (t
  542. ;; Oh well.
  543. nil)))
  544. (defvar widget-button-pressed-face 'widget-button-pressed
  545. "Face used for pressed buttons in widgets.
  546. This exists as a variable so it can be set locally in certain
  547. buffers.")
  548. (defun widget-image-insert (widget tag image &optional _down _inactive)
  549. "In WIDGET, insert the text TAG or, if supported, IMAGE.
  550. IMAGE should either be an image or an image file name sans extension
  551. \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
  552. Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
  553. button is pressed or inactive, respectively. These are currently ignored."
  554. (if (and (featurep 'image)
  555. (setq image (widget-image-find image)))
  556. (progn (widget-put widget :suppress-face t)
  557. (insert-image image tag))
  558. (insert tag)))
  559. (defun widget-move-and-invoke (event)
  560. "Move to where you click, and if it is an active field, invoke it."
  561. (interactive "e")
  562. (mouse-set-point event)
  563. (let ((pos (widget-event-point event)))
  564. (if (and pos (get-char-property pos 'button))
  565. (widget-button-click event))))
  566. ;;; Buttons.
  567. (defgroup widget-button nil
  568. "The look of various kinds of buttons."
  569. :group 'widgets)
  570. (defcustom widget-button-prefix ""
  571. "String used as prefix for buttons."
  572. :type 'string
  573. :group 'widget-button)
  574. (defcustom widget-button-suffix ""
  575. "String used as suffix for buttons."
  576. :type 'string
  577. :group 'widget-button)
  578. ;;; Creating Widgets.
  579. ;;;###autoload
  580. (defun widget-create (type &rest args)
  581. "Create widget of TYPE.
  582. The optional ARGS are additional keyword arguments."
  583. (let ((widget (apply 'widget-convert type args)))
  584. (widget-apply widget :create)
  585. widget))
  586. (defun widget-create-child-and-convert (parent type &rest args)
  587. "As part of the widget PARENT, create a child widget TYPE.
  588. The child is converted, using the keyword arguments ARGS."
  589. (let ((widget (apply 'widget-convert type args)))
  590. (widget-put widget :parent parent)
  591. (unless (widget-get widget :indent)
  592. (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
  593. (or (widget-get widget :extra-offset) 0)
  594. (widget-get parent :offset))))
  595. (widget-apply widget :create)
  596. widget))
  597. (defun widget-create-child (parent type)
  598. "Create widget of TYPE."
  599. (let ((widget (widget-copy type)))
  600. (widget-put widget :parent parent)
  601. (unless (widget-get widget :indent)
  602. (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
  603. (or (widget-get widget :extra-offset) 0)
  604. (widget-get parent :offset))))
  605. (widget-apply widget :create)
  606. widget))
  607. (defun widget-create-child-value (parent type value)
  608. "Create widget of TYPE with value VALUE."
  609. (let ((widget (widget-copy type)))
  610. (widget-put widget :value (widget-apply widget :value-to-internal value))
  611. (widget-put widget :parent parent)
  612. (unless (widget-get widget :indent)
  613. (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
  614. (or (widget-get widget :extra-offset) 0)
  615. (widget-get parent :offset))))
  616. (widget-apply widget :create)
  617. widget))
  618. ;;;###autoload
  619. (defun widget-delete (widget)
  620. "Delete WIDGET."
  621. (widget-apply widget :delete))
  622. (defun widget-copy (widget)
  623. "Make a deep copy of WIDGET."
  624. (widget-apply (copy-sequence widget) :copy))
  625. (defun widget-convert (type &rest args)
  626. "Convert TYPE to a widget without inserting it in the buffer.
  627. The optional ARGS are additional keyword arguments."
  628. ;; Don't touch the type.
  629. (let* ((widget (if (symbolp type)
  630. (list type)
  631. (copy-sequence type)))
  632. (current widget)
  633. done
  634. (keys args))
  635. ;; First set the :args keyword.
  636. (while (cdr current) ;Look in the type.
  637. (if (and (keywordp (cadr current))
  638. ;; If the last element is a keyword,
  639. ;; it is still the :args element,
  640. ;; even though it is a keyword.
  641. (cddr current))
  642. (if (eq (cadr current) :args)
  643. ;; If :args is explicitly specified, obey it.
  644. (setq current nil)
  645. ;; Some other irrelevant keyword.
  646. (setq current (cdr (cdr current))))
  647. (setcdr current (list :args (cdr current)))
  648. (setq current nil)))
  649. (while (and args (not done)) ;Look in ARGS.
  650. (cond ((eq (car args) :args)
  651. ;; Handle explicit specification of :args.
  652. (setq args (cadr args)
  653. done t))
  654. ((keywordp (car args))
  655. (setq args (cddr args)))
  656. (t (setq done t))))
  657. (when done
  658. (widget-put widget :args args))
  659. ;; Then Convert the widget.
  660. (setq type widget)
  661. (while type
  662. (let ((convert-widget (plist-get (cdr type) :convert-widget)))
  663. (if convert-widget
  664. (setq widget (funcall convert-widget widget))))
  665. (setq type (get (car type) 'widget-type)))
  666. ;; Finally set the keyword args.
  667. (while keys
  668. (let ((next (nth 0 keys)))
  669. (if (keywordp next)
  670. (progn
  671. (widget-put widget next (nth 1 keys))
  672. (setq keys (nthcdr 2 keys)))
  673. (setq keys nil))))
  674. ;; Convert the :value to internal format.
  675. (if (widget-member widget :value)
  676. (widget-put widget
  677. :value (widget-apply widget
  678. :value-to-internal
  679. (widget-get widget :value))))
  680. ;; Return the newly create widget.
  681. widget))
  682. ;;;###autoload
  683. (defun widget-insert (&rest args)
  684. "Call `insert' with ARGS even if surrounding text is read only."
  685. (let ((inhibit-read-only t)
  686. (inhibit-modification-hooks t))
  687. (apply 'insert args)))
  688. (defun widget-convert-text (type from to
  689. &optional button-from button-to
  690. &rest args)
  691. "Return a widget of type TYPE with endpoint FROM TO.
  692. No text will be inserted to the buffer, instead the text between FROM
  693. and TO will be used as the widgets end points. If optional arguments
  694. BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
  695. button end points.
  696. Optional ARGS are extra keyword arguments for TYPE."
  697. (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
  698. (from (copy-marker from))
  699. (to (copy-marker to)))
  700. (set-marker-insertion-type from t)
  701. (set-marker-insertion-type to nil)
  702. (widget-put widget :from from)
  703. (widget-put widget :to to)
  704. (when button-from
  705. (widget-specify-button widget button-from button-to))
  706. widget))
  707. (defun widget-convert-button (type from to &rest args)
  708. "Return a widget of type TYPE with endpoint FROM TO.
  709. Optional ARGS are extra keyword arguments for TYPE.
  710. No text will be inserted to the buffer, instead the text between FROM
  711. and TO will be used as the widgets end points, as well as the widgets
  712. button end points."
  713. (apply 'widget-convert-text type from to from to args))
  714. (defun widget-leave-text (widget)
  715. "Remove markers and overlays from WIDGET and its children."
  716. (let ((button (widget-get widget :button-overlay))
  717. (sample (widget-get widget :sample-overlay))
  718. (doc (widget-get widget :doc-overlay))
  719. (field (widget-get widget :field-overlay)))
  720. (set-marker (widget-get widget :from) nil)
  721. (set-marker (widget-get widget :to) nil)
  722. (when button
  723. (delete-overlay button))
  724. (when sample
  725. (delete-overlay sample))
  726. (when doc
  727. (delete-overlay doc))
  728. (when field
  729. (delete-overlay field))
  730. (mapc 'widget-leave-text (widget-get widget :children))))
  731. ;;; Keymap and Commands.
  732. ;; This alias exists only so that one can choose in doc-strings (e.g.
  733. ;; Custom-mode) which key-binding of widget-keymap one wants to refer to.
  734. ;; http://lists.gnu.org/archive/html/emacs-devel/2008-11/msg00480.html
  735. (define-obsolete-function-alias 'advertised-widget-backward
  736. 'widget-backward "23.2")
  737. ;;;###autoload
  738. (defvar widget-keymap
  739. (let ((map (make-sparse-keymap)))
  740. (define-key map "\t" 'widget-forward)
  741. (define-key map "\e\t" 'widget-backward)
  742. (define-key map [(shift tab)] 'widget-backward)
  743. (put 'widget-backward :advertised-binding [(shift tab)])
  744. (define-key map [backtab] 'widget-backward)
  745. (define-key map [down-mouse-2] 'widget-button-click)
  746. (define-key map [down-mouse-1] 'widget-button-click)
  747. ;; The following definition needs to avoid using escape sequences that
  748. ;; might get converted to ^M when building loaddefs.el
  749. (define-key map [(control ?m)] 'widget-button-press)
  750. map)
  751. "Keymap containing useful binding for buffers containing widgets.
  752. Recommended as a parent keymap for modes using widgets.
  753. Note that such modes will need to require wid-edit.")
  754. (defvar widget-global-map global-map
  755. "Keymap used for events a widget does not handle itself.")
  756. (make-variable-buffer-local 'widget-global-map)
  757. (defvar widget-field-keymap
  758. (let ((map (copy-keymap widget-keymap)))
  759. (define-key map "\C-k" 'widget-kill-line)
  760. (define-key map "\M-\t" 'widget-complete)
  761. (define-key map "\C-m" 'widget-field-activate)
  762. ;; Since the widget code uses a `field' property to identify fields,
  763. ;; ordinary beginning-of-line does the right thing.
  764. ;; (define-key map "\C-a" 'widget-beginning-of-line)
  765. (define-key map "\C-e" 'widget-end-of-line)
  766. map)
  767. "Keymap used inside an editable field.")
  768. (defvar widget-text-keymap
  769. (let ((map (copy-keymap widget-keymap)))
  770. ;; Since the widget code uses a `field' property to identify fields,
  771. ;; ordinary beginning-of-line does the right thing.
  772. ;; (define-key map "\C-a" 'widget-beginning-of-line)
  773. (define-key map "\C-e" 'widget-end-of-line)
  774. map)
  775. "Keymap used inside a text field.")
  776. (defun widget-field-activate (pos &optional event)
  777. "Invoke the editable field at point."
  778. (interactive "@d")
  779. (let ((field (widget-field-at pos)))
  780. (if field
  781. (widget-apply-action field event)
  782. (call-interactively
  783. (lookup-key widget-global-map (this-command-keys))))))
  784. (defface widget-button-pressed
  785. '((((min-colors 88) (class color))
  786. (:foreground "red1"))
  787. (((class color))
  788. (:foreground "red"))
  789. (t
  790. (:weight bold :underline t)))
  791. "Face used for pressed buttons."
  792. :group 'widget-faces)
  793. (defvar widget-button-click-moves-point nil
  794. "If non-nil, `widget-button-click' moves point to a button after invoking it.
  795. If nil, point returns to its original position after invoking a button.")
  796. (defun widget-button-click (event)
  797. "Invoke the button that the mouse is pointing at."
  798. (interactive "e")
  799. (if (widget-event-point event)
  800. (let* ((oevent event)
  801. (mouse-1 (memq (event-basic-type event) '(mouse-1 down-mouse-1)))
  802. (pos (widget-event-point event))
  803. (start (event-start event))
  804. (button (get-char-property
  805. pos 'button (and (windowp (posn-window start))
  806. (window-buffer (posn-window start)))))
  807. newpoint)
  808. (when (or (null button)
  809. (catch 'button-press-cancelled
  810. ;; Mouse click on a widget button. Do the following
  811. ;; in a save-excursion so that the click on the button
  812. ;; doesn't change point.
  813. (save-selected-window
  814. (select-window (posn-window (event-start event)))
  815. (save-excursion
  816. (goto-char (posn-point (event-start event)))
  817. (let* ((overlay (widget-get button :button-overlay))
  818. (pressed-face (or (widget-get button :pressed-face)
  819. widget-button-pressed-face))
  820. (face (overlay-get overlay 'face))
  821. (mouse-face (overlay-get overlay 'mouse-face)))
  822. (unwind-protect
  823. ;; Read events, including mouse-movement
  824. ;; events, waiting for a release event. If we
  825. ;; began with a mouse-1 event and receive a
  826. ;; movement event, that means the user wants
  827. ;; to perform drag-selection, so cancel the
  828. ;; button press and do the default mouse-1
  829. ;; action. For mouse-2, just highlight/
  830. ;; unhighlight the button the mouse was
  831. ;; initially on when we move over it.
  832. (save-excursion
  833. (when face ; avoid changing around image
  834. (overlay-put overlay 'face pressed-face)
  835. (overlay-put overlay 'mouse-face pressed-face))
  836. (unless (widget-apply button :mouse-down-action event)
  837. (let ((track-mouse t))
  838. (while (not (widget-button-release-event-p event))
  839. (setq event (read-event))
  840. (when (and mouse-1 (mouse-movement-p event))
  841. (push event unread-command-events)
  842. (setq event oevent)
  843. (throw 'button-press-cancelled t))
  844. (unless (or (integerp event)
  845. (memq (car event) '(switch-frame select-window))
  846. (eq (car event) 'scroll-bar-movement))
  847. (setq pos (widget-event-point event))
  848. (if (and pos
  849. (eq (get-char-property pos 'button)
  850. button))
  851. (when face
  852. (overlay-put overlay 'face pressed-face)
  853. (overlay-put overlay 'mouse-face pressed-face))
  854. (overlay-put overlay 'face face)
  855. (overlay-put overlay 'mouse-face mouse-face))))))
  856. ;; When mouse is released over the button, run
  857. ;; its action function.
  858. (when (and pos (eq (get-char-property pos 'button) button))
  859. (goto-char pos)
  860. (widget-apply-action button event)
  861. (if widget-button-click-moves-point
  862. (setq newpoint (point)))))
  863. (overlay-put overlay 'face face)
  864. (overlay-put overlay 'mouse-face mouse-face))))
  865. (if newpoint (goto-char newpoint))
  866. ;; This loses if the widget action switches windows. -- cyd
  867. ;; (unless (pos-visible-in-window-p (widget-event-point event))
  868. ;; (mouse-set-point event)
  869. ;; (beginning-of-line)
  870. ;; (recenter))
  871. )
  872. nil))
  873. (let ((up t) command)
  874. ;; Mouse click not on a widget button. Find the global
  875. ;; command to run, and check whether it is bound to an
  876. ;; up event.
  877. (if mouse-1
  878. (cond ((setq command ;down event
  879. (lookup-key widget-global-map [down-mouse-1]))
  880. (setq up nil))
  881. ((setq command ;up event
  882. (lookup-key widget-global-map [mouse-1]))))
  883. (cond ((setq command ;down event
  884. (lookup-key widget-global-map [down-mouse-2]))
  885. (setq up nil))
  886. ((setq command ;up event
  887. (lookup-key widget-global-map [mouse-2])))))
  888. (when up
  889. ;; Don't execute up events twice.
  890. (while (not (widget-button-release-event-p event))
  891. (setq event (read-event))))
  892. (when command
  893. (call-interactively command)))))
  894. (message "You clicked somewhere weird.")))
  895. (defun widget-button-press (pos &optional event)
  896. "Invoke button at POS."
  897. (interactive "@d")
  898. (let ((button (get-char-property pos 'button)))
  899. (if button
  900. (widget-apply-action button event)
  901. (let ((command (lookup-key widget-global-map (this-command-keys))))
  902. (when (commandp command)
  903. (call-interactively command))))))
  904. (defun widget-tabable-at (&optional pos)
  905. "Return the tabable widget at POS, or nil.
  906. POS defaults to the value of (point)."
  907. (let ((widget (widget-at pos)))
  908. (if widget
  909. (let ((order (widget-get widget :tab-order)))
  910. (if order
  911. (if (>= order 0)
  912. widget)
  913. widget)))))
  914. (defvar widget-use-overlay-change t
  915. "If non-nil, use overlay change functions to tab around in the buffer.
  916. This is much faster.")
  917. (defun widget-move (arg)
  918. "Move point to the ARG next field or button.
  919. ARG may be negative to move backward."
  920. (or (bobp) (> arg 0) (backward-char))
  921. (let ((wrapped 0)
  922. (number arg)
  923. (old (widget-tabable-at)))
  924. ;; Forward.
  925. (while (> arg 0)
  926. (cond ((eobp)
  927. (goto-char (point-min))
  928. (setq wrapped (1+ wrapped)))
  929. (widget-use-overlay-change
  930. (goto-char (next-overlay-change (point))))
  931. (t
  932. (forward-char 1)))
  933. (and (= wrapped 2)
  934. (eq arg number)
  935. (error "No buttons or fields found"))
  936. (let ((new (widget-tabable-at)))
  937. (when new
  938. (unless (eq new old)
  939. (setq arg (1- arg))
  940. (setq old new)))))
  941. ;; Backward.
  942. (while (< arg 0)
  943. (cond ((bobp)
  944. (goto-char (point-max))
  945. (setq wrapped (1+ wrapped)))
  946. (widget-use-overlay-change
  947. (goto-char (previous-overlay-change (point))))
  948. (t
  949. (backward-char 1)))
  950. (and (= wrapped 2)
  951. (eq arg number)
  952. (error "No buttons or fields found"))
  953. (let ((new (widget-tabable-at)))
  954. (when new
  955. (unless (eq new old)
  956. (setq arg (1+ arg))))))
  957. (let ((new (widget-tabable-at)))
  958. (while (eq (widget-tabable-at) new)
  959. (backward-char)))
  960. (forward-char))
  961. (widget-echo-help (point))
  962. (run-hooks 'widget-move-hook))
  963. (defun widget-forward (arg)
  964. "Move point to the next field or button.
  965. With optional ARG, move across that many fields."
  966. (interactive "p")
  967. (run-hooks 'widget-forward-hook)
  968. (widget-move arg))
  969. (defun widget-backward (arg)
  970. "Move point to the previous field or button.
  971. With optional ARG, move across that many fields."
  972. (interactive "p")
  973. (run-hooks 'widget-backward-hook)
  974. (widget-move (- arg)))
  975. ;; Since the widget code uses a `field' property to identify fields,
  976. ;; ordinary beginning-of-line does the right thing.
  977. (defalias 'widget-beginning-of-line 'beginning-of-line)
  978. (defun widget-end-of-line ()
  979. "Go to end of field or end of line, whichever is first.
  980. Trailing spaces at the end of padded fields are not considered part of
  981. the field."
  982. (interactive)
  983. ;; Ordinary end-of-line does the right thing, because we're inside
  984. ;; text with a `field' property.
  985. (end-of-line)
  986. (unless (eolp)
  987. ;; ... except that we want to ignore trailing spaces in fields that
  988. ;; aren't terminated by a newline, because they are used as padding,
  989. ;; and ignored when extracting the entered value of the field.
  990. (skip-chars-backward " " (field-beginning (1- (point))))))
  991. (defun widget-kill-line ()
  992. "Kill to end of field or end of line, whichever is first."
  993. (interactive)
  994. (let* ((field (widget-field-find (point)))
  995. (end (and field (widget-field-end field))))
  996. (if (and field (> (line-beginning-position 2) end))
  997. (kill-region (point) end)
  998. (call-interactively 'kill-line))))
  999. (defun widget-narrow-to-field ()
  1000. "Narrow to field."
  1001. (interactive)
  1002. (let ((field (widget-field-find (point))))
  1003. (if field
  1004. (narrow-to-region (line-beginning-position) (line-end-position)))))
  1005. ;; This used to say:
  1006. ;; "When not inside a field, move to the previous button or field."
  1007. ;; but AFAICS, it has always just thrown an error.
  1008. (defun widget-complete ()
  1009. "Complete content of editable field from point.
  1010. When not inside a field, signal an error."
  1011. (interactive)
  1012. (let ((data (widget-completions-at-point)))
  1013. (cond
  1014. ((functionp data) (funcall data))
  1015. ((consp data)
  1016. (let ((completion-extra-properties (nth 3 data)))
  1017. (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)
  1018. (plist-get completion-extra-properties
  1019. :predicate))))
  1020. (t
  1021. (error "Not in an editable field")))))
  1022. ;; We may want to use widget completion in buffers where the major mode
  1023. ;; hasn't added widget-completions-at-point to completion-at-point-functions,
  1024. ;; so it's not really obsolete (yet).
  1025. ;; (make-obsolete 'widget-complete 'completion-at-point "24.1")
  1026. (defun widget-completions-at-point ()
  1027. (let ((field (widget-field-find (point))))
  1028. (when field
  1029. (widget-apply field :completions-function))))
  1030. ;;; Setting up the buffer.
  1031. (defvar widget-field-new nil
  1032. "List of all newly created editable fields in the buffer.")
  1033. (make-variable-buffer-local 'widget-field-new)
  1034. (defvar widget-field-list nil
  1035. "List of all editable fields in the buffer.")
  1036. (make-variable-buffer-local 'widget-field-list)
  1037. (defun widget-at (&optional pos)
  1038. "The button or field at POS (default, point)."
  1039. (or (get-char-property (or pos (point)) 'button)
  1040. (widget-field-at pos)))
  1041. ;;;###autoload
  1042. (defun widget-setup ()
  1043. "Setup current buffer so editing string widgets works."
  1044. (let ((inhibit-read-only t)
  1045. (inhibit-modification-hooks t)
  1046. field)
  1047. (while widget-field-new
  1048. (setq field (car widget-field-new)
  1049. widget-field-new (cdr widget-field-new)
  1050. widget-field-list (cons field widget-field-list))
  1051. (let ((from (car (widget-get field :field-overlay)))
  1052. (to (cdr (widget-get field :field-overlay))))
  1053. (widget-specify-field field
  1054. (marker-position from) (marker-position to))
  1055. (set-marker from nil)
  1056. (set-marker to nil))))
  1057. (widget-clear-undo)
  1058. (widget-add-change))
  1059. (defvar widget-field-last nil)
  1060. ;; Last field containing point.
  1061. (make-variable-buffer-local 'widget-field-last)
  1062. (defvar widget-field-was nil)
  1063. ;; The widget data before the change.
  1064. (make-variable-buffer-local 'widget-field-was)
  1065. (defun widget-field-at (pos)
  1066. "Return the widget field at POS, or nil if none."
  1067. (let ((field (get-char-property (or pos (point)) 'field)))
  1068. (if (eq field 'boundary)
  1069. (get-char-property (or pos (point)) 'real-field)
  1070. field)))
  1071. (defun widget-field-buffer (widget)
  1072. "Return the buffer of WIDGET's editing field."
  1073. (let ((overlay (widget-get widget :field-overlay)))
  1074. (cond ((overlayp overlay)
  1075. (overlay-buffer overlay))
  1076. ((consp overlay)
  1077. (marker-buffer (car overlay))))))
  1078. (defun widget-field-start (widget)
  1079. "Return the start of WIDGET's editing field."
  1080. (let ((overlay (widget-get widget :field-overlay)))
  1081. (if (overlayp overlay)
  1082. (overlay-start overlay)
  1083. (car overlay))))
  1084. (defun widget-field-end (widget)
  1085. "Return the end of WIDGET's editing field."
  1086. (let ((overlay (widget-get widget :field-overlay)))
  1087. ;; Don't subtract one if local-map works at the end of the overlay,
  1088. ;; or if a special `boundary' field has been added after the widget
  1089. ;; field.
  1090. (if (overlayp overlay)
  1091. ;; Don't proceed if overlay has been removed from buffer.
  1092. (when (overlay-buffer overlay)
  1093. (if (and (not (eq (with-current-buffer
  1094. (widget-field-buffer widget)
  1095. (save-restriction
  1096. ;; `widget-narrow-to-field' can be
  1097. ;; active when this function is called
  1098. ;; from an change-functions hook. So
  1099. ;; temporarily remove field narrowing
  1100. ;; before to call `get-char-property'.
  1101. (widen)
  1102. (get-char-property (overlay-end overlay)
  1103. 'field)))
  1104. 'boundary))
  1105. (or widget-field-add-space
  1106. (null (widget-get widget :size))))
  1107. (1- (overlay-end overlay))
  1108. (overlay-end overlay)))
  1109. (cdr overlay))))
  1110. (defun widget-field-text-end (widget)
  1111. (let ((to (widget-field-end widget))
  1112. (size (widget-get widget :size)))
  1113. (if (or (null size) (zerop size))
  1114. to
  1115. (let ((from (widget-field-start widget)))
  1116. (if (and from to)
  1117. (with-current-buffer (widget-field-buffer widget)
  1118. (while (and (> to from)
  1119. (eq (char-after (1- to)) ?\s))
  1120. (setq to (1- to)))
  1121. to))))))
  1122. (defun widget-field-find (pos)
  1123. "Return the field at POS.
  1124. Unlike (get-char-property POS \\='field), this works with empty fields too."
  1125. (let ((fields widget-field-list)
  1126. field found)
  1127. (while fields
  1128. (setq field (car fields)
  1129. fields (cdr fields))
  1130. (when (and (<= (widget-field-start field) pos)
  1131. (<= pos (widget-field-end field)))
  1132. (when found
  1133. (error "Overlapping fields"))
  1134. (setq found field)))
  1135. found))
  1136. (defun widget-before-change (from to)
  1137. ;; This is how, for example, a variable changes its state to `modified'.
  1138. ;; when it is being edited.
  1139. (unless inhibit-read-only
  1140. (let ((from-field (widget-field-find from))
  1141. (to-field (widget-field-find to)))
  1142. (cond ((not (eq from-field to-field))
  1143. (add-hook 'post-command-hook 'widget-add-change nil t)
  1144. (signal 'text-read-only
  1145. '("Change should be restricted to a single field")))
  1146. ((null from-field)
  1147. (add-hook 'post-command-hook 'widget-add-change nil t)
  1148. (signal 'text-read-only
  1149. '("Attempt to change text outside editable field")))
  1150. (widget-field-use-before-change
  1151. (widget-apply from-field :notify from-field))))))
  1152. (defun widget-add-change ()
  1153. (remove-hook 'post-command-hook 'widget-add-change t)
  1154. (add-hook 'before-change-functions 'widget-before-change nil t)
  1155. (add-hook 'after-change-functions 'widget-after-change nil t))
  1156. (defun widget-after-change (from to _old)
  1157. "Adjust field size and text properties."
  1158. (let ((field (widget-field-find from))
  1159. (other (widget-field-find to)))
  1160. (when field
  1161. (unless (eq field other)
  1162. (error "Change in different fields"))
  1163. (let ((size (widget-get field :size)))
  1164. (when size
  1165. (let ((begin (widget-field-start field))
  1166. (end (widget-field-end field)))
  1167. (cond ((< (- end begin) size)
  1168. ;; Field too small.
  1169. (save-excursion
  1170. (goto-char end)
  1171. (insert-char ?\s (- (+ begin size) end))))
  1172. ((> (- end begin) size)
  1173. ;; Field too large and
  1174. (if (or (< (point) (+ begin size))
  1175. (> (point) end))
  1176. ;; Point is outside extra space.
  1177. (setq begin (+ begin size))
  1178. ;; Point is within the extra space.
  1179. (setq begin (point)))
  1180. (save-excursion
  1181. (goto-char end)
  1182. (while (and (eq (preceding-char) ?\s)
  1183. (> (point) begin))
  1184. (delete-char -1)))))))
  1185. (widget-specify-secret field))
  1186. (widget-apply field :notify field))))
  1187. ;;; Widget Functions
  1188. ;;
  1189. ;; These functions are used in the definition of multiple widgets.
  1190. (defun widget-parent-action (widget &optional event)
  1191. "Tell :parent of WIDGET to handle the :action.
  1192. Optional EVENT is the event that triggered the action."
  1193. (widget-apply (widget-get widget :parent) :action event))
  1194. (defun widget-children-value-delete (widget)
  1195. "Delete all :children and :buttons in WIDGET."
  1196. (mapc 'widget-delete (widget-get widget :children))
  1197. (widget-put widget :children nil)
  1198. (mapc 'widget-delete (widget-get widget :buttons))
  1199. (widget-put widget :buttons nil))
  1200. (defun widget-children-validate (widget)
  1201. "All the :children must be valid."
  1202. (let ((children (widget-get widget :children))
  1203. child found)
  1204. (while (and children (not found))
  1205. (setq child (car children)
  1206. children (cdr children)
  1207. found (widget-apply child :validate)))
  1208. found))
  1209. (defun widget-child-value-get (widget)
  1210. "Get the value of the first member of :children in WIDGET."
  1211. (widget-value (car (widget-get widget :children))))
  1212. (defun widget-child-value-inline (widget)
  1213. "Get the inline value of the first member of :children in WIDGET."
  1214. (widget-apply (car (widget-get widget :children)) :value-inline))
  1215. (defun widget-child-validate (widget)
  1216. "The result of validating the first member of :children in WIDGET."
  1217. (widget-apply (car (widget-get widget :children)) :validate))
  1218. (defun widget-type-value-create (widget)
  1219. "Convert and instantiate the value of the :type attribute of WIDGET.
  1220. Store the newly created widget in the :children attribute.
  1221. The value of the :type attribute should be an unconverted widget type."
  1222. (let ((value (widget-get widget :value))
  1223. (type (widget-get widget :type)))
  1224. (widget-put widget :children
  1225. (list (widget-create-child-value widget
  1226. (widget-convert type)
  1227. value)))))
  1228. (defun widget-type-default-get (widget)
  1229. "Get default value from the :type attribute of WIDGET.
  1230. The value of the :type attribute should be an unconverted widget type."
  1231. (widget-default-get (widget-convert (widget-get widget :type))))
  1232. (defun widget-type-match (widget value)
  1233. "Non-nil if the :type value of WIDGET matches VALUE.
  1234. The value of the :type attribute should be an unconverted widget type."
  1235. (widget-apply (widget-convert (widget-get widget :type)) :match value))
  1236. (defun widget-types-copy (widget)
  1237. "Copy :args as widget types in WIDGET."
  1238. (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
  1239. widget)
  1240. ;; Made defsubst to speed up face editor creation.
  1241. (defsubst widget-types-convert-widget (widget)
  1242. "Convert :args as widget types in WIDGET."
  1243. (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
  1244. widget)
  1245. (defun widget-value-convert-widget (widget)
  1246. "Initialize :value from :args in WIDGET."
  1247. (let ((args (widget-get widget :args)))
  1248. (when args
  1249. (widget-put widget :value (car args))
  1250. ;; Don't convert :value here, as this is done in `widget-convert'.
  1251. ;; (widget-put widget :value (widget-apply widget
  1252. ;; :value-to-internal (car args)))
  1253. (widget-put widget :args nil)))
  1254. widget)
  1255. (defun widget-value-value-get (widget)
  1256. "Return the :value property of WIDGET."
  1257. (widget-get widget :value))
  1258. ;;; The `default' Widget.
  1259. (define-widget 'default nil
  1260. "Basic widget other widgets are derived from."
  1261. :value-to-internal (lambda (_widget value) value)
  1262. :value-to-external (lambda (_widget value) value)
  1263. :button-prefix 'widget-button-prefix
  1264. :button-suffix 'widget-button-suffix
  1265. :completions-function #'widget-default-completions
  1266. :create 'widget-default-create
  1267. :indent nil
  1268. :offset 0
  1269. :format-handler 'widget-default-format-handler
  1270. :button-face-get 'widget-default-button-face-get
  1271. :mouse-face-get 'widget-default-mouse-face-get
  1272. :sample-face-get 'widget-default-sample-face-get
  1273. :delete 'widget-default-delete
  1274. :copy 'identity
  1275. :value-set 'widget-default-value-set
  1276. :value-inline 'widget-default-value-inline
  1277. :value-delete 'ignore
  1278. :default-get 'widget-default-default-get
  1279. :menu-tag-get 'widget-default-menu-tag-get
  1280. :validate #'ignore
  1281. :active 'widget-default-active
  1282. :activate 'widget-specify-active
  1283. :deactivate 'widget-default-deactivate
  1284. :mouse-down-action #'ignore
  1285. :action 'widget-default-action
  1286. :notify 'widget-default-notify
  1287. :prompt-value 'widget-default-prompt-value)
  1288. (defvar widget--completing-widget)
  1289. (defun widget-default-completions (widget)
  1290. "Return completion data, like `completion-at-point-functions' would."
  1291. (let ((completions (widget-get widget :completions)))
  1292. (if completions
  1293. (list (widget-field-start widget)
  1294. (max (point) (widget-field-text-end widget))
  1295. completions)
  1296. (if (widget-get widget :complete)
  1297. (lambda () (widget-apply widget :complete))
  1298. (if (widget-get widget :complete-function)
  1299. (lambda ()
  1300. (let ((widget--completing-widget widget))
  1301. (call-interactively
  1302. (widget-get widget :complete-function)))))))))
  1303. (defun widget-default-create (widget)
  1304. "Create WIDGET at point in the current buffer."
  1305. (widget-specify-insert
  1306. (let ((from (point))
  1307. button-begin button-end
  1308. sample-begin sample-end
  1309. doc-begin doc-end
  1310. value-pos)
  1311. (insert (widget-get widget :format))
  1312. (goto-char from)
  1313. ;; Parse escapes in format.
  1314. (while (re-search-forward "%\\(.\\)" nil t)
  1315. (let ((escape (char-after (match-beginning 1))))
  1316. (delete-char -2)
  1317. (cond ((eq escape ?%)
  1318. (insert ?%))
  1319. ((eq escape ?\[)
  1320. (setq button-begin (point))
  1321. (insert (widget-get-indirect widget :button-prefix)))
  1322. ((eq escape ?\])
  1323. (insert (widget-get-indirect widget :button-suffix))
  1324. (setq button-end (point)))
  1325. ((eq escape ?\{)
  1326. (setq sample-begin (point)))
  1327. ((eq escape ?\})
  1328. (setq sample-end (point)))
  1329. ((eq escape ?n)
  1330. (when (widget-get widget :indent)
  1331. (insert ?\n)
  1332. (insert-char ?\s (widget-get widget :indent))))
  1333. ((eq escape ?t)
  1334. (let ((image (widget-get widget :tag-glyph))
  1335. (tag (substitute-command-keys
  1336. (widget-get widget :tag))))
  1337. (cond (image
  1338. (widget-image-insert widget (or tag "image") image))
  1339. (tag
  1340. (insert tag))
  1341. (t
  1342. (princ (widget-get widget :value)
  1343. (current-buffer))))))
  1344. ((eq escape ?d)
  1345. (let ((doc (widget-get widget :doc)))
  1346. (when doc
  1347. (setq doc-begin (point))
  1348. (insert (substitute-command-keys doc))
  1349. (while (eq (preceding-char) ?\n)
  1350. (delete-char -1))
  1351. (insert ?\n)
  1352. (setq doc-end (point)))))
  1353. ((eq escape ?h)
  1354. (widget-add-documentation-string-button widget))
  1355. ((eq escape ?v)
  1356. (if (and button-begin (not button-end))
  1357. (widget-apply widget :value-create)
  1358. (setq value-pos (point))))
  1359. (t
  1360. (widget-apply widget :format-handler escape)))))
  1361. ;; Specify button, sample, and doc, and insert value.
  1362. (and button-begin button-end
  1363. (widget-specify-button widget button-begin button-end))
  1364. (and sample-begin sample-end
  1365. (widget-specify-sample widget sample-begin sample-end))
  1366. (and doc-begin doc-end
  1367. (widget-specify-doc widget doc-begin doc-end))
  1368. (when value-pos
  1369. (goto-char value-pos)
  1370. (widget-apply widget :value-create)))
  1371. (let ((from (point-min-marker))
  1372. (to (point-max-marker)))
  1373. (set-marker-insertion-type from t)
  1374. (set-marker-insertion-type to nil)
  1375. (widget-put widget :from from)
  1376. (widget-put widget :to to)))
  1377. (widget-clear-undo))
  1378. (defun widget-default-format-handler (_widget escape)
  1379. (error "Unknown escape `%c'" escape))
  1380. (defun widget-default-button-face-get (widget)
  1381. ;; Use :button-face or widget-button-face
  1382. (or (widget-get widget :button-face)
  1383. (let ((parent (widget-get widget :parent)))
  1384. (if parent
  1385. (widget-apply parent :button-face-get)
  1386. widget-button-face))))
  1387. (defun widget-default-mouse-face-get (widget)
  1388. ;; Use :mouse-face or widget-mouse-face
  1389. (or (widget-get widget :mouse-face)
  1390. (let ((parent (widget-get widget :parent)))
  1391. (if parent
  1392. (widget-apply parent :mouse-face-get)
  1393. widget-mouse-face))))
  1394. (defun widget-default-sample-face-get (widget)
  1395. ;; Use :sample-face.
  1396. (widget-get widget :sample-face))
  1397. (defun widget-default-delete (widget)
  1398. "Remove widget from the buffer."
  1399. (let ((from (widget-get widget :from))
  1400. (to (widget-get widget :to))
  1401. (inactive-overlay (widget-get widget :inactive))
  1402. (button-overlay (widget-get widget :button-overlay))
  1403. (sample-overlay (widget-get widget :sample-overlay))
  1404. (doc-overlay (widget-get widget :doc-overlay))
  1405. (inhibit-modification-hooks t)
  1406. (inhibit-read-only t))
  1407. (widget-apply widget :value-delete)
  1408. (widget-children-value-delete widget)
  1409. (when inactive-overlay
  1410. (delete-overlay inactive-overlay))
  1411. (when button-overlay
  1412. (delete-overlay button-overlay))
  1413. (when sample-overlay
  1414. (delete-overlay sample-overlay))
  1415. (when doc-overlay
  1416. (delete-overlay doc-overlay))
  1417. (when (< from to)
  1418. ;; Kludge: this doesn't need to be true for empty formats.
  1419. (delete-region from to))
  1420. (set-marker from nil)
  1421. (set-marker to nil))
  1422. (widget-clear-undo))
  1423. (defun widget-default-value-set (widget value)
  1424. "Recreate widget with new value."
  1425. (let* ((old-pos (point))
  1426. (from (copy-marker (widget-get widget :from)))
  1427. (to (copy-marker (widget-get widget :to)))
  1428. (offset (if (and (<= from old-pos) (<= old-pos to))
  1429. (if (>= old-pos (1- to))
  1430. (- old-pos to 1)
  1431. (- old-pos from)))))
  1432. ;;??? Bug: this ought to insert the new value before deleting the old one,
  1433. ;; so that markers on either side of the value automatically
  1434. ;; stay on the same side. -- rms.
  1435. (save-excursion
  1436. (goto-char (widget-get widget :from))
  1437. (widget-apply widget :delete)
  1438. (widget-put widget :value value)
  1439. (widget-apply widget :create))
  1440. (if offset
  1441. (if (< offset 0)
  1442. (goto-char (+ (widget-get widget :to) offset 1))
  1443. (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
  1444. (defun widget-default-value-inline (widget)
  1445. "Wrap value in a list unless it is inline."
  1446. (if (widget-get widget :inline)
  1447. (widget-value widget)
  1448. (list (widget-value widget))))
  1449. (defun widget-default-default-get (widget)
  1450. "Get `:value'."
  1451. (widget-get widget :value))
  1452. (defun widget-default-menu-tag-get (widget)
  1453. "Use tag or value for menus."
  1454. (or (widget-get widget :menu-tag)
  1455. (widget-get widget :tag)
  1456. (widget-princ-to-string (widget-get widget :value))))
  1457. (defun widget-default-active (widget)
  1458. "Return t if this widget is active (user modifiable)."
  1459. (or (widget-get widget :always-active)
  1460. (and (not (widget-get widget :inactive))
  1461. (let ((parent (widget-get widget :parent)))
  1462. (or (null parent)
  1463. (widget-apply parent :active))))))
  1464. (defun widget-default-deactivate (widget)
  1465. "Make WIDGET inactive for user modifications."
  1466. (widget-specify-inactive widget
  1467. (widget-get widget :from)
  1468. (widget-get widget :to)))
  1469. (defun widget-default-action (widget &optional event)
  1470. "Notify the parent when a widget changes."
  1471. (let ((parent (widget-get widget :parent)))
  1472. (when parent
  1473. (widget-apply parent :notify widget event))))
  1474. (defun widget-default-notify (widget _child &optional event)
  1475. "Pass notification to parent."
  1476. (widget-default-action widget event))
  1477. (defun widget-default-prompt-value (_widget prompt _value _unbound)
  1478. "Read an arbitrary value."
  1479. (eval-minibuffer prompt))
  1480. (defun widget-docstring (widget)
  1481. "Return the documentation string specified by WIDGET, or nil if none.
  1482. If WIDGET has a `:doc' property, that specifies the documentation string.
  1483. Otherwise, try the `:documentation-property' property. If this
  1484. is a function, call it with the widget's value as an argument; if
  1485. it is a symbol, use this symbol together with the widget's value
  1486. as the argument to `documentation-property'."
  1487. (let ((doc (or (widget-get widget :doc)
  1488. (let ((doc-prop (widget-get widget :documentation-property))
  1489. (value (widget-get widget :value)))
  1490. (cond ((functionp doc-prop)
  1491. (funcall doc-prop value))
  1492. ((symbolp doc-prop)
  1493. (documentation-property value doc-prop t)))))))
  1494. (when (and (stringp doc) (> (length doc) 0))
  1495. ;; Remove any redundant `*' in the beginning.
  1496. (when (eq (aref doc 0) ?*)
  1497. (setq doc (substring doc 1)))
  1498. ;; Remove trailing newlines.
  1499. (when (string-match "\n+\\'" doc)
  1500. (setq doc (substring doc 0 (match-beginning 0))))
  1501. doc)))
  1502. ;;; The `item' Widget.
  1503. (define-widget 'item 'default
  1504. "Constant items for inclusion in other widgets."
  1505. :convert-widget 'widget-value-convert-widget
  1506. :value-create 'widget-item-value-create
  1507. :value-delete 'ignore
  1508. :value-get 'widget-value-value-get
  1509. :match 'widget-item-match
  1510. :match-inline 'widget-item-match-inline
  1511. :action 'widget-item-action
  1512. :format "%t\n")
  1513. (defun widget-item-value-create (widget)
  1514. "Insert the printed representation of the value."
  1515. (princ (widget-get widget :value) (current-buffer)))
  1516. (defun widget-item-match (widget value)
  1517. ;; Match if the value is the same.
  1518. (equal (widget-get widget :value) value))
  1519. (defun widget-item-match-inline (widget vals)
  1520. ;; Match if the value is the same.
  1521. (let ((value (widget-get widget :value)))
  1522. (and (listp value)
  1523. (<= (length value) (length vals))
  1524. (let ((head (widget-sublist vals 0 (length value))))
  1525. (and (equal head value)
  1526. (cons head (widget-sublist vals (length value))))))))
  1527. (defun widget-sublist (list start &optional end)
  1528. "Return the sublist of LIST from START to END.
  1529. If END is omitted, it defaults to the length of LIST."
  1530. (if (> start 0) (setq list (nthcdr start list)))
  1531. (if end
  1532. (unless (<= end start)
  1533. (setq list (copy-sequence list))
  1534. (setcdr (nthcdr (- end start 1) list) nil)
  1535. list)
  1536. (copy-sequence list)))
  1537. (defun widget-item-action (widget &optional event)
  1538. ;; Just notify itself.
  1539. (widget-apply widget :notify widget event))
  1540. ;;; The `push-button' Widget.
  1541. ;; (defcustom widget-push-button-gui t
  1542. ;; "If non-nil, use GUI push buttons when available."
  1543. ;; :group 'widgets
  1544. ;; :type 'boolean)
  1545. ;; Cache already created GUI objects.
  1546. ;; (defvar widget-push-button-cache nil)
  1547. (defcustom widget-push-button-prefix "["
  1548. "String used as prefix for buttons."
  1549. :type 'string
  1550. :group 'widget-button)
  1551. (defcustom widget-push-button-suffix "]"
  1552. "String used as suffix for buttons."
  1553. :type 'string
  1554. :group 'widget-button)
  1555. (define-widget 'push-button 'item
  1556. "A pushable button."
  1557. :button-prefix ""
  1558. :button-suffix ""
  1559. :value-create 'widget-push-button-value-create
  1560. :format "%[%v%]")
  1561. (defun widget-push-button-value-create (widget)
  1562. "Insert text representing the `on' and `off' states."
  1563. (let* ((tag (or (substitute-command-keys (widget-get widget :tag))
  1564. (widget-get widget :value)))
  1565. (tag-glyph (widget-get widget :tag-glyph))
  1566. (text (concat widget-push-button-prefix
  1567. tag widget-push-button-suffix)))
  1568. (if tag-glyph
  1569. (widget-image-insert widget text tag-glyph)
  1570. (insert text))))
  1571. ;; (defun widget-gui-action (widget)
  1572. ;; "Apply :action for WIDGET."
  1573. ;; (widget-apply-action widget (this-command-keys)))
  1574. ;;; The `link' Widget.
  1575. (defcustom widget-link-prefix "["
  1576. "String used as prefix for links."
  1577. :type 'string
  1578. :group 'widget-button)
  1579. (defcustom widget-link-suffix "]"
  1580. "String used as suffix for links."
  1581. :type 'string
  1582. :group 'widget-button)
  1583. (define-widget 'link 'item
  1584. "An embedded link."
  1585. :button-prefix 'widget-link-prefix
  1586. :button-suffix 'widget-link-suffix
  1587. ;; The `follow-link' property should only be used in those contexts where the
  1588. ;; mouse-1 event normally doesn't follow the link, yet the `link' widget
  1589. ;; seems to almost always be used in contexts where (down-)mouse-1 is bound
  1590. ;; to `widget-button-click' and hence the "mouse-1 to mouse-2" remapping is
  1591. ;; not necessary (and can even be harmful). So let's not add a :follow-link
  1592. ;; by default. See (bug#22434).
  1593. ;; :follow-link 'mouse-face
  1594. :help-echo "Follow the link."
  1595. :format "%[%t%]")
  1596. ;;; The `info-link' Widget.
  1597. (define-widget 'info-link 'link
  1598. "A link to an info file."
  1599. :action 'widget-info-link-action)
  1600. (defun widget-info-link-action (widget &optional _event)
  1601. "Open the info node specified by WIDGET."
  1602. (info (widget-value widget)))
  1603. ;;; The `url-link' Widget.
  1604. (define-widget 'url-link 'link
  1605. "A link to an www page."
  1606. :action 'widget-url-link-action)
  1607. (defun widget-url-link-action (widget &optional _event)
  1608. "Open the URL specified by WIDGET."
  1609. (browse-url (widget-value widget)))
  1610. ;;; The `function-link' Widget.
  1611. (define-widget 'function-link 'link
  1612. "A link to an Emacs function."
  1613. :action 'widget-function-link-action)
  1614. (defun widget-function-link-action (widget &optional _event)
  1615. "Show the function specified by WIDGET."
  1616. (describe-function (widget-value widget)))
  1617. ;;; The `variable-link' Widget.
  1618. (define-widget 'variable-link 'link
  1619. "A link to an Emacs variable."
  1620. :action 'widget-variable-link-action)
  1621. (defun widget-variable-link-action (widget &optional _event)
  1622. "Show the variable specified by WIDGET."
  1623. (describe-variable (widget-value widget)))
  1624. ;;; The `file-link' Widget.
  1625. (define-widget 'file-link 'link
  1626. "A link to a file."
  1627. :action 'widget-file-link-action)
  1628. (defun widget-file-link-action (widget &optional _event)
  1629. "Find the file specified by WIDGET."
  1630. (find-file (widget-value widget)))
  1631. ;;; The `emacs-library-link' Widget.
  1632. (define-widget 'emacs-library-link 'link
  1633. "A link to an Emacs Lisp library file."
  1634. :action 'widget-emacs-library-link-action)
  1635. (defun widget-emacs-library-link-action (widget &optional _event)
  1636. "Find the Emacs library file specified by WIDGET."
  1637. (find-file (locate-library (widget-value widget))))
  1638. ;;; The `emacs-commentary-link' Widget.
  1639. (define-widget 'emacs-commentary-link 'link
  1640. "A link to Commentary in an Emacs Lisp library file."
  1641. :action 'widget-emacs-commentary-link-action)
  1642. (defun widget-emacs-commentary-link-action (widget &optional _event)
  1643. "Find the Commentary section of the Emacs file specified by WIDGET."
  1644. (finder-commentary (widget-value widget)))
  1645. ;;; The `editable-field' Widget.
  1646. (define-widget 'editable-field 'default
  1647. "An editable text field.
  1648. Note: In an `editable-field' widget, the `%v' escape must be preceded
  1649. by some other text in the `:format' string (if specified)."
  1650. :convert-widget 'widget-value-convert-widget
  1651. :keymap widget-field-keymap
  1652. :format "%v"
  1653. :help-echo "M-TAB: complete field; RET: enter value"
  1654. :value ""
  1655. :prompt-internal 'widget-field-prompt-internal
  1656. :prompt-history 'widget-field-history
  1657. :prompt-value 'widget-field-prompt-value
  1658. :action 'widget-field-action
  1659. :validate 'widget-field-validate
  1660. :valid-regexp ""
  1661. :error "Field's value doesn't match allowed forms"
  1662. :value-create 'widget-field-value-create
  1663. :value-set 'widget-field-value-set
  1664. :value-delete 'widget-field-value-delete
  1665. :value-get 'widget-field-value-get
  1666. :match 'widget-field-match)
  1667. (defvar widget-field-history nil
  1668. "History of field minibuffer edits.")
  1669. (defun widget-field-prompt-internal (_widget prompt initial history)
  1670. "Read string for WIDGET prompting with PROMPT.
  1671. INITIAL is the initial input and HISTORY is a symbol containing
  1672. the earlier input."
  1673. (read-string prompt initial history))
  1674. (defun widget-field-prompt-value (widget prompt value unbound)
  1675. "Prompt for a string."
  1676. (widget-apply widget
  1677. :value-to-external
  1678. (widget-apply widget
  1679. :prompt-internal prompt
  1680. (unless unbound
  1681. (cons (widget-apply widget
  1682. :value-to-internal value)
  1683. 0))
  1684. (widget-get widget :prompt-history))))
  1685. (defvar widget-edit-functions nil)
  1686. (defun widget-field-action (widget &optional _event)
  1687. "Move to next field."
  1688. (widget-forward 1)
  1689. (run-hook-with-args 'widget-edit-functions widget))
  1690. (defun widget-field-validate (widget)
  1691. "Valid if the content matches `:valid-regexp'."
  1692. (unless (string-match (widget-get widget :valid-regexp)
  1693. (widget-apply widget :value-get))
  1694. widget))
  1695. (defun widget-field-value-set (widget value)
  1696. "Set an editable text field WIDGET to VALUE"
  1697. (let ((from (widget-field-start widget))
  1698. (to (widget-field-text-end widget))
  1699. (buffer (widget-field-buffer widget)))
  1700. (when (and from to (buffer-live-p buffer))
  1701. (with-current-buffer buffer
  1702. (goto-char from)
  1703. (delete-char (- to from))
  1704. (insert value)))))
  1705. (defun widget-field-value-create (widget)
  1706. "Create an editable text field."
  1707. (let ((size (widget-get widget :size))
  1708. (value (widget-get widget :value))
  1709. (from (point))
  1710. ;; This is changed to a real overlay in `widget-setup'. We
  1711. ;; need the end points to behave differently until
  1712. ;; `widget-setup' is called.
  1713. (overlay (cons (make-marker) (make-marker))))
  1714. (widget-put widget :field-overlay overlay)
  1715. (insert value)
  1716. (and size
  1717. (< (length value) size)
  1718. (insert-char ?\s (- size (length value))))
  1719. (unless (memq widget widget-field-list)
  1720. (setq widget-field-new (cons widget widget-field-new)))
  1721. (move-marker (cdr overlay) (point))
  1722. (set-marker-insertion-type (cdr overlay) nil)
  1723. (when (null size)
  1724. (insert ?\n))
  1725. (move-marker (car overlay) from)
  1726. (set-marker-insertion-type (car overlay) t)))
  1727. (defun widget-field-value-delete (widget)
  1728. "Remove the widget from the list of active editing fields."
  1729. (setq widget-field-list (delq widget widget-field-list))
  1730. (setq widget-field-new (delq widget widget-field-new))
  1731. ;; These are nil if the :format string doesn't contain `%v'.
  1732. (let ((overlay (widget-get widget :field-overlay)))
  1733. (when (overlayp overlay)
  1734. (delete-overlay overlay))))
  1735. (defun widget-field-value-get (widget &optional no-truncate)
  1736. "Return current text in editing field.
  1737. Normally, trailing spaces within the editing field are truncated.
  1738. But if NO-TRUNCATE is non-nil, include them."
  1739. (let ((from (widget-field-start widget))
  1740. (to (if no-truncate
  1741. (widget-field-end widget)
  1742. (widget-field-text-end widget)))
  1743. (buffer (widget-field-buffer widget))
  1744. (secret (widget-get widget :secret))
  1745. (old (current-buffer)))
  1746. (if (and from to)
  1747. (progn
  1748. (set-buffer buffer)
  1749. (let ((result (buffer-substring-no-properties from to)))
  1750. (when secret
  1751. (let ((index 0))
  1752. (while (< (+ from index) to)
  1753. (aset result index
  1754. (get-char-property (+ from index) 'secret))
  1755. (setq index (1+ index)))))
  1756. (set-buffer old)
  1757. result))
  1758. (widget-get widget :value))))
  1759. (defun widget-field-match (_widget value)
  1760. ;; Match any string.
  1761. (stringp value))
  1762. ;;; The `text' Widget.
  1763. (define-widget 'text 'editable-field
  1764. "A multiline text area."
  1765. :keymap widget-text-keymap)
  1766. ;;; The `menu-choice' Widget.
  1767. (define-widget 'menu-choice 'default
  1768. "A menu of options."
  1769. :convert-widget 'widget-types-convert-widget
  1770. :copy 'widget-types-copy
  1771. :format "%[%t%]: %v"
  1772. :case-fold t
  1773. :tag "choice"
  1774. :void '(item :format "invalid (%t)\n")
  1775. :value-create 'widget-choice-value-create
  1776. :value-get 'widget-child-value-get
  1777. :value-inline 'widget-child-value-inline
  1778. :default-get 'widget-choice-default-get
  1779. :mouse-down-action 'widget-choice-mouse-down-action
  1780. :action 'widget-choice-action
  1781. :error "Make a choice"
  1782. :validate 'widget-choice-validate
  1783. :match 'widget-choice-match
  1784. :match-inline 'widget-choice-match-inline)
  1785. (defun widget-choice-value-create (widget)
  1786. "Insert the first choice that matches the value."
  1787. (let ((value (widget-get widget :value))
  1788. (args (widget-get widget :args))
  1789. (explicit (widget-get widget :explicit-choice))
  1790. current)
  1791. (if explicit
  1792. (progn
  1793. ;; If the user specified the choice for this value,
  1794. ;; respect that choice.
  1795. (widget-put widget :children (list (widget-create-child-value
  1796. widget explicit value)))
  1797. (widget-put widget :choice explicit)
  1798. (widget-put widget :explicit-choice nil))
  1799. (while args
  1800. (setq current (car args)
  1801. args (cdr args))
  1802. (when (widget-apply current :match value)
  1803. (widget-put widget :children (list (widget-create-child-value
  1804. widget current value)))
  1805. (widget-put widget :choice current)
  1806. (setq args nil
  1807. current nil)))
  1808. (when current
  1809. (let ((void (widget-get widget :void)))
  1810. (widget-put widget :children (list (widget-create-child-and-convert
  1811. widget void :value value)))
  1812. (widget-put widget :choice void))))))
  1813. (defun widget-choice-default-get (widget)
  1814. ;; Get default for the first choice.
  1815. (widget-default-get (car (widget-get widget :args))))
  1816. (defcustom widget-choice-toggle nil
  1817. "If non-nil, a binary choice will just toggle between the values.
  1818. Otherwise, the user will explicitly have to choose between the values
  1819. when he invoked the menu."
  1820. :type 'boolean
  1821. :group 'widgets)
  1822. (defun widget-choice-mouse-down-action (widget &optional _event)
  1823. ;; Return non-nil if we need a menu.
  1824. (let ((args (widget-get widget :args))
  1825. (old (widget-get widget :choice)))
  1826. (cond ((not (display-popup-menus-p))
  1827. ;; No place to pop up a menu.
  1828. nil)
  1829. ((< (length args) 2)
  1830. ;; Empty or singleton list, just return the value.
  1831. nil)
  1832. ((> (length args) widget-menu-max-size)
  1833. ;; Too long, prompt.
  1834. nil)
  1835. ((> (length args) 2)
  1836. ;; Reasonable sized list, use menu.
  1837. t)
  1838. ((and widget-choice-toggle (memq old args))
  1839. ;; We toggle.
  1840. nil)
  1841. (t
  1842. ;; Ask which of the two.
  1843. t))))
  1844. (defun widget-choice-action (widget &optional event)
  1845. ;; Make a choice.
  1846. (let ((args (widget-get widget :args))
  1847. (old (widget-get widget :choice))
  1848. (tag (widget-apply widget :menu-tag-get))
  1849. (completion-ignore-case (widget-get widget :case-fold))
  1850. this-explicit
  1851. current choices)
  1852. ;; Remember old value.
  1853. (if (and old (not (widget-apply widget :validate)))
  1854. (let* ((external (widget-value widget))
  1855. (internal (widget-apply old :value-to-internal external)))
  1856. (widget-put old :value internal)))
  1857. ;; Find new choice.
  1858. (setq current
  1859. (cond ((= (length args) 0)
  1860. nil)
  1861. ((= (length args) 1)
  1862. (nth 0 args))
  1863. ((and widget-choice-toggle
  1864. (= (length args) 2)
  1865. (memq old args))
  1866. (if (eq old (nth 0 args))
  1867. (nth 1 args)
  1868. (nth 0 args)))
  1869. (t
  1870. (while args
  1871. (setq current (car args)
  1872. args (cdr args))
  1873. (setq choices
  1874. (cons (cons (widget-apply current :menu-tag-get)
  1875. current)
  1876. choices)))
  1877. (setq this-explicit t)
  1878. (widget-choose tag (reverse choices) event))))
  1879. (when current
  1880. ;; If this was an explicit user choice, record the choice,
  1881. ;; so that widget-choice-value-create will respect it.
  1882. (when this-explicit
  1883. (widget-put widget :explicit-choice current))
  1884. (widget-value-set widget (widget-default-get current))
  1885. (widget-setup)
  1886. (widget-apply widget :notify widget event)))
  1887. (run-hook-with-args 'widget-edit-functions widget))
  1888. (defun widget-choice-validate (widget)
  1889. ;; Valid if we have made a valid choice.
  1890. (if (eq (widget-get widget :void) (widget-get widget :choice))
  1891. widget
  1892. (widget-apply (car (widget-get widget :children)) :validate)))
  1893. (defun widget-choice-match (widget value)
  1894. ;; Matches if one of the choices matches.
  1895. (let ((args (widget-get widget :args))
  1896. current found)
  1897. (while (and args (not found))
  1898. (setq current (car args)
  1899. args (cdr args)
  1900. found (widget-apply current :match value)))
  1901. found))
  1902. (defun widget-choice-match-inline (widget vals)
  1903. ;; Matches if one of the choices matches.
  1904. (let ((args (widget-get widget :args))
  1905. current found)
  1906. (while (and args (null found))
  1907. (setq current (car args)
  1908. args (cdr args)
  1909. found (widget-match-inline current vals)))
  1910. found))
  1911. ;;; The `toggle' Widget.
  1912. (define-widget 'toggle 'item
  1913. "Toggle between two states."
  1914. :format "%[%v%]\n"
  1915. :value-create 'widget-toggle-value-create
  1916. :action 'widget-toggle-action
  1917. :match (lambda (_widget _value) t)
  1918. :on "on"
  1919. :off "off")
  1920. (defun widget-toggle-value-create (widget)
  1921. "Insert text representing the `on' and `off' states."
  1922. (let* ((val (widget-value widget))
  1923. (text (substitute-command-keys
  1924. (widget-get widget (if val :on :off))))
  1925. (img (widget-image-find
  1926. (widget-get widget (if val :on-glyph :off-glyph)))))
  1927. (widget-image-insert widget (or text "")
  1928. (if img
  1929. (append img '(:ascent center))))))
  1930. (defun widget-toggle-action (widget &optional event)
  1931. ;; Toggle value.
  1932. (widget-value-set widget (not (widget-value widget)))
  1933. (widget-apply widget :notify widget event)
  1934. (run-hook-with-args 'widget-edit-functions widget))
  1935. ;;; The `checkbox' Widget.
  1936. (define-widget 'checkbox 'toggle
  1937. "A checkbox toggle."
  1938. :button-suffix ""
  1939. :button-prefix ""
  1940. :format "%[%v%]"
  1941. :on "[X]"
  1942. ;; We could probably do the same job as the images using single
  1943. ;; space characters in a boxed face with a stretch specification to
  1944. ;; make them square.
  1945. :on-glyph "checked"
  1946. :off "[ ]"
  1947. :off-glyph "unchecked"
  1948. :help-echo "Toggle this item."
  1949. :action 'widget-checkbox-action)
  1950. (defun widget-checkbox-action (widget &optional event)
  1951. "Toggle checkbox, notify parent, and set active state of sibling."
  1952. (widget-toggle-action widget event)
  1953. (let ((sibling (widget-get-sibling widget)))
  1954. (when sibling
  1955. (if (widget-value widget)
  1956. (widget-apply sibling :activate)
  1957. (widget-apply sibling :deactivate))
  1958. (widget-clear-undo))))
  1959. ;;; The `checklist' Widget.
  1960. (define-widget 'checklist 'default
  1961. "A multiple choice widget."
  1962. :convert-widget 'widget-types-convert-widget
  1963. :copy 'widget-types-copy
  1964. :format "%v"
  1965. :offset 4
  1966. :entry-format "%b %v"
  1967. :greedy nil
  1968. :value-create 'widget-checklist-value-create
  1969. :value-get 'widget-checklist-value-get
  1970. :validate 'widget-checklist-validate
  1971. :match 'widget-checklist-match
  1972. :match-inline 'widget-checklist-match-inline)
  1973. (defun widget-checklist-value-create (widget)
  1974. ;; Insert all values
  1975. (let ((alist (widget-checklist-match-find widget))
  1976. (args (widget-get widget :args)))
  1977. (dolist (item args)
  1978. (widget-checklist-add-item widget item (assq item alist)))
  1979. (widget-put widget :children (nreverse (widget-get widget :children)))))
  1980. (defun widget-checklist-add-item (widget type chosen)
  1981. "Create checklist item in WIDGET of type TYPE.
  1982. If the item is checked, CHOSEN is a cons whose cdr is the value."
  1983. (and (eq (preceding-char) ?\n)
  1984. (widget-get widget :indent)
  1985. (insert-char ?\s (widget-get widget :indent)))
  1986. (widget-specify-insert
  1987. (let* ((children (widget-get widget :children))
  1988. (buttons (widget-get widget :buttons))
  1989. (button-args (or (widget-get type :sibling-args)
  1990. (widget-get widget :button-args)))
  1991. (from (point))
  1992. child button)
  1993. (insert (widget-get widget :entry-format))
  1994. (goto-char from)
  1995. ;; Parse % escapes in format.
  1996. (while (re-search-forward "%\\([bv%]\\)" nil t)
  1997. (let ((escape (char-after (match-beginning 1))))
  1998. (delete-char -2)
  1999. (cond ((eq escape ?%)
  2000. (insert ?%))
  2001. ((eq escape ?b)
  2002. (setq button (apply 'widget-create-child-and-convert
  2003. widget 'checkbox
  2004. :value (not (null chosen))
  2005. button-args)))
  2006. ((eq escape ?v)
  2007. (setq child
  2008. (cond ((not chosen)
  2009. (let ((child (widget-create-child widget type)))
  2010. (widget-apply child :deactivate)
  2011. child))
  2012. ((widget-get type :inline)
  2013. (widget-create-child-value
  2014. widget type (cdr chosen)))
  2015. (t
  2016. (widget-create-child-value
  2017. widget type (car (cdr chosen)))))))
  2018. (t
  2019. (error "Unknown escape `%c'" escape)))))
  2020. ;; Update properties.
  2021. (and button child (widget-put child :button button))
  2022. (and button (widget-put widget :buttons (cons button buttons)))
  2023. (and child (widget-put widget :children (cons child children))))))
  2024. (defun widget-checklist-match (widget vals)
  2025. ;; All values must match a type in the checklist.
  2026. (and (listp vals)
  2027. (null (cdr (widget-checklist-match-inline widget vals)))))
  2028. (defun widget-checklist-match-inline (widget vals)
  2029. ;; Find the values which match a type in the checklist.
  2030. (let ((greedy (widget-get widget :greedy))
  2031. (args (copy-sequence (widget-get widget :args)))
  2032. found rest)
  2033. (while vals
  2034. (let ((answer (widget-checklist-match-up args vals)))
  2035. (cond (answer
  2036. (let ((vals2 (widget-match-inline answer vals)))
  2037. (setq found (append found (car vals2))
  2038. vals (cdr vals2)
  2039. args (delq answer args))))
  2040. (greedy
  2041. (setq rest (append rest (list (car vals)))
  2042. vals (cdr vals)))
  2043. (t
  2044. (setq rest (append rest vals)
  2045. vals nil)))))
  2046. (cons found rest)))
  2047. (defun widget-checklist-match-find (widget &optional vals)
  2048. "Find the vals which match a type in the checklist.
  2049. Return an alist of (TYPE MATCH)."
  2050. (or vals (setq vals (widget-get widget :value)))
  2051. (let ((greedy (widget-get widget :greedy))
  2052. (args (copy-sequence (widget-get widget :args)))
  2053. found)
  2054. (while vals
  2055. (let ((answer (widget-checklist-match-up args vals)))
  2056. (cond (answer
  2057. (let ((match (widget-match-inline answer vals)))
  2058. (setq found (cons (cons answer (car match)) found)
  2059. vals (cdr match)
  2060. args (delq answer args))))
  2061. (greedy
  2062. (setq vals (cdr vals)))
  2063. (t
  2064. (setq vals nil)))))
  2065. found))
  2066. (defun widget-checklist-match-up (args vals)
  2067. "Return the first type from ARGS that matches VALS."
  2068. (let (current found)
  2069. (while (and args (null found))
  2070. (setq current (car args)
  2071. args (cdr args)
  2072. found (widget-match-inline current vals)))
  2073. (if found
  2074. current)))
  2075. (defun widget-checklist-value-get (widget)
  2076. ;; The values of all selected items.
  2077. (let ((children (widget-get widget :children))
  2078. child result)
  2079. (while children
  2080. (setq child (car children)
  2081. children (cdr children))
  2082. (if (widget-value (widget-get child :button))
  2083. (setq result (append result (widget-apply child :value-inline)))))
  2084. result))
  2085. (defun widget-checklist-validate (widget)
  2086. ;; Ticked children must be valid.
  2087. (let ((children (widget-get widget :children))
  2088. child button found)
  2089. (while (and children (not found))
  2090. (setq child (car children)
  2091. children (cdr children)
  2092. button (widget-get child :button)
  2093. found (and (widget-value button)
  2094. (widget-apply child :validate))))
  2095. found))
  2096. ;;; The `option' Widget
  2097. (define-widget 'option 'checklist
  2098. "An widget with an optional item."
  2099. :inline t)
  2100. ;;; The `choice-item' Widget.
  2101. (define-widget 'choice-item 'item
  2102. "Button items that delegate action events to their parents."
  2103. :action 'widget-parent-action
  2104. :format "%[%t%] \n")
  2105. ;;; The `radio-button' Widget.
  2106. (define-widget 'radio-button 'toggle
  2107. "A radio button for use in the `radio' widget."
  2108. :notify 'widget-radio-button-notify
  2109. :format "%[%v%]"
  2110. :button-suffix ""
  2111. :button-prefix ""
  2112. :on "(*)"
  2113. :on-glyph "radio1"
  2114. :off "( )"
  2115. :off-glyph "radio0")
  2116. (defun widget-radio-button-notify (widget _child &optional event)
  2117. ;; Tell daddy.
  2118. (widget-apply (widget-get widget :parent) :action widget event))
  2119. ;;; The `radio-button-choice' Widget.
  2120. (define-widget 'radio-button-choice 'default
  2121. "Select one of multiple options."
  2122. :convert-widget 'widget-types-convert-widget
  2123. :copy 'widget-types-copy
  2124. :offset 4
  2125. :format "%v"
  2126. :entry-format "%b %v"
  2127. :value-create 'widget-radio-value-create
  2128. :value-get 'widget-radio-value-get
  2129. :value-inline 'widget-radio-value-inline
  2130. :value-set 'widget-radio-value-set
  2131. :error "You must push one of the buttons"
  2132. :validate 'widget-radio-validate
  2133. :match 'widget-choice-match
  2134. :match-inline 'widget-choice-match-inline
  2135. :action 'widget-radio-action)
  2136. (defun widget-radio-value-create (widget)
  2137. ;; Insert all values
  2138. (let ((args (widget-get widget :args))
  2139. arg)
  2140. (while args
  2141. (setq arg (car args)
  2142. args (cdr args))
  2143. (widget-radio-add-item widget arg))))
  2144. (defun widget-radio-add-item (widget type)
  2145. "Add to radio widget WIDGET a new radio button item of type TYPE."
  2146. ;; (setq type (widget-convert type))
  2147. (and (eq (preceding-char) ?\n)
  2148. (widget-get widget :indent)
  2149. (insert-char ?\s (widget-get widget :indent)))
  2150. (widget-specify-insert
  2151. (let* ((value (widget-get widget :value))
  2152. (children (widget-get widget :children))
  2153. (buttons (widget-get widget :buttons))
  2154. (button-args (or (widget-get type :sibling-args)
  2155. (widget-get widget :button-args)))
  2156. (from (point))
  2157. (chosen (and (null (widget-get widget :choice))
  2158. (widget-apply type :match value)))
  2159. child button)
  2160. (insert (widget-get widget :entry-format))
  2161. (goto-char from)
  2162. ;; Parse % escapes in format.
  2163. (while (re-search-forward "%\\([bv%]\\)" nil t)
  2164. (let ((escape (char-after (match-beginning 1))))
  2165. (delete-char -2)
  2166. (cond ((eq escape ?%)
  2167. (insert ?%))
  2168. ((eq escape ?b)
  2169. (setq button (apply 'widget-create-child-and-convert
  2170. widget 'radio-button
  2171. :value (not (null chosen))
  2172. button-args)))
  2173. ((eq escape ?v)
  2174. (setq child (if chosen
  2175. (widget-create-child-value
  2176. widget type value)
  2177. (widget-create-child widget type)))
  2178. (unless chosen
  2179. (widget-apply child :deactivate)))
  2180. (t
  2181. (error "Unknown escape `%c'" escape)))))
  2182. ;; Update properties.
  2183. (when chosen
  2184. (widget-put widget :choice type))
  2185. (when button
  2186. (widget-put child :button button)
  2187. (widget-put widget :buttons (nconc buttons (list button))))
  2188. (when child
  2189. (widget-put widget :children (nconc children (list child))))
  2190. child)))
  2191. (defun widget-radio-value-get (widget)
  2192. ;; Get value of the child widget.
  2193. (let ((chosen (widget-radio-chosen widget)))
  2194. (and chosen (widget-value chosen))))
  2195. (defun widget-radio-chosen (widget)
  2196. "Return the widget representing the chosen radio button."
  2197. (let ((children (widget-get widget :children))
  2198. current found)
  2199. (while children
  2200. (setq current (car children)
  2201. children (cdr children))
  2202. (when (widget-apply (widget-get current :button) :value-get)
  2203. (setq found current
  2204. children nil)))
  2205. found))
  2206. (defun widget-radio-value-inline (widget)
  2207. ;; Get value of the child widget.
  2208. (let ((children (widget-get widget :children))
  2209. current found)
  2210. (while children
  2211. (setq current (car children)
  2212. children (cdr children))
  2213. (when (widget-apply (widget-get current :button) :value-get)
  2214. (setq found (widget-apply current :value-inline)
  2215. children nil)))
  2216. found))
  2217. (defun widget-radio-value-set (widget value)
  2218. ;; We can't just delete and recreate a radio widget, since children
  2219. ;; can be added after the original creation and won't be recreated
  2220. ;; by `:create'.
  2221. (let ((children (widget-get widget :children))
  2222. current found)
  2223. (while children
  2224. (setq current (car children)
  2225. children (cdr children))
  2226. (let* ((button (widget-get current :button))
  2227. (match (and (not found)
  2228. (widget-apply current :match value))))
  2229. (widget-value-set button match)
  2230. (if match
  2231. (progn
  2232. (widget-value-set current value)
  2233. (widget-apply current :activate))
  2234. (widget-apply current :deactivate))
  2235. (setq found (or found match))))))
  2236. (defun widget-radio-validate (widget)
  2237. ;; Valid if we have made a valid choice.
  2238. (let ((children (widget-get widget :children))
  2239. current found button)
  2240. (while (and children (not found))
  2241. (setq current (car children)
  2242. children (cdr children)
  2243. button (widget-get current :button)
  2244. found (widget-apply button :value-get)))
  2245. (if found
  2246. (widget-apply current :validate)
  2247. widget)))
  2248. (defun widget-radio-action (widget child event)
  2249. ;; Check if a radio button was pressed.
  2250. (let ((children (widget-get widget :children))
  2251. (buttons (widget-get widget :buttons))
  2252. current)
  2253. (when (memq child buttons)
  2254. (while children
  2255. (setq current (car children)
  2256. children (cdr children))
  2257. (let* ((button (widget-get current :button)))
  2258. (cond ((eq child button)
  2259. (widget-value-set button t)
  2260. (widget-apply current :activate))
  2261. ((widget-value button)
  2262. (widget-value-set button nil)
  2263. (widget-apply current :deactivate)))))))
  2264. ;; Pass notification to parent.
  2265. (widget-apply widget :notify child event))
  2266. ;;; The `insert-button' Widget.
  2267. (define-widget 'insert-button 'push-button
  2268. "An insert button for the `editable-list' widget."
  2269. :tag "INS"
  2270. :help-echo "Insert a new item into the list at this position."
  2271. :action 'widget-insert-button-action)
  2272. (defun widget-insert-button-action (widget &optional _event)
  2273. ;; Ask the parent to insert a new item.
  2274. (widget-apply (widget-get widget :parent)
  2275. :insert-before (widget-get widget :widget)))
  2276. ;;; The `delete-button' Widget.
  2277. (define-widget 'delete-button 'push-button
  2278. "A delete button for the `editable-list' widget."
  2279. :tag "DEL"
  2280. :help-echo "Delete this item from the list."
  2281. :action 'widget-delete-button-action)
  2282. (defun widget-delete-button-action (widget &optional _event)
  2283. ;; Ask the parent to insert a new item.
  2284. (widget-apply (widget-get widget :parent)
  2285. :delete-at (widget-get widget :widget)))
  2286. ;;; The `editable-list' Widget.
  2287. ;; (defcustom widget-editable-list-gui nil
  2288. ;; "If non-nil, use GUI push-buttons in editable list when available."
  2289. ;; :type 'boolean
  2290. ;; :group 'widgets)
  2291. (define-widget 'editable-list 'default
  2292. "A variable list of widgets of the same type."
  2293. :convert-widget 'widget-types-convert-widget
  2294. :copy 'widget-types-copy
  2295. :offset 12
  2296. :format "%v%i\n"
  2297. :format-handler 'widget-editable-list-format-handler
  2298. :entry-format "%i %d %v"
  2299. :value-create 'widget-editable-list-value-create
  2300. :value-get 'widget-editable-list-value-get
  2301. :validate 'widget-children-validate
  2302. :match 'widget-editable-list-match
  2303. :match-inline 'widget-editable-list-match-inline
  2304. :insert-before 'widget-editable-list-insert-before
  2305. :delete-at 'widget-editable-list-delete-at)
  2306. (defun widget-editable-list-format-handler (widget escape)
  2307. ;; We recognize the insert button.
  2308. ;; (let ((widget-push-button-gui widget-editable-list-gui))
  2309. (cond ((eq escape ?i)
  2310. (and (widget-get widget :indent)
  2311. (insert-char ?\s (widget-get widget :indent)))
  2312. (apply 'widget-create-child-and-convert
  2313. widget 'insert-button
  2314. (widget-get widget :append-button-args)))
  2315. (t
  2316. (widget-default-format-handler widget escape)))
  2317. ;; )
  2318. )
  2319. (defun widget-editable-list-value-create (widget)
  2320. ;; Insert all values
  2321. (let* ((value (widget-get widget :value))
  2322. (type (nth 0 (widget-get widget :args)))
  2323. children)
  2324. (widget-put widget :value-pos (point-marker))
  2325. (set-marker-insertion-type (widget-get widget :value-pos) t)
  2326. (while value
  2327. (let ((answer (widget-match-inline type value)))
  2328. (if answer
  2329. (setq children (cons (widget-editable-list-entry-create
  2330. widget
  2331. (if (widget-get type :inline)
  2332. (car answer)
  2333. (car (car answer)))
  2334. t)
  2335. children)
  2336. value (cdr answer))
  2337. (setq value nil))))
  2338. (widget-put widget :children (nreverse children))))
  2339. (defun widget-editable-list-value-get (widget)
  2340. ;; Get value of the child widget.
  2341. (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
  2342. (widget-get widget :children))))
  2343. (defun widget-editable-list-match (widget value)
  2344. ;; Value must be a list and all the members must match the type.
  2345. (and (listp value)
  2346. (null (cdr (widget-editable-list-match-inline widget value)))))
  2347. (defun widget-editable-list-match-inline (widget value)
  2348. (let ((type (nth 0 (widget-get widget :args)))
  2349. (ok t)
  2350. found)
  2351. (while (and value ok)
  2352. (let ((answer (widget-match-inline type value)))
  2353. (if answer
  2354. (setq found (append found (car answer))
  2355. value (cdr answer))
  2356. (setq ok nil))))
  2357. (cons found value)))
  2358. (defun widget-editable-list-insert-before (widget before)
  2359. ;; Insert a new child in the list of children.
  2360. (save-excursion
  2361. (let ((children (widget-get widget :children))
  2362. (inhibit-read-only t)
  2363. (inhibit-modification-hooks t))
  2364. (cond (before
  2365. (goto-char (widget-get before :entry-from)))
  2366. (t
  2367. (goto-char (widget-get widget :value-pos))))
  2368. (let ((child (widget-editable-list-entry-create
  2369. widget nil nil)))
  2370. (when (< (widget-get child :entry-from) (widget-get widget :from))
  2371. (set-marker (widget-get widget :from)
  2372. (widget-get child :entry-from)))
  2373. (if (eq (car children) before)
  2374. (widget-put widget :children (cons child children))
  2375. (while (not (eq (car (cdr children)) before))
  2376. (setq children (cdr children)))
  2377. (setcdr children (cons child (cdr children)))))))
  2378. (widget-setup)
  2379. (widget-apply widget :notify widget))
  2380. (defun widget-editable-list-delete-at (widget child)
  2381. ;; Delete child from list of children.
  2382. (save-excursion
  2383. (let ((buttons (copy-sequence (widget-get widget :buttons)))
  2384. button
  2385. (inhibit-read-only t)
  2386. (inhibit-modification-hooks t))
  2387. (while buttons
  2388. (setq button (car buttons)
  2389. buttons (cdr buttons))
  2390. (when (eq (widget-get button :widget) child)
  2391. (widget-put widget
  2392. :buttons (delq button (widget-get widget :buttons)))
  2393. (widget-delete button))))
  2394. (let ((entry-from (widget-get child :entry-from))
  2395. (entry-to (widget-get child :entry-to))
  2396. (inhibit-read-only t)
  2397. (inhibit-modification-hooks t))
  2398. (widget-delete child)
  2399. (delete-region entry-from entry-to)
  2400. (set-marker entry-from nil)
  2401. (set-marker entry-to nil))
  2402. (widget-put widget :children (delq child (widget-get widget :children))))
  2403. (widget-setup)
  2404. (widget-apply widget :notify widget))
  2405. (defun widget-editable-list-entry-create (widget value conv)
  2406. ;; Create a new entry to the list.
  2407. (let ((type (nth 0 (widget-get widget :args)))
  2408. ;; (widget-push-button-gui widget-editable-list-gui)
  2409. child delete insert)
  2410. (widget-specify-insert
  2411. (save-excursion
  2412. (and (widget-get widget :indent)
  2413. (insert-char ?\s (widget-get widget :indent)))
  2414. (insert (widget-get widget :entry-format)))
  2415. ;; Parse % escapes in format.
  2416. (while (re-search-forward "%\\(.\\)" nil t)
  2417. (let ((escape (char-after (match-beginning 1))))
  2418. (delete-char -2)
  2419. (cond ((eq escape ?%)
  2420. (insert ?%))
  2421. ((eq escape ?i)
  2422. (setq insert (apply 'widget-create-child-and-convert
  2423. widget 'insert-button
  2424. (widget-get widget :insert-button-args))))
  2425. ((eq escape ?d)
  2426. (setq delete (apply 'widget-create-child-and-convert
  2427. widget 'delete-button
  2428. (widget-get widget :delete-button-args))))
  2429. ((eq escape ?v)
  2430. (if conv
  2431. (setq child (widget-create-child-value
  2432. widget type value))
  2433. (setq child (widget-create-child-value
  2434. widget type (widget-default-get type)))))
  2435. (t
  2436. (error "Unknown escape `%c'" escape)))))
  2437. (let ((buttons (widget-get widget :buttons)))
  2438. (if insert (push insert buttons))
  2439. (if delete (push delete buttons))
  2440. (widget-put widget :buttons buttons))
  2441. (let ((entry-from (point-min-marker))
  2442. (entry-to (point-max-marker)))
  2443. (set-marker-insertion-type entry-from t)
  2444. (set-marker-insertion-type entry-to nil)
  2445. (widget-put child :entry-from entry-from)
  2446. (widget-put child :entry-to entry-to)))
  2447. (if insert (widget-put insert :widget child))
  2448. (if delete (widget-put delete :widget child))
  2449. child))
  2450. ;;; The `group' Widget.
  2451. (define-widget 'group 'default
  2452. "A widget which groups other widgets inside."
  2453. :convert-widget 'widget-types-convert-widget
  2454. :copy 'widget-types-copy
  2455. :format "%v"
  2456. :value-create 'widget-group-value-create
  2457. :value-get 'widget-editable-list-value-get
  2458. :default-get 'widget-group-default-get
  2459. :validate 'widget-children-validate
  2460. :match 'widget-group-match
  2461. :match-inline 'widget-group-match-inline)
  2462. (defun widget-group-value-create (widget)
  2463. ;; Create each component.
  2464. (let ((args (widget-get widget :args))
  2465. (value (widget-get widget :value))
  2466. arg answer children)
  2467. (while args
  2468. (setq arg (car args)
  2469. args (cdr args)
  2470. answer (widget-match-inline arg value)
  2471. value (cdr answer))
  2472. (and (eq (preceding-char) ?\n)
  2473. (widget-get widget :indent)
  2474. (insert-char ?\s (widget-get widget :indent)))
  2475. (push (cond ((null answer)
  2476. (widget-create-child widget arg))
  2477. ((widget-get arg :inline)
  2478. (widget-create-child-value widget arg (car answer)))
  2479. (t
  2480. (widget-create-child-value widget arg (car (car answer)))))
  2481. children))
  2482. (widget-put widget :children (nreverse children))))
  2483. (defun widget-group-default-get (widget)
  2484. ;; Get the default of the components.
  2485. (mapcar 'widget-default-get (widget-get widget :args)))
  2486. (defun widget-group-match (widget vals)
  2487. ;; Match if the components match.
  2488. (and (listp vals)
  2489. (let ((match (widget-group-match-inline widget vals)))
  2490. (and match (null (cdr match))))))
  2491. (defun widget-group-match-inline (widget vals)
  2492. ;; Match if the components match.
  2493. (let ((args (widget-get widget :args))
  2494. argument answer found)
  2495. (while args
  2496. (setq argument (car args)
  2497. args (cdr args))
  2498. (if (setq answer (widget-match-inline argument vals))
  2499. (setq found (append found (car answer))
  2500. vals (cdr answer))
  2501. (setq vals nil
  2502. args nil)))
  2503. (if answer
  2504. (cons found vals))))
  2505. ;;; The `visibility' Widget.
  2506. (define-widget 'visibility 'item
  2507. "An indicator and manipulator for hidden items.
  2508. The following properties have special meanings for this widget:
  2509. :on-glyph Image filename or spec to display when the item is visible.
  2510. :on Text shown if the \"on\" image is nil or cannot be displayed.
  2511. :off-glyph Image filename or spec to display when the item is hidden.
  2512. :off Text shown if the \"off\" image is nil cannot be displayed."
  2513. :format "%[%v%]"
  2514. :button-prefix ""
  2515. :button-suffix ""
  2516. :on-glyph "down"
  2517. :on "Hide"
  2518. :off-glyph "right"
  2519. :off "Show"
  2520. :value-create 'widget-visibility-value-create
  2521. :action 'widget-toggle-action
  2522. :match (lambda (_widget _value) t))
  2523. (defalias 'widget-visibility-value-create 'widget-toggle-value-create)
  2524. ;;; The `documentation-link' Widget.
  2525. ;;
  2526. ;; This is a helper widget for `documentation-string'.
  2527. (define-widget 'documentation-link 'link
  2528. "Link type used in documentation strings."
  2529. :tab-order -1
  2530. :help-echo "Describe this symbol"
  2531. :action 'widget-documentation-link-action)
  2532. (defun widget-documentation-link-action (widget &optional _event)
  2533. "Display documentation for WIDGET's value. Ignore optional argument EVENT."
  2534. (let* ((string (widget-get widget :value))
  2535. (symbol (intern string)))
  2536. (if (and (fboundp symbol) (boundp symbol))
  2537. ;; If there are two doc strings, give the user a way to pick one.
  2538. (apropos (concat "\\`" (regexp-quote string) "\\'"))
  2539. (cond
  2540. ((fboundp symbol)
  2541. (describe-function symbol))
  2542. ((facep symbol)
  2543. (describe-face symbol))
  2544. ((featurep symbol)
  2545. (describe-package symbol))
  2546. ((or (boundp symbol) (get symbol 'variable-documentation))
  2547. (describe-variable symbol))
  2548. (t
  2549. (message "No documentation available for %s" symbol))))))
  2550. (defcustom widget-documentation-links t
  2551. "Add hyperlinks to documentation strings when non-nil."
  2552. :type 'boolean
  2553. :group 'widget-documentation)
  2554. (defcustom widget-documentation-link-regexp "['`‘]\\([^\n `'‘’]+\\)['’]"
  2555. "Regexp for matching potential links in documentation strings.
  2556. The first group should be the link itself."
  2557. :type 'regexp
  2558. :group 'widget-documentation)
  2559. (defcustom widget-documentation-link-p 'intern-soft
  2560. "Predicate used to test if a string is useful as a link.
  2561. The value should be a function. The function will be called with one
  2562. argument, a string, and should return non-nil if there should be a
  2563. link for that string."
  2564. :type 'function
  2565. :options '(widget-documentation-link-p)
  2566. :group 'widget-documentation)
  2567. (defcustom widget-documentation-link-type 'documentation-link
  2568. "Widget type used for links in documentation strings."
  2569. :type 'symbol
  2570. :group 'widget-documentation)
  2571. (defun widget-documentation-link-add (widget from to)
  2572. (widget-specify-doc widget from to)
  2573. (when widget-documentation-links
  2574. (let ((regexp widget-documentation-link-regexp)
  2575. (buttons (widget-get widget :buttons))
  2576. (widget-mouse-face (default-value 'widget-mouse-face))
  2577. (widget-button-face widget-documentation-face)
  2578. (widget-button-pressed-face widget-documentation-face))
  2579. (save-excursion
  2580. (goto-char from)
  2581. (while (re-search-forward regexp to t)
  2582. (let ((name (match-string 1))
  2583. (begin (match-beginning 1))
  2584. (end (match-end 1)))
  2585. (when (funcall widget-documentation-link-p name)
  2586. (push (widget-convert-button widget-documentation-link-type
  2587. begin end :value name)
  2588. buttons)))))
  2589. (widget-put widget :buttons buttons))))
  2590. ;;; The `documentation-string' Widget.
  2591. (define-widget 'documentation-string 'item
  2592. "A documentation string."
  2593. :format "%v"
  2594. :action 'widget-documentation-string-action
  2595. :value-create 'widget-documentation-string-value-create
  2596. :visibility-widget 'visibility)
  2597. (defun widget-documentation-string-value-create (widget)
  2598. ;; Insert documentation string.
  2599. (let ((doc (substitute-command-keys (widget-value widget)))
  2600. (indent (widget-get widget :indent))
  2601. (shown (widget-get (widget-get widget :parent) :documentation-shown))
  2602. (start (point)))
  2603. (if (string-match "\n" doc)
  2604. (let ((before (substring doc 0 (match-beginning 0)))
  2605. (after (substring doc (match-end 0)))
  2606. button end)
  2607. (widget-documentation-string-indent-to indent)
  2608. (insert before ?\s)
  2609. (widget-documentation-link-add widget start (point))
  2610. (setq button
  2611. (widget-create-child-and-convert
  2612. widget (widget-get widget :visibility-widget)
  2613. :help-echo "Show or hide rest of the documentation."
  2614. :on "Hide"
  2615. :off "More"
  2616. :always-active t
  2617. :action 'widget-parent-action
  2618. shown))
  2619. (when shown
  2620. (insert ?\n)
  2621. (setq start (point))
  2622. (when (and indent (not (zerop indent)))
  2623. (insert-char ?\s indent))
  2624. (insert after)
  2625. (setq end (point))
  2626. (widget-documentation-link-add widget start end)
  2627. ;; Indent the subsequent lines.
  2628. (when (and indent (> indent 0))
  2629. (save-excursion
  2630. (save-restriction
  2631. (narrow-to-region start end)
  2632. (goto-char (point-min))
  2633. (while (search-forward "\n" nil t)
  2634. (widget-documentation-string-indent-to indent))))))
  2635. (widget-put widget :buttons (list button)))
  2636. (widget-documentation-string-indent-to indent)
  2637. (insert doc)
  2638. (widget-documentation-link-add widget start (point))))
  2639. (insert ?\n))
  2640. (defun widget-documentation-string-indent-to (col)
  2641. (when (and (numberp col)
  2642. (> col 0))
  2643. (let ((opoint (point)))
  2644. (indent-to col)
  2645. (put-text-property opoint (point)
  2646. 'display `(space :align-to ,col)))))
  2647. (defun widget-documentation-string-action (widget &rest _ignore)
  2648. ;; Toggle documentation.
  2649. (let ((parent (widget-get widget :parent)))
  2650. (widget-put parent :documentation-shown
  2651. (not (widget-get parent :documentation-shown))))
  2652. ;; Redraw.
  2653. (widget-value-set widget (widget-value widget)))
  2654. (defun widget-add-documentation-string-button (widget &rest args)
  2655. "Insert a new `documentation-string' widget based on WIDGET.
  2656. The new widget becomes a child of WIDGET, and is also added to
  2657. its `:buttons' list. The documentation string is found from
  2658. WIDGET using the function `widget-docstring'.
  2659. Optional ARGS specifies additional keyword arguments for the
  2660. `documentation-string' widget."
  2661. (let ((doc (widget-docstring widget))
  2662. (indent (widget-get widget :indent))
  2663. (doc-indent (widget-get widget :documentation-indent)))
  2664. (when doc
  2665. (and (eq (preceding-char) ?\n)
  2666. indent
  2667. (insert-char ?\s indent))
  2668. (unless (or (numberp doc-indent) (null doc-indent))
  2669. (setq doc-indent 0))
  2670. (widget-put widget :buttons
  2671. (cons (apply 'widget-create-child-and-convert
  2672. widget 'documentation-string
  2673. :indent doc-indent
  2674. (nconc args (list doc)))
  2675. (widget-get widget :buttons))))))
  2676. ;;; The Sexp Widgets.
  2677. (define-widget 'const 'item
  2678. "An immutable sexp."
  2679. :prompt-value 'widget-const-prompt-value
  2680. :format "%t\n%d")
  2681. (defun widget-const-prompt-value (widget _prompt _value _unbound)
  2682. ;; Return the value of the const.
  2683. (widget-value widget))
  2684. (define-widget 'function-item 'const
  2685. "An immutable function name."
  2686. :format "%v\n%h"
  2687. :documentation-property (lambda (symbol)
  2688. (condition-case nil
  2689. (documentation symbol t)
  2690. (error nil))))
  2691. (define-widget 'variable-item 'const
  2692. "An immutable variable name."
  2693. :format "%v\n%h"
  2694. :documentation-property 'variable-documentation)
  2695. (define-widget 'other 'sexp
  2696. "Matches any value, but doesn't let the user edit the value.
  2697. This is useful as last item in a `choice' widget.
  2698. You should use this widget type with a default value,
  2699. as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
  2700. If the user selects this alternative, that specifies DEFAULT
  2701. as the value."
  2702. :tag "Other"
  2703. :format "%t%n"
  2704. :value 'other)
  2705. (defvar widget-string-prompt-value-history nil
  2706. "History of input to `widget-string-prompt-value'.")
  2707. (define-widget 'string 'editable-field
  2708. "A string"
  2709. :tag "String"
  2710. :format "%{%t%}: %v"
  2711. :complete-function 'ispell-complete-word
  2712. :prompt-history 'widget-string-prompt-value-history)
  2713. (define-widget 'regexp 'string
  2714. "A regular expression."
  2715. :match 'widget-regexp-match
  2716. :validate 'widget-regexp-validate
  2717. ;; Doesn't work well with terminating newline.
  2718. ;; :value-face 'widget-single-line-field
  2719. :tag "Regexp")
  2720. (defun widget-regexp-match (_widget value)
  2721. ;; Match valid regexps.
  2722. (and (stringp value)
  2723. (condition-case nil
  2724. (prog1 t
  2725. (string-match value ""))
  2726. (error nil))))
  2727. (defun widget-regexp-validate (widget)
  2728. "Check that the value of WIDGET is a valid regexp."
  2729. (condition-case data
  2730. (prog1 nil
  2731. (string-match (widget-value widget) ""))
  2732. (error (widget-put widget :error (error-message-string data))
  2733. widget)))
  2734. (define-widget 'file 'string
  2735. "A file widget.
  2736. It reads a file name from an editable text field."
  2737. :completions #'completion-file-name-table
  2738. :prompt-value 'widget-file-prompt-value
  2739. :format "%{%t%}: %v"
  2740. ;; Doesn't work well with terminating newline.
  2741. ;; :value-face 'widget-single-line-field
  2742. :tag "File")
  2743. (defun widget-file-prompt-value (widget prompt value unbound)
  2744. ;; Read file from minibuffer.
  2745. (abbreviate-file-name
  2746. (if unbound
  2747. (read-file-name prompt)
  2748. (let ((prompt2 (format "%s (default %s): " prompt value))
  2749. (dir (file-name-directory value))
  2750. (file (file-name-nondirectory value))
  2751. (must-match (widget-get widget :must-match)))
  2752. (read-file-name prompt2 dir nil must-match file)))))
  2753. ;;;(defun widget-file-action (widget &optional event)
  2754. ;;; ;; Read a file name from the minibuffer.
  2755. ;;; (let* ((value (widget-value widget))
  2756. ;;; (dir (file-name-directory value))
  2757. ;;; (file (file-name-nondirectory value))
  2758. ;;; (menu-tag (widget-apply widget :menu-tag-get))
  2759. ;;; (must-match (widget-get widget :must-match))
  2760. ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
  2761. ;;; dir nil must-match file)))
  2762. ;;; (widget-value-set widget (abbreviate-file-name answer))
  2763. ;;; (widget-setup)
  2764. ;;; (widget-apply widget :notify widget event)))
  2765. ;; Fixme: use file-name-as-directory.
  2766. (define-widget 'directory 'file
  2767. "A directory widget.
  2768. It reads a directory name from an editable text field."
  2769. :tag "Directory")
  2770. (defvar widget-symbol-prompt-value-history nil
  2771. "History of input to `widget-symbol-prompt-value'.")
  2772. (define-widget 'symbol 'editable-field
  2773. "A Lisp symbol."
  2774. :value nil
  2775. :tag "Symbol"
  2776. :format "%{%t%}: %v"
  2777. :match (lambda (_widget value) (symbolp value))
  2778. :completions obarray
  2779. :prompt-internal 'widget-symbol-prompt-internal
  2780. :prompt-match 'symbolp
  2781. :prompt-history 'widget-symbol-prompt-value-history
  2782. :value-to-internal (lambda (_widget value)
  2783. (if (symbolp value)
  2784. (symbol-name value)
  2785. value))
  2786. :value-to-external (lambda (_widget value)
  2787. (if (stringp value)
  2788. (intern value)
  2789. value)))
  2790. (defun widget-symbol-prompt-internal (widget prompt initial history)
  2791. ;; Read file from minibuffer.
  2792. (let ((answer (completing-read prompt obarray
  2793. (widget-get widget :prompt-match)
  2794. nil initial history)))
  2795. (if (and (stringp answer)
  2796. (not (zerop (length answer))))
  2797. answer
  2798. (error "No value"))))
  2799. (defvar widget-function-prompt-value-history nil
  2800. "History of input to `widget-function-prompt-value'.")
  2801. (define-widget 'function 'restricted-sexp
  2802. "A Lisp function."
  2803. :completions (apply-partially #'completion-table-with-predicate
  2804. obarray #'fboundp 'strict)
  2805. :prompt-value 'widget-field-prompt-value
  2806. :prompt-internal 'widget-symbol-prompt-internal
  2807. :prompt-match 'fboundp
  2808. :prompt-history 'widget-function-prompt-value-history
  2809. :action 'widget-field-action
  2810. :match-alternatives '(functionp)
  2811. :validate (lambda (widget)
  2812. (unless (functionp (widget-value widget))
  2813. (widget-put widget :error (format "Invalid function: %S"
  2814. (widget-value widget)))
  2815. widget))
  2816. :value 'ignore
  2817. :tag "Function")
  2818. (defvar widget-variable-prompt-value-history nil
  2819. "History of input to `widget-variable-prompt-value'.")
  2820. (define-widget 'variable 'symbol
  2821. "A Lisp variable."
  2822. :prompt-match 'boundp
  2823. :prompt-history 'widget-variable-prompt-value-history
  2824. :completions (apply-partially #'completion-table-with-predicate
  2825. obarray #'boundp 'strict)
  2826. :tag "Variable")
  2827. (define-widget 'coding-system 'symbol
  2828. "A MULE coding-system."
  2829. :format "%{%t%}: %v"
  2830. :tag "Coding system"
  2831. :base-only nil
  2832. :prompt-history 'coding-system-value-history
  2833. :prompt-value 'widget-coding-system-prompt-value
  2834. :action 'widget-coding-system-action
  2835. :completions (apply-partially #'completion-table-with-predicate
  2836. obarray #'coding-system-p 'strict)
  2837. :validate (lambda (widget)
  2838. (unless (coding-system-p (widget-value widget))
  2839. (widget-put widget :error (format "Invalid coding system: %S"
  2840. (widget-value widget)))
  2841. widget))
  2842. :value 'undecided
  2843. :prompt-match 'coding-system-p)
  2844. (defun widget-coding-system-prompt-value (widget prompt value _unbound)
  2845. "Read coding-system from minibuffer."
  2846. (if (widget-get widget :base-only)
  2847. (intern
  2848. (completing-read (format "%s (default %s): " prompt value)
  2849. (mapcar #'list (coding-system-list t)) nil nil nil
  2850. coding-system-history))
  2851. (read-coding-system (format "%s (default %s): " prompt value) value)))
  2852. (defun widget-coding-system-action (widget &optional event)
  2853. (let ((answer
  2854. (widget-coding-system-prompt-value
  2855. widget
  2856. (widget-apply widget :menu-tag-get)
  2857. (widget-value widget)
  2858. t)))
  2859. (widget-value-set widget answer)
  2860. (widget-apply widget :notify widget event)
  2861. (widget-setup)))
  2862. ;;; I'm not sure about what this is good for? KFS.
  2863. (defvar widget-key-sequence-prompt-value-history nil
  2864. "History of input to `widget-key-sequence-prompt-value'.")
  2865. (defvar widget-key-sequence-default-value [ignore]
  2866. "Default value for an empty key sequence.")
  2867. (defvar widget-key-sequence-map
  2868. (let ((map (make-sparse-keymap)))
  2869. (set-keymap-parent map widget-field-keymap)
  2870. (define-key map [(control ?q)] 'widget-key-sequence-read-event)
  2871. map))
  2872. (define-widget 'key-sequence 'restricted-sexp
  2873. "A key sequence."
  2874. :prompt-value 'widget-field-prompt-value
  2875. :prompt-internal 'widget-symbol-prompt-internal
  2876. ; :prompt-match 'fboundp ;; What was this good for? KFS
  2877. :prompt-history 'widget-key-sequence-prompt-value-history
  2878. :action 'widget-field-action
  2879. :match-alternatives '(stringp vectorp)
  2880. :format "%{%t%}: %v"
  2881. :validate 'widget-key-sequence-validate
  2882. :value-to-internal 'widget-key-sequence-value-to-internal
  2883. :value-to-external 'widget-key-sequence-value-to-external
  2884. :value widget-key-sequence-default-value
  2885. :keymap widget-key-sequence-map
  2886. :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
  2887. :tag "Key sequence")
  2888. (defun widget-key-sequence-read-event (ev)
  2889. (interactive (list
  2890. (let ((inhibit-quit t) quit-flag)
  2891. (read-event "Insert KEY, EVENT, or CODE: "))))
  2892. (let ((ev2 (and (memq 'down (event-modifiers ev))
  2893. (read-event)))
  2894. (tr (and (keymapp function-key-map)
  2895. (lookup-key function-key-map (vector ev)))))
  2896. (when (and (integerp ev)
  2897. (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
  2898. (and (<= ?a (downcase ev))
  2899. (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
  2900. (setq unread-command-events (cons ev unread-command-events)
  2901. ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
  2902. tr nil)
  2903. (if (and (integerp ev) (not (characterp ev)))
  2904. (insert (char-to-string ev)))) ;; throw invalid char error
  2905. (setq ev (key-description (list ev)))
  2906. (when (arrayp tr)
  2907. (setq tr (key-description (list (aref tr 0))))
  2908. (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
  2909. (setq ev tr ev2 nil)))
  2910. (insert (if (= (char-before) ?\s) "" " ") ev " ")
  2911. (if ev2
  2912. (insert (key-description (list ev2)) " "))))
  2913. (defun widget-key-sequence-validate (widget)
  2914. (unless (or (stringp (widget-value widget))
  2915. (vectorp (widget-value widget)))
  2916. (widget-put widget :error (format "Invalid key sequence: %S"
  2917. (widget-value widget)))
  2918. widget))
  2919. (defun widget-key-sequence-value-to-internal (widget value)
  2920. (if (widget-apply widget :match value)
  2921. (if (equal value widget-key-sequence-default-value)
  2922. ""
  2923. (key-description value))
  2924. value))
  2925. (defun widget-key-sequence-value-to-external (_widget value)
  2926. (if (stringp value)
  2927. (if (string-match "\\`[[:space:]]*\\'" value)
  2928. widget-key-sequence-default-value
  2929. (read-kbd-macro value))
  2930. value))
  2931. (define-widget 'sexp 'editable-field
  2932. "An arbitrary Lisp expression."
  2933. :tag "Lisp expression"
  2934. :format "%{%t%}: %v"
  2935. :value nil
  2936. :validate 'widget-sexp-validate
  2937. :match (lambda (_widget _value) t)
  2938. :value-to-internal 'widget-sexp-value-to-internal
  2939. :value-to-external (lambda (_widget value) (read value))
  2940. :prompt-history 'widget-sexp-prompt-value-history
  2941. :prompt-value 'widget-sexp-prompt-value)
  2942. (defun widget-sexp-value-to-internal (_widget value)
  2943. ;; Use pp for printer representation.
  2944. (let ((pp (if (symbolp value)
  2945. (prin1-to-string value)
  2946. (pp-to-string value))))
  2947. (while (string-match "\n\\'" pp)
  2948. (setq pp (substring pp 0 -1)))
  2949. (if (or (string-match "\n\\'" pp)
  2950. (> (length pp) 40))
  2951. (concat "\n" pp)
  2952. pp)))
  2953. (defun widget-sexp-validate (widget)
  2954. ;; Valid if we can read the string and there is no junk left after it.
  2955. (with-temp-buffer
  2956. (insert (widget-apply widget :value-get))
  2957. (goto-char (point-min))
  2958. (let (err)
  2959. (condition-case data ;Note: We get a spurious byte-compile warning here.
  2960. (progn
  2961. ;; Avoid a confusing end-of-file error.
  2962. (skip-syntax-forward "\\s-")
  2963. (if (eobp)
  2964. (setq err "Empty sexp -- use nil?")
  2965. (unless (widget-apply widget :match (read (current-buffer)))
  2966. (setq err (widget-get widget :type-error))))
  2967. ;; Allow whitespace after expression.
  2968. (skip-syntax-forward "\\s-")
  2969. (if (and (not (eobp))
  2970. (not err))
  2971. (setq err (format "Junk at end of expression: %s"
  2972. (buffer-substring (point)
  2973. (point-max))))))
  2974. (end-of-file ; Avoid confusing error message.
  2975. (setq err "Unbalanced sexp"))
  2976. (error (setq err (error-message-string data))))
  2977. (if (not err)
  2978. nil
  2979. (widget-put widget :error err)
  2980. widget))))
  2981. (defvar widget-sexp-prompt-value-history nil
  2982. "History of input to `widget-sexp-prompt-value'.")
  2983. (defun widget-sexp-prompt-value (widget prompt value unbound)
  2984. ;; Read an arbitrary sexp.
  2985. (let ((found (read-string prompt
  2986. (if unbound nil (cons (prin1-to-string value) 0))
  2987. (widget-get widget :prompt-history))))
  2988. (let ((answer (read-from-string found)))
  2989. (unless (= (cdr answer) (length found))
  2990. (error "Junk at end of expression: %s"
  2991. (substring found (cdr answer))))
  2992. (car answer))))
  2993. (define-widget 'restricted-sexp 'sexp
  2994. "A Lisp expression restricted to values that match.
  2995. To use this type, you must define :match or :match-alternatives."
  2996. :type-error "The specified value is not valid"
  2997. :match 'widget-restricted-sexp-match
  2998. :value-to-internal (lambda (widget value)
  2999. (if (widget-apply widget :match value)
  3000. (prin1-to-string value)
  3001. value)))
  3002. (defun widget-restricted-sexp-match (widget value)
  3003. (let ((alternatives (widget-get widget :match-alternatives))
  3004. matched)
  3005. (while (and alternatives (not matched))
  3006. (if (cond ((functionp (car alternatives))
  3007. (funcall (car alternatives) value))
  3008. ((and (consp (car alternatives))
  3009. (eq (car (car alternatives)) 'quote))
  3010. (eq value (nth 1 (car alternatives)))))
  3011. (setq matched t))
  3012. (setq alternatives (cdr alternatives)))
  3013. matched))
  3014. (define-widget 'integer 'restricted-sexp
  3015. "An integer."
  3016. :tag "Integer"
  3017. :value 0
  3018. :type-error "This field should contain an integer"
  3019. :match-alternatives '(integerp))
  3020. (define-widget 'number 'restricted-sexp
  3021. "A number (floating point or integer)."
  3022. :tag "Number"
  3023. :value 0.0
  3024. :type-error "This field should contain a number (floating point or integer)"
  3025. :match-alternatives '(numberp))
  3026. (define-widget 'float 'restricted-sexp
  3027. "A floating point number."
  3028. :tag "Floating point number"
  3029. :value 0.0
  3030. :type-error "This field should contain a floating point number"
  3031. :match-alternatives '(floatp))
  3032. (define-widget 'character 'editable-field
  3033. "A character."
  3034. :tag "Character"
  3035. :value 0
  3036. :size 1
  3037. :format "%{%t%}: %v\n"
  3038. :valid-regexp "\\`.\\'"
  3039. :error "This field should contain a single character"
  3040. :value-get (lambda (w) (widget-field-value-get w t))
  3041. :value-to-internal (lambda (_widget value)
  3042. (if (stringp value)
  3043. value
  3044. (char-to-string value)))
  3045. :value-to-external (lambda (_widget value)
  3046. (if (stringp value)
  3047. (aref value 0)
  3048. value))
  3049. :match (lambda (_widget value)
  3050. (characterp value)))
  3051. (define-widget 'list 'group
  3052. "A Lisp list."
  3053. :tag "List"
  3054. :format "%{%t%}:\n%v")
  3055. (define-widget 'vector 'group
  3056. "A Lisp vector."
  3057. :tag "Vector"
  3058. :format "%{%t%}:\n%v"
  3059. :match 'widget-vector-match
  3060. :value-to-internal (lambda (_widget value) (append value nil))
  3061. :value-to-external (lambda (_widget value) (apply 'vector value)))
  3062. (defun widget-vector-match (widget value)
  3063. (and (vectorp value)
  3064. (widget-group-match widget
  3065. (widget-apply widget :value-to-internal value))))
  3066. (define-widget 'cons 'group
  3067. "A cons-cell."
  3068. :tag "Cons-cell"
  3069. :format "%{%t%}:\n%v"
  3070. :match 'widget-cons-match
  3071. :value-to-internal (lambda (_widget value)
  3072. (list (car value) (cdr value)))
  3073. :value-to-external (lambda (_widget value)
  3074. (apply 'cons value)))
  3075. (defun widget-cons-match (widget value)
  3076. (and (consp value)
  3077. (widget-group-match widget
  3078. (widget-apply widget :value-to-internal value))))
  3079. ;;; The `lazy' Widget.
  3080. ;;
  3081. ;; Recursive datatypes.
  3082. (define-widget 'lazy 'default
  3083. "Base widget for recursive data structures.
  3084. The `lazy' widget will, when instantiated, contain a single inferior
  3085. widget, of the widget type specified by the :type parameter. The
  3086. value of the `lazy' widget is the same as the value of the inferior
  3087. widget. When deriving a new widget from the 'lazy' widget, the :type
  3088. parameter is allowed to refer to the widget currently being defined,
  3089. thus allowing recursive data structures to be described.
  3090. The :type parameter takes the same arguments as the defcustom
  3091. parameter with the same name.
  3092. Most composite widgets, i.e. widgets containing other widgets, does
  3093. not allow recursion. That is, when you define a new widget type, none
  3094. of the inferior widgets may be of the same type you are currently
  3095. defining.
  3096. In Lisp, however, it is custom to define data structures in terms of
  3097. themselves. A list, for example, is defined as either nil, or a cons
  3098. cell whose cdr itself is a list. The obvious way to translate this
  3099. into a widget type would be
  3100. (define-widget \\='my-list \\='choice
  3101. \"A list of sexps.\"
  3102. :tag \"Sexp list\"
  3103. :args \\='((const nil) (cons :value (nil) sexp my-list)))
  3104. Here we attempt to define my-list as a choice of either the constant
  3105. nil, or a cons-cell containing a sexp and my-lisp. This will not work
  3106. because the `choice' widget does not allow recursion.
  3107. Using the `lazy' widget you can overcome this problem, as in this
  3108. example:
  3109. (define-widget \\='sexp-list \\='lazy
  3110. \"A list of sexps.\"
  3111. :tag \"Sexp list\"
  3112. :type \\='(choice (const nil) (cons :value (nil) sexp sexp-list)))"
  3113. :format "%{%t%}: %v"
  3114. ;; We don't convert :type because we want to allow recursive
  3115. ;; data structures. This is slow, so we should not create speed
  3116. ;; critical widgets by deriving from this.
  3117. :convert-widget 'widget-value-convert-widget
  3118. :value-create 'widget-type-value-create
  3119. :value-get 'widget-child-value-get
  3120. :value-inline 'widget-child-value-inline
  3121. :default-get 'widget-type-default-get
  3122. :match 'widget-type-match
  3123. :validate 'widget-child-validate)
  3124. ;;; The `plist' Widget.
  3125. ;;
  3126. ;; Property lists.
  3127. (define-widget 'plist 'list
  3128. "A property list."
  3129. :key-type '(symbol :tag "Key")
  3130. :value-type '(sexp :tag "Value")
  3131. :convert-widget 'widget-plist-convert-widget
  3132. :tag "Plist")
  3133. (defvar widget-plist-value-type) ;Dynamic variable
  3134. (defun widget-plist-convert-widget (widget)
  3135. ;; Handle `:options'.
  3136. (let* ((options (widget-get widget :options))
  3137. (widget-plist-value-type (widget-get widget :value-type))
  3138. (other `(editable-list :inline t
  3139. (group :inline t
  3140. ,(widget-get widget :key-type)
  3141. ,widget-plist-value-type)))
  3142. (args (if options
  3143. (list `(checklist :inline t
  3144. :greedy t
  3145. ,@(mapcar 'widget-plist-convert-option
  3146. options))
  3147. other)
  3148. (list other))))
  3149. (widget-put widget :args args)
  3150. widget))
  3151. (defun widget-plist-convert-option (option)
  3152. ;; Convert a single plist option.
  3153. (let (key-type value-type)
  3154. (if (listp option)
  3155. (let ((key (nth 0 option)))
  3156. (setq value-type (nth 1 option))
  3157. (if (listp key)
  3158. (setq key-type key)
  3159. (setq key-type `(const ,key))))
  3160. (setq key-type `(const ,option)
  3161. value-type widget-plist-value-type))
  3162. `(group :format "Key: %v" :inline t ,key-type ,value-type)))
  3163. ;;; The `alist' Widget.
  3164. ;;
  3165. ;; Association lists.
  3166. (define-widget 'alist 'list
  3167. "An association list."
  3168. :key-type '(sexp :tag "Key")
  3169. :value-type '(sexp :tag "Value")
  3170. :convert-widget 'widget-alist-convert-widget
  3171. :tag "Alist")
  3172. (defvar widget-alist-value-type) ;Dynamic variable
  3173. (defun widget-alist-convert-widget (widget)
  3174. ;; Handle `:options'.
  3175. (let* ((options (widget-get widget :options))
  3176. (widget-alist-value-type (widget-get widget :value-type))
  3177. (other `(editable-list :inline t
  3178. (cons :format "%v"
  3179. ,(widget-get widget :key-type)
  3180. ,widget-alist-value-type)))
  3181. (args (if options
  3182. (list `(checklist :inline t
  3183. :greedy t
  3184. ,@(mapcar 'widget-alist-convert-option
  3185. options))
  3186. other)
  3187. (list other))))
  3188. (widget-put widget :args args)
  3189. widget))
  3190. (defun widget-alist-convert-option (option)
  3191. ;; Convert a single alist option.
  3192. (let (key-type value-type)
  3193. (if (listp option)
  3194. (let ((key (nth 0 option)))
  3195. (setq value-type (nth 1 option))
  3196. (if (listp key)
  3197. (setq key-type key)
  3198. (setq key-type `(const ,key))))
  3199. (setq key-type `(const ,option)
  3200. value-type widget-alist-value-type))
  3201. `(cons :format "Key: %v" ,key-type ,value-type)))
  3202. (define-widget 'choice 'menu-choice
  3203. "A union of several sexp types."
  3204. :tag "Choice"
  3205. :format "%{%t%}: %[Value Menu%] %v"
  3206. :button-prefix 'widget-push-button-prefix
  3207. :button-suffix 'widget-push-button-suffix
  3208. :prompt-value 'widget-choice-prompt-value)
  3209. (defun widget-choice-prompt-value (widget prompt value _unbound)
  3210. "Make a choice."
  3211. (let ((args (widget-get widget :args))
  3212. (completion-ignore-case (widget-get widget :case-fold))
  3213. current choices old)
  3214. ;; Find the first arg that matches VALUE.
  3215. (let ((look args))
  3216. (while look
  3217. (if (widget-apply (car look) :match value)
  3218. (setq old (car look)
  3219. look nil)
  3220. (setq look (cdr look)))))
  3221. ;; Find new choice.
  3222. (setq current
  3223. (cond ((= (length args) 0)
  3224. nil)
  3225. ((= (length args) 1)
  3226. (nth 0 args))
  3227. ((and (= (length args) 2)
  3228. (memq old args))
  3229. (if (eq old (nth 0 args))
  3230. (nth 1 args)
  3231. (nth 0 args)))
  3232. (t
  3233. (while args
  3234. (setq current (car args)
  3235. args (cdr args))
  3236. (setq choices
  3237. (cons (cons (widget-apply current :menu-tag-get)
  3238. current)
  3239. choices)))
  3240. (let ((val (completing-read prompt choices nil t)))
  3241. (if (stringp val)
  3242. (let ((try (try-completion val choices)))
  3243. (when (stringp try)
  3244. (setq val try))
  3245. (cdr (assoc val choices)))
  3246. nil)))))
  3247. (if current
  3248. (widget-prompt-value current prompt nil t)
  3249. value)))
  3250. (define-widget 'radio 'radio-button-choice
  3251. "A union of several sexp types."
  3252. :tag "Choice"
  3253. :format "%{%t%}:\n%v"
  3254. :prompt-value 'widget-choice-prompt-value)
  3255. (define-widget 'repeat 'editable-list
  3256. "A variable length homogeneous list."
  3257. :tag "Repeat"
  3258. :format "%{%t%}:\n%v%i\n")
  3259. (define-widget 'set 'checklist
  3260. "A list of members from a fixed set."
  3261. :tag "Set"
  3262. :format "%{%t%}:\n%v")
  3263. (define-widget 'boolean 'toggle
  3264. "To be nil or non-nil, that is the question."
  3265. :tag "Boolean"
  3266. :prompt-value 'widget-boolean-prompt-value
  3267. :button-prefix 'widget-push-button-prefix
  3268. :button-suffix 'widget-push-button-suffix
  3269. :format "%{%t%}: %[Toggle%] %v\n"
  3270. :on "on (non-nil)"
  3271. :off "off (nil)")
  3272. (defun widget-boolean-prompt-value (_widget prompt _value _unbound)
  3273. ;; Toggle a boolean.
  3274. (y-or-n-p prompt))
  3275. ;;; The `color' Widget.
  3276. ;; Fixme: match
  3277. (define-widget 'color 'editable-field
  3278. "Choose a color name (with sample)."
  3279. :format "%{%t%}: %v (%{sample%})\n"
  3280. :value-create 'widget-color-value-create
  3281. :size 10
  3282. :tag "Color"
  3283. :value "black"
  3284. :completions (or facemenu-color-alist (defined-colors))
  3285. :sample-face-get 'widget-color-sample-face-get
  3286. :notify 'widget-color-notify
  3287. :action 'widget-color-action)
  3288. (defun widget-color-value-create (widget)
  3289. (widget-field-value-create widget)
  3290. (widget-insert " ")
  3291. (widget-create-child-and-convert
  3292. widget 'push-button
  3293. :tag " Choose " :action 'widget-color--choose-action)
  3294. (widget-insert " "))
  3295. (defun widget-color--choose-action (widget &optional _event)
  3296. (list-colors-display
  3297. nil nil
  3298. (let ((cbuf (current-buffer))
  3299. (wp (widget-get widget :parent)))
  3300. (lambda (color)
  3301. (when (buffer-live-p cbuf)
  3302. (widget-value-set wp color)
  3303. (let* ((buf (get-buffer "*Colors*"))
  3304. (win (get-buffer-window buf 0)))
  3305. (if win
  3306. (quit-window nil win)
  3307. (bury-buffer buf)))
  3308. (pop-to-buffer cbuf))))))
  3309. (defun widget-color-sample-face-get (widget)
  3310. (let* ((value (condition-case nil
  3311. (widget-value widget)
  3312. (error (widget-get widget :value)))))
  3313. (if (color-defined-p value)
  3314. (list (cons 'foreground-color value))
  3315. 'default)))
  3316. (defun widget-color-action (widget &optional event)
  3317. "Prompt for a color."
  3318. (let* ((tag (widget-apply widget :menu-tag-get))
  3319. (prompt (concat tag ": "))
  3320. (answer (facemenu-read-color prompt)))
  3321. (unless (zerop (length answer))
  3322. (widget-value-set widget answer)
  3323. (widget-setup)
  3324. (widget-apply widget :notify widget event))))
  3325. (defun widget-color-notify (widget child &optional event)
  3326. "Update the sample, and notify the parent."
  3327. (overlay-put (widget-get widget :sample-overlay)
  3328. 'face (widget-apply widget :sample-face-get))
  3329. (widget-default-notify widget child event))
  3330. ;;; The Help Echo
  3331. (defun widget-echo-help (pos)
  3332. "Display help-echo text for widget at POS."
  3333. (let* ((widget (widget-at pos))
  3334. (help-echo (and widget (widget-get widget :help-echo))))
  3335. (if (functionp help-echo)
  3336. (setq help-echo (funcall help-echo widget)))
  3337. (if help-echo (message "%s" (eval help-echo)))))
  3338. ;;; The End:
  3339. (provide 'wid-edit)
  3340. ;;; wid-edit.el ends here