sp: fix quote auto-deleting + conservative backquotes

This commit is contained in:
Henrik Lissner 2016-05-26 18:53:46 -04:00
parent 82fa5d977a
commit 2a4438890f
2 changed files with 43 additions and 35 deletions

View file

@ -237,7 +237,7 @@
;; Auto-close more conservatively ;; Auto-close more conservatively
(sp-pair "'" nil :unless '(sp-point-after-word-p)) (sp-pair "'" nil :unless '(sp-point-after-word-p))
(sp-pair "\"" nil :unless '(sp-point-before-word-p sp-point-before-same-p)) (sp-pair "\"" nil :unless '(sp-point-before-word-p sp-point-after-word-p sp-point-before-same-p))
(sp-pair "{" nil :post-handlers '(("||\n[i]" "RET") ("| " " ")) (sp-pair "{" nil :post-handlers '(("||\n[i]" "RET") ("| " " "))
:unless '(sp-point-before-word-p sp-point-before-same-p)) :unless '(sp-point-before-word-p sp-point-before-same-p))
(sp-pair "(" nil :post-handlers '(("||\n[i]" "RET") ("| " " ")) (sp-pair "(" nil :post-handlers '(("||\n[i]" "RET") ("| " " "))
@ -247,8 +247,7 @@
(sp-local-pair (sp-local-pair
'css-mode "/*" "*/" :post-handlers '(("[d-3]||\n[i]" "RET") ("| " "SPC"))) 'css-mode "/*" "*/" :post-handlers '(("[d-3]||\n[i]" "RET") ("| " "SPC")))
(sp-local-pair (sp-local-pair '(sh-mode markdown-mode) "`" nil
'(sh-mode markdown-mode) "`" "`"
:unless '(sp-point-before-word-p sp-point-before-same-p)) :unless '(sp-point-before-word-p sp-point-before-same-p))
(sp-with-modes '(xml-mode nxml-mode php-mode) (sp-with-modes '(xml-mode nxml-mode php-mode)
(sp-local-pair "<!--" "-->" :post-handlers '(("| " "SPC"))))) (sp-local-pair "<!--" "-->" :post-handlers '(("| " "SPC")))))
@ -279,9 +278,9 @@
;; a) eat spaces on either side of the cursor, if present ( | ) -> (|) ;; a) eat spaces on either side of the cursor, if present ( | ) -> (|)
;; b) allow backspace to delete space-indented blocks intelligently ;; b) allow backspace to delete space-indented blocks intelligently
;; c) but do none of this when inside a string ;; c) but do none of this when inside a string
:i "SPC" 'doom/inflate-space-maybe :i "SPC" 'doom/inflate-space-maybe
:i [remap backward-delete-char-untabify] 'doom/deflate-space-maybe :i [remap delete-backward-char] 'doom/deflate-space-maybe
:i [remap newline] 'doom/newline-and-indent :i [remap newline] 'doom/newline-and-indent
;; Smarter move-to-beginning-of-line ;; Smarter move-to-beginning-of-line
:i [remap move-beginning-of-line] 'doom/move-to-bol :i [remap move-beginning-of-line] 'doom/move-to-bol
;; Restore bash-esque keymaps in insert mode; C-w and C-a already exist ;; Restore bash-esque keymaps in insert mode; C-w and C-a already exist

View file

@ -48,31 +48,41 @@ already there, move it to the true bol."
;; Mimic expandtab in vim ;; Mimic expandtab in vim
;;;###autoload ;;;###autoload
;;;###autoload
(defun doom/backward-delete-whitespace-to-column () (defun doom/backward-delete-whitespace-to-column ()
"Delete back to the previous column of whitespace, or as much "Delete back to the previous column of whitespace, or as much whitespace as
whitespace as possible, or just one char if that's not possible." possible, or just one char if that's not possible."
(interactive) (interactive)
(cond ;; If in a string (let* ((context (sp--get-pair-list-context 'navigate))
((sp-point-in-string) (open-pair (sp--get-opening-regexp context))
(call-interactively 'backward-delete-char-untabify)) (close-pair (sp--get-closing-regexp context))
;; If using tabs (or at bol), just delete normally open-len close-len)
((or indent-tabs-mode (cond ;; When in strings (sp acts weird with quotes; this is the fix)
(= (point-at-bol) (point))) ;; Also, skip closing delimiters
(call-interactively 'backward-delete-char)) ((and (and (sp--looking-back open-pair)
;; Delete up to the nearest tab column IF only whitespace between point (setq open-len (- (match-beginning 0) (match-end 0))))
;; and bol. (and (looking-at close-pair)
((looking-back "^[\\t ]*" (point-at-bol)) (setq close-len (- (match-beginning 0) (match-end 0)))))
(let ((movement (% (current-column) tab-width)) (delete-backward-char open-len)
(p (point))) (delete-char close-len))
(when (= movement 0) ;; If using tabs (or at bol), just delete normally
(setq movement tab-width)) ((or indent-tabs-mode
(save-match-data (= (point-at-bol) (point)))
(if (string-match "\\w*\\(\\s-+\\)$" (call-interactively 'backward-delete-char-untabify))
(buffer-substring-no-properties (- p movement) p)) ;; Delete up to the nearest tab column IF only whitespace between
(backward-delete-char (- (match-end 1) (match-beginning 1))) ;; point and bol.
(backward-delete-char-untabify 1))))) ((sp--looking-back-p "^[\\t ]*" (point-at-bol))
;; Otherwise do a regular delete (let ((movement (% (current-column) tab-width))
(t (backward-delete-char-untabify 1)))) (p (point)))
(when (= movement 0)
(setq movement tab-width))
(save-match-data
(if (string-match "\\w*\\(\\s-+\\)$"
(buffer-substring-no-properties (- p movement) p))
(delete-backward-char (- (match-end 1) (match-beginning 1)))
(call-interactively 'delete-backward-char)))))
;; Otherwise do a regular delete
(t (call-interactively 'delete-backward-char)))))
;;;###autoload ;;;###autoload
(defun doom/dumb-indent (&optional smart) (defun doom/dumb-indent (&optional smart)
@ -96,7 +106,7 @@ auto-complete window is open."
(defun doom/dumb-dedent () (defun doom/dumb-dedent ()
(interactive) (interactive)
(if indent-tabs-mode (if indent-tabs-mode
(delete-char -1) (call-interactively 'backward-delete-char)
(save-excursion (save-excursion
(unless (looking-back "^[\s\t]*") (unless (looking-back "^[\s\t]*")
(evil-first-non-blank)) (evil-first-non-blank))
@ -124,13 +134,12 @@ spaces on either side of the point if so. Resorts to
(if (doom/surrounded-p) (if (doom/surrounded-p)
(let ((whitespace-match (match-string 1))) (let ((whitespace-match (match-string 1)))
(cond ((not whitespace-match) (cond ((not whitespace-match)
(call-interactively 'sp-backward-delete-char)) (call-interactively 'delete-backward-char))
((string-match "\n" whitespace-match) ((string-match "\n" whitespace-match)
(evil-delete (point-at-bol) (point)) (evil-delete (point-at-bol) (point))
(delete-char -1) (call-interactively 'delete-backward-char)
(save-excursion (delete-char 1))) (save-excursion (call-interactively 'delete-char)))
(t (t (just-one-space 0))))
(just-one-space 0))))
(doom/backward-delete-whitespace-to-column)))) (doom/backward-delete-whitespace-to-column))))
;;;###autoload ;;;###autoload