comparison src/insexpand.c @ 26831:537fdb6653a9 v8.2.3944

patch 8.2.3944: insert mode completion functions are too long Commit: https://github.com/vim/vim/commit/5d2e007ccbfbd749a1f201d06965b8811ff50e6e Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Thu Dec 30 11:40:53 2021 +0000 patch 8.2.3944: insert mode completion functions are too long Problem: Insert mode completion functions are too long. Solution: Split up into multiple functions. (Yegappan Lakshmanan, closes #9431)
author Bram Moolenaar <Bram@vim.org>
date Thu, 30 Dec 2021 12:45:03 +0100
parents 3949037c47f9
children 40c184a6b54b
comparison
equal deleted inserted replaced
26830:13ecf040b431 26831:537fdb6653a9
1821 c = PTR2CHAR(p); 1821 c = PTR2CHAR(p);
1822 ins_compl_addleader(c); 1822 ins_compl_addleader(c);
1823 } 1823 }
1824 1824
1825 /* 1825 /*
1826 * Prepare for Insert mode completion, or stop it. 1826 * Set the CTRL-X completion mode based on the key 'c' typed after a CTRL-X.
1827 * Called just after typing a character in Insert mode. 1827 * Uses the global variables: ctrl_x_mode, edit_submode, edit_submode_pre,
1828 * Returns TRUE when the character is not to be inserted; 1828 * compl_cont_mode and compl_cont_status.
1829 */ 1829 * Returns TRUE when the character is not to be inserted.
1830 int 1830 */
1831 ins_compl_prep(int c) 1831 static int
1832 set_ctrl_x_mode(int c)
1833 {
1834 int retval = FALSE;
1835
1836 switch (c)
1837 {
1838 case Ctrl_E:
1839 case Ctrl_Y:
1840 // scroll the window one line up or down
1841 ctrl_x_mode = CTRL_X_SCROLL;
1842 if (!(State & REPLACE_FLAG))
1843 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
1844 else
1845 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
1846 edit_submode_pre = NULL;
1847 showmode();
1848 break;
1849 case Ctrl_L:
1850 // complete whole line
1851 ctrl_x_mode = CTRL_X_WHOLE_LINE;
1852 break;
1853 case Ctrl_F:
1854 // complete filenames
1855 ctrl_x_mode = CTRL_X_FILES;
1856 break;
1857 case Ctrl_K:
1858 // complete words from a dictinoary
1859 ctrl_x_mode = CTRL_X_DICTIONARY;
1860 break;
1861 case Ctrl_R:
1862 // Register insertion without exiting CTRL-X mode
1863 // Simply allow ^R to happen without affecting ^X mode
1864 break;
1865 case Ctrl_T:
1866 // complete words from a thesaurus
1867 ctrl_x_mode = CTRL_X_THESAURUS;
1868 break;
1869 #ifdef FEAT_COMPL_FUNC
1870 case Ctrl_U:
1871 // user defined completion
1872 ctrl_x_mode = CTRL_X_FUNCTION;
1873 break;
1874 case Ctrl_O:
1875 // omni completion
1876 ctrl_x_mode = CTRL_X_OMNI;
1877 break;
1878 #endif
1879 case 's':
1880 case Ctrl_S:
1881 // complete spelling suggestions
1882 ctrl_x_mode = CTRL_X_SPELL;
1883 #ifdef FEAT_SPELL
1884 ++emsg_off; // Avoid getting the E756 error twice.
1885 spell_back_to_badword();
1886 --emsg_off;
1887 #endif
1888 break;
1889 case Ctrl_RSB:
1890 // complete tag names
1891 ctrl_x_mode = CTRL_X_TAGS;
1892 break;
1893 #ifdef FEAT_FIND_ID
1894 case Ctrl_I:
1895 case K_S_TAB:
1896 // complete keywords from included files
1897 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
1898 break;
1899 case Ctrl_D:
1900 // complete definitions from included files
1901 ctrl_x_mode = CTRL_X_PATH_DEFINES;
1902 break;
1903 #endif
1904 case Ctrl_V:
1905 case Ctrl_Q:
1906 // complete vim commands
1907 ctrl_x_mode = CTRL_X_CMDLINE;
1908 break;
1909 case Ctrl_Z:
1910 // stop completion
1911 ctrl_x_mode = CTRL_X_NORMAL;
1912 edit_submode = NULL;
1913 showmode();
1914 retval = TRUE;
1915 break;
1916 case Ctrl_P:
1917 case Ctrl_N:
1918 // ^X^P means LOCAL expansion if nothing interrupted (eg we
1919 // just started ^X mode, or there were enough ^X's to cancel
1920 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
1921 // do normal expansion when interrupting a different mode (say
1922 // ^X^F^X^P or ^P^X^X^P, see below)
1923 // nothing changes if interrupting mode 0, (eg, the flag
1924 // doesn't change when going to ADDING mode -- Acevedo
1925 if (!(compl_cont_status & CONT_INTRPT))
1926 compl_cont_status |= CONT_LOCAL;
1927 else if (compl_cont_mode != 0)
1928 compl_cont_status &= ~CONT_LOCAL;
1929 // FALLTHROUGH
1930 default:
1931 // If we have typed at least 2 ^X's... for modes != 0, we set
1932 // compl_cont_status = 0 (eg, as if we had just started ^X
1933 // mode).
1934 // For mode 0, we set "compl_cont_mode" to an impossible
1935 // value, in both cases ^X^X can be used to restart the same
1936 // mode (avoiding ADDING mode).
1937 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
1938 // 'complete' and local ^P expansions respectively.
1939 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
1940 // mode -- Acevedo
1941 if (c == Ctrl_X)
1942 {
1943 if (compl_cont_mode != 0)
1944 compl_cont_status = 0;
1945 else
1946 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
1947 }
1948 ctrl_x_mode = CTRL_X_NORMAL;
1949 edit_submode = NULL;
1950 showmode();
1951 break;
1952 }
1953
1954 return retval;
1955 }
1956
1957 /*
1958 * Stop insert completion mode
1959 */
1960 static int
1961 ins_compl_stop(int c, int prev_mode, int retval)
1832 { 1962 {
1833 char_u *ptr; 1963 char_u *ptr;
1834 #ifdef FEAT_CINDENT 1964 #ifdef FEAT_CINDENT
1835 int want_cindent; 1965 int want_cindent;
1836 #endif 1966 #endif
1967
1968 // Get here when we have finished typing a sequence of ^N and
1969 // ^P or other completion characters in CTRL-X mode. Free up
1970 // memory that was used, and make sure we can redo the insert.
1971 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
1972 {
1973 // If any of the original typed text has been changed, eg when
1974 // ignorecase is set, we must add back-spaces to the redo
1975 // buffer. We add as few as necessary to delete just the part
1976 // of the original text that has changed.
1977 // When using the longest match, edited the match or used
1978 // CTRL-E then don't use the current match.
1979 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
1980 ptr = compl_curr_match->cp_str;
1981 else
1982 ptr = NULL;
1983 ins_compl_fixRedoBufForLeader(ptr);
1984 }
1985
1986 #ifdef FEAT_CINDENT
1987 want_cindent = (get_can_cindent() && cindent_on());
1988 #endif
1989 // When completing whole lines: fix indent for 'cindent'.
1990 // Otherwise, break line if it's too long.
1991 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
1992 {
1993 #ifdef FEAT_CINDENT
1994 // re-indent the current line
1995 if (want_cindent)
1996 {
1997 do_c_expr_indent();
1998 want_cindent = FALSE; // don't do it again
1999 }
2000 #endif
2001 }
2002 else
2003 {
2004 int prev_col = curwin->w_cursor.col;
2005
2006 // put the cursor on the last char, for 'tw' formatting
2007 if (prev_col > 0)
2008 dec_cursor();
2009 // only format when something was inserted
2010 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2011 insertchar(NUL, 0, -1);
2012 if (prev_col > 0
2013 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2014 inc_cursor();
2015 }
2016
2017 // If the popup menu is displayed pressing CTRL-Y means accepting
2018 // the selection without inserting anything. When
2019 // compl_enter_selects is set the Enter key does the same.
2020 if ((c == Ctrl_Y || (compl_enter_selects
2021 && (c == CAR || c == K_KENTER || c == NL)))
2022 && pum_visible())
2023 retval = TRUE;
2024
2025 // CTRL-E means completion is Ended, go back to the typed text.
2026 // but only do this, if the Popup is still visible
2027 if (c == Ctrl_E)
2028 {
2029 ins_compl_delete();
2030 if (compl_leader != NULL)
2031 ins_bytes(compl_leader + ins_compl_len());
2032 else if (compl_first_match != NULL)
2033 ins_bytes(compl_orig_text + ins_compl_len());
2034 retval = TRUE;
2035 }
2036
2037 auto_format(FALSE, TRUE);
2038
2039 // Trigger the CompleteDonePre event to give scripts a chance to
2040 // act upon the completion before clearing the info, and restore
2041 // ctrl_x_mode, so that complete_info() can be used.
2042 ctrl_x_mode = prev_mode;
2043 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2044
2045 ins_compl_free();
2046 compl_started = FALSE;
2047 compl_matches = 0;
2048 if (!shortmess(SHM_COMPLETIONMENU))
2049 msg_clr_cmdline(); // necessary for "noshowmode"
2050 ctrl_x_mode = CTRL_X_NORMAL;
2051 compl_enter_selects = FALSE;
2052 if (edit_submode != NULL)
2053 {
2054 edit_submode = NULL;
2055 showmode();
2056 }
2057
2058 #ifdef FEAT_CMDWIN
2059 if (c == Ctrl_C && cmdwin_type != 0)
2060 // Avoid the popup menu remains displayed when leaving the
2061 // command line window.
2062 update_screen(0);
2063 #endif
2064 #ifdef FEAT_CINDENT
2065 // Indent now if a key was typed that is in 'cinkeys'.
2066 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2067 do_c_expr_indent();
2068 #endif
2069 // Trigger the CompleteDone event to give scripts a chance to act
2070 // upon the end of completion.
2071 ins_apply_autocmds(EVENT_COMPLETEDONE);
2072
2073 return retval;
2074 }
2075
2076 /*
2077 * Prepare for Insert mode completion, or stop it.
2078 * Called just after typing a character in Insert mode.
2079 * Returns TRUE when the character is not to be inserted;
2080 */
2081 int
2082 ins_compl_prep(int c)
2083 {
1837 int retval = FALSE; 2084 int retval = FALSE;
1838 int prev_mode = ctrl_x_mode; 2085 int prev_mode = ctrl_x_mode;
1839 2086
1840 // Forget any previous 'special' messages if this is actually 2087 // Forget any previous 'special' messages if this is actually
1841 // a ^X mode key - bar ^R, in which case we wait to see what it gives us. 2088 // a ^X mode key - bar ^R, in which case we wait to see what it gives us.
1908 compl_used_match = TRUE; 2155 compl_used_match = TRUE;
1909 2156
1910 } 2157 }
1911 2158
1912 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET) 2159 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
1913 {
1914 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode 2160 // We have just typed CTRL-X and aren't quite sure which CTRL-X mode
1915 // it will be yet. Now we decide. 2161 // it will be yet. Now we decide.
1916 switch (c) 2162 retval = set_ctrl_x_mode(c);
1917 {
1918 case Ctrl_E:
1919 case Ctrl_Y:
1920 ctrl_x_mode = CTRL_X_SCROLL;
1921 if (!(State & REPLACE_FLAG))
1922 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
1923 else
1924 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
1925 edit_submode_pre = NULL;
1926 showmode();
1927 break;
1928 case Ctrl_L:
1929 ctrl_x_mode = CTRL_X_WHOLE_LINE;
1930 break;
1931 case Ctrl_F:
1932 ctrl_x_mode = CTRL_X_FILES;
1933 break;
1934 case Ctrl_K:
1935 ctrl_x_mode = CTRL_X_DICTIONARY;
1936 break;
1937 case Ctrl_R:
1938 // Simply allow ^R to happen without affecting ^X mode
1939 break;
1940 case Ctrl_T:
1941 ctrl_x_mode = CTRL_X_THESAURUS;
1942 break;
1943 #ifdef FEAT_COMPL_FUNC
1944 case Ctrl_U:
1945 ctrl_x_mode = CTRL_X_FUNCTION;
1946 break;
1947 case Ctrl_O:
1948 ctrl_x_mode = CTRL_X_OMNI;
1949 break;
1950 #endif
1951 case 's':
1952 case Ctrl_S:
1953 ctrl_x_mode = CTRL_X_SPELL;
1954 #ifdef FEAT_SPELL
1955 ++emsg_off; // Avoid getting the E756 error twice.
1956 spell_back_to_badword();
1957 --emsg_off;
1958 #endif
1959 break;
1960 case Ctrl_RSB:
1961 ctrl_x_mode = CTRL_X_TAGS;
1962 break;
1963 #ifdef FEAT_FIND_ID
1964 case Ctrl_I:
1965 case K_S_TAB:
1966 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
1967 break;
1968 case Ctrl_D:
1969 ctrl_x_mode = CTRL_X_PATH_DEFINES;
1970 break;
1971 #endif
1972 case Ctrl_V:
1973 case Ctrl_Q:
1974 ctrl_x_mode = CTRL_X_CMDLINE;
1975 break;
1976 case Ctrl_Z:
1977 ctrl_x_mode = CTRL_X_NORMAL;
1978 edit_submode = NULL;
1979 showmode();
1980 retval = TRUE;
1981 break;
1982 case Ctrl_P:
1983 case Ctrl_N:
1984 // ^X^P means LOCAL expansion if nothing interrupted (eg we
1985 // just started ^X mode, or there were enough ^X's to cancel
1986 // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
1987 // do normal expansion when interrupting a different mode (say
1988 // ^X^F^X^P or ^P^X^X^P, see below)
1989 // nothing changes if interrupting mode 0, (eg, the flag
1990 // doesn't change when going to ADDING mode -- Acevedo
1991 if (!(compl_cont_status & CONT_INTRPT))
1992 compl_cont_status |= CONT_LOCAL;
1993 else if (compl_cont_mode != 0)
1994 compl_cont_status &= ~CONT_LOCAL;
1995 // FALLTHROUGH
1996 default:
1997 // If we have typed at least 2 ^X's... for modes != 0, we set
1998 // compl_cont_status = 0 (eg, as if we had just started ^X
1999 // mode).
2000 // For mode 0, we set "compl_cont_mode" to an impossible
2001 // value, in both cases ^X^X can be used to restart the same
2002 // mode (avoiding ADDING mode).
2003 // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
2004 // 'complete' and local ^P expansions respectively.
2005 // In mode 0 an extra ^X is needed since ^X^P goes to ADDING
2006 // mode -- Acevedo
2007 if (c == Ctrl_X)
2008 {
2009 if (compl_cont_mode != 0)
2010 compl_cont_status = 0;
2011 else
2012 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
2013 }
2014 ctrl_x_mode = CTRL_X_NORMAL;
2015 edit_submode = NULL;
2016 showmode();
2017 break;
2018 }
2019 }
2020 else if (ctrl_x_mode != CTRL_X_NORMAL) 2163 else if (ctrl_x_mode != CTRL_X_NORMAL)
2021 { 2164 {
2022 // We're already in CTRL-X mode, do we stay in it? 2165 // We're already in CTRL-X mode, do we stay in it?
2023 if (!vim_is_ctrl_x_key(c)) 2166 if (!vim_is_ctrl_x_key(c))
2024 { 2167 {
2038 // showing what mode we are in. 2181 // showing what mode we are in.
2039 showmode(); 2182 showmode();
2040 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P 2183 if ((ctrl_x_mode == CTRL_X_NORMAL && c != Ctrl_N && c != Ctrl_P
2041 && c != Ctrl_R && !ins_compl_pum_key(c)) 2184 && c != Ctrl_R && !ins_compl_pum_key(c))
2042 || ctrl_x_mode == CTRL_X_FINISHED) 2185 || ctrl_x_mode == CTRL_X_FINISHED)
2043 { 2186 retval = ins_compl_stop(c, prev_mode, retval);
2044 // Get here when we have finished typing a sequence of ^N and
2045 // ^P or other completion characters in CTRL-X mode. Free up
2046 // memory that was used, and make sure we can redo the insert.
2047 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
2048 {
2049 // If any of the original typed text has been changed, eg when
2050 // ignorecase is set, we must add back-spaces to the redo
2051 // buffer. We add as few as necessary to delete just the part
2052 // of the original text that has changed.
2053 // When using the longest match, edited the match or used
2054 // CTRL-E then don't use the current match.
2055 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
2056 ptr = compl_curr_match->cp_str;
2057 else
2058 ptr = NULL;
2059 ins_compl_fixRedoBufForLeader(ptr);
2060 }
2061
2062 #ifdef FEAT_CINDENT
2063 want_cindent = (get_can_cindent() && cindent_on());
2064 #endif
2065 // When completing whole lines: fix indent for 'cindent'.
2066 // Otherwise, break line if it's too long.
2067 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
2068 {
2069 #ifdef FEAT_CINDENT
2070 // re-indent the current line
2071 if (want_cindent)
2072 {
2073 do_c_expr_indent();
2074 want_cindent = FALSE; // don't do it again
2075 }
2076 #endif
2077 }
2078 else
2079 {
2080 int prev_col = curwin->w_cursor.col;
2081
2082 // put the cursor on the last char, for 'tw' formatting
2083 if (prev_col > 0)
2084 dec_cursor();
2085 // only format when something was inserted
2086 if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E)
2087 insertchar(NUL, 0, -1);
2088 if (prev_col > 0
2089 && ml_get_curline()[curwin->w_cursor.col] != NUL)
2090 inc_cursor();
2091 }
2092
2093 // If the popup menu is displayed pressing CTRL-Y means accepting
2094 // the selection without inserting anything. When
2095 // compl_enter_selects is set the Enter key does the same.
2096 if ((c == Ctrl_Y || (compl_enter_selects
2097 && (c == CAR || c == K_KENTER || c == NL)))
2098 && pum_visible())
2099 retval = TRUE;
2100
2101 // CTRL-E means completion is Ended, go back to the typed text.
2102 // but only do this, if the Popup is still visible
2103 if (c == Ctrl_E)
2104 {
2105 ins_compl_delete();
2106 if (compl_leader != NULL)
2107 ins_bytes(compl_leader + ins_compl_len());
2108 else if (compl_first_match != NULL)
2109 ins_bytes(compl_orig_text + ins_compl_len());
2110 retval = TRUE;
2111 }
2112
2113 auto_format(FALSE, TRUE);
2114
2115 // Trigger the CompleteDonePre event to give scripts a chance to
2116 // act upon the completion before clearing the info, and restore
2117 // ctrl_x_mode, so that complete_info() can be used.
2118 ctrl_x_mode = prev_mode;
2119 ins_apply_autocmds(EVENT_COMPLETEDONEPRE);
2120
2121 ins_compl_free();
2122 compl_started = FALSE;
2123 compl_matches = 0;
2124 if (!shortmess(SHM_COMPLETIONMENU))
2125 msg_clr_cmdline(); // necessary for "noshowmode"
2126 ctrl_x_mode = CTRL_X_NORMAL;
2127 compl_enter_selects = FALSE;
2128 if (edit_submode != NULL)
2129 {
2130 edit_submode = NULL;
2131 showmode();
2132 }
2133
2134 #ifdef FEAT_CMDWIN
2135 if (c == Ctrl_C && cmdwin_type != 0)
2136 // Avoid the popup menu remains displayed when leaving the
2137 // command line window.
2138 update_screen(0);
2139 #endif
2140 #ifdef FEAT_CINDENT
2141 // Indent now if a key was typed that is in 'cinkeys'.
2142 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
2143 do_c_expr_indent();
2144 #endif
2145 // Trigger the CompleteDone event to give scripts a chance to act
2146 // upon the end of completion.
2147 ins_apply_autocmds(EVENT_COMPLETEDONE);
2148 }
2149 } 2187 }
2150 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG) 2188 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
2151 // Trigger the CompleteDone event to give scripts a chance to act 2189 // Trigger the CompleteDone event to give scripts a chance to act
2152 // upon the (possibly failed) completion. 2190 // upon the (possibly failed) completion.
2153 ins_apply_autocmds(EVENT_COMPLETEDONE); 2191 ins_apply_autocmds(EVENT_COMPLETEDONE);
3197 vim_free(matches); 3235 vim_free(matches);
3198 #endif 3236 #endif
3199 } 3237 }
3200 3238
3201 /* 3239 /*
3240 * Return the next word or line from buffer "ins_buf" at position
3241 * "cur_match_pos" for completion. The length of the match is set in "len".
3242 */
3243 static char_u *
3244 ins_comp_get_next_word_or_line(
3245 buf_T *ins_buf, // buffer being scanned
3246 pos_T *cur_match_pos, // current match position
3247 int *match_len,
3248 int *cont_s_ipos) // next ^X<> will set initial_pos
3249 {
3250 char_u *ptr;
3251 int len;
3252
3253 *match_len = 0;
3254 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) +
3255 cur_match_pos->col;
3256 if (ctrl_x_mode_line_or_eval())
3257 {
3258 if (compl_cont_status & CONT_ADDING)
3259 {
3260 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3261 return NULL;
3262 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3263 if (!p_paste)
3264 ptr = skipwhite(ptr);
3265 }
3266 len = (int)STRLEN(ptr);
3267 }
3268 else
3269 {
3270 char_u *tmp_ptr = ptr;
3271
3272 if (compl_cont_status & CONT_ADDING)
3273 {
3274 tmp_ptr += compl_length;
3275 // Skip if already inside a word.
3276 if (vim_iswordp(tmp_ptr))
3277 return NULL;
3278 // Find start of next word.
3279 tmp_ptr = find_word_start(tmp_ptr);
3280 }
3281 // Find end of this word.
3282 tmp_ptr = find_word_end(tmp_ptr);
3283 len = (int)(tmp_ptr - ptr);
3284
3285 if ((compl_cont_status & CONT_ADDING) && len == compl_length)
3286 {
3287 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3288 {
3289 // Try next line, if any. the new word will be
3290 // "join" as if the normal command "J" was used.
3291 // IOSIZE is always greater than
3292 // compl_length, so the next STRNCPY always
3293 // works -- Acevedo
3294 STRNCPY(IObuff, ptr, len);
3295 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3296 tmp_ptr = ptr = skipwhite(ptr);
3297 // Find start of next word.
3298 tmp_ptr = find_word_start(tmp_ptr);
3299 // Find end of next word.
3300 tmp_ptr = find_word_end(tmp_ptr);
3301 if (tmp_ptr > ptr)
3302 {
3303 if (*ptr != ')' && IObuff[len - 1] != TAB)
3304 {
3305 if (IObuff[len - 1] != ' ')
3306 IObuff[len++] = ' ';
3307 // IObuf =~ "\k.* ", thus len >= 2
3308 if (p_js
3309 && (IObuff[len - 2] == '.'
3310 || (vim_strchr(p_cpo, CPO_JOINSP)
3311 == NULL
3312 && (IObuff[len - 2] == '?'
3313 || IObuff[len - 2] == '!'))))
3314 IObuff[len++] = ' ';
3315 }
3316 // copy as much as possible of the new word
3317 if (tmp_ptr - ptr >= IOSIZE - len)
3318 tmp_ptr = ptr + IOSIZE - len - 1;
3319 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3320 len += (int)(tmp_ptr - ptr);
3321 *cont_s_ipos = TRUE;
3322 }
3323 IObuff[len] = NUL;
3324 ptr = IObuff;
3325 }
3326 if (len == compl_length)
3327 return NULL;
3328 }
3329 }
3330
3331 *match_len = len;
3332 return ptr;
3333 }
3334
3335 /*
3202 * Get the next set of words matching "compl_pattern" for default completion(s) 3336 * Get the next set of words matching "compl_pattern" for default completion(s)
3203 * (normal ^P/^N and ^X^L). 3337 * (normal ^P/^N and ^X^L).
3204 * Search for "compl_pattern" in the buffer "ins_buf" starting from the 3338 * Search for "compl_pattern" in the buffer "ins_buf" starting from the
3205 * position "start_pos" in the "compl_direction" direction. If "save_match_pos" 3339 * position "start_pos" in the "compl_direction" direction. If "save_match_pos"
3206 * is TRUE, then set the "first_match_pos" and "last_match_pos". 3340 * is TRUE, then set the "first_match_pos" and "last_match_pos".
3297 // when ADDING, the text before the cursor matches, skip it 3431 // when ADDING, the text before the cursor matches, skip it
3298 if ((compl_cont_status & CONT_ADDING) && ins_buf == curbuf 3432 if ((compl_cont_status & CONT_ADDING) && ins_buf == curbuf
3299 && start_pos->lnum == cur_match_pos->lnum 3433 && start_pos->lnum == cur_match_pos->lnum
3300 && start_pos->col == cur_match_pos->col) 3434 && start_pos->col == cur_match_pos->col)
3301 continue; 3435 continue;
3302 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum, FALSE) + 3436
3303 cur_match_pos->col; 3437 ptr = ins_comp_get_next_word_or_line(ins_buf, cur_match_pos, &len,
3304 if (ctrl_x_mode_line_or_eval()) 3438 &cont_s_ipos);
3305 { 3439 if (ptr == NULL)
3306 if (compl_cont_status & CONT_ADDING) 3440 continue;
3307 { 3441
3308 if (cur_match_pos->lnum >= ins_buf->b_ml.ml_line_count)
3309 continue;
3310 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3311 if (!p_paste)
3312 ptr = skipwhite(ptr);
3313 }
3314 len = (int)STRLEN(ptr);
3315 }
3316 else
3317 {
3318 char_u *tmp_ptr = ptr;
3319
3320 if (compl_cont_status & CONT_ADDING)
3321 {
3322 tmp_ptr += compl_length;
3323 // Skip if already inside a word.
3324 if (vim_iswordp(tmp_ptr))
3325 continue;
3326 // Find start of next word.
3327 tmp_ptr = find_word_start(tmp_ptr);
3328 }
3329 // Find end of this word.
3330 tmp_ptr = find_word_end(tmp_ptr);
3331 len = (int)(tmp_ptr - ptr);
3332
3333 if ((compl_cont_status & CONT_ADDING) && len == compl_length)
3334 {
3335 if (cur_match_pos->lnum < ins_buf->b_ml.ml_line_count)
3336 {
3337 // Try next line, if any. the new word will be
3338 // "join" as if the normal command "J" was used.
3339 // IOSIZE is always greater than
3340 // compl_length, so the next STRNCPY always
3341 // works -- Acevedo
3342 STRNCPY(IObuff, ptr, len);
3343 ptr = ml_get_buf(ins_buf, cur_match_pos->lnum + 1, FALSE);
3344 tmp_ptr = ptr = skipwhite(ptr);
3345 // Find start of next word.
3346 tmp_ptr = find_word_start(tmp_ptr);
3347 // Find end of next word.
3348 tmp_ptr = find_word_end(tmp_ptr);
3349 if (tmp_ptr > ptr)
3350 {
3351 if (*ptr != ')' && IObuff[len - 1] != TAB)
3352 {
3353 if (IObuff[len - 1] != ' ')
3354 IObuff[len++] = ' ';
3355 // IObuf =~ "\k.* ", thus len >= 2
3356 if (p_js
3357 && (IObuff[len - 2] == '.'
3358 || (vim_strchr(p_cpo, CPO_JOINSP)
3359 == NULL
3360 && (IObuff[len - 2] == '?'
3361 || IObuff[len - 2] == '!'))))
3362 IObuff[len++] = ' ';
3363 }
3364 // copy as much as possible of the new word
3365 if (tmp_ptr - ptr >= IOSIZE - len)
3366 tmp_ptr = ptr + IOSIZE - len - 1;
3367 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
3368 len += (int)(tmp_ptr - ptr);
3369 cont_s_ipos = TRUE;
3370 }
3371 IObuff[len] = NUL;
3372 ptr = IObuff;
3373 }
3374 if (len == compl_length)
3375 continue;
3376 }
3377 }
3378 if (ins_compl_add_infercase(ptr, len, p_ic, 3442 if (ins_compl_add_infercase(ptr, len, p_ic,
3379 ins_buf == curbuf ? NULL : ins_buf->b_sfname, 3443 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
3380 0, cont_s_ipos) != NOTDONE) 3444 0, cont_s_ipos) != NOTDONE)
3381 { 3445 {
3382 found_new_match = OK; 3446 found_new_match = OK;
3562 compl_curr_match = compl_old_match; 3626 compl_curr_match = compl_old_match;
3563 } 3627 }
3564 trigger_modechanged(); 3628 trigger_modechanged();
3565 3629
3566 return i; 3630 return i;
3631 }
3632
3633 /*
3634 * Update "compl_shown_match" to the actually shown match, it may differ when
3635 * "compl_leader" is used to omit some of the matches.
3636 */
3637 static void
3638 ins_compl_update_shown_match(void)
3639 {
3640 while (!ins_compl_equal(compl_shown_match,
3641 compl_leader, (int)STRLEN(compl_leader))
3642 && compl_shown_match->cp_next != NULL
3643 && compl_shown_match->cp_next != compl_first_match)
3644 compl_shown_match = compl_shown_match->cp_next;
3645
3646 // If we didn't find it searching forward, and compl_shows_dir is
3647 // backward, find the last match.
3648 if (compl_shows_dir == BACKWARD
3649 && !ins_compl_equal(compl_shown_match,
3650 compl_leader, (int)STRLEN(compl_leader))
3651 && (compl_shown_match->cp_next == NULL
3652 || compl_shown_match->cp_next == compl_first_match))
3653 {
3654 while (!ins_compl_equal(compl_shown_match,
3655 compl_leader, (int)STRLEN(compl_leader))
3656 && compl_shown_match->cp_prev != NULL
3657 && compl_shown_match->cp_prev != compl_first_match)
3658 compl_shown_match = compl_shown_match->cp_prev;
3659 }
3567 } 3660 }
3568 3661
3569 /* 3662 /*
3570 * Delete the old text being completed. 3663 * Delete the old text being completed.
3571 */ 3664 */
3620 if (!in_compl_func) 3713 if (!in_compl_func)
3621 compl_curr_match = compl_shown_match; 3714 compl_curr_match = compl_shown_match;
3622 } 3715 }
3623 3716
3624 /* 3717 /*
3625 * Fill in the next completion in the current direction. 3718 * show the file name for the completion match (if any). Truncate the file
3626 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to 3719 * name to avoid a wait for return.
3627 * get more completions. If it is FALSE, then we just do nothing when there 3720 */
3628 * are no more completions in a given direction. The latter case is used when 3721 static void
3629 * we are still in the middle of finding completions, to allow browsing 3722 ins_compl_show_filename(void)
3630 * through the ones found so far. 3723 {
3631 * Return the total number of matches, or -1 if still unknown -- webb. 3724 char *lead = _("match in file");
3725 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3726 char_u *s;
3727 char_u *e;
3728
3729 if (space <= 0)
3730 return;
3731
3732 // We need the tail that fits. With double-byte encoding going
3733 // back from the end is very slow, thus go from the start and keep
3734 // the text that fits in "space" between "s" and "e".
3735 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
3736 {
3737 space -= ptr2cells(e);
3738 while (space < 0)
3739 {
3740 space += ptr2cells(s);
3741 MB_PTR_ADV(s);
3742 }
3743 }
3744 msg_hist_off = TRUE;
3745 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3746 s > compl_shown_match->cp_fname ? "<" : "", s);
3747 msg((char *)IObuff);
3748 msg_hist_off = FALSE;
3749 redraw_cmdline = FALSE; // don't overwrite!
3750 }
3751
3752 /*
3753 * Find the next set of matches for completion. Repeat the completion 'todo'
3754 * times. The number of matches found is returned in 'num_matches'.
3632 * 3755 *
3633 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use 3756 * If "allow_get_expansion" is TRUE, then ins_compl_get_exp() may be called to
3634 * compl_shown_match here. 3757 * get more completions. If it is FALSE, then do nothing when there are no more
3758 * completions in the given direction.
3635 * 3759 *
3636 * Note that this function may be called recursively once only. First with 3760 * If "advance" is TRUE, then completion will move to the first match.
3637 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn 3761 * Otherwise, the original text will be shown.
3638 * calls this function with "allow_get_expansion" FALSE. 3762 *
3763 * Returns OK on success and -1 if the number of matches are unknown.
3639 */ 3764 */
3640 static int 3765 static int
3641 ins_compl_next( 3766 find_next_completion_match(
3642 int allow_get_expansion, 3767 int allow_get_expansion,
3643 int count, // repeat completion this many times; should 3768 int todo, // repeat completion this many times
3644 // be at least 1 3769 int advance,
3645 int insert_match, // Insert the newly selected match 3770 int *num_matches)
3646 int in_compl_func) // called from complete_check() 3771 {
3647 { 3772 int found_end = FALSE;
3648 int num_matches = -1;
3649 int todo = count;
3650 compl_T *found_compl = NULL; 3773 compl_T *found_compl = NULL;
3651 int found_end = FALSE; 3774
3652 int advance;
3653 int started = compl_started;
3654
3655 // When user complete function return -1 for findstart which is next
3656 // time of 'always', compl_shown_match become NULL.
3657 if (compl_shown_match == NULL)
3658 return -1;
3659
3660 if (compl_leader != NULL
3661 && (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0)
3662 {
3663 // Set "compl_shown_match" to the actually shown match, it may differ
3664 // when "compl_leader" is used to omit some of the matches.
3665 while (!ins_compl_equal(compl_shown_match,
3666 compl_leader, (int)STRLEN(compl_leader))
3667 && compl_shown_match->cp_next != NULL
3668 && compl_shown_match->cp_next != compl_first_match)
3669 compl_shown_match = compl_shown_match->cp_next;
3670
3671 // If we didn't find it searching forward, and compl_shows_dir is
3672 // backward, find the last match.
3673 if (compl_shows_dir == BACKWARD
3674 && !ins_compl_equal(compl_shown_match,
3675 compl_leader, (int)STRLEN(compl_leader))
3676 && (compl_shown_match->cp_next == NULL
3677 || compl_shown_match->cp_next == compl_first_match))
3678 {
3679 while (!ins_compl_equal(compl_shown_match,
3680 compl_leader, (int)STRLEN(compl_leader))
3681 && compl_shown_match->cp_prev != NULL
3682 && compl_shown_match->cp_prev != compl_first_match)
3683 compl_shown_match = compl_shown_match->cp_prev;
3684 }
3685 }
3686
3687 if (allow_get_expansion && insert_match
3688 && (!(compl_get_longest || compl_restarting) || compl_used_match))
3689 // Delete old text to be replaced
3690 ins_compl_delete();
3691
3692 // When finding the longest common text we stick at the original text,
3693 // don't let CTRL-N or CTRL-P move to the first match.
3694 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
3695
3696 // When restarting the search don't insert the first match either.
3697 if (compl_restarting)
3698 {
3699 advance = FALSE;
3700 compl_restarting = FALSE;
3701 }
3702
3703 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3704 // around.
3705 while (--todo >= 0) 3775 while (--todo >= 0)
3706 { 3776 {
3707 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL) 3777 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
3708 { 3778 {
3709 compl_shown_match = compl_shown_match->cp_next; 3779 compl_shown_match = compl_shown_match->cp_next;
3710 found_end = (compl_first_match != NULL 3780 found_end = (compl_first_match != NULL
3711 && (compl_shown_match->cp_next == compl_first_match 3781 && (compl_shown_match->cp_next == compl_first_match
3712 || compl_shown_match == compl_first_match)); 3782 || compl_shown_match == compl_first_match));
3713 } 3783 }
3714 else if (compl_shows_dir == BACKWARD 3784 else if (compl_shows_dir == BACKWARD
3715 && compl_shown_match->cp_prev != NULL) 3785 && compl_shown_match->cp_prev != NULL)
3716 { 3786 {
3717 found_end = (compl_shown_match == compl_first_match); 3787 found_end = (compl_shown_match == compl_first_match);
3718 compl_shown_match = compl_shown_match->cp_prev; 3788 compl_shown_match = compl_shown_match->cp_prev;
3719 found_end |= (compl_shown_match == compl_first_match); 3789 found_end |= (compl_shown_match == compl_first_match);
3720 } 3790 }
3739 else 3809 else
3740 ++compl_pending; 3810 ++compl_pending;
3741 } 3811 }
3742 3812
3743 // Find matches. 3813 // Find matches.
3744 num_matches = ins_compl_get_exp(&compl_startpos); 3814 *num_matches = ins_compl_get_exp(&compl_startpos);
3745 3815
3746 // handle any pending completions 3816 // handle any pending completions
3747 while (compl_pending != 0 && compl_direction == compl_shows_dir 3817 while (compl_pending != 0 && compl_direction == compl_shows_dir
3748 && advance) 3818 && advance)
3749 { 3819 {
3750 if (compl_pending > 0 && compl_shown_match->cp_next != NULL) 3820 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
3751 { 3821 {
3752 compl_shown_match = compl_shown_match->cp_next; 3822 compl_shown_match = compl_shown_match->cp_next;
3753 --compl_pending; 3823 --compl_pending;
3763 found_end = FALSE; 3833 found_end = FALSE;
3764 } 3834 }
3765 if ((compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0 3835 if ((compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0
3766 && compl_leader != NULL 3836 && compl_leader != NULL
3767 && !ins_compl_equal(compl_shown_match, 3837 && !ins_compl_equal(compl_shown_match,
3768 compl_leader, (int)STRLEN(compl_leader))) 3838 compl_leader, (int)STRLEN(compl_leader)))
3769 ++todo; 3839 ++todo;
3770 else 3840 else
3771 // Remember a matching item. 3841 // Remember a matching item.
3772 found_compl = compl_shown_match; 3842 found_compl = compl_shown_match;
3773 3843
3780 break; 3850 break;
3781 } 3851 }
3782 todo = 1; // use first usable match after wrapping around 3852 todo = 1; // use first usable match after wrapping around
3783 } 3853 }
3784 } 3854 }
3855
3856 return OK;
3857 }
3858
3859 /*
3860 * Fill in the next completion in the current direction.
3861 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
3862 * get more completions. If it is FALSE, then we just do nothing when there
3863 * are no more completions in a given direction. The latter case is used when
3864 * we are still in the middle of finding completions, to allow browsing
3865 * through the ones found so far.
3866 * Return the total number of matches, or -1 if still unknown -- webb.
3867 *
3868 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
3869 * compl_shown_match here.
3870 *
3871 * Note that this function may be called recursively once only. First with
3872 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
3873 * calls this function with "allow_get_expansion" FALSE.
3874 */
3875 static int
3876 ins_compl_next(
3877 int allow_get_expansion,
3878 int count, // repeat completion this many times; should
3879 // be at least 1
3880 int insert_match, // Insert the newly selected match
3881 int in_compl_func) // called from complete_check()
3882 {
3883 int num_matches = -1;
3884 int todo = count;
3885 int advance;
3886 int started = compl_started;
3887
3888 // When user complete function return -1 for findstart which is next
3889 // time of 'always', compl_shown_match become NULL.
3890 if (compl_shown_match == NULL)
3891 return -1;
3892
3893 if (compl_leader != NULL
3894 && (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0)
3895 // Update "compl_shown_match" to the actually shown match
3896 ins_compl_update_shown_match();
3897
3898 if (allow_get_expansion && insert_match
3899 && (!(compl_get_longest || compl_restarting) || compl_used_match))
3900 // Delete old text to be replaced
3901 ins_compl_delete();
3902
3903 // When finding the longest common text we stick at the original text,
3904 // don't let CTRL-N or CTRL-P move to the first match.
3905 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
3906
3907 // When restarting the search don't insert the first match either.
3908 if (compl_restarting)
3909 {
3910 advance = FALSE;
3911 compl_restarting = FALSE;
3912 }
3913
3914 // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
3915 // around.
3916 if (find_next_completion_match(allow_get_expansion, todo, advance,
3917 &num_matches) == -1)
3918 return -1;
3785 3919
3786 // Insert the text of the new completion, or the compl_leader. 3920 // Insert the text of the new completion, or the compl_leader.
3787 if (compl_no_insert && !started) 3921 if (compl_no_insert && !started)
3788 { 3922 {
3789 ins_bytes(compl_orig_text + ins_compl_len()); 3923 ins_bytes(compl_orig_text + ins_compl_len());
3834 compl_enter_selects = TRUE; 3968 compl_enter_selects = TRUE;
3835 else 3969 else
3836 compl_enter_selects = !insert_match && compl_match_array != NULL; 3970 compl_enter_selects = !insert_match && compl_match_array != NULL;
3837 3971
3838 // Show the file name for the match (if any) 3972 // Show the file name for the match (if any)
3839 // Truncate the file name to avoid a wait for return.
3840 if (compl_shown_match->cp_fname != NULL) 3973 if (compl_shown_match->cp_fname != NULL)
3841 { 3974 ins_compl_show_filename();
3842 char *lead = _("match in file");
3843 int space = sc_col - vim_strsize((char_u *)lead) - 2;
3844 char_u *s;
3845 char_u *e;
3846
3847 if (space > 0)
3848 {
3849 // We need the tail that fits. With double-byte encoding going
3850 // back from the end is very slow, thus go from the start and keep
3851 // the text that fits in "space" between "s" and "e".
3852 for (s = e = compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e))
3853 {
3854 space -= ptr2cells(e);
3855 while (space < 0)
3856 {
3857 space += ptr2cells(s);
3858 MB_PTR_ADV(s);
3859 }
3860 }
3861 msg_hist_off = TRUE;
3862 vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead,
3863 s > compl_shown_match->cp_fname ? "<" : "", s);
3864 msg((char *)IObuff);
3865 msg_hist_off = FALSE;
3866 redraw_cmdline = FALSE; // don't overwrite!
3867 }
3868 }
3869 3975
3870 return num_matches; 3976 return num_matches;
3871 } 3977 }
3872 3978
3873 /* 3979 /*
4355 4461
4356 return OK; 4462 return OK;
4357 } 4463 }
4358 4464
4359 /* 4465 /*
4360 * Do Insert mode completion. 4466 * Continue an interrupted completion mode search in "line".
4361 * Called when character "c" was typed, which has a meaning for completion. 4467 *
4362 * Returns OK if completion was done, FAIL if something failed (out of mem). 4468 * If this same ctrl_x_mode has been interrupted use the text from
4363 */ 4469 * "compl_startpos" to the cursor as a pattern to add a new word instead of
4364 int 4470 * expand the one before the cursor, in word-wise if "compl_startpos" is not in
4365 ins_complete(int c, int enable_pum) 4471 * the same line as the cursor then fix it (the line has been split because it
4472 * was longer than 'tw'). if SOL is set then skip the previous pattern, a word
4473 * at the beginning of the line has been inserted, we'll look for that.
4474 */
4475 static void
4476 ins_compl_continue_search(char_u *line)
4477 {
4478 // it is a continued search
4479 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
4480 if (ctrl_x_mode == CTRL_X_NORMAL
4481 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4482 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4483 {
4484 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4485 {
4486 // line (probably) wrapped, set compl_startpos to the
4487 // first non_blank in the line, if it is not a wordchar
4488 // include it to get a better pattern, but then we don't
4489 // want the "\\<" prefix, check it below
4490 compl_col = (colnr_T)getwhitecols(line);
4491 compl_startpos.col = compl_col;
4492 compl_startpos.lnum = curwin->w_cursor.lnum;
4493 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4494 }
4495 else
4496 {
4497 // S_IPOS was set when we inserted a word that was at the
4498 // beginning of the line, which means that we'll go to SOL
4499 // mode but first we need to redefine compl_startpos
4500 if (compl_cont_status & CONT_S_IPOS)
4501 {
4502 compl_cont_status |= CONT_SOL;
4503 compl_startpos.col = (colnr_T)(skipwhite(
4504 line + compl_length
4505 + compl_startpos.col) - line);
4506 }
4507 compl_col = compl_startpos.col;
4508 }
4509 compl_length = curwin->w_cursor.col - (int)compl_col;
4510 // IObuff is used to add a "word from the next line" would we
4511 // have enough space? just being paranoid
4512 #define MIN_SPACE 75
4513 if (compl_length > (IOSIZE - MIN_SPACE))
4514 {
4515 compl_cont_status &= ~CONT_SOL;
4516 compl_length = (IOSIZE - MIN_SPACE);
4517 compl_col = curwin->w_cursor.col - compl_length;
4518 }
4519 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4520 if (compl_length < 1)
4521 compl_cont_status &= CONT_LOCAL;
4522 }
4523 else if (ctrl_x_mode_line_or_eval())
4524 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4525 else
4526 compl_cont_status = 0;
4527 }
4528
4529 /*
4530 * start insert mode completion
4531 */
4532 static int
4533 ins_compl_start(void)
4366 { 4534 {
4367 char_u *line; 4535 char_u *line;
4368 int startcol = 0; // column where searched text starts 4536 int startcol = 0; // column where searched text starts
4369 colnr_T curs_col; // cursor column 4537 colnr_T curs_col; // cursor column
4370 int n; 4538 int line_invalid = FALSE;
4371 int save_w_wrow;
4372 int save_w_leftcol;
4373 int insert_match;
4374 int save_did_ai = did_ai; 4539 int save_did_ai = did_ai;
4375 int flags = CP_ORIGINAL_TEXT; 4540 int flags = CP_ORIGINAL_TEXT;
4376 int line_invalid = FALSE; 4541
4377 4542 // First time we hit ^N or ^P (in a row, I mean)
4378 compl_direction = ins_compl_key2dir(c); 4543
4379 insert_match = ins_compl_use_match(c); 4544 did_ai = FALSE;
4380
4381 if (!compl_started)
4382 {
4383 // First time we hit ^N or ^P (in a row, I mean)
4384
4385 did_ai = FALSE;
4386 #ifdef FEAT_SMARTINDENT 4545 #ifdef FEAT_SMARTINDENT
4387 did_si = FALSE; 4546 did_si = FALSE;
4388 can_si = FALSE; 4547 can_si = FALSE;
4389 can_si_back = FALSE; 4548 can_si_back = FALSE;
4390 #endif 4549 #endif
4391 if (stop_arrow() == FAIL) 4550 if (stop_arrow() == FAIL)
4392 return FAIL; 4551 return FAIL;
4393 4552
4553 line = ml_get(curwin->w_cursor.lnum);
4554 curs_col = curwin->w_cursor.col;
4555 compl_pending = 0;
4556
4557 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4558 && compl_cont_mode == ctrl_x_mode)
4559 // this same ctrl-x_mode was interrupted previously. Continue the
4560 // completion.
4561 ins_compl_continue_search(line);
4562 else
4563 compl_cont_status &= CONT_LOCAL;
4564
4565 if (!(compl_cont_status & CONT_ADDING)) // normal expansion
4566 {
4567 compl_cont_mode = ctrl_x_mode;
4568 if (ctrl_x_mode != CTRL_X_NORMAL)
4569 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4570 compl_cont_status = 0;
4571 compl_cont_status |= CONT_N_ADDS;
4572 compl_startpos = curwin->w_cursor;
4573 startcol = (int)curs_col;
4574 compl_col = 0;
4575 }
4576
4577 // Work out completion pattern and original text -- webb
4578 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4579 {
4580 if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI
4581 || thesaurus_func_complete(ctrl_x_mode))
4582 // restore did_ai, so that adding comment leader works
4583 did_ai = save_did_ai;
4584 return FAIL;
4585 }
4586 // If "line" was changed while getting completion info get it again.
4587 if (line_invalid)
4394 line = ml_get(curwin->w_cursor.lnum); 4588 line = ml_get(curwin->w_cursor.lnum);
4395 curs_col = curwin->w_cursor.col; 4589
4396 compl_pending = 0; 4590 if (compl_cont_status & CONT_ADDING)
4397 4591 {
4398 // If this same ctrl_x_mode has been interrupted use the text from 4592 edit_submode_pre = (char_u *)_(" Adding");
4399 // "compl_startpos" to the cursor as a pattern to add a new word 4593 if (ctrl_x_mode_line_or_eval())
4400 // instead of expand the one before the cursor, in word-wise if 4594 {
4401 // "compl_startpos" is not in the same line as the cursor then fix it 4595 // Insert a new line, keep indentation but ignore 'comments'.
4402 // (the line has been split because it was longer than 'tw'). if SOL 4596 char_u *old = curbuf->b_p_com;
4403 // is set then skip the previous pattern, a word at the beginning of 4597
4404 // the line has been inserted, we'll look for that -- Acevedo. 4598 curbuf->b_p_com = (char_u *)"";
4405 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT 4599 compl_startpos.lnum = curwin->w_cursor.lnum;
4406 && compl_cont_mode == ctrl_x_mode)
4407 {
4408 // it is a continued search
4409 compl_cont_status &= ~CONT_INTRPT; // remove INTRPT
4410 if (ctrl_x_mode == CTRL_X_NORMAL
4411 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4412 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4413 {
4414 if (compl_startpos.lnum != curwin->w_cursor.lnum)
4415 {
4416 // line (probably) wrapped, set compl_startpos to the
4417 // first non_blank in the line, if it is not a wordchar
4418 // include it to get a better pattern, but then we don't
4419 // want the "\\<" prefix, check it below
4420 compl_col = (colnr_T)getwhitecols(line);
4421 compl_startpos.col = compl_col;
4422 compl_startpos.lnum = curwin->w_cursor.lnum;
4423 compl_cont_status &= ~CONT_SOL; // clear SOL if present
4424 }
4425 else
4426 {
4427 // S_IPOS was set when we inserted a word that was at the
4428 // beginning of the line, which means that we'll go to SOL
4429 // mode but first we need to redefine compl_startpos
4430 if (compl_cont_status & CONT_S_IPOS)
4431 {
4432 compl_cont_status |= CONT_SOL;
4433 compl_startpos.col = (colnr_T)(skipwhite(
4434 line + compl_length
4435 + compl_startpos.col) - line);
4436 }
4437 compl_col = compl_startpos.col;
4438 }
4439 compl_length = curwin->w_cursor.col - (int)compl_col;
4440 // IObuff is used to add a "word from the next line" would we
4441 // have enough space? just being paranoid
4442 #define MIN_SPACE 75
4443 if (compl_length > (IOSIZE - MIN_SPACE))
4444 {
4445 compl_cont_status &= ~CONT_SOL;
4446 compl_length = (IOSIZE - MIN_SPACE);
4447 compl_col = curwin->w_cursor.col - compl_length;
4448 }
4449 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4450 if (compl_length < 1)
4451 compl_cont_status &= CONT_LOCAL;
4452 }
4453 else if (ctrl_x_mode_line_or_eval())
4454 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
4455 else
4456 compl_cont_status = 0;
4457 }
4458 else
4459 compl_cont_status &= CONT_LOCAL;
4460
4461 if (!(compl_cont_status & CONT_ADDING)) // normal expansion
4462 {
4463 compl_cont_mode = ctrl_x_mode;
4464 if (ctrl_x_mode != CTRL_X_NORMAL)
4465 // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL
4466 compl_cont_status = 0;
4467 compl_cont_status |= CONT_N_ADDS;
4468 compl_startpos = curwin->w_cursor;
4469 startcol = (int)curs_col;
4470 compl_col = 0;
4471 }
4472
4473 // Work out completion pattern and original text -- webb
4474 if (compl_get_info(line, startcol, curs_col, &line_invalid) == FAIL)
4475 {
4476 if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI
4477 || thesaurus_func_complete(ctrl_x_mode))
4478 // restore did_ai, so that adding comment leader works
4479 did_ai = save_did_ai;
4480 return FAIL;
4481 }
4482 // If "line" was changed while getting completion info get it again.
4483 if (line_invalid)
4484 line = ml_get(curwin->w_cursor.lnum);
4485
4486 if (compl_cont_status & CONT_ADDING)
4487 {
4488 edit_submode_pre = (char_u *)_(" Adding");
4489 if (ctrl_x_mode_line_or_eval())
4490 {
4491 // Insert a new line, keep indentation but ignore 'comments'.
4492 char_u *old = curbuf->b_p_com;
4493
4494 curbuf->b_p_com = (char_u *)"";
4495 compl_startpos.lnum = curwin->w_cursor.lnum;
4496 compl_startpos.col = compl_col;
4497 ins_eol('\r');
4498 curbuf->b_p_com = old;
4499 compl_length = 0;
4500 compl_col = curwin->w_cursor.col;
4501 }
4502 }
4503 else
4504 {
4505 edit_submode_pre = NULL;
4506 compl_startpos.col = compl_col; 4600 compl_startpos.col = compl_col;
4507 } 4601 ins_eol('\r');
4508 4602 curbuf->b_p_com = old;
4509 if (compl_cont_status & CONT_LOCAL) 4603 compl_length = 0;
4510 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); 4604 compl_col = curwin->w_cursor.col;
4511 else 4605 }
4512 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); 4606 }
4513 4607 else
4514 // If any of the original typed text has been changed we need to fix 4608 {
4515 // the redo buffer. 4609 edit_submode_pre = NULL;
4516 ins_compl_fixRedoBufForLeader(NULL); 4610 compl_startpos.col = compl_col;
4517 4611 }
4518 // Always add completion for the original text. 4612
4519 vim_free(compl_orig_text); 4613 if (compl_cont_status & CONT_LOCAL)
4520 compl_orig_text = vim_strnsave(line + compl_col, compl_length); 4614 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
4521 if (p_ic) 4615 else
4522 flags |= CP_ICASE; 4616 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
4523 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, 4617
4524 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK) 4618 // If any of the original typed text has been changed we need to fix
4525 { 4619 // the redo buffer.
4526 VIM_CLEAR(compl_pattern); 4620 ins_compl_fixRedoBufForLeader(NULL);
4527 VIM_CLEAR(compl_orig_text); 4621
4528 return FAIL; 4622 // Always add completion for the original text.
4529 } 4623 vim_free(compl_orig_text);
4530 4624 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
4531 // showmode might reset the internal line pointers, so it must 4625 if (p_ic)
4532 // be called before line = ml_get(), or when this address is no 4626 flags |= CP_ICASE;
4533 // longer needed. -- Acevedo. 4627 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
4534 edit_submode_extra = (char_u *)_("-- Searching..."); 4628 -1, NULL, NULL, NULL, 0, flags, FALSE) != OK)
4535 edit_submode_highl = HLF_COUNT; 4629 {
4536 showmode(); 4630 VIM_CLEAR(compl_pattern);
4537 edit_submode_extra = NULL; 4631 VIM_CLEAR(compl_orig_text);
4538 out_flush();
4539 }
4540 else if (insert_match && stop_arrow() == FAIL)
4541 return FAIL; 4632 return FAIL;
4542 4633 }
4543 compl_shown_match = compl_curr_match; 4634
4544 compl_shows_dir = compl_direction; 4635 // showmode might reset the internal line pointers, so it must
4545 4636 // be called before line = ml_get(), or when this address is no
4546 // Find next match (and following matches). 4637 // longer needed. -- Acevedo.
4547 save_w_wrow = curwin->w_wrow; 4638 edit_submode_extra = (char_u *)_("-- Searching...");
4548 save_w_leftcol = curwin->w_leftcol; 4639 edit_submode_highl = HLF_COUNT;
4549 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE); 4640 showmode();
4550 4641 edit_submode_extra = NULL;
4551 // may undisplay the popup menu 4642 out_flush();
4552 ins_compl_upd_pum(); 4643
4553 4644 return OK;
4554 if (n > 1) // all matches have been found 4645 }
4555 compl_matches = n; 4646
4556 compl_curr_match = compl_shown_match; 4647 /*
4557 compl_direction = compl_shows_dir; 4648 * display the completion status message
4558 4649 */
4559 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert 4650 static void
4560 // mode. 4651 ins_compl_show_statusmsg(void)
4561 if (got_int && !global_busy) 4652 {
4562 {
4563 (void)vgetc();
4564 got_int = FALSE;
4565 }
4566
4567 // we found no match if the list has only the "compl_orig_text"-entry 4653 // we found no match if the list has only the "compl_orig_text"-entry
4568 if (compl_first_match == compl_first_match->cp_next) 4654 if (compl_first_match == compl_first_match->cp_next)
4569 { 4655 {
4570 edit_submode_extra = (compl_cont_status & CONT_ADDING) 4656 edit_submode_extra = (compl_cont_status & CONT_ADDING)
4571 && compl_length > 1 4657 && compl_length > 1
4572 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf); 4658 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
4573 edit_submode_highl = HLF_E; 4659 edit_submode_highl = HLF_E;
4574 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, 4660 }
4575 // because we couldn't expand anything at first place, but if we used
4576 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4577 // (such as M in M'exico) if not tried already. -- Acevedo
4578 if ( compl_length > 1
4579 || (compl_cont_status & CONT_ADDING)
4580 || (ctrl_x_mode != CTRL_X_NORMAL
4581 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4582 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
4583 compl_cont_status &= ~CONT_N_ADDS;
4584 }
4585
4586 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
4587 compl_cont_status |= CONT_S_IPOS;
4588 else
4589 compl_cont_status &= ~CONT_S_IPOS;
4590 4661
4591 if (edit_submode_extra == NULL) 4662 if (edit_submode_extra == NULL)
4592 { 4663 {
4593 if (compl_curr_match->cp_flags & CP_ORIGINAL_TEXT) 4664 if (compl_curr_match->cp_flags & CP_ORIGINAL_TEXT)
4594 { 4665 {
4621 // Translations may need more than twice that. 4692 // Translations may need more than twice that.
4622 static char_u match_ref[81]; 4693 static char_u match_ref[81];
4623 4694
4624 if (compl_matches > 0) 4695 if (compl_matches > 0)
4625 vim_snprintf((char *)match_ref, sizeof(match_ref), 4696 vim_snprintf((char *)match_ref, sizeof(match_ref),
4626 _("match %d of %d"), 4697 _("match %d of %d"),
4627 compl_curr_match->cp_number, compl_matches); 4698 compl_curr_match->cp_number, compl_matches);
4628 else 4699 else
4629 vim_snprintf((char *)match_ref, sizeof(match_ref), 4700 vim_snprintf((char *)match_ref, sizeof(match_ref),
4630 _("match %d"), 4701 _("match %d"),
4631 compl_curr_match->cp_number); 4702 compl_curr_match->cp_number);
4632 edit_submode_extra = match_ref; 4703 edit_submode_extra = match_ref;
4633 edit_submode_highl = HLF_R; 4704 edit_submode_highl = HLF_R;
4634 if (dollar_vcol >= 0) 4705 if (dollar_vcol >= 0)
4635 curs_columns(FALSE); 4706 curs_columns(FALSE);
4636 } 4707 }
4656 } 4727 }
4657 else 4728 else
4658 msg_clr_cmdline(); // necessary for "noshowmode" 4729 msg_clr_cmdline(); // necessary for "noshowmode"
4659 } 4730 }
4660 } 4731 }
4732 }
4733
4734 /*
4735 * Do Insert mode completion.
4736 * Called when character "c" was typed, which has a meaning for completion.
4737 * Returns OK if completion was done, FAIL if something failed (out of mem).
4738 */
4739 int
4740 ins_complete(int c, int enable_pum)
4741 {
4742 int n;
4743 int save_w_wrow;
4744 int save_w_leftcol;
4745 int insert_match;
4746
4747 compl_direction = ins_compl_key2dir(c);
4748 insert_match = ins_compl_use_match(c);
4749
4750 if (!compl_started)
4751 {
4752 if (ins_compl_start() == FAIL)
4753 return FAIL;
4754 }
4755 else if (insert_match && stop_arrow() == FAIL)
4756 return FAIL;
4757
4758 compl_shown_match = compl_curr_match;
4759 compl_shows_dir = compl_direction;
4760
4761 // Find next match (and following matches).
4762 save_w_wrow = curwin->w_wrow;
4763 save_w_leftcol = curwin->w_leftcol;
4764 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
4765
4766 // may undisplay the popup menu
4767 ins_compl_upd_pum();
4768
4769 if (n > 1) // all matches have been found
4770 compl_matches = n;
4771 compl_curr_match = compl_shown_match;
4772 compl_direction = compl_shows_dir;
4773
4774 // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
4775 // mode.
4776 if (got_int && !global_busy)
4777 {
4778 (void)vgetc();
4779 got_int = FALSE;
4780 }
4781
4782 // we found no match if the list has only the "compl_orig_text"-entry
4783 if (compl_first_match == compl_first_match->cp_next)
4784 {
4785 // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
4786 // because we couldn't expand anything at first place, but if we used
4787 // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
4788 // (such as M in M'exico) if not tried already. -- Acevedo
4789 if (compl_length > 1
4790 || (compl_cont_status & CONT_ADDING)
4791 || (ctrl_x_mode != CTRL_X_NORMAL
4792 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
4793 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
4794 compl_cont_status &= ~CONT_N_ADDS;
4795 }
4796
4797 if (compl_curr_match->cp_flags & CP_CONT_S_IPOS)
4798 compl_cont_status |= CONT_S_IPOS;
4799 else
4800 compl_cont_status &= ~CONT_S_IPOS;
4801
4802 ins_compl_show_statusmsg();
4661 4803
4662 // Show the popup menu, unless we got interrupted. 4804 // Show the popup menu, unless we got interrupted.
4663 if (enable_pum && !compl_interrupted) 4805 if (enable_pum && !compl_interrupted)
4664 show_pum(save_w_wrow, save_w_leftcol); 4806 show_pum(save_w_wrow, save_w_leftcol);
4665 4807