Scite 詞語置換

lua-users home
wiki

這是適用於 SciTE 的簡易即時置換程式碼;類似於 Word 會自動把「the」替換為「teh」的功能。不論你認為它的實用性如何(你可能同意 Phillipe 的說法,認為它會讓人變得懶惰),它顯示了一個 SciTE Lua 擴充功能如何存取輸入的每個字詞。我們對各種檔案類型不使用相同的字詞清單(「fun」會在 Lua 和 Pascal 檔案擴充為「function」,而文本檔案則不會);這個動作會在主動檔案變更時進行觀察,可能是透過開啟(使用 OnOpen)或切換緩衝區(使用 OnSwitchFile)完成。

-- doing word substitutions on the fly!

local txt_words = {
 teh='the', wd='would',cd='could'   
}

local pascal_words = {
 fun='function',lfun='local function',
 proc='procedure',virt='virtual',ctor='constructor',
 dtor='destructor',prog='program',
 int='integer',dbl='double',str='string'
}

local words

function switch_substitution_table()
  local ext = props['FileExt']
  if ext == 'pas' or ext == 'lua' then 
    words = pascal_words  
  elseif ext == 'txt' then
    words = txt_words
  else
    words = nil
  end
end

local function word_substitute(word)
  return words and words[word] or word
end

local word_start,in_word,current_word
local find = string.find

function OnChar(s)
 if not in_word then
    if find(s,'%w') then 
      -- we have hit a word!
      word_start = editor.CurrentPos
      in_word = true
      current_word = s
    end
 else -- we're in a word
   -- and it's another word character, so collect
   if find(s,'%w') then   
      current_word = current_word..s
   else
    -- leaving a word; see if we have a substitution
      local word_end = editor.CurrentPos
      local subst = word_substitute(current_word)
      if subst ~= current_word then
         editor:SetSel(word_start-1,word_end-1)
         -- this is somewhat ad-hoc logic, but
         -- SciTE is handling space differently.
         local was_whitespace = find(s,'%s')
         if was_whitespace then
            subst = subst..s
         end
	 editor:ReplaceSel(subst)
         word_end = editor.CurrentPos
         if not was_whitespace then
            editor:GotoPos(word_end + 1)
         end
      end
      in_word = false
   end   
  end 
  -- don't interfere with usual processing!
  return false
end  

function OnOpen(f)
  switch_substitution_table()
end

function OnSwitchFile(f)
  switch_substitution_table()
end

SteveDonovan


最近變更 · 偏好設定
編輯 · 歷史記錄
最近於 2006 年 10 月 22 日下午 11:46 GMT 編輯 (diff)