Class: Textbringer::Mode

Inherits:
Object
  • Object
show all
Extended by:
Commands
Includes:
Commands
Defined in:
lib/textbringer/mode.rb

Constant Summary collapse

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

Constants included from Commands

Commands::CLIPBOARD_AVAILABLE, Commands::CTAGS, Commands::HELP_RING, Commands::ISEARCH_STATUS, Commands::KEYBOARD_MACROS, Commands::REGISTERS, Commands::RE_SEARCH_STATUS

Constants included from Utils

Utils::COMPLETION, Utils::HOOKS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Commands

[], command_help, command_table, current_prefix_arg, define_command, execute_keyboard_macro, get_tags, isearch_done, isearch_mode, isearch_mode?, isearch_pre_command_hook, isearch_prompt, isearch_repeat, isearch_repeat_backward, isearch_repeat_forward, isearch_search, keymap_bindings, match_beginning, match_end, match_string, number_prefix_arg, prefix_numeric_value, read_input_method_name, read_keyboard_macro, read_register, replace_match, undefine_command, universal_argument_mode

Methods included from Utils

add_hook, background, complete_for_minibuffer, foreground, foreground!, message, read_buffer, read_char, read_command_name, read_encoding, read_event, read_expression, read_file_name, read_from_minibuffer, read_key_sequence, read_object, read_single_char, received_keyboard_quit?, remove_hook, ruby_install_name, run_hooks, self_insert_and_exit_minibuffer, set_transient_map, show_exception, sit_for, sleep_for, y_or_n?, yes_or_no?

Constructor Details

#initialize(buffer) ⇒ Mode

Returns a new instance of Mode.



71
72
73
# File 'lib/textbringer/mode.rb', line 71

def initialize(buffer)
  @buffer = buffer
end

Class Attribute Details

.command_nameObject

Returns the value of attribute command_name.



18
19
20
# File 'lib/textbringer/mode.rb', line 18

def command_name
  @command_name
end

.file_name_patternObject

Returns the value of attribute file_name_pattern.



20
21
22
# File 'lib/textbringer/mode.rb', line 20

def file_name_pattern
  @file_name_pattern
end

.hook_nameObject

Returns the value of attribute hook_name.



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

def hook_name
  @hook_name
end

.interpreter_name_patternObject

Returns the value of attribute interpreter_name_pattern.



21
22
23
# File 'lib/textbringer/mode.rb', line 21

def interpreter_name_pattern
  @interpreter_name_pattern
end

.mode_nameObject

Returns the value of attribute mode_name.



17
18
19
# File 'lib/textbringer/mode.rb', line 17

def mode_name
  @mode_name
end

.syntax_tableObject (readonly)

Returns the value of attribute syntax_table.



22
23
24
# File 'lib/textbringer/mode.rb', line 22

def syntax_table
  @syntax_table
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



69
70
71
# File 'lib/textbringer/mode.rb', line 69

def buffer
  @buffer
end

Class Method Details

.define_generic_command(name, **options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/textbringer/mode.rb', line 25

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

.define_local_command(name, **options, &block) ⇒ Object



41
42
43
44
45
# File 'lib/textbringer/mode.rb', line 41

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

.define_syntax(face_name, re) ⇒ Object



47
48
49
# File 'lib/textbringer/mode.rb', line 47

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

.inherited(child) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/textbringer/mode.rb', line 51

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

.listObject



12
13
14
# File 'lib/textbringer/mode.rb', line 12

def self.list
  @@mode_list
end

Instance Method Details

#nameObject



75
76
77
# File 'lib/textbringer/mode.rb', line 75

def name
  self.class.mode_name
end

#syntax_tableObject



79
80
81
# File 'lib/textbringer/mode.rb', line 79

def syntax_table
  self.class.syntax_table
end