diff --git a/femto.primitives.c b/femto.primitives.c index 84f5143..e13d200 100644 --- a/femto.primitives.c +++ b/femto.primitives.c @@ -62,6 +62,7 @@ extern int get_mode_current_buffer(char *); extern int save_buffer_byname(char *); extern int count_buffers(void); extern void display_prompt_and_response(char *, char *); +extern int replace_string_function(char *, char *, int); extern void msg(char *,...); extern void clear_message_line(void); extern void log_message(char *); @@ -153,6 +154,12 @@ Object *e_show_prompt(Interpreter *interp, Object **args, Object **env) return t; } +Object *e_replace_string_function(Interpreter *interp, Object **args, Object **env) +{ + int result = replace_string_function(FLISP_ARG_ONE->string, FLISP_ARG_TWO->string, FLISP_ARG_THREE->integer); + return (result == 1 ? t : nil); +} + Object *e_prompt(Interpreter *interp, Object **args, Object **env) { char response[81]; diff --git a/femto.register.c b/femto.register.c index f05f861..99bcbcb 100644 --- a/femto.register.c +++ b/femto.register.c @@ -21,6 +21,7 @@ available to be called as lisp functions {"get-point-max", 0, 0, 0, e_get_point_max}, {"set-key", 2, 2, TYPE_STRING, e_set_key}, {"prompt", 2, 2, TYPE_STRING, e_prompt}, + {"replace-string", 3, 3, TYPE_STRING, e_replace_string_function}, {"show-prompt", 2, 2, TYPE_STRING, e_show_prompt}, {"eval-block", 0, 0, 0, e_eval_block}, {"get-buffer-name", 0, 0, 0, e_get_buffer_name}, diff --git a/header.h b/header.h index 0ab868e..485144a 100644 --- a/header.h +++ b/header.h @@ -441,6 +441,7 @@ extern void execute_key(void); extern void make_key(char *, char *); extern void setup_keys(void); + /* functions in main.c */ extern int main(int argc, char **); extern void debug(char *format, ...); @@ -453,6 +454,8 @@ extern void gui(void); /* The GUI loop used in interactive mode */ /* functions in replace.c */ extern void query_replace(void); extern void replace_string(buffer_t *, char *, char *, int, int); +extern int replace_string_function(char *, char *, int); + /* functions in search.c */ extern point_t search_backwards(char *); diff --git a/lisp/dired.lsp b/lisp/dired.lsp index 187ba19..93fe198 100644 --- a/lisp/dired.lsp +++ b/lisp/dired.lsp @@ -15,15 +15,20 @@ ;; ;; ;; -;; (load "path/dired.lsp") ;; to load +;; +;; (load "./lisp/dired.lsp") ;; when testing locally or developing +;; ;; (dired) ;; to call ;; ;; (require 'femto) +;; +;; these contain the state of dired and need to be global ;; (setq dired-dir "") +(setq dired-fspec "") (setq dired-ls-cmd "ls -la ") (setq dired-buffer "*dired*") (setq de-obuf "*scratch*") @@ -36,7 +41,7 @@ (setq de-is-dir nil) (setq de-is-link nil) (setq de-ops 0) -(setq de-max-ops 300) +(setq de-max-ops 3000) (setq de-debug nil) (defun dired () @@ -45,7 +50,7 @@ (delete-other-windows) (setq de-obuf (get-buffer-name)) (kill-buffer dired-buffer) - (shell-command (concat dired-ls-cmd dired-dir)) + (shell-command (concat dired-ls-cmd dired-dir "/" dired-fspec)) (rename-buffer dired-buffer) (beginning-of-buffer) (set-mark) @@ -57,7 +62,6 @@ (de-get-info) (de-loop)) - (defun de-init() (setq de-start-line 1) (setq de-last-line 1) @@ -98,7 +102,7 @@ (setq de-ops (+ de-ops 1)) (cond ((< de-ops de-max-ops) - (message "dired menu: f,x") + (message "dired menu: s,f,x") (update-display) (setq de-key (get-key)) (cond @@ -124,12 +128,21 @@ (t (de-open-file))) (kill-buffer dired-buffer) (setq de-ops (+ de-max-ops 1))) + ((memq k '("s")) + (de-set-fspec) + (dired)) (t (log-debug (concat "command key=" k "\n"))))) (defun de-open-file() (find-file (concat dired-dir "/" de-name))) +;; +;; set the filter specification for the files (eg *.c) +;; +(defun de-set-fspec() + (setq dired-fspec (prompt "Enter file filter specification: " dired-fspec))) + (defun de-open-dir() (dired-debug de-name) (cond diff --git a/misc/questions.txt b/misc/questions.txt new file mode 100644 index 0000000..8d7cf4c --- /dev/null +++ b/misc/questions.txt @@ -0,0 +1,15 @@ + +what are .sht files ? + + + +/usr/share/femto/femto.rc -> startup + +startup + loads the extension files + set the config keys + load the local config from ~/HOME/.config/femto.rc + +core.lsp - not in the share directory +-- assume loaded by the standalone lisp programme + diff --git a/replace.c b/replace.c index 67f90bb..31e835a 100644 --- a/replace.c +++ b/replace.c @@ -84,6 +84,68 @@ void query_replace(void) msg("%d substitutions", numsub); } + +/* + +replace_string_function() + +Non interactive version for calling through lisp +Will replace search string from current point in buffer +to end of buffer + +rcount = number of replacements allowed, 0 means do all + +*/ + +int replace_string_function(char *s, char *r, int rcount) +{ + point_t o_point = curbp->b_point; + point_t l_point = -1; + point_t found; + int slen, rlen; /* length of search and replace strings */ + int numsub = 0; /* number of substitutions */ + + strcpy(searchtext, s); + strcpy(replace, r); + + slen = strlen(searchtext); + rlen = strlen(replace); + + /* + We should do some searious sanity checks here + to avoid potentiall infinite loops etc + */ + + /* scan through the file, from point */ + numsub = 0; + while(TRUE) { + + // have we done the allowed number of replacements ? + // rcount of 0 means do all + if (rcount > 0 && numsub >= rcount) + break; + + found = search_forward(searchtext); + + /* if not found set the point to the last point of replacement, or where we started */ + if (found == -1) { + curbp->b_point = (l_point == -1 ? o_point : l_point); + break; + } + + curbp->b_point = found; + /* search_forward places point at end of search, move to start of search */ + curbp->b_point -= slen; + + l_point = curbp->b_point; /* save last point */ + replace_string(curbp, searchtext, replace, slen, rlen); + numsub++; + } + + return numsub; +} + + void replace_string(buffer_t *bp, char *s, char *r, int slen, int rlen) { /*