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