wid-edit.el 123 KB

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