topfunky / emacs-starter-kit forked from technomancy/emacs-starter-kit

All the code you need to get started, with an emphasis on dynamic languages.

This URL has Read+Write access

e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 1 ;; textmate.el --- TextMate minor mode for Emacs
2
3 ;; Copyright (C) 2008 Chris Wanstrath <chris@ozmm.org>
4
5 ;; Licensed under the same terms as Emacs.
6
7 ;; Version: 0.1.0
8 ;; Keywords: textmate osx mac
9 ;; Created: 22 Nov 2008
10 ;; Author: Chris Wanstrath <chris@ozmm.org>
11
12 ;; This file is NOT part of GNU Emacs.
13
14 ;; Licensed under the same terms as Emacs.
15
16 ;;; Commentary:
17
18 ;; This minor mode exists to mimick TextMate's awesome
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 19 ;; features.
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 20
21 ;; ⌘T - Go to File
22 ;; ⇧⌘T - Go to Symbol
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 23 ;; ⌘L - Go to Line
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 24 ;; ⌘/ - Comment Line (or Selection/Region)
25 ;; ⌘] - Shift Right (currently indents region)
26 ;; ⌘[ - Shift Left (not yet implemented)
27 ;; ⌥⌘] - Align Assignments
28 ;; ⌥⌘[ - Indent Line
29 ;; ⌘RET - Insert Newline at Line's End
30 ;; ⌥⌘T - Reset File Cache (for Go to File)
31
32 ;; A "project" in textmate-mode is determined by the presence of
33 ;; a .git directory. If no .git directory is found in your current
34 ;; directory, textmate-mode will traverse upwards until one (or none)
35 ;; is found. The directory housing the .git directory is presumed
36 ;; to be the project's root.
37
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 38 ;; In other words, calling Go to File from
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 39 ;; ~/Projects/fieldrunners/app/views/towers/show.html.erb will use
40 ;; ~/Projects/fieldrunners/ as the root if ~/Projects/fieldrunners/.git
41 ;; exists.
42
43 ;;; Installation
44
45 ;; $ cd ~/.emacs.d/vendor
46 ;; $ git clone git://github.com/defunkt/textmate.el.git
47 ;;
48 ;; In your emacs config:
49 ;;
50 ;; (add-to-list 'load-path "~/.emacs.d/vendor/textmate.el")
51 ;; (require 'textmate)
52 ;; (textmate-mode)
53
54 ;;; Minor mode
55
56 (defvar textmate-use-file-cache t
57 "* Should `textmate-goto-file' keep a local cache of files?")
58
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 59 (defvar textmate-completing-library 'ido
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 60 "The library `textmade-goto-symbol' and `textmate-goto-file' should use for completing filenames and symbols (`ido' by default)")
61
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 62 (defvar textmate-completing-function-alist '((ido ido-completing-read)
63 (icicles icicle-completing-read)
64 (none completing-read))
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 65 "The function to call to read file names and symbols from the user")
66
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 67 (defvar textmate-completing-minor-mode-alist
68 `((ido ,(lambda (a) (progn (ido-mode a) (setq ido-enable-flex-matching t))))
69 (icicles ,(lambda (a) (icy-mode a)))
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 70 (none ,(lambda (a) ())))
71 "The list of functions to enable and disable completing minor modes")
72
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 73 (defvar textmate-mode-map (make-sparse-keymap))
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 74 (defvar *textmate-project-root* nil)
75 (defvar *textmate-project-files* '())
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 76 (defvar *textmate-gf-exclude*
f7e8ed93 » Geoffrey Grosenbach 2009-12-22 Better ignore pattern for t... 77 "/\\.sass-cache|\\.DS_Store|\\.git|\\/script|\\/vendor|\\/fixtures|\\/tmp|\\/log|\\/build|\\.xcodeproj|\\.nib|\\.framework|\\.app|\\.pbproj|\\.pbxproj|\\.xcode|\\.xcodeproj|\\.bundle|\\/UnitTesting")
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 78
79 ;;; Bindings
80
81 (defun textmate-ido-fix ()
82 "Add up/down keybindings for ido."
83 (define-key ido-completion-map [up] 'ido-prev-match)
84 (define-key ido-completion-map [down] 'ido-next-match))
85
86 (defun textmate-bind-keys ()
87 (add-hook 'ido-setup-hook 'textmate-ido-fix)
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 88 (if (boundp 'aquamacs-version)
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 89 (textmate-bind-aquamacs-keys)
90 (textmate-bind-carbon-keys)))
91
92 (defun textmate-bind-aquamacs-keys ()
93 (define-key textmate-mode-map [A-return] 'textmate-next-line)
94 (define-key textmate-mode-map (kbd "A-M-t") 'textmate-clear-cache)
95 (define-key textmate-mode-map (kbd "A-M-]") 'align)
96 (define-key textmate-mode-map (kbd "A-M-[") 'indent-according-to-mode)
97 (define-key textmate-mode-map (kbd "A-]") 'indent-region)
98 (define-key textmate-mode-map (kbd "A-/") 'comment-or-uncomment-region-or-line)
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 99 (define-key osx-key-mode-map (kbd "A-t") 'textmate-goto-file) ;; Need `osx-key-mode-map' to override
100 (define-key osx-key-mode-map (kbd "A-T") 'textmate-goto-symbol)) ;; Aquamacs menu item key bindings.
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 101
102 (defun textmate-bind-carbon-keys ()
103 (define-key textmate-mode-map [M-return] 'textmate-next-line)
df91923e » Geoffrey Grosenbach 2009-01-05 Implement C-return to add b... 104 (define-key textmate-mode-map [C-return] 'textmate-previous-line)
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 105 ; (define-key textmate-mode-map (kbd "A-M-t") 'textmate-clear-cache)
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 106 (define-key textmate-mode-map (kbd "M-[") 'align)
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 107 ; (define-key textmate-mode-map (kbd "A-M-[") 'indent-according-to-mode)
8843506e » Geoffrey Grosenbach 2009-02-21 Better Objective-C mode. Us... 108 (define-key textmate-mode-map (kbd "M-/") 'comment-or-uncomment-region-or-line)
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 109 (define-key textmate-mode-map [(control tab)] 'indent-region)
110 (define-key textmate-mode-map [(meta t)] 'textmate-goto-file)
111 (define-key textmate-mode-map [(meta T)] 'textmate-goto-symbol))
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 112
113 (defun textmate-completing-read (&rest args)
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 114 (let ((reading-fn (cadr (assoc textmate-completing-library textmate-completing-function-alist))))
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 115 (apply (symbol-function reading-fn) args)))
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 116
117 ;;; Commands
118
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 119 ;; TextMate-like commenting
120 ;; http://paste.lisp.org/display/42657
121 (defun comment-or-uncomment-line (&optional lines)
122 "Comment current line. Argument gives the number of lines
123 forward to comment"
124 (interactive "P")
125 (comment-or-uncomment-region
126 (line-beginning-position)
127 (line-end-position lines)))
128
129 (defun comment-or-uncomment-region-or-line (&optional lines)
130 "If the line or region is not a comment, comments region
131 if mark is active, line otherwise. If the line or region
132 is a comment, uncomment."
133 (interactive "P")
134 (if mark-active
135 (if (< (mark) (point))
136 (comment-or-uncomment-region (mark) (point))
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 137 (comment-or-uncomment-region (point) (mark)))
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 138 (comment-or-uncomment-line lines)))
139
140
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 141 (defun textmate-next-line ()
142 (interactive)
143 (end-of-line)
144 (newline-and-indent))
145
df91923e » Geoffrey Grosenbach 2009-01-05 Implement C-return to add b... 146 (defun textmate-previous-line ()
147 (interactive)
148 (beginning-of-line)
149 (newline-and-indent)
150 (previous-line)
151 (indent-according-to-mode))
152
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 153 ;; http://chopmo.blogspot.com/2008/09/quickly-jumping-to-symbols.html
154 (defun textmate-goto-symbol ()
155 "Will update the imenu index and then use ido to select a symbol to navigate to"
156 (interactive)
157 (imenu--make-index-alist)
158 (let ((name-and-pos '())
159 (symbol-names '()))
160 (flet ((addsymbols (symbol-list)
161 (when (listp symbol-list)
162 (dolist (symbol symbol-list)
163 (let ((name nil) (position nil))
164 (cond
165 ((and (listp symbol) (imenu--subalist-p symbol))
166 (addsymbols symbol))
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 167
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 168 ((listp symbol)
169 (setq name (car symbol))
170 (setq position (cdr symbol)))
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 171
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 172 ((stringp symbol)
173 (setq name symbol)
174 (setq position (get-text-property 1 'org-imenu-marker symbol))))
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 175
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 176 (unless (or (null position) (null name))
177 (add-to-list 'symbol-names name)
178 (add-to-list 'name-and-pos (cons name position))))))))
179 (addsymbols imenu--index-alist))
180 (let* ((selected-symbol (textmate-completing-read "Symbol: " symbol-names))
181 (position (cdr (assoc selected-symbol name-and-pos))))
182 (goto-char position))))
183
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 184 (defun textmate-goto-file (&optional starting)
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 185 (interactive)
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 186 (when (null (textmate-set-project-root))
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 187 (error "Can't find any .git directory"))
188 (find-file (concat (expand-file-name *textmate-project-root*) "/"
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 189 (textmate-completing-read "Find file: "
190 (or (textmate-cached-project-files)
191 (textmate-cache-project-files *textmate-project-root*))))))
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 192
193 (defun textmate-clear-cache ()
194 (interactive)
195 (setq *textmate-project-root* nil)
196 (setq *textmate-project-files* nil)
197 (message "textmate-mode cache cleared."))
198
199 ;;; Utilities
200
201 (defun textmate-project-files (root)
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 202 (split-string
203 (shell-command-to-string
204 (concat
205 "find "
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 206 root
207 " -type f | grep -vE '"
208 *textmate-gf-exclude*
209 "' | sed 's:"
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 210 *textmate-project-root*
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 211 "/::'")) "\n" t))
212
213 (defun textmate-cache-project-files (root)
214 (let ((files (textmate-project-files root)))
215 (setq *textmate-project-files* `(,root . ,files))
216 files))
217
218 (defun textmate-cached-project-files ()
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 219 (when (and
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 220 textmate-use-file-cache
221 (not (null *textmate-project-files*))
222 (equal *textmate-project-root* (car *textmate-project-files*)))
223 (cdr *textmate-project-files*)))
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 224
225 (defun textmate-project-root ()
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 226 (or (textmate-set-project-root) *textmate-project-root*))
227
228 (defun textmate-set-project-root ()
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 229 (when (or
230 (null *textmate-project-root*)
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 231 (not (string-match *textmate-project-root* default-directory)))
232 (let ((root (textmate-find-project-root)))
233 (if root
234 (setq *textmate-project-root* (expand-file-name (concat root "/")))
235 (setq *textmate-project-root* nil))))
236 *textmate-project-root*)
237
238 (defun textmate-find-project-root (&optional root)
239 (when (null root) (setq root default-directory))
240 (cond
241 ((member ".git" (directory-files root)) (expand-file-name root))
242 ((equal (expand-file-name root) "/") nil)
243 (t (textmate-find-project-root (concat (file-name-as-directory root) "..")))))
244
245 ;;;###autoload
246 (define-minor-mode textmate-mode "TextMate Emulation Minor Mode"
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 247 :lighter " mate" :global t :keymap textmate-mode-map
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 248 (textmate-bind-keys)
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 249 ; activate preferred completion library
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 250 (dolist (mode textmate-completing-minor-mode-alist)
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 251 (if (eq (car mode) textmate-completing-library)
252 (funcall (cadr mode) t)
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 253 (when (fboundp
7aef2657 » Geoffrey Grosenbach 2008-12-08 Modifications to work with ... 254 (cadr (assoc (car mode) textmate-completing-function-alist)))
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 255 (funcall (cadr mode) -1)))))
256
11684229 » Geoffrey Grosenbach 2008-12-22 Activated my new theme (may... 257 ;; Topfunky enhancements. Maybe move to passenger.el file?
258
259 (defun passenger-restart (&optional starting)
260 (interactive)
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 261 (when (null (textmate-set-project-root))
262 (error "Can't find any .git directory"))
263 (shell-command-to-string
264 (concat
265 "touch "
266 *textmate-project-root*
267 "/tmp/restart.txt")) "\n" t)
268
269 (defun regen-ctags (&optional starting)
270 (interactive)
271 (when (null (textmate-set-project-root))
11684229 » Geoffrey Grosenbach 2008-12-22 Activated my new theme (may... 272 (error "Can't find any .git directory"))
31e6cc8a » Geoffrey Grosenbach 2008-12-24 Whitespace cleanup. Impleme... 273 (shell-command-to-string
274 (concat
275 "rm -f " *textmate-project-root* "/TAGS && "
276 "cd " *textmate-project-root* " && "
1ea897f2 » Geoffrey Grosenbach 2008-12-31 Rearrange key bindings. Lim... 277 "ctags -R -e -f " *textmate-project-root* "TAGS app config lib vendor")) "\n" t)
11684229 » Geoffrey Grosenbach 2008-12-22 Activated my new theme (may... 278
e79236d2 » Geoffrey Grosenbach 2008-12-03 Added textmate plugin 279 (provide 'textmate)
280 ;;; textmate.el ends here