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