public
Description: Rinari Is Not A Rails IDE
Homepage: http://rinari.rubyforge.org
Clone URL: git://github.com/technomancy/rinari.git
rinari / rails-scripts.el
100644 123 lines (110 sloc) 4.092 kb
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
;;; rails-script.el --- functions for executing Ruby on Rails scripts
 
;;; Commentary:
 
;; Part of Rinari (with pieces taken from emacs-rails)
 
;;; Todo:
 
;; - some scripts require arguments so shouldn't need the prefix
;; argument, these scripts (generators, etc...) should for review of
;; the arguments
;;
 
;;; Code:
(require 'ansi-color)
 
(defvar rails-default-environment
  "eschulte"
  "the default environment to be used when running rails/scripts")
 
;; TODO:
;; - should allow to edit options with a prefix argument
;;;###autoload
(defun rails-script (arg)
  (interactive "P")
  (let* ((script (expand-file-name
     (if (or (equalp ido-mode 'file)
       (equalp ido-mode 'both))
     (ido-read-file-name "script: " (concat (rails-root)
               "/script"))
     (read-file-name "script: " (concat (rails-root)
             "/script")))))
   (command (file-name-nondirectory script)))
    (cond
      ;; console: start inferior ruby process and jump to the buffer
      ((string-equal command "console")
       (rails-script-console arg))
      ;; server: filter ascii colors and dump to a compilation buffer
      ((string-equal command "server")
       (rails-script-server arg))
      ;; all others execute and message the output
      (t
       (message "%s"
    (shell-command-to-string script))))))
 
;; console
(defun rails-script-console (arg)
  (interactive "P")
  (let* ((arguments (list
     (if arg
       (read-from-minibuffer "arguments to script/console: "
           rails-default-environment)
     rails-default-environment)))
   (buffer (apply 'make-comint
      "console"
      (concat (rails-root) "/script/console")
      nil
      arguments)))
    (pop-to-buffer buffer)
    (inferior-ruby-mode)
    ;; rails/console regexps
    (make-local-variable 'inferior-ruby-first-prompt-pattern)
    (make-local-variable 'inferior-ruby-prompt-pattern)
    (setq inferior-ruby-first-prompt-pattern "^>> "
   inferior-ruby-prompt-pattern "^>> ")))
 
;; server
(defvar rails-server-error-regexp
  ;; "\\([^[:space:]]*\\.r[bhtml]+\\):\\([[:digit:]]+\\)"
  "^[[:space:]]+\\([^[:space:]]*\\):\\([[:digit:]]+\\)"
  "regular expression to match errors in the server logs")
 
(defvar rails-server-compilation-error-regexp-alist
  `((,rails-server-error-regexp 1 2))
  "a version of `compilation-error-regexp-alist' to be used in
rails logs (should be used with `make-local-variable')")
 
(defun rails-script-server (arg)
  (interactive "P")
  (let* ((default-directory (rails-root))
   (default-arguments (format "-e %s" rails-default-environment))
   (arguments (split-string
     (if arg
       (read-from-minibuffer
       "arguments to script/server: "
       default-arguments)
     default-arguments)
     " "))
   (mes (message "dir %s" default-directory))
   (buffer (apply 'make-comint
      "server"
      (concat (rails-root) "/script/server")
      nil
      arguments)))
    (save-excursion
      (set-buffer buffer)
      ;; remove ascii coloring tokens
      (set-process-filter (get-buffer-process buffer)
       'rails-script-server-insertion-filter)
      ;; allow jumping to error messages
      (set (make-variable-buffer-local 'compilation-error-regexp-alist)
   rails-server-compilation-error-regexp-alist)
      ;; simple kill server (not ideal but \C-c\C-c is taken)
      (local-set-key "\C-x\C-c" 'comint-interrupt-subjob)
      (compilation-minor-mode)
      ;; TODO: add something to kill the process and filter when the buffer is killed
      (message "started script/server %s" (join-string arguments " ")))))
 
(defun rails-script-server-insertion-filter (proc string)
  (with-current-buffer (process-buffer proc)
    (let ((moving (= (point) (process-mark proc))))
      (save-excursion
  ;; Insert the text, advancing the process marker.
  (goto-char (process-mark proc))
  ;; translate ansi color codes
  ;; (insert (ansi-color-apply string))
  ;; ;; remove ansi color codes
  (insert (ansi-color-filter-apply string))
  (set-marker (process-mark proc) (point)))
      (if moving (goto-char (process-mark proc))))))
 
(provide 'rails-scripts)
;;; rails-scripts.el ends here