Blob
Date:
Sun Apr 24 12:24:18 2022
UTC
Message:
replace has_prefix with strncmp
/** Copyright (c) 2021 Omar Polo <op@omarpolo.com>** Permission to use, copy, modify, and distribute this software for any* purpose with or without fee is hereby granted, provided that the above* copyright notice and this permission notice appear in all copies.** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.*/#include "compat.h"#include <limits.h>#include <stdlib.h>#include <string.h>#include "compl.h"#include "defaults.h"#include "mcache.h"#include "minibuffer.h"#include "session.h"#include "telescope.h"#include "ui.h"#include "utf8.h"#include "utils.h"#define GUARD_RECURSIVE_MINIBUFFER() \do { \if (in_minibuffer) { \message("enable-recursive-minibuffers " \"is not yet available."); \return; \} \} while(0)#define GUARD_READ_ONLY() \do { \if (!in_minibuffer) { \message("text is read-only"); \return; \} \} while(0)/* return 1 if moved, 0 otherwise */static inline intforward_line(struct buffer *buffer, int n){struct vline *vl;int did;if (buffer->current_line == NULL)return 0;vl = buffer->current_line;did = 0;while (n != 0) {if (n > 0) {vl = TAILQ_NEXT(vl, vlines);if (vl == NULL)return did;if (vl->parent->flags & L_HIDDEN)continue;buffer->current_line = vl;n--;} else {vl = TAILQ_PREV(vl, vhead, vlines);if (vl == NULL)return did;if (vl->parent->flags & L_HIDDEN)continue;if (buffer->current_line == buffer->top_line) {buffer->line_off--;buffer->top_line = vl;}buffer->current_line = vl;n++;}did = 1;}return did;}voidcmd_previous_line(struct buffer *buffer){forward_line(buffer, -1);}voidcmd_next_line(struct buffer *buffer){forward_line(buffer, +1);}voidcmd_backward_char(struct buffer *buffer){if (buffer->cpoff != 0)buffer->cpoff--;}voidcmd_forward_char(struct buffer *buffer){size_t len = 0;if (buffer->current_line == NULL)return;if (buffer->current_line->line != NULL)len = utf8_cplen(buffer->current_line->line);if (++buffer->cpoff > len)buffer->cpoff = len;}voidcmd_backward_paragraph(struct buffer *buffer){do {if (!forward_line(buffer, -1)) {message("No previous paragraph");return;}} while (buffer->current_line->line != NULL ||buffer->current_line->parent->type != LINE_TEXT);}voidcmd_forward_paragraph(struct buffer *buffer){do {if (!forward_line(buffer, +1)) {message("No next paragraph");return;}} while (buffer->current_line->line != NULL ||buffer->current_line->parent->type != LINE_TEXT);}voidcmd_move_beginning_of_line(struct buffer *buffer){buffer->cpoff = 0;}voidcmd_move_end_of_line(struct buffer *buffer){struct vline *vl;vl = buffer->current_line;if (vl == NULL || vl->line == NULL)return;buffer->cpoff = utf8_cplen(vl->line);}voidcmd_redraw(struct buffer *buffer){ui_schedule_redraw();}voidcmd_scroll_line_up(struct buffer *buffer){struct vline *vl;for (;;) {if (buffer->top_line == NULL)return;if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))== NULL)return;buffer->top_line = vl;if (vl->parent->flags & L_HIDDEN)continue;break;}buffer->line_off--;forward_line(buffer, -1);}voidcmd_scroll_line_down(struct buffer *buffer){if (!forward_line(buffer, +1))return;for (;;) {if (buffer->top_line == NULL)return;buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);if (buffer->top_line->parent->flags & L_HIDDEN)continue;break;}buffer->line_off++;}voidcmd_scroll_up(struct buffer *buffer){struct vline *vl;int i;if (buffer->top_line == NULL)return;for (i = 0; i < body_lines; ++i) {vl = TAILQ_PREV(buffer->top_line, vhead, vlines);if (vl == NULL)break;buffer->line_off--;buffer->top_line = vl;forward_line(buffer, -1);}}voidcmd_scroll_down(struct buffer *buffer){int i;if (buffer->top_line == NULL)return;for (i = 0; i < body_lines; ++i) {if (!forward_line(buffer, +1))break;buffer->top_line = TAILQ_NEXT(buffer->top_line,vlines);buffer->line_off++;}}voidcmd_beginning_of_buffer(struct buffer *buffer){buffer->current_line = TAILQ_FIRST(&buffer->head);buffer->cpoff = 0;buffer->top_line = buffer->current_line;buffer->line_off = 0;}voidcmd_end_of_buffer(struct buffer *buffer){buffer->current_line = TAILQ_LAST(&buffer->head, vhead);if (buffer->current_line == NULL)return;/* deal with invisible lines */if (buffer->current_line->parent->flags & L_HIDDEN)forward_line(buffer, -1);cmd_move_end_of_line(buffer);}static voidkill_telescope_cb(int r, struct tab *tab){if (r) {save_session();event_loopbreak();}}voidcmd_kill_telescope(struct buffer *buffer){yornp("really quit?", kill_telescope_cb, NULL);}voidcmd_push_button(struct buffer *buffer){struct vline *vl;struct line *l;vl = buffer->current_line;if (vl == NULL)return;switch (vl->parent->type) {case LINE_LINK:load_url_in_tab(current_tab, vl->parent->alt, NULL,LU_MODE_NOCACHE);break;case LINE_PRE_START:l = TAILQ_NEXT(vl->parent, lines);for (; l != NULL; l = TAILQ_NEXT(l, lines)) {if (l->type == LINE_PRE_END)break;l->flags ^= L_HIDDEN;if (l->flags & L_HIDDEN)buffer->line_max--;elsebuffer->line_max++;}break;default:break;}}voidcmd_push_button_new_tab(struct buffer *buffer){struct vline *vl;vl = buffer->current_line;if (vl == NULL || vl->parent->type != LINE_LINK)return;new_tab(vl->parent->alt, current_tab->hist_cur->h, current_tab);}voidcmd_previous_button(struct buffer *buffer){struct excursion place;save_excursion(&place, buffer);do {if (!forward_line(buffer, -1)) {restore_excursion(&place, buffer);message("No previous link");return;}} while (buffer->current_line->parent->type != LINE_LINK);}voidcmd_next_button(struct buffer *buffer){struct excursion place;save_excursion(&place, buffer);do {if (!forward_line(buffer, +1)){restore_excursion(&place, buffer);message("No next link");return;}} while (buffer->current_line->parent->type != LINE_LINK);}static inline intis_heading(const struct line *l){return l->type == LINE_TITLE_1 ||l->type == LINE_TITLE_2 ||l->type == LINE_TITLE_3;}voidcmd_previous_heading(struct buffer *buffer){struct excursion place;save_excursion(&place, buffer);do {if (!forward_line(buffer, -1)) {restore_excursion(&place, buffer);message("No previous heading");return;}} while (!is_heading(buffer->current_line->parent));}voidcmd_next_heading(struct buffer *buffer){struct excursion place;save_excursion(&place, buffer);do {if (!forward_line(buffer, +1)) {restore_excursion(&place, buffer);message("No next heading");return;}} while (!is_heading(buffer->current_line->parent));}voidcmd_previous_page(struct buffer *buffer){if (!load_previous_page(current_tab))message("No previous page");}voidcmd_next_page(struct buffer *buffer){if (!load_next_page(current_tab))message("No next page");}voidcmd_clear_minibuf(struct buffer *buffer){message(NULL);}voidcmd_execute_extended_command(struct buffer *buffer){size_t len;GUARD_RECURSIVE_MINIBUFFER();enter_minibuffer(sensible_self_insert, eecmd_select, exit_minibuffer,&eecmd_history, compl_eecmd, NULL, 1);len = sizeof(ministate.prompt);strlcpy(ministate.prompt, "", len);if (thiskey.meta)strlcat(ministate.prompt, "M-", len);strlcat(ministate.prompt, ui_keyname(thiskey.key), len);if (thiskey.meta)strlcat(ministate.prompt, " ", len);}voidcmd_tab_close(struct buffer *buffer){struct tab *tab, *t;tab = current_tab;if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||(t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {switch_to_tab(t);kill_tab(tab, 0);} elsemessage("Can't close the only tab.");}voidcmd_tab_close_other(struct buffer *buffer){struct tab *t, *i;TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {if (t == current_tab)continue;kill_tab(t, 0);}}voidcmd_tab_undo_close(struct buffer *buffer){struct tab *t;if ((t = unkill_tab()) == NULL) {message("No recently-closed tabs");return;}switch_to_tab(t);}voidcmd_tab_new(struct buffer *buffer){const char *url;if ((url = new_tab_url) == NULL)url = NEW_TAB_URL;new_tab(url, NULL, NULL);}voidcmd_tab_next(struct buffer *buffer){struct tab *t;if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)t = TAILQ_FIRST(&tabshead);switch_to_tab(t);}voidcmd_tab_previous(struct buffer *buffer){struct tab *t;if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)t = TAILQ_LAST(&tabshead, tabshead);switch_to_tab(t);}voidcmd_tab_move(struct buffer *buffer){struct tab *t;t = TAILQ_NEXT(current_tab, tabs);TAILQ_REMOVE(&tabshead, current_tab, tabs);if (t == NULL)TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);elseTAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);}voidcmd_tab_move_to(struct buffer *buffer){struct tab *t;t = TAILQ_PREV(current_tab, tabshead, tabs);TAILQ_REMOVE(&tabshead, current_tab, tabs);if (t == NULL)TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);elseTAILQ_INSERT_BEFORE(t, current_tab, tabs);}voidcmd_tab_select(struct buffer *buffer){GUARD_RECURSIVE_MINIBUFFER();enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,NULL, compl_ts, NULL, 1);strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));}voidcmd_load_url(struct buffer *buffer){GUARD_RECURSIVE_MINIBUFFER();enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,&lu_history, compl_lu, NULL, 0);strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));}voidcmd_load_current_url(struct buffer *buffer){GUARD_RECURSIVE_MINIBUFFER();enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,&lu_history, compl_lu, NULL, 0);strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));ministate.buffer.cpoff = utf8_cplen(ministate.buf);}voidcmd_reload_page(struct buffer *buffer){load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL,LU_MODE_NOHIST|LU_MODE_NOCACHE);}voidcmd_bookmark_page(struct buffer *buffer){GUARD_RECURSIVE_MINIBUFFER();enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,NULL, NULL, 0);strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));ministate.buffer.cpoff = utf8_cplen(ministate.buf);}voidcmd_list_bookmarks(struct buffer *buffer){load_url_in_tab(current_tab, "about:bookmarks", NULL, LU_MODE_NONE);}voidcmd_toggle_help(struct buffer *buffer){ui_toggle_side_window(SIDE_WINDOW_LEFT);}voidcmd_link_select(struct buffer *buffer){struct line *l;GUARD_RECURSIVE_MINIBUFFER();l = TAILQ_FIRST(&buffer->page.head);while (l != NULL && l->type != LINE_LINK)l = TAILQ_NEXT(l, lines);if (l == NULL) {message("No links found");return;}enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,NULL, compl_ls, l, 1);strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));}voidcmd_swiper(struct buffer *buffer){GUARD_RECURSIVE_MINIBUFFER();enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head), 1);strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));}voidcmd_toc(struct buffer *buffer){struct line *l;GUARD_RECURSIVE_MINIBUFFER();l = TAILQ_FIRST(&buffer->page.head);while (l != NULL &&l->type != LINE_TITLE_1 &&l->type != LINE_TITLE_2 &&l->type != LINE_TITLE_3)l = TAILQ_NEXT(l, lines);if (l == NULL) {message("No headings found");return;}enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,NULL, compl_toc, l, 1);strlcpy(ministate.prompt, "Select heading: ",sizeof(ministate.prompt));}voidcmd_inc_fill_column(struct buffer *buffer){if (fill_column == INT_MAX)return;fill_column += 2;message("fill-column: %d", fill_column);ui_schedule_redraw();}voidcmd_dec_fill_column(struct buffer *buffer){if (fill_column == INT_MAX || fill_column < 8)return;fill_column -= 2;message("fill-column: %d", fill_column);ui_schedule_redraw();}voidcmd_olivetti_mode(struct buffer *buffer){olivetti_mode = !olivetti_mode;if (olivetti_mode)message("olivetti-mode enabled");elsemessage("olivetti-mode disabled");ui_schedule_redraw();}voidcmd_mini_delete_char(struct buffer *buffer){char *c, *n;GUARD_READ_ONLY();minibuffer_taint_hist();c = utf8_nth(buffer->current_line->line, buffer->cpoff);if (*c == '\0')return;n = utf8_next_cp(c);memmove(c, n, strlen(n)+1);recompute_completions(0);}voidcmd_mini_delete_backward_char(struct buffer *buffer){char *c, *p, *start;GUARD_READ_ONLY();minibuffer_taint_hist();c = utf8_nth(buffer->current_line->line, buffer->cpoff);start = buffer->current_line->line;if (c == start)return;p = utf8_prev_cp(c-1, start);memmove(p, c, strlen(c)+1);buffer->cpoff--;recompute_completions(0);}voidcmd_mini_kill_line(struct buffer *buffer){char *c;GUARD_READ_ONLY();minibuffer_taint_hist();c = utf8_nth(buffer->current_line->line, buffer->cpoff);*c = '\0';recompute_completions(0);}voidcmd_mini_kill_whole_line(struct buffer *buffer){GUARD_READ_ONLY();minibuffer_taint_hist();*buffer->current_line->line = '\0';buffer->cpoff = 0;}voidcmd_mini_abort(struct buffer *buffer){if (!in_minibuffer)return;ministate.abortfn();}voidcmd_mini_complete_and_exit(struct buffer *buffer){struct vline *vl;if (!in_minibuffer)return;if (ministate.compl.must_select && ministate.hist_cur == NULL) {vl = ministate.compl.buffer.current_line;if (vl == NULL || vl->parent->flags & L_HIDDEN ||vl->parent->type == LINE_COMPL) {message("no match");return;}}minibuffer_taint_hist();ministate.donefn();}voidcmd_mini_previous_history_element(struct buffer *buffer){if (ministate.history == NULL) {message("No history");return;}if (ministate.hist_cur == NULL ||(ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);ministate.hist_off = ministate.history->len - 1;if (ministate.hist_cur == NULL)message("No prev history item");} else {ministate.hist_off--;}if (ministate.hist_cur != NULL) {buffer->current_line->line = ministate.hist_cur->h;recompute_completions(0);}}voidcmd_mini_next_history_element(struct buffer *buffer){if (ministate.history == NULL) {message("No history");return;}if (ministate.hist_cur == NULL ||(ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);ministate.hist_off = 0;if (ministate.hist_cur == NULL)message("No next history item");} else {ministate.hist_off++;}if (ministate.hist_cur != NULL) {buffer->current_line->line = ministate.hist_cur->h;recompute_completions(0);}}voidcmd_previous_completion(struct buffer *buffer){if (in_minibuffer != MB_COMPREAD)return;buffer = &ministate.compl.buffer;if (buffer->current_line == NULL)return;buffer->current_line->parent->type = LINE_COMPL;if (!forward_line(buffer, -1))buffer->current_line->parent->type = LINE_COMPL;elsebuffer->current_line->parent->type = LINE_COMPL_CURRENT;}voidcmd_next_completion(struct buffer *buffer){if (in_minibuffer != MB_COMPREAD)return;buffer = &ministate.compl.buffer;if (buffer->current_line == NULL)return;if (buffer->current_line->parent->type == LINE_COMPL_CURRENT) {buffer->current_line->parent->type = LINE_COMPL;forward_line(buffer, +1);}if (buffer->current_line != NULL)buffer->current_line->parent->type = LINE_COMPL_CURRENT;}voidcmd_insert_current_candidate(struct buffer *buffer){if (in_minibuffer != MB_COMPREAD)return;minibuffer_insert_current_candidate();}voidcmd_suspend_telescope(struct buffer *buffer){message("Zzz...");ui_suspend();}voidcmd_toggle_pre_wrap(struct buffer *buffer){dont_wrap_pre = !dont_wrap_pre;if (dont_wrap_pre)message("Don't wrap preformatted blocks");elsemessage("Wrap preformatted blocks");ui_schedule_redraw();}voidcmd_mini_goto_beginning(struct buffer *buffer){struct vline *vl;if (!in_minibuffer)return;buffer = &ministate.compl.buffer;if ((vl = buffer->current_line) != NULL)vl->parent->type = LINE_COMPL;vl = TAILQ_FIRST(&buffer->head);while (vl != NULL && vl->parent->flags & L_HIDDEN)vl = TAILQ_NEXT(vl, vlines);if (vl == NULL)return;vl->parent->type = LINE_COMPL_CURRENT;buffer->top_line = vl;buffer->current_line = vl;}voidcmd_mini_goto_end(struct buffer *buffer){struct vline *vl;if (!in_minibuffer)return;buffer = &ministate.compl.buffer;if ((vl = buffer->current_line) != NULL)vl->parent->type = LINE_COMPL;vl = TAILQ_LAST(&buffer->head, vhead);while (vl != NULL && vl->parent->flags & L_HIDDEN)vl = TAILQ_PREV(vl, vhead, vlines);if (vl == NULL)return;vl->parent->type = LINE_COMPL_CURRENT;buffer->current_line = vl;}voidcmd_other_window(struct buffer *buffer){ui_other_window();}voidcmd_mini_scroll_up(struct buffer *buffer){if (!in_minibuffer)return;buffer = &ministate.compl.buffer;if (buffer->current_line == NULL)return;buffer->current_line->parent->type = LINE_COMPL;cmd_scroll_up(buffer);buffer->current_line->parent->type = LINE_COMPL_CURRENT;}voidcmd_mini_scroll_down(struct buffer *buffer){if (!in_minibuffer)return;buffer = &ministate.compl.buffer;if (buffer->current_line == NULL)return;buffer->current_line->parent->type = LINE_COMPL;cmd_scroll_down(buffer);buffer->current_line->parent->type = LINE_COMPL_CURRENT;}voidcmd_toggle_downloads(struct buffer *buffer){ui_toggle_side_window(SIDE_WINDOW_BOTTOM);}voidcmd_cache_info(struct buffer *buffer){size_t npages, tot;char fmt[FMT_SCALED_STRSIZE];mcache_info(&npages, &tot);if (fmt_scaled(tot, fmt) == 0)message("pages: %zu, total: %s", npages, fmt);elsemessage("pages: %zu, total: %zu", npages, tot);}voidcmd_reply_last_input(struct buffer *buffer){GUARD_RECURSIVE_MINIBUFFER();if (current_tab->last_input_url == NULL) {message("there was no previous input request in this tab");return;}if (!strncmp(current_tab->last_input_url, "gopher", 6)) {load_url_in_tab(current_tab, current_tab->last_input_url,NULL, LU_MODE_NOCACHE);return;}message("%s", current_tab->last_input_url);ui_require_input(current_tab, 0, ir_select_reply);}voidcmd_write_buffer(struct buffer *buffer){const char *f, *url;char path[PATH_MAX];GUARD_RECURSIVE_MINIBUFFER();if (safe_mode) {message("Can't write buffer in safe-mode.");return;}url = current_tab->hist_cur->h;if ((f = strrchr(url, '/')) != NULL)f++;if (f == NULL || *f == '\0') {/* guess a decent file name based on the protocol used */if (!strncmp(url, "gemini://", 9))f = "index.gmi";elsef = "index.txt";}strlcpy(path, download_path, sizeof(path));strlcat(path, f, sizeof(path));ui_read("Write file", write_buffer, current_tab, path);}
Omar Polo