Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * help.c: functions for Vim help |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | /* |
| 17 | * ":help": open a read-only window on a help file |
| 18 | */ |
| 19 | void |
| 20 | ex_help(exarg_T *eap) |
| 21 | { |
| 22 | char_u *arg; |
| 23 | char_u *tag; |
| 24 | FILE *helpfd; // file descriptor of help file |
| 25 | int n; |
| 26 | int i; |
| 27 | win_T *wp; |
| 28 | int num_matches; |
| 29 | char_u **matches; |
| 30 | char_u *p; |
| 31 | int empty_fnum = 0; |
| 32 | int alt_fnum = 0; |
| 33 | buf_T *buf; |
| 34 | #ifdef FEAT_MULTI_LANG |
| 35 | int len; |
| 36 | char_u *lang; |
| 37 | #endif |
| 38 | #ifdef FEAT_FOLDING |
| 39 | int old_KeyTyped = KeyTyped; |
| 40 | #endif |
| 41 | |
Bram Moolenaar | 349f609 | 2020-10-06 20:46:49 +0200 | [diff] [blame] | 42 | if (ERROR_IF_ANY_POPUP_WINDOW) |
| 43 | return; |
| 44 | |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 45 | if (eap != NULL) |
| 46 | { |
| 47 | // A ":help" command ends at the first LF, or at a '|' that is |
| 48 | // followed by some text. Set nextcmd to the following command. |
| 49 | for (arg = eap->arg; *arg; ++arg) |
| 50 | { |
| 51 | if (*arg == '\n' || *arg == '\r' |
| 52 | || (*arg == '|' && arg[1] != NUL && arg[1] != '|')) |
| 53 | { |
| 54 | *arg++ = NUL; |
| 55 | eap->nextcmd = arg; |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | arg = eap->arg; |
| 60 | |
| 61 | if (eap->forceit && *arg == NUL && !curbuf->b_help) |
| 62 | { |
| 63 | emsg(_("E478: Don't panic!")); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | if (eap->skip) // not executing commands |
| 68 | return; |
| 69 | } |
| 70 | else |
| 71 | arg = (char_u *)""; |
| 72 | |
| 73 | // remove trailing blanks |
| 74 | p = arg + STRLEN(arg) - 1; |
| 75 | while (p > arg && VIM_ISWHITE(*p) && p[-1] != '\\') |
| 76 | *p-- = NUL; |
| 77 | |
| 78 | #ifdef FEAT_MULTI_LANG |
| 79 | // Check for a specified language |
| 80 | lang = check_help_lang(arg); |
| 81 | #endif |
| 82 | |
| 83 | // When no argument given go to the index. |
| 84 | if (*arg == NUL) |
| 85 | arg = (char_u *)"help.txt"; |
| 86 | |
| 87 | // Check if there is a match for the argument. |
| 88 | n = find_help_tags(arg, &num_matches, &matches, |
| 89 | eap != NULL && eap->forceit); |
| 90 | |
| 91 | i = 0; |
| 92 | #ifdef FEAT_MULTI_LANG |
| 93 | if (n != FAIL && lang != NULL) |
| 94 | // Find first item with the requested language. |
| 95 | for (i = 0; i < num_matches; ++i) |
| 96 | { |
| 97 | len = (int)STRLEN(matches[i]); |
| 98 | if (len > 3 && matches[i][len - 3] == '@' |
| 99 | && STRICMP(matches[i] + len - 2, lang) == 0) |
| 100 | break; |
| 101 | } |
| 102 | #endif |
| 103 | if (i >= num_matches || n == FAIL) |
| 104 | { |
| 105 | #ifdef FEAT_MULTI_LANG |
| 106 | if (lang != NULL) |
| 107 | semsg(_("E661: Sorry, no '%s' help for %s"), lang, arg); |
| 108 | else |
| 109 | #endif |
Bram Moolenaar | eb822a2 | 2021-12-31 15:09:27 +0000 | [diff] [blame] | 110 | semsg(_(e_sorry_no_help_for_str), arg); |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 111 | if (n != FAIL) |
| 112 | FreeWild(num_matches, matches); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | // The first match (in the requested language) is the best match. |
| 117 | tag = vim_strsave(matches[i]); |
| 118 | FreeWild(num_matches, matches); |
| 119 | |
| 120 | #ifdef FEAT_GUI |
| 121 | need_mouse_correct = TRUE; |
| 122 | #endif |
| 123 | |
| 124 | // Re-use an existing help window or open a new one. |
| 125 | // Always open a new one for ":tab help". |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 126 | if (!bt_help(curwin->w_buffer) || cmdmod.cmod_tab != 0) |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 127 | { |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 128 | if (cmdmod.cmod_tab != 0) |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 129 | wp = NULL; |
| 130 | else |
| 131 | FOR_ALL_WINDOWS(wp) |
| 132 | if (bt_help(wp->w_buffer)) |
| 133 | break; |
| 134 | if (wp != NULL && wp->w_buffer->b_nwindows > 0) |
| 135 | win_enter(wp, TRUE); |
| 136 | else |
| 137 | { |
| 138 | // There is no help window yet. |
| 139 | // Try to open the file specified by the "helpfile" option. |
| 140 | if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL) |
| 141 | { |
| 142 | smsg(_("Sorry, help file \"%s\" not found"), p_hf); |
| 143 | goto erret; |
| 144 | } |
| 145 | fclose(helpfd); |
| 146 | |
| 147 | // Split off help window; put it at far top if no position |
| 148 | // specified, the current window is vertically split and |
| 149 | // narrow. |
| 150 | n = WSP_HELP; |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 151 | if (cmdmod.cmod_split == 0 && curwin->w_width != Columns |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 152 | && curwin->w_width < 80) |
| 153 | n |= WSP_TOP; |
| 154 | if (win_split(0, n) == FAIL) |
| 155 | goto erret; |
| 156 | |
| 157 | if (curwin->w_height < p_hh) |
| 158 | win_setheight((int)p_hh); |
| 159 | |
| 160 | // Open help file (do_ecmd() will set b_help flag, readfile() will |
| 161 | // set b_p_ro flag). |
| 162 | // Set the alternate file to the previously edited file. |
| 163 | alt_fnum = curbuf->b_fnum; |
| 164 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL, |
| 165 | ECMD_HIDE + ECMD_SET_HELP, |
| 166 | NULL); // buffer is still open, don't store info |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 167 | if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0) |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 168 | curwin->w_alt_fnum = alt_fnum; |
| 169 | empty_fnum = curbuf->b_fnum; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (!p_im) |
| 174 | restart_edit = 0; // don't want insert mode in help file |
| 175 | |
| 176 | #ifdef FEAT_FOLDING |
| 177 | // Restore KeyTyped, setting 'filetype=help' may reset it. |
| 178 | // It is needed for do_tag top open folds under the cursor. |
| 179 | KeyTyped = old_KeyTyped; |
| 180 | #endif |
| 181 | |
| 182 | if (tag != NULL) |
| 183 | do_tag(tag, DT_HELP, 1, FALSE, TRUE); |
| 184 | |
| 185 | // Delete the empty buffer if we're not using it. Careful: autocommands |
| 186 | // may have jumped to another window, check that the buffer is not in a |
| 187 | // window. |
| 188 | if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum) |
| 189 | { |
| 190 | buf = buflist_findnr(empty_fnum); |
| 191 | if (buf != NULL && buf->b_nwindows == 0) |
| 192 | wipe_buffer(buf, TRUE); |
| 193 | } |
| 194 | |
| 195 | // keep the previous alternate file |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 196 | if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum |
| 197 | && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0) |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 198 | curwin->w_alt_fnum = alt_fnum; |
| 199 | |
| 200 | erret: |
| 201 | vim_free(tag); |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * ":helpclose": Close one help window |
| 206 | */ |
| 207 | void |
| 208 | ex_helpclose(exarg_T *eap UNUSED) |
| 209 | { |
| 210 | win_T *win; |
| 211 | |
| 212 | FOR_ALL_WINDOWS(win) |
| 213 | { |
| 214 | if (bt_help(win->w_buffer)) |
| 215 | { |
| 216 | win_close(win, FALSE); |
| 217 | return; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | #if defined(FEAT_MULTI_LANG) || defined(PROTO) |
| 223 | /* |
| 224 | * In an argument search for a language specifiers in the form "@xx". |
| 225 | * Changes the "@" to NUL if found, and returns a pointer to "xx". |
| 226 | * Returns NULL if not found. |
| 227 | */ |
| 228 | char_u * |
| 229 | check_help_lang(char_u *arg) |
| 230 | { |
| 231 | int len = (int)STRLEN(arg); |
| 232 | |
| 233 | if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2]) |
| 234 | && ASCII_ISALPHA(arg[len - 1])) |
| 235 | { |
| 236 | arg[len - 3] = NUL; // remove the '@' |
| 237 | return arg + len - 2; |
| 238 | } |
| 239 | return NULL; |
| 240 | } |
| 241 | #endif |
| 242 | |
| 243 | /* |
| 244 | * Return a heuristic indicating how well the given string matches. The |
| 245 | * smaller the number, the better the match. This is the order of priorities, |
| 246 | * from best match to worst match: |
| 247 | * - Match with least alphanumeric characters is better. |
| 248 | * - Match with least total characters is better. |
| 249 | * - Match towards the start is better. |
| 250 | * - Match starting with "+" is worse (feature instead of command) |
| 251 | * Assumption is made that the matched_string passed has already been found to |
| 252 | * match some string for which help is requested. webb. |
| 253 | */ |
| 254 | int |
| 255 | help_heuristic( |
| 256 | char_u *matched_string, |
| 257 | int offset, // offset for match |
| 258 | int wrong_case) // no matching case |
| 259 | { |
| 260 | int num_letters; |
| 261 | char_u *p; |
| 262 | |
| 263 | num_letters = 0; |
| 264 | for (p = matched_string; *p; p++) |
| 265 | if (ASCII_ISALNUM(*p)) |
| 266 | num_letters++; |
| 267 | |
| 268 | // Multiply the number of letters by 100 to give it a much bigger |
| 269 | // weighting than the number of characters. |
| 270 | // If there only is a match while ignoring case, add 5000. |
| 271 | // If the match starts in the middle of a word, add 10000 to put it |
| 272 | // somewhere in the last half. |
| 273 | // If the match is more than 2 chars from the start, multiply by 200 to |
| 274 | // put it after matches at the start. |
| 275 | if (ASCII_ISALNUM(matched_string[offset]) && offset > 0 |
| 276 | && ASCII_ISALNUM(matched_string[offset - 1])) |
| 277 | offset += 10000; |
| 278 | else if (offset > 2) |
| 279 | offset *= 200; |
| 280 | if (wrong_case) |
| 281 | offset += 5000; |
| 282 | // Features are less interesting than the subjects themselves, but "+" |
| 283 | // alone is not a feature. |
| 284 | if (matched_string[0] == '+' && matched_string[1] != NUL) |
| 285 | offset += 100; |
| 286 | return (int)(100 * num_letters + STRLEN(matched_string) + offset); |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Compare functions for qsort() below, that checks the help heuristics number |
| 291 | * that has been put after the tagname by find_tags(). |
| 292 | */ |
| 293 | static int |
| 294 | help_compare(const void *s1, const void *s2) |
| 295 | { |
| 296 | char *p1; |
| 297 | char *p2; |
| 298 | int cmp; |
| 299 | |
| 300 | p1 = *(char **)s1 + strlen(*(char **)s1) + 1; |
| 301 | p2 = *(char **)s2 + strlen(*(char **)s2) + 1; |
| 302 | |
| 303 | // Compare by help heuristic number first. |
| 304 | cmp = strcmp(p1, p2); |
| 305 | if (cmp != 0) |
| 306 | return cmp; |
| 307 | |
| 308 | // Compare by strings as tie-breaker when same heuristic number. |
| 309 | return strcmp(*(char **)s1, *(char **)s2); |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Find all help tags matching "arg", sort them and return in matches[], with |
| 314 | * the number of matches in num_matches. |
| 315 | * The matches will be sorted with a "best" match algorithm. |
| 316 | * When "keep_lang" is TRUE try keeping the language of the current buffer. |
| 317 | */ |
| 318 | int |
| 319 | find_help_tags( |
| 320 | char_u *arg, |
| 321 | int *num_matches, |
| 322 | char_u ***matches, |
| 323 | int keep_lang) |
| 324 | { |
| 325 | char_u *s, *d; |
| 326 | int i; |
Bram Moolenaar | 6eb36ad | 2020-10-11 19:08:33 +0200 | [diff] [blame] | 327 | // Specific tags that either have a specific replacement or won't go |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 328 | // through the generic rules. |
Bram Moolenaar | 6eb36ad | 2020-10-11 19:08:33 +0200 | [diff] [blame] | 329 | static char *(except_tbl[][2]) = { |
| 330 | {"*", "star"}, |
| 331 | {"g*", "gstar"}, |
| 332 | {"[*", "[star"}, |
| 333 | {"]*", "]star"}, |
| 334 | {":*", ":star"}, |
| 335 | {"/*", "/star"}, |
| 336 | {"/\\*", "/\\\\star"}, |
| 337 | {"\"*", "quotestar"}, |
| 338 | {"**", "starstar"}, |
| 339 | {"cpo-*", "cpo-star"}, |
| 340 | {"/\\(\\)", "/\\\\(\\\\)"}, |
| 341 | {"/\\%(\\)", "/\\\\%(\\\\)"}, |
| 342 | {"?", "?"}, |
| 343 | {"??", "??"}, |
| 344 | {":?", ":?"}, |
| 345 | {"?<CR>", "?<CR>"}, |
| 346 | {"g?", "g?"}, |
| 347 | {"g?g?", "g?g?"}, |
| 348 | {"g??", "g??"}, |
| 349 | {"-?", "-?"}, |
| 350 | {"q?", "q?"}, |
| 351 | {"v_g?", "v_g?"}, |
| 352 | {"/\\?", "/\\\\?"}, |
| 353 | {"/\\z(\\)", "/\\\\z(\\\\)"}, |
| 354 | {"\\=", "\\\\="}, |
| 355 | {":s\\=", ":s\\\\="}, |
| 356 | {"[count]", "\\[count]"}, |
| 357 | {"[quotex]", "\\[quotex]"}, |
| 358 | {"[range]", "\\[range]"}, |
| 359 | {":[range]", ":\\[range]"}, |
| 360 | {"[pattern]", "\\[pattern]"}, |
| 361 | {"\\|", "\\\\bar"}, |
| 362 | {"\\%$", "/\\\\%\\$"}, |
| 363 | {"s/\\~", "s/\\\\\\~"}, |
| 364 | {"s/\\U", "s/\\\\U"}, |
| 365 | {"s/\\L", "s/\\\\L"}, |
| 366 | {"s/\\1", "s/\\\\1"}, |
| 367 | {"s/\\2", "s/\\\\2"}, |
| 368 | {"s/\\3", "s/\\\\3"}, |
| 369 | {"s/\\9", "s/\\\\9"}, |
| 370 | {NULL, NULL} |
| 371 | }; |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 372 | static char *(expr_table[]) = {"!=?", "!~?", "<=?", "<?", "==?", "=~?", |
Bram Moolenaar | 6eb36ad | 2020-10-11 19:08:33 +0200 | [diff] [blame] | 373 | ">=?", ">?", "is?", "isnot?"}; |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 374 | int flags; |
| 375 | |
| 376 | d = IObuff; // assume IObuff is long enough! |
Bram Moolenaar | 6eb36ad | 2020-10-11 19:08:33 +0200 | [diff] [blame] | 377 | d[0] = NUL; |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 378 | |
| 379 | if (STRNICMP(arg, "expr-", 5) == 0) |
| 380 | { |
| 381 | // When the string starting with "expr-" and containing '?' and matches |
| 382 | // the table, it is taken literally (but ~ is escaped). Otherwise '?' |
| 383 | // is recognized as a wildcard. |
K.Takata | eeec254 | 2021-06-02 13:28:16 +0200 | [diff] [blame] | 384 | for (i = (int)ARRAY_LENGTH(expr_table); --i >= 0; ) |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 385 | if (STRCMP(arg + 5, expr_table[i]) == 0) |
| 386 | { |
| 387 | int si = 0, di = 0; |
| 388 | |
| 389 | for (;;) |
| 390 | { |
| 391 | if (arg[si] == '~') |
| 392 | d[di++] = '\\'; |
| 393 | d[di++] = arg[si]; |
| 394 | if (arg[si] == NUL) |
| 395 | break; |
| 396 | ++si; |
| 397 | } |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | // Recognize a few exceptions to the rule. Some strings that contain |
Bram Moolenaar | 6eb36ad | 2020-10-11 19:08:33 +0200 | [diff] [blame] | 404 | // '*'are changed to "star", otherwise '*' is recognized as a wildcard. |
| 405 | for (i = 0; except_tbl[i][0] != NULL; ++i) |
| 406 | if (STRCMP(arg, except_tbl[i][0]) == 0) |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 407 | { |
Bram Moolenaar | 6eb36ad | 2020-10-11 19:08:33 +0200 | [diff] [blame] | 408 | STRCPY(d, except_tbl[i][1]); |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 409 | break; |
| 410 | } |
| 411 | } |
| 412 | |
Bram Moolenaar | 6eb36ad | 2020-10-11 19:08:33 +0200 | [diff] [blame] | 413 | if (d[0] == NUL) // no match in table |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 414 | { |
| 415 | // Replace "\S" with "/\\S", etc. Otherwise every tag is matched. |
| 416 | // Also replace "\%^" and "\%(", they match every tag too. |
| 417 | // Also "\zs", "\z1", etc. |
| 418 | // Also "\@<", "\@=", "\@<=", etc. |
| 419 | // And also "\_$" and "\_^". |
| 420 | if (arg[0] == '\\' |
| 421 | && ((arg[1] != NUL && arg[2] == NUL) |
| 422 | || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL |
| 423 | && arg[2] != NUL))) |
| 424 | { |
Bram Moolenaar | bd228fd | 2021-11-25 10:50:12 +0000 | [diff] [blame] | 425 | vim_snprintf((char *)d, IOSIZE, "/\\\\%s", arg + 1); |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 426 | // Check for "/\\_$", should be "/\\_\$" |
| 427 | if (d[3] == '_' && d[4] == '$') |
| 428 | STRCPY(d + 4, "\\$"); |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | // Replace: |
| 433 | // "[:...:]" with "\[:...:]" |
| 434 | // "[++...]" with "\[++...]" |
| 435 | // "\{" with "\\{" -- matching "} \}" |
| 436 | if ((arg[0] == '[' && (arg[1] == ':' |
| 437 | || (arg[1] == '+' && arg[2] == '+'))) |
| 438 | || (arg[0] == '\\' && arg[1] == '{')) |
| 439 | *d++ = '\\'; |
| 440 | |
| 441 | // If tag starts with "('", skip the "(". Fixes CTRL-] on ('option'. |
| 442 | if (*arg == '(' && arg[1] == '\'') |
| 443 | arg++; |
| 444 | for (s = arg; *s; ++s) |
| 445 | { |
| 446 | // Replace "|" with "bar" and '"' with "quote" to match the name of |
| 447 | // the tags for these commands. |
| 448 | // Replace "*" with ".*" and "?" with "." to match command line |
| 449 | // completion. |
| 450 | // Insert a backslash before '~', '$' and '.' to avoid their |
| 451 | // special meaning. |
| 452 | if (d - IObuff > IOSIZE - 10) // getting too long!? |
| 453 | break; |
| 454 | switch (*s) |
| 455 | { |
| 456 | case '|': STRCPY(d, "bar"); |
| 457 | d += 3; |
| 458 | continue; |
| 459 | case '"': STRCPY(d, "quote"); |
| 460 | d += 5; |
| 461 | continue; |
| 462 | case '*': *d++ = '.'; |
| 463 | break; |
| 464 | case '?': *d++ = '.'; |
| 465 | continue; |
| 466 | case '$': |
| 467 | case '.': |
| 468 | case '~': *d++ = '\\'; |
| 469 | break; |
| 470 | } |
| 471 | |
| 472 | // Replace "^x" by "CTRL-X". Don't do this for "^_" to make |
| 473 | // ":help i_^_CTRL-D" work. |
| 474 | // Insert '-' before and after "CTRL-X" when applicable. |
| 475 | if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1]) |
| 476 | || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL))) |
| 477 | { |
| 478 | if (d > IObuff && d[-1] != '_' && d[-1] != '\\') |
| 479 | *d++ = '_'; // prepend a '_' to make x_CTRL-x |
| 480 | STRCPY(d, "CTRL-"); |
| 481 | d += 5; |
| 482 | if (*s < ' ') |
| 483 | { |
| 484 | #ifdef EBCDIC |
| 485 | *d++ = CtrlChar(*s); |
| 486 | #else |
| 487 | *d++ = *s + '@'; |
| 488 | #endif |
| 489 | if (d[-1] == '\\') |
| 490 | *d++ = '\\'; // double a backslash |
| 491 | } |
| 492 | else |
| 493 | *d++ = *++s; |
| 494 | if (s[1] != NUL && s[1] != '_') |
| 495 | *d++ = '_'; // append a '_' |
| 496 | continue; |
| 497 | } |
| 498 | else if (*s == '^') // "^" or "CTRL-^" or "^_" |
| 499 | *d++ = '\\'; |
| 500 | |
| 501 | // Insert a backslash before a backslash after a slash, for search |
| 502 | // pattern tags: "/\|" --> "/\\|". |
| 503 | else if (s[0] == '\\' && s[1] != '\\' |
| 504 | && *arg == '/' && s == arg + 1) |
| 505 | *d++ = '\\'; |
| 506 | |
| 507 | // "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in |
| 508 | // "CTRL-\_CTRL-N" |
| 509 | if (STRNICMP(s, "CTRL-\\_", 7) == 0) |
| 510 | { |
| 511 | STRCPY(d, "CTRL-\\\\"); |
| 512 | d += 7; |
| 513 | s += 6; |
| 514 | } |
| 515 | |
| 516 | *d++ = *s; |
| 517 | |
| 518 | // If tag contains "({" or "([", tag terminates at the "(". |
| 519 | // This is for help on functions, e.g.: abs({expr}). |
| 520 | if (*s == '(' && (s[1] == '{' || s[1] =='[')) |
| 521 | break; |
| 522 | |
| 523 | // If tag starts with ', toss everything after a second '. Fixes |
| 524 | // CTRL-] on 'option'. (would include the trailing '.'). |
| 525 | if (*s == '\'' && s > arg && *arg == '\'') |
| 526 | break; |
| 527 | // Also '{' and '}'. |
| 528 | if (*s == '}' && s > arg && *arg == '{') |
| 529 | break; |
| 530 | } |
| 531 | *d = NUL; |
| 532 | |
| 533 | if (*IObuff == '`') |
| 534 | { |
| 535 | if (d > IObuff + 2 && d[-1] == '`') |
| 536 | { |
| 537 | // remove the backticks from `command` |
| 538 | mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff)); |
| 539 | d[-2] = NUL; |
| 540 | } |
| 541 | else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',') |
| 542 | { |
| 543 | // remove the backticks and comma from `command`, |
| 544 | mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff)); |
| 545 | d[-3] = NUL; |
| 546 | } |
| 547 | else if (d > IObuff + 4 && d[-3] == '`' |
| 548 | && d[-2] == '\\' && d[-1] == '.') |
| 549 | { |
| 550 | // remove the backticks and dot from `command`\. |
| 551 | mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff)); |
| 552 | d[-4] = NUL; |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | *matches = (char_u **)""; |
| 559 | *num_matches = 0; |
| 560 | flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE | TAG_NO_TAGFUNC; |
| 561 | if (keep_lang) |
| 562 | flags |= TAG_KEEP_LANG; |
| 563 | if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK |
| 564 | && *num_matches > 0) |
| 565 | { |
| 566 | // Sort the matches found on the heuristic number that is after the |
| 567 | // tag name. |
| 568 | qsort((void *)*matches, (size_t)*num_matches, |
| 569 | sizeof(char_u *), help_compare); |
| 570 | // Delete more than TAG_MANY to reduce the size of the listing. |
| 571 | while (*num_matches > TAG_MANY) |
| 572 | vim_free((*matches)[--*num_matches]); |
| 573 | } |
| 574 | return OK; |
| 575 | } |
| 576 | |
| 577 | #ifdef FEAT_MULTI_LANG |
| 578 | /* |
| 579 | * Cleanup matches for help tags: |
| 580 | * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first |
| 581 | * tag matches it. Otherwise remove "@en" if "en" is the only language. |
| 582 | */ |
| 583 | void |
| 584 | cleanup_help_tags(int num_file, char_u **file) |
| 585 | { |
| 586 | int i, j; |
| 587 | int len; |
| 588 | char_u buf[4]; |
| 589 | char_u *p = buf; |
| 590 | |
| 591 | if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n')) |
| 592 | { |
| 593 | *p++ = '@'; |
| 594 | *p++ = p_hlg[0]; |
| 595 | *p++ = p_hlg[1]; |
| 596 | } |
| 597 | *p = NUL; |
| 598 | |
| 599 | for (i = 0; i < num_file; ++i) |
| 600 | { |
| 601 | len = (int)STRLEN(file[i]) - 3; |
| 602 | if (len <= 0) |
| 603 | continue; |
| 604 | if (STRCMP(file[i] + len, "@en") == 0) |
| 605 | { |
| 606 | // Sorting on priority means the same item in another language may |
| 607 | // be anywhere. Search all items for a match up to the "@en". |
| 608 | for (j = 0; j < num_file; ++j) |
| 609 | if (j != i && (int)STRLEN(file[j]) == len + 3 |
| 610 | && STRNCMP(file[i], file[j], len + 1) == 0) |
| 611 | break; |
| 612 | if (j == num_file) |
| 613 | // item only exists with @en, remove it |
| 614 | file[i][len] = NUL; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | if (*buf != NUL) |
| 619 | for (i = 0; i < num_file; ++i) |
| 620 | { |
| 621 | len = (int)STRLEN(file[i]) - 3; |
| 622 | if (len <= 0) |
| 623 | continue; |
| 624 | if (STRCMP(file[i] + len, buf) == 0) |
| 625 | { |
| 626 | // remove the default language |
| 627 | file[i][len] = NUL; |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | #endif |
| 632 | |
| 633 | /* |
| 634 | * Called when starting to edit a buffer for a help file. |
| 635 | */ |
| 636 | void |
| 637 | prepare_help_buffer(void) |
| 638 | { |
| 639 | char_u *p; |
| 640 | |
| 641 | curbuf->b_help = TRUE; |
| 642 | #ifdef FEAT_QUICKFIX |
| 643 | set_string_option_direct((char_u *)"buftype", -1, |
| 644 | (char_u *)"help", OPT_FREE|OPT_LOCAL, 0); |
| 645 | #endif |
| 646 | |
| 647 | // Always set these options after jumping to a help tag, because the |
| 648 | // user may have an autocommand that gets in the way. |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 649 | // When adding an option here, also update the help file helphelp.txt. |
| 650 | |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 651 | // Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and |
| 652 | // latin1 word characters (for translated help files). |
| 653 | // Only set it when needed, buf_init_chartab() is some work. |
| 654 | p = |
| 655 | #ifdef EBCDIC |
| 656 | (char_u *)"65-255,^*,^|,^\""; |
| 657 | #else |
| 658 | (char_u *)"!-~,^*,^|,^\",192-255"; |
| 659 | #endif |
| 660 | if (STRCMP(curbuf->b_p_isk, p) != 0) |
| 661 | { |
| 662 | set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0); |
| 663 | check_buf_options(curbuf); |
| 664 | (void)buf_init_chartab(curbuf, FALSE); |
| 665 | } |
| 666 | |
| 667 | #ifdef FEAT_FOLDING |
| 668 | // Don't use the global foldmethod. |
| 669 | set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual", |
| 670 | OPT_FREE|OPT_LOCAL, 0); |
| 671 | #endif |
| 672 | |
| 673 | curbuf->b_p_ts = 8; // 'tabstop' is 8 |
| 674 | curwin->w_p_list = FALSE; // no list mode |
| 675 | |
| 676 | curbuf->b_p_ma = FALSE; // not modifiable |
| 677 | curbuf->b_p_bin = FALSE; // reset 'bin' before reading file |
| 678 | curwin->w_p_nu = 0; // no line numbers |
| 679 | curwin->w_p_rnu = 0; // no relative line numbers |
| 680 | RESET_BINDING(curwin); // no scroll or cursor binding |
| 681 | #ifdef FEAT_ARABIC |
| 682 | curwin->w_p_arab = FALSE; // no arabic mode |
| 683 | #endif |
| 684 | #ifdef FEAT_RIGHTLEFT |
| 685 | curwin->w_p_rl = FALSE; // help window is left-to-right |
| 686 | #endif |
| 687 | #ifdef FEAT_FOLDING |
| 688 | curwin->w_p_fen = FALSE; // No folding in the help window |
| 689 | #endif |
| 690 | #ifdef FEAT_DIFF |
| 691 | curwin->w_p_diff = FALSE; // No 'diff' |
| 692 | #endif |
| 693 | #ifdef FEAT_SPELL |
| 694 | curwin->w_p_spell = FALSE; // No spell checking |
| 695 | #endif |
| 696 | |
| 697 | set_buflisted(FALSE); |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * After reading a help file: May cleanup a help buffer when syntax |
| 702 | * highlighting is not used. |
| 703 | */ |
| 704 | void |
| 705 | fix_help_buffer(void) |
| 706 | { |
| 707 | linenr_T lnum; |
| 708 | char_u *line; |
| 709 | int in_example = FALSE; |
| 710 | int len; |
| 711 | char_u *fname; |
| 712 | char_u *p; |
| 713 | char_u *rt; |
| 714 | int mustfree; |
| 715 | |
| 716 | // Set filetype to "help" if still needed. |
| 717 | if (STRCMP(curbuf->b_p_ft, "help") != 0) |
| 718 | { |
| 719 | ++curbuf_lock; |
| 720 | set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL); |
| 721 | --curbuf_lock; |
| 722 | } |
| 723 | |
| 724 | #ifdef FEAT_SYN_HL |
| 725 | if (!syntax_present(curwin)) |
| 726 | #endif |
| 727 | { |
| 728 | for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) |
| 729 | { |
| 730 | line = ml_get_buf(curbuf, lnum, FALSE); |
| 731 | len = (int)STRLEN(line); |
| 732 | if (in_example && len > 0 && !VIM_ISWHITE(line[0])) |
| 733 | { |
| 734 | // End of example: non-white or '<' in first column. |
| 735 | if (line[0] == '<') |
| 736 | { |
| 737 | // blank-out a '<' in the first column |
| 738 | line = ml_get_buf(curbuf, lnum, TRUE); |
| 739 | line[0] = ' '; |
| 740 | } |
| 741 | in_example = FALSE; |
| 742 | } |
| 743 | if (!in_example && len > 0) |
| 744 | { |
| 745 | if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' ')) |
| 746 | { |
| 747 | // blank-out a '>' in the last column (start of example) |
| 748 | line = ml_get_buf(curbuf, lnum, TRUE); |
| 749 | line[len - 1] = ' '; |
| 750 | in_example = TRUE; |
| 751 | } |
| 752 | else if (line[len - 1] == '~') |
| 753 | { |
| 754 | // blank-out a '~' at the end of line (header marker) |
| 755 | line = ml_get_buf(curbuf, lnum, TRUE); |
| 756 | line[len - 1] = ' '; |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | // In the "help.txt" and "help.abx" file, add the locally added help |
| 763 | // files. This uses the very first line in the help file. |
| 764 | fname = gettail(curbuf->b_fname); |
| 765 | if (fnamecmp(fname, "help.txt") == 0 |
| 766 | #ifdef FEAT_MULTI_LANG |
| 767 | || (fnamencmp(fname, "help.", 5) == 0 |
| 768 | && ASCII_ISALPHA(fname[5]) |
| 769 | && ASCII_ISALPHA(fname[6]) |
| 770 | && TOLOWER_ASC(fname[7]) == 'x' |
| 771 | && fname[8] == NUL) |
| 772 | #endif |
| 773 | ) |
| 774 | { |
| 775 | for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum) |
| 776 | { |
| 777 | line = ml_get_buf(curbuf, lnum, FALSE); |
| 778 | if (strstr((char *)line, "*local-additions*") == NULL) |
| 779 | continue; |
| 780 | |
| 781 | // Go through all directories in 'runtimepath', skipping |
| 782 | // $VIMRUNTIME. |
| 783 | p = p_rtp; |
| 784 | while (*p != NUL) |
| 785 | { |
| 786 | copy_option_part(&p, NameBuff, MAXPATHL, ","); |
| 787 | mustfree = FALSE; |
| 788 | rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree); |
| 789 | if (rt != NULL && |
| 790 | fullpathcmp(rt, NameBuff, FALSE, TRUE) != FPC_SAME) |
| 791 | { |
| 792 | int fcount; |
| 793 | char_u **fnames; |
| 794 | FILE *fd; |
| 795 | char_u *s; |
| 796 | int fi; |
| 797 | vimconv_T vc; |
| 798 | char_u *cp; |
| 799 | |
| 800 | // Find all "doc/ *.txt" files in this directory. |
| 801 | add_pathsep(NameBuff); |
| 802 | #ifdef FEAT_MULTI_LANG |
| 803 | STRCAT(NameBuff, "doc/*.??[tx]"); |
| 804 | #else |
| 805 | STRCAT(NameBuff, "doc/*.txt"); |
| 806 | #endif |
| 807 | if (gen_expand_wildcards(1, &NameBuff, &fcount, |
| 808 | &fnames, EW_FILE|EW_SILENT) == OK |
| 809 | && fcount > 0) |
| 810 | { |
| 811 | #ifdef FEAT_MULTI_LANG |
| 812 | int i1, i2; |
| 813 | char_u *f1, *f2; |
| 814 | char_u *t1, *t2; |
| 815 | char_u *e1, *e2; |
| 816 | |
| 817 | // If foo.abx is found use it instead of foo.txt in |
| 818 | // the same directory. |
| 819 | for (i1 = 0; i1 < fcount; ++i1) |
| 820 | { |
| 821 | for (i2 = 0; i2 < fcount; ++i2) |
| 822 | { |
| 823 | if (i1 == i2) |
| 824 | continue; |
| 825 | if (fnames[i1] == NULL || fnames[i2] == NULL) |
| 826 | continue; |
| 827 | f1 = fnames[i1]; |
| 828 | f2 = fnames[i2]; |
| 829 | t1 = gettail(f1); |
| 830 | t2 = gettail(f2); |
| 831 | e1 = vim_strrchr(t1, '.'); |
| 832 | e2 = vim_strrchr(t2, '.'); |
| 833 | if (e1 == NULL || e2 == NULL) |
| 834 | continue; |
| 835 | if (fnamecmp(e1, ".txt") != 0 |
| 836 | && fnamecmp(e1, fname + 4) != 0) |
| 837 | { |
| 838 | // Not .txt and not .abx, remove it. |
| 839 | VIM_CLEAR(fnames[i1]); |
| 840 | continue; |
| 841 | } |
| 842 | if (e1 - f1 != e2 - f2 |
| 843 | || fnamencmp(f1, f2, e1 - f1) != 0) |
| 844 | continue; |
| 845 | if (fnamecmp(e1, ".txt") == 0 |
| 846 | && fnamecmp(e2, fname + 4) == 0) |
| 847 | // use .abx instead of .txt |
| 848 | VIM_CLEAR(fnames[i1]); |
| 849 | } |
| 850 | } |
| 851 | #endif |
| 852 | for (fi = 0; fi < fcount; ++fi) |
| 853 | { |
| 854 | if (fnames[fi] == NULL) |
| 855 | continue; |
| 856 | fd = mch_fopen((char *)fnames[fi], "r"); |
| 857 | if (fd != NULL) |
| 858 | { |
| 859 | vim_fgets(IObuff, IOSIZE, fd); |
| 860 | if (IObuff[0] == '*' |
| 861 | && (s = vim_strchr(IObuff + 1, '*')) |
| 862 | != NULL) |
| 863 | { |
| 864 | int this_utf = MAYBE; |
| 865 | |
| 866 | // Change tag definition to a |
| 867 | // reference and remove <CR>/<NL>. |
| 868 | IObuff[0] = '|'; |
| 869 | *s = '|'; |
| 870 | while (*s != NUL) |
| 871 | { |
| 872 | if (*s == '\r' || *s == '\n') |
| 873 | *s = NUL; |
| 874 | // The text is utf-8 when a byte |
| 875 | // above 127 is found and no |
| 876 | // illegal byte sequence is found. |
| 877 | if (*s >= 0x80 && this_utf != FALSE) |
| 878 | { |
| 879 | int l; |
| 880 | |
| 881 | this_utf = TRUE; |
| 882 | l = utf_ptr2len(s); |
| 883 | if (l == 1) |
| 884 | this_utf = FALSE; |
| 885 | s += l - 1; |
| 886 | } |
| 887 | ++s; |
| 888 | } |
| 889 | |
| 890 | // The help file is latin1 or utf-8; |
| 891 | // conversion to the current |
| 892 | // 'encoding' may be required. |
| 893 | vc.vc_type = CONV_NONE; |
| 894 | convert_setup(&vc, (char_u *)( |
| 895 | this_utf == TRUE ? "utf-8" |
| 896 | : "latin1"), p_enc); |
| 897 | if (vc.vc_type == CONV_NONE) |
| 898 | // No conversion needed. |
| 899 | cp = IObuff; |
| 900 | else |
| 901 | { |
| 902 | // Do the conversion. If it fails |
| 903 | // use the unconverted text. |
| 904 | cp = string_convert(&vc, IObuff, |
| 905 | NULL); |
| 906 | if (cp == NULL) |
| 907 | cp = IObuff; |
| 908 | } |
| 909 | convert_setup(&vc, NULL, NULL); |
| 910 | |
| 911 | ml_append(lnum, cp, (colnr_T)0, FALSE); |
| 912 | if (cp != IObuff) |
| 913 | vim_free(cp); |
| 914 | ++lnum; |
| 915 | } |
| 916 | fclose(fd); |
| 917 | } |
| 918 | } |
| 919 | FreeWild(fcount, fnames); |
| 920 | } |
| 921 | } |
| 922 | if (mustfree) |
| 923 | vim_free(rt); |
| 924 | } |
| 925 | break; |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * ":exusage" |
| 932 | */ |
| 933 | void |
| 934 | ex_exusage(exarg_T *eap UNUSED) |
| 935 | { |
| 936 | do_cmdline_cmd((char_u *)"help ex-cmd-index"); |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * ":viusage" |
| 941 | */ |
| 942 | void |
| 943 | ex_viusage(exarg_T *eap UNUSED) |
| 944 | { |
| 945 | do_cmdline_cmd((char_u *)"help normal-index"); |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | * Generate tags in one help directory. |
| 950 | */ |
| 951 | static void |
| 952 | helptags_one( |
| 953 | char_u *dir, // doc directory |
| 954 | char_u *ext, // suffix, ".txt", ".itx", ".frx", etc. |
| 955 | char_u *tagfname, // "tags" for English, "tags-fr" for French. |
| 956 | int add_help_tags, // add "help-tags" tag |
| 957 | int ignore_writeerr) // ignore write error |
| 958 | { |
| 959 | FILE *fd_tags; |
| 960 | FILE *fd; |
| 961 | garray_T ga; |
| 962 | int filecount; |
| 963 | char_u **files; |
| 964 | char_u *p1, *p2; |
| 965 | int fi; |
| 966 | char_u *s; |
| 967 | int i; |
| 968 | char_u *fname; |
| 969 | int dirlen; |
| 970 | int utf8 = MAYBE; |
| 971 | int this_utf8; |
| 972 | int firstline; |
| 973 | int mix = FALSE; // detected mixed encodings |
| 974 | |
| 975 | // Find all *.txt files. |
| 976 | dirlen = (int)STRLEN(dir); |
| 977 | STRCPY(NameBuff, dir); |
| 978 | STRCAT(NameBuff, "/**/*"); |
| 979 | STRCAT(NameBuff, ext); |
| 980 | if (gen_expand_wildcards(1, &NameBuff, &filecount, &files, |
| 981 | EW_FILE|EW_SILENT) == FAIL |
| 982 | || filecount == 0) |
| 983 | { |
| 984 | if (!got_int) |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 985 | semsg(_(e_no_match_str_1), NameBuff); |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 986 | return; |
| 987 | } |
| 988 | |
| 989 | // Open the tags file for writing. |
| 990 | // Do this before scanning through all the files. |
| 991 | STRCPY(NameBuff, dir); |
| 992 | add_pathsep(NameBuff); |
| 993 | STRCAT(NameBuff, tagfname); |
| 994 | fd_tags = mch_fopen((char *)NameBuff, "w"); |
| 995 | if (fd_tags == NULL) |
| 996 | { |
| 997 | if (!ignore_writeerr) |
Bram Moolenaar | 1a99222 | 2021-12-31 17:25:48 +0000 | [diff] [blame] | 998 | semsg(_(e_cannot_open_str_for_writing_1), NameBuff); |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 999 | FreeWild(filecount, files); |
| 1000 | return; |
| 1001 | } |
| 1002 | |
| 1003 | // If using the "++t" argument or generating tags for "$VIMRUNTIME/doc" |
| 1004 | // add the "help-tags" tag. |
| 1005 | ga_init2(&ga, (int)sizeof(char_u *), 100); |
| 1006 | if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc", |
| 1007 | dir, FALSE, TRUE) == FPC_SAME) |
| 1008 | { |
| 1009 | if (ga_grow(&ga, 1) == FAIL) |
| 1010 | got_int = TRUE; |
| 1011 | else |
| 1012 | { |
| 1013 | s = alloc(18 + (unsigned)STRLEN(tagfname)); |
| 1014 | if (s == NULL) |
| 1015 | got_int = TRUE; |
| 1016 | else |
| 1017 | { |
| 1018 | sprintf((char *)s, "help-tags\t%s\t1\n", tagfname); |
| 1019 | ((char_u **)ga.ga_data)[ga.ga_len] = s; |
| 1020 | ++ga.ga_len; |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | // Go over all the files and extract the tags. |
| 1026 | for (fi = 0; fi < filecount && !got_int; ++fi) |
| 1027 | { |
| 1028 | fd = mch_fopen((char *)files[fi], "r"); |
| 1029 | if (fd == NULL) |
| 1030 | { |
Bram Moolenaar | eb822a2 | 2021-12-31 15:09:27 +0000 | [diff] [blame] | 1031 | semsg(_(e_unable_to_open_str_for_reading), files[fi]); |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 1032 | continue; |
| 1033 | } |
| 1034 | fname = files[fi] + dirlen + 1; |
| 1035 | |
| 1036 | firstline = TRUE; |
| 1037 | while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) |
| 1038 | { |
| 1039 | if (firstline) |
| 1040 | { |
| 1041 | // Detect utf-8 file by a non-ASCII char in the first line. |
| 1042 | this_utf8 = MAYBE; |
| 1043 | for (s = IObuff; *s != NUL; ++s) |
| 1044 | if (*s >= 0x80) |
| 1045 | { |
| 1046 | int l; |
| 1047 | |
| 1048 | this_utf8 = TRUE; |
| 1049 | l = utf_ptr2len(s); |
| 1050 | if (l == 1) |
| 1051 | { |
| 1052 | // Illegal UTF-8 byte sequence. |
| 1053 | this_utf8 = FALSE; |
| 1054 | break; |
| 1055 | } |
| 1056 | s += l - 1; |
| 1057 | } |
| 1058 | if (this_utf8 == MAYBE) // only ASCII characters found |
| 1059 | this_utf8 = FALSE; |
| 1060 | if (utf8 == MAYBE) // first file |
| 1061 | utf8 = this_utf8; |
| 1062 | else if (utf8 != this_utf8) |
| 1063 | { |
| 1064 | semsg(_("E670: Mix of help file encodings within a language: %s"), files[fi]); |
| 1065 | mix = !got_int; |
| 1066 | got_int = TRUE; |
| 1067 | } |
| 1068 | firstline = FALSE; |
| 1069 | } |
| 1070 | p1 = vim_strchr(IObuff, '*'); // find first '*' |
| 1071 | while (p1 != NULL) |
| 1072 | { |
| 1073 | // Use vim_strbyte() instead of vim_strchr() so that when |
| 1074 | // 'encoding' is dbcs it still works, don't find '*' in the |
| 1075 | // second byte. |
| 1076 | p2 = vim_strbyte(p1 + 1, '*'); // find second '*' |
| 1077 | if (p2 != NULL && p2 > p1 + 1) // skip "*" and "**" |
| 1078 | { |
| 1079 | for (s = p1 + 1; s < p2; ++s) |
| 1080 | if (*s == ' ' || *s == '\t' || *s == '|') |
| 1081 | break; |
| 1082 | |
| 1083 | // Only accept a *tag* when it consists of valid |
| 1084 | // characters, there is white space before it and is |
| 1085 | // followed by a white character or end-of-line. |
| 1086 | if (s == p2 |
| 1087 | && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t') |
| 1088 | && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL |
| 1089 | || s[1] == '\0')) |
| 1090 | { |
| 1091 | *p2 = '\0'; |
| 1092 | ++p1; |
| 1093 | if (ga_grow(&ga, 1) == FAIL) |
| 1094 | { |
| 1095 | got_int = TRUE; |
| 1096 | break; |
| 1097 | } |
| 1098 | s = alloc(p2 - p1 + STRLEN(fname) + 2); |
| 1099 | if (s == NULL) |
| 1100 | { |
| 1101 | got_int = TRUE; |
| 1102 | break; |
| 1103 | } |
| 1104 | ((char_u **)ga.ga_data)[ga.ga_len] = s; |
| 1105 | ++ga.ga_len; |
| 1106 | sprintf((char *)s, "%s\t%s", p1, fname); |
| 1107 | |
| 1108 | // find next '*' |
| 1109 | p2 = vim_strchr(p2 + 1, '*'); |
| 1110 | } |
| 1111 | } |
| 1112 | p1 = p2; |
| 1113 | } |
| 1114 | line_breakcheck(); |
| 1115 | } |
| 1116 | |
| 1117 | fclose(fd); |
| 1118 | } |
| 1119 | |
| 1120 | FreeWild(filecount, files); |
| 1121 | |
| 1122 | if (!got_int) |
| 1123 | { |
| 1124 | // Sort the tags. |
| 1125 | if (ga.ga_data != NULL) |
| 1126 | sort_strings((char_u **)ga.ga_data, ga.ga_len); |
| 1127 | |
| 1128 | // Check for duplicates. |
| 1129 | for (i = 1; i < ga.ga_len; ++i) |
| 1130 | { |
| 1131 | p1 = ((char_u **)ga.ga_data)[i - 1]; |
| 1132 | p2 = ((char_u **)ga.ga_data)[i]; |
| 1133 | while (*p1 == *p2) |
| 1134 | { |
| 1135 | if (*p2 == '\t') |
| 1136 | { |
| 1137 | *p2 = NUL; |
| 1138 | vim_snprintf((char *)NameBuff, MAXPATHL, |
Bram Moolenaar | eb822a2 | 2021-12-31 15:09:27 +0000 | [diff] [blame] | 1139 | _(e_duplicate_tag_str_in_file_str_str), |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 1140 | ((char_u **)ga.ga_data)[i], dir, p2 + 1); |
| 1141 | emsg((char *)NameBuff); |
| 1142 | *p2 = '\t'; |
| 1143 | break; |
| 1144 | } |
| 1145 | ++p1; |
| 1146 | ++p2; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | if (utf8 == TRUE) |
| 1151 | fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n"); |
| 1152 | |
| 1153 | // Write the tags into the file. |
| 1154 | for (i = 0; i < ga.ga_len; ++i) |
| 1155 | { |
| 1156 | s = ((char_u **)ga.ga_data)[i]; |
| 1157 | if (STRNCMP(s, "help-tags\t", 10) == 0) |
| 1158 | // help-tags entry was added in formatted form |
| 1159 | fputs((char *)s, fd_tags); |
| 1160 | else |
| 1161 | { |
| 1162 | fprintf(fd_tags, "%s\t/*", s); |
| 1163 | for (p1 = s; *p1 != '\t'; ++p1) |
| 1164 | { |
| 1165 | // insert backslash before '\\' and '/' |
| 1166 | if (*p1 == '\\' || *p1 == '/') |
| 1167 | putc('\\', fd_tags); |
| 1168 | putc(*p1, fd_tags); |
| 1169 | } |
| 1170 | fprintf(fd_tags, "*\n"); |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | if (mix) |
| 1175 | got_int = FALSE; // continue with other languages |
| 1176 | |
| 1177 | for (i = 0; i < ga.ga_len; ++i) |
| 1178 | vim_free(((char_u **)ga.ga_data)[i]); |
| 1179 | ga_clear(&ga); |
| 1180 | fclose(fd_tags); // there is no check for an error... |
| 1181 | } |
| 1182 | |
| 1183 | /* |
| 1184 | * Generate tags in one help directory, taking care of translations. |
| 1185 | */ |
| 1186 | static void |
| 1187 | do_helptags(char_u *dirname, int add_help_tags, int ignore_writeerr) |
| 1188 | { |
| 1189 | #ifdef FEAT_MULTI_LANG |
| 1190 | int len; |
| 1191 | int i, j; |
| 1192 | garray_T ga; |
| 1193 | char_u lang[2]; |
| 1194 | char_u ext[5]; |
| 1195 | char_u fname[8]; |
| 1196 | int filecount; |
| 1197 | char_u **files; |
| 1198 | |
| 1199 | // Get a list of all files in the help directory and in subdirectories. |
| 1200 | STRCPY(NameBuff, dirname); |
| 1201 | add_pathsep(NameBuff); |
| 1202 | STRCAT(NameBuff, "**"); |
| 1203 | if (gen_expand_wildcards(1, &NameBuff, &filecount, &files, |
| 1204 | EW_FILE|EW_SILENT) == FAIL |
| 1205 | || filecount == 0) |
| 1206 | { |
| 1207 | semsg(_("E151: No match: %s"), NameBuff); |
| 1208 | return; |
| 1209 | } |
| 1210 | |
| 1211 | // Go over all files in the directory to find out what languages are |
| 1212 | // present. |
| 1213 | ga_init2(&ga, 1, 10); |
| 1214 | for (i = 0; i < filecount; ++i) |
| 1215 | { |
| 1216 | len = (int)STRLEN(files[i]); |
| 1217 | if (len > 4) |
| 1218 | { |
| 1219 | if (STRICMP(files[i] + len - 4, ".txt") == 0) |
| 1220 | { |
| 1221 | // ".txt" -> language "en" |
| 1222 | lang[0] = 'e'; |
| 1223 | lang[1] = 'n'; |
| 1224 | } |
| 1225 | else if (files[i][len - 4] == '.' |
| 1226 | && ASCII_ISALPHA(files[i][len - 3]) |
| 1227 | && ASCII_ISALPHA(files[i][len - 2]) |
| 1228 | && TOLOWER_ASC(files[i][len - 1]) == 'x') |
| 1229 | { |
| 1230 | // ".abx" -> language "ab" |
| 1231 | lang[0] = TOLOWER_ASC(files[i][len - 3]); |
| 1232 | lang[1] = TOLOWER_ASC(files[i][len - 2]); |
| 1233 | } |
| 1234 | else |
| 1235 | continue; |
| 1236 | |
| 1237 | // Did we find this language already? |
| 1238 | for (j = 0; j < ga.ga_len; j += 2) |
| 1239 | if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0) |
| 1240 | break; |
| 1241 | if (j == ga.ga_len) |
| 1242 | { |
| 1243 | // New language, add it. |
| 1244 | if (ga_grow(&ga, 2) == FAIL) |
| 1245 | break; |
| 1246 | ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0]; |
| 1247 | ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1]; |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | // Loop over the found languages to generate a tags file for each one. |
| 1253 | for (j = 0; j < ga.ga_len; j += 2) |
| 1254 | { |
| 1255 | STRCPY(fname, "tags-xx"); |
| 1256 | fname[5] = ((char_u *)ga.ga_data)[j]; |
| 1257 | fname[6] = ((char_u *)ga.ga_data)[j + 1]; |
| 1258 | if (fname[5] == 'e' && fname[6] == 'n') |
| 1259 | { |
| 1260 | // English is an exception: use ".txt" and "tags". |
| 1261 | fname[4] = NUL; |
| 1262 | STRCPY(ext, ".txt"); |
| 1263 | } |
| 1264 | else |
| 1265 | { |
| 1266 | // Language "ab" uses ".abx" and "tags-ab". |
| 1267 | STRCPY(ext, ".xxx"); |
| 1268 | ext[1] = fname[5]; |
| 1269 | ext[2] = fname[6]; |
| 1270 | } |
| 1271 | helptags_one(dirname, ext, fname, add_help_tags, ignore_writeerr); |
| 1272 | } |
| 1273 | |
| 1274 | ga_clear(&ga); |
| 1275 | FreeWild(filecount, files); |
| 1276 | |
| 1277 | #else |
| 1278 | // No language support, just use "*.txt" and "tags". |
| 1279 | helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags, |
| 1280 | ignore_writeerr); |
| 1281 | #endif |
| 1282 | } |
| 1283 | |
| 1284 | static void |
| 1285 | helptags_cb(char_u *fname, void *cookie) |
| 1286 | { |
| 1287 | do_helptags(fname, *(int *)cookie, TRUE); |
| 1288 | } |
| 1289 | |
| 1290 | /* |
| 1291 | * ":helptags" |
| 1292 | */ |
| 1293 | void |
| 1294 | ex_helptags(exarg_T *eap) |
| 1295 | { |
| 1296 | expand_T xpc; |
| 1297 | char_u *dirname; |
| 1298 | int add_help_tags = FALSE; |
| 1299 | |
| 1300 | // Check for ":helptags ++t {dir}". |
| 1301 | if (STRNCMP(eap->arg, "++t", 3) == 0 && VIM_ISWHITE(eap->arg[3])) |
| 1302 | { |
| 1303 | add_help_tags = TRUE; |
| 1304 | eap->arg = skipwhite(eap->arg + 3); |
| 1305 | } |
| 1306 | |
| 1307 | if (STRCMP(eap->arg, "ALL") == 0) |
| 1308 | { |
| 1309 | do_in_path(p_rtp, (char_u *)"doc", DIP_ALL + DIP_DIR, |
| 1310 | helptags_cb, &add_help_tags); |
| 1311 | } |
| 1312 | else |
| 1313 | { |
| 1314 | ExpandInit(&xpc); |
| 1315 | xpc.xp_context = EXPAND_DIRECTORIES; |
| 1316 | dirname = ExpandOne(&xpc, eap->arg, NULL, |
| 1317 | WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE); |
| 1318 | if (dirname == NULL || !mch_isdir(dirname)) |
Bram Moolenaar | eb822a2 | 2021-12-31 15:09:27 +0000 | [diff] [blame] | 1319 | semsg(_(e_not_a_directory_str), eap->arg); |
Bram Moolenaar | f868ba8 | 2020-07-21 21:07:20 +0200 | [diff] [blame] | 1320 | else |
| 1321 | do_helptags(dirname, add_help_tags, FALSE); |
| 1322 | vim_free(dirname); |
| 1323 | } |
| 1324 | } |