qemacs-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemacs-commit] qemacs extras.c qe.c qe.h util.c


From: Charlie Gordon
Subject: [Qemacs-commit] qemacs extras.c qe.c qe.h util.c
Date: Thu, 16 Jan 2014 14:24:04 +0000

CVSROOT:        /sources/qemacs
Module name:    qemacs
Changes by:     Charlie Gordon <chqrlie>        14/01/16 14:24:04

Modified files:
        .              : extras.c qe.c qe.h util.c 

Log message:
        add buf_put_key, buf_put_keys, use buf_t more consistently
        
        * changed keytostr to buf_put_key using buf_t structure
        * changed keys_to_str tp buf_put_keys using buf_t structure
        * add buf_attach to initialize buf_t with non empty buffer
        * simplify qe_list_bindings with inherit argument

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/qemacs/extras.c?cvsroot=qemacs&r1=1.12&r2=1.13
http://cvs.savannah.gnu.org/viewcvs/qemacs/qe.c?cvsroot=qemacs&r1=1.124&r2=1.125
http://cvs.savannah.gnu.org/viewcvs/qemacs/qe.h?cvsroot=qemacs&r1=1.122&r2=1.123
http://cvs.savannah.gnu.org/viewcvs/qemacs/util.c?cvsroot=qemacs&r1=1.57&r2=1.58

Patches:
Index: extras.c
===================================================================
RCS file: /sources/qemacs/qemacs/extras.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -b -r1.12 -r1.13
--- extras.c    15 Jan 2014 19:44:25 -0000      1.12
+++ extras.c    16 Jan 2014 14:24:03 -0000      1.13
@@ -380,40 +380,44 @@
 
 /*---------------- help ----------------*/
 
