JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Fix issue replacing certain configuration bindings.
[spectrwm.git] / spectrwm.c
index 082d04e..6b94bcb 100644 (file)
@@ -1304,7 +1304,7 @@ socket_setnonblock(int fd)
 }
 
 void
-bar_print(struct swm_region *r, char *s)
+bar_print(struct swm_region *r, const char *s)
 {
        int                     x = 0;
        size_t                  len;
@@ -1369,6 +1369,26 @@ bar_title_name(char *s, size_t sz, struct swm_region *r)
 }
 
 void
+bar_class_title_name(char *s, size_t sz, struct swm_region *r)
+{
+       if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
+               return;
+
+       bar_class_name(s, sz, r);
+       strlcat(s, ":", sz);
+       bar_title_name(s, sz, r);
+}
+
+void
+bar_window_float(char *s, size_t sz, struct swm_region *r)
+{
+       if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
+               return;
+       if (r->ws->focus->floating)
+               strlcat(s, "(f)", sz);
+}
+
+void
 bar_window_name(char *s, size_t sz, struct swm_region *r)
 {
        unsigned char           *title;
@@ -1378,11 +1398,7 @@ bar_window_name(char *s, size_t sz, struct swm_region *r)
        if ((title = get_win_name(r->ws->focus->id)) == NULL)
                return;
 
-       if (r->ws->focus->floating)
-               strlcat(s, "(f) ", sz);
        strlcat(s, (char *)title, sz);
-       strlcat(s, " ", sz);
-
        XFree(title);
 }
 
@@ -1430,7 +1446,7 @@ bar_workspace_name(char *s, size_t sz, struct swm_region *r)
 
 /* build the default bar format according to the defined enabled options */
 void
