tag-write.el 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ;;; semantic/tag-write.el --- Write tags to a text stream
  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. ;; Routine for writing out a list of tags to a text stream.
  18. ;;
  19. ;; These routines will be used by semanticdb to output a tag list into
  20. ;; a text stream to be saved to a file. Ideally, you could use tag streams
  21. ;; to share tags between processes as well.
  22. ;;
  23. ;; As a bonus, these routines will also validate the tag structure, and make sure
  24. ;; that they conform to good semantic tag hygiene.
  25. ;;
  26. (require 'semantic)
  27. ;;; Code:
  28. (defun semantic-tag-write-one-tag (tag &optional indent)
  29. "Write a single tag TAG to standard out.
  30. INDENT is the amount of indentation to use for this tag."
  31. (when (not (semantic-tag-p tag))
  32. (signal 'wrong-type-argument (list tag 'semantic-tag-p)))
  33. (when (not indent) (setq indent 0))
  34. ;(princ (make-string indent ? ))
  35. (princ "(\"")
  36. ;; Base parts
  37. (let ((name (semantic-tag-name tag))
  38. (class (semantic-tag-class tag)))
  39. (princ name)
  40. (princ "\" ")
  41. (princ (symbol-name class))
  42. )
  43. (let ((attr (semantic-tag-attributes tag))
  44. )
  45. ;; Attributes
  46. (cond ((not attr)
  47. (princ " nil"))
  48. ((= (length attr) 2) ;; One item
  49. (princ " (")
  50. (semantic-tag-write-one-attribute attr indent)
  51. (princ ")")
  52. )
  53. (t
  54. ;; More than one tag.
  55. (princ "\n")
  56. (princ (make-string (+ indent 3) ? ))
  57. (princ "(")
  58. (while attr
  59. (semantic-tag-write-one-attribute attr (+ indent 4))
  60. (setq attr (cdr (cdr attr)))
  61. (when attr
  62. (princ "\n")
  63. (princ (make-string (+ indent 4) ? )))
  64. )
  65. (princ ")\n")
  66. (princ (make-string (+ indent 3) ? ))
  67. ))
  68. ;; Properties - for now, always nil.
  69. (let ((rs (semantic--tag-get-property tag 'reparse-symbol)))
  70. (if (not rs)
  71. (princ " nil")
  72. ;; Else, put in the property list.
  73. (princ " (reparse-symbol ")
  74. (princ (symbol-name rs))
  75. (princ ")"))
  76. ))
  77. ;; Overlay
  78. (if (semantic-tag-with-position-p tag)
  79. (let ((bounds (semantic-tag-bounds tag)))
  80. (princ " ")
  81. (prin1 (apply 'vector bounds))
  82. )
  83. (princ " nil"))
  84. ;; End it.
  85. (princ ")")
  86. )
  87. (defun semantic-tag-write-tag-list (tlist &optional indent dontaddnewline)
  88. "Write the tag list TLIST to the current stream.
  89. INDENT indicates the current indentation level.
  90. If optional DONTADDNEWLINE is non-nil, then don't add a newline."
  91. (if (not indent)
  92. (setq indent 0)
  93. (unless dontaddnewline
  94. ;; Assume cursor at end of current line. Add a CR, and make the list.
  95. (princ "\n")
  96. (princ (make-string indent ? ))))
  97. (princ "( ")
  98. (while tlist
  99. (if (semantic-tag-p (car tlist))
  100. (semantic-tag-write-one-tag (car tlist) (+ indent 2))
  101. ;; If we don't have a tag in the tag list, use the below hack, and hope
  102. ;; it doesn't contain anything bad. If we find something bad, go back here
  103. ;; and start extending what's expected here.
  104. (princ (format "%S" (car tlist))))
  105. (setq tlist (cdr tlist))
  106. (when tlist
  107. (princ "\n")
  108. (princ (make-string (+ indent 2) ? )))
  109. )
  110. (princ ")")
  111. (princ (make-string indent ? ))
  112. )
  113. ;; Writing out random stuff.
  114. (defun semantic-tag-write-one-attribute (attrs indent)
  115. "Write out one attribute from the head of the list of attributes ATTRS.
  116. INDENT is the current amount of indentation."
  117. (when (not attrs) (signal 'wrong-type-argument (list 'listp attrs)))
  118. (when (not (symbolp (car attrs))) (error "Bad Attribute List in tag"))
  119. (princ (symbol-name (car attrs)))
  120. (princ " ")
  121. (semantic-tag-write-one-value (car (cdr attrs)) indent)
  122. )
  123. (defun semantic-tag-write-one-value (value indent)
  124. "Write out a VALUE for something in a tag.
  125. INDENT is the current tag indentation.
  126. Items that are long lists of tags may need their own line."
  127. (cond
  128. ;; Another tag.
  129. ((semantic-tag-p value)
  130. (semantic-tag-write-one-tag value (+ indent 2)))
  131. ;; A list of more tags
  132. ((and (listp value) (semantic-tag-p (car value)))
  133. (semantic-tag-write-tag-list value (+ indent 2))
  134. )
  135. ;; Some arbitrary data.
  136. (t
  137. (let ((str (format "%S" value)))
  138. ;; Protect against odd data types in tags.
  139. (if (= (aref str 0) ?#)
  140. (progn
  141. (princ "nil")
  142. (message "Warning: Value %s not writable in tag." str))
  143. (princ str)))))
  144. )
  145. ;;; EIEIO USAGE
  146. ;;;###autoload
  147. (defun semantic-tag-write-list-slot-value (value)
  148. "Write out the VALUE of a slot for EIEIO.
  149. The VALUE is a list of tags."
  150. (if (not value)
  151. (princ "nil")
  152. (princ "\n '")
  153. (semantic-tag-write-tag-list value 10 t)
  154. ))
  155. (provide 'semantic/tag-write)
  156. ;; Local variables:
  157. ;; generated-autoload-file: "loaddefs.el"
  158. ;; generated-autoload-load-name: "semantic/tag-write"
  159. ;; End:
  160. ;;; semantic/tag-write.el ends here