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