Convert editor to C.

This commit is contained in:
Jonas 'Sortie' Termansen 2016-02-28 17:36:45 +01:00
parent 238f0cca0b
commit e004de8827
19 changed files with 124 additions and 141 deletions

View File

@ -5,10 +5,10 @@ include ../build-aux/version.mak
include ../build-aux/dirs.mak
OPTLEVEL?=$(DEFAULT_OPTLEVEL)
CXXFLAGS?=$(OPTLEVEL)
CFLAGS?=$(OPTLEVEL)
CPPFLAGS:=$(CPPFLAGS) -DVERSIONSTR=\"$(VERSION)\"
CXXFLAGS:=$(CXXFLAGS) -Wall -Wextra -fno-exceptions -fno-rtti
CFLAGS:=$(CFLAGS) -Wall -Wextra
BINARY=editor
@ -28,10 +28,10 @@ all: $(BINARY)
.PHONY: all install clean
$(BINARY): $(OBJS)
$(CXX) $(OBJS) -o $(BINARY) $(CXXFLAGS) $(LIBS)
$(CC) $(CFLAGS) $(OBJS) -o $(BINARY) $(LIBS)
%.o: %.c++
$(CXX) -std=gnu++11 $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
%.o: %.c
$(CC) -std=gnu11 $(CFLAGS) $(CPPFLAGS) -c $< -o $@
install: all
mkdir -p $(DESTDIR)$(BINDIR)

View File

@ -15,28 +15,25 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
command.c++
command.c
Editor commands.
*******************************************************************************/
#define __STDC_CONSTANT_MACROS
#define __STDC_FORMAT_MACROS
#define __STDC_LIMIT_MACROS
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#include "command.h++"
#include "cursor.h++"
#include "display.h++"
#include "editor.h++"
#include "multibyte.h++"
#include "terminal.h++"
#include "command.h"
#include "cursor.h"
#include "display.h"
#include "editor.h"
#include "multibyte.h"
#include "terminal.h"
void editor_type_newline(struct editor* editor)
{
@ -45,10 +42,11 @@ void editor_type_newline(struct editor* editor)
if ( editor->lines_used == editor->lines_length )
{
size_t new_length = editor->lines_length ? 2 * editor->lines_length : 8;
struct line* new_lines = new struct line[new_length];
struct line* new_lines = (struct line*)
malloc(sizeof(struct line) * new_length);
for ( size_t i = 0; i < editor->lines_used; i++ )
new_lines[i] = editor->lines[i];
delete[] editor->lines;
free(editor->lines);
editor->lines = new_lines;
editor->lines_length = new_length;
}
@ -65,19 +63,19 @@ void editor_type_newline(struct editor* editor)
struct line* keep_line = &editor->lines[editor->cursor_row];
struct line* move_line = &editor->lines[editor->cursor_row+1];
keep_line->data = new wchar_t[keep_length];
keep_line->data = (wchar_t*) malloc(sizeof(wchar_t) * keep_length);
keep_line->used = keep_length;
keep_line->length = keep_length;
memcpy(keep_line->data, old_line.data + 0, sizeof(wchar_t) * keep_length);
move_line->data = new wchar_t[move_length];
move_line->data = (wchar_t*) malloc(sizeof(wchar_t) * move_length);
move_line->used = move_length;
move_line->length = move_length;
memcpy(move_line->data, old_line.data + keep_length, sizeof(wchar_t) * move_length);
editor_cursor_set(editor, editor->cursor_row+1, 0);
delete[] old_line.data;
free(old_line.data);
}
void editor_type_delete_selection(struct editor* editor);
@ -96,7 +94,7 @@ void editor_type_combine_with_last(struct editor* editor)
wchar_t* gone_line_data = gone_line->data;
size_t new_length = keep_line->used + gone_line->used;
wchar_t* new_data = new wchar_t[new_length];
wchar_t* new_data = (wchar_t*) malloc(sizeof(wchar_t) * new_length);
memcpy(new_data, keep_line_data, sizeof(wchar_t) * keep_line->used);
memcpy(new_data + keep_line->used, gone_line_data, sizeof(wchar_t) * gone_line->used);
@ -153,7 +151,7 @@ void editor_type_combine_with_next(struct editor* editor)
wchar_t* gone_line_data = gone_line->data;
size_t new_length = keep_line->used + gone_line->used;
wchar_t* new_data = new wchar_t[new_length];
wchar_t* new_data = (wchar_t*) malloc(sizeof(wchar_t) * new_length);
memcpy(new_data, keep_line_data, sizeof(wchar_t) * keep_line->used);
memcpy(new_data + keep_line->used, gone_line_data, sizeof(wchar_t) * gone_line->used);
@ -706,10 +704,10 @@ void editor_type_raw_character(struct editor* editor, wchar_t c)
if ( current_line->used == current_line->length )
{
size_t new_length = current_line->length ? 2 * current_line->length : 8;
wchar_t* new_data = new wchar_t[new_length];
wchar_t* new_data = (wchar_t*) malloc(sizeof(wchar_t) * new_length);
for ( size_t i = 0; i < current_line->used; i++ )
new_data[i] = current_line->data[i];
delete[] current_line->data;
free(current_line->data);
current_line->data = new_data;
current_line->length = new_length;
}
@ -728,7 +726,7 @@ void editor_type_copy(struct editor* editor)
editor->cursor_column == editor->select_column )
return;
delete[] editor->clipboard;
free(editor->clipboard);
size_t start_row;
size_t start_column;
@ -767,7 +765,7 @@ void editor_type_copy(struct editor* editor)
}
}
editor->clipboard = new wchar_t[length + 1];
editor->clipboard = (wchar_t*) malloc(sizeof(wchar_t) * (length + 1));
size_t offset = 0;
for ( size_t row = start_row, column = start_column;
is_row_column_lt(row, column, end_row, end_column); )

