diff --git a/lib/textbringer/commands.rb b/lib/textbringer/commands.rb index 793cffa..092ffb6 100644 --- a/lib/textbringer/commands.rb +++ b/lib/textbringer/commands.rb @@ -2,7 +2,11 @@ require "io/wait" module Textbringer - Command = Struct.new(:name, :block, :doc) + class Command < Data.define(:name, :block, :doc, :source_location_proc) + def source_location + source_location_proc&.call || block.source_location + end + end module Commands include Utils @@ -21,11 +25,11 @@ def self.[](name) @command_table[name.intern] end - def define_command(name, doc: "No documentation", &block) + def define_command(name, doc: "No documentation", source_location_proc: nil, &block) name = name.intern Commands.send(:define_method, name, &block) Commands.send(:module_function, name) - Commands.command_table[name] = Command.new(name, block, doc) + Commands.command_table[name] = Command.new(name, block, doc, source_location_proc) name end module_function :define_command diff --git a/lib/textbringer/commands/help.rb b/lib/textbringer/commands/help.rb index 330d07d..d2fa93e 100644 --- a/lib/textbringer/commands/help.rb +++ b/lib/textbringer/commands/help.rb @@ -65,7 +65,7 @@ def keymap_bindings(keymap) end def command_help(cmd) - file, line = *cmd.block.source_location + file, line = *cmd.source_location s = format("%s:%d\n", file, line) s << "-" * (Window.columns - 2) + "\n" s << "#{cmd.name}" diff --git a/lib/textbringer/mode.rb b/lib/textbringer/mode.rb index 6938f2f..04e6256 100644 --- a/lib/textbringer/mode.rb +++ b/lib/textbringer/mode.rb @@ -24,7 +24,9 @@ class << self def self.define_generic_command(name, **options) command_name = (name.to_s + "_command").intern - define_command(command_name, **options) do |*args| + define_command(command_name, + source_location_proc: -> { Buffer.current.mode.method(name).source_location rescue nil }, + **options) do |*args| begin Buffer.current.mode.send(name, *args) rescue NoMethodError => e