summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/util/whelp.el
blob: 77b8149b554ea018c7dc7a1fe11d8a84576a0817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
;; This is a test file for some enhancement to the possibilities to
;; find out about widgets or buttons at point in a buffer.
;;
;; To use this just load the file. Then put point on a widget or
;; button and do
;;
;;    M-x describe-field
;;
;; You find a lot of widgets in a Custom buffer. You can find buttons
;; in for example a help buffer. (Please tell me more places so I can
;; test!)
;;
;; TODO: Add backtrace collecting to some more functions!

;; For widget-get-backtrace-info
;;(require 'debug)
(eval-when-compile (require 'cl))  ;; gensym
(require 'help-mode)

;; Last wins!
(require 'wid-browse)

(intern ":created-in-function")

(define-widget 'widget-browse-link 'item
  "Button for creating a link style button.
The :value of the widget shuld be the widget to be browsed."
  :format "%[%v%]"
  ;;:value-create 'widget-browse-value-create
  ;;:action 'widget-browse-action
  )

(defun define-button-type (name &rest properties)
  "Define a `button type' called NAME.
The remaining arguments form a sequence of PROPERTY VALUE pairs,
specifying properties to use as defaults for buttons with this type
\(a button's type may be set by giving it a `type' property when
creating the button, using the :type keyword argument).

In addition, the keyword argument :supertype may be used to specify a
button-type from which NAME inherits its default property values
\(however, the inheritance happens only when NAME is defined; subsequent
changes to a supertype are not reflected in its subtypes)."
  (let ((catsym (make-symbol (concat (symbol-name name) "-button")))
	(super-catsym
	 (button-category-symbol
	  (or (plist-get properties 'supertype)
	      (plist-get properties :supertype)
	      'button))))
    ;; Provide a link so that it's easy to find the real symbol.
    (put name 'button-category-symbol catsym)
    ;; Initialize NAME's properties using the global defaults.
    (let ((default-props (symbol-plist super-catsym))
          (where-fun (widget-get-backtrace-info 8)))
      (setq default-props
            (cons :created-in-function
                  (cons where-fun
                        default-props)))
      (while default-props
	(put catsym (pop default-props) (pop default-props))))
    ;; Add NAME as the `type' property, which will then be returned as
    ;; the type property of individual buttons.
    (put catsym 'type name)
    ;; Add the properties in PROPERTIES to the real symbol.
    (while properties
      (let ((prop (pop properties)))
	(when (eq prop :supertype)
	  (setq prop 'supertype))
	(put catsym prop (pop properties))))
    ;; Make sure there's a `supertype' property
    (unless (get catsym 'supertype)
      (put catsym 'supertype 'button))
    name))

(defun define-widget (name class doc &rest args)
  "Define a new widget type named NAME from CLASS.

NAME and CLASS should both be symbols, CLASS should be one of the
existing widget types, or nil to create the widget from scratch.

After the new widget has been defined, the following two calls will
create identical widgets:

* (widget-create NAME)

* (apply 'widget-create CLASS ARGS)

The third argument DOC is a documentation string for the widget."
  (put name 'widget-type (cons class args))
  (put name 'widget-documentation doc)
  (put name :created-in-function (widget-get-backtrace-info  8))
  name)

(defvar describe-temp-help-buffer nil)
(defun describe-get-temp-help-buffer ()
  (setq describe-temp-help-buffer (get-buffer-create "*Copy of *Help* Buffer for Description*")))

(defun describe-field (pos)
  "Describe field at marker POS."
  (interactive (list (point)))
  (unless (markerp pos) (setq pos (copy-marker pos)))
  (when (eq (marker-buffer pos) (get-buffer (help-buffer)))
    (with-current-buffer (describe-get-temp-help-buffer)
      (erase-buffer)
      (insert (with-current-buffer (help-buffer)
                (buffer-string)))
      (goto-char (marker-position pos))
      (setq pos (point-marker))))
  (let (field wbutton doc button widget)
    (with-current-buffer (marker-buffer pos)
      (setq field (get-char-property pos 'field))
      (setq wbutton (get-char-property pos 'button))
      (setq doc (get-char-property pos 'widget-doc))
      (setq button (button-at pos))
      (setq widget (or field wbutton doc)))
    (cond ((and widget
                (if (symbolp widget)
                    (get widget 'widget-type)
                  (and (consp widget)
                       (get (widget-type widget) 'widget-type))))
           (describe-widget pos))
          (button
           (describe-button pos))
          ((and (eq major-mode 'Info-mode)
                (memq (get-text-property pos 'font-lock-face)
                      '(info-xref info-xref-visited)))
           (message "info link"))
          (t
           (message "No widget or button at point")))))

(defun describe-insert-header (pos)
  (widget-insert
   (add-string-property
    (concat
     (format "Description of the field at position %d in "
             (marker-position pos))
     (format "\"%s\"" (marker-buffer pos))
     ":\n\n")
    'face '(italic))))

(defun describe-widget (pos)
  ;;(interactive (list (point-marker)))
  (unless (markerp pos) (setq pos (copy-marker pos)))
  (with-output-to-temp-buffer (help-buffer)
    (help-setup-xref (list #'describe-widget pos) (interactive-p))
    (with-current-buffer (help-buffer)
      (let ((inhibit-read-only t))
        (describe-insert-header pos)
        (insert-text-button "This field"
                            'action (lambda (button)
                                      (let* ((m (button-get button 'field-location))
                                             (p (marker-position m))
                                             (b (marker-buffer m)))
                                        (if (not (buffer-live-p b))
                                            (message "Sorry the markers buffer is gone")
                                          (switch-to-buffer b)
                                          (goto-char p))))
                            'field-location pos)
        (princ " is of type ")
        (insert-text-button "widget"
                            'action (lambda (button)
                                      (info "(widget)")))
        (princ ". You can ")
        (insert-text-button "browse the widget's properties"
                            'action (lambda (button)
                                      (widget-browse-at
                                       (button-get button 'field-location)))
                            'field-location pos))
      (princ " to find out more about it.")
      (fill-region (point-min) (point-max))
      )
    (with-no-warnings (print-help-return-message))))

(defun describe-button (pos)
  (let ((button (button-at pos)))
    (with-output-to-temp-buffer (help-buffer)
      (help-setup-xref (list #'describe-button pos) (interactive-p))
      (with-current-buffer (help-buffer)
        (let ((inhibit-read-only t)
              ;;(button-marker (gensym))
              )
          (describe-insert-header pos)
          (insert-text-button "This field"
                              'action (lambda (button)
                                        (let* ((m (button-get button 'field-location))
                                               (p (marker-position m))
                                               (b (marker-buffer m)))
                                          (switch-to-buffer b)
                                          (goto-char p)))
                              'field-location pos)
          (princ " is of type ")
          (insert-text-button "button"
                              'action (lambda (button)
                                        (info "(elisp) Buttons")))
          (princ ". You can ")
          ;;(set button-marker pos)
          (insert-text-button "browse the button's properties"
                              'action `(lambda (button)
                                         ;;(button-browse-at (symbol-value ',button-marker)))))
                                         (button-browse-at ,pos))))
        (princ " to find out more about it.")
        (fill-region (point-min) (point-max))
        )
      (with-no-warnings (print-help-return-message)))))

;; Obsolete
;; (defun whelp-describe-symbol (sym)
;;   (interactive "SSymbol: ")
;;   (with-output-to-temp-buffer (help-buffer)
;;     (help-setup-xref (list #'describe-symbol sym) (interactive-p))
;;     (with-current-buffer (help-buffer)
;;       (let ((inhibit-read-only t))
;;         (if (not (symbolp sym))
;;             (progn
;;               (princ "Argument does not look like it is a ")
;;               (insert-text-button "symbol"
;;                                   'action (lambda (button)
;;                                             (info "(elisp) Symbols")))
;;               (princ "."))
;;           (let ((n 0))
;;             (when (fboundp sym)        (setq n (1+ n)))
;;             (when (boundp sym)         (setq n (1+ n)))
;;             (when (facep sym)          (setq n (1+ n)))
;;             (when (custom-group-p sym) (setq n (1+ n)))
;;             (if (= n 0)
;;                 (progn
;;                   (princ "Can't determine usage for the ")
;;                   (insert-text-button "symbol"
;;                                       'action (lambda (button)
;;                                                 (info "(elisp) Symbols")))
;;                   (princ " '")
;;                   (princ (symbol-name sym))
;;                   (princ "."))
;;               (princ "The ")
;;               (insert-text-button "symbol"
;;                                   'action (lambda (button)
;;                                             (info "(elisp) Symbols")))
;;               (princ " '")
;;               (princ (symbol-name sym))
;;               (if (= n 1)
;;                   (progn
;;                     (princ " is a ")
;;                     (cond ((fboundp sym)
;;                            (princ "function (")
;;                            (insert-text-button
;;                             "describe it"
;;                             'action (lambda (button)
;;                                       (let ((value (button-get button 'value)))
;;                                         (describe-function value)))
;;                             'value sym)
;;                            (insert ")"))
;;                           ((boundp sym)
;;                            (insert "variable (")
;;                            (insert-text-button
;;                             "describe it"
;;                             'action (lambda (button)
;;                                       (let ((value (button-get button 'value)))
;;                                         (describe-variable value)))
;;                             'value sym)
;;                            (insert ")"))
;;                           ((facep sym)
;;                            (insert "face (")
;;                            (insert-text-button
;;                             "describe it"
;;                             'action (lambda (button)
;;                                       (let ((value (button-get button 'value)))
;;                                         (describe-face value)))
;;                             'value sym)
;;                            (insert ")"))
;;                           ((custom-group-p sym)
;;                            (insert "customize group (")
;;                            (insert-text-button
;;                             "customize it"
;;                             'action (lambda (button)
;;                                       (let ((value (button-get button 'value)))
;;                                         (customize-group value)))
;;                             'value sym)
;;                            (insert ")")))
;;                     (princ "."))
;;                 (princ " has several usages currently.")
;;                 (princ " It can be:\n\n")
;;                 (when (fboundp sym)
;;                   (princ "  - A function (")
;;                   (insert-text-button "describe it"
;;                                       'action (lambda (button)
;;                                                 (let ((value (button-get button 'value)))
;;                                                   (describe-function value)))
;;                                       'value sym)
;;                   (princ ")\n"))
;;                 (when (boundp sym)
;;                   (princ "  - A variable (")
;;                   (insert-text-button "describe it"
;;                                       'action (lambda (button)
;;                                                     (let ((value (button-get button 'value)))
;;                                                       (describe-variable value)))
;;                                       'value sym)
;;                   (princ ")\n"))
;;                 (when (facep sym)
;;                   (princ "  - A face (")
;;                   (insert-text-button "describe it"
;;                                       'action (lambda (button)
;;                                                 (let ((value (button-get button 'value)))
;;                                                   (describe-face value)))
;;                                       'value sym)
;;                   (princ ")\n"))
;;                 (when (custom-group-p sym)
;;                   (princ "  - A customization group (")
;;                   (insert-text-button "customize it"
;;                                       'action (lambda (button)
;;                                                 (let ((value (button-get button 'value)))
;;                                                   (customize-group value)))
;;                                       'value sym)
;;                   (princ ")\n"))
;;                 )))
;;           (princ "\n\nSymbol's property list:\n\n")
;;           (let ((pl (symbol-plist sym))
;;                 key
;;                 val)
;;             (princ (format "  %25s   %s\n" "Key" "Value"))
;;             (princ (format "  %25s   %s\n" "---" "-----"))
;;             (while pl
;;               (setq key (car pl))
;;               (setq pl (cdr pl))
;;                 (setq val (car pl))
;;                 (setq pl (cdr pl))
;;                 (let ((first (point-marker))
;;                       last)
;;                   (princ (format "  %25s - %s" key val))
;;                   (setq last (point-marker))
;;                   (let ((adaptive-fill-function
;;                          (lambda ()
;;                            (format "  %25s - " key))))
;;                     (fill-region first last)
;;                     ))
;;                 (princ "\n")
;;                 )))
;;         (with-no-warnings (print-help-return-message))))))



(defun widget-browse-sexp (widget key value)
  "Insert description of WIDGET's KEY VALUE.
Nothing is assumed about value."
  (let ((pp (condition-case signal
		(pp-to-string value)
	      (error (prin1-to-string signal)))))
    (when (string-match "\n\\'" pp)
      (setq pp (substring pp 0 (1- (length pp)))))
    (if (cond ((string-match "\n" pp)
	       nil)
	      ((> (length pp) (- (window-width) (current-column)))
	       nil)
	      (t t))
        (cond
         (  (and value
                 (symbolp value)
                 (or (fboundp value)
                     (boundp value)
                     (facep value)))
            (widget-create 'push-button
                           :tag pp
                           :value value
                           :action '(lambda (widget &optional event)
                                      (let ((value (widget-get widget :value))
                                            (n 0))
                                        (when (fboundp value) (setq n (1+ n)))
                                        (when (boundp value) (setq n (1+ n)))
                                        (when (facep value) (setq n (1+ n)))
                                        (if (= n 1)
                                            (cond ((fboundp value)
                                                   (describe-function value))
                                                  ((boundp value)
                                                   (describe-variable value))
                                                  ((facep value)
                                                   (describe-face value)))
                                          (describe-symbol value))))))
         (  (markerp value)
            (widget-create 'push-button
                           :tag pp
                           :value (list (marker-position value) (marker-buffer value))
                           :action '(lambda (widget &optional event)
                                      (let ((value (widget-get widget :value)))
                                        (let ((pos (car value))
                                              (buf (cadr value)))
                                          (switch-to-buffer-other-window buf)
                                          (goto-char pos))))))
         (  (overlayp value)
            (widget-create 'push-button
                           :tag pp
                           :value (list (overlay-start value) (overlay-buffer value))
                           :action '(lambda (widget &optional event)
                                      (let ((value (widget-get widget :value)))
                                        (let ((pos (car value))
                                              (buf (cadr value)))
                                          (switch-to-buffer-other-window buf)
                                          (goto-char pos))))))
         (  t
            (widget-insert pp)))

      (widget-create 'push-button
		     :tag "show"
		     :action (lambda (widget &optional event)
			       (with-output-to-temp-buffer
				   "*Pp Eval Output*"
				 (princ (widget-get widget :value))))
		     pp))))


(defvar widget-get-backtrace-active t
  "Whether to collect backtrace info for widgets and buttons.
Turn this on only for debugging purposes.

Note: This must be t when Emacs is loading to collect the needed
information.")

(defun widget-get-backtrace-info (n)
  (if widget-get-backtrace-active
      (let ((frame-n t)
            fun)
        (while (and frame-n
                    (not fun))
          (setq frame-n (backtrace-frame n))
          (when frame-n
            ;;(message "**BT %s:  %s" n (cadr frame-n))
            (when (car frame-n)
              (setq fun (cadr frame-n))
              (when (or (listp fun)
                        (member fun
                                '(
                                  backtrace-frame
                                  widget-get-backtrace-info

                                  eval
                                  eval-expression
                                  call-interactively
                                  apply
                                  funcall
                                  ;;lambda

                                  if
                                  when
                                  cond
                                  condition
                                  mapc
                                  mapcar
                                  while

                                  let
                                  let*
                                  set
                                  setq
                                  set-variable
                                  set-default

                                  widget-create
                                  widget-create-child-and-convert
                                  widget-create-child
                                  widget-create-child-value
                                  define-button-type
                                  define-widget
                                  make-text-button
                                  insert-text-button
                                  make-button
                                  insert-button
                                  )))
                (setq fun)))
            (setq n (1+ n))))
        ;;(message "---------- fun=%s" fun)
        fun)
    "Set widget-get-backtrace-info to show this"))

(defun widget-create (type &rest args)
  "Create widget of TYPE.
The optional ARGS are additional keyword arguments."
  (unless (keywordp :created-in-function) (error ":wcw not interned"))
  (let ((where-fun (widget-get-backtrace-info 8))
        yargs)
    (setq args
          (cons :created-in-function
                (cons where-fun
                      args)))
    (let ((widget (apply 'widget-convert type args)))
      (widget-apply widget :create)
      widget)))


(defun widget-create-child-and-convert (parent type &rest args)
  "As part of the widget PARENT, create a child widget TYPE.
The child is converted, using the keyword arguments ARGS."
  (let ((widget (apply 'widget-convert type args)))
    (widget-put widget :parent parent)
    (widget-put widget :created-in-function (widget-get-backtrace-info  15))
    (unless (widget-get widget :indent)
      (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
				    (or (widget-get widget :extra-offset) 0)
				    (widget-get parent :offset))))
    (widget-apply widget :create)
    widget))

(defun widget-create-child (parent type)
  "Create widget of TYPE."
  (let ((widget (widget-copy type)))
    (widget-put widget :parent parent)
    (widget-put widget :created-in-function (widget-get-backtrace-info  15))
    (unless (widget-get widget :indent)
      (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
				    (or (widget-get widget :extra-offset) 0)
				    (widget-get parent :offset))))
    (widget-apply widget :create)
    widget))

(defun widget-create-child-value (parent type value)
  "Create widget of TYPE with value VALUE."
  (let ((widget (widget-copy type)))
    (widget-put widget :value (widget-apply widget :value-to-internal value))
    (widget-put widget :parent parent)
    (widget-put widget :created-in-function (widget-get-backtrace-info  15))
    (unless (widget-get widget :indent)
      (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
				    (or (widget-get widget :extra-offset) 0)
				    (widget-get parent :offset))))
    (widget-apply widget :create)
    widget))

(defvar widget-browse-fb-history nil
  "Forward/backward history.")
(setq widget-browse-fb-history nil)

(defun widget-fb-button-action (widget &ignore)
  (let* ((num (widget-get widget :history-number))
         (rec (nth num widget-browse-fb-history))
         (fun (nth 0 rec))
         (val (nth 1 rec))
         (loc (nth 2 rec)))
    ;;(message "fun=%s, val=%s, loc=%s" fun val loc)(sit-for 4)
    (funcall fun num)))

(defun widget-insert-fb-buttons (current-number)
  ;;(message "current-number=%s" current-number)(sit-for 2)
  (if (<= 0 (1- current-number))
      (widget-create 'push-button
                     :action 'widget-fb-button-action
                     :history-number (1- current-number)
                     :format "%[%v%]"
                     "back")
    (widget-insert (add-string-property "[back]"
                                        'face 'shadow)))
  (widget-insert " ")
  (if (< (1+ current-number) (length widget-browse-fb-history))
      (widget-create 'push-button
                     :action 'widget-fb-button-action
                     :history-number (1+ current-number)
                     :format "%[%v%]"
                     "forward")
    (widget-insert (add-string-property "[forward]"
                                        'face 'shadow)))
  (widget-insert "\n"))

(defun widget-add-fb-history (elt)
  (let ((last (car widget-browse-fb-history)))
    (unless (equal elt last)
      (setq widget-browse-fb-history
            (reverse (cons elt
                           (reverse widget-browse-fb-history)))))))

(defun widget-browse (widget &optional location)
  "Create a widget browser for WIDGET."
  (interactive (list (completing-read "Widget: "
				      obarray
				      (lambda (symbol)
					(get symbol 'widget-type))
				      t nil 'widget-browse-history)))
  (let (history-number)
    (if (integerp widget)
        (progn
          ;;(message "was integer=%s" widget)(sit-for 2)
          (setq history-number widget)
          (setq widget (nth 1 (nth widget widget-browse-fb-history))))
      ;;(message "was NOT integer=%s" widget)(sit-for 2)
      (widget-add-fb-history (list 'widget-browse widget location))
      (setq history-number (1- (length widget-browse-fb-history))))
    ;;(message "history-number=%s" history-number)(sit-for 2)

    (if (stringp widget)
        (setq widget (intern widget)))
    (unless (if (symbolp widget)
                (get widget 'widget-type)
              (and (consp widget)
                   (get (widget-type widget) 'widget-type)))
      (error "Not a widget"))

    ;; Create the buffer.
    (if (symbolp widget)
        (let ((buffer (format "*Browse %s Widget*" widget)))
          (kill-buffer (get-buffer-create buffer))
          (switch-to-buffer (get-buffer-create buffer)))
      (kill-buffer (get-buffer-create "*Browse Widget*"))
      (switch-to-buffer (get-buffer-create "*Browse Widget*")))
    (widget-browse-mode)

    (make-local-variable 'widget-button-face)
    (setq widget-button-face 'link)
    (set (make-local-variable 'widget-push-button-prefix) "")
    (set (make-local-variable 'widget-push-button-suffix) "")
    (set (make-local-variable 'widget-link-prefix) "")
    (set (make-local-variable 'widget-link-suffix) "")

    ;; Top text indicating whether it is a class or object browser.
    (widget-insert-fb-buttons history-number)
    (widget-insert "----------------\n")
    (if (listp widget)
        (progn
          (widget-insert (add-string-property
                          "Widget object browser"
                          'face 'widget-browse-h1))
          (widget-insert "\n\n")
          (when location
            (let ((b (marker-buffer location))
                  (p (marker-position location)))
              (widget-insert (add-string-property "Location: "
                                                  'face 'italic))
              (widget-create 'push-button
                             :tag (format "position %s in buffer %s" p b)
                             :value (list p b)
                             :action '(lambda (widget &optional event)
                                        (let ((value (widget-get widget :value)))
                                          (let ((pos (car value))
                                                (buf (cadr value)))
                                            (switch-to-buffer-other-window buf)
                                            (goto-char pos)))))
              (widget-insert "\n\n")))
          (widget-insert (add-string-property "Class: "
                                              'face 'italic)))
      (widget-insert (add-string-property "Widget class browser"
                                          'face 'widget-browse-h1))
      (widget-insert ".\n\n")
      (widget-insert (add-string-property "Class: " 'face 'italic))
      (widget-insert (add-string-property (format "%s\n" widget)
                                          'face '(bold)))
      (widget-insert (format "%s" (get widget 'widget-documentation)))
      (unless (eq (preceding-char) ?\n) (widget-insert "\n"))
      (widget-insert (add-string-property "\nSuper: " 'face 'italic))
      (setq widget (get widget 'widget-type))
      )

    ;(widget-insert (format "%s\n" widget))

    ;; Now show the attributes.
    (let ((name (car widget))
          (items (cdr widget))
          key value printer)
      (if (not name)
          (widget-insert "none\n")
        (let ((ancestors (list name))
              a
              (i1 7)
              i
              )
          (setq i i1)
          (while name
            (setq a (intern-soft name))
            (if a
                (progn
                  (setq a (get a 'widget-type))
                  (setq name (car a))
                  (when (intern-soft name)
                    (push name ancestors)))
              (setq name)))
          ;;(widget-insert (format "ancestors=%s\n" ancestors))
          (mapc (lambda (w)
                  (widget-insert (make-string (if (= i i1) 0 i) ? ))
                  (widget-create 'widget-browse
                                 :format "%[%v%]"
                                 w)
                  (widget-insert "\n")
                  (setq i (+ i 2)))
                ancestors)))
      (while items
        (setq key (nth 0 items)
              value (nth 1 items)
              printer (or (get key 'widget-keyword-printer)
                          'widget-browse-sexp)
              items (cdr (cdr items)))
        (widget-insert "\n"
                       (add-string-property (symbol-name key)
                                            'face 'italic))
        (when (widget-browse-explained key)
          (widget-insert " (")
          (widget-create
           ;;'push-button
           ;;:tag "explain"
           ;;:format "%[%v%]"
           ;;:button-prefix ""
           ;;:button-suffix ""
           'widget-browse-link
           :value key
           :tag "explain"
           :format "%[%t%]"
           :action '(lambda (widget &optional event)
                      (widget-browse-explain
                       ;;(widget-get widget :value)
                       (widget-value widget)
                       ))
           )
          (widget-insert ")"))
        (widget-insert "\n\t")
        (funcall printer widget key value)
        (widget-insert "\n")))

    (widget-insert "\n-----------\n")
    (widget-insert-fb-buttons history-number)

    (widget-setup)
    (goto-char (point-min))
;;     (when wid-to-history
;;       (setq widget-browse-fb-history
;;             (reverse (cons (list 'widget-browse wid-to-history location)
;;                            (reverse widget-browse-fb-history)))))
    ))

(defun widget-browse-at (pos)
  "Browse the widget under point."
  (interactive "d")
  (let ((mp pos)
        (b (if (markerp pos) (marker-buffer pos)
             (current-buffer))))
    (if (not (buffer-live-p b))
        (message "Sorry the markers buffer is gone")
      (with-current-buffer b
        (when (markerp pos)
          (setq pos (marker-position pos)))
        (let* ((field (get-char-property pos 'field))
               (button (get-char-property pos 'button))
               (doc (get-char-property pos 'widget-doc))
               (text (cond (field "This is an editable text area.")
                           (button "This is an active area.")
                           (doc "This is documentation text.")
                           (t "This is unidentified text.")))
               (widget (or field button doc)))
          (when widget
            (widget-browse widget mp))
          (message text))))))

(defun button-at (pos)
  "Return the button at marker or position POS, or nil.
If not a marker use the current buffer."
  (with-current-buffer (if (markerp pos) (marker-buffer pos)
                         (current-buffer))
    (when (markerp pos)
      (setq pos (marker-position pos)))
    (let ((button (get-char-property pos 'button)))
      (if (or (overlayp button) (null button))
          button
        ;; Must be a text-property button; return a marker pointing to it.
        (copy-marker pos t)))))

(defun button-browse-at (pos)
  (interactive "d")
  (let ((b (if (markerp pos) (marker-buffer pos)
             (current-buffer))))
    (if (not (buffer-live-p b))
        (message "Sorry the button's buffer is gone")
      (button-browse (button-at pos)))))

(defun button-browse (button)
  "Create a widget browser for WIDGET."
  (interactive (list (completing-read "Button: "
				      obarray
				      (lambda (symbol)
                                        (or (get symbol 'button-category-symbol)
                                            (get symbol 'supertype)))
				      t nil 'button-browse-history)))
  (let (history-number)
    (if (integerp button)
        (progn
          (setq history-number button)
          (setq button (nth 1 (nth button widget-browse-fb-history))))
      (widget-add-fb-history (list 'button-browse button))
      (setq history-number (1- (length widget-browse-fb-history))))

    (when (stringp button)
      (setq button (intern-soft button)))
    (when (symbolp button)
      (unless (and button
                   (or (eq button 'default-button)
                       (get button 'supertype)
                       (get button 'button-category-symbol)
                       (save-match-data
                         (string-match "-button$" (symbol-name button)))))
        (error "Not a button")))
    ;; Create the buffer.
    (kill-buffer (get-buffer-create "*Browse Button*"))
    (switch-to-buffer (get-buffer-create "*Browse Button*"))
    (widget-browse-mode)

    (make-local-variable 'widget-button-face)
    (setq widget-button-face 'link)

    (widget-insert-fb-buttons history-number)
    (widget-insert "----------------\n")

    ;; Top text indicating whether it is a class or object browser.
    (if (or (overlayp button)
            (markerp button))
        (progn
          (widget-insert (add-string-property "Button object browser"
                                              'face 'widget-browse-h1))
          (widget-insert "\n\n")
          (let ((b (if (markerp button)
                       (marker-buffer button)
                     (overlay-buffer button)))
                (p (if (markerp button)
                       (marker-position button)
                     (overlay-start button))))
            (widget-insert (add-string-property "Location: "
                                                'face 'italic))
            (widget-create 'push-button
                           :tag (format "position %s in buffer %s" p b)
                           :value (list p b)
                           :action '(lambda (widget &optional event)
                                      (let ((value (widget-get widget :value)))
                                        (let ((pos (car value))
                                              (buf (cadr value)))
                                          (switch-to-buffer-other-window buf)
                                          (goto-char pos)))))
            (widget-insert "\n\n")))
      (widget-insert (add-string-property "Button class browser"
                                          'face 'widget-browse-h1))
      (widget-insert "\n\n")
      (widget-insert (add-string-property "Type: "
                                          'face 'italic))
      (widget-insert (add-string-property (symbol-name button)
                                          'face 'bold))
      (widget-insert "\n"))

    ;; Now show the attributes.
    (let (
          (items
           (if (symbolp button)
               (if (get button 'button-category-symbol)
                   (symbol-plist (get button 'button-category-symbol))
                 (symbol-plist button))
             (if (markerp button)
                 (let ((pos (marker-position button))
                       (buf (marker-buffer button)))
                   (text-properties-at pos buf))
               (overlay-properties button))))
          rest-items
          name
          key value printer)
      ;;(insert (format "\n%s\n\n" items))
      (let ((copied-items (copy-seq items)))
        (while copied-items
          (setq key   (nth 0 copied-items)
                value (nth 1 copied-items)
                copied-items (cdr (cdr copied-items)))
          (if (eq key 'category)
              (setq name value)
            (if (eq key 'supertype)
                (setq name (make-symbol (concat (symbol-name value) "-button")))
              (push value rest-items)
              (push key   rest-items)))))
      ;;(insert "\nname=" (symbol-name value) "\n\n")
      (when name
        (widget-insert (add-string-property
                        (if (symbolp button)
                            (if (get button 'supertype)
                                "Supertype: "
                              "")
                          "Category:  ")
                        'face 'italic))
        (let* (a
               (ancestors
                (list name))
               (i1 11)
               (i i1))
          (while name
            (setq a (or (get name 'supertype)
                        (get name :supertype)))
            ;;(message "name=%s, a=%s\n    name plist=%s" name a (symbol-plist name));(sit-for 4)
            (if (or (not a)
                    (eq a 'default-button))
                (setq name)
              (setq name (make-symbol (concat (symbol-name a) "-button")))
              (setq ancestors (cons name ancestors))))
          ;;(message "ancestors=%s" ancestors)(sit-for 2)
          (mapc (lambda (w)
                  (widget-insert (make-string (if (= i i1) 0 i) ? ))
                  (widget-create 'button-browse
                                 :format "%[%v%]"
                                 w)
                  (widget-insert "\n")
                  (setq i (+ i 2)))
                ancestors)))
      (while rest-items
        (setq key   (nth 0 rest-items)
              value (nth 1 rest-items)
              printer (or (get key 'widget-keyword-printer)
                          'widget-browse-sexp)
              rest-items (cdr (cdr rest-items)))
        (widget-insert "\n"
                       (add-string-property (symbol-name key)
                                            'face 'italic))
        (when (widget-browse-explained key)
          (widget-insert " (")
          (widget-create 'push-button
                         :tag "explain"
                         :value key
                         :action '(lambda (widget &optional event)
                                    (widget-browse-explain
                                     (widget-get widget :value))))
          (widget-insert ")"))
        (widget-insert "\n\t")
        (funcall printer button key value)
        (widget-insert "\n")))
    (widget-setup)
    (goto-char (point-min))

;;     (when button-to-history
;;       (setq widget-browse-fb-history
;;             (reverse (cons (list 'button-browse button-to-history)
;;                            (reverse widget-browse-fb-history)))))
    ))


;;;###autoload
(defgroup whelp nil
  "Customization group for whelp."
  :group 'emacs)

(defface widget-browse-h1
  '((t (:weight bold :height 1.5)))
  "Face for top header in widget/button browse buffers."
  :group 'whelp)

(defun add-string-property (str prop val)
  (let ((s (copy-seq str)))
    (put-text-property 0 (length s)
                       prop val
                       s)
    s))

;;; The `button-browse' Widget.

(define-widget 'button-browse 'push-button
  "Widget button for creating a button browser.
The :value of the widget shuld be the button to be browsed."
  :format "%[[%v]%]"
  :value-create 'widget-browse-button-value-create
  :action 'widget-browse-button-action)

(defun widget-browse-button-action (widget &optional event)
  ;; Create widget browser for WIDGET's :value.
  (button-browse (widget-get widget :value)))

(defun widget-browse-button-value-create (widget)
  ;; Insert type name.
  (let ((value (widget-get widget :value)))
    (cond ((symbolp value)
	   (insert (symbol-name value)))
	  ((consp value)
	   (insert (symbol-name (widget-type value))))
	  (t
	   (insert "strange")))))


(defun widget-browse-explained (property)
  (memq property
        '(
          :created-in-function
        )))

(defun widget-browse-explain (property)
  (with-output-to-temp-buffer (help-buffer)
    (help-setup-xref (list #'widget-browse-explain property) (interactive-p))
    (with-current-buffer (help-buffer)
      (let ((inhibit-read-only t))
        (cond
         ( (eq property :created-in-function)
           (princ "Property :created-in-function tells where a field object or class is created.")
           )
         ( t
           (princ (format "No explanation found for %s" property))
           )
         )
        (with-no-warnings (print-help-return-message))))))

(provide 'whelp)