Syntax highlighting and more in ruby

In a previous post I used the ‘syntax’ gem to process text into tags which could then be tagged with terminal colour codes.

There’s a plugin which already does all of this called coderay, I will combine this with highline (to control the input and output and maybe create an interface too)

require "rubygems"
require "highline/import"
require "coderay"

class Editor
  def say(saying)
    puts saying
  def getBinding
    return binding()

e =
e.say("hello world")

# now for a bit of monkey typing
monkey = <<MONKEY
  def reverseSaying(saying)

  self.reverseSaying("hello world")

puts monkey

eval("reverseSaying('hello world')",e.getBinding)

# now try to monkey patch user input
usermonkeysay = ask("Monkey do?")
puts CodeRay.scan(usermonkeysay,:ruby).term

One thing you might have noticed is that I have a function called getBinding in the ‘Editor’ class, what this does is return a handle to the current class to modify that class programmatically.

When we have the binding, we can do evil things on that class. I mean ‘eval’, but rather than a straight up eval you can do an eval within the context of that class.

And in ruby you can eval this inside of the context of the class

  def reverseSaying(saying)

Something like a ‘def’ in the context of the binding will let you run that function later on using the binding.

What does this mean?

A text editor which took advantage of this (something like diakonos, redcar maybe) would be able to provide user friendly commands such as open, save, gotoLine, regex, replace, column(x,y, w, h), select(x,y,w,h), copy, paste.

This would provide a clear level of abstraction between a command mode and text entry in editors.

My plan is to have a 3 pane editor with a readonly text view, a text entry box and a command box.

This entry was posted in Uncategorized by admin. Bookmark the permalink.

Comments are closed.