Sha256: 7965b614a9a56691fe285fd82df75ca46bb4f3871e1b3ebc204d778b2789e8e1

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Textbringer
  class Mode
    extend Commands
    include Commands
    
    @@mode_list = []
    
    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
    end

    def self.define_generic_command(name)
      command_name = (name.to_s + "_command").intern
      define_command(command_name) do |*args|
        begin
          Buffer.current.mode.send(name, *args)
        rescue NoMethodError => e
          if e.receiver == 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.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)
    end

    attr_reader :buffer

    def initialize(buffer)
      @buffer = buffer
    end

    def name
      self.class.mode_name
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
textbringer-0.1.7 lib/textbringer/mode.rb
textbringer-0.1.6 lib/textbringer/mode.rb
textbringer-0.1.5 lib/textbringer/mode.rb
textbringer-0.1.4 lib/textbringer/mode.rb