Guest User

Untitled

a guest
Apr 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. " plugin/firefox.vim
  2. if !has('ruby')
  3. finish
  4. endif
  5.  
  6. ruby << RUBY
  7. module Vim
  8. def self.var(name)
  9. evaluate("exists('#{name}')") == '1' ? parse(name) : nil
  10. end
  11.  
  12. CONVERSION = [
  13. # Number
  14. proc {|e| evaluate(e).to_i(0) },
  15. # String
  16. proc {|e| evaluate(e) },
  17. # Funcref
  18. proc {|e|
  19. proc {|args|
  20. parse "call(#{e}, [#{args.map {|a| a.inspect }.join(',')}])"
  21. }
  22. },
  23. # List
  24. proc {|e|
  25. (0...parse("len(#{e})")).map {|i|
  26. parse("#{e}[#{i}]")
  27. }
  28. },
  29. # Dictionary
  30. proc {|e|
  31. parse("keys(#{e})").inject({}) {|h, k|
  32. h.merge(k => parse("#{e}['#{k}']"))
  33. }
  34. }
  35. ]
  36.  
  37. def self.parse(expr)
  38. type = evaluate("type(#{expr})").to_i
  39. CONVERSION[type].call(expr)
  40. end
  41. end
  42.  
  43. require 'net/telnet'
  44. require 'singleton'
  45.  
  46. class Firefox
  47. include Singleton
  48.  
  49. DEFAULT_CONFIG = {
  50. 'host' => 'localhost',
  51. 'port' => 4242,
  52. 'mappings' => [
  53. [/^/, 'file://']
  54. ]
  55. }.freeze
  56.  
  57. def initialize
  58. conf = DEFAULT_CONFIG.merge(Vim.var('g:firefox') || {})
  59. @conn = Net::Telnet.new('Host' => conf['host'], 'Port' => conf['port'])
  60. @mappings = conf['mappings'].map {|k, v| [Regexp.compile(k), v] }
  61. end
  62.  
  63. def open(uri)
  64. set 'content.location.href', uri
  65. end
  66.  
  67. def open_buffer
  68. return unless path = $curbuf.name and map = @mappings.find {|k, v| k =~ path }
  69. open path.sub(*map)
  70. end
  71.  
  72. def reload
  73. call 'content.location.reload', true
  74. end
  75.  
  76. def call(method, *args)
  77. @conn.cmd("#{method}(#{args.map {|a| a.inspect }.join(',')})")
  78. end
  79.  
  80. def set(prop, value)
  81. @conn.cmd("#{prop} = #{value.inspect}")
  82. end
  83. end
  84. RUBY
  85.  
  86. command! -nargs=1 FirefoxOpen ruby Firefox.instance.open(<q-args>)
  87. command! FirefoxOpenBuffer ruby Firefox.instance.open_buffer
  88. command! FirefoxReload ruby Firefox.instance.reload
  89. command! FirefoxStartObserve autocmd BufWritePost <buffer> :FirefoxReload
  90. command! FirefoxStopObserve autocmd! BufWritePost <buffer>
  91.  
  92. command! -bar -nargs=1 OpenURL :FirefoxOpen <args>
Add Comment
Please, Sign In to add comment