static bool argv_number_formatter(struct format_context *format, struct format_var *var) { unsigned long value = *(unsigned long *) var->value_ref; return string_format_from(format->buf, &format->bufpos, "%ld", value); }
static bool format_append_arg(struct format_context *format, const char ***dst_argv, const char *arg) { memset(format->buf, 0, sizeof(format->buf)); format->bufpos = 0; while (arg) { const char *var = strstr(arg, "%("); const char *closing = var ? strchr(var, ')') : NULL; const char *next = closing ? closing + 1 : NULL; const int len = var ? var - arg : strlen(arg); if (var && !closing) return FALSE; if (len && !string_format_from(format->buf, &format->bufpos, "%.*s", len, arg)) return FALSE; if (var && !format_expand_arg(format, var, next)) return FALSE; arg = next; } return argv_append(dst_argv, format->buf); }
static void add_pager_refs(struct view *view, const char *commit_id) { char buf[SIZEOF_STR]; const struct ref *list; size_t bufpos = 0; const char *sep = "Refs: "; list = get_ref_list(commit_id); if (!list) { if (view_has_flags(view, VIEW_ADD_DESCRIBE_REF) && refs_contain_tag()) add_line_text(view, sep, LINE_PP_REFS); return; } for (; list; list = list->next) { const struct ref *ref = list; const struct ref_format *fmt = get_ref_format(opt_reference_format, ref); if (!string_format_from(buf, &bufpos, "%s%s%s%s", sep, fmt->start, ref->name, fmt->end)) return; sep = ", "; } if (bufpos == 0) return; add_line_text(view, buf, LINE_PP_REFS); }
static bool bool_formatter(struct format_context *format, struct format_var *var) { bool value = *(bool *)var->value_ref; return string_format_from(format->buf, &format->bufpos, "%s", value ? "true" : "false"); }
static bool format_expand_arg(struct format_context *format, const char *name, const char *end) { struct format_var *vars = format->vars; int i; if (!prefixcmp(name, "%(prompt")) { const char *prompt = "Command argument: "; char msgbuf[SIZEOF_STR]; const char *value; const char *msgstart = name + STRING_SIZE("%(prompt"); int msglen = end - msgstart - 1; if (end && msglen > 0 && string_format(msgbuf, "%.*s", msglen, msgstart)) { const char *msg = msgbuf; while (isspace(*msg)) msg++; if (*msg) prompt = msg; } value = read_prompt(prompt); if (value == NULL) return FALSE; return string_format_from(format->buf, &format->bufpos, "%s", value); } for (i = 0; i < format->vars_size; i++) { const char *value; if (strncmp(name, vars[i].name, vars[i].namelen)) continue; if (vars[i].value == argv_env.file && !format->file_filter) return TRUE; value = *vars[i].value ? vars[i].value : vars[i].value_if_empty; if (!*value) return TRUE; return string_format_from(format->buf, &format->bufpos, "%s", value); } return FALSE; }
static bool argv_string_formatter(struct format_context *format, struct format_var *var) { argv_string *value_ref = var->value_ref; const char *value = *value_ref; if (!*value) value = var->value_if_empty; if (!*value) return TRUE; return string_format_from(format->buf, &format->bufpos, "%s", value); }
const char * get_key_name(const struct key key[], size_t keys, bool quote_comma) { static char buf[SIZEOF_STR]; size_t pos = 0; int i; for (i = 0; i < keys; i++) { bool multibytes = key[i].modifiers.multibytes; const char *name = multibytes ? key[i].data.bytes : ""; const char *start = ""; const char *end = ""; bool use_symbolic; if (key[i].modifiers.control) { start = "<Ctrl-"; end = ">"; } else if (*name == ',' && quote_comma) { /* Quote commas so they stand out in the help view. */ start = "'"; end = "'"; } /* Use symbolic name for spaces so they are readable. */ use_symbolic = !*name || *name == ' '; /* When listing keys for :save-options quote illegal characters. */ if (!quote_comma && (*name == '<' || *name == '#')) use_symbolic = true; if (use_symbolic) { int value = *name ? *name : key[i].data.value; int j; name = "<?>"; for (j = 0; j < ARRAY_SIZE(key_mappings); j++) if (key_mappings[j].value == value) { start = "<"; end = ">"; name = key_mappings[j].name; break; } } if (!string_format_from(buf, &pos, "%s%s%s", start, name, end)) return "(no key)"; } return buf; }
static void set_remote_branch(const char *name, const char *value, size_t valuelen) { if (!strcmp(name, ".remote")) { string_ncopy(repo.remote, value, valuelen); } else if (*repo.remote && !strcmp(name, ".merge")) { size_t from = strlen(repo.remote); if (!prefixcmp(value, "refs/heads/")) value += STRING_SIZE("refs/heads/"); if (!string_format_from(repo.remote, &from, "/%s", value)) repo.remote[0] = 0; } }
static void push_tree_stack_entry(struct view *view, const char *name, struct position *position) { size_t pathlen = strlen(view->env->directory); char *path_position = view->env->directory + pathlen; struct view_state *state = push_view_history_state(&tree_view_history, position, &path_position); if (!state) return; if (!string_format_from(view->env->directory, &pathlen, "%s/", name)) { pop_tree_stack_entry(NULL); return; } clear_position(position); }
static void add_pager_refs(struct view *view, const char *commit_id) { char buf[SIZEOF_STR]; struct ref_list *list; size_t bufpos = 0, i; const char *sep = "Refs: "; bool is_tag = FALSE; list = get_ref_list(commit_id); if (!list) { if (view_has_flags(view, VIEW_ADD_DESCRIBE_REF)) goto try_add_describe_ref; return; } for (i = 0; i < list->size; i++) { struct ref *ref = list->refs[i]; const char *fmt = ref->tag ? "%s[%s]" : ref->remote ? "%s<%s>" : "%s%s"; if (!string_format_from(buf, &bufpos, fmt, sep, ref->name)) return; sep = ", "; if (ref->tag) is_tag = TRUE; } if (!is_tag && view_has_flags(view, VIEW_ADD_DESCRIBE_REF)) { try_add_describe_ref: /* Add <tag>-g<commit_id> "fake" reference. */ if (!add_describe_ref(buf, &bufpos, commit_id, sep)) return; } if (bufpos == 0) return; add_line_text(view, buf, LINE_PP_REFS); }