From 152e79e94bb935e75b866bd55479648cde11066a Mon Sep 17 00:00:00 2001
From: Bram Moolenaar <Bram@vim.org>
Date: Wed, 10 Jun 2020 15:32:08 +0200
Subject: [PATCH] patch 8.2.0945: cannot use "z=" when 'spell' is off

Reference:https://github.com/vim/vim/commit/152e79e94bb935e75b866bd55479648cde11066a
Conflict:The software package does not contain the Test_spellsuggest function.

Problem:    Cannot use "z=" when 'spell' is off.
Solution:   Make "z=" work even when 'spell' is off. (Christian Brabandt,
            Gary Johnson, closes #6227)
---
 runtime/doc/eval.txt       |  8 +++----
 src/evalfunc.c             | 46 ++++++++++++++++++++++++++++++++++++--
 src/globals.h              |  3 +++
 src/spell.c                |  2 +-
 src/spellsuggest.c         | 13 ++++++++++-
 src/testdir/test_spell.vim | 37 +++++++++++++++++++++++++++---
 6 files changed, 97 insertions(+), 12 deletions(-)

diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index b28fac9..1aeb193 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -9071,9 +9071,8 @@ spellbadword([{sentence}])
 			echo spellbadword("the quik brown fox")
 <			['quik', 'bad'] ~
 
-		The spelling information for the current window is used.  The
-		'spell' option must be set and the value of 'spelllang' is
-		used.
+		The spelling information for the current window and the value
+		of 'spelllang' are used.
 
 		Can also be used as a |method|: >
 			GetText()->spellbadword()
@@ -9098,8 +9097,7 @@ spellsuggest({word} [, {max} [, {capital}]])
 		although it may appear capitalized.
 
 		The spelling information for the current window is used.  The
-		'spell' option must be set and the values of 'spelllang' and
-		'spellsuggest' are used.
+		values of 'spelllang' and 'spellsuggest' are used.
 
 		Can also be used as a |method|: >
 			GetWord()->spellsuggest()
diff --git a/src/evalfunc.c b/src/evalfunc.c
index 892a753..24bd7b1 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -6903,9 +6903,30 @@ f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
     char_u	*word = (char_u *)"";
     hlf_T	attr = HLF_COUNT;
     int		len = 0;
+#ifdef FEAT_SPELL
+    int		wo_spell_save = curwin->w_p_spell;
+
+    if (!curwin->w_p_spell)
+    {
+	did_set_spelllang(curwin);
+	curwin->w_p_spell = TRUE;
+    }
+
+    if (*curwin->w_s->b_p_spl == NUL)
+    {
+	emsg(_(e_no_spell));
+	curwin->w_p_spell = wo_spell_save;
+	return;
+    }
+#endif
 
     if (rettv_list_alloc(rettv) == FAIL)
+    {
+#ifdef FEAT_SPELL
+	curwin->w_p_spell = wo_spell_save;
+#endif
 	return;
+    }
 
 #ifdef FEAT_SPELL
     if (argvars[0].v_type == VAR_UNKNOWN)
@@ -6918,7 +6939,7 @@ f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
 	    curwin->w_set_curswant = TRUE;
 	}
     }
-    else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
+    else if (*curbuf->b_s.b_p_spl != NUL)
     {
 	char_u	*str = tv_get_string_chk(&argvars[0]);
 	int	capcol = -1;
@@ -6940,6 +6961,7 @@ f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
 	    }
 	}
     }
+    curwin->w_p_spell = wo_spell_save;
 #endif
 
     list_append_string(rettv->vval.v_list, word, len);
@@ -6965,13 +6987,32 @@ f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
     int		i;
     listitem_T	*li;
     int		need_capital = FALSE;
+    int		wo_spell_save = curwin->w_p_spell;
+
+    if (!curwin->w_p_spell)
+    {
+	did_set_spelllang(curwin);
+	curwin->w_p_spell = TRUE;
+    }
+
+    if (*curwin->w_s->b_p_spl == NUL)
+    {
+	emsg(_(e_no_spell));
+	curwin->w_p_spell = wo_spell_save;
+	return;
+    }
 #endif
 
     if (rettv_list_alloc(rettv) == FAIL)
+    {
+#ifdef FEAT_SPELL
+	curwin->w_p_spell = wo_spell_save;
+#endif
 	return;
+    }
 
 #ifdef FEAT_SPELL
-    if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
+    if (*curwin->w_s->b_p_spl != NUL)
     {
 	str = tv_get_string(&argvars[0]);
 	if (argvars[1].v_type != VAR_UNKNOWN)
@@ -7008,6 +7049,7 @@ f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
 	}
 	ga_clear(&ga);
     }
