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