-static int qe_list_bindings(buf_t *out, CmdDef *d, ModeDef *mode)
+static int qe_list_bindings(char *buf, int size, CmdDef *d,
+                            ModeDef *mode, int inherit)
 {
-    char buf[128];
-    KeyDef *kd;
+    int pos;
+    buf_t outbuf, *out;
+
+    out = buf_init(&outbuf, buf, size);
+    pos = 0;
+    for (;;) {
+        KeyDef *kd = mode ? mode->first_key : qe_state.first_key;
 
-    kd = mode ? mode->first_key : qe_state.first_key;
     for (; kd != NULL; kd = kd->next) {
         if (kd->cmd == d) {
-            if (out->pos)
+                if (out->len > pos)
                 buf_puts(out, ", ");
-            buf_puts(out, keys_to_str(buf, sizeof(buf),
-                                      kd->keys, kd->nb_keys));
+
+                buf_put_keys(out, kd->keys, kd->nb_keys);
+            }
         }
+        if (!inherit || !mode)
+            break;
+        /* Should move up to base mode */
+        mode = NULL;
     }
-    return out->pos;
+    return out->len;
 }
 
 void do_show_bindings(EditState *s, const char *cmd_name)
 {
     char buf[256];
-    buf_t out;
     CmdDef *d;
     
     if ((d = qe_find_cmd(cmd_name)) == NULL) {
         put_status(s, "No command %s", cmd_name);
         return;
     }
-    buf_init(&out, buf, sizeof(buf));
-
-    qe_list_bindings(&out, d, s->mode);
-    qe_list_bindings(&out, d, NULL);
-
-    if (out.pos) {
-        put_status(s, "%s is bound to %s", cmd_name, out.buf);
+    if (qe_list_bindings(buf, sizeof(buf), d, s->mode, 1)) {
+        put_status(s, "%s is bound to %s", cmd_name, buf);
     } else {
         put_status(s, "%s is not bound to any key", cmd_name);
     }
@@ -423,7 +427,6 @@
                            __unused__ int type, ModeDef *mode)
 {
     char buf[256];
-    buf_t out;
     CmdDef *d;
     int gfound;
 
@@ -431,8 +434,7 @@
     d = qe_state.first_cmd;
     while (d != NULL) {
         while (d->name != NULL) {
-            buf_init(&out, buf, sizeof(buf));
-            if (qe_list_bindings(&out, d, mode)) {
+            if (qe_list_bindings(buf, sizeof(buf), d, mode, 0)) {
                 if (!gfound) {
                     if (title) {
                         eb_printf(b, "%s:\n\n", title);
@@ -441,7 +443,7 @@
                     }
                     gfound = 1;
                 }
-                eb_printf(b, "%24s : %s\n", d->name, out.buf);
+                eb_printf(b, "%24s : %s\n", d->name, buf);
             }
             d++;
         }
@@ -488,7 +490,10 @@
             if (strstr(d->name, str)) {
                 /* print name and prototype */
                 qe_get_prototype(d, buf, sizeof(buf));
-                eb_printf(b, "command: %s(%s);\n", d->name, buf);
+                eb_printf(b, "command: %s(%s)", d->name, buf);
+                if (qe_list_bindings(buf, sizeof(buf), d, s->mode, 1))
+                    eb_printf(b, " bound to %s", buf);
+                eb_printf(b, "\n");
                 /* TODO: print short description */
                 eb_printf(b, "\n");
                 found = 1;

Index: qe.c
===================================================================
RCS file: /sources/qemacs/qemacs/qe.c,v
retrieving revision 1.124
retrieving revision 1.125
diff -u -b -r1.124 -r1.125
--- qe.c        16 Jan 2014 14:00:28 -0000      1.124
+++ qe.c        16 Jan 2014 14:24:04 -0000      1.125
@@ -1990,45 +1990,45 @@
 void do_what_cursor_position(EditState *s)
 {
     char buf[256];
-    buf_t out;
+    buf_t outbuf, *out;
     unsigned char cc;
     int line_num, col_num;
     int c, offset1, off;
 
-    buf_init(&out, buf, sizeof(buf));
+    out = buf_init(&outbuf, buf, sizeof(buf));
     if (s->offset < s->b->total_size) {
         c = eb_nextc(s->b, s->offset, &offset1);
-        buf_puts(&out, "char: ");
+        buf_puts(out, "char: ");
         if (c < 32 || c == 127) {
-            buf_printf(&out, "^%c ", (c + '@') & 127);
+            buf_printf(out, "^%c ", (c + '@') & 127);
         } else
         if (c < 127 || c >= 160) {
-            buf_put_byte(&out, '\'');
-            buf_putc_utf8(&out, c);
-            buf_put_byte(&out, '\'');
-            buf_put_byte(&out, ' ');
+            buf_put_byte(out, '\'');
+            buf_putc_utf8(out, c);
+            buf_put_byte(out, '\'');
+            buf_put_byte(out, ' ');
         }
         if (c < 0x100)
-            buf_printf(&out, "\\%03o ", c);
-        buf_printf(&out, "%d 0x%02x ", c, c);
+            buf_printf(out, "\\%03o ", c);
+        buf_printf(out, "%d 0x%02x ", c, c);
 
         /* Display buffer bytes if char is encoded */
         off = s->offset;
         eb_read(s->b, off++, &cc, 1);
         if (cc != c || off != offset1) {
-            buf_printf(&out, "[%02X", cc);
+            buf_printf(out, "[%02X", cc);
             while (off < offset1) {
                 eb_read(s->b, off++, &cc, 1);
-                buf_printf(&out, " %02X", cc);
+                buf_printf(out, " %02X", cc);
             }
-            buf_put_byte(&out, ']');
-            buf_put_byte(&out, ' ');
+            buf_put_byte(out, ']');
+            buf_put_byte(out, ' ');
         }
-        buf_put_byte(&out, ' ');
+        buf_put_byte(out, ' ');
     }
     eb_get_pos(s->b, &line_num, &col_num, s->offset);
     put_status(s, "%spoint=%d col=%d mark=%d size=%d region=%d",
-               out.buf, s->offset, col_num, s->b->mark, s->b->total_size,
+               out->buf, s->offset, col_num, s->b->mark, s->b->total_size,
                abs(s->offset - s->b->mark));
 }
 
@@ -3000,6 +3000,7 @@
 {
     char buf[256], *p;
     va_list ap;
+
     va_start(ap, fmt);
     vsnprintf(buf, sizeof(buf), fmt, ap);
     va_end(ap);
@@ -3684,7 +3685,7 @@
     return 1;
 }
 
-void qe_get_prototype(CmdDef *d, char *buf, int size)
+int qe_get_prototype(CmdDef *d, char *buf, int size)
 {
     buf_t outbuf, *out;
     const char *r;
@@ -3734,6 +3735,7 @@
             buf_puts(out, *history ? history : completion);
         }
     }
+    return out->len;
 }
 
 static void arg_edit_cb(void *opaque, char *str);
@@ -4159,22 +4161,6 @@
     qs->macro_keys[qs->nb_macro_keys++] = key;
 }
 
-const char *keys_to_str(char *buf, int buf_size,
-                        unsigned int *keys, int nb_keys)
-{
-    char buf1[64];
-    int i;
-
-    buf[0] = '\0';
-    for (i = 0; i < nb_keys; i++) {
-        keytostr(buf1, sizeof(buf1), keys[i]);
-        if (i != 0)
-            pstrcat(buf, buf_size, " ");
-        pstrcat(buf, buf_size, buf1);
-    }
-    return buf;
-}
-
 void do_numeric_argument(__unused__ EditState *s)
 {
     /* nothing is done there (see qe_key_process()) */
@@ -4278,7 +4264,7 @@
     KeyDef *kd;
     CmdDef *d;
     char buf1[128];
-    int len;
+    buf_t outbuf, *out;
 
     if (qs->defining_macro && !qs->executing_macro) {
         macro_add_key(key);
@@ -4357,8 +4343,9 @@
             /* CG: should beep */;
         }
 
-        put_status(s, "No command on %s",
-                   keys_to_str(buf1, sizeof(buf1), c->keys, c->nb_keys));
+        out = buf_init(&outbuf, buf1, sizeof(buf1));
+        buf_put_keys(out, c->keys, c->nb_keys);
+        put_status(s, "No command on %s", buf1);
         c->describe_key = 0;
         qe_key_init(c);
         dpy_flush(&global_screen);
@@ -4369,6 +4356,7 @@
         d = kd->cmd;
         if (d->action.ES == do_numeric_argument && !c->describe_key) {
             /* special handling for numeric argument */
+            /* CG: XXX: should display value of numeric argument */
             c->is_numeric_arg = 1;
             if (key == KEY_META('-')) {
                 c->sign = -c->sign;
@@ -4385,9 +4373,9 @@
                 c->argval *= c->sign;
             }
             if (c->describe_key) {
-                put_status(s, "%s runs the command %s",
-                           keys_to_str(buf1, sizeof(buf1), c->keys, 
c->nb_keys),
-                           d->name);
+                out = buf_init(&outbuf, buf1, sizeof(buf1));
+                buf_put_keys(out, c->keys, c->nb_keys);
+                put_status(s, "%s runs the command %s", buf1, d->name);
                 c->describe_key = 0;
             } else {
                 int argval = c->argval;
@@ -4414,13 +4402,15 @@
  next:
     /* display key pressed */
     if (!s->minibuf) {
-        /* Should print argument if any in a more readable way */
-        keytostr(buf1, sizeof(buf1), key);
+        int len;
+
         len = strlen(c->buf);
         if (len >= 1)
             c->buf[len-1] = ' ';
-        pstrcat(c->buf, sizeof(c->buf), buf1);
-        pstrcat(c->buf, sizeof(c->buf), "-");
+        /* Should print argument if any in a more readable way */
+        out = buf_attach(&outbuf, c->buf, sizeof(c->buf), len);
+        buf_put_key(out, key);
+        buf_put_byte(out, '-');
         put_status(s, "~%s", c->buf);
         dpy_flush(&global_screen);
     }
@@ -4454,24 +4444,22 @@
     release_font(screen, font);
 }
 
+/* XXX: should take va_list */
 static void eb_format_message(QEmacsState *qs, const char *bufname,
                               const char *message)
 {
     char header[128];
-    int len;
     EditBuffer *eb;
+    buf_t outbuf, *out;
+
+    out = buf_init(&outbuf, header, sizeof(header));
+
+    if (qs->ec.filename)
+        buf_printf(out, "%s:%d: ", qs->ec.filename, qs->ec.lineno);
+
+    if (qs->ec.function)
+        buf_printf(out, "%s: ", qs->ec.function);
 
-    header[len = 0] = '\0';
-    if (qs->ec.filename) {
-        snprintf(header, sizeof(header), "%s:%d: ",
-                 qs->ec.filename, qs->ec.lineno);
-        len = strlen(header);
-    }
-    if (qs->ec.function) {
-        snprintf(header + len, sizeof(header) - len, "%s: ",
-                 qs->ec.function);
-        len = strlen(header);
-    }
     eb = eb_find_new(bufname, BF_UTF8);
     if (eb) {
         eb_printf(eb, "%s%s\n", header, message);
@@ -4833,7 +4821,7 @@
 {
     memset(cp, 0, sizeof(*cp));
     cp->s = s;
-    cp->len = eb_get_contents(s->b, cp->current, sizeof(cp->current) - 1);
+    cp->len = eb_get_contents(s->b, cp->current, sizeof(cp->current));
 }
 
 void complete_test(CompleteState *cp, const char *str)
@@ -6032,7 +6020,7 @@
 {
     EditState *s = is->s;
     char ubuf[256];
-    buf_t out;
+    buf_t outbuf, *out;
     char buf[2*SEARCH_LENGTH], *q; /* XXX: incorrect size */
     int i, len, hex_nibble, h;
     unsigned int v;
@@ -6082,27 +6070,27 @@
     }
 
     /* display search string */
-    buf_init(&out, ubuf, sizeof(ubuf));
+    out = buf_init(&outbuf, ubuf, sizeof(ubuf));
     if (is->found_offset < 0 && len > 0)
-        buf_printf(&out, "Failing ");
+        buf_printf(out, "Failing ");
     if (s->hex_mode) {
-        buf_printf(&out, "hex ");
+        buf_printf(out, "hex ");
     } else {
         if (is->search_flags & SEARCH_FLAG_WORD)
-            buf_printf(&out, "word ");
+            buf_printf(out, "word ");
         if (is->search_flags & SEARCH_FLAG_IGNORECASE)
-            buf_printf(&out, "case-insensitive ");
+            buf_printf(out, "case-insensitive ");
         else if (!(is->search_flags & SEARCH_FLAG_SMARTCASE))
-            buf_printf(&out, "case-sensitive ");
+            buf_printf(out, "case-sensitive ");
     }
-    buf_printf(&out, "I-search");
+    buf_printf(out, "I-search");
     if (is->dir < 0)
-        buf_printf(&out, " backward");
-    buf_printf(&out, ": ");
+        buf_printf(out, " backward");
+    buf_printf(out, ": ");
     for (i = 0; i < is->pos; i++) {
         v = is->search_string[i];
         if (!(v & FOUND_TAG)) {
-            if (!buf_putc_utf8(&out, v))
+            if (!buf_putc_utf8(out, v))
                 break;
         }
     }
@@ -6111,7 +6099,7 @@
     do_center_cursor(s);
     edit_display(s->qe_state);
 
-    put_status(NULL, "%s", out.buf);
+    put_status(NULL, "%s", out->buf);
 
     dpy_flush(s->screen);
 }

Index: qe.h
===================================================================
RCS file: /sources/qemacs/qemacs/qe.h,v
retrieving revision 1.122
retrieving revision 1.123
diff -u -b -r1.122 -r1.123
--- qe.h        16 Jan 2014 14:00:28 -0000      1.122
+++ qe.h        16 Jan 2014 14:24:04 -0000      1.123
@@ -299,12 +299,6 @@
 
 void get_str(const char **pp, char *buf, int buf_size, const char *stop);
 int css_get_enum(const char *str, const char *enum_str);
-int compose_keys(unsigned int *keys, int *nb_keys);
-int strtokey(const char **pp);
-int strtokeys(const char *keystr, unsigned int *keys, int max_keys);
-void keytostr(char *buf, int buf_size, int key);
-const char *keys_to_str(char *buf, int buf_size,
-                        unsigned int *keys, int nb_keys);
 int to_hex(int key);
 void color_completion(CompleteState *cp);
 int css_define_color(const char *name, const char *value);
@@ -376,6 +370,13 @@
     bp->len = bp->pos = 0;
     return bp;
 }
+static inline buf_t *buf_attach(buf_t *bp, char *buf, int size, int pos) {
+    /* assuming 0 <= pos < size */
+    bp->buf = buf;
+    bp->size = size;
+    bp->len = bp->pos = pos;
+    return bp;
+}
 static inline int buf_avail(buf_t *bp) {
     return bp->size - bp->pos - 1;
 }
@@ -449,6 +450,12 @@
     return b <= 0 ? 0 : (int)((long long)a * 100 / b);
 }
 
+int compose_keys(unsigned int *keys, int *nb_keys);
+int strtokey(const char **pp);
+int strtokeys(const char *keystr, unsigned int *keys, int max_keys);
+int buf_put_key(buf_t *out, int key);
+int buf_put_keys(buf_t *out, unsigned int *keys, int nb_keys);
+
 /* charset.c */
 
 /* maximum number of bytes for a character in all the supported charsets */
@@ -1376,7 +1383,7 @@
 void qe_register_cmd_table(CmdDef *cmds, ModeDef *m);
 int qe_register_binding(int key, const char *cmd_name, ModeDef *m);
 CmdDef *qe_find_cmd(const char *cmd_name);
-void qe_get_prototype(CmdDef *d, char *buf, int size);
+int qe_get_prototype(CmdDef *d, char *buf, int size);
 
 /* text display system */
 

Index: util.c
===================================================================
RCS file: /sources/qemacs/qemacs/util.c,v
retrieving revision 1.57
retrieving revision 1.58
diff -u -b -r1.57 -r1.58
--- util.c      2 Jan 2014 10:12:59 -0000       1.57
+++ util.c      16 Jan 2014 14:24:04 -0000      1.58
@@ -962,36 +962,44 @@
     return nb_keys;
 }
 
-void keytostr(char *buf, int buf_size, int key)
+int buf_put_key(buf_t *out, int key)
 {
-    int i;
-    char buf1[32];
-    buf_t out;
-
-    buf_init(&out, buf, buf_size);
+    int i, start = out->len;
 
     for (i = 0; i < countof(keycodes); i++) {
         if (keycodes[i] == key) {
-            buf_puts(&out, keystr[i]);
-            return;
+            return buf_puts(out, keystr[i]);
         }
     }
     if (key >= KEY_META(0) && key <= KEY_META(0xff)) {
-        keytostr(buf1, sizeof(buf1), key & 0xff);
-        buf_printf(&out, "M-%s", buf1);
+        buf_puts(out, "M-");
+        buf_put_key(out, key & 0xff);
     } else
     if (key >= KEY_CTRL('a') && key <= KEY_CTRL('z')) {
-        buf_printf(&out, "C-%c", key + 'a' - 1);
+        buf_printf(out, "C-%c", key + 'a' - 1);
     } else
 #if 0
     /* Cannot do this because KEY_F1..KEY_F20 are not consecutive */
     if (key >= KEY_F1 && key <= KEY_F20) {
-        buf_printf(&out, "f%d", key - KEY_F1 + 1);
+        buf_printf(out, "f%d", key - KEY_F1 + 1);
     } else
 #endif
     {
-        buf_putc_utf8(&out, key);
+        buf_putc_utf8(out, key);
+    }
+    return out->len - start;
+}
+
+int buf_put_keys(buf_t *out, unsigned int *keys, int nb_keys)
+{
+    int i, start = out->len;
+
+    for (i = 0; i < nb_keys; i++) {
+        if (i != 0)
+            buf_put_byte(out, ' ');
+        buf_put_key(out, keys[i]);
     }
+    return out->len - start;
 }
 
 int to_hex(int key)
@@ -1440,19 +1448,19 @@
              const char *s1, const char *s2)
 {
     const char *p, *q;
-    buf_t out;
+    buf_t outbuf, *out;
 
-    buf_init(&out, buf, buf_size);
+    out = buf_init(&outbuf, buf, buf_size);
 
     p = from;
     while ((q = strstr(p, s1)) != NULL) {
-        buf_write(&out, p, q - p);
-        buf_puts(&out, s2);
+        buf_write(out, p, q - p);
+        buf_puts(out, s2);
         p = q + strlen(s1);
     }
-    buf_puts(&out, p);
+    buf_puts(out, p);
 
-    return out.pos;
+    return out->pos;
 }
 
 int strquote(char *dest, int size, const char *str, int len)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]