Sha256: 14f0d9faff3675f003aaa81cfe005eee2bfb68e71161fd085bd9328c8ce3c55e

Contents?: true

Size: 1.99 KB

Versions: 11

Compression:

Stored size: 1.99 KB

Contents

module Textbringer
  class Mode
    extend Commands
    include Commands

    @@mode_list = []

    DEFAULT_SYNTAX_TABLE = {
      control: /[\0-\t\v-\x1f\x7f\u{3000}]+/
    }

    def self.list
      @@mode_list
    end

    class << self
      attr_accessor :mode_name
      attr_accessor :command_name
      attr_accessor :hook_name
      attr_accessor :file_name_pattern
      attr_accessor :interpreter_name_pattern
      attr_reader :syntax_table
    end

    def self.define_generic_command(name, **options)
      command_name = (name.to_s + "_command").intern
      define_command(command_name, **options) do |*args|
        begin
          Buffer.current.mode.send(name, *args)
        rescue NoMethodError => e
          if (e.receiver rescue nil) == Buffer.current.mode && e.name == name
            raise EditorError,
              "#{command_name} is not supported in the current mode"
          else
            raise
          end
        end
      end
    end

    def self.define_local_command(name, **options, &block)
      define_generic_command(name, **options)
      define_method(name, &block)
      name
    end

    def self.define_syntax(face_name, re)
      @syntax_table[face_name] = re
    end

    def self.inherited(child)
      base_name = child.name.slice(/[^:]*\z/)
      child.mode_name = base_name.sub(/Mode\z/, "")
      command_name = base_name.sub(/\A[A-Z]/) { |s| s.downcase }.
        gsub(/(?<=[a-z])([A-Z])/) {
          "_" + $1.downcase
        }
      command = command_name.intern
      hook = (command_name + "_hook").intern
      child.command_name = command
      child.hook_name = hook
      define_command(command) do
        Buffer.current.apply_mode(child)
      end
      @@mode_list.push(child)
      child.instance_variable_set(:@syntax_table, DEFAULT_SYNTAX_TABLE.dup)
    end

    attr_reader :buffer

    def initialize(buffer)
      @buffer = buffer
    end

    def name
      self.class.mode_name
    end

    def syntax_table
      self.class.syntax_table
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
textbringer-1.4.1 lib/textbringer/mode.rb
textbringer-1.3.0 lib/textbringer/mode.rb
textbringer-1.2.0 lib/textbringer/mode.rb
textbringer-1.1.2 lib/textbringer/mode.rb
textbringer-1.1.1 lib/textbringer/mode.rb
textbringer-1.1.0 lib/textbringer/mode.rb
textbringer-1.0.9 lib/textbringer/mode.rb
textbringer-1.0.4 lib/textbringer/mode.rb
textbringer-1.0.3 lib/textbringer/mode.rb
textbringer-1.0.2 lib/textbringer/mode.rb
textbringer-1.0.1 lib/textbringer/mode.rb