forked from tomtt/emacs-rails
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrails-lib.el
381 lines (319 loc) · 13 KB
/
rails-lib.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
;;; rails-lib.el ---
;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>,
;; Rezikov Peter <crazypit13 (at) gmail.com>
;; Howard Yeh <hayeah at gmail dot com>
;; Keywords: ruby rails languages oop
;; $URL$
;; $Id$
;;; 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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;;; Code:
(defun rails-lib:run-primary-switch ()
"Run the primary switch function."
(interactive)
(if rails-primary-switch-func
(apply rails-primary-switch-func nil)))
(defun rails-lib:run-secondary-switch ()
"Run the secondary switch function."
(interactive)
(if rails-secondary-switch-func
(apply rails-secondary-switch-func nil)))
;;;;; Non Rails realted helper functions ;;;;;
;; Syntax macro
(defmacro* when-bind ((var expr) &rest body)
"Binds VAR to the result of EXPR.
If EXPR is not nil exeutes BODY.
(when-bind (var (func foo))
(do-somth (with var)))."
`(let ((,var ,expr))
(when ,var
,@body)))
;; Lists
(defun list->alist (list)
"Convert ((a . b) c d) to ((a . b) (c . c) (d . d))."
(mapcar
#'(lambda (el)
(if (listp el) el(cons el el)))
list))
(defun uniq-list (list)
"Return a list of unique elements."
(let ((result '()))
(dolist (elem list)
(when (not (member elem result))
(push elem result)))
(nreverse result)))
;; Strings
(defun string-repeat (char num)
(let ((len num)
(str ""))
(while (not (zerop len))
(setq len (- len 1))
(setq str (concat char str)))
str))
(defmacro string=~ (regex string &rest body)
"regex matching similar to the =~ operator found in other languages."
(let ((str (gensym)))
`(lexical-let ((,str ,string))
;; Use lexical-let to make closures (in flet).
(when (string-match ,regex ,str)
(symbol-macrolet ,(loop for i to 9 collect
(let ((sym (intern (concat "$" (number-to-string i)))))
`(,sym (match-string ,i ,str))))
(flet (($ (i) (match-string i ,str))
(sub (replacement &optional (i 0) &key fixedcase literal-string)
(replace-match replacement fixedcase literal-string ,str i)))
(symbol-macrolet ( ;;before
($b (substring ,str 0 (match-beginning 0)))
;;match
($m (match-string 0 ,str))
;;after
($a (substring ,str (match-end 0) (length ,str))))
,@body)))))))
(defun decamelize (string)
"Convert from CamelCaseString to camel_case_string."
(let ((case-fold-search nil))
(downcase
(replace-regexp-in-string
"\\([A-Z]+\\)\\([A-Z][a-z]\\)" "\\1_\\2"
(replace-regexp-in-string
"\\([a-z0-9]\\)\\([A-Z]\\)" "\\1_\\2"
string)))))
(defun string-not-empty (str) ;(+)
"Return t if string STR is not empty."
(and (stringp str) (not (or (string-equal "" str)
(string-match "^ +$" str)))))
(defun yml-value (name)
"Return the value of the parameter named NAME in the current
buffer or an empty string."
(save-excursion
(goto-char (point-min))
(if (search-forward-regexp (format "%s:[ ]*\\(.*\\)[ ]*$" name) nil t)
(match-string 1)
"")))
(defun current-line-string ()
"Return the string value of the current line."
(buffer-substring-no-properties
(progn (beginning-of-line) (point))
(progn (end-of-line) (point))))
(defun remove-prefix (word prefix)
"Remove the PREFIX string in WORD if it exists.
PrefixBla -> Bla."
(replace-regexp-in-string (format "^%s" prefix) "" word))
(defun remove-postfix (word postfix)
"Remove the POSTFIX string in WORD if it exists.
BlaPostfix -> Bla."
(replace-regexp-in-string (format "%s$" postfix) "" word))
(defun strings-join (separator strings)
"Join all STRINGS using a SEPARATOR."
(mapconcat 'identity strings separator))
(defalias 'string-join 'strings-join)
(defun capital-word-p (word)
"Return t if first letter of WORD is uppercased."
(= (elt word 0)
(elt (capitalize word) 0)))
;;; Define keys
(defmacro define-keys (key-map &rest key-funcs)
"Define key bindings for KEY-MAP (create KEY-MAP, if it does
not exist."
`(progn
(unless (boundp ',key-map)
(setf ,key-map (make-sparse-keymap)))
,@(mapcar
#'(lambda (key-func)
`(define-key ,key-map ,(first key-func) ,(second key-func)))
key-funcs)))
;; Files
(defun append-string-to-file (file string)
"Append a string to end of a file."
(write-region string nil file t))
(defun write-string-to-file (file string)
"Write a string to a file (erasing the previous content)."
(write-region string nil file))
(defun read-from-file (file-name)
"Read sexpr from a file named FILE-NAME."
(with-temp-buffer
(insert-file-contents file-name)
(read (current-buffer))))
;; File hierarchy functions
(defun directory-files-recursive (directory &optional full match nosort base)
"Like `directory-files' but traversed recursively."
(apply #'append
(mapcar (lambda (file)
(cond ((and (file-regular-p (concat directory "/" file))
(or (not match) (string-match match file)))
(list (concat base file)))
((and (file-directory-p (concat directory "/" file))
(not (string-match "^\\." file)))
(directory-files-recursive (concat directory "/" file) full match nosort (concat base file "/")))))
(ignore-errors (directory-files directory full nil nosort)))))
(defun directory-name (path)
"Return the name of a directory with a given path.
For example, (path \"/foo/bar/baz/../\") returns bar."
;; Rewrite me
(let ((old-path default-directory))
(cd path)
(let ((dir (pwd)))
(cd old-path)
(replace-regexp-in-string "^Directory[ ]*" "" dir))))
(defun find-or-ask-to-create (question file)
"Open file if it exists. If it does not exist, ask to create
it."
(if (file-exists-p file)
(find-file file)
(when (y-or-n-p question)
(when (string-match "\\(.*\\)/[^/]+$" file)
(make-directory (match-string 1 file) t))
(find-file file))))
(defun directory-of-file (file-name)
"Return the parent directory of a file named FILE-NAME."
(replace-regexp-in-string "[^/]*$" "" file-name))
(defmacro* in-directory ((directory) &rest body)
(let ((before-directory (gensym)))
`(let ((,before-directory default-directory)
(default-directory ,directory))
(cd ,directory)
,@body
(cd ,before-directory))))
;; Buffers
(defun buffer-string-by-name (buffer-name)
"Return the content of buffer named BUFFER-NAME as a string."
(interactive)
(save-excursion
(set-buffer buffer-name)
(buffer-string)))
(defun buffer-visible-p (buffer-name)
"Return t when BUFFER-NAME is visible in any frame."
(if (get-buffer-window buffer-name t) t nil))
;; Misc
(defun rails-browse-api-url (url)
"Browse preferentially with Emacs w3m browser."
(if rails-browse-api-with-w3m
(when (fboundp 'w3m-find-file)
(w3m-find-file (remove-prefix url "file://")))
(rails-alternative-browse-url url)))
(defun rails-alternative-browse-url (url &rest args)
"Fix a problem with Internet Explorer not being able to load
URLs with anchors via ShellExecute. It will only be invoked it
the user explicit sets `rails-use-alternative-browse-url'."
(if (and (eq system-type 'windows-nt) rails-use-alternative-browse-url)
(w32-shell-execute "open" "iexplore" url)
(browse-url url args)))
;; Colorize
(defun apply-colorize-to-buffer (name)
(let ((buffer (current-buffer)))
(set-buffer name)
(make-local-variable 'after-change-functions)
(add-hook 'after-change-functions
'(lambda (start end len)
(ansi-color-apply-on-region start end)))
(set-buffer buffer)))
;; completion-read
(defun rails-completing-read (prompt table history require-match)
(let ((history-value (symbol-value history)))
(list (completing-read
(format "%s?%s: "
prompt
(if (car history-value)
(format " (%s)" (car history-value))
""))
(list->alist table) ; table
nil ; predicate
require-match ; require-match
nil ; initial input
history ; hist
(car history-value))))) ;def
;; railsy-replace
(defun camelized-p (string)
"Return nil unless string is in camelized format (first character is capital, there is at least on lower capital and all characters are letters of numbers"
(let ((case-fold-search nil))
(string-match "^[A-Z][A-Za-z0-9]*[a-z]+[A-Za-z0-9]*$" string)))
(defun underscored-p (string)
"Return nil unless string is in underscored format (containing only lower case characters, numbers or underscores)"
(let ((case-fold-search nil))
(string-match "^[a-z][a-z0-9_]*$" string)))
(defun replace-rails-variable ()
(interactive)
)
;; MMM
;; (defvar mmm-indent-sandbox-finish-position nil)
;; (defun mmm-run-indent-with-sandbox (indent-func)
;; (interactive)
;; (let* ((fragment-name "*mmm-indent-sandbox*")
;; (ovl (mmm-overlay-at (point)))
;; (current (when ovl (overlay-buffer ovl)))
;; (start (when ovl (overlay-start ovl)))
;; (end (when ovl (overlay-end ovl)))
;; (current-pos (when ovl (point)))
;; (ovl-line-start (when start
;; (progn (goto-char start)
;; (line-beginning-position))))
;; (current-line-start (when current-pos
;; (progn (goto-char current-pos)
;; (line-beginning-position))))
;; (fragment-pos (when (and start end) (- (point) (- start 1))))
;; (ovl-offset (when ovl (- (progn
;; (goto-char start)
;; (while (not (looking-at "<"))
;; (goto-char (- (point) 1)))
;; (point))
;; ovl-line-start)))
;; (content (when (and start end) (buffer-substring-no-properties start end)))
;; (fragment (when content (get-buffer-create fragment-name))))
;; (when fragment
;; (setq mmm-indent-sandbox-finish-position nil)
;; (save-excursion
;; (set-buffer fragment-name)
;; (goto-char (point-min))
;; (insert content)
;; (goto-char fragment-pos)
;; (funcall indent-func t)
;; (let ((start-line)
;; (end-line)
;; (kill-after-start)
;; (finish-pos (- (+ start (point)) 1))
;; (indented (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
;; (set-buffer current)
;; (kill-buffer fragment-name)
;; (princ ovl-offset)
;; (goto-char current-pos)
;; (setq start-line (line-beginning-position))
;; (setq end-line (line-end-position))
;; (when (> start start-line)
;; (setq start-line (+ start 1))
;; (setq kill-after-start t))
;; (when (> end-line end)
;; (setq end-line end))
;; (kill-region start-line end-line)
;; (goto-char start-line)
;; (unless (= ovl-line-start current-line-start)
;; (dotimes (i ovl-offset)
;; (setq indented (concat " " indented))))
;; ;; (insert-char (string-to-char " ") ovl-offset))
;; (insert indented)
;; (when kill-after-start
;; (goto-char (+ start 1))
;; (backward-delete-char 1))
;; ;; (setq mmm-indent-sandbox-finish-position finish-pos)))
;; (if (= ovl-line-start current-line-start)
;; (setq mmm-indent-sandbox-finish-position finish-pos)
;; (setq mmm-indent-sandbox-finish-position (+ finish-pos ovl-offset)))))
;; (goto-char mmm-indent-sandbox-finish-position))))
;; (defadvice ruby-indent-line (around mmm-sandbox-ruby-indent-line)
;; (if (and (fboundp 'mmm-overlay-at)
;; (mmm-overlay-at (point)))
;; (mmm-run-indent-with-sandbox 'ruby-indent-line)
;; ad-do-it))
;; (ad-activate 'ruby-indent-line)
;; Cross define functions from my rc files
(provide 'rails-lib)