forked from remvee/emacs-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rails-cmd-proxy.el
161 lines (139 loc) · 6.36 KB
/
rails-cmd-proxy.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
;;; rails-cmd-proxy.el ---
;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
;; Authors: Dmitry Galinsky <dima dot exe 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:
(defstruct rails-cmd-proxy:struct local remote args)
(defvar rails-cmd-proxy:directories-list
'(("y:" "/mnt/www" "-t @server-cmd")))
(defvar rails-cmd-proxy:remote-cmd
"plink")
(defun rails-cmd-proxy:lookup (root &optional lookup-local)
"Lookup ROOT using `rails-cmd-proxy:directories-list' and
return the `rails-cmd-proxy:struct'. If not found ROOT return
nil."
(loop for (local remote args) in rails-cmd-proxy:directories-list
when (string-match (concat "^" (if lookup-local remote local)) root)
do (return
(make-rails-cmd-proxy:struct
:local local
:remote remote
:args args))))
(defun rails-cmd-proxy:convert (proxy-struct path &optional reverse)
"Convert PATH from local to remote using PROXY-STRUCT,
otherwise if set REVERSE convert from remote to local."
(let* ((local (rails-cmd-proxy:struct-local proxy-struct))
(remote (rails-cmd-proxy:struct-remote proxy-struct))
(regexp (concat "^" (if reverse remote local)))
(replacement (if reverse local remote)))
(when (string-match regexp path)
(replace-regexp-in-string regexp replacement path))))
(defun rails-cmd-proxy:construct-remote-cmd (proxy-struct root command &optional command-args)
(let ((root (rails-cmd-proxy:convert proxy-struct root))
(args (rails-cmd-proxy:struct-args proxy-struct)))
(if command-args
(format "%s \"cd %s && %s %s\"" args root command command-args)
(format "%s \"cd %s && %s\"" args root command))))
;; remote wrappers
(defun rails-cmd-proxy:start-process-color (name buffer command command-args)
""
(rails-project:with-root
(root)
(let ((proxy-struct (rails-cmd-proxy:lookup root))
(command command)
(command-args command-args))
(when proxy-struct
(setq command-args
(rails-cmd-proxy:construct-remote-cmd proxy-struct
root
command
command-args))
(setq command rails-cmd-proxy:remote-cmd))
(let ((process (start-process-shell-command name
buffer
command
command-args)))
(set-process-filter process
'ansi-color-insertion-filter)
process))))
(defun rails-cmd-proxy:start-process (name buffer command command-args)
""
(rails-project:with-root
(root)
(let ((proxy-struct (rails-cmd-proxy:lookup root))
(command command)
(command-args command-args))
(when proxy-struct
(setq command-args
(rails-cmd-proxy:construct-remote-cmd proxy-struct
root
command
command-args))
(setq command rails-cmd-proxy:remote-cmd))
(save-excursion
(set-buffer (get-buffer-create buffer))
(set (make-local-variable 'comint-scroll-to-bottom-on-output) t)
(set (make-local-variable 'compilation-error-regexp-alist)
rails-error-regexp-alist)
(compilation-shell-minor-mode t)
(rails-minor-mode t))
(start-process-shell-command name
buffer
command
command-args))))
(defun rails-cmd-proxy:shell-command-to-string (command)
(rails-project:with-root
(root)
(let ((proxy-struct (rails-cmd-proxy:lookup root))
(command command))
(when proxy-struct
(setq command
(format "%s %s"
rails-cmd-proxy:remote-cmd
(rails-cmd-proxy:construct-remote-cmd proxy-struct
root
command))))
(shell-command-to-string command))))
;; helper functions
(defun rails-cmd-proxy:convert-buffer-from-remote (start end len)
(when-bind
(struct (rails-cmd-proxy:lookup default-directory))
(save-excursion
(goto-char start)
(let* ((local (rails-cmd-proxy:struct-local struct))
(remote (rails-cmd-proxy:struct-remote struct))
(root default-directory)
(remote-with-root (concat remote (substring root (length local))))
(buffer-read-only nil)
point)
(while (setq point (re-search-forward (format "^\\s-*\\(%s\\)"
remote-with-root) end t))
(replace-match (format "%s "
(string-repeat " " (- (length (match-string 1)) 1)))
nil t nil 1))))))
(defun ansi-color-insertion-filter (proc string)
(with-current-buffer (process-buffer proc)
(let ((buffer-read-only nil)
(moving (= (point) (process-mark proc))))
(save-excursion
;; Insert the text, advancing the process marker.
(goto-char (process-mark proc))
;; decode ansi color sequences
(insert (ansi-color-apply string))
(set-marker (process-mark proc) (point)))
(if moving (goto-char (process-mark proc))))))
(provide 'rails-cmd-proxy)