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

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 1997-1998, 2001-2015 Free Software Foundation, Inc.
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

11
;; GNU Emacs is free software: you can redistribute it and/or modify
Karl Heuer's avatar
Karl Heuer committed
12
;; it under the terms of the GNU General Public License as published by
13 14
;; the Free Software Foundation, either version 3 of the License, 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
;; You should have received a copy of the GNU General Public License
22
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Karl Heuer's avatar
Karl Heuer committed
23 24 25

;;; Commentary:
;;
Richard M. Stallman's avatar
Richard M. Stallman committed
26
;;   The Emacs Lisp manual has a nice chapter on how to write
Karl Heuer's avatar
Karl Heuer committed
27 28 29 30 31 32
;; 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:
33 34 35
;;   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
36
;;   2) Use `checkdoc-minor-mode' to automatically check your
Richard M. Stallman's avatar
Richard M. Stallman committed
37
;;      documentation whenever you evaluate Lisp code with C-M-x
Karl Heuer's avatar
Karl Heuer committed
38 39 40
;;      or [menu-bar emacs-lisp eval-buffer].  Additional key-bindings
;;      are also provided under C-c ? KEY
;;        (require 'checkdoc)
41
;;        (add-hook 'emacs-lisp-mode-hook 'checkdoc-minor-mode)
Karl Heuer's avatar
Karl Heuer committed
42
;;
43 44 45 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
;; 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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
;; 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.
;;
86
;; Spell checking text:
Karl Heuer's avatar
Karl Heuer committed
87 88 89 90
;;
;;   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
91
;; checking done.  The default is `defun', which spell checks each time
Karl Heuer's avatar
Karl Heuer committed
92 93 94 95 96 97
;; `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
98 99
;;   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
100 101 102
;; running.  Use `ispell-kill-ispell' to make checkdoc restart it with
;; these words enabled.
;;
103
;; Checking parameters:
104
;;
Karl Heuer's avatar
Karl Heuer committed
105
;;   You might not always want a function to have its parameters listed
106 107 108 109 110 111 112 113 114 115
;; 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)"
;;
116
;; Checking message strings:
117
;;
118
;;   The text that follows the `error' and `y-or-n-p' commands is
119 120 121 122 123
;; 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
124 125 126
;; Adding your own checks:
;;
;;   You can experiment with adding your own checks by setting the
127
;; hooks `checkdoc-style-functions' and `checkdoc-comment-style-functions'.
Karl Heuer's avatar
Karl Heuer committed
128 129 130
;; Return a string which is the error you wish to report.  The cursor
;; position should be preserved.
;;
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
;; 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
158 159

;;; TO DO:
160
;;   Hook into the byte compiler on a defun/defvar level to generate
Karl Heuer's avatar
Karl Heuer committed
161 162 163 164 165 166 167 168 169 170
;;     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:
171
(defvar checkdoc-version "0.6.1"
Karl Heuer's avatar
Karl Heuer committed
172 173
  "Release version of checkdoc you are currently running.")

