--[[
VPLanguage = lua
VPScriptMenuTitle = In Page
VPScriptSuperMenuTitle = Word Count
VPEndConfig
--]]

textView        = windowController:textView()
text            = textView:textStorage():objc_string()

lText           = textView:textStorage():string()
lText           = string.gsub(lText, '\r\n', '\n')
lText           = string.gsub(lText, '\r', '\n')
letterCount     = string.len(lText)
lineCount       = 1
wordCount       = 0

for w in string.gfind(lText, '\n') do
    lineCount = lineCount + 1
end

for w in string.gfind(lText, ' ') do
    wordCount = wordCount + 1
end

for w in string.gfind(lText, '\t') do
    wordCount = wordCount + 1
end

for w in string.gfind(lText, ':') do
    wordCount = wordCount + 1
end

s = string.format("%d words.\n%d lines.\n%d characters.", wordCount, lineCount, letterCount)

objc.nsAlert("Word Count", s, nil, nil, nil)
