1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-29 14:26:39 +02:00

aspell: add support of enchant library (patch #6858)

Enchant is enabled only if the new option for cmake (or configure) is enabled:
- for cmake: cmake -DENABLE_ENCHANT=ON
- for configure: ./configure --enable-enchant
This commit is contained in:
Sebastien Helleu
2013-06-07 21:01:48 +02:00
parent 4080047da0
commit fb7edb3518
30 changed files with 596 additions and 167 deletions
+19 -5
View File
@@ -44,11 +44,25 @@ IF(ENABLE_ALIAS)
ENDIF(ENABLE_ALIAS)
IF(ENABLE_ASPELL)
# Check for aspell libraries
FIND_PACKAGE(Aspell)
IF(ASPELL_FOUND)
ADD_SUBDIRECTORY( aspell )
ENDIF(ASPELL_FOUND)
IF(ENABLE_ENCHANT)
# Check for enchant libraries
FIND_PACKAGE(ENCHANT)
IF(ENCHANT_FOUND)
ADD_SUBDIRECTORY( aspell )
ELSE(ENCHANT_FOUND)
# Check for aspell libraries
FIND_PACKAGE(Aspell)
IF(ASPELL_FOUND)
ADD_SUBDIRECTORY( aspell )
ENDIF(ASPELL_FOUND)
ENDIF(ENCHANT_FOUND)
ELSE(ENABLE_ENCHANT)
# Check for aspell libraries
FIND_PACKAGE(Aspell)
IF(ASPELL_FOUND)
ADD_SUBDIRECTORY( aspell )
ENDIF(ASPELL_FOUND)
ENDIF(ENABLE_ENCHANT)
ENDIF(ENABLE_ASPELL)
IF(ENABLE_CHARSET)
+10 -4
View File
@@ -28,9 +28,15 @@ weechat-aspell-info.c weechat-aspell-info.h
weechat-aspell-speller.c weechat-aspell-speller.h)
SET_TARGET_PROPERTIES(aspell PROPERTIES PREFIX "")
IF(ASPELL_FOUND)
INCLUDE_DIRECTORIES(${ASPELL_INCLUDE_PATH})
TARGET_LINK_LIBRARIES(aspell ${ASPELL_LIBRARY})
ENDIF(ASPELL_FOUND)
IF(ENCHANT_FOUND)
INCLUDE_DIRECTORIES(${ENCHANT_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(aspell ${ENCHANT_LIBRARIES})
ADD_DEFINITIONS(-DUSE_ENCHANT)
ELSE(ENCHANT_FOUND)
IF(ASPELL_FOUND)
INCLUDE_DIRECTORIES(${ASPELL_INCLUDE_PATH})
TARGET_LINK_LIBRARIES(aspell ${ASPELL_LIBRARY})
ENDIF(ASPELL_FOUND)
ENDIF(ENCHANT_FOUND)
INSTALL(TARGETS aspell LIBRARY DESTINATION ${LIBDIR}/plugins)
+2 -2
View File
@@ -18,7 +18,7 @@
# along with WeeChat. If not, see <http://www.gnu.org/licenses/>.
#
INCLUDES = -DLOCALEDIR=\"$(datadir)/locale\" $(ASPELL_CFLAGS)
INCLUDES = -DLOCALEDIR=\"$(datadir)/locale\" $(ASPELL_CFLAGS) $(ENCHANT_CFLAGS)
libdir = ${weechat_libdir}/plugins
@@ -39,6 +39,6 @@ aspell_la_SOURCES = weechat-aspell.c \
weechat-aspell-speller.c \
weechat-aspell-speller.h
aspell_la_LDFLAGS = -module
aspell_la_LIBADD = $(ASPELL_LFLAGS)
aspell_la_LIBADD = $(ASPELL_LFLAGS) $(ENCHANT_LIBS)
EXTRA_DIST = CMakeLists.txt
+98 -12
View File
@@ -71,6 +71,67 @@ weechat_aspell_command_iso_to_country (const char *code)
return strdup ("Unknown");
}
/*
* Displays one dictionary when using enchant.
*/
#ifdef USE_ENCHANT
void
weechat_aspell_enchant_dict_describe_cb (const char *lang_tag,
const char *provider_name,
const char *provider_desc,
const char *provider_file,
void *user_data)
{
char *country, *lang, *pos, *iso;
char str_dict[256];
/* make C compiler happy */
(void) provider_name;
(void) provider_desc;
(void) provider_file;
(void) user_data;
lang = NULL;
country = NULL;
pos = strchr (lang_tag, '_');
if (pos)
{
iso = weechat_strndup (lang_tag, pos - lang_tag);
if (iso)
{
lang = weechat_aspell_command_iso_to_lang (iso);
country = weechat_aspell_command_iso_to_country (pos + 1);
free (iso);
}
}
else
lang = weechat_aspell_command_iso_to_lang ((char *)lang_tag);
if (lang)
{
if (country)
{
snprintf (str_dict, sizeof (str_dict), "%-22s %s (%s)",
lang_tag, lang, country);
}
else
{
snprintf (str_dict, sizeof (str_dict), "%-22s %s",
lang_tag, lang);
}
weechat_printf (NULL, " %s", str_dict);
}
if (lang)
free (lang);
if (country)
free (country);
}
#endif
/*
* Displays list of aspell dictionaries installed on system.
*/
@@ -78,16 +139,14 @@ weechat_aspell_command_iso_to_country (const char *code)
void
weechat_aspell_command_speller_list_dicts ()
{
char *country, *lang, *pos;
#ifndef USE_ENCHANT
char *country, *lang, *pos, *iso;
char str_dict[256], str_country[128];
struct AspellConfig *config;
AspellDictInfoList *list;
AspellDictInfoEnumeration *elements;
const AspellDictInfo *dict;
config = new_aspell_config();
list = get_aspell_dict_info_list (config);
elements = aspell_dict_info_list_elements (list);
#endif
weechat_printf (NULL, "");
weechat_printf (NULL,
@@ -95,6 +154,14 @@ weechat_aspell_command_speller_list_dicts ()
_( "%s dictionaries list:"),
ASPELL_PLUGIN_NAME);
#ifdef USE_ENCHANT
enchant_broker_list_dicts (broker, weechat_aspell_enchant_dict_describe_cb,
NULL);
#else
config = new_aspell_config();
list = get_aspell_dict_info_list (config);
elements = aspell_dict_info_list_elements (list);
while ((dict = aspell_dict_info_enumeration_next (elements)) != NULL)
{
country = NULL;
@@ -102,10 +169,13 @@ weechat_aspell_command_speller_list_dicts ()
if (pos)
{
pos[0] = '\0';
lang = weechat_aspell_command_iso_to_lang ((char*)dict->code);
pos[0] = '_';
country = weechat_aspell_command_iso_to_country (pos + 1);
iso = weechat_strndup (dict->code, pos - dict->code);
if (iso)
{
lang = weechat_aspell_command_iso_to_lang (iso);
country = weechat_aspell_command_iso_to_country (pos + 1);
free (iso);
}
}
else
lang = weechat_aspell_command_iso_to_lang ((char*)dict->code);
@@ -132,6 +202,7 @@ weechat_aspell_command_speller_list_dicts ()
delete_aspell_dict_info_enumeration (elements);
delete_aspell_config (config);
#endif
}
/*
@@ -169,7 +240,11 @@ weechat_aspell_command_add_word (struct t_gui_buffer *buffer, const char *dict,
const char *word)
{
struct t_aspell_speller_buffer *ptr_speller_buffer;
#ifdef USE_ENCHANT
EnchantDict *new_speller, *ptr_speller;
#else
AspellSpeller *new_speller, *ptr_speller;
#endif
new_speller = NULL;
@@ -221,6 +296,9 @@ weechat_aspell_command_add_word (struct t_gui_buffer *buffer, const char *dict,
ptr_speller = ptr_speller_buffer->spellers[0];
}
#ifdef USE_ENCHANT
enchant_dict_add (ptr_speller, word, strlen (word));
#else
if (aspell_speller_add_to_personal (ptr_speller,
word,
strlen (word)) == 1)
@@ -231,6 +309,7 @@ weechat_aspell_command_add_word (struct t_gui_buffer *buffer, const char *dict,
}
else
goto error;
#endif
goto end;
@@ -265,9 +344,16 @@ weechat_aspell_command_cb (void *data, struct t_gui_buffer *buffer,
{
/* display aspell status */
weechat_printf (NULL, "");
weechat_printf (NULL, "%s",
(aspell_enabled) ?
_("Aspell is enabled") : _("Aspell is disabled"));
weechat_printf (NULL,
/* TRANSLATORS: second "%s" is "aspell" or "enchant" */
_("%s (using %s)"),
(aspell_enabled) ? _("Spell checking is enabled") : _("Spell checking is disabled"),
#ifdef USE_ENCHANT
"enchant"
#else
"aspell"
#endif
);
default_dict = weechat_config_string (weechat_aspell_config_check_default_dict);
weechat_printf (NULL,
_("Default dictionary: %s"),
@@ -54,6 +54,28 @@ weechat_aspell_completion_langs_cb (void *data, const char *completion_item,
return WEECHAT_RC_OK;
}
/*
* Adds a dictionary to completion when using enchant.
*/
#ifdef USE_ENCHANT
void
weechat_aspell_completion_enchant_add_dict_cb (const char *lang_tag,
const char *provider_name,
const char *provider_desc,
const char *provider_file,
void *user_data)
{
/* make C compiler happy */
(void) provider_name;
(void) provider_desc;
(void) provider_file;
weechat_hook_completion_list_add ((struct t_gui_completion *)user_data,
lang_tag, 0, WEECHAT_LIST_POS_SORT);
}
#endif
/*
* Adds aspell dictionaries (only installed dictionaries) to completion list.
*/
@@ -64,16 +86,23 @@ weechat_aspell_completion_dicts_cb (void *data,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
#ifndef USE_ENCHANT
struct AspellConfig *config;
AspellDictInfoList *list;
AspellDictInfoEnumeration *elements;
const AspellDictInfo *dict;
#endif
/* make C compiler happy */
(void) data;
(void) completion_item;
(void) buffer;
#ifdef USE_ENCHANT
enchant_broker_list_dicts (broker,
weechat_aspell_completion_enchant_add_dict_cb,
completion);
#else
config = new_aspell_config ();
list = get_aspell_dict_info_list (config);
elements = aspell_dict_info_list_elements (list);
@@ -86,6 +115,7 @@ weechat_aspell_completion_dicts_cb (void *data,
delete_aspell_dict_info_enumeration (elements);
delete_aspell_config (config);
#endif
return WEECHAT_RC_OK;
}
+52 -2
View File
@@ -53,6 +53,9 @@ struct t_hashtable *weechat_aspell_speller_buffer = NULL;
int
weechat_aspell_speller_dict_supported (const char *lang)
{
#ifdef USE_ENCHANT
return enchant_broker_dict_exists (broker, lang);
#else
struct AspellConfig *config;
AspellDictInfoList *list;
AspellDictInfoEnumeration *elements;
@@ -78,6 +81,7 @@ weechat_aspell_speller_dict_supported (const char *lang)
delete_aspell_config (config);
return rc;
#endif
}
/*
@@ -119,12 +123,20 @@ weechat_aspell_speller_check_dictionaries (const char *dict_list)
* Returns pointer to new aspell speller, NULL if error.
*/
#ifdef USE_ENCHANT
EnchantDict *
#else
AspellSpeller *
#endif
weechat_aspell_speller_new (const char *lang)
{
#ifdef USE_ENCHANT
EnchantDict *new_speller;
#else
AspellConfig *config;
AspellCanHaveError *ret;
AspellSpeller *new_speller;
#endif
struct t_infolist *infolist;
if (!lang)
@@ -137,23 +149,40 @@ weechat_aspell_speller_new (const char *lang)
ASPELL_PLUGIN_NAME, lang);
}
#ifdef USE_ENCHANT
new_speller = enchant_broker_request_dict (broker, lang);
if (!new_speller)
{
weechat_printf (NULL,
_("%s%s: error: unable to create speller for lang \"%s\""),
weechat_prefix ("error"), ASPELL_PLUGIN_NAME,
lang);
return NULL;
}
#else
/* create a speller instance for the newly created cell */
config = new_aspell_config();
aspell_config_replace (config, "lang", lang);
#endif
/* apply all options on speller */
/* apply all options */
infolist = weechat_infolist_get ("option", NULL, "aspell.option.*");
if (infolist)
{
while (weechat_infolist_next (infolist))
{
#ifdef USE_ENCHANT
/* TODO: set option with enchant */
#else
aspell_config_replace (config,
weechat_infolist_string (infolist, "option_name"),
weechat_infolist_string (infolist, "value"));
#endif
}
weechat_infolist_free (infolist);
}
#ifndef USE_ENCHANT
ret = new_aspell_speller (config);
if (aspell_error (ret) != 0)
@@ -168,10 +197,14 @@ weechat_aspell_speller_new (const char *lang)
}
new_speller = to_aspell_speller (ret);
#endif
weechat_hashtable_set (weechat_aspell_spellers, lang, new_speller);
#ifndef USE_ENCHANT
/* free configuration */
delete_aspell_config (config);
#endif
return new_speller;
}
@@ -277,7 +310,11 @@ void
weechat_aspell_speller_free_value_cb (struct t_hashtable *hashtable,
const void *key, void *value)
{
#ifdef USE_ENCHANT
EnchantDict *ptr_speller;
#else
AspellSpeller *ptr_speller;
#endif
/* make C compiler happy */
(void) hashtable;
@@ -289,10 +326,15 @@ weechat_aspell_speller_free_value_cb (struct t_hashtable *hashtable,
ASPELL_PLUGIN_NAME, (const char *)key);
}
/* free aspell data */
/* free speller */
#ifdef USE_ENCHANT
ptr_speller = (EnchantDict *)value;
enchant_broker_free_dict (broker, ptr_speller);
#else
ptr_speller = (AspellSpeller *)value;
aspell_speller_save_all_word_lists (ptr_speller);
delete_aspell_speller (ptr_speller);
#endif
}
/*
@@ -307,7 +349,11 @@ weechat_aspell_speller_buffer_new (struct t_gui_buffer *buffer)
char **dicts;
int num_dicts, i;
struct t_aspell_speller_buffer *new_speller_buffer;
#ifdef USE_ENCHANT
EnchantDict *ptr_speller;
#else
AspellSpeller *ptr_speller;
#endif
if (!buffer)
return NULL;
@@ -330,7 +376,11 @@ weechat_aspell_speller_buffer_new (struct t_gui_buffer *buffer)
if (dicts && (num_dicts > 0))
{
new_speller_buffer->spellers =
#ifdef USE_ENCHANT
malloc ((num_dicts + 1) * sizeof (EnchantDict *));
#else
malloc ((num_dicts + 1) * sizeof (AspellSpeller *));
#endif
if (new_speller_buffer->spellers)
{
for (i = 0; i < num_dicts; i++)
+9 -1
View File
@@ -23,7 +23,11 @@
struct t_aspell_speller_buffer
{
AspellSpeller **spellers; /* pointer to spellers for buf. */
#ifdef USE_ENCHANT
EnchantDict **spellers; /* enchant spellers for buffer */
#else
AspellSpeller **spellers; /* aspell spellers for buffer */
#endif
char *modifier_string; /* last modifier string */
int input_pos; /* position of cursor in input */
char *modifier_result; /* last modifier result */
@@ -34,7 +38,11 @@ extern struct t_hashtable *weechat_aspell_speller_buffer;
extern int weechat_aspell_speller_dict_supported (const char *lang);
extern void weechat_aspell_speller_check_dictionaries (const char *dict_list);
#ifdef USE_ENCHANT
extern EnchantDict *weechat_aspell_speller_new (const char *lang);
#else
extern AspellSpeller *weechat_aspell_speller_new (const char *lang);
#endif
extern void weechat_aspell_speller_remove_unused ();
extern struct t_aspell_speller_buffer *weechat_aspell_speller_buffer_new (struct t_gui_buffer *buffer);
extern int weechat_aspell_speller_init ();
+57
View File
@@ -47,6 +47,10 @@ struct t_weechat_plugin *weechat_aspell_plugin = NULL;
int aspell_enabled = 0;
#ifdef USE_ENCHANT
EnchantBroker *broker = NULL;
#endif
/*
* aspell supported languages, updated on 2012-07-05
* URL: ftp://ftp.gnu.org/gnu/aspell/dict/0index.html
@@ -410,7 +414,11 @@ weechat_aspell_check_word (struct t_gui_buffer *buffer,
{
for (i = 0; speller_buffer->spellers[i]; i++)
{
#ifdef USE_ENCHANT
if (enchant_dict_check (speller_buffer->spellers[i], word, strlen (word)) == 0)
#else
if (aspell_speller_check (speller_buffer->spellers[i], word, -1) == 1)
#endif
return 1;
}
}
@@ -434,8 +442,13 @@ weechat_aspell_get_suggestions (struct t_aspell_speller_buffer *speller_buffer,
int i, size, max_suggestions, num_suggestions;
char *suggestions, *suggestions2;
const char *ptr_word;
#ifdef USE_ENCHANT
char **elements;
size_t num_elements;
#else
const AspellWordList *list;
AspellStringEnumeration *elements;
#endif
max_suggestions = weechat_config_integer (weechat_aspell_config_check_suggestions);
if (max_suggestions < 0)
@@ -451,6 +464,37 @@ weechat_aspell_get_suggestions (struct t_aspell_speller_buffer *speller_buffer,
{
for (i = 0; speller_buffer->spellers[i]; i++)
{
#ifdef USE_ENCHANT
elements = enchant_dict_suggest (speller_buffer->spellers[i], word,
-1, &num_elements);
if (elements)
{
if (num_elements > 0)
{
num_suggestions = 0;
while ((ptr_word = elements[num_suggestions]) != NULL)
{
size += strlen (ptr_word) + ((suggestions[0]) ? 1 : 0);
suggestions2 = realloc (suggestions, size);
if (!suggestions2)
{
free (suggestions);
enchant_dict_free_string_list (speller_buffer->spellers[i],
elements);
return NULL;
}
suggestions = suggestions2;
if (suggestions[0])
strcat (suggestions, (num_suggestions == 0) ? "/" : ",");
strcat (suggestions, ptr_word);
num_suggestions++;
if ((max_suggestions >= 0) && (num_suggestions == max_suggestions))
break;
}
}
enchant_dict_free_string_list (speller_buffer->spellers[i], elements);
}
#else
list = aspell_speller_suggest (speller_buffer->spellers[i], word, -1);
if (list)
{
@@ -476,6 +520,7 @@ weechat_aspell_get_suggestions (struct t_aspell_speller_buffer *speller_buffer,
}
delete_aspell_string_enumeration (elements);
}
#endif
}
}
@@ -876,6 +921,13 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
weechat_plugin = plugin;
#ifdef USE_ENCHANT
/* acquire enchant broker */
broker = enchant_broker_init ();
if (!broker)
return WEECHAT_RC_ERROR;
#endif
if (!weechat_aspell_speller_init ())
return WEECHAT_RC_ERROR;
@@ -926,5 +978,10 @@ weechat_plugin_end (struct t_weechat_plugin *plugin)
weechat_aspell_speller_end ();
#ifdef USE_ENCHANT
/* release enchant broker */
enchant_broker_free (broker);
#endif
return WEECHAT_RC_OK;
}
+8
View File
@@ -21,7 +21,11 @@
#ifndef __WEECHAT_ASPELL_H
#define __WEECHAT_ASPELL_H 1
#ifdef USE_ENCHANT
#include <enchant.h>
#else
#include <aspell.h>
#endif
#define weechat_plugin weechat_aspell_plugin
#define ASPELL_PLUGIN_NAME "aspell"
@@ -32,6 +36,10 @@ struct t_aspell_code
char *name;
};
#ifdef USE_ENCHANT
extern EnchantBroker *broker;
#endif
extern struct t_weechat_plugin *weechat_aspell_plugin;
extern int aspell_enabled;
extern struct t_aspell_code aspell_langs[];