Add editor(5).

This commit is contained in:
Jonas 'Sortie' Termansen 2016-01-01 15:01:39 +00:00
parent ee971b463b
commit 01ee906d25
6 changed files with 107 additions and 4 deletions

View File

@ -38,6 +38,8 @@ install: all
install $(BINARY) $(DESTDIR)$(BINDIR)
mkdir -p $(DESTDIR)$(MANDIR)/man1
install editor.1 $(DESTDIR)$(MANDIR)/man1/editor.1
mkdir -p $(DESTDIR)$(MANDIR)/man5
install editor.5 $(DESTDIR)$(MANDIR)/man5/editor.5
clean:
rm -f $(BINARY) $(OBJS) *.o

View File

@ -54,8 +54,22 @@ Open new file containing output of running
.It Sy tabsize Ar tab-size
Select tab size.
.El
.Pp
A subset of these commands can be stored in the
.Xr editor 5
configuration file loaded on
.Nm
startup.
.Sh FILES
.Bl -tag -width "$HOME/.editor" -compact
.It Pa /etc/editor
Global configuration.
.It Pa ~/.editor
User configuration.
.El
.Sh SEE ALSO
.Xr pager 1
.Xr pager 1 ,
.Xr editor 5
.Sh BUGS
.Nm
lacks a number of crucial features, such as undo and redo, ability to open

26
editor/editor.5 Normal file
View File

@ -0,0 +1,26 @@
.Dd $Mdocdate: January 8 2015 $
.Dt EDITOR 5
.Os
.Sh NAME
.Nm editor
.Nd editor configuration
.Sh SYNOPSIS
.Nm /etc/editor
.Nm ~/.editor
.Sh DESCRIPTION
.Xr editor 1
loads these files on startup and executes the commands therein as described in
.Xr editor 1 . Only commands applicable as global configuration are executed to
avoid undesirable side effects. For instance,
.Sy popen
isn't executed as that would open another file than the current one. Unknown
commands are silently ignored.
.Sh FILES
.Bl -tag -width "$HOME/.editor" -compact
.It Pa /etc/editor
Global configuration.
.It Pa ~/.editor
User configuration.
.El
.Sh SEE ALSO
.Xr editor 1

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014, 2015.
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014, 2015, 2016.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
@ -31,6 +31,7 @@
#include <error.h>
#include <limits.h>
#include <locale.h>
#include <pwd.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@ -44,6 +45,7 @@
#include "editor.h++"
#include "highlight.h++"
#include "input.h++"
#include "modal.h++"
#include "terminal.h++"
void initialize_editor(struct editor* editor)
@ -88,6 +90,47 @@ void initialize_editor(struct editor* editor)
editor->color_lines = NULL;
}
void editor_load_config_path(struct editor* editor, const char* path)
{
FILE* fp = fopen(path, "r");
if ( !fp )
{
if ( errno != ENOENT )
error(0, errno, "%s", path);
return;
}
char* line = NULL;
size_t line_size = 0;
ssize_t line_length = 0;
while ( 0 <= (errno = 0, line_length = getline(&line, &line_size, fp)) )
{
if ( line[line_length - 1] == '\n' )
line[--line_length] = '\0';
line_length = strcspn(line, "#");
line[line_length] = '\0';
editor_modal_command_config(editor, line);
}
if ( errno != 0 )
error(0, errno, "getline: %s", path);
fclose(fp);
}
void editor_load_config(struct editor* editor)
{
editor_load_config_path(editor, "/etc/editor");
const char* home = getenv("HOME");
if ( !home )
return;
char* path;
if ( asprintf(&path, "%s/.editor", home) < 0 )
{
error(0, errno, "malloc");
return;
}
editor_load_config_path(editor, path);
free(path);
}
void editor_reset_contents(struct editor* editor)
{
for ( size_t i = 0; i < editor->lines_used; i++ )
@ -242,6 +285,8 @@ int main(int argc, char* argv[])
struct editor editor;
initialize_editor(&editor);
editor_load_config(&editor);
if ( 2 <= argc && !editor_load_file(&editor, argv[1]) )
error(1, errno, "`%s'", argv[1]);

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014, 2015.
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014, 2015, 2016.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
@ -282,6 +282,21 @@ void editor_modal_command(struct editor* editor, const char* cmd)
editor->modal_error = true;
}
void editor_modal_command_config(struct editor* editor, const char* cmd)
{
while ( *cmd && isspace((unsigned char) *cmd) )
cmd++;
if ( is_modal_command(cmd, "margin", &cmd) )
editor_modal_margin(editor, cmd);
else if ( is_modal_command(cmd, "tabsize", &cmd) )
editor_modal_tabsize(editor, cmd);
else if ( is_modal_command(cmd, "language", &cmd) )
editor_modal_language(editor, cmd);
else if ( is_modal_command(cmd, "line-numbering", &cmd) )
editor_modal_line_numbering(editor, cmd);
}
void editor_modal_character(struct editor* editor, wchar_t c)
{
if ( editor->control )

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014.
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014, 2016.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
@ -47,6 +47,7 @@ void editor_modal_line_numbering(struct editor* editor, const char* truth);
bool is_modal_command(const char* cmd, const char* candidate, const char** rest);
void editor_modal_command(struct editor* editor, const char* cmd);
void editor_modal_command_config(struct editor* editor, const char* cmd);
void editor_modal_character(struct editor* editor, wchar_t c);
#endif