Emacs笔记


Linux

emacs gui进程放置到后台

 1EMACS=/usr/bin/emacs
 2GUI=0
 3ARGS="$@"
 4ARRAY=("-nw" "--no-window-system" "--batch" "--help")
 5
 6for arg in $ARGS; do
 7    for keyword in "${ARRAY[@]}";do
 8        if [ $keyword = $arg ];then
 9            GUI=1
10            break
11        fi
12    done
13    if [ $GUI -eq 1 ]; then
14        break
15    fi
16done
17
18if [ $GUI -eq 1 ]; then
19    $EMACS $ARGS
20else
21    $EMACS $ARGS &> /dev/null &
22    # nohup $EMACS $ARGS &> /dev/null & disown
23fi

Evil

粘贴替换的时候会复制替换的文本

1(defun evil-paste-after-from-0 ()
2  (interactive)
3  (let ((evil-this-register ?0))
4    (call-interactively 'evil-paste-after)))
5
6(define-key evil-visual-state-map "p" 'evil-paste-after-from-0)

自动搜索已选中的字符

经常使用 evil 中的 / 进行搜索, 但有时候想要搜索已有的字符, 在 evil-visual-state 下选中某个字符,按 / 时自动把选中的字符填入待搜索项中

 1(use-package isearch
 2  :ensure nil
 3  :init
 4  (defun maple/evil-search-paste()
 5    (when (region-active-p)
 6      (isearch-yank-string
 7       (save-excursion
 8         (buffer-substring-no-properties
 9          (region-beginning) (1+ (region-end)))))
10      (deactivate-mark)))
11  :hook (isearch-mode . maple/evil-search-paste)
12  :bind (:map isearch-mode-map
13              ([remap isearch-delete-char] . isearch-del-char)))

evil-mc不忽略大小写

evil-mc 默认会通过evil-ex-search进行搜索,可以通过设置(setq evil-ex-search-case 'sensitive)来不忽略大小写, 但我只想在使用evil-mc时不忽略,其他时间忽略

1;; ignore case in evil-mc
2(add-hook 'evil-mc-before-cursors-created (lambda() (setq evil-ex-search-case 'sensitive)))
3(add-hook 'evil-mc-after-cursors-deleted (lambda() (setq evil-ex-search-case 'smart)))

Font

设置中英文字体

 1;; 中英文表格对齐
 2(defvar emacs-english-font "DejaVu Sans Mono"
 3  "The font name of English.")
 4(defvar emacs-cjk-font "WenQuanYi Micro Hei Mono"
 5  "The font name for CJK.")
 6(defvar emacs-font-size-pair '(15 . 18)
 7  "Default font size pair for (english . chinese)")
 8
 9(defvar emacs-font-size-pair-list
10  '(( 5 .  6) (10 . 12)
11    (13 . 16) (15 . 18) (17 . 20)
12    (19 . 22) (20 . 24) (21 . 26)
13    (24 . 28) (26 . 32) (28 . 34)
14    (30 . 36) (34 . 40) (36 . 44))
15  "This list is used to store matching (englis . chinese) font-size.")
16(defun font-exist-p (fontname)
17  "Test if this font is exist or not."
18  (if (or (not fontname) (string= fontname ""))
19      nil
20    (if (not (x-list-fonts fontname)) nil t)))
21
22(defun maple/set-font (english chinese size-pair)
23  "Setup emacs English and Chinese font on x window-system."
24  (if (font-exist-p english)
25      (set-frame-font (format "%s:pixelsize=%d" english (car size-pair)) t))
26
27  (if (font-exist-p chinese)
28      (dolist (charset '(kana han symbol cjk-misc bopomofo))
29        (set-fontset-font (frame-parameter nil 'font) charset
30                          (font-spec :family chinese :size (cdr size-pair))))))
31;; (add-hook 'ctbl:table-mode-hook
32;;           (maple/set-font emacs-english-font emacs-cjk-font emacs-font-size-pair))

Ivy

指定目录或文件进行搜索

1(defun maple/counsel-ag-file()
2  (interactive)
3  (counsel-ag nil (read-file-name "Search in file(s): ")))

查找当前目录以及子目录的某个文件

 1(defun maple/counsel-find-file()
 2  (interactive)
 3  (ivy-read "Find file: "
 4            (mapcar 'file-relative-name
 5                    (directory-files-recursively default-directory ""))
 6            :matcher #'counsel--find-file-matcher
 7            :action #'counsel-find-file-action
 8            :preselect (counsel--preselect-file)
 9            :require-match 'confirm-after-completion
10            :history 'file-name-history
11            :keymap counsel-find-file-map
12            :caller 'counsel-find-file))

当选中单词时,swiper使用已选单词进行搜索

1(defun maple/ivy-search-at-point (func)
2  (let* ((region (region-active-p))
3         (string (if (not region) ""
4                   (buffer-substring-no-properties
5                    (region-beginning) (region-end))))
6         (ivy-initial-inputs-alist
7          (list (cons func string))))
8    (when region (deactivate-mark))
9    (funcall func)))
1(defun maple/swiper()
2  (interactive)
3  (maple/ivy-search-at-point 'swiper))

File

获取文件或目录的basename

1(defun maple/basename (fname)
2  (if (or (file-directory-p fname)
3          (string-match "/$" fname))
4      (let ((dirname (directory-file-name fname)))
5        (file-name-nondirectory dirname))
6    (file-name-nondirectory fname)))

Elisp

golang自动增加注释

因为flycheck使用了golint, flycheck总是显示代码没有注释的警告

1exported method Raw should have comment or be unexported (go-golint)

也没找到什么方法来禁止它, 作为强迫症受不了,总不能一个一个去注释, 所以写了一个函数来自动注释整个文件的所有未注释函数:

 1(defun maple/go-auto-comment()
 2  (interactive)
 3  (unless (featurep 'imenu)
 4    (require 'imenu nil t))
 5  (let* ((imenu-auto-rescan t)
 6         (imenu-auto-rescan-maxout (if current-prefix-arg
 7                                       (buffer-size)
 8                                     imenu-auto-rescan-maxout))
 9         (items (imenu--make-index-alist t))
10         (items (delete (assoc "*Rescan*" items) items)))
11    (cl-mapcan
12     (lambda(item)
13       (cl-mapcan
14        (if (string= (car item) "func")
15            'maple/go-func-comment
16          'maple/go-type-comment)
17        (cdr item)))
18     items)))
19
20(defun maple/go-add-comment(func point)
21  (save-excursion
22    (goto-char point)
23    (forward-line -1)
24    (when (not (looking-at (concat "// " func)))
25      (end-of-line) (newline-and-indent)
26      (insert (concat "// " func " ..")))))
27
28(defun maple/go-func-comment(f)
29  (let ((func (car f)))
30    (if (and (string-prefix-p "(" func)
31             (string-match "[)] \\(.*\\)[(]\\(.*\\)[)]\\(.*\\)$" func))
32        (maple/go-add-comment (match-string 1 func) (cdr f))
33      (if (string-match "\\(.*\\)[(]\\(.*\\)[)]\\(.*\\)$" func)
34          (maple/go-add-comment (match-string 1 func) (cdr f))
35        (maple/go-add-comment (car f) (cdr f))))))
36
37(defun maple/go-type-comment(f)
38  (maple/go-add-comment (car f) (cdr f)))

相关配置将持续更新: init-go.el

增加到hook但仅调用一次

1(defmacro maple/add-hook-once (hook f &optional append local)
2  "Like `add-hook`, remove after call with HOOK F &OPTIONAL APPEND LOCAL."
3  (let ((func (intern (format "maple/run-once-%s"
4                              (symbol-name f)))))
5    `(progn
6       (defun ,func ()
7         (remove-hook ',hook ',func ,local)
8         (funcall ',f))
9       (add-hook ',hook ',func ,append ,local))))

迭代并获取列表索引

1(defmacro maple/dolist (spec &rest body)
2  "Like dolist but get INDEX, SPEC &REST BODY."
3  (declare (indent 1) (debug ((symbolp form &optional form) body)))
4  `(let ((num 0))
5     (dolist ,(cdr spec)
6       (let ((,(car spec) num))
7         ,@body
8         (setq num (+ num 1))))))

这样就可以使用

1(maple/dolist (index item '("aaa" "bbb"))
2              (print index))

深拷贝function

https://www.gnu.org/software/emacs/manual/html_node/elisp/Function-Cells.html

1(fset 'maple/put-text-property (symbol-function 'put-text-property))

获取文件行数

作者: honmaple
链接: https://honmaple.me/articles/2017/04/emacs-note.html
版权: CC BY-NC-SA 4.0 知识共享署名-非商业性使用-相同方式共享4.0国际许可协议
wechat
alipay

加载评论