reddit / emacs

ivy and counsel-fzf: get user input, if no file was found

pragmat1c1 · 2022-06-10 09:08
I am trying to use `counsel-fzf` in a way where I would like to get the input of the user when the search for a filename was unsuccesful. Reviewing the code for `ivy-read` and `counsel-fzf`, I have no clue where to start. So my workaround is this: (let* ((result (ivy-read "Search or enter new page name: " #'counsel-fzf-function :re-builder #'ivy--regex-fuzzy :dynamic-collection t)) (page-name (if (or (string= result "Nothing found") (string= result "error code 1")) ;; ;; This here is redundant as I have typed it in `ivy-read` anyway (read-string "Enter new page name: ") (file-name-sans-extension (file-name-nondirectory result)))) (page-filename (replace-regexp-in-string "[ ]+" "-" page-name))) (list result page-name page-filename)) I try to read via `ivy-read`, if it finds nothing, I let the user type his initial search again via `(read-string "Enter new page name: ")`. This is not user friendly. So I would like to know: Is there a way to get the input in the first step via `ivy-read`? ## Edit Or to put it this way: How can I get the user input if `completing-read` has no match?
DPaluche · 2022-06-10 16:23
Are you looking for ivy-immediate-done?
pragmat1c1 · 2022-06-10 21:58
Excellent. But I cannot figure how I pass it to `ivy-read`. This is the signature of ivy-read: ``` (cl-defun ivy-read (prompt collection &key predicate require-match initial-input history preselect def keymap update-fn sort action multi-action unwind re-builder matcher dynamic-collection extra-props caller) ``` Here is how I tried, without success: ``` (ivy-read "fzf: " #'counsel-fzf-function :initial-input nil :re-builder #'ivy--regex-fuzzy :dynamic-collection t :action #'ivy-immediate-done :caller 'counsel-fzf) ```
pragmat1c1 · 2022-06-10 22:05
Ok, get it: > select some directory and run ivy-immediate-done (i.e. C-M-j)
DPaluche · 2022-06-10 22:12
You’re just trying to create a new file from the counsel-fzf prompt instead of opening an existing one right?
xenodium · 2022-06-10 22:32
Not quite what you’re after, but may yield some ideas: https://xenodium.com/enhanced-emacs-searching-with-counsel-switches https://xenodium.com/emacs-counsel-default-search-switches