From 4079a2a4644be890845daaa5df867fcdc5bed2ab Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Tue, 15 Dec 2020 11:32:38 +0100 Subject: [PATCH] Helm: Allow C-c C-x to open multiple files. --- .emacs.d/lisp/init-helm.el | 2 + .emacs.d/lisp/patch-helm.el | 91 +++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 .emacs.d/lisp/patch-helm.el diff --git a/.emacs.d/lisp/init-helm.el b/.emacs.d/lisp/init-helm.el index a396a65a..013f0755 100644 --- a/.emacs.d/lisp/init-helm.el +++ b/.emacs.d/lisp/init-helm.el @@ -367,6 +367,8 @@ Useful for Guix." (defmethod helm-setup-user-source ((source helm-moccur-class)) (setf (slot-value source 'action-transformer) 'helm-occur-action-transformer)) +(require 'patch-helm) + (when (require 'helm-switch-to-repl nil :noerror) (helm-switch-to-repl-setup)) diff --git a/.emacs.d/lisp/patch-helm.el b/.emacs.d/lisp/patch-helm.el new file mode 100644 index 00000000..5516857b --- /dev/null +++ b/.emacs.d/lisp/patch-helm.el @@ -0,0 +1,91 @@ +(defun ambrevar/helm-run-or-raise (exe &optional files) + "Run asynchronously EXE or jump to the application window. +If EXE is already running just jump to his window if +`helm-raise-command' is non-nil. +When FILE argument is provided run EXE with FILE." + (let* ((real-com (car (split-string exe))) + (proc (if files + (concat real-com " " (mapconcat #'identity files " ")) + real-com)) + process-connection-type) + (if (get-process proc) + (if helm-raise-command + (shell-command (format helm-raise-command real-com)) + (error "Error: %s is already running" real-com)) + (when (member real-com helm-external-commands-list) + (message "Starting %s..." real-com) + (if files + (start-process-shell-command + proc nil (format "%s %s" + real-com + (mapconcat + (lambda (file) + (shell-quote-argument + (if (eq system-type 'windows-nt) + (helm-w32-prepare-filename file) + (expand-file-name file)))) + files " "))) + (start-process-shell-command proc nil real-com)) + (set-process-sentinel + (get-process proc) + (lambda (process event) + (when (and (string= event "finished\n") + helm-raise-command + (not (helm-get-pid-from-process-name real-com))) + (shell-command (format helm-raise-command "emacs"))) + (message "%s process...Finished." process)))) + (setq helm-external-commands-list + (cons real-com + (delete real-com helm-external-commands-list)))))) + +;; This change was rejected upstream. +(defun ambrevar/helm-open-file-externally (_file) + "Open FILE with an external program. +Try to guess which program to use with +`helm-get-default-program-for-file'. +If not found or a prefix arg is given query the user which tool +to use." + (let* ((fname (expand-file-name (car (helm-marked-candidates)))) + (collection (helm-external-commands-list-1 'sort)) + (def-prog (helm-get-default-program-for-file fname)) + (program (if (or helm-current-prefix-arg (not def-prog)) + ;; Prefix arg or no default program. + (prog1 + (helm-comp-read + "Program: " collection + :must-match t + :name "Open file Externally" + :del-input nil + :history helm-external-command-history) + ;; Always prompt to set this program as default. + (setq def-prog nil)) + ;; No prefix arg or default program exists. + def-prog))) + (unless (or def-prog ; Association exists, no need to record it. + ;; Don't try to record non--filenames associations (e.g urls). + (not (file-exists-p fname))) + (when + (y-or-n-p + (format + "Do you want to make `%s' the default program for this kind of files? " + program)) + (helm-aif (assoc (file-name-extension fname) + helm-external-programs-associations) + (setq helm-external-programs-associations + (delete it helm-external-programs-associations))) + (push (cons (file-name-extension fname) + (helm-read-string + "Program (Add args maybe and confirm): " program)) + helm-external-programs-associations) + (customize-save-variable 'helm-external-programs-associations + helm-external-programs-associations))) + (ambrevar/helm-run-or-raise program (helm-marked-candidates)) + (setq helm-external-command-history + (cons program + (delete program + (cl-loop for i in helm-external-command-history + when (executable-find i) collect i)))))) + +(advice-add 'helm-open-file-externally :override 'ambrevar/helm-open-file-externally) + +(provide 'patch-helm)