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