]> git.lyx.org Git - lyx.git/blobdiff - src/spellchecker.C
more keyboard/keysym changes
[lyx.git] / src / spellchecker.C
index d4a373d477f9017f4bb336d2b6d0614fe608fda7..04a578affd791a636bf98aca837ba5c9bff223dd 100644 (file)
 
 #include <config.h>
 
+#ifdef __GNUG__
+#pragma implementation
+#endif
+
 #include <unistd.h>
 #include <fcntl.h>
 #include <cstdlib>
 #endif
 
 #ifdef HAVE_SYS_SELECT_H
+# ifdef HAVE_STRINGS_H
+   // <strings.h> is needed at least on AIX because FD_ZERO uses bzero().
+# include <strings.h> 
+# endif
 #include <sys/select.h>
 #endif
 
+#include <algorithm>
+
 #include "LString.h"
 #include "sp_form.h"
 #include "spellchecker.h"
 #include "lyx_gui_misc.h"
 #include "debug.h"
 #include "support/lstrings.h"
+#include "encoding.h"
 
-extern LyXRC * lyxrc;
+using std::reverse;
+using std::endl;
 
 // Spellchecker status
 enum {
@@ -107,13 +119,13 @@ struct isp_result {
 void SpellOptionsUpdate() 
 {
        // Alternate language
-       if (lyxrc->isp_alt_lang.empty()) {
-               lyxrc->isp_use_alt_lang = false;
+       if (lyxrc.isp_alt_lang.empty()) {
+               lyxrc.isp_use_alt_lang = false;
        } else {
                fl_set_input(fd_form_spell_options->altlang_input,
-                            lyxrc->isp_alt_lang.c_str());
+                            lyxrc.isp_alt_lang.c_str());
        }
-       if (lyxrc->isp_use_alt_lang) {
+       if (lyxrc.isp_use_alt_lang) {
                fl_set_button(fd_form_spell_options->buflang, 0);
                fl_set_button(fd_form_spell_options->altlang, 1);
        } else {
@@ -122,53 +134,53 @@ void SpellOptionsUpdate()
        }  
 
        // Personal dictionary
-       if (lyxrc->isp_pers_dict.empty()) {
-               lyxrc->isp_use_pers_dict = false;
+       if (lyxrc.isp_pers_dict.empty()) {
+               lyxrc.isp_use_pers_dict = false;
        } else {
                fl_set_input(fd_form_spell_options->perdict_input,
-                            lyxrc->isp_pers_dict.c_str());
+                            lyxrc.isp_pers_dict.c_str());
        }
        fl_set_button(fd_form_spell_options->perdict,
-                     lyxrc->isp_use_pers_dict ? 1:0);
+                     lyxrc.isp_use_pers_dict ? 1:0);
 
        // Escape chars
-       if (lyxrc->isp_esc_chars.empty()) {
-               lyxrc->isp_use_esc_chars = false;
+       if (lyxrc.isp_esc_chars.empty()) {
+               lyxrc.isp_use_esc_chars = false;
        } else {
                fl_set_input(fd_form_spell_options->esc_chars_input,
-                            lyxrc->isp_esc_chars.c_str());
+                            lyxrc.isp_esc_chars.c_str());
        }
        fl_set_button(fd_form_spell_options->esc_chars,
-                     lyxrc->isp_use_esc_chars ? 1:0);
+                     lyxrc.isp_use_esc_chars ? 1:0);
 
        // Options
        fl_set_button(fd_form_spell_options->compounds,
-                     lyxrc->isp_accept_compound ? 1 : 0);
+                     lyxrc.isp_accept_compound ? 1 : 0);
        fl_set_button(fd_form_spell_options->inpenc,
-                     lyxrc->isp_use_input_encoding ? 1 : 0);
+                     lyxrc.isp_use_input_encoding ? 1 : 0);
 }
 
 // Update spellchecker options
 void SpellOptionsApplyCB(FL_OBJECT *, long)
 {
        // Build new status from form data
-       lyxrc->isp_use_alt_lang = 
+       lyxrc.isp_use_alt_lang = 
                fl_get_button(fd_form_spell_options->altlang);
-       lyxrc->isp_use_pers_dict = 
+       lyxrc.isp_use_pers_dict = 
                fl_get_button(fd_form_spell_options->perdict);
-       lyxrc->isp_accept_compound = 
+       lyxrc.isp_accept_compound = 
                fl_get_button(fd_form_spell_options->compounds);
-       lyxrc->isp_use_esc_chars = 
+       lyxrc.isp_use_esc_chars = 
                fl_get_button(fd_form_spell_options->esc_chars);
-       lyxrc->isp_use_input_encoding = 
+       lyxrc.isp_use_input_encoding = 
                fl_get_button(fd_form_spell_options->inpenc);
 
        // Update strings with data from input fields
-       lyxrc->isp_alt_lang = 
+       lyxrc.isp_alt_lang = 
                fl_get_input(fd_form_spell_options->altlang_input);
-       lyxrc->isp_pers_dict = 
+       lyxrc.isp_pers_dict = 
                fl_get_input(fd_form_spell_options->perdict_input);
-       lyxrc->isp_esc_chars = 
+       lyxrc.isp_esc_chars = 
                fl_get_input(fd_form_spell_options->esc_chars_input);
 
        // Update form
@@ -271,9 +283,9 @@ void create_ispell_pipe(BufferParams const & params, string const & lang)
                close(pipeout[1]);
 
                argc = 0;
-               char * tmp = new char[lyxrc->isp_command.length() + 1];
-               lyxrc->isp_command.copy(tmp, lyxrc->isp_command.length());
-               tmp[lyxrc->isp_command.length()] = '\0';
+               char * tmp = new char[lyxrc.isp_command.length() + 1];
+               lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
+               tmp[lyxrc.isp_command.length()] = '\0';
                argv[argc++] = tmp;
                tmp = new char[3];
                string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
@@ -288,7 +300,7 @@ void create_ispell_pipe(BufferParams const & params, string const & lang)
                        argv[argc++] = tmp;
                }
 
