Class: Editserver

Inherits:
Object
  • Object
show all
Defined in:
lib/editserver.rb,
lib/editserver/editor.rb,
lib/editserver/command.rb,
lib/editserver/version.rb,
lib/editserver/response.rb,
lib/editserver/terminal/vim.rb,
lib/editserver/terminal/emacs.rb

Defined Under Namespace

Classes: Command, EditError, Editor, Emacs, Response, RoutingError, Vim

Constant Summary collapse

GUI_EDITORS =
{
  # OS X editors
  'mate'   => 'mate -w',
  'mvim'   => 'mvim --nofork --servername EDITSERVER', # does not return when app must be launched
  'kod'    => 'open -a Kod -W',                        # app must quit to release control
  'bbedit' => 'bbedit -w'                              # does not open file properly when app is launched
}
VERSION =
'0.1.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Editserver

TODO: We are playing with global constants here;

this is simple, but we should stop


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/editserver.rb', line 23

def initialize options = {}
  opts            = options.dup
  Editor.terminal = opts.delete 'terminal'

  # seed with terminal-based editors, whose server/client modes are a bit too
  # complicated to configure dynamically
  @editors = {
    'vim'   => Vim,
    'emacs' => Emacs
  }

  register_editors GUI_EDITORS.merge(opts)
end

Instance Attribute Details

#editorsObject (readonly)

Returns the value of attribute editors.



19
20
21
# File 'lib/editserver.rb', line 19

def editors
  @editors
end

Instance Method Details

#call(env) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/editserver.rb', line 76

def call env
  request = Rack::Request.new env
  klass   = editor request.path_info
  Response.new(klass.new, request).call
rescue RoutingError => e
  warn e.to_s
  res = Rack::Response.new
  res.status = 500
  res.finish
end

#editor(path_info) ⇒ Object

returns Editserver handler based on path

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/editserver.rb', line 63

def editor path_info
  path = path_info[%r(\A([/\w\.-]+)), 1]

  if klass = editors[path[/\/(.*)/, 1]]
    klass
  elsif path == '/'
    klass = editors[editors['default']]
  end

  raise RoutingError, "No handler for #{path}" if klass.nil?
  klass
end

#register_editors(options = {}) ⇒ Object

returns Hash of name => EditorClass



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/editserver.rb', line 38

def register_editors options = {}
  opts = options.dup

  if default = opts.delete('default')
    @editors['default'] = default
  end

  # rest should be editor definitions
  opts.each do |name, cmd|
    klass = pascalize name
    editor, params = cmd.shellsplit.partition.with_index { |w,i| i.zero? }

    self.class.class_eval %Q(                                  # e.g. mate: mate -w
      class #{klass} < Editor                                  # class Mate < Editor
        define_editor #{editor[0].inspect}, *#{params.inspect} #   define_editor 'mate', *['-w']
      end                                                      # end
    )

    @editors[name] = self.class.const_get klass.to_sym
  end

  @editors
end