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