1
;;; rails-cmd-proxy.el ---
2
3
;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
4
5
;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>
6
7
;; Keywords: ruby rails languages oop
8
;; $URL$
9
;; $Id$
10
11
;;; License
12
13
;; This program is free software; you can redistribute it and/or
14
;; modify it under the terms of the GNU General Public License
15
;; as published by the Free Software Foundation; either version 2
16
;; of the License, or (at your option) any later version.
17
18
;; This program is distributed in the hope that it will be useful,
19
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
;; GNU General Public License for more details.
22
23
;; You should have received a copy of the GNU General Public License
24
;; along with this program; if not, write to the Free Software
25
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26
27
;;; Code:
28
29
(defstruct rails-cmd-proxy:struct local remote args)
30
31
(defvar rails-cmd-proxy:directories-list
32
  '(("y:" "/mnt/www" "-t @server-cmd")))
33
34
(defvar rails-cmd-proxy:remote-cmd
35
  "plink")
36
37
(defun rails-cmd-proxy:lookup (root &optional lookup-local)
38
  "Lookup ROOT using `rails-cmd-proxy:directories-list' and
39
return the `rails-cmd-proxy:struct'. If not found ROOT return
40
nil."
41
  (loop for (local remote args) in rails-cmd-proxy:directories-list
42
        when (string-match (concat "^" (if lookup-local remote local)) root)
43
        do (return
44
            (make-rails-cmd-proxy:struct
45
             :local local
46
             :remote remote
47
             :args args))))
48
49
(defun rails-cmd-proxy:convert (proxy-struct path &optional reverse)
50
  "Convert PATH from local to remote using PROXY-STRUCT,
51
otherwise if set REVERSE convert from remote to local."
52
  (let* ((local (rails-cmd-proxy:struct-local proxy-struct))
53
         (remote (rails-cmd-proxy:struct-remote proxy-struct))
54
         (regexp (concat "^" (if reverse remote local)))
55
         (replacement (if reverse local remote)))
56
    (when (string-match regexp path)
57
      (replace-regexp-in-string regexp replacement path))))
58
59
(defun rails-cmd-proxy:construct-remote-cmd (proxy-struct root command &optional command-args)
60
  (let ((root (rails-cmd-proxy:convert proxy-struct root))
61
        (args (rails-cmd-proxy:struct-args proxy-struct)))
62
    (if command-args
63
        (format "%s \"cd %s && %s %s\"" args root command command-args)
64
      (format "%s \"cd %s && %s\"" args root command))))
65
66
;; remote wrappers
67
68
(defun rails-cmd-proxy:start-process-color (name buffer command command-args)
69
  ""
70
  (rails-project:with-root
71
   (root)
72
   (let ((proxy-struct (rails-cmd-proxy:lookup root))
73
         (command command)
74
         (command-args command-args))
75
     (when proxy-struct
76
       (setq command-args
77
             (rails-cmd-proxy:construct-remote-cmd proxy-struct
78
                                                   root
79
                                                   command
80
                                                   command-args))
81
       (setq command rails-cmd-proxy:remote-cmd))
82
     (let ((process (start-process-shell-command name
83
                                                 buffer
84
                                                 command
85
                                                 command-args)))
86
       (set-process-filter process
87
                           'ansi-color-insertion-filter)
88
       process)))) 
89
90
(defun rails-cmd-proxy:start-process (name buffer command command-args)
91
  ""
92
  (rails-project:with-root
93
   (root)
94
   (let ((proxy-struct (rails-cmd-proxy:lookup root))
95
         (command command)
96
         (command-args command-args))
97
     (when proxy-struct
98
       (setq command-args
99
             (rails-cmd-proxy:construct-remote-cmd proxy-struct
100
                                                   root
101
                                                   command
102
                                                   command-args))
103
       (setq command rails-cmd-proxy:remote-cmd))
104
     (save-excursion
105
       (set-buffer (get-buffer-create buffer))
106
       (set (make-local-variable 'comint-scroll-to-bottom-on-output) t)
107
       (set (make-local-variable 'compilation-error-regexp-alist)
108
	    rails-error-regexp-alist)
109
       (compilation-shell-minor-mode t)
110
       (rails-minor-mode t))
111
     (start-process-shell-command name
112
                                  buffer
113
                                  command
114
                                  command-args))))
115
116
(defun rails-cmd-proxy:shell-command-to-string (command)
117
  (rails-project:with-root
118
   (root)
119
   (let ((proxy-struct (rails-cmd-proxy:lookup root))
120
         (command command))
121
     (when proxy-struct
122
       (setq command
123
             (format "%s %s"
124
                     rails-cmd-proxy:remote-cmd
125
                     (rails-cmd-proxy:construct-remote-cmd proxy-struct
126
                                                           root
127
                                                           command))))
128
     (shell-command-to-string command))))
129
130
;; helper functions
131
132
(defun rails-cmd-proxy:convert-buffer-from-remote (start end len)
133
  (when-bind
134
   (struct (rails-cmd-proxy:lookup default-directory))
135
   (save-excursion
136
     (goto-char start)
137
     (let* ((local (rails-cmd-proxy:struct-local struct))
138
            (remote (rails-cmd-proxy:struct-remote struct))
139
            (root default-directory)
140
            (remote-with-root (concat remote (substring root (length local))))
141
            (buffer-read-only nil)
142
            point)
143
       (while (setq point (re-search-forward (format "^\\s-*\\(%s\\)"
144
                                                     remote-with-root) end t))
145
         (replace-match (format "%s "
146
                                (string-repeat " " (- (length (match-string 1)) 1)))
147
                        nil t nil 1))))))
148
149
(defun ansi-color-insertion-filter (proc string)
150
  (with-current-buffer (process-buffer proc)
151
    (let ((buffer-read-only nil)
152
          (moving (= (point) (process-mark proc))))
153
      (save-excursion
154
        ;; Insert the text, advancing the process marker.
155
        (goto-char (process-mark proc))
156
        ;; decode ansi color sequences
157
        (insert (ansi-color-apply string))
158
        (set-marker (process-mark proc) (point)))
159
      (if moving (goto-char (process-mark proc))))))
160
161
(provide 'rails-cmd-proxy)