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