Advertisement
Guest User

Insert missing whitespace in JS mode

a guest
Jun 5th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.69 KB | None | 0 0
  1. (defun js-fix-spacing-line ()
  2.   (interactive)
  3.   (let ((current (char-after))
  4.     (next (char-after 1))
  5.     (old-point (point))
  6.     (postfix)
  7.     (difference 0))
  8.     (labels
  9.      ((incf-dif
  10.        ()
  11.        (when (< 0 (+ difference (- old-point (point))))
  12.      (incf difference))
  13.        (insert " "))
  14.       (js-face
  15.        (&optional (pos (point)))
  16.        (get-char-property pos 'face)))
  17.      (beginning-of-line)
  18.      (while (not (eolp))
  19.        (setf current (char-after (1- (point)))
  20.          next (char-after))
  21.        (cond
  22.     ((or (char-equal current ?\ )
  23.          (char-equal current ?\t)
  24.          (char-equal next ?\ )
  25.          (char-equal next ?\t)))
  26.     ((and (equal (js-face (1- (point))) 'font-lock-string-face)
  27.           (equal (js-face) 'font-lock-string-face)))
  28.     ((and (equal (js-face (1- (point))) 'font-lock-comment-face)
  29.           (equal (js-face) 'font-lock-comment-face)))
  30.     ((and (equal (js-face (1- (point))) 'font-lock-keyword-face)
  31.           (not (equal (js-face)
  32.               'font-lock-keyword-face))
  33.           (not (position current " \t")))
  34.      (incf-dif))
  35.     ((position next "=/|&%?><*^:}")
  36.      (unless (and (position current "=!&|*+-^><") (not postfix))
  37.        (setf postfix nil)
  38.        (incf-dif)))
  39.     ((position current "?:,{});,")
  40.      (unless (char-equal next ?\;)
  41.        (unless (and (char-equal current ?})
  42.             (char-equal next ?\,))
  43.          (unless (and (char-equal current ?\))
  44.               (or (char-equal next ?\))
  45.                   (char-equal next ?\.)
  46.                   (char-equal next ?\[)))
  47.            (incf-dif)))))
  48.     ((char-equal current ?=)
  49.      (unless (position next "=")
  50.        (incf-dif)))
  51.     ((position current "<>&|*/%^")
  52.      (unless (and (char-equal next ?=)
  53.               (and (char-equal current ?&)
  54.                (char-equal next ?&))
  55.               (and (char-equal current ?|)
  56.                (char-equal next ?|)))
  57.        (incf-dif)))
  58.     ((position next "!~")
  59.      (unless (position current "([")
  60.        (incf-dif)))
  61.     ((char-equal next ?+)
  62.      (cond
  63.       ((char-equal current ?+))
  64.       ((looking-back "\\(\\sw\\|\\s_\\|\\]\\)\\s-*")
  65.        (setf postfix t))
  66.       (t (incf-dif))))
  67.     ((char-equal next ?-)
  68.      (cond
  69.       ((char-equal current ?-))
  70.       ((looking-back "\\(\\sw\\|\\s_\\|\\]\\)\\s-*")
  71.        (setf postfix t))
  72.       (t (incf-dif))))
  73.     ((char-equal current ?+)
  74.      (unless (position (char-before 1) "+;=/&%?><*:,{}[]")
  75.          (unless (position next ")]}")
  76.            (unless (looking-back "[=><?;/&%*,+|:]\\s-*+")
  77.          (incf-dif)))))
  78.     ((char-equal current ?-)
  79.      (unless (position (char-before 1) "-;=/&%?><*:,{}[]")
  80.          (unless (position next ")]}")
  81.            (unless (looking-back "[=><?;/&%*,+|:]\\s-*-")
  82.          (incf-dif))))))
  83.        (forward-char)))
  84.     (goto-char (+ old-point difference))))
  85.  
  86. (add-hook
  87.  'js-mode-hook
  88.  (lambda ()
  89.    (local-set-key (kbd "C-x SPC") 'js-fix-spacing-line)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement