diff -urpN cvs/gretl/gretl/cli/gretlcli.c gretl-cvs12/cli/gretlcli.c
--- cvs/gretl/gretl/cli/gretlcli.c	2009-06-20 21:45:00.000000000 +0100
+++ gretl-cvs12/cli/gretlcli.c	2009-07-06 10:18:36.000000000 +0100
@@ -212,27 +212,20 @@ static void get_a_filename (char *fname)
     fgets(fname, MAXLEN - 1, stdin);
 }
 
-static int 
-cli_get_input_line (ExecState *s, double ***pZ, DATAINFO *pdinfo)
-{
+int cli_get_interactive_line(ExecState *s, double ***pZ, DATAINFO *pdinfo, char *line) {
     int coding = gretl_compiling_function() || 
 	gretl_compiling_loop();
-    int err = 0;
-
-    if (runit || batch) {
-	/* reading from script file */
-	err = file_get_line(s->line, s->cmd);
-    } else {
-	/* interactive use */
+	int err=0;
 #ifdef HAVE_READLINE
-	rl_gets(&line_read, (coding)? "> " : "? ");
-	if (line_read == NULL) {
+
+	rl_gets(&line, (coding)? "> " : "? ");
+	if (line == NULL) {
 	    strcpy(s->line, "quit");
-	} else if (strlen(line_read) > MAXLINE - 2) {
+	} else if (strlen(line) > MAXLINE - 2) {
 	    err = E_TOOLONG;
 	} else {
 	    *s->line = '\0';
-	    strncat(s->line, line_read, MAXLINE - 2);
+	    strncat(s->line, line, MAXLINE - 2);
 	    strcat(s->line, "\n");
 	}
 #else
@@ -240,6 +233,20 @@ cli_get_input_line (ExecState *s, double
 	fflush(stdout);
 	file_get_line(s->line, s->cmd); /* note: "file" = stdin here */
 #endif
+	return err;
+}
+
+int 
+cli_get_input_line (ExecState *s, double ***pZ, DATAINFO *pdinfo)
+{
+    int err = 0;
+
+    if (runit || batch) {
+	/* reading from script file */
+	err = file_get_line(s->line, s->cmd);
+    } else {
+	/* interactive use */
+		err=cli_get_interactive_line(s, pZ, pdinfo,line_read);
     }
 
     return err;
@@ -315,6 +322,7 @@ int main (int argc, char *argv[])
     CMD cmd;
     PRN *prn;
     int err = 0;
+	DEBUG_READLINE *pFunction=&cli_get_interactive_line;
 
 #ifdef ENABLE_NLS
     nls_init();
@@ -493,6 +501,7 @@ int main (int argc, char *argv[])
 
     gretl_cmd_init(&cmd);
     gretl_exec_state_init(&state, 0, line, &cmd, models, prn);
+	libset_set_dbg_read(pFunction);
 
     /* print list of variables */
     if (data_status) {
@@ -523,7 +532,7 @@ int main (int argc, char *argv[])
     }
 
 #ifdef HAVE_READLINE
-    if (!batch) initialize_readline();
+    initialize_readline();
 #endif
 
     if (batch || runit) {
diff -urpN cvs/gretl/gretl/lib/Makefile.in gretl-cvs12/lib/Makefile.in
--- cvs/gretl/gretl/lib/Makefile.in	2009-07-06 09:59:56.000000000 +0100
+++ gretl-cvs12/lib/Makefile.in	2009-07-06 10:18:36.000000000 +0100
@@ -76,6 +76,7 @@ PUBLIC_HDRS = \
 	dataio.h \
 	dataset.h \
 	dbread.h \
+	debugger.h \
 	describe.h \
 	discrete.h \
 	estimate.h \
@@ -149,6 +150,7 @@ SRCS = adf_kpss.c \
 	dataset.c \
 	dbread.c \
 	dbwrite.c \
+	debugger.c \
 	describe.c \
 	discrete.c \
 	estimate.c \
diff -urpN cvs/gretl/gretl/lib/src/debugger.c gretl-cvs12/lib/src/debugger.c
--- cvs/gretl/gretl/lib/src/debugger.c	1970-01-01 01:00:00.000000000 +0100
+++ gretl-cvs12/lib/src/debugger.c	2009-07-06 10:18:59.000000000 +0100
@@ -0,0 +1,47 @@
+#include "libgretl.h"
+#include "cmd_private.h"
+#include "libset.h"
+
+int debugCommandLoop(ExecState *state, double **Z,
+  DATAINFO *datainfo)
+{
+	int err=0;
+	int breakLoop=0;
+	char *line=NULL;
+
+
+	DEBUG_READLINE* get_line= libset_get_dbg_read();
+	if (get_line==NULL) {
+		return 0;
+	} 
+    /* main command loop */
+    while (!breakLoop) {
+
+	err = (*get_line)(state, &Z, datainfo, line);
+	    
+	if (err) {
+		break;
+	}
+
+	err = parse_command_line(state->line, state->cmd, &Z, datainfo);
+	switch (state->cmd->ci) {
+		case DBG_CONT:
+		case DBG_NEXT: {
+			breakLoop=1;
+			break;
+		}
+		default: {
+			err = gretl_cmd_exec(state, &Z, datainfo);
+			break;
+		}	
+	}
+	
+	
+
+    }
+	if (state->cmd->ci == DBG_NEXT) {
+		return 1;
+	} else
+    	return 0;
+}
+
diff -urpN cvs/gretl/gretl/lib/src/debugger.h gretl-cvs12/lib/src/debugger.h
--- cvs/gretl/gretl/lib/src/debugger.h	1970-01-01 01:00:00.000000000 +0100
+++ gretl-cvs12/lib/src/debugger.h	2009-07-06 10:18:59.000000000 +0100
@@ -0,0 +1,5 @@
+int debugCommandLoop(ExecState *state, double **Z,
+  DATAINFO *datainfo);
+
+typedef int (DEBUG_READLINE) (ExecState *, double ***, DATAINFO *, char *);
+
diff -urpN cvs/gretl/gretl/lib/src/gretl_commands.c gretl-cvs12/lib/src/gretl_commands.c
--- cvs/gretl/gretl/lib/src/gretl_commands.c	2009-06-16 18:56:40.000000000 +0100
+++ gretl-cvs12/lib/src/gretl_commands.c	2009-07-06 10:18:36.000000000 +0100
@@ -47,12 +47,14 @@ static struct gretl_cmd gretl_cmds[] = {
     { COEFFSUM, "coeffsum" },
     { COINT,    "coint" },
     { COINT2,   "coint2" },
+	{ DBG_CONT,	"continue" },
     { CORR,     "corr" },     
     { CORRGM,   "corrgm" },   
     { CRITERIA, "criteria" },
     { CUSUM,    "cusum" },
     { DATA,     "data" },
     { DATAMOD,  "dataset" },
+    { DBG_FUNC,  "debug" },
     { DELEET,   "delete" },
     { DIFF,     "diff" },
     { DIFFTEST, "difftest" },
@@ -103,6 +105,7 @@ static struct gretl_cmd gretl_cmds[] = {
     { MODPRINT, "modprint" },
     { MODTEST,  "modtest" },
     { MPOLS,    "mpols" },
+	{ DBG_NEXT,	"next" },
     { NLS,      "nls" },
     { NORMTEST, "normtest" },
     { NULLDATA, "nulldata" },
@@ -147,6 +150,7 @@ static struct gretl_cmd gretl_cmds[] = {
     { TABPRINT, "tabprint" },
     { TOBIT,    "tobit" },
     { IVREG,    "tsls", },
+    { UNDEBUG,  "undebug", },
     { VAR,      "var" },
     { VARLIST,  "varlist" },
     { VARTEST,  "vartest" },
diff -urpN cvs/gretl/gretl/lib/src/gretl_commands.h gretl-cvs12/lib/src/gretl_commands.h
--- cvs/gretl/gretl/lib/src/gretl_commands.h	2009-06-16 18:56:40.000000000 +0100
+++ gretl-cvs12/lib/src/gretl_commands.h	2009-07-06 10:18:36.000000000 +0100
@@ -43,6 +43,9 @@ typedef enum {
     CUSUM,
     DATA,
     DATAMOD,
+	DBG_CONT,
+	DBG_NEXT,
+	DBG_FUNC,
     DELEET,
     DIFF,
     DIFFTEST,
@@ -137,6 +140,7 @@ typedef enum {
     TABPRINT,
     TOBIT,
     IVREG,
+	UNDEBUG,
     VAR,
     VARLIST,
     VARTEST,
diff -urpN cvs/gretl/gretl/lib/src/gretl_func.c gretl-cvs12/lib/src/gretl_func.c
--- cvs/gretl/gretl/lib/src/gretl_func.c	2009-06-20 16:59:44.000000000 +0100
+++ gretl-cvs12/lib/src/gretl_func.c	2009-07-06 11:14:11.000000000 +0100
@@ -80,6 +80,7 @@ struct ufunc_ {
     fn_param *params;
     int rettype;
     char *retname;
+	int debug;
 };
 
 struct fnpkg_ {
@@ -621,6 +622,8 @@ static ufunc *ufunc_new (void)
     fun->rettype = GRETL_TYPE_NONE;
     fun->retname = NULL;
 
+	fun->debug=0;
+
     return fun;
 }
 
@@ -4071,6 +4074,31 @@ static int check_function_args (ufunc *u
     return err;
 }
 
+/**
+ * debug_function:
+ * @function: the function that should be debugged.
+ * @undebug: boolean, if 1 then stop debugging function.
+ * @prn: pointer to gretl printing struct.
+ *
+ * enables or disables debugging for a funcion
+ *
+ * Returns: 0 on success, 1 if the function is non-existant
+ * and 2 if the function is already debugged
+ */
+
+int debug_function (const char *function, int undebug, PRN *prn)
+{
+	ufunc *fun = get_user_function_by_name(function);
+	if (fun==NULL) {
+		return 1;
+	}
+	if (!(undebug && (fun->debug)) || (!undebug && (fun->debug))) {
+		return 2;
+	}
+	fun->debug=(undebug) ? 0 : 1;
+	return 0;
+}
+
 static void fn_state_init (CMD *cmd, ExecState *state, int *indent0)
 {
     cmd->list = NULL;
@@ -4101,6 +4129,8 @@ int gretl_function_exec (ufunc *u, fnarg
     int indent0, started = 0;
     int i, err = 0;
 
+    int dbg_status=u->debug;
+
     *funcerr_msg = '\0';
 
 #if EXEC_DEBUG
@@ -4177,6 +4207,10 @@ int gretl_function_exec (ufunc *u, fnarg
 
     /* get function lines in sequence and check, parse, execute */
 
+	if (dbg_status) {
+		set_gretl_echo(1);
+		set_gretl_messages(1);
+	}
     for (i=0; i<u->n_lines && !err; i++) {
 	if (*u->lines[i] == '\0') {
 	    continue;
@@ -4188,7 +4222,10 @@ int gretl_function_exec (ufunc *u, fnarg
 	}
 
 	err = maybe_exec_line(&state, pZ, pdinfo);
-
+	if (dbg_status && (state.cmd->ci > 0) && (!gretl_compiling_loop()) && (!state.cmd->context)) {
+		pprintf(prn, "Debugging %s, line %d\n", u->name, i);
+		dbg_status=debugCommandLoop(&state, *pZ,pdinfo);
+	}
 	if (err) {
 	    if (*gretl_errmsg == '\0') {
 		gretl_errmsg_sprintf("error in function %s\n"
diff -urpN cvs/gretl/gretl/lib/src/interact.c gretl-cvs12/lib/src/interact.c
--- cvs/gretl/gretl/lib/src/interact.c	2009-06-20 21:45:01.000000000 +0100
+++ gretl-cvs12/lib/src/interact.c	2009-07-06 10:18:36.000000000 +0100
@@ -306,8 +306,13 @@ static int catch_command_alias (char *li
 	strcpy(s, "ols");
 	cmd->ci = OLS;
 	cmd->opt |= OPT_J;
-    }	
-
+    } else if (!strcmp(s, "n")) {
+	strcpy(s, "next");
+	cmd->ci = DBG_NEXT;
+    } else if (!strcmp(s, "c")) {
+	strcpy(s, "continue");
+	cmd->ci = DBG_CONT;	
+	}
     return cmd->ci;
 }
 
@@ -333,6 +338,9 @@ static int catch_command_alias (char *li
 	               c == CRITERIA || \
 	               c == CUSUM || \
                        c == DATA || \
+		       c == DBG_CONT || \
+		       c == DBG_NEXT || \
+		       c == DBG_FUNC || \
                        c == END || \
 	               c == ENDLOOP || \
                        c == ESTIMATE || \
@@ -376,6 +384,7 @@ static int catch_command_alias (char *li
 		       c == SSCANF || \
                        c == SYSTEM || \
                        c == TABPRINT || \
+		       c == UNDEBUG || \
                        c == VARLIST || \
                        c == VIF)
 
@@ -4195,6 +4204,11 @@ int gretl_cmd_exec (ExecState *s, double
 			  cmd->opt | OPT_N, prn);
 	break;
 
+	case DBG_FUNC:
+	case UNDEBUG:
+	debug_function(cmd->param,(cmd->ci == DBG_FUNC) ? 0 : 1,prn);
+	break;
+
     case BREAK:
     case ENDLOOP:
 	pprintf(prn, _("You can't end a loop here, "
diff -urpN cvs/gretl/gretl/lib/src/libset.c gretl-cvs12/lib/src/libset.c
--- cvs/gretl/gretl/lib/src/libset.c	2009-05-14 15:53:38.000000000 +0100
+++ gretl-cvs12/lib/src/libset.c	2009-07-06 10:18:36.000000000 +0100
@@ -98,6 +98,7 @@ struct set_vars_ {
     gretl_matrix *initvals;     /* parameter initializer */
     struct robust_opts ropts;   /* robust standard error options */
     char shelldir[MAXLEN];      /* working dir for shell commands */
+	DEBUG_READLINE* dbg_readline;
 };
 
 #define ECHO "echo"
@@ -346,6 +347,8 @@ static void state_vars_copy (set_vars *s
     strcpy(sv->shelldir, state->shelldir);
 
     robust_opts_copy(&sv->ropts);
+
+	sv->dbg_readline = state->dbg_readline;
 }
 
 static void state_vars_init (set_vars *sv)
@@ -382,6 +385,8 @@ static void state_vars_init (set_vars *s
     *sv->shelldir = '\0';
 
     robust_opts_init(&sv->ropts);
+
+	sv->dbg_readline=NULL;
 }
 
 int get_bkbp_k (const DATAINFO *pdinfo)
@@ -1830,6 +1835,26 @@ static void libset_set_decpoint (int on)
 #endif
 }
 
+void libset_set_dbg_read(DEBUG_READLINE* dbg_readline) {
+
+    if (check_for_state()) {
+	return NULL;
+    }
+
+	 state->dbg_readline = dbg_readline;
+
+    return;
+}
+
+DEBUG_READLINE* libset_get_dbg_read() {
+
+    if (check_for_state()) {
+	return NULL;
+    }
+
+    return state->dbg_readline;
+}
+
 void libset_set_bool (const char *s, int set)
 {
     int flag;
diff -urpN cvs/gretl/gretl/lib/src/libset.h gretl-cvs12/lib/src/libset.h
--- cvs/gretl/gretl/lib/src/libset.h	2009-04-23 15:39:58.000000000 +0100
+++ gretl-cvs12/lib/src/libset.h	2009-07-06 10:18:36.000000000 +0100
@@ -20,6 +20,9 @@
 #ifndef LIBSET_H
 #define LIBSET_H
 
+#include "debugger.h"
+
+
 typedef enum {
     NORM_PHILLIPS,
     NORM_DIAG,
@@ -84,6 +87,9 @@ int libset_set_double (const char *s, do
 int libset_get_int (const char *s);
 int libset_set_int (const char *s, int k);
 
+DEBUG_READLINE* libset_get_dbg_read();
+void libset_set_dbg_read(DEBUG_READLINE* dbg_readline);
+
 /* GUI setter functions */
 void set_xsect_hccme (const char *s);
 void set_tseries_hccme (const char *s);
diff -urpN cvs/gretl/gretl/lib/src/options.c gretl-cvs12/lib/src/options.c
--- cvs/gretl/gretl/lib/src/options.c	2009-07-06 09:59:58.000000000 +0100
+++ gretl-cvs12/lib/src/options.c	2009-07-06 10:18:36.000000000 +0100
@@ -104,6 +104,9 @@ struct gretl_option gretl_opts[] = {
     { CUSUM,    OPT_R, "squares", 0 },
     { DATA,     OPT_O, "odbc", 0 },
     { DATAMOD,  OPT_P, "preserve", 0 },
+    { DBG_CONT, OPT_X, "continue", 0 },
+    { DBG_NEXT, OPT_X, "next", 0 },
+    { DBG_FUNC, 	OPT_F, "func", 0 },
     { DELEET,   OPT_D, "db", 0 },
     { DIFFTEST, OPT_G, "sign", 0 },
     { DIFFTEST, OPT_R, "rank-sum", 0 },
@@ -337,6 +340,7 @@ struct gretl_option gretl_opts[] = {
     { SYSTEM,   OPT_I, "iterate", 0 },
     { SYSTEM,   OPT_V, "verbose", 0 },
     { TOBIT,    OPT_V, "verbose", 0 },
+    { UNDEBUG, 	OPT_F, "func", 0 },
     { VAR,      OPT_D, "seasonals", 0 },
     { VAR,      OPT_F, "variance-decomp", 0 },
     { VAR,      OPT_I, "impulse-responses", 0 },
