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