aboutsummaryrefslogtreecommitdiff
path: root/edit.c
blob: 9017831c12772da0c67b725ca30b97c5da307302 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "weekly.h"

int edit_file(const char *filename) {
    char editor[255] = {0};
    char editor_cmd[PATH_MAX] = {0};
    int result;
    const char *user_editor;

    // Allow the user to override the default editor (vi/notepad)
    user_editor = getenv("EDITOR");
    char *editor_path;
    if (user_editor != NULL) {
        sprintf(editor, "%s", user_editor);
    } else {
        editor_path = find_program("vim");
        if (editor_path != NULL) {
            sprintf(editor, "\"%s\"", editor_path);
        }
#if HAVE_WINDOWS
        else {
            strcpy(editor, "notepad");
        }
#endif
    }

    if (!strlen(editor)) {
        fprintf(stderr, "Unable to find editor: %s\n", editor);
        exit(1);
    }

    // Tell editor to jump to the end of the file (when supported)
    // Standard 'vi' does not support '+'
    if(strstr(editor, "vim")) {
        strcat(editor, " +");
    } else if (strstr(editor, "nano") != NULL) {
        strcat(editor, " +9999");
    }

    sprintf(editor_cmd, "%s %s", editor, filename);
    result = system(editor_cmd);
    return result;
}