]> granicus.if.org Git - neomutt/commitdiff
Add the "attach_save_dir" config variable 1468/head
authorPietro Cerutti <gahr@gahr.ch>
Fri, 30 Nov 2018 13:04:48 +0000 (13:04 +0000)
committerRichard Russon <rich@flatcap.org>
Tue, 4 Dec 2018 14:10:03 +0000 (14:10 +0000)
This specifies the location where attachments are saved. The default is
the current working directory.

- Suggest a directory even if the attachment doesn't have a name
- Do not clear the prompt buffer when modifying a suggested filename
- Default to "./" if attach_save_dir is empty or unset

Fixes #1465

init.h
recvattach.c
recvattach.h

diff --git a/init.h b/init.h
index bc28b95be0f6a363fa7e66b8bb2329a9826ae241..643b2ab289992c054f2664dcad22dd2bb341e9b1 100644 (file)
--- a/init.h
+++ b/init.h
@@ -321,6 +321,11 @@ struct ConfigDef MuttVars[] = {
   ** .pp
   ** For an explanation of ``soft-fill'', see the $$index_format documentation.
   */
+  { "attach_save_dir",  DT_PATH, R_NONE, &AttachSaveDir, IP "./" },
+  /*
+  ** .pp
+  ** The directory where attachments are saved.
+  */
   { "attach_sep",       DT_STRING,  R_NONE, &AttachSep, IP "\n" },
   /*
   ** .pp
index 15c94a684e5ed017bf8053afae9c6609e404c852..3b58c828df17f3dcd6dfa1eab611747a4f8d63c0 100644 (file)
@@ -64,6 +64,7 @@
 #endif
 
 /* These Config Variables are only used in recvattach.c */
+char *AttachSaveDir; ///< Config: Default directory where attachments are saved
 char *AttachSep; ///< Config: Separator to add between saved/printed/piped attachments
 bool AttachSplit;    ///< Config: Save/print/pipe tagged messages individually
 bool DigestCollapse; ///< Config: Hide the subparts of a multipart/digest
@@ -441,26 +442,34 @@ int attach_tag(struct Menu *menu, int sel, int act)
 }
 
 /**
- * prepend_curdir - Add './' to the beginning of a path
- * @param buf    Buffer for the result
+ * prepend_savedir - Add AttachSaveDir to the beginning of a path
+ * @param buf    Buffer for the result, must be valid
  * @param buflen Size of the buffer
  */
-static void prepend_curdir(char *buf, size_t buflen)
+static void prepend_savedir(char *buf, size_t bufsize)
 {
-  if (!buf || !*buf || (*buf == '/') || (buflen < 3) ||
-      /* XXX bad modularization, these are special to mutt_expand_path() */
-      !strchr("~=+@<>!-^", *buf))
+  const char *savedir = AttachSaveDir;
+
+  if (!savedir || !*savedir)
+    savedir = "./";
+
+  size_t savedirlen = strlen(savedir);
+  size_t buflen = strlen(buf);
+  bool addsep = (savedir[savedirlen - 1] != '/');
+  size_t newbuflen = savedirlen + buflen + addsep;
+
+  if (bufsize < newbuflen)
   {
     return;
   }
 
-  buflen -= 3;
-  size_t l = strlen(buf) + 2;
-  l = (l > buflen ? buflen : l);
-  memmove(buf + 2, buf, l);
-  buf[0] = '.';
-  buf[1] = '/';
-  buf[l + 2] = 0;
+  memmove(buf + savedirlen + addsep, buf, buflen);
+  memcpy(buf, savedir, savedirlen);
+  if (addsep)
+  {
+    buf[savedirlen] = '/';
+  }
+  buf[newbuflen] = '\0';
 }
 
 /**
@@ -496,12 +505,12 @@ static int query_save_attachment(FILE *fp, struct Body *body, struct Email *e, c
   else
     buf[0] = 0;
 
-  prepend_curdir(buf, sizeof(buf));
+  prepend_savedir(buf, sizeof(buf));
 
   prompt = _("Save to file: ");
   while (prompt)
   {
-    if (mutt_get_field(prompt, buf, sizeof(buf), MUTT_FILE | MUTT_CLEAR) != 0 || !buf[0])
+    if (mutt_get_field(prompt, buf, sizeof(buf), MUTT_FILE) != 0 || !buf[0])
     {
       mutt_clear_error();
       return -1;
@@ -592,9 +601,9 @@ void mutt_save_attachment_list(struct AttachCtx *actx, FILE *fp, bool tag,
           int append = 0;
 
           mutt_str_strfcpy(buf, mutt_path_basename(NONULL(top->filename)), sizeof(buf));
-          prepend_curdir(buf, sizeof(buf));
+          prepend_savedir(buf, sizeof(buf));
 
-          if (mutt_get_field(_("Save to file: "), buf, sizeof(buf), MUTT_FILE | MUTT_CLEAR) != 0 ||
+          if (mutt_get_field(_("Save to file: "), buf, sizeof(buf), MUTT_FILE) != 0 ||
               !buf[0])
           {
             return;
index f6c7410809e40bf2e65f4147c46bc0f9da1e5a1a..add715445b6b2b09af1bd0bf78d01dfc4cd20a50 100644 (file)
@@ -32,6 +32,7 @@ struct AttachCtx;
 struct Email;
 
 /* These Config Variables are only used in recvattach.c */
+extern char *AttachSaveDir;
 extern char *AttachSep;
 extern bool  AttachSplit;
 extern bool  DigestCollapse;