-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathprojectile-speedbar.el
175 lines (150 loc) · 5.56 KB
/
projectile-speedbar.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
;;; projectile-speedbar.el --- projectile integration for speedbar
;; Copyright (C) 2014 Anshul Verma
;; Author: Anshul Verma <[email protected]>
;; URL: https://github.com/anshulverma/projectile-speedbar
;; Keywords: project, convenience, speedbar, projectile
;; Version: 0.0.1
;; Package-Requires: ((projectile "0.11.0") (sr-speedbar "0"))
;; This file is NOT part of GNU Emacs.
;;; License
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; This package sits on top of speedbar and projectile and provides an
;; easy to use and useful integration between the two.
;;
;; With this package when you switch between projects that work with
;; projectile, speedbar will automatically show the directly listing
;; of that project as well as expand the tree to show the file in the
;; project.
;;
;; Features that are required by this library:
;;
;; `speedbar' `sr-speedbar' `projectile'
;;
;; To invoke this function manually:
;;
;; `projectile-speedbar-open-current-buffer-in-tree
;;
;;; Installation
;;
;; Copy speedbar-projectile.el to your load-path and add this to ~/.emacs
;;
;; (require 'projectile-speedbar)
;;
;;; Customize:
;;
;; Sometimes, when I am deep in a project tree, I like to use this shortcut
;; to see full context:
;;
;; (global-set-key (kbd "M-<f2>") 'projectile-speedbar-open-current-buffer-in-tree)
;;
;; You can also disable the feature completely:
;;
;; (setq projectile-speedbar-projectile-speedbar-enable nil)
;;; Change log:
;;
;; * 13 June 2014:
;; * Anshul Verma
;; * Initial feature with commentary
;;
;; * 13 June 2014
;; * Anshul Verma
;; * fix bug "should switch to file buffer after opening a file via projectile-find-file"
;;
;; * 27 June 2014
;; * Anshul Verma
;; * add ability to turn projectile speedbar off
;;
;; * 13 January 2015
;; * Anshul Verma
;; * fix headers to comply with standards
;;; Acknowledgments
;;
;; All emacsers ... :)
;;
;;; Bug
;;
;; * Should select the current buffer file in directory listing after project switch
;;
;;; TODO
;;
;; * Find a better way to get project root
;;
;;; Code:
(require 'speedbar)
(require 'sr-speedbar)
(require 'projectile)
(defgroup projectile-speedbar nil
"Auto refresh speedbar based on projectile."
:group 'speedbar)
(defcustom projectile-speedbar-enable t
"Do not auto-refresh speedbar using `projectile-speedbar'.
Set to nil to disable projectile speedbar. Default is t."
:type 'boolean
:set (lambda (symbol value)
(set symbol value))
:group 'projectile-speedbar)
(defun projectile-speedbar-project-refresh (root-dir)
"Refresh the context of speedbar based on project root"
(when (and (not (equal root-dir sr-speedbar-last-refresh-dictionary))
(not (sr-speedbar-window-p)))
(setq sr-speedbar-last-refresh-dictionary root-dir))
(setq default-directory root-dir)
(speedbar-refresh))
(defun projectile-speedbar-open-current-project-in-speedbar (root-dir)
"Refresh speedbar to show current project in tree"
(if (not (sr-speedbar-exist-p))
(sr-speedbar-toggle))
(projectile-speedbar-project-refresh root-dir))
(defun projectile-speedbar-expand-line-list (&optional arg)
(when arg
(re-search-forward (concat " " (car arg) "$"))
(speedbar-expand-line (car arg))
(speedbar-next 1)
(projectile-speedbar-expand-line-list (cdr arg))))
;;;###autoload
(defun projectile-speedbar-open-current-buffer-in-tree ()
(interactive)
(let* ((root-dir (projectile-project-root))
(original-buffer-file-directory (file-name-directory
(file-truename (buffer-file-name))))
(relative-buffer-path (car (cdr (split-string original-buffer-file-directory
(regexp-quote root-dir)))))
(parents (butlast (split-string relative-buffer-path "/")))
(original-window (get-buffer-window)))
(if projectile-speedbar-enable
(save-excursion
(projectile-speedbar-open-current-project-in-speedbar root-dir)
(select-window (get-buffer-window speedbar-buffer))
(beginning-of-buffer)
(projectile-speedbar-expand-line-list parents)
(if (not (eq original-window (get-buffer-window speedbar-buffer)))
(select-window original-window)
(other-window 1))))))
;;;###autoload
(defun projectile-speedbar-toggle ()
(interactive)
(sr-speedbar-toggle)
(if (sr-speedbar-exist-p)
(projectile-speedbar-open-current-buffer-in-tree)))
(add-hook 'projectile-find-dir-hook 'projectile-speedbar-open-current-buffer-in-tree)
(add-hook 'projectile-find-file-hook 'projectile-speedbar-open-current-buffer-in-tree)
(add-hook 'projectile-cache-projects-find-file-hook
'projectile-speedbar-open-current-buffer-in-tree)
(add-hook 'projectile-cache-files-find-file-hook
'projectile-speedbar-open-current-buffer-in-tree)
(provide 'projectile-speedbar)
;;; projectile-speedbar.el ends here