1 ;;; semantic/decorate/include.el --- Decoration modes for include statements
3 ;; Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; Highlight any include that is in a state the user may care about.
25 ;; The basic idea is to have the state be highly visible so users will
26 ;; as 'what is this?" and get the info they need to fix problems that
27 ;; are otherwise transparent when trying to get smart completion
30 (require 'semantic
/decorate
/mode
)
31 (require 'semantic
/db
)
32 (require 'semantic
/db-ref
)
33 (require 'semantic
/db-find
)
36 (require 'semantic
/find
))
38 (defvar semantic-dependency-system-include-path
)
39 (declare-function ede-get-locator-object
"ede/files")
40 (declare-function ede-system-include-path
"ede/cpp-root")
45 (defvar semantic-decoratiton-mouse-3
(if (featurep 'xemacs
) [ button3
] [ mouse-3
])
46 "The keybinding lisp object to use for binding the right mouse button.")
48 ;;; Includes that that are in a happy state!
50 (defface semantic-decoration-on-includes
52 "*Overlay Face used on includes that are not in some other state.
53 Used by the decoration style: `semantic-decoration-on-includes'."
54 :group
'semantic-faces
)
56 (defvar semantic-decoration-on-include-map
57 (let ((km (make-sparse-keymap)))
58 (define-key km semantic-decoratiton-mouse-3
'semantic-decoration-include-menu
)
60 "Keymap used on includes.")
63 (defvar semantic-decoration-on-include-menu nil
64 "Menu used for include headers.")
67 semantic-decoration-on-include-menu
68 semantic-decoration-on-include-map
73 ["What Is This?" semantic-decoration-include-describe
75 :help
"Describe why this include has been marked this way." ])
77 ["Visit This Include" semantic-decoration-include-visit
79 :help
"Visit this include file." ])
82 ["Summarize includes current buffer" semantic-decoration-all-include-summary
84 :help
"Show a summary for the current buffer containing this include." ])
86 ["List found includes (load unparsed)" semanticdb-find-test-translate-path
88 :help
"List all includes found for this file, and parse unparsed files." ])
90 ["List found includes (no loading)" semanticdb-find-test-translate-path-no-loading
92 :help
"List all includes found for this file, do not parse unparsed files." ])
94 ["List all unknown includes" semanticdb-find-adebug-lost-includes
96 :help
"Show a list of all includes semantic cannot find for this file." ])
99 ["Customize System Include Path" semantic-customize-system-include-path
100 :active
(get 'semantic-dependency-system-include-path major-mode
)
101 :help
"Run customize for the system include path for this major mode." ])
103 ["Add a System Include Path" semantic-add-system-include
105 :help
"Add an include path for this session." ])
107 ["Remove a System Include Path" semantic-remove-system-include
109 :help
"Add an include path for this session." ])
110 ;;["" semantic-decoration-include-
115 ;;; Unknown Includes!
117 (defface semantic-decoration-on-unknown-includes
118 '((((class color
) (background dark
))
119 (:background
"#900000"))
120 (((class color
) (background light
))
121 (:background
"#fff0f0")))
122 "*Face used to show includes that cannot be found.
123 Used by the decoration style: `semantic-decoration-on-unknown-includes'."
124 :group
'semantic-faces
)
126 (defvar semantic-decoration-on-unknown-include-map
127 (let ((km (make-sparse-keymap)))
128 ;(define-key km [ mouse-2 ] 'semantic-decoration-unknown-include-describe)
129 (define-key km semantic-decoratiton-mouse-3
'semantic-decoration-unknown-include-menu
)
131 "Keymap used on unparsed includes.")
133 (defvar semantic-decoration-on-unknown-include-menu nil
134 "Menu used for unparsed include headers.")
137 semantic-decoration-on-unknown-include-menu
138 semantic-decoration-on-unknown-include-map
139 "Unknown Include Menu"
143 ["What Is This?" semantic-decoration-unknown-include-describe
145 :help
"Describe why this include has been marked this way." ])
147 ["List all unknown includes" semanticdb-find-adebug-lost-includes
149 :help
"Show a list of all includes semantic cannot find for this file." ])
152 ["Summarize includes current buffer" semantic-decoration-all-include-summary
154 :help
"Show a summary for the current buffer containing this include." ])
156 ["List found includes (load unparsed)" semanticdb-find-test-translate-path
158 :help
"List all includes found for this file, and parse unparsed files." ])
160 ["List found includes (no loading)" semanticdb-find-test-translate-path-no-loading
162 :help
"List all includes found for this file, do not parse unparsed files." ])
165 ["Customize System Include Path" semantic-customize-system-include-path
166 :active
(get 'semantic-dependency-system-include-path major-mode
)
167 :help
"Run customize for the system include path for this major mode." ])
169 ["Add a System Include Path" semantic-add-system-include
171 :help
"Add an include path for this session." ])
173 ["Remove a System Include Path" semantic-remove-system-include
175 :help
"Add an include path for this session." ])
178 ;;; Includes that need to be parsed.
180 (defface semantic-decoration-on-unparsed-includes
181 '((((class color
) (background dark
))
182 (:background
"#555500"))
183 (((class color
) (background light
))
184 (:background
"#ffff55")))
185 "*Face used to show includes that have not yet been parsed.
186 Used by the decoration style: `semantic-decoration-on-unparsed-includes'."
187 :group
'semantic-faces
)
189 (defvar semantic-decoration-on-unparsed-include-map
190 (let ((km (make-sparse-keymap)))
191 (define-key km semantic-decoratiton-mouse-3
'semantic-decoration-unparsed-include-menu
)
193 "Keymap used on unparsed includes.")
196 (defvar semantic-decoration-on-unparsed-include-menu nil
197 "Menu used for unparsed include headers.")
200 semantic-decoration-on-unparsed-include-menu
201 semantic-decoration-on-unparsed-include-map
202 "Unparsed Include Menu"
206 ["What Is This?" semantic-decoration-unparsed-include-describe
208 :help
"Describe why this include has been marked this way." ])
210 ["Visit This Include" semantic-decoration-include-visit
212 :help
"Visit this include file so that header file's tags can be used." ])
214 ["Parse This Include" semantic-decoration-unparsed-include-parse-include
216 :help
"Parse this include file so that header file's tags can be used." ])
218 ["Parse All Includes" semantic-decoration-unparsed-include-parse-all-includes
220 :help
"Parse all the includes so the contents can be used." ])
223 ["Summarize includes current buffer" semantic-decoration-all-include-summary
225 :help
"Show a summary for the current buffer containing this include." ])
227 ["List found includes (load unparsed)" semanticdb-find-test-translate-path
229 :help
"List all includes found for this file, and parse unparsed files." ])
231 ["List found includes (no loading)" semanticdb-find-test-translate-path-no-loading
233 :help
"List all includes found for this file, do not parse unparsed files." ])
235 ["List all unknown includes" semanticdb-find-adebug-lost-includes
237 :help
"Show a list of all includes semantic cannot find for this file." ])
240 ["Customize System Include Path" semantic-customize-system-include-path
241 :active
(get 'semantic-dependency-system-include-path major-mode
)
242 :help
"Run customize for the system include path for this major mode." ])
244 ["Add a System Include Path" semantic-add-system-include
246 :help
"Add an include path for this session." ])
248 ["Remove a System Include Path" semantic-remove-system-include
250 :help
"Add an include path for this session." ])
251 ;;["" semantic-decoration-unparsed-include-
259 ;;; Include statement Decorate Mode
261 ;; This mode handles the three states of an include statements
263 (define-semantic-decoration-style semantic-decoration-on-includes
264 "Highlight class members that are includes.
265 This mode provides a nice context menu on the include statements."
268 (defun semantic-decoration-on-includes-p-default (tag)
269 "Return non-nil if TAG has is an includes that can't be found."
270 (semantic-tag-of-class-p tag
'include
))
272 (defun semantic-decoration-on-includes-highlight-default (tag)
273 "Highlight the include TAG to show that semantic can't find it."
274 (let* ((file (semantic-dependency-tag-file tag
))
276 (semanticdb-file-table-object file t
)))
282 ;; Cannot find this header.
283 (setq face
'semantic-decoration-on-unknown-includes
284 map semantic-decoration-on-unknown-include-map
)
286 ((and table
(number-or-marker-p (oref table pointmax
)))
287 ;; A found and parsed file.
288 (setq face
'semantic-decoration-on-includes
289 map semantic-decoration-on-include-map
)
293 (setq face
'semantic-decoration-on-unparsed-includes
294 map semantic-decoration-on-unparsed-include-map
)
296 ;; Set ourselves up for synchronization
297 (semanticdb-cache-get
298 table
'semantic-decoration-unparsed-include-cache
)
300 (let ((table semanticdb-current-table
))
301 (semanticdb-add-reference table tag
))
305 ;; @TODO - if not a tag w/ a position, we need to get one. How?
307 (when (semantic-tag-with-position-p tag
)
308 (let ((ol (semantic-decorate-tag tag
309 (semantic-tag-start tag
)
310 (semantic-tag-end tag
)
313 (semantic-overlay-put ol
'mouse-face
'highlight
)
314 (semantic-overlay-put ol
'keymap map
)
315 (semantic-overlay-put ol
'help-echo
316 "Header File : mouse-3 - Context menu")
319 ;;; Regular Include Functions
321 (defun semantic-decoration-include-describe ()
322 "Describe what unparsed includes are in the current buffer.
323 Argument EVENT is the mouse clicked event."
325 (let* ((tag (or (semantic-current-tag)
326 (error "No tag under point")))
327 (file (semantic-dependency-tag-file tag
))
329 (semanticdb-file-table-object file t
))))
330 (with-output-to-temp-buffer (help-buffer) ; "*Help*"
331 (help-setup-xref (list #'semantic-decoration-include-describe
)
332 (called-interactively-p 'interactive
))
333 (princ "Include File: ")
334 (princ (semantic-format-tag-name tag nil t
))
336 (princ "This include file was found at:\n ")
337 (princ (semantic-dependency-tag-file tag
))
339 (princ "Semantic knows where this include file is, and has parsed
343 (let ((inc (semantic-find-tags-by-class 'include table
))
349 (let* ((fileinner (semantic-dependency-tag-file i
))
351 (cond ((not fileinner
)
352 (setq unknown
(1+ unknown
)))
353 ((number-or-marker-p (oref table pointmax
))
356 (setq unparsed
(1+ unparsed
))))))
357 (setq all
(+ ok unknown unparsed
))
359 (princ "There are no other includes in this file.\n")
360 (princ (format "There are %d more includes in this file.\n"
362 (princ (format " Unknown Includes: %d\n" unknown
))
363 (princ (format " Unparsed Includes: %d\n" unparsed
))
364 (princ (format " Parsed Includes: %d\n" ok
)))
366 ;; Get the semanticdb statement, and display it's contents.
367 (princ "\nDetails for header file...\n")
368 (princ "\nMajor Mode: ")
369 (princ (oref table
:major-mode
))
371 (princ (format "%s entries" (length (oref table
:tags
))))
372 (princ "\nFile Size: ")
373 (princ (format "%s chars" (oref table
:pointmax
)))
374 (princ "\nSave State: ")
375 (cond ((oref table dirty
)
376 (princ "Table needs to be saved."))
378 (princ "Table is saved on disk."))
380 (princ "\nExternal References:")
381 (dolist (r (oref table db-refs
))
383 (princ (oref r file
)))
387 (defun semantic-decoration-include-visit ()
388 "Visit the included file at point."
390 (let ((tag (semantic-current-tag)))
391 (unless (eq (semantic-tag-class tag
) 'include
)
392 (error "Point is not on an include tag"))
393 (let ((file (semantic-dependency-tag-file tag
)))
395 ((or (not file
) (not (file-exists-p file
)))
396 (error "Could not location include %s"
397 (semantic-tag-name tag
)))
398 ((get-file-buffer file
)
399 (switch-to-buffer (get-file-buffer file
)))
404 (defun semantic-decoration-include-menu (event)
405 "Popup a menu that can help a user understand unparsed includes.
406 Argument EVENT describes the event that caused this function to be called."
408 (let* ((startwin (selected-window))
409 (win (semantic-event-window event
))
411 (select-window win t
)
413 ;(goto-char (window-start win))
414 (mouse-set-point event
)
416 (semantic-popup-menu semantic-decoration-on-include-menu
)
418 (select-window startwin
)))
421 ;;; Unknown Include functions
423 (defun semantic-decoration-unknown-include-describe ()
424 "Describe what unknown includes are in the current buffer.
425 Argument EVENT is the mouse clicked event."
427 (let ((tag (semantic-current-tag))
429 (with-output-to-temp-buffer (help-buffer) ; "*Help*"
430 (help-setup-xref (list #'semantic-decoration-unknown-include-describe
)
431 (called-interactively-p 'interactive
))
432 (princ "Include File: ")
433 (princ (semantic-format-tag-name tag nil t
))
435 (princ "This header file has been marked \"Unknown\".
436 This means that Semantic has not been able to locate this file on disk.
438 When Semantic cannot find an include file, this means that the
439 idle summary mode and idle completion modes cannot use the contents of
440 that file to provide coding assistance.
442 If this is a system header and you want it excluded from Semantic's
443 searches (which may be desirable for speed reasons) then you can
444 safely ignore this state.
446 If this is a system header, and you want to include it in Semantic's
447 searches, then you will need to use:
449 M-x semantic-add-system-include RET /path/to/includes RET
451 or, in your .emacs file do:
453 (semantic-add-system-include \"/path/to/include\" '")
454 (princ (symbol-name mm
))
457 to add the path to Semantic's search.
459 If this is an include file that belongs to your project, then you may
460 need to update `semanticdb-project-roots' or better yet, use `ede'
461 to manage your project. See the ede manual for projects that will
462 wrap existing project code for Semantic's benifit.
465 (when (or (eq mm
'c
++-mode
) (eq mm
'c-mode
))
467 For C/C++ includes located within a project, you can use a special
468 EDE project that will wrap an existing build system. You can do that
469 like this in your .emacs file:
471 (ede-cpp-root-project \"NAME\" :file \"FILENAME\" :locate-fcn 'MYFCN)
473 See the CEDET manual, the EDE manual, or the commentary in
474 ede-cpp-root.el for more.
476 If you think this header tag is marked in error, you may need to do:
480 to refresh the tags in this buffer, and recalculate the state."))
483 See the Semantic manual node on SemanticDB for more about search paths.")
486 (defun semantic-decoration-unknown-include-menu (event)
487 "Popup a menu that can help a user understand unparsed includes.
488 Argument EVENT describes the event that caused this function to be called."
490 (let* ((startwin (selected-window))
491 ;; This line has an issue in XEmacs.
492 (win (semantic-event-window event
))
494 (select-window win t
)
496 ;(goto-char (window-start win))
497 (mouse-set-point event
)
499 (semantic-popup-menu semantic-decoration-on-unknown-include-menu
)
501 (select-window startwin
)))
504 ;;; Interactive parts of unparsed includes
506 (defun semantic-decoration-unparsed-include-describe ()
507 "Describe what unparsed includes are in the current buffer.
508 Argument EVENT is the mouse clicked event."
510 (let ((tag (semantic-current-tag)))
511 (with-output-to-temp-buffer (help-buffer); "*Help*"
512 (help-setup-xref (list #'semantic-decoration-unparsed-include-describe
)
513 (called-interactively-p 'interactive
))
515 (princ "Include File: ")
516 (princ (semantic-format-tag-name tag nil t
))
518 (princ "This include file was found at:\n ")
519 (princ (semantic-dependency-tag-file tag
))
521 (princ "This header file has been marked \"Unparsed\".
522 This means that Semantic has located this header file on disk
523 but has not yet opened and parsed this file.
525 So long as this header file is unparsed, idle summary and
526 idle completion will not be able to reference the details in this
529 To resolve this, use the context menu to parse this include file,
530 or all include files referred to in ")
531 (princ (buffer-name))
533 This can take a while in large projects.
535 Alternately, you can call:
537 M-x semanticdb-find-test-translate-path RET
539 to search path Semantic uses to perform completion.
542 If you think this header tag is marked in error, you may need to do:
546 to refresh the tags in this buffer, and recalculate the state.
547 If you find a repeatable case where a header is marked in error,
548 report it to cedet-devel@lists.sf.net.") )))
551 (defun semantic-decoration-unparsed-include-menu (event)
552 "Popup a menu that can help a user understand unparsed includes.
553 Argument EVENT describes the event that caused this function to be called."
555 (let* ((startwin (selected-window))
556 (win (semantic-event-window event
))
558 (select-window win t
)
560 ;(goto-char (window-start win))
561 (mouse-set-point event
)
563 (semantic-popup-menu semantic-decoration-on-unparsed-include-menu
)
565 (select-window startwin
)))
567 (defun semantic-decoration-unparsed-include-parse-include ()
568 "Parse the include file the user menu-selected from."
570 (let* ((file (semantic-dependency-tag-file (semantic-current-tag))))
571 (semanticdb-file-table-object file
)
572 (semantic-decoration-unparsed-include-do-reset)))
575 (defun semantic-decoration-unparsed-include-parse-all-includes ()
576 "Parse the include file the user menu-selected from."
578 (semanticdb-find-translate-path nil nil
)
582 ;;; General Includes Information
584 (defun semantic-decoration-all-include-summary ()
585 "Provide a general summary for the state of all includes."
587 (require 'semantic
/dep
)
588 (let* ((table semanticdb-current-table
)
589 (tags (semantic-fetch-tags))
590 (inc (semantic-find-tags-by-class 'include table
))
592 (with-output-to-temp-buffer (help-buffer) ;"*Help*"
593 (help-setup-xref (list #'semantic-decoration-all-include-summary
)
594 (called-interactively-p 'interactive
))
596 (princ "Include Summary for File: ")
597 (princ (file-truename (buffer-file-name)))
600 (when (oref table db-refs
)
601 (princ "\nExternal Database References to this buffer:")
602 (dolist (r (oref table db-refs
))
604 (princ (oref r file
)))
607 (princ (format "\nThis file contains %d tags, %d of which are includes.\n"
608 (length tags
) (length inc
)))
614 (let* ((fileinner (semantic-dependency-tag-file i
))
615 (tableinner (when fileinner
616 (semanticdb-file-table-object fileinner t
))))
617 (cond ((not fileinner
)
618 (setq unknown
(1+ unknown
)))
619 ((number-or-marker-p (oref tableinner pointmax
))
622 (setq unparsed
(1+ unparsed
))))))
623 (setq all
(+ ok unknown unparsed
))
624 (when (not (= 0 all
))
625 (princ (format " Unknown Includes: %d\n" unknown
))
626 (princ (format " Unparsed Includes: %d\n" unparsed
))
627 (princ (format " Parsed Includes: %d\n" ok
)))
630 (princ "\nInclude Path Summary:\n\n")
631 (when (and (boundp 'ede-object
)
632 (boundp 'ede-object-project
)
634 (princ " This file's project include search is handled by the EDE object:\n")
635 (princ " Buffer Target: ")
636 (princ (object-print ede-object
))
638 (when (not (eq ede-object ede-object-project
))
639 (princ " Buffer Project: ")
640 (princ (object-print ede-object-project
))
643 (when ede-object-project
644 (let ((loc (ede-get-locator-object ede-object-project
)))
645 (princ " Backup in-project Locator: ")
646 (princ (object-print loc
))
648 (let ((syspath (ede-system-include-path ede-object-project
)))
650 (princ " EDE Project system include path: Empty\n")
651 (princ " EDE Project system include path:\n")
652 (dolist (dir syspath
)
658 (princ "\n This file's system include path is:\n")
659 (dolist (dir semantic-dependency-system-include-path
)
664 (let ((unk semanticdb-find-lost-includes
))
666 (princ "\nAll unknown includes:\n")
669 (princ (semantic-tag-name tag
))
673 (let* ((semanticdb-find-default-throttle
674 (if (featurep 'semantic
/db-find
)
675 (remq 'unloaded semanticdb-find-default-throttle
)
677 (path (semanticdb-find-translate-path nil nil
)))
678 (if (<= (length path
) (length inc
))
679 (princ "\nThere are currently no includes found recursively.\n")
680 ;; List the full include list.
681 (princ "\nSummary of all includes needed by ")
682 (princ (buffer-name))
684 (if (slot-boundp p
'tags
)
685 (princ (format "\n %s :\t%d tags, %d are includes. %s"
686 (object-name-string p
)
687 (length (oref p tags
))
688 (length (semantic-find-tags-by-class
695 ((not (number-or-marker-p (oref table pointmax
)))
696 " Needs to be parsed.")
698 (princ (format "\n %s :\tUnparsed"
699 (object-name-string p
))))
704 ;;; Unparsed Include Features
706 ;; This section handles changing states of unparsed include
707 ;; decorations base on what happens in other files.
710 (defclass semantic-decoration-unparsed-include-cache
(semanticdb-abstract-cache)
712 "Class used to reset decorated includes.
713 When an include's referring file is parsed, we need to undecorate
714 any decorated referring includes.")
717 (defmethod semantic-reset ((obj semantic-decoration-unparsed-include-cache
))
718 "Reset OBJ back to it's empty settings."
719 (let ((table (oref obj table
)))
720 ;; This is a hack. Add in something better?
721 (semanticdb-notify-references
722 table
(lambda (tab me
)
723 (semantic-decoration-unparsed-include-refrence-reset tab
)
727 (defmethod semanticdb-partial-synchronize ((cache semantic-decoration-unparsed-include-cache
)
729 "Synchronize CACHE with some NEW-TAGS."
730 (if (semantic-find-tags-by-class 'include new-tags
)
731 (semantic-reset cache
)))
733 (defmethod semanticdb-synchronize ((cache semantic-decoration-unparsed-include-cache
)
735 "Synchronize a CACHE with some NEW-TAGS."
736 (semantic-reset cache
))
738 (defun semantic-decoration-unparsed-include-refrence-reset (table)
739 "Refresh any highlighting in buffers referred to by TABLE.
740 If TABLE is not in a buffer, do nothing."
741 ;; This cache removal may seem odd in that we are "creating one", but
742 ;; since we cant get in the fcn unless one exists, this ought to be
744 (let ((c (semanticdb-cache-get
745 table
'semantic-decoration-unparsed-include-cache
)))
746 (semanticdb-cache-remove table c
))
748 (let ((buf (semanticdb-in-buffer-p table
)))
750 (semantic-decorate-add-pending-decoration
751 'semantic-decoration-unparsed-include-do-reset
756 (defun semantic-decoration-unparsed-include-do-reset ()
757 "Do a reset of unparsed includes in the current buffer."
758 (let* ((style (assoc "semantic-decoration-on-includes"
759 semantic-decoration-styles
)))
761 (let ((allinc (semantic-find-tags-included
762 (semantic-fetch-tags-fast))))
763 ;; This will do everything, but it should be speedy since it
764 ;; would have been done once already.
765 (semantic-decorate-add-decorations allinc
)
769 (provide 'semantic
/decorate
/include
)
772 ;; generated-autoload-file: "../loaddefs.el"
773 ;; generated-autoload-load-name: "semantic/decorate/include"
776 ;; arch-tag: c3277137-be3f-43e2-af89-3b14b9bd7479
777 ;;; semantic/decorate/include.el ends here