aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMauro Aranda2023-09-22 20:45:00 -0300
committerStefan Kangas2024-01-10 22:28:01 +0100
commit4fadbfe300a338f8e6e167331bc7ca0bbca26dbc (patch)
treed196049561d00a1d07731926b0d2fe377b9afeed
parent1bbb610821eb143e0828d2541a3f856d29d67b6f (diff)
downloademacs-4fadbfe300a338f8e6e167331bc7ca0bbca26dbc.tar.gz
emacs-4fadbfe300a338f8e6e167331bc7ca0bbca26dbc.zip
Add examples to the Widget manual
* doc/misc/widget.texi (Widget Gallery, Defining New Widgets): Add examples. (Bug#66229)
-rw-r--r--doc/misc/widget.texi206
1 files changed, 206 insertions, 0 deletions
diff --git a/doc/misc/widget.texi b/doc/misc/widget.texi
index 93b7606b01e..82d89449dd2 100644
--- a/doc/misc/widget.texi
+++ b/doc/misc/widget.texi
@@ -1384,6 +1384,15 @@ a specific way. If present, @var{value} is used to initialize the
1384@code{:value} property. When created, it inserts the value as a 1384@code{:value} property. When created, it inserts the value as a
1385string in the buffer. 1385string in the buffer.
1386 1386
1387@noindent
1388Example:
1389
1390@lisp
1391(widget-create 'item :tag "Today is" :format "%t: %v\n"
1392 (format-time-string "%d-%m-%Y"))
1393@end lisp
1394
1395
1387By default, it has the following properties: 1396By default, it has the following properties:
1388 1397
1389@table @code 1398@table @code
@@ -1428,6 +1437,20 @@ The @var{value}, if present, is used to initialize the @code{:value}
1428property. The value should be a string, which will be inserted in the 1437property. The value should be a string, which will be inserted in the
1429buffer. 1438buffer.
1430 1439
1440@noindent
1441Example:
1442
1443@lisp
1444(widget-create 'link
1445 :button-prefix ""
1446 :button-suffix ""
1447 :tag "Mail yourself"
1448 :action #'(lambda (widget &optional _event)
1449 (compose-mail-other-window (widget-value widget)))
1450 user-mail-address)
1451@end lisp
1452
1453
1431By default, it has the following properties: 1454By default, it has the following properties:
1432 1455
1433@table @code 1456@table @code
@@ -1471,6 +1494,29 @@ A widget to represent a link to a web page. Its super is the
1471It overrides the @code{:action} property to open up the @var{url} 1494It overrides the @code{:action} property to open up the @var{url}
1472specified. 1495specified.
1473 1496
1497@noindent
1498Example:
1499
1500@lisp
1501(widget-create 'url-link
1502 :button-prefix ""
1503 :button-suffix ""
1504 ;; Return appropriate face.
1505 :button-face-get (lambda (widget)
1506 (if (widget-get widget :visited)
1507 'link-visited
1508 'link))
1509 :format "%[%t%]"
1510 :tag "Browse this manual"
1511 :action (lambda (widget &optional _event)
1512 (widget-put widget :visited t)
1513 ;; Takes care of redrawing the widget.
1514 (widget-value-set widget (widget-value widget))
1515 ;; And then call the original function.
1516 (widget-url-link-action widget))
1517 "https://www.gnu.org/software/emacs/manual/html_mono/widget.html")
1518@end lisp
1519
1474@node info-link 1520@node info-link
1475@subsection The @code{info-link} Widget 1521@subsection The @code{info-link} Widget
1476@findex info-link@r{ widget} 1522@findex info-link@r{ widget}
@@ -1487,6 +1533,17 @@ A widget to represent a link to an info file. Its super is the
1487It overrides the @code{:action} property, to a function to start the 1533It overrides the @code{:action} property, to a function to start the
1488built-in Info reader on @var{address}, when invoked. 1534built-in Info reader on @var{address}, when invoked.
1489 1535
1536@noindent
1537Example:
1538
1539@lisp
1540(widget-create 'info-link
1541 :button-prefix ""
1542 :button-suffix ""
1543 :tag "Browse this manual"
1544 "(widget) info-link")))
1545@end lisp
1546
1490@node function-link 1547@node function-link
1491@subsection The @code{function-link} Widget 1548@subsection The @code{function-link} Widget
1492@findex function-link@r{ widget} 1549@findex function-link@r{ widget}
@@ -1502,6 +1559,17 @@ A widget to represent a link to an Emacs function. Its super is the
1502It overrides the @code{:action} property, to a function to describe 1559It overrides the @code{:action} property, to a function to describe
1503@var{function}. 1560@var{function}.
1504 1561
1562@noindent
1563Example:
1564
1565@lisp
1566(widget-create 'function-link
1567 :button-prefix ""
1568 :button-suffix ""
1569 :tag "Describe the function that gets called"
1570 #'widget-function-link-action)
1571@end lisp
1572
1505@node variable-link 1573@node variable-link
1506@subsection The @code{variable-link} Widget 1574@subsection The @code{variable-link} Widget
1507@findex variable-link@r{ widget} 1575@findex variable-link@r{ widget}
@@ -1517,6 +1585,17 @@ A widget to represent a link to an Emacs variable. Its super is the
1517It overrides the @code{:action} property, to a function to describe 1585It overrides the @code{:action} property, to a function to describe
1518@var{var}. 1586@var{var}.
1519 1587
1588@noindent
1589Example:
1590
1591@lisp
1592(widget-create 'variable-link
1593 :button-prefix ""
1594 :button-suffix ""
1595 :tag "What setting controlls button-prefix?"
1596 'widget-button-prefix)
1597@end lisp
1598
1520@node face-link 1599@node face-link
1521@subsection The @code{face-link} Widget 1600@subsection The @code{face-link} Widget
1522@findex face-link@r{ widget} 1601@findex face-link@r{ widget}
@@ -1532,6 +1611,17 @@ A widget to represent a link to an Emacs face. Its super is the
1532It overrides the @code{:action} property, to a function to describe 1611It overrides the @code{:action} property, to a function to describe
1533@var{face}. 1612@var{face}.
1534 1613
1614@noindent
1615Example:
1616
1617@lisp
1618(widget-create 'face-link
1619 :button-prefix ""
1620 :button-suffix ""
1621 :tag "Which face is this one?"
1622 'widget-button)
1623@end lisp
1624
1535@node file-link 1625@node file-link
1536@subsection The @code{file-link} Widget 1626@subsection The @code{file-link} Widget
1537@findex file-link@r{ widget} 1627@findex file-link@r{ widget}
@@ -1547,6 +1637,19 @@ A widget to represent a link to a file. Its super is the
1547It overrides the @code{:action} property, to a function to find the file 1637It overrides the @code{:action} property, to a function to find the file
1548@var{file}. 1638@var{file}.
1549 1639
1640@noindent
1641Example:
1642
1643@lisp
1644(let ((elisp-files (directory-files user-emacs-directory t ".el$")))
1645 (dolist (file elisp-files)
1646 (widget-create 'file-link
1647 :button-prefix ""
1648 :button-suffix ""
1649 file)
1650 (widget-insert "\n")))
1651@end lisp
1652
1550@node emacs-library-link 1653@node emacs-library-link
1551@subsection The @code{emacs-library-link} Widget 1654@subsection The @code{emacs-library-link} Widget
1552@findex emacs-library-link@r{ widget} 1655@findex emacs-library-link@r{ widget}
@@ -1562,6 +1665,17 @@ A widget to represent a link to an Emacs Lisp file. Its super is the
1562It overrides the @code{:action} property, to a function to find the file 1665It overrides the @code{:action} property, to a function to find the file
1563@var{file}. 1666@var{file}.
1564 1667
1668@noindent
1669Example:
1670
1671@lisp
1672(widget-create 'emacs-library-link
1673 :button-prefix ""
1674 :button-suffix ""
1675 :tag "Show yourself, Widget Library!"
1676 "wid-edit.el")
1677@end lisp
1678
1565@node emacs-commentary-link 1679@node emacs-commentary-link
1566@subsection The @code{emacs-commentary-link} Widget 1680@subsection The @code{emacs-commentary-link} Widget
1567@findex emacs-commentary-link@r{ widget} 1681@findex emacs-commentary-link@r{ widget}
@@ -1577,6 +1691,17 @@ file. Its super is the @code{link} widget.
1577It overrides the @code{:action} property, to a function to find the file 1691It overrides the @code{:action} property, to a function to find the file
1578@var{file} and put point in the Comment section. 1692@var{file} and put point in the Comment section.
1579 1693
1694@noindent
1695Example:
1696
1697@lisp
1698(widget-create 'emacs-commentary-link
1699 :button-prefix ""
1700 :button-suffix ""
1701 :tag "Check our good friend Customize"
1702 "cus-edit.el")
1703@end lisp
1704
1580@node push-button 1705@node push-button
1581@subsection The @code{push-button} Widget 1706@subsection The @code{push-button} Widget
1582@findex push-button@r{ widget} 1707@findex push-button@r{ widget}
@@ -2009,6 +2134,29 @@ A widget that can toggle between two states. Its super is the
2009The widget has two possible states, @samp{on} and @samp{off}, which 2134The widget has two possible states, @samp{on} and @samp{off}, which
2010correspond to a @code{t} or @code{nil} value, respectively. 2135correspond to a @code{t} or @code{nil} value, respectively.
2011 2136
2137@noindent
2138Example:
2139
2140@lisp
2141(widget-insert "Press the button to activate/deactivate the field: ")
2142(widget-create 'toggle
2143 :notify (lambda (widget &rest _ignored)
2144 (widget-apply widget-example-field
2145 (if (widget-value widget)
2146 :activate
2147 :deactivate))))
2148(widget-insert "\n")
2149(setq widget-example-field
2150 (widget-create 'editable-field
2151 :deactivate (lambda (widget)
2152 (widget-specify-inactive
2153 widget
2154 (widget-field-start widget)
2155 (widget-get widget :to)))))
2156(widget-apply widget-example-field :deactivate)))
2157@end lisp
2158
2159
2012It either overrides or adds the following properties: 2160It either overrides or adds the following properties:
2013 2161
2014@table @code 2162@table @code
@@ -2148,6 +2296,21 @@ The @var{type} arguments represent each checklist item. The widget's
2148value will be a list containing the values of all checked @var{type} 2296value will be a list containing the values of all checked @var{type}
2149arguments. 2297arguments.
2150 2298
2299@noindent
2300Example:
2301
2302@lisp
2303(widget-create 'checklist
2304 :notify (lambda (widget child &optional _event)
2305 (funcall
2306 (widget-value (widget-get-sibling child))
2307 'toggle))
2308 :value (list 'tool-bar-mode 'menu-bar-mode)
2309 '(item :tag "Tool-bar" tool-bar-mode)
2310 '(item :tag "Menu-bar" menu-bar-mode))))
2311@end lisp
2312
2313
2151It either overrides or adds the following properties: 2314It either overrides or adds the following properties:
2152 2315
2153@table @code 2316@table @code
@@ -2899,6 +3062,49 @@ The predefined functions @code{widget-types-convert-widget} and
2899@code{widget-value-convert-widget} can be used here. 3062@code{widget-value-convert-widget} can be used here.
2900@end table 3063@end table
2901 3064
3065@noindent
3066Example:
3067
3068@lisp
3069(defvar widget-ranged-integer-map
3070 (let ((map (copy-keymap widget-keymap)))
3071 (define-key map [up] #'widget-ranged-integer-increase)
3072 (define-key map [down] #'widget-ranged-integer-decrease)
3073 map))
3074
3075(define-widget 'ranged-integer 'integer
3076 "A ranged integer widget."
3077 :min-value most-negative-fixnum
3078 :max-value most-positive-fixnum
3079 :keymap widget-ranged-integer-map)
3080
3081(defun widget-ranged-integer-change (widget how)
3082 "Change the value of the ranged-integer WIDGET, according to HOW."
3083 (let* ((value (widget-value widget))
3084 (newval (cond
3085 ((eq how 'up)
3086 (if (< (1+ value) (widget-get widget :max-value))
3087 (1+ value)
3088 (widget-get widget :max-value)))
3089 ((eq how 'down)
3090 (if (> (1- value) (widget-get widget :min-value))
3091 (1- value)
3092 (widget-get widget :min-value)))
3093 (t (error "HOW has a bad value"))))
3094 (inhibit-read-only t))
3095 (widget-value-set widget newval)))
3096
3097(defun widget-ranged-integer-increase (widget)
3098 "Increase the value of the ranged-integer WIDGET."
3099 (interactive (list (widget-at)))
3100 (widget-ranged-integer-change widget 'up))
3101
3102(defun widget-ranged-integer-decrease (widget)
3103 "Decrease the value of the ranged-integer WIDGET."
3104 (interactive (list (widget-at)))
3105 (widget-ranged-integer-change widget 'down))
3106@end lisp
3107
2902@node Inspecting Widgets 3108@node Inspecting Widgets
2903@chapter Inspecting Widgets 3109@chapter Inspecting Widgets
2904@cindex widget browser 3110@cindex widget browser