Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions electric-spacing.el
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

(defvar electric-spacing-rules
'((?= . electric-spacing-self-insert-command)
(?! . electric-spacing-!)
(?< . electric-spacing-<)
(?> . electric-spacing->)
(?% . electric-spacing-%)
Expand All @@ -70,6 +71,11 @@
(?. . electric-spacing-.)
(?^ . electric-spacing-self-insert-command)))

(defconst electric-spacing-operators-regexp
(regexp-opt
(mapcar (lambda (el) (char-to-string (car el)))
electric-spacing-rules)))

(defun electric-spacing-post-self-insert-function ()
(when (electric-spacing-should-run?)
(let ((rule (cdr (assq last-command-event electric-spacing-rules))))
Expand Down Expand Up @@ -126,9 +132,7 @@ when `only-where' is 'middle, we will not insert space."
(`after (insert op " "))
(_
(let ((begin? (bolp)))
(unless (or (looking-back (regexp-opt
(mapcar 'char-to-string
(mapcar 'car electric-spacing-rules)))
(unless (or (looking-back electric-spacing-operators-regexp
(line-beginning-position))
begin?)
(insert " "))
Expand Down Expand Up @@ -168,14 +172,22 @@ so let's not get too insert-happy."
(t
(electric-spacing-insert-1 op 'middle))))

(defconst electric-spacing-operators-regexp
(regexp-opt
(mapcar (lambda (el) (char-to-string (car el)))
electric-spacing-rules)))


;;; Fine Tunings

(defun electric-spacing-! ()
"See `electric-spacing-insert'."
;; ,----[ cases ]
;; | (!a), !a
;; | a != b, a !== b
;; | !!a
;; | #!
;; | a = !b
;; `----
(if (looking-back "[a-z]\\|= ")
Copy link
Owner

@xwl xwl Oct 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it should be something like: "[a-zA-Z0-9_]\|= "

(electric-spacing-insert "!" 'before)
(electric-spacing-insert "!" 'middle)))

(defun electric-spacing-< ()
"See `electric-spacing-insert'."
(cond
Expand Down Expand Up @@ -236,9 +248,10 @@ so let's not get too insert-happy."
(if (looking-back ".")
(insert ".")
(insert " . ")))
;; if in empty line, insert "." and indent
(t
(electric-spacing-insert "." 'after)
(insert " "))))
(electric-spacing-insert "." 'middle)
(indent-according-to-mode))))

(defun electric-spacing-& ()
"See `electric-spacing-insert'."
Expand Down Expand Up @@ -383,7 +396,10 @@ so let's not get too insert-happy."
(looking-at "#!")))
(insert "/"))
(t
(electric-spacing-insert "/"))))
(electric-spacing-insert "/")
;; Fix indentation in JavaScript modes
(when (derived-mode-p 'js2-mode 'js-mode)
(indent-according-to-mode)))))


(defun electric-spacing-enclosing-paren ()
Expand Down