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