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