mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Import ruby-electric.el 2.2.1.
* misc/ruby-electric.el: Import version 2.2.1 from https://github.com/knu/ruby-electric.el. Improve compatibility with and optimize for Emacs 24.4. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49365 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
c51293ffae
commit
e65fa1f467
2 changed files with 155 additions and 77 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
Wed Jan 21 22:33:51 2015 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
|
* misc/ruby-electric.el: Import version 2.2.1 from
|
||||||
|
https://github.com/knu/ruby-electric.el. Improve compatibility
|
||||||
|
with and optimize for Emacs 24.4.
|
||||||
|
|
||||||
Wed Jan 21 09:40:52 2015 Zachary Scott <e@zzak.io>
|
Wed Jan 21 09:40:52 2015 Zachary Scott <e@zzak.io>
|
||||||
|
|
||||||
* file.c: Document other cases of missing birthtime on OS with patch
|
* file.c: Document other cases of missing birthtime on OS with patch
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
;; URL: https://github.com/knu/ruby-electric.el
|
;; URL: https://github.com/knu/ruby-electric.el
|
||||||
;; Keywords: languages ruby
|
;; Keywords: languages ruby
|
||||||
;; License: The same license terms as Ruby
|
;; License: The same license terms as Ruby
|
||||||
;; Version: 2.1.1
|
;; Version: 2.2.1
|
||||||
|
|
||||||
;;; Commentary:
|
;;; Commentary:
|
||||||
;;
|
;;
|
||||||
|
@ -172,6 +172,7 @@ instead."
|
||||||
(define-key map [remap delete-backward-char] 'ruby-electric-delete-backward-char)
|
(define-key map [remap delete-backward-char] 'ruby-electric-delete-backward-char)
|
||||||
(define-key map [remap newline] 'ruby-electric-space/return)
|
(define-key map [remap newline] 'ruby-electric-space/return)
|
||||||
(define-key map [remap newline-and-indent] 'ruby-electric-space/return)
|
(define-key map [remap newline-and-indent] 'ruby-electric-space/return)
|
||||||
|
(define-key map [remap electric-newline-and-maybe-indent] 'ruby-electric-space/return)
|
||||||
(dolist (x ruby-electric-delimiters-alist)
|
(dolist (x ruby-electric-delimiters-alist)
|
||||||
(let* ((delim (car x))
|
(let* ((delim (car x))
|
||||||
(plist (cdr x))
|
(plist (cdr x))
|
||||||
|
@ -247,8 +248,11 @@ enabled."
|
||||||
(interactive "*P")
|
(interactive "*P")
|
||||||
(and (boundp 'sp-last-operation)
|
(and (boundp 'sp-last-operation)
|
||||||
(setq sp-delayed-pair nil))
|
(setq sp-delayed-pair nil))
|
||||||
(cond (arg
|
(cond ((or arg
|
||||||
(insert (make-string (prefix-numeric-value arg) last-command-event)))
|
(region-active-p))
|
||||||
|
(or (= last-command-event ?\s)
|
||||||
|
(setq last-command-event ?\n))
|
||||||
|
(ruby-electric-replace-region-or-insert))
|
||||||
((ruby-electric-space/return-can-be-expanded-p)
|
((ruby-electric-space/return-can-be-expanded-p)
|
||||||
(let (action)
|
(let (action)
|
||||||
(save-excursion
|
(save-excursion
|
||||||
|
@ -310,131 +314,198 @@ enabled."
|
||||||
(and (ruby-electric-code-at-point-p)
|
(and (ruby-electric-code-at-point-p)
|
||||||
(looking-back ruby-electric-expandable-keyword-re)))
|
(looking-back ruby-electric-expandable-keyword-re)))
|
||||||
|
|
||||||
(defun ruby-electric-cua-replace-region-maybe()
|
(defun ruby-electric-replace-region-or-insert ()
|
||||||
(let ((func (key-binding [remap self-insert-command])))
|
(and (region-active-p)
|
||||||
(when (memq func '(cua-replace-region
|
(bound-and-true-p delete-selection-mode)
|
||||||
sp--cua-replace-region))
|
(fboundp 'delete-selection-helper)
|
||||||
(setq this-original-command 'self-insert-command)
|
(delete-selection-helper (get 'self-insert-command 'delete-selection)))
|
||||||
(funcall (setq this-command func))
|
(insert (make-string (prefix-numeric-value current-prefix-arg)
|
||||||
t)))
|
last-command-event))
|
||||||
|
(setq this-command 'self-insert-command))
|
||||||
|
|
||||||
(defmacro ruby-electric-insert (arg &rest body)
|
(defmacro ruby-electric-insert (arg &rest body)
|
||||||
`(cond ((ruby-electric-cua-replace-region-maybe))
|
`(cond ((and
|
||||||
((and
|
|
||||||
(null ,arg)
|
(null ,arg)
|
||||||
(ruby-electric-command-char-expandable-punct-p last-command-event))
|
(ruby-electric-command-char-expandable-punct-p last-command-event))
|
||||||
(insert last-command-event)
|
(let ((region-beginning
|
||||||
,@body)
|
(cond ((region-active-p)
|
||||||
(t
|
(prog1
|
||||||
(setq this-command 'self-insert-command)
|
(save-excursion
|
||||||
(insert (make-string (prefix-numeric-value ,arg) last-command-event)))))
|
(goto-char (region-beginning))
|
||||||
|
(insert last-command-event)
|
||||||
|
(point))
|
||||||
|
(goto-char (region-end))))
|
||||||
|
(t
|
||||||
|
(insert last-command-event)
|
||||||
|
nil))))
|
||||||
|
,@body
|
||||||
|
(and region-beginning
|
||||||
|
;; If no extra character is inserted, go back to the
|
||||||
|
;; region beginning.
|
||||||
|
(eq this-command 'self-insert-command)
|
||||||
|
(goto-char region-beginning))))
|
||||||
|
((ruby-electric-replace-region-or-insert))))
|
||||||
|
|
||||||
(defun ruby-electric-curlies(arg)
|
(defun ruby-electric-curlies (arg)
|
||||||
(interactive "*P")
|
(interactive "*P")
|
||||||
(ruby-electric-insert
|
(ruby-electric-insert
|
||||||
arg
|
arg
|
||||||
(cond
|
(cond
|
||||||
((ruby-electric-code-at-point-p)
|
((ruby-electric-code-at-point-p)
|
||||||
(insert "}")
|
(save-excursion
|
||||||
(backward-char 1)
|
(insert "}")
|
||||||
(redisplay)
|
(font-lock-fontify-region (line-beginning-position) (point)))
|
||||||
(cond
|
(cond
|
||||||
((ruby-electric-string-at-point-p) ;; %w{}, %r{}, etc.
|
((ruby-electric-string-at-point-p) ;; %w{}, %r{}, etc.
|
||||||
t)
|
(if region-beginning
|
||||||
|
(forward-char 1)))
|
||||||
(ruby-electric-newline-before-closing-bracket
|
(ruby-electric-newline-before-closing-bracket
|
||||||
(insert " ")
|
(cond (region-beginning
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(newline)
|
(goto-char region-beginning)
|
||||||
(ruby-indent-line t)))
|
(newline))
|
||||||
|
(newline)
|
||||||
|
(forward-char 1)
|
||||||
|
(indent-region region-beginning (line-end-position)))
|
||||||
|
(t
|
||||||
|
(insert " ")
|
||||||
|
(save-excursion
|
||||||
|
(newline)
|
||||||
|
(ruby-indent-line t)))))
|
||||||
(t
|
(t
|
||||||
(insert " ")
|
(if region-beginning
|
||||||
(backward-char 1))))
|
(save-excursion
|
||||||
|
(goto-char region-beginning)
|
||||||
|
(insert " "))
|
||||||
|
(insert " "))
|
||||||
|
(insert " ")
|
||||||
|
(and region-beginning
|
||||||
|
(forward-char 1)))))
|
||||||
((ruby-electric-string-at-point-p)
|
((ruby-electric-string-at-point-p)
|
||||||
(save-excursion
|
(let ((start-position (1- (or region-beginning (point)))))
|
||||||
(backward-char 1)
|
|
||||||
(cond
|
(cond
|
||||||
((char-equal ?\# (preceding-char))
|
((char-equal ?\# (char-before start-position))
|
||||||
(unless (save-excursion
|
(unless (save-excursion
|
||||||
(backward-char 1)
|
(goto-char (1- start-position))
|
||||||
(ruby-electric-escaped-p))
|
(ruby-electric-escaped-p))
|
||||||
(forward-char 1)
|
(insert "}")
|
||||||
(insert "}")))
|
(or region-beginning
|
||||||
|
(backward-char 1))))
|
||||||
((or
|
((or
|
||||||
(ruby-electric-command-char-expandable-punct-p ?\#)
|
(ruby-electric-command-char-expandable-punct-p ?\#)
|
||||||
(ruby-electric-escaped-p))
|
(save-excursion
|
||||||
|
(goto-char start-position)
|
||||||
|
(ruby-electric-escaped-p)))
|
||||||
|
(if region-beginning
|
||||||
|
(goto-char region-beginning))
|
||||||
(setq this-command 'self-insert-command))
|
(setq this-command 'self-insert-command))
|
||||||
(t
|
(t
|
||||||
(insert "#")
|
(save-excursion
|
||||||
(forward-char 1)
|
(goto-char start-position)
|
||||||
(insert "}")))))
|
(insert "#"))
|
||||||
|
(insert "}")
|
||||||
|
(or region-beginning
|
||||||
|
(backward-char 1))))))
|
||||||
(t
|
(t
|
||||||
(setq this-command 'self-insert-command)))))
|
(delete-char -1)
|
||||||
|
(ruby-electric-replace-region-or-insert)))))
|
||||||
|
|
||||||
(defun ruby-electric-hash(arg)
|
(defun ruby-electric-hash (arg)
|
||||||
(interactive "*P")
|
(interactive "*P")
|
||||||
(ruby-electric-insert
|
(ruby-electric-insert
|
||||||
arg
|
arg
|
||||||
(and (ruby-electric-string-at-point-p)
|
(if (ruby-electric-string-at-point-p)
|
||||||
(or (char-equal (following-char) ?') ;; likely to be in ''
|
(let ((start-position (1- (or region-beginning (point)))))
|
||||||
(save-excursion
|
(cond
|
||||||
(backward-char 1)
|
((char-equal (following-char) ?')) ;; likely to be in ''
|
||||||
(ruby-electric-escaped-p))
|
((save-excursion
|
||||||
(progn
|
(goto-char start-position)
|
||||||
(insert "{}")
|
(ruby-electric-escaped-p)))
|
||||||
(backward-char 1))))))
|
(region-beginning
|
||||||
|
(save-excursion
|
||||||
|
(goto-char (1+ start-position))
|
||||||
|
(insert "{"))
|
||||||
|
(insert "}"))
|
||||||
|
(t
|
||||||
|
(insert "{")
|
||||||
|
(save-excursion
|
||||||
|
(insert "}")))))
|
||||||
|
(delete-char -1)
|
||||||
|
(ruby-electric-replace-region-or-insert))))
|
||||||
|
|
||||||
(defmacro ruby-electric-avoid-eob(&rest body)
|
(defun ruby-electric-matching-char (arg)
|
||||||
`(if (eobp)
|
|
||||||
(save-excursion
|
|
||||||
(insert "\n")
|
|
||||||
(backward-char)
|
|
||||||
,@body
|
|
||||||
(prog1
|
|
||||||
(ruby-electric-string-at-point-p)
|
|
||||||
(delete-char 1)))
|
|
||||||
,@body))
|
|
||||||
|
|
||||||
(defun ruby-electric-matching-char(arg)
|
|
||||||
(interactive "*P")
|
(interactive "*P")
|
||||||
(ruby-electric-insert
|
(ruby-electric-insert
|
||||||
arg
|
arg
|
||||||
(let ((closing (cdr (assoc last-command-event
|
(let ((closing (cdr (assoc last-command-event
|
||||||
ruby-electric-matching-delimeter-alist))))
|
ruby-electric-matching-delimeter-alist))))
|
||||||
(cond
|
(cond
|
||||||
|
;; quotes
|
||||||
((char-equal closing last-command-event)
|
((char-equal closing last-command-event)
|
||||||
(if (and (not (ruby-electric-string-at-point-p))
|
(cond ((let ((start-position (or region-beginning (point))))
|
||||||
(ruby-electric-avoid-eob
|
;; check if this quote has just started a string
|
||||||
(redisplay)
|
(and
|
||||||
(ruby-electric-string-at-point-p)))
|
(unwind-protect
|
||||||
(save-excursion (insert closing))
|
(save-excursion
|
||||||
(and (eq last-command 'ruby-electric-matching-char)
|
(subst-char-in-region (1- start-position) start-position
|
||||||
(char-equal (following-char) closing) ;; repeated quotes
|
last-command-event ?\s)
|
||||||
(delete-forward-char 1))
|
(goto-char (1- start-position))
|
||||||
(setq this-command 'self-insert-command)))
|
(save-excursion
|
||||||
|
(font-lock-fontify-region (line-beginning-position) (1+ (point))))
|
||||||
|
(not (ruby-electric-string-at-point-p)))
|
||||||
|
(subst-char-in-region (1- start-position) start-position
|
||||||
|
?\s last-command-event))
|
||||||
|
(save-excursion
|
||||||
|
(goto-char (1- start-position))
|
||||||
|
(save-excursion
|
||||||
|
(font-lock-fontify-region (line-beginning-position) (1+ (point))))
|
||||||
|
(ruby-electric-string-at-point-p))))
|
||||||
|
(if region-beginning
|
||||||
|
;; escape quotes of the same kind, backslash and hash
|
||||||
|
(let ((re (format "[%c\\%s]"
|
||||||
|
last-command-event
|
||||||
|
(if (char-equal last-command-event ?\")
|
||||||
|
"#" "")))
|
||||||
|
(bound (point)))
|
||||||
|
(save-excursion
|
||||||
|
(goto-char region-beginning)
|
||||||
|
(while (re-search-forward re bound t)
|
||||||
|
(let ((end (point)))
|
||||||
|
(replace-match "\\\\\\&")
|
||||||
|
(setq bound (+ bound (- (point) end))))))))
|
||||||
|
(insert closing)
|
||||||
|
(or region-beginning
|
||||||
|
(backward-char 1)))
|
||||||
|
(t
|
||||||
|
(and (eq last-command 'ruby-electric-matching-char)
|
||||||
|
(char-equal (following-char) closing) ;; repeated quotes
|
||||||
|
(delete-char 1))
|
||||||
|
(setq this-command 'self-insert-command))))
|
||||||
((ruby-electric-code-at-point-p)
|
((ruby-electric-code-at-point-p)
|
||||||
(save-excursion (insert closing)))))))
|
(insert closing)
|
||||||
|
(or region-beginning
|
||||||
|
(backward-char 1)))))))
|
||||||
|
|
||||||
(defun ruby-electric-closing-char(arg)
|
(defun ruby-electric-closing-char(arg)
|
||||||
(interactive "*P")
|
(interactive "*P")
|
||||||
(cond
|
(cond
|
||||||
((ruby-electric-cua-replace-region-maybe))
|
|
||||||
(arg
|
(arg
|
||||||
(setq this-command 'self-insert-command)
|
(ruby-electric-replace-region-or-insert))
|
||||||
(insert (make-string (prefix-numeric-value arg) last-command-event)))
|
|
||||||
((and
|
((and
|
||||||
(eq last-command 'ruby-electric-curlies)
|
(eq last-command 'ruby-electric-curlies)
|
||||||
(= last-command-event ?})) ;; {}
|
(= last-command-event ?})
|
||||||
|
(not (char-equal (preceding-char) last-command-event))) ;; {}
|
||||||
(if (char-equal (following-char) ?\n) (delete-char 1))
|
(if (char-equal (following-char) ?\n) (delete-char 1))
|
||||||
(delete-horizontal-space)
|
(delete-horizontal-space)
|
||||||
(forward-char))
|
(forward-char))
|
||||||
((and
|
((and
|
||||||
(= last-command-event (following-char))
|
(= last-command-event (following-char))
|
||||||
|
(not (char-equal (preceding-char) last-command-event))
|
||||||
(memq last-command '(ruby-electric-matching-char
|
(memq last-command '(ruby-electric-matching-char
|
||||||
ruby-electric-closing-char))) ;; ()/[] and (())/[[]]
|
ruby-electric-closing-char))) ;; ()/[] and (())/[[]]
|
||||||
(forward-char))
|
(forward-char))
|
||||||
(t
|
(t
|
||||||
(setq this-command 'self-insert-command)
|
(ruby-electric-replace-region-or-insert)
|
||||||
(self-insert-command 1)
|
|
||||||
(if ruby-electric-autoindent-on-closing-char
|
(if ruby-electric-autoindent-on-closing-char
|
||||||
(ruby-indent-line)))))
|
(ruby-indent-line)))))
|
||||||
|
|
||||||
|
@ -446,7 +517,8 @@ enabled."
|
||||||
(looking-back ruby-electric-expandable-bar-re))
|
(looking-back ruby-electric-expandable-bar-re))
|
||||||
(save-excursion (insert "|")))
|
(save-excursion (insert "|")))
|
||||||
(t
|
(t
|
||||||
(setq this-command 'self-insert-command)))))
|
(delete-char -1)
|
||||||
|
(ruby-electric-replace-region-or-insert)))))
|
||||||
|
|
||||||
(defun ruby-electric-delete-backward-char(arg)
|
(defun ruby-electric-delete-backward-char(arg)
|
||||||
(interactive "*p")
|
(interactive "*p")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue