class Utils::Editor
def activate
file and then closes it. Otherwise, it identifies the appropriate tmux
existing Vim pane. When the '-g' flag is present, it creates a temporary
whether to open a new buffer in the current window or switch to an
This method checks if the Vim default arguments include the '-g' flag to determine
The activate method switches to the Vim editor window or opens a new one.
def activate if Array(config.vim_default_args).include?('-g') edit_remote("stupid_trick#{rand}") sleep pause_duration edit_remote_send('<ESC>:bw<CR>') else pstree = PSTree.new switch_to_index = `tmux list-panes -F '\#{pane_pid} \#{pane_index}'`.lines.find { |l| pid, index = l.split(' ') pid = pid.to_i if pstree.find { |ps| ps.pid != $$ && ps.ppid == pid && ps.cmd =~ %r(/edit( |$)) } break index.to_i end } switch_to_index and system "tmux select-pane -t #{switch_to_index}" end end
def cmd(*parts)
-
(Boolean)
- true if the command was successful, false otherwise
Parameters:
-
parts
(Array
) -- the parts to be included in the command
def cmd(*parts) command = parts.compact.inject([]) do |a, p| case when p == nil, p == [] a when p.respond_to?(:to_ary) a.concat p.to_ary else a << p.to_s end end $DEBUG and warn command * ' ' system(*command.map(&:to_s)) end
def derive_server_name
-
(String)
- the constructed server name based on environment and
def derive_server_name ENV['VIM_SERVER'] || Dir.pwd ig::CONFIG['host_os'] =~ /mswin|mingw/ and name = "G_#{name}" pcase
def edit(*filenames)
-
filenames
(Array
) -- an array of filenames that
def edit(*filenames) source_location = nil if filenames.size == 1 and source_location = filenames.first.source_location then if source_location.respond_to?(:filename) and source_location.respond_to?(:linenumber) edit_source_location(source_location) else edit_file_linenumber(*source_location) end elsif source_locations = filenames.map(&:source_location).compact.full? filenames = expand_globs(source_locations.map(&:first)) edit_file(*filenames) end.tap do activate end end
def edit_file(*filenames)
-
filenames
(Array
) -- an array of filename strings to be processed
def edit_file(*filenames) make_dirs(*filenames) edit_remote_file(*filenames) end
def edit_file_linenumber(filename, linenumber, rangeend = nil)
-
rangeend
(Integer, nil
) -- the ending line number for selection, or -
linenumber
(Integer
) -- the line number where the file should be -
filename
(String
) -- the path to the file to be opened
def edit_file_linenumber(filename, linenumber, rangeend = nil) make_dirs filename if rangeend Thread.new do while !started? sleep 1 end edit_remote_send("<ESC>:normal #{linenumber}GV#{rangeend}G<CR>") end end if wait? activate edit_remote_wait("+#{linenumber}", filename) else edit_remote("+#{linenumber}", filename) end end
def edit_remote(*args)
-
args
(Array
) -- the arguments to be passed to the remote Vim command
def edit_remote(*args) rename_window cmd(*vim, '--servername', servername, '--remote', *args) end
def edit_remote_file(*filenames)
-
filenames
(Array
) -- an array of filenames to be processed
def edit_remote_file(*filenames) if wait? edit_remote_wait(*filenames) else edit_remote(*filenames) end end
def edit_remote_send(*args)
-
args
(Array
) -- the arguments to be sent to the remote Vim server
def edit_remote_send(*args) rename_window cmd(*vim, '--servername', servername, '--remote-send', *args) end
def edit_remote_wait(*args)
-
args
(Array
) -- the arguments to be passed to the remote command
def edit_remote_wait(*args) rename_window cmd(*vim, '--servername', servername, '--remote-wait', *args) end
def edit_source_location(source_location)
-
source_location
(Array
) -- the source location
def edit_source_location(source_location) edit_file_linenumber( source_location.filename, source_location.linenumber, source_location.rangeend ) end
def expand_globs(filenames)
-
(Array
- a sorted array of unique filenames with glob)
Parameters:
-
filenames
(Array
) -- an array of filename patterns that may
def expand_globs(filenames) filenames.map { |f| Dir[f] }.flatten.uniq.sort.full? || filenames end
def file_linenumber?(filename)
-
(MatchData, nil)
- a match data object if the filename matches the pattern,
Parameters:
-
filename
(String
) -- the filename string to be checked
def file_linenumber?(filename) filename.match(Utils::Xt::SourceLocationExtension::FILE_LINENUMBER_REGEXP) end
def fullscreen=(enabled)
-
enabled
(TrueClass, FalseClass
) -- determines whether to enable or
def fullscreen=(enabled) start sleep pause_duration if enabled edit_remote_send '<ESC>:set fullscreen<CR>' else edit_remote_send '<ESC>:set nofullscreen<CR>' end activate end
def initialize
-
(Utils::Editor)
- a new editor instance configured with default settings
Parameters:
-
block
(Proc
) -- optional block to be executed after initialization
def initialize self.wait = false self.pause_duration = 1 self.servername = derive_server_name config = Utils::ConfigFile.new config.configure_from_paths self.config = config.edit yield self if block_given? end
def make_dirs(*filenames)
-
filenames
(Array
) -- an array of filenames for which to
def make_dirs(*filenames) ir filename in filenames leUtils.mkdir_p File.dirname(filename)
def rename_window
script being executed. It only performs the renaming operation if a tmux
and, if so, renames the current window to reflect the base name of the
This method checks if the application is running within a tmux session
base name of the current script.
The rename_window method renames the current tmux window to match the
def rename_window if started? MUX'] and system "tmux rename-window #{File.basename($0)}"
def serverlist
-
(Array
- an array of Vim server names currently available)
def serverlist `#{vim.map(&:inspect) * ' '} --serverlist`.split end
def start
not, it executes the command to launch the Vim server with the specified
server name, then checks if the Vim server has already been started. If
This method first attempts to rename the terminal window to reflect the
already running.
The start method initializes the Vim server connection if it is not
def start rename_window started? or cmd(*vim, '--servername', servername) end
def started?(name = servername)
-
(TrueClass, FalseClass)
- true if the server is running, false otherwise
Parameters:
-
name
(String
) -- the name of the server to check for
def started?(name = servername) serverlist.member?(name) end
def stop
This method checks if the editor is currently running and, if so, sends a
The stop method sends a quit command to the remote editor.
def stop started? and edit_remote_send('<ESC>:qa<CR>') end
def vim
-
(Array
- an array containing the Vim executable path and)
def vim ([ config.vim_path ] + Array(config.vim_default_args)) end