View File

@ -15,13 +15,13 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
command.h++
command.h
Editor commands.
*******************************************************************************/
#ifndef EDITOR_COMMAND_HXX
#define EDITOR_COMMAND_HXX
#ifndef EDITOR_COMMAND_H
#define EDITOR_COMMAND_H
#include <stddef.h>

View File

@ -15,21 +15,18 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
cursor.c++
cursor.c
Editor cursor.
*******************************************************************************/
#define __STDC_CONSTANT_MACROS
#define __STDC_FORMAT_MACROS
#define __STDC_LIMIT_MACROS
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include "cursor.h++"
#include "display.h++"
#include "editor.h++"
#include "cursor.h"
#include "display.h"
#include "editor.h"
size_t editor_select_column_set(struct editor* editor, size_t x)
{

View File

@ -15,13 +15,13 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
cursor.h++
cursor.h
Editor cursor.
*******************************************************************************/
#ifndef EDITOR_CURSOR_HXX
#define EDITOR_CURSOR_HXX
#ifndef EDITOR_CURSOR_H
#define EDITOR_CURSOR_H
#include <stddef.h>

View File

@ -15,24 +15,21 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
display.c++
display.c
Display handling.
*******************************************************************************/
#define __STDC_CONSTANT_MACROS
#define __STDC_FORMAT_MACROS
#define __STDC_LIMIT_MACROS
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "display.h++"
#include "editor.h++"
#include "multibyte.h++"
#include "terminal.h++"
#include "display.h"
#include "editor.h"
#include "multibyte.h"
#include "terminal.h"
size_t editor_display_column_of_line_offset(struct editor* editor,
const struct line* line,
@ -93,17 +90,18 @@ struct display_char* expand_tabs(const wchar_t* str, size_t len, uint8_t* colors
size_t tabsize)
{
size_t ret_len = displayed_string_length(str, len, tabsize);
struct display_char* ret = new struct display_char[ret_len+1];
struct display_char* ret = (struct display_char*)
malloc(sizeof(struct display_char) * (ret_len + 1));
for ( size_t i = 0, j = 0; i < len; i++ )
{
uint8_t color = i < colors_len ? colors[i] : 7;
if ( str[i] == L'\t' )
do ret[j++] = { L' ', color};
do ret[j++] = (struct display_char) { L' ', color};
while ( j % tabsize );
else
ret[j++] = { str[i], color };
ret[j++] = (struct display_char) { str[i], color };
}
ret[ret_len] = { L'\0', 0 };
ret[ret_len] = (struct display_char) { L'\0', 0 };
if ( ret_len_ptr )
*ret_len_ptr = ret_len;
return ret;
@ -238,7 +236,7 @@ void render_editor(struct editor* editor, struct terminal_state* state)
is_blank && at_margin ? make_terminal_datum(L'|', 0x01) :
make_terminal_datum(c, color);
}
delete[] expanded;
free(expanded);
}
// Set the rest of the terminal state.

View File

@ -15,13 +15,13 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
display.h++
display.h
Display handling.
*******************************************************************************/
#ifndef EDITOR_DISPLAY_HXX
#define EDITOR_DISPLAY_HXX
#ifndef EDITOR_DISPLAY_H
#define EDITOR_DISPLAY_H
#include <stddef.h>
#include <stdint.h>

View File

@ -15,15 +15,11 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
editor.c++
editor.c
Editor.
*******************************************************************************/
#define __STDC_CONSTANT_MACROS
#define __STDC_FORMAT_MACROS
#define __STDC_LIMIT_MACROS
#include <sys/stat.h>
#include <assert.h>
@ -32,6 +28,7 @@
#include <limits.h>
#include <locale.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@ -39,14 +36,14 @@
#include <wchar.h>
#include <unistd.h>
#include "command.h++"
#include "cursor.h++"
#include "display.h++"
#include "editor.h++"
#include "highlight.h++"
#include "input.h++"
#include "modal.h++"
#include "terminal.h++"
#include "command.h"
#include "cursor.h"
#include "display.h"
#include "editor.h"
#include "highlight.h"
#include "input.h"
#include "modal.h"
#include "terminal.h"
void initialize_editor(struct editor* editor)
{
@ -80,7 +77,8 @@ void initialize_editor(struct editor* editor)
editor->lines_used = 1;
editor->lines_length = 1;
editor->lines = new struct line[editor->lines_length];
editor->lines =
(struct line*) malloc(sizeof(struct line) * editor->lines_length);
editor->lines[0].data = NULL;
editor->lines[0].used = 0;
editor->lines[0].length = 0;
@ -134,12 +132,13 @@ void editor_load_config(struct editor* editor)
void editor_reset_contents(struct editor* editor)
{
for ( size_t i = 0; i < editor->lines_used; i++ )
delete[] editor->lines[i].data;
delete[] editor->lines;
free(editor->lines[i].data);
free(editor->lines);
editor->lines_used = 1;
editor->lines_length = 1;
editor->lines = new struct line[editor->lines_length];
editor->lines =
(struct line*) malloc(sizeof(struct line) * editor->lines_length);
editor->lines[0].data = NULL;
editor->lines[0].used = 0;
editor->lines[0].length = 0;
@ -197,7 +196,8 @@ bool editor_load_file_contents(struct editor* editor, FILE* fp)
bool editor_load_file(struct editor* editor, const char* path)
{
if ( FILE* fp = fopen(path, "r") )
FILE* fp;
if ( (fp = fopen(path, "r")) )
{
bool success = editor_load_file_contents(editor, fp);
fclose(fp);

View File

@ -15,19 +15,19 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
editor.h++
editor.h
Editor.
*******************************************************************************/
#ifndef EDITOR_EDITOR_HXX
#define EDITOR_EDITOR_HXX
#ifndef EDITOR_EDITOR_H
#define EDITOR_EDITOR_H
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include "highlight.h++"
#include "highlight.h"
struct line
{

View File

@ -15,23 +15,21 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
highlight.c++
highlight.c
Syntax highlighting.
*******************************************************************************/
#define __STDC_CONSTANT_MACROS
#define __STDC_FORMAT_MACROS
#define __STDC_LIMIT_MACROS
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#include "editor.h++"
#include "highlight.h++"
#include "editor.h"
#include "highlight.h"
enum language language_of_path(const char* path)
{
@ -143,8 +141,8 @@ void editor_colorize(struct editor* editor)
editor->highlight_source == LANGUAGE_NONE )
{
for ( size_t i = 0; i < editor->color_lines_used; i++ )
delete[] editor->color_lines[i].data;
delete[] editor->color_lines;
free(editor->color_lines[i].data);
free(editor->color_lines);
editor->color_lines_used = 0;
editor->color_lines_length = 0;
editor->color_lines = NULL;
@ -155,7 +153,9 @@ void editor_colorize(struct editor* editor)
if ( !editor->color_lines )
{
if ( !(editor->color_lines = new struct color_line[editor->lines_used]) )
editor->color_lines = (struct color_line*)
malloc(sizeof(struct color_line) * editor->lines_used);
if ( !editor->color_lines )
return;
editor->color_lines_used = editor->lines_used;
editor->color_lines_length = editor->lines_used;
@ -169,11 +169,12 @@ void editor_colorize(struct editor* editor)
if ( editor->color_lines[i].length == editor->lines[i].used )
continue;
if ( !(editor->color_lines[i].data = new uint8_t[editor->lines[i].used]) )
editor->color_lines[i].data = (uint8_t*) malloc(editor->lines[i].used);
if ( !editor->color_lines[i].data )
{
for ( size_t n = 0; n < i; i++ )
delete[] editor->color_lines[n].data;
delete[] editor->color_lines;
free(editor->color_lines[n].data);
free(editor->color_lines);
editor->color_lines_used = 0;
editor->color_lines_length = 0;
editor->color_lines = NULL;

View File

@ -15,13 +15,13 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
highlight.h++
highlight.h
Syntax highlighting.
*******************************************************************************/
#ifndef EDITOR_HIGHLIGHT_HXX
#define EDITOR_HIGHLIGHT_HXX
#ifndef EDITOR_HIGHLIGHT_H
#define EDITOR_HIGHLIGHT_H
struct editor;

View File

@ -15,28 +15,25 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
input.c++
input.c+
Keyboard input.
*******************************************************************************/
#define __STDC_CONSTANT_MACROS
#define __STDC_FORMAT_MACROS
#define __STDC_LIMIT_MACROS
#if defined(__sortix__)
#include <sys/keycodes.h>
#include <sys/termmode.h>
#endif
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
#include "command.h++"
#include "editor.h++"
#include "input.h++"
#include "modal.h++"
#include "command.h"
#include "editor.h"
#include "input.h"
#include "modal.h"
void editor_codepoint(struct editor* editor, uint32_t codepoint)
{
@ -184,7 +181,8 @@ void editor_input_process(struct editor_input* editor_input,
uint32_t input;
if ( read(0, &input, sizeof(input)) != sizeof(input) )
return;
if ( int kbkey = KBKEY_DECODE(input) )
int kbkey;
if ( (kbkey = KBKEY_DECODE(input)) )
editor_kbkey(editor, kbkey);
else
editor_codepoint(editor, input);

View File

@ -15,13 +15,13 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
input.h++
input.h
Keyboard input.
*******************************************************************************/
#ifndef EDITOR_INPUT_HXX
#define EDITOR_INPUT_HXX
#ifndef EDITOR_INPUT_H
#define EDITOR_INPUT_H
struct editor;

View File

@ -15,28 +15,25 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
modal.c++
modal.c
Modal commands.
*******************************************************************************/
#define __STDC_CONSTANT_MACROS
#define __STDC_FORMAT_MACROS
#define __STDC_LIMIT_MACROS
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <wctype.h>
#include "command.h++"
#include "cursor.h++"
#include "editor.h++"
#include "highlight.h++"
#include "modal.h++"
#include "multibyte.h++"
#include "command.h"
#include "cursor.h"
#include "editor.h"
#include "highlight.h"
#include "modal.h"
#include "multibyte.h"
static void editor_reset_modal(struct editor* editor)
{

View File

@ -15,13 +15,13 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
modal.h++
modal.h
Modal commands.
*******************************************************************************/
#ifndef EDITOR_MODAL_HXX
#define EDITOR_MODAL_HXX
#ifndef EDITOR_MODAL_H
#define EDITOR_MODAL_H
struct editor;

View File

@ -15,22 +15,19 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
multibyte.c++
multibyte.c
Conversion from multibyte strings to wide strings.
*******************************************************************************/
#define __STDC_CONSTANT_MACROS
#define __STDC_FORMAT_MACROS
#define __STDC_LIMIT_MACROS
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "multibyte.h++"
#include "multibyte.h"
wchar_t* convert_mbs_to_wcs(const char* mbs)
{

View File

@ -15,13 +15,13 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
multibyte.h++
multibyte.h
Conversion from multibyte strings to wide strings.
*******************************************************************************/
#ifndef EDITOR_MULTIBYTE_HXX
#define EDITOR_MULTIBYTE_HXX
#ifndef EDITOR_MULTIBYTE_H
#define EDITOR_MULTIBYTE_H
#include <stddef.h>

View File

@ -15,16 +15,13 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
terminal.c++
terminal.c
Terminal handling.
*******************************************************************************/
#define __STDC_CONSTANT_MACROS
#define __STDC_FORMAT_MACROS
#define __STDC_LIMIT_MACROS
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
@ -33,7 +30,7 @@
#include <wchar.h>
#include <termios.h>
#include "terminal.h++"
#include "terminal.h"
void update_terminal_color(FILE* fp, uint8_t desired_color,
struct terminal_state* current)

View File

@ -15,13 +15,13 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
terminal.h++
terminal.h
Terminal handling.
*******************************************************************************/
#ifndef EDITOR_TERMINAL_HXX
#define EDITOR_TERMINAL_HXX
#ifndef EDITOR_TERMINAL_H
#define EDITOR_TERMINAL_H
#include <stddef.h>
#include <stdint.h>