Mercurial > emacs
annotate lisp/emacs-lisp/checkdoc.el @ 20980:85cc901f00ee
(winner-mode): Autoload cookie for defcustom too.
author | Dave Love <fx@gnu.org> |
---|---|
date | Wed, 25 Feb 1998 23:09:07 +0000 |
parents | f3f9df46d008 |
children | 2b9774c50f91 |
rev | line source |
---|---|
20085 | 1 ;;; checkdoc --- Check documentation strings for style requirements |
2 | |
20603
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
3 ;;; Copyright (C) 1997, 1998 Free Software Foundation |
20085 | 4 ;; |
20603
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
5 ;; Author: Eric M. Ludlam <zappo@gnu.org> |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
6 ;; Version: 0.4.2 |
20085 | 7 ;; Keywords: docs, maint, lisp |
8 ;; | |
9 ;; This file is part of GNU Emacs. | |
20603
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
10 ;; |
20085 | 11 ;; GNU Emacs is free software; you can redistribute it and/or modify |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
20603
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
15 ;; |
20085 | 16 ;; GNU Emacs is distributed in the hope that it will be useful, |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20603
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
20 ;; |
20085 | 21 ;; You should have received a copy of the GNU General Public License |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
25 | |
26 ;;; Commentary: | |
27 ;; | |
28 ;; The emacs lisp manual has a nice chapter on how to write | |
29 ;; documentation strings. Many stylistic suggestions are fairly | |
30 ;; deterministic and easy to check for syntactically, but also easy | |
31 ;; to forget. The main checkdoc engine will perform the stylistic | |
32 ;; checks needed to make sure these styles are remembered. | |
33 ;; | |
34 ;; There are two ways to use checkdoc: | |
35 ;; 1) Periodically use `checkdoc'. `checkdoc-current-buffer' and | |
36 ;; `checkdoc-defun' to check your documentation. | |
37 ;; 2) Use `checkdoc-minor-mode' to automatically check your | |
38 ;; documentation whenever you evaluate lisp code with C-M-x | |
39 ;; or [menu-bar emacs-lisp eval-buffer]. Additional key-bindings | |
40 ;; are also provided under C-c ? KEY | |
41 ;; (require 'checkdoc) | |
42 ;; (add-hook 'emacs-lisp-mode-hook | |
43 ;; '(lambda () (checkdoc-minor-mode 1))) | |
44 ;; | |
45 ;; Auto-fixing: | |
46 ;; | |
47 ;; There are four classifications of style errors in terms of how | |
48 ;; easy they are to fix. They are simple, complex, really complex, | |
49 ;; and impossible. (Impossible really means that checkdoc does not | |
50 ;; have a fixing routine yet.) Typically white-space errors are | |
51 ;; classified as simple, and are auto-fixed by default. Typographic | |
52 ;; changes are considered complex, and the user is asked if they want | |
53 ;; the problem fixed before checkdoc makes the change. These changes | |
54 ;; can be done without asking if `checkdoc-autofix-flag' is properly | |
55 ;; set. Potentially redundant changes are considered really complex, | |
56 ;; and the user is always asked before a change is inserted. The | |
57 ;; variable `checkdoc-autofix-flag' controls how these types of errors | |
58 ;; are fixed. | |
59 ;; | |
60 ;; Spell checking doc-strings: | |
61 ;; | |
62 ;; The variable `checkdoc-spellcheck-documentation-flag' can be set | |
63 ;; to customize how spell checking is to be done. Since spell | |
64 ;; checking can be quite slow, you can optimize how best you want your | |
65 ;; checking done. The default is 'defun, which spell checks each time | |
66 ;; `checkdoc-defun' or `checkdoc-eval-defun' is used. Setting to nil | |
67 ;; prevents spell checking during normal usage. | |
68 ;; Setting this variable to nil does not mean you cannot take | |
69 ;; advantage of the spell checking. You can instead use the | |
70 ;; interactive functions `checkdoc-ispell-*' to check the spelling of | |
71 ;; your documentation. | |
72 ;; There is a list of lisp-specific words which checkdoc will | |
73 ;; install into ispell on the fly, but only if ispell is not already | |
74 ;; running. Use `ispell-kill-ispell' to make checkdoc restart it with | |
75 ;; these words enabled. | |
76 ;; | |
20603
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
77 ;; Checking parameters |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
78 ;; |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
79 ;; You might not always want a function to have it's parameters listed |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
80 ;; in order. When this is the case, put the following comment just in |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
81 ;; front of the documentation string: "; checkdoc-order: nil" This |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
82 ;; overrides the value of `checkdoc-arguments-in-order-flag'. |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
83 ;; |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
84 ;; If you specifically wish to avoid mentioning a parameter of a |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
85 ;; function in the doc string (such as a hidden parameter, or a |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
86 ;; parameter which is very obvious like events), you can have checkdoc |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
87 ;; skip looking for it by putting the following comment just in front |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
88 ;; of the documentation string: "; checkdoc-params: (args go here)" |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
89 ;; |
20085 | 90 ;; Adding your own checks: |
91 ;; | |
92 ;; You can experiment with adding your own checks by setting the | |
93 ;; hooks `checkdoc-style-hooks' and `checkdoc-comment-style-hooks'. | |
94 ;; Return a string which is the error you wish to report. The cursor | |
95 ;; position should be preserved. | |
96 ;; | |
97 ;; This file requires lisp-mnt (lisp maintenance routines) for the | |
98 ;; comment checkers. | |
99 ;; | |
100 ;; Requires custom for emacs v20. | |
101 | |
102 ;;; Change log: | |
103 ;; 0.1 Initial revision | |
104 ;; 0.2 Fixed comments in this file to match the emacs lisp standards. | |
105 ;; Added new doc checks for: variable-flags, function arguments | |
106 ;; Added autofix functionality for white-space, and quoted variables. | |
107 ;; Unquoted symbols are allowed after ( character. (Sample code) | |
108 ;; Check for use of `? ' at end of line and warn. | |
109 ;; Check for spaces at end of lines for whole file, or one defun. | |
110 ;; Check for comments standards, including headinds like Code: | |
111 ;; and use of triple semicolons versus double semicolons | |
112 ;; Check that interactive functions have a doc-string. Optionally | |
113 ;; set `checkdoc-force-docstrings-flag' to non-nil to make all | |
114 ;; definitions have a doc-string. | |
115 ;; 0.3 Regexp changse for accuracy on var checking and param checking. | |
116 ;; lm-verify check expanded to each sub-call w/ more descriptive | |
117 ;; messages, and two autofix-options. | |
118 ;; Suggestions/patches from Christoph Wedler <wedler@fmi.uni-passau.de> | |
119 ;; XEmacs support w/ extents/overlays. | |
120 ;; Better Whitespace finding regexps | |
121 ;; Added `checkdoc-arguments-in-order-flag' to optionally turn off | |
122 ;; warnings of arguments that do not appear in order in doc | |
123 ;; strings. | |
124 ;; 0.4 New fix routine when two lines can be joined to make the | |
125 ;; first line a comlete sentence. | |
126 ;; Added ispell code. Use `checkdoc-spellcheck-documentation-flag' | |
127 ;; to enable or disable this test in certain contexts. | |
128 ;; Added ispell interface functions `checkdoc-ispell', | |
129 ;; `checkdoc-ispell-continue', `checkdoc-ispell-defun' | |
130 ;; `checkdoc-ispell-interactive', `checkdoc-ispell-current-buffer'. | |
131 ;; Loop through all potential unquoted symbols. | |
132 ;; Auto-fixing no longer screws up the "end" of the doc-string. | |
133 ;; Maintain a different syntax table when examining arguments. | |
134 ;; Autofix enabled for parameters which are not uppercase iff they | |
135 ;; occur in lower case in the doc-string. | |
136 ;; Autofix enable if there is no Code: label. | |
137 ;; The comment text ";; checkdoc-order: nil|t" inside a defun to | |
138 ;; enable or disable the checking of argument order for one defun. | |
139 ;; The comment text ";; checkdoc-params: (arg1 arg2)" inside a defun | |
140 ;; (Such as just before the doc string) will list ARG1 and ARG2 as | |
141 ;; being paramters that need not show up in the doc string. | |
142 ;; Brought in suggestions from Jari Aalto <jaalto@tre.tele.nokia.fi> | |
143 ;; More robustness (comments in/around doc-strings/ arg lists) | |
144 ;; Don't offer to `quote'afy symbols or keystroke representations | |
145 ;; that are in lists (sample code) This added new fn | |
146 ;; `checkdoc-in-sample-code-p' | |
147 ;; Added more comments near the ;;; comment check about why it | |
148 ;; is being done. ;;; Are also now allowed inside a defun. | |
149 ;; This added the function `checkdoc-outside-major-sexp' | |
150 ;; Added `checkdoc-interactive' which permits interactive | |
151 ;; perusal of document warnings, and editing of strings. | |
152 ;; Fixed `checkdoc-defun-info' to be more robust when creating | |
153 ;; the paramter list. | |
154 ;; Added list of verbs in the wrong tense, and their fixes. | |
155 ;; Added defconst/subst/advice to checked items. | |
156 ;; Added `checkdoc-style-hooks' and `checkdoc-comment-style-hooks' | |
157 ;; for adding in user tests. | |
158 ;; Added `checkdoc-continue', a version of checkdoc that continues | |
159 ;; from point. | |
160 ;; [X]Emacs 20 support for extended characters. | |
161 ;; Only check comments on real files. | |
162 ;; Put `checkdoc' and `checkdoc-continue' into keymap/menu | |
163 ;; 0.4.1 Made `custom' friendly. | |
164 ;; C-m in warning buffer also goes to error. | |
165 ;; Shrink error buffer to size of text. | |
166 ;; Added `checkdoc-tripple-semi-comment-check-flag'. | |
167 ;; `checkdoc-spellcheck-documentation-flag' off by default. | |
168 ;; Re-sorted check order so white space is removed before adding a . | |
20603
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
169 ;; 0.4.2 Added some more comments in the commentary. |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
170 ;; You can now `quote' symbols that look like keystrokes |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
171 ;; When spell checking, meta variables can end in `th' or `s'. |
20085 | 172 |
173 ;;; TO DO: | |
174 ;; Hook into the byte compiler on a defun/defver level to generate | |
175 ;; warnings in the byte-compiler's warning/error buffer. | |
176 ;; Better ways to override more typical `eval' functions. Advice | |
177 ;; might be good but hard to turn on/off as a minor mode. | |
178 ;; | |
179 ;;; Maybe Do: | |
180 ;; Code sweep checks for "forbidden functions", proper use of hooks, | |
181 ;; proper keybindings, and other items from the manual that are | |
182 ;; not specifically docstring related. Would this even be useful? | |
183 | |
184 ;;; Code: | |
20603
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
185 (defvar checkdoc-version "0.4.2" |
20085 | 186 "Release version of checkdoc you are currently running.") |
187 | |
188 ;; From custom web page for compatibility between versions of custom: | |
189 (eval-and-compile | |
190 (condition-case () | |
191 (require 'custom) | |
192 (error nil)) | |
193 (if (and (featurep 'custom) (fboundp 'custom-declare-variable)) | |
194 nil ;; We've got what we needed | |
195 ;; We have the old custom-library, hack around it! | |
196 (defmacro defgroup (&rest args) | |
197 nil) | |
198 (defmacro custom-add-option (&rest args) | |
199 nil) | |
20603
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
200 (defmacro defcustom (var value doc &rest args) |
20085 | 201 (` (defvar (, var) (, value) (, doc)))))) |
202 | |
203 (defcustom checkdoc-autofix-flag 'semiautomatic | |
204 "*Non-nil means attempt auto-fixing of doc-strings. | |
205 If this value is the symbol 'query, then the user is queried before | |
206 any change is made. If the value is 'automatic, then all changes are | |
207 made without asking unless the change is very-complex. If the value | |
208 is 'semiautomatic, or any other value, then simple fixes are made | |
209 without asking, and complex changes are made by asking the user first. | |
210 The value 'never is the same as nil, never ask or change anything." | |
211 :group 'checkdoc | |
212 :type '(choice (const automatic) | |
213 (const semiautomatic) | |
214 (const query) | |
215 (const never))) | |
216 | |
217 (defcustom checkdoc-bouncy-flag t | |
218 "*Non-nil means to 'bounce' to auto-fix locations. | |
219 Setting this to nil will silently make fixes that require no user | |
220 interaction. See `checkdoc-autofix-flag' for auto-fixing details." | |
221 :group 'checkdoc | |
222 :type 'boolean) | |
223 | |
224 (defcustom checkdoc-force-docstrings-flag t | |
225 "*Non-nil means that all checkable definitions should have documentation. | |
226 Style guide dictates that interactive functions MUST have documentation, | |
227 and that its good but not required practice to make non user visible items | |
228 have doc-strings." | |
229 :group 'checkdoc | |
230 :type 'boolean) | |
231 | |
232 (defcustom checkdoc-tripple-semi-comment-check-flag t | |
233 "*Non-nil means to check for multiple adjacent occurrences of ;;; comments. | |
234 According to the style of emacs code in the lisp libraries, a block | |
235 comment can look like this: | |
236 ;;; Title | |
237 ;; text | |
238 ;; text | |
239 But when inside a function, code can be commented out using the ;;; | |
240 construct for all lines. When this variable is nil, the ;;; construct | |
241 is ignored regardless of it's location in the code." | |
242 :group 'checkdoc | |
243 :type 'boolean) | |
244 | |
245 (defcustom checkdoc-spellcheck-documentation-flag nil | |
246 "*Non-nil means run ispell on doc-strings based on value. | |
247 This will be automatically set to nil if ispell does not exist on your | |
248 system. Possible values are: | |
249 | |
250 nil - Don't spell-check during basic style checks. | |
251 'defun - Spell-check when style checking a single defun | |
252 'buffer - Spell-check only when style checking the whole buffer | |
253 'interactive - Spell-check only during `checkdoc-interactive' | |
254 t - Always spell-check" | |
255 :group 'checkdoc | |
256 :type '(choice (const nil) | |
257 (const defun) | |
258 (const buffer) | |
259 (const interactive) | |
260 (const t))) | |
261 | |
262 (defvar checkdoc-ispell-lisp-words | |
263 '("alist" "etags" "iff" "keymap" "paren" "regexp" "sexp" "xemacs") | |
264 "List of words that are correct when spell-checking lisp documentation.") | |
265 | |
266 (defcustom checkdoc-max-keyref-before-warn 10 | |
267 "*The number of \\ [command-to-keystroke] tokens allowed in a doc-string. | |
268 Any more than this and a warning is generated suggesting that the construct | |
269 \\ {keymap} be used instead." | |
270 :group 'checkdoc | |
271 :type 'integer) | |
272 | |
273 (defcustom checkdoc-arguments-in-order-flag t | |
274 "*Non-nil means warn if arguments appear out of order. | |
275 Setting this to nil will mean only checking that all the arguments | |
276 appear in the proper form in the documentation, not that they are in | |
277 the same order as they appear in the argument list. No mention is | |
278 made in the style guide relating to order." | |
279 :group 'checkdoc | |
280 :type 'boolean) | |
281 | |
282 (defvar checkdoc-style-hooks nil | |
283 "Hooks called after the standard style check is completed. | |
284 All hooks must return nil or a string representing the error found. | |
285 Useful for adding new user implemented commands. | |
286 | |
287 Each hook is called with two parameters, (DEFUNINFO ENDPOINT). | |
288 DEFUNINFO is the return value of `checkdoc-defun-info'. ENDPOINT is the | |
289 location of end of the documentation string.") | |
290 | |
291 (defvar checkdoc-comment-style-hooks nil | |
292 "Hooks called after the standard comment style check is completed. | |
293 Must return nil if no errors are found, or a string describing the | |
294 problem discovered. This is useful for adding additional checks.") | |
295 | |
296 (defvar checkdoc-diagnostic-buffer "*Style Warnings*" | |
20603
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
297 "Name of warning message buffer.") |
20085 | 298 |
299 (defvar checkdoc-defun-regexp | |
300 "^(def\\(un\\|var\\|custom\\|macro\\|const\\|subst\\|advice\\)\ | |
301 \\s-+\\(\\(\\sw\\|\\s_\\)+\\)[ \t\n]+" | |
302 "Regular expression used to identify a defun. | |
303 A search leaves the cursor in front of the parameter list.") | |
304 | |
305 (defcustom checkdoc-verb-check-experimental-flag t | |
306 "*Non-nil means to attempt to check the voice of the doc-string. | |
307 This check keys off some words which are commonly misused. See the | |
308 variable `checkdoc-common-verbs-wrong-voice' if you wish to add your | |
309 own." | |
310 :group 'checkdoc | |
311 :type 'boolean) | |
312 | |
313 (defvar checkdoc-common-verbs-regexp nil | |
314 "Regular expression derived from `checkdoc-common-verbs-regexp'.") | |
315 | |
316 (defvar checkdoc-common-verbs-wrong-voice | |
317 '(("adds" . "add") | |
318 ("allows" . "allow") | |
319 ("appends" . "append") | |
320 ("applies" "apply") | |
321 ("arranges" "arrange") | |
322 ("brings" . "bring") | |
323 ("calls" . "call") | |
324 ("catches" . "catch") | |
325 ("changes" . "change") | |
326 ("checks" . "check") | |
327 ("contains" . "contain") | |
328 ("creates" . "create") | |
329 ("destroys" . "destroy") | |
330 ("disables" . "disable") | |
331 ("executes" . "execute") | |
332 ("evals" . "evaluate") | |
333 ("evaluates" . "evaluate") | |
334 ("finds" . "find") | |
335 ("forces" . "force") | |
336 ("gathers" . "gather") | |
337 ("generates" . "generate") | |
338 ("goes" . "go") | |
339 ("guesses" . "guess") | |
340 ("highlights" . "highlight") | |
341 ("holds" . "hold") | |
342 ("ignores" . "ignore") | |
343 ("indents" . "indent") | |
344 ("initializes" . "initialize") | |
345 ("inserts" . "insert") | |
346 ("installs" . "install") | |
347 ("investigates" . "investigate") | |
348 ("keeps" . "keep") | |
349 ("kills" . "kill") | |
350 ("leaves" . "leave") | |
351 ("lets" . "let") | |
352 ("loads" . "load") | |
353 ("looks" . "look") | |
354 ("makes" . "make") | |
355 ("marks" . "mark") | |
356 ("matches" . "match") | |
357 ("notifies" . "notify") | |
358 ("offers" . "offer") | |
359 ("parses" . "parse") | |
360 ("performs" . "perform") | |
361 ("prepares" . "prepare") | |
362 ("prepends" . "prepend") | |
363 ("reads" . "read") | |
364 ("raises" . "raise") | |
365 ("removes" . "remove") | |
366 ("replaces" . "replace") | |
367 ("resets" . "reset") | |
368 ("restores" . "restore") | |
369 ("returns" . "return") | |
370 ("runs" . "run") | |
371 ("saves" . "save") | |
372 ("says" . "say") | |
373 ("searches" . "search") | |
374 ("selects" . "select") | |
375 ("sets" . "set") | |
376 ("sex" . "s*x") | |
377 ("shows" . "show") | |
378 ("signifies" . "signify") | |
379 ("sorts" . "sort") | |
380 ("starts" . "start") | |
381 ("stores" . "store") | |
382 ("switches" . "switch") | |
383 ("tells" . "tell") | |
384 ("tests" . "test") | |
385 ("toggles" . "toggle") | |
386 ("tries" . "try") | |
387 ("turns" . "turn") | |
388 ("undoes" . "undo") | |
389 ("unloads" . "unload") | |
390 ("unmarks" . "unmark") | |
391 ("updates" . "update") | |
392 ("uses" . "use") | |
393 ("yanks" . "yank") | |
394 ) | |
395 "Alist of common words in the wrong voice and what should be used instead. | |
396 Set `checkdoc-verb-check-experimental-flag' to nil to avoid this costly | |
397 and experimental check. Do not modify this list without setting | |
398 the value of `checkdoc-common-verbs-regexp' to nil which cause it to | |
399 be re-created.") | |
400 | |
401 (defvar checkdoc-syntax-table nil | |
402 "Syntax table used by checkdoc in document strings.") | |
403 | |
404 (if checkdoc-syntax-table | |
405 nil | |
406 (setq checkdoc-syntax-table (copy-syntax-table emacs-lisp-mode-syntax-table)) | |
407 ;; When dealing with syntax in doc-strings, make sure that - are encompased | |
408 ;; in words so we can use cheap \\> to get the end of a symbol, not the | |
409 ;; end of a word in a conglomerate. | |
410 (modify-syntax-entry ?- "w" checkdoc-syntax-table) | |
411 ) | |
412 | |
413 | |
414 ;;; Compatibility | |
415 ;; | |
416 (if (string-match "X[Ee]macs" emacs-version) | |
417 (progn | |
418 (defalias 'checkdoc-make-overlay 'make-extent) | |
419 (defalias 'checkdoc-overlay-put 'set-extent-property) | |
420 (defalias 'checkdoc-delete-overlay 'delete-extent) | |
421 (defalias 'checkdoc-overlay-start 'extent-start) | |
422 (defalias 'checkdoc-overlay-end 'extent-end) | |
423 (defalias 'checkdoc-mode-line-update 'redraw-modeline) | |
424 (defalias 'checkdoc-call-eval-buffer 'eval-buffer) | |
425 ) | |
426 (defalias 'checkdoc-make-overlay 'make-overlay) | |
427 (defalias 'checkdoc-overlay-put 'overlay-put) | |
428 (defalias 'checkdoc-delete-overlay 'delete-overlay) | |
429 (defalias 'checkdoc-overlay-start 'overlay-start) | |
430 (defalias 'checkdoc-overlay-end 'overlay-end) | |
431 (defalias 'checkdoc-mode-line-update 'force-mode-line-update) | |
432 (defalias 'checkdoc-call-eval-buffer 'eval-current-buffer) | |
433 ) | |
434 | |
435 ;; Emacs 20s have MULE characters which dont equate to numbers. | |
436 (if (fboundp 'char=) | |
437 (defalias 'checkdoc-char= 'char=) | |
438 (defalias 'checkdoc-char= '=)) | |
439 | |
440 ;; Emacs 19.28 and earlier don't have the handy 'add-to-list function | |
441 (if (fboundp 'add-to-list) | |
442 | |
443 (defalias 'checkdoc-add-to-list 'add-to-list) | |
444 | |
445 (defun checkdoc-add-to-list (list-var element) | |
446 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet." | |
447 (if (not (member element (symbol-value list-var))) | |
448 (set list-var (cons element (symbol-value list-var))))) | |
449 ) | |
450 | |
451 ;; To be safe in new emacsen, we want to read events, not characters | |
452 (if (fboundp 'read-event) | |
453 (defalias 'checkdoc-read-event 'read-event) | |
454 (defalias 'checkdoc-read-event 'read-char)) | |
455 | |
456 ;;; User level commands | |
457 ;; | |
458 ;;;###autoload | |
459 (defun checkdoc-eval-current-buffer () | |
460 "Evaluate and check documentation for the current buffer. | |
461 Evaluation is done first because good documentation for something that | |
462 doesn't work is just not useful. Comments, Doc-strings, and rogue | |
463 spacing are all verified." | |
464 (interactive) | |
465 (checkdoc-call-eval-buffer nil) | |
466 (checkdoc-current-buffer t)) | |
467 | |
468 ;;;###autoload | |
469 (defun checkdoc-current-buffer (&optional take-notes) | |
470 "Check the current buffer for document style, comment style, and rogue spaces. | |
471 Optional argument TAKE-NOTES non-nil will store all found errors in a | |
472 warnings buffer, otherwise it stops after the first error." | |
473 (interactive "P") | |
474 (if (interactive-p) (message "Checking buffer for style...")) | |
475 ;; Assign a flag to spellcheck flag | |
476 (let ((checkdoc-spellcheck-documentation-flag | |
477 (memq checkdoc-spellcheck-documentation-flag '(buffer t)))) | |
478 ;; every test is responsible for returning the cursor. | |
479 (or (and buffer-file-name ;; only check comments in a file | |
480 (checkdoc-comments take-notes)) | |
481 (checkdoc take-notes) | |
482 (checkdoc-rogue-spaces take-notes) | |
483 (not (interactive-p)) | |
484 (message "Checking buffer for style...Done.")))) | |
485 | |
486 ;;;###autoload | |
487 (defun checkdoc-interactive (&optional start-here) | |
488 "Interactively check the current buffers for errors. | |
489 Prefix argument START-HERE will start the checking from the current | |
490 point, otherwise the check starts at the beginning of the current | |
491 buffer. Allows navigation forward and backwards through document | |
492 errors. Does not check for comment or space warnings." | |
493 (interactive "P") | |
494 ;; Determine where to start the test | |
495 (let* ((begin (prog1 (point) | |
496 (if (not start-here) (goto-char (point-min))))) | |
497 ;; Assign a flag to spellcheck flag | |
498 (checkdoc-spellcheck-documentation-flag | |
499 (member checkdoc-spellcheck-documentation-flag | |
500 '(buffer interactive t))) | |
501 ;; Fetch the error list | |
502 (err-list (list (checkdoc-next-error)))) | |
503 (if (not (car err-list)) (setq err-list nil)) | |
504 ;; Include whatever function point is in for good measure. | |
505 (beginning-of-defun) | |
506 (while err-list | |
507 (goto-char (cdr (car err-list))) | |
508 ;; The cursor should be just in front of the offending doc-string | |
509 (let ((cdo (save-excursion | |
510 (checkdoc-make-overlay (point) | |
511 (progn (forward-sexp 1) | |
512 (point))))) | |
513 c) | |
514 (unwind-protect | |
515 (progn | |
516 (checkdoc-overlay-put cdo 'face 'highlight) | |
517 ;; Make sure the whole doc-string is visible if possible. | |
518 (sit-for 0) | |
519 (if (not (pos-visible-in-window-p | |
520 (save-excursion (forward-sexp 1) (point)) | |
521 (selected-window))) | |
522 (recenter)) | |
523 (message "%s(? e n p q)" (car (car err-list))) | |
524 (setq c (checkdoc-read-event)) | |
525 (if (not (integerp c)) (setq c ??)) | |
526 (cond ((or (checkdoc-char= c ?n) (checkdoc-char= c ?\ )) | |
527 (let ((ne (checkdoc-next-error))) | |
528 (if (not ne) | |
529 (progn | |
530 (message "No More Stylistic Errors.") | |
531 (sit-for 2)) | |
532 (setq err-list (cons ne err-list))))) | |
533 ((or (checkdoc-char= c ?p) (checkdoc-char= c ?\C-?)) | |
534 (if (/= (length err-list) 1) | |
535 (progn | |
536 (setq err-list (cdr err-list)) | |
537 ;; This will just re-ask fixup questions if | |
538 ;; it was skipped the last time. | |
539 (checkdoc-next-error)) | |
540 (message "No Previous Errors.") | |
541 (sit-for 2))) | |
542 ((checkdoc-char= c ?e) | |
543 (message "Edit the docstring, and press C-M-c to exit.") | |
544 (recursive-edit) | |
545 (checkdoc-delete-overlay cdo) | |
546 (setq err-list (cdr err-list)) ;back up the error found. | |
547 (beginning-of-defun) | |
548 (let ((ne (checkdoc-next-error))) | |
549 (if (not ne) | |
550 (progn | |
551 (message "No More Stylistic Errors.") | |
552 (sit-for 2)) | |
553 (setq err-list (cons ne err-list))))) | |
554 ((checkdoc-char= c ?q) | |
555 (setq err-list nil | |
556 begin (point))) | |
557 (t | |
558 (message "[E]dit [SPC|n] next error [DEL|p] prev error\ | |
559 [q]uit [?] help: ") | |
560 (sit-for 5)))) | |
561 (checkdoc-delete-overlay cdo)))) | |
562 (goto-char begin) | |
563 (message "Checkdoc: Done."))) | |
564 | |
565 (defun checkdoc-next-error () | |
566 "Find and return the next checkdoc error list, or nil. | |
567 Add error vector is of the form (WARNING . POSITION) where WARNING | |
568 is the warning text, and POSITION is the point in the buffer where the | |
569 error was found. We can use points and not markers because we promise | |
570 not to edit the buffer before point without re-executing this check." | |
571 (let ((msg nil) (p (point))) | |
572 (condition-case nil | |
573 (while (and (not msg) (checkdoc-next-docstring)) | |
574 (message "Searching for doc-string error...%d%%" | |
575 (/ (* 100 (point)) (point-max))) | |
576 (if (setq msg (checkdoc-this-string-valid)) | |
577 (setq msg (cons msg (point))))) | |
578 ;; Quit.. restore position, Other errors, leave alone | |
579 (quit (goto-char p))) | |
580 msg)) | |
581 | |
582 ;;;###autoload | |
583 (defun checkdoc (&optional take-notes) | |
584 "Use `checkdoc-continue' starting at the beginning of the current buffer. | |
585 Prefix argument TAKE-NOTES means to collect all the warning messages into | |
586 a separate buffer." | |
587 (interactive "P") | |
588 (let ((p (point))) | |
589 (goto-char (point-min)) | |
590 (checkdoc-continue take-notes) | |
591 ;; Go back since we can't be here without success above. | |
592 (goto-char p) | |
593 nil)) | |
594 | |
595 ;;;###autoload | |
596 (defun checkdoc-continue (&optional take-notes) | |
597 "Find the next doc-string in the current buffer which is stylisticly poor. | |
598 Prefix argument TAKE-NOTES means to continue through the whole buffer and | |
599 save warnings in a separate buffer. Second optional argument START-POINT | |
600 is the starting location. If this is nil, `point-min' is used instead." | |
601 (interactive "P") | |
602 (let ((wrong nil) (msg nil) (errors nil) | |
603 ;; Assign a flag to spellcheck flag | |
604 (checkdoc-spellcheck-documentation-flag | |
605 (member checkdoc-spellcheck-documentation-flag | |
606 '(buffer t)))) | |
607 (save-excursion | |
608 ;; If we are taking notes, encompass the whole buffer, otherwise | |
609 ;; the user is navigating down through the buffer. | |
610 (if take-notes (checkdoc-start-section "checkdoc")) | |
611 (while (and (not wrong) (checkdoc-next-docstring)) | |
612 (if (not (checkdoc-char= (following-char) ?\")) | |
613 ;; No doc-string... | |
614 nil | |
615 ;; OK, lets look at the doc-string. | |
616 (setq msg (checkdoc-this-string-valid)) | |
617 (if msg | |
618 ;; Oops | |
619 (if take-notes | |
620 (progn | |
621 (checkdoc-error (point) msg) | |
622 (setq errors t)) | |
623 (setq wrong (point))))))) | |
624 (if wrong | |
625 (progn | |
626 (goto-char wrong) | |
627 (error msg))) | |
628 (if (and take-notes errors) | |
629 (checkdoc-show-diagnostics) | |
630 (if (interactive-p) | |
631 (message "No style warnings."))))) | |
632 | |
633 (defun checkdoc-next-docstring () | |
634 "Find the next doc-string after point and return t. | |
635 Return nil if there are no more doc-strings." | |
636 (if (not (re-search-forward checkdoc-defun-regexp nil t)) | |
637 nil | |
638 ;; search drops us after the identifier. The next sexp is either | |
639 ;; the argument list or the value of the variable. skip it. | |
640 (forward-sexp 1) | |
641 (skip-chars-forward " \n\t") | |
642 t)) | |
643 | |
644 ;;; ###autoload | |
645 (defun checkdoc-comments (&optional take-notes) | |
646 "Find missing comment sections in the current emacs lisp file. | |
647 Prefix argument TAKE-NOTES non-nil means to save warnings in a | |
648 separate buffer. Otherwise print a message. This returns the error | |
649 if there is one." | |
650 (interactive "P") | |
651 (if take-notes (checkdoc-start-section "checkdoc-comments")) | |
652 (if (not buffer-file-name) | |
653 (error "Can only check comments for a file buffer.")) | |
654 (let* ((checkdoc-spellcheck-documentation-flag | |
655 (member checkdoc-spellcheck-documentation-flag | |
656 '(buffer t))) | |
657 (e (checkdoc-file-comments-engine))) | |
658 (if e | |
659 (if take-notes | |
660 (checkdoc-error nil e) | |
661 (error e))) | |
662 (if (and e take-notes) | |
663 (checkdoc-show-diagnostics)) | |
664 e)) | |
665 | |
666 ;;;###autoload | |
667 (defun checkdoc-rogue-spaces (&optional take-notes) | |
668 "Find extra spaces at the end of lines in the current file. | |
669 Prefix argument TAKE-NOTES non-nil means to save warnings in a | |
670 separate buffer. Otherwise print a message. This returns the error | |
671 if there is one." | |
672 (interactive "P") | |
673 (if take-notes (checkdoc-start-section "checkdoc-rogue-spaces")) | |
674 (let ((e (checkdoc-rogue-space-check-engine))) | |
675 (if e | |
676 (if take-notes | |
677 (checkdoc-error nil e) | |
678 (message e))) | |
679 (if (and e take-notes) | |
680 (checkdoc-show-diagnostics)) | |
681 (if (not (interactive-p)) | |
682 e | |
683 (if e (message e) (message "Space Check: done."))))) | |
684 | |
685 | |
686 ;;;###autoload | |
687 (defun checkdoc-eval-defun () | |
688 "Evaluate the current form with `eval-defun' and check it's documentation. | |
689 Evaluation is done first so the form will be read before the | |
690 documentation is checked. If there is a documentation error, then the display | |
691 of what was evaluated will be overwritten by the diagnostic message." | |
692 (interactive) | |
693 (eval-defun nil) | |
694 (checkdoc-defun)) | |
695 | |
696 ;;;###autoload | |
697 (defun checkdoc-defun (&optional no-error) | |
698 "Examine the doc-string of the function or variable under point. | |
699 Calls `error' if the doc-string produces diagnostics. If NO-ERROR is | |
700 non-nil, then do not call error, but call `message' instead. | |
701 If the document check passes, then check the function for rogue white | |
702 space at the end of each line." | |
703 (interactive) | |
704 (save-excursion | |
705 (beginning-of-defun) | |
706 (if (not (looking-at checkdoc-defun-regexp)) | |
707 ;; I found this more annoying than useful. | |
708 ;;(if (not no-error) | |
709 ;; (message "Cannot check this sexp's doc-string.")) | |
710 nil | |
711 ;; search drops us after the identifier. The next sexp is either | |
712 ;; the argument list or the value of the variable. skip it. | |
713 (goto-char (match-end 0)) | |
714 (forward-sexp 1) | |
715 (skip-chars-forward " \n\t") | |
716 (let* ((checkdoc-spellcheck-documentation-flag | |
717 (member checkdoc-spellcheck-documentation-flag | |
718 '(defun t))) | |
719 (msg (checkdoc-this-string-valid))) | |
720 (if msg (if no-error (message msg) (error msg)) | |
721 (setq msg (checkdoc-rogue-space-check-engine | |
722 (save-excursion (beginning-of-defun) (point)) | |
723 (save-excursion (end-of-defun) (point)))) | |
724 (if msg (if no-error (message msg) (error msg)) | |
725 (if (interactive-p) (message "Checkdoc: done.")))))))) | |
726 | |
727 ;;; Ispell interface for forcing a spell check | |
728 ;; | |
729 | |
730 ;;;###autoload | |
731 (defun checkdoc-ispell-current-buffer (&optional take-notes) | |
732 "Check the style and spelling of the current buffer interactively. | |
733 Calls `checkdoc-current-buffer' with spell-checking turned on. | |
734 Prefix argument TAKE-NOTES is the same as for `checkdoc-current-buffer'" | |
735 (interactive) | |
736 (let ((checkdoc-spellcheck-documentation-flag t)) | |
737 (call-interactively 'checkdoc-current-buffer nil current-prefix-arg))) | |
738 | |
739 ;;;###autoload | |
740 (defun checkdoc-ispell-interactive (&optional take-notes) | |
741 "Check the style and spelling of the current buffer interactively. | |
742 Calls `checkdoc-interactive' with spell-checking turned on. | |
743 Prefix argument TAKE-NOTES is the same as for `checkdoc-interacitve'" | |
744 (interactive) | |
745 (let ((checkdoc-spellcheck-documentation-flag t)) | |
746 (call-interactively 'checkdoc-interactive nil current-prefix-arg))) | |
747 | |
748 ;;;###autoload | |
749 (defun checkdoc-ispell (&optional take-notes) | |
750 "Check the style and spelling of the current buffer. | |
751 Calls `checkdoc' with spell-checking turned on. | |
752 Prefix argument TAKE-NOTES is the same as for `checkdoc'" | |
753 (interactive) | |
754 (let ((checkdoc-spellcheck-documentation-flag t)) | |
755 (call-interactively 'checkdoc nil current-prefix-arg))) | |
756 | |
757 ;;;###autoload | |
758 (defun checkdoc-ispell-continue (&optional take-notes) | |
759 "Check the style and spelling of the current buffer after point. | |
760 Calls `checkdoc-continue' with spell-checking turned on. | |
761 Prefix argument TAKE-NOTES is the same as for `checkdoc-continue'" | |
762 (interactive) | |
763 (let ((checkdoc-spellcheck-documentation-flag t)) | |
764 (call-interactively 'checkdoc-continue nil current-prefix-arg))) | |
765 | |
766 ;;;###autoload | |
767 (defun checkdoc-ispell-comments (&optional take-notes) | |
768 "Check the style and spelling of the current buffer's comments. | |
769 Calls `checkdoc-comments' with spell-checking turned on. | |
770 Prefix argument TAKE-NOTES is the same as for `checkdoc-comments'" | |
771 (interactive) | |
772 (let ((checkdoc-spellcheck-documentation-flag t)) | |
773 (call-interactively 'checkdoc-comments nil current-prefix-arg))) | |
774 | |
775 ;;;###autoload | |
776 (defun checkdoc-ispell-defun (&optional take-notes) | |
777 "Check the style and spelling of the current defun with ispell. | |
778 Calls `checkdoc-defun' with spell-checking turned on. | |
779 Prefix argument TAKE-NOTES is the same as for `checkdoc-defun'" | |
780 (interactive) | |
781 (let ((checkdoc-spellcheck-documentation-flag t)) | |
782 (call-interactively 'checkdoc-defun nil current-prefix-arg))) | |
783 | |
784 ;;; Minor Mode specification | |
785 ;; | |
786 (defvar checkdoc-minor-mode nil | |
787 "Non-nil in `emacs-lisp-mode' for automatic documentation checking.") | |
788 (make-variable-buffer-local 'checkdoc-minor-mode) | |
789 | |
790 (checkdoc-add-to-list 'minor-mode-alist '(checkdoc-minor-mode " CDoc")) | |
791 | |
792 (defvar checkdoc-minor-keymap | |
793 (let ((map (make-sparse-keymap)) | |
794 (pmap (make-sparse-keymap))) | |
795 ;; Override some bindings | |
796 (define-key map "\C-\M-x" 'checkdoc-eval-defun) | |
797 (if (not (string-match "XEmacs" emacs-version)) | |
798 (define-key map [menu-bar emacs-lisp eval-buffer] | |
799 'checkdoc-eval-current-buffer)) | |
800 (define-key pmap "x" 'checkdoc-defun) | |
801 (define-key pmap "X" 'checkdoc-ispell-defun) | |
802 (define-key pmap "`" 'checkdoc-continue) | |
803 (define-key pmap "~" 'checkdoc-ispell-continue) | |
804 (define-key pmap "d" 'checkdoc) | |
805 (define-key pmap "D" 'checkdoc-ispell) | |
806 (define-key pmap "i" 'checkdoc-interactive) | |
807 (define-key pmap "I" 'checkdoc-ispell-interactive) | |
808 (define-key pmap "b" 'checkdoc-current-buffer) | |
809 (define-key pmap "B" 'checkdoc-ispell-current-buffer) | |
810 (define-key pmap "e" 'checkdoc-eval-current-buffer) | |
811 (define-key pmap "c" 'checkdoc-comments) | |
812 (define-key pmap "C" 'checkdoc-ispell-comments) | |
813 (define-key pmap " " 'checkdoc-rogue-spaces) | |
814 | |
815 ;; bind our submap into map | |
816 (define-key map "\C-c?" pmap) | |
817 map) | |
818 "Keymap used to override evaluation key-bindings for documentation checking.") | |
819 | |
820 ;; Add in a menubar with easy-menu | |
821 | |
822 (if checkdoc-minor-keymap | |
823 (easy-menu-define | |
824 checkdoc-minor-menu checkdoc-minor-keymap "Checkdoc Minor Mode Menu" | |
825 '("CheckDoc" | |
826 ["First Style Error" checkdoc t] | |
827 ["First Style or Spelling Error " checkdoc-ispell t] | |
828 ["Next Style Error" checkdoc-continue t] | |
829 ["Next Style or Spelling Error" checkdoc-ispell-continue t] | |
830 ["Interactive Style Check" checkdoc-interactive t] | |
831 ["Interactive Style and Spelling Check" checkdoc-ispell-interactive t] | |
832 ["Check Defun" checkdoc-defun t] | |
833 ["Check and Spell Defun" checkdoc-ispell-defun t] | |
834 ["Check and Evaluate Defun" checkdoc-eval-defun t] | |
835 ["Check Buffer" checkdoc-current-buffer t] | |
836 ["Check and Spell Buffer" checkdoc-ispell-current-buffer t] | |
837 ["Check and Evaluate Buffer" checkdoc-eval-current-buffer t] | |
838 ["Check Comment Style" checkdoc-comments buffer-file-name] | |
839 ["Check Comment Style and Spelling" checkdoc-ispell-comments | |
840 buffer-file-name] | |
841 ["Check for Rogue Spaces" checkdoc-rogue-spaces t] | |
842 ))) | |
843 ;; XEmacs requires some weird stuff to add this menu in a minor mode. | |
844 ;; What is it? | |
845 | |
846 ;; Allow re-insertion of a new keymap | |
847 (let ((a (assoc 'checkdoc-minor-mode minor-mode-map-alist))) | |
848 (if a | |
849 (setcdr a checkdoc-minor-keymap) | |
850 (checkdoc-add-to-list 'minor-mode-map-alist (cons 'checkdoc-minor-mode | |
851 checkdoc-minor-keymap)))) | |
852 | |
853 ;;;###autoload | |
854 (defun checkdoc-minor-mode (&optional arg) | |
855 "Toggle checkdoc minor mode. A mode for checking lisp doc-strings. | |
856 With prefix ARG, turn checkdoc minor mode on iff ARG is positive. | |
857 | |
858 In checkdoc minor mode, the usual bindings for `eval-defun' which is | |
859 bound to \\<checkdoc-minor-keymap> \\[checkdoc-eval-defun] and `checkdoc-eval-current-buffer' are overridden to include | |
860 checking of documentation strings. | |
861 | |
862 \\{checkdoc-minor-keymap}" | |
863 (interactive "P") | |
864 (setq checkdoc-minor-mode | |
865 (not (or (and (null arg) checkdoc-minor-mode) | |
866 (<= (prefix-numeric-value arg) 0)))) | |
867 (checkdoc-mode-line-update)) | |
868 | |
869 ;;; Subst utils | |
870 ;; | |
871 (defsubst checkdoc-run-hooks (hookvar &rest args) | |
872 "Run hooks in HOOKVAR with ARGS." | |
873 (if (fboundp 'run-hook-with-args-until-success) | |
874 (apply 'run-hook-with-args-until-success hookvar args) | |
875 ;; This method was similar to above. We ignore the warning | |
876 ;; since we will use the above for future emacs versions | |
877 (apply 'run-hook-with-args hookvar args))) | |
878 | |
879 (defsubst checkdoc-create-common-verbs-regexp () | |
880 "Rebuild the contents of `checkdoc-common-verbs-regexp'." | |
881 (or checkdoc-common-verbs-regexp | |
882 (setq checkdoc-common-verbs-regexp | |
883 (concat "\\<\\(" | |
884 (mapconcat (lambda (e) (concat (car e))) | |
885 checkdoc-common-verbs-wrong-voice "\\|") | |
886 "\\)\\>")))) | |
887 | |
888 ;; Profiler says this is not yet faster than just calling assoc | |
889 ;;(defun checkdoc-word-in-alist-vector (word vector) | |
890 ;; "Check to see if WORD is in the car of an element of VECTOR. | |
891 ;;VECTOR must be sorted. The CDR should be a replacement. Since the | |
892 ;;word list is getting bigger, it is time for a quick bisecting search." | |
893 ;; (let ((max (length vector)) (min 0) i | |
894 ;; (found nil) (fw nil)) | |
895 ;; (setq i (/ max 2)) | |
896 ;; (while (and (not found) (/= min max)) | |
897 ;; (setq fw (car (aref vector i))) | |
898 ;; (cond ((string= word fw) (setq found (cdr (aref vector i)))) | |
899 ;; ((string< word fw) (setq max i)) | |
900 ;; (t (setq min i))) | |
901 ;; (setq i (/ (+ max min) 2)) | |
902 ;; ) | |
903 ;; found)) | |
904 | |
905 ;;; Checking engines | |
906 ;; | |
907 (defun checkdoc-this-string-valid () | |
908 "Return a message string if the current doc-string is invalid. | |
909 Check for style only, such as the first line always being a complete | |
910 sentence, whitespace restrictions, and making sure there are no | |
911 hard-coded key-codes such as C-[char] or mouse-[number] in the comment. | |
912 See the style guide in the Emacs Lisp manual for more details." | |
913 | |
914 ;; Jump over comments between the last object and the doc-string | |
915 (while (looking-at "[ \t\n]*;") | |
916 (forward-line 1) | |
917 (beginning-of-line) | |
918 (skip-chars-forward " \n\t")) | |
919 | |
920 (if (not (looking-at "[ \t\n]*\"")) | |
921 nil | |
922 (let ((old-syntax-table (syntax-table))) | |
923 (unwind-protect | |
924 (progn | |
925 (set-syntax-table checkdoc-syntax-table) | |
926 (checkdoc-this-string-valid-engine)) | |
927 (set-syntax-table old-syntax-table))))) | |
928 | |
929 (defun checkdoc-this-string-valid-engine () | |
930 "Return a message string if the current doc-string is invalid. | |
931 Depends on `checkdoc-this-string-valid' to reset the syntax table so that | |
932 regexp short cuts work." | |
933 (let ((case-fold-search nil) | |
934 ;; Use a marker so if an early check modifies the text, | |
935 ;; we won't accidentally loose our place. This could cause | |
936 ;; end-of doc-string whitespace to also delete the " char. | |
937 (e (save-excursion (forward-sexp 1) (point-marker))) | |
938 (fp (checkdoc-defun-info))) | |
939 (or | |
940 ;; * *Do not* indent subsequent lines of a documentation string so that | |
941 ;; the text is lined up in the source code with the text of the first | |
942 ;; line. This looks nice in the source code, but looks bizarre when | |
943 ;; users view the documentation. Remember that the indentation | |
944 ;; before the starting double-quote is not part of the string! | |
945 (save-excursion | |
946 (forward-line 1) | |
947 (beginning-of-line) | |
948 (if (and (< (point) e) | |
949 (looking-at "\\([ \t]+\\)[^ \t\n]")) | |
950 (if (checkdoc-autofix-ask-replace (match-beginning 1) | |
951 (match-end 1) | |
952 "Remove this whitespace?" | |
953 "") | |
954 nil | |
955 "Second line should not have indentation"))) | |
956 ;; * Do not start or end a documentation string with whitespace. | |
957 (let (start end) | |
958 (if (or (if (looking-at "\"\\([ \t\n]+\\)") | |
959 (setq start (match-beginning 1) | |
960 end (match-end 1))) | |
961 (save-excursion | |
962 (forward-sexp 1) | |
963 (forward-char -1) | |
964 (if (/= (skip-chars-backward " \t\n") 0) | |
965 (setq start (point) | |
966 end (1- e))))) | |
967 (if (checkdoc-autofix-ask-replace | |
968 start end "Remove this whitespace?" "") | |
969 nil | |
970 "Documentation strings should not start or end with whitespace"))) | |
971 ;; * Every command, function, or variable intended for users to know | |
972 ;; about should have a documentation string. | |
973 ;; | |
974 ;; * An internal variable or subroutine of a Lisp program might as well | |
975 ;; have a documentation string. In earlier Emacs versions, you could | |
976 ;; save space by using a comment instead of a documentation string, | |
977 ;; but that is no longer the case. | |
978 (if (and (not (nth 1 fp)) ; not a variable | |
979 (or (nth 2 fp) ; is interactive | |
980 checkdoc-force-docstrings-flag) ;or we always complain | |
981 (not (checkdoc-char= (following-char) ?\"))) ; no doc-string | |
982 (if (nth 2 fp) | |
983 "All interactive functions should have documentation" | |
984 "All variables and subroutines might as well have a \ | |
985 documentation string")) | |
986 ;; * The first line of the documentation string should consist of one | |
987 ;; or two complete sentences that stand on their own as a summary. | |
988 ;; `M-x apropos' displays just the first line, and if it doesn't | |
989 ;; stand on its own, the result looks bad. In particular, start the | |
990 ;; first line with a capital letter and end with a period. | |
991 (save-excursion | |
992 (end-of-line) | |
993 (skip-chars-backward " \t\n") | |
994 (if (> (point) e) (goto-char e)) ;of the form (defun n () "doc" nil) | |
995 (forward-char -1) | |
996 (cond | |
997 ((and (checkdoc-char= (following-char) ?\") | |
998 ;; A backslashed double quote at the end of a sentence | |
999 (not (checkdoc-char= (preceding-char) ?\\))) | |
1000 ;; We might have to add a period in this case | |
1001 (forward-char -1) | |
1002 (if (looking-at "[.!]") | |
1003 nil | |
1004 (forward-char 1) | |
1005 (if (checkdoc-autofix-ask-replace | |
1006 (point) (1+ (point)) "Add period to sentence?" | |
1007 ".\"" t) | |
1008 nil | |
1009 "First sentence should end with punctuation."))) | |
1010 ((looking-at "[\\!;:.)]") | |
1011 ;; These are ok | |
1012 nil) | |
1013 (t | |
1014 ;; If it is not a complete sentence, lets see if we can | |
1015 ;; predict a clever way to make it one. | |
1016 (let ((msg "First line is not a complete sentence") | |
1017 (e (point))) | |
1018 (beginning-of-line) | |
1019 (if (re-search-forward "\\. +" e t) | |
1020 ;; Here we have found a complete sentence, but no break. | |
1021 (if (checkdoc-autofix-ask-replace | |
1022 (1+ (match-beginning 0)) (match-end 0) | |
1023 "First line not a complete sentence. Add CR here?" | |
1024 "\n" t) | |
1025 (let (l1 l2) | |
1026 (forward-line 1) | |
1027 (end-of-line) | |
1028 (setq l1 (current-column) | |
1029 l2 (save-excursion | |
1030 (forward-line 1) | |
1031 (end-of-line) | |
1032 (current-column))) | |
1033 (if (> (+ l1 l2 1) 80) | |
1034 (setq msg "Incomplete auto-fix. Doc-string \ | |
1035 may require more formatting.") | |
1036 ;; We can merge these lines! Replace this CR | |
1037 ;; with a space. | |
1038 (delete-char 1) (insert " ") | |
1039 (setq msg nil)))) | |
1040 ;; Lets see if there is enough room to draw the next | |
1041 ;; line's sentence up here. I often get hit w/ | |
1042 ;; auto-fill moving my words around. | |
1043 (let ((numc (progn (end-of-line) (- 80 (current-column)))) | |
1044 (p (point))) | |
1045 (forward-line 1) | |
1046 (beginning-of-line) | |
1047 (if (and (re-search-forward "[.!:\"][ \n\"]" (save-excursion | |
1048 (end-of-line) | |
1049 (point)) | |
1050 t) | |
1051 (< (current-column) numc)) | |
1052 (if (checkdoc-autofix-ask-replace | |
1053 p (1+ p) | |
1054 "1st line not a complete sentence. Join these lines?" | |
1055 " " t) | |
1056 (progn | |
1057 ;; They said yes. We have more fill work to do... | |
1058 (delete-char 1) | |
1059 (insert "\n") | |
1060 (setq msg nil)))))) | |
1061 msg)))) | |
1062 ;; Continuation of above. Make sure our sentence is capitalized. | |
1063 (save-excursion | |
1064 (skip-chars-forward "\"\\*") | |
1065 (if (looking-at "[a-z]") | |
1066 (if (checkdoc-autofix-ask-replace | |
1067 (match-beginning 0) (match-end 0) | |
1068 "Capitalize your sentence?" (upcase (match-string 0)) | |
1069 t) | |
1070 nil | |
1071 "First line should be capitalized.") | |
1072 nil)) | |
1073 ;; * For consistency, phrase the verb in the first sentence of a | |
1074 ;; documentation string as an infinitive with "to" omitted. For | |
1075 ;; instance, use "Return the cons of A and B." in preference to | |
1076 ;; "Returns the cons of A and B." Usually it looks good to do | |
1077 ;; likewise for the rest of the first paragraph. Subsequent | |
1078 ;; paragraphs usually look better if they have proper subjects. | |
1079 ;; | |
1080 ;; For our purposes, just check to first sentence. A more robust | |
1081 ;; grammar checker would be preferred for the rest of the | |
1082 ;; documentation string. | |
1083 (and checkdoc-verb-check-experimental-flag | |
1084 (save-excursion | |
1085 ;; Maybe rebuild the monster-regex | |
1086 (checkdoc-create-common-verbs-regexp) | |
1087 (let ((lim (save-excursion | |
1088 (end-of-line) | |
1089 ;; check string-continuation | |
1090 (if (checkdoc-char= (preceding-char) ?\\) | |
1091 (progn (forward-line 1) | |
1092 (end-of-line))) | |
1093 (point))) | |
1094 (rs nil) replace original (case-fold-search t)) | |
1095 (while (and (not rs) | |
1096 (re-search-forward checkdoc-common-verbs-regexp | |
1097 lim t)) | |
1098 (setq original (buffer-substring-no-properties | |
1099 (match-beginning 1) (match-end 1)) | |
1100 rs (assoc (downcase original) | |
1101 checkdoc-common-verbs-wrong-voice)) | |
1102 (if (not rs) (error "Verb voice alist corrupted.")) | |
1103 (setq replace (let ((case-fold-search nil)) | |
1104 (save-match-data | |
1105 (if (string-match "^[A-Z]" original) | |
1106 (capitalize (cdr rs)) | |
1107 (cdr rs))))) | |
1108 (if (checkdoc-autofix-ask-replace | |
1109 (match-beginning 1) (match-end 1) | |
1110 (format "Wrong voice for verb `%s'. Replace with `%s'?" | |
1111 original replace) | |
1112 replace t) | |
1113 (setq rs nil))) | |
1114 (if rs | |
1115 ;; there was a match, but no replace | |
1116 (format | |
1117 "Incorrect voice in sentence. Use `%s' instead of `%s'." | |
1118 replace original))))) | |
1119 ;; * Don't write key sequences directly in documentation strings. | |
1120 ;; Instead, use the `\\[...]' construct to stand for them. | |
1121 (save-excursion | |
1122 (let ((f nil) (m nil) (start (point)) | |
20603
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
1123 (re "[^`]\\([CMA]-[a-zA-Z]\\|\\(\\([CMA]-\\)?\ |
20085 | 1124 mouse-[0-3]\\)\\)\\>")) |
1125 ;; Find the first key sequence not in a sample | |
1126 (while (and (not f) (setq m (re-search-forward re e t))) | |
1127 (setq f (not (checkdoc-in-sample-code-p start e)))) | |
1128 (if m | |
1129 (concat | |
1130 "Keycode " (match-string 1) | |
1131 " embedded in doc-string. Use \\\\<keymap> & \\\\[function] " | |
1132 "instead")))) | |
1133 ;; It is not practical to use `\\[...]' very many times, because | |
1134 ;; display of the documentation string will become slow. So use this | |
1135 ;; to describe the most important commands in your major mode, and | |
1136 ;; then use `\\{...}' to display the rest of the mode's keymap. | |
1137 (save-excursion | |
1138 (if (re-search-forward "\\\\\\\\\\[\\w+" e t | |
1139 (1+ checkdoc-max-keyref-before-warn)) | |
1140 "Too many occurrences of \\[function]. Use \\{keymap} instead")) | |
1141 ;; * Format the documentation string so that it fits in an | |
1142 ;; Emacs window on an 80-column screen. It is a good idea | |
1143 ;; for most lines to be no wider than 60 characters. The | |
1144 ;; first line can be wider if necessary to fit the | |
1145 ;; information that ought to be there. | |
1146 (save-excursion | |
1147 (let ((start (point))) | |
1148 (while (and (< (point) e) | |
1149 (or (progn (end-of-line) (< (current-column) 80)) | |
1150 (progn (beginning-of-line) | |
1151 (re-search-forward "\\\\\\\\[[<{]" | |
1152 (save-excursion | |
1153 (end-of-line) | |
1154 (point)) t)) | |
1155 (checkdoc-in-sample-code-p start e))) | |
1156 (forward-line 1)) | |
1157 (end-of-line) | |
1158 (if (and (< (point) e) (> (current-column) 80)) | |
1159 "Some lines are over 80 columns wide"))) | |
1160 ;;* When a documentation string refers to a Lisp symbol, write it as | |
1161 ;; it would be printed (which usually means in lower case), with | |
1162 ;; single-quotes around it. For example: `lambda'. There are two | |
1163 ;; exceptions: write t and nil without single-quotes. (In this | |
1164 ;; manual, we normally do use single-quotes for those symbols.) | |
1165 (save-excursion | |
1166 (let ((found nil) (start (point)) (msg nil) (ms nil)) | |
1167 (while (and (not msg) | |
1168 (re-search-forward | |
1169 "[^([`':]\\(\\w\+[:-]\\(\\w\\|\\s_\\)+\\)[^]']" | |
1170 e t)) | |
1171 (setq ms (match-string 1)) | |
1172 (save-match-data | |
1173 ;; A . is a \s_ char, so we must remove periods from | |
1174 ;; sentences more carefully. | |
1175 (if (string-match "\\.$" ms) | |
1176 (setq ms (substring ms 0 (1- (length ms)))))) | |
1177 (if (and (not (checkdoc-in-sample-code-p start e)) | |
1178 (setq found (intern-soft ms)) | |
1179 (or (boundp found) (fboundp found))) | |
1180 (progn | |
1181 (setq msg (format "Lisp symbol %s should appear in `quotes'" | |
1182 ms)) | |
1183 (if (checkdoc-autofix-ask-replace | |
1184 (match-beginning 1) (+ (match-beginning 1) | |
1185 (length ms)) | |
1186 msg (concat "`" ms "'") t) | |
1187 (setq msg nil))))) | |
1188 msg)) | |
1189 ;; t and nil case | |
1190 (save-excursion | |
1191 (if (re-search-forward "\\(`\\(t\\|nil\\)'\\)" e t) | |
1192 (if (checkdoc-autofix-ask-replace | |
1193 (match-beginning 1) (match-end 1) | |
1194 (format "%s should not appear in quotes. Remove?" | |
1195 (match-string 2)) | |
1196 (match-string 2) t) | |
1197 nil | |
1198 "Symbols t and nil should not appear in `quotes'"))) | |
1199 ;; Here we deviate to tests based on a variable or function. | |
1200 (cond ((eq (nth 1 fp) t) | |
1201 ;; This is if we are in a variable | |
1202 (or | |
1203 ;; * The documentation string for a variable that is a | |
1204 ;; yes-or-no flag should start with words such as "Non-nil | |
1205 ;; means...", to make it clear that all non-`nil' values are | |
1206 ;; equivalent and indicate explicitly what `nil' and non-`nil' | |
1207 ;; mean. | |
1208 ;; * If a user option variable records a true-or-false | |
1209 ;; condition, give it a name that ends in `-flag'. | |
1210 | |
1211 ;; If the variable has -flag in the name, make sure | |
1212 (if (and (string-match "-flag$" (car fp)) | |
1213 (not (looking-at "\"\\*?Non-nil\\s-+means\\s-+"))) | |
1214 "Flag variable doc-strings should start: Non-nil means") | |
1215 ;; If the doc-string starts with "Non-nil means" | |
1216 (if (and (looking-at "\"\\*?Non-nil\\s-+means\\s-+") | |
1217 (not (string-match "-flag$" (car fp)))) | |
1218 "Flag variables should end in: -flag") | |
1219 ;; Done with variables | |
1220 )) | |
1221 (t | |
1222 ;; This if we are in a function definition | |
1223 (or | |
1224 ;; * When a function's documentation string mentions the value | |
1225 ;; of an argument of the function, use the argument name in | |
1226 ;; capital letters as if it were a name for that value. Thus, | |
1227 ;; the documentation string of the function `/' refers to its | |
1228 ;; second argument as `DIVISOR', because the actual argument | |
1229 ;; name is `divisor'. | |
1230 | |
1231 ;; Addendum: Make sure they appear in the doc in the same | |
1232 ;; order that they are found in the arg list. | |
1233 (let ((args (cdr (cdr (cdr (cdr fp))))) | |
1234 (last-pos 0) | |
1235 (found 1) | |
1236 (order (and (nth 3 fp) (car (nth 3 fp)))) | |
1237 (nocheck (append '("&optional" "&rest") (nth 3 fp)))) | |
1238 (while (and args found (> found last-pos)) | |
1239 (if (member (car args) nocheck) | |
1240 (setq args (cdr args)) | |
1241 (setq last-pos found | |
1242 found (save-excursion | |
1243 (re-search-forward | |
1244 (concat "\\<" (upcase (car args)) | |
1245 ;; Require whitespace OR | |
1246 ;; ITEMth<space> OR | |
1247 ;; ITEMs<space> | |
1248 "\\(\\>\\|th\\>\\|s\\>\\)") | |
1249 e t))) | |
1250 (if (not found) | |
1251 (let ((case-fold-search t)) | |
1252 ;; If the symbol was not found, lets see if we | |
1253 ;; can find it with a different capitalization | |
1254 ;; and see if the user wants to capitalize it. | |
1255 (if (save-excursion | |
1256 (re-search-forward | |
1257 (concat "\\<\\(" (car args) | |
1258 ;; Require whitespace OR | |
1259 ;; ITEMth<space> OR | |
1260 ;; ITEMs<space> | |
1261 "\\)\\(\\>\\|th\\>\\|s\\>\\)") | |
1262 e t)) | |
1263 (if (checkdoc-autofix-ask-replace | |
1264 (match-beginning 1) (match-end 1) | |
1265 (format | |
1266 "Argument `%s' should appear as `%s'. Fix?" | |
1267 (car args) (upcase (car args))) | |
1268 (upcase (car args)) t) | |
1269 (setq found (match-beginning 1)))))) | |
1270 (if found (setq args (cdr args))))) | |
1271 (if (not found) | |
1272 (format | |
1273 "Argument `%s' should appear as `%s' in the doc-string" | |
1274 (car args) (upcase (car args))) | |
1275 (if (or (and order (eq order 'yes)) | |
1276 (and (not order) checkdoc-arguments-in-order-flag)) | |
1277 (if (< found last-pos) | |
1278 "Arguments occur in the doc-string out of order")))) | |
1279 ;; Done with functions | |
1280 ))) | |
1281 ;; Make sure the doc-string has correctly spelled english words | |
1282 ;; in it. This functions is extracted due to it's complexity, | |
1283 ;; and reliance on the ispell program. | |
1284 (checkdoc-ispell-docstring-engine e) | |
1285 ;; User supplied checks | |
1286 (save-excursion (checkdoc-run-hooks 'checkdoc-style-hooks fp e)) | |
1287 ;; Done! | |
1288 ))) | |
1289 | |
1290 (defun checkdoc-defun-info nil | |
1291 "Return a list of details about the current sexp. | |
1292 It is a list of the form: | |
1293 '( NAME VARIABLE INTERACTIVE NODOCPARAMS PARAMETERS ... ) | |
1294 where NAME is the name, VARIABLE is t if this is a `defvar', | |
1295 INTERACTIVE is nil if this is not an interactive function, otherwise | |
1296 it is the position of the `interactive' call, and PARAMETERS is a | |
1297 string which is the name of each variable in the function's argument | |
1298 list. The NODOCPARAMS is a sublist of parameters specified by a checkdoc | |
1299 comment for a given defun. If the first element is not a string, then | |
1300 the token checkdoc-order: <TOKEN> exists, and TOKEN is a symbol read | |
1301 from the comment." | |
1302 (save-excursion | |
1303 (beginning-of-defun) | |
1304 (let ((defun (looking-at "(def\\(un\\|macro\\|subst\\|advice\\)")) | |
1305 (is-advice (looking-at "(defadvice")) | |
1306 (lst nil) | |
1307 (ret nil) | |
1308 (oo (make-vector 3 0))) ;substitute obarray for `read' | |
1309 (forward-char 1) | |
1310 (forward-sexp 1) | |
1311 (skip-chars-forward " \n\t") | |
1312 (setq ret | |
1313 (list (buffer-substring-no-properties | |
1314 (point) (progn (forward-sexp 1) (point))))) | |
1315 (if (not defun) | |
1316 (setq ret (cons t ret)) | |
1317 ;; The variable spot | |
1318 (setq ret (cons nil ret)) | |
1319 ;; Interactive | |
1320 (save-excursion | |
1321 (setq ret (cons | |
1322 (re-search-forward "(interactive" | |
1323 (save-excursion (end-of-defun) (point)) | |
1324 t) | |
1325 ret))) | |
1326 (skip-chars-forward " \t\n") | |
1327 (let ((bss (buffer-substring (point) (save-excursion (forward-sexp 1) | |
1328 (point)))) | |
1329 ;; Overload th main obarray so read doesn't intern the | |
1330 ;; local symbols of the function we are checking. | |
1331 ;; Without this we end up cluttering the symbol space w/ | |
1332 ;; useless symbols. | |
1333 (obarray oo)) | |
1334 ;; Ok, check for checkdoc parameter comment here | |
1335 (save-excursion | |
1336 (setq ret | |
1337 (cons | |
1338 (let ((sl1 nil)) | |
1339 (if (re-search-forward ";\\s-+checkdoc-order:\\s-+" | |
1340 (save-excursion (end-of-defun) | |
1341 (point)) | |
1342 t) | |
1343 (setq sl1 (list (cond ((looking-at "nil") 'no) | |
1344 ((looking-at "t") 'yes))))) | |
1345 (if (re-search-forward ";\\s-+checkdoc-params:\\s-+" | |
1346 (save-excursion (end-of-defun) | |
1347 (point)) | |
1348 t) | |
1349 (let ((sl nil)) | |
1350 (goto-char (match-end 0)) | |
1351 (setq lst (read (current-buffer))) | |
1352 (while lst | |
1353 (setq sl (cons (symbol-name (car lst)) sl) | |
1354 lst (cdr lst))) | |
1355 (setq sl1 (append sl1 sl)))) | |
1356 sl1) | |
1357 ret))) | |
1358 ;; Read the list of paramters, but do not put the symbols in | |
1359 ;; the standard obarray. | |
1360 (setq lst (read bss))) | |
1361 ;; This is because read will intern nil if it doesn't into the | |
1362 ;; new obarray. | |
1363 (if (not (listp lst)) (setq lst nil)) | |
1364 (if is-advice nil | |
1365 (while lst | |
1366 (setq ret (cons (symbol-name (car lst)) ret) | |
1367 lst (cdr lst))))) | |
1368 (nreverse ret)))) | |
1369 | |
1370 (defun checkdoc-in-sample-code-p (start limit) | |
1371 "Return Non-nil if the current point is in a code-fragment. | |
1372 A code fragment is identified by an open parenthesis followed by a | |
1373 symbol which is a valid function, or a parenthesis that is quoted with the ' | |
1374 character. Only the region from START to LIMIT is is allowed while | |
1375 searching for the bounding parenthesis." | |
1376 (save-match-data | |
1377 (save-restriction | |
1378 (narrow-to-region start limit) | |
1379 (save-excursion | |
1380 (and (condition-case nil (progn (up-list 1) t) (error nil)) | |
1381 (condition-case nil (progn (forward-list -1) t) (error nil)) | |
1382 (or (save-excursion (forward-char -1) (looking-at "'(")) | |
1383 (and (looking-at "(\\(\\(\\w\\|[-:_]\\)+\\)[ \t\n;]") | |
1384 (let ((ms (buffer-substring-no-properties | |
1385 (match-beginning 1) (match-end 1)))) | |
1386 ;; if this string is function bound, we are in | |
1387 ;; sample code. If it has a - or : character in | |
1388 ;; the name, then it is probably supposed to be bound | |
1389 ;; but isn't yet. | |
1390 (or (fboundp (intern-soft ms)) | |
1391 (string-match "\\w[-:_]+\\w" ms)))))))))) | |
1392 | |
1393 ;;; Ispell engine | |
1394 ;; | |
1395 (eval-when-compile (require 'ispell)) | |
1396 | |
1397 (defun checkdoc-ispell-init () | |
1398 "Initialize ispell process (default version) with lisp words. | |
1399 The words used are from `checkdoc-ispell-lisp-words'. If `ispell' | |
1400 cannot be loaded, then set `checkdoc-spellcheck-documentation-flag' to | |
1401 nil." | |
1402 (require 'ispell) | |
1403 (if (not (symbol-value 'ispell-process)) ;Silence byteCompiler | |
1404 (condition-case nil | |
1405 (progn | |
1406 (ispell-buffer-local-words) | |
1407 ;; This code copied in part from ispell.el emacs 19.34 | |
1408 (let ((w checkdoc-ispell-lisp-words)) | |
1409 (while w | |
1410 (process-send-string | |
1411 ;; Silence byte compiler | |
1412 (symbol-value 'ispell-process) | |
1413 (concat "@" (car w) "\n")) | |
1414 (setq w (cdr w))))) | |
1415 (error (setq checkdoc-spellcheck-documentation-flag nil))))) | |
1416 | |
1417 (defun checkdoc-ispell-docstring-engine (end) | |
1418 "Run the ispell tools on the doc-string between point and END. | |
1419 Since ispell isn't lisp smart, we must pre-process the doc-string | |
1420 before using the ispell engine on it." | |
1421 (if (not checkdoc-spellcheck-documentation-flag) | |
1422 nil | |
1423 (checkdoc-ispell-init) | |
1424 (save-excursion | |
1425 (skip-chars-forward "^a-zA-Z") | |
1426 (let ((word nil) (sym nil) (case-fold-search nil) (err nil)) | |
1427 (while (and (not err) (< (point) end)) | |
1428 (if (save-excursion (forward-char -1) (looking-at "[('`]")) | |
1429 ;; Skip lists describing meta-syntax, or bound variables | |
1430 (forward-sexp 1) | |
1431 (setq word (buffer-substring-no-properties | |
1432 (point) (progn | |
1433 (skip-chars-forward "a-zA-Z-") | |
1434 (point))) | |
1435 sym (intern-soft word)) | |
1436 (if (and sym (or (boundp sym) (fboundp sym))) | |
1437 ;; This is probably repetative in most cases, but not always. | |
1438 nil | |
1439 ;; Find out how we spell-check this word. | |
1440 (if (or | |
20603
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
1441 ;; All caps w/ option th, or s tacked on the end |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
1442 ;; for pluralization or nuberthness. |
24dda0afd915
Added some more comments in the commentary.
Eric M. Ludlam <zappo@gnu.org>
parents:
20085
diff
changeset
|
1443 (string-match "^[A-Z][A-Z]+\\(s\\|th\\)?$" word) |
20085 | 1444 (looking-at "}") ; a keymap expression |
1445 ) | |
1446 nil | |
1447 (save-excursion | |
1448 (if (not (eq checkdoc-autofix-flag 'never)) | |
1449 (let ((lk last-input-event)) | |
1450 (ispell-word nil t) | |
1451 (if (not (equal last-input-event lk)) | |
1452 (progn | |
1453 (sit-for 0) | |
1454 (message "Continuing...")))) | |
1455 ;; Nothing here. | |
1456 ))))) | |
1457 (skip-chars-forward "^a-zA-Z")) | |
1458 err)))) | |
1459 | |
1460 ;;; Rogue space checking engine | |
1461 ;; | |
1462 (defun checkdoc-rogue-space-check-engine (&optional start end) | |
1463 "Return a message string if there is a line with white space at the end. | |
1464 If `checkdoc-autofix-flag' permits, delete that whitespace instead. | |
1465 If optional arguments START and END are non nil, bound the check to | |
1466 this region." | |
1467 (let ((p (point)) | |
1468 (msg nil)) | |
1469 (if (not start) (setq start (point-min))) | |
1470 ;; If end is nil, it means end of buffer to search anyway | |
1471 (or | |
1472 ;; Checkfor and error if `? ' or `?\ ' is used at the end of a line. | |
1473 ;; (It's dangerous) | |
1474 (progn | |
1475 (goto-char start) | |
1476 (if (re-search-forward "\\?\\\\?[ \t][ \t]*$" end t) | |
1477 (setq msg | |
1478 "Don't use `? ' at the end of a line. \ | |
1479 Some editors & news agents may remove it"))) | |
1480 ;; Check for, and pottentially remove whitespace appearing at the | |
1481 ;; end of different lines. | |
1482 (progn | |
1483 (goto-char start) | |
1484 ;; There is no documentation in the elisp manual about this check, | |
1485 ;; it is intended to help clean up messy code and reduce the file size. | |
1486 (while (and (not msg) (re-search-forward "[^ \t\n]\\([ \t]+\\)$" end t)) | |
1487 ;; This is not a complex activity | |
1488 (if (checkdoc-autofix-ask-replace | |
1489 (match-beginning 1) (match-end 1) | |
1490 "White space at end of line. Remove?" "") | |
1491 nil | |
1492 (setq msg "White space found at end of line."))))) | |
1493 ;; Return an error and leave the cursor at that spot, or restore | |
1494 ;; the cursor. | |
1495 (if msg | |
1496 msg | |
1497 (goto-char p) | |
1498 nil))) | |
1499 | |
1500 ;;; Comment checking engine | |
1501 ;; | |
1502 (eval-when-compile | |
1503 ;; We must load this to: | |
1504 ;; a) get symbols for comple and | |
1505 ;; b) determine if we have lm-history symbol which doesn't always exist | |
1506 (require 'lisp-mnt)) | |
1507 | |
1508 (defun checkdoc-file-comments-engine () | |
1509 "Return a message string if this file does not match the emacs standard. | |
1510 This checks for style only, such as the first line, Commentary:, | |
1511 Code:, and others referenced in the style guide." | |
1512 (if (featurep 'lisp-mnt) | |
1513 nil | |
1514 (require 'lisp-mnt) | |
1515 ;; Old Xemacs don't have `lm-commentary-mark' | |
1516 (if (and (not (fboundp 'lm-commentary-mark)) (boundp 'lm-commentary)) | |
1517 (defalias 'lm-commentary-mark 'lm-commentary))) | |
1518 (save-excursion | |
1519 (let* ((f1 (file-name-nondirectory (buffer-file-name))) | |
1520 (fn (file-name-sans-extension f1)) | |
1521 (fe (substring f1 (length fn)))) | |
1522 (goto-char (point-min)) | |
1523 (or | |
1524 ;; Lisp Maintenance checks first | |
1525 ;; Was: (lm-verify) -> not flexible enough for some people | |
1526 ;; * Summary at the beginning of the file: | |
1527 (if (not (lm-summary)) | |
1528 ;; This certifies as very complex so always ask unless | |
1529 ;; it's set to never | |
1530 (if (and checkdoc-autofix-flag | |
1531 (not (eq checkdoc-autofix-flag 'never)) | |
1532 (y-or-n-p "There is no first line summary! Add one?")) | |
1533 (progn | |
1534 (goto-char (point-min)) | |
1535 (insert ";;; " fn fe " --- " (read-string "Summary: ") "\n")) | |
1536 "The first line should be of the form: \";;; package --- Summary\"") | |
1537 nil) | |
1538 ;; * Commentary Section | |
1539 (if (not (lm-commentary-mark)) | |
1540 "You should have a section marked \";;; Commentary:\"" | |
1541 nil) | |
1542 ;; * History section. Say nothing if there is a file ChangeLog | |
1543 (if (or (file-exists-p "ChangeLog") | |
1544 (let ((fn 'lm-history-mark)) ;bestill byte-compiler | |
1545 (and (fboundp fn) (funcall fn)))) | |
1546 nil | |
1547 "You should have a section marked \";;; History:\" or use a ChangeLog") | |
1548 ;; * Code section | |
1549 (if (not (lm-code-mark)) | |
1550 (let ((cont t)) | |
1551 (goto-char (point-min)) | |
1552 (while (and cont (re-search-forward "^(" nil t)) | |
1553 (setq cont (looking-at "require\\s-+"))) | |
1554 (if (and (not cont) | |
1555 checkdoc-autofix-flag | |
1556 (not (eq checkdoc-autofix-flag 'never)) | |
1557 (y-or-n-p "There is no ;;; Code: marker. Insert one? ")) | |
1558 (progn (beginning-of-line) | |
1559 (insert ";;; Code:\n") | |
1560 nil) | |
1561 "You should have a section marked \";;; Code:\"")) | |
1562 nil) | |
1563 ;; * A footer. Not compartamentalized from lm-verify: too bad. | |
1564 ;; The following is partially clipped from lm-verify | |
1565 (save-excursion | |
1566 (goto-char (point-max)) | |
1567 (if (not (re-search-backward | |
1568 (concat "^;;;[ \t]+" fn "\\(" (regexp-quote fe) | |
1569 "\\)?[ \t]+ends here[ \t]*$" | |
1570 "\\|^;;;[ \t]+ End of file[ \t]+" | |
1571 fn "\\(" (regexp-quote fe) "\\)?") | |
1572 nil t)) | |
1573 (if (and checkdoc-autofix-flag | |
1574 (not (eq checkdoc-autofix-flag 'never)) | |
1575 (y-or-n-p "No identifiable footer! Add one?")) | |
1576 (progn | |
1577 (goto-char (point-max)) | |
1578 (insert "\n(provide '" fn ")\n;;; " fn fe " ends here\n")) | |
1579 (format "The footer should be (provide '%s)\\n;;; %s%s ends here" | |
1580 fn fn fe)))) | |
1581 ;; Ok, now lets look for multiple occurances of ;;;, and offer | |
1582 ;; to remove the extra ";" if applicable. This pre-supposes | |
1583 ;; that the user has semiautomatic fixing on to be useful. | |
1584 | |
1585 ;; In the info node (elisp)Library Headers a header is three ; | |
1586 ;; (the header) followed by text of only two ; | |
1587 ;; In (elisp)Comment Tips, however it says this: | |
1588 ;; * Another use for triple-semicolon comments is for commenting out | |
1589 ;; lines within a function. We use triple-semicolons for this | |
1590 ;; precisely so that they remain at the left margin. | |
1591 (let ((msg nil)) | |
1592 (goto-char (point-min)) | |
1593 (while (and checkdoc-tripple-semi-comment-check-flag | |
1594 (not msg) (re-search-forward "^;;;[^;]" nil t)) | |
1595 ;; We found a triple, lets check all following lines. | |
1596 (if (not (bolp)) (progn (beginning-of-line) (forward-line 1))) | |
1597 (let ((complex-replace t)) | |
1598 (while (looking-at ";;\\(;\\)[^;]") | |
1599 (if (and (checkdoc-outside-major-sexp) ;in code is ok. | |
1600 (checkdoc-autofix-ask-replace | |
1601 (match-beginning 1) (match-end 1) | |
1602 "Multiple occurances of ;;; found. Use ;; instead?" "" | |
1603 complex-replace)) | |
1604 ;; Learn that, yea, the user did want to do this a | |
1605 ;; whole bunch of times. | |
1606 (setq complex-replace nil)) | |
1607 (beginning-of-line) | |
1608 (forward-line 1))))) | |
1609 ;; Lets spellcheck the commentary section. This is the only | |
1610 ;; section that is easy to pick out, and it is also the most | |
1611 ;; visible section (with the finder) | |
1612 (save-excursion | |
1613 (goto-char (lm-commentary-mark)) | |
1614 ;; Spellcheck between the commentary, and the first | |
1615 ;; non-comment line. We could use lm-commentary, but that | |
1616 ;; returns a string, and ispell wants to talk to a buffer. | |
1617 ;; Since the comments talk about lisp, use the specialized | |
1618 ;; spell-checker we also used for doc-strings. | |
1619 (checkdoc-ispell-docstring-engine (save-excursion | |
1620 (re-search-forward "^[^;]" nil t) | |
1621 (point)))) | |
1622 ;;; test comment out code | |
1623 ;;; (foo 1 3) | |
1624 ;;; (bar 5 7) | |
1625 ;; Generic Full-file checks (should be comment related) | |
1626 (checkdoc-run-hooks 'checkdoc-comment-style-hooks) | |
1627 ;; Done with full file comment checks | |
1628 )))) | |
1629 | |
1630 (defun checkdoc-outside-major-sexp () | |
1631 "Return t if point is outside the bounds of a valid sexp." | |
1632 (save-match-data | |
1633 (save-excursion | |
1634 (let ((p (point))) | |
1635 (or (progn (beginning-of-defun) (bobp)) | |
1636 (progn (end-of-defun) (< (point) p))))))) | |
1637 | |
1638 ;;; Auto-fix helper functions | |
1639 ;; | |
1640 (defun checkdoc-autofix-ask-replace (start end question replacewith | |
1641 &optional complex) | |
1642 "Highlight between START and END and queries the user with QUESTION. | |
1643 If the user says yes, or if `checkdoc-autofix-flag' permits, replace | |
1644 the region marked by START and END with REPLACEWITH. If optional flag | |
1645 COMPLEX is non-nil, then we may ask the user a question. See the | |
1646 documentation for `checkdoc-autofix-flag' for details. | |
1647 | |
1648 If a section is auto-replaced without asking the user, this function | |
1649 will pause near the fixed code so the user will briefly see what | |
1650 happened. | |
1651 | |
1652 This function returns non-nil if the text was replaced." | |
1653 (if checkdoc-autofix-flag | |
1654 (let ((o (checkdoc-make-overlay start end)) | |
1655 (ret nil)) | |
1656 (unwind-protect | |
1657 (progn | |
1658 (checkdoc-overlay-put o 'face 'highlight) | |
1659 (if (or (eq checkdoc-autofix-flag 'automatic) | |
1660 (and (eq checkdoc-autofix-flag 'semiautomatic) | |
1661 (not complex)) | |
1662 (and (or (eq checkdoc-autofix-flag 'query) complex) | |
1663 (y-or-n-p question))) | |
1664 (save-excursion | |
1665 (goto-char start) | |
1666 ;; On the off chance this is automatic, display | |
1667 ;; the question anyway so the user knows whats | |
1668 ;; going on. | |
1669 (if checkdoc-bouncy-flag (message "%s -> done" question)) | |
1670 (delete-region start end) | |
1671 (insert replacewith) | |
1672 (if checkdoc-bouncy-flag (sit-for 0)) | |
1673 (setq ret t))) | |
1674 (checkdoc-delete-overlay o)) | |
1675 (checkdoc-delete-overlay o)) | |
1676 ret))) | |
1677 | |
1678 ;;; Warning management | |
1679 ;; | |
1680 (defvar checkdoc-output-font-lock-keywords | |
1681 '(("\\(\\w+\\.el\\):" 1 font-lock-function-name-face) | |
1682 ("style check: \\(\\w+\\)" 1 font-lock-comment-face) | |
20953
f3f9df46d008
Changed font-lock-reference-face to font-lock-constant-face.
Simon Marshall <simon@gnu.org>
parents:
20603
diff
changeset
|
1683 ("^\\([0-9]+\\):" 1 font-lock-constant-face)) |
20085 | 1684 "Keywords used to highlight a checkdoc diagnostic buffer.") |
1685 | |
1686 (defvar checkdoc-output-mode-map nil | |
1687 "Keymap used in `checkdoc-output-mode'.") | |
1688 | |
1689 (if checkdoc-output-mode-map | |
1690 nil | |
1691 (setq checkdoc-output-mode-map (make-sparse-keymap)) | |
1692 (if (not (string-match "XEmacs" emacs-version)) | |
1693 (define-key checkdoc-output-mode-map [mouse-2] | |
1694 'checkdoc-find-error-mouse)) | |
1695 (define-key checkdoc-output-mode-map "\C-c\C-c" 'checkdoc-find-error) | |
1696 (define-key checkdoc-output-mode-map "\C-m" 'checkdoc-find-error)) | |
1697 | |
1698 (defun checkdoc-output-mode () | |
1699 "Create and setup the buffer used to maintain checkdoc warnings. | |
1700 \\<checkdoc-output-mode-map>\\[checkdoc-find-error] - Go to this error location | |
1701 \\[checkdoc-find-error-mouse] - Goto the error clicked on." | |
1702 (if (get-buffer checkdoc-diagnostic-buffer) | |
1703 (get-buffer checkdoc-diagnostic-buffer) | |
1704 (save-excursion | |
1705 (set-buffer (get-buffer-create checkdoc-diagnostic-buffer)) | |
1706 (kill-all-local-variables) | |
1707 (setq mode-name "Checkdoc" | |
1708 major-mode 'checkdoc-output-mode) | |
1709 (set (make-local-variable 'font-lock-defaults) | |
1710 '((checkdoc-output-font-lock-keywords) t t ((?- . "w") (?_ . "w")))) | |
1711 (use-local-map checkdoc-output-mode-map) | |
1712 (run-hooks 'checkdoc-output-mode-hook) | |
1713 (current-buffer)))) | |
1714 | |
1715 (defun checkdoc-find-error-mouse (e) | |
1716 ;; checkdoc-params: (e) | |
1717 "Call `checkdoc-find-error' where the user clicks the mouse." | |
1718 (interactive "e") | |
1719 (mouse-set-point e) | |
1720 (checkdoc-find-error)) | |
1721 | |
1722 (defun checkdoc-find-error () | |
1723 "In a checkdoc diagnostic buffer, find the error under point." | |
1724 (interactive) | |
1725 (beginning-of-line) | |
1726 (if (looking-at "[0-9]+") | |
1727 (let ((l (string-to-int (match-string 0))) | |
1728 (f (save-excursion | |
1729 (re-search-backward " \\(\\(\\w+\\|\\s_\\)+\\.el\\):") | |
1730 (match-string 1)))) | |
1731 (if (not (get-buffer f)) | |
1732 (error "Can't find buffer %s" f)) | |
1733 (switch-to-buffer-other-window (get-buffer f)) | |
1734 (goto-line l)))) | |
1735 | |
1736 (defun checkdoc-start-section (check-type) | |
1737 "Initialize the checkdoc diagnostic buffer for a pass. | |
1738 Create the header so that the string CHECK-TYPE is displayed as the | |
1739 function called to create the messages." | |
1740 (checkdoc-output-to-error-buffer | |
1741 "\n\n*** " (current-time-string) " " | |
1742 (file-name-nondirectory (buffer-file-name)) ": style check: " check-type | |
1743 " V " checkdoc-version)) | |
1744 | |
1745 (defun checkdoc-error (point msg) | |
1746 "Store POINT and MSG as errors in the checkdoc diagnostic buffer." | |
1747 (checkdoc-output-to-error-buffer | |
1748 "\n" (int-to-string (count-lines (point-min) (or point 1))) ": " | |
1749 msg)) | |
1750 | |
1751 (defun checkdoc-output-to-error-buffer (&rest text) | |
1752 "Place TEXT into the checkdoc diagnostic buffer." | |
1753 (save-excursion | |
1754 (set-buffer (checkdoc-output-mode)) | |
1755 (goto-char (point-max)) | |
1756 (apply 'insert text))) | |
1757 | |
1758 (defun checkdoc-show-diagnostics () | |
1759 "Display the checkdoc diagnostic buffer in a temporary window." | |
1760 (let ((b (get-buffer checkdoc-diagnostic-buffer))) | |
1761 (if b (progn (pop-to-buffer b) | |
1762 (beginning-of-line))) | |
1763 (other-window -1) | |
1764 (shrink-window-if-larger-than-buffer))) | |
1765 | |
1766 (defgroup checkdoc nil | |
1767 "Support for doc-string checking in emacs lisp." | |
1768 :prefix "checkdoc" | |
1769 :group 'lisp) | |
1770 | |
1771 (custom-add-option 'emacs-lisp-mode-hook | |
1772 (lambda () (checkdoc-minor-mode 1))) | |
1773 | |
1774 (provide 'checkdoc) | |
1775 ;;; checkdoc.el ends here |