[PATCH] Custom mc xterm title
yarodin
yarodin at gmail.com
Tue Aug 21 13:28:08 UTC 2007
This patch will added possibility to configure xterm title through MC_XTITLE
environment variable (like a zsh/bash promt)
%% - percent '%' character
%$,%# - '#' for root, '$' for normal user
%w,%d - working directory with edit/view filename
%W,%C - working directory (last component only) with edit/view filename
%p - process id (pid)
%u,%n - username
%h,m - hostname up to the first dot
%H,M - hostname.domainname
%t,%y - tty connected to stdin without /dev/
%l - tty connected to stdin without /dev/ and tty (if present)
%V - mc version
--- edit/editwidget.c.orig Fri May 27 20:19:18 2005
+++ edit/editwidget.c Thu Aug 16 12:59:49 2007
@@ -28,6 +28,9 @@
#include "../src/widget.h" /* redraw_labels() */
#include "../src/menu.h" /* menubar_new() */
#include "../src/key.h" /* is_idle() */
+#include "../src/main.h" /* xterm_filename */
+#include "../src/win.h" /* xterm_flag */
+#include "../src/layout.h" /* xterm_title */
WEdit *wedit;
struct WMenu *edit_menubar;
@@ -168,6 +171,11 @@ edit_file (const char *_file, int line)
static int made_directory = 0;
Dlg_head *edit_dlg;
WButtonBar *edit_bar;
+
+ if (xterm_flag && xterm_title) {
+ xterm_filename = _file;
+ update_xterm_title_path ();
+ }
if (option_backup_ext_int != -1) {
option_backup_ext = g_malloc (sizeof (int) + 1);
--- src/main.c.orig Sat Jul 23 22:52:02 2005
+++ src/main.c Thu Aug 16 16:00:33 2007
@@ -279,6 +279,8 @@ char *mc_home = NULL;
char cmd_buf[512];
+char *xterm_filename = NULL;
+
static void
reload_panelized (WPanel *panel)
{
@@ -1600,22 +1602,159 @@ midnight_callback (struct Dlg_head *h, d
#define xtoolkit_panel_setup()
-/* Show current directory in the xterm title */
+/* Show current directory in the xterm title
+ Added possibility to configure xterm title through MC_XTITLE environment variable (like a zsh/bash promt).
+ %% - percent '%' character
+ %$,%# - '#' for root, '$' for normal user
+ %w,%d - working directory with edit/view filename
+ %W,%C - working directory (last component only) with edit/view filename
+ %p - process id (pid)
+ %u,%n - username
+ %h,m - hostname up to the first dot
+ %H,M - hostname.domainname
+ %t,%y - tty connected to stdin without /dev/
+ %l - tty connected to stdin without /dev/ and tty (if present)
+ %V - mc version */
+
void
update_xterm_title_path (void)
{
+ int i = 0;
unsigned char *p, *s;
+ unsigned char title [BUF_MEDIUM+1],hostname[MAXHOSTNAMELEN+1],domainname[MAXHOSTNAMELEN+1],nametty[BUF_SMALL+1];
if (xterm_flag && xterm_title) {
- p = s = g_strdup (strip_home_and_password (current_panel->cwd));
+ /* Use special environment variable to format title */
+ if ((p = getenv ("MC_XTITLE")) == NULL) {
+ p = "mc - %n@%m:%w";
+ }
+
do {
+ if (*p != '%') {
+ title [i++] = *p;
+ continue;
+ }
+ if (!*++p)
+ break;
+
+ /* Substitute '%' special characters
+ * (meaning the same as for bash, but preceded by '%', not '\')
+ */
+ s = NULL;
+ switch (*p) {
+ case '%' : /* % - percent '%' character */
+ title [i++] = '%';
+ break;
+ case '#' :
+ case '$' : /* %$ or %# - '#' for root, '$' for normal user */
+ title [i++] = (geteuid()==0 ? '#' : '$');
+ break;
+ case 'd' :
+ case 'w' : /* %w or %d - working directory */
+ if (xterm_filename==NULL) {
+ s = g_strdup (strip_home_and_password (current_panel->cwd));
+ } else {
+ if (view_one_file == NULL) {
+ if (g_ascii_strcasecmp (strip_home_and_password (current_panel->cwd),"/")==0) {
+ s = g_strdup_printf("/%s",xterm_filename);
+ } else {
+ s = g_strdup_printf("%s/%s",strip_home_and_password (current_panel->cwd),xterm_filename);
+ }
+ } else {
+ s = g_strdup_printf("%s",strip_home_and_password (xterm_filename));
+ }
+ }
+ break;
+ case 'C' :
+ case 'W' : /* %W or %C- working directory (last component only) */
+ if (xterm_filename==NULL) {
+ s = g_strdup (g_path_get_basename ((strip_home_and_password (current_panel->cwd))));
+ } else {
+ if (view_one_file == NULL) {
+ s = g_strdup_printf ("%s/%s",x_basename (strip_home_and_password (current_panel->cwd)),xterm_filename);
+ } else {
+ s = g_strdup_printf ("%s/%s",x_basename (g_path_get_dirname (strip_home_and_password (xterm_filename))),x_basename(strip_home_and_password(xterm_filename)));
+ }
+ }
+ break;
+ case 'p' : /* %p - process id (pid) */
+ s = g_strdup_printf ("%d", getpid ());
+ break;
+ case 'n' :
+ case 'u' : /* %u or %n - username */
+ s = g_strdup (getpwuid (getuid ()) -> pw_name);
+ break;
+ case 'm' :
+ case 'h' : /* %h or %m - hostname up to the first dot */
+ if (gethostname (hostname,MAXHOSTNAMELEN) == 0) {
+ hostname [strcspn(hostname,".")] = '\0';
+ s = g_strdup(hostname);
+ }
+ break;
+ case 'M' :
+ case 'H' : /* %H or %M - hostname.domainname */
+ if (gethostname (hostname,MAXHOSTNAMELEN) == 0) {
+ hostname [MAXHOSTNAMELEN] = '\0';
+ if (getdomainname (domainname, MAXHOSTNAMELEN) == 0) {
+ domainname [MAXHOSTNAMELEN] = '\0';
+ if (strlen(g_strstrip(hostname))>0 && strlen(g_strstrip(domainname))==0) {
+ s = g_strdup(hostname);
+ } else if (strlen(g_strstrip(hostname))==0 && strlen(g_strstrip(domainname))>0) {
+ s = g_strdup (domainname);
+ } else if (strlen(g_strstrip(hostname))>0 && strlen(g_strstrip(domainname))>0) {
+ if (g_str_has_suffix (g_strstrip(hostname), ".") || g_str_has_prefix (g_strstrip(domainname), ".")) {
+ s = g_strconcat(hostname,domainname,NULL);
+ } else {
+ s = g_strconcat(hostname,".",domainname,NULL);
+ }
+ }
+ }
+ }
+ break;
+ case 'y' :
+ case 't' : /* %t or %y - tty connected to stdin without /dev/ */
+ s = g_strdup (x_basename(ttyname (0)));
+ break;
+ case 'l' : /* %l - tty connected to stdin */
+ if (ttyname_r (0,nametty,BUF_SMALL) == 0) {
+ nametty [BUF_SMALL] = '\0';
+ if (g_str_has_prefix (nametty, "/dev/tty")) {
+ strncpy(nametty,nametty+8,BUF_SMALL-8);
+ s = g_strdup(g_strstrip(nametty));
+ } else {
+ s = g_strdup(x_basename(nametty));
+ }
+ }
+ break;
+ case 'V' : /* %V - mc version */
+ s = g_strdup (VERSION);
+ break;
+ }
+
+ /* Append substituted string */
+ if (s) {
+ strncpy (title+i, s, BUF_MEDIUM-i);
+ title [BUF_MEDIUM] = '\0';
+ i = strlen (title);
+ g_free (s);
+ }
+ } while (*++p && i<BUF_MEDIUM);
+ title [i] = '\0';
+
+ /* Replace non-printable characters with '?' */
+ s = title;
+ while (*s) {
+
if (!is_printable (*s))
*s = '?';
- } while (*++s);
- fprintf (stdout, "\33]0;mc - %s\7", p);
+ s++;
+ }
+
+ /* Use xterm escape sequence to set window title */
+ fprintf (stdout, "\33]0;%s\7", title);
fflush (stdout);
- g_free (p);
}
+ xterm_filename=NULL;
}
/*
--- src/main.h.orig Fri Jul 1 21:47:06 2005
+++ src/main.h Thu Aug 16 13:05:33 2007
@@ -64,6 +64,7 @@ extern int alternate_plus_minus;
extern int only_leading_plus_minus;
extern int output_starts_shell;
extern int midnight_shutdown;
+extern char *xterm_filename;
extern char cmd_buf [512];
extern const char *shell;
--- src/view.c.orig Fri May 27 20:19:18 2005
+++ src/view.c Tue Aug 14 12:10:25 2007
@@ -2654,6 +2654,11 @@ view (const char *_command, const char *
WButtonBar *bar;
Dlg_head *view_dlg;
+ if (xterm_flag && xterm_title) {
+ xterm_filename = _file;
+ update_xterm_title_path ();
+ }
+
/* Create dialog and widgets, put them on the dialog */
view_dlg =
create_dlg (0, 0, LINES, COLS, NULL, view_dialog_callback,
More information about the mc-devel
mailing list