X-Git-Url: https://code.delx.au/gnu-emacs-elpa/blobdiff_plain/3d72d1d6aef4aecdda3b068ba4de319f456d870a..8d7b29c99927fe1b84e60bd39c11053f9d4f2dcf:/packages/el-search/el-search.el diff --git a/packages/el-search/el-search.el b/packages/el-search/el-search.el index c56c9d0f9..2d032e622 100644 --- a/packages/el-search/el-search.el +++ b/packages/el-search/el-search.el @@ -292,11 +292,10 @@ error." case-fold-search))) (string-match-p regexp string))) -(defun el-search--print (expr) - (let ((print-quoted t) - (print-length nil) +(defun el-search--pp-to-string (expr) + (let ((print-length nil) (print-level nil)) - (prin1-to-string expr))) + (pp-to-string expr))) (defvar el-search-read-expression-map (let ((map (make-sparse-keymap))) @@ -310,12 +309,15 @@ error." "Map for reading input with `el-search-read-expression'.") (defun el-search--setup-minibuffer () + (let ((inhibit-read-only t)) + (put-text-property 1 (minibuffer-prompt-end) 'font-lock-face 'minibuffer-prompt)) (emacs-lisp-mode) (use-local-map el-search-read-expression-map) (setq font-lock-mode t) (funcall font-lock-function 1) - (backward-sexp) - (indent-sexp) + (goto-char (minibuffer-prompt-end)) + (when (looking-at ".*\n") + (indent-sexp)) (goto-char (point-max)) (when-let ((this-sexp (with-current-buffer (window-buffer (minibuffer-selected-window)) (thing-at-point 'sexp)))) @@ -333,14 +335,18 @@ error." (or hist 'read-expression-history) default))) (defvar el-search-history '() - "List of input strings.") + "List of search input strings.") + +(defvar el-search-query-replace-history '() + "List of input strings from `el-search-query-replace'.") (defvar el-search--initial-mb-contents nil) -(defun el-search--read-pattern (prompt &optional default read) +(defun el-search--read-pattern (prompt &optional default read histvar) + (cl-callf or histvar 'el-search-history) (let ((input (el-search-read-expression - prompt el-search--initial-mb-contents 'el-search-history default read))) - (if (or read (not (string= input ""))) input (car el-search-history)))) + prompt el-search--initial-mb-contents histvar default read))) + (if (or read (not (string= input ""))) input (car (symbol-value histvar))))) (defun el-search--end-of-sexp () ;;Point must be at sexp beginning @@ -356,9 +362,10 @@ and return it." (while not-done (let ((stop-here nil) (looking-at-from-back (lambda (regexp n) - (save-excursion - (backward-char n) - (looking-at regexp))))) + (and (> (point) n) + (save-excursion + (backward-char n) + (looking-at regexp)))))) (while (not stop-here) (cond ((eobp) (signal 'end-of-buffer nil)) @@ -517,8 +524,8 @@ return nil (no error)." (with-temp-buffer (emacs-lisp-mode) (insert (if splice - (mapconcat #'el-search--print replacement " ") - (el-search--print replacement))) + (mapconcat #'el-search--pp-to-string replacement " ") + (el-search--pp-to-string replacement))) (goto-char 1) (let (start this-sexp end orig-match-start orig-match-end done) (while (and (< (point) (point-max)) @@ -755,7 +762,7 @@ matches any of these expressions: "argument not a string or vector") `(pred (el-search--match-key-sequence ,key-sequence))) -(defun el-search--s (expr) +(defun el-search--transform-nontrivial-lpat (expr) (cond ((symbolp expr) `(or (symbol ,(symbol-name expr)) (,'\` (,'quote (,'\, (symbol ,(symbol-name expr))))) @@ -806,7 +813,7 @@ could use this pattern: ('_ '`(,_)) ('_? '(or '() `(,_))) ;FIXME: useful - document? or should we provide a (? PAT) ;thing? - (_ `(,'\` ((,'\, ,(el-search--s elt))))))) + (_ `(,'\` ((,'\, ,(el-search--transform-nontrivial-lpat elt))))))) lpats) ,@(if match-end '() '(_))))) @@ -936,7 +943,7 @@ You need `diff-hl-mode' turned on, provided by the library ;;;; Core functions ;;;###autoload -(defun el-search-pattern (pattern) +(defun el-search-pattern (pattern &optional no-error) "Start new or resume last elisp search. Search current buffer for expressions that are matched by `pcase' @@ -963,7 +970,7 @@ The following additional pattern types are currently defined:" (error "Please don't forget the quote when searching for a symbol")) (el-search--wrap-pattern pattern))))) (if (not (called-interactively-p 'any)) - (el-search--search-pattern pattern) + (el-search--search-pattern pattern no-error) (setq this-command 'el-search-pattern) ;in case we come from isearch (setq el-search-current-pattern pattern) (let ((opoint (point))) @@ -1011,12 +1018,12 @@ Hit any key to proceed." (unless (eq this-command last-command) (el-search-hl-other-matches pattern))) (let* ((region (list (point) (el-search--end-of-sexp))) - (substring (apply #'buffer-substring-no-properties region)) - (expr (read substring)) + (original-text (apply #'buffer-substring-no-properties region)) + (expr (read original-text)) (replaced-this nil) (new-expr (funcall get-replacement expr)) (get-replacement-string - (lambda () (el-search--format-replacement new-expr substring to-input-string splice))) + (lambda () (el-search--format-replacement new-expr original-text to-input-string splice))) (to-insert (funcall get-replacement-string)) (replacement-contains-another-match (with-temp-buffer @@ -1091,7 +1098,7 @@ Hit any key to proceed." (message "Replacement contains another match%s" (if replace-all " - falling back to interactive mode" "")) (setq replace-all nil) - (sit-for 3.))))))) + (sit-for 2.))))))) (el-search-hl-remove) (goto-char opoint) (message "Replaced %d matches%s" @@ -1101,9 +1108,31 @@ Hit any key to proceed." (defun el-search-query-replace--read-args () (barf-if-buffer-read-only) - (let* ((from (el-search--read-pattern "Query replace pattern: ")) - (to (let ((el-search--initial-mb-contents nil)) - (el-search--read-pattern "Replace with result of evaluation of: " from)))) + (let ((from-input (el-search--read-pattern "Query replace pattern: " nil nil + 'el-search-query-replace-history)) + from to) + (with-temp-buffer + (emacs-lisp-mode) + (insert from-input) + (goto-char 1) + (forward-sexp) + (skip-chars-forward " \t\n ") + ;; FIXME: maybe more sanity tests here... + (if (not (looking-at "->")) + (setq from from-input + to (let ((el-search--initial-mb-contents nil)) + (el-search--read-pattern "Replace with result of evaluation of: " from))) + (delete-char 2) + (goto-char 1) + (forward-sexp) + (setq from (buffer-substring 1 (point))) + (skip-chars-forward " \t\n ") + (setq to (buffer-substring (point) (progn (forward-sexp) (point)))))) + (unless (and el-search-query-replace-history + (not (string= from from-input)) + (string= from-input (car el-search-query-replace-history))) + (push (format "%s -> %s" from to) ;FIXME: add line break when FROM or TO is multiline? + el-search-query-replace-history)) (list (el-search--wrap-pattern (read from)) (read to) to))) ;;;###autoload @@ -1112,10 +1141,19 @@ Hit any key to proceed." TO-EXPR is an Elisp expression that is evaluated repeatedly for each match with bindings created in FROM-PATTERN in effect to -produce a replacement expression. +produce a replacement expression. Operate from point +to (point-max). As each match is found, the user must type a character saying -what to do with it. For directions, type ? at that time." +what to do with it. For directions, type ? at that time. + +As an alternative to enter FROM-PATTERN and TO-EXPR separately, +you can also give an input of the form + + FROM-PATTERN -> TO-EXPR + +to the first prompt and specify both expressions at once. This +format is also used for history entries." (interactive (el-search-query-replace--read-args)) (setq this-command 'el-search-query-replace) ;in case we come from isearch (setq el-search-current-pattern from-pattern)