func update_candidates(serial int, input string, providersp unsafe.Pointer, info map[string]interface{}) { texts := make(map[string]bool) providers := make(map[string][]string) descriptions := make(map[string][]string) distances := make(map[string]int) var lock sync.Mutex // from GlobalVocabulary l := GlobalVocabulary.Len() var word *Word for i := 0; i < l; i++ { word = GlobalVocabulary.GetByIndex(i) if match, distance := fuzzyMatch(word.Text, input); match { texts[word.Text] = true distances[word.Text] = distance providers[word.Text] = []string{} } } // get buffer content buffer := (*C.GtkTextBuffer)(info["buffer"].(unsafe.Pointer)) var start_iter, end_iter C.GtkTextIter C.gtk_text_buffer_get_start_iter(buffer, &start_iter) C.gtk_text_buffer_get_end_iter(buffer, &end_iter) cContent := C.gtk_text_buffer_get_text(buffer, &start_iter, &end_iter, C.gtk_false()) content := C.GoBytes(unsafe.Pointer(cContent), C.int(C.strlen((*C.char)(cContent)))) result := sort(input, texts, distances, providers, descriptions) // extra providers for source, provider := range (*Providers)(providersp).Providers { go func() { lock.Lock() for _, pair := range provider(input, content, info) { text := pair[0] if input != "" { if match, _ := fuzzyMatch(text, input); !match { continue } } GlobalVocabulary.Add(text) texts[text] = true providers[text] = append(providers[text], source) descriptions[text] = append(descriptions[text], "<"+source+"> "+pair[1]) distances[text] = 0 } result := sort(input, texts, distances, providers, descriptions) lock.Unlock() C.emit() results <- Result{serial, result} }() } updateStore(result) }
func NewView() *View { widget := C.webkit_web_view_new() view := &View{ Widget: widget, View: (*C.WebKitWebView)(unsafe.Pointer(widget)), } // ready to show connect(view.View, "ready-to-show", func() { C.gtk_widget_show_all(view.Widget) }) // page load state changed connect(view.View, "load-changed", func(_, ev interface{}) { p("load changed %d\n", ev.(int)) }) // context context := C.webkit_web_view_get_context(view.View) C.webkit_web_context_set_spell_checking_enabled(context, C.gtk_false()) C.webkit_web_context_set_tls_errors_policy(context, C.WEBKIT_TLS_ERRORS_POLICY_IGNORE) C.webkit_web_context_set_disk_cache_directory(context, toGStr(os.TempDir())) // settings settings := C.webkit_web_view_get_settings(view.View) C.webkit_settings_set_enable_java(settings, C.gtk_false()) C.webkit_settings_set_enable_tabs_to_links(settings, C.gtk_false()) C.webkit_settings_set_enable_dns_prefetching(settings, C.gtk_true()) C.webkit_settings_set_javascript_can_access_clipboard(settings, C.gtk_true()) C.webkit_settings_set_enable_site_specific_quirks(settings, C.gtk_true()) C.webkit_settings_set_enable_smooth_scrolling(settings, C.gtk_true()) // handle cookie cookieManager := C.webkit_web_context_get_cookie_manager(context) C.webkit_cookie_manager_set_persistent_storage(cookieManager, toGStr(cookieFilePath), C.WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT) return view }
func collect_words(bufferp unsafe.Pointer, isEditMode bool, rep unsafe.Pointer) { var start_iter, end_iter C.GtkTextIter buf := (*C.GtkTextBuffer)(bufferp) C.gtk_text_buffer_get_start_iter(buf, &start_iter) C.gtk_text_buffer_get_end_iter(buf, &end_iter) text := []byte(C.GoString((*C.char)(C.gtk_text_buffer_get_text(buf, &start_iter, &end_iter, C.gtk_false())))) C.gtk_text_buffer_get_iter_at_mark(buf, &start_iter, C.gtk_text_buffer_get_insert(buf)) cursor_char_offset := int(C.gtk_text_iter_get_offset(&start_iter)) go func() { byte_offset := 0 char_offset := 0 re := (*regexp.Regexp)(rep) decode_byte_offset := 0 var word_start_char_offset, word_end_char_offset int var size int for { // find next word loc := re.FindIndex(text[byte_offset:]) if loc == nil { return } // convert byte offset to char offset byte_offset += loc[0] for decode_byte_offset < byte_offset { _, size = utf8.DecodeRune(text[decode_byte_offset:]) decode_byte_offset += size char_offset += 1 } word_start_char_offset = char_offset byte_offset += loc[1] - loc[0] for decode_byte_offset < byte_offset { _, size = utf8.DecodeRune(text[decode_byte_offset:]) decode_byte_offset += size char_offset += 1 } word_end_char_offset = char_offset // skip current word in edit mode if isEditMode { if cursor_char_offset >= word_start_char_offset && cursor_char_offset <= word_end_char_offset { continue } } // add to global vocabulary GlobalVocabulary.Add(string(text[byte_offset-(loc[1]-loc[0]) : byte_offset])) } }() }