document.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. ;;; srecode/document.el --- Documentation (comment) generation
  2. ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <eric@siege-engine.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; Routines for fabricating human readable text from function and
  18. ;; variable names as base-text for function comments. Document is not
  19. ;; meant to generate end-text for any function. It is merely meant to
  20. ;; provide some useful base words and text, and as a framework for
  21. ;; managing comments.
  22. ;;
  23. ;;; Origins:
  24. ;;
  25. ;; Document was first written w/ cparse, a custom regexp based c parser.
  26. ;;
  27. ;; Document was then ported to cedet/semantic using sformat (super
  28. ;; format) as the templating engine.
  29. ;;
  30. ;; Document has now been ported to srecode, using the semantic recoder
  31. ;; as the templating engine.
  32. ;; This file combines srecode/document.el and srecode/document-vars.el
  33. ;; from the CEDET repository.
  34. (require 'srecode/args)
  35. (require 'srecode/dictionary)
  36. (require 'srecode/extract)
  37. (require 'srecode/insert)
  38. (require 'srecode/semantic)
  39. (require 'semantic)
  40. (require 'semantic/tag)
  41. (require 'semantic/doc)
  42. (require 'pulse)
  43. ;;; Code:
  44. (defgroup document nil
  45. "File and tag browser frame."
  46. :group 'texinfo
  47. :group 'srecode)
  48. (defcustom srecode-document-autocomment-common-nouns-abbrevs
  49. '(
  50. ("sock\\(et\\)?" . "socket")
  51. ("addr\\(ess\\)?" . "address")
  52. ("buf\\(f\\(er\\)?\\)?" . "buffer")
  53. ("cur\\(r\\(ent\\)?\\)?" . "current")
  54. ("dev\\(ice\\)?" . "device")
  55. ("doc" . "document")
  56. ("i18n" . "internationalization")
  57. ("file" . "file")
  58. ("line" . "line")
  59. ("l10n" . "localization")
  60. ("msg\\|message" . "message")
  61. ("name" . "name")
  62. ("next\\|nxt" . "next")
  63. ("num\\(ber\\)?" . "number")
  64. ("port" . "port")
  65. ("host" . "host")
  66. ("obj\\|object" . "object")
  67. ("previous\\|prev" . "previous")
  68. ("str\\(ing\\)?" . "string")
  69. ("use?r" . "user")
  70. ("\\(^\\|\\s-\\)id\\($\\|\\s-\\)" . "Identifier") ;complex cause ;common syllable
  71. )
  72. "List of common English abbreviations or full words.
  73. These are nouns (as opposed to verbs) for use in creating expanded
  74. versions of names. This is an alist with each element of the form:
  75. (MATCH . RESULT)
  76. MATCH is a regexp to match in the type field.
  77. RESULT is a string."
  78. :group 'document
  79. :type '(repeat (cons (string :tag "Regexp")
  80. (string :tag "Doc Text"))))
  81. (defcustom srecode-document-autocomment-function-alist
  82. '(
  83. ("abort" . "Aborts the")
  84. ;; trick to get re-alloc and alloc to pair into one sentence.
  85. ("realloc" . "moves or ")
  86. ("alloc\\(ate\\)?" . "Allocates and initializes a new ")
  87. ("clean" . "Cleans up the")
  88. ("clobber" . "Removes")
  89. ("close" . "Cleanly closes")
  90. ("check" . "Checks the")
  91. ("comp\\(are\\)?" . "Compares the")
  92. ("create" . "Creates a new ")
  93. ("find" . "Finds ")
  94. ("free" . "Frees up space")
  95. ("gen\\(erate\\)?" . "Generates a new ")
  96. ("get\\|find" . "Looks for the given ")
  97. ("gobble" . "Removes")
  98. ("he?lp" . "Provides help for")
  99. ("li?ste?n" . "Listens for ")
  100. ("connect" . "Connects to ")
  101. ("acc?e?pt" . "Accepts a ")
  102. ("load" . "Loads in ")
  103. ("match" . "Check that parameters match")
  104. ("name" . "Provides a name which ")
  105. ("new" . "Allocates a ")
  106. ("parse" . "Parses the parameters and returns ")
  107. ("print\\|display" . "Prints out")
  108. ("read" . "Reads from")
  109. ("reset" . "Resets the parameters and returns")
  110. ("scan" . "Scans the ")
  111. ("setup\\|init\\(ialize\\)?" . "Initializes the ")
  112. ("select" . "Chooses the ")
  113. ("send" . "Sends a")
  114. ("re?c\\(v\\|ieves?\\)" . "Receives a ")
  115. ("to" . "Converts ")
  116. ("update" . "Updates the ")
  117. ("wait" . "Waits for ")
  118. ("write" . "Writes to")
  119. )
  120. "List of names to string match against the function name.
  121. This is an alist with each element of the form:
  122. (MATCH . RESULT)
  123. MATCH is a regexp to match in the type field.
  124. RESULT is a string.
  125. Certain prefixes may always mean the same thing, and the same comment
  126. can be used as a beginning for the description. Regexp should be
  127. lower case since the string they are compared to is downcased.
  128. A string may end in a space, in which case, last-alist is searched to
  129. see how best to describe what can be returned.
  130. Doesn't always work correctly, but that is just because English
  131. doesn't always work correctly."
  132. :group 'document
  133. :type '(repeat (cons (string :tag "Regexp")
  134. (string :tag "Doc Text"))))
  135. (defcustom srecode-document-autocomment-common-nouns-abbrevs
  136. '(
  137. ("sock\\(et\\)?" . "socket")
  138. ("addr\\(ess\\)?" . "address")
  139. ("buf\\(f\\(er\\)?\\)?" . "buffer")
  140. ("cur\\(r\\(ent\\)?\\)?" . "current")
  141. ("dev\\(ice\\)?" . "device")
  142. ("file" . "file")
  143. ("line" . "line")
  144. ("msg\\|message" . "message")
  145. ("name" . "name")
  146. ("next\\|nxt" . "next")
  147. ("port" . "port")
  148. ("host" . "host")
  149. ("obj\\|object" . "object")
  150. ("previous\\|prev" . "previous")
  151. ("str\\(ing\\)?" . "string")
  152. ("use?r" . "user")
  153. ("num\\(ber\\)?" . "number")
  154. ("\\(^\\|\\s-\\)id\\($\\|\\s-\\)" . "Identifier") ;complex cause ;common syllable
  155. )
  156. "List of common English abbreviations or full words.
  157. These are nouns (as opposed to verbs) for use in creating expanded
  158. versions of names. This is an alist with each element of the form:
  159. (MATCH . RESULT)
  160. MATCH is a regexp to match in the type field.
  161. RESULT is a string."
  162. :group 'document
  163. :type '(repeat (cons (string :tag "Regexp")
  164. (string :tag "Doc Text"))))
  165. (defcustom srecode-document-autocomment-return-first-alist
  166. '(
  167. ;; Static must be first in the list to provide the intro to the sentence
  168. ("static" . "Locally defined function which ")
  169. ("Bool\\|BOOL" . "Status of ")
  170. )
  171. "List of regexp matches for types.
  172. They provide a little bit of text when typing information is
  173. described.
  174. This is an alist with each element of the form:
  175. (MATCH . RESULT)
  176. MATCH is a regexp to match in the type field.
  177. RESULT is a string."
  178. :group 'document
  179. :type '(repeat (cons (string :tag "Regexp")
  180. (string :tag "Doc Text"))))
  181. (defcustom srecode-document-autocomment-return-last-alist
  182. '(
  183. ("static[ \t\n]+struct \\([a-zA-Z0-9_]+\\)" . "%s")
  184. ("struct \\([a-zA-Z0-9_]+\\)" . "%s")
  185. ("static[ \t\n]+union \\([a-zA-Z0-9_]+\\)" . "%s")
  186. ("union \\([a-zA-Z0-9_]+\\)" . "%s")
  187. ("static[ \t\n]+enum \\([a-zA-Z0-9_]+\\)" . "%s")
  188. ("enum \\([a-zA-Z0-9_]+\\)" . "%s")
  189. ("static[ \t\n]+\\([a-zA-Z0-9_]+\\)" . "%s")
  190. ("\\([a-zA-Z0-9_]+\\)" . "of type %s")
  191. )
  192. "List of regexps which provide the type of the return value.
  193. This is an alist with each element of the form:
  194. (MATCH . RESULT)
  195. MATCH is a regexp to match in the type field.
  196. RESULT is a string, which can contain %s, which is replaced with
  197. `match-string' 1."
  198. :group 'document
  199. :type '(repeat (cons (string :tag "Regexp")
  200. (string :tag "Doc Text"))))
  201. (defcustom srecode-document-autocomment-param-alist
  202. '( ("[Cc]txt" . "Context")
  203. ("[Ii]d" . "Identifier of")
  204. ("[Tt]ype" . "Type of")
  205. ("[Nn]ame" . "Name of")
  206. ("argc" . "Number of arguments")
  207. ("argv" . "Argument vector")
  208. ("envp" . "Environment variable vector")
  209. )
  210. "Alist of common variable names appearing as function parameters.
  211. This is an alist with each element of the form:
  212. (MATCH . RESULT)
  213. MATCH is a regexp to match in the type field.
  214. RESULT is a string of text to use to describe MATCH.
  215. When one is encountered, document-insert-parameters will automatically
  216. place this comment after the parameter name."
  217. :group 'document
  218. :type '(repeat (cons (string :tag "Regexp")
  219. (string :tag "Doc Text"))))
  220. (defcustom srecode-document-autocomment-param-type-alist
  221. '(("const" . "Constant")
  222. ("void" . "Empty")
  223. ("char[ ]*\\*" . "String ")
  224. ("\\*\\*" . "Pointer to ")
  225. ("\\*" . "Pointer ")
  226. ("char[ ]*\\([^ \t*]\\|$\\)" . "Character")
  227. ("int\\|long" . "Number of")
  228. ("FILE" . "File of")
  229. ("float\\|double" . "Value of")
  230. ;; How about some X things?
  231. ("Bool\\|BOOL" . "Flag")
  232. ("Window" . "Window")
  233. ("GC" . "Graphic Context")
  234. ("Widget" . "Widget")
  235. )
  236. "Alist of input parameter types and strings describing them.
  237. This is an alist with each element of the form:
  238. (MATCH . RESULT)
  239. MATCH is a regexp to match in the type field.
  240. RESULT is a string."
  241. :group 'document
  242. :type '(repeat (cons (string :tag "Regexp")
  243. (string :tag "Doc Text"))))
  244. ;;;###autoload
  245. (defun srecode-document-insert-comment ()
  246. "Insert some comments.
  247. Whack any comments that may be in the way and replace them.
  248. If the region is active, then insert group function comments.
  249. If the cursor is in a comment, figure out what kind of comment it is
  250. and replace it.
  251. If the cursor is in a function, insert a function comment.
  252. If the cursor is on a one line prototype, then insert post-fcn comments."
  253. (interactive)
  254. (semantic-fetch-tags)
  255. (let ((ctxt (srecode-calculate-context)))
  256. (if ;; Active region stuff.
  257. (or srecode-handle-region-when-non-active-flag
  258. (eq last-command 'mouse-drag-region)
  259. (and transient-mark-mode mark-active))
  260. (if (> (point) (mark))
  261. (srecode-document-insert-group-comments (mark) (point))
  262. (srecode-document-insert-group-comments (point) (mark)))
  263. ;; ELSE
  264. ;; A declaration comment. Find what it documents.
  265. (when (equal ctxt '("declaration" "comment"))
  266. ;; If we are on a one line tag/comment, go to that fcn.
  267. (if (save-excursion (back-to-indentation)
  268. (semantic-current-tag))
  269. (back-to-indentation)
  270. ;; Else, do we have a fcn following us?
  271. (let ((tag (semantic-find-tag-by-overlay-next)))
  272. (when tag (semantic-go-to-tag tag))))
  273. )
  274. ;; Now analyze the tag we may be on.
  275. (if (semantic-current-tag)
  276. (cond
  277. ;; A one-line variable
  278. ((and (semantic-tag-of-class-p (semantic-current-tag) 'variable)
  279. (srecode-document-one-line-tag-p (semantic-current-tag)))
  280. (srecode-document-insert-variable-one-line-comment))
  281. ;; A plain function
  282. ((semantic-tag-of-class-p (semantic-current-tag) 'function)
  283. (srecode-document-insert-function-comment))
  284. ;; Don't know.
  285. (t
  286. (error "Not sure what to comment"))
  287. )
  288. ;; ELSE, no tag. Perhaps we should just insert a nice section
  289. ;; header??
  290. (let ((title (read-string "Section Title (RET to skip): ")))
  291. (when (and (stringp title) (not (= (length title) 0)))
  292. (srecode-document-insert-section-comment title)))
  293. ))))
  294. (defun srecode-document-insert-section-comment (&optional title)
  295. "Insert a section comment with TITLE."
  296. (interactive "sSection Title: ")
  297. (srecode-load-tables-for-mode major-mode)
  298. (srecode-load-tables-for-mode major-mode 'document)
  299. (if (not (srecode-table))
  300. (error "No template table found for mode %s" major-mode))
  301. (let* ((dict (srecode-create-dictionary))
  302. (temp (srecode-template-get-table (srecode-table)
  303. "section-comment"
  304. "declaration"
  305. 'document)))
  306. (if (not temp)
  307. (error "No templates for inserting section comments"))
  308. (when title
  309. (srecode-dictionary-set-value
  310. dict "TITLE" title))
  311. (srecode-insert-fcn temp dict)
  312. ))
  313. (defun srecode-document-trim-whitespace (str)
  314. "Strip stray whitespace from around STR."
  315. (when (string-match "^\\(\\s-\\|\n\\)+" str)
  316. (setq str (replace-match "" t t str)))
  317. (when (string-match "\\(\\s-\\|\n\\)+$" str)
  318. (setq str (replace-match "" t t str)))
  319. str)
  320. ;;;###autoload
  321. (defun srecode-document-insert-function-comment (&optional fcn-in)
  322. "Insert or replace a function comment.
  323. FCN-IN is the Semantic tag of the function to add a comment too.
  324. If FCN-IN is not provided, the current tag is used instead.
  325. It is assumed that the comment occurs just in front of FCN-IN."
  326. (interactive)
  327. (srecode-load-tables-for-mode major-mode)
  328. (srecode-load-tables-for-mode major-mode 'document)
  329. (if (not (srecode-table))
  330. (error "No template table found for mode %s" major-mode))
  331. (let* ((dict (srecode-create-dictionary))
  332. (temp (srecode-template-get-table (srecode-table)
  333. "function-comment"
  334. "declaration"
  335. 'document)))
  336. (if (not temp)
  337. (error "No templates for inserting function comments"))
  338. ;; Try to figure out the tag we want to use.
  339. (when (not fcn-in)
  340. (semantic-fetch-tags)
  341. (setq fcn-in (semantic-current-tag)))
  342. (when (or (not fcn-in)
  343. (not (semantic-tag-of-class-p fcn-in 'function)))
  344. (error "No tag of class 'function to insert comment for"))
  345. (if (not (eq (current-buffer) (semantic-tag-buffer fcn-in)))
  346. (error "Only insert comments for tags in the current buffer"))
  347. ;; Find any existing doc strings.
  348. (semantic-go-to-tag fcn-in)
  349. (beginning-of-line)
  350. (forward-char -1)
  351. (let ((lextok (semantic-documentation-comment-preceeding-tag fcn-in 'lex))
  352. (doctext
  353. (srecode-document-function-name-comment fcn-in))
  354. )
  355. (when lextok
  356. (let* ((s (semantic-lex-token-start lextok))
  357. (e (semantic-lex-token-end lextok))
  358. (plaintext
  359. (srecode-document-trim-whitespace
  360. (save-excursion
  361. (goto-char s)
  362. (semantic-doc-snarf-comment-for-tag nil))))
  363. (extract (condition-case nil
  364. (srecode-extract temp s e)
  365. (error nil))
  366. )
  367. (distance (count-lines e (semantic-tag-start fcn-in)))
  368. (belongelsewhere (save-excursion
  369. (goto-char s)
  370. (back-to-indentation)
  371. (semantic-current-tag)))
  372. )
  373. (when (not belongelsewhere)
  374. (pulse-momentary-highlight-region s e)
  375. ;; There are many possible states that comment could be in.
  376. ;; Take a guess about what the user would like to do, and ask
  377. ;; the right kind of question.
  378. (when (or (not (> distance 2))
  379. (y-or-n-p "Replace this comment? "))
  380. (when (> distance 2)
  381. (goto-char e)
  382. (delete-horizontal-space)
  383. (delete-blank-lines))
  384. (cond
  385. ((and plaintext (not extract))
  386. (if (y-or-n-p "Convert old-style comment to Template with old text? ")
  387. (setq doctext plaintext))
  388. (delete-region s e)
  389. (goto-char s))
  390. (extract
  391. (when (y-or-n-p "Refresh pre-existing comment (recycle old doc)? ")
  392. (delete-region s e)
  393. (goto-char s)
  394. (setq doctext
  395. (srecode-document-trim-whitespace
  396. (srecode-dictionary-lookup-name extract "DOC")))))
  397. ))
  398. )))
  399. (beginning-of-line)
  400. ;; Perform the insertion
  401. (let ((srecode-semantic-selected-tag fcn-in)
  402. (srecode-semantic-apply-tag-augment-hook
  403. (lambda (tag dict)
  404. (srecode-dictionary-set-value
  405. dict "DOC"
  406. (if (eq tag fcn-in)
  407. doctext
  408. (srecode-document-parameter-comment tag))
  409. )))
  410. )
  411. (srecode-insert-fcn temp dict)
  412. ))
  413. ))
  414. ;;;###autoload
  415. (defun srecode-document-insert-variable-one-line-comment (&optional var-in)
  416. "Insert or replace a variable comment.
  417. VAR-IN is the Semantic tag of the function to add a comment too.
  418. If VAR-IN is not provided, the current tag is used instead.
  419. It is assumed that the comment occurs just after VAR-IN."
  420. (interactive)
  421. (srecode-load-tables-for-mode major-mode)
  422. (srecode-load-tables-for-mode major-mode 'document)
  423. (if (not (srecode-table))
  424. (error "No template table found for mode %s" major-mode))
  425. (let* ((dict (srecode-create-dictionary))
  426. (temp (srecode-template-get-table (srecode-table)
  427. "variable-same-line-comment"
  428. "declaration"
  429. 'document)))
  430. (if (not temp)
  431. (error "No templates for inserting variable comments"))
  432. ;; Try to figure out the tag we want to use.
  433. (when (not var-in)
  434. (semantic-fetch-tags)
  435. (setq var-in (semantic-current-tag)))
  436. (when (or (not var-in)
  437. (not (semantic-tag-of-class-p var-in 'variable)))
  438. (error "No tag of class 'variable to insert comment for"))
  439. (if (not (eq (current-buffer) (semantic-tag-buffer var-in)))
  440. (error "Only insert comments for tags in the current buffer"))
  441. ;; Find any existing doc strings.
  442. (goto-char (semantic-tag-end var-in))
  443. (skip-syntax-forward "-" (point-at-eol))
  444. (let ((lextok (semantic-doc-snarf-comment-for-tag 'lex))
  445. )
  446. (when lextok
  447. (let ((s (semantic-lex-token-start lextok))
  448. (e (semantic-lex-token-end lextok)))
  449. (pulse-momentary-highlight-region s e)
  450. (when (not (y-or-n-p "A comment already exists. Replace? "))
  451. (error "Quit"))
  452. ;; Extract text from the existing comment.
  453. (srecode-extract temp s e)
  454. (delete-region s e)
  455. (goto-char s) ;; To avoid adding a CR.
  456. ))
  457. )
  458. ;; Clean up the end of the line and use handy comment-column.
  459. (end-of-line)
  460. (delete-horizontal-space)
  461. (move-to-column comment-column t)
  462. (when (< (point) (point-at-eol)) (end-of-line))
  463. ;; Perform the insertion
  464. (let ((srecode-semantic-selected-tag var-in)
  465. (srecode-semantic-apply-tag-augment-hook
  466. (lambda (tag dict)
  467. (srecode-dictionary-set-value
  468. dict "DOC" (srecode-document-parameter-comment
  469. tag))))
  470. )
  471. (srecode-insert-fcn temp dict)
  472. ))
  473. )
  474. ;;;###autoload
  475. (defun srecode-document-insert-group-comments (beg end)
  476. "Insert group comments around the active between BEG and END.
  477. If the region includes only parts of some tags, expand out
  478. to the beginning and end of the tags on the region.
  479. If there is only one tag in the region, complain."
  480. (interactive "r")
  481. (srecode-load-tables-for-mode major-mode)
  482. (srecode-load-tables-for-mode major-mode 'document)
  483. (if (not (srecode-table))
  484. (error "No template table found for mode %s" major-mode))
  485. (let* ((dict (srecode-create-dictionary))
  486. (context "declaration")
  487. (temp-start nil)
  488. (temp-end nil)
  489. (tag-start (save-excursion
  490. (goto-char beg)
  491. (or (semantic-current-tag)
  492. (semantic-find-tag-by-overlay-next))))
  493. (tag-end (save-excursion
  494. (goto-char end)
  495. (or (semantic-current-tag)
  496. (semantic-find-tag-by-overlay-prev))))
  497. (parent-tag nil)
  498. (first-pos beg)
  499. (second-pos end)
  500. )
  501. ;; If beg/end wrapped nothing, then tag-start,end would actually
  502. ;; point at some odd stuff that is out of order.
  503. (when (or (not tag-start) (not tag-end)
  504. (> (semantic-tag-end tag-start)
  505. (semantic-tag-start tag-end)))
  506. (setq tag-start nil
  507. tag-end nil))
  508. (when tag-start
  509. ;; If tag-start and -end are the same, and it is a class or
  510. ;; struct, try to find child tags inside the classdecl.
  511. (cond
  512. ((and (eq tag-start tag-end)
  513. tag-start
  514. (semantic-tag-of-class-p tag-start 'type))
  515. (setq parent-tag tag-start)
  516. (setq tag-start (semantic-find-tag-by-overlay-next beg)
  517. tag-end (semantic-find-tag-by-overlay-prev end))
  518. )
  519. ((eq (semantic-find-tag-parent-by-overlay tag-start) tag-end)
  520. (setq parent-tag tag-end)
  521. (setq tag-end (semantic-find-tag-by-overlay-prev end))
  522. )
  523. ((eq tag-start (semantic-find-tag-parent-by-overlay tag-end))
  524. (setq parent-tag tag-start)
  525. (setq tag-start (semantic-find-tag-by-overlay-next beg))
  526. )
  527. )
  528. (when parent-tag
  529. ;; We are probably in a classdecl
  530. ;; @todo -could I really use (srecode-calculate-context) ?
  531. (setq context "classdecl")
  532. )
  533. ;; Derive start and end locations based on the tags.
  534. (setq first-pos (semantic-tag-start tag-start)
  535. second-pos (semantic-tag-end tag-end))
  536. )
  537. ;; Now load the templates
  538. (setq temp-start (srecode-template-get-table (srecode-table)
  539. "group-comment-start"
  540. context
  541. 'document)
  542. temp-end (srecode-template-get-table (srecode-table)
  543. "group-comment-end"
  544. context
  545. 'document))
  546. (when (or (not temp-start) (not temp-end))
  547. (error "No templates for inserting group comments"))
  548. ;; Setup the name of this group ahead of time.
  549. ;; @todo - guess at a name based on common strings
  550. ;; of the tags in the group.
  551. (srecode-dictionary-set-value
  552. dict "GROUPNAME"
  553. (read-string "Name of group: "))
  554. ;; Perform the insertion
  555. ;; Do the end first so we don't need to recalculate anything.
  556. ;;
  557. (goto-char second-pos)
  558. (end-of-line)
  559. (srecode-insert-fcn temp-end dict)
  560. (goto-char first-pos)
  561. (beginning-of-line)
  562. (srecode-insert-fcn temp-start dict)
  563. ))
  564. ;;; Document Generation Functions
  565. ;;
  566. ;; Routines for making up English style comments.
  567. (defun srecode-document-function-name-comment (tag)
  568. "Create documentation for the function defined in TAG.
  569. If we can identify a verb in the list followed by some
  570. name part then check the return value to see if we can use that to
  571. finish off the sentence. That is, any function with 'alloc' in it will be
  572. allocating something based on its type."
  573. (let ((al srecode-document-autocomment-return-first-alist)
  574. (dropit nil)
  575. (tailit nil)
  576. (news "")
  577. (fname (semantic-tag-name tag))
  578. (retval (or (semantic-tag-type tag) "")))
  579. (if (listp retval)
  580. ;; convert a type list into a long string to analyze.
  581. (setq retval (car retval)))
  582. ;; check for modifiers like static
  583. (while al
  584. (if (string-match (car (car al)) (downcase retval))
  585. (progn
  586. (setq news (concat news (cdr (car al))))
  587. (setq dropit t)
  588. (setq al nil)))
  589. (setq al (cdr al)))
  590. ;; check for verb parts!
  591. (setq al srecode-document-autocomment-function-alist)
  592. (while al
  593. (if (string-match (car (car al)) (downcase fname))
  594. (progn
  595. (setq news
  596. (concat news (if dropit (downcase (cdr (car al)))
  597. (cdr (car al)))))
  598. ;; if we end in a space, then we are expecting a potential
  599. ;; return value.
  600. (if (= ? (aref news (1- (length news))))
  601. (setq tailit t))
  602. (setq al nil)))
  603. (setq al (cdr al)))
  604. ;; check for noun parts!
  605. (setq al srecode-document-autocomment-common-nouns-abbrevs)
  606. (while al
  607. (if (string-match (car (car al)) (downcase fname))
  608. (progn
  609. (setq news
  610. (concat news (if dropit (downcase (cdr (car al)))
  611. (cdr (car al)))))
  612. (setq al nil)))
  613. (setq al (cdr al)))
  614. ;; add trailers to names which are obviously returning something.
  615. (if tailit
  616. (progn
  617. (setq al srecode-document-autocomment-return-last-alist)
  618. (while al
  619. (if (string-match (car (car al)) (downcase retval))
  620. (progn
  621. (setq news
  622. (concat news " "
  623. ;; this one may use parts of the return value.
  624. (format (cdr (car al))
  625. (srecode-document-programmer->english
  626. (substring retval (match-beginning 1)
  627. (match-end 1))))))
  628. (setq al nil)))
  629. (setq al (cdr al)))))
  630. news))
  631. (defun srecode-document-parameter-comment (param &optional commentlist)
  632. "Convert tag or string PARAM into a name,comment pair.
  633. Optional COMMENTLIST is list of previously existing comments to
  634. use instead in alist form. If the name doesn't appear in the list of
  635. standard names, then english it instead."
  636. (let ((cmt "")
  637. (aso srecode-document-autocomment-param-alist)
  638. (fnd nil)
  639. (name (if (stringp param) param (semantic-tag-name param)))
  640. (tt (if (stringp param) nil (semantic-tag-type param))))
  641. ;; Make sure the type is a string.
  642. (if (listp tt)
  643. (setq tt (semantic-tag-name tt)))
  644. ;; Find name description parts.
  645. (while aso
  646. (if (string-match (car (car aso)) name)
  647. (progn
  648. (setq fnd t)
  649. (setq cmt (concat cmt (cdr (car aso))))))
  650. (setq aso (cdr aso)))
  651. (if (/= (length cmt) 0)
  652. nil
  653. ;; finally check for array parts
  654. (if (and (not (stringp param)) (semantic-tag-modifiers param))
  655. (setq cmt (concat cmt "array of ")))
  656. (setq aso srecode-document-autocomment-param-type-alist)
  657. (while (and aso tt)
  658. (if (string-match (car (car aso)) tt)
  659. (setq cmt (concat cmt (cdr (car aso)))))
  660. (setq aso (cdr aso))))
  661. ;; Convert from programmer to english.
  662. (if (not fnd)
  663. (setq cmt (concat cmt " "
  664. (srecode-document-programmer->english name))))
  665. cmt))
  666. (defun srecode-document-programmer->english (programmer)
  667. "Take PROGRAMMER and convert it into English.
  668. Works with the following rules:
  669. 1) convert all _ into spaces.
  670. 2) inserts spaces between CamelCasing word breaks.
  671. 3) expands noun names based on common programmer nouns.
  672. This function is designed for variables, not functions. This does
  673. not account for verb parts."
  674. (if (string= "" programmer)
  675. ""
  676. (let ((ind 0) ;index in string
  677. (llow nil) ;lower/upper case flag
  678. (newstr nil) ;new string being generated
  679. (al nil)) ;autocomment list
  680. ;;
  681. ;; 1) Convert underscores
  682. ;;
  683. (while (< ind (length programmer))
  684. (setq newstr (concat newstr
  685. (if (= (aref programmer ind) ?_)
  686. " " (char-to-string (aref programmer ind)))))
  687. (setq ind (1+ ind)))
  688. (setq programmer newstr
  689. newstr nil
  690. ind 0)
  691. ;;
  692. ;; 2) Find word breaks between case changes
  693. ;;
  694. (while (< ind (length programmer))
  695. (setq newstr
  696. (concat newstr
  697. (let ((tc (aref programmer ind)))
  698. (if (and (>= tc ?a) (<= tc ?z))
  699. (progn
  700. (setq llow t)
  701. (char-to-string tc))
  702. (if llow
  703. (progn
  704. (setq llow nil)
  705. (concat " " (char-to-string tc)))
  706. (char-to-string tc))))))
  707. (setq ind (1+ ind)))
  708. ;;
  709. ;; 3) Expand the words if possible
  710. ;;
  711. (setq llow nil
  712. ind 0
  713. programmer newstr
  714. newstr nil)
  715. (while (string-match (concat "^\\s-*\\([^ \t\n]+\\)") programmer)
  716. (let ((ts (substring programmer (match-beginning 1) (match-end 1)))
  717. (end (match-end 1)))
  718. (setq al srecode-document-autocomment-common-nouns-abbrevs)
  719. (setq llow nil)
  720. (while al
  721. (if (string-match (car (car al)) (downcase ts))
  722. (progn
  723. (setq newstr (concat newstr (cdr (car al))))
  724. ;; don't terminate because we may actually have 2 words
  725. ;; next to each other we didn't identify before
  726. (setq llow t)))
  727. (setq al (cdr al)))
  728. (if (not llow) (setq newstr (concat newstr ts)))
  729. (setq newstr (concat newstr " "))
  730. (setq programmer (substring programmer end))))
  731. newstr)))
  732. ;;; UTILS
  733. ;;
  734. (defun srecode-document-one-line-tag-p (tag)
  735. "Does TAG fit on one line with space on the end?"
  736. (save-excursion
  737. (semantic-go-to-tag tag)
  738. (and (<= (semantic-tag-end tag) (point-at-eol))
  739. (goto-char (semantic-tag-end tag))
  740. (< (current-column) 70))))
  741. (provide 'srecode/document)
  742. ;; Local variables:
  743. ;; generated-autoload-file: "loaddefs.el"
  744. ;; generated-autoload-load-name: "srecode/document"
  745. ;; End:
  746. ;;; srecode/document.el ends here