-               if (lyxrc->isp_accept_compound) {
+               if (lyxrc.isp_accept_compound) {
                        // Consider run-together words as legal compounds
                        tmp = new char[3];
                        string("-C").copy(tmp, 2); tmp[2] = '\0';
@@ -300,43 +312,47 @@ void create_ispell_pipe(BufferParams const & params, string const & lang)
                        string("-B").copy(tmp, 2); tmp[2] = '\0';
                        argv[argc++] = tmp; 
                }
-               if (lyxrc->isp_use_esc_chars) {
+               if (lyxrc.isp_use_esc_chars) {
                        // Specify additional characters that
                        // can be part of a word
                        tmp = new char[3];
                        string("-w").copy(tmp, 2); tmp[2] = '\0';
                        argv[argc++] = tmp; 
                        // Put the escape chars in ""s
-                       string tms = "\"" + lyxrc->isp_esc_chars + "\"";
+                       string tms = "\"" + lyxrc.isp_esc_chars + "\"";
                        tmp = new char[tms.length() + 1];
                        tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
                        argv[argc++] = tmp;
                }
-               if (lyxrc->isp_use_pers_dict) {
+               if (lyxrc.isp_use_pers_dict) {
                        // Specify an alternate personal dictionary
                        tmp = new char[3];
                        string("-p").copy(tmp, 2);
                        tmp[2]= '\0';
                        argv[argc++] = tmp;
-                       tmp = new char[lyxrc->isp_pers_dict.length() + 1];
-                       lyxrc->isp_pers_dict.copy(tmp, lyxrc->isp_pers_dict.length());
-                       tmp[lyxrc->isp_pers_dict.length()] = '\0';
+                       tmp = new char[lyxrc.isp_pers_dict.length() + 1];
+                       lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
+                       tmp[lyxrc.isp_pers_dict.length()] = '\0';
                        argv[argc++] = tmp;
                }
-               if (lyxrc->isp_use_input_encoding &&
-                   params.inputenc != "default") {
+               if (lyxrc.isp_use_input_encoding &&
+                   params.inputenc != "default") {
+                       string enc = (params.inputenc == "auto")
+                               ? params.language_info->encoding()->LatexName()
+                               : params.inputenc;
+                       string::size_type n = enc.length();
                        tmp = new char[3];
                        string("-T").copy(tmp, 2); tmp[2] = '\0';
                        argv[argc++] = tmp; // Input encoding
-                       tmp = new char[params.inputenc.length() + 1];
-                       params.inputenc.copy(tmp, params.inputenc.length());
-                       tmp[params.inputenc.length()] = '\0';
+                       tmp = new char[n + 1];
+                       enc.copy(tmp, n);
+                       tmp[n] = '\0';
                        argv[argc++] = tmp;
                }
 
                argv[argc++] = 0;
 
-               execvp("ispell", const_cast<char * const *>(argv));
+               execvp(argv[0], const_cast<char * const *>(argv));
 
                // free the memory used by string::copy in the
                // setup of argv
@@ -531,7 +547,8 @@ void ShowSpellChecker(BufferView * bv)
        if (fd_form_spell_check == 0) {
                fd_form_spell_check = create_form_form_spell_check();
                // Make sure pressing the close box does not kill LyX. (RvdK)
-               fl_set_form_atclose(fd_form_spell_check->form_spell_check, IgnoreCloseBoxCB, 0);
+               fl_set_form_atclose(fd_form_spell_check->form_spell_check, 
+                                   CancelCloseBoxCB, 0);
        }
 
        // Clear form
@@ -641,7 +658,8 @@ bool RunSpellChecker(BufferView * bv)
        int i, newvalue;
        FL_OBJECT * obj;
 
-       string tmp = (lyxrc->isp_use_alt_lang) ? lyxrc->isp_alt_lang : bv->buffer()->GetLanguage();
+       string tmp = (lyxrc.isp_use_alt_lang) ? lyxrc.isp_alt_lang : bv->buffer()->GetLanguage();
+       bool rtl = tmp == "hebrew" || tmp == "arabic";
 
        int oldval = 0;  /* used for updating slider only when needed */
        float newval = 0.0;
@@ -658,7 +676,7 @@ bool RunSpellChecker(BufferView * bv)
                          "Check /usr/lib/ispell or set another\n"
                          "dictionary in the Spellchecker Options menu."), "", "");
                fclose(out);
-               return true;
+               return false;
        }
 
        // Put ispell in terse mode to improve speed
@@ -675,38 +693,49 @@ bool RunSpellChecker(BufferView * bv)
                if(newvalue!= oldval) {
                        oldval = newvalue;
                        fl_set_slider_value(fd_form_spell_check->slider, oldval);
+               }
+
+               if (word_count%1000 == 0) {
+                       obj =  fl_check_forms();
+                       if (obj == fd_form_spell_check->stop) {
+                               delete[] word;
+                               ispell_terminate();
+                               return true;
+                       }
+                       if (obj == fd_form_spell_check->done) {
+                               delete[] word;
+                               ispell_terminate(); 
+                               return false;
+                       }
                }
 
                result = ispell_check_word(word);
                if (isp_pid == -1) {
+                       delete result;
                        delete[] word;
                        break;
                }
 
-               obj =  fl_check_forms();
-               if (obj == fd_form_spell_check->stop) {
-                       delete result;
-                       delete[] word;
-                       ispell_terminate();
-                       return true;
-               }
-               if (obj == fd_form_spell_check->done) {
-                       delete result;
-                       delete[] word;
-                       ispell_terminate(); 
-                       return false;
-               }
-    
                switch (result->flag) {
                case ISP_UNKNOWN:
                case ISP_MISSED:
                {
                        bv->selectLastWord();
-                       fl_set_object_label(fd_form_spell_check->text, word);
+                       if (rtl) {
+                               string tmp = word;
+                               reverse(tmp.begin(),tmp.end());
+                               fl_set_object_label(fd_form_spell_check->text, tmp.c_str());
+                       } else
+                               fl_set_object_label(fd_form_spell_check->text, word);
                        fl_set_input(fd_form_spell_check->input, word);
                        fl_clear_browser(fd_form_spell_check->browser);
                        for (i = 0; i < result->count; ++i) {
-                               fl_add_browser_line(fd_form_spell_check->browser, result->misses[i]);
+                               if (rtl) {
+                                       string tmp = result->misses[i];
+                                       reverse(tmp.begin(),tmp.end());
+                                       fl_add_browser_line(fd_form_spell_check->browser, tmp.c_str());
+                               } else
+                                       fl_add_browser_line(fd_form_spell_check->browser, result->misses[i]);
                        }
 
                        int clickline = -1;
@@ -739,10 +768,17 @@ bool RunSpellChecker(BufferView * bv)
                                                break;
                                        }
                                        clickline = fl_get_browser(fd_form_spell_check->browser);
-                                       fl_set_input(fd_form_spell_check->input, 
-                                                    fl_get_browser_line(fd_form_spell_check->browser,
-                                                                        fl_get_browser(fd_form_spell_check->browser)));
-                                                    
+                                       /// Why not use
+                                       /// fl_set_input(fd_form_spell_check->input, result->misses[clickline-1]); ?
+                                       if (rtl) {
+                                               string tmp = fl_get_browser_line(fd_form_spell_check->browser,
+                                                                                clickline);
+                                               reverse(tmp.begin(),tmp.end());
+                                               fl_set_input(fd_form_spell_check->input, tmp.c_str());
+                                       } else
+                                               fl_set_input(fd_form_spell_check->input,
+                                                            fl_get_browser_line(fd_form_spell_check->browser,
+                                                                                clickline));
                                }
                                if (obj == fd_form_spell_check->stop) {
                                        delete result;