From 9972058dc0c52466f37e7958a743d2f3fbe3b1a2 Mon Sep 17 00:00:00 2001 From: Andrea Date: Tue, 15 Oct 2019 13:16:29 +0200 Subject: [PATCH 1/2] Add support for file extension through argv, created syntax highlight applying standalone function --- README.md | 5 +++-- tte.c | 34 ++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 7f96777..396c2d9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # tte -tte (tiny text editor) is a terminal based text editor written in C from scratch, trying to be very minimalistic and dependency independent (it's not even using **curses**). +tte (tiny text editor) is a terminal based text editor written in C from scratch, trying to be very minimalistic and dependency independent (it's not even using **curses**). This project was mainly created for educational purposes, so is very commented! @@ -19,13 +19,14 @@ Download it from [here](https://github.com/GrenderG/tte/releases/latest), then ``` sudo mv tte /usr/local/bin/ sudo chmod +x /usr/local/bin/tte -``` +``` ## Usage ``` tte [file_name] tte -h | --help tte -v | --version +tte -e | --extension .sh bash_script ``` If you are planning to use special characters like (á, é, í, ó, ú, ¡, ¿, ...) you must use `ISO 8859-1` encoding in your terminal. See [this issue](https://github.com/GrenderG/tte/issues/2) for more info. diff --git a/tte.c b/tte.c index 1875960..2f41f62 100644 --- a/tte.c +++ b/tte.c @@ -105,6 +105,7 @@ struct editor_config { editor_row* row; int dirty; // To know if a file has been modified since opening. char* file_name; + char extension[10]; char status_msg[80]; time_t status_msg_time; char* copied_char_buffer; @@ -750,29 +751,39 @@ int editorSyntaxToColor(int highlight) { } } +void editorApplySyntaxHighlight() { + if (ec.syntax == NULL) + return; + + int file_row; + for (file_row = 0; file_row < ec.num_rows; file_row++) { + editorUpdateSyntax(&ec.row[file_row]); + } +} + void editorSelectSyntaxHighlight() { ec.syntax = NULL; if (ec.file_name == NULL) return; + char* ext_name = ec.extension[0] == '\0' ? ec.file_name : ec.extension; + for (unsigned int j = 0; j < HL_DB_ENTRIES; j++) { struct editor_syntax* es = &HL_DB[j]; unsigned int i = 0; while (es -> file_match[i]) { - char* p = strstr(ec.file_name, es -> file_match[i]); + char* p = strstr(ext_name, es -> file_match[i]); if (p != NULL) { // Returns a pointer to the first occurrence of str2 in str1, // or a null pointer if str2 is not part of str1. int pat_len = strlen(es -> file_match[i]); if (es -> file_match[i][0] != '.' || p[pat_len] == '\0') { ec.syntax = es; - - int file_row; - for (file_row = 0; file_row < ec.num_rows; file_row++) { - editorUpdateSyntax(&ec.row[file_row]); - } - + size_t len = strlen(es -> file_match[i]); + strncpy(ec.extension, es -> file_match[i], len); + // Apply the highlighting + editorApplySyntaxHighlight(); return; } } @@ -1095,7 +1106,7 @@ void editorSave() { editorSetStatusMessage("Save aborted"); return; } - editorSelectSyntaxHighlight(); + editorApplySyntaxHighlight(); } int len; @@ -1638,6 +1649,7 @@ void initEditor() { ec.row = NULL; ec.dirty = 0; ec.file_name = NULL; + ec.extension[0] = '\0'; ec.status_msg[0] = '\0'; ec.status_msg_time = 0; ec.copied_char_buffer = NULL; @@ -1687,6 +1699,10 @@ int handleArgs(int argc, char* argv[]) { } else if(strncmp("-v", argv[1], 2) == 0 || strncmp("--version", argv[1], 9) == 0) { printf("tte - version %s\n", TTE_VERSION); return -1; + } else if (strncmp("-e", argv[1], 2) == 0 || strncmp("--extension", argv[1], 11) == 0) { + size_t len = strlen(argv[2]); + strncpy(ec.extension, argv[2], len); + return 2; } } @@ -1698,6 +1714,8 @@ int main(int argc, char* argv[]) { int arg_response = handleArgs(argc, argv); if (arg_response == 1) editorOpen(argv[1]); + else if (arg_response == 2) + editorOpen(argv[3]); else if (arg_response == -1) return 0; enableRawMode(); From b8ec98424400e8c79e9e6c174f28e7d014a0bf3e Mon Sep 17 00:00:00 2001 From: Andrea Date: Tue, 15 Oct 2019 13:21:57 +0200 Subject: [PATCH 2/2] Restored editorSelectSyntaxHighlight call after a save --- tte.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tte.c b/tte.c index 2f41f62..9c32ac1 100644 --- a/tte.c +++ b/tte.c @@ -1106,7 +1106,7 @@ void editorSave() { editorSetStatusMessage("Save aborted"); return; } - editorApplySyntaxHighlight(); + editorSelectSyntaxHighlight(); } int len;