checkdoc.el 101 KB

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