Advertisement
Guest User

emr-c:semantic-insert-function-prototype-or-implementation

a guest
Jan 26th, 2015
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 5.15 KB | None | 0 0
  1. (defvar emr-c:--semantic-tag-list '()
  2.   "[INTERNAL] List of semantic tags in current buffer.")
  3.  
  4. (defcustom emr-c-implementation-extensions
  5.   '("c" "cpp")
  6.   "Specify extensions that indicate current file is an implementation files."
  7.   :group 'emr)
  8.  
  9. (defun emr-c:--semantic-fetch-candidates ()
  10.   (setq emr-c:--semantic-tag-list nil)
  11.   (emr-c:--semantic-fetch-candidates-helper (semantic-fetch-tags) 0 nil))
  12.  
  13. (defun emr-c:--semantic-fetch-candidates-helper (tags depth &optional class)
  14.   "Return a list of pairs '(DISPLAY . REAL), where DISPLAY is the string to be
  15.    presented to user, while REAL is a semantic tag.
  16.  
  17.    TAGS are collection of Semantic tags in current buffer.
  18.    DEPTH is current recursion depth.
  19.    CLASS is the parent class."
  20.   (let ((spaces (make-string (* depth 2) ?\s))
  21.         (class class) cur-type display)
  22.     (cl-dolist (tag tags)
  23.       (when (listp tag)
  24.         (cl-case (setq cur-type (semantic-tag-class tag))
  25.           ((function variable type)
  26.            (let ((type-p (eq cur-type 'type)))
  27.              (unless (and (> depth 0) (not type-p))2Dut83xe
  28.                (setq class nil))
  29.              (setq display (concat (if (null class)
  30.                                        spaces
  31.                                      (format "%s(%s) " spaces
  32.                                              (propertize (semantic-format-tag-name (semantic-tag-calculate-parent tag) nil t)
  33.                                                          'semantic-tag
  34.                                                          (semantic-tag-calculate-parent tag))))
  35.                                    (propertize (semantic-format-tag-summarize tag nil t)
  36.                                                'semantic-tag tag)))
  37.              (message "display is %s" display)
  38.              (and type-p
  39.                   (setq class (car tag)))
  40.              (push (cons display tag)
  41.                    emr-c:--semantic-tag-list)
  42.              ;; Recurse to children
  43.              (emr-c:--semantic-fetch-candidates-helper (semantic-tag-components tag)
  44.                                                        (1+ depth)
  45.                                                        class)))
  46.           ;; Don't do anything with packages or includes for now
  47.           ((package include)
  48.            (push (cons (propertize (semantic-format-tag-summarize tag nil t)
  49.                                    'semantic-tag tag)
  50.                        tag)
  51.                  emr-c:--semantic-tag-list))
  52.           ;; Catch-all
  53.           (t))))))
  54.  
  55. (defun emr-c:semantic-insert-function-prototype-or-implementation ()
  56.   "Insert function at point as prototype or implementation to
  57.    other file (files with same names but different extensions),
  58.    depends on file extension. If the file extension is in
  59.    emr-c-implementation-extensions, insert \";\"; otherwise, insert
  60.    {}. If there is more than one file, prompt for a file. If there's
  61.    no file, prompt for the entire projectile project files."
  62.   (interactive)
  63.   (let (file other-files l)
  64.     (if (featurep 'projectile)
  65.         (progn
  66.           (setf other-files
  67.                 (projectile-get-other-files (buffer-file-name)
  68.                                             (projectile-current-project-files)
  69.                                             nil))
  70.           (setf l (length other-files))
  71.           (setf file (concat (projectile-project-root)
  72.                              (cond ((> l 1)
  73.                                     (completing-read "Select a file to insert: "
  74.                                                      other-files))
  75.                                    ((= l 1)
  76.                                     (car other-files))
  77.                                    (t (projectile-find-file)))))))
  78.     (senator-copy-tag)
  79.     (with-current-buffer (if (featurep 'projectile)
  80.                              (find-file file)
  81.                            (ff-find-other-file))
  82.       (emr-c:--semantic-fetch-candidates)
  83.  
  84.       (if emr-c:--semantic-tag-list
  85.           (progn
  86.             (setq emr-c:--semantic-tag-list (nreverse emr-c:--semantic-tag-list))
  87.             (emr-c:--semantic-insert-prototype (cdr (assoc (completing-read "Select a place to insert: "
  88.                                                                             emr-c:--semantic-tag-list)
  89.                                                            emr-c:--semantic-tag-list))))
  90.         (emr-c:--semantic-insert-prototype nil)))))
  91.  
  92. (defun emr-c:--semantic-insert-prototype (tag)
  93.   "[INTERNAL] Insert a Semantic TAG to current buffer.
  94.    If the file extension is in emr-c-implementation-extensions,
  95.    insert \";\"; otherwise, insert {}."
  96.   (when tag
  97.     (semantic-go-to-tag tag)
  98.     (goto-char (semantic-tag-end tag))
  99.     (newline 2))
  100.  
  101.   (senator-yank-tag)
  102.   (indent-according-to-mode)
  103.  
  104.   (if (member (file-name-extension (buffer-file-name))
  105.               emr-c-implementation-extensions)
  106.       (progn
  107.         (insert "{}")
  108.         (forward-char -1)
  109.         (open-line 1)
  110.         (newline 1)
  111.         (indent-according-to-mode))
  112.     (insert ";")
  113.     (save-excursion
  114.       (forward-line 1)
  115.       (unless (c-guess-empty-line-p)
  116.         (newline 1)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement