From 572dd078fc56a3f71d43bf50f78f2ecddf62cf8d Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Tue, 8 Jun 2021 09:03:14 +0200 Subject: [PATCH] SLY: Escape wildcards and double quotes when completing / inserting pathnames. --- .emacs.d/lisp/functions.el | 3 +++ .emacs.d/lisp/init-sly.el | 25 +++++++++++++------ .../lisp/patch-helm-file-name-completion.el | 13 +--------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/.emacs.d/lisp/functions.el b/.emacs.d/lisp/functions.el index cb8c0850..a7662423 100644 --- a/.emacs.d/lisp/functions.el +++ b/.emacs.d/lisp/functions.el @@ -52,6 +52,9 @@ See also `process-lines'." (ediff-get-region-contents ediff-current-difference 'A ediff-control-buffer) (ediff-get-region-contents ediff-current-difference 'B ediff-control-buffer)))) +(defun ambrevar/escape-cl-wildcards (s) + (replace-regexp-in-string "\\([]*?[]\\)" "\\\\\\1" s)) + (defun ambrevar/escape-region (&optional regex to-string) "Escape double-quotes and backslashes. This is useful for writing Elisp strings containing those diff --git a/.emacs.d/lisp/init-sly.el b/.emacs.d/lisp/init-sly.el index bac8b63c..49a986d2 100644 --- a/.emacs.d/lisp/init-sly.el +++ b/.emacs.d/lisp/init-sly.el @@ -200,6 +200,11 @@ Good substitute for `sly-mrepl-indent-and-complete-symbol'." "Prefix for `ambrevar/complete-cl-file'." :type 'string) + ;; #f is for FOF. + (defcustom ambrevar/complete-cl-reader-macros '("#p" "#f") + "List of supported reader macros for `ambrevar/complete-cl-file'." + :type 'list) + (defun ambrevar/complete-cl-file (action &rest _) (interactive (list 'interactive)) (cl-flet ((look-back @@ -210,19 +215,25 @@ Good substitute for `sly-mrepl-indent-and-complete-symbol'." (looking-back expr (when use-limit (- (point) (length expr)))))) - ;; #f is for FOF. - '("#p" "#f")))) + ambrevar/complete-cl-reader-macros)) + (cl-files + () + (mapcar (lambda (s) (prin1-to-string (make-symbol (ambrevar/escape-cl-wildcards s)))) + (company-files 'candidates ambrevar/complete-cl-file-prefix)))) (cl-case action (interactive (company-begin-backend #'my/complete-cl-file)) (candidates (cond ((and (save-match-data (look-back :use-limit t))) + ;; TODO: Is it possible to move point after quotes after completing? (if (looking-at-p "\"") - (company-files 'candidates ambrevar/complete-cl-file-prefix) - (mapcar (lambda (s) - (concat "\"" s "\"")) - (company-files 'candidates ambrevar/complete-cl-file-prefix)))) + (forward-char) + (insert "\"") + (save-excursion (insert "\""))) + (cl-files)) ((save-match-data (look-back :suffix "\"" :use-limit t)) - (company-files 'candidates ambrevar/complete-cl-file-prefix)))) + (mapcar (lambda (s) (prin1-to-string (make-symbol (ambrevar/escape-cl-wildcards s)))) + (company-files 'candidates ambrevar/complete-cl-file-prefix)) + (cl-files)))) (prefix (and (save-match-data (look-back :suffix "\"?")) "")) (t (company-files action))))) diff --git a/.emacs.d/lisp/patch-helm-file-name-completion.el b/.emacs.d/lisp/patch-helm-file-name-completion.el index ad68cc34..12c65723 100644 --- a/.emacs.d/lisp/patch-helm-file-name-completion.el +++ b/.emacs.d/lisp/patch-helm-file-name-completion.el @@ -27,18 +27,7 @@ of the full, abbreviated or relative paths to insert." guess)))) (escape-fn (cond ((derived-mode-p 'sly-mrepl-mode) - ;; TODO: Inserting the path as is does not work with - ;; #p if the file has a wildcard, so we must escape it. - ;; Or output uiop:ensure-pathname? - ;; - ;; In sbcl-ambrevar, we have FOF and we can use #f instead. - ;; - ;; (lambda (s) - ;; (if (string-match-p (regexp-opt '("[" "]" "*") ) - ;; s) - ;; (concat "(uiop:ensure-pathname \"" s "\")") - ;; (concat "#p\"" s "\""))) - #'prin1-to-string) + (lambda (s) (prin1-to-string (ambrevar/escape-cl-wildcards s)))) ((memq major-mode helm-modes-using-escaped-strings) #'shell-quote-argument)