sandy

text editor
git clone git://git.suckless.org/sandy
Log | Files | Refs | README | LICENSE

commit a413df0cf1ebcc7e9da18094e3dc721ccdc2d0d4
parent 85f1d00d6f2aa21aaf05d1218b2038bcdee44fac
Author: Rafael Garcia <rafael.garcia.gallego@gmail.com>
Date:   Tue,  5 Apr 2011 12:53:43 +0200

Simpler config.h delegates the check for X11 to sh
Diffstat:
TODO | 1+
config.def.h | 118++++++++++++++++++++++++++-----------------------------------------------------
2 files changed, 39 insertions(+), 80 deletions(-)

diff --git a/TODO b/TODO @@ -12,6 +12,7 @@ In no particular order, at sandy.c: - Improve syntax highlight, multiline? - Groups Undos - Repeat operation (or last pipe at least!) +- Clarify on undo/redo/repeat At config.def.h: - Bindings! diff --git a/config.def.h b/config.def.h @@ -7,50 +7,32 @@ static int tabstop = 8; /* Not const, as it may be changed via param static const char tabstr[3] = { (char)0xC2, (char)0xBB, 0x00 }; /* Double right arrow */ static const char spcstr[3] = { (char)0xC2, (char)0xB7, 0x00 }; /* Middle dot */ -/* Custom function declaration */ -static bool t_x(void); - /* Paths */ //static const char systempath[] = "/etc/sandy"; //static const char userpath[] = ".sandy"; /* Relative to $HOME */ -/* Args to f_spawn, X version */ -#define FIND_X { .v = (const char *[]){ "/bin/sh", "-c", \ - "arg=\"`echo \\\"${SANDY_FIND}\\\" | dmenu -p Find:`\" &&" \ - "echo find \"$arg\" > ${SANDY_FIFO}", NULL } } -#define FINDBW_X { .v = (const char *[]){ "/bin/sh", "-c", \ - "arg=\"`echo \\\"${SANDY_FIND}\\\" | dmenu -p 'Find (back):'`\" &&" \ - "echo findbw \"$arg\" > ${SANDY_FIFO}", NULL } } -#define PIPE_X { .v = (const char *[]){ "/bin/sh", "-c", \ - "arg=\"`echo \\\"${SANDY_PIPE}\\\" | dmenu -p Pipe:`\" " \ - "&&" "echo pipe \"$arg\" > ${SANDY_FIFO}" , NULL } } -#define SAVEAS_X { .v = (const char *[]){ "/bin/sh", "-c", \ - "arg=\"`echo \\\"${SANDY_FILE}\\\" | dmenu -p 'Save as:'`\" &&" \ - "echo save \"$arg\" > ${SANDY_FIFO}", NULL } } -#define LINE_X { .v = (const char *[]){ "/bin/sh", "-c", \ - "arg=\"`echo \\\"${SANDY_LINE}\\\" | dmenu -p 'Line:'`\" &&" \ - "echo line \"$arg\" > ${SANDY_FIFO}", NULL } } -#define SYNTAX_X { .v = (const char *[]){ "/bin/sh", "-c", \ - "arg=\"`echo \\\"${SANDY_SYNTAX}\\\" | dmenu -p 'Syntax:'`\" &&" \ - "echo syntax \"$arg\" > ${SANDY_FIFO}", NULL } } - -/* Args to f_spawn, non-X version */ -#define FIND_NOX { .v = (const char *[]){ "/bin/sh", "-c", \ - "echo -n '\033[H\033[K\033[7m'; read -p 'Find: ' arg && echo find $arg > ${SANDY_FIFO}", NULL } } -#define FINDBW_NOX { .v = (const char *[]){ "/bin/sh", "-c", \ - "echo -n '\033[H\033[K\033[7m'; read -p 'Find (back): ' arg && echo findbw $arg > ${SANDY_FIFO}", NULL } } -#define PIPE_NOX { .v = (const char *[]){ "/bin/sh", "-c", \ - "echo -n '\033[H\033[K\033[7m'; read -p 'Pipe: ' arg && echo pipe $arg > ${SANDY_FIFO}", NULL } } -#define SAVEAS_NOX { .v = (const char *[]){ "/bin/sh", "-c", \ - "echo -n '\033[H\033[K\033[7m'; read -p 'Save as: ' arg && echo save $arg > ${SANDY_FIFO}", NULL } } -#define LINE_NOX { .v = (const char *[]){ "/bin/sh", "-c", \ - "echo -n '\033[H\033[K\033[7m'; read -p 'Line: ' arg && echo line $arg > ${SANDY_FIFO}", NULL } } -#define SYNTAX_NOX { .v = (const char *[]){ "/bin/sh", "-c", \ - "echo -n '\033[H\033[K\033[7m'; read -p 'Syntax: ' arg && echo syntax $arg > ${SANDY_FIFO}", NULL } } +/* Args to f_spawn */ +#define PROMPT(prompt, default, cmd) { .v = (const char *[]){ "/bin/sh", "-c", \ + "if [ $DISPLAY ]; then arg=\"`echo \\\"" default "\\\" | dmenu -p " prompt "`\";" \ + "else echo -n '\033[H\033[K\033[7m'; read -p '" prompt " ' arg; fi &&" \ + "echo " cmd " \"$arg\" > ${SANDY_FIFO}", NULL } } + +#define FIND PROMPT("Find:", "${SANDY_FIND}", "find") +#define FINDBW PROMPT("Find (back):", "${SANDY_FIND}", "findbw") +#define PIPE PROMPT("Pipe:", "${SANDY_PIPE}", "pipe") +#define SAVEAS PROMPT("Save as:", "${SANDY_FILE}", "save") +#define LINE PROMPT("Line:", "${SANDY_LINE}", "line") +#define SYNTAX PROMPT("Syntax:", "${SANDY_SYNTAX}", "syntax") + +/* Args to f_pipe / f_pipero */ +#define TOCLIP { .v = "if [ $DISPLAY ] ; then xsel -ib; else cat > $HOME/.sandy.clipboard ; fi" } +#define FROMCLIP { .v = "if [ $DISPLAY ] ; then xsel -ob; else cat $HOME/.sandy.clipboard ; fi" } +#define TOSEL { .v = "if [ $DISPLAY ] ; then xsel -i; else cat > $HOME/.sandy.selection ; fi" } +#define FROMSEL { .v = "if [ $DISPLAY ] ; then xsel -o; else cat $HOME/.sandy.selection ; fi" } /* Hooks are launched from the main code */ -#define HOOK_SAVE_NO_FILE if(t_x()) f_spawn(&(const Arg)SAVEAS_X); else f_spawn(&(const Arg)SAVEAS_NOX) -#define HOOK_SELECT_MOUSE if(t_x()) f_pipero(&(const Arg){ .v = ("xsel -i")}) +#define HOOK_SAVE_NO_FILE f_spawn (&(const Arg)SAVEAS) +#define HOOK_SELECT_MOUSE f_pipero(&(const Arg)TOSEL) #undef HOOK_PRE_DELETE /* This affects every delete */ #undef HOOK_SELECT_ALL /* Do not bother */ @@ -66,12 +48,9 @@ static const Key curskeys[] = { /* Don't use CONTROL or META here */ { {KEY_BACKSPACE}, { t_rw, 0, 0, 0 }, f_delete, { .m = m_prevchar } }, { {KEY_DC}, { t_sel, t_rw, 0, 0 }, f_delete, { .m = m_tosel } }, { {KEY_DC}, { t_rw, 0, 0, 0 }, f_delete, { .m = m_nextchar } }, -{ {KEY_IC}, { t_sel, t_x, 0, 0 }, f_pipero, { .v = "xsel -ib" } }, -{ {KEY_IC}, { t_sel, 0, 0, 0 }, f_pipero, { .v = "cat > $HOME/.sandy.clipboard" } }, -{ {KEY_SDC}, { t_sel, t_rw, t_x, 0 }, f_pipe, { .v = "xsel -ib" } }, -{ {KEY_SDC}, { t_sel, t_rw, 0, 0 }, f_pipe, { .v = "cat > $HOME/.sandy.clipboard" } }, -{ {KEY_SIC}, { t_rw, t_x, 0, 0 }, f_pipe, { .v = "xsel -ob" } }, -{ {KEY_SIC}, { t_rw, 0, 0, 0 }, f_pipe, { .v = "cat $HOME/.sandy.clipboard" } }, +{ {KEY_IC}, { t_sel, 0, 0, 0 }, f_pipero, TOCLIP }, +{ {KEY_SDC}, { t_sel, t_rw, 0, 0 }, f_pipe, TOCLIP }, +{ {KEY_SIC}, { t_rw, 0, 0, 0 }, f_pipe, FROMCLIP }, { {KEY_HOME}, { 0, 0, 0, 0 }, f_move, { .m = m_bol } }, { {KEY_END}, { 0, 0, 0, 0 }, f_move, { .m = m_eol } }, { {KEY_SHOME}, { 0, 0, 0, 0 }, f_move, { .m = m_bof } }, @@ -109,14 +88,11 @@ static const Key stdkeys[] = { { META('.'), { 0, 0, 0, 0 }, f_move, { .m = m_nextscr } }, { META('<'), { 0, 0, 0, 0 }, f_move, { .m = m_bof } }, { META('>'), { 0, 0, 0, 0 }, f_move, { .m = m_eof } }, -{ META('g'), { t_x, 0, 0, 0 }, f_spawn, LINE_X }, -{ META('g'), { 0, 0, 0, 0 }, f_spawn, LINE_NOX }, +{ META('g'), { 0, 0, 0, 0 }, f_spawn, LINE }, /* Finding and selecting */ -{ CONTROL('S'), { t_x, 0, 0, 0 }, f_spawn, FIND_X }, -{ CONTROL('S'), { 0, 0, 0, 0 }, f_spawn, FIND_NOX }, -{ CONTROL('R'), { t_x, 0, 0, 0 }, f_spawn, FINDBW_X }, -{ CONTROL('R'), { 0, 0, 0, 0 }, f_spawn, FINDBW_NOX }, +{ CONTROL('S'), { 0, 0, 0, 0 }, f_spawn, FIND }, +{ CONTROL('R'), { 0, 0, 0, 0 }, f_spawn, FINDBW }, { META('n'), { 0, 0, 0, 0 }, f_findfw, { 0 } }, { META('p'), { 0, 0, 0, 0 }, f_findbw, { 0 } }, { CONTROL('X'), { 0, 0, 0, 0 }, f_extsel, { .i = ExtDefault } }, @@ -128,34 +104,27 @@ static const Key stdkeys[] = { { META('r'), { t_sel, 0, 0, 0 }, f_pipero, { .v = "(sed 's/$/\\n/;2q' | (read arg && echo findbw \"$arg\" > ${SANDY_FIFO}))" } }, /* Text deletion */ -{ CONTROL('D'), { t_sel, t_rw, t_x, 0 }, f_pipe, { .v = "xsel -ib" } }, -{ CONTROL('D'), { t_sel, t_rw, 0, 0 }, f_pipe, { .v = "cat > $HOME/.sandy.clipboard" } }, +{ CONTROL('D'), { t_sel, t_rw, 0, 0 }, f_pipe, TOCLIP }, { CONTROL('D'), { t_rw, 0, 0, 0 }, f_delete, { .m = m_nextchar } }, { CONTROL('D'), { 0, 0, 0, 0 }, f_select, { .m = m_nextchar } }, -{ CONTROL('?'), { t_sel, t_rw, t_x, 0 }, f_pipe, { .v = "xsel -ib" } }, -{ CONTROL('?'), { t_sel, t_rw, 0, 0 }, f_pipe, { .v = "cat > $HOME/.sandy.clipboard" } }, +{ CONTROL('?'), { t_sel, t_rw, 0, 0 }, f_pipe, TOCLIP }, { CONTROL('?'), { t_rw, 0, 0, 0 }, f_delete, { .m = m_prevchar } }, { CONTROL('?'), { 0, 0, 0, 0 }, f_select, { .m = m_prevchar } }, -{ CONTROL('H'), { t_sel, t_rw, t_x, 0 }, f_pipe, { .v = "xsel -ib" } }, -{ CONTROL('H'), { t_sel, t_rw, 0, 0 }, f_pipe, { .v = "cat > $HOME/.sandy.clipboard" } }, +{ CONTROL('H'), { t_sel, t_rw, 0, 0 }, f_pipe, TOCLIP }, { CONTROL('H'), { t_rw, 0, 0, 0 }, f_delete, { .m = m_prevchar } }, { CONTROL('H'), { 0, 0, 0, 0 }, f_select, { .m = m_prevchar } }, -{ CONTROL('U'), { t_sel, t_rw, t_x, 0 }, f_pipe, { .v = "xsel -ib" } }, -{ CONTROL('U'), { t_sel, t_rw, 0, 0 }, f_pipe, { .v = "cat > $HOME/.sandy.clipboard" } }, +{ CONTROL('U'), { t_sel, t_rw, 0, 0 }, f_pipe, TOCLIP }, { CONTROL('U'), { t_bol, t_rw, 0, 0 }, f_delete, { .m = m_prevchar } }, { CONTROL('U'), { t_rw, 0, 0, 0 }, f_delete, { .m = m_bol } }, { CONTROL('U'), { 0, 0, 0, 0 }, f_select, { .m = m_bol } }, -{ CONTROL('K'), { t_sel, t_rw, t_x, 0 }, f_pipe, { .v = "xsel -ib" } }, -{ CONTROL('K'), { t_sel, t_rw, 0, 0 }, f_pipe, { .v = "cat > $HOME/.sandy.clipboard" } }, +{ CONTROL('K'), { t_sel, t_rw, 0, 0 }, f_pipe, TOCLIP }, { CONTROL('K'), { t_eol, t_rw, 0, 0 }, f_delete, { .m = m_nextchar } }, { CONTROL('K'), { t_rw, 0, 0, 0 }, f_delete, { .m = m_eol } }, { CONTROL('K'), { 0, 0, 0, 0 }, f_select, { .m = m_eol } }, -{ CONTROL('W'), { t_sel, t_rw, t_x, 0 }, f_pipe, { .v = "xsel -ib" } }, -{ CONTROL('W'), { t_sel, t_rw, 0, 0 }, f_pipe, { .v = "cat > $HOME/.sandy.clipboard" } }, +{ CONTROL('W'), { t_sel, t_rw, 0, 0 }, f_pipe, TOCLIP }, { CONTROL('W'), { t_rw, 0, 0, 0 }, f_delete, { .m = m_prevword } }, { CONTROL('W'), { 0, 0, 0, 0 }, f_select, { .m = m_prevword } }, -{ META('d'), { t_sel, t_rw, t_x, 0 }, f_pipe, { .v = "xsel -ib" } }, -{ META('d'), { t_sel, t_rw, 0, 0 }, f_pipe, { .v = "cat > $HOME/.sandy.clipboard" } }, +{ META('d'), { t_sel, t_rw, 0, 0 }, f_pipe, TOCLIP }, { META('d'), { t_rw, 0, 0, 0 }, f_delete, { .m = m_nextword } }, { META('d'), { 0, 0, 0, 0 }, f_select, { .m = m_nextword } }, @@ -168,16 +137,12 @@ static const Key stdkeys[] = { { META('q'), { 0, 0, 0, 0 }, f_toggle, { .i = S_Running } }, { META('Q'), { 0, 0, 0, 0 }, f_toggle, { .i = S_Running } }, { META('w'), { 0, 0, 0, 0 }, f_save, { 0 } }, -{ META('W'), { t_x, 0, 0, 0 }, f_spawn, SAVEAS_X }, -{ META('W'), { 0, 0, 0, 0 }, f_spawn, SAVEAS_NOX }, +{ META('W'), { 0, 0, 0, 0 }, f_spawn, SAVEAS }, /* Text piping and modification */ -{ CONTROL('\\'),{ t_x, 0, 0, 0 }, f_spawn, PIPE_X }, -{ CONTROL('\\'),{ 0, 0, 0, 0 }, f_spawn, PIPE_NOX }, -{ CONTROL('Y'), { t_rw, t_x, 0, 0 }, f_pipe, { .v = "xsel -ob" } }, -{ CONTROL('Y'), { t_rw, 0, 0, 0 }, f_pipe, { .v = "cat $HOME/.sandy.clipboard" } }, -{ CONTROL('C'), { t_sel, t_x, 0, 0 }, f_pipero, { .v = "xsel -ib" } }, -{ CONTROL('C'), { t_sel, 0, 0, 0 }, f_pipero, { .v = "cat > $HOME/.sandy.clipboard" } }, +{ CONTROL('\\'),{ 0, 0, 0, 0 }, f_spawn, PIPE }, +{ CONTROL('Y'), { t_rw, 0, 0, 0 }, f_pipe, FROMCLIP }, +{ CONTROL('C'), { t_sel, 0, 0, 0 }, f_pipero, TOCLIP }, /* Windows-like crap TO REMOVE */ { CONTROL('Z'), { t_undo,0, 0, 0 }, f_undo, { .i = 1 } }, @@ -186,8 +151,7 @@ static const Key stdkeys[] = { { CONTROL('L'), { 0, 0, 0, 0 }, f_center, { 0 } }, { CONTROL('V'), { 0, 0, 0, 0 }, f_toggle, { .i = S_InsEsc } }, { META('R'), { 0, 0, 0, 0 }, f_toggle, { .i = S_Readonly } }, -{ META('S'), { t_x, 0, 0, 0 }, f_spawn, SYNTAX_X }, -{ META('S'), { 0, 0, 0, 0 }, f_spawn, SYNTAX_NOX }, +{ META('S'), { 0, 0, 0, 0 }, f_spawn, SYNTAX }, }; /* Commands read at the fifo */ @@ -272,9 +236,3 @@ static const short bgcolors[LastBG] = { [SelBG] = COLOR_YELLOW, }; -/* Custom function implementation */ -bool -t_x(void) { - return (getenv("DISPLAY") != NULL); -} -