My Avatar

LanternD's Castle

PhD Student in ECE @ MSU

Spacemacs Rocks Note Day 5

2017-10-27

A series of notes that I learn Emacs hacking. Parenthesis highlighting, web-mode for web development, js2-refactor, occur-mode, imenu-mode, expand-region, iedit-mode, org-mode export.

Highlight parenthesis within the bracket

;; Highlight the enclosing parenthensis while the cursor is inside it.
(define-advice show-paren-function (:around (fn) fix-show-paren-functioon)
  (cond ((looking-at-p "\\s(") (funcall fn))
	(t (save-excursion
	     (ignore-errors (backward-up-list))
	     (funcall fn)))))

Towards web development

DOS EOL processing

(defun hidden-dos-eol()
  (interactive)
  (setq buffer-display-table (make-display-table))
  (aset buffer-display-table ?\^M []))
;; Option 2: remove ^M
(defun remove-dos-eol()
  (interactive)
  (goto-char (point-min))
  (while (serach-forward "\r" nil t) (replace-match "")))

Deal with the toggle of the web-mode

;; Configuration for web-mode
;; set the indent to 2 by default.
(defun my-web-mode-indent-setup ()
  (setq web-mode-markup-indent-offset 2) ; html
  (setq web-mode-css-indent-offset 2) ; css
  (setq web-mode-code-indent-offset 2)) ; js

(add-hook 'web-mode-hook 'my-web-mode-indent-setup)
;; toggle between 2 indent and 4 indent
(defun my-toggle-web-indent ()
  (interactive)
  (if (or (eq major-mode 'js-mode) (eq major-mode 'js2-mode))
      (progn
	(setq js-indent-level (if (= js-indent-level 2) 4 2))
	(setq js2-basic-offset (if (= js2-basic-offset 2) 4 2))))
  (if (eq major-mode 'web-mode)
      (progn
	(setq web-mode-markup-indent-offset (if (= web-mode-markup-indent-offset 2) 4 2))
	(setq web-mode-css-indent-offset (if (= web-mode-css-indent-offset 2) 4 2))
	(setq web-mode-code-indent-offset (if (= web-mode-code-indent-offset 2) 4 2))))
  (if (eq major-mode 'css-mode)
      (setq css-indent-offset (if (= css-indent-offset 2) 4 2)))
  (setq indent-tabs-mode nil))

(global-set-key (kbd "C-c t i") 'my-toggle-web-indent)

Use js2-refactor to increase the development efficiency

(add-hook 'js2-mode-hook #'js2-refactro-mode)
(js2r-add-keybindings-with-prefix "C-c C-m")

Improve some built-in modes

Occur-mode

(defun occur-dwim ()
  (interactive)
  (push (if (region-active-p)
	    (buffer-substring-no-properties
	     (region-beginning)
	     (region-end))
	  (let ((sym (thing-at-point 'symbol)))
	    (when (stringp sym)
(regexp-quote sym))))
	regexp-history)
  (call-interactively 'occur))
(global-set-key (kbd "M-s o") 'occur-dwim)

imenu-mode

Expand-region and iedit-mode (Pretty useful)

expand-region

iedit-mode

Export in Org-mode



Disqus Comment 0