forked from eschulte/rinari
-
Notifications
You must be signed in to change notification settings - Fork 1
/
toggle.el
173 lines (150 loc) · 6.95 KB
/
toggle.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
;;; toggle.el --- quickly open corresponding file (eg test vs impl).
;; Copyright (C) 2006-2007 by Ryan Davis
;; Author: Ryan Davis <ryand-ruby@zenspider.com>
;; Version 1.3
;; Keywords: files, extensions, convenience
;; Created: 2006-03-22
;; Compatibility: Emacs 22, 21?
;; URL(en): http://seattlerb.rubyforge.org/
;;; Posted using:
;; (setq emacs-wiki-name "RyanDavis")
;; (wikiput-buffer "update")
;;; The MIT License:
;; http://en.wikipedia.org/wiki/MIT_License
;;
;; Permission is hereby granted, free of charge, to any person obtaining
;; a copy of this software and associated documentation files (the
;; "Software"), to deal in the Software without restriction, including
;; without limitation the rights to use, copy, modify, merge, publish,
;; distribute, sublicense, and/or sell copies of the Software, and to
;; permit persons to whom the Software is furnished to do so, subject to
;; the following conditions:
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
;;; Commentary:
;; This package provides the ability to quickly open a corresponding
;; file for the current buffer by using a bi-directional mapping of
;; regular expression pairs. You can select a mapping style from
;; `toggle-mapping-styles' using the `toggle-style' function or set
;; your default style via the `toggle-mapping-style' variable.
;; There are 3 different mapping styles in this version: zentest,
;; rails, and ruby. Feel free to submit more and I'll incorporate
;; them.
;;; Example Mapping (ruby style):
;;
;; blah.rb <-> test_blah.rb
;; lib/blah.rb <-> test/test_blah.rb
;;; History:
;; 1.3 2007-05-10 Added tab completion to toggle-style. Suggested by TingWang.
;; 1.2 2007-04-06 Interleave bidirectional mappings. Fixed interactive setter. Added rspec mappings.
;; 1.1 2007-03-30 Initial release to emacswiki.org. Added named styles and bidi.
;; 1.0 2006-03-22 Birfday.
(require 'cl)
(require 'which-func)
(defcustom toggle-which-function-command
'which-function
"Function used to determine the current function. Defaults to
`which-function', but others may be preferable, for example
`ruby-add-log-current-method' is more reliable in ruby code.")
(defcustom toggle-method-format
"def %s"
"`format' string when searching for a method in search code")
(defcustom toggle-mapping-styles
'((zentest . (("app/controllers/\\1.rb" . "test/controllers/\\1_test.rb")
("app/views/\\1.rb" . "test/views/\\1_test.rb")
("app/models/\\1.rb" . "test/unit/\\1_test.rb")
("lib/\\1.rb" . "test/unit/test_\\1.rb")))
(rspec . (("app/models/\\1.rb" . "spec/models.\\1_spec.rb")
("app/controllers/\\1.rb" . "spec/controllers/\\1_spec.rb")
("app/views/\\1.rb" . "spec/views/\\1_spec.rb")
("app/helpers/\\1.rb" . "spec/helpers/\\1_spec.rb")))
(rails . (("app/controllers/\\1.rb" . "test/functional/\\1_test.rb")
("app/models/\\1.rb" . "test/unit/\\1_test.rb")
("lib/\\1.rb" . "test/unit/test_\\1.rb")))
(ruby . (("lib/\\1.rb" . "test/test_\\1.rb")
("\\1.rb" . "test_\\1.rb"))))
"A list of (name . toggle-mapping) rules used by toggle-filename."
:group 'toggle
:type '(repeat (cons string string)))
(defcustom toggle-mapping-style
'rails
"The default toggle mapping style to load when initialized."
:group 'toggle
:type '(symbol))
(defun toggle-style (name)
(interactive (list (completing-read "Style: "
(mapcar
#'symbol-name
(mapcar #'car toggle-mapping-styles))
nil t "")))
(let* ((style (if (stringp name) (intern name) name))
(pairs (cdr (assoc style toggle-mapping-styles))))
(if pairs
(let ((mappings
(mapcar (lambda (pair)
(cons
(replace-regexp-in-string
"\\\\[[:digit:]]" "\\\\(.*\\\\)"
(replace-regexp-in-string ; special case for "\\1.ext"
"^\\\\[[:digit:]]" "\\\\([^/]*\\\\)" (car pair)))
(cdr pair)))
(mapcan 'list
pairs
(mapcar (lambda (pair)
(cons (cdr pair) (car pair)))
pairs)))))
(if (interactive-p)
(setq toggle-mappings mappings)
mappings))
nil)))
(defvar toggle-mappings (toggle-style toggle-mapping-style)
"*The current file mappings for `toggle-filename' to use.")
(defun toggle-filename (path rules)
"Transform a matching subpath in PATH as given by RULES.
Each element in RULES is a pair (RE . TRANS). If the regular
expression RE matches PATH, then replace-match is invoked with
TRANS. After the first successful match, this returns. If no rule
matches, it returns nil"
(cond ((null rules) nil)
((string-match (caar rules) path)
(replace-match (cdar rules) nil nil path))
(t (toggle-filename path (rest rules)))))
(defun toggle-buffer ()
"Opens a related file to the current buffer using matching rules.
Matches the current buffer against rules in toggle-mappings. If a
match is found, switches to that buffer."
(interactive)
(let* ((function (eval (list toggle-which-function-command)))
(func-add (if function
(concat "#" (and (string-match "#\\(.+\\)" function)
(match-string 1 function)))
""))
(new-name (or (toggle-filename (concat (buffer-file-name) func-add)
toggle-mappings)
(toggle-filename (buffer-file-name) toggle-mappings))))
(if new-name
(if (string-match "\\(.+\\)#\\(.+\\)" new-name)
(let ((path (match-string 1 new-name))
(method (match-string 2 new-name)))
(flet ((try-rest (meth)
(goto-char (point-min))
(if meth
(if (and (goto-char (point-max))
(search-backward
(format toggle-method-format meth) nil t)) t
(try-rest (and (string-match "\\(.*\\)[-_]" meth)
(match-string 1 meth))))
(message "%s not defined in %s" method (file-name-nondirectory path)))))
(find-file path)
(try-rest method)))
(find-file new-name))
(message (concat "Match not found for " (buffer-file-name))))))
(provide 'toggle)
;;; toggle.el ends here