diff --git a/bufed.c b/bufed.c index e11776c..329b8d5 100644 --- a/bufed.c +++ b/bufed.c @@ -1,8 +1,8 @@ /* * Buffer editor mode for QEmacs. * - * Copyright (c) 2001, 2002 Fabrice Bellard. - * Copyright (c) 2002-2013 Charlie Gordon. + * Copyright (c) 2001-2002 Fabrice Bellard. + * Copyright (c) 2002-2014 Charlie Gordon. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -69,17 +69,17 @@ static void build_bufed_list(EditState *s) eb_printf(b, " %-2s%-16s", flags, hs->items.items[i]->str); if (b1) { char path[MAX_FILENAME_SIZE]; - const char *mode; + const char *mode_name; EditState *e; if (b1->saved_data) { - mode = b1->saved_data->mode->name; + mode_name = b1->saved_data->mode->name; } else { - mode = "none"; + mode_name = "none"; for (e = qs->first_window; e != NULL; e = e->next_window) { if (e->b == b1) { if (e->mode) { - mode = e->mode->name; + mode_name = e->mode->name; break; } } @@ -87,7 +87,7 @@ static void build_bufed_list(EditState *s) } eb_printf(b, " %10d %-8s %-8s %s", - b1->total_size, b1->charset->name, mode, + b1->total_size, b1->charset->name, mode_name, make_user_path(path, sizeof(path), b1->filename)); } eb_printf(b, "\n"); diff --git a/buffer.c b/buffer.c index 0e2deae..c79c4b7 100644 --- a/buffer.c +++ b/buffer.c @@ -1660,7 +1660,7 @@ int eb_write_buffer(EditBuffer *b, int start, int end, const char *filename) */ int eb_save_buffer(EditBuffer *b) { - int ret, mode; + int ret, st_mode; char buf1[MAX_FILENAME_SIZE]; const char *filename; struct stat st; @@ -1670,9 +1670,9 @@ int eb_save_buffer(EditBuffer *b) filename = b->filename; /* get old file permission */ - mode = 0644; + st_mode = 0644; if (stat(filename, &st) == 0) - mode = st.st_mode & 0777; + st_mode = st.st_mode & 0777; /* backup old file if present */ if (strlen(filename) < MAX_FILENAME_SIZE - 1) { @@ -1682,14 +1682,14 @@ int eb_save_buffer(EditBuffer *b) } } - /* CG: should pass mode to buffer_save */ + /* CG: should pass st_mode to buffer_save */ ret = b->data_type->buffer_save(b, 0, b->total_size, filename); if (ret < 0) return ret; #ifndef CONFIG_WIN32 - /* set correct file mode to old file permissions */ - chmod(filename, mode); + /* set correct file st_mode to old file permissions */ + chmod(filename, st_mode); #endif /* reset log */ /* CG: should not do this! */ diff --git a/clang.c b/clang.c index 78c5420..07ebbd3 100644 --- a/clang.c +++ b/clang.c @@ -1,8 +1,8 @@ /* * C mode for QEmacs. * - * Copyright (c) 2001, 2002 Fabrice Bellard. - * Copyright (c) 2002-2008 Charlie Gordon. + * Copyright (c) 2001-2002 Fabrice Bellard. + * Copyright (c) 2002-2014 Charlie Gordon. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -44,7 +44,7 @@ static const char *c_mode_types = "char|double|float|int|long|unsigned|short|signed|void|var|" "_Bool|_Complex|_Imaginary|"; -static const char *c_mode_extensions = +static const char c_mode_extensions[] = "c|h|y|l|lex|e|qe|cs|idl|jav|java|js|json|" "ec|ecp|" /* Informix embedded C */ "pgc|" /* Postgres embedded C */ @@ -599,10 +599,10 @@ static void do_c_forward_preprocessor(EditState *s, int dir) { } -static int c_mode_probe(ModeProbeData *p) +static int c_mode_probe(ModeDef *mode, ModeProbeData *p) { /* currently, only use the file extension */ - if (match_extension(p->filename, c_mode_extensions)) + if (match_extension(p->filename, mode->extensions)) return 80; /* weaker match on C comment start */ @@ -613,6 +613,10 @@ static int c_mode_probe(ModeProbeData *p) if (p->buf[0] == '/' && p->buf[1] == '/') return 50; + /* same for file starting with '#include' */ + if (strstart(cs8(p->buf), "#include", NULL)) + return 50; + /* same for file starting with '#pragma' as in #pragma once */ if (strstart(cs8(p->buf), "#pragma", NULL)) return 50; @@ -653,9 +657,10 @@ static int c_init(void) { const char *p; - /* c mode is almost like the text mode, so we copy and patch it */ + /* C mode is almost like the text mode, so we copy and patch it */ memcpy(&c_mode, &text_mode, sizeof(ModeDef)); c_mode.name = "C"; + c_mode.extensions = c_mode_extensions; c_mode.mode_probe = c_mode_probe; c_mode.mode_init = c_mode_init; diff --git a/dired.c b/dired.c index 2ce44a5..a6848e9 100644 --- a/dired.c +++ b/dired.c @@ -1,8 +1,8 @@ /* * Directory editor mode for QEmacs. * - * Copyright (c) 2001, 2002 Fabrice Bellard. - * Copyright (c) 2002-2008 Charlie Gordon. + * Copyright (c) 2001-2002 Fabrice Bellard. + * Copyright (c) 2002-2014 Charlie Gordon. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -43,7 +43,7 @@ typedef struct DiredState { /* opaque structure for sorting DiredState.items StringArray */ typedef struct DiredItem { DiredState *state; - mode_t mode; + mode_t st_mode; off_t size; time_t mtime; int offset; @@ -122,29 +122,29 @@ static int dired_sort_func(const void *p1, const void *p2) const StringItem *item2 = *(const StringItem **)p2; const DiredItem *dip1 = item1->opaque; const DiredItem *dip2 = item2->opaque; - int mode = dip1->state->sort_mode, res; + int sort_mode = dip1->state->sort_mode, res; int is_dir1, is_dir2; - if (mode & DIRED_SORT_GROUP) { - is_dir1 = !!S_ISDIR(dip1->mode); - is_dir2 = !!S_ISDIR(dip2->mode); + if (sort_mode & DIRED_SORT_GROUP) { + is_dir1 = !!S_ISDIR(dip1->st_mode); + is_dir2 = !!S_ISDIR(dip2->st_mode); if (is_dir1 != is_dir2) return is_dir2 - is_dir1; } for (;;) { - if (mode & DIRED_SORT_DATE) { + if (sort_mode & DIRED_SORT_DATE) { if (dip1->mtime != dip2->mtime) { res = (dip1->mtime < dip2->mtime) ? -1 : 1; break; } } - if (mode & DIRED_SORT_SIZE) { + if (sort_mode & DIRED_SORT_SIZE) { if (dip1->size != dip2->size) { res = (dip1->size < dip2->size) ? -1 : 1; break; } } - if (mode & DIRED_SORT_EXTENSION) { + if (sort_mode & DIRED_SORT_EXTENSION) { res = qe_strcollate(get_extension(dip1->name), get_extension(dip2->name)); if (res) @@ -153,7 +153,7 @@ static int dired_sort_func(const void *p1, const void *p2) res = qe_strcollate(dip1->name, dip2->name); break; } - return (mode & DIRED_SORT_DESCENDING) ? -res : res; + return (sort_mode & DIRED_SORT_DESCENDING) ? -res : res; } /* select current item */ @@ -358,7 +358,7 @@ static void dired_build_list(EditState *s, const char *path, dip = qe_malloc_hack(DiredItem, plen); dip->state = hs; - dip->mode = st.st_mode; + dip->st_mode = st.st_mode; dip->size = st.st_size; dip->mtime = st.st_mtime; dip->mark = ' '; @@ -513,9 +513,9 @@ static void dired_mode_close(EditState *s) } /* can only apply dired mode on directories */ -static int dired_mode_probe(ModeProbeData *p) +static int dired_mode_probe(ModeDef *mode, ModeProbeData *p) { - if (S_ISDIR(p->mode)) + if (S_ISDIR(p->st_mode)) return 100; else return 0; diff --git a/docbook.c b/docbook.c index 47a80d3..833ae3f 100644 --- a/docbook.c +++ b/docbook.c @@ -21,10 +21,11 @@ #include "qe.h" #include "css.h" -static int docbook_mode_probe(ModeProbeData *p1) +static int docbook_mode_probe(ModeDef *mode, ModeProbeData *p1) { - if (xml_mode_probe(p1) == 0) + if (xml_mode.mode_probe(&xml_mode, p1) == 0) return 0; + /* well, very crude, but it may work OK */ if (strstr((const char *)p1->buf, "DocBook")) return 100; @@ -44,6 +45,7 @@ static int docbook_init(void) /* inherit from html mode */ memcpy(&docbook_mode, &html_mode, sizeof(ModeDef)); docbook_mode.name = "docbook"; + docbook_mode.extensions = NULL; docbook_mode.mode_probe = docbook_mode_probe; docbook_mode.mode_init = docbook_mode_init; diff --git a/hex.c b/hex.c index 7e89359..ca34ba7 100644 --- a/hex.c +++ b/hex.c @@ -1,8 +1,8 @@ /* * Hexadecimal modes for QEmacs. * - * Copyright (c) 2000, 2001 Fabrice Bellard. - * Copyright (c) 2002-2008 Charlie Gordon. + * Copyright (c) 2000-2001 Fabrice Bellard. + * Copyright (c) 2002-2014 Charlie Gordon. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -21,13 +21,6 @@ #include "qe.h" -/* CG: should get rid of this forward reference */ -#if defined(__GNUC__) && (__GNUC__ >= 4) -static ModeDef hex_mode; -#else -extern ModeDef hex_mode; -#endif - static int to_disp(int c) { #if 1 @@ -155,7 +148,7 @@ static CmdDef hex_commands[] = { CMD_DEF_END, }; -static int ascii_mode_init(EditState *s, ModeSavedData *saved_data) +static int binary_mode_init(EditState *s, ModeSavedData *saved_data) { QEFont *font; QEStyleDef style; @@ -214,7 +207,7 @@ static int detect_binary(const unsigned char *buf, int size) return 0; } -static int hex_mode_probe(ModeProbeData *p) +static int hex_mode_probe(ModeDef *mode, ModeProbeData *p) { if (detect_binary(p->buf, p->buf_size)) return 50; @@ -331,11 +324,11 @@ static int hex_mode_line(EditState *s, char *buf, int buf_size) return pos; } -static ModeDef ascii_mode = { - "ascii", +static ModeDef binary_mode = { + .name = "binary", .instance_size = 0, .mode_probe = NULL, - .mode_init = ascii_mode_init, + .mode_init = binary_mode_init, .mode_close = text_mode_close, .text_display = hex_display, .text_backward_offset = hex_backward_offset, @@ -350,8 +343,8 @@ static ModeDef ascii_mode = { .get_mode_line = hex_mode_line, }; -static ModeDef hex_mode = { - "hex", +ModeDef hex_mode = { + .name = "hex", .instance_size = 0, .mode_probe = hex_mode_probe, .mode_init = hex_mode_init, @@ -372,12 +365,12 @@ static ModeDef hex_mode = { static int hex_init(void) { /* first register mode(s) */ - qe_register_mode(&ascii_mode); + qe_register_mode(&binary_mode); qe_register_mode(&hex_mode); /* commands and default keys */ qe_register_cmd_table(hex_commands, &hex_mode); - qe_register_cmd_table(hex_commands, &ascii_mode); + qe_register_cmd_table(hex_commands, &binary_mode); /* additional mode specific keys */ qe_register_binding(KEY_TAB, "toggle-hex", &hex_mode); diff --git a/html.c b/html.c index 23cc61d..5f06360 100644 --- a/html.c +++ b/html.c @@ -1,7 +1,7 @@ /* * Graphical HTML mode for QEmacs. * - * Copyright (c) 2001, 2002 Fabrice Bellard. + * Copyright (c) 2001-2002 Fabrice Bellard. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -827,7 +827,7 @@ static void html_mode_close(EditState *s) } /* search for HTML tag */ -static int html_mode_probe(ModeProbeData *p1) +static int html_mode_probe(ModeDef *mode, ModeProbeData *p1) { const unsigned char *p = p1->buf; int c, score; @@ -874,7 +874,7 @@ static CmdDef html_commands[] = { }; ModeDef html_mode = { - "html", + .name = "html", .instance_size = sizeof(HTMLState), .mode_probe = html_mode_probe, .mode_init = html_mode_init, diff --git a/htmlsrc.c b/htmlsrc.c index c39068e..15398fb 100644 --- a/htmlsrc.c +++ b/htmlsrc.c @@ -1,7 +1,7 @@ /* * HTML Source mode for QEmacs. * - * Copyright (c) 2000-2008 Charlie Gordon. + * Copyright (c) 2000-2014 Charlie Gordon. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,8 +20,6 @@ #include "qe.h" -static const char *html_mode_extensions = "html|htm|asp|shtml|hta|htp|phtml"; - static int get_html_entity(unsigned int *p) { unsigned int *p_start, c; @@ -251,12 +249,12 @@ static int html_tagcmp(const char *s1, const char *s2) return 0; } -static int htmlsrc_mode_probe(ModeProbeData *p) +static int htmlsrc_mode_probe(ModeDef *mode, ModeProbeData *p) { const char *buf = (const char*)p->buf; /* first check file extension */ - if (match_extension(p->filename, html_mode_extensions)) + if (match_extension(p->filename, mode->extensions)) return 80; /* then try buffer contents */ @@ -292,9 +290,10 @@ static ModeDef htmlsrc_mode; static int htmlsrc_init(void) { - /* c mode is almost like the text mode, so we copy and patch it */ + /* html-src mode is almost like the text mode, so we copy and patch it */ memcpy(&htmlsrc_mode, &text_mode, sizeof(ModeDef)); htmlsrc_mode.name = "html-src"; + htmlsrc_mode.extensions = "html|htm|asp|shtml|hta|htp|phtml"; htmlsrc_mode.mode_probe = htmlsrc_mode_probe; htmlsrc_mode.mode_init = htmlsrc_mode_init; diff --git a/image.c b/image.c index 1756b19..6bbe578 100644 --- a/image.c +++ b/image.c @@ -1,7 +1,7 @@ /* * Image mode for QEmacs. * - * Copyright (c) 2002, 2003 Fabrice Bellard. + * Copyright (c) 2002-2003 Fabrice Bellard. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -246,7 +246,7 @@ static void image_set_size(EditState *s, int w, int h) } #endif -static int image_mode_probe(ModeProbeData *pd) +static int image_mode_probe(ModeDef *mode, ModeProbeData *pd) { AVProbeData avpd; AVImageFormat *fmt; @@ -828,7 +828,7 @@ static CmdDef image_commands[] = { }; ModeDef image_mode = { - "image", + .name = "image", .instance_size = sizeof(ImageState), .mode_probe = image_mode_probe, .mode_init = image_mode_init, diff --git a/latex-mode.c b/latex-mode.c index 2254d03..dadce32 100644 --- a/latex-mode.c +++ b/latex-mode.c @@ -24,8 +24,6 @@ #define MAX_BUF_SIZE 512 -static ModeDef latex_mode; - /* TODO: add state handling to allow colorization of elements longer * than one line (eg, multi-line functions and strings) */ @@ -123,12 +121,12 @@ static void latex_colorize_line(unsigned int *buf, __unused__ int len, *colorize_state_ptr = state; } -static int latex_mode_probe(ModeProbeData *p) +static int latex_mode_probe(ModeDef *mode, ModeProbeData *p) { /* currently, only use the file extension */ /* Halibut (by Simon Tatham) has a syntax similar to TeX and uses * .but extension */ - if (match_extension(p->filename, "tex|but")) + if (match_extension(p->filename, mode->extensions)) return 100; return 0; @@ -326,11 +324,14 @@ static CmdDef latex_commands[] = { CMD_DEF_END, }; +static ModeDef latex_mode; + static int latex_init(void) { /* LaTeX mode is almost like the text mode, so we copy and patch it */ memcpy(&latex_mode, &text_mode, sizeof(ModeDef)); latex_mode.name = "LaTeX"; + latex_mode.extensions = "tex|but"; latex_mode.mode_probe = latex_mode_probe; latex_mode.mode_init = latex_mode_init; diff --git a/lisp.c b/lisp.c index d2e00af..91d0008 100644 --- a/lisp.c +++ b/lisp.c @@ -1,7 +1,7 @@ /* * Lisp Source mode for QEmacs. * - * Copyright (c) 2000-2008 Charlie Gordon. + * Copyright (c) 2000-2014 Charlie Gordon. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -22,8 +22,6 @@ /* TODO: lisp-indent = 2 */ -static const char *lisp_mode_extensions = "ll|li|lh|lo|lm|lisp|el"; - /*---------------- Lisp colors ----------------*/ #define IN_COMMENT 0x01 @@ -105,10 +103,10 @@ static void lisp_colorize_line(unsigned int *str, int n, int *statep, #undef LISP_COMMENT #undef LISP_STRING -static int lisp_mode_probe(ModeProbeData *p) +static int lisp_mode_probe(ModeDef *mode, ModeProbeData *p) { /* just check file extension */ - if (match_extension(p->filename, lisp_mode_extensions) + if (match_extension(p->filename, mode->extensions) || strstart(p->filename, ".emacs", NULL)) return 80; @@ -135,9 +133,10 @@ static ModeDef lisp_mode; static int lisp_init(void) { - /* c mode is almost like the text mode, so we copy and patch it */ + /* lisp mode is almost like the text mode, so we copy and patch it */ memcpy(&lisp_mode, &text_mode, sizeof(ModeDef)); lisp_mode.name = "Lisp"; + lisp_mode.extensions = "ll|li|lh|lo|lm|lisp|el"; lisp_mode.mode_probe = lisp_mode_probe; lisp_mode.mode_init = lisp_mode_init; diff --git a/makemode.c b/makemode.c index 569580e..8ff4bd3 100644 --- a/makemode.c +++ b/makemode.c @@ -1,7 +1,7 @@ /* * Makefile mode for QEmacs. * - * Copyright (c) 2000-2008 Charlie Gordon. + * Copyright (c) 2000-2014 Charlie Gordon. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -144,12 +144,12 @@ static void makefile_colorize_line(unsigned int *str, int n, int *statep, #undef MAKEFILE_TARGET #undef MAKEFILE_MACRO -static int makefile_mode_probe(ModeProbeData *p) +static int makefile_mode_probe(ModeDef *mode, ModeProbeData *p) { const char *base = get_basename(p->filename); /* check file name or extension */ - if (match_extension(base, "mk|mak") + if (match_extension(base, mode->extensions) || stristart(base, "makefile", NULL)) return 70; @@ -176,9 +176,10 @@ static ModeDef makefile_mode; static int makefile_init(void) { - /* c mode is almost like the text mode, so we copy and patch it */ + /* Makefile mode is almost like the text mode, so we copy and patch it */ memcpy(&makefile_mode, &text_mode, sizeof(ModeDef)); makefile_mode.name = "Makefile"; + makefile_mode.extensions = "mk|mak"; makefile_mode.mode_probe = makefile_mode_probe; makefile_mode.mode_init = makefile_mode_init; diff --git a/mpeg.c b/mpeg.c index fa6938b..abc25cd 100644 --- a/mpeg.c +++ b/mpeg.c @@ -151,7 +151,7 @@ static int mpeg_mode_init(EditState *s, ModeSavedData *saved_data) return 0; } -static int mpeg_mode_probe(ModeProbeData *p) +static int mpeg_mode_probe(ModeDef *mode, ModeProbeData *p) { if (p->buf_size >= 4 && p->buf[0] == 0x00 && @@ -164,13 +164,13 @@ static int mpeg_mode_probe(ModeProbeData *p) } static ModeDef mpeg_mode = { - "mpeg", - mode_probe: mpeg_mode_probe, - mode_init: mpeg_mode_init, - mode_close: NULL, - text_display: mpeg_display, - text_backward_offset: mpeg_backward_offset, - write_char: hex_write_char, + .name = "mpeg", + .mode_probe = mpeg_mode_probe, + .mode_init = mpeg_mode_init, + .mode_close = NULL, + .text_display = mpeg_display, + .text_backward_offset = mpeg_backward_offset, + .write_char = hex_write_char, }; static int mpeg_init(void) diff --git a/perl.c b/perl.c index 17fea2e..7068f28 100644 --- a/perl.c +++ b/perl.c @@ -1,7 +1,7 @@ /* * Perl Source mode for QEmacs. * - * Copyright (c) 2000-2008 Charlie Gordon. + * Copyright (c) 2000-2014 Charlie Gordon. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,8 +20,6 @@ #include "qe.h" -static const char *perl_mode_extensions = "pl|perl|pm"; - /*---------------- Perl colors ----------------*/ #define PERL_TEXT QE_STYLE_DEFAULT @@ -330,10 +328,10 @@ static void perl_colorize_line(unsigned int *str, int n, int *statep, #undef IN_FORMAT #undef IN_INPUT -static int perl_mode_probe(ModeProbeData *p) +static int perl_mode_probe(ModeDef *mode, ModeProbeData *p) { /* just check file extension */ - if (match_extension(p->filename, perl_mode_extensions)) + if (match_extension(p->filename, mode->extensions)) return 80; if (p->buf[0] == '#' && p->buf[1] == '!' && @@ -363,9 +361,10 @@ static ModeDef perl_mode; static int perl_init(void) { - /* c mode is almost like the text mode, so we copy and patch it */ + /* perl mode is almost like the text mode, so we copy and patch it */ memcpy(&perl_mode, &text_mode, sizeof(ModeDef)); perl_mode.name = "Perl"; + perl_mode.extensions = "pl|perl|pm"; perl_mode.mode_probe = perl_mode_probe; perl_mode.mode_init = perl_mode_init; diff --git a/qe.c b/qe.c index 1404baa..b6011c9 100644 --- a/qe.c +++ b/qe.c @@ -1,8 +1,8 @@ /* * QEmacs, tiny but powerful multimode editor * - * Copyright (c) 2000, 2001, 2002 Fabrice Bellard. - * Copyright (c) 2000-2013 Charlie Gordon. + * Copyright (c) 2000-2002 Fabrice Bellard. + * Copyright (c) 2000-2014 Charlie Gordon. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -1731,10 +1731,12 @@ void do_next_mode(EditState *s) probe_data.buf_size = size; probe_data.real_filename = s->b->filename; probe_data.total_size = s->b->total_size; + probe_data.st_mode = 0644; probe_data.filename = reduce_filename(fname, sizeof(fname), get_basename(s->b->filename)); /* CG: should pass EditState? QEmacsState ? */ + /* Should cycle modes in order of decreasing scores */ m = m0 = s->mode; for (;;) { m = m->next; @@ -1743,7 +1745,7 @@ void do_next_mode(EditState *s) if (m == m0) break; if (!m->mode_probe - || m->mode_probe(&probe_data) > 0) { + || m->mode_probe(m, &probe_data) > 0) { edit_set_mode(s, m); break; } @@ -1972,9 +1974,9 @@ void do_set_indent_width(EditState *s, int indent_width) s->indent_size = indent_width; } -void do_set_indent_tabs_mode(EditState *s, int mode) +void do_set_indent_tabs_mode(EditState *s, int val) { - s->indent_tabs_mode = (mode != 0); + s->indent_tabs_mode = (val != 0); } /* compute string for the first part of the mode line (flags, @@ -3004,6 +3006,7 @@ int text_backward_offset(EditState *s, int offset) { int line, col; + /* CG: beware: offset may fall inside a character */ eb_get_pos(s->b, &line, &col, offset); return eb_goto_pos(s->b, line, 0); } @@ -5349,7 +5352,7 @@ static void get_default_path(EditState *s, char *buf, int buf_size) splitpath(buf, buf_size, NULL, 0, buf1); } -static ModeDef *probe_mode(EditState *s, int mode, const uint8_t *buf, +static ModeDef *probe_mode(EditState *s, int st_mode, const uint8_t *buf, int len, long total_size) { QEmacsState *qs = s->qe_state; @@ -5357,19 +5360,19 @@ static ModeDef *probe_mode(EditState *s, int mode, const uint8_t *buf, EditBuffer *b; ModeDef *m, *selected_mode; ModeProbeData probe_data; - int best_probe_percent, percent; + int best_probe_score, score; const uint8_t *p; b = s->b; selected_mode = NULL; - best_probe_percent = 0; + best_probe_score = 0; probe_data.buf = buf; probe_data.buf_size = len; p = memchr(buf, '\n', len); probe_data.line_len = p ? p - buf : len; probe_data.real_filename = b->filename; - probe_data.mode = mode; + probe_data.st_mode = st_mode; probe_data.total_size = total_size; probe_data.filename = reduce_filename(fname, sizeof(fname), get_basename(b->filename)); @@ -5378,10 +5381,10 @@ static ModeDef *probe_mode(EditState *s, int mode, const uint8_t *buf, m = qs->first_mode; while (m != NULL) { if (m->mode_probe) { - percent = m->mode_probe(&probe_data); - if (percent > best_probe_percent) { + score = m->mode_probe(m, &probe_data); + if (score > best_probe_score) { selected_mode = m; - best_probe_percent = percent; + best_probe_score = score; } } m = m->next; @@ -5395,7 +5398,7 @@ static void do_load1(EditState *s, const char *filename1, { u8 buf[4097]; char filename[MAX_FILENAME_SIZE]; - int mode, buf_size; + int st_mode, buf_size; ModeDef *selected_mode; EditBuffer *b; EditBufferDataType *bdt; @@ -5462,11 +5465,11 @@ static void do_load1(EditState *s, const char *filename1, edit_set_mode(s, selected_mode); return; } else { - mode = st.st_mode; + st_mode = st.st_mode; buf_size = 0; f = NULL; /* CG: should check for ISDIR and do dired */ - if (S_ISREG(mode)) { + if (S_ISREG(st_mode)) { f = fopen(filename, "r"); if (!f) goto fail; @@ -5480,7 +5483,7 @@ static void do_load1(EditState *s, const char *filename1, } } buf[buf_size] = '\0'; - selected_mode = probe_mode(s, mode, buf, buf_size, st.st_size); + selected_mode = probe_mode(s, st_mode, buf, buf_size, st.st_size); if (!selected_mode) goto fail1; bdt = selected_mode->data_type; @@ -5521,19 +5524,19 @@ static void load_progress_cb(void *opaque, int size) static void load_completion_cb(void *opaque, int err) { EditState *s = opaque; - int mode; + int st_mode; - mode = S_IFREG; + st_mode = S_IFREG; /* CG: potential problem: EXXX may be negative, as in Haiku */ if (err == -ENOENT || err == -ENOTDIR) { put_status(s, "(New file)"); } else if (err == -EISDIR) { - mode = S_IFDIR; + st_mode = S_IFDIR; } else if (err < 0) { put_status(s, "Could not read file"); } if (!s->b->probed) - probe_mode(s, mode); + probe_mode(s, st_mode); edit_display(s->qe_state); dpy_flush(&global_screen); } @@ -6961,7 +6964,8 @@ int detect_binary(const u8 *buf, int size) } #endif -static int text_mode_probe(__unused__ ModeProbeData *p) +static int text_mode_probe(__unused__ ModeDef *mode, + __unused__ ModeProbeData *p) { #if 0 /* text mode inappropriate for huge binary files */ @@ -7013,7 +7017,7 @@ void text_mode_close(EditState *s) } ModeDef text_mode = { - "text", + .name = "text", .instance_size = 0, .mode_probe = text_mode_probe, .mode_init = text_mode_init, @@ -7394,7 +7398,7 @@ void qe_register_cmd_line_options(CmdOptionDef *table) const char str_version[] = "QEmacs version " QE_VERSION; const char str_credits[] = "Copyright (c) 2000-2003 Fabrice Bellard\n" - "Copyright (c) 2000-2008 Charlie Gordon\n"; + "Copyright (c) 2000-2014 Charlie Gordon\n"; static void show_version(void) { diff --git a/qe.h b/qe.h index e8cf00d..5dd35c4 100644 --- a/qe.h +++ b/qe.h @@ -858,7 +858,7 @@ int eb_goto_eol(EditBuffer *b, int offset); int eb_next_line(EditBuffer *b, int offset); void eb_register_data_type(EditBufferDataType *bdt); -EditBufferDataType *eb_probe_data_type(const char *filename, int mode, +EditBufferDataType *eb_probe_data_type(const char *filename, int st_mode, uint8_t *buf, int buf_size); void eb_set_data_type(EditBuffer *b, EditBufferDataType *bdt); void eb_invalidate_raw_data(EditBuffer *b); @@ -952,7 +952,7 @@ struct EditState { int y_disp; /* virtual position of the displayed text */ int x_disp[2]; /* position for LTR and RTL text resp. */ int minibuf; /* true if single line editing */ - int disp_width; /* width in hex or ascii mode */ + int disp_width; /* width in binary, hex and unihex modes */ int hex_mode; /* true if we are currently editing hexa */ int unihex_mode; /* true if unihex editing (hex_mode must be true too) */ int hex_nibble; /* current hexa nibble */ @@ -1039,31 +1039,36 @@ struct EditState { #define SAVED_DATA_SIZE offsetof(EditState, end_of_saved_data) typedef struct DisplayState DisplayState; +typedef struct ModeProbeData ModeProbeData; +typedef struct ModeSavedData ModeSavedData; +typedef struct ModeDef ModeDef; -typedef struct ModeProbeData { +struct ModeProbeData { const char *real_filename; const char *filename; /* reduced filename for mode matching purposes */ const u8 *buf; int buf_size; int line_len; - int mode; /* unix mode */ + int st_mode; /* unix file mode */ long total_size; -} ModeProbeData; +}; /* private data saved by a mode so that it can be restored when the mode is started again on a buffer */ -typedef struct ModeSavedData { - struct ModeDef *mode; /* the mode is saved there */ +struct ModeSavedData { + ModeDef *mode; /* the mode is saved there */ char generic_data[SAVED_DATA_SIZE]; /* generic text data */ int data_size; /* mode specific saved data */ char data[1]; -} ModeSavedData; +}; -typedef struct ModeDef { +struct ModeDef { const char *name; + const char *extensions; //const char *mode_line; int instance_size; /* size of malloced instance */ - int (*mode_probe)(ModeProbeData *); /* return the percentage of confidence */ + /* return the percentage of confidence */ + int (*mode_probe)(ModeDef *, ModeProbeData *); int (*mode_init)(EditState *, ModeSavedData *); void (*mode_close)(EditState *); /* save the internal state of the mode so that it can be opened @@ -1099,8 +1104,8 @@ typedef struct ModeDef { /* mode specific key bindings */ struct KeyDef *first_key; - struct ModeDef *next; -} ModeDef; + ModeDef *next; +}; /* special bit to indicate tty styles (for shell mode) */ #define QE_STYLE_TTY 0x800 @@ -1673,7 +1678,7 @@ void do_count_lines(EditState *s); void do_what_cursor_position(EditState *s); void do_set_tab_width(EditState *s, int tab_width); void do_set_indent_width(EditState *s, int indent_width); -void do_set_indent_tabs_mode(EditState *s, int mode); +void do_set_indent_tabs_mode(EditState *s, int val); void display_window_borders(EditState *e); QEStyleDef *find_style(const char *name); void style_completion(CompleteState *cp); @@ -1744,6 +1749,8 @@ void do_show_date_and_time(EditState *s, int argval); /* hex.c */ +extern ModeDef hex_mode; + void hex_write_char(EditState *s, int key); /* list.c */ @@ -1755,14 +1762,17 @@ int list_get_pos(EditState *s); int list_get_offset(EditState *s); /* dired.c */ + void do_dired(EditState *s); /* c_mode.c */ + void c_colorize_line(unsigned int *buf, int len, int *colorize_state_ptr, int state_only); /* xml.c */ -int xml_mode_probe(ModeProbeData *p1); + +extern ModeDef xml_mode; /* html.c */ @@ -1773,10 +1783,12 @@ int gxml_mode_init(EditState *s, int is_html, const char *default_stylesheet); /* image.c */ + void fill_border(EditState *s, int x, int y, int w, int h, int color); int qe_bitmap_format_to_pix_fmt(int format); /* shell.c */ + const char *get_shell(void); #define SF_INTERACTIVE 0x01 diff --git a/script.c b/script.c index e5deecd..b032fe6 100644 --- a/script.c +++ b/script.c @@ -1,7 +1,7 @@ /* * Shell script mode for QEmacs. * - * Copyright (c) 2000-2008 Charlie Gordon. + * Copyright (c) 2000-2014 Charlie Gordon. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,8 +20,6 @@ #include "qe.h" -static const char *script_mode_extensions = "sh|bash|zsh"; - /*---------------- Perl colors ----------------*/ enum { @@ -104,9 +102,9 @@ static void script_colorize_line(unsigned int *str, int n, int *statep, } } -static int script_mode_probe(ModeProbeData *p) +static int script_mode_probe(ModeDef *mode, ModeProbeData *p) { - if (match_extension(p->filename, script_mode_extensions)) + if (match_extension(p->filename, mode->extensions)) return 80; if (p->buf[0] == '#') { @@ -138,9 +136,10 @@ static ModeDef script_mode; static int script_init(void) { - /* c mode is almost like the text mode, so we copy and patch it */ + /* Shell-script mode is almost like the text mode, so we copy and patch it */ memcpy(&script_mode, &text_mode, sizeof(ModeDef)); script_mode.name = "Shell-script"; + script_mode.extensions = "sh|bash|zsh"; script_mode.mode_probe = script_mode_probe; script_mode.mode_init = script_mode_init; diff --git a/variables.c b/variables.c index aa607f1..40365a7 100644 --- a/variables.c +++ b/variables.c @@ -1,7 +1,7 @@ /* * Module for handling variables in QEmacs * - * Copyright (c) 2000-2008 Charlie Gordon. + * Copyright (c) 2000-2014 Charlie Gordon. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -74,7 +74,7 @@ static VarDef var_table[] = { */ //G_VAR( "text-mode-line", text_mode.mode_line, VAR_STRING, VAR_RW ) - //G_VAR( "ascii-mode-line", ascii_mode.mode_line, VAR_STRING, VAR_RW ) + //G_VAR( "binary-mode-line", binary_mode.mode_line, VAR_STRING, VAR_RW ) //G_VAR( "hex-mode-line", hex_mode.mode_line, VAR_STRING, VAR_RW ) //G_VAR( "unicode-mode-line", unihex_mode.mode_line, VAR_STRING, VAR_RW ) diff --git a/video.c b/video.c index 7657427..bfc1c49 100644 --- a/video.c +++ b/video.c @@ -1,7 +1,7 @@ /* * Video mode for QEmacs * - * Copyright (c) 2002, 2003 Fabrice Bellard. + * Copyright (c) 2002-2003 Fabrice Bellard. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -96,7 +96,7 @@ static void video_buffer_close(EditBuffer *b) { } -static int video_mode_probe(ModeProbeData *pd) +static int video_mode_probe(ModeDef *mode, ModeProbeData *pd) { AVProbeData avpd; AVInputFormat *fmt; @@ -951,7 +951,7 @@ static CmdDef video_commands[] = { }; ModeDef video_mode = { - "av", + .name = "av", .instance_size = sizeof(VideoState), .mode_probe = video_mode_probe, .mode_init = video_mode_init, diff --git a/xml.c b/xml.c index b1cf7e5..c71f979 100644 --- a/xml.c +++ b/xml.c @@ -160,7 +160,7 @@ static void xml_colorize_line(unsigned int *buf, __unused__ int len, *colorize_state_ptr = state; } -int xml_mode_probe(ModeProbeData *p1) +static int xml_mode_probe(ModeDef *mode, ModeProbeData *p1) { const u8 *p; @@ -185,11 +185,11 @@ static int xml_mode_init(EditState *s, ModeSavedData *saved_data) return 0; } -static ModeDef xml_mode; +ModeDef xml_mode; static int xml_init(void) { - /* c mode is almost like the text mode, so we copy and patch it */ + /* xml mode is almost like the text mode, so we copy and patch it */ memcpy(&xml_mode, &text_mode, sizeof(ModeDef)); xml_mode.name = "xml"; xml_mode.mode_probe = xml_mode_probe;