174 175 176
(require 'help-mode) ;; for help-xref-info-regexp
(require 'thingatpt) ;; for handy thing-at-point-looking-at

177 178 179
(defvar compilation-error-regexp-alist)
(defvar compilation-mode-font-lock-keywords)

Stefan Monnier's avatar
Stefan Monnier committed
180 181 182 183 184 185
(defgroup checkdoc nil
  "Support for doc string checking in Emacs Lisp."
  :prefix "checkdoc"
  :group 'lisp
  :version "20.3")

186
(defcustom checkdoc-minor-mode-string " CDoc"
187
  "String to display in mode line when Checkdoc mode is enabled; nil for none."
188 189 190
  :type '(choice string (const :tag "None" nil))
  :version "23.1")

Karl Heuer's avatar
Karl Heuer committed
191
(defcustom checkdoc-autofix-flag 'semiautomatic
Stefan Monnier's avatar
Stefan Monnier committed
192
  "Non-nil means attempt auto-fixing of doc strings.
Richard M. Stallman's avatar
Richard M. Stallman committed
193 194
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
195
made without asking unless the change is very-complex.  If the value
196
is `semiautomatic' or any other value, then simple fixes are made
Karl Heuer's avatar
Karl Heuer committed
197
without asking, and complex changes are made by asking the user first.
Richard M. Stallman's avatar
Richard M. Stallman committed
198
The value `never' is the same as nil, never ask or change anything."
Karl Heuer's avatar
Karl Heuer committed
199
  :type '(choice (const automatic)
200 201 202
          (const query)
          (const never)
          (other :tag "semiautomatic" semiautomatic)))
Karl Heuer's avatar
Karl Heuer committed
203 204

(defcustom checkdoc-bouncy-flag t
Stefan Monnier's avatar
Stefan Monnier committed
205
  "Non-nil means to \"bounce\" to auto-fix locations.
Karl Heuer's avatar
Karl Heuer committed
206 207 208 209 210
Setting this to nil will silently make fixes that require no user
interaction.  See `checkdoc-autofix-flag' for auto-fixing details."
  :type 'boolean)

(defcustom checkdoc-force-docstrings-flag t
Stefan Monnier's avatar
Stefan Monnier committed
211
  "Non-nil means that all checkable definitions should have documentation.
Karl Heuer's avatar
Karl Heuer committed
212
Style guide dictates that interactive functions MUST have documentation,
213
and that it's good but not required practice to make non user visible items
Richard M. Stallman's avatar
Richard M. Stallman committed
214
have doc strings."
Karl Heuer's avatar
Karl Heuer committed
215
  :type 'boolean)
216
;;;###autoload(put 'checkdoc-force-docstrings-flag 'safe-local-variable #'booleanp)
Karl Heuer's avatar
Karl Heuer committed
217

218
(defcustom checkdoc-force-history-flag nil
Stefan Monnier's avatar
Stefan Monnier committed
219
  "Non-nil means that files should have a History section or ChangeLog file.
220 221
This helps document the evolution of, and recent changes to, the package."
  :type 'boolean)
222
;;;###autoload(put 'checkdoc-force-history-flag 'safe-local-variable #'booleanp)
223 224

(defcustom checkdoc-permit-comma-termination-flag nil
Stefan Monnier's avatar
Stefan Monnier committed
225
  "Non-nil means the first line of a docstring may end with a comma.
226 227 228 229 230
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."
  :type 'boolean)
231
;;;###autoload(put 'checkdoc-permit-comma-termination-flag 'safe-local-variable #'booleanp)
232

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

Richard M. Stallman's avatar
Richard M. Stallman committed
238 239
  nil         - Don't spell-check during basic style checks.
  defun       - Spell-check when style checking a single defun
240 241
  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
242
  t           - Always spell-check"
Karl Heuer's avatar
Karl Heuer committed
243
  :type '(choice (const nil)
244 245 246 247
          (const defun)
          (const buffer)
          (const interactive)
          (const t)))
248
;;;###autoload(put 'checkdoc-spellcheck-documentation-flag 'safe-local-variable #'booleanp)
Karl Heuer's avatar
Karl Heuer committed
249 250

(defvar checkdoc-ispell-lisp-words
251
  '("alist" "emacs" "etags" "keymap" "paren" "regexp" "sexp" "xemacs")
Richard M. Stallman's avatar
Richard M. Stallman committed
252
  "List of words that are correct when spell-checking Lisp documentation.")
253
;;;###autoload(put 'checkdoc-ispell-list-words 'safe-local-variable #'checkdoc-list-of-strings-p)
Karl Heuer's avatar
Karl Heuer committed
254 255

(defcustom checkdoc-max-keyref-before-warn 10
Stefan Monnier's avatar
Stefan Monnier committed
256
  "The number of \\ [command-to-keystroke] tokens allowed in a doc string.
Karl Heuer's avatar
Karl Heuer committed
257 258 259 260 261
Any more than this and a warning is generated suggesting that the construct
\\ {keymap} be used instead."
  :type 'integer)

(defcustom checkdoc-arguments-in-order-flag t
Stefan Monnier's avatar
Stefan Monnier committed
262
  "Non-nil means warn if arguments appear out of order.
Karl Heuer's avatar
Karl Heuer committed
263 264 265 266 267
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."
  :type 'boolean)
268
;;;###autoload(put 'checkdoc-arguments-in-order-flag 'safe-local-variable #'booleanp)
Karl Heuer's avatar
Karl Heuer committed
269

Stefan Monnier's avatar
Stefan Monnier committed
270 271 272 273 274
(define-obsolete-variable-alias 'checkdoc-style-hooks
  'checkdoc-style-functions "24.3")
(defvar checkdoc-style-functions nil
  "Hook run after the standard style check is completed.
All functions must return nil or a string representing the error found.
Karl Heuer's avatar
Karl Heuer committed
275 276 277 278 279 280
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.")

Stefan Monnier's avatar
Stefan Monnier committed
281
(define-obsolete-variable-alias 'checkdoc-comment-style-hooks
282
  'checkdoc-comment-style-functions "24.3")
Stefan Monnier's avatar
Stefan Monnier committed
283 284
(defvar checkdoc-comment-style-functions nil
  "Hook run after the standard comment style check is completed.
Karl Heuer's avatar
Karl Heuer committed
285 286 287 288
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*"
289
  "Name of warning message buffer.")
Karl Heuer's avatar
Karl Heuer committed
290 291 292 293 294 295 296 297

(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
Stefan Monnier's avatar
Stefan Monnier committed
298
  "Non-nil means to attempt to check the voice of the doc string.
Karl Heuer's avatar
Karl Heuer committed
299
This check keys off some words which are commonly misused.  See the
Richard M. Stallman's avatar
Richard M. Stallman committed
300
variable `checkdoc-common-verbs-wrong-voice' if you wish to add your own."
Karl Heuer's avatar
Karl Heuer committed
301
  :type 'boolean)
302
;;;###autoload(put 'checkdoc-verb-check-experimental-flag 'safe-local-variable #'booleanp)
Karl Heuer's avatar
Karl Heuer committed
303

304
(defvar checkdoc-generate-compile-warnings-flag nil
Richard M. Stallman's avatar
Richard M. Stallman committed
305
  "Non-nil means generate warnings in a buffer for browsing.
306 307 308 309
Do not set this by hand, use a function like `checkdoc-current-buffer'
with a universal argument.")

(defcustom checkdoc-symbol-words nil
310 311
  "A list of symbol names (strings) which also happen to make good words.
These words are ignored when unquoted symbols are searched for.
312 313
This should be set in an Emacs Lisp file's local variables."
  :type '(repeat (symbol :tag "Word")))
314
;;;###autoload(put 'checkdoc-symbol-words 'safe-local-variable #'checkdoc-list-of-strings-p)
315 316 317 318 319 320

;;;###autoload
(defun checkdoc-list-of-strings-p (obj)
  ;; this is a function so it might be shared by checkdoc-proper-noun-list
  ;; and/or checkdoc-ispell-lisp-words in the future
  (and (listp obj)
321
       (not (memq nil (mapcar #'stringp obj)))))
322 323 324 325 326 327

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

(defvar checkdoc-proper-noun-regexp
328 329 330
  ;; "[.!?]" is for noun at end of a sentence, since those chars
  ;; are symbol syntax in emacs-lisp-mode and so don't match \\_>.
  ;; The \" allows it to be the last sentence in a docstring too.
331 332 333
  (concat "\\_<"
	  (regexp-opt checkdoc-proper-noun-list t)
	  "\\(\\_>\\|[.!?][ \t\n\"]\\)")
334
  "Regular expression derived from `checkdoc-proper-noun-regexp'.")
335
;;;###autoload(put 'checkdoc-proper-noun-regexp 'safe-local-variable 'stringp)
336

Karl Heuer's avatar
Karl Heuer committed
337 338
(defvar checkdoc-common-verbs-regexp nil
  "Regular expression derived from `checkdoc-common-verbs-regexp'.")
339
;;;###autoload(put 'checkdoc-common-verbs-regexp 'safe-local-variable 'stringp)
Karl Heuer's avatar
Karl Heuer committed
340 341 342 343 344

(defvar checkdoc-common-verbs-wrong-voice
  '(("adds" . "add")
    ("allows" . "allow")
    ("appends" . "append")
Karl Heuer's avatar
Karl Heuer committed
345 346
    ("applies" . "apply")
    ("arranges" . "arrange")
Karl Heuer's avatar
Karl Heuer committed
347 348 349 350 351 352
    ("brings" . "bring")
    ("calls" . "call")
    ("catches" . "catch")
    ("changes" . "change")
    ("checks" . "check")
    ("contains" . "contain")
353
    ("converts" . "convert")
Karl Heuer's avatar
Karl Heuer committed
354 355 356 357
    ("creates" . "create")
    ("destroys" . "destroy")
    ("disables" . "disable")
    ("executes" . "execute")
Karl Heuer's avatar
Karl Heuer committed
358
    ("evals" . "evaluate")
Karl Heuer's avatar
Karl Heuer committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    ("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")
383
    ("moves" . "move")
Karl Heuer's avatar
Karl Heuer committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
    ("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
413
    ("tries" . "try")
Karl Heuer's avatar
Karl Heuer committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427
    ("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.")

428 429 430 431 432
(defvar checkdoc-syntax-table
  (let ((st (make-syntax-table emacs-lisp-mode-syntax-table)))
    ;; When dealing with syntax in doc strings, make sure that - are
    ;; encompassed in words so we can use cheap \\> to get the end of a symbol,
    ;; not the end of a word in a conglomerate.
433
    (modify-syntax-entry ?- "w" st)
434
    st)
Karl Heuer's avatar
Karl Heuer committed
435 436 437 438
  "Syntax table used by checkdoc in document strings.")

;;; Compatibility
;;
439
(defalias 'checkdoc-make-overlay
440
  (if (featurep 'xemacs) #'make-extent #'make-overlay))
441
(defalias 'checkdoc-overlay-put
442
  (if (featurep 'xemacs) #'set-extent-property #'overlay-put))
443
(defalias 'checkdoc-delete-overlay
444
  (if (featurep 'xemacs) #'delete-extent #'delete-overlay))
445
(defalias 'checkdoc-overlay-start
446
  (if (featurep 'xemacs) #'extent-start #'overlay-start))
447
(defalias 'checkdoc-overlay-end
448
  (if (featurep 'xemacs) #'extent-end #'overlay-end))
449
(defalias 'checkdoc-mode-line-update
450
  (if (featurep 'xemacs) #'redraw-modeline #'force-mode-line-update))
451
(defalias 'checkdoc-char=
452
  (if (featurep 'xemacs) #'char= #'=))
Karl Heuer's avatar
Karl Heuer committed
453 454 455 456

;;; User level commands
;;
;;;###autoload
457
(defun checkdoc ()
Richard M. Stallman's avatar
Richard M. Stallman committed
458 459
  "Interactively check the entire buffer for style errors.
The current status of the check will be displayed in a buffer which
460
the users will view as each check is completed."
Karl Heuer's avatar
Karl Heuer committed
461
  (interactive)
462 463
  (let ((status (list "Checking..." "-" "-" "-"))
	(checkdoc-spellcheck-documentation-flag
464 465
	 (car (memq checkdoc-spellcheck-documentation-flag
                    '(buffer interactive t))))
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
	;; 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.
506 507
CHECK is a list of four strings stating the current status of each
test; the nth string describes the status of the nth test."
508
  (let (temp-buffer-setup-hook)
509
    (with-output-to-temp-buffer "*Checkdoc Status*"
510 511 512 513 514
      (mapc #'princ
            (list "Buffer comments and tags:  " (nth 0 check)
                  "\nDocumentation style:       " (nth 1 check)
                  "\nMessage/Query text style:  " (nth 2 check)
                  "\nUnwanted Spaces:           " (nth 3 check)))))
515
  (shrink-window-if-larger-than-buffer
516
   (get-buffer-window "*Checkdoc Status*"))
517 518
  (message nil)
  (sit-for 0))
Karl Heuer's avatar
Karl Heuer committed
519 520

;;;###autoload
521 522 523 524 525 526 527 528
(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
529 530
  (interactive "P")
  (let ((checkdoc-spellcheck-documentation-flag
531 532
	 (car (memq checkdoc-spellcheck-documentation-flag
                    '(interactive t)))))
533 534 535 536
    (prog1
        ;; Due to a design flaw, this will never spell check
        ;; docstrings.
        (checkdoc-interactive-loop start-here showstatus
537
                                   #'checkdoc-next-error)
538 539
      ;; This is a workaround to perform spell checking.
      (checkdoc-interactive-ispell-loop start-here))))
Karl Heuer's avatar
Karl Heuer committed
540 541

;;;###autoload
542 543
(defun checkdoc-message-interactive (&optional start-here showstatus)
  "Interactively check the current buffer for message string errors.
Karl Heuer's avatar
Karl Heuer committed
544 545 546
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
547 548 549
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
550
  (interactive "P")
551
  (let ((checkdoc-spellcheck-documentation-flag
552 553
	 (car (memq checkdoc-spellcheck-documentation-flag
                    '(interactive t)))))
554 555 556
    (prog1
        ;; Due to a design flaw, this will never spell check messages.
        (checkdoc-interactive-loop start-here showstatus
557
                                   #'checkdoc-next-message-error)
558 559
      ;; This is a workaround to perform spell checking.
      (checkdoc-message-interactive-ispell-loop start-here))))
560 561

(defun checkdoc-interactive-loop (start-here showstatus findfunc)
Richard M. Stallman's avatar
Richard M. Stallman committed
562
  "Interactively loop over all errors that can be found by a given method.
563 564 565 566 567 568

If START-HERE is nil, searching starts at the beginning of the current
buffer, otherwise searching starts at START-HERE.  SHOWSTATUS
expresses the verbosity of the search, and whether ending the search
will auto-exit this function.

569
FINDFUNC is a symbol representing a function that will position the
Pavel Janík's avatar
Pavel Janík committed
570
cursor, and return error message text to present to the user.  It is
571 572 573
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
574 575 576 577 578
  ;; 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
579 580
	  (car (memq checkdoc-spellcheck-documentation-flag
                     '(buffer interactive t))))
Karl Heuer's avatar
Karl Heuer committed
581
	 ;; Fetch the error list
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
	 (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
600 601 602
	(unwind-protect
	    (progn
	      (checkdoc-overlay-put cdo 'face 'highlight)
Richard M. Stallman's avatar
Richard M. Stallman committed
603
	      ;; Make sure the whole doc string is visible if possible.
Karl Heuer's avatar
Karl Heuer committed
604
	      (sit-for 0)
605 606 607 608 609 610 611 612 613 614 615
	      (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))
616
	      (message "%s (C-h,%se,n,p,q)" (checkdoc-error-text
617
                                             (car (car err-list)))
618 619 620 621 622 623
		       (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)))
624
		(setq c (read-event)))
Karl Heuer's avatar
Karl Heuer committed
625
	      (if (not (integerp c)) (setq c ??))
626 627 628 629 630 631 632 633 634 635
	      (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
636
		;; function will then set the flag to `never', allowing
637 638 639 640 641 642 643 644 645 646 647 648
		;; 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)
649
		(let ((ne (funcall findfunc nil)))
650 651 652 653 654 655 656 657 658 659
		  (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)
660
	       ((or (checkdoc-char= c ?n) (checkdoc-char= c ?\s))
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
		(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)))
700
	       ;; Goofy stuff
701 702 703 704 705 706
	       (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*"
707 708 709 710 711 712 713
                    (with-current-buffer standard-output
                      (insert
                       "Checkdoc Keyboard Summary:\n"
                       (if (checkdoc-error-unfixable (car (car err-list)))
                           ""
                         (concat
                          "f, y    - auto Fix this warning without asking (if\
714
 available.)\n"
715 716 717 718 719 720 721
                          "         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.")))
722 723 724
		  (shrink-window-if-larger-than-buffer
		   (get-buffer-window "*Checkdoc Help*"))))))
	  (if cdo (checkdoc-delete-overlay cdo)))))
Karl Heuer's avatar
Karl Heuer committed
725
    (goto-char begin)
726 727 728
    (if (get-buffer "*Checkdoc Help*") (kill-buffer "*Checkdoc Help*"))
    (message "Checkdoc: Done.")
    returnme))
Karl Heuer's avatar
Karl Heuer committed
729

730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
(defun checkdoc-interactive-ispell-loop (start-here)
  "Interactively spell check doc strings in the current buffer.
If START-HERE is nil, searching starts at the beginning of the current
buffer, otherwise searching starts at START-HERE."
  (when checkdoc-spellcheck-documentation-flag
    (save-excursion
      ;; Move point to where we need to start.
      (if start-here
          ;; Include whatever function point is in for good measure.
          (beginning-of-defun)
        (goto-char (point-min)))
      ;; Loop over docstrings.
      (while (checkdoc-next-docstring)
        (message "Searching for doc string spell error...%d%%"
                 (/ (* 100 (point)) (point-max)))
        (if (looking-at "\"")
            (checkdoc-ispell-docstring-engine
             (save-excursion (forward-sexp 1) (point-marker)))))
      (message "Checkdoc: Done."))))

(defun checkdoc-message-interactive-ispell-loop (start-here)
  "Interactively spell check messages in the current buffer.
If START-HERE is nil, searching starts at the beginning of the current
buffer, otherwise searching starts at START-HERE."
  (when checkdoc-spellcheck-documentation-flag
    (save-excursion
      ;; Move point to where we need to start.
      (if start-here
          ;; Include whatever function point is in for good measure.
          (beginning-of-defun)
        (goto-char (point-min)))
      ;; Loop over message strings.
      (while (checkdoc-message-text-next-string (point-max))
        (message "Searching for message string spell error...%d%%"
                 (/ (* 100 (point)) (point-max)))
        (if (looking-at "\"")
            (checkdoc-ispell-docstring-engine
             (save-excursion (forward-sexp 1) (point-marker)))))
      (message "Checkdoc: Done."))))


771
(defun checkdoc-next-error (enable-fix)
Karl Heuer's avatar
Karl Heuer committed
772
  "Find and return the next checkdoc error list, or nil.
773
Only documentation strings are checked.
774 775 776 777
An error list 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 not
to edit the buffer before point without re-executing this check.
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
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)
796
  "Find and return the next checkdoc message related error list, or nil.
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
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*"
821 822 823
    (mapc #'princ
          (list "Error message:\n  " msg
                "\n\nEdit to fix this problem, and press C-M-c to continue.")))
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
  (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)
841
  (eval-buffer nil)
842
  (checkdoc-current-buffer t))
Karl Heuer's avatar
Karl Heuer committed
843 844

;;;###autoload
845 846 847 848 849 850
(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")
851 852
  (if (called-interactively-p 'interactive)
      (message "Checking buffer for style..."))
853 854
  ;; Assign a flag to spellcheck flag
  (let ((checkdoc-spellcheck-documentation-flag
855 856
	 (car (memq checkdoc-spellcheck-documentation-flag
                    '(buffer t))))
857 858 859 860 861 862 863 864 865 866 867 868
	(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)
869
	(not (called-interactively-p 'interactive))
870 871 872
	(if take-notes (checkdoc-show-diagnostics))
	(message "Checking buffer for style...Done."))))

873 874 875 876 877 878 879
;;;###autoload
(defun checkdoc-file (file)
  "Check FILE for document, comment, error style, and rogue spaces."
  (with-current-buffer (find-file-noselect file)
    (let ((checkdoc-diagnostic-buffer "*warn*"))
      (checkdoc-current-buffer t))))

880 881 882 883 884
;;;###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
885 886 887 888 889
Prefix argument TAKE-NOTES means to collect all the warning messages into
a separate buffer."
  (interactive "P")
  (let ((p (point)))
    (goto-char (point-min))
890
    (if (and take-notes (called-interactively-p 'interactive))
891
	(checkdoc-start-section "checkdoc-start"))
Karl Heuer's avatar
Karl Heuer committed
892 893 894 895 896 897 898
    (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)
899
  "Find the next doc string in the current buffer which has a style error.
Karl Heuer's avatar
Karl Heuer committed
900
Prefix argument TAKE-NOTES means to continue through the whole buffer and
901 902
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
903
  (interactive "P")
904
  (let ((wrong nil) (msg nil)
Karl Heuer's avatar
Karl Heuer committed
905 906
	;; Assign a flag to spellcheck flag
	(checkdoc-spellcheck-documentation-flag
907 908
	 (car (memq checkdoc-spellcheck-documentation-flag
                    '(buffer t))))
909 910 911 912
	(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
913 914 915 916
    (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))
917
	;; OK, let's look at the doc string.
Eric M. Ludlam's avatar
Eric M. Ludlam committed
918
	(setq msg (checkdoc-this-string-valid))
919
	(if msg (setq wrong (point)))))
Karl Heuer's avatar
Karl Heuer committed
920 921 922
    (if wrong
	(progn
	  (goto-char wrong)
923
	  (if (not take-notes)
924
	      (user-error "%s" (checkdoc-error-text msg)))))
925
    (checkdoc-show-diagnostics)
926
    (if (called-interactively-p 'interactive)
927
	(message "No style warnings."))))
Karl Heuer's avatar
Karl Heuer committed
928 929

(defun checkdoc-next-docstring ()
Richard M. Stallman's avatar
Richard M. Stallman committed
930 931
  "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
932 933 934 935 936 937 938 939
  (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
940
;;;###autoload
Karl Heuer's avatar
Karl Heuer committed
941
(defun checkdoc-comments (&optional take-notes)
Richard M. Stallman's avatar
Richard M. Stallman committed
942
  "Find missing comment sections in the current Emacs Lisp file.
Karl Heuer's avatar
Karl Heuer committed
943 944 945 946 947 948
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)
949
      (error "Can only check comments for a file buffer"))
Karl Heuer's avatar
Karl Heuer committed
950
  (let* ((checkdoc-spellcheck-documentation-flag
951 952
	  (car (memq checkdoc-spellcheck-documentation-flag
                     '(buffer t))))
953 954
	 (checkdoc-autofix-flag (if take-notes 'never checkdoc-autofix-flag))
	 (e (checkdoc-file-comments-engine))
955 956
         (checkdoc-generate-compile-warnings-flag
          (or take-notes checkdoc-generate-compile-warnings-flag)))
957
    (if e (user-error "%s" (checkdoc-error-text e)))
958
    (checkdoc-show-diagnostics)
Karl Heuer's avatar
Karl Heuer committed
959 960 961
    e))

;;;###autoload
962
(defun checkdoc-rogue-spaces (&optional take-notes interact)
Karl Heuer's avatar
Karl Heuer committed
963 964 965
  "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
966 967
if there is one.
Optional argument INTERACT permits more interactive fixing."
Karl Heuer's avatar
Karl Heuer committed
968 969
  (interactive "P")
  (if take-notes (checkdoc-start-section "checkdoc-rogue-spaces"))
970 971
  (let* ((checkdoc-autofix-flag (if take-notes 'never checkdoc-autofix-flag))
	 (e (checkdoc-rogue-space-check-engine nil nil interact))
972 973
         (checkdoc-generate-compile-warnings-flag
          (or take-notes checkdoc-generate-compile-warnings-flag)))
974
    (if (not (called-interactively-p 'interactive))
Karl Heuer's avatar
Karl Heuer committed
975
	e
976
      (if e
977
	  (message "%s" (checkdoc-error-text e))
978 979
	(checkdoc-show-diagnostics)
	(message "Space Check: done.")))))
Karl Heuer's avatar
Karl Heuer committed
980

981 982 983 984 985 986 987 988 989 990 991
;;;###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))
992
    (if (not (called-interactively-p 'interactive))
993 994
	e
      (if e
995
	  (user-error "%s" (checkdoc-error-text e))
996 997
	(checkdoc-show-diagnostics)))
    (goto-char p))
998 999
  (if (called-interactively-p 'interactive)
      (message "Checking interactive message text...done.")))
1000

Karl Heuer's avatar
Karl Heuer committed
1001 1002
;;;###autoload
(defun checkdoc-eval-defun ()
Karl Heuer's avatar
Karl Heuer committed
1003
  "Evaluate the current form with `eval-defun' and check its documentation.
Karl Heuer's avatar
Karl Heuer committed
1004 1005 1006 1007
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)
1008
  (call-interactively #'eval-defun)
Karl Heuer's avatar
Karl Heuer committed
1009 1010 1011 1012
  (checkdoc-defun))

;;;###autoload
(defun checkdoc-defun (&optional no-error)
Richard M. Stallman's avatar
Richard M. Stallman committed
1013 1014
  "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
1015
non-nil, then do not call error, but call `message' instead.
Richard M. Stallman's avatar
Richard M. Stallman committed
1016
If the doc string passes the test, then check the function for rogue white
Karl Heuer's avatar
Karl Heuer committed
1017 1018 1019 1020 1021 1022 1023
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)
1024
	;;    (message "Cannot check this sexp's doc string."))
Karl Heuer's avatar
Karl Heuer committed
1025 1026 1027 1028 1029 1030 1031
	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
1032 1033
	      (car (memq checkdoc-spellcheck-documentation-flag
                         '(defun t))))
1034
	     (beg (save-excursion (beginning-of-defun) (point)))
1035 1036 1037 1038 1039 1040 1041 1042
	     (end (save-excursion (end-of-defun) (point))))
        (dolist (fun (list #'checkdoc-this-string-valid
                           (lambda () (checkdoc-message-text-search beg end))
                           (lambda () (checkdoc-rogue-space-check-engine beg end))))
          (let ((msg (funcall fun)))
            (if msg (if no-error
                        (message "%s" (checkdoc-error-text msg))
                      (user-error "%s" (checkdoc-error-text msg))))))
1043 1044
	(if (called-interactively-p 'interactive)
	    (message "Checkdoc: done."))))))
Karl Heuer's avatar
Karl Heuer committed
1045 1046 1047 1048

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

1049
;;;###autoload
1050
(defun checkdoc-ispell ()
1051 1052
  "Check the style and spelling of everything interactively.
Calls `checkdoc' with spell-checking turned on.
1053
Prefix argument is the same as for `checkdoc'"
1054 1055
  (interactive)
  (let ((checkdoc-spellcheck-documentation-flag t))
1056
    (call-interactively #'checkdoc nil current-prefix-arg)))
1057

Karl Heuer's avatar
Karl Heuer committed
1058
;;;###autoload
1059
(defun checkdoc-ispell-current-buffer ()
1060
  "Check the style and spelling of the current buffer.
Karl Heuer's avatar
Karl Heuer committed
1061
Calls `checkdoc-current-buffer' with spell-checking turned on.
1062
Prefix argument is the same as for `checkdoc-current-buffer'"
Karl Heuer's avatar
Karl Heuer committed
1063 1064
  (interactive)
  (let ((checkdoc-spellcheck-documentation-flag t))
1065
    (call-interactively #'checkdoc-current-buffer nil current-prefix-arg)))