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