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