示例#1
0
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]))
		}
	}()
}
示例#2
0
// GetOffset is a wrapper around gtk_text_iter_get_offset().
func (v *TextIter) GetOffset() int {
	return int(C.gtk_text_iter_get_offset(v.native()))
}