]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/checkdoc.el
*** empty log message ***
[gnu-emacs] / lisp / emacs-lisp / checkdoc.el
1 ;;; checkdoc.el --- check documentation strings for style requirements
2
3 ;;; Copyright (C) 1997, 1998, 2001 Free Software Foundation
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Version: 0.6.2
7 ;; Keywords: docs, maint, lisp
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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' or `checkdoc-current-buffer'.
36 ;; `checkdoc' is a more interactive version of
37 ;; `checkdoc-current-buffer'
38 ;; 2) Use `checkdoc-minor-mode' to automatically check your
39 ;; documentation whenever you evaluate Lisp code with C-M-x
40 ;; or [menu-bar emacs-lisp eval-buffer]. Additional key-bindings
41 ;; are also provided under C-c ? KEY
42 ;; (require 'checkdoc)
43 ;; (add-hook 'emacs-lisp-mode-hook
44 ;; '(lambda () (checkdoc-minor-mode 1)))
45 ;;
46 ;; Using `checkdoc':
47 ;;
48 ;; The commands `checkdoc' and `checkdoc-ispell' are the top-level
49 ;; entry points to all of the different checks that are available. It
50 ;; breaks examination of your Lisp file into four sections (comments,
51 ;; documentation, messages, and spacing) and indicates its current
52 ;; state in a status buffer.
53 ;;
54 ;; The Comments check examines your headers, footers, and
55 ;; various tags (such as "Code:") to make sure that your code is ready
56 ;; for easy integration into existing systems.
57 ;;
58 ;; The Documentation check deals with documentation strings
59 ;; and their elements that help make Emacs easier to use.
60 ;;
61 ;; The Messages check ensures that the strings displayed in the
62 ;; minibuffer by some commands (such as `error' and `y-or-n-p')
63 ;; are consistent with the Emacs environment.
64 ;;
65 ;; The Spacing check cleans up white-space at the end of lines.
66 ;;
67 ;; The interface while working with documentation and messages is
68 ;; slightly different when being run in the interactive mode. The
69 ;; interface offers several options, including the ability to skip to
70 ;; the next error, or back up to previous errors. Auto-fixing is
71 ;; turned off at this stage, but you can use the `f' or `F' key to fix
72 ;; a given error (if the fix is available.)
73 ;;
74 ;; Auto-fixing:
75 ;;
76 ;; There are four classifications of style errors in terms of how
77 ;; easy they are to fix. They are simple, complex, really complex,
78 ;; and impossible. (Impossible really means that checkdoc does not
79 ;; have a fixing routine yet.) Typically white-space errors are
80 ;; classified as simple, and are auto-fixed by default. Typographic
81 ;; changes are considered complex, and the user is asked if they want
82 ;; the problem fixed before checkdoc makes the change. These changes
83 ;; can be done without asking if `checkdoc-autofix-flag' is properly
84 ;; set. Potentially redundant changes are considered really complex,
85 ;; and the user is always asked before a change is inserted. The
86 ;; variable `checkdoc-autofix-flag' controls how these types of errors
87 ;; are fixed.
88 ;;
89 ;; Spell checking text:
90 ;;
91 ;; The variable `checkdoc-spellcheck-documentation-flag' can be set
92 ;; to customize how spell checking is to be done. Since spell
93 ;; checking can be quite slow, you can optimize how best you want your
94 ;; checking done. The default is 'defun, which spell checks each time
95 ;; `checkdoc-defun' or `checkdoc-eval-defun' is used. Setting to nil
96 ;; prevents spell checking during normal usage.
97 ;; Setting this variable to nil does not mean you cannot take
98 ;; advantage of the spell checking. You can instead use the
99 ;; interactive functions `checkdoc-ispell-*' to check the spelling of
100 ;; your documentation.
101 ;; There is a list of Lisp-specific words which checkdoc will
102 ;; install into Ispell on the fly, but only if Ispell is not already
103 ;; running. Use `ispell-kill-ispell' to make checkdoc restart it with
104 ;; these words enabled.
105 ;;
106 ;; Checking parameters:
107 ;;
108 ;; You might not always want a function to have its parameters listed
109 ;; in order. When this is the case, put the following comment just in
110 ;; front of the documentation string: "; checkdoc-order: nil" This
111 ;; overrides the value of `checkdoc-arguments-in-order-flag'.
112 ;;
113 ;; If you specifically wish to avoid mentioning a parameter of a
114 ;; function in the doc string (such as a hidden parameter, or a
115 ;; parameter which is very obvious like events), you can have checkdoc
116 ;; skip looking for it by putting the following comment just in front
117 ;; of the documentation string: "; checkdoc-params: (args go here)"
118 ;;
119 ;; Checking message strings:
120 ;;
121 ;; The text that follows the `error' and `y-or-n-p' commands is
122 ;; also checked. The documentation for `error' clearly states some
123 ;; simple style rules to follow which checkdoc will auto-fix for you.
124 ;; `y-or-n-p' also states that it should end in a space. I added that
125 ;; it should end in "? " since that is almost always used.
126 ;;
127 ;; Adding your own checks:
128 ;;
129 ;; You can experiment with adding your own checks by setting the
130 ;; hooks `checkdoc-style-hooks' and `checkdoc-comment-style-hooks'.
131 ;; Return a string which is the error you wish to report. The cursor
132 ;; position should be preserved.
133 ;;
134 ;; Error errors:
135 ;;
136 ;; Checkdoc does not always flag errors correctly. There are a
137 ;; couple ways you can coax your file into passing all of checkdoc's
138 ;; tests through buffer local variables.
139 ;;
140 ;; The variable `checkdoc-verb-check-experimental-flag' can be used
141 ;; to turn off the check for verb-voice in case you use words that are
142 ;; not semantically verbs, but are still in the incomplete list.
143 ;;
144 ;; The variable `checkdoc-symbol-words' can be a list of words that
145 ;; happen to also be symbols. This is not a problem for one-word
146 ;; symbols, but if you use a hyphenated word that is also a symbol,
147 ;; then you may need this.
148 ;;
149 ;; The symbol `checkdoc-force-docstrings-flag' can be set to nil if
150 ;; you have many undocumented functions you don't wish to document.
151 ;;
152 ;; See the above section "Checking Parameters" for details about
153 ;; parameter checking.
154 ;;
155 ;; Dependencies:
156 ;;
157 ;; This file requires lisp-mnt (Lisp maintenance routines) for the
158 ;; comment checkers.
159 ;;
160 ;; Requires custom for Emacs v20.
161
162 ;;; TO DO:
163 ;; Hook into the byte compiler on a defun/defvar level to generate
164 ;; warnings in the byte-compiler's warning/error buffer.
165 ;; Better ways to override more typical `eval' functions. Advice
166 ;; might be good but hard to turn on/off as a minor mode.
167 ;;
168 ;;; Maybe Do:
169 ;; Code sweep checks for "forbidden functions", proper use of hooks,
170 ;; proper keybindings, and other items from the manual that are
171 ;; not specifically docstring related. Would this even be useful?
172
173 ;;; Code:
174 (defvar checkdoc-version "0.6.1"
175 "Release version of checkdoc you are currently running.")
176
177 ;; From custom web page for compatibility between versions of custom:
178 (eval-and-compile
179 (condition-case ()
180 (require 'custom)
181 (error nil))
182 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
183 nil ;; We've got what we needed
184 ;; We have the old custom-library, hack around it!
185 (defmacro defgroup (&rest args)
186 nil)
187 (defmacro custom-add-option (&rest args)
188 nil)
189 (defmacro defcustom (var value doc &rest args)
190 (` (defvar (, var) (, value) (, doc))))))
191
192 (defcustom checkdoc-autofix-flag 'semiautomatic
193 "*Non-nil means attempt auto-fixing of doc strings.
194 If this value is the symbol `query', then the user is queried before
195 any change is made. If the value is `automatic', then all changes are
196 made without asking unless the change is very-complex. If the value
197 is `semiautomatic' or any other value, then simple fixes are made
198 without asking, and complex changes are made by asking the user first.
199 The value `never' is the same as nil, never ask or change anything."
200 :group 'checkdoc
201 :type '(choice (const automatic)
202 (const query)
203 (const never)
204 (other :tag "semiautomatic" semiautomatic)))
205
206 (defcustom checkdoc-bouncy-flag t
207 "*Non-nil means to \"bounce\" to auto-fix locations.
208 Setting this to nil will silently make fixes that require no user
209 interaction. See `checkdoc-autofix-flag' for auto-fixing details."
210 :group 'checkdoc
211 :type 'boolean)
212
213 (defcustom checkdoc-force-docstrings-flag t
214 "*Non-nil means that all checkable definitions should have documentation.
215 Style guide dictates that interactive functions MUST have documentation,
216 and that it's good but not required practice to make non user visible items
217 have doc strings."
218 :group 'checkdoc
219 :type 'boolean)
220
221 (defcustom checkdoc-force-history-flag t
222 "*Non-nil means that files should have a History section or ChangeLog file.
223 This helps document the evolution of, and recent changes to, the package."
224 :group 'checkdoc
225 :type 'boolean)
226
227 (defcustom checkdoc-permit-comma-termination-flag nil
228 "*Non-nil means the first line of a docstring may end with a comma.
229 Ordinarily, a full sentence is required. This may be misleading when
230 there is a substantial caveat to the one-line description -- the comma
231 should be used when the first part could stand alone as a sentence, but
232 it indicates that a modifying clause follows."
233 :group 'checkdoc
234 :type 'boolean)
235
236 (defcustom checkdoc-spellcheck-documentation-flag nil
237 "*Non-nil means run Ispell on text based on value.
238 This is automatically set to nil if Ispell does not exist on your
239 system. Possible values are:
240
241 nil - Don't spell-check during basic style checks.
242 defun - Spell-check when style checking a single defun
243 buffer - Spell-check when style checking the whole buffer
244 interactive - Spell-check during any interactive check.
245 t - Always spell-check"
246 :group 'checkdoc
247 :type '(choice (const nil)
248 (const defun)
249 (const buffer)
250 (const interactive)
251 (const t)))
252
253 (defvar checkdoc-ispell-lisp-words
254 '("alist" "emacs" "etags" "iff" "keymap" "paren" "regexp" "sexp" "xemacs")
255 "List of words that are correct when spell-checking Lisp documentation.")
256
257 (defcustom checkdoc-max-keyref-before-warn 10
258 "*The number of \\ [command-to-keystroke] tokens allowed in a doc string.
259 Any more than this and a warning is generated suggesting that the construct
260 \\ {keymap} be used instead."
261 :group 'checkdoc
262 :type 'integer)
263
264 (defcustom checkdoc-arguments-in-order-flag t
265 "*Non-nil means warn if arguments appear out of order.
266 Setting this to nil will mean only checking that all the arguments
267 appear in the proper form in the documentation, not that they are in
268 the same order as they appear in the argument list. No mention is
269 made in the style guide relating to order."
270 :group 'checkdoc
271 :type 'boolean)
272
273 (defvar checkdoc-style-hooks nil
274 "Hooks called after the standard style check is completed.
275 All hooks must return nil or a string representing the error found.
276 Useful for adding new user implemented commands.
277
278 Each hook is called with two parameters, (DEFUNINFO ENDPOINT).
279 DEFUNINFO is the return value of `checkdoc-defun-info'. ENDPOINT is the
280 location of end of the documentation string.")
281
282 (defvar checkdoc-comment-style-hooks nil
283 "Hooks called after the standard comment style check is completed.
284 Must return nil if no errors are found, or a string describing the
285 problem discovered. This is useful for adding additional checks.")
286
287 (defvar checkdoc-diagnostic-buffer "*Style Warnings*"
288 "Name of warning message buffer.")
289
290 (defvar checkdoc-defun-regexp
291 "^(def\\(un\\|var\\|custom\\|macro\\|const\\|subst\\|advice\\)\
292 \\s-+\\(\\(\\sw\\|\\s_\\)+\\)[ \t\n]+"
293 "Regular expression used to identify a defun.
294 A search leaves the cursor in front of the parameter list.")
295
296 (defcustom checkdoc-verb-check-experimental-flag t
297 "*Non-nil means to attempt to check the voice of the doc string.
298 This check keys off some words which are commonly misused. See the
299 variable `checkdoc-common-verbs-wrong-voice' if you wish to add your own."
300 :group 'checkdoc
301 :type 'boolean)
302
303 (defvar checkdoc-generate-compile-warnings-flag nil
304 "Non-nil means generage warnings in a buffer for browsing.
305 Do not set this by hand, use a function like `checkdoc-current-buffer'
306 with a universal argument.")
307
308 (defcustom checkdoc-symbol-words nil
309 "A list of symbols which also happen to make good words.
310 These symbol-words are ignored when unquoted symbols are searched for.
311 This should be set in an Emacs Lisp file's local variables."
312 :group 'checkdoc
313 :type '(repeat (symbol :tag "Word")))
314
315 (defvar checkdoc-proper-noun-list
316 '("ispell" "xemacs" "emacs" "lisp")
317 "List of words (not capitalized) which should be capitalized.")
318
319 (defvar checkdoc-proper-noun-regexp
320 (let ((expr "\\<\\(")
321 (l checkdoc-proper-noun-list))
322 (while l
323 (setq expr (concat expr (car l) (if (cdr l) "\\|" ""))
324 l (cdr l)))
325 (concat expr "\\)\\>"))
326 "Regular expression derived from `checkdoc-proper-noun-regexp'.")
327
328 (defvar checkdoc-common-verbs-regexp nil
329 "Regular expression derived from `checkdoc-common-verbs-regexp'.")
330
331 (defvar checkdoc-common-verbs-wrong-voice
332 '(("adds" . "add")
333 ("allows" . "allow")
334 ("appends" . "append")
335 ("applies" . "apply")
336 ("arranges" . "arrange")
337 ("brings" . "bring")
338 ("calls" . "call")
339 ("catches" . "catch")
340 ("changes" . "change")
341 ("checks" . "check")
342 ("contains" . "contain")
343 ("converts" . "convert")
344 ("creates" . "create")
345 ("destroys" . "destroy")
346 ("disables" . "disable")
347 ("executes" . "execute")
348 ("evals" . "evaluate")
349 ("evaluates" . "evaluate")
350 ("finds" . "find")
351 ("forces" . "force")
352 ("gathers" . "gather")
353 ("generates" . "generate")
354 ("goes" . "go")
355 ("guesses" . "guess")
356 ("highlights" . "highlight")
357 ("holds" . "hold")
358 ("ignores" . "ignore")
359 ("indents" . "indent")
360 ("initializes" . "initialize")
361 ("inserts" . "insert")
362 ("installs" . "install")
363 ("investigates" . "investigate")
364 ("keeps" . "keep")
365 ("kills" . "kill")
366 ("leaves" . "leave")
367 ("lets" . "let")
368 ("loads" . "load")
369 ("looks" . "look")
370 ("makes" . "make")
371 ("marks" . "mark")
372 ("matches" . "match")
373 ("moves" . "move")
374 ("notifies" . "notify")
375 ("offers" . "offer")
376 ("parses" . "parse")
377 ("performs" . "perform")
378 ("prepares" . "prepare")
379 ("prepends" . "prepend")
380 ("reads" . "read")
381 ("raises" . "raise")
382 ("removes" . "remove")
383 ("replaces" . "replace")
384 ("resets" . "reset")
385 ("restores" . "restore")
386 ("returns" . "return")
387 ("runs" . "run")
388 ("saves" . "save")
389 ("says" . "say")
390 ("searches" . "search")
391 ("selects" . "select")
392 ("sets" . "set")
393 ("sex" . "s*x")
394 ("shows" . "show")
395 ("signifies" . "signify")
396 ("sorts" . "sort")
397 ("starts" . "start")
398 ("stores" . "store")
399 ("switches" . "switch")
400 ("tells" . "tell")
401 ("tests" . "test")
402 ("toggles" . "toggle")
403 ("tries" . "try")
404 ("turns" . "turn")
405 ("undoes" . "undo")
406 ("unloads" . "unload")
407 ("unmarks" . "unmark")
408 ("updates" . "update")
409 ("uses" . "use")
410 ("yanks" . "yank")
411 )
412 "Alist of common words in the wrong voice and what should be used instead.
413 Set `checkdoc-verb-check-experimental-flag' to nil to avoid this costly
414 and experimental check. Do not modify this list without setting
415 the value of `checkdoc-common-verbs-regexp' to nil which cause it to
416 be re-created.")
417
418 (defvar checkdoc-syntax-table nil
419 "Syntax table used by checkdoc in document strings.")
420
421 (if checkdoc-syntax-table
422 nil
423 (setq checkdoc-syntax-table (copy-syntax-table emacs-lisp-mode-syntax-table))
424 ;; When dealing with syntax in doc strings, make sure that - are encompassed
425 ;; in words so we can use cheap \\> to get the end of a symbol, not the
426 ;; end of a word in a conglomerate.
427 (modify-syntax-entry ?- "w" checkdoc-syntax-table)
428 )
429
430
431 ;;; Compatibility
432 ;;
433 (if (string-match "X[Ee]macs" emacs-version)
434 (progn
435 (defalias 'checkdoc-make-overlay 'make-extent)
436 (defalias 'checkdoc-overlay-put 'set-extent-property)
437 (defalias 'checkdoc-delete-overlay 'delete-extent)
438 (defalias 'checkdoc-overlay-start 'extent-start)
439 (defalias 'checkdoc-overlay-end 'extent-end)
440 (defalias 'checkdoc-mode-line-update 'redraw-modeline)
441 (defalias 'checkdoc-call-eval-buffer 'eval-buffer)
442 )
443 (defalias 'checkdoc-make-overlay 'make-overlay)
444 (defalias 'checkdoc-overlay-put 'overlay-put)
445 (defalias 'checkdoc-delete-overlay 'delete-overlay)
446 (defalias 'checkdoc-overlay-start 'overlay-start)
447 (defalias 'checkdoc-overlay-end 'overlay-end)
448 (defalias 'checkdoc-mode-line-update 'force-mode-line-update)
449 (defalias 'checkdoc-call-eval-buffer 'eval-current-buffer)
450 )
451
452 ;; Emacs 20 has this handy function.
453 (if (not (fboundp 'princ-list))
454 (defun princ-list (&rest args)
455 "Call `princ' on ARGS."
456 (mapcar 'princ args)))
457
458 ;; Emacs 20s have MULE characters which don't equate to numbers.
459 (if (fboundp 'char=)
460 (defalias 'checkdoc-char= 'char=)
461 (defalias 'checkdoc-char= '=))
462
463 ;; Emacs 19.28 and earlier don't have the handy 'add-to-list function
464 (if (fboundp 'add-to-list)
465
466 (defalias 'checkdoc-add-to-list 'add-to-list)
467
468 (defun checkdoc-add-to-list (list-var element)
469 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet."
470 (if (not (member element (symbol-value list-var)))
471 (set list-var (cons element (symbol-value list-var)))))
472 )
473
474 ;; To be safe in new Emacsen, we want to read events, not characters
475 (if (fboundp 'read-event)
476 (defalias 'checkdoc-read-event 'read-event)
477 (defalias 'checkdoc-read-event 'read-char))
478
479 ;;; User level commands
480 ;;
481 ;;;###autoload
482 (defun checkdoc ()
483 "Interactivly check the entire buffer for style errors.
484 The current status of the ckeck will be displayed in a buffer which
485 the users will view as each check is completed."
486 (interactive)
487 (let ((status (list "Checking..." "-" "-" "-"))
488 (checkdoc-spellcheck-documentation-flag
489 (member checkdoc-spellcheck-documentation-flag
490 '(buffer interactive t)))
491 ;; if the user set autofix to never, then that breaks the
492 ;; obviously requested asking implied by using this function.
493 ;; Set it to paranoia level.
494 (checkdoc-autofix-flag (if (or (not checkdoc-autofix-flag)
495 (eq checkdoc-autofix-flag 'never))
496 'query
497 checkdoc-autofix-flag))
498 tmp)
499 (checkdoc-display-status-buffer status)
500 ;; check the comments
501 (if (not buffer-file-name)
502 (setcar status "Not checked")
503 (if (checkdoc-file-comments-engine)
504 (setcar status "Errors")
505 (setcar status "Ok")))
506 (setcar (cdr status) "Checking...")
507 (checkdoc-display-status-buffer status)
508 ;; Check the documentation
509 (setq tmp (checkdoc-interactive nil t))
510 (if tmp
511 (setcar (cdr status) (format "%d Errors" (length tmp)))
512 (setcar (cdr status) "Ok"))
513 (setcar (cdr (cdr status)) "Checking...")
514 (checkdoc-display-status-buffer status)
515 ;; Check the message text
516 (if (setq tmp (checkdoc-message-interactive nil t))
517 (setcar (cdr (cdr status)) (format "%d Errors" (length tmp)))
518 (setcar (cdr (cdr status)) "Ok"))
519 (setcar (cdr (cdr (cdr status))) "Checking...")
520 (checkdoc-display-status-buffer status)
521 ;; Rogue spacing
522 (if (condition-case nil
523 (checkdoc-rogue-spaces nil t)
524 (error t))
525 (setcar (cdr (cdr (cdr status))) "Errors")
526 (setcar (cdr (cdr (cdr status))) "Ok"))
527 (checkdoc-display-status-buffer status)))
528
529 (defun checkdoc-display-status-buffer (check)
530 "Display and update the status buffer for the current checkdoc mode.
531 CHECK is a vector stating the current status of each test as an
532 element is the status of that level of teset."
533 (let (temp-buffer-setup-hook)
534 (with-output-to-temp-buffer " *Checkdoc Status*"
535 (princ-list
536 "Buffer comments and tags: " (nth 0 check) "\n"
537 "Documentation style: " (nth 1 check) "\n"
538 "Message/Query text style: " (nth 2 check) "\n"
539 "Unwanted Spaces: " (nth 3 check)
540 )))
541 (shrink-window-if-larger-than-buffer
542 (get-buffer-window " *Checkdoc Status*"))
543 (message nil)
544 (sit-for 0))
545
546 ;;;###autoload
547 (defun checkdoc-interactive (&optional start-here showstatus)
548 "Interactively check the current buffer for doc string errors.
549 Prefix argument START-HERE will start the checking from the current
550 point, otherwise the check starts at the beginning of the current
551 buffer. Allows navigation forward and backwards through document
552 errors. Does not check for comment or space warnings.
553 Optional argument SHOWSTATUS indicates that we should update the
554 checkdoc status window instead of the usual behavior."
555 (interactive "P")
556 (let ((checkdoc-spellcheck-documentation-flag
557 (member checkdoc-spellcheck-documentation-flag
558 '(interactive t))))
559 (checkdoc-interactive-loop start-here showstatus 'checkdoc-next-error)))
560
561 ;;;###autoload
562 (defun checkdoc-message-interactive (&optional start-here showstatus)
563 "Interactively check the current buffer for message string errors.
564 Prefix argument START-HERE will start the checking from the current
565 point, otherwise the check starts at the beginning of the current
566 buffer. Allows navigation forward and backwards through document
567 errors. Does not check for comment or space warnings.
568 Optional argument SHOWSTATUS indicates that we should update the
569 checkdoc status window instead of the usual behavior."
570 (interactive "P")
571 (let ((checkdoc-spellcheck-documentation-flag
572 (member checkdoc-spellcheck-documentation-flag
573 '(interactive t))))
574 (checkdoc-interactive-loop start-here showstatus
575 'checkdoc-next-message-error)))
576
577 (defun checkdoc-interactive-loop (start-here showstatus findfunc)
578 "Interactivly loop over all errors that can be found by a given method.
579 Searching starts at START-HERE. SHOWSTATUS expresses the verbosity
580 of the search, and wether ending the search will auto-exit this function.
581 FINDFUNC is a symbol representing a function that will position the
582 cursor, and return error message text to present the the user. It is
583 assumed that the cursor will stop just before a major sexp, which will
584 be highlighted to present the user with feedback as to the offending
585 style."
586 ;; Determine where to start the test
587 (let* ((begin (prog1 (point)
588 (if (not start-here) (goto-char (point-min)))))
589 ;; Assign a flag to spellcheck flag
590 (checkdoc-spellcheck-documentation-flag
591 (member checkdoc-spellcheck-documentation-flag
592 '(buffer interactive t)))
593 ;; Fetch the error list
594 (err-list (list (funcall findfunc nil)))
595 (cdo nil)
596 (returnme nil)
597 c)
598 (save-window-excursion
599 (if (not (car err-list)) (setq err-list nil))
600 ;; Include whatever function point is in for good measure.
601 (beginning-of-defun)
602 (while err-list
603 (goto-char (cdr (car err-list)))
604 ;; The cursor should be just in front of the offending doc string
605 (if (stringp (car (car err-list)))
606 (setq cdo (save-excursion (checkdoc-make-overlay
607 (point) (progn (forward-sexp 1)
608 (point)))))
609 (setq cdo (checkdoc-make-overlay
610 (checkdoc-error-start (car (car err-list)))
611 (checkdoc-error-end (car (car err-list))))))
612 (unwind-protect
613 (progn
614 (checkdoc-overlay-put cdo 'face 'highlight)
615 ;; Make sure the whole doc string is visible if possible.
616 (sit-for 0)
617 (if (and (looking-at "\"")
618 (not (pos-visible-in-window-p
619 (save-excursion (forward-sexp 1) (point))
620 (selected-window))))
621 (let ((l (count-lines (point)
622 (save-excursion
623 (forward-sexp 1) (point)))))
624 (if (> l (window-height))
625 (recenter 1)
626 (recenter (/ (- (window-height) l) 2))))
627 (recenter))
628 (message "%s (C-h,%se,n,p,q)" (checkdoc-error-text
629 (car (car err-list)))
630 (if (checkdoc-error-unfixable (car (car err-list)))
631 "" "f,"))
632 (save-excursion
633 (goto-char (checkdoc-error-start (car (car err-list))))
634 (if (not (pos-visible-in-window-p))
635 (recenter (- (window-height) 2)))
636 (setq c (checkdoc-read-event)))1
637 (if (not (integerp c)) (setq c ??))
638 (cond
639 ;; Exit condition
640 ((checkdoc-char= c ?\C-g) (signal 'quit nil))
641 ;; Request an auto-fix
642 ((or (checkdoc-char= c ?y) (checkdoc-char= c ?f))
643 (checkdoc-delete-overlay cdo)
644 (setq cdo nil)
645 (goto-char (cdr (car err-list)))
646 ;; `automatic-then-never' tells the autofix function
647 ;; to only allow one fix to be automatic. The autofix
648 ;; function will than set the flag to 'never, allowing
649 ;; the checker to return a different error.
650 (let ((checkdoc-autofix-flag 'automatic-then-never)
651 (fixed nil))
652 (funcall findfunc t)
653 (setq fixed (not (eq checkdoc-autofix-flag
654 'automatic-then-never)))
655 (if (not fixed)
656 (progn
657 (message "A Fix was not available.")
658 (sit-for 2))
659 (setq err-list (cdr err-list))))
660 (beginning-of-defun)
661 (let ((pe (car err-list))
662 (ne (funcall findfunc nil)))
663 (if ne
664 (setq err-list (cons ne err-list))
665 (cond ((not err-list)
666 (message "No More Stylistic Errors.")
667 (sit-for 2))
668 (t
669 (message
670 "No Additional style errors. Continuing...")
671 (sit-for 2))))))
672 ;; Move to the next error (if available)
673 ((or (checkdoc-char= c ?n) (checkdoc-char= c ?\ ))
674 (let ((ne (funcall findfunc nil)))
675 (if (not ne)
676 (if showstatus
677 (setq returnme err-list
678 err-list nil)
679 (if (not err-list)
680 (message "No More Stylistic Errors.")
681 (message "No Additional style errors. Continuing..."))
682 (sit-for 2))
683 (setq err-list (cons ne err-list)))))
684 ;; Go backwards in the list of errors
685 ((or (checkdoc-char= c ?p) (checkdoc-char= c ?\C-?))
686 (if (/= (length err-list) 1)
687 (progn
688 (setq err-list (cdr err-list))
689 (goto-char (cdr (car err-list)))
690 (beginning-of-defun))
691 (message "No Previous Errors.")
692 (sit-for 2)))
693 ;; Edit the buffer recursively.
694 ((checkdoc-char= c ?e)
695 (checkdoc-recursive-edit
696 (checkdoc-error-text (car (car err-list))))
697 (checkdoc-delete-overlay cdo)
698 (setq err-list (cdr err-list)) ;back up the error found.
699 (beginning-of-defun)
700 (let ((ne (funcall findfunc nil)))
701 (if (not ne)
702 (if showstatus
703 (setq returnme err-list
704 err-list nil)
705 (message "No More Stylistic Errors.")
706 (sit-for 2))
707 (setq err-list (cons ne err-list)))))
708 ;; Quit checkdoc
709 ((checkdoc-char= c ?q)
710 (setq returnme err-list
711 err-list nil
712 begin (point)))
713 ;; Goofy s tuff
714 (t
715 (if (get-buffer-window "*Checkdoc Help*")
716 (progn
717 (delete-window (get-buffer-window "*Checkdoc Help*"))
718 (kill-buffer "*Checkdoc Help*"))
719 (with-output-to-temp-buffer "*Checkdoc Help*"
720 (princ-list
721 "Checkdoc Keyboard Summary:\n"
722 (if (checkdoc-error-unfixable (car (car err-list)))
723 ""
724 (concat
725 "f, y - auto Fix this warning without asking (if\
726 available.)\n"
727 " Very complex operations will still query.\n")
728 )
729 "e - Enter recursive Edit. Press C-M-c to exit.\n"
730 "SPC, n - skip to the Next error.\n"
731 "DEL, p - skip to the Previous error.\n"
732 "q - Quit checkdoc.\n"
733 "C-h - Toggle this help buffer."))
734 (shrink-window-if-larger-than-buffer
735 (get-buffer-window "*Checkdoc Help*"))))))
736 (if cdo (checkdoc-delete-overlay cdo)))))
737 (goto-char begin)
738 (if (get-buffer "*Checkdoc Help*") (kill-buffer "*Checkdoc Help*"))
739 (message "Checkdoc: Done.")
740 returnme))
741
742 (defun checkdoc-next-error (enable-fix)
743 "Find and return the next checkdoc error list, or nil.
744 Only documentation strings are checked.
745 Add error vector is of the form (WARNING . POSITION) where WARNING
746 is the warning text, and POSITION is the point in the buffer where the
747 error was found. We can use points and not markers because we promise
748 not to edit the buffer before point without re-executing this check.
749 Argument ENABLE-FIX will enable auto-fixing while looking for the next
750 error. This argument assumes that the cursor is already positioned to
751 perform the fix."
752 (if enable-fix
753 (checkdoc-this-string-valid)
754 (let ((msg nil) (p (point))
755 (checkdoc-autofix-flag nil))
756 (condition-case nil
757 (while (and (not msg) (checkdoc-next-docstring))
758 (message "Searching for doc string error...%d%%"
759 (/ (* 100 (point)) (point-max)))
760 (if (setq msg (checkdoc-this-string-valid))
761 (setq msg (cons msg (point)))))
762 ;; Quit.. restore position, Other errors, leave alone
763 (quit (goto-char p)))
764 msg)))
765
766 (defun checkdoc-next-message-error (enable-fix)
767 "Find and return the next checkdoc mesasge related error list, or nil.
768 Only text for error and `y-or-n-p' strings are checked. See
769 `checkdoc-next-error' for details on the return value.
770 Argument ENABLE-FIX turns on the auto-fix feature. This argument
771 assumes that the cursor is already positioned to perform the fix."
772 (if enable-fix
773 (checkdoc-message-text-engine)
774 (let ((msg nil) (p (point)) (type nil)
775 (checkdoc-autofix-flag nil))
776 (condition-case nil
777 (while (and (not msg)
778 (setq type
779 (checkdoc-message-text-next-string (point-max))))
780 (message "Searching for message string error...%d%%"
781 (/ (* 100 (point)) (point-max)))
782 (if (setq msg (checkdoc-message-text-engine type))
783 (setq msg (cons msg (point)))))
784 ;; Quit.. restore position, Other errors, leave alone
785 (quit (goto-char p)))
786 msg)))
787
788 (defun checkdoc-recursive-edit (msg)
789 "Enter recursive edit to permit a user to fix some error checkdoc has found.
790 MSG is the error that was found, which is displayed in a help buffer."
791 (with-output-to-temp-buffer "*Checkdoc Help*"
792 (princ-list
793 "Error message:\n " msg
794 "\n\nEdit to fix this problem, and press C-M-c to continue."))
795 (shrink-window-if-larger-than-buffer
796 (get-buffer-window "*Checkdoc Help*"))
797 (message "When you're done editing press C-M-c to continue.")
798 (unwind-protect
799 (recursive-edit)
800 (if (get-buffer-window "*Checkdoc Help*")
801 (progn
802 (delete-window (get-buffer-window "*Checkdoc Help*"))
803 (kill-buffer "*Checkdoc Help*")))))
804
805 ;;;###autoload
806 (defun checkdoc-eval-current-buffer ()
807 "Evaluate and check documentation for the current buffer.
808 Evaluation is done first because good documentation for something that
809 doesn't work is just not useful. Comments, doc strings, and rogue
810 spacing are all verified."
811 (interactive)
812 (checkdoc-call-eval-buffer nil)
813 (checkdoc-current-buffer t))
814
815 ;;;###autoload
816 (defun checkdoc-current-buffer (&optional take-notes)
817 "Check current buffer for document, comment, error style, and rogue spaces.
818 With a prefix argument (in Lisp, the argument TAKE-NOTES),
819 store all errors found in a warnings buffer,
820 otherwise stop after the first error."
821 (interactive "P")
822 (if (interactive-p) (message "Checking buffer for style..."))
823 ;; Assign a flag to spellcheck flag
824 (let ((checkdoc-spellcheck-documentation-flag
825 (memq checkdoc-spellcheck-documentation-flag '(buffer t)))
826 (checkdoc-autofix-flag (if take-notes 'never
827 checkdoc-autofix-flag))
828 (checkdoc-generate-compile-warnings-flag
829 (or take-notes checkdoc-generate-compile-warnings-flag)))
830 (if take-notes
831 (checkdoc-start-section "checkdoc-current-buffer"))
832 ;; every test is responsible for returning the cursor.
833 (or (and buffer-file-name ;; only check comments in a file
834 (checkdoc-comments))
835 (checkdoc-start)
836 (checkdoc-message-text)
837 (checkdoc-rogue-spaces)
838 (not (interactive-p))
839 (if take-notes (checkdoc-show-diagnostics))
840 (message "Checking buffer for style...Done."))))
841
842 ;;;###autoload
843 (defun checkdoc-start (&optional take-notes)
844 "Start scanning the current buffer for documentation string style errors.
845 Only documentation strings are checked.
846 Use `checkdoc-continue' to continue checking if an error cannot be fixed.
847 Prefix argument TAKE-NOTES means to collect all the warning messages into
848 a separate buffer."
849 (interactive "P")
850 (let ((p (point)))
851 (goto-char (point-min))
852 (if (and take-notes (interactive-p))
853 (checkdoc-start-section "checkdoc-start"))
854 (checkdoc-continue take-notes)
855 ;; Go back since we can't be here without success above.
856 (goto-char p)
857 nil))
858
859 ;;;###autoload
860 (defun checkdoc-continue (&optional take-notes)
861 "Find the next doc string in the current buffer which has a style error.
862 Prefix argument TAKE-NOTES means to continue through the whole buffer and
863 save warnings in a separate buffer. Second optional argument START-POINT
864 is the starting location. If this is nil, `point-min' is used instead."
865 (interactive "P")
866 (let ((wrong nil) (msg nil) (errors nil)
867 ;; Assign a flag to spellcheck flag
868 (checkdoc-spellcheck-documentation-flag
869 (member checkdoc-spellcheck-documentation-flag
870 '(buffer t)))
871 (checkdoc-autofix-flag (if take-notes 'never
872 checkdoc-autofix-flag))
873 (checkdoc-generate-compile-warnings-flag
874 (or take-notes checkdoc-generate-compile-warnings-flag)))
875 (save-excursion
876 ;; If we are taking notes, encompass the whole buffer, otherwise
877 ;; the user is navigating down through the buffer.
878 (while (and (not wrong) (checkdoc-next-docstring))
879 ;; OK, let's look at the doc string.
880 (setq msg (checkdoc-this-string-valid))
881 (if msg (setq wrong (point)))))
882 (if wrong
883 (progn
884 (goto-char wrong)
885 (if (not take-notes)
886 (error (checkdoc-error-text msg)))))
887 (checkdoc-show-diagnostics)
888 (if (interactive-p)
889 (message "No style warnings."))))
890
891 (defun checkdoc-next-docstring ()
892 "Move to the next doc string after point, and return t.
893 Return nil if there are no more doc strings."
894 (if (not (re-search-forward checkdoc-defun-regexp nil t))
895 nil
896 ;; search drops us after the identifier. The next sexp is either
897 ;; the argument list or the value of the variable. skip it.
898 (forward-sexp 1)
899 (skip-chars-forward " \n\t")
900 t))
901
902 ;;;###autoload
903 (defun checkdoc-comments (&optional take-notes)
904 "Find missing comment sections in the current Emacs Lisp file.
905 Prefix argument TAKE-NOTES non-nil means to save warnings in a
906 separate buffer. Otherwise print a message. This returns the error
907 if there is one."
908 (interactive "P")
909 (if take-notes (checkdoc-start-section "checkdoc-comments"))
910 (if (not buffer-file-name)
911 (error "Can only check comments for a file buffer"))
912 (let* ((checkdoc-spellcheck-documentation-flag
913 (member checkdoc-spellcheck-documentation-flag
914 '(buffer t)))
915 (checkdoc-autofix-flag (if take-notes 'never checkdoc-autofix-flag))
916 (e (checkdoc-file-comments-engine))
917 (checkdoc-generate-compile-warnings-flag
918 (or take-notes checkdoc-generate-compile-warnings-flag)))
919 (if e (error (checkdoc-error-text e)))
920 (checkdoc-show-diagnostics)
921 e))
922
923 ;;;###autoload
924 (defun checkdoc-rogue-spaces (&optional take-notes interact)
925 "Find extra spaces at the end of lines in the current file.
926 Prefix argument TAKE-NOTES non-nil means to save warnings in a
927 separate buffer. Otherwise print a message. This returns the error
928 if there is one.
929 Optional argument INTERACT permits more interactive fixing."
930 (interactive "P")
931 (if take-notes (checkdoc-start-section "checkdoc-rogue-spaces"))
932 (let* ((checkdoc-autofix-flag (if take-notes 'never checkdoc-autofix-flag))
933 (e (checkdoc-rogue-space-check-engine nil nil interact))
934 (checkdoc-generate-compile-warnings-flag
935 (or take-notes checkdoc-generate-compile-warnings-flag)))
936 (if (not (interactive-p))
937 e
938 (if e
939 (message (checkdoc-error-text e))
940 (checkdoc-show-diagnostics)
941 (message "Space Check: done.")))))
942
943 ;;;###autoload
944 (defun checkdoc-message-text (&optional take-notes)
945 "Scan the buffer for occurrences of the error function, and verify text.
946 Optional argument TAKE-NOTES causes all errors to be logged."
947 (interactive "P")
948 (if take-notes (checkdoc-start-section "checkdoc-message-text"))
949 (let* ((p (point)) e
950 (checkdoc-autofix-flag (if take-notes 'never checkdoc-autofix-flag))
951 (checkdoc-generate-compile-warnings-flag
952 (or take-notes checkdoc-generate-compile-warnings-flag)))
953 (setq e (checkdoc-message-text-search))
954 (if (not (interactive-p))
955 e
956 (if e
957 (error (checkdoc-error-text e))
958 (checkdoc-show-diagnostics)))
959 (goto-char p))
960 (if (interactive-p) (message "Checking interactive message text...done.")))
961
962 ;;;###autoload
963 (defun checkdoc-eval-defun ()
964 "Evaluate the current form with `eval-defun' and check its documentation.
965 Evaluation is done first so the form will be read before the
966 documentation is checked. If there is a documentation error, then the display
967 of what was evaluated will be overwritten by the diagnostic message."
968 (interactive)
969 (call-interactively 'eval-defun)
970 (checkdoc-defun))
971
972 ;;;###autoload
973 (defun checkdoc-defun (&optional no-error)
974 "Examine the doc string of the function or variable under point.
975 Call `error' if the doc string has problems. If NO-ERROR is
976 non-nil, then do not call error, but call `message' instead.
977 If the doc string passes the test, then check the function for rogue white
978 space at the end of each line."
979 (interactive)
980 (save-excursion
981 (beginning-of-defun)
982 (if (not (looking-at checkdoc-defun-regexp))
983 ;; I found this more annoying than useful.
984 ;;(if (not no-error)
985 ;; (message "Cannot check this sexp's doc string."))
986 nil
987 ;; search drops us after the identifier. The next sexp is either
988 ;; the argument list or the value of the variable. skip it.
989 (goto-char (match-end 0))
990 (forward-sexp 1)
991 (skip-chars-forward " \n\t")
992 (let* ((checkdoc-spellcheck-documentation-flag
993 (member checkdoc-spellcheck-documentation-flag
994 '(defun t)))
995 (beg (save-excursion (beginning-of-defun) (point)))
996 (end (save-excursion (end-of-defun) (point)))
997 (msg (checkdoc-this-string-valid)))
998 (if msg (if no-error
999 (message (checkdoc-error-text msg))
1000 (error (checkdoc-error-text msg)))
1001 (setq msg (checkdoc-message-text-search beg end))
1002 (if msg (if no-error
1003 (message (checkdoc-error-text msg))
1004 (error (checkdoc-error-text msg)))
1005 (setq msg (checkdoc-rogue-space-check-engine beg end))
1006 (if msg (if no-error
1007 (message (checkdoc-error-text msg))
1008 (error (checkdoc-error-text msg))))))
1009 (if (interactive-p) (message "Checkdoc: done."))))))
1010
1011 ;;; Ispell interface for forcing a spell check
1012 ;;
1013
1014 ;;;###autoload
1015 (defun checkdoc-ispell (&optional take-notes)
1016 "Check the style and spelling of everything interactively.
1017 Calls `checkdoc' with spell-checking turned on.
1018 Prefix argument TAKE-NOTES is the same as for `checkdoc'"
1019 (interactive)
1020 (let ((checkdoc-spellcheck-documentation-flag t))
1021 (call-interactively 'checkdoc nil current-prefix-arg)))
1022
1023 ;;;###autoload
1024 (defun checkdoc-ispell-current-buffer (&optional take-notes)
1025 "Check the style and spelling of the current buffer.
1026 Calls `checkdoc-current-buffer' with spell-checking turned on.
1027 Prefix argument TAKE-NOTES is the same as for `checkdoc-current-buffer'"
1028 (interactive)
1029 (let ((checkdoc-spellcheck-documentation-flag t))
1030 (call-interactively 'checkdoc-current-buffer nil current-prefix-arg)))
1031
1032 ;;;###autoload
1033 (defun checkdoc-ispell-interactive (&optional take-notes)
1034 "Check the style and spelling of the current buffer interactively.
1035 Calls `checkdoc-interactive' with spell-checking turned on.
1036 Prefix argument TAKE-NOTES is the same as for `checkdoc-interactive'"
1037 (interactive)
1038 (let ((checkdoc-spellcheck-documentation-flag t))
1039 (call-interactively 'checkdoc-interactive nil current-prefix-arg)))
1040
1041 ;;;###autoload
1042 (defun checkdoc-ispell-message-interactive (&optional take-notes)
1043 "Check the style and spelling of message text interactively.
1044 Calls `checkdoc-message-interactive' with spell-checking turned on.
1045 Prefix argument TAKE-NOTES is the same as for `checkdoc-message-interactive'"
1046 (interactive)
1047 (let ((checkdoc-spellcheck-documentation-flag t))
1048 (call-interactively 'checkdoc-message-interactive nil current-prefix-arg)))
1049
1050 ;;;###autoload
1051 (defun checkdoc-ispell-message-text (&optional take-notes)
1052 "Check the style and spelling of message text interactively.
1053 Calls `checkdoc-message-text' with spell-checking turned on.
1054 Prefix argument TAKE-NOTES is the same as for `checkdoc-message-text'"
1055 (interactive)
1056 (let ((checkdoc-spellcheck-documentation-flag t))
1057 (call-interactively 'checkdoc-message-text nil current-prefix-arg)))
1058
1059 ;;;###autoload
1060 (defun checkdoc-ispell-start (&optional take-notes)
1061 "Check the style and spelling of the current buffer.
1062 Calls `checkdoc-start' with spell-checking turned on.
1063 Prefix argument TAKE-NOTES is the same as for `checkdoc-start'"
1064 (interactive)
1065 (let ((checkdoc-spellcheck-documentation-flag t))
1066 (call-interactively 'checkdoc-start nil current-prefix-arg)))
1067
1068 ;;;###autoload
1069 (defun checkdoc-ispell-continue (&optional take-notes)
1070 "Check the style and spelling of the current buffer after point.
1071 Calls `checkdoc-continue' with spell-checking turned on.
1072 Prefix argument TAKE-NOTES is the same as for `checkdoc-continue'"
1073 (interactive)
1074 (let ((checkdoc-spellcheck-documentation-flag t))
1075 (call-interactively 'checkdoc-continue nil current-prefix-arg)))
1076
1077 ;;;###autoload
1078 (defun checkdoc-ispell-comments (&optional take-notes)
1079 "Check the style and spelling of the current buffer's comments.
1080 Calls `checkdoc-comments' with spell-checking turned on.
1081 Prefix argument TAKE-NOTES is the same as for `checkdoc-comments'"
1082 (interactive)
1083 (let ((checkdoc-spellcheck-documentation-flag t))
1084 (call-interactively 'checkdoc-comments nil current-prefix-arg)))
1085
1086 ;;;###autoload
1087 (defun checkdoc-ispell-defun (&optional take-notes)
1088 "Check the style and spelling of the current defun with Ispell.
1089 Calls `checkdoc-defun' with spell-checking turned on.
1090 Prefix argument TAKE-NOTES is the same as for `checkdoc-defun'"
1091 (interactive)
1092 (let ((checkdoc-spellcheck-documentation-flag t))
1093 (call-interactively 'checkdoc-defun nil current-prefix-arg)))
1094
1095 ;;; Error Management
1096 ;;
1097 ;; Errors returned from checkdoc functions can have various
1098 ;; features and behaviors, so we need some ways of specifying
1099 ;; them, and making them easier to use in the wacked-out interfaces
1100 ;; people are requesting
1101 (defun checkdoc-create-error (text start end &optional unfixable)
1102 "Used to create the return error text returned from all engines.
1103 TEXT is the descriptive text of the error. START and END define the region
1104 it is sensible to highlight when describing the problem.
1105 Optional argument UNFIXABLE means that the error has no auto-fix available.
1106
1107 A list of the form (TEXT START END UNFIXABLE) is returned if we are not
1108 generating a buffered list of errors."
1109 (if checkdoc-generate-compile-warnings-flag
1110 (progn (checkdoc-error start text)
1111 nil)
1112 (list text start end unfixable)))
1113
1114 (defun checkdoc-error-text (err)
1115 "Return the text specified in the checkdoc ERR."
1116 ;; string-p part is for backwards compatibility
1117 (if (stringp err) err (car err)))
1118
1119 (defun checkdoc-error-start (err)
1120 "Return the start point specified in the checkdoc ERR."
1121 ;; string-p part is for backwards compatibility
1122 (if (stringp err) nil (nth 1 err)))
1123
1124 (defun checkdoc-error-end (err)
1125 "Return the end point specified in the checkdoc ERR."
1126 ;; string-p part is for backwards compatibility
1127 (if (stringp err) nil (nth 2 err)))
1128
1129 (defun checkdoc-error-unfixable (err)
1130 "Return the t if we cannot autofix the error specified in the checkdoc ERR."
1131 ;; string-p part is for backwards compatibility
1132 (if (stringp err) nil (nth 3 err)))
1133
1134 ;;; Minor Mode specification
1135 ;;
1136
1137 (defvar checkdoc-minor-mode-map
1138 (let ((map (make-sparse-keymap))
1139 (pmap (make-sparse-keymap)))
1140 ;; Override some bindings
1141 (define-key map "\C-\M-x" 'checkdoc-eval-defun)
1142 (define-key map "\C-x`" 'checkdoc-continue)
1143 (if (not (string-match "XEmacs" emacs-version))
1144 (define-key map [menu-bar emacs-lisp eval-buffer]
1145 'checkdoc-eval-current-buffer))
1146 ;; Add some new bindings under C-c ?
1147 (define-key pmap "x" 'checkdoc-defun)
1148 (define-key pmap "X" 'checkdoc-ispell-defun)
1149 (define-key pmap "`" 'checkdoc-continue)
1150 (define-key pmap "~" 'checkdoc-ispell-continue)
1151 (define-key pmap "s" 'checkdoc-start)
1152 (define-key pmap "S" 'checkdoc-ispell-start)
1153 (define-key pmap "d" 'checkdoc)
1154 (define-key pmap "D" 'checkdoc-ispell)
1155 (define-key pmap "b" 'checkdoc-current-buffer)
1156 (define-key pmap "B" 'checkdoc-ispell-current-buffer)
1157 (define-key pmap "e" 'checkdoc-eval-current-buffer)
1158 (define-key pmap "m" 'checkdoc-message-text)
1159 (define-key pmap "M" 'checkdoc-ispell-message-text)
1160 (define-key pmap "c" 'checkdoc-comments)
1161 (define-key pmap "C" 'checkdoc-ispell-comments)
1162 (define-key pmap " " 'checkdoc-rogue-spaces)
1163
1164 ;; bind our submap into map
1165 (define-key map "\C-c?" pmap)
1166 map)
1167 "Keymap used to override evaluation key-bindings for documentation checking.")
1168
1169 (defvar checkdoc-minor-keymap checkdoc-minor-mode-map
1170 "Obsolete! Use `checkdoc-minor-mode-map'.")
1171
1172 ;; Add in a menubar with easy-menu
1173
1174 (easy-menu-define
1175 checkdoc-minor-menu checkdoc-minor-mode-map "Checkdoc Minor Mode Menu"
1176 '("CheckDoc"
1177 ["Interactive Buffer Style Check" checkdoc t]
1178 ["Interactive Buffer Style and Spelling Check" checkdoc-ispell t]
1179 ["Check Buffer" checkdoc-current-buffer t]
1180 ["Check and Spell Buffer" checkdoc-ispell-current-buffer t]
1181 "---"
1182 ["Interactive Style Check" checkdoc-interactive t]
1183 ["Interactive Style and Spelling Check" checkdoc-ispell-interactive t]
1184 ["Find First Style Error" checkdoc-start t]
1185 ["Find First Style or Spelling Error" checkdoc-ispell-start t]
1186 ["Next Style Error" checkdoc-continue t]
1187 ["Next Style or Spelling Error" checkdoc-ispell-continue t]
1188 ["Interactive Message Text Style Check" checkdoc-message-interactive t]
1189 ["Interactive Message Text Style and Spelling Check"
1190 checkdoc-ispell-message-interactive t]
1191 ["Check Message Text" checkdoc-message-text t]
1192 ["Check and Spell Message Text" checkdoc-ispell-message-text t]
1193 ["Check Comment Style" checkdoc-comments buffer-file-name]
1194 ["Check Comment Style and Spelling" checkdoc-ispell-comments
1195 buffer-file-name]
1196 ["Check for Rogue Spaces" checkdoc-rogue-spaces t]
1197 "---"
1198 ["Check Defun" checkdoc-defun t]
1199 ["Check and Spell Defun" checkdoc-ispell-defun t]
1200 ["Check and Evaluate Defun" checkdoc-eval-defun t]
1201 ["Check and Evaluate Buffer" checkdoc-eval-current-buffer t]
1202 ))
1203 ;; XEmacs requires some weird stuff to add this menu in a minor mode.
1204 ;; What is it?
1205
1206 ;;;###autoload
1207 (easy-mmode-define-minor-mode checkdoc-minor-mode
1208 "Toggle Checkdoc minor mode, a mode for checking Lisp doc strings.
1209 With prefix ARG, turn Checkdoc minor mode on iff ARG is positive.
1210
1211 In Checkdoc minor mode, the usual bindings for `eval-defun' which is
1212 bound to \\<checkdoc-minor-mode-map> \\[checkdoc-eval-defun] and `checkdoc-eval-current-buffer' are overridden to include
1213 checking of documentation strings.
1214
1215 \\{checkdoc-minor-mode-map}"
1216 nil " CDoc" nil)
1217
1218 ;;; Subst utils
1219 ;;
1220 (defsubst checkdoc-run-hooks (hookvar &rest args)
1221 "Run hooks in HOOKVAR with ARGS."
1222 (if (fboundp 'run-hook-with-args-until-success)
1223 (apply 'run-hook-with-args-until-success hookvar args)
1224 ;; This method was similar to above. We ignore the warning
1225 ;; since we will use the above for future Emacs versions
1226 (apply 'run-hook-with-args hookvar args)))
1227
1228 (defsubst checkdoc-create-common-verbs-regexp ()
1229 "Rebuild the contents of `checkdoc-common-verbs-regexp'."
1230 (or checkdoc-common-verbs-regexp
1231 (setq checkdoc-common-verbs-regexp
1232 (concat "\\<\\("
1233 (mapconcat (lambda (e) (concat (car e)))
1234 checkdoc-common-verbs-wrong-voice "\\|")
1235 "\\)\\>"))))
1236
1237 ;; Profiler says this is not yet faster than just calling assoc
1238 ;;(defun checkdoc-word-in-alist-vector (word vector)
1239 ;; "Check to see if WORD is in the car of an element of VECTOR.
1240 ;;VECTOR must be sorted. The CDR should be a replacement. Since the
1241 ;;word list is getting bigger, it is time for a quick bisecting search."
1242 ;; (let ((max (length vector)) (min 0) i
1243 ;; (found nil) (fw nil))
1244 ;; (setq i (/ max 2))
1245 ;; (while (and (not found) (/= min max))
1246 ;; (setq fw (car (aref vector i)))
1247 ;; (cond ((string= word fw) (setq found (cdr (aref vector i))))
1248 ;; ((string< word fw) (setq max i))
1249 ;; (t (setq min i)))
1250 ;; (setq i (/ (+ max min) 2))
1251 ;; )
1252 ;; found))
1253
1254 ;;; Checking engines
1255 ;;
1256 (defun checkdoc-this-string-valid ()
1257 "Return a message string if the current doc string is invalid.
1258 Check for style only, such as the first line always being a complete
1259 sentence, whitespace restrictions, and making sure there are no
1260 hard-coded key-codes such as C-[char] or mouse-[number] in the comment.
1261 See the style guide in the Emacs Lisp manual for more details."
1262
1263 ;; Jump over comments between the last object and the doc string
1264 (while (looking-at "[ \t\n]*;")
1265 (forward-line 1)
1266 (beginning-of-line)
1267 (skip-chars-forward " \n\t"))
1268
1269 (let ((fp (checkdoc-defun-info))
1270 (err nil))
1271 (setq
1272 err
1273 ;; * Every command, function, or variable intended for users to know
1274 ;; about should have a documentation string.
1275 ;;
1276 ;; * An internal variable or subroutine of a Lisp program might as well
1277 ;; have a documentation string. In earlier Emacs versions, you could
1278 ;; save space by using a comment instead of a documentation string,
1279 ;; but that is no longer the case.
1280 (if (and (not (nth 1 fp)) ; not a variable
1281 (or (nth 2 fp) ; is interactive
1282 checkdoc-force-docstrings-flag) ;or we always complain
1283 (not (checkdoc-char= (following-char) ?\"))) ; no doc string
1284 ;; Sometimes old code has comments where the documentation should
1285 ;; be. Let's see if we can find the comment, and offer to turn it
1286 ;; into documentation for them.
1287 (let ((have-comment nil)
1288 (comment-start ";")) ; in case it's not default
1289 (condition-case nil
1290 (progn
1291 (forward-sexp -1)
1292 (forward-sexp 1)
1293 (skip-chars-forward "\n \t")
1294 (setq have-comment (looking-at comment-start)))
1295 (error nil))
1296 (if have-comment
1297 (if (or (eq checkdoc-autofix-flag
1298 'automatic-then-never)
1299 (checkdoc-y-or-n-p
1300 "Convert comment to documentation? "))
1301 (save-excursion
1302 ;; Our point is at the beginning of the comment!
1303 ;; Insert a quote, then remove the comment chars.
1304 (insert "\"")
1305 (let ((docstring-start-point (point)))
1306 (while (looking-at comment-start)
1307 (while (looking-at comment-start)
1308 (delete-char 1))
1309 (if (looking-at "[ \t]+")
1310 (delete-region (match-beginning 0) (match-end 0)))
1311 (forward-line 1)
1312 (beginning-of-line)
1313 (skip-chars-forward " \t")
1314 (if (looking-at comment-start)
1315 (progn
1316 (beginning-of-line)
1317 (zap-to-char 1 ?\;))))
1318 (beginning-of-line)
1319 (forward-char -1)
1320 (insert "\"")
1321 (forward-char -1)
1322 ;; quote any double-quote characters in the comment.
1323 (while (search-backward "\"" docstring-start-point t)
1324 (insert "\\"))
1325 (if (eq checkdoc-autofix-flag 'automatic-then-never)
1326 (setq checkdoc-autofix-flag 'never))))
1327 (checkdoc-create-error
1328 "You should convert this comment to documentation"
1329 (point) (save-excursion (end-of-line) (point))))
1330 (checkdoc-create-error
1331 (if (nth 2 fp)
1332 "All interactive functions should have documentation"
1333 "All variables and subroutines might as well have a \
1334 documentation string")
1335 (point) (+ (point) 1) t)))))
1336 (if (and (not err) (looking-at "\""))
1337 (let ((old-syntax-table (syntax-table)))
1338 (unwind-protect
1339 (progn
1340 (set-syntax-table checkdoc-syntax-table)
1341 (checkdoc-this-string-valid-engine fp))
1342 (set-syntax-table old-syntax-table)))
1343 err)))
1344
1345 (defun checkdoc-this-string-valid-engine (fp)
1346 "Return an error list or string if the current doc string is invalid.
1347 Depends on `checkdoc-this-string-valid' to reset the syntax table so that
1348 regexp short cuts work. FP is the function defun information."
1349 (let ((case-fold-search nil)
1350 ;; Use a marker so if an early check modifies the text,
1351 ;; we won't accidentally loose our place. This could cause
1352 ;; end-of doc string whitespace to also delete the " char.
1353 (s (point))
1354 (e (if (looking-at "\"")
1355 (save-excursion (forward-sexp 1) (point-marker))
1356 (point))))
1357 (or
1358 ;; * *Do not* indent subsequent lines of a documentation string so that
1359 ;; the text is lined up in the source code with the text of the first
1360 ;; line. This looks nice in the source code, but looks bizarre when
1361 ;; users view the documentation. Remember that the indentation
1362 ;; before the starting double-quote is not part of the string!
1363 (save-excursion
1364 (forward-line 1)
1365 (beginning-of-line)
1366 (if (and (< (point) e)
1367 (looking-at "\\([ \t]+\\)[^ \t\n]"))
1368 (if (checkdoc-autofix-ask-replace (match-beginning 1)
1369 (match-end 1)
1370 "Remove this whitespace? "
1371 "")
1372 nil
1373 (checkdoc-create-error
1374 "Second line should not have indentation"
1375 (match-beginning 1)
1376 (match-end 1)))))
1377 ;; * Check for '(' in column 0.
1378 (save-excursion
1379 (when (re-search-forward "^(" e t)
1380 (if (checkdoc-autofix-ask-replace (match-beginning 0)
1381 (match-end 0)
1382 "Escape this '('? "
1383 "\\(")
1384 nil
1385 (checkdoc-create-error
1386 "Open parenthesis in column 0 should be escaped"
1387 (match-beginning 0) (match-end 0)))))
1388 ;; * Do not start or end a documentation string with whitespace.
1389 (let (start end)
1390 (if (or (if (looking-at "\"\\([ \t\n]+\\)")
1391 (setq start (match-beginning 1)
1392 end (match-end 1)))
1393 (save-excursion
1394 (forward-sexp 1)
1395 (forward-char -1)
1396 (if (/= (skip-chars-backward " \t\n") 0)
1397 (setq start (point)
1398 end (1- e)))))
1399 (if (checkdoc-autofix-ask-replace
1400 start end "Remove this whitespace? " "")
1401 nil
1402 (checkdoc-create-error
1403 "Documentation strings should not start or end with whitespace"
1404 start end))))
1405 ;; * The first line of the documentation string should consist of one
1406 ;; or two complete sentences that stand on their own as a summary.
1407 ;; `M-x apropos' displays just the first line, and if it doesn't
1408 ;; stand on its own, the result looks bad. In particular, start the
1409 ;; first line with a capital letter and end with a period.
1410 (save-excursion
1411 (end-of-line)
1412 (skip-chars-backward " \t\n")
1413 (if (> (point) e) (goto-char e)) ;of the form (defun n () "doc" nil)
1414 (forward-char -1)
1415 (cond
1416 ((and (checkdoc-char= (following-char) ?\")
1417 ;; A backslashed double quote at the end of a sentence
1418 (not (checkdoc-char= (preceding-char) ?\\)))
1419 ;; We might have to add a period in this case
1420 (forward-char -1)
1421 (if (looking-at "[.!?]")
1422 nil
1423 (forward-char 1)
1424 (if (checkdoc-autofix-ask-replace
1425 (point) (1+ (point)) "Add period to sentence? "
1426 ".\"" t)
1427 nil
1428 (checkdoc-create-error
1429 "First sentence should end with punctuation"
1430 (point) (1+ (point))))))
1431 ((looking-at "[\\!?;:.)]")
1432 ;; These are ok
1433 nil)
1434 ((and checkdoc-permit-comma-termination-flag (looking-at ","))
1435 nil)
1436 (t
1437 ;; If it is not a complete sentence, let's see if we can
1438 ;; predict a clever way to make it one.
1439 (let ((msg "First line is not a complete sentence")
1440 (e (point)))
1441 (beginning-of-line)
1442 (if (re-search-forward "\\. +" e t)
1443 ;; Here we have found a complete sentence, but no break.
1444 (if (checkdoc-autofix-ask-replace
1445 (1+ (match-beginning 0)) (match-end 0)
1446 "First line not a complete sentence. Add RET here? "
1447 "\n" t)
1448 (let (l1 l2)
1449 (forward-line 1)
1450 (end-of-line)
1451 (setq l1 (current-column)
1452 l2 (save-excursion
1453 (forward-line 1)
1454 (end-of-line)
1455 (current-column)))
1456 (if (> (+ l1 l2 1) 80)
1457 (setq msg "Incomplete auto-fix; doc string \
1458 may require more formatting")
1459 ;; We can merge these lines! Replace this CR
1460 ;; with a space.
1461 (delete-char 1) (insert " ")
1462 (setq msg nil))))
1463 ;; Let's see if there is enough room to draw the next
1464 ;; line's sentence up here. I often get hit w/
1465 ;; auto-fill moving my words around.
1466 (let ((numc (progn (end-of-line) (- 80 (current-column))))
1467 (p (point)))
1468 (forward-line 1)
1469 (beginning-of-line)
1470 (if (and (re-search-forward "[.!?:\"]\\([ \t\n]+\\|\"\\)"
1471 (save-excursion
1472 (end-of-line)
1473 (point))
1474 t)
1475 (< (current-column) numc))
1476 (if (checkdoc-autofix-ask-replace
1477 p (1+ p)
1478 "1st line not a complete sentence. Join these lines? "
1479 " " t)
1480 (progn
1481 ;; They said yes. We have more fill work to do...
1482 (goto-char (match-beginning 1))
1483 (delete-region (point) (match-end 1))
1484 (insert "\n")
1485 (setq msg nil))))))
1486 (if msg
1487 (checkdoc-create-error msg s (save-excursion
1488 (goto-char s)
1489 (end-of-line)
1490 (point)))
1491 nil) ))))
1492 ;; Continuation of above. Make sure our sentence is capitalized.
1493 (save-excursion
1494 (skip-chars-forward "\"\\*")
1495 (if (looking-at "[a-z]")
1496 (if (checkdoc-autofix-ask-replace
1497 (match-beginning 0) (match-end 0)
1498 "Capitalize your sentence? " (upcase (match-string 0))
1499 t)
1500 nil
1501 (checkdoc-create-error
1502 "First line should be capitalized"
1503 (match-beginning 0) (match-end 0)))
1504 nil))
1505 ;; * Don't write key sequences directly in documentation strings.
1506 ;; Instead, use the `\\[...]' construct to stand for them.
1507 (save-excursion
1508 (let ((f nil) (m nil) (start (point))
1509 (re "[^`A-Za-z0-9_]\\([CMA]-[a-zA-Z]\\|\\(\\([CMA]-\\)?\
1510 mouse-[0-3]\\)\\)\\>"))
1511 ;; Find the first key sequence not in a sample
1512 (while (and (not f) (setq m (re-search-forward re e t)))
1513 (setq f (not (checkdoc-in-sample-code-p start e))))
1514 (if m
1515 (checkdoc-create-error
1516 (concat
1517 "Keycode " (match-string 1)
1518 " embedded in doc string. Use \\\\<keymap> & \\\\[function] "
1519 "instead")
1520 (match-beginning 1) (match-end 1) t))))
1521 ;; It is not practical to use `\\[...]' very many times, because
1522 ;; display of the documentation string will become slow. So use this
1523 ;; to describe the most important commands in your major mode, and
1524 ;; then use `\\{...}' to display the rest of the mode's keymap.
1525 (save-excursion
1526 (if (re-search-forward "\\\\\\\\\\[\\w+" e t
1527 (1+ checkdoc-max-keyref-before-warn))
1528 (checkdoc-create-error
1529 "Too many occurrences of \\[function]. Use \\{keymap} instead"
1530 s (marker-position e))))
1531 ;; Ambiguous quoted symbol. When a symbol is both bound and fbound,
1532 ;; and is referred to in documentation, it should be prefixed with
1533 ;; something to disambiguate it. This check must be before the
1534 ;; 80 column check because it will probably break that.
1535 (save-excursion
1536 (let ((case-fold-search t)
1537 (ret nil) mb me)
1538 (while (and (re-search-forward "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'" e t)
1539 (not ret))
1540 (let* ((ms1 (match-string 1))
1541 (sym (intern-soft ms1)))
1542 (setq mb (match-beginning 1)
1543 me (match-end 1))
1544 (if (and sym (boundp sym) (fboundp sym)
1545 (save-excursion
1546 (goto-char mb)
1547 (forward-word -1)
1548 (not (looking-at
1549 "variable\\|option\\|function\\|command\\|symbol"))))
1550 (if (checkdoc-autofix-ask-replace
1551 mb me "Prefix this ambiguous symbol? " ms1 t)
1552 ;; We didn't actually replace anything. Here we find
1553 ;; out what special word form they wish to use as
1554 ;; a prefix.
1555 (let ((disambiguate
1556 (completing-read
1557 "Disambiguating Keyword (default: variable): "
1558 '(("function") ("command") ("variable")
1559 ("option") ("symbol"))
1560 nil t nil nil "variable")))
1561 (goto-char (1- mb))
1562 (insert disambiguate " ")
1563 (forward-word 1))
1564 (setq ret
1565 (format "Disambiguate %s by preceding w/ \
1566 function,command,variable,option or symbol." ms1))))))
1567 (if ret
1568 (checkdoc-create-error ret mb me)
1569 nil)))
1570 ;; * Format the documentation string so that it fits in an
1571 ;; Emacs window on an 80-column screen. It is a good idea
1572 ;; for most lines to be no wider than 60 characters. The
1573 ;; first line can be wider if necessary to fit the
1574 ;; information that ought to be there.
1575 (save-excursion
1576 (let ((start (point))
1577 (eol nil))
1578 (while (and (< (point) e)
1579 (or (progn (end-of-line) (setq eol (point))
1580 (< (current-column) 80))
1581 (progn (beginning-of-line)
1582 (re-search-forward "\\\\\\\\[[<{]"
1583 eol t))
1584 (checkdoc-in-sample-code-p start e)))
1585 (forward-line 1))
1586 (end-of-line)
1587 (if (and (< (point) e) (> (current-column) 80))
1588 (checkdoc-create-error
1589 "Some lines are over 80 columns wide"
1590 s (save-excursion (goto-char s) (end-of-line) (point)) ))))
1591 ;; Here we deviate to tests based on a variable or function.
1592 ;; We must do this before checking for symbols in quotes because there
1593 ;; is a chance that just such a symbol might really be an argument.
1594 (cond ((eq (nth 1 fp) t)
1595 ;; This is if we are in a variable
1596 (or
1597 ;; * The documentation string for a variable that is a
1598 ;; yes-or-no flag should start with words such as Non-nil
1599 ;; means..., to make it clear that all non-`nil' values are
1600 ;; equivalent and indicate explicitly what `nil' and non-`nil'
1601 ;; mean.
1602 ;; * If a user option variable records a true-or-false
1603 ;; condition, give it a name that ends in `-flag'.
1604
1605 ;; If the variable has -flag in the name, make sure
1606 (if (and (string-match "-flag$" (car fp))
1607 (not (looking-at "\"\\*?Non-nil\\s-+means\\s-+")))
1608 (checkdoc-create-error
1609 "Flag variable doc strings should usually start: Non-nil means"
1610 s (marker-position e) t))
1611 ;; If the doc string starts with "Non-nil means"
1612 (if (and (looking-at "\"\\*?Non-nil\\s-+means\\s-+")
1613 (not (string-match "-flag$" (car fp))))
1614 (if (checkdoc-y-or-n-p
1615 (format
1616 "Rename to %s and Query-Replace all occurances? "
1617 (concat (car fp) "-flag")))
1618 (progn
1619 (beginning-of-defun)
1620 (query-replace-regexp
1621 (concat "\\<" (regexp-quote (car fp)) "\\>")
1622 (concat (car fp) "-flag")))
1623 (checkdoc-create-error
1624 "Flag variable names should normally end in `-flag'" s
1625 (marker-position e))))
1626 ;; Done with variables
1627 ))
1628 (t
1629 ;; This if we are in a function definition
1630 (or
1631 ;; * When a function's documentation string mentions the value
1632 ;; of an argument of the function, use the argument name in
1633 ;; capital letters as if it were a name for that value. Thus,
1634 ;; the documentation string of the function `/' refers to its
1635 ;; second argument as `DIVISOR', because the actual argument
1636 ;; name is `divisor'.
1637
1638 ;; Addendum: Make sure they appear in the doc in the same
1639 ;; order that they are found in the arg list.
1640 (let ((args (cdr (cdr (cdr (cdr fp)))))
1641 (last-pos 0)
1642 (found 1)
1643 (order (and (nth 3 fp) (car (nth 3 fp))))
1644 (nocheck (append '("&optional" "&rest") (nth 3 fp)))
1645 (inopts nil))
1646 (while (and args found (> found last-pos))
1647 (if (member (car args) nocheck)
1648 (setq args (cdr args)
1649 inopts t)
1650 (setq last-pos found
1651 found (save-excursion
1652 (re-search-forward
1653 (concat "\\<" (upcase (car args))
1654 ;; Require whitespace OR
1655 ;; ITEMth<space> OR
1656 ;; ITEMs<space>
1657 "\\(\\>\\|th\\>\\|s\\>\\|[.,;:]\\)")
1658 e t)))
1659 (if (not found)
1660 (let ((case-fold-search t))
1661 ;; If the symbol was not found, let's see if we
1662 ;; can find it with a different capitalization
1663 ;; and see if the user wants to capitalize it.
1664 (if (save-excursion
1665 (re-search-forward
1666 (concat "\\<\\(" (car args)
1667 ;; Require whitespace OR
1668 ;; ITEMth<space> OR
1669 ;; ITEMs<space>
1670 "\\)\\(\\>\\|th\\>\\|s\\>\\)")
1671 e t))
1672 (if (checkdoc-autofix-ask-replace
1673 (match-beginning 1) (match-end 1)
1674 (format
1675 "If this is the argument `%s', it should appear as %s. Fix? "
1676 (car args) (upcase (car args)))
1677 (upcase (car args)) t)
1678 (setq found (match-beginning 1))))))
1679 (if found (setq args (cdr args)))))
1680 (if (not found)
1681 ;; It wasn't found at all! Offer to attach this new symbol
1682 ;; to the end of the documentation string.
1683 (if (checkdoc-y-or-n-p
1684 (format
1685 "Add %s documentation to end of doc string? "
1686 (upcase (car args))))
1687 ;; Now do some magic and invent a doc string.
1688 (save-excursion
1689 (goto-char e) (forward-char -1)
1690 (insert "\n"
1691 (if inopts "Optional a" "A")
1692 "rgument " (upcase (car args))
1693 " ")
1694 (insert (read-string "Describe: "))
1695 (if (not (save-excursion (forward-char -1)
1696 (looking-at "[.?!]")))
1697 (insert "."))
1698 nil)
1699 (checkdoc-create-error
1700 (format
1701 "Argument `%s' should appear (as %s) in the doc string"
1702 (car args) (upcase (car args)))
1703 s (marker-position e)))
1704 (if (or (and order (eq order 'yes))
1705 (and (not order) checkdoc-arguments-in-order-flag))
1706 (if (< found last-pos)
1707 (checkdoc-create-error
1708 "Arguments occur in the doc string out of order"
1709 s (marker-position e) t)))))
1710 ;; * For consistency, phrase the verb in the first sentence of a
1711 ;; documentation string for functions as an imperative.
1712 ;; For instance, use `Return the cons of A and
1713 ;; B.' in preference to `Returns the cons of A and B.'
1714 ;; Usually it looks good to do likewise for the rest of the
1715 ;; first paragraph. Subsequent paragraphs usually look better
1716 ;; if they have proper subjects.
1717 ;;
1718 ;; This is the least important of the above tests. Make sure
1719 ;; it occurs last.
1720 (and checkdoc-verb-check-experimental-flag
1721 (save-excursion
1722 ;; Maybe rebuild the monster-regex
1723 (checkdoc-create-common-verbs-regexp)
1724 (let ((lim (save-excursion
1725 (end-of-line)
1726 ;; check string-continuation
1727 (if (checkdoc-char= (preceding-char) ?\\)
1728 (progn (forward-line 1)
1729 (end-of-line)))
1730 (point)))
1731 (rs nil) replace original (case-fold-search t))
1732 (while (and (not rs)
1733 (re-search-forward
1734 checkdoc-common-verbs-regexp
1735 lim t))
1736 (setq original (buffer-substring-no-properties
1737 (match-beginning 1) (match-end 1))
1738 rs (assoc (downcase original)
1739 checkdoc-common-verbs-wrong-voice))
1740 (if (not rs) (error "Verb voice alist corrupted"))
1741 (setq replace (let ((case-fold-search nil))
1742 (save-match-data
1743 (if (string-match "^[A-Z]" original)
1744 (capitalize (cdr rs))
1745 (cdr rs)))))
1746 (if (checkdoc-autofix-ask-replace
1747 (match-beginning 1) (match-end 1)
1748 (format "Use the imperative for \"%s\". \
1749 Replace with \"%s\"? " original replace)
1750 replace t)
1751 (setq rs nil)))
1752 (if rs
1753 ;; there was a match, but no replace
1754 (checkdoc-create-error
1755 (format
1756 "Probably \"%s\" should be imperative \"%s\""
1757 original replace)
1758 (match-beginning 1) (match-end 1))))))
1759 ;; Done with functions
1760 )))
1761 ;;* When a documentation string refers to a Lisp symbol, write it as
1762 ;; it would be printed (which usually means in lower case), with
1763 ;; single-quotes around it. For example: `lambda'. There are two
1764 ;; exceptions: write t and nil without single-quotes. (In this
1765 ;; manual, we normally do use single-quotes for those symbols.)
1766 (save-excursion
1767 (let ((found nil) (start (point)) (msg nil) (ms nil))
1768 (while (and (not msg)
1769 (re-search-forward
1770 "[^-([`':a-zA-Z]\\(\\w+[:-]\\(\\w\\|\\s_\\)+\\)[^]']"
1771 e t))
1772 (setq ms (match-string 1))
1773 (save-match-data
1774 ;; A . is a \s_ char, so we must remove periods from
1775 ;; sentences more carefully.
1776 (if (string-match "\\.$" ms)
1777 (setq ms (substring ms 0 (1- (length ms))))))
1778 (if (and (not (checkdoc-in-sample-code-p start e))
1779 (not (checkdoc-in-example-string-p start e))
1780 (not (member ms checkdoc-symbol-words))
1781 (setq found (intern-soft ms))
1782 (or (boundp found) (fboundp found)))
1783 (progn
1784 (setq msg (format "Add quotes around Lisp symbol `%s'? "
1785 ms))
1786 (if (checkdoc-autofix-ask-replace
1787 (match-beginning 1) (+ (match-beginning 1)
1788 (length ms))
1789 msg (concat "`" ms "'") t)
1790 (setq msg nil)
1791 (setq msg
1792 (format "Lisp symbol `%s' should appear in quotes"
1793 ms))))))
1794 (if msg
1795 (checkdoc-create-error msg (match-beginning 1)
1796 (+ (match-beginning 1)
1797 (length ms)))
1798 nil)))
1799 ;; t and nil case
1800 (save-excursion
1801 (if (re-search-forward "\\(`\\(t\\|nil\\)'\\)" e t)
1802 (if (checkdoc-autofix-ask-replace
1803 (match-beginning 1) (match-end 1)
1804 (format "%s should not appear in quotes. Remove? "
1805 (match-string 2))
1806 (match-string 2) t)
1807 nil
1808 (checkdoc-create-error
1809 "Symbols t and nil should not appear in `...' quotes"
1810 (match-beginning 1) (match-end 1)))))
1811 ;; Here is some basic sentence formatting
1812 (checkdoc-sentencespace-region-engine (point) e)
1813 ;; Here are common proper nouns that should always appear capitalized.
1814 (checkdoc-proper-noun-region-engine (point) e)
1815 ;; Make sure the doc string has correctly spelled English words
1816 ;; in it. This function is extracted due to its complexity,
1817 ;; and reliance on the Ispell program.
1818 (checkdoc-ispell-docstring-engine e)
1819 ;; User supplied checks
1820 (save-excursion (checkdoc-run-hooks 'checkdoc-style-hooks fp e))
1821 ;; Done!
1822 )))
1823
1824 (defun checkdoc-defun-info nil
1825 "Return a list of details about the current sexp.
1826 It is a list of the form:
1827 (NAME VARIABLE INTERACTIVE NODOCPARAMS PARAMETERS ...)
1828 where NAME is the name, VARIABLE is t if this is a `defvar',
1829 INTERACTIVE is nil if this is not an interactive function, otherwise
1830 it is the position of the `interactive' call, and PARAMETERS is a
1831 string which is the name of each variable in the function's argument
1832 list. The NODOCPARAMS is a sublist of parameters specified by a checkdoc
1833 comment for a given defun. If the first element is not a string, then
1834 the token checkdoc-order: <TOKEN> exists, and TOKEN is a symbol read
1835 from the comment."
1836 (save-excursion
1837 (beginning-of-defun)
1838 (let ((defun (looking-at "(def\\(un\\|macro\\|subst\\|advice\\)"))
1839 (is-advice (looking-at "(defadvice"))
1840 (lst nil)
1841 (ret nil)
1842 (oo (make-vector 3 0))) ;substitute obarray for `read'
1843 (forward-char 1)
1844 (forward-sexp 1)
1845 (skip-chars-forward " \n\t")
1846 (setq ret
1847 (list (buffer-substring-no-properties
1848 (point) (progn (forward-sexp 1) (point)))))
1849 (if (not defun)
1850 (setq ret (cons t ret))
1851 ;; The variable spot
1852 (setq ret (cons nil ret))
1853 ;; Interactive
1854 (save-excursion
1855 (setq ret (cons
1856 (re-search-forward "^\\s-*(interactive"
1857 (save-excursion (end-of-defun) (point))
1858 t)
1859 ret)))
1860 (skip-chars-forward " \t\n")
1861 (let ((bss (buffer-substring (point) (save-excursion (forward-sexp 1)
1862 (point))))
1863 ;; Overload th main obarray so read doesn't intern the
1864 ;; local symbols of the function we are checking.
1865 ;; Without this we end up cluttering the symbol space w/
1866 ;; useless symbols.
1867 (obarray oo))
1868 ;; Ok, check for checkdoc parameter comment here
1869 (save-excursion
1870 (setq ret
1871 (cons
1872 (let ((sl1 nil))
1873 (if (re-search-forward ";\\s-+checkdoc-order:\\s-+"
1874 (save-excursion (end-of-defun)
1875 (point))
1876 t)
1877 (setq sl1 (list (cond ((looking-at "nil") 'no)
1878 ((looking-at "t") 'yes)))))
1879 (if (re-search-forward ";\\s-+checkdoc-params:\\s-+"
1880 (save-excursion (end-of-defun)
1881 (point))
1882 t)
1883 (let ((sl nil))
1884 (goto-char (match-end 0))
1885 (condition-case nil
1886 (setq lst (read (current-buffer)))
1887 (error (setq lst nil))) ; error in text
1888 (if (not (listp lst)) ; not a list of args
1889 (setq lst (list lst)))
1890 (if (and lst (not (symbolp (car lst)))) ;weird arg
1891 (setq lst nil))
1892 (while lst
1893 (setq sl (cons (symbol-name (car lst)) sl)
1894 lst (cdr lst)))
1895 (setq sl1 (append sl1 sl))))
1896 sl1)
1897 ret)))
1898 ;; Read the list of parameters, but do not put the symbols in
1899 ;; the standard obarray.
1900 (setq lst (read bss)))
1901 ;; This is because read will intern nil if it doesn't into the
1902 ;; new obarray.
1903 (if (not (listp lst)) (setq lst nil))
1904 (if is-advice nil
1905 (while lst
1906 (setq ret (cons (symbol-name (car lst)) ret)
1907 lst (cdr lst)))))
1908 (nreverse ret))))
1909
1910 (defun checkdoc-in-sample-code-p (start limit)
1911 "Return non-nil if the current point is in a code fragment.
1912 A code fragment is identified by an open parenthesis followed by a
1913 symbol which is a valid function or a word in all CAPS, or a parenthesis
1914 that is quoted with the ' character. Only the region from START to LIMIT
1915 is is allowed while searching for the bounding parenthesis."
1916 (save-match-data
1917 (save-restriction
1918 (narrow-to-region start limit)
1919 (save-excursion
1920 (and (condition-case nil (progn (up-list 1) t) (error nil))
1921 (condition-case nil (progn (forward-list -1) t) (error nil))
1922 (or (save-excursion (forward-char -1) (looking-at "'("))
1923 (and (looking-at "(\\(\\(\\w\\|[-:_]\\)+\\)[ \t\n;]")
1924 (let ((ms (buffer-substring-no-properties
1925 (match-beginning 1) (match-end 1))))
1926 ;; if this string is function bound, we are in
1927 ;; sample code. If it has a - or : character in
1928 ;; the name, then it is probably supposed to be bound
1929 ;; but isn't yet.
1930 (or (fboundp (intern-soft ms))
1931 (let ((case-fold-search nil))
1932 (string-match "^[A-Z-]+$" ms))
1933 (string-match "\\w[-:_]+\\w" ms))))))))))
1934
1935 (defun checkdoc-in-example-string-p (start limit)
1936 "Return non-nil if the current point is in an \"example string\".
1937 This string is identified by the characters \\\" surrounding the text.
1938 The text checked is between START and LIMIT."
1939 (save-match-data
1940 (save-excursion
1941 (let ((p (point))
1942 (c 0))
1943 (goto-char start)
1944 (while (and (< (point) p) (re-search-forward "\\\\\"" limit t))
1945 (setq c (1+ c)))
1946 (and (< 0 c) (= (% c 2) 0))))))
1947
1948 (defun checkdoc-proper-noun-region-engine (begin end)
1949 "Check all text between BEGIN and END for lower case proper nouns.
1950 These are Emacs centric proper nouns which should be capitalized for
1951 consistency. Return an error list if any are not fixed, but
1952 internally skip over no answers.
1953 If the offending word is in a piece of quoted text, then it is skipped."
1954 (save-excursion
1955 (let ((case-fold-search nil)
1956 (errtxt nil) bb be
1957 (old-syntax-table (syntax-table)))
1958 (unwind-protect
1959 (progn
1960 (set-syntax-table checkdoc-syntax-table)
1961 (goto-char begin)
1962 (while (re-search-forward checkdoc-proper-noun-regexp end t)
1963 (let ((text (match-string 1))
1964 (b (match-beginning 1))
1965 (e (match-end 1)))
1966 (if (and (not (save-excursion
1967 (goto-char b)
1968 (forward-char -1)
1969 (looking-at "`\\|\"\\|\\.\\|\\\\")))
1970 ;; surrounded by /, as in a URL or filename: /emacs/
1971 (not (and (= ?/ (char-after e))
1972 (= ?/ (char-before b))))
1973 (not (checkdoc-in-example-string-p begin end)))
1974 (if (checkdoc-autofix-ask-replace
1975 b e (format "Text %s should be capitalized. Fix? "
1976 text)
1977 (capitalize text) t)
1978 nil
1979 (if errtxt
1980 ;; If there is already an error, then generate
1981 ;; the warning output if applicable
1982 (if checkdoc-generate-compile-warnings-flag
1983 (checkdoc-create-error
1984 (format
1985 "Name %s should appear capitalized as %s"
1986 text (capitalize text))
1987 b e))
1988 (setq errtxt
1989 (format
1990 "Name %s should appear capitalized as %s"
1991 text (capitalize text))
1992 bb b be e)))))))
1993 (set-syntax-table old-syntax-table))
1994 (if errtxt (checkdoc-create-error errtxt bb be)))))
1995
1996 (defun checkdoc-sentencespace-region-engine (begin end)
1997 "Make sure all sentences have double spaces between BEGIN and END."
1998 (if sentence-end-double-space
1999 (save-excursion
2000 (let ((case-fold-search nil)
2001 (errtxt nil) bb be
2002 (old-syntax-table (syntax-table)))
2003 (unwind-protect
2004 (progn
2005 (set-syntax-table checkdoc-syntax-table)
2006 (goto-char begin)
2007 (while (re-search-forward "[^ .0-9]\\(\\. \\)[^ \n]" end t)
2008 (let ((b (match-beginning 1))
2009 (e (match-end 1)))
2010 (unless (or (checkdoc-in-sample-code-p begin end)
2011 (checkdoc-in-example-string-p begin end)
2012 (save-excursion
2013 (goto-char b)
2014 (condition-case nil
2015 (progn
2016 (forward-sexp -1)
2017 ;; piece of an abbreviation
2018 (looking-at
2019 "\\([a-z]\\|[iI]\\.?e\\|[eE]\\.?g\\)\\."))
2020 (error t))))
2021 (if (checkdoc-autofix-ask-replace
2022 b e
2023 "There should be two spaces after a period. Fix? "
2024 ". ")
2025 nil
2026 (if errtxt
2027 ;; If there is already an error, then generate
2028 ;; the warning output if applicable
2029 (if checkdoc-generate-compile-warnings-flag
2030 (checkdoc-create-error
2031 "There should be two spaces after a period"
2032 b e))
2033 (setq errtxt
2034 "There should be two spaces after a period"
2035 bb b be e)))))))
2036 (set-syntax-table old-syntax-table))
2037 (if errtxt (checkdoc-create-error errtxt bb be))))))
2038
2039 ;;; Ispell engine
2040 ;;
2041 (eval-when-compile (require 'ispell))
2042
2043 (defun checkdoc-ispell-init ()
2044 "Initialize Ispell process (default version) with Lisp words.
2045 The words used are from `checkdoc-ispell-lisp-words'. If `ispell'
2046 cannot be loaded, then set `checkdoc-spellcheck-documentation-flag' to
2047 nil."
2048 (require 'ispell)
2049 (if (not (symbol-value 'ispell-process)) ;Silence byteCompiler
2050 (condition-case nil
2051 (progn
2052 (ispell-buffer-local-words)
2053 ;; This code copied in part from ispell.el Emacs 19.34
2054 (let ((w checkdoc-ispell-lisp-words))
2055 (while w
2056 (process-send-string
2057 ;; Silence byte compiler
2058 (symbol-value 'ispell-process)
2059 (concat "@" (car w) "\n"))
2060 (setq w (cdr w)))))
2061 (error (setq checkdoc-spellcheck-documentation-flag nil)))))
2062
2063 (defun checkdoc-ispell-docstring-engine (end)
2064 "Run the Ispell tools on the doc string between point and END.
2065 Since Ispell isn't Lisp-smart, we must pre-process the doc string
2066 before using the Ispell engine on it."
2067 (if (or (not checkdoc-spellcheck-documentation-flag)
2068 ;; If the user wants no questions or fixing, then we must
2069 ;; disable spell checking as not useful.
2070 (not checkdoc-autofix-flag)
2071 (eq checkdoc-autofix-flag 'never))
2072 nil
2073 (checkdoc-ispell-init)
2074 (save-excursion
2075 (skip-chars-forward "^a-zA-Z")
2076 (let ((word nil) (sym nil) (case-fold-search nil) (err nil))
2077 (while (and (not err) (< (point) end))
2078 (if (save-excursion (forward-char -1) (looking-at "[('`]"))
2079 ;; Skip lists describing meta-syntax, or bound variables
2080 (forward-sexp 1)
2081 (setq word (buffer-substring-no-properties
2082 (point) (progn
2083 (skip-chars-forward "a-zA-Z-")
2084 (point)))
2085 sym (intern-soft word))
2086 (if (and sym (or (boundp sym) (fboundp sym)))
2087 ;; This is probably repetitive in most cases, but not always.
2088 nil
2089 ;; Find out how we spell-check this word.
2090 (if (or
2091 ;; All caps w/ option th, or s tacked on the end
2092 ;; for pluralization or numberthness.
2093 (string-match "^[A-Z][A-Z]+\\(s\\|th\\)?$" word)
2094 (looking-at "}") ; a keymap expression
2095 )
2096 nil
2097 (save-excursion
2098 (if (not (eq checkdoc-autofix-flag 'never))
2099 (let ((lk last-input-event))
2100 (ispell-word nil t)
2101 (if (not (equal last-input-event lk))
2102 (progn
2103 (sit-for 0)
2104 (message "Continuing..."))))
2105 ;; Nothing here.
2106 )))))
2107 (skip-chars-forward "^a-zA-Z"))
2108 err))))
2109
2110 ;;; Rogue space checking engine
2111 ;;
2112 (defun checkdoc-rogue-space-check-engine (&optional start end interact)
2113 "Return a message list if there is a line with white space at the end.
2114 If `checkdoc-autofix-flag' permits, delete that whitespace instead.
2115 If optional arguments START and END are non nil, bound the check to
2116 this region.
2117 Optional argument INTERACT may permit the user to fix problems on the fly."
2118 (let ((p (point))
2119 (msg nil) s e (f nil))
2120 (if (not start) (setq start (point-min)))
2121 ;; If end is nil, it means end of buffer to search anyway
2122 (or
2123 ;; Check for an error if `? ' or `?\ ' is used at the end of a line.
2124 ;; (It's dangerous)
2125 (progn
2126 (goto-char start)
2127 (while (and (not msg) (re-search-forward "\\?\\\\?[ \t][ \t]*$" end t))
2128 (setq msg
2129 "Don't use `? ' at the end of a line. \
2130 News agents may remove it"
2131 s (match-beginning 0) e (match-end 0) f t)
2132 ;; If interactive is passed down, give them a chance to fix things.
2133 (if (and interact (y-or-n-p (concat msg ". Fix? ")))
2134 (progn
2135 (checkdoc-recursive-edit msg)
2136 (setq msg nil)
2137 (goto-char s)
2138 (beginning-of-line)))))
2139 ;; Check for, and potentially remove whitespace appearing at the
2140 ;; end of different lines.
2141 (progn
2142 (goto-char start)
2143 ;; There is no documentation in the Emacs Lisp manual about this check,
2144 ;; it is intended to help clean up messy code and reduce the file size.
2145 (while (and (not msg) (re-search-forward "[^ \t\n;]\\([ \t]+\\)$" end t))
2146 ;; This is not a complex activity
2147 (if (checkdoc-autofix-ask-replace
2148 (match-beginning 1) (match-end 1)
2149 "White space at end of line. Remove? " "")
2150 nil
2151 (setq msg "White space found at end of line"
2152 s (match-beginning 1) e (match-end 1))))))
2153 ;; Return an error and leave the cursor at that spot, or restore
2154 ;; the cursor.
2155 (if msg
2156 (checkdoc-create-error msg s e f)
2157 (goto-char p)
2158 nil)))
2159
2160 ;;; Comment checking engine
2161 ;;
2162 (eval-when-compile
2163 ;; We must load this to:
2164 ;; a) get symbols for compile and
2165 ;; b) determine if we have lm-history symbol which doesn't always exist
2166 (require 'lisp-mnt))
2167
2168 (defun checkdoc-file-comments-engine ()
2169 "Return a message list if this file does not match the Emacs standard.
2170 This checks for style only, such as the first line, Commentary:,
2171 Code:, and others referenced in the style guide."
2172 (if (featurep 'lisp-mnt)
2173 nil
2174 (require 'lisp-mnt)
2175 ;; Old XEmacs don't have `lm-commentary-mark'
2176 (if (and (not (fboundp 'lm-commentary-mark)) (boundp 'lm-commentary))
2177 (defalias 'lm-commentary-mark 'lm-commentary)))
2178 (save-excursion
2179 (let* ((f1 (file-name-nondirectory (buffer-file-name)))
2180 (fn (file-name-sans-extension f1))
2181 (fe (substring f1 (length fn)))
2182 (err nil))
2183 (goto-char (point-min))
2184 ;; This file has been set up where ERR is a variable. Each check is
2185 ;; asked, and the function will make sure that if the user does not
2186 ;; auto-fix some error, that we still move on to the next auto-fix,
2187 ;; AND we remember the past errors.
2188 (setq
2189 err
2190 ;; Lisp Maintenance checks first
2191 ;; Was: (lm-verify) -> not flexible enough for some people
2192 ;; * Summary at the beginning of the file:
2193 (if (not (lm-summary))
2194 ;; This certifies as very complex so always ask unless
2195 ;; it's set to never
2196 (if (checkdoc-y-or-n-p "There is no first line summary! Add one? ")
2197 (progn
2198 (goto-char (point-min))
2199 (insert ";;; " fn fe " --- " (read-string "Summary: ") "\n"))
2200 (checkdoc-create-error
2201 "The first line should be of the form: \";;; package --- Summary\""
2202 (point-min) (save-excursion (goto-char (point-min)) (end-of-line)
2203 (point))))
2204 nil))
2205 (setq
2206 err
2207 (or
2208 ;; * Commentary Section
2209 (if (not (lm-commentary-mark))
2210 (progn
2211 (goto-char (point-min))
2212 (cond
2213 ((re-search-forward
2214 "write\\s-+to\\s-+the\\s-+Free Software Foundation, Inc."
2215 nil t)
2216 (re-search-forward "^;;\\s-*\n\\|^\n" nil t))
2217 ((or (re-search-forward "^;;; History" nil t)
2218 (re-search-forward "^;;; Code" nil t)
2219 (re-search-forward "^(require" nil t)
2220 (re-search-forward "^("))
2221 (beginning-of-line)))
2222 (if (checkdoc-y-or-n-p
2223 "You should have a \";;; Commentary:\", add one? ")
2224 (insert "\n;;; Commentary:\n;; \n\n")
2225 (checkdoc-create-error
2226 "You should have a section marked \";;; Commentary:\""
2227 nil nil t)))
2228 nil)
2229 err))
2230 (setq
2231 err
2232 (or
2233 ;; * History section. Say nothing if there is a file ChangeLog
2234 (if (or (not checkdoc-force-history-flag)
2235 (file-exists-p "ChangeLog")
2236 (file-exists-p "../ChangeLog")
2237 (let ((fn 'lm-history-mark)) ;bestill byte-compiler
2238 (and (fboundp fn) (funcall fn))))
2239 nil
2240 (progn
2241 (goto-char (or (lm-commentary-mark) (point-min)))
2242 (cond
2243 ((re-search-forward
2244 "write\\s-+to\\s-+the\\s-+Free Software Foundation, Inc."
2245 nil t)
2246 (re-search-forward "^;;\\s-*\n\\|^\n" nil t))
2247 ((or (re-search-forward "^;;; Code" nil t)
2248 (re-search-forward "^(require" nil t)
2249 (re-search-forward "^("))
2250 (beginning-of-line)))
2251 (if (checkdoc-y-or-n-p
2252 "You should have a \";;; History:\", add one? ")
2253 (insert "\n;;; History:\n;; \n\n")
2254 (checkdoc-create-error
2255 "You should have a section marked \";;; History:\" or use a ChangeLog"
2256 (point) nil))))
2257 err))
2258 (setq
2259 err
2260 (or
2261 ;; * Code section
2262 (if (not (lm-code-mark))
2263 (let ((cont t))
2264 (goto-char (point-min))
2265 (while (and cont (re-search-forward "^(" nil t))
2266 (setq cont (looking-at "require\\s-+")))
2267 (if (and (not cont)
2268 (checkdoc-y-or-n-p
2269 "There is no ;;; Code: marker. Insert one? "))
2270 (progn (beginning-of-line)
2271 (insert ";;; Code:\n")
2272 nil)
2273 (checkdoc-create-error
2274 "You should have a section marked \";;; Code:\""
2275 (point) nil)))
2276 nil)
2277 err))
2278 (setq
2279 err
2280 (or
2281 ;; * A footer. Not compartmentalized from lm-verify: too bad.
2282 ;; The following is partially clipped from lm-verify
2283 (save-excursion
2284 (goto-char (point-max))
2285 (if (not (re-search-backward
2286 (concat "^;;;[ \t]+" fn "\\(" (regexp-quote fe)
2287 "\\)?[ \t]+ends here[ \t]*$"
2288 "\\|^;;;[ \t]+ End of file[ \t]+"
2289 fn "\\(" (regexp-quote fe) "\\)?")
2290 nil t))
2291 (if (checkdoc-y-or-n-p "No identifiable footer! Add one? ")
2292 (progn
2293 (goto-char (point-max))
2294 (insert "\n(provide '" fn ")\n\n;;; " fn fe " ends here\n"))
2295 (checkdoc-create-error
2296 (format "The footer should be: (provide '%s)\\n;;; %s%s ends here"
2297 fn fn fe)
2298 (1- (point-max)) (point-max)))))
2299 err))
2300 ;; The below checks will not return errors if the user says NO
2301
2302 ;; Let's spellcheck the commentary section. This is the only
2303 ;; section that is easy to pick out, and it is also the most
2304 ;; visible section (with the finder).
2305 (let ((cm (lm-commentary-mark)))
2306 (if cm
2307 (save-excursion
2308 (goto-char (lm-commentary-mark))
2309 ;; Spellcheck between the commentary, and the first
2310 ;; non-comment line. We could use lm-commentary, but that
2311 ;; returns a string, and Ispell wants to talk to a buffer.
2312 ;; Since the comments talk about Lisp, use the specialized
2313 ;; spell-checker we also used for doc strings.
2314 (let ((e (save-excursion (re-search-forward "^[^;]" nil t)
2315 (point))))
2316 (checkdoc-sentencespace-region-engine (point) e)
2317 (checkdoc-proper-noun-region-engine (point) e)
2318 (checkdoc-ispell-docstring-engine e)))))
2319 ;;; test comment out code
2320 ;;; (foo 1 3)
2321 ;;; (bar 5 7)
2322 (setq
2323 err
2324 (or
2325 ;; Generic Full-file checks (should be comment related)
2326 (checkdoc-run-hooks 'checkdoc-comment-style-hooks)
2327 err))
2328 ;; Done with full file comment checks
2329 err)))
2330
2331 (defun checkdoc-outside-major-sexp ()
2332 "Return t if point is outside the bounds of a valid sexp."
2333 (save-match-data
2334 (save-excursion
2335 (let ((p (point)))
2336 (or (progn (beginning-of-defun) (bobp))
2337 (progn (end-of-defun) (< (point) p)))))))
2338
2339 ;;; `error' and `message' text verifier.
2340 ;;
2341 (defun checkdoc-message-text-search (&optional beg end)
2342 "Search between BEG and END for a style error with message text.
2343 Optional arguments BEG and END represent the boundary of the check.
2344 The default boundary is the entire buffer."
2345 (let ((e nil)
2346 (type nil))
2347 (if (not (or beg end)) (setq beg (point-min) end (point-max)))
2348 (goto-char beg)
2349 (while (setq type (checkdoc-message-text-next-string end))
2350 (setq e (checkdoc-message-text-engine type)))
2351 e))
2352
2353 (defun checkdoc-message-text-next-string (end)
2354 "Move cursor to the next checkable message string after point.
2355 Return the message classification.
2356 Argument END is the maximum bounds to search in."
2357 (let ((return nil))
2358 (while (and (not return)
2359 (re-search-forward
2360 "(\\s-*\\(\\(\\w\\|\\s_\\)*error\\|\
2361 \\(\\w\\|\\s_\\)*y-or-n-p\\(-with-timeout\\)?\
2362 \\|checkdoc-autofix-ask-replace\\)[ \t\n]+" end t))
2363 (let* ((fn (match-string 1))
2364 (type (cond ((string-match "error" fn)
2365 'error)
2366 (t 'y-or-n-p))))
2367 (if (string-match "checkdoc-autofix-ask-replace" fn)
2368 (progn (forward-sexp 2)
2369 (skip-chars-forward " \t\n")))
2370 (if (and (eq type 'y-or-n-p)
2371 (looking-at "(format[ \t\n]+"))
2372 (goto-char (match-end 0)))
2373 (skip-chars-forward " \t\n")
2374 (if (not (looking-at "\""))
2375 nil
2376 (setq return type))))
2377 return))
2378
2379 (defun checkdoc-message-text-engine (&optional type)
2380 "Return or fix errors found in strings passed to a message display function.
2381 According to the documentation for the function `error', the error list
2382 should not end with a period, and should start with a capital letter.
2383 The function `y-or-n-p' has similar constraints.
2384 Argument TYPE specifies the type of question, such as `error or `y-or-n-p."
2385 ;; If type is nil, then attempt to derive it.
2386 (if (not type)
2387 (save-excursion
2388 (up-list -1)
2389 (if (looking-at "(format")
2390 (up-list -1))
2391 (setq type
2392 (cond ((looking-at "(error")
2393 'error)
2394 (t 'y-or-n-p)))))
2395 (let ((case-fold-search nil))
2396 (or
2397 ;; From the documentation of the symbol `error':
2398 ;; In Emacs, the convention is that error messages start with a capital
2399 ;; letter but *do not* end with a period. Please follow this convention
2400 ;; for the sake of consistency.
2401 (if (and (save-excursion (forward-char 1)
2402 (looking-at "[a-z]\\w+"))
2403 (not (checkdoc-autofix-ask-replace
2404 (match-beginning 0) (match-end 0)
2405 "Capitalize your message text? "
2406 (capitalize (match-string 0))
2407 t)))
2408 (checkdoc-create-error
2409 "Messages should start with a capital letter"
2410 (match-beginning 0) (match-end 0))
2411 nil)
2412 ;; In general, sentences should have two spaces after the period.
2413 (checkdoc-sentencespace-region-engine (point)
2414 (save-excursion (forward-sexp 1)
2415 (point)))
2416 ;; Look for proper nouns in this region too.
2417 (checkdoc-proper-noun-region-engine (point)
2418 (save-excursion (forward-sexp 1)
2419 (point)))
2420 ;; Here are message type specific questions.
2421 (if (and (eq type 'error)
2422 (save-excursion (forward-sexp 1)
2423 (forward-char -2)
2424 (looking-at "\\."))
2425 (not (checkdoc-autofix-ask-replace (match-beginning 0)
2426 (match-end 0)
2427 "Remove period from error? "
2428 ""
2429 t)))
2430 (checkdoc-create-error
2431 "Error messages should *not* end with a period"
2432 (match-beginning 0) (match-end 0))
2433 nil)
2434 ;; `y-or-n-p' documentation explicitly says:
2435 ;; It should end in a space; `y-or-n-p' adds `(y or n) ' to it.
2436 ;; I added the ? requirement. Without it, it is unclear that we
2437 ;; ask a question and it appears to be an undocumented style.
2438 (if (eq type 'y-or-n-p)
2439 (if (not (save-excursion (forward-sexp 1)
2440 (forward-char -3)
2441 (not (looking-at "\\? "))))
2442 nil
2443 (if (save-excursion (forward-sexp 1)
2444 (forward-char -2)
2445 (looking-at "\\?"))
2446 ;; If we see a ?, then replace with "? ".
2447 (if (checkdoc-autofix-ask-replace
2448 (match-beginning 0) (match-end 0)
2449 "`y-or-n-p' argument should end with \"? \". Fix? "
2450 "? " t)
2451 nil
2452 (checkdoc-create-error
2453 "`y-or-n-p' argument should end with \"? \""
2454 (match-beginning 0) (match-end 0)))
2455 (if (save-excursion (forward-sexp 1)
2456 (forward-char -2)
2457 (looking-at " "))
2458 (if (checkdoc-autofix-ask-replace
2459 (match-beginning 0) (match-end 0)
2460 "`y-or-n-p' argument should end with \"? \". Fix? "
2461 "? " t)
2462 nil
2463 (checkdoc-create-error
2464 "`y-or-n-p' argument should end with \"? \""
2465 (match-beginning 0) (match-end 0)))
2466 (if (and ;; if this isn't true, we have a problem.
2467 (save-excursion (forward-sexp 1)
2468 (forward-char -1)
2469 (looking-at "\""))
2470 (checkdoc-autofix-ask-replace
2471 (match-beginning 0) (match-end 0)
2472 "`y-or-n-p' argument should end with \"? \". Fix? "
2473 "? \"" t))
2474 nil
2475 (checkdoc-create-error
2476 "`y-or-n-p' argument should end with \"? \""
2477 (match-beginning 0) (match-end 0)))))))
2478 ;; Now, let's just run the spell checker on this guy.
2479 (checkdoc-ispell-docstring-engine (save-excursion (forward-sexp 1)
2480 (point)))
2481 )))
2482
2483 ;;; Auto-fix helper functions
2484 ;;
2485 (defun checkdoc-y-or-n-p (question)
2486 "Like `y-or-n-p', but pays attention to `checkdoc-autofix-flag'.
2487 Argument QUESTION is the prompt passed to `y-or-n-p'."
2488 (prog1
2489 (if (or (not checkdoc-autofix-flag)
2490 (eq checkdoc-autofix-flag 'never))
2491 nil
2492 (y-or-n-p question))
2493 (if (eq checkdoc-autofix-flag 'automatic-then-never)
2494 (setq checkdoc-autofix-flag 'never))))
2495
2496 (defun checkdoc-autofix-ask-replace (start end question replacewith
2497 &optional complex)
2498 "Highlight between START and END and queries the user with QUESTION.
2499 If the user says yes, or if `checkdoc-autofix-flag' permits, replace
2500 the region marked by START and END with REPLACEWITH. If optional flag
2501 COMPLEX is non-nil, then we may ask the user a question. See the
2502 documentation for `checkdoc-autofix-flag' for details.
2503
2504 If a section is auto-replaced without asking the user, this function
2505 will pause near the fixed code so the user will briefly see what
2506 happened.
2507
2508 This function returns non-nil if the text was replaced.
2509
2510 This function will not modify `match-data'."
2511 (if (and checkdoc-autofix-flag
2512 (not (eq checkdoc-autofix-flag 'never)))
2513 (let ((o (checkdoc-make-overlay start end))
2514 (ret nil)
2515 (md (match-data)))
2516 (unwind-protect
2517 (progn
2518 (checkdoc-overlay-put o 'face 'highlight)
2519 (if (or (eq checkdoc-autofix-flag 'automatic)
2520 (eq checkdoc-autofix-flag 'automatic-then-never)
2521 (and (eq checkdoc-autofix-flag 'semiautomatic)
2522 (not complex))
2523 (and (or (eq checkdoc-autofix-flag 'query) complex)
2524 (y-or-n-p question)))
2525 (save-excursion
2526 (goto-char start)
2527 ;; On the off chance this is automatic, display
2528 ;; the question anyway so the user knows what's
2529 ;; going on.
2530 (if checkdoc-bouncy-flag (message "%s -> done" question))
2531 (delete-region start end)
2532 (insert replacewith)
2533 (if checkdoc-bouncy-flag (sit-for 0))
2534 (setq ret t)))
2535 (checkdoc-delete-overlay o)
2536 (set-match-data md))
2537 (checkdoc-delete-overlay o)
2538 (set-match-data md))
2539 (if (eq checkdoc-autofix-flag 'automatic-then-never)
2540 (setq checkdoc-autofix-flag 'never))
2541 ret)))
2542
2543 ;;; Warning management
2544 ;;
2545 (defvar checkdoc-output-font-lock-keywords
2546 '(("\\(\\w+\\.el\\): \\(\\w+\\)"
2547 (1 font-lock-function-name-face)
2548 (2 font-lock-comment-face))
2549 ("^\\(\\w+\\.el\\):" 1 font-lock-function-name-face)
2550 (":\\([0-9]+\\):" 1 font-lock-constant-face))
2551 "Keywords used to highlight a checkdoc diagnostic buffer.")
2552
2553 (defvar checkdoc-output-mode-map nil
2554 "Keymap used in `checkdoc-output-mode'.")
2555
2556 (defvar checkdoc-pending-errors nil
2557 "Non-nil when there are errors that have not been displayed yet.")
2558
2559 (if checkdoc-output-mode-map
2560 nil
2561 (setq checkdoc-output-mode-map (make-sparse-keymap))
2562 (if (not (string-match "XEmacs" emacs-version))
2563 (define-key checkdoc-output-mode-map [mouse-2]
2564 'checkdoc-find-error-mouse))
2565 (define-key checkdoc-output-mode-map "\C-c\C-c" 'checkdoc-find-error)
2566 (define-key checkdoc-output-mode-map "\C-m" 'checkdoc-find-error))
2567
2568 (defun checkdoc-output-mode ()
2569 "Create and setup the buffer used to maintain checkdoc warnings.
2570 \\<checkdoc-output-mode-map>\\[checkdoc-find-error] - Go to this error location
2571 \\[checkdoc-find-error-mouse] - Goto the error clicked on."
2572 (if (get-buffer checkdoc-diagnostic-buffer)
2573 (get-buffer checkdoc-diagnostic-buffer)
2574 (save-excursion
2575 (set-buffer (get-buffer-create checkdoc-diagnostic-buffer))
2576 (kill-all-local-variables)
2577 (setq mode-name "Checkdoc"
2578 major-mode 'checkdoc-output-mode)
2579 (set (make-local-variable 'font-lock-defaults)
2580 '((checkdoc-output-font-lock-keywords) t t ((?- . "w") (?_ . "w"))))
2581 (use-local-map checkdoc-output-mode-map)
2582 (run-hooks 'checkdoc-output-mode-hook)
2583 (current-buffer))))
2584
2585 (defun checkdoc-find-error-mouse (e)
2586 ;; checkdoc-params: (e)
2587 "Call `checkdoc-find-error' where the user clicks the mouse."
2588 (interactive "e")
2589 (mouse-set-point e)
2590 (checkdoc-find-error))
2591
2592 (defun checkdoc-find-error ()
2593 "In a checkdoc diagnostic buffer, find the error under point."
2594 (interactive)
2595 (beginning-of-line)
2596 (if (looking-at "\\(\\(\\w+\\|\\s_\\)+\\.el\\):\\([0-9]+\\):")
2597 (let ((l (string-to-int (match-string 3)))
2598 (f (match-string 1)))
2599 (if (not (get-buffer f))
2600 (error "Can't find buffer %s" f))
2601 (switch-to-buffer-other-window (get-buffer f))
2602 (goto-line l))))
2603
2604 (defun checkdoc-buffer-label ()
2605 "The name to use for a checkdoc buffer in the error list."
2606 (if (buffer-file-name)
2607 (file-name-nondirectory (buffer-file-name))
2608 (concat "#<buffer "(buffer-name) ">")))
2609
2610 (defun checkdoc-start-section (check-type)
2611 "Initialize the checkdoc diagnostic buffer for a pass.
2612 Create the header so that the string CHECK-TYPE is displayed as the
2613 function called to create the messages."
2614 (checkdoc-output-to-error-buffer
2615 "\n\n\C-l\n*** "
2616 (checkdoc-buffer-label) ": " check-type " V " checkdoc-version))
2617
2618 (defun checkdoc-error (point msg)
2619 "Store POINT and MSG as errors in the checkdoc diagnostic buffer."
2620 (setq checkdoc-pending-errors t)
2621 (checkdoc-output-to-error-buffer
2622 "\n" (checkdoc-buffer-label) ":"
2623 (int-to-string (count-lines (point-min) (or point 1))) ": "
2624 msg))
2625
2626 (defun checkdoc-output-to-error-buffer (&rest text)
2627 "Place TEXT into the checkdoc diagnostic buffer."
2628 (save-excursion
2629 (set-buffer (checkdoc-output-mode))
2630 (goto-char (point-max))
2631 (apply 'insert text)))
2632
2633 (defun checkdoc-show-diagnostics ()
2634 "Display the checkdoc diagnostic buffer in a temporary window."
2635 (if checkdoc-pending-errors
2636 (let ((b (get-buffer checkdoc-diagnostic-buffer)))
2637 (if b (progn (pop-to-buffer b)
2638 (goto-char (point-max))
2639 (re-search-backward "\C-l" nil t)
2640 (beginning-of-line)
2641 (forward-line 1)
2642 (recenter 0)))
2643 (other-window -1)
2644 (setq checkdoc-pending-errors nil)
2645 nil)))
2646
2647 (defgroup checkdoc nil
2648 "Support for doc string checking in Emacs Lisp."
2649 :prefix "checkdoc"
2650 :group 'lisp
2651 :version "20.3")
2652
2653 (custom-add-option 'emacs-lisp-mode-hook
2654 (lambda () (checkdoc-minor-mode 1)))
2655
2656 (add-to-list 'debug-ignored-errors
2657 "Argument `.*' should appear (as .*) in the doc string")
2658 (add-to-list 'debug-ignored-errors "Disambiguate .* by preceding .*")
2659
2660 (provide 'checkdoc)
2661
2662 ;;; checkdoc.el ends here