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