diff options
-rw-r--r-- | main.c | 73 |
1 files changed, 65 insertions, 8 deletions
@@ -376,14 +376,31 @@ int main(int argc, char* argv[]){ case 'i': case 's': { char* number = NULL; + contact* currentContact = getContactSelected(); + char* contactName = "???"; + sms* lastMessage = NULL; - if( ! currentConv ) { - contact* tmp = getContactSelected(); - if( !tmp ) + if( currentContact ) + contactName = currentContact->display_name; + + if( currentConv ) { + number = currentConv->number; + int tmpLastIndex = currentConv->nbSMS - 1; + + while(tmpLastIndex >= 0 && currentConv->listSMS[tmpLastIndex].in_out) + tmpLastIndex--; + + if( tmpLastIndex >= 0 ) + lastMessage = ¤tConv->listSMS[tmpLastIndex]; + + } + else { + if( !contactName ) { + // Should be a repported error break; - number = tmp->number; + } + number = currentContact->number; } - else number = currentConv->number; const char* defaultEditor = "vim", * const envEditor = getenv("EDITOR"), @@ -397,7 +414,21 @@ int main(int argc, char* argv[]){ FILE* tmp = NULL; char fileName[] = "mesms_XXXXXX"; int fd = mkstemp(fileName); - close(fd); + { + FILE* infos = fdopen(fd, "w"); + fputc('\n', infos); + if( lastMessage && lastMessage->text) { + fprintf(infos, "\n\n# >"); + for(int i = 0; lastMessage->text[i]; ++i) { + if(lastMessage->text[i] == '\n') + fprintf(infos, "\n# >"); + else + fputc(lastMessage->text[i], infos); + } + } + fprintf(infos, "\n#\n# %s", contactName); + fclose(infos); + } char* cmd = malloc(2 + strlen(defaultEditor) + strlen(fileName) ); @@ -405,17 +436,45 @@ int main(int argc, char* argv[]){ edit: system(cmd); + free(cmd); { FILE* r = fopen(fileName,"r"); FILE* t = tmpfile(); int c = 0; int nb = 0; + int lastCR = -1; + fpos_t posLastCR = {}; + while( (c = fgetc(r)) != EOF ) { + if(c == '\n') { + bool isEnd = 1; + lastCR = nb; + fgetpos(r, &posLastCR); + int C = c; + + while( (C = fgetc(r)) != EOF) { + if(C == '\n'); + else if(C == '#') + while( (C = fgetc(r)) != '\n' && C != EOF); + else { + isEnd = 0; + break; + } + } + + if(isEnd) { + break; + } else { + fsetpos(r, &posLastCR); + } + } + nb++; fputc(c,t); } char* string = malloc( nb+1 ); fclose(r); + unlink( fileName ); rewind(t); nb = 0; while( (c = fgetc(t)) != EOF ) { @@ -448,8 +507,6 @@ edit: goto edit; } } - free(cmd); - unlink( fileName ); resize(); } case KEY_HOME: |