/// Gets the hostname of the current machine. /// /// @param hostname Buffer to store the hostname. /// @param size Size of `hostname`. void os_get_hostname(char *hostname, size_t size) { #ifdef HAVE_SYS_UTSNAME_H struct utsname vutsname; if (uname(&vutsname) < 0) { *hostname = '\0'; } else { xstrlcpy(hostname, vutsname.nodename, size); } #elif defined(WIN32) wchar_t host_utf16[MAX_COMPUTERNAME_LENGTH + 1]; DWORD host_wsize = sizeof(host_utf16) / sizeof(host_utf16[0]); if (GetComputerNameW(host_utf16, &host_wsize) == 0) { *hostname = '\0'; DWORD err = GetLastError(); EMSG2("GetComputerNameW failed: %d", err); return; } host_utf16[host_wsize] = '\0'; char *host_utf8; int conversion_result = utf16_to_utf8(host_utf16, &host_utf8); if (conversion_result != 0) { EMSG2("utf16_to_utf8 failed: %d", conversion_result); return; } xstrlcpy(hostname, host_utf8, size); xfree(host_utf8); #else EMSG("os_get_hostname failed: missing uname()"); *hostname = '\0'; #endif }
/* * Load library and get all pointers. * Parameter 'libname' provides name of DLL. * Return OK or FAIL. */ static int ruby_runtime_link_init(char *libname, int verbose) { int i; if (hinstRuby) return OK; hinstRuby = LoadLibrary(libname); if (!hinstRuby) { if (verbose) EMSG2(_(e_loadlib), libname); return FAIL; } for (i = 0; ruby_funcname_table[i].ptr; ++i) { if (!(*ruby_funcname_table[i].ptr = GetProcAddress(hinstRuby, ruby_funcname_table[i].name))) { FreeLibrary(hinstRuby); hinstRuby = 0; if (verbose) EMSG2(_(e_loadfunc), ruby_funcname_table[i].name); return FAIL; } } return OK; }
/* * Load library and get all pointers. * Parameter 'libname' provides name of DLL. * Return OK or FAIL. */ static int ruby_runtime_link_init(char *libname, int verbose) { int i; if (hinstRuby) return OK; hinstRuby = load_dll(libname); if (!hinstRuby) { if (verbose) EMSG2(_(e_loadlib), libname); return FAIL; } for (i = 0; ruby_funcname_table[i].ptr; ++i) { if (!(*ruby_funcname_table[i].ptr = symbol_from_dll(hinstRuby, ruby_funcname_table[i].name))) { close_dll(hinstRuby); hinstRuby = NULL; if (verbose) EMSG2(_(e_loadfunc), ruby_funcname_table[i].name); return FAIL; } } return OK; }
static int lua_link_init(char *libname, int verbose) { const luaV_Reg *reg; if (hinstLua) return OK; hinstLua = load_dll(libname); if (!hinstLua) { if (verbose) EMSG2(_(e_loadlib), libname); return FAIL; } for (reg = luaV_dll; reg->func; reg++) { if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL) { close_dll(hinstLua); hinstLua = 0; if (verbose) EMSG2(_(e_loadfunc), reg->name); return FAIL; } } return OK; }
/* * Allocate a variable for a List and fill it from "*arg". * Return OK or FAIL. */ int get_list_tv(char_u **arg, typval_T *rettv, int evaluate) { list_T *l = NULL; typval_T tv; listitem_T *item; if (evaluate) { l = list_alloc(); if (l == NULL) return FAIL; } *arg = skipwhite(*arg + 1); while (**arg != ']' && **arg != NUL) { if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */ goto failret; if (evaluate) { item = listitem_alloc(); if (item != NULL) { item->li_tv = tv; item->li_tv.v_lock = 0; list_append(l, item); } else clear_tv(&tv); } if (**arg == ']') break; if (**arg != ',') { EMSG2(_("E696: Missing comma in List: %s"), *arg); goto failret; } *arg = skipwhite(*arg + 1); } if (**arg != ']') { EMSG2(_("E697: Missing end of List ']': %s"), *arg); failret: if (evaluate) list_free(l); return FAIL; } *arg = skipwhite(*arg + 1); if (evaluate) rettv_list_set(rettv, l); return OK; }
/* * Get a font structure for highlighting. */ GuiFont gui_mch_get_font(char_u *name, int giveErrorIfMissing) { if(vimjs_check_font((char*)name)) return (char*)vim_strsave(name); if (giveErrorIfMissing) EMSG2(_(e_font), name); return NOFONT; }
/// Add item with key "key" to hashtable "ht". /// /// @param ht /// @param key /// /// @returns FAIL when out of memory or the key is already present. int hash_add(hashtab_T *ht, char_u *key) { hash_T hash = hash_hash(key); hashitem_T *hi = hash_lookup(ht, key, hash); if (!HASHITEM_EMPTY(hi)) { EMSG2(_(e_intern2), "hash_add()"); return FAIL; } return hash_add_item(ht, hi, key, hash); }
/* * Lookup a property type by name. First in "buf" and when not found in the * global types. * When not found gives an error message and returns NULL. */ static proptype_T * lookup_prop_type(char_u *name, buf_T *buf) { proptype_T *type = find_prop(name, buf); if (type == NULL) type = find_prop(name, NULL); if (type == NULL) EMSG2(_(e_type_not_exist), name); return type; }
/* * Load library and get all pointers. * Parameter 'libname' provides name of DLL. * Return OK or FAIL. */ static int python_runtime_link_init(char *libname, int verbose) { int i; #if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3) /* Can't have Python and Python3 loaded at the same time. * It cause a crash, because RTLD_GLOBAL is needed for * standard C extension libraries of one or both python versions. */ if (python3_loaded()) { if (verbose) EMSG(_("E836: This Vim cannot execute :python after using :py3")); return FAIL; } #endif if (hinstPython) return OK; hinstPython = load_dll(libname); if (!hinstPython) { if (verbose) EMSG2(_(e_loadlib), libname); return FAIL; } for (i = 0; python_funcname_table[i].ptr; ++i) { if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython, python_funcname_table[i].name)) == NULL) { close_dll(hinstPython); hinstPython = 0; if (verbose) EMSG2(_(e_loadfunc), python_funcname_table[i].name); return FAIL; } } return OK; }
/* * Remove item "item" from Dictionary "dict" and free it. */ void dictitem_remove(dict_T *dict, dictitem_T *item) { hashitem_T *hi; hi = hash_find(&dict->dv_hashtab, item->di_key); if (HASHITEM_EMPTY(hi)) EMSG2(_(e_intern2), "dictitem_remove()"); else hash_remove(&dict->dv_hashtab, hi); dictitem_free(item); }
static void show_one_mark( int c, char_u *arg, pos_T *p, char_u *name, int current /* in current file */ ) { static int did_title = FALSE; int mustfree = FALSE; if (c == -1) { /* finish up */ if (did_title) did_title = FALSE; else { if (arg == NULL) MSG(_("No marks set")); else EMSG2(_("E283: No marks matching \"%s\""), arg); } } /* don't output anything if 'q' typed at --more-- prompt */ else if (!got_int && (arg == NULL || vim_strchr(arg, c) != NULL) && p->lnum != 0) { if (!did_title) { /* Highlight title */ MSG_PUTS_TITLE(_("\nmark line col file/text")); did_title = TRUE; } msg_putchar('\n'); if (!got_int) { sprintf((char *)IObuff, " %c %6ld %4d ", c, p->lnum, p->col); msg_outtrans(IObuff); if (name == NULL && current) { name = mark_line(p, 15); mustfree = TRUE; } if (name != NULL) { msg_outtrans_attr(name, current ? HL_ATTR(HLF_D) : 0); if (mustfree) { xfree(name); } } } ui_flush(); /* show one line at a time */ } }
/* * Go over all entries in "d2" and add them to "d1". * When "action" is "error" then a duplicate key is an error. * When "action" is "force" then a duplicate key is overwritten. * Otherwise duplicate keys are ignored ("action" is "keep"). */ void dict_extend(dict_T *d1, dict_T *d2, char_u *action) { dictitem_T *di1; hashitem_T *hi2; int todo; char_u *arg_errmsg = (char_u *)N_("extend() argument"); todo = (int)d2->dv_hashtab.ht_used; for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2) { if (!HASHITEM_EMPTY(hi2)) { --todo; di1 = dict_find(d1, hi2->hi_key, -1); if (d1->dv_scope != 0) { /* Disallow replacing a builtin function in l: and g:. * Check the key to be valid when adding to any scope. */ if (d1->dv_scope == VAR_DEF_SCOPE && HI2DI(hi2)->di_tv.v_type == VAR_FUNC && var_check_func_name(hi2->hi_key, di1 == NULL)) break; if (!valid_varname(hi2->hi_key)) break; } if (di1 == NULL) { di1 = dictitem_copy(HI2DI(hi2)); if (di1 != NULL && dict_add(d1, di1) == FAIL) dictitem_free(di1); } else if (*action == 'e') { EMSG2(_("E737: Key already exists: %s"), hi2->hi_key); break; } else if (*action == 'f' && HI2DI(hi2) != di1) { if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE) || var_check_ro(di1->di_flags, arg_errmsg, TRUE)) break; clear_tv(&di1->di_tv); copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv); } } } }
/* * Show the mapping associated with a menu item or hierarchy in a sub-menu. */ static int show_menus(char_u *path_name, int modes) { char_u *p; char_u *name; vimmenu_T *menu; vimmenu_T *parent = NULL; menu = root_menu; name = path_name = vim_strsave(path_name); /* First, find the (sub)menu with the given name */ while (*name) { p = menu_name_skip(name); while (menu != NULL) { if (menu_name_equal(name, menu)) { /* Found menu */ if (*p != NUL && menu->children == NULL) { EMSG(_(e_notsubmenu)); free(path_name); return FAIL; } else if ((menu->modes & modes) == 0x0) { EMSG(_(e_othermode)); free(path_name); return FAIL; } break; } menu = menu->next; } if (menu == NULL) { EMSG2(_(e_nomenu), name); free(path_name); return FAIL; } name = p; parent = menu; menu = menu->children; } free(path_name); /* Now we have found the matching menu, and we list the mappings */ /* Highlight title */ MSG_PUTS_TITLE(_("\n--- Menus ---")); show_menus_recursive(parent, modes, 0); return OK; }
/** * Get the font with the given name * */ GuiFont gui_mch_get_font(char_u *name, int giveErrorIfMissing) { QString family = (char*)name; QFont font; font.setStyleHint(QFont::TypeWriter); if ( name == NULL ) { // Fallback font font.setFamily("Monospace"); font.setPointSize(10); font.setKerning(false); return new QFont(font); } bool ok; int size = family.section(' ', -1).trimmed().toInt(&ok); if ( ok ) { QString realname = family.section(' ', 0, -2).trimmed(); font.setFamily(realname); font.setPointSize(size); } else if ( !font.fromString((char*)name) ) { font.setRawName((char*)name); } // I expected QFont::exactMatch to do this - but I was wrong // FIXME: this needs further testing // This makes it impossible to set the font to "Monospace". if ( QFontInfo(font).family() != font.family() && giveErrorIfMissing ) { QString errmsg; QTextStream(&errmsg) << "Font called " << font.family() << " is actually called " << QFontInfo(font).family(); EMSG2(e_font, errmsg.toUtf8().data()); return NOFONT; } font.setFixedPitch(true); font.setBold(false); font.setItalic(false); font.setKerning(false); return new QFont(font); }
static int lcall(char *sub, char **subargs) { int retcode, retry = 0; pid_t iwait, child; for (;;) { switch (child = fork()) { default: while ((iwait = wait(&retcode)) != child && iwait != (pid_t)-1) ; if (iwait == (pid_t)-1) { PERR(WAITFAIL); exit(122); /* NOTREACHED */ } if (WIFSIGNALED(retcode)) { EMSG2(CHILDSIG, WTERMSIG(retcode)); exit(125); /* NOTREACHED */ } if ((WEXITSTATUS(retcode) & 0377) == 0377) { EMSG(CHILDFAIL); exit(124); /* NOTREACHED */ } return (WEXITSTATUS(retcode)); case 0: (void) execvp(sub, subargs); PERR(EXECFAIL); if (errno == EACCES) exit(126); exit(127); /* NOTREACHED */ case -1: if (errno != EAGAIN && retry++ < FORK_RETRY) { PERR(FORKFAIL); exit(123); } (void) sleep(1); } } }
/* * Set the (sub)menu with the given name to enabled or disabled. * Called recursively. */ static int menu_nable_recurse(vimmenu_T *menu, char_u *name, int modes, int enable) { char_u *p; if (menu == NULL) return OK; /* Got to bottom of hierarchy */ /* Get name of this element in the menu hierarchy */ p = menu_name_skip(name); /* Find the menu */ while (menu != NULL) { if (*name == NUL || *name == '*' || menu_name_equal(name, menu)) { if (*p != NUL) { if (menu->children == NULL) { EMSG(_(e_notsubmenu)); return FAIL; } if (menu_nable_recurse(menu->children, p, modes, enable) == FAIL) return FAIL; } else if (enable) menu->enabled |= modes; else menu->enabled &= ~modes; /* * When name is empty, we are doing all menu items for the given * modes, so keep looping, otherwise we are just doing the named * menu item (which has been found) so break here. */ if (*name != NUL && *name != '*') break; } menu = menu->next; } if (*name != NUL && *name != '*' && menu == NULL) { EMSG2(_(e_nomenu), name); return FAIL; } return OK; }
/* * Initialization routine for vim_findfile(). * * Returns the newly allocated search context or NULL if an error occurred. * * Don't forget to clean up by calling vim_findfile_cleanup() if you are done * with the search context. * * Find the file 'filename' in the directory 'path'. * The parameter 'path' may contain wildcards. If so only search 'level' * directories deep. The parameter 'level' is the absolute maximum and is * not related to restricts given to the '**' wildcard. If 'level' is 100 * and you use '**200' vim_findfile() will stop after 100 levels. * * 'filename' cannot contain wildcards! It is used as-is, no backslashes to * escape special characters. * * If 'stopdirs' is not NULL and nothing is found downward, the search is * restarted on the next higher directory level. This is repeated until the * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the * format ";*<dirname>*\(;<dirname>\)*;\=$". * * If the 'path' is relative, the starting dir for the search is either VIM's * current dir or if the path starts with "./" the current files dir. * If the 'path' is absolute, the starting dir is that part of the path before * the first wildcard. * * Upward search is only done on the starting dir. * * If 'free_visited' is TRUE the list of already visited files/directories is * cleared. Set this to FALSE if you just want to search from another * directory, but want to be sure that no directory from a previous search is * searched again. This is useful if you search for a file at different places. * The list of visited files/dirs can also be cleared with the function * vim_findfile_free_visited(). * * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both. * * A search context returned by a previous call to vim_findfile_init() can be * passed in the parameter "search_ctx_arg". This context is reused and * reinitialized with the new parameters. The list of already visited * directories from this context is only deleted if the parameter * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed * if the reinitialization fails. * * If you don't have a search context from a previous call "search_ctx_arg" * must be NULL. * * This function silently ignores a few errors, vim_findfile() will have * limited functionality then. */ void * vim_findfile_init ( char_u *path, char_u *filename, char_u *stopdirs, int level, int free_visited, int find_what, void *search_ctx_arg, int tagfile, /* expanding names of tags files */ char_u *rel_fname /* file name to use for "." */ ) { char_u *wc_part; ff_stack_T *sptr; ff_search_ctx_T *search_ctx; /* If a search context is given by the caller, reuse it, else allocate a * new one. */ if (search_ctx_arg != NULL) search_ctx = search_ctx_arg; else { search_ctx = xcalloc(1, sizeof(ff_search_ctx_T)); } search_ctx->ffsc_find_what = find_what; search_ctx->ffsc_tagfile = tagfile; /* clear the search context, but NOT the visited lists */ ff_clear(search_ctx); /* clear visited list if wanted */ if (free_visited == TRUE) vim_findfile_free_visited(search_ctx); else { /* Reuse old visited lists. Get the visited list for the given * filename. If no list for the current filename exists, creates a new * one. */ search_ctx->ffsc_visited_list = ff_get_visited_list(filename, &search_ctx->ffsc_visited_lists_list); if (search_ctx->ffsc_visited_list == NULL) goto error_return; search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename, &search_ctx->ffsc_dir_visited_lists_list); if (search_ctx->ffsc_dir_visited_list == NULL) goto error_return; } if (ff_expand_buffer == NULL) { ff_expand_buffer = xmalloc(MAXPATHL); } /* Store information on starting dir now if path is relative. * If path is absolute, we do that later. */ if (path[0] == '.' && (vim_ispathsep(path[1]) || path[1] == NUL) && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL) && rel_fname != NULL) { int len = (int)(path_tail(rel_fname) - rel_fname); if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL) { /* Make the start dir an absolute path name. */ STRLCPY(ff_expand_buffer, rel_fname, len + 1); search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE); } else search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len); if (*++path != NUL) ++path; } else if (*path == NUL || !vim_isAbsName(path)) { #ifdef BACKSLASH_IN_FILENAME /* "c:dir" needs "c:" to be expanded, otherwise use current dir */ if (*path != NUL && path[1] == ':') { char_u drive[3]; drive[0] = path[0]; drive[1] = ':'; drive[2] = NUL; if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL) goto error_return; path += 2; } else #endif if (os_dirname(ff_expand_buffer, MAXPATHL) == FAIL) goto error_return; search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer); #ifdef BACKSLASH_IN_FILENAME /* A path that starts with "/dir" is relative to the drive, not to the * directory (but not for "//machine/dir"). Only use the drive name. */ if ((*path == '/' || *path == '\\') && path[1] != path[0] && search_ctx->ffsc_start_dir[1] == ':') search_ctx->ffsc_start_dir[2] = NUL; #endif } /* * If stopdirs are given, split them into an array of pointers. * If this fails (mem allocation), there is no upward search at all or a * stop directory is not recognized -> continue silently. * If stopdirs just contains a ";" or is empty, * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This * is handled as unlimited upward search. See function * ff_path_in_stoplist() for details. */ if (stopdirs != NULL) { char_u *walker = stopdirs; int dircount; while (*walker == ';') walker++; dircount = 1; search_ctx->ffsc_stopdirs_v = xmalloc(sizeof(char_u *)); do { char_u *helper; void *ptr; helper = walker; ptr = xrealloc(search_ctx->ffsc_stopdirs_v, (dircount + 1) * sizeof(char_u *)); search_ctx->ffsc_stopdirs_v = ptr; walker = vim_strchr(walker, ';'); if (walker) { search_ctx->ffsc_stopdirs_v[dircount-1] = vim_strnsave(helper, (int)(walker - helper)); walker++; } else /* this might be "", which means ascent till top * of directory tree. */ search_ctx->ffsc_stopdirs_v[dircount-1] = vim_strsave(helper); dircount++; } while (walker != NULL); search_ctx->ffsc_stopdirs_v[dircount-1] = NULL; } search_ctx->ffsc_level = level; /* split into: * -fix path * -wildcard_stuff (might be NULL) */ wc_part = vim_strchr(path, '*'); if (wc_part != NULL) { int llevel; int len; char *errpt; /* save the fix part of the path */ search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path)); /* * copy wc_path and add restricts to the '**' wildcard. * The octet after a '**' is used as a (binary) counter. * So '**3' is transposed to '**^C' ('^C' is ASCII value 3) * or '**76' is transposed to '**N'( 'N' is ASCII value 76). * For EBCDIC you get different character values. * If no restrict is given after '**' the default is used. * Due to this technique the path looks awful if you print it as a * string. */ len = 0; while (*wc_part != NUL) { if (len + 5 >= MAXPATHL) { EMSG(_(e_pathtoolong)); break; } if (STRNCMP(wc_part, "**", 2) == 0) { ff_expand_buffer[len++] = *wc_part++; ff_expand_buffer[len++] = *wc_part++; llevel = strtol((char *)wc_part, &errpt, 10); if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255) ff_expand_buffer[len++] = llevel; else if ((char_u *)errpt != wc_part && llevel == 0) /* restrict is 0 -> remove already added '**' */ len -= 2; else ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND; wc_part = (char_u *)errpt; if (*wc_part != NUL && !vim_ispathsep(*wc_part)) { EMSG2(_( "E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR); goto error_return; } } else ff_expand_buffer[len++] = *wc_part++; } ff_expand_buffer[len] = NUL; search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer); } else search_ctx->ffsc_fix_path = vim_strsave(path); if (search_ctx->ffsc_start_dir == NULL) { /* store the fix part as startdir. * This is needed if the parameter path is fully qualified. */ search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path); search_ctx->ffsc_fix_path[0] = NUL; } /* create an absolute path */ if (STRLEN(search_ctx->ffsc_start_dir) + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL) { EMSG(_(e_pathtoolong)); goto error_return; } STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir); add_pathsep(ff_expand_buffer); { size_t eb_len = STRLEN(ff_expand_buffer); char_u *buf = xmalloc(eb_len + STRLEN(search_ctx->ffsc_fix_path) + 1); STRCPY(buf, ff_expand_buffer); STRCPY(buf + eb_len, search_ctx->ffsc_fix_path); if (os_isdir(buf)) { STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path); add_pathsep(ff_expand_buffer); } else { char_u *p = path_tail(search_ctx->ffsc_fix_path); char_u *wc_path = NULL; char_u *temp = NULL; int len = 0; if (p > search_ctx->ffsc_fix_path) { len = (int)(p - search_ctx->ffsc_fix_path) - 1; STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len); add_pathsep(ff_expand_buffer); } else len = (int)STRLEN(search_ctx->ffsc_fix_path); if (search_ctx->ffsc_wc_path != NULL) { wc_path = vim_strsave(search_ctx->ffsc_wc_path); temp = xmalloc(STRLEN(search_ctx->ffsc_wc_path) + STRLEN(search_ctx->ffsc_fix_path + len) + 1); } if (temp == NULL || wc_path == NULL) { free(buf); free(temp); free(wc_path); goto error_return; } STRCPY(temp, search_ctx->ffsc_fix_path + len); STRCAT(temp, search_ctx->ffsc_wc_path); free(search_ctx->ffsc_wc_path); free(wc_path); search_ctx->ffsc_wc_path = temp; } free(buf); } sptr = ff_create_stack_element(ff_expand_buffer, search_ctx->ffsc_wc_path, level, 0); ff_push(search_ctx, sptr); search_ctx->ffsc_file_to_search = vim_strsave(filename); return search_ctx; error_return: /* * We clear the search context now! * Even when the caller gave us a (perhaps valid) context we free it here, * as we might have already destroyed it. */ vim_findfile_cleanup(search_ctx); return NULL; }
/* * Remove the (sub)menu with the given name from the menu hierarchy * Called recursively. */ static int remove_menu ( vimmenu_T **menup, char_u *name, int modes, bool silent /* don't give error messages */ ) { vimmenu_T *menu; vimmenu_T *child; char_u *p; if (*menup == NULL) return OK; /* Got to bottom of hierarchy */ /* Get name of this element in the menu hierarchy */ p = menu_name_skip(name); /* Find the menu */ while ((menu = *menup) != NULL) { if (*name == NUL || menu_name_equal(name, menu)) { if (*p != NUL && menu->children == NULL) { if (!silent) EMSG(_(e_notsubmenu)); return FAIL; } if ((menu->modes & modes) != 0x0) { if (remove_menu(&menu->children, p, modes, silent) == FAIL) return FAIL; } else if (*name != NUL) { if (!silent) EMSG(_(e_othermode)); return FAIL; } /* * When name is empty, we are removing all menu items for the given * modes, so keep looping, otherwise we are just removing the named * menu item (which has been found) so break here. */ if (*name != NUL) break; /* Remove the menu item for the given mode[s]. If the menu item * is no longer valid in ANY mode, delete it */ menu->modes &= ~modes; if (modes & MENU_TIP_MODE) free_menu_string(menu, MENU_INDEX_TIP); if ((menu->modes & MENU_ALL_MODES) == 0) free_menu(menup); else menup = &menu->next; } else menup = &menu->next; } if (*name != NUL) { if (menu == NULL) { if (!silent) EMSG2(_(e_nomenu), name); return FAIL; } /* Recalculate modes for menu based on the new updated children */ menu->modes &= ~modes; child = menu->children; for (; child != NULL; child = child->next) menu->modes |= child->modes; if (modes & MENU_TIP_MODE) { free_menu_string(menu, MENU_INDEX_TIP); } if ((menu->modes & MENU_ALL_MODES) == 0) { /* The menu item is no longer valid in ANY mode, so delete it */ *menup = menu; free_menu(menup); } } return OK; }
/* * Allocate a variable for a Dictionary and fill it from "*arg". * Return OK or FAIL. Returns NOTDONE for {expr}. */ int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate) { dict_T *d = NULL; typval_T tvkey; typval_T tv; char_u *key = NULL; dictitem_T *item; char_u *start = skipwhite(*arg + 1); char_u buf[NUMBUFLEN]; /* * First check if it's not a curly-braces thing: {expr}. * Must do this without evaluating, otherwise a function may be called * twice. Unfortunately this means we need to call eval1() twice for the * first item. * But {} is an empty Dictionary. */ if (*start != '}') { if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */ return FAIL; if (*start == '}') return NOTDONE; } if (evaluate) { d = dict_alloc(); if (d == NULL) return FAIL; } tvkey.v_type = VAR_UNKNOWN; tv.v_type = VAR_UNKNOWN; *arg = skipwhite(*arg + 1); while (**arg != '}' && **arg != NUL) { if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */ goto failret; if (**arg != ':') { EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg); clear_tv(&tvkey); goto failret; } if (evaluate) { key = get_tv_string_buf_chk(&tvkey, buf); if (key == NULL) { /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */ clear_tv(&tvkey); goto failret; } } *arg = skipwhite(*arg + 1); if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */ { if (evaluate) clear_tv(&tvkey); goto failret; } if (evaluate) { item = dict_find(d, key, -1); if (item != NULL) { EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key); clear_tv(&tvkey); clear_tv(&tv); goto failret; } item = dictitem_alloc(key); clear_tv(&tvkey); if (item != NULL) { item->di_tv = tv; item->di_tv.v_lock = 0; if (dict_add(d, item) == FAIL) dictitem_free(item); } } if (**arg == '}') break; if (**arg != ',') { EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg); goto failret; } *arg = skipwhite(*arg + 1); } if (**arg != '}') { EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg); failret: if (evaluate) dict_free(d); return FAIL; } *arg = skipwhite(*arg + 1); if (evaluate) { rettv->v_type = VAR_DICT; rettv->vval.v_dict = d; ++d->dv_refcount; } return OK; }
/* * Load library and get all pointers. * Parameter 'libname' provides name of DLL. * Return OK or FAIL. */ static int py3_runtime_link_init(char *libname, int verbose) { int i; void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string; # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON) /* Can't have Python and Python3 loaded at the same time. * It cause a crash, because RTLD_GLOBAL is needed for * standard C extension libraries of one or both python versions. */ if (python_loaded()) { if (verbose) EMSG(_("E837: This Vim cannot execute :py3 after using :python")); return FAIL; } # endif if (hinstPy3 != 0) return OK; hinstPy3 = load_dll(libname); if (!hinstPy3) { if (verbose) EMSG2(_(e_loadlib), libname); return FAIL; } for (i = 0; py3_funcname_table[i].ptr; ++i) { if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3, py3_funcname_table[i].name)) == NULL) { close_dll(hinstPy3); hinstPy3 = 0; if (verbose) EMSG2(_(e_loadfunc), py3_funcname_table[i].name); return FAIL; } } /* Load unicode functions separately as only the ucs2 or the ucs4 functions * will be present in the library. */ # if PY_VERSION_HEX >= 0x030300f0 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString"); ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode"); ucs_as_encoded_string = symbol_from_dll(hinstPy3, "PyUnicode_AsEncodedString"); # else ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString"); ucs_decode = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_Decode"); ucs_as_encoded_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_AsEncodedString"); if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string) { ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS4_FromString"); ucs_decode = symbol_from_dll(hinstPy3, "PyUnicodeUCS4_Decode"); ucs_as_encoded_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS4_AsEncodedString"); } # endif if (ucs_from_string && ucs_decode && ucs_as_encoded_string) { py3_PyUnicode_FromString = ucs_from_string; py3_PyUnicode_Decode = ucs_decode; py3_PyUnicode_AsEncodedString = ucs_as_encoded_string; } else { close_dll(hinstPy3); hinstPy3 = 0; if (verbose) EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*"); return FAIL; } return OK; }
/* * Encode "val" into "gap". * Return FAIL or OK. */ static int json_encode_item(garray_T *gap, typval_T *val, int copyID) { char_u numbuf[NUMBUFLEN]; char_u *res; list_T *l; dict_T *d; switch (val->v_type) { case VAR_SPECIAL: switch (val->vval.v_number) { case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break; case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break; case VVAL_NONE: break; case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break; } break; case VAR_NUMBER: vim_snprintf((char *)numbuf, NUMBUFLEN, "%ld", (long)val->vval.v_number); ga_concat(gap, numbuf); break; case VAR_STRING: res = val->vval.v_string; write_string(gap, res); break; case VAR_FUNC: /* no JSON equivalent */ EMSG(_(e_invarg)); return FAIL; case VAR_LIST: l = val->vval.v_list; if (l == NULL) ga_concat(gap, (char_u *)"null"); else { if (l->lv_copyID == copyID) ga_concat(gap, (char_u *)"[]"); else { listitem_T *li; l->lv_copyID = copyID; ga_append(gap, '['); for (li = l->lv_first; li != NULL && !got_int; ) { if (json_encode_item(gap, &li->li_tv, copyID) == FAIL) return FAIL; li = li->li_next; if (li != NULL) ga_append(gap, ','); } ga_append(gap, ']'); l->lv_copyID = 0; } } break; case VAR_DICT: d = val->vval.v_dict; if (d == NULL) ga_concat(gap, (char_u *)"null"); else { if (d->dv_copyID == copyID) ga_concat(gap, (char_u *)"{}"); else { int first = TRUE; int todo = (int)d->dv_hashtab.ht_used; hashitem_T *hi; d->dv_copyID = copyID; ga_append(gap, '{'); for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi) if (!HASHITEM_EMPTY(hi)) { --todo; if (first) first = FALSE; else ga_append(gap, ','); write_string(gap, hi->hi_key); ga_append(gap, ':'); if (json_encode_item(gap, &dict_lookup(hi)->di_tv, copyID) == FAIL) return FAIL; } ga_append(gap, '}'); d->dv_copyID = 0; } } break; #ifdef FEAT_FLOAT case VAR_FLOAT: vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", val->vval.v_float); ga_concat(gap, numbuf); break; #endif default: EMSG2(_(e_intern2), "json_encode_item()"); break; return FAIL; } return OK; }
/* * ":delmarks[!] [marks]" */ void ex_delmarks(exarg_T *eap) { char_u *p; int from, to; int i; int lower; int digit; int n; if (*eap->arg == NUL && eap->forceit) /* clear all marks */ clrallmarks(curbuf); else if (eap->forceit) EMSG(_(e_invarg)); else if (*eap->arg == NUL) EMSG(_(e_argreq)); else { /* clear specified marks only */ for (p = eap->arg; *p != NUL; ++p) { lower = ASCII_ISLOWER(*p); digit = VIM_ISDIGIT(*p); if (lower || digit || ASCII_ISUPPER(*p)) { if (p[1] == '-') { /* clear range of marks */ from = *p; to = p[2]; if (!(lower ? ASCII_ISLOWER(p[2]) : (digit ? VIM_ISDIGIT(p[2]) : ASCII_ISUPPER(p[2]))) || to < from) { EMSG2(_(e_invarg2), p); return; } p += 2; } else /* clear one lower case mark */ from = to = *p; for (i = from; i <= to; ++i) { if (lower) curbuf->b_namedm[i - 'a'].lnum = 0; else { if (digit) n = i - '0' + NMARKS; else n = i - 'A'; namedfm[n].fmark.mark.lnum = 0; vim_free(namedfm[n].fname); namedfm[n].fname = NULL; } } } else switch (*p) { case '"': curbuf->b_last_cursor.lnum = 0; break; case '^': curbuf->b_last_insert.lnum = 0; break; case '.': curbuf->b_last_change.lnum = 0; break; case '[': curbuf->b_op_start.lnum = 0; break; case ']': curbuf->b_op_end.lnum = 0; break; case '<': curbuf->b_visual.vi_start.lnum = 0; break; case '>': curbuf->b_visual.vi_end.lnum = 0; break; case ' ': break; default: EMSG2(_(e_invarg2), p); return; } } } }
/* * Encode "val" into "gap". * Return FAIL or OK. */ static int json_encode_item(garray_T *gap, typval_T *val, int copyID, int options) { char_u numbuf[NUMBUFLEN]; char_u *res; list_T *l; dict_T *d; switch (val->v_type) { case VAR_SPECIAL: switch (val->vval.v_number) { case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break; case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break; case VVAL_NONE: if ((options & JSON_JS) != 0 && (options & JSON_NO_NONE) == 0) /* empty item */ break; /* FALLTHROUGH */ case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break; } break; case VAR_NUMBER: vim_snprintf((char *)numbuf, NUMBUFLEN, "%ld", (long)val->vval.v_number); ga_concat(gap, numbuf); break; case VAR_STRING: res = val->vval.v_string; write_string(gap, res); break; case VAR_FUNC: case VAR_JOB: case VAR_CHANNEL: /* no JSON equivalent TODO: better error */ EMSG(_(e_invarg)); return FAIL; case VAR_LIST: l = val->vval.v_list; if (l == NULL) ga_concat(gap, (char_u *)"null"); else { if (l->lv_copyID == copyID) ga_concat(gap, (char_u *)"[]"); else { listitem_T *li; l->lv_copyID = copyID; ga_append(gap, '['); for (li = l->lv_first; li != NULL && !got_int; ) { if (json_encode_item(gap, &li->li_tv, copyID, options & JSON_JS) == FAIL) return FAIL; if ((options & JSON_JS) && li->li_next == NULL && li->li_tv.v_type == VAR_SPECIAL && li->li_tv.vval.v_number == VVAL_NONE) /* add an extra comma if the last item is v:none */ ga_append(gap, ','); li = li->li_next; if (li != NULL) ga_append(gap, ','); } ga_append(gap, ']'); l->lv_copyID = 0; } } break; case VAR_DICT: d = val->vval.v_dict; if (d == NULL) ga_concat(gap, (char_u *)"null"); else { if (d->dv_copyID == copyID) ga_concat(gap, (char_u *)"{}"); else { int first = TRUE; int todo = (int)d->dv_hashtab.ht_used; hashitem_T *hi; d->dv_copyID = copyID; ga_append(gap, '{'); for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi) if (!HASHITEM_EMPTY(hi)) { --todo; if (first) first = FALSE; else ga_append(gap, ','); if ((options & JSON_JS) && is_simple_key(hi->hi_key)) ga_concat(gap, hi->hi_key); else write_string(gap, hi->hi_key); ga_append(gap, ':'); if (json_encode_item(gap, &dict_lookup(hi)->di_tv, copyID, options | JSON_NO_NONE) == FAIL) return FAIL; } ga_append(gap, '}'); d->dv_copyID = 0; } } break; case VAR_FLOAT: #ifdef FEAT_FLOAT vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", val->vval.v_float); ga_concat(gap, numbuf); break; #endif case VAR_UNKNOWN: EMSG2(_(e_intern2), "json_encode_item()"); return FAIL; } return OK; }
static char * getarg(char *arg) { char *xarg = arg; wchar_t c; char mbc[MB_LEN_MAX]; size_t len; int escape = 0; int inquote = 0; arg[0] = '\0'; while (MORE) { len = 0; c = getwchr(mbc, &len); if (((arg - xarg) + len) > BUFLIM) { EMSG2(ARG2LONG, BUFLIM); exit(2); ERR = TRUE; return (NULL); } switch (c) { case '\n': if (ZERO) { store_str(&arg, mbc, len); continue; } /* FALLTHRU */ case '\0': case WEOF: /* Note WEOF == EOF */ if (escape) { EMSG(BADESCAPE); ERR = TRUE; return (NULL); } if (inquote) { EMSG(MISSQUOTE); ERR = TRUE; return (NULL); } N_lines++; break; case '"': if (ZERO || escape || (inquote == 1)) { /* treat it literally */ escape = 0; store_str(&arg, mbc, len); } else if (inquote == 2) { /* terminating double quote */ inquote = 0; } else { /* starting quoted string */ inquote = 2; } continue; case '\'': if (ZERO || escape || (inquote == 2)) { /* treat it literally */ escape = 0; store_str(&arg, mbc, len); } else if (inquote == 1) { /* terminating single quote */ inquote = 0; } else { /* starting quoted string */ inquote = 1; } continue; case '\\': /* * Any unquoted character can be escaped by * preceding it with a backslash. */ if (ZERO || inquote || escape) { escape = 0; store_str(&arg, mbc, len); } else { escape = 1; } continue; default: /* most times we will just want to store it */ if (inquote || escape || ZERO || !iswctype(c, blank)) { escape = 0; store_str(&arg, mbc, len); continue; } /* unquoted blank */ break; } /* * At this point we are processing a complete argument. */ if (strcmp(xarg, LEOF) == 0 && *LEOF != '\0') { MORE = FALSE; return (NULL); } if (c == WEOF) { MORE = FALSE; } if (xarg[0] == '\0') continue; break; } return (xarg[0] == '\0' ? NULL : xarg); }
/* * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and * execute it. */ void ex_emenu(exarg_T *eap) { vimmenu_T *menu; char_u *name; char_u *saved_name; char_u *p; int idx; char_u *mode; saved_name = vim_strsave(eap->arg); menu = root_menu; name = saved_name; while (*name) { /* Find in the menu hierarchy */ p = menu_name_skip(name); while (menu != NULL) { if (menu_name_equal(name, menu)) { if (*p == NUL && menu->children != NULL) { EMSG(_("E333: Menu path must lead to a menu item")); menu = NULL; } else if (*p != NUL && menu->children == NULL) { EMSG(_(e_notsubmenu)); menu = NULL; } break; } menu = menu->next; } if (menu == NULL || *p == NUL) break; menu = menu->children; name = p; } free(saved_name); if (menu == NULL) { EMSG2(_("E334: Menu not found: %s"), eap->arg); return; } /* Found the menu, so execute. * Use the Insert mode entry when returning to Insert mode. */ if (restart_edit && !current_SID ) { mode = (char_u *)"Insert"; idx = MENU_INDEX_INSERT; } else if (eap->addr_count) { pos_T tpos; mode = (char_u *)"Visual"; idx = MENU_INDEX_VISUAL; /* GEDDES: This is not perfect - but it is a * quick way of detecting whether we are doing this from a * selection - see if the range matches up with the visual * select start and end. */ if ((curbuf->b_visual.vi_start.lnum == eap->line1) && (curbuf->b_visual.vi_end.lnum) == eap->line2) { /* Set it up for visual mode - equivalent to gv. */ VIsual_mode = curbuf->b_visual.vi_mode; tpos = curbuf->b_visual.vi_end; curwin->w_cursor = curbuf->b_visual.vi_start; curwin->w_curswant = curbuf->b_visual.vi_curswant; } else { /* Set it up for line-wise visual mode */ VIsual_mode = 'V'; curwin->w_cursor.lnum = eap->line1; curwin->w_cursor.col = 1; tpos.lnum = eap->line2; tpos.col = MAXCOL; tpos.coladd = 0; } /* Activate visual mode */ VIsual_active = TRUE; VIsual_reselect = TRUE; check_cursor(); VIsual = curwin->w_cursor; curwin->w_cursor = tpos; check_cursor(); /* Adjust the cursor to make sure it is in the correct pos * for exclusive mode */ if (*p_sel == 'e' && gchar_cursor() != NUL) ++curwin->w_cursor.col; } else { mode = (char_u *)"Normal"; idx = MENU_INDEX_NORMAL; } if (idx != MENU_INDEX_INVALID && menu->strings[idx] != NULL) { /* When executing a script or function execute the commands right now. * Otherwise put them in the typeahead buffer. */ if (current_SID != 0) exec_normal_cmd(menu->strings[idx], menu->noremap[idx], menu->silent[idx]); else ins_typebuf(menu->strings[idx], menu->noremap[idx], 0, TRUE, menu->silent[idx]); } else EMSG2(_("E335: Menu not defined for %s mode"), mode); }
/* * Load library and get all pointers. * Parameter 'libname' provides name of DLL. * Return OK or FAIL. */ static int py3_runtime_link_init(char *libname, int verbose) { int i; void *ucs_from_string, *ucs_from_string_and_size; #if defined(UNIX) && defined(FEAT_PYTHON) /* Can't have Python and Python3 loaded at the same time, it may cause a * crash. */ if (python_loaded()) { EMSG(_("E999: Python: Cannot use :py and :py3 in one session")); return FAIL; } #endif if (hinstPy3 != 0) return OK; hinstPy3 = load_dll(libname); if (!hinstPy3) { if (verbose) EMSG2(_(e_loadlib), libname); return FAIL; } for (i = 0; py3_funcname_table[i].ptr; ++i) { if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3, py3_funcname_table[i].name)) == NULL) { close_dll(hinstPy3); hinstPy3 = 0; if (verbose) EMSG2(_(e_loadfunc), py3_funcname_table[i].name); return FAIL; } } /* Load unicode functions separately as only the ucs2 or the ucs4 functions * will be present in the library. */ ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString"); ucs_from_string_and_size = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromStringAndSize"); if (!ucs_from_string || !ucs_from_string_and_size) { ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS4_FromString"); ucs_from_string_and_size = symbol_from_dll(hinstPy3, "PyUnicodeUCS4_FromStringAndSize"); } if (ucs_from_string && ucs_from_string_and_size) { py3_PyUnicode_FromString = ucs_from_string; py3_PyUnicode_FromStringAndSize = ucs_from_string_and_size; } else { close_dll(hinstPy3); hinstPy3 = 0; if (verbose) EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*"); return FAIL; } return OK; }
/* * Load library and get all pointers. * Parameter 'libname' provides name of DLL. * Return OK or FAIL. */ static int python_runtime_link_init(char *libname, int verbose) { int i; void *ucs_as_encoded_string; #if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3) /* Can't have Python and Python3 loaded at the same time. * It cause a crash, because RTLD_GLOBAL is needed for * standard C extension libraries of one or both python versions. */ if (python3_loaded()) { if (verbose) EMSG(_("E836: This Vim cannot execute :python after using :py3")); return FAIL; } #endif if (hinstPython) return OK; hinstPython = load_dll(libname); if (!hinstPython) { if (verbose) EMSG2(_(e_loadlib), libname); return FAIL; } for (i = 0; python_funcname_table[i].ptr; ++i) { if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython, python_funcname_table[i].name)) == NULL) { close_dll(hinstPython); hinstPython = 0; if (verbose) EMSG2(_(e_loadfunc), python_funcname_table[i].name); return FAIL; } } /* Load unicode functions separately as only the ucs2 or the ucs4 functions * will be present in the library. */ ucs_as_encoded_string = symbol_from_dll(hinstPython, "PyUnicodeUCS2_AsEncodedString"); if (ucs_as_encoded_string == NULL) ucs_as_encoded_string = symbol_from_dll(hinstPython, "PyUnicodeUCS4_AsEncodedString"); if (ucs_as_encoded_string != NULL) py_PyUnicode_AsEncodedString = ucs_as_encoded_string; else { close_dll(hinstPython); hinstPython = 0; if (verbose) EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*"); return FAIL; } return OK; }
/* * Remove the (sub)menu with the given name from the menu hierarchy * Called recursively. */ static int remove_menu ( vimmenu_T **menup, char_u *name, int modes, int silent /* don't give error messages */ ) { vimmenu_T *menu; vimmenu_T *child; char_u *p; if (*menup == NULL) return OK; /* Got to bottom of hierarchy */ /* Get name of this element in the menu hierarchy */ p = menu_name_skip(name); /* Find the menu */ while ((menu = *menup) != NULL) { if (*name == NUL || menu_name_equal(name, menu)) { if (*p != NUL && menu->children == NULL) { if (!silent) EMSG(_(e_notsubmenu)); return FAIL; } if ((menu->modes & modes) != 0x0) { #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF) /* * If we are removing all entries for this menu,MENU_ALL_MODES, * Then kill any tearoff before we start */ if (*p == NUL && modes == MENU_ALL_MODES) { if (IsWindow(menu->tearoff_handle)) DestroyWindow(menu->tearoff_handle); } #endif if (remove_menu(&menu->children, p, modes, silent) == FAIL) return FAIL; } else if (*name != NUL) { if (!silent) EMSG(_(e_othermode)); return FAIL; } /* * When name is empty, we are removing all menu items for the given * modes, so keep looping, otherwise we are just removing the named * menu item (which has been found) so break here. */ if (*name != NUL) break; /* Remove the menu item for the given mode[s]. If the menu item * is no longer valid in ANY mode, delete it */ menu->modes &= ~modes; if (modes & MENU_TIP_MODE) free_menu_string(menu, MENU_INDEX_TIP); if ((menu->modes & MENU_ALL_MODES) == 0) free_menu(menup); else menup = &menu->next; } else menup = &menu->next; } if (*name != NUL) { if (menu == NULL) { if (!silent) EMSG2(_(e_nomenu), name); return FAIL; } /* Recalculate modes for menu based on the new updated children */ menu->modes &= ~modes; #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF) if ((s_tearoffs) && (menu->children != NULL)) /* there's a tear bar.. */ child = menu->children->next; /* don't count tearoff bar */ else #endif child = menu->children; for (; child != NULL; child = child->next) menu->modes |= child->modes; if (modes & MENU_TIP_MODE) { free_menu_string(menu, MENU_INDEX_TIP); #if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \ && (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK)) /* Need to update the menu tip. */ if (gui.in_use) gui_mch_menu_set_tip(menu); #endif } if ((menu->modes & MENU_ALL_MODES) == 0) { /* The menu item is no longer valid in ANY mode, so delete it */ #if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF) if (s_tearoffs && menu->children != NULL) /* there's a tear bar.. */ free_menu(&menu->children); #endif *menup = menu; free_menu(menup); } } return OK; }
/* * Decode one item and put it in "res". If "res" is NULL only advance. * Must already have skipped white space. * * Return FAIL for a decoding error (and give an error). * Return MAYBE for an incomplete message. */ static int json_decode_item(js_read_T *reader, typval_T *res, int options) { char_u *p; int len; int retval; garray_T stack; typval_T item; typval_T *cur_item; json_dec_item_T *top_item; char_u key_buf[NUMBUFLEN]; ga_init2(&stack, sizeof(json_dec_item_T), 100); cur_item = res; init_tv(&item); if (res != NULL) init_tv(res); fill_numbuflen(reader); p = reader->js_buf + reader->js_used; for (;;) { top_item = NULL; if (stack.ga_len > 0) { top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1; json_skip_white(reader); p = reader->js_buf + reader->js_used; if (*p == NUL) { retval = MAYBE; if (top_item->jd_type == JSON_OBJECT) /* did get the key, clear it */ clear_tv(&top_item->jd_key_tv); goto theend; } if (top_item->jd_type == JSON_OBJECT_KEY || top_item->jd_type == JSON_ARRAY) { /* Check for end of object or array. */ if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}')) { ++reader->js_used; /* consume the ']' or '}' */ --stack.ga_len; if (stack.ga_len == 0) { retval = OK; goto theend; } if (cur_item != NULL) cur_item = &top_item->jd_tv; goto item_end; } } } if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY && (options & JSON_JS) && reader->js_buf[reader->js_used] != '"' && reader->js_buf[reader->js_used] != '\'' && reader->js_buf[reader->js_used] != '[' && reader->js_buf[reader->js_used] != '{') { char_u *key; /* accept an object key that is not in quotes */ key = p = reader->js_buf + reader->js_used; while (*p != NUL && *p != ':' && *p > ' ') ++p; if (cur_item != NULL) { cur_item->v_type = VAR_STRING; cur_item->vval.v_string = vim_strnsave(key, (int)(p - key)); top_item->jd_key = cur_item->vval.v_string; } reader->js_used += (int)(p - key); } else { switch (*p) { case '[': /* start of array */ if (top_item && top_item->jd_type == JSON_OBJECT_KEY) { retval = FAIL; break; } if (ga_grow(&stack, 1) == FAIL) { retval = FAIL; break; } if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL) { cur_item->v_type = VAR_SPECIAL; cur_item->vval.v_number = VVAL_NONE; retval = FAIL; break; } ++reader->js_used; /* consume the '[' */ top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len; top_item->jd_type = JSON_ARRAY; ++stack.ga_len; if (cur_item != NULL) { top_item->jd_tv = *cur_item; cur_item = &item; } continue; case '{': /* start of object */ if (top_item && top_item->jd_type == JSON_OBJECT_KEY) { retval = FAIL; break; } if (ga_grow(&stack, 1) == FAIL) { retval = FAIL; break; } if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL) { cur_item->v_type = VAR_SPECIAL; cur_item->vval.v_number = VVAL_NONE; retval = FAIL; break; } ++reader->js_used; /* consume the '{' */ top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len; top_item->jd_type = JSON_OBJECT_KEY; ++stack.ga_len; if (cur_item != NULL) { top_item->jd_tv = *cur_item; cur_item = &top_item->jd_key_tv; } continue; case '"': /* string */ retval = json_decode_string(reader, cur_item, *p); break; case '\'': if (options & JSON_JS) retval = json_decode_string(reader, cur_item, *p); else { EMSG(_(e_invarg)); retval = FAIL; } break; case ',': /* comma: empty item */ if ((options & JSON_JS) == 0) { EMSG(_(e_invarg)); retval = FAIL; break; } /* FALLTHROUGH */ case NUL: /* empty */ if (cur_item != NULL) { cur_item->v_type = VAR_SPECIAL; cur_item->vval.v_number = VVAL_NONE; } retval = OK; break; default: if (VIM_ISDIGIT(*p) || *p == '-') { #ifdef FEAT_FLOAT char_u *sp = p; if (*sp == '-') { ++sp; if (*sp == NUL) { retval = MAYBE; break; } if (!VIM_ISDIGIT(*sp)) { EMSG(_(e_invarg)); retval = FAIL; break; } } sp = skipdigits(sp); if (*sp == '.' || *sp == 'e' || *sp == 'E') { if (cur_item == NULL) { float_T f; len = string2float(p, &f); } else { cur_item->v_type = VAR_FLOAT; len = string2float(p, &cur_item->vval.v_float); } } else #endif { varnumber_T nr; vim_str2nr(reader->js_buf + reader->js_used, NULL, &len, 0, /* what */ &nr, NULL, 0); if (cur_item != NULL) { cur_item->v_type = VAR_NUMBER; cur_item->vval.v_number = nr; } } reader->js_used += len; retval = OK; break; } if (STRNICMP((char *)p, "false", 5) == 0) { reader->js_used += 5; if (cur_item != NULL) { cur_item->v_type = VAR_SPECIAL; cur_item->vval.v_number = VVAL_FALSE; } retval = OK; break; } if (STRNICMP((char *)p, "true", 4) == 0) { reader->js_used += 4; if (cur_item != NULL) { cur_item->v_type = VAR_SPECIAL; cur_item->vval.v_number = VVAL_TRUE; } retval = OK; break; } if (STRNICMP((char *)p, "null", 4) == 0) { reader->js_used += 4; if (cur_item != NULL) { cur_item->v_type = VAR_SPECIAL; cur_item->vval.v_number = VVAL_NULL; } retval = OK; break; } #ifdef FEAT_FLOAT if (STRNICMP((char *)p, "NaN", 3) == 0) { reader->js_used += 3; if (cur_item != NULL) { cur_item->v_type = VAR_FLOAT; cur_item->vval.v_float = NAN; } retval = OK; break; } if (STRNICMP((char *)p, "Infinity", 8) == 0) { reader->js_used += 8; if (cur_item != NULL) { cur_item->v_type = VAR_FLOAT; cur_item->vval.v_float = INFINITY; } retval = OK; break; } #endif /* check for truncated name */ len = (int)(reader->js_end - (reader->js_buf + reader->js_used)); if ( (len < 5 && STRNICMP((char *)p, "false", len) == 0) #ifdef FEAT_FLOAT || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0) || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0) #endif || (len < 4 && (STRNICMP((char *)p, "true", len) == 0 || STRNICMP((char *)p, "null", len) == 0))) retval = MAYBE; else retval = FAIL; break; } /* We are finished when retval is FAIL or MAYBE and when at the * toplevel. */ if (retval == FAIL) break; if (retval == MAYBE || stack.ga_len == 0) goto theend; if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY && cur_item != NULL) { top_item->jd_key = get_tv_string_buf_chk(cur_item, key_buf); if (top_item->jd_key == NULL) { clear_tv(cur_item); EMSG(_(e_invarg)); retval = FAIL; goto theend; } } } item_end: top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1; switch (top_item->jd_type) { case JSON_ARRAY: if (res != NULL) { listitem_T *li = listitem_alloc(); if (li == NULL) { clear_tv(cur_item); retval = FAIL; goto theend; } li->li_tv = *cur_item; list_append(top_item->jd_tv.vval.v_list, li); } if (cur_item != NULL) cur_item = &item; json_skip_white(reader); p = reader->js_buf + reader->js_used; if (*p == ',') ++reader->js_used; else if (*p != ']') { if (*p == NUL) retval = MAYBE; else { EMSG(_(e_invarg)); retval = FAIL; } goto theend; } break; case JSON_OBJECT_KEY: json_skip_white(reader); p = reader->js_buf + reader->js_used; if (*p != ':') { if (cur_item != NULL) clear_tv(cur_item); if (*p == NUL) retval = MAYBE; else { EMSG(_(e_invarg)); retval = FAIL; } goto theend; } ++reader->js_used; json_skip_white(reader); top_item->jd_type = JSON_OBJECT; if (cur_item != NULL) cur_item = &item; break; case JSON_OBJECT: if (cur_item != NULL && dict_find(top_item->jd_tv.vval.v_dict, top_item->jd_key, -1) != NULL) { EMSG2(_("E938: Duplicate key in JSON: \"%s\""), top_item->jd_key); clear_tv(&top_item->jd_key_tv); clear_tv(cur_item); retval = FAIL; goto theend; } if (cur_item != NULL) { dictitem_T *di = dictitem_alloc(top_item->jd_key); clear_tv(&top_item->jd_key_tv); if (di == NULL) { clear_tv(cur_item); retval = FAIL; goto theend; } di->di_tv = *cur_item; di->di_tv.v_lock = 0; if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL) { dictitem_free(di); retval = FAIL; goto theend; } } json_skip_white(reader); p = reader->js_buf + reader->js_used; if (*p == ',') ++reader->js_used; else if (*p != '}') { if (*p == NUL) retval = MAYBE; else { EMSG(_(e_invarg)); retval = FAIL; } goto theend; } top_item->jd_type = JSON_OBJECT_KEY; if (cur_item != NULL) cur_item = &top_item->jd_key_tv; break; } } /* Get here when parsing failed. */ if (res != NULL) { clear_tv(res); res->v_type = VAR_SPECIAL; res->vval.v_number = VVAL_NONE; } EMSG(_(e_invarg)); theend: ga_clear(&stack); return retval; }
/* * Do the :menu command and relatives. */ void ex_menu ( exarg_T *eap /* Ex command arguments */ ) { char_u *menu_path; int modes; char_u *map_to; int noremap; int silent = FALSE; int special = FALSE; int unmenu; char_u *map_buf; char_u *arg; char_u *p; int i; int pri_tab[MENUDEPTH + 1]; int enable = MAYBE; /* TRUE for "menu enable", FALSE for "menu * disable */ vimmenu_T menuarg; modes = get_menu_cmd_modes(eap->cmd, eap->forceit, &noremap, &unmenu); arg = eap->arg; for (;; ) { if (STRNCMP(arg, "<script>", 8) == 0) { noremap = REMAP_SCRIPT; arg = skipwhite(arg + 8); continue; } if (STRNCMP(arg, "<silent>", 8) == 0) { silent = TRUE; arg = skipwhite(arg + 8); continue; } if (STRNCMP(arg, "<special>", 9) == 0) { special = TRUE; arg = skipwhite(arg + 9); continue; } break; } /* Locate an optional "icon=filename" argument. */ if (STRNCMP(arg, "icon=", 5) == 0) { arg += 5; while (*arg != NUL && *arg != ' ') { if (*arg == '\\') STRMOVE(arg, arg + 1); mb_ptr_adv(arg); } if (*arg != NUL) { *arg++ = NUL; arg = skipwhite(arg); } } /* * Fill in the priority table. */ for (p = arg; *p; ++p) if (!VIM_ISDIGIT(*p) && *p != '.') break; if (vim_iswhite(*p)) { for (i = 0; i < MENUDEPTH && !vim_iswhite(*arg); ++i) { pri_tab[i] = getdigits_int(&arg); if (pri_tab[i] == 0) pri_tab[i] = 500; if (*arg == '.') ++arg; } arg = skipwhite(arg); } else if (eap->addr_count && eap->line2 != 0) { pri_tab[0] = eap->line2; i = 1; } else i = 0; while (i < MENUDEPTH) pri_tab[i++] = 500; pri_tab[MENUDEPTH] = -1; /* mark end of the table */ /* * Check for "disable" or "enable" argument. */ if (STRNCMP(arg, "enable", 6) == 0 && vim_iswhite(arg[6])) { enable = TRUE; arg = skipwhite(arg + 6); } else if (STRNCMP(arg, "disable", 7) == 0 && vim_iswhite(arg[7])) { enable = FALSE; arg = skipwhite(arg + 7); } /* * If there is no argument, display all menus. */ if (*arg == NUL) { show_menus(arg, modes); return; } menu_path = arg; if (*menu_path == '.') { EMSG2(_(e_invarg2), menu_path); goto theend; } map_to = menu_translate_tab_and_shift(arg); /* * If there is only a menu name, display menus with that name. */ if (*map_to == NUL && !unmenu && enable == MAYBE) { show_menus(menu_path, modes); goto theend; } else if (*map_to != NUL && (unmenu || enable != MAYBE)) { EMSG(_(e_trailing)); goto theend; } if (enable != MAYBE) { /* * Change sensitivity of the menu. * For the PopUp menu, remove a menu for each mode separately. * Careful: menu_nable_recurse() changes menu_path. */ if (STRCMP(menu_path, "*") == 0) /* meaning: do all menus */ menu_path = (char_u *)""; if (menu_is_popup(menu_path)) { for (i = 0; i < MENU_INDEX_TIP; ++i) if (modes & (1 << i)) { p = popup_mode_name(menu_path, i); menu_nable_recurse(root_menu, p, MENU_ALL_MODES, enable); free(p); } } menu_nable_recurse(root_menu, menu_path, modes, enable); } else if (unmenu) { /* * Delete menu(s). */ if (STRCMP(menu_path, "*") == 0) /* meaning: remove all menus */ menu_path = (char_u *)""; /* * For the PopUp menu, remove a menu for each mode separately. */ if (menu_is_popup(menu_path)) { for (i = 0; i < MENU_INDEX_TIP; ++i) if (modes & (1 << i)) { p = popup_mode_name(menu_path, i); remove_menu(&root_menu, p, MENU_ALL_MODES, TRUE); free(p); } } /* Careful: remove_menu() changes menu_path */ remove_menu(&root_menu, menu_path, modes, FALSE); } else { /* * Add menu(s). * Replace special key codes. */ if (STRICMP(map_to, "<nop>") == 0) { /* "<Nop>" means nothing */ map_to = (char_u *)""; map_buf = NULL; } else if (modes & MENU_TIP_MODE) map_buf = NULL; /* Menu tips are plain text. */ else map_to = replace_termcodes(map_to, &map_buf, FALSE, TRUE, special); menuarg.modes = modes; menuarg.noremap[0] = noremap; menuarg.silent[0] = silent; add_menu_path(menu_path, &menuarg, pri_tab, map_to ); /* * For the PopUp menu, add a menu for each mode separately. */ if (menu_is_popup(menu_path)) { for (i = 0; i < MENU_INDEX_TIP; ++i) if (modes & (1 << i)) { p = popup_mode_name(menu_path, i); // Include all modes, to make ":amenu" work menuarg.modes = modes; add_menu_path(p, &menuarg, pri_tab, map_to); free(p); } } free(map_buf); } theend: ; }