From 01ee906d2546c6af379e7dd421d1078a25882204 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Fri, 1 Jan 2016 15:01:39 +0000 Subject: [PATCH] Add editor(5). --- editor/Makefile | 2 ++ editor/editor.1 | 16 +++++++++++++++- editor/editor.5 | 26 ++++++++++++++++++++++++++ editor/editor.c++ | 47 ++++++++++++++++++++++++++++++++++++++++++++++- editor/modal.c++ | 17 ++++++++++++++++- editor/modal.h++ | 3 ++- 6 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 editor/editor.5 diff --git a/editor/Makefile b/editor/Makefile index f6df645d..a5828a14 100644 --- a/editor/Makefile +++ b/editor/Makefile @@ -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 diff --git a/editor/editor.1 b/editor/editor.1 index 4a697a52..0b9d7416 100644 --- a/editor/editor.1 +++ b/editor/editor.1 @@ -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 diff --git a/editor/editor.5 b/editor/editor.5 new file mode 100644 index 00000000..025ecfed --- /dev/null +++ b/editor/editor.5 @@ -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 diff --git a/editor/editor.c++ b/editor/editor.c++ index c85f9ab2..fd448255 100644 --- a/editor/editor.c++ +++ b/editor/editor.c++ @@ -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 #include #include +#include #include #include #include @@ -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]); diff --git a/editor/modal.c++ b/editor/modal.c++ index 6cea80cb..e22d5140 100644 --- a/editor/modal.c++ +++ b/editor/modal.c++ @@ -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 ) diff --git a/editor/modal.h++ b/editor/modal.h++ index 17214a75..e2c5ab87 100644 --- a/editor/modal.h++ +++ b/editor/modal.h++ @@ -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