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