checkdoc.el 98.7 KB
Newer Older
1
;;; checkdoc.el --- check documentation strings for style requirements
Karl Heuer's avatar
Karl Heuer committed
2

3
;;;  Copyright (C) 1997, 1998, 2001  Free Software Foundation
Eric M. Ludlam's avatar
Eric M. Ludlam committed
4

5
;; Author: Eric M. Ludlam <zappo@gnu.org>
6
;; Version: 0.6.2
Karl Heuer's avatar
Karl Heuer committed
7
;; Keywords: docs, maint, lisp
Eric M. Ludlam's avatar
Eric M. Ludlam committed
8

Karl Heuer's avatar
Karl Heuer committed
9
;; This file is part of GNU Emacs.
Eric M. Ludlam's avatar
Eric M. Ludlam committed
10

Karl Heuer's avatar
Karl Heuer committed
11 12 13 14
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
Eric M. Ludlam's avatar
Eric M. Ludlam committed
15

Karl Heuer's avatar
Karl Heuer committed
16 17 18 19
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
Eric M. Ludlam's avatar
Eric M. Ludlam committed
20

Karl Heuer's avatar
Karl Heuer committed
21 22 23 24 25 26 27
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:
;;
Richard M. Stallman's avatar
Richard M. Stallman committed
28
;;   The Emacs Lisp manual has a nice chapter on how to write
Karl Heuer's avatar
Karl Heuer committed
29 30 31 32 33 34
;; documentation strings.  Many stylistic suggestions are fairly
;; deterministic and easy to check for syntactically, but also easy
;; to forget.  The main checkdoc engine will perform the stylistic
;; checks needed to make sure these styles are remembered.
;;
;; There are two ways to use checkdoc:
35 36 37
;;   1) Periodically use `checkdoc' or `checkdoc-current-buffer'.
;;      `checkdoc' is a more interactive version of
;;      `checkdoc-current-buffer'
Karl Heuer's avatar
Karl Heuer committed
38
;;   2) Use `checkdoc-minor-mode' to automatically check your
Richard M. Stallman's avatar
Richard M. Stallman committed
39
;;      documentation whenever you evaluate Lisp code with C-M-x
Karl Heuer's avatar
Karl Heuer committed
40 41 42 43 44 45
;;      or [menu-bar emacs-lisp eval-buffer].  Additional key-bindings
;;      are also provided under C-c ? KEY
;;        (require 'checkdoc)
;;        (add-hook 'emacs-lisp-mode-hook
;;	             '(lambda () (checkdoc-minor-mode 1)))
;;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
;; Using `checkdoc':
;;
;;   The commands `checkdoc' and `checkdoc-ispell' are the top-level
;; entry points to all of the different checks that are available.  It
;; breaks examination of your Lisp file into four sections (comments,
;; documentation, messages, and spacing) and indicates its current
;; state in a status buffer.
;;
;;   The Comments check examines your headers, footers, and
;; various tags (such as "Code:") to make sure that your code is ready
;; for easy integration into existing systems.
;;
;;   The Documentation check deals with documentation strings
;; and their elements that help make Emacs easier to use.
;;
;;   The Messages check ensures that the strings displayed in the
;; minibuffer by some commands (such as `error' and `y-or-n-p')
;; are consistent with the Emacs environment.
;;
;;   The Spacing check cleans up white-space at the end of lines.
;;
;;   The interface while working with documentation and messages is
;; slightly different when being run in the interactive mode.  The
;; interface offers several options, including the ability to skip to
;; the next error, or back up to previous errors.  Auto-fixing is
;; turned off at this stage, but you can use the `f' or `F' key to fix
;; a given error (if the fix is available.)
;;
Karl Heuer's avatar
Karl Heuer committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
;; Auto-fixing:
;;
;;   There are four classifications of style errors in terms of how
;; easy they are to fix.  They are simple, complex, really complex,
;; and impossible.  (Impossible really means that checkdoc does not
;; have a fixing routine yet.)  Typically white-space errors are
;; classified as simple, and are auto-fixed by default.  Typographic
;; changes are considered complex, and the user is asked if they want
;; the problem fixed before checkdoc makes the change.  These changes
;; can be done without asking if `checkdoc-autofix-flag' is properly
;; set.  Potentially redundant changes are considered really complex,
;; and the user is always asked before a change is inserted.  The
;; variable `checkdoc-autofix-flag' controls how these types of errors
;; are fixed.
;;
89
;; Spell checking text:
Karl Heuer's avatar
Karl Heuer committed
90 91 92 93 94 95 96 97 98 99 100
;;
;;   The variable `checkdoc-spellcheck-documentation-flag' can be set
;; to customize how spell checking is to be done.  Since spell
;; checking can be quite slow, you can optimize how best you want your
;; checking done.  The default is 'defun, which spell checks each time
;; `checkdoc-defun' or `checkdoc-eval-defun' is used.  Setting to nil
;; prevents spell checking during normal usage.
;;   Setting this variable to nil does not mean you cannot take
;; advantage of the spell checking.  You can instead use the
;; interactive functions `checkdoc-ispell-*' to check the spelling of
;; your documentation.
Richard M. Stallman's avatar
Richard M. Stallman committed
101 102
;;   There is a list of Lisp-specific words which checkdoc will
;; install into Ispell on the fly, but only if Ispell is not already
Karl Heuer's avatar
Karl Heuer committed
103 104 105
;; running.  Use `ispell-kill-ispell' to make checkdoc restart it with
;; these words enabled.
;;
106
;; Checking parameters:
107
;;
Karl Heuer's avatar
Karl Heuer committed
108
;;   You might not always want a function to have its parameters listed
109 110 111 112 113 114 115 116 117 118
;; in order.  When this is the case, put the following comment just in
;; front of the documentation string: "; checkdoc-order: nil"  This
;; overrides the value of `checkdoc-arguments-in-order-flag'.
;;
;;   If you specifically wish to avoid mentioning a parameter of a
;; function in the doc string (such as a hidden parameter, or a
;; parameter which is very obvious like events), you can have checkdoc
;; skip looking for it by putting the following comment just in front
;; of the documentation string: "; checkdoc-params: (args go here)"
;;
119
;; Checking message strings:
120
;;
121
;;   The text that follows the `error' and `y-or-n-p' commands is
122 123 124 125 126
;; also checked.  The documentation for `error' clearly states some
;; simple style rules to follow which checkdoc will auto-fix for you.
;; `y-or-n-p' also states that it should end in a space.  I added that
;; it should end in "? " since that is almost always used.
;;
Karl Heuer's avatar
Karl Heuer committed
127 128 129 130 131 132 133
;; Adding your own checks:
;;
;;   You can experiment with adding your own checks by setting the
;; hooks `checkdoc-style-hooks' and `checkdoc-comment-style-hooks'.
;; Return a string which is the error you wish to report.  The cursor
;; position should be preserved.
;;
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
;; Error errors:
;;
;;   Checkdoc does not always flag errors correctly.  There are a
;; couple ways you can coax your file into passing all of checkdoc's
;; tests through buffer local variables.
;;
;;   The variable `checkdoc-verb-check-experimental-flag' can be used
;; to turn off the check for verb-voice in case you use words that are
;; not semantically verbs, but are still in the incomplete list.
;;
;;   The variable `checkdoc-symbol-words' can be a list of words that
;; happen to also be symbols.  This is not a problem for one-word
;; symbols, but if you use a hyphenated word that is also a symbol,
;; then you may need this.
;;
;;   The symbol `checkdoc-force-docstrings-flag' can be set to nil if
;; you have many undocumented functions you don't wish to document.
;;
;;   See the above section "Checking Parameters" for details about
;; parameter checking.
;;
;; Dependencies:
;;
;;   This file requires lisp-mnt (Lisp maintenance routines) for the
;;   comment checkers.
;;
;;   Requires custom for Emacs v20.
Karl Heuer's avatar
Karl Heuer committed
161 162

;;; TO DO:
163
;;   Hook into the byte compiler on a defun/defvar level to generate
Karl Heuer's avatar
Karl Heuer committed
164 165 166 167 168 169 170 171 172 173
;;     warnings in the byte-compiler's warning/error buffer.
;;   Better ways to override more typical `eval' functions.  Advice
;;     might be good but hard to turn on/off as a minor mode.
;;
;;; Maybe Do:
;;   Code sweep checks for "forbidden functions", proper use of hooks,
;;     proper keybindings, and other items from the manual that are
;;     not specifically docstring related.  Would this even be useful?

;;; Code:
174
(defvar checkdoc-version "0.6.1"
Karl Heuer's avatar
Karl Heuer committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188
  "Release version of checkdoc you are currently running.")

;; From custom web page for compatibility between versions of custom:
(eval-and-compile
  (condition-case ()
      (require 'custom)
    (error nil))
  (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
      nil ;; We've got what we needed
    ;; We have the old custom-library, hack around it!
    (defmacro defgroup (&rest args)
      nil)
    (defmacro custom-add-option (&rest args)
      nil)
189
    (defmacro defcustom (var value doc &rest args)
Karl Heuer's avatar
Karl Heuer committed
190 191 192
      (` (defvar (, var) (, value) (, doc))))))

(defcustom checkdoc-autofix-flag 'semiautomatic
Richard M. Stallman's avatar
Richard M. Stallman committed
193 194 195
  "*Non-nil means attempt auto-fixing of doc strings.
If this value is the symbol `query', then the user is queried before
any change is made.  If the value is `automatic', then all changes are
Karl Heuer's avatar
Karl Heuer committed
196
made without asking unless the change is very-complex.  If the value
197
is `semiautomatic' or any other value, then simple fixes are made
Karl Heuer's avatar
Karl Heuer committed
198
without asking, and complex changes are made by asking the user first.
Richard M. Stallman's avatar
Richard M. Stallman committed
199
The value `never' is the same as nil, never ask or change anything."
Karl Heuer's avatar
Karl Heuer committed
200 201 202
  :group 'checkdoc
  :type '(choice (const automatic)
		 (const query)
203 204
		 (const never)
		 (other :tag "semiautomatic" semiautomatic)))
Karl Heuer's avatar
Karl Heuer committed
205 206

(defcustom checkdoc-bouncy-flag t
Richard M. Stallman's avatar
Richard M. Stallman committed
207
  "*Non-nil means to \"bounce\" to auto-fix locations.
Karl Heuer's avatar
Karl Heuer committed
208 209 210 211 212 213 214 215
Setting this to nil will silently make fixes that require no user
interaction.  See `checkdoc-autofix-flag' for auto-fixing details."
  :group 'checkdoc
  :type 'boolean)

(defcustom checkdoc-force-docstrings-flag t
  "*Non-nil means that all checkable definitions should have documentation.
Style guide dictates that interactive functions MUST have documentation,
216
and that it's good but not required practice to make non user visible items
Richard M. Stallman's avatar
Richard M. Stallman committed
217
have doc strings."
Karl Heuer's avatar
Karl Heuer committed
218 219 220
  :group 'checkdoc
  :type 'boolean)

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
(defcustom checkdoc-force-history-flag t
  "*Non-nil means that files should have a History section or ChangeLog file.
This helps document the evolution of, and recent changes to, the package."
  :group 'checkdoc
  :type 'boolean)

(defcustom checkdoc-permit-comma-termination-flag nil
  "*Non-nil means the first line of a docstring may end with a comma.
Ordinarily, a full sentence is required.  This may be misleading when
there is a substantial caveat to the one-line description -- the comma
should be used when the first part could stand alone as a sentence, but
it indicates that a modifying clause follows."
  :group 'checkdoc
  :type 'boolean)

Karl Heuer's avatar
Karl Heuer committed
236
(defcustom checkdoc-spellcheck-documentation-flag nil
237
  "*Non-nil means run Ispell on text based on value.
Richard M. Stallman's avatar
Richard M. Stallman committed
238
This is automatically set to nil if Ispell does not exist on your
Karl Heuer's avatar
Karl Heuer committed
239 240
system.  Possible values are:

Richard M. Stallman's avatar
Richard M. Stallman committed
241 242
  nil         - Don't spell-check during basic style checks.
  defun       - Spell-check when style checking a single defun
243 244
  buffer      - Spell-check when style checking the whole buffer
  interactive - Spell-check during any interactive check.
Richard M. Stallman's avatar
Richard M. Stallman committed
245
  t           - Always spell-check"
Karl Heuer's avatar
Karl Heuer committed
246 247 248 249 250 251 252 253
  :group 'checkdoc
  :type '(choice (const nil)
		 (const defun)
		 (const buffer)
		 (const interactive)
		 (const t)))

(defvar checkdoc-ispell-lisp-words
254
  '("alist" "emacs" "etags" "iff" "keymap" "paren" "regexp" "sexp" "xemacs")
Richard M. Stallman's avatar
Richard M. Stallman committed
255
  "List of words that are correct when spell-checking Lisp documentation.")
Karl Heuer's avatar
Karl Heuer committed
256 257

(defcustom checkdoc-max-keyref-before-warn 10
Richard M. Stallman's avatar
Richard M. Stallman committed
258
  "*The number of \\ [command-to-keystroke] tokens allowed in a doc string.
Karl Heuer's avatar
Karl Heuer committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
Any more than this and a warning is generated suggesting that the construct
\\ {keymap} be used instead."
  :group 'checkdoc
  :type 'integer)

(defcustom checkdoc-arguments-in-order-flag t
  "*Non-nil means warn if arguments appear out of order.
Setting this to nil will mean only checking that all the arguments
appear in the proper form in the documentation, not that they are in
the same order as they appear in the argument list.  No mention is
made in the style guide relating to order."
  :group 'checkdoc
  :type 'boolean)

(defvar checkdoc-style-hooks nil
  "Hooks called after the standard style check is completed.
All hooks must return nil or a string representing the error found.
Useful for adding new user implemented commands.

Each hook is called with two parameters, (DEFUNINFO ENDPOINT).
DEFUNINFO is the return value of `checkdoc-defun-info'.  ENDPOINT is the
location of end of the documentation string.")

(defvar checkdoc-comment-style-hooks nil
  "Hooks called after the standard comment style check is completed.
Must return nil if no errors are found, or a string describing the
problem discovered.  This is useful for adding additional checks.")

(defvar checkdoc-diagnostic-buffer "*Style Warnings*"
288
  "Name of warning message buffer.")
Karl Heuer's avatar
Karl Heuer committed
289 290 291 292 293 294 295 296

(defvar checkdoc-defun-regexp
  "^(def\\(un\\|var\\|custom\\|macro\\|const\\|subst\\|advice\\)\
\\s-+\\(\\(\\sw\\|\\s_\\)+\\)[ \t\n]+"
  "Regular expression used to identify a defun.
A search leaves the cursor in front of the parameter list.")

(defcustom checkdoc-verb-check-experimental-flag t
Richard M. Stallman's avatar
Richard M. Stallman committed
297
  "*Non-nil means to attempt to check the voice of the doc string.
Karl Heuer's avatar
Karl Heuer committed
298
This check keys off some words which are commonly misused.  See the
Richard M. Stallman's avatar
Richard M. Stallman committed
299
variable `checkdoc-common-verbs-wrong-voice' if you wish to add your own."
Karl Heuer's avatar
Karl Heuer committed
300 301 302
  :group 'checkdoc
  :type 'boolean)

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
(defvar checkdoc-generate-compile-warnings-flag nil
  "Non-nil means generage warnings in a buffer for browsing.
Do not set this by hand, use a function like `checkdoc-current-buffer'
with a universal argument.")

(defcustom checkdoc-symbol-words nil
  "A list of symbols which also happen to make good words.
These symbol-words are ignored when unquoted symbols are searched for.
This should be set in an Emacs Lisp file's local variables."
  :group 'checkdoc
  :type '(repeat (symbol :tag "Word")))

(defvar checkdoc-proper-noun-list
  '("ispell" "xemacs" "emacs" "lisp")
  "List of words (not capitalized) which should be capitalized.")

(defvar checkdoc-proper-noun-regexp
  (let ((expr "\\<\\(")
	(l checkdoc-proper-noun-list))
    (while l
      (setq expr (concat expr (car l) (if (cdr l) "\\|" ""))
	    l (cdr l)))
    (concat expr "\\)\\>"))
  "Regular expression derived from `checkdoc-proper-noun-regexp'.")

Karl Heuer's avatar
Karl Heuer committed
328 329 330 331 332 333 334
(defvar checkdoc-common-verbs-regexp nil
  "Regular expression derived from `checkdoc-common-verbs-regexp'.")

(defvar checkdoc-common-verbs-wrong-voice
  '(("adds" . "add")
    ("allows" . "allow")
    ("appends" . "append")
Karl Heuer's avatar
Karl Heuer committed
335 336
    ("applies" . "apply")
    ("arranges" . "arrange")
Karl Heuer's avatar
Karl Heuer committed
337 338 339 340 341 342
    ("brings" . "bring")
    ("calls" . "call")
    ("catches" . "catch")
    ("changes" . "change")
    ("checks" . "check")
    ("contains" . "contain")
343
    ("converts" . "convert")
Karl Heuer's avatar
Karl Heuer committed
344 345 346 347
    ("creates" . "create")
    ("destroys" . "destroy")
    ("disables" . "disable")
    ("executes" . "execute")
Karl Heuer's avatar
Karl Heuer committed
348
    ("evals" . "evaluate")
Karl Heuer's avatar
Karl Heuer committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    ("evaluates" . "evaluate")
    ("finds" . "find")
    ("forces" . "force")
    ("gathers" . "gather")
    ("generates" . "generate")
    ("goes" . "go")
    ("guesses" . "guess")
    ("highlights" . "highlight")
    ("holds" . "hold")
    ("ignores" . "ignore")
    ("indents" . "indent")
    ("initializes" . "initialize")
    ("inserts" . "insert")
    ("installs" . "install")
    ("investigates" . "investigate")
    ("keeps" . "keep")
    ("kills" . "kill")
    ("leaves" . "leave")
    ("lets" . "let")
    ("loads" . "load")
    ("looks" . "look")
    ("makes" . "make")
    ("marks" . "mark")
    ("matches" . "match")
373
    ("moves" . "move")
Karl Heuer's avatar
Karl Heuer committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    ("notifies" . "notify")
    ("offers" . "offer")
    ("parses" . "parse")
    ("performs" . "perform")
    ("prepares" . "prepare")
    ("prepends" . "prepend")
    ("reads" . "read")
    ("raises" . "raise")
    ("removes" . "remove")
    ("replaces" . "replace")
    ("resets" . "reset")
    ("restores" . "restore")
    ("returns" . "return")
    ("runs" . "run")
    ("saves" . "save")
    ("says" . "say")
    ("searches" . "search")
    ("selects" . "select")
    ("sets" . "set")
    ("sex" . "s*x")
    ("shows" . "show")
    ("signifies" . "signify")
    ("sorts" . "sort")
    ("starts" . "start")
    ("stores" . "store")
    ("switches" . "switch")
    ("tells" . "tell")
    ("tests" . "test")
    ("toggles" . "toggle")
Karl Heuer's avatar
Karl Heuer committed
403
    ("tries" . "try")
Karl Heuer's avatar
Karl Heuer committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    ("turns" . "turn")
    ("undoes" . "undo")
    ("unloads" . "unload")
    ("unmarks" . "unmark")
    ("updates" . "update")
    ("uses" . "use")
    ("yanks" . "yank")
    )
  "Alist of common words in the wrong voice and what should be used instead.
Set `checkdoc-verb-check-experimental-flag' to nil to avoid this costly
and experimental check.  Do not modify this list without setting
the value of `checkdoc-common-verbs-regexp' to nil which cause it to
be re-created.")

(defvar checkdoc-syntax-table nil
  "Syntax table used by checkdoc in document strings.")

(if checkdoc-syntax-table
    nil
  (setq checkdoc-syntax-table (copy-syntax-table emacs-lisp-mode-syntax-table))
424
  ;; When dealing with syntax in doc strings, make sure that - are encompassed
Karl Heuer's avatar
Karl Heuer committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
  ;; in words so we can use cheap \\> to get the end of a symbol, not the
  ;; end of a word in a conglomerate.
  (modify-syntax-entry ?- "w" checkdoc-syntax-table)
  )
	

;;; Compatibility
;;
(if (string-match "X[Ee]macs" emacs-version)
    (progn
      (defalias 'checkdoc-make-overlay 'make-extent)
      (defalias 'checkdoc-overlay-put 'set-extent-property)
      (defalias 'checkdoc-delete-overlay 'delete-extent)
      (defalias 'checkdoc-overlay-start 'extent-start)
      (defalias 'checkdoc-overlay-end 'extent-end)
      (defalias 'checkdoc-mode-line-update 'redraw-modeline)
      (defalias 'checkdoc-call-eval-buffer 'eval-buffer)
      )
  (defalias 'checkdoc-make-overlay 'make-overlay)
  (defalias 'checkdoc-overlay-put 'overlay-put)
  (defalias 'checkdoc-delete-overlay 'delete-overlay)
  (defalias 'checkdoc-overlay-start 'overlay-start)
  (defalias 'checkdoc-overlay-end 'overlay-end)
  (defalias 'checkdoc-mode-line-update 'force-mode-line-update)
  (defalias 'checkdoc-call-eval-buffer 'eval-current-buffer)
  )

452 453 454 455 456 457 458
;; Emacs 20 has this handy function.
(if (not (fboundp 'princ-list))
    (defun princ-list (&rest args)
      "Call `princ' on ARGS."
      (mapcar 'princ args)))

;; Emacs 20s have MULE characters which don't equate to numbers.
Karl Heuer's avatar
Karl Heuer committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
(if (fboundp 'char=)
    (defalias 'checkdoc-char= 'char=)
  (defalias 'checkdoc-char= '=))

;; Emacs 19.28 and earlier don't have the handy 'add-to-list function
(if (fboundp 'add-to-list)

    (defalias 'checkdoc-add-to-list 'add-to-list)

  (defun checkdoc-add-to-list (list-var element)
    "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet."
    (if (not (member element (symbol-value list-var)))
	(set list-var (cons element (symbol-value list-var)))))
  )

Richard M. Stallman's avatar
Richard M. Stallman committed
474
;; To be safe in new Emacsen, we want to read events, not characters
Karl Heuer's avatar
Karl Heuer committed
475 476 477 478 479 480 481
(if (fboundp 'read-event)
    (defalias 'checkdoc-read-event 'read-event)
  (defalias 'checkdoc-read-event 'read-char))

;;; User level commands
;;
;;;###autoload
482 483 484 485
(defun checkdoc ()
  "Interactivly check the entire buffer for style errors.
The current status of the ckeck will be displayed in a buffer which
the users will view as each check is completed."
Karl Heuer's avatar
Karl Heuer committed
486
  (interactive)
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
  (let ((status (list "Checking..." "-" "-" "-"))
	(checkdoc-spellcheck-documentation-flag
	 (member checkdoc-spellcheck-documentation-flag
		 '(buffer interactive t)))
	;; if the user set autofix to never, then that breaks the
	;; obviously requested asking implied by using this function.
	;; Set it to paranoia level.
	(checkdoc-autofix-flag (if (or (not checkdoc-autofix-flag)
				       (eq checkdoc-autofix-flag 'never))
				   'query
				 checkdoc-autofix-flag))
	tmp)
    (checkdoc-display-status-buffer status)
    ;; check the comments
    (if (not buffer-file-name)
	(setcar status "Not checked")
      (if (checkdoc-file-comments-engine)
	  (setcar status "Errors")
	(setcar status "Ok")))
    (setcar (cdr status) "Checking...")
    (checkdoc-display-status-buffer status)
    ;; Check the documentation
    (setq tmp (checkdoc-interactive nil t))
    (if tmp
	(setcar (cdr status) (format "%d Errors" (length tmp)))
      (setcar (cdr status) "Ok"))
    (setcar (cdr (cdr status)) "Checking...")
    (checkdoc-display-status-buffer status)
    ;; Check the message text
    (if (setq tmp (checkdoc-message-interactive nil t))
	(setcar (cdr (cdr status)) (format "%d Errors" (length tmp)))
      (setcar (cdr (cdr status)) "Ok"))
    (setcar (cdr (cdr (cdr status))) "Checking...")
    (checkdoc-display-status-buffer status)
    ;; Rogue spacing
    (if (condition-case nil
	    (checkdoc-rogue-spaces nil t)
	  (error t))
	(setcar (cdr (cdr (cdr status))) "Errors")
      (setcar (cdr (cdr (cdr status))) "Ok"))
    (checkdoc-display-status-buffer status)))

(defun checkdoc-display-status-buffer (check)
  "Display and update the status buffer for the current checkdoc mode.
CHECK is a vector stating the current status of each test as an
element is the status of that level of teset."
533 534 535 536 537 538 539 540
  (let (temp-buffer-setup-hook)
    (with-output-to-temp-buffer " *Checkdoc Status*"
      (princ-list
       "Buffer comments and tags:  " (nth 0 check) "\n"
       "Documentation style:       " (nth 1 check) "\n"
       "Message/Query text style:  " (nth 2 check) "\n"
       "Unwanted Spaces:           " (nth 3 check)
       )))
541 542 543 544
  (shrink-window-if-larger-than-buffer
   (get-buffer-window " *Checkdoc Status*"))
  (message nil)
  (sit-for 0))
Karl Heuer's avatar
Karl Heuer committed
545 546

;;;###autoload
547 548 549 550 551 552 553 554
(defun checkdoc-interactive (&optional start-here showstatus)
  "Interactively check the current buffer for doc string errors.
Prefix argument START-HERE will start the checking from the current
point, otherwise the check starts at the beginning of the current
buffer.  Allows navigation forward and backwards through document
errors.  Does not check for comment or space warnings.
Optional argument SHOWSTATUS indicates that we should update the
checkdoc status window instead of the usual behavior."
Karl Heuer's avatar
Karl Heuer committed
555 556
  (interactive "P")
  (let ((checkdoc-spellcheck-documentation-flag
557 558 559
	 (member checkdoc-spellcheck-documentation-flag
		 '(interactive t))))
    (checkdoc-interactive-loop start-here showstatus 'checkdoc-next-error)))
Karl Heuer's avatar
Karl Heuer committed
560 561

;;;###autoload
562 563
(defun checkdoc-message-interactive (&optional start-here showstatus)
  "Interactively check the current buffer for message string errors.
Karl Heuer's avatar
Karl Heuer committed
564 565 566
Prefix argument START-HERE will start the checking from the current
point, otherwise the check starts at the beginning of the current
buffer.  Allows navigation forward and backwards through document
567 568 569
errors.  Does not check for comment or space warnings.
Optional argument SHOWSTATUS indicates that we should update the
checkdoc status window instead of the usual behavior."
Karl Heuer's avatar
Karl Heuer committed
570
  (interactive "P")
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
  (let ((checkdoc-spellcheck-documentation-flag
	 (member checkdoc-spellcheck-documentation-flag
		 '(interactive t))))
    (checkdoc-interactive-loop start-here showstatus
			       'checkdoc-next-message-error)))

(defun checkdoc-interactive-loop (start-here showstatus findfunc)
  "Interactivly loop over all errors that can be found by a given method.
Searching starts at START-HERE.  SHOWSTATUS expresses the verbosity
of the search, and wether ending the search will auto-exit this function.
FINDFUNC is a symbol representing a function that will position the
cursor, and return error message text to present the the user.  It is
assumed that the cursor will stop just before a major sexp, which will
be highlighted to present the user with feedback as to the offending
style."
Karl Heuer's avatar
Karl Heuer committed
586 587 588 589 590 591 592 593
  ;; Determine where to start the test
  (let* ((begin (prog1 (point)
		  (if (not start-here) (goto-char (point-min)))))
	 ;; Assign a flag to spellcheck flag
	 (checkdoc-spellcheck-documentation-flag
	  (member checkdoc-spellcheck-documentation-flag
		  '(buffer interactive t)))
	 ;; Fetch the error list
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
	 (err-list (list (funcall findfunc nil)))
	 (cdo nil)
	 (returnme nil)
	 c)
    (save-window-excursion
      (if (not (car err-list)) (setq err-list nil))
      ;; Include whatever function point is in for good measure.
      (beginning-of-defun)
      (while err-list
	(goto-char (cdr (car err-list)))
	;; The cursor should be just in front of the offending doc string
	(if (stringp (car (car err-list)))
	    (setq cdo (save-excursion (checkdoc-make-overlay
				       (point) (progn (forward-sexp 1)
						      (point)))))
	  (setq cdo (checkdoc-make-overlay
		     (checkdoc-error-start (car (car err-list)))
		     (checkdoc-error-end (car (car err-list))))))
Karl Heuer's avatar
Karl Heuer committed
612 613 614
	(unwind-protect
	    (progn
	      (checkdoc-overlay-put cdo 'face 'highlight)
Richard M. Stallman's avatar
Richard M. Stallman committed
615
	      ;; Make sure the whole doc string is visible if possible.
Karl Heuer's avatar
Karl Heuer committed
616
	      (sit-for 0)
617 618 619 620 621 622 623 624 625 626 627
	      (if (and (looking-at "\"")
		       (not (pos-visible-in-window-p
			     (save-excursion (forward-sexp 1) (point))
			     (selected-window))))
		  (let ((l (count-lines (point)
					(save-excursion
					  (forward-sexp 1) (point)))))
		    (if (> l (window-height))
			(recenter 1)
		      (recenter (/ (- (window-height) l) 2))))
		(recenter))
628 629 630 631 632 633 634 635 636
	      (message "%s (C-h,%se,n,p,q)" (checkdoc-error-text
					      (car (car err-list)))
		       (if (checkdoc-error-unfixable (car (car err-list)))
			   "" "f,"))
	      (save-excursion
		(goto-char (checkdoc-error-start (car (car err-list))))
		(if (not (pos-visible-in-window-p))
		    (recenter (- (window-height) 2)))
		(setq c (checkdoc-read-event)))1
Karl Heuer's avatar
Karl Heuer committed
637
	      (if (not (integerp c)) (setq c ??))
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
	      (cond
	       ;; Exit condition
	       ((checkdoc-char= c ?\C-g) (signal 'quit nil))
	       ;; Request an auto-fix
	       ((or (checkdoc-char= c ?y) (checkdoc-char= c ?f))
		(checkdoc-delete-overlay cdo)
		(setq cdo nil)
		(goto-char (cdr (car err-list)))
		;; `automatic-then-never' tells the autofix function
		;; to only allow one fix to be automatic.  The autofix
		;; function will than set the flag to 'never, allowing
		;; the checker to return a different error.
		(let ((checkdoc-autofix-flag 'automatic-then-never)
		      (fixed nil))
		  (funcall findfunc t)
		  (setq fixed (not (eq checkdoc-autofix-flag
				       'automatic-then-never)))
		  (if (not fixed)
		      (progn
			(message "A Fix was not available.")
			(sit-for 2))
		    (setq err-list (cdr err-list))))
		(beginning-of-defun)
		(let ((pe (car err-list))
		      (ne (funcall findfunc nil)))
		  (if ne
		      (setq err-list (cons ne err-list))
		    (cond ((not err-list)
			   (message "No More Stylistic Errors.")
			   (sit-for 2))
			  (t
			   (message
			    "No Additional style errors.  Continuing...")
			   (sit-for 2))))))
	       ;; Move to the next error (if available)
	       ((or (checkdoc-char= c ?n) (checkdoc-char= c ?\ ))
		(let ((ne (funcall findfunc nil)))
		  (if (not ne)
		      (if showstatus
			  (setq returnme err-list
				err-list nil)
			(if (not err-list)
			    (message "No More Stylistic Errors.")
			  (message "No Additional style errors.  Continuing..."))
			(sit-for 2))
		    (setq err-list (cons ne err-list)))))
	       ;; Go backwards in the list of errors
	       ((or (checkdoc-char= c ?p) (checkdoc-char= c ?\C-?))
		(if (/= (length err-list) 1)
		    (progn
		      (setq err-list (cdr err-list))
		      (goto-char (cdr (car err-list)))
		      (beginning-of-defun))
		  (message "No Previous Errors.")
		  (sit-for 2)))
	       ;; Edit the buffer recursively.
	       ((checkdoc-char= c ?e)
		(checkdoc-recursive-edit
		 (checkdoc-error-text (car (car err-list))))
		(checkdoc-delete-overlay cdo)
		(setq err-list (cdr err-list)) ;back up the error found.
		(beginning-of-defun)
		(let ((ne (funcall findfunc nil)))
		  (if (not ne)
		      (if showstatus
			  (setq returnme err-list
				err-list nil)
			(message "No More Stylistic Errors.")
			(sit-for 2))
		    (setq err-list (cons ne err-list)))))
	       ;; Quit checkdoc
	       ((checkdoc-char= c ?q)
		(setq returnme err-list
		      err-list nil
		      begin (point)))
	       ;; Goofy s tuff
	       (t
		(if (get-buffer-window "*Checkdoc Help*")
		    (progn
		      (delete-window (get-buffer-window "*Checkdoc Help*"))
		      (kill-buffer "*Checkdoc Help*"))
		  (with-output-to-temp-buffer "*Checkdoc Help*"
		    (princ-list
		     "Checkdoc Keyboard Summary:\n"
		     (if (checkdoc-error-unfixable (car (car err-list)))
			 ""
		       (concat
			"f, y    - auto Fix this warning without asking (if\
 available.)\n"
			"         Very complex operations will still query.\n")
		       )
		     "e      - Enter recursive Edit.  Press C-M-c to exit.\n"
		     "SPC, n - skip to the Next error.\n"
		     "DEL, p - skip to the Previous error.\n"
		     "q      - Quit checkdoc.\n"
		     "C-h    - Toggle this help buffer."))
		  (shrink-window-if-larger-than-buffer
		   (get-buffer-window "*Checkdoc Help*"))))))
	  (if cdo (checkdoc-delete-overlay cdo)))))
Karl Heuer's avatar
Karl Heuer committed
737
    (goto-char begin)
738 739 740
    (if (get-buffer "*Checkdoc Help*") (kill-buffer "*Checkdoc Help*"))
    (message "Checkdoc: Done.")
    returnme))
Karl Heuer's avatar
Karl Heuer committed
741

742
(defun checkdoc-next-error (enable-fix)
Karl Heuer's avatar
Karl Heuer committed
743
  "Find and return the next checkdoc error list, or nil.
744
Only documentation strings are checked.
Karl Heuer's avatar
Karl Heuer committed
745 746 747
Add error vector is of the form (WARNING . POSITION) where WARNING
is the warning text, and POSITION is the point in the buffer where the
error was found.  We can use points and not markers because we promise
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
not to edit the buffer before point without re-executing this check.
Argument ENABLE-FIX will enable auto-fixing while looking for the next
error.  This argument assumes that the cursor is already positioned to
perform the fix."
  (if enable-fix
      (checkdoc-this-string-valid)
    (let ((msg nil) (p (point))
	  (checkdoc-autofix-flag nil))
      (condition-case nil
	  (while (and (not msg) (checkdoc-next-docstring))
	    (message "Searching for doc string error...%d%%"
		     (/ (* 100 (point)) (point-max)))
	    (if (setq msg (checkdoc-this-string-valid))
		(setq msg (cons msg (point)))))
	;; Quit.. restore position,  Other errors, leave alone
	(quit (goto-char p)))
      msg)))

(defun checkdoc-next-message-error (enable-fix)
  "Find and return the next checkdoc mesasge related error list, or nil.
Only text for error and `y-or-n-p' strings are checked.  See
`checkdoc-next-error' for details on the return value.
Argument ENABLE-FIX turns on the auto-fix feature.  This argument
assumes that the cursor is already positioned to perform the fix."
  (if enable-fix
      (checkdoc-message-text-engine)
    (let ((msg nil) (p (point)) (type nil)
	  (checkdoc-autofix-flag nil))
      (condition-case nil
	  (while (and (not msg)
		      (setq type
			    (checkdoc-message-text-next-string (point-max))))
	    (message "Searching for message string error...%d%%"
		     (/ (* 100 (point)) (point-max)))
	    (if (setq msg (checkdoc-message-text-engine type))
		(setq msg (cons msg (point)))))
	;; Quit.. restore position,  Other errors, leave alone
	(quit (goto-char p)))
      msg)))

(defun checkdoc-recursive-edit (msg)
  "Enter recursive edit to permit a user to fix some error checkdoc has found.
MSG is the error that was found, which is displayed in a help buffer."
  (with-output-to-temp-buffer "*Checkdoc Help*"
    (princ-list
     "Error message:\n  " msg
     "\n\nEdit to fix this problem, and press C-M-c to continue."))
  (shrink-window-if-larger-than-buffer
   (get-buffer-window "*Checkdoc Help*"))
  (message "When you're done editing press C-M-c to continue.")
  (unwind-protect
      (recursive-edit)
    (if (get-buffer-window "*Checkdoc Help*")
	(progn
	  (delete-window (get-buffer-window "*Checkdoc Help*"))
	  (kill-buffer "*Checkdoc Help*")))))

;;;###autoload
(defun checkdoc-eval-current-buffer ()
  "Evaluate and check documentation for the current buffer.
Evaluation is done first because good documentation for something that
doesn't work is just not useful.  Comments, doc strings, and rogue
spacing are all verified."
  (interactive)
  (checkdoc-call-eval-buffer nil)
  (checkdoc-current-buffer t))
Karl Heuer's avatar
Karl Heuer committed
814 815

;;;###autoload
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
(defun checkdoc-current-buffer (&optional take-notes)
  "Check current buffer for document, comment, error style, and rogue spaces.
With a prefix argument (in Lisp, the argument TAKE-NOTES),
store all errors found in a warnings buffer,
otherwise stop after the first error."
  (interactive "P")
  (if (interactive-p) (message "Checking buffer for style..."))
  ;; Assign a flag to spellcheck flag
  (let ((checkdoc-spellcheck-documentation-flag
	 (memq checkdoc-spellcheck-documentation-flag '(buffer t)))
	(checkdoc-autofix-flag (if take-notes 'never
				 checkdoc-autofix-flag))
	(checkdoc-generate-compile-warnings-flag
	 (or take-notes checkdoc-generate-compile-warnings-flag)))
    (if take-notes
	(checkdoc-start-section "checkdoc-current-buffer"))
    ;; every test is responsible for returning the cursor.
    (or (and buffer-file-name ;; only check comments in a file
	     (checkdoc-comments))
	(checkdoc-start)
	(checkdoc-message-text)
	(checkdoc-rogue-spaces)
	(not (interactive-p))
	(if take-notes (checkdoc-show-diagnostics))
	(message "Checking buffer for style...Done."))))

;;;###autoload
(defun checkdoc-start (&optional take-notes)
  "Start scanning the current buffer for documentation string style errors.
Only documentation strings are checked.
Use `checkdoc-continue' to continue checking if an error cannot be fixed.
Karl Heuer's avatar
Karl Heuer committed
847 848 849 850 851
Prefix argument TAKE-NOTES means to collect all the warning messages into
a separate buffer."
  (interactive "P")
  (let ((p (point)))
    (goto-char (point-min))
852 853
    (if (and take-notes (interactive-p))
	(checkdoc-start-section "checkdoc-start"))
Karl Heuer's avatar
Karl Heuer committed
854 855 856 857 858 859 860
    (checkdoc-continue take-notes)
    ;; Go back since we can't be here without success above.
    (goto-char p)
    nil))

;;;###autoload
(defun checkdoc-continue (&optional take-notes)
861
  "Find the next doc string in the current buffer which has a style error.
Karl Heuer's avatar
Karl Heuer committed
862
Prefix argument TAKE-NOTES means to continue through the whole buffer and
863 864
save warnings in a separate buffer.  Second optional argument START-POINT
is the starting location.  If this is nil, `point-min' is used instead."
Karl Heuer's avatar
Karl Heuer committed
865 866 867 868 869
  (interactive "P")
  (let ((wrong nil) (msg nil) (errors nil)
	;; Assign a flag to spellcheck flag
	(checkdoc-spellcheck-documentation-flag
	 (member checkdoc-spellcheck-documentation-flag
870 871 872 873 874
		 '(buffer t)))
	(checkdoc-autofix-flag (if take-notes 'never
				 checkdoc-autofix-flag))
	(checkdoc-generate-compile-warnings-flag
	 (or take-notes checkdoc-generate-compile-warnings-flag)))
Karl Heuer's avatar
Karl Heuer committed
875 876 877 878
    (save-excursion
      ;; If we are taking notes, encompass the whole buffer, otherwise
      ;; the user is navigating down through the buffer.
      (while (and (not wrong) (checkdoc-next-docstring))
879
	;; OK, let's look at the doc string.
Eric M. Ludlam's avatar
Eric M. Ludlam committed
880
	(setq msg (checkdoc-this-string-valid))
881
	(if msg (setq wrong (point)))))
Karl Heuer's avatar
Karl Heuer committed
882 883 884
    (if wrong
	(progn
	  (goto-char wrong)
885 886 887 888 889
	  (if (not take-notes)
	      (error (checkdoc-error-text msg)))))
    (checkdoc-show-diagnostics)
    (if (interactive-p)
	(message "No style warnings."))))
Karl Heuer's avatar
Karl Heuer committed
890 891

(defun checkdoc-next-docstring ()
Richard M. Stallman's avatar
Richard M. Stallman committed
892 893
  "Move to the next doc string after point, and return t.
Return nil if there are no more doc strings."
Karl Heuer's avatar
Karl Heuer committed
894 895 896 897 898 899 900 901
  (if (not (re-search-forward checkdoc-defun-regexp nil t))
      nil
    ;; search drops us after the identifier.  The next sexp is either
    ;; the argument list or the value of the variable.  skip it.
    (forward-sexp 1)
    (skip-chars-forward " \n\t")
    t))

Gerd Moellmann's avatar
Gerd Moellmann committed
902
;;;###autoload
Karl Heuer's avatar
Karl Heuer committed
903
(defun checkdoc-comments (&optional take-notes)
Richard M. Stallman's avatar
Richard M. Stallman committed
904
  "Find missing comment sections in the current Emacs Lisp file.
Karl Heuer's avatar
Karl Heuer committed
905 906 907 908 909 910
Prefix argument TAKE-NOTES non-nil means to save warnings in a
separate buffer.  Otherwise print a message.  This returns the error
if there is one."
  (interactive "P")
  (if take-notes (checkdoc-start-section "checkdoc-comments"))
  (if (not buffer-file-name)
911
     (error "Can only check comments for a file buffer"))
Karl Heuer's avatar
Karl Heuer committed
912 913 914
  (let* ((checkdoc-spellcheck-documentation-flag
	  (member checkdoc-spellcheck-documentation-flag
		  '(buffer t)))
915 916 917 918 919 920
	 (checkdoc-autofix-flag (if take-notes 'never checkdoc-autofix-flag))
	 (e (checkdoc-file-comments-engine))
	(checkdoc-generate-compile-warnings-flag
	 (or take-notes checkdoc-generate-compile-warnings-flag)))
    (if e (error (checkdoc-error-text e)))
    (checkdoc-show-diagnostics)
Karl Heuer's avatar
Karl Heuer committed
921 922 923
    e))

;;;###autoload
924
(defun checkdoc-rogue-spaces (&optional take-notes interact)
Karl Heuer's avatar
Karl Heuer committed
925 926 927
  "Find extra spaces at the end of lines in the current file.
Prefix argument TAKE-NOTES non-nil means to save warnings in a
separate buffer.  Otherwise print a message.  This returns the error
928 929
if there is one.
Optional argument INTERACT permits more interactive fixing."
Karl Heuer's avatar
Karl Heuer committed
930 931
  (interactive "P")
  (if take-notes (checkdoc-start-section "checkdoc-rogue-spaces"))
932 933 934 935
  (let* ((checkdoc-autofix-flag (if take-notes 'never checkdoc-autofix-flag))
	 (e (checkdoc-rogue-space-check-engine nil nil interact))
	(checkdoc-generate-compile-warnings-flag
	 (or take-notes checkdoc-generate-compile-warnings-flag)))
Karl Heuer's avatar
Karl Heuer committed
936 937
    (if (not (interactive-p))
	e
938 939 940 941
      (if e
	  (message (checkdoc-error-text e))
	(checkdoc-show-diagnostics)
	(message "Space Check: done.")))))
Karl Heuer's avatar
Karl Heuer committed
942

943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
;;;###autoload
(defun checkdoc-message-text (&optional take-notes)
  "Scan the buffer for occurrences of the error function, and verify text.
Optional argument TAKE-NOTES causes all errors to be logged."
  (interactive "P")
  (if take-notes (checkdoc-start-section "checkdoc-message-text"))
  (let* ((p (point)) e
	 (checkdoc-autofix-flag (if take-notes 'never checkdoc-autofix-flag))
	 (checkdoc-generate-compile-warnings-flag
	  (or take-notes checkdoc-generate-compile-warnings-flag)))
    (setq e (checkdoc-message-text-search))
    (if (not (interactive-p))
	e
      (if e
	  (error (checkdoc-error-text e))
	(checkdoc-show-diagnostics)))
    (goto-char p))
  (if (interactive-p) (message "Checking interactive message text...done.")))
    
Karl Heuer's avatar
Karl Heuer committed
962 963
;;;###autoload
(defun checkdoc-eval-defun ()
Karl Heuer's avatar
Karl Heuer committed
964
  "Evaluate the current form with `eval-defun' and check its documentation.
Karl Heuer's avatar
Karl Heuer committed
965 966 967 968
Evaluation is done first so the form will be read before the
documentation is checked.  If there is a documentation error, then the display
of what was evaluated will be overwritten by the diagnostic message."
  (interactive)
969
  (call-interactively 'eval-defun)
Karl Heuer's avatar
Karl Heuer committed
970 971 972 973
  (checkdoc-defun))

;;;###autoload
(defun checkdoc-defun (&optional no-error)
Richard M. Stallman's avatar
Richard M. Stallman committed
974 975
  "Examine the doc string of the function or variable under point.
Call `error' if the doc string has problems.  If NO-ERROR is
Karl Heuer's avatar
Karl Heuer committed
976
non-nil, then do not call error, but call `message' instead.
Richard M. Stallman's avatar
Richard M. Stallman committed
977
If the doc string passes the test, then check the function for rogue white
Karl Heuer's avatar
Karl Heuer committed
978 979 980 981 982 983 984
space at the end of each line."
  (interactive)
  (save-excursion
    (beginning-of-defun)
    (if (not (looking-at checkdoc-defun-regexp))
	;; I found this more annoying than useful.
	;;(if (not no-error)
985
	;;    (message "Cannot check this sexp's doc string."))
Karl Heuer's avatar
Karl Heuer committed
986 987 988 989 990 991 992 993 994
	nil
      ;; search drops us after the identifier.  The next sexp is either
      ;; the argument list or the value of the variable.  skip it.
      (goto-char (match-end 0))
      (forward-sexp 1)
      (skip-chars-forward " \n\t")
      (let* ((checkdoc-spellcheck-documentation-flag
	      (member checkdoc-spellcheck-documentation-flag
		      '(defun t)))
995 996
	     (beg (save-excursion (beginning-of-defun) (point)))
	     (end (save-excursion (end-of-defun) (point)))
Karl Heuer's avatar
Karl Heuer committed
997
	     (msg (checkdoc-this-string-valid)))
998 999 1000
	(if msg (if no-error
		    (message (checkdoc-error-text msg))
		  (error (checkdoc-error-text msg)))
1001
	  (setq msg (checkdoc-message-text-search beg end))
1002 1003 1004
	  (if msg (if no-error
		      (message (checkdoc-error-text msg))
		    (error (checkdoc-error-text msg)))
1005
	    (setq msg (checkdoc-rogue-space-check-engine beg end))
1006 1007 1008
	    (if msg (if no-error
			(message (checkdoc-error-text msg))
		      (error (checkdoc-error-text msg))))))
1009
	(if (interactive-p) (message "Checkdoc: done."))))))
Karl Heuer's avatar
Karl Heuer committed
1010 1011 1012 1013

;;; Ispell interface for forcing a spell check
;;

1014 1015 1016 1017 1018 1019 1020 1021 1022
;;;###autoload
(defun checkdoc-ispell (&optional take-notes)
  "Check the style and spelling of everything interactively.
Calls `checkdoc' with spell-checking turned on.
Prefix argument TAKE-NOTES is the same as for `checkdoc'"
  (interactive)
  (let ((checkdoc-spellcheck-documentation-flag t))
    (call-interactively 'checkdoc nil current-prefix-arg)))

Karl Heuer's avatar
Karl Heuer committed
1023 1024
;;;###autoload
(defun checkdoc-ispell-current-buffer (&optional take-notes)
1025
  "Check the style and spelling of the current buffer.
Karl Heuer's avatar
Karl Heuer committed
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
Calls `checkdoc-current-buffer' with spell-checking turned on.
Prefix argument TAKE-NOTES is the same as for `checkdoc-current-buffer'"
  (interactive)
  (let ((checkdoc-spellcheck-documentation-flag t))
    (call-interactively 'checkdoc-current-buffer nil current-prefix-arg)))

;;;###autoload
(defun checkdoc-ispell-interactive (&optional take-notes)
  "Check the style and spelling of the current buffer interactively.
Calls `checkdoc-interactive' with spell-checking turned on.
1036
Prefix argument TAKE-NOTES is the same as for `checkdoc-interactive'"
Karl Heuer's avatar
Karl Heuer committed
1037 1038 1039 1040 1041
  (interactive)
  (let ((checkdoc-spellcheck-documentation-flag t))
    (call-interactively 'checkdoc-interactive nil current-prefix-arg)))

;;;###autoload
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
(defun checkdoc-ispell-message-interactive (&optional take-notes)
  "Check the style and spelling of message text interactively.
Calls `checkdoc-message-interactive' with spell-checking turned on.
Prefix argument TAKE-NOTES is the same as for `checkdoc-message-interactive'"
  (interactive)
  (let ((checkdoc-spellcheck-documentation-flag t))
    (call-interactively 'checkdoc-message-interactive nil current-prefix-arg)))

;;;###autoload
(defun checkdoc-ispell-message-text (&optional take-notes)
  "Check the style and spelling of message text interactively.
Calls `checkdoc-message-text' with spell-checking turned on.
Prefix argument TAKE-NOTES is the same as for `checkdoc-message-text'"
  (interactive)
  (let ((checkdoc-spellcheck-documentation-flag t))
    (call-interactively 'checkdoc-message-text nil current-prefix-arg)))

;;;###autoload
(defun checkdoc-ispell-start (&optional take-notes)
Karl Heuer's avatar
Karl Heuer committed
1061
  "Check the style and spelling of the current buffer.
1062 1063
Calls `checkdoc-start' with spell-checking turned on.
Prefix argument TAKE-NOTES is the same as for `checkdoc-start'"
Karl Heuer's avatar
Karl Heuer committed
1064 1065
  (interactive)
  (let ((checkdoc-spellcheck-documentation-flag t))
1066
    (call-interactively 'checkdoc-start nil current-prefix-arg)))
Karl Heuer's avatar
Karl Heuer committed
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087

;;;###autoload
(defun checkdoc-ispell-continue (&optional take-notes)
  "Check the style and spelling of the current buffer after point.
Calls `checkdoc-continue' with spell-checking turned on.
Prefix argument TAKE-NOTES is the same as for `checkdoc-continue'"
  (interactive)
  (let ((checkdoc-spellcheck-documentation-flag t))
    (call-interactively 'checkdoc-continue nil current-prefix-arg)))

;;;###autoload
(defun checkdoc-ispell-comments (&optional take-notes)
  "Check the style and spelling of the current buffer's comments.
Calls `checkdoc-comments' with spell-checking turned on.
Prefix argument TAKE-NOTES is the same as for `checkdoc-comments'"
  (interactive)
  (let ((checkdoc-spellcheck-documentation-flag t))
    (call-interactively 'checkdoc-comments nil current-prefix-arg)))

;;;###autoload
(defun checkdoc-ispell-defun (&optional take-notes)
Richard M. Stallman's avatar
Richard M. Stallman committed
1088
  "Check the style and spelling of the current defun with Ispell.
Karl Heuer's avatar
Karl Heuer committed
1089 1090 1091 1092 1093 1094
Calls `checkdoc-defun' with spell-checking turned on.
Prefix argument TAKE-NOTES is the same as for `checkdoc-defun'"
  (interactive)
  (let ((checkdoc-spellcheck-documentation-flag t))
    (call-interactively 'checkdoc-defun nil current-prefix-arg)))

1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
;;; Error Management
;;
;; Errors returned from checkdoc functions can have various
;; features and behaviors, so we need some ways of specifying
;; them, and making them easier to use in the wacked-out interfaces
;; people are requesting
(defun checkdoc-create-error (text start end &optional unfixable)
  "Used to create the return error text returned from all engines.
TEXT is the descriptive text of the error.  START and END define the region
it is sensible to highlight when describing the problem.
Optional argument UNFIXABLE means that the error has no auto-fix available.

A list of the form (TEXT START END UNFIXABLE) is returned if we are not
generating a buffered list of errors."
  (if checkdoc-generate-compile-warnings-flag
      (progn (checkdoc-error start text)
	     nil)
    (list text start end unfixable)))

(defun checkdoc-error-text (err)
  "Return the text specified in the checkdoc ERR."
  ;; string-p part is for backwards compatibility
  (if (stringp err) err (car err)))

(defun checkdoc-error-start (err)
  "Return the start point specified in the checkdoc ERR."
  ;; string-p part is for backwards compatibility
  (if (stringp err) nil (nth 1 err)))

(defun checkdoc-error-end (err)
  "Return the end point specified in the checkdoc ERR."
  ;; string-p part is for backwards compatibility
  (if (stringp err) nil (nth 2 err)))

(defun checkdoc-error-unfixable (err)
  "Return the t if we cannot autofix the error specified in the checkdoc ERR."
  ;; string-p part is for backwards compatibility
  (if (stringp err) nil (nth 3 err)))

Karl Heuer's avatar
Karl Heuer committed
1134 1135 1136
;;; Minor Mode specification
;;

1137
(defvar checkdoc-minor-mode-map
Karl Heuer's avatar
Karl Heuer committed
1138 1139 1140 1141
  (let ((map (make-sparse-keymap))
	(pmap (make-sparse-keymap)))
    ;; Override some bindings
    (define-key map "\C-\M-x" 'checkdoc-eval-defun)
1142
    (define-key map "\C-x`" 'checkdoc-continue)
Karl Heuer's avatar
Karl Heuer committed
1143 1144 1145
    (if (not (string-match "XEmacs" emacs-version))
	(define-key map [menu-bar emacs-lisp eval-buffer]
	  'checkdoc-eval-current-buffer))
1146
    ;; Add some new bindings under C-c ?
Karl Heuer's avatar
Karl Heuer committed
1147 1148 1149 1150
    (define-key pmap "x" 'checkdoc-defun)
    (define-key pmap "X" 'checkdoc-ispell-defun)
    (define-key pmap "`" 'checkdoc-continue)
    (define-key pmap "~" 'checkdoc-ispell-continue)
1151 1152
    (define-key pmap "s" 'checkdoc-start)
    (define-key pmap "S" 'checkdoc-ispell-start)
Karl Heuer's avatar
Karl Heuer committed
1153 1154 1155 1156 1157
    (define-key pmap "d" 'checkdoc)
    (define-key pmap "D" 'checkdoc-ispell)
    (define-key pmap "b" 'checkdoc-current-buffer)
    (define-key pmap "B" 'checkdoc-ispell-current-buffer)
    (define-key pmap "e" 'checkdoc-eval-current-buffer)
1158
    (define-key pmap "m" 'checkdoc-message-text)
1159
    (define-key pmap "M" 'checkdoc-ispell-message-text)
Karl Heuer's avatar
Karl Heuer committed
1160 1161 1162 1163 1164 1165 1166 1167 1168
    (define-key pmap "c" 'checkdoc-comments)
    (define-key pmap "C" 'checkdoc-ispell-comments)
    (define-key pmap " " 'checkdoc-rogue-spaces)

    ;; bind our submap into map
    (define-key map "\C-c?" pmap)
    map)
  "Keymap used to override evaluation key-bindings for documentation checking.")

1169 1170 1171
(defvar checkdoc-minor-keymap checkdoc-minor-mode-map
  "Obsolete!  Use `checkdoc-minor-mode-map'.")

Karl Heuer's avatar
Karl Heuer committed
1172 1173
;; Add in a menubar with easy-menu

1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
(easy-menu-define
 checkdoc-minor-menu checkdoc-minor-mode-map "Checkdoc Minor Mode Menu"
 '("CheckDoc"
   ["Interactive Buffer Style Check" checkdoc t]
   ["Interactive Buffer Style and Spelling Check" checkdoc-ispell t]
   ["Check Buffer" checkdoc-current-buffer t]
   ["Check and Spell Buffer" checkdoc-ispell-current-buffer t]
   "---"
   ["Interactive Style Check" checkdoc-interactive t]
   ["Interactive Style and Spelling Check" checkdoc-ispell-interactive t]
   ["Find First Style Error" checkdoc-start t]
   ["Find First Style or Spelling  Error" checkdoc-ispell-start t]
   ["Next Style Error" checkdoc-continue t]
   ["Next Style or Spelling  Error" checkdoc-ispell-continue t]
   ["Interactive Message Text Style Check" checkdoc-message-interactive t]
   ["Interactive Message Text Style and Spelling Check"
    checkdoc-ispell-message-interactive t]
   ["Check Message Text" checkdoc-message-text t]
   ["Check and Spell Message Text" checkdoc-ispell-message-text t]
   ["Check Comment Style" checkdoc-comments buffer-file-name]
   ["Check Comment Style and Spelling" checkdoc-ispell-comments
    buffer-file-name]
   ["Check for Rogue Spaces" checkdoc-rogue-spaces t]
   "---"
   ["Check Defun" checkdoc-defun t]
   ["Check and Spell Defun" checkdoc-ispell-defun t]
   ["Check and Evaluate Defun" checkdoc-eval-defun t]
   ["Check and Evaluate Buffer" checkdoc-eval-current-buffer t]
   ))
Karl Heuer's avatar
Karl Heuer committed
1203 1204 1205 1206
;; XEmacs requires some weird stuff to add this menu in a minor mode.
;; What is it?

;;;###autoload
1207
(easy-mmode-define-minor-mode checkdoc-minor-mode
Richard M. Stallman's avatar
Richard M. Stallman committed
1208 1209
  "Toggle Checkdoc minor mode, a mode for checking Lisp doc strings.
With prefix ARG, turn Checkdoc minor mode on iff ARG is positive.
Karl Heuer's avatar
Karl Heuer committed
1210

Richard M. Stallman's avatar
Richard M. Stallman committed
1211
In Checkdoc minor mode, the usual bindings for `eval-defun' which is
1212
bound to \\<checkdoc-minor-mode-map> \\[checkdoc-eval-defun] and `checkdoc-eval-current-buffer' are overridden to include
Karl Heuer's avatar
Karl Heuer committed
1213 1214
checking of documentation strings.

1215 1216
\\{checkdoc-minor-mode-map}"
  nil " CDoc" nil)
Karl Heuer's avatar
Karl Heuer committed
1217 1218 1219 1220 1221 1222 1223 1224

;;; Subst utils
;;
(defsubst checkdoc-run-hooks (hookvar &rest args)
  "Run hooks in HOOKVAR with ARGS."
  (if (fboundp 'run-hook-with-args-until-success)
      (apply 'run-hook-with-args-until-success hookvar args)
    ;; This method was similar to above.  We ignore the warning
Richard M. Stallman's avatar
Richard M. Stallman committed
1225
    ;; since we will use the above for future Emacs versions
Karl Heuer's avatar
Karl Heuer committed
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
    (apply 'run-hook-with-args hookvar args)))

(defsubst checkdoc-create-common-verbs-regexp ()
  "Rebuild the contents of `checkdoc-common-verbs-regexp'."
  (or checkdoc-common-verbs-regexp
      (setq checkdoc-common-verbs-regexp
	    (concat "\\<\\("
		    (mapconcat (lambda (e) (concat (car e)))
			       checkdoc-common-verbs-wrong-voice "\\|")
		    "\\)\\>"))))

;; Profiler says this is not yet faster than just calling assoc
;;(defun checkdoc-word-in-alist-vector (word vector)
;;  "Check to see if WORD is in the car of an element of VECTOR.
;;VECTOR must be sorted.  The CDR should be a replacement.  Since the
;;word list is getting bigger, it is time for a quick bisecting search."
;;  (let ((max (length vector)) (min 0) i
;;	(found nil) (fw nil))
;;    (setq i (/ max 2))
;;    (while (and (not found) (/= min max))
;;      (setq fw (car (aref vector i)))
;;      (cond ((string= word fw) (setq found (cdr (aref vector i))))
;;	    ((string< word fw) (setq max i))
;;	    (t (setq min i)))
;;      (setq i (/ (+ max min) 2))
;;      )
;;    found))

;;; Checking engines
;;
(defun checkdoc-this-string-valid ()
Richard M. Stallman's avatar
Richard M. Stallman committed
1257
  "Return a message string if the current doc string is invalid.
Karl Heuer's avatar
Karl Heuer committed
1258 1259 1260 1261 1262
Check for style only, such as the first line always being a complete
sentence, whitespace restrictions, and making sure there are no
hard-coded key-codes such as C-[char] or mouse-[number] in the comment.
See the style guide in the Emacs Lisp manual for more details."

Richard M. Stallman's avatar
Richard M. Stallman committed
1263
  ;; Jump over comments between the last object and the doc string
Karl Heuer's avatar
Karl Heuer committed
1264 1265 1266 1267 1268
  (while (looking-at "[ \t\n]*;")
    (forward-line 1)
    (beginning-of-line)
    (skip-chars-forward " \n\t"))

1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
  (let ((fp (checkdoc-defun-info))
	(err nil))
    (setq
     err
     ;; * Every command, function, or variable intended for users to know
     ;;   about should have a documentation string.
     ;;
     ;; * An internal variable or subroutine of a Lisp program might as well
     ;;   have a documentation string.  In earlier Emacs versions, you could
     ;;   save space by using a comment instead of a documentation string,
     ;;   but that is no longer the case.
     (if (and (not (nth 1 fp))		; not a variable
	      (or (nth 2 fp)		; is interactive
		  checkdoc-force-docstrings-flag) ;or we always complain
	      (not (checkdoc-char= (following-char) ?\"))) ; no doc string
	 ;; Sometimes old code has comments where the documentation should
1285
	 ;; be.  Let's see if we can find the comment, and offer to turn it
1286
	 ;; into documentation for them.
1287 1288
	 (let ((have-comment nil)
	       (comment-start ";"))	; in case it's not default
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
	   (condition-case nil
	       (progn
		 (forward-sexp -1)
		 (forward-sexp 1)
		 (skip-chars-forward "\n \t")
		 (setq have-comment (looking-at comment-start)))
	     (error nil))
	   (if have-comment
	       (if (or (eq checkdoc-autofix-flag
			   'automatic-then-never)
		       (checkdoc-y-or-n-p
			"Convert comment to documentation? "))
		   (save-excursion
		     ;; Our point is at the beginning of the comment!
		     ;; Insert a quote, then remove the comment chars.
		     (insert "\"")
1305
		     (let ((docstring-start-point (point)))
1306
		       (while (looking-at comment-start)
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
			 (while (looking-at comment-start)
			   (delete-char 1))
			 (if (looking-at "[ \t]+")
			     (delete-region (match-beginning 0) (match-end 0)))
			 (forward-line 1)
			 (beginning-of-line)
			 (skip-chars-forward " \t")
			 (if (looking-at comment-start)
			     (progn
			       (beginning-of-line)
			       (zap-to-char 1 ?\;))))
1318
		       (beginning-of-line)
1319 1320 1321 1322 1323 1324 1325 1326
		       (forward-char -1)
		       (insert "\"")
		       (forward-char -1)
		       ;; quote any double-quote characters in the comment.
		       (while (search-backward "\"" docstring-start-point t)
			 (insert "\\"))
		       (if (eq checkdoc-autofix-flag 'automatic-then-never)
			   (setq checkdoc-autofix-flag 'never))))
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
		 (checkdoc-create-error
		  "You should convert this comment to documentation"
		  (point) (save-excursion (end-of-line) (point))))
	     (checkdoc-create-error
	      (if (nth 2 fp)
		  "All interactive functions should have documentation"
		"All variables and subroutines might as well have a \
documentation string")
	      (point) (+ (point) 1) t)))))
    (if (and (not err) (looking-at "\""))
	(let ((old-syntax-table (syntax-table)))
	  (unwind-protect
	      (progn
		(set-syntax-table checkdoc-syntax-table)
		(checkdoc-this-string-valid-engine fp))
	    (set-syntax-table old-syntax-table)))
      err)))

(defun checkdoc-this-string-valid-engine (fp)
  "Return an error list or string if the current doc string is invalid.
Karl Heuer's avatar
Karl Heuer committed
1347
Depends on `checkdoc-this-string-valid' to reset the syntax table so that
1348
regexp short cuts work.  FP is the function defun information."
Karl Heuer's avatar
Karl Heuer committed
1349 1350 1351
  (let ((case-fold-search nil)
	;; Use a marker so if an early check modifies the text,
	;; we won't accidentally loose our place.  This could cause
Richard M. Stallman's avatar
Richard M. Stallman committed
1352
	;; end-of doc string whitespace to also delete the " char.
1353 1354 1355 1356
	(s (point))
	(e (if (looking-at "\"")
	       (save-excursion (forward-sexp 1) (point-marker))
	     (point))))
Karl Heuer's avatar
Karl Heuer committed
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
    (or
     ;; * *Do not* indent subsequent lines of a documentation string so that
     ;;   the text is lined up in the source code with the text of the first
     ;;   line.  This looks nice in the source code, but looks bizarre when
     ;;   users view the documentation.  Remember that the indentation
     ;;   before the starting double-quote is not part of the string!
     (save-excursion
       (forward-line 1)
       (beginning-of-line)
       (if (and (< (point) e)
		(looking-at "\\([ \t]+\\)[^ \t\n]"))
	   (if (checkdoc-autofix-ask-replace (match-beginning 1)
					     (match-end 1)
1370
					     "Remove this whitespace? "
Karl Heuer's avatar
Karl Heuer committed
1371 1372
					     "")
	       nil
1373 1374 1375 1376
	     (checkdoc-create-error
	      "Second line should not have indentation"
	      (match-beginning 1)
	      (match-end 1)))))
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
     ;; * Check for '(' in column 0.
     (save-excursion
       (when (re-search-forward "^(" e t)
	 (if (checkdoc-autofix-ask-replace (match-beginning 0)
					   (match-end 0)
					   "Escape this '('? "
					   "\\(")
	     nil
	   (checkdoc-create-error
	    "Open parenthesis in column 0 should be escaped"
	    (match-beginning 0) (match-end 0)))))
Karl Heuer's avatar
Karl Heuer committed
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
     ;; * Do not start or end a documentation string with whitespace.
     (let (start end)
       (if (or (if (looking-at "\"\\([ \t\n]+\\)")
		   (setq start (match-beginning 1)
			 end (match-end 1)))
	       (save-excursion
		 (forward-sexp 1)
		 (forward-char -1)
		 (if (/= (skip-chars-backward " \t\n") 0)
		     (setq start (point)
			   end (1- e)))))
	   (if (checkdoc-autofix-ask-replace
1400
		start end "Remove this whitespace? " "")
Karl Heuer's avatar
Karl Heuer committed