From 58312fffee425dfeac3b526ddcd54166480db02b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 24 Jul 2023 10:53:39 +0530 Subject: [PATCH] Also detect makefiles as plain text files --- kitty/guess_mime_type.py | 12 ++++++++---- tools/utils/mimetypes.go | 15 +++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/kitty/guess_mime_type.py b/kitty/guess_mime_type.py index 9a26517ed..b05238f7c 100644 --- a/kitty/guess_mime_type.py +++ b/kitty/guess_mime_type.py @@ -41,9 +41,14 @@ text_mimes = ( ) -def is_rc_file(path: str) -> bool: +def is_special_file(path: str) -> Optional[str]: name = os.path.basename(path) - return '.' not in name and name.endswith('rc') + lname = name.lower() + if lname == 'makefile' or lname.startswith('makefile.'): + return 'text/makefile' + if '.' not in name and name.endswith('rc'): + return 'text/plain' # rc file + return None def is_folder(path: str) -> bool: @@ -90,8 +95,7 @@ def guess_type(path: str, allow_filesystem_access: bool = False) -> Optional[str mt = known_extensions.get(ext) if mt in text_mimes: mt = f'text/{mt.split("/", 1)[-1]}' - if not mt and is_rc_file(path): - mt = 'text/plain' + mt = mt or is_special_file(path) if not mt: if is_dir: mt = 'inode/directory' # type: ignore diff --git a/tools/utils/mimetypes.go b/tools/utils/mimetypes.go index c742d9d65..026aaaf2a 100644 --- a/tools/utils/mimetypes.go +++ b/tools/utils/mimetypes.go @@ -54,9 +54,16 @@ var UserMimeMap = Once(func() map[string]string { return ans }) -func is_rcfile(path string) bool { +func is_special_file(path string) string { name := filepath.Base(path) - return strings.HasSuffix(name, "rc") && !strings.Contains(name, ".") + lname := strings.ToLower(name) + if lname == "makefile" || strings.HasPrefix(lname, "makefile.") { + return "text/makefile" + } + if strings.HasSuffix(name, "rc") && !strings.Contains(name, ".") { + return "text/plain" + } + return "" } func GuessMimeType(filename string) string { @@ -74,8 +81,8 @@ func GuessMimeType(filename string) string { if mime_with_parameters == "" { mime_with_parameters = KnownExtensions[lext] } - if mime_with_parameters == "" && is_rcfile(filename) { - mime_with_parameters = "text/plain" + if mime_with_parameters == "" { + mime_with_parameters = is_special_file(filename) } if mime_with_parameters == "" { return ""