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