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