searchpos 

Send to Kindle
home » snippets » vim » functions » searchpos



Code snippets

def TransposeWords():
  saved_cursor = _V.cursor
  current_word_begin = Vim.searchpos(r'\<\w\+', 'bcnW')
  if current_word_begin == (-1, -1):
    return
  current_word_end = Vim.searchpos(r'\>', 'cnW')
  if current_word_end == (-1, -1):
    return
  # Go to the beginning of the current word.
  _V.cursor = current_word_begin
  # This moves the cursor to the beginning of the previous word.
  prev_word_begin = Vim.searchpos(r'\<\w\+\>', 'bsW')
  if prev_word_begin == (-1, -1):
    _V.cursor = saved_cursor
    return
  prev_word_end = Vim.searchpos(r'\>', 'cnW')
  if prev_word_end == (-1, -1):
    _V.cursor = saved_cursor
    return
  current_word = _V.cb[current_word_begin.line][
      current_word_begin.col:current_word_end.col]
  prev_word = _V.cb[prev_word_begin.line][
      prev_word_begin.col:prev_word_end.col]
  if current_word_begin.line == prev_word_begin.line:
    _V.cl = replace_string_slices(
        _V.cl,
        slice(prev_word_begin.col, prev_word_end.col),
        current_word,
        slice(current_word_begin.col, current_word_end.col),
        prev_word)
  else:
    _V.cb[prev_word_begin.line] = replace_string_slices(
        _V.cb[prev_word_begin.line],
        slice(prev_word_begin.col, prev_word_end.col),
        current_word)
    _V.cb[current_word_begin.line] = replace_string_slices(
        _V.cb[current_word_begin.line],
        slice(current_word_begin.col, current_word_end.col),
        prev_word)