Mercurial > emacs
annotate lisp/descr-text.el @ 45721:6bc9049644e2
(Cursor Position Information): Update "C-u C-x =" example.
author | Colin Walters <walters@gnu.org> |
---|---|
date | Sun, 09 Jun 2002 21:58:46 +0000 |
parents | a54155344566 |
children | 97041c98624e |
rev | line source |
---|---|
45057 | 1 ;;; descr-text.el --- describe text mode |
45022 | 2 |
3 ;; Copyright (c) 1994, 1995, 1996, 2001, 2002 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Boris Goldowsky <boris@gnu.org> | |
6 ;; Keywords: faces | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
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 2, or (at your option) | |
13 ;; any later version. | |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
24 | |
25 ;;; Commentary: | |
26 | |
27 ;;; Describe-Text Mode. | |
28 | |
29 ;;; Code: | |
30 | |
31 (defun describe-text-done () | |
32 "Delete the current window or bury the current buffer." | |
33 (interactive) | |
34 (if (> (count-windows) 1) | |
35 (delete-window) | |
36 (bury-buffer))) | |
37 | |
38 (defvar describe-text-mode-map | |
39 (let ((map (make-sparse-keymap))) | |
40 (set-keymap-parent map widget-keymap) | |
41 map) | |
42 "Keymap for `describe-text-mode'.") | |
43 | |
44 (defcustom describe-text-mode-hook nil | |
45 "List of hook functions ran by `describe-text-mode'." | |
46 :type 'hook) | |
47 | |
48 (defun describe-text-mode () | |
49 "Major mode for buffers created by `describe-text-at'. | |
50 | |
51 \\{describe-text-mode-map} | |
52 Entry to this mode calls the value of `describe-text-mode-hook' | |
53 if that value is non-nil." | |
54 (kill-all-local-variables) | |
55 (setq major-mode 'describe-text-mode | |
56 mode-name "Describe-Text") | |
57 (use-local-map describe-text-mode-map) | |
58 (widget-setup) | |
59 (run-hooks 'describe-text-mode-hook)) | |
60 | |
61 ;;; Describe-Text Utilities. | |
62 | |
63 (defun describe-text-widget (widget) | |
64 "Insert text to describe WIDGET in the current buffer." | |
65 (widget-create 'link | |
66 :notify `(lambda (&rest ignore) | |
67 (widget-browse ',widget)) | |
68 (format "%S" (if (symbolp widget) | |
69 widget | |
70 (car widget)))) | |
71 (widget-insert " ") | |
72 (widget-create 'info-link :tag "widget" "(widget)Top")) | |
73 | |
74 (defun describe-text-sexp (sexp) | |
75 "Insert a short description of SEXP in the current buffer." | |
76 (let ((pp (condition-case signal | |
77 (pp-to-string sexp) | |
78 (error (prin1-to-string signal))))) | |
79 (when (string-match "\n\\'" pp) | |
80 (setq pp (substring pp 0 (1- (length pp))))) | |
81 (if (cond ((string-match "\n" pp) | |
82 nil) | |
83 ((> (length pp) (- (window-width) (current-column))) | |
84 nil) | |
85 (t t)) | |
86 (widget-insert pp) | |
87 (widget-create 'push-button | |
88 :tag "show" | |
89 :action (lambda (widget &optional event) | |
90 (with-output-to-temp-buffer | |
91 "*Pp Eval Output*" | |
92 (princ (widget-get widget :value)))) | |
93 pp)))) | |
94 | |
95 (defun describe-text-properties (properties) | |
96 "Insert a description of PROPERTIES in the current buffer. | |
97 PROPERTIES should be a list of overlay or text properties. | |
98 The `category' property is made into a widget button that call | |
99 `describe-text-category' when pushed." | |
45697
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
100 ;; Sort the properties by the size of their value. |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
101 (dolist (elt (sort (let ((ret nil) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
102 (key nil) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
103 (val nil) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
104 (len nil)) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
105 (while properties |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
106 (setq key (pop properties) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
107 val (pop properties) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
108 len 0) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
109 (unless (or (eq key 'category) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
110 (widgetp val)) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
111 (setq val (pp-to-string val) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
112 len (length val))) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
113 (push (list key val len) ret)) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
114 ret) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
115 (lambda (a b) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
116 (< (nth 2 a) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
117 (nth 2 b))))) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
118 (let ((key (nth 0 elt)) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
119 (value (nth 1 elt))) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
120 (widget-insert (propertize (format " %-20s" key) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
121 'font-lock-face 'italic)) |
45022 | 122 (cond ((eq key 'category) |
123 (widget-create 'link | |
124 :notify `(lambda (&rest ignore) | |
125 (describe-text-category ',value)) | |
126 (format "%S" value))) | |
127 ((widgetp value) | |
128 (describe-text-widget value)) | |
129 (t | |
45697
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
130 (widget-insert value)))) |
234b16d90545
(describe-text-properties): Sort the output by the size of the values.
Colin Walters <walters@gnu.org>
parents:
45057
diff
changeset
|
131 (widget-insert "\n"))) |
45022 | 132 |
133 ;;; Describe-Text Commands. | |
134 | |
135 (defun describe-text-category (category) | |
136 "Describe a text property category." | |
137 (interactive "S") | |
138 (when (get-buffer "*Text Category*") | |
139 (kill-buffer "*Text Category*")) | |
140 (save-excursion | |
141 (with-output-to-temp-buffer "*Text Category*" | |
142 (set-buffer "*Text Category*") | |
143 (widget-insert "Category " (format "%S" category) ":\n\n") | |
144 (describe-text-properties (symbol-plist category)) | |
145 (describe-text-mode) | |
146 (goto-char (point-min))))) | |
147 | |
148 ;;;###autoload | |
149 (defun describe-text-at (pos) | |
150 "Describe widgets, buttons, overlays and text properties at POS." | |
151 (interactive "d") | |
152 (when (eq (current-buffer) (get-buffer "*Text Description*")) | |
153 (error "Can't do self inspection")) | |
154 (let* ((properties (text-properties-at pos)) | |
155 (overlays (overlays-at pos)) | |
156 overlay | |
157 (wid-field (get-char-property pos 'field)) | |
158 (wid-button (get-char-property pos 'button)) | |
159 (wid-doc (get-char-property pos 'widget-doc)) | |
160 ;; If button.el is not loaded, we have no buttons in the text. | |
161 (button (and (fboundp 'button-at) (button-at pos))) | |
162 (button-type (and button (button-type button))) | |
163 (button-label (and button (button-label button))) | |
164 (widget (or wid-field wid-button wid-doc))) | |
165 (if (not (or properties overlays)) | |
166 (message "This is plain text.") | |
167 (when (get-buffer "*Text Description*") | |
168 (kill-buffer "*Text Description*")) | |
169 (save-excursion | |
170 (with-output-to-temp-buffer "*Text Description*" | |
171 (set-buffer "*Text Description*") | |
172 (widget-insert "Text content at position " (format "%d" pos) ":\n\n") | |
173 ;; Widgets | |
174 (when (widgetp widget) | |
175 (widget-insert (cond (wid-field "This is an editable text area") | |
176 (wid-button "This is an active area") | |
177 (wid-doc "This is documentation text"))) | |
178 (widget-insert " of a ") | |
179 (describe-text-widget widget) | |
180 (widget-insert ".\n\n")) | |
181 ;; Buttons | |
182 (when (and button (not (widgetp wid-button))) | |
183 (widget-insert "Here is a " (format "%S" button-type) | |
184 " button labeled `" button-label "'.\n\n")) | |
185 ;; Overlays | |
186 (when overlays | |
187 (if (eq (length overlays) 1) | |
188 (widget-insert "There is an overlay here:\n") | |
189 (widget-insert "There are " (format "%d" (length overlays)) | |
190 " overlays here:\n")) | |
191 (dolist (overlay overlays) | |
192 (widget-insert " From " (format "%d" (overlay-start overlay)) | |
193 " to " (format "%d" (overlay-end overlay)) "\n") | |
194 (describe-text-properties (overlay-properties overlay))) | |
195 (widget-insert "\n")) | |
196 ;; Text properties | |
197 (when properties | |
198 (widget-insert "There are text properties here:\n") | |
199 (describe-text-properties properties)) | |
200 (describe-text-mode) | |
201 (goto-char (point-min))))))) | |
202 | |
45700
a54155344566
(toplevel): Provide `descr-text'.
Colin Walters <walters@gnu.org>
parents:
45697
diff
changeset
|
203 (provide 'descr-text) |
a54155344566
(toplevel): Provide `descr-text'.
Colin Walters <walters@gnu.org>
parents:
45697
diff
changeset
|
204 |
45022 | 205 ;;; descr-text.el ends here |