Add support for file extension through argv, created syntax highlight applying standalone function
This commit is contained in:
1 parent
b0a99acf5f
commit
9972058dc0
2 files changed
+29
-10
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
# tte
|
# 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!
|
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 mv tte /usr/local/bin/
|
||||||
sudo chmod +x /usr/local/bin/tte
|
sudo chmod +x /usr/local/bin/tte
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
```
|
```
|
||||||
tte [file_name]
|
tte [file_name]
|
||||||
tte -h | --help
|
tte -h | --help
|
||||||
tte -v | --version
|
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.
|
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.
|
||||||
|
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ struct editor_config {
|
|||||||
editor_row* row;
|
editor_row* row;
|
||||||
int dirty; // To know if a file has been modified since opening.
|
int dirty; // To know if a file has been modified since opening.
|
||||||
char* file_name;
|
char* file_name;
|
||||||
|
char extension[10];
|
||||||
char status_msg[80];
|
char status_msg[80];
|
||||||
time_t status_msg_time;
|
time_t status_msg_time;
|
||||||
char* copied_char_buffer;
|
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() {
|
void editorSelectSyntaxHighlight() {
|
||||||
ec.syntax = NULL;
|
ec.syntax = NULL;
|
||||||
if (ec.file_name == NULL)
|
if (ec.file_name == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
char* ext_name = ec.extension[0] == '\0' ? ec.file_name : ec.extension;
|
||||||
|
|
||||||
for (unsigned int j = 0; j < HL_DB_ENTRIES; j++) {
|
for (unsigned int j = 0; j < HL_DB_ENTRIES; j++) {
|
||||||
struct editor_syntax* es = &HL_DB[j];
|
struct editor_syntax* es = &HL_DB[j];
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
|
|
||||||
while (es -> file_match[i]) {
|
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) {
|
if (p != NULL) {
|
||||||
// Returns a pointer to the first occurrence of str2 in str1,
|
// Returns a pointer to the first occurrence of str2 in str1,
|
||||||
// or a null pointer if str2 is not part of str1.
|
// or a null pointer if str2 is not part of str1.
|
||||||
int pat_len = strlen(es -> file_match[i]);
|
int pat_len = strlen(es -> file_match[i]);
|
||||||
if (es -> file_match[i][0] != '.' || p[pat_len] == '\0') {
|
if (es -> file_match[i][0] != '.' || p[pat_len] == '\0') {
|
||||||
ec.syntax = es;
|
ec.syntax = es;
|
||||||
|
size_t len = strlen(es -> file_match[i]);
|
||||||
int file_row;
|
strncpy(ec.extension, es -> file_match[i], len);
|
||||||
for (file_row = 0; file_row < ec.num_rows; file_row++) {
|
// Apply the highlighting
|
||||||
editorUpdateSyntax(&ec.row[file_row]);
|
editorApplySyntaxHighlight();
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1095,7 +1106,7 @@ void editorSave() {
|
|||||||
editorSetStatusMessage("Save aborted");
|
editorSetStatusMessage("Save aborted");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
editorSelectSyntaxHighlight();
|
editorApplySyntaxHighlight();
|
||||||
}
|
}
|
||||||
|
|
||||||
int len;
|
int len;
|
||||||
@@ -1638,6 +1649,7 @@ void initEditor() {
|
|||||||
ec.row = NULL;
|
ec.row = NULL;
|
||||||
ec.dirty = 0;
|
ec.dirty = 0;
|
||||||
ec.file_name = NULL;
|
ec.file_name = NULL;
|
||||||
|
ec.extension[0] = '\0';
|
||||||
ec.status_msg[0] = '\0';
|
ec.status_msg[0] = '\0';
|
||||||
ec.status_msg_time = 0;
|
ec.status_msg_time = 0;
|
||||||
ec.copied_char_buffer = NULL;
|
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) {
|
} else if(strncmp("-v", argv[1], 2) == 0 || strncmp("--version", argv[1], 9) == 0) {
|
||||||
printf("tte - version %s\n", TTE_VERSION);
|
printf("tte - version %s\n", TTE_VERSION);
|
||||||
return -1;
|
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);
|
int arg_response = handleArgs(argc, argv);
|
||||||
if (arg_response == 1)
|
if (arg_response == 1)
|
||||||
editorOpen(argv[1]);
|
editorOpen(argv[1]);
|
||||||
|
else if (arg_response == 2)
|
||||||
|
editorOpen(argv[3]);
|
||||||
else if (arg_response == -1)
|
else if (arg_response == -1)
|
||||||
return 0;
|
return 0;
|
||||||
enableRawMode();
|
enableRawMode();
|
||||||
|
|||||||
Reference in new issue
Block a user