doc.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. ;;; semantic/doc.el --- Routines for documentation strings
  2. ;; Copyright (C) 1999-2003, 2005, 2008-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Keywords: syntax
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; It is good practice to write documentation for your functions and
  19. ;; variables. These core routines deal with these documentation
  20. ;; comments or strings. They can exist either as a tag property
  21. ;; (:documentation) or as a comment just before the symbol, or after
  22. ;; the symbol on the same line.
  23. (require 'semantic/tag)
  24. ;;; Code:
  25. ;;;###autoload
  26. (define-overloadable-function semantic-documentation-for-tag (&optional tag nosnarf)
  27. "Find documentation from TAG and return it as a clean string.
  28. TAG might have DOCUMENTATION set in it already. If not, there may be
  29. some documentation in a comment preceding TAG's definition which we
  30. can look for. When appropriate, this can be overridden by a language specific
  31. enhancement.
  32. Optional argument NOSNARF means to only return the lexical analyzer token for it.
  33. If nosnarf if 'lex, then only return the lex token."
  34. (if (not tag) (setq tag (semantic-current-tag)))
  35. (save-excursion
  36. (when (semantic-tag-with-position-p tag)
  37. (set-buffer (semantic-tag-buffer tag)))
  38. (:override
  39. ;; No override. Try something simple to find documentation nearby
  40. (save-excursion
  41. (semantic-go-to-tag tag)
  42. (let ((doctmp (semantic-tag-docstring tag (current-buffer))))
  43. (or
  44. ;; Is there doc in the tag???
  45. doctmp
  46. ;; Check just before the definition.
  47. (when (semantic-tag-with-position-p tag)
  48. (semantic-documentation-comment-preceeding-tag tag nosnarf))
  49. ;; Let's look for comments either after the definition, but before code:
  50. ;; Not sure yet. Fill in something clever later....
  51. nil))))))
  52. ;; FIXME this is not how you spell "preceding".
  53. (defun semantic-documentation-comment-preceeding-tag (&optional tag nosnarf)
  54. "Find a comment preceding TAG.
  55. If TAG is nil. use the tag under point.
  56. Searches the space between TAG and the preceding tag for a comment,
  57. and converts the comment into clean documentation.
  58. Optional argument NOSNARF with a value of 'lex means to return
  59. just the lexical token and not the string."
  60. (if (not tag) (setq tag (semantic-current-tag)))
  61. (save-excursion
  62. ;; Find this tag.
  63. (semantic-go-to-tag tag)
  64. (let* ((starttag (semantic-find-tag-by-overlay-prev
  65. (semantic-tag-start tag)))
  66. (start (if starttag
  67. (semantic-tag-end starttag)
  68. (point-min))))
  69. (when (and comment-start-skip
  70. (re-search-backward comment-start-skip start t))
  71. ;; We found a comment that doesn't belong to the body
  72. ;; of a function.
  73. (semantic-doc-snarf-comment-for-tag nosnarf)))
  74. ))
  75. (defun semantic-doc-snarf-comment-for-tag (nosnarf)
  76. "Snarf up the comment at POINT for `semantic-documentation-for-tag'.
  77. Attempt to strip out comment syntactic sugar.
  78. Argument NOSNARF means don't modify the found text.
  79. If NOSNARF is 'lex, then return the lex token."
  80. (let* ((semantic-ignore-comments nil)
  81. (semantic-lex-analyzer #'semantic-comment-lexer))
  82. (if (memq nosnarf '(lex flex)) ;; keep `flex' for compatibility
  83. (car (semantic-lex (point) (1+ (point))))
  84. (let ((ct (semantic-lex-token-text
  85. (car (semantic-lex (point) (1+ (point)))))))
  86. (if nosnarf
  87. nil
  88. ;; ok, try to clean the text up.
  89. ;; Comment start thingy
  90. (while (string-match (concat "^\\s-*" comment-start-skip) ct)
  91. (setq ct (concat (substring ct 0 (match-beginning 0))
  92. (substring ct (match-end 0)))))
  93. ;; Arbitrary punctuation at the beginning of each line.
  94. (while (string-match "^\\s-*\\s.+\\s-*" ct)
  95. (setq ct (concat (substring ct 0 (match-beginning 0))
  96. (substring ct (match-end 0)))))
  97. ;; End of a block comment.
  98. (if (and (boundp 'block-comment-end)
  99. block-comment-end
  100. (string-match block-comment-end ct))
  101. (setq ct (concat (substring ct 0 (match-beginning 0))
  102. (substring ct (match-end 0)))))
  103. ;; In case it's a real string, STRIPIT.
  104. (while (string-match "\\s-*\\s\"+\\s-*" ct)
  105. (setq ct (concat (substring ct 0 (match-beginning 0))
  106. (substring ct (match-end 0))))))
  107. ;; Now return the text.
  108. ct))))
  109. (provide 'semantic/doc)
  110. ;; Local variables:
  111. ;; generated-autoload-file: "loaddefs.el"
  112. ;; generated-autoload-load-name: "semantic/doc"
  113. ;; End:
  114. ;;; semantic/doc.el ends here