Permalink
Browse files

integrates corrections by Stěpán

  • Loading branch information...
1 parent efa57e4 commit 6312e09b3220a2b927ad62a098b71d0bf04e6ff0 @timcharper committed Jul 24, 2010
Showing with 74 additions and 56 deletions.
  1. +74 −56 vimpulse-surround.el
View
@@ -1,4 +1,4 @@
-;;; vimpulse-surround.el --- emulates surround.vim, for vimpulse -*- coding: utf-8 -*-
+;;; vimpulse-surround.el --- emulates surround.vim, for vimpulse
;; Copyright (C) 2010 Tim Harper
;;
@@ -19,77 +19,83 @@
;;; Commentary:
-;; Vimpulse-surround emulates surround.vim, a popular plugin for vim.
+;; `vimpulse-surround' emulates surround.vim, a popular Vim plugin.
;;
-;; Requires a recent install of vimpulse, from git: More information
-;; about vimpulse and how to install it here:
+;; Requires a recent Vimpulse version. More information on Vimpulse and how to
+;; get it can be found here:
;; http://www.assembla.com/spaces/vimpulse
;; Tested with GNU Emacs 23.2
+;;; Code:
+
(require 'vimpulse)
-(defgroup vimpulse-surround-pairs nil
+(defgroup vimpulse-surround nil
"surround.vim in Emacs"
:prefix "vimpulse-surround-"
:group 'vimpulse)
(defcustom vimpulse-surround-pairs
- '((")" . ("(" . ")"))
- ("(" . ("( " . " )"))
- ("]" . ("[" . "]"))
- ("[" . ("[ " . " ]"))
- ("}" . ("{" . "}"))
- ("{" . ("{ " . " }"))
- ("#" . ("#{" . "}")))
- "An alist of surround values. This only has effect on creating new surround pairs, not deleting them."
+ '((")" ("(" . ")"))
+ ("(" ("( " . " )"))
+ ("]" ("[" . "]"))
+ ("[" ("[ " . " ]"))
+ ("}" ("{" . "}"))
+ ("{" ("{ " . " }"))
+ ("#" ("#{" . "}")))
+ "Alist of surround items.
+Each item is of the form (TRIGGER (LEFT . RIGHT)), all strings.
+Does not apply to delete surround, change surround."
:group 'vimpulse-surround
:type '(repeat (cons (regexp :tag "Key")
(symbol :tag "Surround pair"))))
(defun vimpulse-surround-char-to-pair (char)
(let ((pairs (assoc-default char vimpulse-surround-pairs)))
- (if pairs
- pairs
- (cons char char))))
+ (or pairs
+ (cons char char))))
(defvar *vimpulse-surrounding* nil
- "This value is set by the vimpulse-surround-define-text-object. It triggers vimpulse-change. Nothing to see here, move along.")
+ "Internal variable set by `vimpulse-surround-define-text-object'.
+It triggers `vimpulse-change'. Nothing to see here, move along.")
(defvar *vimpulse-surround-start-size* nil)
(defvar *vimpulse-surround-end-size* nil)
(defun vimpulse-surround-region (start end)
- "surround selection with input"
+ "Surround selection with input."
(interactive "r")
- (message "hi")
- (let ((pair))
+ (let (pair)
(viper-special-read-and-insert-char)
- (setq pair (vimpulse-surround-char-to-pair (format "%c" (viper-char-at-pos 'backward))))
- (delete-char -1 t)
+ (setq pair (vimpulse-surround-char-to-pair
+ (format "%c" (viper-char-at-pos 'backward))))
+ (delete-char -1)
(goto-char end)
(insert (cdr pair))
(goto-char start)
(insert (car pair))
(goto-char start)))
(defun vimpulse-surround-prepend-key-prefix (keys)
- (map 'list (lambda (key) (concat "s" key)) keys))
+ (mapcar (lambda (key) (concat "s" key)) keys))
(defmacro vimpulse-surround-define-text-object (object args &rest body)
- (let ((forward-args '())
- (strip-keys)
+ (let (forward-args
+ strip-keys
(strip-object-name (intern (concat (symbol-name object) "-strip")))
- (keys)
+ keys
(docstring (pop body)))
(while (keywordp (car body))
(setq keyword (pop body))
(cond
((eq keyword :keys)
- (setq keys (vimpulse-surround-prepend-key-prefix (vimpulse-unquote (pop body)))))
+ (setq keys (vimpulse-surround-prepend-key-prefix
+ (vimpulse-unquote (pop body)))))
((eq keyword :strip-keys)
- (setq strip-keys (vimpulse-surround-prepend-key-prefix (vimpulse-unquote (pop body)))))
+ (setq strip-keys (vimpulse-surround-prepend-key-prefix
+ (vimpulse-unquote (pop body)))))
(t
(push (pop body) forward-args)
(push keyword forward-args))))
@@ -112,20 +118,19 @@
output))
(defun vimpulse-surround-zap-whitespace (direction boundary)
- (let ((looking_at_space? (if (= 1 direction)
+ (let ((looking-at-space? (if (= direction 1)
(lambda () (looking-at "[ \t]"))
(lambda () (looking-back "[ \t]")))))
- (while (and (funcall looking_at_space?) (not (= (point) boundary)))
+ (while (and (funcall looking-at-space?) (not (= (point) boundary)))
(delete-char direction)
- (when (= 1 direction) (setq boundary (- boundary 1))))))
+ (when (= direction 1) (setq boundary (1- boundary))))))
(defun vimpulse-surround-delete (begin end strip)
- "Deletes the surrounding characters in the range. If strip is t,
- then eliminate all surrounding whitespace around the range"
+ "Delete the surrounding characters in the range BEGIN END.
+If STRIP is non-nil, eliminate all whitespace surrounding the range."
(let ((o (make-overlay begin end)))
- (goto-char (overlay-start o)) (delete-char 1 t)
- (goto-char (overlay-end o)) (delete-char -1 t)
-
+ (goto-char (overlay-start o)) (delete-char 1)
+ (goto-char (overlay-end o)) (delete-char -1)
(when strip
(vimpulse-surround-zap-whitespace -1 (overlay-start o))
(goto-char (overlay-start o))
@@ -134,67 +139,80 @@
(delete-overlay o)))
(defun vimpulse-surround-change (begin end strip)
- "Delete specified surrounding items, and then prompt for a new pair."
+ "Replace items surrounding the range BEGIN END for new ones.
+See `vimpulse-surround-delete' for the meaning of the STRIP argument."
(let ((o (make-overlay begin end)))
(vimpulse-surround-delete begin end strip)
(vimpulse-surround-region (overlay-start o) (overlay-end o))
(delete-overlay o)))
-(defun vimpulse-delete-surround-or-delete (&optional BEGIN END DONT SAVE)
- "Prompts for a range. If the range returned is detected to be a surround range, dispatch to the vimpulse-surround-delete. Otherwise, dispatch to vimpulse-delete. "
+(defun vimpulse-delete-surround-or-delete (&optional beg end dont-save)
+ "Dispatcher replacement for `vimpulse-delete'.
+Prompt for a range. If the range returned is detected to be a surround
+range, dispatch to `vimpulse-surround-delete'.
+Otherwise, dispatch to `vimpulse-delete'."
(interactive)
- (let* ((*vimpulse-surrounding* nil)
- (range (if BEGIN (list BEGIN END) (vimpulse-range))))
+ (let (*vimpulse-surrounding*)
+ (when (not beg) (let ((range (vimpulse-range)))
+ (setq beg (car range)
+ end (cadr range))))
(if *vimpulse-surrounding*
- (vimpulse-surround-delete (car range) (cadr range) (equal 'strip *vimpulse-surrounding*))
- (eval (append '(vimpulse-delete) range)))))
-
-(defun vimpulse-change-surround-or-change (&optional BEGIN END DONT SAVE)
- "Prompts for a range. If the range returned is detected to be a surround range, dispatch to the vimpulse-surround-change. Otherwise, dispatch to vimpulse-change."
+ (vimpulse-surround-delete beg end (eq *vimpulse-surrounding* 'strip))
+ (vimpulse-delete beg end dont-save))))
+
+(defun vimpulse-change-surround-or-change (&optional beg end dont-save)
+ "Dispatcher replacement for `vimpulse-change'.
+Prompt for a range. If the range returned is detected to be a surround
+range, dispatch to `vimpulse-surround-change'.
+Otherwise, dispatch to `vimpulse-change'."
(interactive)
- (let* ((*vimpulse-surrounding* nil)
- (range (if BEGIN (list BEGIN END) (vimpulse-range))))
+
+ (let (*vimpulse-surrounding*)
+ (when (not beg) (let ((range (vimpulse-range)))
+ (setq beg (car range)
+ end (cadr range))))
(if *vimpulse-surrounding*
- (vimpulse-surround-change (car range) (cadr range) (equal 'strip *vimpulse-surrounding*))
- (eval (append '(vimpulse-change) range)))))
+ (vimpulse-surround-change beg end (eq *vimpulse-surrounding* 'strip))
+ (vimpulse-change beg end dont-save))))
(define-key viper-vi-basic-map "d" 'vimpulse-delete-surround-or-delete)
(define-key viper-vi-basic-map "c" 'vimpulse-change-surround-or-change)
(define-key vimpulse-visual-basic-map "s" 'vimpulse-surround-region)
(vimpulse-surround-define-text-object vimpulse-surround-paren (arg)
- "Delete surrounding parenthesis."
+ "Select surrounding parentheses."
:keys '("b" ")")
:strip-keys '("(")
(vimpulse-paren-range arg ?\( nil t))
(vimpulse-surround-define-text-object vimpulse-surround-bracket (arg)
- "Delete surrounding brackets."
+ "Select surrounding brackets."
:keys '("]")
:strip-keys '("[")
(vimpulse-paren-range arg ?\[ nil t))
(vimpulse-surround-define-text-object vimpulse-surround-brace (arg)
- "Delete surrounding curly braces."
+ "Select surrounding curly braces."
:keys '("}")
:strip-keys '("{")
(vimpulse-paren-range arg ?\{ nil t))
(vimpulse-surround-define-text-object vimpulse-surround-angle (arg)
- "Delete surrounding curly braces."
+ "Select surrounding curly braces."
:keys '(">")
:strip-keys '("<")
(vimpulse-paren-range arg ?< nil t))
(vimpulse-surround-define-text-object vimpulse-surround-single-quote (arg)
- "Select a single quoted expression."
+ "Select a single-quoted expression."
:keys '("'")
(vimpulse-quote-range arg ?' t))
(vimpulse-surround-define-text-object vimpulse-surround-double-quote (arg)
- "Select a double quoted expression."
+ "Select a double-quoted expression."
:keys '("\"")
(vimpulse-quote-range arg ?\" t))
(provide 'vimpulse-surround)
+;;; vimpulse-surround.el ends here

0 comments on commit 6312e09

Please sign in to comment.