123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- (require 'semantic)
- (require 'semantic/tag-ls)
- (declare-function srecode-dictionary-show-section "srecode/dictionary")
- (declare-function srecode-dictionary-set-value "srecode/dictionary")
- (define-overload srecode-calculate-context ()
- "Calculate the context at the current point.
- The returned context is a list, with the top-most context first.
- Each returned context is a string that would show up in a `context'
- statement in an `.srt' file.
- Some useful context values used by the provided srecode templates are:
- \"file\" - Templates that for a file (such as an empty file.)
- \"empty\" - The file is empty
- \"declaration\" - Top-level declarations in a file.
- \"include\" - In or near include statements
- \"package\" - In or near provide statements
- \"function\" - In or near function statements
- \"NAME\" - Near functions within NAME namespace or class
- \"variable\" - In or near variable statements.
- \"type\" - In or near type declarations.
- \"comment\" - In a comment
- \"classdecl\" - Declarations within a class/struct/etc.
- \"variable\" - In or near class fields
- \"function\" - In or near methods/functions
- \"virtual\" - Nearby items are virtual
- \"pure\" - and those virtual items are pure virtual
- \"type\" - In or near type declarations.
- \"comment\" - In a comment in a block of code
- -- these items show up at the end of the context list. --
- \"public\", \"protected\", \"private\" -
- In or near a section of public/protected/private entries.
- \"code\" - In a block of code.
- \"string\" - In a string in a block of code
- \"comment\" - In a comment in a block of code
- ... More later."
- )
- (defun srecode-calculate-nearby-things ()
-
- "Calculate the CONTEXT type items nearby the current point.
- Assume that what we want to insert next is based on what is just
- before point. If there is nothing, then assume it is whatever is
- after point."
-
-
-
- (let ((near (semantic-find-tag-by-overlay-prev))
- (prot nil)
- (ans nil))
- (if (not near)
- (setq near (semantic-find-tag-by-overlay-next)))
- (when near
-
- (if (not (semantic-tag-of-class-p near 'function))
- (setq ans (cons (symbol-name (semantic-tag-class near)) ans))
-
- (let ((p (semantic-tag-function-parent near)))
- (setq ans (cons (symbol-name (semantic-tag-class near)) ans))
- (cond ((semantic-tag-p p)
- (setq ans (cons (semantic-tag-name p) ans)))
- ((stringp p)
- (setq ans (cons p ans)))
- (t nil)))
-
- (when (semantic-tag-get-attribute near :virtual)
- (setq ans (cons "virtual" ans)))
-
- (when (semantic-tag-get-attribute near :pure-virtual-flag)
- (setq ans (cons "pure" ans)))
- )
-
- (setq prot (semantic-tag-protection near))
- (when (and prot (not (eq prot 'unknown)))
- (setq ans (cons (symbol-name prot) ans)))
- )
- (nreverse ans)))
- (defun srecode-calculate-context-font-lock ()
- "Calculate an srecode context by using font-lock."
- (let ((face (get-text-property (point) 'face))
- )
- (cond ((member face '(font-lock-string-face
- font-lock-doc-face))
- (list "string"))
- ((member face '(font-lock-comment-face
- font-lock-comment-delimiter-face))
- (list "comment"))
- )
- ))
- (defun srecode-calculate-context-default ()
- "Generic method for calculating a context for srecode."
- (if (= (point-min) (point-max))
- (list "file" "empty")
- (semantic-fetch-tags)
- (let ((ct (semantic-find-tag-by-overlay))
- )
- (cond ((or (not ct)
-
- (and (eq (semantic-tag-class (car ct)) 'type)
- (string= (semantic-tag-type (car ct)) "namespace")))
- (cons "declaration"
- (or (srecode-calculate-context-font-lock)
- (srecode-calculate-nearby-things)
- ))
- )
- ((eq (semantic-tag-class (car ct)) 'function)
- (cons "code" (srecode-calculate-context-font-lock))
- )
- ((eq (semantic-tag-class (car ct)) 'type)
- (cons "classdecl"
- (or (srecode-calculate-context-font-lock)
- (srecode-calculate-nearby-things)))
- )
- ((and (car (cdr ct))
- (eq (semantic-tag-class (car (cdr ct))) 'type))
- (list "classdecl"
- (symbol-name (semantic-tag-class (car ct))))
- )
- )
- )))
- (defun srecode-semantic-handle-:ctxt (dict &optional template)
- "Add macros into the dictionary DICT based on the current Emacs Lisp file.
- Argument TEMPLATE is the template object adding context dictionary
- entries.
- This might add the following:
- VIRTUAL - show a section if a function is virtual
- PURE - show a section if a function is pure virtual.
- PARENT - The name of a parent type for functions.
- PROTECTION - Show a protection section, and what the protection is."
- (require 'srecode/dictionary)
- (when template
- (let ((name (oref template object-name))
- (cc (if (boundp 'srecode-insertion-start-context)
- srecode-insertion-start-context))
-
- )
-
- (let ((ct (nth 0 cc))
- (it (nth 1 cc))
- (last (last cc))
- (parent nil)
- )
- (cond ((string= it "function")
- (setq parent (nth 2 cc))
- (when parent
- (cond ((string= parent "virtual")
- (srecode-dictionary-show-section dict "VIRTUAL")
- (when (nth 3 cc)
- (srecode-dictionary-show-section dict "PURE"))
- )
- (t
- (srecode-dictionary-set-value dict "PARENT" parent))))
- )
- ((and (string= it "type")
- (or (string= name "function") (string= name "method")))
-
-
- (let ((near (semantic-find-tag-by-overlay-prev)))
- (when (and near (semantic-tag-of-class-p near 'type))
- (srecode-dictionary-set-value
- dict "PARENT" (semantic-tag-name near))))
- )
- ((string= ct "code")
-
-
-
- nil
-
- )
- (t
- nil))
- (when (member last '("public" "private" "protected"))
-
- (srecode-dictionary-set-value dict "PROTECTION" parent)
- (srecode-dictionary-show-section dict "PROTECTION"))
- ))
- ))
- (provide 'srecode/ctxt)
|