doc.el 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ;;; semantic/doc.el --- Routines for documentation strings
  2. ;; Copyright (C) 1999-2003, 2005, 2008-2017 Free Software Foundation,
  3. ;; Inc.
  4. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  5. ;; Keywords: syntax
  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. ;; It is good practice to write documentation for your functions and
  20. ;; variables. These core routines deal with these documentation
  21. ;; comments or strings. They can exist either as a tag property
  22. ;; (:documentation) or as a comment just before the symbol, or after
  23. ;; the symbol on the same line.
  24. (require 'semantic/tag)
  25. ;;; Code:
  26. ;;;###autoload
  27. (define-overloadable-function semantic-documentation-for-tag (&optional tag nosnarf)
  28. "Find documentation from TAG and return it as a clean string.
  29. TAG might have DOCUMENTATION set in it already. If not, there may be
  30. some documentation in a comment preceding TAG's definition which we
  31. can look for. When appropriate, this can be overridden by a language specific
  32. enhancement.
  33. Optional argument NOSNARF means to only return the lexical analyzer token for it.
  34. If NOSNARF is `lex', then only return the lex token."
  35. (if (not tag) (setq tag (semantic-current-tag)))
  36. (save-excursion
  37. (when (semantic-tag-with-position-p tag)
  38. (set-buffer (semantic-tag-buffer tag)))
  39. (:override
  40. ;; No override. Try something simple to find documentation nearby
  41. (save-excursion
  42. (semantic-go-to-tag tag)
  43. (let ((doctmp (semantic-tag-docstring tag (current-buffer))))
  44. (or
  45. ;; Is there doc in the tag???
  46. doctmp
  47. ;; Check just before the definition.
  48. (when (semantic-tag-with-position-p tag)
  49. (semantic-documentation-comment-preceding-tag tag nosnarf))
  50. ;; Let's look for comments either after the definition, but before code:
  51. ;; Not sure yet. Fill in something clever later....
  52. nil))))))
  53. (defun semantic-documentation-comment-preceding-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. (define-obsolete-function-alias
  76. 'semantic-documentation-comment-preceeding-tag
  77. 'semantic-documentation-comment-preceding-tag
  78. "25.1")
  79. (defun semantic-doc-snarf-comment-for-tag (nosnarf)
  80. "Snarf up the comment at POINT for `semantic-documentation-for-tag'.
  81. Attempt to strip out comment syntactic sugar.
  82. Argument NOSNARF means don't modify the found text.
  83. If NOSNARF is `lex', then return the lex token."
  84. (let* ((semantic-ignore-comments nil)
  85. (semantic-lex-analyzer #'semantic-comment-lexer))
  86. (if (memq nosnarf '(lex flex)) ;; keep `flex' for compatibility
  87. (car (semantic-lex (point) (1+ (point))))
  88. (let ((ct (semantic-lex-token-text
  89. (car (semantic-lex (point) (1+ (point)))))))
  90. (if nosnarf
  91. nil
  92. ;; ok, try to clean the text up.
  93. ;; Comment start thingy
  94. (while (string-match (concat "^\\s-*" comment-start-skip) ct)
  95. (setq ct (concat (substring ct 0 (match-beginning 0))
  96. (substring ct (match-end 0)))))
  97. ;; Arbitrary punctuation at the beginning of each line.
  98. (while (string-match "^\\s-*\\s.+\\s-*" ct)
  99. (setq ct (concat (substring ct 0 (match-beginning 0))
  100. (substring ct (match-end 0)))))
  101. ;; End of a block comment.
  102. (if (and (boundp 'block-comment-end)
  103. block-comment-end
  104. (string-match block-comment-end ct))
  105. (setq ct (concat (substring ct 0 (match-beginning 0))
  106. (substring ct (match-end 0)))))
  107. ;; In case it's a real string, STRIPIT.
  108. (while (string-match "\\s-*\\s\"+\\s-*" ct)
  109. (setq ct (concat (substring ct 0 (match-beginning 0))
  110. (substring ct (match-end 0)))))
  111. ;; Remove comment delimiter at the end of the string.
  112. (when (and comment-end (not (string= comment-end ""))
  113. (string-match (concat (regexp-quote comment-end) "$") ct))
  114. (setq ct (substring ct 0 (match-beginning 0)))))
  115. ;; Now return the text.
  116. ct))))
  117. (provide 'semantic/doc)
  118. ;; Local variables:
  119. ;; generated-autoload-file: "loaddefs.el"
  120. ;; generated-autoload-load-name: "semantic/doc"
  121. ;; End:
  122. ;;; semantic/doc.el ends here