diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2012-11-11 20:28:12 +0100 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2012-11-11 20:28:12 +0100 |
commit | 59eb0628db77e1145db797406e4de9f97b70d8e2 (patch) | |
tree | 5d797e99a5081e750cf906fe620247d5fa3e7fb7 | |
parent | 8c6c6afe144adca0635edd0438b9889b3c3df6e4 (diff) | |
download | sciteco-59eb0628db77e1145db797406e4de9f97b70d8e2.tar.gz |
<x,y>Xq command, automatic profile munging, explicit munging, commandline option processing, unhandled commandline options in default buffer
-rw-r--r-- | main.cpp | 58 | ||||
-rw-r--r-- | parser.cpp | 27 | ||||
-rw-r--r-- | parser.h | 1 | ||||
-rw-r--r-- | qbuffers.cpp | 39 | ||||
-rw-r--r-- | qbuffers.h | 6 |
5 files changed, 130 insertions, 1 deletions
@@ -1,7 +1,10 @@ +#include <string.h> #include <stdarg.h> +#include <stdlib.h> #include <glib.h> #include <glib/gprintf.h> +#include <glib/gstdio.h> #include <gdk/gdk.h> #include <gdk/gdkkeysyms.h> @@ -24,6 +27,8 @@ static GtkWidget *info_widget, *message_widget; GtkInfoPopup *filename_popup; +#define INI_FILE ".teco_ini" + void cmdline_display(const gchar *cmdline_str) { @@ -101,13 +106,51 @@ widget_set_font(GtkWidget *widget, const gchar *font_name) pango_font_description_free(font_desc); } +static gchar *mung_file = NULL; + +static inline void +process_options(int &argc, char **&argv) +{ + static const GOptionEntry option_entries[] = { + {"mung", 'm', 0, G_OPTION_ARG_FILENAME, &mung_file, + "Mung file instead of " INI_FILE, "filename"}, + {NULL} + }; + + GOptionContext *options; + + options = g_option_context_new("- Advanced interactive TECO"); + g_option_context_add_main_entries(options, option_entries, NULL); + g_option_context_add_group(options, gtk_get_option_group(TRUE)); + if (!g_option_context_parse(options, &argc, &argv, NULL)) { + g_printf("Option parsing failed!\n"); + exit(EXIT_FAILURE); + } + g_option_context_free(options); + + if (mung_file) { + if (!g_file_test(mung_file, G_FILE_TEST_IS_REGULAR)) { + g_printf("Cannot mung %s. File does not exist!\n", + mung_file); + exit(EXIT_FAILURE); + } + } else { + mung_file = g_build_filename(g_get_user_config_dir(), + INI_FILE, NULL); + } + + gtk_init(&argc, &argv); + + /* remaining arguments, are arguments to the munged file */ +} + int main(int argc, char **argv) { GtkWidget *window, *vbox; GtkWidget *info_content; - gtk_init(&argc, &argv); + process_options(argc, argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "SciTECO"); @@ -157,6 +200,19 @@ main(int argc, char **argv) qregisters.initialize(); ring.edit(NULL); + /* add remaining arguments to unnamed buffer */ + for (int i = 1; i < argc; i++) { + editor_msg(SCI_ADDTEXT, strlen(argv[i]), (sptr_t)argv[i]); + editor_msg(SCI_ADDTEXT, 1, (sptr_t)"\r"); + } + editor_msg(SCI_GOTOPOS, 0); + + if (g_file_test(mung_file, G_FILE_TEST_IS_REGULAR)) { + if (!file_execute(mung_file)) + exit(EXIT_FAILURE); + } + g_free(mung_file); + undo.enabled = true; cmdline_display("*"); @@ -2,6 +2,7 @@ #include <glib.h> #include <glib/gprintf.h> +#include <glib/gstdio.h> #include "sciteco.h" #include "undo.h" @@ -63,6 +64,31 @@ macro_execute(const gchar *macro) return true; } +bool +file_execute(const gchar *filename) +{ + gchar *macro, *p = NULL; + bool rc; + + macro_pc = 0; + States::current = &States::start; + + if (!g_file_get_contents(filename, ¯o, NULL, NULL)) + return false; + /* only when executing files, ignore Hash-Bang line */ + if (macro[0] == '#') + p = MAX(strchr(macro, '\r'), strchr(macro, '\n')); + + rc = macro_execute(p ? p+1 : macro); + g_free(macro); + if (!rc) + return false; + + macro_pc = 0; + States::current = &States::start; + return true; +} + State::State() { for (guint i = 0; i < G_N_ELEMENTS(transitions); i++) @@ -310,6 +336,7 @@ StateStart::StateStart() : State() transitions['U'] = &States::setqreginteger; transitions['%'] = &States::increaseqreg; transitions['M'] = &States::macro; + transitions['X'] = &States::copytoqreg; } void @@ -178,5 +178,6 @@ extern gchar *strings[2]; extern gchar escape_char; bool macro_execute(const gchar *macro); +bool file_execute(const gchar *filename); #endif diff --git a/qbuffers.cpp b/qbuffers.cpp index acbcb9a..c649fce 100644 --- a/qbuffers.cpp +++ b/qbuffers.cpp @@ -25,6 +25,7 @@ namespace States { StateSetQRegInteger setqreginteger; StateIncreaseQReg increaseqreg; StateMacro macro; + StateCopyToQReg copytoqreg; } Ring ring; @@ -426,3 +427,41 @@ StateMacro::got_register(QRegister *reg) return &States::start; } + +State * +StateCopyToQReg::got_register(QRegister *reg) +{ + gint64 from, len; + Sci_TextRange tr; + + BEGIN_EXEC(&States::start); + expressions.eval(); + + if (expressions.args() <= 1) { + from = editor_msg(SCI_GETCURRENTPOS); + sptr_t line = editor_msg(SCI_LINEFROMPOSITION, from) + + expressions.pop_num_calc(); + len = editor_msg(SCI_POSITIONFROMLINE, line) - from; + + if (len < 0) { + from += len; + len *= -1; + } + } else { + gint64 to = expressions.pop_num(); + from = expressions.pop_num(); + len = to - from; + } + + tr.chrg.cpMin = from; + tr.chrg.cpMax = from + len; + tr.lpstrText = (char *)g_malloc(len + 1); + editor_msg(SCI_GETTEXTRANGE, 0, (sptr_t)&tr); + + undo.push_var<gint>(reg->dot); + undo.push_msg(SCI_UNDO); + reg->set_string(tr.lpstrText); + g_free(tr.lpstrText); + + return &States::start; +} @@ -274,6 +274,11 @@ private: State *got_register(QRegister *reg); }; +class StateCopyToQReg : public StateExpectQReg { +private: + State *got_register(QRegister *reg); +}; + namespace States { extern StateFile file; extern StateEQCommand eqcommand; @@ -284,6 +289,7 @@ namespace States { extern StateSetQRegInteger setqreginteger; extern StateIncreaseQReg increaseqreg; extern StateMacro macro; + extern StateCopyToQReg copytoqreg; } /* |