<<< Date Index >>>     <<< Thread Index >>>

[PATCH] Create a "cd" command to change the current directory



Hello! (Long time no see, heh.)

I'd very grateful if the attached patch could be considered for
inclusion. It adds a very simple "cd" command that changes the current
working directory of the Mutt process.

I've missed this eg. when receiving a patch over email, wanting to apply
it with git-apply(1), and failing because Mutt was not started in the
right directory. It's easier to :cd from Mutt, than to quit, cd from the
shell *and* find the message again.

If you don't find this command suitable for inclusion, please let me
know why. :-)

Cheers,

-- 
Adeodato Simó                                     dato at net.com.org.es
Debian Developer                                  adeodato at debian.org
 
Debugging is twice as hard as writing the code in the first place. Therefore,
if you write the code as cleverly as possible, you are, by definition, not
smart enough to debug it.
                -- Brian W. Kernighan
# HG changeset patch
# User Adeodato Simó <dato@xxxxxxxxxxxxxx>
# Date 1230470440 -3600
# Branch HEAD
# Node ID bd0973548818abf31b25a70aacd225e831ad8560
# Parent  4917897ac9b10be922b449ed7aa8c4be058e4b5a
Create a "cd" command to change the current directory.

diff -r 4917897ac9b1 -r bd0973548818 doc/manual.xml.head
--- a/doc/manual.xml.head       Fri Dec 26 14:36:50 2008 +0100
+++ b/doc/manual.xml.head       Sun Dec 28 14:20:40 2008 +0100
@@ -3325,6 +3325,28 @@
 This command can be used to execute any function. Functions are
 listed in the <link linkend="functions">function reference</link>.
 <quote>exec function</quote> is equivalent to <quote>push 
&lt;function&gt;</quote>.
+</para>
+
+</sect1>
+
+<sect1 id="cd">
+<title>Changing the current directory</title>
+
+<para>Usage:</para>
+
+<cmdsynopsis>
+<command>cd</command>
+<arg choice="plain">
+<replaceable class="parameter">directory</replaceable>
+</arg>
+</cmdsynopsis>
+
+<para>
+This command can be used to change the current directory of the Mutt
+process. Further commands that reference a path, like
+<command><link linkend="source">source</link></command> or
+<command>cd</command> itself, will interpret non-absolute paths as
+relative to this directory.
 </para>
 
 </sect1>
@@ -6956,6 +6978,15 @@
 
 <listitem>
 <cmdsynopsis>
+<command><link linkend="cd">cd</link></command>
+<arg choice="plain">
+<replaceable class="parameter">directory</replaceable>
+</arg>
+</cmdsynopsis>
+</listitem>
+
+<listitem>
+<cmdsynopsis>
 <command><link linkend="charset-hook">charset-hook</link></command>
 <arg choice="plain">
 <replaceable class="parameter">alias</replaceable>
diff -r 4917897ac9b1 -r bd0973548818 init.c
--- a/init.c    Fri Dec 26 14:36:50 2008 +0100
+++ b/init.c    Sun Dec 28 14:20:40 2008 +0100
@@ -2333,6 +2333,30 @@
   return (source_rc (path, err));
 }
 
+static int parse_cd (BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err)
+{
+  char path[_POSIX_PATH_MAX];
+
+  if (mutt_extract_token (tmp, s, 0) != 0)
+  {
+    snprintf (err->data, err->dsize, _("cd: error at %s"), s->dptr);
+    return (-1);
+  }
+  if (MoreArgs (s))
+  {
+    strfcpy (err->data, _("cd: too many arguments"), err->dsize);
+    return (-1);
+  }
+  strfcpy (path, tmp->data, sizeof (path));
+  mutt_expand_path (path, sizeof (path));
+  if (chdir (path) != 0)
+  {
+    snprintf (err->data, err->dsize, "%s: %s", path, strerror (errno));
+    return (-1);
+  }
+  return (0);
+}
+
 /* line                command to execute
 
    token       scratch buffer to be used by parser.  caller should free
diff -r 4917897ac9b1 -r bd0973548818 init.h
--- a/init.h    Fri Dec 26 14:36:50 2008 +0100
+++ b/init.h    Sun Dec 28 14:20:40 2008 +0100
@@ -3278,6 +3278,7 @@
 static int parse_ignore (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 static int parse_unignore (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 static int parse_source (BUFFER *, BUFFER *, unsigned long, BUFFER *);
+static int parse_cd (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 static int parse_set (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 static int parse_my_hdr (BUFFER *, BUFFER *, unsigned long, BUFFER *);
 static int parse_unmy_hdr (BUFFER *, BUFFER *, unsigned long, BUFFER *);
@@ -3313,6 +3314,7 @@
   { "auto_view",       parse_list,             UL &AutoViewList },
   { "alternative_order",       parse_list,     UL &AlternativeOrderList},
   { "bind",            mutt_parse_bind,        0 },
+  { "cd",              parse_cd,               0 },
   { "charset-hook",    mutt_parse_hook,        M_CHARSETHOOK },
 #ifdef HAVE_COLOR
   { "color",           mutt_parse_color,       0 },