Find file
Fetching contributors…
Cannot retrieve contributors at this time
8211 lines (7039 sloc) 320 KB
;;; smartparens.el --- Automatic insertion, wrapping and paredit-like navigation with user defined pairs.
;; Copyright (C) 2012-2016 Matus Goljer
;; Author: Matus Goljer <>
;; Maintainer: Matus Goljer <>
;; Created: 17 Nov 2012
;; Keywords: abbrev convenience editing
;; URL:
;; This file is not part of GNU Emacs.
;;; License:
;; This file is part of Smartparens.
;; Smartparens is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; Smartparens is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with Smartparens. If not, see <>.
;;; Commentary:
;; Smartparens is minor mode for Emacs that deals with parens pairs
;; and tries to be smart about it. It started as a unification effort
;; to combine functionality of several existing packages in a single,
;; compatible and extensible way to deal with parentheses, delimiters,
;; tags and the like. Some of these packages include autopair,
;; textmate, wrap-region, electric-pair-mode, paredit and others. With
;; the basic features found in other packages it also brings many
;; improvements as well as completely new features.
;; For a basic overview, see github readme at
;; For the complete documentation visit the documentation wiki located
;; at
;; If you like this project, you can donate here:
;;; Code:
(eval-when-compile (require 'cl)) ; for `lexical-let'
(require 'cl-lib)
(require 'dash)
(require 'thingatpt)
(eval-when-compile (defvar cua--region-keymap))
(declare-function cua-replace-region "cua-base")
(declare-function cua--pre-command-handler "cua-base")
(declare-function delete-selection-pre-hook "delsel")
;;; backport for older emacsen
;; introduced in 24.3
(unless (fboundp 'defvar-local)
(defmacro defvar-local (var val &optional docstring)
"Define VAR as a buffer-local variable with default value VAL.
Like `defvar' but additionally marks the variable as being automatically
buffer-local wherever it is set."
(declare (debug defvar) (doc-string 3))
;; Can't use backquote here, it's too early in the bootstrap.
(list 'progn (list 'defvar var val docstring)
(list 'make-variable-buffer-local (list 'quote var)))))
(defun sp-cheat-sheet (&optional arg)
"Generate a cheat sheet of all the smartparens interactive functions.
Without a prefix argument, print only the short documentation and examples.
With non-nil prefix argument, show the full documentation for each function.
You can follow the links to the function or variable help page.
To get back to the full list, use \\[help-go-back].
You can use `beginning-of-defun' and `end-of-defun' to jump to
the previous/next entry.
Examples are fontified using the `font-lock-string-face' for
better orientation."
(interactive "P")
(setq arg (not arg))
(require 'help-mode) ;; for help-xref-following #85
(let ((do-not-display '(
sp-splice-sexp-killing-around ;; is aliased to `sp-raise-sexp'
(do-not-display-with-arg '(
(commands (cl-loop for i in (cdr (assoc-string (file-truename (locate-library "smartparens")) load-history))
if (and (consp i) (eq (car i) 'defun) (commandp (cdr i)))
collect (cdr i))))
(with-current-buffer (get-buffer-create "*Smartparens cheat sheet*")
(let ((standard-output (current-buffer))
(help-xref-following t))
(read-only-mode -1)
(smartparens-mode 1)
(help-setup-xref (list #'sp-cheat-sheet)
(called-interactively-p 'interactive))
(read-only-mode -1)
(--each (--remove (or (memq it do-not-display)
(and arg (memq it do-not-display-with-arg)))
(unless (equal (symbol-name it) "advice-compilation")
(let ((start (point)) kill-from)
(insert (propertize (symbol-name it) 'face 'font-lock-function-name-face))
(insert " is ")
(describe-function-1 it)
(when arg
(goto-char start)
(forward-paragraph 1)
(forward-line 1)
(if (looking-at "^It is bound")
(forward-paragraph 2)
(forward-paragraph 1))
(setq kill-from (point))
(when (re-search-forward "^Examples:" nil t)
(delete-region kill-from
(forward-line 1)
(insert (propertize (concat
(make-string 72 ?―)
"\n\n") 'face 'font-lock-function-name-face)))))
(goto-char (point-min))
(while (re-search-forward "\\(->\\|​\\)" nil t)
(let ((thing (bounds-of-thing-at-point 'line)))
(put-text-property (car thing) (cdr thing) 'face 'font-lock-string-face)))
(goto-char (point-min))
(while (re-search-forward "|" nil t)
(put-text-property (1- (point)) (point) 'face 'font-lock-warning-face))
(goto-char (point-min))
(while (re-search-forward "^It is bound to \\(.*?\\)\\." nil t)
(put-text-property (match-beginning 1) (match-end 1) 'face 'font-lock-keyword-face))
(goto-char (point-min))
(while (re-search-forward ";;.*?$" nil t)
(put-text-property (match-beginning 0) (match-end 0) 'face 'font-lock-comment-face))
(goto-char (point-min))))
(pop-to-buffer "*Smartparens cheat sheet*")))
;; Variables
(defvar-local sp-forward-bound-fn nil
"Function to restrict the forward search")
(defvar-local sp-backward-bound-fn nil
"Function to restrict the backward search")
(defun sp--get-forward-bound ()
"Get the bound to limit the forward search for looking for pairs.
If it returns nil, the original bound passed to the search
function will be considered."
(and sp-forward-bound-fn (funcall sp-forward-bound-fn)))
(defun sp--get-backward-bound ()
"Get the bound to limit the backward search for looking for pairs.
If it returns nil, the original bound passed to the search
function will be considered."
(and sp-backward-bound-fn (funcall sp-backward-bound-fn)))
(defvar smartparens-mode-map (make-sparse-keymap)
"Keymap used for `smartparens-mode'.")
(defvaralias 'sp-keymap 'smartparens-mode-map)
(make-obsolete-variable 'sp-keymap 'smartparens-mode-map "2015-01-01")
(defvar sp-paredit-bindings '(
("C-M-f" . sp-forward-sexp) ;; navigation
("C-M-b" . sp-backward-sexp)
("C-M-u" . sp-backward-up-sexp)
("C-M-d" . sp-down-sexp)
("C-M-p" . sp-backward-down-sexp)
("C-M-n" . sp-up-sexp)
("M-s" . sp-splice-sexp) ;; depth-changing commands
("M-<up>" . sp-splice-sexp-killing-backward)
("M-<down>" . sp-splice-sexp-killing-forward)
("M-r" . sp-splice-sexp-killing-around)
("C-)" . sp-forward-slurp-sexp) ;; barf/slurp
("C-<right>" . sp-forward-slurp-sexp)
("C-}" . sp-forward-barf-sexp)
("C-<left>" . sp-forward-barf-sexp)
("C-(" . sp-backward-slurp-sexp)
("C-M-<left>" . sp-backward-slurp-sexp)
("C-{" . sp-backward-barf-sexp)
("C-M-<right>" . sp-backward-barf-sexp)
("M-S" . sp-split-sexp) ;; misc
"Alist containing the default paredit bindings to corresponding
smartparens functions.")
(defun sp--populate-keymap (bindings)
"Populates the `smartparens-mode-map' from the BINDINGS alist."
(--each bindings
(define-key smartparens-mode-map (read-kbd-macro (car it)) (cdr it))))
(defun sp-use-paredit-bindings ()
"Initiate `smartparens-mode-map' with paredit-compatible bindings for
corresponding functions provided by smartparens. See variable
(sp--populate-keymap sp-paredit-bindings))
(defvar sp-smartparens-bindings '(
("C-M-f" . sp-forward-sexp)
("C-M-b" . sp-backward-sexp)
("C-M-d" . sp-down-sexp)
("C-M-a" . sp-backward-down-sexp)
("C-S-d" . sp-beginning-of-sexp)
("C-S-a" . sp-end-of-sexp)
("C-M-e" . sp-up-sexp)
("C-M-u" . sp-backward-up-sexp)
("C-M-n" . sp-next-sexp)
("C-M-p" . sp-previous-sexp)
("C-M-k" . sp-kill-sexp)
("C-M-w" . sp-copy-sexp)
("M-<delete>" . sp-unwrap-sexp)
("M-<backspace>" . sp-backward-unwrap-sexp)
("C-<right>" . sp-forward-slurp-sexp)
("C-<left>" . sp-forward-barf-sexp)
("C-M-<left>" . sp-backward-slurp-sexp)
("C-M-<right>" . sp-backward-barf-sexp)
("M-D" . sp-splice-sexp)
("C-M-<delete>" . sp-splice-sexp-killing-forward)
("C-M-<backspace>" . sp-splice-sexp-killing-backward)
("C-S-<backspace>" . sp-splice-sexp-killing-around)
("C-]" . sp-select-next-thing-exchange)
("C-M-]" . sp-select-next-thing)
("M-F" . sp-forward-symbol)
("M-B" . sp-backward-symbol)
"Alist containing the default smartparens bindings.")
(defun sp-use-smartparens-bindings ()
"Initiate `smartparens-mode-map' with smartparens bindings for navigation functions.
See variable `sp-smartparens-bindings'."
(sp--populate-keymap sp-smartparens-bindings))
(defun sp--set-base-key-bindings (&optional symbol value)
"Set up the default keymap based on `sp-base-key-bindings'.
This function is also used as a setter for this customize value."
(when symbol (set-default symbol value))
((eq sp-base-key-bindings 'sp)
((eq sp-base-key-bindings 'paredit)
(defun sp--update-override-key-bindings (&optional symbol value)
"Override the key bindings with values from `sp-override-key-bindings'.
This function is also used as a setter for this customize value."
(when symbol (set-default symbol value))
;; this also needs to reload the base set, if any is present.
(sp--populate-keymap sp-override-key-bindings))
(defcustom sp-base-key-bindings nil
"A default set of key bindings for commands provided by smartparens.
Paredit binding adds the bindings in `sp-paredit-bindings' to the
corresponding smartparens commands. It does not add bindings to
any other commands, or commands that do not have a paredit
Smartparens binding adds the bindings in
`sp-smartparens-bindings' to most common smartparens commands.
These are somewhat inspired by paredit, but in many cases differ.
Note that neither \"paredit\" nor \"smartparens\" bindings add a
binding for all the provided commands."
:type '(radio
(const :tag "Don't use any default set of bindings" nil)
(const :tag "Use smartparens set of bindings" sp)
(const :tag "Use paredit set of bindings" paredit))
:set 'sp--set-base-key-bindings
:group 'smartparens)
(defcustom sp-override-key-bindings nil
"An alist of bindings and commands that should override the base key set.
If you wish to override a binding from the base set, set the
value for the binding to the `kbd' recognizable string constant
and command to the command symbol you wish to bind there.
If you wish to disable a binding from the base set, set the value
for the command to nil.
(\"C-M-f\" . sp-forward-sexp)
(\"C-<right>\" . nil)
See `sp-base-key-bindings'."
:type '(alist
:key-type string
:value-type symbol)
:set 'sp--update-override-key-bindings
:group 'smartparens)
(defvar sp-escape-char nil
"Character used to escape quotes inside strings.")
(make-variable-buffer-local 'sp-escape-char)
(defvar sp-comment-char nil
"Character used to start comments.")
(make-variable-buffer-local 'sp-comment-char)
(defvar sp-pair-list nil
"List of pairs for autoinsertion or wrapping.
Maximum length of opening or closing pair is
`sp-max-pair-length' characters.")
(make-variable-buffer-local 'sp-pair-list)
(defvar sp-local-pairs nil
"List of pair definitions used for current buffer.")
(make-variable-buffer-local 'sp-local-pairs)
(defvar sp-last-operation nil
"Symbol holding the last successful operation.")
(make-variable-buffer-local 'sp-last-operation)
(cl-defstruct sp-state
"Smartparens state for the current buffer."
;; A "counter" to track delayed hook. When a pair is inserted, a
;; cons of the form (:next . pair) is stored. On the next
;; (immediately after insertion) invocation of post-command-hook, it
;; is changed to (:this . pair). When the `car' is :this, the
;; post-command-hook checks the delayed hooks for `pair' and
;; executes them, then reset the "counter".
(defvar sp-state nil
"Smartparens state for the current buffer.")
(make-variable-buffer-local 'sp-state)
;; TODO: get rid of this
(defvar sp-previous-point -1
"Location of point before last command.
This is only updated when some pair-overlay is active. Do not
rely on the value of this variable anywhere else!")
(make-variable-buffer-local 'sp-previous-point)
;; TODO: get rid of this
(defvar sp-wrap-point nil
"Save the value of point before attemt to wrap a region.
Used for restoring the original state if the wrapping is
(make-variable-buffer-local 'sp-wrap-point)
;; TODO: get rid of this
(defvar sp-wrap-mark nil
"Save the value of mark before attemt to wrap a region.
Used for restoring the original state if the wrapping is
(make-variable-buffer-local 'sp-wrap-mark)
(defvar sp-last-inserted-characters ""
"Characters typed during the wrapping selection.
If wrapping is cancelled, these characters are re-inserted to the
location of point before the wrapping.")
(make-variable-buffer-local 'sp-last-inserted-characters)
(defvar sp-last-inserted-pair nil
"Last inserted pair.")
(make-variable-buffer-local 'sp-last-inserted-pair)
(defvar sp-delayed-pair nil
"A pair whose insertion is delayed to be carried out in
`sp--post-command-hook-handler'. The format is (opening delim
. beg of the opening delim)")
(make-variable-buffer-local 'sp-delayed-pair)
(defvar sp-last-wrapped-region nil
"Information about the last wrapped region.
The format is the same as returned by `sp-get-sexp'.")
(make-variable-buffer-local 'sp-last-wrapped-region)
(defvar sp-point-inside-string nil
"Non-nil if point is inside a string.
Used to remember the state from before `self-insert-command' is
(defvar sp-buffer-modified-p nil
"Non-nil if buffer was modified before the advice on
`self-insert-command' executed.")
(defvar sp-pre-command-point nil
"Position of `point' before `this-command' gets executed.")
(defconst sp-max-pair-length 10
"Maximum length of an opening or closing delimiter.
Only the pairs defined by `sp-pair' are considered. Tag pairs
can be of any length.")
(defconst sp-max-prefix-length 100
"Maximum length of a pair prefix.
Because prefixes for pairs can be specified using regular
expressions, they can potentially be of arbitrary length. This
settings solves the problem where the parser would decide to
backtrack the entire buffer which would lock up Emacs.")
(defvar sp-pairs '((t
((:open "\\\\(" :close "\\\\)" :actions (insert wrap autoskip navigate))
(:open "\\{" :close "\\}" :actions (insert wrap autoskip navigate))
(:open "\\(" :close "\\)" :actions (insert wrap autoskip navigate))
(:open "\\\"" :close "\\\"" :actions (insert wrap autoskip navigate))
(:open "\"" :close "\"" :actions (insert wrap autoskip navigate))
(:open "'" :close "'" :actions (insert wrap autoskip navigate))
(:open "(" :close ")" :actions (insert wrap autoskip navigate))
(:open "[" :close "]" :actions (insert wrap autoskip navigate))
(:open "{" :close "}" :actions (insert wrap autoskip navigate))
(:open "`" :close "`" :actions (insert wrap autoskip navigate)))))
"List of pair definitions.
Maximum length of opening or closing pair is
`sp-max-pair-length' characters.")
(defvar sp-tags nil
"List of tag definitions. See `sp-local-tag' for more information.")
(defvar sp-prefix-tag-object nil
"If non-nil, only consider tags while searching for next thing.")
(defvar sp-prefix-pair-object nil
"If non-nil, only consider pairs while searching for next thing.
Pairs are defined as expressions delimited by pairs from
(defvar sp-prefix-symbol-object nil
"If non-nil, only consider symbols while searching for next thing.
Symbol is defined as a chunk of text recognized by
(define-obsolete-variable-alias 'sp--lisp-modes 'sp-lisp-modes "2015-11-08")
(defcustom sp-lisp-modes '(
"List of Lisp modes."
:type '(repeat symbol)
:group 'smartparens)
(defcustom sp-no-reindent-after-kill-modes '(
"List of modes that should not reindent after kill."
:type '(repeat symbol)
:group 'smartparens)
(defvar sp--html-modes '(
"List of HTML modes.")
(defvar sp-message-alist
"Search failed. This means there is unmatched expression somewhere or we are at the beginning/end of file."
"Unmatched expression.")
"Opening or closing pair is inside a string or comment and matching pair is outside (or vice versa). Ignored.")
"Search failed. No matching tag found."
"No matching tag.")
"Invalid context: previous h-sexp ends after the next one."
"Invalid context.")
"Invalid context: current h-sexp starts after the next one."
"Invalid context.")
"Previous sexp starts after current h-sexp or no structure was found."
"No valid structure found.")
"This operation would result in invalid structure. Ignored."
"Ignored because of invalid structure.")
"We can't slurp without breaking strictly balanced expression. Ignored."
"Can't slurp without breaking balance.")
"Point is in blank sexp, nothing to barf."
"Point is in blank sexp.")
"Point has to be at least two levels deep to swap the enclosing delimiters."
"Point has to be at least two levels deep."
"Point not deep enough.")
"The expressions to be joined are of different type."
"Expressions are of different type."))
"List of predefined messages to be displayed by `sp-message'.
Each element is a list consisting of a keyword and one or more
strings, which are chosen based on the `sp-message-width'
variable. If the latter is `t', the first string is chosen as
default, which should be the most verbose option available.")
;; Customize & Mode definitions
(defgroup smartparens ()
"Smartparens minor mode."
:group 'editing
:prefix "sp-")
(define-minor-mode smartparens-mode
"Toggle smartparens mode.
You can enable pre-set bindings by customizing
`sp-base-key-bindings' variable. The current content of
`smartparens-mode-map' is:
:init-value nil
:lighter (" SP" (:eval (if smartparens-strict-mode "/s" "")))
:group 'smartparens
:keymap smartparens-mode-map
(if smartparens-mode
(run-hooks 'smartparens-enabled-hook))
(run-hooks 'smartparens-disabled-hook)))
(defvar smartparens-strict-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [remap delete-char] 'sp-delete-char)
(define-key map [remap delete-forward-char] 'sp-delete-char)
(define-key map [remap backward-delete-char-untabify] 'sp-backward-delete-char)
(define-key map [remap backward-delete-char] 'sp-backward-delete-char)
(define-key map [remap delete-backward-char] 'sp-backward-delete-char)
(define-key map [remap kill-word] 'sp-kill-word)
(define-key map [remap kill-line] 'sp-kill-hybrid-sexp)
(define-key map [remap backward-kill-word] 'sp-backward-kill-word)
"Keymap used for `smartparens-strict-mode'.")
(define-minor-mode smartparens-strict-mode
"Toggle the strict smartparens mode.
When strict mode is active, `delete-char', `kill-word' and their
backward variants will skip over the pair delimiters in order to
keep the structure always valid (the same way as `paredit-mode'
does). This is accomplished by remapping them to
`sp-delete-char' and `sp-kill-word'. There is also function
`sp-kill-symbol' that deletes symbols instead of words, otherwise
working exactly the same (it is not bound to any key by default).
When strict mode is active, this is indicated with \"/s\"
after the smartparens indicator in the mode list."
:init-value nil
:group 'smartparens
(if smartparens-strict-mode
(unless smartparens-mode
(smartparens-mode 1))
(unless (-find-indices (lambda (it) (eq (car it) 'smartparens-strict-mode)) minor-mode-overriding-map-alist)
(setq minor-mode-overriding-map-alist
(cons `(smartparens-strict-mode . ,smartparens-strict-mode-map) minor-mode-overriding-map-alist)))
(setq sp-autoskip-closing-pair 'always))
(setq minor-mode-overriding-map-alist
(-remove (lambda (it) (eq (car it) 'smartparens-strict-mode)) minor-mode-overriding-map-alist))
(let ((std-val (car (plist-get (symbol-plist 'sp-autoskip-closing-pair) 'standard-value)))
(saved-val (car (plist-get (symbol-plist 'sp-autoskip-closing-pair) 'saved-value))))
(setq sp-autoskip-closing-pair (eval (or saved-val std-val))))))
(define-globalized-minor-mode smartparens-global-strict-mode
:group 'smartparens)
(defun turn-on-smartparens-strict-mode ()
"Turn on `smartparens-strict-mode'."
(unless (or (member major-mode sp-ignore-modes-list)
(and (not (derived-mode-p 'comint-mode))
(eq (get major-mode 'mode-class) 'special)))
(smartparens-strict-mode 1)))
(defun sp--init ()
"Initialize the buffer local pair bindings and other buffer
local variables that depend on the active `major-mode'."
;; setup local state
(setq sp-state (make-sp-state))
;; setup local pair replacements
;; set the escape char
(dotimes (char 256)
(unless sp-escape-char
(when (= ?\\ (char-syntax char))
(setq sp-escape-char (string char))))
(unless sp-comment-char
(when (= ?< (char-syntax char))
(setq sp-comment-char (string char))))))
(defun sp--maybe-init ()
"Initialize the buffer if it is not already initialized. See `sp--init'."
(unless sp-pair-list
(defun sp--update-local-pairs ()
"Update local pairs after removal or at mode initialization."
(setq sp-local-pairs
(->> (sp--merge-with-local major-mode)
(--filter (plist-get it :actions))))
;; update the `sp-pair-list'. This is a list only containing
;; (open.close) cons pairs for easier querying. We also must order
;; it by length of opening delimiter in descending order (first
;; value is the longest)
(setq sp-pair-list
(->> sp-local-pairs
(--map (cons (plist-get it :open) (plist-get it :close)))
(-sort (lambda (x y) (> (length (car x)) (length (car y))))))))
(defun sp--update-local-pairs-everywhere (&rest modes)
"Run `sp--update-local-pairs' in all buffers.
This is necessary to update all the buffer-local definitions. If
MODES is non-nil, only update buffers with `major-mode' equal to
(setq modes (-flatten modes))
(--each (buffer-list)
(with-current-buffer it
(when (and smartparens-mode
(or (not modes)
(memq major-mode modes)))
(defcustom smartparens-enabled-hook nil
"Called after `smartparens-mode' is turned on."
:type 'hook
:group 'smartparens)
(defcustom smartparens-disabled-hook nil
"Called after `smartparens-mode' is turned off."
:type 'hook
:group 'smartparens)
;; global custom
(defcustom sp-ignore-modes-list '(
"Modes where smartparens mode is inactive if allowed globally."
:type '(repeat symbol)
:group 'smartparens)
(define-globalized-minor-mode smartparens-global-mode
(defun turn-on-smartparens-mode ()
"Turn on `smartparens-mode'.
This function is used to turn on `smartparens-global-mode'.
By default `smartparens-global-mode' ignores buffers with
`mode-class' set to special, but only if they are also not comint
Additionally, buffers on `sp-ignore-modes-list' are ignored.
You can still turn on smartparens in these mode manually (or
in mode's startup-hook etc.) by calling `smartparens-mode'."
(unless (or (member major-mode sp-ignore-modes-list)
(and (not (derived-mode-p 'comint-mode))
(eq (get major-mode 'mode-class) 'special)))
(smartparens-mode t)))
(defun turn-off-smartparens-mode ()
"Turn off `smartparens-mode'."
(smartparens-mode -1))
;; insert custom
(defcustom sp-autoinsert-pair t
"If non-nil, autoinsert pairs. See `sp-insert-pair'."
:type 'boolean
:group 'smartparens)
(defcustom sp-autoinsert-quote-if-followed-by-closing-pair nil
"If non-nil, autoinsert string quote pair even if the point is followed by closing pair.
This option only changes behaviour of the insertion process if
point is inside a string. In other words, if string is not
closed and next character is a closing pair.
For example, in a situation like this:
[\"some text|]
after pressing \", one would probably want to insert the closing
quote, not a nested pair (\\\"\\\"), to close the string literal
in the array. To enable such behaviour, set this variable to
Note: the values of this varible seem to be backward, i.e. it is
\"enabled\" when the value is nil. This was an unfortunate
choice of wording. It is kept this way to preserve backward
compatibility. The intended meaning is \"insert the pair if
followed by closing pair?\", t = yes."
:type 'boolean
:group 'smartparens)
(defcustom sp-autoskip-closing-pair 'always-end
"If t, skip the following closing pair if the expression is
active (that is right after insertion). This is controlled by
If set to \"always-end\", skip the closing pair even if the
expression is not active and point is at the end of the
expression. This only works for expressions with
single-character delimiters. If the expression is a string-like
expression, these must be enabled in current major-mode to work
with this setting, see `sp-navigate-consider-stringlike-sexp'.
If set to \"always\", `sp-up-sexp' is called whenever the closing
delimiter is typed inside a sexp of the same type. This is the
paredit-like behaviour. This setting only works for
single-character delimiters and does not work for string-like
See `sp-autoskip-opening-pair' for similar setting for
string-like delimiters.
See also `sp-skip-closing-pair'."
:type '(radio
(const :tag "Never skip closing delimiter" nil)
(const :tag "Skip closing delimiter in active expressions" t)
(const :tag "Always skip closing delimiter if at the end of sexp" always-end)
(const :tag "Always skip closing delimiter" always))
:group 'smartparens)
(make-variable-buffer-local 'sp-autoskip-closing-pair)
(defcustom sp-autoskip-opening-pair nil
"If non-nil, skip into the following string-like expression
instead of inserting a new pair."
:type 'boolean
:group 'smartparens)
(make-variable-buffer-local 'sp-autoskip-opening-pair)
(defcustom sp-cancel-autoskip-on-backward-movement t
"If non-nil, autoskip of closing pair is cancelled not only
when point is moved outside of the pair, but also if the point
moved backwards. See `sp-skip-closing-pair' for more info."
:type 'boolean
:group 'smartparens)
;; delete custom
(defcustom sp-autodelete-pair t
"If non-nil, auto delete pairs. See `sp-delete-pair'."
:type 'boolean
:group 'smartparens)
(defcustom sp-autodelete-closing-pair t
"If non-nil, auto delete the whole closing-pair. See `sp-delete-pair'."
:type 'boolean
:group 'smartparens)
(defcustom sp-autodelete-opening-pair t
"If non-nil, auto delete the whole opening-pair. See `sp-delete-pair'."
:type 'boolean
:group 'smartparens)
(defcustom sp-undo-pairs-separately nil
"If non-nil, put an `undo-boundary' before each inserted pair.
Calling undo after smartparens complete a pair will remove only
the pair before undoing any previous insertion.
WARNING: This option is implemented by hacking the
`buffer-undo-list'. Turning this option on might have
irreversible consequences on the buffer's undo information and in
some cases might remove important information. Usage of package
`undo-tree' is recommended if you ever need to revert to a state
unreachable by undo."
:type 'boolean
:group 'smartparens)
(defcustom sp-successive-kill-preserve-whitespace 1
"Control the behaviour of `sp-kill-sexp' on successive kills.
In the description, we consider more than one space
\"superfluous\", however, newlines are preserved."
:type '(radio
(const :tag "Always preserve the whitespace" 0)
(const :tag "Remove superfluous whitespace after last kill" 1)
(const :tag "Remove superfluous whitespace after all kills" 2))
:group 'smartparens)
;; wrap custom
(defcustom sp-autowrap-region t
"If non-nil, wrap the active region with pair."
:type 'boolean
:group 'smartparens)
(defcustom sp-wrap-show-possible-pairs t
"If non-nil, show possible pairs which can complete the wrapping."
:type 'boolean
:group 'smartparens)
(defcustom sp-autodelete-wrap t
"If non-nil, auto delete both opening and closing pair of most recent wrapping.
Deletion command must be the very first command after the
insertion, otherwise normal behaviour is applied."
:type 'boolean
:group 'smartparens)
(defcustom sp-wrap-repeat-last 1
"Context in which smartparens repeats the last wrap.
If the last operation was a wrap and we insert another pair at
the beginning or end of the last wrapped region, repeat the
wrap on this region with current pair."
:type '(radio
(const :tag "Do not repeat wrapping" 0)
(const :tag "Only repeat if current tag is the same as the last one" 1)
(const :tag "Always repeat if the point is after the opening/closing delimiter of last wrapped region" 2))
:group 'smartparens)
(defcustom sp-wrap-entire-symbol nil
"If non-nil, do NOT wrap the entire symbol, only the part after point.
If set to \"Enable globally\", smart symbol wrapping is active
everywhere. This is the default option.
If set to \"Disable globally\", smart symbol wrapping is disabled
Otherwise, a list of major modes where smart symbol wrapping is
*disabled* can be supplied.
foo-ba|r-baz -> (|foo-bar-baz) ;; if enabled
foo-ba|r-baz -> foo-ba(|r-baz) ;; if disabled"
:type '(choice
(const :tag "Enable globally" nil)
(const :tag "Disable globally" globally)
(repeat :tag "Disable in these major modes" symbol))
:group 'smartparens)
(defcustom sp-wrap-from-point nil
"If non-nil, do not wrap from the beginning of next expression but from point.
However, if the point is inside a symbol/word, the entire
symbol/word is wrapped. To customize this behaviour, see
variable `sp-wrap-entire-symbol'."
:type 'boolean
:group 'smartparens)
(defcustom sp-wrap-respect-direction nil
"When non-nil respect the wrap direction.
When non-nil, wrapping with opening pair always jumps to the
beginning of the region and wrapping with closing pair always
jumps to the end of the region.
When nil, closing pair places the point at the end of the region
and the opening pair leaves the point at its original
position (before or after the region)."
:type 'boolean
:group 'smartparens)
;; escaping custom
(defcustom sp-autoescape-string-quote t
"If non-nil, autoescape string quotes if typed inside string."
:type 'boolean
:group 'smartparens)
(make-obsolete-variable 'sp-autoescape-string-quote "smartparens' global autoescape system was removed." "2015-02-07")
(defcustom sp-autoescape-string-quote-if-empty '(
"List of modes where the string quotes aren't escaped if the string is empty.
You can list modes where multiple quote characters are used for
multi-line strings, such as `python-mode' to make the insertion
less annoying (that is, three times pressing \" would insert
\"\"\"|\"\"\" instead of \"\\\"\\\"|\\\"\\\"\")."
:type '(repeat symbol)
:group 'smartparens)
(make-obsolete-variable 'sp-autoescape-string-quote-if-empty "smartparens' global autoescape system was removed." "2015-02-07")
;; navigation & manip custom
(defcustom sp-navigate-consider-sgml-tags '(
"List of modes where sgml tags are considered to be sexps."
:type '(repeat symbol)
:group 'smartparens)
(defcustom sp-navigate-consider-stringlike-sexp '(
"List of modes where string-like sexps are considered to be sexps.
A string-like sexp is an expression where opening and closing
delimeter is the same sequence of characters. For example: *...*,
Warning: these are problematic in modes where the symbol might
have multiple functions, such as * in markdown, where it denotes
start of list item (unary) OR emphatic text (binary)."
:type '(repeat symbol)
:group 'smartparens)
(defcustom sp-navigate-use-textmode-stringlike-parser '((derived . text-mode))
"List of modes where textmode stringlike parser is used.
See `sp-get-textmode-stringlike-expression'.
Each element of the list can either be a symbol which is then
checked against `major-mode', or a cons (derived . PARENT-MODE),
where PARENT-MODE is checked using `derived-mode-p'."
:type '(repeat (choice
(symbol :tag "Major mode")
(cons :tag "Derived mode"
(const derived)
(symbol :tag "Parent major mode name"))))
:group 'smartparens)
(defvaralias 'sp-nagivate-use-textmode-stringlike-parser 'sp-navigate-use-textmode-stringlike-parser)
;; For backward compatibility?
(defcustom sp-navigate-consider-symbols t
"If non-nil, consider symbols outside balanced expressions as such.
Symbols are recognized by function `sp-forward-symbol'. This
setting affect all the navigation and manipulation functions
where it make sense.
Also, special handling of strings is enabled, where the whole
string delimited with \"\" is considered as one token.
WARNING: This is a legacy setting and changing its value to NIL
may break many things. It is kept only for backward
compatibility and will be removed in the next major release."
:type 'boolean
:group 'smartparens)
(defcustom sp-navigate-comments-as-sexps t
"If non-nil, consider comments as sexps in `sp-get-enclosing-sexp'.
If this option is enabled, unbalanced expressions in comments are
never automatically closed (see `sp-navigate-close-if-unbalanced')."
:type 'boolean
:group 'smartparens)
(defcustom sp-navigate-skip-match `(
(,sp-lisp-modes . sp--elisp-skip-match)
"Alist of list of major-modes and a function used to skip over matches in
`sp-get-paired-expression'. This function takes three arguments:
the currently matched delimiter, beginning of match and end of
match. If this function returns true, the current match will be
You can use this to skip over expressions that serve multiple
functions, such as if/end pair or unary if in Ruby or * in
markdown when it signifies list item instead of emphasis. If the
exception is only relevant to one pair, you should rather
use :skip-match option in `sp-local-pair'."
:type '(alist
:key-type (repeat symbol)
:value-type symbol)
:group 'smartparens)
(defcustom sp-navigate-reindent-after-up `(
"Modes where sexps should be reindented after jumping out of them with `sp-up-sexp'.
The whitespace between the closing delimiter and last \"thing\"
inside the expression is removed. It works analogically for the
If the mode is in the list \"interactive\", only reindent the sexp
if the command was called interactively. This is recommended for
general use.
If the mode is in the list \"always\", reindend the sexp even if the
command was called programatically."
:type '(alist
:options (interactive always)
:value-type (repeat symbol))
:group 'smartparens)
(defcustom sp-navigate-reindent-after-up-in-string t
"If non-nil, `sp-up-sexp' will reindent inside strings.
If `sp-navigate-reindent-after-up' is enabled and the point is
inside a string, this setting determines if smartparens should
reindent the current (string) sexp or not."
:type 'boolean
:group 'smartparens)
(defcustom sp-navigate-close-if-unbalanced nil
"If non-nil, insert the closing pair of the un-matched pair on `sp-up-sexp'.
The closing delimiter is inserted after the symbol at
point (using `sp-previous-sexp')."
:type 'boolean
:group 'smartparens)
(defcustom sp-sexp-prefix nil
"Alist of major-mode specific prefix specification.
Each item is a list with three properties:
- major mode
- a constant symbol 'regexp or 'syntax
- a regexp or a string containing syntax class codes.
If the second argument is 'regexp, the third argument is
interpreted as a regexp to search backward from the start of an
If the second argument is 'syntax, the third argument is
interpreted as string containing syntax codes that will be
You can also override this property locally for a specific pair
by specifying its :prefix property."
:type '(repeat
(list symbol
(const :tag "Regexp" regexp)
(const :tag "Syntax class codes" syntax))
:group 'smartparens)
(defcustom sp-sexp-suffix nil
"Alist of major-mode specific suffix specification.
Each item is a list with three properties:
- major mode
- a constant symbol 'regexp or 'syntax
- a regexp or a string containing syntax class codes.
If the second argument is 'regexp, the third argument is
interpreted as a regexp to search forward from the end of an
If the second argument is 'syntax, the third argument is
interpreted as string containing syntax codes that will be
You can also override this property locally for a specific pair
by specifying its :suffix property."
:type '(repeat
(list symbol
(const :tag "Regexp" regexp)
(const :tag "Syntax class codes" syntax))
:group 'smartparens)
(defcustom sp-split-sexp-always-split-as-string t
"Determine if sexp inside string is split.
If the point is inside a sexp inside a string, the default
behaviour is now to split the string, such that:
\"foo (|) bar\"
\"foo (\"|\") bar\"
instead of
\"foo ()|() bar\".
Note: the old default behaviour was the reverse, it would split
the sexp, but this is hardly ever what you want.
You can add a post-handler on string pair and check for
'split-string action to add concatenation operators of the
language you work in (in each major-mode you can have a separate
For example, in PHP the string concatenation operator is a
dot (.), so you would add:
(defun my-php-post-split-handler (_ action _)
(when (eq action 'split-sexp)
(insert \". . \")
(backward-char 3)))
(sp-local-pair 'php-mode \"'\" nil
:post-handlers '(my-php-post-split-handler))
echo 'foo |baz';
results in
echo 'foo' . | . 'baz';"
:type 'boolean
:group 'smartparens)
;; hybrid lines
(defcustom sp-hybrid-kill-excessive-whitespace nil
"If non-nil, `sp-kill-hybrid-sexp' will kill all whitespace up
until next hybrid sexp if the point is at the end of line or on a
blank line."
:type 'boolean
:group 'smartparens)
(defcustom sp-hybrid-kill-entire-symbol nil
"Governs how symbols under point are treated by `sp-kill-hybrid-sexp'.
If t, always kill the symbol under point.
If nil, never kill the entire symbol and only kill the part after point.
If a function, this should be a zero-arg predicate. When it
returns non-nil value, we should kill from point."
:type '(radio
(const :tag "Always kill entire symbol" t)
(const :tag "Always kill from point" nil)
(const :tag "Kill from point only inside strings" sp-point-in-string)
(function :tag "Custom predicate"))
:group 'smartparens)
(defcustom sp-comment-string nil
"String that is inserted after calling `sp-comment'.
It is an alist of list of major modes to a string.
The value of `comment-start' is used if the major mode is not found."
:type '(alist
:key-type (repeat symbol)
:value-type string)
:group 'smartparens)
;; ui custom
(defcustom sp-highlight-pair-overlay t
"If non-nil, autoinserted pairs are highlighted while point is inside the pair."
:type 'boolean
:group 'smartparens)
(defcustom sp-highlight-wrap-overlay t
"If non-nil, wrap overlays are highlighted during editing of the wrapping pair."
:type 'boolean
:group 'smartparens)
(defcustom sp-highlight-wrap-tag-overlay t
"If non-nil, wrap tag overlays are highlighted during editing of the wrapping tag pair."
:type 'boolean
:group 'smartparens)
(defcustom sp-message-width 'frame
"Length of information and error messages to display. If set to
'frame (the default), messages are chosen based of the frame
width. `t' means chose the default (verbose) message, `nil' means
mute. Integers specify the maximum width."
:type '(choice (const :tag "Fit to frame" frame)
(const :tag "Verbose" t)
(const :tag "Mute" nil)
(integer :tag "Max width"))
:group 'smartparens)
(defcustom sp-use-subword nil
"If non-nill, `sp-kill-word' and `sp-backward-kill-word' only
kill \"subwords\" when `subword-mode' is active."
:type 'boolean
:group 'smartparens)
;; Selection mode handling
(defun sp--delete-selection-p ()
"Return t if `delete-selection-mode' or `cua-delete-selection' is enabled."
(or (and (boundp 'delete-selection-mode) delete-selection-mode)
(and (boundp 'cua-delete-selection) cua-delete-selection cua-mode)))
(defadvice cua-replace-region (around fix-sp-wrap activate)
(if (sp-wrap--can-wrap-p)
(defadvice delete-selection-pre-hook (around fix-sp-wrap activate)
(unless (sp-wrap--can-wrap-p)
;; Misc/Utility functions
(defun sp--indent-region (start end &optional column)
"Call `indent-region' unless `aggressive-indent-mode' is enabled."
(unless (bound-and-true-p aggressive-indent-mode)
;; Don't issue "Indenting region..." message.
(cl-letf (((symbol-function 'message) #'ignore))
(indent-region start end column))))
(defmacro sp-with-modes (arg &rest forms)
"Add ARG as first argument to each form in FORMS.
This can be used with `sp-local-pair' calls to automatically
insert the modes."
(declare (indent 1)
(debug (form body)))
,@(mapcar (lambda (form) (append (list (car form) arg) (cdr form))) forms)))
(font-lock-add-keywords 'emacs-lisp-mode `((,(concat "("
(regexp-opt '("sp-with-modes"
"sp-compare-sexps") t)
(1 font-lock-keyword-face))))
(defun sp--evil-normal-state-p ()
"Checks to see if the current `evil-state' is in normal mode."
(and (fboundp 'evil-normal-state-p) (evil-normal-state-p)))
(defun sp--evil-motion-state-p ()
"Checks to see if the current `evil-state' is in motion mode."
(and (fboundp 'evil-motion-state-p) (evil-motion-state-p)))
(defun sp--evil-visual-state-p ()
"Checks to see if the current `evil-state' is in visual mode."
(and (fboundp 'evil-visual-state-p) (evil-visual-state-p)))
(defun sp--reverse-string (str)
"Reverse the string STR."
(concat (reverse (append str nil))))
(defun sp-point-in-blank-line (&optional p)
"Return non-nil if line at point is blank (whitespace only).
If optional argument P is present test this instead of point."
(when p (goto-char p))
(looking-at "[ \t]*$")))
(defun sp-point-in-blank-sexp (&optional p)
"Return non-nil if point is inside blank (whitespace only) sexp.
If optional argument P is present test this instead of point.
Warning: it is only safe to call this when point is inside a
sexp, otherwise the call may be very slow."
(when p (goto-char p))
(-when-let (enc (sp-get-enclosing-sexp))
(sp-get enc (string-match-p
"\\`[ \t\n]*\\'"
(buffer-substring-no-properties :beg-in :end-in))))))
(defun sp-point-in-string (&optional p)
"Return non-nil if point is inside string or documentation string.
If optional argument P is present test this instead of point."
(nth 3 (syntax-ppss p)))))
(defun sp-point-in-comment (&optional p)
"Return non-nil if point is inside comment.
If optional argument P is present test this instead off point."
(setq p (or p (point)))
(or (nth 4 (syntax-ppss p))
;; this also test opening and closing comment delimiters... we
;; need to chack that it is not newline, which is in "comment
;; ender" class in elisp-mode, but we just want it to be
;; treated as whitespace
(and (< p (point-max))
(memq (char-syntax (char-after p)) '(?< ?>))
(not (eq (char-after p) ?\n)))
;; we also need to test the special syntax flag for comment
;; starters and enders, because `syntax-ppss' does not yet
;; know if we are inside a comment or not (e.g. / can be a
;; division or comment starter...).
(-when-let (s (car (syntax-after p)))
(or (and (/= 0 (logand (lsh 1 16) s))
(nth 4 (syntax-ppss (+ p 2))))
(and (/= 0 (logand (lsh 1 17) s))
(nth 4 (syntax-ppss (+ p 1))))
(and (/= 0 (logand (lsh 1 18) s))
(nth 4 (syntax-ppss (- p 1))))
(and (/= 0 (logand (lsh 1 19) s))
(nth 4 (syntax-ppss (- p 2))))))))))
(defun sp-point-in-string-or-comment (&optional p)
"Return non-nil if point is inside string, documentation string or a comment.
If optional argument P is present, test this instead of point."
(or (sp-point-in-string p)
(sp-point-in-comment p)))
(defun sp-point-in-symbol (&optional p)
"Return non-nil if point is inside symbol.
Point is inside symbol if characters on both sides of the point
are in either word or symbol class."
(setq p (or p (point)))
(goto-char p)
(and (memq (char-syntax (following-char)) '(?w ?_))
(memq (char-syntax (preceding-char)) '(?w ?_)))))
(defun sp--single-key-description (event)
"Return a description of the last event. Replace all the function
key symbols with garbage character (ň).
TODO: fix this!"
(let ((original (single-key-description event)))
((string-match-p "<.*?>" original) "ň")
((string-match-p "SPC" original) " ")
(t original))))
(defun sp--split-string (string by)
"Split STRING on BY. This simply calls `split-string' and if it
returns a list of length one, empty string is inserted to the
(let ((sp (split-string string by)))
(if (not (cdr sp)) (cons "" sp) sp)))
;; see
(defun sp--current-indentation ()
"Get the indentation offset of the current line."
(defun sp--calculate-indentation-offset (old-column old-indentation)
"Calculate correct indentation after re-indent."
(let ((indentation (sp--current-indentation)))
;; Point was in code, so move it along with the re-indented code
((>= old-column old-indentation)
(+ old-column (- indentation old-indentation)))
;; Point was indentation, but would be in code now, so move to
;; the beginning of indentation
((<= indentation old-column) indentation)
;; Point was in indentation, and still is, so leave it there
(:else old-column))))
(defun sp--back-to-indentation (old-column old-indentation)
(let ((offset (sp--calculate-indentation-offset old-column old-indentation)))
(move-to-column offset)))
(defmacro sp--keep-indentation (&rest body)
"Execute BODY and restore the indentation."
(declare (indent 0)
(debug (body)))
(let ((c (make-symbol "c"))
(i (make-symbol "i")))
`(let ((,c (current-column))
(,i (sp--current-indentation)))
(sp--back-to-indentation ,c ,i))))
;; Please contribute these if you come across some!
(defvar sp--self-insert-commands
"List of commands that are some sort of `self-insert-command'.
Many modes rebind \"self-inserting\" keys to \"smart\" versions
which do some additional processing before delegating the
insertion to `self-insert-command'. Smartparens needs to be able
to distinguish these to properly handle insertion and reinsertion
of pairs and wraps.")
;; Please contribute these if you come across some!
(defvar sp--special-self-insert-commands
"List of commands which are handled as if they were `self-insert-command's.
Some modes redefine \"self-inserting\" keys to \"smart\" versions
which do some additional processing but do _not_ delegate the
insertion to `self-insert-command', instead inserting via
`insert'. Smartparens needs to be able to distinguish these to
properly handle insertion and reinsertion of pairs and wraps.
The `sp--post-self-insert-hook-handler' is called in the
`post-command-hook' for these commands.")
(defun sp--self-insert-command-p ()
"Return non-nil if `this-command' is some sort of `self-insert-command'."
(memq this-command sp--self-insert-commands))
(defun sp--special-self-insert-command-p ()
"Return non-nil if `this-command' is \"special\" self insert command.
A special self insert command is one that inserts a character but
does not trigger `post-self-insert-hook'."
(memq this-command sp--special-self-insert-commands))
(defun sp--signum (x)
"Return 1 if X is positive, -1 if negative, 0 if zero."
(cond ((> x 0) 1) ((< x 0) -1) (t 0)))
(cl-eval-when (compile eval load)
(defun sp--get-substitute (struct list)
"Only ever call this from sp-get! This function does the
replacement of all the keywords with actual calls to sp-get."
(if (listp list)
(if (eq (car list) 'sp-get)
(mapcar (lambda (x) (sp--get-substitute struct x))
(let ((command (car list)))
((eq command 'sp-do-move-op)
(let ((argument (make-symbol "--sp-argument--")))
`(let ((,argument ,(cadr list)))
(if (< ,argument :beg-prf)
(goto-char :beg-prf)
(delete-char (+ :op-l :prefix-l))
(goto-char ,argument)
(insert :prefix :op))
(goto-char ,argument)
(insert :prefix :op)
(goto-char :beg-prf)
(delete-char (+ :op-l :prefix-l))))))
((eq command 'sp-do-move-cl)
(let ((argument (make-symbol "--sp-argument--")))
`(let ((,argument ,(cadr list)))
(if (> ,argument :end-in)
(goto-char ,argument)
(insert :cl :suffix)
(goto-char :end-in)
(delete-char (+ :cl-l :suffix-l)))
(goto-char :end-in)
(delete-char (+ :cl-l :suffix-l))
(goto-char ,argument)
(insert :cl :suffix)))))
((eq command 'sp-do-del-op)
(goto-char :beg-prf)
(delete-char (+ :op-l :prefix-l))))
((eq command 'sp-do-del-cl)
(goto-char :end-in)
(delete-char (+ :cl-l :suffix-l))))
((eq command 'sp-do-put-op)
(goto-char ,(cadr list))
(insert :prefix :op)))
((eq command 'sp-do-put-cl)
(goto-char ,(cadr list))
(insert :cl :suffix)))
(t list)))))
(if (keywordp list)
(sp--get-replace-keyword struct list)
(defun sp--get-replace-keyword (struct keyword)
(cl-case keyword
;; point in buffer before the opening delimiter
(:beg `(plist-get ,struct :beg))
;; point in the buffer after the closing delimiter
(:end `(plist-get ,struct :end))
;; point in buffer after the opening delimiter
(:beg-in `(+ (plist-get ,struct :beg) (length (plist-get ,struct :op))))
;; point in buffer before the closing delimiter
(:end-in `(- (plist-get ,struct :end) (length (plist-get ,struct :cl))))
;; point in buffer before the prefix of this expression
(:beg-prf `(- (plist-get ,struct :beg) (length (plist-get ,struct :prefix))))
;; point in the buffer after the suffix of this expression
(:end-suf `(+ (plist-get ,struct :end) (length (plist-get ,struct :suffix))))
;; opening delimiter
(:op `(plist-get ,struct :op))
;; closing delimiter
(:cl `(plist-get ,struct :cl))
;; length of the opening pair
(:op-l `(length (plist-get ,struct :op)))
;; length of the closing pair
(:cl-l `(length (plist-get ,struct :cl)))
;; length of the entire expression, including enclosing
;; delimiters and the prefix and suffix
(:len `(- (plist-get ,struct :end)
(plist-get ,struct :beg)
(- (length (plist-get ,struct :prefix)))
(- (length (plist-get ,struct :suffix)))))
;; length of the the pair ignoring the prefix, including delimiters
(:len-out `(- (plist-get ,struct :end) (plist-get ,struct :beg)))
;; length of the pair inside the delimiters
(:len-in `(- (plist-get ,struct :end)
(plist-get ,struct :beg)
(length (plist-get ,struct :op))
(length (plist-get ,struct :cl))))
;; expression prefix
(:prefix `(plist-get ,struct :prefix))
;; expression prefix length
(:prefix-l `(length (plist-get ,struct :prefix)))
(:suffix `(plist-get ,struct :suffix))
(:suffix-l `(length (plist-get ,struct :suffix)))
;; combined op/cl and suffix/prefix
(:opp `(concat (plist-get ,struct :prefix)
(plist-get ,struct :op)))
(:opp-l `(+ (length (plist-get ,struct :prefix))
(length (plist-get ,struct :op))))
(:cls `(concat (plist-get ,struct :cl)
(plist-get ,struct :suffix)))
(:cls-l `(+ (length (plist-get ,struct :cl))
(length (plist-get ,struct :suffix))))
(t keyword))))
;; The structure returned by sp-get-sexp is a plist with following properties:
;; :beg - point in the buffer before the opening delimiter (ignoring prefix)
;; :end - point in the buffer after the closing delimiter
;; :op - opening delimiter
;; :cl - closing delimiter
;; :prefix - expression prefix
;; This structure should never be accessed directly and should only be
;; exposed by the sp-get macro. This way, we can later change the
;; internal representation without much trouble.
;; TODO: rewrite this in terms of `symbol-macrolet' ??
(defmacro sp-get (struct &rest forms)
"Get a property from a structure.
STRUCT is a plist with the format as returned by `sp-get-sexp'.
Which means this macro also works with `sp-get-symbol',
`sp-get-string' and `sp-get-thing'.
FORMS is an attribute we want to query. Currently supported
attributes are:
:beg - point in buffer before the opening delimiter
:end - point in the buffer after the closing delimiter
:beg-in - point in buffer after the opening delimiter
:end-in - point in buffer before the closing delimiter
:beg-prf - point in buffer before the prefix of this expression
:end-suf - point in buffer after the suffix of this expression
:op - opening delimiter
:cl - closing delimiter
:op-l - length of the opening pair
:cl-l - length of the closing pair
:len - length of the entire expression, including enclosing
delimiters, the prefix and the suffix
:len-out - length of the the pair ignoring the prefix and suffix,
including delimiters
:len-in - length of the pair inside the delimiters
:prefix - expression prefix
:prefix-l - expression prefix length
:suffix - expression suffix
:suffix-l - expression suffix length
These special \"functions\" are expanded to do the selected
action in the context of currently queried pair:
\(sp-do-del-op) - remove prefix and opening delimiter
\(sp-do-del-cl) - remove closing delimiter and suffix
\(sp-do-move-op p) - move prefix and opening delimiter to point p
\(sp-do-move-cl p) - move closing delimiter and suffix to point p
\(sp-do-put-op p) - put prefix and opening delimiter at point p
\(sp-do-put-cl p) - put closing delimiter and suffix at point p
In addition to these simple queries and commands, this macro
understands arbitrary forms where any of the aforementioned
attributes are used. Therefore, you can for example query for
\"(+ :op-l :cl-l)\". This query would return the sum of lengths
of opening and closing delimiter. A query
\"(concat :prefix :op)\" would return the string containing
expression prefix and the opening delimiter.
Special care is taken to only evaluate the STRUCT argument once."
(declare (indent 1)
(debug (form body)))
(let ((st (make-symbol "struct")))
(sp--get-substitute st `(let ((,st ,struct)) ,@forms))))
(defmacro sp-compare-sexps (a b &optional fun what-a what-b)
"Return non-nil if the expressions A and B are equal.
Two expressions are equal if their :beg property is the same.
If optional argument WHAT is non-nil, use it as a keyword on
which to do the comparsion."
(declare (debug (form form &optional functionp keywordp keywordp)))
(setq fun (or fun 'equal))
(setq what-a (or what-a :beg))
(setq what-b (or what-b what-a))
`(,fun (sp-get ,a ,what-a) (sp-get ,b ,what-b)))
(defun sp-message (key)
"Display a message. The argument is either a string or list of
strings, or a keyword, in which case the string list is looked up
in `sp-message-alist'. The string to be displayed is chosen based
on the `sp-message-width' variable."
(let ((msgs (cond ((listp key) key)
((stringp key) (list key))
(t (cdr (assq key sp-message-alist))))))
(when (and msgs sp-message-width)
(if (eq sp-message-width t)
(message (car msgs))
(let ((maxlen (if (eq sp-message-width 'frame)
(s nil))
(dolist (msg msgs)
(if (and (<= (length msg) maxlen)
(> (length msg) (length s)))
(setf s msg)))
(when s
(message s)))))))
;; Adding/removing of pairs/bans/allows etc.
(defun sp--merge-prop (old-pair new-pair prop)
"Merge a property PROP from NEW-PAIR into OLD-PAIR.
The list OLD-PAIR must not be nil."
(let ((new-val (plist-get new-pair prop)))
(cl-case prop
(:close (plist-put old-pair :close new-val))
(:prefix (plist-put old-pair :prefix new-val))
(:suffix (plist-put old-pair :suffix new-val))
(:skip-match (plist-put old-pair :skip-match new-val))
(:trigger (plist-put old-pair :trigger new-val))
((:actions :when :unless :pre-handlers :post-handlers)
(cl-case (car new-val)
(:add (plist-put old-pair prop (-union (plist-get old-pair prop) (cdr new-val))))
(:rem (plist-put old-pair prop (-difference (plist-get old-pair prop) (cdr new-val))))
;; this means we have ((:add ...) (:rem ...)) argument
((and new-val
(listp (car new-val))
(memq (caar new-val) '(:add :rem)))
(let ((a (assq :add new-val))
(r (assq :rem new-val)))
(plist-put old-pair prop (-union (plist-get old-pair prop) (cdr a)))
(plist-put old-pair prop (-difference (plist-get old-pair prop) (cdr r)))))
(plist-put old-pair prop (plist-get new-pair prop))))))))))
(defun sp--merge-pairs (old-pair new-pair)
This modifies the OLD-PAIR by side effect."
(let ((ind 0))
(--each new-pair
(when (= 0 (% ind 2))
(sp--merge-prop old-pair new-pair it))
(setq ind (1+ ind))))
(defun sp--update-pair (old-pair new-pair)
"Copy properties from NEW-PAIR to OLD-PAIR.
The list OLD-PAIR must not be nil."
(let ((ind 0))
(--each new-pair
(when (= 0 (% ind 2))
(when (or (not (plist-get old-pair it))
;; HACK: we don't want to overwrite list properties
;; that aren't just :add with :add because this
;; would break the "idempotency".
(not (equal '(:add) (plist-get new-pair it))))
(plist-put old-pair it (plist-get new-pair it))))
(setq ind (1+ ind))))
(defun sp--update-pair-list (pair mode)
"Update the PAIR for major mode MODE. If this pair is not
defined yet for this major mode, add it. If this pair is already
defined, replace all the properties in the old definition with
values from PAIR."
;; get the structure relevant to mode. t means global setting
(let ((struct (--first (eq mode (car it)) sp-pairs)))
(if (not struct)
(!cons (cons mode (list pair)) sp-pairs)
;; this does NOT merge changes, only replace the values at
;; properties. Open delimiter works as ID as usual.
(let ((old-pair (--first (equal (plist-get pair :open)
(plist-get it :open))
(cdr struct))))
(if (not old-pair)
(setcdr struct (cons pair (cdr struct)))
(sp--update-pair old-pair pair)))))
(defun sp--get-pair (open list)
"Get the pair with id OPEN from list LIST."
(--first (equal open (plist-get it :open)) list))
(defun sp--get-pair-definition (open list &optional prop)
"Get the definition of a pair identified by OPEN from list LIST.
If PROP is non-nil, return the value of that property instead."
(let ((pair (sp--get-pair open list)))
(if prop
((eq prop :op-l)
(length (plist-get pair :open)))
((eq prop :cl-l)
(length (plist-get pair :close)))
((eq prop :len)
(+ (length (plist-get pair :open)) (length (plist-get pair :close))))
((eq prop :post-handlers)
(--filter (not (listp it)) (plist-get pair prop)))
((eq prop :post-handlers-cond)
(--filter (listp it) (plist-get pair :post-handlers)))
((eq prop :when)
(--filter (not (listp it)) (plist-get pair :when)))
((eq prop :when-cond)
(-flatten (-concat (--filter (listp it) (plist-get pair :when)))))
(t (plist-get pair prop)))
(defun sp-get-pair-definition (open mode &optional prop)
"Get the definition of pair identified by OPEN (opening
delimiter) for major mode MODE (or global definition if MODE is
If PROP is non-nil, return the value of that property instead."
(sp--get-pair-definition open (cdr (assq mode sp-pairs)) prop))
(defun sp-get-pair (open &optional prop)
"Return the current value of pair defined by OPEN in the
current buffer, querying the variable `sp-local-pairs'.
If PROP is non-nil, return the value of that property instead."
(sp--get-pair-definition open sp-local-pairs prop))
(defun sp--merge-with-local (mode)
"Merge the global pairs definitions with definitions for major mode MODE."
(let* ((global (cdr (assq t sp-pairs)))
(local (cdr (assq mode sp-pairs)))
(result nil))
;; copy the pairs on global list first. This creates new plists
;; so we can modify them without changing the global "template"
;; values.
(dolist (old-pair global)
(!cons (list :open (plist-get old-pair :open)) result))
;; merge the global list with result. This basically "deep copy"
;; global list. We use `sp--merge-pairs' because it also clones
;; the list properties (actions, filters etc.)
(dolist (new-pair global)
(let ((old-pair (sp--get-pair (plist-get new-pair :open) result)))
(sp--merge-pairs old-pair new-pair)))
;; for each local pair, merge it into the global definition
(dolist (new-pair local)
(let ((old-pair (sp--get-pair (plist-get new-pair :open) result)))
(if old-pair
(sp--merge-pairs old-pair new-pair)
;; pair does not have global definition, simply copy it over
;; this "deep copy" the new-pair
(sp--merge-pairs (list :open (plist-get new-pair :open)) new-pair)
;; TODO: remove the nil lists from the definitions
(defun sp-wrap-with-pair (pair)
"Wrap the following expression with PAIR.
This function is a non-interactive helper. To use this function
interactively, bind the following lambda to a key:
(lambda (&optional arg) (interactive \"P\") (sp-wrap-with-pair \"(\"))
This lambda accepts the same prefix arguments as
If region is active and `use-region-p' returns true, the region
is wrapped instead. This is useful with selection functions in
`evil-mode' to wrap regions with pairs."
(let* ((arg (or current-prefix-arg 1))
(sel (and (not (use-region-p))
;; point is inside symbol and smart symbol wrapping is disabled
((and (sp-point-in-symbol)
(or (eq sp-wrap-entire-symbol 'globally)
(memq major-mode sp-wrap-entire-symbol)))
;; wrap from point, not the start of the next expression
((and sp-wrap-from-point
(not (sp-point-in-symbol)))
(active-pair (--first (equal (car it) pair) sp-pair-list))
(rb (region-beginning))
(re (region-end)))
(goto-char re)
(insert (cdr active-pair))
(goto-char rb)
(insert (car active-pair))
(if (use-region-p)
(sp--indent-region rb re)
(sp-get sel (sp--indent-region :beg :end)))))
(cl-defun sp-pair (open
(actions '(wrap insert autoskip navigate))
"Add a pair definition.
OPEN is the opening delimiter. Every pair is uniquely determined
by this string.
CLOSE is the closing delimiter. You can use nil for this
argument if you are updating an existing definition. In this
case, the old value is retained.
TRIGGER is an optional trigger for the pair. The pair will be
inserted if either OPEN or TRIGGER is typed. This is usually
used as a shortcut for longer pairs or for pairs that can't be
typed easily.
ACTIONS is a list of actions that smartparens will perform with
this pair. Possible values are:
- insert - autoinsert the closing pair when opening pair is
- wrap - wrap an active region with the pair defined by opening
delimiter if this is typed while region is active.
- autoskip - if the sexp is active or `sp-autoskip-closing-pair' is
set to 'always, skip over the closing delimiter if user types its
characters in order.
- navigate - enable this pair for navigation/highlight and strictness
If the ACTIONS argument has value :rem, the pair is removed.
This can be used to remove default pairs you don't want to use.
For example: (sp-pair \"[\" nil :actions :rem)
WHEN is a list of predicates that test whether the action
should be performed in current context. The values in the list
should be names of the predicates (that is symbols, not
lambdas!). They should accept three arguments: opening
delimiter (which uniquely determines the pair), action and
context. The context argument can have values:
- string - if point is inside string.
- comment - if point is inside comment.
- code - if point is inside code. This context is only
recognized in programming modes that define string semantics.
If *any* filter returns t, the action WILL be performed. A number
of filters are predefined: `sp-point-after-word-p',
`sp-point-before-word-p', `sp-in-string-p',
`sp-point-before-eol-p' etc.
When clause also supports a special format for delayed insertion.
The condition is a list with commands, predicates (with three
arguments as regular when form) or strings specifying the last
event. All three types can be combined in one list. The pair
will be inserted *after* the next command if it matches the any
command on the list, if the last event matches any string on the
list or if any predicate returns true. If the pair's :when
clause contains this special form, it will never be immediately
inserted and will always test for delayed insertion.
UNLESS is a list of predicates. The conventions are the same as
for the WHEN list. If *any* filter on this list returns t, the
action WILL NOT be performed. The predicates in the WHEN list
are checked first, and if any of them succeeds, the UNLESS list
is not checked.
Note: the functions on the WHEN/UNLESS lists are also called
\"filters\" in the documentation.
All the filters are run *after* the trigger character is
PRE-HANDLERS is a list of functions that are called before there
has been some action caused by this pair. The arguments are the
same as for filters. Context is relative to the point *before*
the last inserted character. Because of the nature of the
wrapping operation, this hook is not called if the action is
POST-HANDLERS is a list of functions that are called after there
has been some action caused by this pair. The arguments are the
same as for filters. Context is relative to current position of
point *after* the closing pair was inserted.
After a wrapping action, the point might end on either side of
the wrapped region, depending on the original direction. You can
use the variable `sp-last-wrapped-region' to retrieve information
about the wrapped region and position the point to suit your
A special syntax for conditional execution of hooks is also
supported. If the added item is a list (function command1
command2...), where function is a 3 argument function described
above and command(s) can be either name of a command or a string
representing an event. If the last command or event as described
by `single-key-description' matches any on the list, the hook
will be executed. This means these hooks are run not after the
insertion, but after the *next* command is executed.
((lambda (id act con)
(newline))) \"RET\" newline)
This function will move the closing pair on its own line only if
the next command is `newline' or is triggered by RET. Otherwise
the pairs stay on the same line.
WRAP is a key binding to which a \"wrapping\" action is bound.
The key should be in format that is accepted by `kbd'. This
option binds a lambda form:
`(lambda (&optional arg)
(interactive \"P\")
(sp-wrap-with-pair ,OPEN))
to the specified key sequence. The binding is added to global
keymap. When executed, it wraps ARG (default 1) expressions with
this pair (like `paredit-wrap-round' and friends). Additionally,
it accepts the same prefix arguments as `sp-select-next-thing'.
BIND is equivalent to WRAP. It is a legacy setting and will be
removed soon.
INSERT is a key binding to which an \"insert\" action is bound.
The key should be in format that is accepted by `kbd'. This is
achieved by binding a lambda form:
(lambda () (interactive) (sp-insert-pair \"pair-id\"))
to the supplied key, where pair-id is the open delimiter of the
pair. The binding is added to the global map. You can also bind
a similar lambda manually. To only bind this in specific major
modes, use this property on `sp-local-pair' instead."
(if (eq actions :rem)
(let ((remove (concat
(sp-get-pair-definition open t :open)
(sp-get-pair-definition open t :close)))
(global-list (assq t sp-pairs)))
(setcdr global-list (--remove (equal (plist-get it :open) open) (cdr global-list))))
(let ((pair nil))
(setq pair (plist-put pair :open open))
(when close (plist-put pair :close close))
(when trigger (plist-put pair :trigger trigger))
(dolist (arg '((:actions . actions)
(:when . when)
(:unless . unless)
(:pre-handlers . pre-handlers)
(:post-handlers . post-handlers)))
;; We only consider "nil" as a proper value if the property
;; already exists in the pair. In that case, we will set it to
;; nil. This allows for removing properties in global
;; definitions.
(when (or (eval (cdr arg))
(sp-get-pair-definition open t (car arg)))
(plist-put pair (car arg) (eval (cdr arg)))))
(sp--update-pair-list pair t))
(when (or wrap bind) (global-set-key (read-kbd-macro (or wrap bind))
`(lambda (&optional arg)
(interactive "P")
(sp-wrap-with-pair ,open))))
(when insert (global-set-key (kbd insert) `(lambda () (interactive) (sp-insert-pair ,open)))))
(cl-defun sp-local-pair (modes
(actions '(:add))
(when '(:add))
(unless '(:add))
(pre-handlers '(:add))
(post-handlers '(:add))
"Add a local pair definition or override a global definition.
MODES can be a single mode or a list of modes where these settings
should be applied.
PREFIX is a regular expression matching an optional prefix for
this pair in the specified major modes. If not specified, the
characters of expression prefix syntax class are automatically
considered instead. This can be used to attach custom prefixes
to pairs, such as prefix \"\\function\" in \\function{arg} in
SUFFIX is a regular expression matching an optional suffix for
this pair in the specified major modes. If not specified, the
characters of punctuation syntax class are automatically
considered instead.
The rest of the arguments have same semantics as in `sp-pair'.
If the pair is not defined globally, ACTIONS defaults to (wrap
insert) instead of (:add) (which inherits global settings)
The pairs are uniquely identified by the opening delimiter. If you
replace the closing one with a different string in the local
definition, this will override the global closing delimiter.
The list arguments can optionally be of form starting with
\":add\" or \":rem\" when these mean \"add to the global list\"
and \"remove from the global list\" respectivelly. Otherwise,
the global list is replaced. If you wish to both add and remove
things with single call, use \"((:add ...) (:rem ...))\" as an
argument. Therefore,
:when '(:add my-test)
would mean \"use the global settings for this pair, but also this
additional test\". If no value is provided for list arguments,
they default to \"(:add)\" which means they inherit the list from
the global definition.
To disable a pair in a major mode, simply set its actions set to
nil. This will ensure the pair is not even loaded when the mode is
If WRAP is non-nil, the binding is added into major mode keymap
called \"foo-mode-map\". If the mode does not follow this
convention, you will need to bind the function manually (see
`sp-pair' to how the function is named for each particular pair).
The bindings are not added into `smartparens-mode-map' to prevent
clashes between different modes.
BIND is equivalent to WRAP. It is a legacy setting and will be
removed soon.
The binding for INSERT follows the same convention as BIND. See
`sp-pair' for more info.
You can provide a function SKIP-MATCH, that will take three
arguments: the currently matched delimiter, beginning of match
and end of match. If this function returns true, the
`sp-get-paired-expression' matcher will ignore this match. You
can use this to skip over expressions that serve multiple
functions, such as if/end pair or unary if in Ruby or * in
markdown when it signifies list item instead of emphasis. In
addition, there is a global per major-mode option, see
(if (eq actions :rem)
(let ((remove ""))
(dolist (m (-flatten (list modes)))
(setq remove (concat remove
(sp-get-pair-definition open m :open)
(sp-get-pair-definition open m :close)))
(let ((mode-pairs (assq m sp-pairs)))
(setcdr mode-pairs
(--remove (equal (plist-get it :open) open)
(cdr mode-pairs))))))
(dolist (m (-flatten (list modes)))
(let* ((pair nil))
(setq pair (plist-put pair :open open))
(when close (plist-put pair :close close))
(when trigger (plist-put pair :trigger trigger))
(when prefix (plist-put pair :prefix prefix))
(when suffix (plist-put pair :suffix suffix))
(when skip-match (plist-put pair :skip-match skip-match))
(when (and (not (sp-get-pair-definition open t))
(equal actions '(:add)))
(setq actions '(wrap insert autoskip navigate)))
(plist-put pair :actions actions)
(plist-put pair :when when)
(plist-put pair :unless unless)
(plist-put pair :pre-handlers pre-handlers)
(plist-put pair :post-handlers post-handlers)
(sp--update-pair-list pair m)
(-when-let* ((symbol (intern (concat (symbol-name m) "-map")))
(map (and (boundp symbol) (symbol-value symbol))))
(when (or wrap bind) (define-key map
(read-kbd-macro (or wrap bind))
`(lambda (&optional arg)
(interactive "P")
(sp-wrap-with-pair ,open))))
(when insert (define-key map
(kbd insert)
`(lambda () (interactive) (sp-insert-pair ,open))))))))
(sp--update-local-pairs-everywhere (-flatten (list modes)))
(cl-defun sp-local-tag (modes trig open close &key
(transform 'identity)
(actions '(wrap insert))
"Add a tag definition.
MODES is a mode or a list of modes where this tag should
activate. It is impossible to define global tags.
TRIG is the trigger sequence. It can be a string of any length.
If more triggers share a common prefix, the shortest trigger is
OPEN is the format of the opening tag. This is inserted before
the active region.
CLOSE is the format of the closing tag. This is inserted after
the active region.
Opening and closing tags can optionally contain the _ character.
If the opening tag contains the _ character, after you type the
trigger, the region is wrapped with \"skeleton\" tags and a
special tag editing mode is entered. The text you now type is
substituted for the _ character in the opening tag.
If the closing tag contains the _ character, the text from the
opening pair is mirrored to the closing pair and substituted for
the _ character.
TRANSFORM is a function name (symbol) that is called to perform a
transformation of the opening tag text before this is inserted to
the closing tag. For example, in html tag it might simply select
the name of the tag and cut off the tag attributes (like
class/style etc.). Defaults to identity.
ACTIONS is a list of actions this tag should support. Currently,
only \"wrap\" action is supported. Usually, you don't need to
specify this argument.
POST-HANDLERS is a list of functions that are called after the
tag is inserted. If the tag does contain the _ character, these
functions are called after the tag editing mode is exited. Each
function on this list should accept two arguments: the trigger
string and the action."
(dolist (mode (-flatten (list modes)))
(let* ((tag-list (assq mode sp-tags))
(tag (--first (equal trig (plist-get it :trigger)) (cdr tag-list)))
(new-tag nil))
(setq new-tag (plist-put new-tag :trigger trig))
(plist-put new-tag :open open)
(plist-put new-tag :close close)
(when transform (plist-put new-tag :transform transform))
(when actions (plist-put new-tag :actions actions))
(when post-handlers (plist-put new-tag :post-handlers post-handlers))
(if tag-list
(if (not actions)
(setcdr tag-list (--remove (equal trig (plist-get it :trigger)) (cdr tag-list)))
(if (not tag)
(setcdr tag-list (cons new-tag (cdr tag-list)))
(sp--update-pair tag new-tag)))
;; mode doesn't exist
(when actions
(!cons (cons mode (list new-tag)) sp-tags))))))
;; Overlay management
;; burlywood4
(defface sp-pair-overlay-face
'((t (:inherit highlight)))
"The face used to highlight pair overlays."
:group 'smartparens)
(defface sp-wrap-overlay-face
'((t (:inherit sp-pair-overlay-face)))
"The face used to highlight wrap overlays."
:group 'smartparens)
(defface sp-wrap-overlay-opening-pair
'((t (:inherit sp-wrap-overlay-face
:foreground "green")))
"The face used to highlight wrap overlays."
:group 'smartparens)
(defface sp-wrap-overlay-closing-pair
'((t (:inherit sp-wrap-overlay-face
:foreground "red")))
"The face used to highlight wrap overlays."
:group 'smartparens)
(defface sp-wrap-tag-overlay-face
'((t (:inherit sp-pair-overlay-face)))
"The face used to highlight wrap tag overlays."
:group 'smartparens)
(defvar sp-pair-overlay-list '()
"List of overlays used for tracking inserted pairs.
When a pair is inserted, an overlay is created over it. When the
user starts typing the closing pair we will not insert it again.
If user leaves the overlay, it is canceled and the insertion
works again as usual.")
(make-variable-buffer-local 'sp-pair-overlay-list)
(defvar sp-wrap-overlays nil
"Cons pair of wrap overlays.")
(make-variable-buffer-local 'sp-wrap-overlays)
(defvar sp-wrap-tag-overlays nil
"Cons pair of tag wrap overlays.")
(make-variable-buffer-local 'sp-wrap-tag-overlays)
(defvar sp-pair-overlay-keymap (make-sparse-keymap)
"Keymap for the pair overlays.")
(define-key sp-pair-overlay-keymap (kbd "C-g") 'sp-remove-active-pair-overlay)
(defvar sp-wrap-overlay-keymap (make-sparse-keymap)
"Keymap for the wrap overlays.")
(define-key sp-wrap-overlay-keymap (kbd "C-g") 'sp-wrap-cancel)
(defun sp--overlays-at (&optional pos)
"Simple wrapper of `overlays-at' to get only overlays from
smartparens. Smartparens functions must use this function
instead of `overlays-at' directly."
(--filter (overlay-get it 'type) (overlays-at (or pos (point)))))
(defun sp--point-in-overlay-p (overlay)
"Return t if point is in OVERLAY."
(and (< (point) (overlay-end overlay))
(> (point) (overlay-start overlay))))
(defun sp--get-overlay-length (overlay)
"Compute the length of OVERLAY."
(- (overlay-end overlay) (overlay-start overlay)))
(defun sp--get-active-overlay (&optional type)
"Get active overlay. Active overlay is the shortest overlay at
point. Optional argument TYPE restrict overlays to only those
with given type."
(let ((overlays (sp--overlays-at)))
(when type
(setq overlays (--filter (eq (overlay-get it 'type) type) overlays)))
((not overlays) nil)
((not (cdr overlays)) (car overlays))
(--reduce (if (< (sp--get-overlay-length it) (sp--get-overlay-length acc)) it acc) overlays)))))
(defun sp--pair-overlay-create (start end id)
"Create an overlay over the currently inserted pair for
tracking the position of the point. START and END are the
boundaries of the overlay, ID is the id of the pair."
(let ((overlay (make-overlay start end)))
(overlay-put overlay 'priority 100)
(overlay-put overlay 'keymap sp-pair-overlay-keymap)
(overlay-put overlay 'pair-id id)
(overlay-put overlay 'type 'pair)
(!cons overlay sp-pair-overlay-list)
(add-hook 'post-command-hook 'sp--pair-overlay-post-command-handler nil t)))
(defun sp-wrap-cancel ()
"Cancel the active wrapping."
(-let (((obeg . oend) sp-wrap-overlays))
(when (and (not (called-interactively-p))
(kill-region (overlay-end obeg) (overlay-start oend)))
(delete-region (overlay-start oend) (overlay-end oend))
(when (> sp-wrap-point sp-wrap-mark)
(let ((beg (delete-and-extract-region (overlay-start obeg) (overlay-end obeg))))
(goto-char (overlay-start oend))
(insert beg))))
(defun sp-wrap--clean-overlays ()
"Delete wrap overlays."
(-let [(obeg . oend) sp-wrap-overlays]
(delete-overlay obeg)
(delete-overlay oend)
(setq sp-wrap-overlays nil)))
(defun sp--pair-overlay-fix-highlight ()
"Fix highlighting of the pair overlays. Only the active overlay
should be highlighted."
(--each (sp--overlays-at) (overlay-put it 'face nil))
(let* ((active (sp--get-active-overlay))
(type (and active (overlay-get active 'type))))
(if active
((eq 'wrap-tag type)
(when sp-highlight-wrap-tag-overlay
(overlay-put active 'face 'sp-wrap-tag-overlay-face)))
((eq 'pair type)
(when sp-highlight-pair-overlay
(overlay-put active 'face 'sp-pair-overlay-face))))
;; edge case where we're at the end of active overlay. If
;; there is a wrap-tag overlay, restore it's face
(when sp-wrap-tag-overlays
(overlay-put (car sp-wrap-tag-overlays) 'face 'sp-wrap-tag-overlay-face)))))
(defun sp--pair-overlay-post-command-handler ()
"Remove all pair overlays that doesn't have point inside them,
are of zero length, or if point moved backwards."
;; if the point moved backwards, remove all overlays
(if (and sp-cancel-autoskip-on-backward-movement
(< (point) sp-previous-point))
(dolist (o sp-pair-overlay-list) (sp--remove-overlay o))
;; else only remove the overlays where point is outside them or
;; their length is zero
(dolist (o (--remove (and (sp--point-in-overlay-p it)
(> (sp--get-overlay-length it) 0))
(sp--remove-overlay o)))
(when sp-pair-overlay-list
(setq sp-previous-point (point))))
(defun sp-remove-active-pair-overlay ()
"Deactivate the active overlay. See `sp--get-active-overlay'."
(-when-let (active-overlay (sp--get-active-overlay 'pair))
(sp--remove-overlay active-overlay)))
(defun sp--remove-overlay (overlay)
"Remove OVERLAY."
;; if it's not a pair overlay, nothing happens here anyway
(setq sp-pair-overlay-list (--remove (equal it overlay) sp-pair-overlay-list))
;; if we have zero pair overlays, remove the post-command hook
(when (not sp-pair-overlay-list)
(remove-hook 'post-command-hook 'sp--pair-overlay-post-command-handler t)
;; this is only updated when sp--pair-overlay-post-command-handler
;; is active. Therefore, we need to reset this to 1. If not, newly
;; created overlay could be removed right after creation - if
;; sp-previous-point was greater than actual point
(setq sp-previous-point -1))
(delete-overlay overlay)
(defun sp--replace-overlay-text (o string)
"Replace text inside overlay O with STRING."
(goto-char (overlay-start o))
(insert string)
(delete-region (point) (overlay-end o))))
(defun sp--get-overlay-text (o)
"Get text inside overlay O."
(buffer-substring (overlay-start o) (overlay-end o)))
;; Action predicates
(defun sp-in-string-p (id action context)
"Return t if point is inside string or comment, nil otherwise."
(eq context 'string))
(defun sp-in-docstring-p (id action context)
"Return t if point is inside elisp docstring, nil otherwise."
(and (eq context 'string)
(goto-char (car (sp-get-quoted-string-bounds)))
(ignore-errors (backward-sexp 3))
(looking-at-p (regexp-opt '("defun" "defmacro"
"cl-defun" "cl-defmacro"
"defun*" "defmacro*"
"lambda" "-lambda"))))))
(defun sp-in-code-p (id action context)
"Return t if point is inside code, nil otherwise."
(eq context 'code))
(defun sp-in-comment-p (id action context)
"Return t if point is inside comment, nil otherwise."
(eq context 'comment))
(defun sp-in-math-p (id action context)
"Return t if point is inside code, nil otherwise."
(when (functionp 'texmathp)
(defun sp-point-before-eol-p (id action context)
"Return t if point is followed by optional white spaces and end of line, nil otherwise.
This predicate is only tested on \"insert\" action."
(when (eq action 'insert)
(sp--looking-at-p "\\s-*$")))
(defun sp-point-after-bol-p (id action context)
"Return t if point follows beginning of line and possibly white spaces, nil otherwise.
This predicate is only tested on \"insert\" action."
(when (eq action 'insert)
(sp--looking-back-p (concat "^\\s-*" (regexp-quote id)))))
(defun sp-point-at-bol-p (id action context)
"Return t if point is at the beginning of line, nil otherwise.
This predicate is only tested on \"insert\" action."
(when (eq action 'insert)
(sp--looking-back-p (concat "^" (regexp-quote id)))))
(defun sp-point-before-symbol-p (id action context)
"Return t if point is followed by a symbol, nil otherwise.
This predicate is only tested on \"insert\" action."
(when (eq action 'insert)
(sp--looking-at-p "\\s_")))
(defun sp-point-before-word-p (id action context)
"Return t if point is followed by a word, nil otherwise.
This predicate is only tested on \"insert\" action."
(when (eq action 'insert)
(sp--looking-at-p "\\sw\\|\\s_")))
(defun sp-point-after-word-p (id action context)
"Return t if point is after a word, nil otherwise.
This predicate is only tested on \"insert\" action."
(when (eq action 'insert)
(sp--looking-back-p (concat "\\(\\sw\\|\\s_\\)" (regexp-quote id)))))
(defun sp-point-before-same-p (id action context)
"Return t if point is followed by ID, nil otherwise.
This predicate is only tested on \"insert\" action."
(when (eq action 'insert)
(sp--looking-at-p (regexp-quote id))))
(defun sp-point-in-empty-line-p (id action context)
"Return t if point is on an empty line, nil otherwise"
(and (sp--looking-at-p "\\s-*$")
(sp--looking-back-p (concat "^\\s-*" (regexp-quote id)))))
;; Pair insertion/deletion/skipping
(defun sp--do-action-p (id action &optional use-inside-string)
"Return t if action ACTION can be performed with pair ID.
If ACTION is a list, return t if at least one action from the
list can be performed.
If USE-INSIDE-STRING is non-nil, use value of
`sp-point-inside-string' instead of testing with
(setq action (-flatten (list action)))
(let* ((actions (sp-get-pair id :actions))
(when-l (sp-get-pair id :when))
(unless-l (sp-get-pair id :unless))
(in-string (if use-inside-string
(context (cond
(in-string 'string)
((sp-point-in-comment) 'comment)
(t 'code)))
a r)
(while (and action (not r))
(setq a (car action))
(setq r (when (memq a actions)
;;(and (when-clause) (not (unless-clause)))
(and (or (not when-l)
(run-hook-with-args-until-success 'when-l id a context))
(or (not unless-l)
(not (run-hook-with-args-until-success 'unless-l id a context))))))
(!cdr action))
(defun sp--get-handler-context (type)
"Return the context constant. TYPE is type of the handler."
(let ((in-string (cl-case type
(unless (bobp) (backward-char 1))
(if in-string 'string 'code)))
(defun sp--get-context (&optional point in-string in-comment)
"Return the context of POINT.
If the optional arguments IN-STRING or IN-COMMENT non-nil, their
value is used instead of a test."
(goto-char (or point (point)))
((or in-string (sp-point-in-string)) 'string)
((or in-comment (sp-point-in-comment)) 'comment)
(t 'code))))
(defun sp--parse-insertion-spec (fun)
"Parse the insertion specification FUN and return a form to evaluate."
(let ((spec nil)
(after nil)
(last 1))
(cl-labels ((push-non-empty
(unless (equal (cadr what) "")
;; relies on dynamic binding
(push what spec))))
(insert fun)
(goto-char (point-min))
(while (re-search-forward "\\(|\\|\\[\\)" nil t)
((equal (match-string 0) "[")
(if (save-excursion (backward-char 1) (eq (preceding-char) 92))
(push-non-empty `(insert ,(concat (buffer-substring-no-properties last (- (point) 2)) "[")))
(push-non-empty `(insert ,(buffer-substring-no-properties last (1- (point)))))
(let* ((p (point))
(fun-end (progn
(re-search-forward "]" nil t)
(1- (point))))
(fun-spec (buffer-substring-no-properties p fun-end))
(instruction (cond
((equal fun-spec "i")
((equal (aref fun-spec 0) ?d)
`(delete-char ,(string-to-number (substring fun-spec 1)))))))
(when instruction (push instruction spec)))))
((equal (match-string 0) "|")
((save-excursion (backward-char 1) (eq (preceding-char) 92))
(push-non-empty `(insert ,(concat (buffer-substring-no-properties last (- (point) 2)) "|"))))
(push-non-empty `(insert ,(buffer-substring-no-properties last (1- (point)))))
(push 'save-excursion spec)
(when (eq (following-char) 124)
(forward-char 1)
(setq after '(indent-according-to-mode)))))))
(setq last (point)))
(push-non-empty `(insert ,(buffer-substring-no-properties last (point-max)))))
(let* ((specr (nreverse spec))
(specsplit (--split-with (not (eq it 'save-excursion)) specr))
(re (-concat (car specsplit) (if (cadr specsplit) (cdr specsplit) nil))))
(cons 'progn (if after (-snoc re after) re))))))
(defun sp--run-function-or-insertion (fun id action context)
"Run a function or insertion.
If FUN is a function, call it with `funcall' with ID, ACTION and
CONTEXT as arguments.
If FUN is a string, interpret it as \"insertion specification\",
see `sp-pair' for description."
((functionp fun)
(funcall fun id action context))
((stringp fun)
(eval (sp--parse-insertion-spec fun)))))
(defun sp--run-hook-with-args (id type action)
"Run all the hooks for pair ID of type TYPE on action ACTION."
(let ((hook (sp-get-pair id type))
(context (sp--get-handler-context type)))
(if hook
(--each hook (sp--run-function-or-insertion it id action context))
(let ((tag-hook (plist-get
(--first (string-match-p
(replace-regexp-in-string "_" ".*?" (plist-get it :open))
(cdr (assq 'html-mode sp-tags))) ;; REALLY?
(run-hook-with-args 'tag-hook id action context))))))
;; TODO: add a test for a symbol property that would tell this handler
;; not to re=set `sp-last-operation'. Useful for example in "macro
;; funcions" like `my-wrap-with-paren'.
(defun sp--post-command-hook-handler ()
"Handle the situation after some command has executed."
(when (sp--special-self-insert-command-p)
(when smartparens-mode
;; handle the wrap overlays
(when sp-wrap-overlays
(let* ((overlay (car sp-wrap-overlays))
(start (overlay-start overlay))
(end (overlay-end overlay))
(p (point)))
(when (or (< p sp-previous-point)
(> p end)
(< p start))
(when sp-wrap-overlays
(setq sp-previous-point (point)))
;; Here we run the delayed hooks. See issue #80
((eq (car-safe (sp-state-delayed-hook sp-state)) :next)
(setf (car (sp-state-delayed-hook sp-state)) :this))
((eq (car-safe (sp-state-delayed-hook sp-state)) :this)
(let* ((pair (cdr (sp-state-delayed-hook sp-state)))
(hooks (sp-get-pair pair :post-handlers-cond)))
(--each hooks
(let ((fun (car it))
(conds (cdr it)))
(when (or (--any? (eq this-command it) conds)
(--any? (equal (single-key-description last-command-event) it) conds))
fun pair 'insert
(sp--get-handler-context :post-handlers)))))
(setf (sp-state-delayed-hook sp-state) nil)
(setq sp-last-inserted-pair nil))))
;; Here we run the delayed insertion. Some details in issue #113
(when (and (not (eq sp-last-operation 'sp-insert-pair-delayed))
(let* ((pair (car sp-delayed-pair))
(beg (cdr sp-delayed-pair))
(conds (sp-get-pair pair :when-cond))
(open-pair pair)
(close-pair (sp-get-pair pair :close)))
(when (and conds
(--any? (cond
((and (commandp it)
(not (stringp it)))
(eq this-command it))
((stringp it)
(equal (single-key-description last-command-event) it))
((ignore-errors (funcall it pair 'insert (sp--get-handler-context :post-handlers))))) conds))
;; TODO: refactor this and the same code in
;; `sp-insert-pair' to a separate function
(sp--run-hook-with-args open-pair :pre-handlers 'insert)
(insert close-pair)
(backward-char (length close-pair))
(sp--pair-overlay-create beg
(+ (point) (length close-pair))
;; no auto-escape here? Should be fairly safe
(sp--run-hook-with-args open-pair :post-handlers 'insert)
(setq sp-last-inserted-pair open-pair)
;; TODO: this is probably useless
(setq sp-last-operation 'sp-insert-pair)))
(setq sp-delayed-pair nil))
(when (eq sp-last-operation 'sp-insert-pair-delayed)
(setq sp-last-operation nil))
(unless (or (sp--self-insert-command-p)
;; unless the last command was a self-insert, remove the
;; information about the last wrapped region. It is only used
;; for: 1. deleting the wrapping immediately after the wrap,
;; 2. re-wrapping region immediatelly after a sucessful wrap.
;; Therefore, the deletion should have no ill-effect. If the
;; necessity will arise, we can add a different flag.
(setq sp-last-wrapped-region nil)
(setq sp-last-operation nil))
(when show-smartparens-mode
(if (member this-command sp-show-enclosing-pair-commands)
(when (not (eq this-command 'sp-highlight-current-sexp))
(defmacro sp--setaction (action &rest forms)
(declare (debug (form body)))
`(unless action
(setq action (progn ,@forms))))
;; TODO: this introduces a regression, where doing C-4 [ inserts [[[[]
;; figure out how to detect the argument to self-insert-command that
;; resulted to this insertion
(defun sp--post-self-insert-hook-handler ()
(with-demoted-errors "sp--post-self-insert-hook-handler: %S"
(when smartparens-mode
(let (op action)
(setq op sp-last-operation)
(when (region-active-p)
;; TODO: this does not pick correct pair!! it uses insert and not wrapping code
(sp--setaction action (-when-let ((_ . open-pairs) (sp--all-pairs-to-insert))
(catch 'done
(-each open-pairs
(-lambda ((&keys :open open :close close))
(--when-let (sp--wrap-repeat-last (cons open close))
(throw 'done it)))))))
(sp--setaction action (sp-insert-pair))
(sp--setaction action (sp-skip-closing-pair))
;; if nothing happened, we just inserted a character, so
;; set the apropriate operation. We also need to check
;; for `sp--self-insert-no-escape' not to overwrite
;; it. See `sp-autoinsert-quote-if-followed-by-closing-pair'.
(when (and (not action)
(not (eq sp-last-operation 'sp-self-insert-no-escape)))
(setq sp-last-operation 'sp-self-insert))))))))
;; Unfortunately, some modes rebind "inserting" keys to their own
;; handlers but do not hand over the insertion back to
;; `self-insert-command', rather, they insert via `insert'.
;; Therefore, we need to call this handler in `post-command-hook' too.
;; The list `sp--special-self-insert-commands' specifies which
;; commands to handle specially.
(add-hook 'post-self-insert-hook 'sp--post-self-insert-hook-handler)
;; TODO: make a proper data structure for state tracking and describe
;; why we need each of these.
(defun sp--save-pre-command-state ()
(setq sp-point-inside-string (sp-point-in-string))
(setq sp-pre-command-point (point))
(setq sp-buffer-modified-p (buffer-modified-p)))
(add-hook 'pre-command-hook 'sp--save-pre-command-state)
(defun sp--pre-command-hook-handler ()
"Main handler of pre-command-hook.
Handle the `delete-selection-mode' or `cua-delete-selection'
stuff here.")
(defun sp--get-pair-list ()
"Return all pairs that are recognized in this
`major-mode' and do not have same opening and closing delimiter.
This is used for navigation functions."
(--filter (not (string= (car it) (cdr it))) sp-pair-list))
(defun sp--get-stringlike-list ()
"Return all pairs that are recognized in this `major-mode' that
have same opening and closing delimiter."
(--filter (string= (car it) (cdr it)) sp-pair-list))
(defun sp--get-allowed-pair-list ()
"Return all pairs that are recognized in this
`major-mode', do not have same opening and closing delimiter and
are allowed in the current context. See also
(--filter (and (sp--do-action-p (car it) 'navigate)
(not (equal (car it) (cdr it)))) sp-pair-list))
(defun sp--get-allowed-stringlike-list ()
"Return all pairs that are recognized in this `major-mode',
have the same opening and closing delimiter and are allowed in
the current context."
(--filter (and (sp--do-action-p (car it) 'navigate)
(equal (car it) (cdr it))) sp-pair-list))
(defun sp--get-pair-list-context (&optional action)
"Return all pairs that are recognized in this `major-mode' and
are allowed in the current context."
(setq action (or action 'insert))
(--filter (sp--do-action-p (car it) action) sp-pair-list))
(defun sp--get-pair-list-wrap ()
"Return the list of all pairs that can be used for wrapping."
(--filter (sp--do-action-p (car it) 'wrap) sp-pair-list))
(defun sp--wrap-regexp (string start end)
"Wraps regexp with start and end boundary conditions to avoid
matching symbols in symbols."
(concat "\\(?:" (when start "\\<") string (when end "\\>") "\\)"))
(defun sp--regexp-for-group (parens &rest strings)
"Generates an optimized regexp matching all string, but with
extra boundary conditions depending on parens."
(let* ((start (car parens))
(end (cadr parens)))
(sp--wrap-regexp (regexp-opt strings) start end)))
(defun sp--strict-regexp-opt (strings &optional ignored)
"Like regexp-opt, but with extra boundary conditions to ensure
that the strings are not matched in-symbol."
;; HACK: this is a terrible hack to make ' be treated as a
;; punctuation. Many text modes set it as word character which
;; messes up the regexps
(let ((table (make-syntax-table (syntax-table))))
(modify-syntax-entry ?' "." table)
(--> strings
(-group-by (lambda (string)
(list (and (string-match-p "\\`\\<" string) t)
(and (string-match-p "\\>\\'" string) t)))
(mapconcat (lambda (g) (apply 'sp--regexp-for-group g)) it "\\|")
(concat "\\(?:" it "\\)"))))
(defun sp--strict-regexp-quote (string)
"Like regexp-quote, but make sure that the string is not
matched in-symbol."
(sp--wrap-regexp (regexp-quote string)
(string-match-p "\\`\\<" string)
(string-match-p "\\>\\'" string)))
(cl-defun sp--get-opening-regexp (&optional (pair-list (sp--get-pair-list)))
"Return regexp matching any opening pair."
(sp--strict-regexp-opt (--map (car it) pair-list)))
(cl-defun sp--get-closing-regexp (&optional (pair-list (sp--get-pair-list)))
"Return regexp matching any closing pair."
(sp--strict-regexp-opt (--map (cdr it) pair-list)))
(cl-defun sp--get-allowed-regexp (&optional (pair-list (sp--get-allowed-pair-list)))
"Return regexp matching any opening or closing
delimiter for any pair allowed in current context."
(sp--strict-regexp-opt (--mapcat (list (car it) (cdr it)) pair-list)))
(cl-defun sp--get-stringlike-regexp (&optional (pair-list (sp--get-allowed-stringlike-list)))
(regexp-opt (--map (car it) pair-list)))
(defun sp-pair-is-stringlike-p (delim)
(--first (equal delim (car it)) (sp--get-allowed-stringlike-list)))
(defun sp--get-last-wraped-region (beg end open close)
"Return `sp-get-sexp' style plist about the last wrapped region.
Note: this function does not retrieve the actual value of
`sp-last-wrapped-region', it merely construct the plist from the
provided values."
(let ((b (make-marker))
(e (make-marker)))
(set-marker b beg)
(set-marker e end)
(set-marker-insertion-type e t)
`(:beg ,b :end ,e :op ,open :cl ,close :prefix "")))
;; Wrapping is basically the same thing as insertion, only the closing
;; pair is placed at a distance.
;; However, we want to be able to insert the *closing* delimiter and
;; go to the end of block. This will only work with delimiters which
;; are unique wrt their opening one. For more complex wrapping, there
;; will probably be an IDO/minibuffer interface. Openings are checked
;; first.
;; Inserting the opening delimiter should put the point wherever it
;; was when we started insertion.
(defun sp-wrap--can-wrap-p ()
"Return non-nil if we can wrap a region.
This is used in advices on various pre-command-hooks from
\"selection deleting\" modes to intercept their actions."
(--any? (or (string-prefix-p (sp--single-key-description last-command-event) (car it))
(string-prefix-p (sp--single-key-description last-command-event) (cdr it)))
(defun sp--pair-to-wrap-comparator (prop a b)
"Comparator for wrapping pair selection.
PROP specifies wrapping-end. A and B are pairs to be compared."
(let ((rprop (if (eq prop :open) :close :open)))
(if (< (length (plist-get it prop)) (length (plist-get other prop)))
(string-prefix-p (plist-get it rprop) (plist-get other rprop))
(not (string-prefix-p (plist-get other rprop) (plist-get it rprop))))))
(defun sp--pair-to-wrap (&optional prefix)
"Return information about possible wrapping pairs.
If optional PREFIX is non-nil, this is used to determine the
possible wrapping pairs instead of the text in the wrapping
(let* ((working-pairs
;; TODO: abstract this into a new "sp--get-..." hierarchy
(--filter (sp--do-action-p (plist-get it :open) 'wrap) sp-local-pairs))
(obeg (car sp-wrap-overlays))
(prefix (or prefix (sp--get-overlay-text obeg)))
(opening-pairs (--filter (string-prefix-p prefix (plist-get it :open)) working-pairs))
(closing-pairs (--filter (string-prefix-p prefix (plist-get it :close)) working-pairs))
(open (car (--sort (sp--pair-to-wrap-comparator :open it other) opening-pairs)))
;; TODO: do we need the special sorting here?
(close (car (--sort (sp--pair-to-wrap-comparator :close it other) closing-pairs))))
(list :open open
:close close
:opening opening-pairs
:closing closing-pairs)))
(defun sp-wrap--initialize ()
"Initialize wrapping."
(when (and sp-autowrap-region
;; This is the length of string which was inserted by the last
;; "self-insert" action. Typically this is 1, but sometimes a
;; single key inserts two or more characters, such as " in latex
;; where it translates into `` or ''.
(let ((inserted-string-length (- (point) sp-pre-command-point)))
;; TODO: get rid of the following variables
(setq sp-wrap-point (- (point) inserted-string-length))
(setq sp-wrap-mark (mark))
;; if point > mark, we need to move point to mark and reinsert the
;; just inserted character.
(when (> (point) (mark))
(let ((char (delete-and-extract-region (- (point) inserted-string-length) (point))))
(insert char)))
(let* ((oleft (make-overlay (- (region-beginning) inserted-string-length)
(region-beginning) nil nil t))
(oright (make-overlay (region-end) (region-end) nil nil t)))
(setq sp-wrap-overlays (cons oleft oright))
(when sp-highlight-wrap-overlay
(overlay-put oleft 'face 'sp-wrap-overlay-face)
(overlay-put oright 'face 'sp-wrap-overlay-face))
(overlay-put oleft 'priority 100)
(overlay-put oright 'priority 100)
(overlay-put oleft 'keymap sp-wrap-overlay-keymap)
(overlay-put oleft 'type 'wrap)
(setq sp-previous-point (point))
(goto-char (1+ (overlay-start oleft)))))))
(defun sp-wrap--finalize (wrapping-end open close)
"Finalize a successful wrapping.
WRAPPING-END specifies the wrapping end. If we wrapped using
opening delimiter it is :open. If we wrapped using closing
delimiter it is :close. Position of point after wrapping depends
on this value---if :open, go where the wrapping was initalized,
if :close, go after the newly-formed sexp.
OPEN and CLOSE are the delimiters."
(-let (((obeg . oend) sp-wrap-overlays))
(sp--replace-overlay-text obeg open)
(sp--replace-overlay-text oend close)
(setq sp-last-operation 'sp-wrap-region)
(setq sp-last-wrapped-region
(overlay-start obeg) (overlay-end oend)
open close))
((eq wrapping-end :open)
(if sp-wrap-respect-direction
(goto-char (overlay-start obeg))
(when (> sp-wrap-point sp-wrap-mark)
(goto-char (overlay-end oend)))))
((eq wrapping-end :close)
(goto-char (overlay-end oend))))
(sp--run-hook-with-args open :post-handlers 'wrap)))
(defun sp-wrap ()
"Try to wrap the active region with some pair.
This function is not ment to be used to wrap sexps with pairs
programatically. Use `sp-wrap-with-pair' instead."
(-let* (((&plist :open open :close close
:opening opening-pairs
:closing closing-pairs) (sp--pair-to-wrap))
((obeg . oend) sp-wrap-overlays))
(-let (((&plist :open open :close close) open))
(when sp-wrap-show-possible-pairs
oend 'after-string
(mapconcat (lambda (x)
(if sp-highlight-wrap-overlay
(plist-get x :open) 'face
(plist-get x :close)
'face 'sp-wrap-overlay-closing-pair))
(concat (plist-get x :open) (plist-get x :close))))
opening-pairs " ")))
(when (equal (sp--get-overlay-text obeg) open)
(sp-wrap--finalize :open open close))))
((and close (= 1 (length closing-pairs)))
(-let (((&plist :open open :close close) close))
(when (equal (sp--get-overlay-text obeg) close)
(sp-wrap--finalize :close open close))))
;; kept to not break people's config... remove later
(defun sp-match-sgml-tags (tag)
"Split the html tag TAG at the first space and return its name."
(let* ((split (split-string tag " "))
(close (car split)))
(make-obsolete 'sp-match-sgml-tags "do not use this function as the tag system has been removed." "2015-02-07")
(defun sp--is-number-cons (c)
(and (consp c) (numberp (car c)) (numberp (cdr c))))
;; TODO: more research is needed
(defun sp--undo-pop-to-last-insertion-node ()
"Pop all undo info until an insertion node (beg . end) is found.
This can potentially remove some undo important information."
(while (and buffer-undo-list
(or (null (car buffer-undo-list)) ;; is nil
;; is not undo action we're interested in
(not (sp--is-number-cons (car buffer-undo-list)))))
(pop buffer-undo-list)))
;; modified from:
(defun sp--split-last-insertion-undo (len)
"Split the last insertion node in the `buffer-undo-list' to
include separate pair node."
(when buffer-undo-list
(let* ((previous-undo-actions (cdr buffer-undo-list))
(beg (caar buffer-undo-list))
(end (cdar buffer-undo-list))
first-action second-action)
(unless (< beg (- end len))
;; We need to go back more than one action. Given the pairs
;; are limited to 10 chars now and the chunks seem to be 20
;; chars, we probably wouldn't need more.
(pop buffer-undo-list)
(when buffer-undo-list
(setq beg (caar buffer-undo-list))
(setq previous-undo-actions (cdr buffer-undo-list))))
(setq first-action (cons beg (- end len)))
(setq second-action (cons (- end len) end))
(setq buffer-undo-list
(append (list nil second-action nil first-action)
(defun sp--string-empty-p (delimeter)
"Return t if point is inside an empty string."
(and (equal (char-after (1+ (point))) delimeter)
(equal (char-after (- (point) 2)) delimeter)))
(defun sp--all-pairs-to-insert (&optional looking-fn)
"Return all pairs that can be inserted at point.
Return nil if such pair does not exist.
Pairs inserted using a trigger have higher priority over pairs
without a trigger and only one or the other list is returned.
In other words, if any pair can be inserted using a trigger, only
pairs insertable by trigger are returned."
(setq looking-fn (or looking-fn 'sp--looking-back-p))
(let ((working-pairs
;; TODO: abstract this into a new "sp--get-..." hierarchy
(--filter (sp--do-action-p (plist-get it :open) 'insert) sp-local-pairs)))
(-if-let (trigs (--filter (and (plist-get it :trigger)
(funcall looking-fn (sp--strict-regexp-quote (plist-get it :trigger))))
(cons :trigger trigs)
(-when-let (pairs (--filter (funcall looking-fn (sp--strict-regexp-quote (plist-get it :open))) working-pairs))
(cons :open pairs)))))
(defun sp--pair-to-insert-comparator (prop a b)
;; in case of triggers shorter always wins
((eq prop :trigger)
(< (length (plist-get a :trigger)) (length (plist-get b :trigger))))
;; Shorter wins only if the shorter's closing is a prefix of the
;; longer's closing. In other words, if we are looking at
;; shorter's closing and we are trying to nest it.
(if (< (length (plist-get a :open)) (length (plist-get b :open)))
(and (string-prefix-p (plist-get a :close) (plist-get b :close))
(sp--looking-at-p (plist-get a :close)))
(not (and (string-prefix-p (plist-get b :close) (plist-get a :close))
(sp--looking-at-p (plist-get b :close))))))))
(defun sp--pair-to-insert ()
"Return pair that can be inserted at point.
Return nil if such pair does not exist.
If more triggers or opening pairs are possible select the
shortest one."
(-when-let ((property . pairs) (sp--all-pairs-to-insert))
(car (--sort (sp--pair-to-insert-comparator property it other) pairs))))
(defun sp--longest-prefix-to-insert ()
"Return pair with the longest :open which can be inserted at point."
(-when-let (pairs (--filter (sp--looking-back-p (sp--strict-regexp-quote (plist-get it :open))) sp-local-pairs))
(car (--sort (> (length (plist-get it :open)) (length (plist-get other :open))) pairs))))
(defun sp--pair-to-uninsert ()
"Return pair to uninsert.
If the current to-be-inserted pair shares a prefix with
another (shorter) pair, we must first remove the effect of
inserting its closing pair before inserting the current one.
The previously inserted pair must be the one with the longest
common prefix excluding the current pair."
(-when-let (lp (sp--longest-prefix-to-insert))
(backward-char (length (plist-get lp :open)))
(-when-let ((property . pairs) (sp--all-pairs-to-insert 'sp--looking-at-p))
(car (--sort (> (length (plist-get it property)) (length (plist-get other property)))
;; remove pairs whose open is longer than the
;; current longest possible prefix---otherwise
;; they would overflow to the closing pair
;; TODO: this ignores the possibility when lp is
;; inserted by trigger. We assume triggers are
;; shorter than the openings and this situation,
;; if ever, should be very rare
(--remove (>= (length (plist-get it :open))
(length (plist-get lp :open))) pairs)))))))
(defun sp--insert-pair-get-pair-info (active-pair)
"Get basic info about the to-be-inserted pair."
(let ((open-pair (plist-get active-pair :open)))
(plist-get active-pair :close)
(-if-let (tr (plist-get active-pair :trigger))
(if (sp--looking-back-p (sp--strict-regexp-quote tr)) tr open-pair)
(defun sp-insert-pair (&optional pair)
"Automatically insert the closing pair if it is allowed in current context.
If PAIR is provided, use this as pair ID instead of looking
through the recent history of pressed keys.
You can disable this feature completely for all modes and all pairs by
setting `sp-autoinsert-pair' to nil.
You can globally disable insertion of closing pair if point is
followed by the matching opening pair. It is disabled by
(-let* ((active-pair (unwind-protect
;; This fake insertion manufactures proper
;; context for the tests below... in effect
;; we must make it look as if the user
;; typed in the opening part themselves
;; TODO: it is duplicated in the test
;; below, maybe it wouldn't hurt to
;; restructure this function a bit
(when pair (insert pair))
(when pair (delete-char (- (length pair))))))
((open-pair close-pair trig) (sp--insert-pair-get-pair-info active-pair)))
(if (not (unwind-protect
(when pair (insert pair))
;; TODO: all these tests must go into `sp--pair-to-insert'
(and sp-autoinsert-pair
(if (eq sp-autoskip-closing-pair 'always)
(or (not (equal open-pair close-pair))
(not (sp-skip-closing-pair nil t)))
(sp--do-action-p open-pair 'insert t)
(if sp-autoinsert-quote-if-followed-by-closing-pair t
(if (and (eq (char-syntax (preceding-char)) ?\")
;; this is called *after* the character is
;; inserted. Therefore, if we are not in string, it
;; must have been closed just now
(not (sp-point-in-string)))
(let ((pattern (sp--get-closing-regexp)))
;; If we simply insert closing ", we also
;; don't want to escape it. Therefore, we
;; need to set `sp-last-operation'
;; accordingly to be checked in
;; `self-insert-command' advice.
(if (sp--looking-at pattern)
(progn (setq sp-last-operation 'sp-self-insert-no-escape) nil)
;; was sp-autoinsert-if-followed-by-same
(or (not (sp--get-active-overlay 'pair))
(not (sp--looking-at (sp--strict-regexp-quote open-pair)))
(and (equal open-pair close-pair)
(eq sp-last-operation 'sp-insert-pair)
(backward-char (length trig))
(sp--looking-back (sp--strict-regexp-quote open-pair))))
(not (equal open-pair close-pair)))))
(when pair (delete-char (- (length pair))))))
;; if this pair could not be inserted, we try the procedure
;; again with this pair removed from sp-pair-list to give
;; chance to other pairs sharing a common suffix (for
;; example \[ and [)
(let ((new-sp-pair-list (--remove (equal (car it) open-pair) sp-pair-list))
(new-sp-local-pairs (--remove (equal (plist-get it :open) open-pair) sp-local-pairs)))
(when (> (length sp-pair-list) (length new-sp-pair-list))
(let ((sp-pair-list new-sp-pair-list)
(sp-local-pairs new-sp-local-pairs))
;; setup the delayed insertion here.
(if (sp-get-pair open-pair :when-cond)
(setq sp-delayed-pair (cons open-pair (- (point) (length open-pair))))
(setq sp-last-operation 'sp-insert-pair-delayed))
(unless pair (delete-char (- (length trig))))
(insert open-pair)
(sp--run-hook-with-args open-pair :pre-handlers 'insert)
(--when-let (sp--pair-to-uninsert)
(let ((cl (plist-get it :close)))
(when (and (sp--looking-at-p (sp--strict-regexp-quote cl))
(not (string-prefix-p cl close-pair)))
(delete-char (length cl)))))
(insert close-pair)
(backward-char (length close-pair))
(sp--pair-overlay-create (- (point) (length open-pair))
(+ (point) (length close-pair))
(when sp-undo-pairs-separately
(sp--split-last-insertion-undo (+ (length open-pair) (length close-pair)))
;; TODO: abc\{abc\} undo undo \{asd\} . next undo removes the
;; entire \{asd\} if we do not insert two nils here.
;; Normally, repeated nils are ignored so it shouldn't
;; matter. It would still be useful to inspect further.
(push nil buffer-undo-list)
(push nil buffer-undo-list))
(sp--run-hook-with-args open-pair :post-handlers 'insert)
(setq sp-last-inserted-pair open-pair)
(setf (sp-state-delayed-hook sp-state) (cons :next open-pair))
(setq sp-last-operation 'sp-insert-pair)))))
(defun sp--wrap-repeat-last (active-pair)
"If the last operation was a wrap and `sp-wrap-repeat-last' is
non-nil, repeat the wrapping with this pair around the last
active region."
(unless (= 0 sp-wrap-repeat-last)
(when sp-last-wrapped-region
(let* ((b (sp-get sp-last-wrapped-region :beg))
(e (sp-get sp-last-wrapped-region :end))
(op (sp-get sp-last-wrapped-region :op))
(oplen (length op))
(cllen (sp-get sp-last-wrapped-region :cl-l))
(acolen (length (car active-pair))))
(when (and
((= 1 sp-wrap-repeat-last)
(equal (car active-pair) op))
((= 2 sp-wrap-repeat-last)))
(memq sp-last-operation '(sp-self-insert sp-wrap-region))
(or (= (point) (+ b oplen acolen))
(= (point) e)))
(delete-char (- acolen))
(if (< (point) e)
(progn (goto-char (+ b oplen))
(insert (car active-pair))
(goto-char (- e cllen))
(insert (cdr active-pair))
(setq sp-last-wrapped-region
(+ b oplen) (point)
(car active-pair) (cdr active-pair)))
(goto-char (+ b oplen acolen)))
(goto-char b)
(insert (car active-pair))
(goto-char e)
(insert (cdr active-pair))
(setq sp-last-wrapped-region
b e (car active-pair) (cdr active-pair))))
(setq sp-last-operation 'sp-wrap-region)
(sp--run-hook-with-args (car active-pair) :post-handlers 'wrap)
(defun sp--char-is-part-of-stringlike (char)
"Return non-nil if CHAR is part of a string-like delimiter of length 1."
(->> (sp--get-stringlike-list)
(--filter (= 1 (length (cdr it))))
(-map 'car)
(--any? (string-match-p (regexp-quote char) it))))
(defun sp--char-is-part-of-closing (char)
"Return non-nil if CHAR is part of a pair delimiter of length 1."
(->> (sp--get-pair-list)
(--filter (= 1 (length (cdr it))))
(-map 'cdr)
(--any? (string-match-p (regexp-quote char) it))))
;; TODO: this only supports single-char delimiters. Maybe it should
;; that that way.
(defun sp-skip-closing-pair (&optional last test-only)
"Automatically skip the closing delimiters of pairs.
If point is inside an inserted pair, and the user only moved
forward with point (that is, only inserted text), if the closing
pair is typed, we shouldn't insert it again but skip forward. We
call this state \"active sexp\". The setting
`sp-cancel-autoskip-on-backward-movement' controls when an active
expression become inactive.
For example, pressing ( is followed by inserting the pair (|). If
we then type 'word' and follow by ), the result should be (word)|
instead of (word)|).
This behaviour can be customized by various settings of
`sp-autoskip-closing-pair' and `sp-autoskip-opening-pair'.
Additionally, this behaviour can be selectively disabled for
specific pairs by removing their \"autoskip\" action. You can
achieve this by using `sp-pair' or `sp-local-pair' with
\":actions '(:rem autoskip)\"."
(when (or (and (eq sp-autoskip-closing-pair t)
(sp--get-active-overlay 'pair))
(memq sp-autoskip-closing-pair '(always always-end)))
;; these two are pretty hackish ~_~
(cl-labels ((get-sexp
(delete-char -1)
(insert " ")
(prog1 (sp-get-sexp)
(delete-char -1)
(insert last)))
(delete-char -1)
(insert " ")
(prog1 (sp-get-enclosing-sexp)
(delete-char -1)
(insert last))))
(let ((last (or last (sp--single-key-description last-command-event))))
(-when-let (active-sexp
((-when-let* ((ov (sp--get-active-overlay 'pair))
(op (overlay-get ov 'pair-id))
(cl (cdr (assoc op sp-pair-list))))
;; if the sexp is active, we are inside it.
(when (and (= 1 (length op))
(equal last cl))
(list :beg (overlay-start ov)
:end (overlay-end ov)
:op op
:cl cl
:prefix ""
:suffix ""))))
((sp--char-is-part-of-stringlike last)
;; a part of closing delimiter is typed. There are four
;; options now:
;; - we are inside the sexp, at its end
;; - we are inside the sexp, somewhere in the middle
;; - we are outside, in front of a sexp
;; - we are outside, somewhere between sexps
((and (sp--looking-at (sp--get-stringlike-regexp))
(not (sp--skip-match-p (match-string-no-properties 0)
(match-beginning 0)
(match-end 0))))
;; if we're looking at the delimiter, and it is valid in
;; current context, get the sexp.
;; here comes the feature when we're somewhere in the
;; middle of the sexp (or outside), if ever supported.
((sp--char-is-part-of-closing last)
((and (sp--looking-at (sp--get-closing-regexp))
(not (sp--skip-match-p (match-string-no-properties 0)
(match-beginning 0)
(match-end 0))))
((eq sp-autoskip-closing-pair 'always)
(when (and active-sexp
(equal (sp-get active-sexp :cl) last)
(sp--do-action-p (sp-get active-sexp :op) 'autoskip))
(-when-let (re (cond
((= (point) (sp-get active-sexp :beg))
;; we are in front of a string-like sexp
(when sp-autoskip-opening-pair
(if test-only t
(delete-char -1)
(setq sp-last-operation 'sp-skip-closing-pair))))
((= (point) (sp-get active-sexp :end-in))
(if test-only t
(delete-char 1)
(setq sp-last-operation 'sp-skip-closing-pair)))
((sp-get active-sexp
(and (> (point) :beg-in)
(< (point) :end-in)))
(if test-only t
(delete-char -1)
(unless (or test-only
(set-buffer-modified-p nil))
(unless test-only
(sp--run-hook-with-args (sp-get active-sexp :op) :post-handlers 'skip-closing-pair))
(defun sp-delete-pair (&optional arg)
"Automatically delete opening or closing pair, or both, depending on
position of point.
If the point is inside an empty pair, automatically delete both. That
is, [(|) turns to [|, [\{|\} turns to [|. Can be disabled by setting
`sp-autodelete-pair' to nil.
If the point is behind a closing pair or behind an opening pair delete
it as a whole. That is, \{\}| turns to \{|, \{| turns to |. Can be
disabled by setting `sp-autodelete-closing-pair' and
`sp-autodelete-opening-pair' to nil.
If the last operation was a wrap and `sp-autodelete-wrap' is
enabled, invoking this function will unwrap the expression, that
is remove the just added wrapping."
;; NOTE: Only use delete-char inside this function, so we
;; don't activate the advice recursively!
;; only activate if argument is 1 (this is 0-th argument of the
;; delete-backward-char), otherwise the user wants to delete
;; multiple character, so let him do that
(when (and (= arg 1)
(if (and sp-autodelete-wrap
(eq sp-last-operation 'sp-wrap-region))
(let ((p (point))
(b (sp-get sp-last-wrapped-region :beg))
(e (sp-get sp-last-wrapped-region :end))
(o (sp-get sp-last-wrapped-region :op-l))
(c (sp-get sp-last-wrapped-region :cl-l)))
;; if the last operation was `sp-wrap-region', and we are at
;; the position of either opening or closing pair, delete the
;; just-inserted pair
(when (or (= p (+ b o))
(= p e))
(insert "x") ;dummy char to account for the regularly deleted one
(goto-char e)
(delete-char (- c))
(goto-char b)
(delete-char o))
(setq sp-last-operation 'sp-delete-pair-wrap)))
(let ((p (point))
(inside-pair (--first (and (sp--looking-back (sp--strict-regexp-quote (car it)))
(sp--looking-at (concat "[ \n\t]*" (sp--strict-regexp-quote (cdr it)))))
(behind-pair (--first (sp--looking-back (sp--strict-regexp-quote (cdr it))) sp-pair-list))
(opening-pair (--first (sp--looking-back (sp--strict-regexp-quote (car it))) sp-pair-list)))
;; we're just before the closing quote of a string. If there
;; is an opening or closing pair behind the point, remove
;; it. This is only really relevant if the pair ends in the
;; same character as string quote. We almost never want to
;; delete it as an autopair (it would "open up the string").
;; So, word\"|" and <backspace> should produce word\|" or
;; word|" (if \" is autopair) instead of word\|.
((and (sp-point-in-string)
(not (sp-point-in-string (1+ p)))
(sp-point-in-string (1- p))) ;; the string isn't empty
(cond ;; oh, you ugly duplication :/
((and behind-pair sp-autodelete-closing-pair)
(delete-char (- (1- (length (car behind-pair)))))
(setq sp-last-operation 'sp-delete-pair-closing))
((and opening-pair sp-autodelete-opening-pair)
(delete-char (- (1- (length (car opening-pair)))))
(setq sp-last-operation 'sp-delete-pair-opening))))
;; we're inside a pair
((and inside-pair sp-autodelete-pair)
(let* ((end (save-excursion
(search-forward (cdr inside-pair))))
(cs (sp--get-context p))
(ce (sp--get-context end)))
(when (and (or (not (eq cs 'comment)) ;; a => b <=> ~a v b
(eq ce 'comment))
(eq end (sp-get (sp-get-sexp) :end)))
(delete-char (- end p))
(delete-char (- (1- (length (car inside-pair)))))
(setq sp-last-operation 'sp-delete-pair))))
;; we're behind a closing pair
((and behind-pair sp-autodelete-closing-pair)
(delete-char (- (1- (length (cdr behind-pair)))))
(setq sp-last-operation 'sp-delete-pair-closing))
;; we're behind an opening pair and there's no closing pair
((and opening-pair sp-autodelete-opening-pair)
(delete-char (- (1- (length (car opening-pair)))))
(setq sp-last-operation 'sp-delete-pair-opening)))))))
;; Navigation
(defun sp--looking-at (regexp)
"Like `looking-at', but always case sensitive."
(let ((case-fold-search nil))
(looking-at regexp)))
(defun sp--looking-at-p (regexp)
"Like `looking-at-p', but always case sensitive."
(let ((case-fold-search nil))
(looking-at-p regexp)))
(defun sp--looking-back (regexp &optional limit not-greedy)
"Return non-nil if text before point matches regular expression REGEXP.
With optional argument LIMIT search only that many characters
backward. If LIMIT is nil, default to `sp-max-pair-length'.
If optional argument NON-GREEDY is t search for any matching
sequence, not necessarily the longest possible."
(setq limit (or limit sp-max-pair-length))
(let ((case-fold-search nil)
(from (max 1 (- (point) limit)))
(to (point))
(greedy (not not-greedy))
(set-match-data '(0 0))
(if greedy
(goto-char from)
(while (and (not has-match) (< (point) to))
(sp--looking-at regexp)
(if (= (match-end 0) to)
(setq has-match t)
(forward-char 1)))
(not (null (search-backward-regexp (concat "\\(?:" regexp "\\)\\=") from t)))))))
(defun sp--looking-back-p (regexp &optional limit not-greedy)
"Same as `sp--looking-back' but do not change the match data."
(sp--looking-back regexp limit not-greedy)))
(defun sp--search-backward-regexp (regexp &optional bound noerror count)
"Works just like `search-backward-regexp', but returns the
longest possible match. That means that searching for
\"defun|fun\" backwards would return \"defun\" instead of
\"fun\", which would be matched first.
This is an internal function. Only use this for searching for
(setq count (or count 1))
(setq bound (or (sp--get-backward-bound) bound))
(let ((case-fold-search nil) r)
(while (> count 0)
(when (search-backward-regexp regexp bound noerror)
(goto-char (match-end 0))
(sp--looking-back regexp)
(setq r (goto-char (match-beginning 0))))
(setq count (1- count)))
(defun sp--search-forward-regexp (regexp &optional bound noerror count)
"Just like `search-forward-regexp', but always case sensitive."
(setq bound (or (sp--get-forward-bound) bound))
(let ((case-fold-search nil))
(search-forward-regexp regexp bound noerror count)))
(defun sp-get-quoted-string-bounds ()
"Return the bounds of the string around point.
If the point is not inside a quoted string, return nil."
(let ((parse-data (syntax-ppss)))
(when (nth 3 parse-data)
(let* ((open (nth 8 parse-data))
(close (save-excursion
(point) (point-max)
nil nil parse-data 'syntax-table)
(cons open close)))))
;; TODO: the repeated conditions are ugly, refactor this!
(defun sp-get-comment-bounds ()
"If the point is inside a comment, return its bounds."
(when (or (sp-point-in-comment)
(looking-at "[[:space:]]+\\s<"))
(let ((open (save-excursion
(while (and (not (bobp))
(or (sp-point-in-comment)
(backward-char 1)
(looking-at "[[:space:]]+\\s<"))))
(backward-char 1))
(when (not (or (bobp)
(or (sp-point-in-comment)
(backward-char 1)
(looking-at "[[:space:]]+\\s<")))))
(close (save-excursion
(while (and (not (eobp))
(or (sp-point-in-comment)
(looking-at "[[:space:]]+\\s<")))
(forward-char 1))
(let ((pp (1- (point))))
(when (not (or (eobp)
(looking-at "[[:space:]]+\\s<")
(and (eq (char-syntax
(char-after pp)) ?>)
(not (eq (char-after pp) ?\n)))
(/= (logand
(lsh 1 18)
(car (syntax-after pp))) 0)
(/= (logand
(lsh 1 19)
(car (syntax-after pp))) 0)))
(backward-char 1)))
(cons open close))))
(defun sp--get-string-or-comment-bounds ()
"Get the bounds of string or comment the point is in."
(or (sp-get-quoted-string-bounds)
(defmacro sp--search-and-save-match (search-fn pattern bound res beg end str)
"Save the last match info."
(setq ,res (funcall ,search-fn ,pattern ,bound t))
(when ,res
(setq ,beg (match-beginning 0))
(setq ,end (match-end 0))
(setq ,str (match-string 0)))
(cl-defun sp--skip-match-p (ms mb me
(global-skip (cdr (--first (memq major-mode (car it)) sp-navigate-skip-match)))
(pair-skip (sp-get-pair ms :skip-match)))
"Return non-nil if this match should be skipped.
This function uses two tests, one specified in
`sp-navigate-skip-match' (this is global setting for all pairs in
given major mode) and by a function specified in :skip-match
property of the pair.
If you are calling this function in a heavy loop, you can supply
the test functions as keyword arguments to speed up the lookup."
(or (when global-skip (funcall global-skip ms mb me))
(when pair-skip (funcall pair-skip ms mb me)))))
(defmacro sp--valid-initial-delimiter-p (form)
"Test the last match using `sp--skip-match-p'. The form should
be a function call that sets the match data."
(declare (debug (form)))
`(and ,form
(not (sp--skip-match-p
(match-string 0)
(match-beginning 0)
(match-end 0)))))
(defun sp--elisp-skip-match (ms mb me)
"Function used to test for escapes in lisp modes.
Non-nil return value means to skip the result."
(and ms
(> mb 1)
(goto-char mb)
(or (and (sp--looking-back "\\\\" 1 t)
;; it might be a part of ?\\ token
(not (sp--looking-back "\\?\\\\\\\\" 3 t)))
(and (not (sp-point-in-string-or-comment))
(sp--looking-back "\\?" 1 t) ;;TODO surely we can do better
(not (sp--looking-back "\\\\\\?" 2 t))
(not (sp--looking-back "\\s_\\?" 2 t))
(not (sp--looking-back "\\sw\\?" 2 t))))))))
(defun sp--backslash-skip-match (ms mb me)
(and ms
(goto-char mb)
(sp--looking-back "\\\\" 1 t))))
;; TODO: since this function is used for all the navigation, we should
;; optimize it a lot! Get some elisp profiler! Also, we should split
;; this into smaller functions (esp. the "first expression search"
;; business)
(defun sp-get-paired-expression (&optional back)
"Find the nearest balanced pair expression after point.
The expressions considered are those delimited by pairs on
(let* ((search-fn (if (not back) 'sp--search-forward-regexp 'sp--search-backward-regexp))
(global-skip-fn (cdr (--first (memq major-mode (car it)) sp-navigate-skip-match)))
(pair-list (sp--get-allowed-pair-list))
;; TODO UGLY HACK!!! When the situation is:
;; ..)|;; comment
;; the context the point gets is the comment. But if we
;; are searching backward, that is incorrect, because in
;; that case we want the context of the closing pair.
;; Therefore, if the direction is backward, we need to move
;; one point backward, then test the comment/string thing,
;; then compute the correct bounds, and then restore the
;; point so the search will pick up the )
;; However, we need to distinguish the cases where we are
;; in comment and trying to get out, and when we are in any
;; context and we jump into string (in that case, we should
;; report code context!). For example:
;; "foo"|;bar
;; or
;; "foo"|bar
;; should both report code context
;; and "|(foo)" should report string context.
;; Beware the case when we have a string inside a comment, like
;; (foo) ;; bar "baz"| qux
;; In this case we want to report comment context even when
;; backing into the "" (which however is commented)
;; Yet another case is when we are not in a comment but
;; directly after one and we search backwards, consider:
;; /* foo bar */|
;; in C-like language. In this case, we want to report the
;; context as comment.
;; Thanks for being consistent at handling syntax bounds Emacs!
(in-string-or-comment (if back
(let ((in-comment (sp-point-in-comment))
(in-string (sp-point-in-string)))
(unless (= (point) (point-min))
(in-comment (and in-comment (sp-point-in-comment)))
((and (not in-comment) (sp-point-in-comment)) t)
((or in-comment in-string))))))
(string-bounds (and in-string-or-comment (sp--get-string-or-comment-bounds)))
(fw-bound (if in-string-or-comment (cdr string-bounds) (point-max)))
(bw-bound (if in-string-or-comment (car string-bounds) (point-min)))
s e active-pair forward mb me ms r done
possible-pairs possible-interfering-pairs possible-ops possible-cls)
(while (and (not done)
;; #556 The regexp we use here might exclude or
;; include extra pairs in case the next match is in
;; a different context. There's no way to know
;; beforehand where we land, so we need to consider
;; *all* pairs in the search and then re-check with
;; a regexp based on the context of the found pair
;; use all the pairs!
(if back bw-bound fw-bound)
r mb me ms))
;; search for the first opening pair. Here, only consider tags
;; that are allowed in the current context.
(unless (or (not (save-excursion
(if back
(goto-char me)
(sp--looking-back-p (sp--get-allowed-regexp)))
(goto-char mb)
(sp--looking-at-p (sp--get-allowed-regexp)))))
(sp--skip-match-p ms mb me :global-skip global-skip-fn))
;; if the point originally wasn't inside of a string or comment
;; but now is, jump out of the string/comment and only search
;; the code. This ensures that the comments and strings are
;; skipped if we search inside code.
(if (and (not in-string-or-comment)
(if back
;; When searching back, the point lands on the
;; first character of whatever pair we've found
;; and it is in the proper context, for example
;; "|(foo)"
;; However, when searching forward, the point
;; lands after the last char of the pair so to get
;; its context we must back up one character
(sp-point-in-string-or-comment (1- (point)))))
(-if-let (bounds (sp--get-string-or-comment-bounds))
(let ((jump-to (if back (car bounds) (cdr bounds))))
(goto-char jump-to)
;; Can't move out of comment because eob, #427
(when (eobp)
(setq done t)))
(setq done t))
(setq done t))))
(when r
(setq possible-pairs (--filter (or (equal ms (car it))
(equal ms (cdr it)))
(setq possible-ops (-map 'car possible-pairs))
(setq possible-cls (-map 'cdr possible-pairs))
(setq pair-list (-difference pair-list possible-pairs))
(setq possible-interfering-pairs pair-list)
(while possible-interfering-pairs
(setq possible-interfering-pairs
(--filter (or (-contains? possible-ops (car it))
(-contains? possible-cls (cdr it)))
(setq pair-list (-difference pair-list possible-interfering-pairs))
(setq possible-ops (append possible-ops (-map 'car possible-interfering-pairs)))
(setq possible-cls (append possible-cls (-map 'cdr possible-interfering-pairs))))
(when (--any? (equal ms it) possible-ops)
(setq forward t)
(setq s mb)
(when back
(forward-char (length ms))))
(when (--any? (equal ms it) possible-cls)
(setq forward nil)
(setq e me)
(when (not back)
(backward-char (length ms))))
(let* ((opens (if forward possible-ops possible-cls))
(closes (if forward possible-cls possible-ops))
(needle (sp--strict-regexp-opt (append possible-ops possible-cls)))
(search-fn (if forward 'sp--search-forward-regexp 'sp--search-backward-regexp))
(depth 1)
(eof (if forward 'eobp 'bobp))
(b (if forward fw-bound bw-bound))
(open (substring-no-properties ms))
(close (substring-no-properties ms))
(failure (funcall eof))
(skip-match-pair-fns (->> possible-ops
(--mapcat (-when-let (smf (sp-get-pair it :skip-match))
(list (cons it smf) (cons (sp-get-pair it :close) smf)))))))
(while (and (> depth 0) (not (funcall eof)))
(sp--search-and-save-match search-fn needle b r mb me ms)
(if r
(unless (or (and (not in-string-or-comment)
(if forward (save-excursion
;; check the individual pair skipper. We
;; need to test all the possible-ops,
;; which makes it a bit ugly :/
(let ((skip-match-pair-fn
(cdr (--first (equal (car it) ms) skip-match-pair-fns))))
(sp--skip-match-p ms mb me :global-skip global-skip-fn :pair-skip skip-match-pair-fn)))
(when (--any? (equal ms it) opens) (setq depth (1+ depth)))
(when (--any? (equal ms it) closes) (setq depth (1- depth))))
(unless (minibufferp)
(sp-message :unmatched-expression))
(setq depth -1)
(setq failure t)))
(if forward
(setq e me)
(setq s mb))
(setq close (substring-no-properties ms))
(if (or failure
(/= depth 0))
(unless (minibufferp)
(sp-message :unmatched-expression))
(let ((end-in-cos (sp-point-in-string-or-comment (1- e)))) ;; fix the "point on comment" issue
((or (and (sp-point-in-string-or-comment s) (not end-in-cos))
(and (not (sp-point-in-string-or-comment s)) end-in-cos))
(unless (minibufferp)
(sp-message :delimiter-in-string))
(let* ((op (if forward open close)))
(list :beg s
:end e
:op op
:cl (if forward close open)
:prefix (sp--get-prefix s op)
:suffix (sp--get-suffix e op))))))))))))
;; TODO: this does not consider unbalanced quotes in comments!!!
(defun sp--find-next-stringlike-delimiter (needle search-fn-f &optional limit skip-fn)
"Find the next string-like delimiter, considering the escapes
and the skip-match predicate."