From 1c11d63f020a1a6f2e20064027246cb55800f89f Mon Sep 17 00:00:00 2001
From: Kaz Kylheku <kaz@kylheku.com>
Date: Fri, 18 Sep 2015 06:45:50 -0700
Subject: linenoise: support suffix on temp file name.

This is so that when a command line is edited in an external
editor, editors can choose appropriate syntax highlighting
mode based on the suffix.

* linenoise/linenoise.c (struct lino_state): New member,
suffix.
(edit_in_editor): If we have the mkstemps function, then
use it to create a suffixed name.
(lino_set_tempfile_suffix): New function.

* linenoise/linenoise.h (lino_set_tempfile_suffix): Declared.
---
 linenoise/linenoise.c | 20 ++++++++++++++++++--
 linenoise/linenoise.h |  1 +
 2 files changed, 19 insertions(+), 2 deletions(-)

(limited to 'linenoise')

diff --git a/linenoise/linenoise.c b/linenoise/linenoise.c
index ff931121..d1973e94 100644
--- a/linenoise/linenoise.c
+++ b/linenoise/linenoise.c
@@ -88,6 +88,7 @@ struct lino_state {
     char buf[LINENOISE_MAX_DISP];       /* Displayed line bufer. */
     char data[LINENOISE_MAX_LINE];      /* True data corresponding to display */
     const char *prompt; /* Prompt to display. */
+    const char *suffix; /* Suffix when creating temp file. */
     size_t plen;        /* Prompt length. */
     size_t pos;         /* Current cursor position. */
     size_t len;         /* Current edited line display length. */
@@ -1022,14 +1023,24 @@ static void edit_in_editor(lino_t *l) {
     if (ed) {
         char *ho = getenv("HOME");
         int fd;
+#if HAVE_MKSTEMPS
+        const char *suffix = l->suffix ? l->suffix : "";
+#else
+        const char *suffix = "";
+#endif
 
         if (ho)
-            snprintf(path, sizeof path, "%s/%s", ho, template);
+            snprintf(path, sizeof path, "%s/%s%s", ho, template, suffix);
         else
-            snprintf(path, sizeof path, "%s", template);
+            snprintf(path, sizeof path, "%s%s", template, suffix);
 
+#if HAVE_MKSTEMPS
+        if ((fd = mkstemps(path, strlen(suffix))) != -1)
+            fo = fdopen(fd, "w");
+#else
         if ((fd = mkstemp(path)) != -1)
             fo = fdopen(fd, "w");
+#endif
 
         if (!fo && fd != -1)
             close(fd);
@@ -1486,6 +1497,11 @@ void lino_free(lino_t *ls)
     }
 }
 
+void lino_set_tempfile_suffix(lino_t *l, const char *suffix)
+{
+    l->suffix = suffix;
+}
+
 lino_error_t lino_get_error(lino_t *l)
 {
     return l->error;
diff --git a/linenoise/linenoise.h b/linenoise/linenoise.h
index 688ed64c..6471993f 100644
--- a/linenoise/linenoise.h
+++ b/linenoise/linenoise.h
@@ -61,6 +61,7 @@ lino_t *lino_copy(lino_t *);
 void lino_free(lino_t *);
 
 char *linenoise(lino_t *, const char *prompt);
+void lino_set_tempfile_suffix(lino_t *, const char *);
 lino_error_t lino_get_error(lino_t *);
 lino_error_t lino_set_error(lino_t *, lino_error_t); /* returns old */
 int lino_hist_add(lino_t *, const char *line);
-- 
cgit v1.2.3