@@ -4453,6 +4453,20 @@ iconv_end()
44534453# if defined(FEAT_GUI_GTK ) || defined(FEAT_GUI_MACVIM ) || defined(PROTO )
44544454static int xim_has_preediting INIT (= FALSE); /* IM current status */
44554455
4456+ # ifdef FEAT_GUI_MACVIM
4457+ /*
4458+ * Set preedit_start_col to the current cursor position.
4459+ */
4460+ static void
4461+ init_preedit_start_col (void )
4462+ {
4463+ if (State & CMDLINE )
4464+ preedit_start_col = cmdline_getvcol_cursor ();
4465+ else if (curwin != NULL )
4466+ getvcol (curwin , & curwin -> w_cursor , & preedit_start_col , NULL , NULL );
4467+ }
4468+ # endif
4469+
44564470static int im_is_active = FALSE; /* IM is enabled for current mode */
44574471static int preedit_is_active = FALSE;
44584472static int im_preedit_start = 0 ; /* start offset in characters */
@@ -4756,6 +4770,14 @@ im_commit_cb(GtkIMContext *context UNUSED,
47564770 if (add_to_input )
47574771 im_add_to_input ((char_u * )str , slen );
47584772
4773+ # ifdef FEAT_GUI_MACVIM
4774+ /* Inserting chars while "im_is_active" is set does not cause a change of
4775+ * buffer. When the chars are committed the buffer must be marked as
4776+ * changed. */
4777+ if (!commit_with_preedit )
4778+ preedit_start_col = MAXCOL ;
4779+ # endif
4780+
47594781 if (gtk_main_level () > 0 )
47604782 gtk_main_quit ();
47614783}
@@ -4799,6 +4821,9 @@ im_preedit_end_macvim()
47994821 im_delete_preedit ();
48004822
48014823 /* Indicate that preediting has finished */
4824+ # ifdef FEAT_GUI_MACVIM
4825+ preedit_start_col = MAXCOL ;
4826+ # endif
48024827 xim_has_preediting = FALSE;
48034828
48044829#if 0
@@ -4882,6 +4907,8 @@ im_preedit_changed_macvim(char *preedit_string, int start_index, int cursor_inde
48824907 gtk_im_context_get_preedit_string (context ,
48834908 & preedit_string , NULL ,
48844909 & cursor_index );
4910+ # else
4911+ im_preedit_start = start_index ;
48854912# endif
48864913
48874914#ifdef XIM_DEBUG
@@ -4890,8 +4917,38 @@ im_preedit_changed_macvim(char *preedit_string, int start_index, int cursor_inde
48904917
48914918 g_return_if_fail (preedit_string != NULL ); /* just in case */
48924919
4920+ # ifdef FEAT_GUI_MACVIM
4921+ /* If preedit_start_col is MAXCOL set it to the current cursor position. */
4922+ if (preedit_start_col == MAXCOL && preedit_string [0 ] != '\0' )
4923+ {
4924+ xim_has_preediting = TRUE;
4925+
4926+ /* Urgh, this breaks if the input buffer isn't empty now */
4927+ init_preedit_start_col ();
4928+ }
4929+ else if (cursor_index == 0 && preedit_string [0 ] == '\0' )
4930+ {
4931+ xim_has_preediting = FALSE;
4932+
4933+ /* If at the start position (after typing backspace)
4934+ * preedit_start_col must be reset. */
4935+ preedit_start_col = MAXCOL ;
4936+ }
4937+ # endif
4938+
48934939 im_delete_preedit ();
48944940
4941+ # ifdef FEAT_GUI_MACVIM
4942+ /*
4943+ * Compute the end of the preediting area: "preedit_end_col".
4944+ * According to the documentation of gtk_im_context_get_preedit_string(),
4945+ * the cursor_pos output argument returns the offset in bytes. This is
4946+ * unfortunately not true -- real life shows the offset is in characters,
4947+ * and the GTK+ source code agrees with me. Will file a bug later.
4948+ */
4949+ if (preedit_start_col != MAXCOL )
4950+ preedit_end_col = preedit_start_col ;
4951+ # endif
48954952 str = (char_u * )preedit_string ;
48964953 for (p = str , i = 0 ; * p != NUL ; p += utf_byte2len (* p ), ++ i )
48974954 {
@@ -4916,6 +4973,10 @@ im_preedit_changed_macvim(char *preedit_string, int start_index, int cursor_inde
49164973 * composing characters are not counted even if p_deco is set. */
49174974 ++ num_move_back ;
49184975 }
4976+ # ifdef FEAT_GUI_MACVIM
4977+ if (preedit_start_col != MAXCOL )
4978+ preedit_end_col += utf_ptr2cells (p );
4979+ # endif
49194980 }
49204981
49214982 if (p > str )
@@ -4991,6 +5052,9 @@ im_shutdown(void)
49915052# endif
49925053 im_is_active = FALSE;
49935054 im_commit_handler_id = 0 ;
5055+ # ifdef FEAT_GUI_MACVIM
5056+ preedit_start_col = MAXCOL ;
5057+ # endif
49945058 xim_has_preediting = FALSE;
49955059}
49965060
@@ -5149,6 +5213,9 @@ xim_reset(void)
51495213 }
51505214# endif
51515215
5216+ # ifdef FEAT_GUI_MACVIM
5217+ preedit_start_col = MAXCOL ;
5218+ # endif
51525219 xim_has_preediting = FALSE;
51535220}
51545221
0 commit comments