SLY: Escape wildcards and double quotes when completing / inserting pathnames.

master
Pierre Neidhardt 2021-06-08 09:03:14 +02:00
parent 934c3a5a75
commit 572dd078fc
3 changed files with 22 additions and 19 deletions

View File

@ -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

View File

@ -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)))))

View File

@ -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)