+ /* Strip trailing whitespace */
+ while (out > cmd2 && isspace (out[-1]))
+ *(--out) = 0;
+
+ return cmd2;
+}
+
+
+/* Returns a new string describing the shell command.
+ This may be just the name of the program, capitalized.
+ It also may be something from the resource database (gotten
+ by looking for "hacks.XYZ.name", where XYZ is the program.)
+ */
+char *
+make_hack_name (Display *dpy, const char *shell_command)
+{
+ char *s = strdup (shell_command);
+ char *s2;
+ char res_name[255];
+
+ for (s2 = s; *s2; s2++) /* truncate at first whitespace */
+ if (isspace (*s2))
+ {
+ *s2 = 0;
+ break;
+ }
+
+ s2 = strrchr (s, '/'); /* if pathname, take last component */
+ if (s2)
+ {
+ s2 = strdup (s2+1);
+ free (s);
+ s = s2;
+ }
+
+ if (strlen (s) > 50) /* 51 is hereby defined as "unreasonable" */
+ s[50] = 0;
+
+ sprintf (res_name, "hacks.%s.name", s); /* resource? */
+ s2 = get_string_resource (dpy, res_name, res_name);
+ if (s2)
+ {
+ free (s);
+ return s2;
+ }
+
+ for (s2 = s; *s2; s2++) /* if it has any capitals, return it */
+ if (*s2 >= 'A' && *s2 <= 'Z')
+ return s;
+
+ if (s[0] >= 'a' && s[0] <= 'z') /* else cap it */
+ s[0] -= 'a'-'A';
+ if (s[0] == 'X' && s[1] >= 'a' && s[1] <= 'z') /* (magic leading X) */
+ s[1] -= 'a'-'A';
+ if (s[0] == 'G' && s[1] == 'l' &&
+ s[2] >= 'a' && s[2] <= 'z') /* (magic leading GL) */
+ s[1] -= 'a'-'A',
+ s[2] -= 'a'-'A';
+ return s;
+}
+
+
+char *
+format_hack (Display *dpy, screenhack *hack, Bool wrap_p)
+{
+ int tab = 32;
+ int size;
+ char *h2, *out, *s;
+ int col = 0;
+
+ char *def_name = make_hack_name (dpy, hack->command);
+
+ /* Don't ever write out a name for a hack if it's the same as the default.
+ */
+ if (hack->name && !strcmp (hack->name, def_name))
+ {
+ free (hack->name);
+ hack->name = 0;
+ }
+ free (def_name);
+
+ size = (2 * (strlen(hack->command) +
+ (hack->visual ? strlen(hack->visual) : 0) +
+ (hack->name ? strlen(hack->name) : 0) +
+ tab));
+ h2 = (char *) malloc (size);
+ out = h2;
+
+ if (!hack->enabled_p) *out++ = '-'; /* write disabled flag */
+
+ if (hack->visual && *hack->visual) /* write visual name */
+ {
+ if (hack->enabled_p) *out++ = ' ';
+ *out++ = ' ';
+ strcpy (out, hack->visual);
+ out += strlen (hack->visual);
+ *out++ = ':';
+ *out++ = ' ';
+ }
+
+ *out = 0;
+ col = string_columns (h2, strlen (h2), 0);
+
+ if (hack->name && *hack->name) /* write pretty name */
+ {
+ int L = (strlen (hack->name) + 2);
+ if (L + col < tab)
+ out = stab_to (out, col, tab - L - 2);
+ else
+ *out++ = ' ';
+ *out++ = '"';
+ strcpy (out, hack->name);
+ out += strlen (hack->name);
+ *out++ = '"';
+ *out = 0;
+
+ col = string_columns (h2, strlen (h2), 0);
+ if (wrap_p && col >= tab)
+ out = stab_to (out, col, 77);
+ else
+ *out++ = ' ';
+
+ if (out >= h2+size) abort();
+ }
+
+ *out = 0;
+ col = string_columns (h2, strlen (h2), 0);
+ out = stab_to (out, col, tab); /* indent */
+
+ if (out >= h2+size) abort();
+ s = format_command (hack->command, wrap_p);
+ strcpy (out, s);
+ out += strlen (s);
+ free (s);
+ *out = 0;