/
titanium.el
241 lines (204 loc) · 6.81 KB
/
titanium.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
;;;titanium.el --- Titanium Minor Mode
;; -*- Mode: Emacs-Lisp -*-
;; Copyright (C) 2010 by 101000code/101000LAB
;; 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 2 of the License, 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; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
;; Version: 0.0.1
;; Author: k1LoW (Kenichirou Oyama), <k1lowxb [at] gmail [dot] com> <k1low [at] 101000lab [dot] org>
;; URL: http://code.101000lab.org, http://trac.codecheck.in
;;; Install
;; Put this file into load-path'ed directory, and byte compile it if
;; desired. And put the following expression into your ~/.emacs.
;;
;; (require 'titanium)
;; (global-titanium t)
;;
;; If you use default key map, Put the following expression into your ~/.emacs.
;;
;; (titanium-set-default-keymap)
;;; Commentary:
;;; Commands:
;;
;; Below are complete command list:
;;
;; `titanium'
;; Titanium minor mode.
;; `titanium-switch-to-function'
;; Switch to function.
;; `titanium-switch-to-file-history'
;; Switch to file history.
;; `titanium-open-dir'
;; Open directory.
;; `titanium-open-resources-dir'
;; Open JavaScript directory.
;;
;;; Customizable Options:
;;
;; Below are customizable option list:
;;
;; `titanium-root-path-search-limit'
;; Search limit
;; default = 5
;;; Change Log
;; 0.0.1: Initial commit
;;; TODO
;;
;;; Code:
;;require
(require 'cl)
(require 'anything)
(require 'historyf)
(require 'easy-mmode)
(when (require 'anything-show-completion nil t)
(use-anything-show-completion 'titanium-complete
'(length titanium-initial-input)))
(defgroup titanium nil
"Titanium minor mode"
:group 'convenience
:prefix "titanium-")
(defcustom titanium-root-path-search-limit 5
"Search limit"
:type 'integer
:group 'titanium)
;;(global-set-key "\C-c\C-v" 'titanium)
(define-minor-mode titanium
"Titanium minor mode."
:lighter " Ti"
:group 'titanium
(if titanium
(progn
(setq minor-mode-map-alist
(cons (cons 'titanium titanium-key-map)
minor-mode-map-alist))
(run-hooks 'titanium-hook))
nil))
(if (fboundp 'define-global-minor-mode)
(define-global-minor-mode global-titanium
titanium titanium-maybe
:group 'titanium))
(defun titanium-maybe ()
"What buffer `titanium' prefers."
(if (and (not (minibufferp (current-buffer)))
(titanium-set-root-path))
(titanium 1)
nil))
;; key-map
(defvar titanium-key-map
(make-sparse-keymap)
"Keymap for Titanium.")
(defvar titanium-root-path nil
"Titanium app directory path.")
(defvar titanium-file-history nil
"Switch file history.")
(defvar titanium-hook nil
"Hook")
(defun titanium-set-default-keymap ()
"Set default key-map"
(setq titanium-key-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-cf" 'titanium-switch-to-function)
(define-key map "\C-cr" 'titanium-open-resources-dir)
map)))
(defun titanium-get-current-line ()
"Get current line."
(thing-at-point 'line))
(defun titanium-set-root-path ()
"Set app path."
(titanium-is-root-path))
(defun titanium-is-root-path ()
"Check app directory name and set regExp."
(setq titanium-root-path (titanium-find-root-path))
(if (not titanium-root-path)
nil
t))
(defun titanium-find-root-path ()
"Find app directory"
(let ((current-dir default-directory))
(loop with count = 0
until (file-exists-p (concat current-dir "tiapp.xml"))
;; Return nil if outside the value of
if (= count titanium-root-path-search-limit)
do (return nil)
;; Or search upper directories.
else
do (incf count)
(setq current-dir (expand-file-name (concat current-dir "../")))
finally return current-dir)))
(defun titanium-switch-to-function ()
"Switch to function."
(interactive)
(require 'imenu nil t)
(anything 'anything-c-source-imenu))
(defun titanium-switch-to-file-history ()
"Switch to file history."
(interactive)
(historyf-back '(titanium)))
(defun titanium-open-dir (dir &optional recursive)
"Open directory."
(interactive)
(if (titanium-set-root-path)
(anything-other-buffer
(titanium-create-open-dir-anything-sources dir recursive) nil)
(message "Can't set app path.")))
(defun titanium-create-open-dir-anything-sources (dir &optional recursive)
"Careate 'Open dir' anything-sources"
(let (sources)
(unless (listp dir)
(setq dir (list dir)))
(if (titanium-set-root-path)
(progn
(loop for d in dir do
(unless (not (file-directory-p (concat titanium-root-path d)))
(push
`((name . ,(concat "Open directory: " d))
(candidates . ,(titanium-directory-files d recursive))
(display-to-real . (lambda (candidate)
(concat ,titanium-root-path ,d candidate)))
(type . file))
sources)))
(reverse sources))
(message "Can't set app path."))))
(defun titanium-directory-files (dir &optional recursive)
"Get directory files recuresively."
(let
((file-list nil))
(if (not recursive)
(directory-files (concat titanium-root-path dir))
(loop for x in (titanium-get-recuresive-path-list (concat titanium-root-path dir))
do (progn
(string-match (concat titanium-root-path dir "\\(.+\\)") x)
(push (match-string 1 x) file-list)))
file-list)))
(defun titanium-get-recuresive-path-list (file-list)
"Get file path list recuresively."
(let ((path-list nil))
(unless (listp file-list)
(setq file-list (list file-list)))
(loop for x
in file-list
do (if (file-directory-p x)
(setq path-list
(append
(titanium-get-recuresive-path-list
(remove-if
(lambda(y) (string-match "\\.$\\|\\.svn" y)) (directory-files x t)))
path-list))
(setq path-list (push x path-list))))
path-list))
(defun titanium-open-resources-dir ()
"Open JavaScript directory."
(interactive)
(titanium-open-dir "Resources/" t))
;; mode provide
(provide 'titanium)
;;; end
;;; titanium.el ends here