Skip to content

Commit 503c0af

Browse files
committed
(edit_load_syntax_file): create user syntax directory if it doesn't exist.
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
1 parent e85ca16 commit 503c0af

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

src/editor/editcmd.c

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
#include "lib/strutil.h" // utf string functions
5151
#include "lib/fileloc.h"
5252
#include "lib/lock.h"
53-
#include "lib/util.h" // tilde_expand()
53+
#include "lib/util.h" // tilde_expand(), x_basename()
5454
#include "lib/vfs/vfs.h"
5555
#include "lib/widget.h"
5656
#include "lib/event.h" // mc_event_raise()
@@ -763,6 +763,49 @@ editcmd_dialog_raw_key_query_cb (Widget *w, Widget *sender, widget_msg_t msg, in
763763
}
764764
}
765765

766+
/* --------------------------------------------------------------------------------------------- */
767+
768+
static gboolean
769+
editcmd_check_and_create_user_syntax_directory (const vfs_path_t *user_syntax_file_vpath)
770+
{
771+
gboolean ret;
772+
struct stat st;
773+
774+
ret = mc_stat (user_syntax_file_vpath, &st) == 0;
775+
if (!ret)
776+
{
777+
// file doesn't exist -- check why
778+
const char *user_syntax_file_path = vfs_path_as_str (user_syntax_file_vpath);
779+
780+
// check directory
781+
const char *user_syntax_file_basename = x_basename (user_syntax_file_path);
782+
char *user_syntax_dir;
783+
vfs_path_t *user_syntax_vpath;
784+
785+
user_syntax_dir =
786+
g_strndup (user_syntax_file_path, user_syntax_file_basename - user_syntax_file_path);
787+
user_syntax_vpath = vfs_path_from_str (user_syntax_dir);
788+
789+
ret = mc_stat (user_syntax_vpath, &st) == 0;
790+
if (!ret)
791+
ret = mc_mkdir (user_syntax_vpath, 0700) == 0;
792+
else if (!S_ISDIR (st.st_mode))
793+
{
794+
ret = FALSE;
795+
// set for file_error_message()
796+
errno = EEXIST;
797+
}
798+
799+
if (!ret)
800+
file_error_message (_ ("Cannot create directory\n%s"), user_syntax_dir);
801+
802+
vfs_path_free (user_syntax_vpath, TRUE);
803+
g_free (user_syntax_dir);
804+
}
805+
806+
return ret;
807+
}
808+
766809
/* --------------------------------------------------------------------------------------------- */
767810
/*** public functions ****************************************************************************/
768811
/* --------------------------------------------------------------------------------------------- */
@@ -1146,9 +1189,14 @@ edit_load_syntax_file (WDialog *h)
11461189
vfs_path_t *user_syntax_file_vpath;
11471190

11481191
user_syntax_file_vpath = mc_config_get_full_vpath (EDIT_SYNTAX_FILE);
1149-
check_for_default (extdir_vpath, user_syntax_file_vpath);
1150-
edit_arg_init (&arg, user_syntax_file_vpath, 0);
1151-
ret = edit_load_file_from_filename (h, &arg);
1192+
1193+
if (editcmd_check_and_create_user_syntax_directory (user_syntax_file_vpath))
1194+
{
1195+
check_for_default (extdir_vpath, user_syntax_file_vpath);
1196+
edit_arg_init (&arg, user_syntax_file_vpath, 0);
1197+
ret = edit_load_file_from_filename (h, &arg);
1198+
}
1199+
11521200
vfs_path_free (user_syntax_file_vpath, TRUE);
11531201
}
11541202
else if (dir == 1)

0 commit comments

Comments
 (0)