Return to Snippet

Revision: 3014
at May 24, 2007 13:19 by gtcaz


Initial Code
#!/usr/bin/env ruby -w

require 'cgi'
require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes.rb"
require ENV['TM_SUPPORT_PATH'] + "/lib/escape.rb"

COCOA_DIALOG = "#{ENV['TM_SUPPORT_PATH']}/bin/CocoaDialog"
HTML_FILE = "/cygdrive/c/tmp/sss_tmp.html"

selection = STDIN.read

IO.popen("aspell -a", 'w+') do |aspell|
  aspell.puts(selection)
  aspell.close_write
  to_skip = []
  aspell.each do |check|
    if check =~ /^\&/
      data, raw_suggestions = check.split(":")
      signal, original, count, offset = data.split
      next if to_skip.include?(original)
      suggestions = ""
      raw_suggestions.split(', ').each { |s| suggestions += "<a href=\'cocoadialog://ReplaceAll #{CGI.escape(s.chomp)}\'>#{CGI.escapeHTML(s.chomp)}</a> "}
html_dialog = <<EOF
<div style='font-family: Verdana, Lucida, Arial, Helvetica, sans-serif; font-size: 11px;'>
<p>There are #{count} suggestions for \'#{original}\' (click on one to replace):</p>
<p style='border: medium double #4D819D; padding: .3cm; background-color: #D2D2D2; word-spacing: 5px;'>#{suggestions}</p>
<ul>
<li><a style='text-decoration: none' href=\"cocoadialog://Skip\"><strong>Skip</strong> #{original} this time</a></li>
<li><a style='text-decoration: none' href=\"cocoadialog://SkipAll\"><strong>Skip all</strong> occurrences of #{original} in this document</a></li>
<li><a style='text-decoration: none' href=\"cocoadialog://Add\"><strong>Add</strong> #{original} to your personal dictionary</a></li>
<li><a style='text-decoration: none' href=\"cocoadialog://End\"><strong>End</strong> spell checking, committing any changes you've made</a></li>
<li><a style='text-decoration: none' href=\"cocoadialog://Cancel\"><strong>Cancel</strong>, discarding any changes you've made</a></li>
</ul></p>
</div>
EOF
      open(HTML_FILE, 'w') { |f| f << html_dialog}
      choice=%x("#{COCOA_DIALOG}" html --title "Misspelled Word" --html-from-file #{HTML_FILE} --width 500 --height 300)
      button, replacement = choice.split(" ")
      case button
      when "Cancel"
        TextMate.exit_show_tool_tip("Spell-check cancelled!")
      when "End"
        break
      when "SkipAll"
        to_skip.push(original)
      when "ReplaceAll"
        replacement.chomp!
        selection.gsub!(/#{original}/, replacement) # Replace done globally so all instances of original replaced
      when "Replace"
        selection.gsub!(/#{original}/, replacement)
        # Todo: Need to use the offest here to replace the original using the offset variable,
        # which is a 0-offset index of the character position where the misspelled word
        # starts.
      when "Add"
        `echo -e "*#{original}\n#" | aspell -a`
        `"#{COCOA_DIALOG}" ok-msgbox \
          --text "Added '#{original}' to your personal dictionary." \
          --no-cancel --timeout 2`
        to_skip.push(original)
      end
    end
  end
end
print selection

Initial URL


Initial Description


Initial Title
Super Simple Speller v2

Initial Tags
textmate

Initial Language
Ruby