Skip to content

Commit

Permalink
Editor (Windows): support editor version detection
Browse files Browse the repository at this point in the history
  • Loading branch information
CarterLi committed May 24, 2024
1 parent 374997b commit 099e2b5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
72 changes: 55 additions & 17 deletions src/detection/editor/editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

#include <stdlib.h>

#ifdef _WIN32
#include <windows.h>
static inline char* realpath(const char* restrict file_name, char* restrict resolved_name)
{
return _fullpath(resolved_name, file_name, _MAX_PATH);
}
#endif

const char* ffDetectEditor(FFEditorResult* result)
{
ffStrbufSetS(&result->name, getenv("VISUAL"));
Expand All @@ -22,6 +30,21 @@ const char* ffDetectEditor(FFEditorResult* result)
}) != NULL || result->path.length == 0)
return NULL;
}
#else
if (!(result->name.length > 3 && ffCharIsEnglishAlphabet(result->name.chars[0]) && result->name.chars[1] == ':' && result->name.chars[2] == '\\'))
{
char buf[32];
uint32_t len = GetSystemDirectoryA(buf, sizeof(buf));
if (len < strlen("C:\\WINDOWS\\system32")) return NULL;
strncpy(buf + len, "\\where.exe", sizeof(buf) - len);
if (ffProcessAppendStdOut(&result->path, (char* const[]){
buf,
result->name.chars,
NULL,
}) != NULL || result->path.length == 0)
return NULL;
}
#endif
else
ffStrbufSet(&result->path, &result->name);

Expand All @@ -30,29 +53,45 @@ const char* ffDetectEditor(FFEditorResult* result)
return NULL;

ffStrbufSetS(&result->path, buf);
const char* exe = &result->path.chars[ffStrbufLastIndexC(&result->path, '/')];
if (!*exe) return NULL;
++exe;
result->exe = exe;

{
uint32_t index = ffStrbufLastIndexC(&result->path,
#ifndef _WIN32
'/'
#else
'\\'
#endif
);
if (index == result->path.length)
return NULL;
ffStrbufSetS(&result->exe, &result->path.chars[index + 1]);
if (!result->exe.length)
return NULL;

#ifdef _WIN32
if (ffStrbufEndsWithS(&result->exe, ".exe"))
ffStrbufSubstrBefore(&result->exe, result->exe.length - 4);
#endif
}

const char* param = NULL;
if (
ffStrEquals(exe, "nano") ||
ffStrEquals(exe, "vim") ||
ffStrEquals(exe, "nvim") ||
ffStrEquals(exe, "micro") ||
ffStrEquals(exe, "emacs") ||
ffStrStartsWith(exe, "emacs-") || // emacs-29.3
ffStrEquals(exe, "hx") ||
ffStrEquals(exe, "code") ||
ffStrEquals(exe, "sublime_text")
ffStrbufEqualS(&result->exe, "nano") ||
ffStrbufEqualS(&result->exe, "vim") ||
ffStrbufEqualS(&result->exe, "nvim") ||
ffStrbufEqualS(&result->exe, "micro") ||
ffStrbufEqualS(&result->exe, "emacs") ||
ffStrbufStartsWithS(&result->exe, "emacs-") || // emacs-29.3
ffStrbufEqualS(&result->exe, "hx") ||
ffStrbufEqualS(&result->exe, "code") ||
ffStrbufEqualS(&result->exe, "sublime_text")
) param = "--version";
else if (
ffStrEquals(exe, "kak") ||
ffStrEquals(exe, "pico")
ffStrbufEqualS(&result->exe, "kak") ||
ffStrbufEqualS(&result->exe, "pico")
) param = "-version";
else if (
ffStrEquals(exe, "ne")
ffStrbufEqualS(&result->exe, "ne")
) param = "-h";
else return NULL;

Expand Down Expand Up @@ -85,7 +124,6 @@ const char* ffDetectEditor(FFEditorResult* result)
break;
}
}
#endif

return NULL;
}
2 changes: 1 addition & 1 deletion src/detection/editor/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
typedef struct FFEditorResult
{
FFstrbuf name;
const char* exe;
FFstrbuf exe;
FFstrbuf path;
FFstrbuf version;
} FFEditorResult;
Expand Down
12 changes: 6 additions & 6 deletions src/modules/editor/editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void ffPrintEditor(FFEditorOptions* options)
FFEditorResult result = {
.name = ffStrbufCreate(),
.path = ffStrbufCreate(),
.exe = ffStrbufCreate(),
.version = ffStrbufCreate(),
};
const char* error = ffDetectEditor(&result);
Expand All @@ -23,9 +24,9 @@ void ffPrintEditor(FFEditorOptions* options)
}

ffPrintLogoAndKey(FF_EDITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
if (result.exe)
if (result.exe.length)
{
fputs(result.exe, stdout);
ffStrbufWriteTo(&result.exe, stdout);
if (result.version.length)
printf(" (%s)", result.version.chars);
}
Expand All @@ -37,6 +38,7 @@ void ffPrintEditor(FFEditorOptions* options)

ffStrbufDestroy(&result.name);
ffStrbufDestroy(&result.path);
ffStrbufDestroy(&result.exe);
ffStrbufDestroy(&result.version);
}

Expand Down Expand Up @@ -94,14 +96,12 @@ void ffGenerateEditorJsonResult(FF_MAYBE_UNUSED FFEditorOptions* options, yyjson
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
yyjson_mut_obj_add_strbuf(doc, obj, "name", &result.name);
yyjson_mut_obj_add_strbuf(doc, obj, "path", &result.path);
if (result.exe)
yyjson_mut_obj_add_strcpy(doc, obj, "exe", result.exe);
else
yyjson_mut_obj_add_null(doc, obj, "exe");
yyjson_mut_obj_add_strbuf(doc, obj, "exe", &result.exe);
yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);

ffStrbufDestroy(&result.name);
ffStrbufDestroy(&result.path);
ffStrbufDestroy(&result.exe);
ffStrbufDestroy(&result.version);
}

Expand Down

0 comments on commit 099e2b5

Please sign in to comment.