Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
15 changes: 9 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,16 @@ int main(int argc, char *argv[]) {
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");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -442,11 +446,10 @@ for (int i = 1; i < argc; i++) {
}
// 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 <vault's name> or --vault <vault's name>.");


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);
}
Expand Down
13 changes: 10 additions & 3 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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");
Expand Down
4 changes: 3 additions & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...);
Expand Down