From ee690b2895bd340bc7f69ff7089beca46cb543c7 Mon Sep 17 00:00:00 2001 From: Charlie Gordon Date: Wed, 19 Mar 2014 19:42:30 +0000 Subject: [PATCH] add support for web protocols: ftp, http, https * add wget-mode to retrieve web contents via find-file * add shell flags for buffer mode change upon download completion * fix do_electric_filename to keep http:// prefix and such --- archive.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++------ qe.c | 38 ++++++++++++++----- qe.h | 4 ++ shell.c | 10 ++++- tty.c | 5 ++- 5 files changed, 140 insertions(+), 24 deletions(-) diff --git a/archive.c b/archive.c index b25d147..4e882a2 100644 --- a/archive.c +++ b/archive.c @@ -47,7 +47,7 @@ static ArchiveType *archive_types; /* Compressors */ typedef struct CompressType { - const char *name; /* name of archive format */ + const char *name; /* name of compressed format */ const char *extensions; const char *load_cmd; /* uncompress file to stdout */ const char *save_cmd; /* compress to file from stdin */ @@ -86,8 +86,15 @@ static int archive_mode_probe(ModeDef *mode, ModeProbeData *p) { ArchiveType *atp = find_archive_type(p->filename); - if (atp) - return 70; + if (atp) { + if (p->b && p->b->priv_data) { + /* buffer loaded, re-selecting mode causes buffer reload */ + return 9; + } else { + /* buffer not yet loaded */ + return 70; + } + } return 0; } @@ -117,7 +124,7 @@ static int archive_buffer_load(EditBuffer *b, FILE *f) atp->name, b->filename); snprintf(cmd, sizeof(cmd), atp->list_cmd, b->filename); new_shell_buffer(b, get_basename(b->filename), NULL, cmd, - SF_INFINITE); + SF_INFINITE | SF_BUFED_MODE); /* XXX: should check for archiver error */ /* XXX: should delay BF_SAVELOG until buffer is fully loaded */ @@ -154,7 +161,7 @@ static int archive_init(void) { int i; - /* archive mode is almost like the text mode, so we copy and patch it */ + /* copy and patch text_mode */ memcpy(&archive_mode, &text_mode, sizeof(ModeDef)); archive_mode.name = "archive"; archive_mode.mode_probe = archive_mode_probe; @@ -193,8 +200,15 @@ static int compress_mode_probe(ModeDef *mode, ModeProbeData *p) { CompressType *ctp = find_compress_type(p->filename); - if (ctp) - return 60; + if (ctp) { + if (p->b && p->b->priv_data) { + /* buffer loaded, re-selecting mode causes buffer reload */ + return 9; + } else { + /* buffer not yet loaded */ + return 60; + } + } return 0; } @@ -213,7 +227,7 @@ static ModeDef compress_mode; static int compress_buffer_load(EditBuffer *b, FILE *f) { - /* Launch subprocess to list compress contents */ + /* Launch subprocess to expand compressed contents */ char cmd[1024]; CompressType *ctp; @@ -222,7 +236,7 @@ static int compress_buffer_load(EditBuffer *b, FILE *f) eb_clear(b); snprintf(cmd, sizeof(cmd), ctp->load_cmd, b->filename); new_shell_buffer(b, get_basename(b->filename), NULL, cmd, - SF_INFINITE); + SF_INFINITE | SF_AUTO_CODING | SF_AUTO_MODE); /* XXX: should check for archiver error */ /* XXX: should delay BF_SAVELOG until buffer is fully loaded */ b->flags |= BF_READONLY; @@ -258,7 +272,7 @@ static int compress_init(void) { int i; - /* compress mode is almost like the text mode, so we copy and patch it */ + /* copy and patch text_mode */ memcpy(&compress_mode, &text_mode, sizeof(ModeDef)); compress_mode.name = "compress"; compress_mode.mode_probe = compress_mode_probe; @@ -277,11 +291,82 @@ static int compress_init(void) return 0; } +/*---------------- Wget ----------------*/ + +static ModeDef wget_mode; + +static int wget_mode_probe(ModeDef *mode, ModeProbeData *p) +{ + if (strstart(p->real_filename, "http:", NULL) + || strstart(p->real_filename, "https:", NULL) + || strstart(p->real_filename, "ftp:", NULL)) { + if (p->b && p->b->priv_data) { + /* buffer loaded, re-selecting mode causes buffer reload */ + return 9; + } else { + /* buffer not yet loaded */ + return 90; + } + } + + return 0; +} + +static int wget_buffer_load(EditBuffer *b, FILE *f) +{ + /* Launch wget subprocess to retrieve contents */ + char cmd[1024]; + + eb_clear(b); + snprintf(cmd, sizeof(cmd), "wget -q -O - %s", b->filename); + new_shell_buffer(b, get_basename(b->filename), NULL, cmd, + SF_INFINITE | SF_AUTO_CODING | SF_AUTO_MODE); + /* XXX: should check for wget error */ + /* XXX: should delay BF_SAVELOG until buffer is fully loaded */ + b->flags |= BF_READONLY; + + return 0; +} + +static int wget_buffer_save(EditBuffer *b, int start, int end, + const char *filename) +{ + /* XXX: should put contents back to web server */ + return -1; +} + +static void wget_buffer_close(EditBuffer *b) +{ + /* XXX: kill process? */ +} + +static EditBufferDataType wget_data_type = { + "wget", + wget_buffer_load, + wget_buffer_save, + wget_buffer_close, + NULL, /* next */ +}; + +static int wget_init(void) +{ + /* copy and patch text_mode */ + memcpy(&wget_mode, &text_mode, sizeof(ModeDef)); + wget_mode.name = "wget"; + wget_mode.mode_probe = wget_mode_probe; + wget_mode.data_type = &wget_data_type; + + eb_register_data_type(&wget_data_type); + qe_register_mode(&wget_mode); + + return 0; +} + /*---------------- Initialization ----------------*/ static int archive_compress_init(void) { - return archive_init() || compress_init(); + return archive_init() || compress_init() || wget_init(); } qe_module_init(archive_compress_init); diff --git a/qe.c b/qe.c index 718fe3f..fb89bd0 100644 --- a/qe.c +++ b/qe.c @@ -1686,7 +1686,7 @@ static int reload_buffer(EditState *s, EditBuffer *b, FILE *f1) if (b->filename[0] == '\0') return 0; - if (!f1) { + if (!f1 && b->data_type == &raw_data_type) { struct stat st; if (stat(b->filename, &st) < 0 || !S_ISREG(st.st_mode)) @@ -5089,14 +5089,33 @@ void do_completion(EditState *s) complete_end(&cs); } +static int eb_match_string_reverse(EditBuffer *b, int offset, const char *str, + int *offsetp) +{ + int len = strlen(str); + + while (len > 0) { + if (offset <= 0 || eb_prevc(b, offset, &offset) != str[--len]) + return 0; + } + *offsetp = offset; + return 1; +} + void do_electric_filename(EditState *s, int key) { - int c, offset; + int c, offset, stop; if (completion_function == file_completion) { + stop = s->offset; c = eb_prevc(s->b, s->offset, &offset); - if (c == '/') - eb_delete(s->b, 0, s->offset); + if (c == '/') { + if (eb_match_string_reverse(s->b, offset, "http:", &stop) + || eb_match_string_reverse(s->b, offset, "https:", &stop) + || eb_match_string_reverse(s->b, offset, "ftp:", &stop)) + ; + eb_delete(s->b, 0, stop); + } } do_char(s, key, 1); } @@ -5793,12 +5812,12 @@ static void do_load1(EditState *s, const char *filename1, /* First we try to read the first block to determine the data type */ if (stat(filename, &st) < 0) { - /* XXX: default charset should be selectable. Use utf8 for now */ + /* XXX: default charset should be selectable. Should have auto + * charset transparent support for both utf8 and latin1. + * Use utf8 for now */ eb_set_charset(b, &charset_utf8, b->eol_type); /* XXX: dired_mode_probe will check for wildcards in real_filename */ - put_status(s, "(New file)"); - /* Try to determine the desired mode based on the filename. - * This avoids having to set c-mode for each new .c or .h file. */ + /* Try to determine the desired mode based on the filename. */ b->st_mode = st_mode = S_IFREG; buf[0] = '\0'; buf_size = 0; @@ -5811,6 +5830,8 @@ static void do_load1(EditState *s, const char *filename1, */ b->default_mode = selected_mode; switch_to_buffer(s, b); + if (b->data_type == &raw_data_type) + put_status(s, "(New file)"); do_load_qerc(s, s->b->filename); return; } else { @@ -5818,7 +5839,6 @@ static void do_load1(EditState *s, const char *filename1, buf_size = 0; f = NULL; - /* CG: should check for ISDIR and do dired */ if (S_ISREG(st_mode)) { f = fopen(filename, "r"); if (!f) diff --git a/qe.h b/qe.h index 8a6aa6d..6535919 100644 --- a/qe.h +++ b/qe.h @@ -1695,6 +1695,7 @@ void edit_display(QEmacsState *qs); void edit_invalidate(EditState *s); void display_mode_line(EditState *s); void edit_set_mode(EditState *s, ModeDef *m); +void do_set_next_mode(EditState *s, int dir); /* loading files */ void do_exit_qemacs(EditState *s, int argval); @@ -1971,6 +1972,9 @@ const char *get_shell(void); #define SF_INTERACTIVE 0x01 #define SF_COLOR 0x02 #define SF_INFINITE 0x04 +#define SF_AUTO_CODING 0x08 +#define SF_AUTO_MODE 0x10 +#define SF_BUFED_MODE 0x20 EditBuffer *new_shell_buffer(EditBuffer *b0, const char *bufname, const char *caption, const char *cmd, int shell_flags); diff --git a/shell.c b/shell.c index 94e0cf4..d3fc233 100644 --- a/shell.c +++ b/shell.c @@ -1367,6 +1367,10 @@ static void shell_pid_cb(void *opaque, int status) for (e = qs->first_window; e != NULL; e = e->next_window) { if (e->b == b) e->interactive = 0; + if (s->shell_flags & SF_AUTO_CODING) + do_set_auto_coding(e, 0); + if (s->shell_flags & SF_AUTO_MODE) + do_set_next_mode(e, 0); } if (!(s->shell_flags & SF_INTERACTIVE)) { shell_close(b); @@ -1935,8 +1939,10 @@ static int shell_mode_probe(ModeDef *mode, ModeProbeData *p) { if (p->b && p->b->priv_data) { ShellState *s = p->b->priv_data; - if (s->signature == &shell_signature) - return 100; + if (s->signature == &shell_signature) { + if (s->shell_flags & SF_INTERACTIVE) + return 100; + } } return 0; } diff --git a/tty.c b/tty.c index a3cc89e..7ce90ce 100644 --- a/tty.c +++ b/tty.c @@ -251,7 +251,7 @@ static void tty_term_close(QEditScreen *s) s->height, 1); #else /* go to last line and clear it */ - TTY_FPRINTF(s->STDOUT, "\033[%d;%dH\033[m\033[K", s->height, 1); + TTY_FPRINTF(s->STDOUT, "\033[%d;%dH" "\033[m\033[K", s->height, 1); TTY_FPRINTF(s->STDOUT, "\033[?1049l" /* exit_ca_mode */ "\r" /* return */ @@ -1291,7 +1291,8 @@ static void tty_term_flush(QEditScreen *s) TTY_FPUTS("\033[0m", s->STDOUT); if (ts->cursor_y + 1 >= 0 && ts->cursor_x + 1 >= 0) { - TTY_FPRINTF(s->STDOUT, "\033[%d;%dH", ts->cursor_y + 1, ts->cursor_x + 1); + TTY_FPRINTF(s->STDOUT, "\033[%d;%dH", + ts->cursor_y + 1, ts->cursor_x + 1); } fflush(s->STDOUT); }