/ emacs.d / rails / rails-cmd-proxy.el
rails-cmd-proxy.el
  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: svn://rubyforge.org/var/svn/emacs-rails/trunk/rails-cmd-proxy.el $
  9  ;; $Id: rails-cmd-proxy.el 158 2007-04-03 08:45:46Z dimaexe $
 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 (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       (start-process-shell-command name
 83                                    buffer
 84                                    command
 85                                    command-args))))
 86  
 87  (defun rails-cmd-proxy:shell-command-to-string (command)
 88    (rails-project:with-root
 89     (root)
 90     (let ((proxy-struct (rails-cmd-proxy:lookup root))
 91           (command command))
 92       (when proxy-struct
 93         (setq command
 94               (format "%s %s"
 95                       rails-cmd-proxy:remote-cmd
 96                       (rails-cmd-proxy:construct-remote-cmd proxy-struct
 97                                                             root
 98                                                             command))))
 99       (shell-command-to-string command))))
100  
101  ;; helper functions
102  
103  (defun rails-cmd-proxy:convert-buffer-from-remote (start end len)
104    (when-bind
105     (struct (rails-cmd-proxy:lookup default-directory))
106     (save-excursion
107       (goto-char start)
108       (let* ((local (rails-cmd-proxy:struct-local struct))
109              (remote (rails-cmd-proxy:struct-remote struct))
110              (root default-directory)
111              (remote-with-root (concat remote (substring root (length local))))
112              (buffer-read-only nil)
113              point)
114         (while (setq point (re-search-forward (format "^\\s-*\\(%s\\)"
115                                                       remote-with-root) end t))
116           (replace-match (format "%s "
117                                  (string-repeat " " (- (length (match-string 1)) 1)))
118                          nil t nil 1))))))
119  
120  (provide 'rails-cmd-proxy)