-bar_fmt(char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
+bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
 {
        /* if format provided, just copy the buffers */
        if (bar_format != NULL) {
@@ -1460,89 +1476,140 @@ bar_fmt(char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
        if (urgent_enabled)
                strlcat(fmtnew, "* +U*    ", sz);
 
-       if (title_class_enabled)
+       if (title_class_enabled) {
                strlcat(fmtnew, "+C", sz);
-       if (title_name_enabled) {
-               /* add a colon if showing the class and something is focused */
-               if (title_class_enabled && r != NULL && r->ws != NULL &&
-                   r->ws->focus != NULL)
-                       strlcat(fmtnew, ":", sz);
-               strlcat(fmtnew, "+T", sz);
+               if (title_name_enabled == 0)
+                       strlcat(fmtnew, "    ", sz);
        }
 
-       strlcat(fmtnew, "    ", sz);
-       if (window_name_enabled)
-               strlcat(fmtnew, "+W", sz);
+       /* checks needed by the colon and floating strlcat(3) calls below */
+       if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
+               if (title_name_enabled) {
+                       if (title_class_enabled)
+                               strlcat(fmtnew, ":", sz);
+                       strlcat(fmtnew, "+T    ", sz);
+               }
+               if (window_name_enabled) {
+                       if (r->ws->focus->floating)
+                               strlcat(fmtnew, "+F ", sz);
+                       strlcat(fmtnew, "+64W ", sz);
+               }
+       }
 
        /* finally add the action script output and the version */
-       strlcat(fmtnew, "     +A    +V", sz);
+       strlcat(fmtnew, "    +A    +V", sz);
 }
 
 /* replaces the bar format character sequences (like in tmux(1)) */
+char *
+bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
+    size_t sz)
+{
+       char                    *ptr;
+       char                    num[8], tmp[SWM_BAR_MAX];
+       int                     limit;
+       size_t                  len, numoff = 0;
+
+       /* reset strlcat(3) buffer */
+       *tmp = '\0';
+
+       /* get number, if any */
+       fmt++;
+       while (*fmt != '\0' && isdigit((unsigned char) *fmt)) {
+               if (numoff >= sizeof num - 1)
+                       break;
+               num[numoff++] = *fmt++;
+       }
+       num[numoff] = '\0';
+
+       if ((limit = strtonum(num, 1, sizeof tmp - 1, NULL)) == 0)
+               limit = sizeof tmp - 1;
+
+       /* if number is too big, skip to the first non-digit */
+       if (numoff >= sizeof num - 1) {
+               while (*fmt != '\0' && isdigit((unsigned char) *fmt))
+                       fmt++;
+       }
+       /* there is nothing to replace (ie EOL) */
+       if (*fmt == '\0')
+               return (fmt);
+
+       switch (*fmt) {
+       case 'A':
+               snprintf(tmp, sizeof tmp, "%s", bar_ext);
+               break;
+       case 'C':
+               bar_class_name(tmp, sizeof tmp, r);
+               break;
+       case 'D':
+               bar_workspace_name(tmp, sizeof tmp, r);
+               break;
+       case 'F':
+               bar_window_float(tmp, sizeof tmp, r);
+               break;
+       case 'I':
+               snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
+               break;
+       case 'N':
+               snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
+               break;
+       case 'P':
+               bar_class_title_name(tmp, sizeof tmp, r);
+               break;
+       case 'S':
+               snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
+               break;
+       case 'T':
+               bar_title_name(tmp, sizeof tmp, r);
+               break;
+       case 'U':
+               bar_urgent(tmp, sizeof tmp);
+               break;
+       case 'V':
+               snprintf(tmp, sizeof tmp, "%s", bar_vertext);
+               break;
+       case 'W':
+               bar_window_name(tmp, sizeof tmp, r);
+               break;
+       default:
+               /* unknown character sequence; copy as-is */
+               snprintf(tmp, sizeof tmp, "+%c", *fmt);
+               break;
+       }
+
+       len = strlen(tmp);
+       ptr = tmp;
+       if (len < limit)
+               limit = len;
+       while (limit-- > 0) {
+               if (*offrep >= sz - 1)
+                       break;
+               fmtrep[(*offrep)++] = *ptr++;
+       }
+
+       fmt++;
+       return (fmt);
+}
+
 void
 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
 {
-       char                    *ptr;
-       char                    tmp[SWM_BAR_MAX];
        size_t                  off;
 
        off = 0;
-       ptr = fmt;
-       while (*ptr != '\0') {
-               if (*ptr != '+') {
+       while (*fmt != '\0') {
+               if (*fmt != '+') {
                        /* skip ordinary characters */
                        if (off >= sz - 1)
                                break;
-                       fmtrep[off++] = *ptr++;
+                       fmtrep[off++] = *fmt++;
                        continue;
                }
 
                /* character sequence found; replace it */
-               *tmp = '\0';
-
-               switch (*++ptr) {
-               case 'A':
-                       snprintf(tmp, sizeof tmp, "%s", bar_ext);
-                       break;
-               case 'C':
-                       bar_class_name(tmp, sizeof tmp, r);
-                       break;
-               case 'D':
-                       bar_workspace_name(tmp, sizeof tmp, r);
+               fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
+               if (off >= sz - 1)
                        break;
-               case 'I':
-                       snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
-                       break;
-               case 'N':
-                       snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
-                       break;
-               case 'S':
-                       snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
-                       break;
-               case 'T':
-                       bar_title_name(tmp, sizeof tmp, r);
-                       break;
-               case 'U':
-                       bar_urgent(tmp, sizeof tmp);
-                       break;
-               case 'V':
-                       snprintf(tmp, sizeof tmp, "%s", bar_vertext);
-                       break;
-               case 'W':
-                       bar_window_name(tmp, sizeof tmp, r);
-                       break;
-               default:
-                       /* unknown character sequence; copy as-is */
-                       snprintf(tmp, sizeof tmp, "+%c", *ptr);
-                       break;
-               }
-
-               off += snprintf(fmtrep + off, sz - off, "%s", tmp);
-               if (off >= sz - 1) {
-                       off = sz;
-                       break;
-               }
-               ptr++;
        }
 
        fmtrep[off] = '\0';
@@ -1575,7 +1642,8 @@ bar_fmt_expand(char *fmtexp, size_t sz)
        strlcpy(fmtexp, fmt, sz);
        /* finally pass the string through strftime(3) */
 #ifndef SWM_DENY_CLOCK_FORMAT
-       len = strftime(fmtexp, sz, fmt, &tm);
+       if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
+               warnx("format too long");
        fmtexp[len] = '\0';
 #endif
 }
@@ -4326,7 +4394,6 @@ enum keyfuncid {
        kf_swap_next,
        kf_swap_prev,
        kf_spawn_term,
-       kf_spawn_menu,
        kf_quit,
        kf_restart,
        kf_focus_main,
@@ -4384,12 +4451,8 @@ enum keyfuncid {
        kf_bar_toggle,
        kf_wind_kill,
        kf_wind_del,
-       kf_screenshot_all,
-       kf_screenshot_wind,
        kf_float_toggle,
        kf_version,
-       kf_spawn_lock,
-       kf_spawn_initscr,
        kf_spawn_custom,
        kf_iconify,
        kf_uniconify,
@@ -4416,11 +4479,6 @@ dummykeyfunc(struct swm_region *r, union arg *args)
 {
 };
 
-void
-legacyfunc(struct swm_region *r, union arg *args)
-{
-};
-
 struct keyfunc {
        char                    name[SWM_FUNCNAME_LEN];
        void                    (*func)(struct swm_region *r, union arg *);
@@ -4442,7 +4500,6 @@ struct keyfunc {
        { "swap_next",          swapwin,        {.id = SWM_ARG_ID_SWAPNEXT} },
        { "swap_prev",          swapwin,        {.id = SWM_ARG_ID_SWAPPREV} },
        { "spawn_term",         spawnterm,      {.argv = spawn_term} },
-       { "spawn_menu",         legacyfunc,     {0} },
        { "quit",               quit,           {0} },
        { "restart",            restart,        {0} },
        { "focus_main",         focus,          {.id = SWM_ARG_ID_FOCUSMAIN} },
@@ -4500,12 +4557,8 @@ struct keyfunc {
        { "bar_toggle",         bar_toggle,     {0} },
        { "wind_kill",          wkill,          {.id = SWM_ARG_ID_KILLWINDOW} },
        { "wind_del",           wkill,          {.id = SWM_ARG_ID_DELETEWINDOW} },
-       { "screenshot_all",     legacyfunc,     {0} },
-       { "screenshot_wind",    legacyfunc,     {0} },
        { "float_toggle",       floating_toggle,{0} },
        { "version",            version,        {0} },
-       { "spawn_lock",         legacyfunc,     {0} },
-       { "spawn_initscr",      legacyfunc,     {0} },
        { "spawn_custom",       dummykeyfunc,   {0} },
        { "iconify",            iconify,        {0} },
        { "uniconify",          uniconify,      {0} },
@@ -4532,7 +4585,7 @@ struct key {
        enum keyfuncid          funcid;
        char                    *spawn_name;
 };
-RB_HEAD(key_list, key);
+RB_HEAD(key_tree, key);
 
 int
 key_cmp(struct key *kp1, struct key *kp2)
@@ -4550,8 +4603,8 @@ key_cmp(struct key *kp1, struct key *kp2)
        return (0);
 }
 
-RB_GENERATE_STATIC(key_list, key, entry, key_cmp);
-struct key_list                        keys;
+RB_GENERATE(key_tree, key, entry, key_cmp);
+struct key_tree                        keys;
 
 /* mouse */
 enum { client_click, root_click };
@@ -4575,7 +4628,7 @@ update_modkey(unsigned int mod)
        struct key              *kp;
 
        mod_key = mod;
-       RB_FOREACH(kp, key_list, &keys)
+       RB_FOREACH(kp, key_tree, &keys)
                if (kp->mod & ShiftMask)
                        kp->mod = mod | ShiftMask;
                else
@@ -4943,7 +4996,7 @@ key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
        kp->keysym = ks;
        kp->funcid = kfid;
        kp->spawn_name = strdupsafe(spawn_name);
-       RB_INSERT(key_list, &keys, kp);
+       RB_INSERT(key_tree, &keys, kp);
 
        DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
 }
@@ -4956,7 +5009,7 @@ key_lookup(unsigned int mod, KeySym ks)
        kp.keysym = ks;
        kp.mod = mod;
 
-       return (RB_FIND(key_list, &keys, &kp));
+       return (RB_FIND(key_tree, &keys, &kp));
 }
 
 void
@@ -4964,7 +5017,7 @@ key_remove(struct key *kp)
 {
        DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
 
-       RB_REMOVE(key_list, &keys, kp);
+       RB_REMOVE(key_tree, &keys, kp);
        free(kp->spawn_name);
        free(kp);
 
@@ -5227,7 +5280,7 @@ grabkeys(void)
                if (TAILQ_EMPTY(&screens[k].rl))
                        continue;
                XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
-               RB_FOREACH(kp, key_list, &keys) {
+               RB_FOREACH(kp, key_tree, &keys) {
                        if ((code = XKeysymToKeycode(display, kp->keysym)))
                                for (j = 0; j < LENGTH(modifiers); j++)
                                        XGrabKey(display, code,