commit 6dd160ea93711cac5deb9ac18ce5e26f455c8e22
parent 4fbd06e22f1e445b3b513d32a62ae18ff1de3c19
Author: Rafael Garcia <rafael.garcia.gallego@gmail.com>
Date: Sat, 11 Jun 2011 17:29:30 +0200
Write interface to stderr, simplify warning.
Diffstat:
3 files changed, 16 insertions(+), 20 deletions(-)
diff --git a/config.def.h b/config.def.h
@@ -24,7 +24,7 @@ static const char nlstr[1] = { 0 };
#define PROMPT(prompt, default, cmd) { .v = (const char *[]){ "/bin/sh", "-c", \
"dmenu -v >/dev/null 2>&1 || DISPLAY=\"\";"\
"if [ -n \"$DISPLAY\" ]; then arg=\"`echo \\\"" default "\\\" | dmenu -p '" prompt "'`\";" \
- "else printf \"\033[0;0H\033[7m"prompt"\033[K\033[0m \"; read arg; fi &&" \
+ "else printf \"\033[0;0H\033[7m"prompt"\033[K\033[0m \" >&2; read arg; fi &&" \
"echo " cmd "\"$arg\" > ${SANDY_FIFO}", NULL } }
#define FIND PROMPT("Find:", "${SANDY_FIND}", "/")
@@ -111,7 +111,7 @@ static const Key stdkeys[] = {
{ CONTROL('O'), { 0, 0, 0, 0 }, f_move, { .m = m_tomark } },
{ CONTROL('P'), { 0, 0, 0, 0 }, f_move, { .m = m_prevline } },
{ CONTROL('Q'), { t_warn,t_mod,0, 0 }, f_toggle, { .i = S_Running } },
-{ CONTROL('Q'), { t_mod, 0, 0, 0 }, f_warn, { .v = "WARNING! File modified!!!" } },
+{ CONTROL('Q'), { t_mod, 0, 0, 0 }, f_toggle, { .i = S_Warned } },
{ CONTROL('Q'), { 0, 0, 0, 0 }, f_toggle, { .i = S_Running } },
{ CONTROL('R'), { t_sel, 0, 0, 0 }, f_findbw, { 0 } },
{ CONTROL('R'), { 0, 0, 0, 0 }, f_spawn, FINDBW },
diff --git a/sandy.1 b/sandy.1
@@ -16,7 +16,7 @@ A small degree of external control can be achieved by writing to a named pipe.
.SH OPTIONS
.TP
.B \-h
-Prints usage information to stdout, then exits.
+Prints usage information to stderr, then exits.
.TP
.B \-r
Opens the selected file read\-only.
@@ -31,7 +31,7 @@ Use no syntax colors at all.
Use selected tabstop in characters.
.TP
.B \-v
-Prints version information to stdout, then exits.
+Prints version information to stderr, then exits.
.SH USAGE
The default key\-bindings try to stick to the "emacs\-mode" present in modern
shells (specifically
diff --git a/sandy.c b/sandy.c
@@ -64,7 +64,7 @@ typedef struct { /** A position in the file */
} Filepos;
typedef union { /** An argument to a f_* function, generic */
- int i;
+ long i;
const void *v;
Filepos (*m)(Filepos);
} Arg;
@@ -197,7 +197,6 @@ static void f_suspend(const Arg*);
static void f_syntax(const Arg *arg);
static void f_toggle(const Arg *arg);
static void f_undo(const Arg*);
-static void f_warn(const Arg *arg);
/* i_* funcions are called from inside the main code only */
static Filepos i_addtext(char*, Filepos);
@@ -229,7 +228,7 @@ static void i_sortpos(Filepos*, Filepos*);
static void i_termwininit(void);
static void i_update(void);
static void i_usage(void);
-static bool i_writefile(void);
+static bool i_writefile(char*);
/* t_* functions to know whether to process an action or keybinding */
static bool t_bol(void);
@@ -444,7 +443,7 @@ f_save(const Arg *arg) {
return;
}
- if(i_writefile()) {
+ if(i_writefile(filename)) {
statusflags&=~S_Modified;
for(savestep=0,u=undos; u; u=u->prev, savestep++);
}
@@ -510,13 +509,16 @@ f_syntax(const Arg *arg) {
void /* Toggle the arg->i statusflag. Careful with this one! */
f_toggle(const Arg *arg) {
- statusflags^=(char)arg->i;
+ statusflags^=(long)arg->i;
/* Specific operations for some toggles */
switch(arg->i) {
case S_CaseIns: /* Re-compile regex with/without REG_ICASE */
i_setfindterm(getenv(envs[EnvFind]));
break;
+ case S_Warned: /* Set warning title */
+ tmptitle="Warning! File Modified!!!";
+ break;
}
}
@@ -555,12 +557,6 @@ f_undo(const Arg *arg) {
statusflags|=S_Modified;
}
-void /* Set screen title to arg->v, set warning bit */
-f_warn(const Arg *arg) {
- tmptitle=(char*)arg->v;
- statusflags|=S_Warned;
-}
-
/* I_* FUNCTIONS
Called internally from the program code */
@@ -1061,7 +1057,7 @@ i_readfile(char *fname) {
ssize_t n;
char *buf = NULL;
- if(!strcmp(fname, "-")) {
+ if(fname == NULL || !strcmp(fname, "-")) {
fd=0;
reset_shell_mode();
} else {
@@ -1185,7 +1181,7 @@ i_setup(void){
regcomp(find_res[0], "\0\0", 0); /* This should not match anything */
regcomp(find_res[1], "\0\0", 0);
- initscr();
+ newterm(NULL, stderr, stdin);
if(has_colors()) {
start_color();
use_default_colors();
@@ -1452,12 +1448,12 @@ i_usage(void) {
}
bool /* Write buffer to disk */
-i_writefile(void) {
- int fd;
+i_writefile(char *fname) {
+ int fd=1; /* default: write to stdout */
bool wok=TRUE;
Line *l;
- if (filename == NULL || (fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO)) == -1) {
+ if (fname != NULL && (fd = open(fname, O_WRONLY|O_TRUNC|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO)) == -1) {
/* error */
tmptitle="WARNING! Can't save file!!!";
return FALSE;