Compare commits

...

5 Commits

Author SHA1 Message Date
c8ff246cc2 remove extraneous tmp vars in libre_redirect 2024-03-23 16:58:23 -03:00
43cac7f3f1 simplify libre_redirect code 2024-03-23 16:46:28 -03:00
8f4b4b9abf formatting/syntax tweaks
in particular, for switch cases which declare const, use a new block
2024-03-23 16:26:26 -03:00
f2e06785f9 Add https automatically if url contains .com or .org
also delete extraneous shortcut
2024-03-23 16:18:00 -03:00
b4c3e3a0e3 add anna's archive shortcut 2024-03-23 11:14:29 -03:00
4 changed files with 35 additions and 39 deletions

View File

@ -1,5 +1,7 @@
# To do
- [ ] Settle on a C standard (C11?), and use safer string handling functions provided by it.
- See make lint for purported insecurities
- [ ] Document creating new applications, e.g., as in [Asana for Linux](https://git.nunosempere.com/NunoSempere/asana-for-linux)
- [ ] This time, use something other than Whatsapp as an example syslink.
- [ ] Fix bug about distorted audio. Maybe related to [this pipewire issue](<https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/1547>)?

View File

@ -7,28 +7,17 @@
#define LIBRE_N 50
/* Uncomment for debug */
/* #define DEBUG */
/* Inspired by https://libredirect.github.io/, but in C. */
int libre_redirect(const char* uri, char* output)
{
int l1 = strlen(uri);
int l2 = strlen(output);
int len;
char tmp_uri[l2++];
char tmp_output[l2++];
int len_uri = strlen(uri);
int len_output = strlen(output);
if ((l2 - l1) < LIBRE_N) {
#ifdef DEBUG
if ((len_output - len_uri) < LIBRE_N) {
printf("Not enough memory\n");
#endif
return 1; // not enough memory.
} else {
strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0'
strcpy(tmp_output, output);
char* annoying_sites[] = {
"https://www.reddit.com",
"https://www.youtube.com",
@ -55,30 +44,26 @@ int libre_redirect(const char* uri, char* output)
// "https://nitter.net"
};
len = sizeof(annoying_sites) / sizeof(annoying_sites[0]);
int len = sizeof(annoying_sites) / sizeof(annoying_sites[0]);
for (int i = 0; i < len; i++) {
int replace_check = str_replace_start(tmp_uri, annoying_sites[i],
str_init(output, len_output);
int replace_check = str_replace_start(uri, annoying_sites[i],
alternatives[i], output);
if (replace_check == 2) {
#ifdef DEBUG
printf("tmp_uri: %s\n", tmp_uri);
printf("output: %s\n", output);
#endif
return 2;
} else if (replace_check == 1) {
#ifdef DEBUG
printf("replace_check failed\n");
#endif
return 1;
switch(replace_check){
case 0: // no match found
break;
case 1: // str_replace_start somehow failed
printf("str_replace_start failed\n");
return 1;
break;
case 2: // match succeeded
return 2;
break;
default:
printf("Unreachable state");
}
strcpy(tmp_uri, output);
str_init(output, l2);
}
strcpy(output, tmp_uri);
strcpy(output, uri);
}
#ifdef DEBUG
printf("No match found\n\n");
#endif
return 0;
}

View File

@ -31,21 +31,23 @@ int shortcut_expand(const char* uri, char* output)
strcpy(tmp_output, output);
char* shortcuts[] = {
"!x",
"!aa",
"!blog",
"!fnf",
"!fnc",
"!hn",
"!hnb"
"!x",
};
char* expansions[] = {
"https://twitter.com",
"https://annas-archive.org",
"https://nunosempere.com/blog",
"https://forum.nunosempere.com/frontpage",
"https://forum.nunosempere.com/comments",
"https://news.ycombinator.com",
"https://news.ycombinator.com/best",
"https://twitter.com",
};
// len = sizeof(shortcuts) / sizeof(shortcuts[0]);

View File

@ -37,6 +37,11 @@ void load_uri(WebKitWebView* view, const char* uri)
toggle_bar(notebook);
} else if (g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:")) {
webkit_web_view_load_uri(view, uri);
} else if (strstr(uri, ".com") || strstr(uri, ".org")) {
char tmp[strlen("https://") + strlen(uri)];
snprintf(tmp, sizeof(tmp), "https://%s", uri);
webkit_web_view_load_uri(view, tmp);
} else {
// Check for shortcuts
int l = SHORTCUT_N + strlen(uri) + 1;
@ -212,14 +217,15 @@ void notebook_create_new_tab(GtkNotebook* notebook, const char* uri)
void toggle_bar(GtkNotebook* notebook)
{
switch (bar.entry_mode) {
case _SEARCH:
case _SEARCH: {
const char* url = webkit_web_view_get_uri(notebook_get_webview(notebook));
gtk_entry_set_placeholder_text(bar.line, "Search");
gtk_entry_buffer_set_text(bar.line_text, url, strlen(url));
gtk_widget_show(GTK_WIDGET(bar.widget));
gtk_window_set_focus(window, GTK_WIDGET(bar.line));
break;
case _FIND:
}
case _FIND: {
const char* search_text = webkit_find_controller_get_search_text(
webkit_web_view_get_find_controller(notebook_get_webview(notebook)));
@ -230,6 +236,7 @@ void toggle_bar(GtkNotebook* notebook)
gtk_widget_show(GTK_WIDGET(bar.widget));
gtk_window_set_focus(window, GTK_WIDGET(bar.line));
break;
}
default:
// fallthrough
case _HIDDEN:
@ -398,7 +405,7 @@ int handle_signal_keypress(void* self, GdkEvent* event, GtkNotebook* notebook)
}
printf("Keypress value: %d\n", event_keyval);
printf("PageUp: %d %d\n", KEY(Page_Up), GDK_KEY_KP_Page_Up);
printf("PageDown: %d %d\n", KEY(Page_Down),GDK_KEY_KP_Page_Down);
printf("PageDown: %d %d\n", KEY(Page_Down), GDK_KEY_KP_Page_Down);
}
for (int i = 0; i < sizeof(shortcut) / sizeof(shortcut[0]); i++)