My Avatar

LanternD's Castle

An electronics enthusiast - survive technically

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

1
2
3
4
5
6
;; 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

1
2
3
4
(defun hidden-dos-eol()
  (interactive)
  (setq buffer-display-table (make-display-table))
  (aset buffer-display-table ?\^M []))
1
2
3
4
5
;; 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
;; 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

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

Improve some built-in modes

Occur-mode

1
2
3
4
5
6
7
8
9
10
11
12
(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