expandproto.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ;;; srecode/expandproto.el --- Expanding prototypes.
  2. ;; Copyright (C) 2007, 2009-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. ;; Methods for expanding a prototype into an implementation.
  18. (require 'ring)
  19. (require 'semantic)
  20. (require 'semantic/analyze)
  21. (require 'semantic/senator)
  22. (require 'srecode/insert)
  23. (require 'srecode/dictionary)
  24. (declare-function semantic-brute-find-tag-by-attribute-value "semantic/find")
  25. ;;; Code:
  26. (defcustom srecode-expandproto-template-file-alist
  27. '( ( c++-mode . "srecode-expandproto-cpp.srt" )
  28. )
  29. ;; @todo - Make this variable auto-generated from the Makefile.
  30. "Associate template files for expanding prototypes to a major mode."
  31. :group 'srecode
  32. :type '(repeat (cons (sexp :tag "Mode")
  33. (sexp :tag "Filename"))
  34. ))
  35. ;;;###autoload
  36. (defun srecode-insert-prototype-expansion ()
  37. "Insert get/set methods for the current class."
  38. (interactive)
  39. (srecode-load-tables-for-mode major-mode)
  40. (srecode-load-tables-for-mode major-mode
  41. srecode-expandproto-template-file-alist)
  42. (if (not (srecode-table))
  43. (error "No template table found for mode %s" major-mode))
  44. (let ((proto
  45. ;; Step 1: Find the prototype, or prototype list to expand.
  46. (srecode-find-prototype-for-expansion)))
  47. (if (not proto)
  48. (error "Could not find prototype to expand"))
  49. ;; Step 2: Insert implementations of the prototypes.
  50. ))
  51. (defun srecode-find-prototype-for-expansion ()
  52. "Find a prototype to use for expanding into an implementation."
  53. ;; We may find a prototype tag in one of several places.
  54. ;; Search in order of logical priority.
  55. (let ((proto nil)
  56. )
  57. ;; 1) A class full of prototypes under point.
  58. (let ((tag (semantic-current-tag)))
  59. (when tag
  60. (when (not (semantic-tag-of-class-p tag 'type))
  61. (setq tag (semantic-current-tag-parent))))
  62. (when (and tag (semantic-tag-of-class-p tag 'type))
  63. ;; If the current class has prototype members, then
  64. ;; we will do the whole class!
  65. (require 'semantic/find)
  66. (if (semantic-brute-find-tag-by-attribute-value
  67. :prototype t
  68. (semantic-tag-type-members tag))
  69. (setq proto tag)))
  70. )
  71. ;; 2) A prototype under point.
  72. (when (not proto)
  73. (let ((tag (semantic-current-tag)))
  74. (when (and tag
  75. (and
  76. (semantic-tag-of-class-p tag 'function)
  77. (semantic-tag-get-attribute tag :prototype)))
  78. (setq proto tag))))
  79. ;; 3) A tag in the kill ring that is a prototype
  80. (when (not proto)
  81. (if (ring-empty-p senator-tag-ring)
  82. nil ;; Not for us.
  83. (let ((tag (ring-ref senator-tag-ring 0))
  84. )
  85. (when
  86. (and tag
  87. (or
  88. (and
  89. (semantic-tag-of-class-p tag 'function)
  90. (semantic-tag-get-attribute tag :prototype))
  91. (and
  92. (semantic-tag-of-class-p tag 'type)
  93. (require 'semantic/find)
  94. (semantic-brute-find-tag-by-attribute-value
  95. :prototype t
  96. (semantic-tag-type-members tag))))
  97. )
  98. (setq proto tag))
  99. )))
  100. proto))
  101. (provide 'srecode/expandproto)
  102. ;; Local variables:
  103. ;; generated-autoload-file: "loaddefs.el"
  104. ;; generated-autoload-load-name: "srecode/expandproto"
  105. ;; End:
  106. ;;; srecode/expandproto.el ends here