Helm: Prompt which file name to insert with C-c i.

master
Pierre Neidhardt 2021-02-21 17:16:30 +01:00
parent 9a4dddacf2
commit 20b1e6a36c
2 changed files with 70 additions and 0 deletions

View File

@ -335,6 +335,7 @@ Useful for Guix."
(setf (slot-value source 'action-transformer) 'helm-occur-action-transformer))
(require 'patch-helm)
(require 'patch-helm-file-name-completion)
(when (require 'helm-switch-to-repl nil :noerror)
(helm-switch-to-repl-setup))

View File

@ -0,0 +1,69 @@
(defun ambrevar/helm-insert-file-name-completion-at-point (_candidate)
"Insert file name completion at point.
Like `helm-insert-file-name-completion-at-point' but prompt which
of the full, abbreviated or relative paths to insert."
(with-helm-current-buffer
(if buffer-read-only
(error "Error: Buffer `%s' is read-only" (buffer-name))
(let* ((mkds (helm-marked-candidates :with-wildcard t))
(candidate (car mkds))
(end (point))
(tap (helm-ffap-guesser))
(guess (and (stringp tap)
(substring-no-properties tap)))
(beg (helm-aif (and guess
(save-excursion
(when (re-search-backward
(regexp-quote guess)
(point-at-bol) t)
(point))))
it (point)))
(full-path-p (and (stringp guess)
(or (string-match-p
(concat "^" (getenv "HOME"))
guess)
(string-match-p
"\\`\\(/\\|[[:lower:][:upper:]]:/\\)"
guess))))
(escape-fn (if (memq major-mode
helm-modes-using-escaped-strings)
#'shell-quote-argument #'identity))
(fname-formats '(:full nil
:abbreviated (4)
:relative (16)
:basename (64)))
(format-fname (lambda (format)
(helm-ff--format-fname-to-insert
candidate beg end full-path-p guess
format)))
(abbreviated-path (funcall format-fname (plist-get fname-formats :abbreviated)))
(relative-path (funcall format-fname (plist-get fname-formats :relative)))
(basename-path (funcall format-fname (plist-get fname-formats :basename)))
(full-path (funcall format-fname (plist-get fname-formats :full)))
(format-fname-rest (lambda (candidate format)
(helm-ff--format-fname-to-insert
candidate nil nil nil nil
format))))
(when (and beg end)
(delete-region beg end))
(let* ((path-type (completing-read "Path type to insert: "
(list abbreviated-path
relative-path
basename-path
full-path)))
(format-choice (plist-get fname-formats (cond
((string= abbreviated-path path-type) :abbreviated)
((string= relative-path path-type) :relative)
((string= basename-path path-type) :basename)
((string= full-path path-type) :full)))))
(insert
(funcall
escape-fn
path-type)
(if (cdr mkds) " " "")
(mapconcat escape-fn
(cl-loop for f in (cdr mkds)
collect (funcall format-fname-rest f format-choice))
" ")))))))
(advice-add 'helm-insert-file-name-completion-at-point :override 'ambrevar/helm-insert-file-name-completion-at-point)