diff --git a/README.md b/README.md index efb0079..7848846 100644 --- a/README.md +++ b/README.md @@ -184,7 +184,7 @@ Edit `~/.config/notewrapper/config.json`. If it does not exist, it will be creat * `directory`: root directory containing all vaults * `render`: enable/disable Vivify rendering * `jumpToEndOfFileOnLaunch`: move cursor to end of file on open -* `editor`: selected editor (must be supported) +* `editor`: selected editor (must be supported). If not set, it defaults to `$EDITOR`. * `journalRegex`: regex used to detect journal files * `dateEntry`: format for journal entries (see `strftime`) * `newLineOnOpening`: add a newline when opening a note @@ -209,4 +209,4 @@ It is recommended to use a browser different from your main one for rendering. * [ ] A converter between journal types * [ ] Support multiple vault directories * [ ] Port NoteWrapper to other editors (non-exhaustive list of planned ports: `emacs -nw`, `jed`, `ad`, flow-control, `ee`, `amp`, `dte`, `cano`, `mle`, `zee`, `ptext`, `kibi`, `ox`, `ne`, `dit`, `zile`, `moe`, `joe`, `pico`, `vis`) -* [ ] Default to $EDITOR +* [x] Default to $EDITOR diff --git a/src/main.c b/src/main.c index 94a5b2a..f84e55d 100644 --- a/src/main.c +++ b/src/main.c @@ -136,14 +136,16 @@ arg_next: debug("In %s, \"jumpToEndOfFileOnLaunch\" wasn't set or we encountered a abnormal type. Defaulting to true.", configPath); } - char *editorToOpen = "neovim"; // default + char *editorToOpen = getenv("EDITOR"); // default to $EDITOR + int defaultEditor = 1; // this will be used in the warning if the editor does not exists or is unsupported. cJSON *editorToOpenJSON = cJSON_GetObjectItem(json, "editor"); if (editorToOpenJSON && cJSON_IsString(editorToOpenJSON)) { editorToOpen = strdup(cJSON_GetStringValue(editorToOpenJSON)); // we must strdup and not just = as we will free all the json after (before parsing args) + defaultEditor = 0; debug("In %s, \"editor\" was set to %s.", configPath, editorToOpen); - error(!isStringInArray(editorToOpen, supportedEditor, numEditors), "user", "%s (fetched from config.json) is not a supported editor.", editorToOpen); + //error(!isStringInArray(editorToOpen, supportedEditor, numEditors), "user", "%s (fetched from config.json) is not a supported editor.", editorToOpen); // we check if editor is supported at the end } else { - debug("In %s, \"editor\" wasn't set or we encountered a abnormal type. Defaulting to %s.", configPath, editorToOpen); + debug("In %s, \"editor\" wasn't set or we encountered a abnormal type. Defaulting to $EDITOR (%s).\n P.S. this might still be overwritten by -e or --editor.", configPath, editorToOpen); } cJSON *journalRegexJSON = cJSON_GetObjectItem(json, "journalRegex"); @@ -274,6 +276,7 @@ for (int i = 1; i < argc; i++) { } else if (strcmp(arg, "--editor") == 0) { error(i + 1 == argc, "user", "Missing argument for --editor"); editorToOpen = argv[++i]; + defaultEditor = 0; debug("--editor set to %s", editorToOpen); } else if (strcmp(arg, "--note") == 0) { @@ -404,6 +407,7 @@ for (int i = 1; i < argc; i++) { case 'e': editorToOpen = value; + defaultEditor = 0; debug("-e set editor to %s", value); break; @@ -442,11 +446,10 @@ next_arg: } // if -n or --note is set but not -v or --vaults it gives an error error(!bypassSelectionVault && bypassSelectionNote, "user", "If you want to specify the note, you must also specify the vault with -v or --vault ."); - - - error(!doesEditorExist(editorToOpen, shouldDebug), "user", "%s is either not in your path or not installed.", editorToOpen); debug("Finished parsing the attribute flags"); + isEditorValid(editorToOpen, defaultEditor, shouldDebug); // check if editor is supported and if it is installed. If not, it will throw an error. + if (doesBackup) { handleBackups(notesDirectoryString, pathToBackup, homedir, interval, (const char**)rsyncArgs, rsyncArgsNumber, shouldDebug); } diff --git a/src/utils.c b/src/utils.c index fd7836c..2bf8d9b 100644 --- a/src/utils.c +++ b/src/utils.c @@ -206,9 +206,16 @@ void handleBackups(const char *pathOfVaults, const char *pathOfBackup, const cha } } -int doesEditorExist (char *editorToCheck, int shouldDebug) { // Some exectuables have not exaclty the same name as the editor. - char *editor; - if (strcmp(editorToCheck, "neovim") == 0) { +int isEditorValid (char *editorToCheck, int useDefaultEditor, int shouldDebug) { // check if editor is supported and if it is installed + // check if supported + if (useDefaultEditor) { // we use a different error message if it defaulted to $EDITOR + error(!isStringInArray(editorToCheck, supportedEditor, numEditors), "user", "%s is not a supported editor.\n(defaulted to $EDITOR as neither \"editor\" was set in the configuration file nor -e/--editor was set.)\n See https://github.com/tomasriveral/NoteWrapper#editor-support for a list of supported editors.", editorToCheck); + } else { + error(!isStringInArray(editorToCheck, supportedEditor, numEditors), "user", "%s is not a supported editor.\n See https://github.com/tomasriveral/NoteWrapper#editor-support for a list of supported editors.", editorToCheck); + } + // check if installed + char *editor; + if (strcmp(editorToCheck, "neovim") == 0) { // some executables are not name the same as the project editor = strdup("nvim"); // we must use strdup and not just copy as we would have modified editorToOpen in main } else if (strcmp(editorToCheck, "helix") == 0) { editor = strdup("hx"); diff --git a/src/utils.h b/src/utils.h index e8b4704..08f4141 100644 --- a/src/utils.h +++ b/src/utils.h @@ -43,9 +43,11 @@ int compareString(const void *a, const void *b); // compares two strings in reversed alphabetical order // this function is used for qsort int reverseCompareString(const void *a, const void *b); +// checks if editor is supported and if it installed. // this basically checks all the dirs from your path for the editor. This is a safety check. // If the executable from an editor is not the editor name (for example neovim and nvim), you must handle at the start of the function. -int doesEditorExist(char *editorToCheck, int debug); +// This can return an error and stop the program. +int isEditorValid(char *editorToCheck, int useDefaultEditor, int debug); // please use the macro debug instead of _debug. //formated debugging. void _debug(const int d, const char *file, const int line, const char *function, const char *message, ...);