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