forked from defunkt/textmate.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textmate.el
228 lines (187 loc) · 8.51 KB
/
textmate.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
;; textmate.el --- TextMate minor mode for Emacs
;; Copyright (C) 2008 Chris Wanstrath <chris@ozmm.org>
;; Licensed under the same terms as Emacs.
;; Version: 0.1.0
;; Keywords: textmate osx mac
;; Created: 22 Nov 2008
;; Author: Chris Wanstrath <chris@ozmm.org>
;; This file is NOT part of GNU Emacs.
;; Licensed under the same terms as Emacs.
;;; Commentary:
;; This minor mode exists to mimick TextMate's awesome
;; features.
;; ⌘T - Go to File
;; ⇧⌘T - Go to Symbol
;; ⌘L - Go to Line
;; ⌘/ - Comment Line (or Selection/Region)
;; ⌘] - Shift Right (currently indents region)
;; ⌘[ - Shift Left (not yet implemented)
;; ⌥⌘] - Align Assignments
;; ⌥⌘[ - Indent Line
;; ⌘RET - Insert Newline at Line's End
;; ⌥⌘T - Reset File Cache (for Go to File)
;; A "project" in textmate-mode is determined by the presence of
;; a .git directory. If no .git directory is found in your current
;; directory, textmate-mode will traverse upwards until one (or none)
;; is found. The directory housing the .git directory is presumed
;; to be the project's root.
;; In other words, calling Go to File from
;; ~/Projects/fieldrunners/app/views/towers/show.html.erb will use
;; ~/Projects/fieldrunners/ as the root if ~/Projects/fieldrunners/.git
;; exists.
;;; Installation
;; $ cd ~/.emacs.d/vendor
;; $ git clone git://github.com/defunkt/textmate.el.git
;;
;; In your emacs config:
;;
;; (add-to-list 'load-path "~/.emacs.d/vendor/textmate.el")
;; (require 'textmate)
;; (textmate-mode)
;;; Minor mode
(defvar textmate-use-file-cache t
"* Should `textmate-goto-file' keep a local cache of files?")
(defvar textmate-completing-library 'ido
"The library `textmade-goto-symbol' and `textmate-goto-file' should use for completing filenames and symbols (`ido' by default)")
(defvar textmate-completing-function-alist '((ido ido-completing-read)
(icicles icicle-completing-read)
(none completing-read))
"The function to call to read file names and symbols from the user")
(defvar textmate-completing-minor-mode-alist
`((ido ,(lambda (a) (progn (ido-mode a) (setq ido-enable-flex-matching t))))
(icicles ,(lambda (a) (icy-mode a)))
(none ,(lambda (a) ())))
"The list of functions to enable and disable completing minor modes")
(defvar textmate-mode-map (make-sparse-keymap))
(defvar *textmate-project-root* nil)
(defvar *textmate-project-files* '())
(defvar *textmate-gf-exclude*
"/\\.|vendor|fixtures|tmp|log|build|\\.xcodeproj|\\.nib|\\.framework|\\.app|\\.pbproj|\\.pbxproj|\\.xcode|\\.xcodeproj|\\.bundle")
;;; Bindings
(defun textmate-ido-fix ()
"Add up/down keybindings for ido."
(define-key ido-completion-map [up] 'ido-prev-match)
(define-key ido-completion-map [down] 'ido-next-match))
(defun textmate-bind-keys ()
(add-hook 'ido-setup-hook 'textmate-ido-fix)
(if (boundp 'aquamacs-version)
(textmate-bind-aquamacs-keys)
(textmate-bind-carbon-keys)))
(defun textmate-bind-aquamacs-keys ()
(define-key textmate-mode-map [A-return] 'textmate-next-line)
(define-key textmate-mode-map (kbd "A-M-t") 'textmate-clear-cache)
(define-key textmate-mode-map (kbd "A-M-]") 'align)
(define-key textmate-mode-map (kbd "A-M-[") 'indent-according-to-mode)
(define-key textmate-mode-map (kbd "A-]") 'indent-region)
(define-key textmate-mode-map (kbd "A-/") 'comment-region-or-line)
(define-key osx-key-mode-map (kbd "A-t") 'textmate-goto-file) ;; Need `osx-key-mode-map' to override
(define-key osx-key-mode-map (kbd "A-T") 'textmate-goto-symbol)) ;; Aquamacs menu item key bindings.
(defun textmate-bind-carbon-keys ()
;; Are these any good? Anyone have good Carbon defaults?
(define-key textmate-mode-map [M-return] 'textmate-next-line)
; (define-key textmate-mode-map (kbd "A-M-t") 'textmate-clear-cache)
; (define-key textmate-mode-map [(meta ])] 'align)
; (define-key textmate-mode-map (kbd "A-M-[") 'indent-according-to-mode)
; (define-key textmate-mode-map (kbd "A-/") 'comment-region-or-line)
(define-key textmate-mode-map [(control tab)] 'indent-region)
(define-key textmate-mode-map [(meta t)] 'textmate-goto-file)
(define-key textmate-mode-map [(meta T)] 'textmate-goto-symbol))
(defun textmate-completing-read (&rest args)
(let ((reading-fn (cadr (assoc textmate-completing-library textmate-completing-function-alist))))
(apply (symbol-function reading-fn) args)))
;;; Commands
(defun textmate-next-line ()
(interactive)
(end-of-line)
(newline-and-indent))
;; http://chopmo.blogspot.com/2008/09/quickly-jumping-to-symbols.html
(defun textmate-goto-symbol ()
"Will update the imenu index and then use ido to select a symbol to navigate to"
(interactive)
(imenu--make-index-alist)
(let ((name-and-pos '())
(symbol-names '()))
(flet ((addsymbols (symbol-list)
(when (listp symbol-list)
(dolist (symbol symbol-list)
(let ((name nil) (position nil))
(cond
((and (listp symbol) (imenu--subalist-p symbol))
(addsymbols symbol))
((listp symbol)
(setq name (car symbol))
(setq position (cdr symbol)))
((stringp symbol)
(setq name symbol)
(setq position (get-text-property 1 'org-imenu-marker symbol))))
(unless (or (null position) (null name))
(add-to-list 'symbol-names name)
(add-to-list 'name-and-pos (cons name position))))))))
(addsymbols imenu--index-alist))
(let* ((selected-symbol (textmate-completing-read "Symbol: " symbol-names))
(position (cdr (assoc selected-symbol name-and-pos))))
(goto-char position))))
(defun textmate-goto-file (&optional starting)
(interactive)
(when (null (textmate-set-project-root))
(error "Can't find any .git directory"))
(find-file (concat (expand-file-name *textmate-project-root*) "/"
(textmate-completing-read "Find file: "
(or (textmate-cached-project-files)
(textmate-cache-project-files *textmate-project-root*))))))
(defun textmate-clear-cache ()
(interactive)
(setq *textmate-project-root* nil)
(setq *textmate-project-files* nil)
(message "textmate-mode cache cleared."))
;;; Utilities
(defun textmate-project-files (root)
(split-string
(shell-command-to-string
(concat
"find "
root
" -type f | grep -vE '"
*textmate-gf-exclude*
"' | sed 's:"
*textmate-project-root*
"/::'")) "\n" t))
(defun textmate-cache-project-files (root)
(let ((files (textmate-project-files root)))
(setq *textmate-project-files* `(,root . ,files))
files))
(defun textmate-cached-project-files ()
(when (and
textmate-use-file-cache
(not (null *textmate-project-files*))
(equal *textmate-project-root* (car *textmate-project-files*)))
(cdr *textmate-project-files*)))
(defun textmate-project-root ()
(or (textmate-set-project-root) *textmate-project-root*))
(defun textmate-set-project-root ()
(when (or
(null *textmate-project-root*)
(not (string-match *textmate-project-root* default-directory)))
(let ((root (textmate-find-project-root)))
(if root
(setq *textmate-project-root* (expand-file-name (concat root "/")))
(setq *textmate-project-root* nil)))))
(defun textmate-find-project-root (&optional root)
(when (null root) (setq root default-directory))
(cond
((member ".git" (directory-files root)) (expand-file-name root))
((equal (expand-file-name root) "/") nil)
(t (textmate-find-project-root (concat (file-name-as-directory root) "..")))))
;;;###autoload
(define-minor-mode textmate-mode "TextMate Emulation Minor Mode"
:lighter " mate" :global t :keymap textmate-mode-map
(textmate-bind-keys)
; activate preferred completion library
(dolist (mode textmate-completing-minor-mode-alist)
(if (eq (car mode) textmate-completing-library)
(funcall (cadr mode) t)
(when (fboundp
(cadr (assoc (car mode) textmate-completing-function-alist)))
(funcall (cadr mode) -1)))))
(provide 'textmate)
;;; textmate.el ends here