+    curwin->w_p_spell = wo_spell_save;
 #endif
 }
 
diff --git a/src/globals.h b/src/globals.h
index 77a42d8..bdda224 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -1492,6 +1492,9 @@ EXTERN char e_invcmd[]	INIT(= N_("E476: Invalid command"));
 #if defined(UNIX) || defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
 EXTERN char e_isadir2[]	INIT(= N_("E17: \"%s\" is a directory"));
 #endif
+#ifdef FEAT_SPELL
+EXTERN char e_no_spell[]	INIT(= N_("E756: Spell checking is not possible"));
+#endif
 #ifdef FEAT_LIBCALL
 EXTERN char e_libcall[]	INIT(= N_("E364: Library call failed for \"%s()\""));
 #endif
diff --git a/src/spell.c b/src/spell.c
index 572f7fb..d8310fa 100644
--- a/src/spell.c
+++ b/src/spell.c
@@ -1225,7 +1225,7 @@ no_spell_checking(win_T *wp)
     if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
 					 || wp->w_s->b_langp.ga_len == 0)
     {
-	emsg(_("E756: Spell checking is not enabled"));
+	emsg(_(e_no_spell));
 	return TRUE;
     }
     return FALSE;
diff --git a/src/spellsuggest.c b/src/spellsuggest.c
index 9d6df79..06e0ad1 100644
--- a/src/spellsuggest.c
+++ b/src/spellsuggest.c
@@ -471,9 +471,19 @@ spell_suggest(int count)
     int		selected = count;
     int		badlen = 0;
     int		msg_scroll_save = msg_scroll;
+    int		wo_spell_save = curwin->w_p_spell;
 
-    if (no_spell_checking(curwin))
+    if (!curwin->w_p_spell)
+    {
+	did_set_spelllang(curwin);
+	curwin->w_p_spell = TRUE;
+    }
+
+    if (*curwin->w_s->b_p_spl == NUL)
+    {
+	emsg(_(e_no_spell));
 	return;
+    }
 
     if (VIsual_active)
     {
@@ -687,6 +697,7 @@ spell_suggest(int count)
     spell_find_cleanup(&sug);
 skip:
     vim_free(line);
+    curwin->w_p_spell = wo_spell_save;
 }
 
 /*
diff --git a/src/testdir/test_spell.vim b/src/testdir/test_spell.vim
index 79fb892..f10ec61 100644
--- a/src/testdir/test_spell.vim
+++ b/src/testdir/test_spell.vim
@@ -99,11 +99,14 @@ foobar/?
    set spelllang=Xwords.spl
    call assert_equal(['foobar', 'rare'], spellbadword('foo foobar'))
 
-  " Typo should not be detected without the 'spell' option.
+  " Typo should be detected even without the 'spell' option.
   set spelllang=en_gb nospell
   call assert_equal(['', ''], spellbadword('centre'))
-  call assert_equal(['', ''], spellbadword('My bycycle.'))
-  call assert_equal(['', ''], spellbadword('A sentence. another sentence'))
+  call assert_equal(['bycycle', 'bad'], spellbadword('My bycycle.'))
+  call assert_equal(['another', 'caps'], spellbadword('A sentence. another sentence'))
+
+  set spelllang=
+  call assert_fails("call spellbadword('maxch')", 'E756:')
 
   call delete('Xwords.spl')
   call delete('Xwords')
@@ -415,6 +418,34 @@ func Test_zeq_crash()
   bwipe!
 endfunc
 
+" Check that z= works even when 'nospell' is set.  This test uses one of the
+" tests in Test_spellsuggest_option_number() just to verify that z= basically
+" works and that "E756: Spell checking is not enabled" is not generated.
+func Test_zeq_nospell()
+  new
+  set nospell spellsuggest=1,best
+  call setline(1, 'A baord')
+  try
+    norm $1z=
+    call assert_equal('A board', getline(1))
+  catch
+    call assert_report("Caught exception: " . v:exception)
+  endtry
+  set spell& spellsuggest&
+  bwipe!
+endfunc
+
+" Check that "E756: Spell checking is not possible" is reported when z= is
+" executed and 'spelllang' is empty.
+func Test_zeq_no_spelllang()
+  new
+  set spelllang= spellsuggest=1,best
+  call setline(1, 'A baord')
+  call assert_fails('normal $1z=', 'E756:')
+  set spelllang& spellsuggest&
+  bwipe!
+endfunc
+
 " Check handling a word longer than MAXWLEN.
 func Test_spell_long_word()
   set enc=utf-8
-- 
2.27.0

