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