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