Remove trailing / from $ns_appbindir
[bpt/emacs.git] / lisp / emacs-lisp / checkdoc.el
CommitLineData
e8af40ee 1;;; checkdoc.el --- check documentation strings for style requirements
5b531322 2
acaf905b 3;; Copyright (C) 1997-1998, 2001-2012 Free Software Foundation, Inc.
04f3f5a2 4
0a0a3dee 5;; Author: Eric M. Ludlam <zappo@gnu.org>
84f473b0 6;; Version: 0.6.2
5b531322 7;; Keywords: docs, maint, lisp
04f3f5a2 8
5b531322 9;; This file is part of GNU Emacs.
04f3f5a2 10
d6cba7ae 11;; GNU Emacs is free software: you can redistribute it and/or modify
5b531322 12;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
04f3f5a2 15
5b531322
KH
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
04f3f5a2 20
5b531322 21;; You should have received a copy of the GNU General Public License
d6cba7ae 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
5b531322
KH
23
24;;; Commentary:
25;;
5fecb21a 26;; The Emacs Lisp manual has a nice chapter on how to write
5b531322
KH
27;; documentation strings. Many stylistic suggestions are fairly
28;; deterministic and easy to check for syntactically, but also easy
29;; to forget. The main checkdoc engine will perform the stylistic
30;; checks needed to make sure these styles are remembered.
31;;
32;; There are two ways to use checkdoc:
bca0d607
EL
33;; 1) Periodically use `checkdoc' or `checkdoc-current-buffer'.
34;; `checkdoc' is a more interactive version of
35;; `checkdoc-current-buffer'
5b531322 36;; 2) Use `checkdoc-minor-mode' to automatically check your
5fecb21a 37;; documentation whenever you evaluate Lisp code with C-M-x
5b531322
KH
38;; or [menu-bar emacs-lisp eval-buffer]. Additional key-bindings
39;; are also provided under C-c ? KEY
40;; (require 'checkdoc)
4f91a816 41;; (add-hook 'emacs-lisp-mode-hook 'checkdoc-minor-mode)
5b531322 42;;
bca0d607
EL
43;; Using `checkdoc':
44;;
45;; The commands `checkdoc' and `checkdoc-ispell' are the top-level
46;; entry points to all of the different checks that are available. It
47;; breaks examination of your Lisp file into four sections (comments,
48;; documentation, messages, and spacing) and indicates its current
49;; state in a status buffer.
50;;
51;; The Comments check examines your headers, footers, and
52;; various tags (such as "Code:") to make sure that your code is ready
53;; for easy integration into existing systems.
54;;
55;; The Documentation check deals with documentation strings
56;; and their elements that help make Emacs easier to use.
57;;
58;; The Messages check ensures that the strings displayed in the
59;; minibuffer by some commands (such as `error' and `y-or-n-p')
60;; are consistent with the Emacs environment.
61;;
62;; The Spacing check cleans up white-space at the end of lines.
63;;
64;; The interface while working with documentation and messages is
65;; slightly different when being run in the interactive mode. The
66;; interface offers several options, including the ability to skip to
67;; the next error, or back up to previous errors. Auto-fixing is
68;; turned off at this stage, but you can use the `f' or `F' key to fix
69;; a given error (if the fix is available.)
70;;
5b531322
KH
71;; Auto-fixing:
72;;
73;; There are four classifications of style errors in terms of how
74;; easy they are to fix. They are simple, complex, really complex,
75;; and impossible. (Impossible really means that checkdoc does not
76;; have a fixing routine yet.) Typically white-space errors are
77;; classified as simple, and are auto-fixed by default. Typographic
78;; changes are considered complex, and the user is asked if they want
79;; the problem fixed before checkdoc makes the change. These changes
80;; can be done without asking if `checkdoc-autofix-flag' is properly
81;; set. Potentially redundant changes are considered really complex,
82;; and the user is always asked before a change is inserted. The
83;; variable `checkdoc-autofix-flag' controls how these types of errors
84;; are fixed.
85;;
bca0d607 86;; Spell checking text:
5b531322
KH
87;;
88;; The variable `checkdoc-spellcheck-documentation-flag' can be set
89;; to customize how spell checking is to be done. Since spell
90;; checking can be quite slow, you can optimize how best you want your
84003f38 91;; checking done. The default is `defun', which spell checks each time
5b531322
KH
92;; `checkdoc-defun' or `checkdoc-eval-defun' is used. Setting to nil
93;; prevents spell checking during normal usage.
94;; Setting this variable to nil does not mean you cannot take
95;; advantage of the spell checking. You can instead use the
96;; interactive functions `checkdoc-ispell-*' to check the spelling of
97;; your documentation.
5fecb21a
RS
98;; There is a list of Lisp-specific words which checkdoc will
99;; install into Ispell on the fly, but only if Ispell is not already
5b531322
KH
100;; running. Use `ispell-kill-ispell' to make checkdoc restart it with
101;; these words enabled.
102;;
bca0d607 103;; Checking parameters:
0a0a3dee 104;;
6deb1543 105;; You might not always want a function to have its parameters listed
0a0a3dee
EL
106;; in order. When this is the case, put the following comment just in
107;; front of the documentation string: "; checkdoc-order: nil" This
108;; overrides the value of `checkdoc-arguments-in-order-flag'.
109;;
110;; If you specifically wish to avoid mentioning a parameter of a
111;; function in the doc string (such as a hidden parameter, or a
112;; parameter which is very obvious like events), you can have checkdoc
113;; skip looking for it by putting the following comment just in front
114;; of the documentation string: "; checkdoc-params: (args go here)"
115;;
bca0d607 116;; Checking message strings:
a4370a77 117;;
bca0d607 118;; The text that follows the `error' and `y-or-n-p' commands is
a4370a77
EL
119;; also checked. The documentation for `error' clearly states some
120;; simple style rules to follow which checkdoc will auto-fix for you.
121;; `y-or-n-p' also states that it should end in a space. I added that
122;; it should end in "? " since that is almost always used.
123;;
5b531322
KH
124;; Adding your own checks:
125;;
126;; You can experiment with adding your own checks by setting the
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)
71873e2b 919 (user-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)))
71873e2b 952 (if e (user-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
71873e2b 990 (user-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 1029 (beg (save-excursion (beginning-of-defun) (point)))
71873e2b
SM
1030 (end (save-excursion (end-of-defun) (point))))
1031 (dolist (fun (list #'checkdoc-this-string-valid
1032 (lambda () (checkdoc-message-text-search beg end))
1033 (lambda () (checkdoc-rogue-space-check-engine beg end))))
1034 (let ((msg (funcall fun)))
1035 (if msg (if no-error
1036 (message "%s" (checkdoc-error-text msg))
1037 (user-error "%s" (checkdoc-error-text msg))))))
32226619
JB
1038 (if (called-interactively-p 'interactive)
1039 (message "Checkdoc: done."))))))
5b531322
KH
1040
1041;;; Ispell interface for forcing a spell check
1042;;
1043
bca0d607
EL
1044;;;###autoload
1045(defun checkdoc-ispell (&optional take-notes)
1046 "Check the style and spelling of everything interactively.
1047Calls `checkdoc' with spell-checking turned on.
1048Prefix argument TAKE-NOTES is the same as for `checkdoc'"
1049 (interactive)
1050 (let ((checkdoc-spellcheck-documentation-flag t))
1051 (call-interactively 'checkdoc nil current-prefix-arg)))
1052
5b531322
KH
1053;;;###autoload
1054(defun checkdoc-ispell-current-buffer (&optional take-notes)
bca0d607 1055 "Check the style and spelling of the current buffer.
5b531322
KH
1056Calls `checkdoc-current-buffer' with spell-checking turned on.
1057Prefix argument TAKE-NOTES is the same as for `checkdoc-current-buffer'"
1058 (interactive)
1059 (let ((checkdoc-spellcheck-documentation-flag t))
1060 (call-interactively 'checkdoc-current-buffer nil current-prefix-arg)))
1061
1062;;;###autoload
1063(defun checkdoc-ispell-interactive (&optional take-notes)
1064 "Check the style and spelling of the current buffer interactively.
1065Calls `checkdoc-interactive' with spell-checking turned on.
bca0d607 1066Prefix argument TAKE-NOTES is the same as for `checkdoc-interactive'"
5b531322
KH
1067 (interactive)
1068 (let ((checkdoc-spellcheck-documentation-flag t))
1069 (call-interactively 'checkdoc-interactive nil current-prefix-arg)))
1070
1071;;;###autoload
bca0d607
EL
1072(defun checkdoc-ispell-message-interactive (&optional take-notes)
1073 "Check the style and spelling of message text interactively.
1074Calls `checkdoc-message-interactive' with spell-checking turned on.
1075Prefix argument TAKE-NOTES is the same as for `checkdoc-message-interactive'"
1076 (interactive)
1077 (let ((checkdoc-spellcheck-documentation-flag t))
1078 (call-interactively 'checkdoc-message-interactive nil current-prefix-arg)))
1079
1080;;;###autoload
1081(defun checkdoc-ispell-message-text (&optional take-notes)
1082 "Check the style and spelling of message text interactively.
1083Calls `checkdoc-message-text' with spell-checking turned on.
1084Prefix argument TAKE-NOTES is the same as for `checkdoc-message-text'"
1085 (interactive)
1086 (let ((checkdoc-spellcheck-documentation-flag t))
1087 (call-interactively 'checkdoc-message-text nil current-prefix-arg)))
1088
1089;;;###autoload
1090(defun checkdoc-ispell-start (&optional take-notes)
5b531322 1091 "Check the style and spelling of the current buffer.
bca0d607
EL
1092Calls `checkdoc-start' with spell-checking turned on.
1093Prefix argument TAKE-NOTES is the same as for `checkdoc-start'"
5b531322
KH
1094 (interactive)
1095 (let ((checkdoc-spellcheck-documentation-flag t))
bca0d607 1096 (call-interactively 'checkdoc-start nil current-prefix-arg)))
5b531322
KH
1097
1098;;;###autoload
1099(defun checkdoc-ispell-continue (&optional take-notes)
1100 "Check the style and spelling of the current buffer after point.
1101Calls `checkdoc-continue' with spell-checking turned on.
1102Prefix argument TAKE-NOTES is the same as for `checkdoc-continue'"
1103 (interactive)
1104 (let ((checkdoc-spellcheck-documentation-flag t))
1105 (call-interactively 'checkdoc-continue nil current-prefix-arg)))
1106
1107;;;###autoload
1108(defun checkdoc-ispell-comments (&optional take-notes)
1109 "Check the style and spelling of the current buffer's comments.
1110Calls `checkdoc-comments' with spell-checking turned on.
1111Prefix argument TAKE-NOTES is the same as for `checkdoc-comments'"
1112 (interactive)
1113 (let ((checkdoc-spellcheck-documentation-flag t))
1114 (call-interactively 'checkdoc-comments nil current-prefix-arg)))
1115
1116;;;###autoload
1117(defun checkdoc-ispell-defun (&optional take-notes)
5fecb21a 1118 "Check the style and spelling of the current defun with Ispell.
5b531322
KH
1119Calls `checkdoc-defun' with spell-checking turned on.
1120Prefix argument TAKE-NOTES is the same as for `checkdoc-defun'"
1121 (interactive)
1122 (let ((checkdoc-spellcheck-documentation-flag t))
1123 (call-interactively 'checkdoc-defun nil current-prefix-arg)))
1124
bca0d607
EL
1125;;; Error Management
1126;;
1127;; Errors returned from checkdoc functions can have various
1128;; features and behaviors, so we need some ways of specifying
1129;; them, and making them easier to use in the wacked-out interfaces
1130;; people are requesting
1131(defun checkdoc-create-error (text start end &optional unfixable)
1132 "Used to create the return error text returned from all engines.
1133TEXT is the descriptive text of the error. START and END define the region
1134it is sensible to highlight when describing the problem.
1135Optional argument UNFIXABLE means that the error has no auto-fix available.
1136
1137A list of the form (TEXT START END UNFIXABLE) is returned if we are not
1138generating a buffered list of errors."
1139 (if checkdoc-generate-compile-warnings-flag
1140 (progn (checkdoc-error start text)
1141 nil)
1142 (list text start end unfixable)))
1143
1144(defun checkdoc-error-text (err)
1145 "Return the text specified in the checkdoc ERR."
1146 ;; string-p part is for backwards compatibility
1147 (if (stringp err) err (car err)))
1148
1149(defun checkdoc-error-start (err)
1150 "Return the start point specified in the checkdoc ERR."
1151 ;; string-p part is for backwards compatibility
1152 (if (stringp err) nil (nth 1 err)))
1153
1154(defun checkdoc-error-end (err)
1155 "Return the end point specified in the checkdoc ERR."
1156 ;; string-p part is for backwards compatibility
1157 (if (stringp err) nil (nth 2 err)))
1158
1159(defun checkdoc-error-unfixable (err)
1160 "Return the t if we cannot autofix the error specified in the checkdoc ERR."
1161 ;; string-p part is for backwards compatibility
1162 (if (stringp err) nil (nth 3 err)))
1163
5b531322
KH
1164;;; Minor Mode specification
1165;;
5b531322 1166
82bc80bf 1167(defvar checkdoc-minor-mode-map
5b531322
KH
1168 (let ((map (make-sparse-keymap))
1169 (pmap (make-sparse-keymap)))
1170 ;; Override some bindings
1171 (define-key map "\C-\M-x" 'checkdoc-eval-defun)
bca0d607 1172 (define-key map "\C-x`" 'checkdoc-continue)
f8246027 1173 (if (not (featurep 'xemacs))
5b531322
KH
1174 (define-key map [menu-bar emacs-lisp eval-buffer]
1175 'checkdoc-eval-current-buffer))
bca0d607 1176 ;; Add some new bindings under C-c ?
5b531322
KH
1177 (define-key pmap "x" 'checkdoc-defun)
1178 (define-key pmap "X" 'checkdoc-ispell-defun)
1179 (define-key pmap "`" 'checkdoc-continue)
1180 (define-key pmap "~" 'checkdoc-ispell-continue)
bca0d607
EL
1181 (define-key pmap "s" 'checkdoc-start)
1182 (define-key pmap "S" 'checkdoc-ispell-start)
5b531322
KH
1183 (define-key pmap "d" 'checkdoc)
1184 (define-key pmap "D" 'checkdoc-ispell)
5b531322
KH
1185 (define-key pmap "b" 'checkdoc-current-buffer)
1186 (define-key pmap "B" 'checkdoc-ispell-current-buffer)
1187 (define-key pmap "e" 'checkdoc-eval-current-buffer)
a4370a77 1188 (define-key pmap "m" 'checkdoc-message-text)
bca0d607 1189 (define-key pmap "M" 'checkdoc-ispell-message-text)
5b531322
KH
1190 (define-key pmap "c" 'checkdoc-comments)
1191 (define-key pmap "C" 'checkdoc-ispell-comments)
1192 (define-key pmap " " 'checkdoc-rogue-spaces)
1193
1194 ;; bind our submap into map
1195 (define-key map "\C-c?" pmap)
1196 map)
1197 "Keymap used to override evaluation key-bindings for documentation checking.")
1198
1199;; Add in a menubar with easy-menu
1200
82bc80bf 1201(easy-menu-define
1398b795
SM
1202 nil checkdoc-minor-mode-map "Checkdoc Minor Mode Menu"
1203 '("CheckDoc"
1204 ["Interactive Buffer Style Check" checkdoc t]
1205 ["Interactive Buffer Style and Spelling Check" checkdoc-ispell t]
1206 ["Check Buffer" checkdoc-current-buffer t]
1207 ["Check and Spell Buffer" checkdoc-ispell-current-buffer t]
1208 "---"
1209 ["Interactive Style Check" checkdoc-interactive t]
1210 ["Interactive Style and Spelling Check" checkdoc-ispell-interactive t]
1211 ["Find First Style Error" checkdoc-start t]
1212 ["Find First Style or Spelling Error" checkdoc-ispell-start t]
1213 ["Next Style Error" checkdoc-continue t]
1214 ["Next Style or Spelling Error" checkdoc-ispell-continue t]
1215 ["Interactive Message Text Style Check" checkdoc-message-interactive t]
1216 ["Interactive Message Text Style and Spelling Check"
1217 checkdoc-ispell-message-interactive t]
1218 ["Check Message Text" checkdoc-message-text t]
1219 ["Check and Spell Message Text" checkdoc-ispell-message-text t]
1220 ["Check Comment Style" checkdoc-comments buffer-file-name]
1221 ["Check Comment Style and Spelling" checkdoc-ispell-comments
1222 buffer-file-name]
1223 ["Check for Rogue Spaces" checkdoc-rogue-spaces t]
1224 "---"
1225 ["Check Defun" checkdoc-defun t]
1226 ["Check and Spell Defun" checkdoc-ispell-defun t]
1227 ["Check and Evaluate Defun" checkdoc-eval-defun t]
1228 ["Check and Evaluate Buffer" checkdoc-eval-current-buffer t]
1229 ))
5b531322
KH
1230;; XEmacs requires some weird stuff to add this menu in a minor mode.
1231;; What is it?
1232
5b531322 1233;;;###autoload
b7f30cf5 1234(define-minor-mode checkdoc-minor-mode
ac6c8639
CY
1235 "Toggle automatic docstring checking (Checkdoc minor mode).
1236With a prefix argument ARG, enable Checkdoc minor mode if ARG is
1237positive, and disable it otherwise. If called from Lisp, enable
1238the mode if ARG is omitted or nil.
5b531322 1239
5fecb21a 1240In Checkdoc minor mode, the usual bindings for `eval-defun' which is
07051573 1241bound to \\<checkdoc-minor-mode-map>\\[checkdoc-eval-defun] and `checkdoc-eval-current-buffer' are overridden to include
5b531322
KH
1242checking of documentation strings.
1243
82bc80bf 1244\\{checkdoc-minor-mode-map}"
b92317dc 1245 nil checkdoc-minor-mode-string nil
90620d3d 1246 :group 'checkdoc)
5b531322
KH
1247
1248;;; Subst utils
1249;;
1250(defsubst checkdoc-run-hooks (hookvar &rest args)
1251 "Run hooks in HOOKVAR with ARGS."
1252 (if (fboundp 'run-hook-with-args-until-success)
1253 (apply 'run-hook-with-args-until-success hookvar args)
1254 ;; This method was similar to above. We ignore the warning
5fecb21a 1255 ;; since we will use the above for future Emacs versions
5b531322
KH
1256 (apply 'run-hook-with-args hookvar args)))
1257
1258(defsubst checkdoc-create-common-verbs-regexp ()
1259 "Rebuild the contents of `checkdoc-common-verbs-regexp'."
1260 (or checkdoc-common-verbs-regexp
1261 (setq checkdoc-common-verbs-regexp
1262 (concat "\\<\\("
1263 (mapconcat (lambda (e) (concat (car e)))
1264 checkdoc-common-verbs-wrong-voice "\\|")
1265 "\\)\\>"))))
1266
1267;; Profiler says this is not yet faster than just calling assoc
1268;;(defun checkdoc-word-in-alist-vector (word vector)
1269;; "Check to see if WORD is in the car of an element of VECTOR.
1270;;VECTOR must be sorted. The CDR should be a replacement. Since the
1271;;word list is getting bigger, it is time for a quick bisecting search."
1272;; (let ((max (length vector)) (min 0) i
1273;; (found nil) (fw nil))
1274;; (setq i (/ max 2))
1275;; (while (and (not found) (/= min max))
1276;; (setq fw (car (aref vector i)))
1277;; (cond ((string= word fw) (setq found (cdr (aref vector i))))
1278;; ((string< word fw) (setq max i))
1279;; (t (setq min i)))
1280;; (setq i (/ (+ max min) 2))
1281;; )
1282;; found))
1283
1284;;; Checking engines
1285;;
1286(defun checkdoc-this-string-valid ()
5fecb21a 1287 "Return a message string if the current doc string is invalid.
5b531322
KH
1288Check for style only, such as the first line always being a complete
1289sentence, whitespace restrictions, and making sure there are no
1290hard-coded key-codes such as C-[char] or mouse-[number] in the comment.
1291See the style guide in the Emacs Lisp manual for more details."
1292
5fecb21a 1293 ;; Jump over comments between the last object and the doc string
5b531322
KH
1294 (while (looking-at "[ \t\n]*;")
1295 (forward-line 1)
1296 (beginning-of-line)
1297 (skip-chars-forward " \n\t"))
1298
bca0d607
EL
1299 (let ((fp (checkdoc-defun-info))
1300 (err nil))
1301 (setq
1302 err
1303 ;; * Every command, function, or variable intended for users to know
1304 ;; about should have a documentation string.
1305 ;;
1306 ;; * An internal variable or subroutine of a Lisp program might as well
1307 ;; have a documentation string. In earlier Emacs versions, you could
1308 ;; save space by using a comment instead of a documentation string,
1309 ;; but that is no longer the case.
1310 (if (and (not (nth 1 fp)) ; not a variable
1311 (or (nth 2 fp) ; is interactive
1312 checkdoc-force-docstrings-flag) ;or we always complain
1313 (not (checkdoc-char= (following-char) ?\"))) ; no doc string
1314 ;; Sometimes old code has comments where the documentation should
84f473b0 1315 ;; be. Let's see if we can find the comment, and offer to turn it
bca0d607 1316 ;; into documentation for them.
eb83be8a
DL
1317 (let ((have-comment nil)
1318 (comment-start ";")) ; in case it's not default
bca0d607
EL
1319 (condition-case nil
1320 (progn
1321 (forward-sexp -1)
1322 (forward-sexp 1)
1323 (skip-chars-forward "\n \t")
1324 (setq have-comment (looking-at comment-start)))
1325 (error nil))
1326 (if have-comment
1327 (if (or (eq checkdoc-autofix-flag
1328 'automatic-then-never)
1329 (checkdoc-y-or-n-p
1330 "Convert comment to documentation? "))
1331 (save-excursion
1332 ;; Our point is at the beginning of the comment!
1333 ;; Insert a quote, then remove the comment chars.
1334 (insert "\"")
84f473b0 1335 (let ((docstring-start-point (point)))
bca0d607 1336 (while (looking-at comment-start)
84f473b0
EL
1337 (while (looking-at comment-start)
1338 (delete-char 1))
1339 (if (looking-at "[ \t]+")
1340 (delete-region (match-beginning 0) (match-end 0)))
1341 (forward-line 1)
1342 (beginning-of-line)
1343 (skip-chars-forward " \t")
1344 (if (looking-at comment-start)
1345 (progn
1346 (beginning-of-line)
1347 (zap-to-char 1 ?\;))))
bca0d607 1348 (beginning-of-line)
84f473b0
EL
1349 (forward-char -1)
1350 (insert "\"")
1351 (forward-char -1)
1352 ;; quote any double-quote characters in the comment.
1353 (while (search-backward "\"" docstring-start-point t)
1354 (insert "\\"))
1355 (if (eq checkdoc-autofix-flag 'automatic-then-never)
1356 (setq checkdoc-autofix-flag 'never))))
bca0d607
EL
1357 (checkdoc-create-error
1358 "You should convert this comment to documentation"
5ed619e0 1359 (point) (line-end-position)))
bca0d607
EL
1360 (checkdoc-create-error
1361 (if (nth 2 fp)
1362 "All interactive functions should have documentation"
1363 "All variables and subroutines might as well have a \
1364documentation string")
1365 (point) (+ (point) 1) t)))))
1366 (if (and (not err) (looking-at "\""))
cca982d0
SM
1367 (with-syntax-table checkdoc-syntax-table
1368 (checkdoc-this-string-valid-engine fp))
bca0d607
EL
1369 err)))
1370
1371(defun checkdoc-this-string-valid-engine (fp)
1372 "Return an error list or string if the current doc string is invalid.
5b531322 1373Depends on `checkdoc-this-string-valid' to reset the syntax table so that
bca0d607 1374regexp short cuts work. FP is the function defun information."
5b531322
KH
1375 (let ((case-fold-search nil)
1376 ;; Use a marker so if an early check modifies the text,
537b04b9 1377 ;; we won't accidentally lose our place. This could cause
5fecb21a 1378 ;; end-of doc string whitespace to also delete the " char.
bca0d607
EL
1379 (s (point))
1380 (e (if (looking-at "\"")
1381 (save-excursion (forward-sexp 1) (point-marker))
1382 (point))))
5b531322
KH
1383 (or
1384 ;; * *Do not* indent subsequent lines of a documentation string so that
1385 ;; the text is lined up in the source code with the text of the first
1386 ;; line. This looks nice in the source code, but looks bizarre when
1387 ;; users view the documentation. Remember that the indentation
1388 ;; before the starting double-quote is not part of the string!
1389 (save-excursion
1390 (forward-line 1)
1391 (beginning-of-line)
1392 (if (and (< (point) e)
1393 (looking-at "\\([ \t]+\\)[^ \t\n]"))
1394 (if (checkdoc-autofix-ask-replace (match-beginning 1)
1395 (match-end 1)
a4370a77 1396 "Remove this whitespace? "
5b531322
KH
1397 "")
1398 nil
bca0d607
EL
1399 (checkdoc-create-error
1400 "Second line should not have indentation"
1401 (match-beginning 1)
1402 (match-end 1)))))
9c07782e
GM
1403 ;; * Check for '(' in column 0.
1404 (save-excursion
1405 (when (re-search-forward "^(" e t)
1406 (if (checkdoc-autofix-ask-replace (match-beginning 0)
1407 (match-end 0)
1408 "Escape this '('? "
1409 "\\(")
1410 nil
1411 (checkdoc-create-error
1412 "Open parenthesis in column 0 should be escaped"
1413 (match-beginning 0) (match-end 0)))))
5b531322
KH
1414 ;; * Do not start or end a documentation string with whitespace.
1415 (let (start end)
1416 (if (or (if (looking-at "\"\\([ \t\n]+\\)")
1417 (setq start (match-beginning 1)
1418 end (match-end 1)))
1419 (save-excursion
1420 (forward-sexp 1)
1421 (forward-char -1)
1422 (if (/= (skip-chars-backward " \t\n") 0)
1423 (setq start (point)
1424 end (1- e)))))
1425 (if (checkdoc-autofix-ask-replace
a4370a77 1426 start end "Remove this whitespace? " "")
5b531322 1427 nil
bca0d607
EL
1428 (checkdoc-create-error
1429 "Documentation strings should not start or end with whitespace"
1430 start end))))
5b531322
KH
1431 ;; * The first line of the documentation string should consist of one
1432 ;; or two complete sentences that stand on their own as a summary.
1433 ;; `M-x apropos' displays just the first line, and if it doesn't
1434 ;; stand on its own, the result looks bad. In particular, start the
1435 ;; first line with a capital letter and end with a period.
1436 (save-excursion
1437 (end-of-line)
1438 (skip-chars-backward " \t\n")
1439 (if (> (point) e) (goto-char e)) ;of the form (defun n () "doc" nil)
1440 (forward-char -1)
1441 (cond
1442 ((and (checkdoc-char= (following-char) ?\")
1443 ;; A backslashed double quote at the end of a sentence
1444 (not (checkdoc-char= (preceding-char) ?\\)))
1445 ;; We might have to add a period in this case
1446 (forward-char -1)
84f473b0 1447 (if (looking-at "[.!?]")
5b531322
KH
1448 nil
1449 (forward-char 1)
1450 (if (checkdoc-autofix-ask-replace
a4370a77 1451 (point) (1+ (point)) "Add period to sentence? "
5b531322
KH
1452 ".\"" t)
1453 nil
bca0d607
EL
1454 (checkdoc-create-error
1455 "First sentence should end with punctuation"
1456 (point) (1+ (point))))))
84f473b0 1457 ((looking-at "[\\!?;:.)]")
5b531322
KH
1458 ;; These are ok
1459 nil)
bca0d607
EL
1460 ((and checkdoc-permit-comma-termination-flag (looking-at ","))
1461 nil)
5b531322 1462 (t
bca0d607 1463 ;; If it is not a complete sentence, let's see if we can
5b531322
KH
1464 ;; predict a clever way to make it one.
1465 (let ((msg "First line is not a complete sentence")
1466 (e (point)))
1467 (beginning-of-line)
1468 (if (re-search-forward "\\. +" e t)
1469 ;; Here we have found a complete sentence, but no break.
1470 (if (checkdoc-autofix-ask-replace
1471 (1+ (match-beginning 0)) (match-end 0)
a4370a77 1472 "First line not a complete sentence. Add RET here? "
5b531322
KH
1473 "\n" t)
1474 (let (l1 l2)
76fc02b6 1475 (end-of-line 2)
5b531322
KH
1476 (setq l1 (current-column)
1477 l2 (save-excursion
76fc02b6 1478 (end-of-line 2)
5b531322
KH
1479 (current-column)))
1480 (if (> (+ l1 l2 1) 80)
5fecb21a 1481 (setq msg "Incomplete auto-fix; doc string \
a4370a77 1482may require more formatting")
5b531322
KH
1483 ;; We can merge these lines! Replace this CR
1484 ;; with a space.
1485 (delete-char 1) (insert " ")
1486 (setq msg nil))))
bca0d607 1487 ;; Let's see if there is enough room to draw the next
5b531322
KH
1488 ;; line's sentence up here. I often get hit w/
1489 ;; auto-fill moving my words around.
1490 (let ((numc (progn (end-of-line) (- 80 (current-column))))
1491 (p (point)))
1492 (forward-line 1)
1493 (beginning-of-line)
84f473b0 1494 (if (and (re-search-forward "[.!?:\"]\\([ \t\n]+\\|\"\\)"
76fc02b6 1495 (line-end-position) t)
5b531322
KH
1496 (< (current-column) numc))
1497 (if (checkdoc-autofix-ask-replace
1498 p (1+ p)
5fecb21a 1499 "1st line not a complete sentence. Join these lines? "
5b531322
KH
1500 " " t)
1501 (progn
1502 ;; They said yes. We have more fill work to do...
bca0d607
EL
1503 (goto-char (match-beginning 1))
1504 (delete-region (point) (match-end 1))
5b531322
KH
1505 (insert "\n")
1506 (setq msg nil))))))
bca0d607
EL
1507 (if msg
1508 (checkdoc-create-error msg s (save-excursion
1509 (goto-char s)
76fc02b6 1510 (line-end-position))))))))
5b531322
KH
1511 ;; Continuation of above. Make sure our sentence is capitalized.
1512 (save-excursion
1513 (skip-chars-forward "\"\\*")
1514 (if (looking-at "[a-z]")
1515 (if (checkdoc-autofix-ask-replace
1516 (match-beginning 0) (match-end 0)
a4370a77 1517 "Capitalize your sentence? " (upcase (match-string 0))
5b531322
KH
1518 t)
1519 nil
bca0d607
EL
1520 (checkdoc-create-error
1521 "First line should be capitalized"
1522 (match-beginning 0) (match-end 0)))
5b531322 1523 nil))
5b531322
KH
1524 ;; * Don't write key sequences directly in documentation strings.
1525 ;; Instead, use the `\\[...]' construct to stand for them.
1526 (save-excursion
1527 (let ((f nil) (m nil) (start (point))
04f3f5a2 1528 (re "[^`A-Za-z0-9_]\\([CMA]-[a-zA-Z]\\|\\(\\([CMA]-\\)?\
5b531322
KH
1529mouse-[0-3]\\)\\)\\>"))
1530 ;; Find the first key sequence not in a sample
1531 (while (and (not f) (setq m (re-search-forward re e t)))
1532 (setq f (not (checkdoc-in-sample-code-p start e))))
1533 (if m
bca0d607
EL
1534 (checkdoc-create-error
1535 (concat
1536 "Keycode " (match-string 1)
1537 " embedded in doc string. Use \\\\<keymap> & \\\\[function] "
1538 "instead")
1539 (match-beginning 1) (match-end 1) t))))
5b531322
KH
1540 ;; It is not practical to use `\\[...]' very many times, because
1541 ;; display of the documentation string will become slow. So use this
1542 ;; to describe the most important commands in your major mode, and
1543 ;; then use `\\{...}' to display the rest of the mode's keymap.
1544 (save-excursion
25d91d94
SM
1545 (if (and (re-search-forward "\\\\\\\\\\[\\w+" e t
1546 (1+ checkdoc-max-keyref-before-warn))
1547 (not (re-search-forward "\\\\\\\\{\\w+}" e t)))
bca0d607
EL
1548 (checkdoc-create-error
1549 "Too many occurrences of \\[function]. Use \\{keymap} instead"
1550 s (marker-position e))))
5fecb21a 1551 ;; Ambiguous quoted symbol. When a symbol is both bound and fbound,
a4370a77
EL
1552 ;; and is referred to in documentation, it should be prefixed with
1553 ;; something to disambiguate it. This check must be before the
1554 ;; 80 column check because it will probably break that.
1555 (save-excursion
1556 (let ((case-fold-search t)
bca0d607
EL
1557 (ret nil) mb me)
1558 (while (and (re-search-forward "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'" e t)
1559 (not ret))
1560 (let* ((ms1 (match-string 1))
1561 (sym (intern-soft ms1)))
1562 (setq mb (match-beginning 1)
1563 me (match-end 1))
1564 (if (and sym (boundp sym) (fboundp sym)
1565 (save-excursion
1566 (goto-char mb)
1567 (forward-word -1)
1568 (not (looking-at
1569 "variable\\|option\\|function\\|command\\|symbol"))))
a4370a77 1570 (if (checkdoc-autofix-ask-replace
bca0d607
EL
1571 mb me "Prefix this ambiguous symbol? " ms1 t)
1572 ;; We didn't actually replace anything. Here we find
a4370a77
EL
1573 ;; out what special word form they wish to use as
1574 ;; a prefix.
1575 (let ((disambiguate
1576 (completing-read
5b76833f 1577 "Disambiguating Keyword (default variable): "
a4370a77
EL
1578 '(("function") ("command") ("variable")
1579 ("option") ("symbol"))
1580 nil t nil nil "variable")))
1581 (goto-char (1- mb))
1582 (insert disambiguate " ")
1583 (forward-word 1))
1584 (setq ret
bca0d607
EL
1585 (format "Disambiguate %s by preceding w/ \
1586function,command,variable,option or symbol." ms1))))))
1587 (if ret
1588 (checkdoc-create-error ret mb me)
1589 nil)))
5b531322
KH
1590 ;; * Format the documentation string so that it fits in an
1591 ;; Emacs window on an 80-column screen. It is a good idea
1592 ;; for most lines to be no wider than 60 characters. The
1593 ;; first line can be wider if necessary to fit the
1594 ;; information that ought to be there.
1595 (save-excursion
bca0d607
EL
1596 (let ((start (point))
1597 (eol nil))
5b531322 1598 (while (and (< (point) e)
bca0d607
EL
1599 (or (progn (end-of-line) (setq eol (point))
1600 (< (current-column) 80))
5b531322
KH
1601 (progn (beginning-of-line)
1602 (re-search-forward "\\\\\\\\[[<{]"
bca0d607 1603 eol t))
5b531322
KH
1604 (checkdoc-in-sample-code-p start e)))
1605 (forward-line 1))
1606 (end-of-line)
1607 (if (and (< (point) e) (> (current-column) 80))
bca0d607
EL
1608 (checkdoc-create-error
1609 "Some lines are over 80 columns wide"
76fc02b6 1610 s (save-excursion (goto-char s) (line-end-position))))))
5b531322 1611 ;; Here we deviate to tests based on a variable or function.
bca0d607
EL
1612 ;; We must do this before checking for symbols in quotes because there
1613 ;; is a chance that just such a symbol might really be an argument.
5b531322
KH
1614 (cond ((eq (nth 1 fp) t)
1615 ;; This is if we are in a variable
1616 (or
1617 ;; * The documentation string for a variable that is a
bca0d607
EL
1618 ;; yes-or-no flag should start with words such as Non-nil
1619 ;; means..., to make it clear that all non-`nil' values are
5b531322
KH
1620 ;; equivalent and indicate explicitly what `nil' and non-`nil'
1621 ;; mean.
1622 ;; * If a user option variable records a true-or-false
1623 ;; condition, give it a name that ends in `-flag'.
1624
1625 ;; If the variable has -flag in the name, make sure
1626 (if (and (string-match "-flag$" (car fp))
1627 (not (looking-at "\"\\*?Non-nil\\s-+means\\s-+")))
bca0d607 1628 (checkdoc-create-error
995e028a 1629 "Flag variable doc strings should usually start: Non-nil means"
bca0d607 1630 s (marker-position e) t))
145b6377
SM
1631 ;; Don't rename variable to "foo-flag". This is unnecessary
1632 ;; and such names often end up inconvenient when the variable
1633 ;; is later expanded to non-boolean values. --Stef
5fecb21a 1634 ;; If the doc string starts with "Non-nil means"
145b6377
SM
1635 ;; (if (and (looking-at "\"\\*?Non-nil\\s-+means\\s-+")
1636 ;; (not (string-match "-flag$" (car fp))))
1637 ;; (let ((newname
1638 ;; (if (string-match "-p$" (car fp))
1639 ;; (concat (substring (car fp) 0 -2) "-flag")
1640 ;; (concat (car fp) "-flag"))))
1641 ;; (if (checkdoc-y-or-n-p
1642 ;; (format
1643 ;; "Rename to %s and Query-Replace all occurrences? "
1644 ;; newname))
1645 ;; (progn
1646 ;; (beginning-of-defun)
1647 ;; (query-replace-regexp
1648 ;; (concat "\\<" (regexp-quote (car fp)) "\\>")
1649 ;; newname))
1650 ;; (checkdoc-create-error
1651 ;; "Flag variable names should normally end in `-flag'" s
1652 ;; (marker-position e)))))
5b531322
KH
1653 ;; Done with variables
1654 ))
1655 (t
1656 ;; This if we are in a function definition
1657 (or
1658 ;; * When a function's documentation string mentions the value
1659 ;; of an argument of the function, use the argument name in
1660 ;; capital letters as if it were a name for that value. Thus,
1661 ;; the documentation string of the function `/' refers to its
1662 ;; second argument as `DIVISOR', because the actual argument
1663 ;; name is `divisor'.
1664
1665 ;; Addendum: Make sure they appear in the doc in the same
1666 ;; order that they are found in the arg list.
1667 (let ((args (cdr (cdr (cdr (cdr fp)))))
1668 (last-pos 0)
1669 (found 1)
1670 (order (and (nth 3 fp) (car (nth 3 fp))))
a4370a77
EL
1671 (nocheck (append '("&optional" "&rest") (nth 3 fp)))
1672 (inopts nil))
5b531322
KH
1673 (while (and args found (> found last-pos))
1674 (if (member (car args) nocheck)
a4370a77
EL
1675 (setq args (cdr args)
1676 inopts t)
5b531322
KH
1677 (setq last-pos found
1678 found (save-excursion
1679 (re-search-forward
1680 (concat "\\<" (upcase (car args))
1681 ;; Require whitespace OR
1682 ;; ITEMth<space> OR
1683 ;; ITEMs<space>
2e898692 1684 "\\(\\>\\|th\\>\\|s\\>\\|[.,;:]\\)")
5b531322
KH
1685 e t)))
1686 (if (not found)
1687 (let ((case-fold-search t))
bca0d607 1688 ;; If the symbol was not found, let's see if we
5b531322
KH
1689 ;; can find it with a different capitalization
1690 ;; and see if the user wants to capitalize it.
1691 (if (save-excursion
1692 (re-search-forward
84f473b0
EL
1693 (concat "\\<\\(" (car args)
1694 ;; Require whitespace OR
1695 ;; ITEMth<space> OR
1696 ;; ITEMs<space>
1697 "\\)\\(\\>\\|th\\>\\|s\\>\\)")
1698 e t))
5b531322
KH
1699 (if (checkdoc-autofix-ask-replace
1700 (match-beginning 1) (match-end 1)
1701 (format
995e028a 1702 "If this is the argument `%s', it should appear as %s. Fix? "
5b531322
KH
1703 (car args) (upcase (car args)))
1704 (upcase (car args)) t)
1705 (setq found (match-beginning 1))))))
1706 (if found (setq args (cdr args)))))
1707 (if (not found)
a4370a77
EL
1708 ;; It wasn't found at all! Offer to attach this new symbol
1709 ;; to the end of the documentation string.
bca0d607
EL
1710 (if (checkdoc-y-or-n-p
1711 (format
1712 "Add %s documentation to end of doc string? "
1713 (upcase (car args))))
5fecb21a 1714 ;; Now do some magic and invent a doc string.
a4370a77
EL
1715 (save-excursion
1716 (goto-char e) (forward-char -1)
1717 (insert "\n"
1718 (if inopts "Optional a" "A")
1719 "rgument " (upcase (car args))
1720 " ")
1721 (insert (read-string "Describe: "))
1722 (if (not (save-excursion (forward-char -1)
1723 (looking-at "[.?!]")))
1724 (insert "."))
1725 nil)
bca0d607
EL
1726 (checkdoc-create-error
1727 (format
995e028a 1728 "Argument `%s' should appear (as %s) in the doc string"
bca0d607
EL
1729 (car args) (upcase (car args)))
1730 s (marker-position e)))
5b531322
KH
1731 (if (or (and order (eq order 'yes))
1732 (and (not order) checkdoc-arguments-in-order-flag))
1733 (if (< found last-pos)
bca0d607
EL
1734 (checkdoc-create-error
1735 "Arguments occur in the doc string out of order"
1736 s (marker-position e) t)))))
1737 ;; * For consistency, phrase the verb in the first sentence of a
995e028a
RS
1738 ;; documentation string for functions as an imperative.
1739 ;; For instance, use `Return the cons of A and
bca0d607
EL
1740 ;; B.' in preference to `Returns the cons of A and B.'
1741 ;; Usually it looks good to do likewise for the rest of the
1742 ;; first paragraph. Subsequent paragraphs usually look better
1743 ;; if they have proper subjects.
1744 ;;
1745 ;; This is the least important of the above tests. Make sure
1746 ;; it occurs last.
1747 (and checkdoc-verb-check-experimental-flag
1748 (save-excursion
b08b261e 1749 ;; Maybe rebuild the monster-regexp
bca0d607
EL
1750 (checkdoc-create-common-verbs-regexp)
1751 (let ((lim (save-excursion
1752 (end-of-line)
1753 ;; check string-continuation
1754 (if (checkdoc-char= (preceding-char) ?\\)
76fc02b6
GM
1755 (line-end-position 2)
1756 (point))))
bca0d607
EL
1757 (rs nil) replace original (case-fold-search t))
1758 (while (and (not rs)
1759 (re-search-forward
1760 checkdoc-common-verbs-regexp
1761 lim t))
1762 (setq original (buffer-substring-no-properties
1763 (match-beginning 1) (match-end 1))
1764 rs (assoc (downcase original)
1765 checkdoc-common-verbs-wrong-voice))
1766 (if (not rs) (error "Verb voice alist corrupted"))
1767 (setq replace (let ((case-fold-search nil))
66e9f105
JB
1768 (if (string-match-p "^[A-Z]" original)
1769 (capitalize (cdr rs))
1770 (cdr rs))))
bca0d607
EL
1771 (if (checkdoc-autofix-ask-replace
1772 (match-beginning 1) (match-end 1)
995e028a
RS
1773 (format "Use the imperative for \"%s\". \
1774Replace with \"%s\"? " original replace)
bca0d607
EL
1775 replace t)
1776 (setq rs nil)))
1777 (if rs
1778 ;; there was a match, but no replace
1779 (checkdoc-create-error
1780 (format
995e028a 1781 "Probably \"%s\" should be imperative \"%s\""
bca0d607
EL
1782 original replace)
1783 (match-beginning 1) (match-end 1))))))
5b531322
KH
1784 ;; Done with functions
1785 )))
bca0d607
EL
1786 ;;* When a documentation string refers to a Lisp symbol, write it as
1787 ;; it would be printed (which usually means in lower case), with
1788 ;; single-quotes around it. For example: `lambda'. There are two
1789 ;; exceptions: write t and nil without single-quotes. (In this
1790 ;; manual, we normally do use single-quotes for those symbols.)
1791 (save-excursion
1792 (let ((found nil) (start (point)) (msg nil) (ms nil))
1793 (while (and (not msg)
1794 (re-search-forward
b762841f 1795 ;; Ignore manual page references like
26a27884
JA
1796 ;; git-config(1).
1797 "[^-([`':a-zA-Z]\\(\\w+[:-]\\(\\w\\|\\s_\\)+\\)[^](']"
bca0d607
EL
1798 e t))
1799 (setq ms (match-string 1))
66e9f105
JB
1800 ;; A . is a \s_ char, so we must remove periods from
1801 ;; sentences more carefully.
1802 (when (string-match-p "\\.$" ms)
1803 (setq ms (substring ms 0 (1- (length ms)))))
bca0d607
EL
1804 (if (and (not (checkdoc-in-sample-code-p start e))
1805 (not (checkdoc-in-example-string-p start e))
1806 (not (member ms checkdoc-symbol-words))
1807 (setq found (intern-soft ms))
1808 (or (boundp found) (fboundp found)))
1809 (progn
1810 (setq msg (format "Add quotes around Lisp symbol `%s'? "
1811 ms))
1812 (if (checkdoc-autofix-ask-replace
1813 (match-beginning 1) (+ (match-beginning 1)
1814 (length ms))
1815 msg (concat "`" ms "'") t)
1816 (setq msg nil)
1817 (setq msg
1818 (format "Lisp symbol `%s' should appear in quotes"
1819 ms))))))
1820 (if msg
1821 (checkdoc-create-error msg (match-beginning 1)
1822 (+ (match-beginning 1)
1823 (length ms)))
1824 nil)))
1825 ;; t and nil case
1826 (save-excursion
1827 (if (re-search-forward "\\(`\\(t\\|nil\\)'\\)" e t)
1828 (if (checkdoc-autofix-ask-replace
1829 (match-beginning 1) (match-end 1)
1830 (format "%s should not appear in quotes. Remove? "
1831 (match-string 2))
1832 (match-string 2) t)
1833 nil
1834 (checkdoc-create-error
995e028a 1835 "Symbols t and nil should not appear in `...' quotes"
bca0d607
EL
1836 (match-beginning 1) (match-end 1)))))
1837 ;; Here is some basic sentence formatting
1838 (checkdoc-sentencespace-region-engine (point) e)
1839 ;; Here are common proper nouns that should always appear capitalized.
1840 (checkdoc-proper-noun-region-engine (point) e)
1841 ;; Make sure the doc string has correctly spelled English words
1842 ;; in it. This function is extracted due to its complexity,
5fecb21a 1843 ;; and reliance on the Ispell program.
5b531322
KH
1844 (checkdoc-ispell-docstring-engine e)
1845 ;; User supplied checks
1846 (save-excursion (checkdoc-run-hooks 'checkdoc-style-hooks fp e))
1847 ;; Done!
1848 )))
1849
1850(defun checkdoc-defun-info nil
1851 "Return a list of details about the current sexp.
1852It is a list of the form:
5fecb21a 1853 (NAME VARIABLE INTERACTIVE NODOCPARAMS PARAMETERS ...)
5b531322
KH
1854where NAME is the name, VARIABLE is t if this is a `defvar',
1855INTERACTIVE is nil if this is not an interactive function, otherwise
1856it is the position of the `interactive' call, and PARAMETERS is a
1857string which is the name of each variable in the function's argument
1858list. The NODOCPARAMS is a sublist of parameters specified by a checkdoc
1859comment for a given defun. If the first element is not a string, then
1860the token checkdoc-order: <TOKEN> exists, and TOKEN is a symbol read
1861from the comment."
1862 (save-excursion
1863 (beginning-of-defun)
1864 (let ((defun (looking-at "(def\\(un\\|macro\\|subst\\|advice\\)"))
1865 (is-advice (looking-at "(defadvice"))
1866 (lst nil)
1867 (ret nil)
1868 (oo (make-vector 3 0))) ;substitute obarray for `read'
1869 (forward-char 1)
1870 (forward-sexp 1)
1871 (skip-chars-forward " \n\t")
1872 (setq ret
1873 (list (buffer-substring-no-properties
1874 (point) (progn (forward-sexp 1) (point)))))
1875 (if (not defun)
1876 (setq ret (cons t ret))
1877 ;; The variable spot
1878 (setq ret (cons nil ret))
1879 ;; Interactive
1880 (save-excursion
1881 (setq ret (cons
82bc80bf 1882 (re-search-forward "^\\s-*(interactive"
5b531322
KH
1883 (save-excursion (end-of-defun) (point))
1884 t)
1885 ret)))
1886 (skip-chars-forward " \t\n")
1887 (let ((bss (buffer-substring (point) (save-excursion (forward-sexp 1)
1888 (point))))
1889 ;; Overload th main obarray so read doesn't intern the
1890 ;; local symbols of the function we are checking.
1891 ;; Without this we end up cluttering the symbol space w/
1892 ;; useless symbols.
1893 (obarray oo))
1894 ;; Ok, check for checkdoc parameter comment here
1895 (save-excursion
1896 (setq ret
1897 (cons
1898 (let ((sl1 nil))
1899 (if (re-search-forward ";\\s-+checkdoc-order:\\s-+"
1900 (save-excursion (end-of-defun)
1901 (point))
1902 t)
1903 (setq sl1 (list (cond ((looking-at "nil") 'no)
1904 ((looking-at "t") 'yes)))))
1905 (if (re-search-forward ";\\s-+checkdoc-params:\\s-+"
1906 (save-excursion (end-of-defun)
1907 (point))
1908 t)
1909 (let ((sl nil))
1910 (goto-char (match-end 0))
bca0d607
EL
1911 (condition-case nil
1912 (setq lst (read (current-buffer)))
1913 (error (setq lst nil))) ; error in text
1914 (if (not (listp lst)) ; not a list of args
1915 (setq lst (list lst)))
1916 (if (and lst (not (symbolp (car lst)))) ;weird arg
1917 (setq lst nil))
5b531322
KH
1918 (while lst
1919 (setq sl (cons (symbol-name (car lst)) sl)
1920 lst (cdr lst)))
1921 (setq sl1 (append sl1 sl))))
1922 sl1)
1923 ret)))
bca0d607 1924 ;; Read the list of parameters, but do not put the symbols in
5b531322
KH
1925 ;; the standard obarray.
1926 (setq lst (read bss)))
1927 ;; This is because read will intern nil if it doesn't into the
1928 ;; new obarray.
1929 (if (not (listp lst)) (setq lst nil))
1930 (if is-advice nil
1931 (while lst
1932 (setq ret (cons (symbol-name (car lst)) ret)
1933 lst (cdr lst)))))
1934 (nreverse ret))))
1935
1936(defun checkdoc-in-sample-code-p (start limit)
5fecb21a 1937 "Return non-nil if the current point is in a code fragment.
5b531322 1938A code fragment is identified by an open parenthesis followed by a
bca0d607
EL
1939symbol which is a valid function or a word in all CAPS, or a parenthesis
1940that is quoted with the ' character. Only the region from START to LIMIT
5a5fa834 1941is allowed while searching for the bounding parenthesis."
5b531322
KH
1942 (save-match-data
1943 (save-restriction
1944 (narrow-to-region start limit)
1945 (save-excursion
1946 (and (condition-case nil (progn (up-list 1) t) (error nil))
1947 (condition-case nil (progn (forward-list -1) t) (error nil))
1948 (or (save-excursion (forward-char -1) (looking-at "'("))
1949 (and (looking-at "(\\(\\(\\w\\|[-:_]\\)+\\)[ \t\n;]")
1950 (let ((ms (buffer-substring-no-properties
1951 (match-beginning 1) (match-end 1))))
1952 ;; if this string is function bound, we are in
1953 ;; sample code. If it has a - or : character in
1954 ;; the name, then it is probably supposed to be bound
1955 ;; but isn't yet.
1956 (or (fboundp (intern-soft ms))
bca0d607
EL
1957 (let ((case-fold-search nil))
1958 (string-match "^[A-Z-]+$" ms))
5b531322
KH
1959 (string-match "\\w[-:_]+\\w" ms))))))))))
1960
bca0d607
EL
1961(defun checkdoc-in-example-string-p (start limit)
1962 "Return non-nil if the current point is in an \"example string\".
1963This string is identified by the characters \\\" surrounding the text.
1964The text checked is between START and LIMIT."
1965 (save-match-data
1966 (save-excursion
1967 (let ((p (point))
1968 (c 0))
1969 (goto-char start)
1970 (while (and (< (point) p) (re-search-forward "\\\\\"" limit t))
1971 (setq c (1+ c)))
1972 (and (< 0 c) (= (% c 2) 0))))))
1973
1974(defun checkdoc-proper-noun-region-engine (begin end)
1975 "Check all text between BEGIN and END for lower case proper nouns.
1976These are Emacs centric proper nouns which should be capitalized for
1977consistency. Return an error list if any are not fixed, but
1978internally skip over no answers.
1979If the offending word is in a piece of quoted text, then it is skipped."
1980 (save-excursion
1981 (let ((case-fold-search nil)
cca982d0
SM
1982 (errtxt nil) bb be)
1983 (with-syntax-table checkdoc-syntax-table
1984 (goto-char begin)
1985 (while (re-search-forward checkdoc-proper-noun-regexp end t)
1986 (let ((text (match-string 1))
1987 (b (match-beginning 1))
1988 (e (match-end 1)))
1989 (if (and (not (save-excursion
1990 (goto-char b)
1991 (forward-char -1)
1992 (looking-at "`\\|\"\\|\\.\\|\\\\")))
1993 ;; surrounded by /, as in a URL or filename: /emacs/
1994 (not (and (= ?/ (char-after e))
1995 (= ?/ (char-before b))))
1996 (not (checkdoc-in-example-string-p begin end))
1997 ;; info or url links left alone
1998 (not (thing-at-point-looking-at
1999 help-xref-info-regexp))
2000 (not (thing-at-point-looking-at
2001 help-xref-url-regexp)))
2002 (if (checkdoc-autofix-ask-replace
2003 b e (format "Text %s should be capitalized. Fix? "
2004 text)
2005 (capitalize text) t)
2006 nil
2007 (if errtxt
2008 ;; If there is already an error, then generate
2009 ;; the warning output if applicable
2010 (if checkdoc-generate-compile-warnings-flag
2011 (checkdoc-create-error
2012 (format
2013 "Name %s should appear capitalized as %s"
2014 text (capitalize text))
2015 b e))
2016 (setq errtxt
2017 (format
2018 "Name %s should appear capitalized as %s"
2019 text (capitalize text))
2020 bb b be e)))))))
bca0d607
EL
2021 (if errtxt (checkdoc-create-error errtxt bb be)))))
2022
2023(defun checkdoc-sentencespace-region-engine (begin end)
2024 "Make sure all sentences have double spaces between BEGIN and END."
84f473b0
EL
2025 (if sentence-end-double-space
2026 (save-excursion
2027 (let ((case-fold-search nil)
cca982d0
SM
2028 (errtxt nil) bb be)
2029 (with-syntax-table checkdoc-syntax-table
2030 (goto-char begin)
2031 (while (re-search-forward "[^ .0-9]\\(\\. \\)[^ \n]" end t)
2032 (let ((b (match-beginning 1))
2033 (e (match-end 1)))
2034 (unless (or (checkdoc-in-sample-code-p begin end)
2035 (checkdoc-in-example-string-p begin end)
2036 (save-excursion
2037 (goto-char b)
2038 (condition-case nil
2039 (progn
2040 (forward-sexp -1)
2041 ;; piece of an abbreviation
2042 ;; FIXME etc
2043 (looking-at
1dcf791f 2044 "\\([a-zA-Z]\\|[iI]\\.?e\\|[eE]\\.?g\\)\\."))
cca982d0
SM
2045 (error t))))
2046 (if (checkdoc-autofix-ask-replace
2047 b e
2048 "There should be two spaces after a period. Fix? "
2049 ". ")
2050 nil
2051 (if errtxt
2052 ;; If there is already an error, then generate
2053 ;; the warning output if applicable
2054 (if checkdoc-generate-compile-warnings-flag
2055 (checkdoc-create-error
2056 "There should be two spaces after a period"
2057 b e))
2058 (setq errtxt
2059 "There should be two spaces after a period"
2060 bb b be e)))))))
84f473b0 2061 (if errtxt (checkdoc-create-error errtxt bb be))))))
bca0d607 2062
5b531322
KH
2063;;; Ispell engine
2064;;
2065(eval-when-compile (require 'ispell))
2066
2067(defun checkdoc-ispell-init ()
5fecb21a 2068 "Initialize Ispell process (default version) with Lisp words.
5b531322
KH
2069The words used are from `checkdoc-ispell-lisp-words'. If `ispell'
2070cannot be loaded, then set `checkdoc-spellcheck-documentation-flag' to
2071nil."
2072 (require 'ispell)
2073 (if (not (symbol-value 'ispell-process)) ;Silence byteCompiler
2074 (condition-case nil
2075 (progn
2076 (ispell-buffer-local-words)
5fecb21a 2077 ;; This code copied in part from ispell.el Emacs 19.34
5b531322
KH
2078 (let ((w checkdoc-ispell-lisp-words))
2079 (while w
2080 (process-send-string
2081 ;; Silence byte compiler
2082 (symbol-value 'ispell-process)
2083 (concat "@" (car w) "\n"))
2084 (setq w (cdr w)))))
2085 (error (setq checkdoc-spellcheck-documentation-flag nil)))))
2086
2087(defun checkdoc-ispell-docstring-engine (end)
5fecb21a
RS
2088 "Run the Ispell tools on the doc string between point and END.
2089Since Ispell isn't Lisp-smart, we must pre-process the doc string
2090before using the Ispell engine on it."
bca0d607
EL
2091 (if (or (not checkdoc-spellcheck-documentation-flag)
2092 ;; If the user wants no questions or fixing, then we must
2093 ;; disable spell checking as not useful.
b08b261e 2094 (not checkdoc-autofix-flag)
bca0d607 2095 (eq checkdoc-autofix-flag 'never))
5b531322
KH
2096 nil
2097 (checkdoc-ispell-init)
2098 (save-excursion
2099 (skip-chars-forward "^a-zA-Z")
2100 (let ((word nil) (sym nil) (case-fold-search nil) (err nil))
2101 (while (and (not err) (< (point) end))
2102 (if (save-excursion (forward-char -1) (looking-at "[('`]"))
2103 ;; Skip lists describing meta-syntax, or bound variables
2104 (forward-sexp 1)
2105 (setq word (buffer-substring-no-properties
2106 (point) (progn
2107 (skip-chars-forward "a-zA-Z-")
2108 (point)))
2109 sym (intern-soft word))
2110 (if (and sym (or (boundp sym) (fboundp sym)))
bca0d607 2111 ;; This is probably repetitive in most cases, but not always.
5b531322
KH
2112 nil
2113 ;; Find out how we spell-check this word.
2114 (if (or
0a0a3dee 2115 ;; All caps w/ option th, or s tacked on the end
e1dbe924 2116 ;; for pluralization or number.
0a0a3dee 2117 (string-match "^[A-Z][A-Z]+\\(s\\|th\\)?$" word)
5b531322
KH
2118 (looking-at "}") ; a keymap expression
2119 )
2120 nil
2121 (save-excursion
2122 (if (not (eq checkdoc-autofix-flag 'never))
2123 (let ((lk last-input-event))
2124 (ispell-word nil t)
2125 (if (not (equal last-input-event lk))
2126 (progn
2127 (sit-for 0)
2128 (message "Continuing..."))))
2129 ;; Nothing here.
2130 )))))
2131 (skip-chars-forward "^a-zA-Z"))
2132 err))))
2133
2134;;; Rogue space checking engine
2135;;
bca0d607
EL
2136(defun checkdoc-rogue-space-check-engine (&optional start end interact)
2137 "Return a message list if there is a line with white space at the end.
5b531322 2138If `checkdoc-autofix-flag' permits, delete that whitespace instead.
c67c14f3 2139If optional arguments START and END are non-nil, bound the check to
bca0d607
EL
2140this region.
2141Optional argument INTERACT may permit the user to fix problems on the fly."
5b531322 2142 (let ((p (point))
bca0d607 2143 (msg nil) s e (f nil))
5b531322
KH
2144 (if (not start) (setq start (point-min)))
2145 ;; If end is nil, it means end of buffer to search anyway
2146 (or
bca0d607 2147 ;; Check for an error if `? ' or `?\ ' is used at the end of a line.
5b531322
KH
2148 ;; (It's dangerous)
2149 (progn
2150 (goto-char start)
bca0d607
EL
2151 (while (and (not msg) (re-search-forward "\\?\\\\?[ \t][ \t]*$" end t))
2152 (setq msg
2153 "Don't use `? ' at the end of a line. \
2154News agents may remove it"
2155 s (match-beginning 0) e (match-end 0) f t)
2156 ;; If interactive is passed down, give them a chance to fix things.
2157 (if (and interact (y-or-n-p (concat msg ". Fix? ")))
2158 (progn
2159 (checkdoc-recursive-edit msg)
2160 (setq msg nil)
2161 (goto-char s)
2162 (beginning-of-line)))))
2163 ;; Check for, and potentially remove whitespace appearing at the
5b531322
KH
2164 ;; end of different lines.
2165 (progn
2166 (goto-char start)
5fecb21a 2167 ;; There is no documentation in the Emacs Lisp manual about this check,
5b531322 2168 ;; it is intended to help clean up messy code and reduce the file size.
bca0d607 2169 (while (and (not msg) (re-search-forward "[^ \t\n;]\\([ \t]+\\)$" end t))
5b531322
KH
2170 ;; This is not a complex activity
2171 (if (checkdoc-autofix-ask-replace
2172 (match-beginning 1) (match-end 1)
5fecb21a 2173 "White space at end of line. Remove? " "")
5b531322 2174 nil
bca0d607
EL
2175 (setq msg "White space found at end of line"
2176 s (match-beginning 1) e (match-end 1))))))
5b531322
KH
2177 ;; Return an error and leave the cursor at that spot, or restore
2178 ;; the cursor.
2179 (if msg
bca0d607 2180 (checkdoc-create-error msg s e f)
5b531322
KH
2181 (goto-char p)
2182 nil)))
2183
2184;;; Comment checking engine
2185;;
2186(eval-when-compile
2187 ;; We must load this to:
bca0d607 2188 ;; a) get symbols for compile and
5b531322
KH
2189 ;; b) determine if we have lm-history symbol which doesn't always exist
2190 (require 'lisp-mnt))
2191
e8f2a5d5
GM
2192(defvar generate-autoload-cookie)
2193
5b531322 2194(defun checkdoc-file-comments-engine ()
bca0d607 2195 "Return a message list if this file does not match the Emacs standard.
5b531322
KH
2196This checks for style only, such as the first line, Commentary:,
2197Code:, and others referenced in the style guide."
2198 (if (featurep 'lisp-mnt)
2199 nil
2200 (require 'lisp-mnt)
bca0d607 2201 ;; Old XEmacs don't have `lm-commentary-mark'
5b531322
KH
2202 (if (and (not (fboundp 'lm-commentary-mark)) (boundp 'lm-commentary))
2203 (defalias 'lm-commentary-mark 'lm-commentary)))
2204 (save-excursion
2205 (let* ((f1 (file-name-nondirectory (buffer-file-name)))
2206 (fn (file-name-sans-extension f1))
bca0d607
EL
2207 (fe (substring f1 (length fn)))
2208 (err nil))
5b531322 2209 (goto-char (point-min))
bca0d607
EL
2210 ;; This file has been set up where ERR is a variable. Each check is
2211 ;; asked, and the function will make sure that if the user does not
2212 ;; auto-fix some error, that we still move on to the next auto-fix,
2213 ;; AND we remember the past errors.
2214 (setq
2215 err
5b531322
KH
2216 ;; Lisp Maintenance checks first
2217 ;; Was: (lm-verify) -> not flexible enough for some people
2218 ;; * Summary at the beginning of the file:
2219 (if (not (lm-summary))
2220 ;; This certifies as very complex so always ask unless
2221 ;; it's set to never
bca0d607 2222 (if (checkdoc-y-or-n-p "There is no first line summary! Add one? ")
5b531322
KH
2223 (progn
2224 (goto-char (point-min))
2225 (insert ";;; " fn fe " --- " (read-string "Summary: ") "\n"))
bca0d607
EL
2226 (checkdoc-create-error
2227 "The first line should be of the form: \";;; package --- Summary\""
76fc02b6
GM
2228 (point-min) (save-excursion (goto-char (point-min))
2229 (line-end-position))))
bca0d607
EL
2230 nil))
2231 (setq
2232 err
2233 (or
2234 ;; * Commentary Section
2235 (if (not (lm-commentary-mark))
2236 (progn
2237 (goto-char (point-min))
2238 (cond
2239 ((re-search-forward
2240 "write\\s-+to\\s-+the\\s-+Free Software Foundation, Inc."
2241 nil t)
2242 (re-search-forward "^;;\\s-*\n\\|^\n" nil t))
2243 ((or (re-search-forward "^;;; History" nil t)
2244 (re-search-forward "^;;; Code" nil t)
2245 (re-search-forward "^(require" nil t)
c9d557f5 2246 (re-search-forward "^(" nil t))
81e213dc
CY
2247 (beginning-of-line))
2248 (t (re-search-forward ";;; .* --- .*\n")))
bca0d607
EL
2249 (if (checkdoc-y-or-n-p
2250 "You should have a \";;; Commentary:\", add one? ")
2251 (insert "\n;;; Commentary:\n;; \n\n")
2252 (checkdoc-create-error
2253 "You should have a section marked \";;; Commentary:\""
2254 nil nil t)))
2255 nil)
2256 err))
2257 (setq
2258 err
2259 (or
2260 ;; * History section. Say nothing if there is a file ChangeLog
2261 (if (or (not checkdoc-force-history-flag)
2262 (file-exists-p "ChangeLog")
2263 (file-exists-p "../ChangeLog")
2264 (let ((fn 'lm-history-mark)) ;bestill byte-compiler
2265 (and (fboundp fn) (funcall fn))))
2266 nil
2267 (progn
2268 (goto-char (or (lm-commentary-mark) (point-min)))
2269 (cond
2270 ((re-search-forward
2271 "write\\s-+to\\s-+the\\s-+Free Software Foundation, Inc."
2272 nil t)
2273 (re-search-forward "^;;\\s-*\n\\|^\n" nil t))
2274 ((or (re-search-forward "^;;; Code" nil t)
2275 (re-search-forward "^(require" nil t)
c9d557f5 2276 (re-search-forward "^(" nil t))
bca0d607
EL
2277 (beginning-of-line)))
2278 (if (checkdoc-y-or-n-p
2279 "You should have a \";;; History:\", add one? ")
2280 (insert "\n;;; History:\n;; \n\n")
2281 (checkdoc-create-error
2282 "You should have a section marked \";;; History:\" or use a ChangeLog"
2283 (point) nil))))
2284 err))
2285 (setq
2286 err
2287 (or
2288 ;; * Code section
2289 (if (not (lm-code-mark))
68634374
KR
2290 (let ((cont t)
2291 pos)
bca0d607 2292 (goto-char (point-min))
68634374
KR
2293 ;; match ";;;###autoload" cookie to keep it with the form
2294 (require 'autoload)
2295 (while (and cont (re-search-forward
2296 (concat "^\\("
2297 (regexp-quote generate-autoload-cookie)
2298 "\n\\)?"
2299 "(")
2300 nil t))
2301 (setq pos (match-beginning 0)
2302 cont (looking-at "require\\s-+")))
bca0d607
EL
2303 (if (and (not cont)
2304 (checkdoc-y-or-n-p
2305 "There is no ;;; Code: marker. Insert one? "))
68634374
KR
2306 (progn (goto-char pos)
2307 (insert ";;; Code:\n\n")
bca0d607
EL
2308 nil)
2309 (checkdoc-create-error
2310 "You should have a section marked \";;; Code:\""
2311 (point) nil)))
2312 nil)
2313 err))
2314 (setq
2315 err
2316 (or
2317 ;; * A footer. Not compartmentalized from lm-verify: too bad.
2318 ;; The following is partially clipped from lm-verify
2319 (save-excursion
2320 (goto-char (point-max))
2321 (if (not (re-search-backward
65a30f58 2322 (concat "^;;;[ \t]+" (regexp-quote fn) "\\(" (regexp-quote fe)
bca0d607
EL
2323 "\\)?[ \t]+ends here[ \t]*$"
2324 "\\|^;;;[ \t]+ End of file[ \t]+"
65a30f58 2325 (regexp-quote fn) "\\(" (regexp-quote fe) "\\)?")
bca0d607
EL
2326 nil t))
2327 (if (checkdoc-y-or-n-p "No identifiable footer! Add one? ")
2328 (progn
2329 (goto-char (point-max))
2330 (insert "\n(provide '" fn ")\n\n;;; " fn fe " ends here\n"))
2331 (checkdoc-create-error
2332 (format "The footer should be: (provide '%s)\\n;;; %s%s ends here"
2333 fn fn fe)
2334 (1- (point-max)) (point-max)))))
2335 err))
2336 ;; The below checks will not return errors if the user says NO
bca0d607
EL
2337
2338 ;; Let's spellcheck the commentary section. This is the only
2339 ;; section that is easy to pick out, and it is also the most
2340 ;; visible section (with the finder).
2341 (let ((cm (lm-commentary-mark)))
d547e25f
LK
2342 (when cm
2343 (save-excursion
2344 (goto-char cm)
2345 (let ((e (copy-marker (lm-commentary-end))))
2346 ;; Since the comments talk about Lisp, use the
2347 ;; specialized spell-checker we also used for doc
2348 ;; strings.
2349 (checkdoc-sentencespace-region-engine (point) e)
2350 (checkdoc-proper-noun-region-engine (point) e)
2351 (checkdoc-ispell-docstring-engine e)))))
bca0d607
EL
2352 (setq
2353 err
2354 (or
2355 ;; Generic Full-file checks (should be comment related)
2356 (checkdoc-run-hooks 'checkdoc-comment-style-hooks)
2357 err))
2358 ;; Done with full file comment checks
2359 err)))
5b531322
KH
2360
2361(defun checkdoc-outside-major-sexp ()
2362 "Return t if point is outside the bounds of a valid sexp."
2363 (save-match-data
2364 (save-excursion
2365 (let ((p (point)))
2366 (or (progn (beginning-of-defun) (bobp))
2367 (progn (end-of-defun) (< (point) p)))))))
2368
a4370a77
EL
2369;;; `error' and `message' text verifier.
2370;;
a4370a77 2371(defun checkdoc-message-text-search (&optional beg end)
bca0d607 2372 "Search between BEG and END for a style error with message text.
a4370a77
EL
2373Optional arguments BEG and END represent the boundary of the check.
2374The default boundary is the entire buffer."
bca0d607
EL
2375 (let ((e nil)
2376 (type nil))
a4370a77
EL
2377 (if (not (or beg end)) (setq beg (point-min) end (point-max)))
2378 (goto-char beg)
bca0d607
EL
2379 (while (setq type (checkdoc-message-text-next-string end))
2380 (setq e (checkdoc-message-text-engine type)))
a4370a77 2381 e))
a1506d29 2382
bca0d607
EL
2383(defun checkdoc-message-text-next-string (end)
2384 "Move cursor to the next checkable message string after point.
2385Return the message classification.
2386Argument END is the maximum bounds to search in."
2387 (let ((return nil))
2388 (while (and (not return)
2389 (re-search-forward
2390 "(\\s-*\\(\\(\\w\\|\\s_\\)*error\\|\
2391\\(\\w\\|\\s_\\)*y-or-n-p\\(-with-timeout\\)?\
2392\\|checkdoc-autofix-ask-replace\\)[ \t\n]+" end t))
2393 (let* ((fn (match-string 1))
2394 (type (cond ((string-match "error" fn)
2395 'error)
2396 (t 'y-or-n-p))))
2397 (if (string-match "checkdoc-autofix-ask-replace" fn)
2398 (progn (forward-sexp 2)
2399 (skip-chars-forward " \t\n")))
2400 (if (and (eq type 'y-or-n-p)
2401 (looking-at "(format[ \t\n]+"))
2402 (goto-char (match-end 0)))
2403 (skip-chars-forward " \t\n")
2404 (if (not (looking-at "\""))
2405 nil
2406 (setq return type))))
2407 return))
2408
2409(defun checkdoc-message-text-engine (&optional type)
a4370a77 2410 "Return or fix errors found in strings passed to a message display function.
bca0d607
EL
2411According to the documentation for the function `error', the error list
2412should not end with a period, and should start with a capital letter.
a4370a77
EL
2413The function `y-or-n-p' has similar constraints.
2414Argument TYPE specifies the type of question, such as `error or `y-or-n-p."
bca0d607
EL
2415 ;; If type is nil, then attempt to derive it.
2416 (if (not type)
2417 (save-excursion
2418 (up-list -1)
2419 (if (looking-at "(format")
2420 (up-list -1))
2421 (setq type
2422 (cond ((looking-at "(error")
2423 'error)
2424 (t 'y-or-n-p)))))
a4370a77
EL
2425 (let ((case-fold-search nil))
2426 (or
2427 ;; From the documentation of the symbol `error':
2428 ;; In Emacs, the convention is that error messages start with a capital
2429 ;; letter but *do not* end with a period. Please follow this convention
2430 ;; for the sake of consistency.
2431 (if (and (save-excursion (forward-char 1)
2432 (looking-at "[a-z]\\w+"))
2433 (not (checkdoc-autofix-ask-replace
2434 (match-beginning 0) (match-end 0)
2435 "Capitalize your message text? "
2436 (capitalize (match-string 0))
2437 t)))
bca0d607
EL
2438 (checkdoc-create-error
2439 "Messages should start with a capital letter"
2440 (match-beginning 0) (match-end 0))
a4370a77 2441 nil)
bca0d607
EL
2442 ;; In general, sentences should have two spaces after the period.
2443 (checkdoc-sentencespace-region-engine (point)
2444 (save-excursion (forward-sexp 1)
2445 (point)))
2446 ;; Look for proper nouns in this region too.
2447 (checkdoc-proper-noun-region-engine (point)
2448 (save-excursion (forward-sexp 1)
2449 (point)))
2450 ;; Here are message type specific questions.
a4370a77
EL
2451 (if (and (eq type 'error)
2452 (save-excursion (forward-sexp 1)
2453 (forward-char -2)
2454 (looking-at "\\."))
2455 (not (checkdoc-autofix-ask-replace (match-beginning 0)
2456 (match-end 0)
2457 "Remove period from error? "
2458 ""
2459 t)))
bca0d607
EL
2460 (checkdoc-create-error
2461 "Error messages should *not* end with a period"
2462 (match-beginning 0) (match-end 0))
a4370a77
EL
2463 nil)
2464 ;; `y-or-n-p' documentation explicitly says:
2465 ;; It should end in a space; `y-or-n-p' adds `(y or n) ' to it.
2466 ;; I added the ? requirement. Without it, it is unclear that we
2467 ;; ask a question and it appears to be an undocumented style.
bca0d607
EL
2468 (if (eq type 'y-or-n-p)
2469 (if (not (save-excursion (forward-sexp 1)
2470 (forward-char -3)
2471 (not (looking-at "\\? "))))
2472 nil
2473 (if (save-excursion (forward-sexp 1)
2474 (forward-char -2)
2475 (looking-at "\\?"))
2476 ;; If we see a ?, then replace with "? ".
2477 (if (checkdoc-autofix-ask-replace
2478 (match-beginning 0) (match-end 0)
995e028a 2479 "`y-or-n-p' argument should end with \"? \". Fix? "
bca0d607
EL
2480 "? " t)
2481 nil
2482 (checkdoc-create-error
995e028a 2483 "`y-or-n-p' argument should end with \"? \""
bca0d607
EL
2484 (match-beginning 0) (match-end 0)))
2485 (if (save-excursion (forward-sexp 1)
2486 (forward-char -2)
2487 (looking-at " "))
2488 (if (checkdoc-autofix-ask-replace
2489 (match-beginning 0) (match-end 0)
995e028a 2490 "`y-or-n-p' argument should end with \"? \". Fix? "
bca0d607
EL
2491 "? " t)
2492 nil
2493 (checkdoc-create-error
995e028a 2494 "`y-or-n-p' argument should end with \"? \""
bca0d607
EL
2495 (match-beginning 0) (match-end 0)))
2496 (if (and ;; if this isn't true, we have a problem.
2497 (save-excursion (forward-sexp 1)
2498 (forward-char -1)
2499 (looking-at "\""))
2500 (checkdoc-autofix-ask-replace
2501 (match-beginning 0) (match-end 0)
995e028a 2502 "`y-or-n-p' argument should end with \"? \". Fix? "
bca0d607
EL
2503 "? \"" t))
2504 nil
2505 (checkdoc-create-error
995e028a 2506 "`y-or-n-p' argument should end with \"? \""
bca0d607
EL
2507 (match-beginning 0) (match-end 0)))))))
2508 ;; Now, let's just run the spell checker on this guy.
2509 (checkdoc-ispell-docstring-engine (save-excursion (forward-sexp 1)
2510 (point)))
a4370a77
EL
2511 )))
2512
5b531322
KH
2513;;; Auto-fix helper functions
2514;;
bca0d607
EL
2515(defun checkdoc-y-or-n-p (question)
2516 "Like `y-or-n-p', but pays attention to `checkdoc-autofix-flag'.
2517Argument QUESTION is the prompt passed to `y-or-n-p'."
2518 (prog1
2519 (if (or (not checkdoc-autofix-flag)
2520 (eq checkdoc-autofix-flag 'never))
2521 nil
2522 (y-or-n-p question))
2523 (if (eq checkdoc-autofix-flag 'automatic-then-never)
2524 (setq checkdoc-autofix-flag 'never))))
2525
5b531322
KH
2526(defun checkdoc-autofix-ask-replace (start end question replacewith
2527 &optional complex)
2528 "Highlight between START and END and queries the user with QUESTION.
2529If the user says yes, or if `checkdoc-autofix-flag' permits, replace
2530the region marked by START and END with REPLACEWITH. If optional flag
2531COMPLEX is non-nil, then we may ask the user a question. See the
2532documentation for `checkdoc-autofix-flag' for details.
2533
2534If a section is auto-replaced without asking the user, this function
2535will pause near the fixed code so the user will briefly see what
2536happened.
2537
bca0d607
EL
2538This function returns non-nil if the text was replaced.
2539
2540This function will not modify `match-data'."
2541 (if (and checkdoc-autofix-flag
2542 (not (eq checkdoc-autofix-flag 'never)))
5b531322 2543 (let ((o (checkdoc-make-overlay start end))
bca0d607
EL
2544 (ret nil)
2545 (md (match-data)))
5b531322
KH
2546 (unwind-protect
2547 (progn
2548 (checkdoc-overlay-put o 'face 'highlight)
2549 (if (or (eq checkdoc-autofix-flag 'automatic)
bca0d607 2550 (eq checkdoc-autofix-flag 'automatic-then-never)
5b531322
KH
2551 (and (eq checkdoc-autofix-flag 'semiautomatic)
2552 (not complex))
2553 (and (or (eq checkdoc-autofix-flag 'query) complex)
2554 (y-or-n-p question)))
2555 (save-excursion
2556 (goto-char start)
2557 ;; On the off chance this is automatic, display
bca0d607 2558 ;; the question anyway so the user knows what's
5b531322
KH
2559 ;; going on.
2560 (if checkdoc-bouncy-flag (message "%s -> done" question))
2561 (delete-region start end)
2562 (insert replacewith)
2563 (if checkdoc-bouncy-flag (sit-for 0))
2564 (setq ret t)))
bca0d607
EL
2565 (checkdoc-delete-overlay o)
2566 (set-match-data md))
2567 (checkdoc-delete-overlay o)
2568 (set-match-data md))
2569 (if (eq checkdoc-autofix-flag 'automatic-then-never)
2570 (setq checkdoc-autofix-flag 'never))
5b531322
KH
2571 ret)))
2572
2573;;; Warning management
2574;;
2575(defvar checkdoc-output-font-lock-keywords
f578d9f8 2576 '(("^\\*\\*\\* \\(.+\\.el\\): \\([^ \n]+\\)"
bca0d607 2577 (1 font-lock-function-name-face)
f578d9f8 2578 (2 font-lock-comment-face)))
5b531322
KH
2579 "Keywords used to highlight a checkdoc diagnostic buffer.")
2580
f578d9f8
SM
2581(defvar checkdoc-output-error-regex-alist
2582 '(("^\\(.+\\.el\\):\\([0-9]+\\): " 1 2)))
5b531322 2583
bca0d607
EL
2584(defvar checkdoc-pending-errors nil
2585 "Non-nil when there are errors that have not been displayed yet.")
2586
f578d9f8
SM
2587(define-derived-mode checkdoc-output-mode compilation-mode "Checkdoc"
2588 "Set up the major mode for the buffer containing the list of errors."
2589 (set (make-local-variable 'compilation-error-regexp-alist)
2590 checkdoc-output-error-regex-alist)
2591 (set (make-local-variable 'compilation-mode-font-lock-keywords)
2592 checkdoc-output-font-lock-keywords))
5b531322 2593
84f473b0
EL
2594(defun checkdoc-buffer-label ()
2595 "The name to use for a checkdoc buffer in the error list."
2596 (if (buffer-file-name)
780b142e 2597 (file-relative-name (buffer-file-name))
84f473b0
EL
2598 (concat "#<buffer "(buffer-name) ">")))
2599
5b531322
KH
2600(defun checkdoc-start-section (check-type)
2601 "Initialize the checkdoc diagnostic buffer for a pass.
2602Create the header so that the string CHECK-TYPE is displayed as the
2603function called to create the messages."
780b142e
SM
2604 (let ((dir default-directory)
2605 (label (checkdoc-buffer-label)))
2606 (with-current-buffer (get-buffer-create checkdoc-diagnostic-buffer)
2607 (checkdoc-output-mode)
2608 (setq default-directory dir)
2609 (goto-char (point-max))
b260aab7
SM
2610 (let ((inhibit-read-only t))
2611 (insert "\n\n\C-l\n*** " label ": "
2612 check-type " V " checkdoc-version)))))
5b531322
KH
2613
2614(defun checkdoc-error (point msg)
2615 "Store POINT and MSG as errors in the checkdoc diagnostic buffer."
bca0d607 2616 (setq checkdoc-pending-errors t)
780b142e
SM
2617 (let ((text (list "\n" (checkdoc-buffer-label) ":"
2618 (int-to-string
2619 (count-lines (point-min) (or point (point-min))))
2620 ": " msg)))
2621 (with-current-buffer (get-buffer checkdoc-diagnostic-buffer)
2622 (goto-char (point-max))
b260aab7
SM
2623 (let ((inhibit-read-only t))
2624 (apply 'insert text)))))
5b531322
KH
2625
2626(defun checkdoc-show-diagnostics ()
2627 "Display the checkdoc diagnostic buffer in a temporary window."
bca0d607
EL
2628 (if checkdoc-pending-errors
2629 (let ((b (get-buffer checkdoc-diagnostic-buffer)))
2630 (if b (progn (pop-to-buffer b)
2631 (goto-char (point-max))
2632 (re-search-backward "\C-l" nil t)
2633 (beginning-of-line)
2634 (forward-line 1)
2635 (recenter 0)))
2636 (other-window -1)
2637 (setq checkdoc-pending-errors nil)
2638 nil)))
5b531322 2639
1398b795 2640(custom-add-option 'emacs-lisp-mode-hook 'checkdoc-minor-mode)
5b531322
KH
2641
2642(provide 'checkdoc)
5fecb21a 2643
5b531322 2644;;; checkdoc.el ends here