API
This package provides several different ways of triggering Visual
Replace, from different initial states. For example,
visual-replace uses the region to restrict the search, while
visual-replace-selection uses the region text as match string.
Another example is visual-replace-from-isearch, which copies the
state of a running search.
Many more variations are possible. You could imagine a variant of Visual Replace that uses the current region as match string unless it contains more tan one line, in which case it’d use it to restrict search.
In an attempt to support such variations, Visual Replace includes some public functions you’re encouraged to use to build your own variation.
To add a variation as the one described above, you could write the following, which modifies the default behavior in one specific case by overriding the search arguments and scope :
(defun my-visual-replace ()
(interactive)
(let (args scope)
(when (and (region-active-p)
(let ((beg (region-beginning))
(end (region-end)))
(save-excursion
(goto-char beg)
(not (search-forward "\n" end 'noerror)))))
(setq args (visual-replace-make-args
:from (buffer-substring-no-properties
(region-beginning) (region-end))
:to ""))
(setq scope (visual-replace-make-scope
;; Don't use bounds from the region
:bounds nil)))
(apply #'visual-replace
(visual-replace-read args scope))))
- (visual-replace-read ARGS SCOPE RUN-HOOK)function
Build arguments for running visual-replace and return them as a list, containing search range and modified
visual-replace-args.To call
visual-replacedirectly aftervisual-replace-read, useapply:(apply #'visual-replace (visual-replace-args args scope))This function takes two optional arguments, ARGS, created with
visual-replace-make-args, and SCOPE, created withvisual-replace-make-scope. If an argument is unspecified or nil, the default behavior applies.If both ARGS and SCOPE are nil,
visual-replace-readcallsvisual-replace-defaults-hookallow configuring the search using hooks unless RUN-HOOK is non-nil.- (visual-replace-make-args KEY-ARGS)function
This function builds a struct of type visual-replace-args. It can take the following key arguments:
:fromto specify the search text. It must be a regular expression in regexp mode.:toto specify the replacement text.This is often set to the empty string when
:fromis specified.When
:tois nil, which is the default, only the search text appears in the minibuffer and it it selected. When this is non-nil, even if it is an empty string, the search text is followed by a separator arrow, and the replacement text is selected.:regexpif non-nil, run a regexp search.:fromis a regular expression and:toa replacement string, which might include back-references surch as \&, \N or \,.:queryif non-nil, query replacements likequery-replacedoes.:wordif non-nil and:regexpis nil,:fromis searched as a word.:case-foldif non-nil, search is non case-sensitive and replacement are case-aware. Defaults tocase-fold-search.:backwardsif non-nil, replace backwards Defaults to nil (replace forwards).:lax-ws-non-regexpif non-nil, whitespaces in regexp searches skip text. Ignored in non-regexp searches. Defaults toreplace-lax-whitespace.:lax-ws-regexpif non-nil, whitespaces in non-regexp searches skip text. Ignored in regexp searches.
- (visual-replace-make-scope KEY-ARGS)function
This function builds a struct of type
visual-replace-scopethat configures the available scopes and sets the current search and replace scope. It can take the following key arguments::typedefines the scopevisual-replace-readstarts in. Set to'fullto search the whole buffer,'from-pointto search from given point to the end of the buffer,'regionto search within the given bounds.Setting this overrides the default, customizable by visual-replace-default-to-full-scope. Leave it to nil to keep the default behavior.
:pointdefine the starting point for the search and replace in “from point” mode. Defaults to(point).:boundsdefines the search ranges. This is in the same format as the one returned byregion-bounds: a list of cons cells of the form (START . END). Empty by default.:rectangleset it to non-nil if:boundscontain multiple cons cells that draw a rectangle. This controls how non-contiguous region is drawn. Nil by default.