module Textbringer::DabbrevExtension

Public Instance Methods

dabbrev_expand(contd = false) click to toggle source
# File lib/textbringer/commands/dabbrev.rb, line 4
def dabbrev_expand(contd = false)
  if contd && self[:dabbrev_stem]
    buffers = self[:dabbrev_buffers]
    buffer = self[:dabbrev_buffer]
    stem = self[:dabbrev_stem]
    pos = self[:dabbrev_pos]
    direction = self[:dabbrev_direction]
    candidates = self[:dabbrev_candidates]
  else
    buffers = Buffer.to_a
    buffers.delete(self)
    buffer = self
    stem = get_stem_at_point
    pos = point
    direction = :backward
    candidates = []
  end
  re = dabbrev_regexp(stem, candidates)
  candidate = nil
  loop do
    pos, candidate = buffer.dabbrev_search(re, pos, direction)
    break if pos
    if direction == :backward
      pos = buffer.point
      direction = :forward
    else
      buffer = buffers.pop
      break if buffer.nil?
      pos = buffer.point
      direction = :backward
    end
  end
  if !candidates.empty?
    undo
  end
  if candidate
    candidates.push(candidate)
    insert(candidate)
  else
    self[:dabbrev_stem] = nil
    raise EditorError, "No more abbreviation"
  end
  self[:dabbrev_buffers] = buffers
  self[:dabbrev_buffer] = buffer
  self[:dabbrev_stem] = stem
  self[:dabbrev_pos] = pos
  self[:dabbrev_direction] = direction
  self[:dabbrev_candidates] = candidates
end
dabbrev_regexp(stem, candidates) click to toggle source
# File lib/textbringer/commands/dabbrev.rb, line 80
def dabbrev_regexp(stem, candidates)
  candidates_exclusion = candidates.empty? ? "" :
    "(?!(?:" + candidates.map { |s|
      Regexp.quote(s)
    }.join("|") + ")\\b)"
  /\b#{Regexp.quote(stem)}#{candidates_exclusion}
  ([\p{Letter}\p{Number}_\-]+)/x
end
get_stem_at_point() click to toggle source
# File lib/textbringer/commands/dabbrev.rb, line 69
def get_stem_at_point
  save_excursion {
    pos = point
    backward_word(regexp: /[\p{Letter}\p{Number}_\-]/)
    if point == pos
      raise EditorError, "No possible abbreviation"
    end
    substring(point, pos)
  }
end