-
Notifications
You must be signed in to change notification settings - Fork 3
/
yangchenyun-util.el
89 lines (76 loc) · 2.64 KB
/
yangchenyun-util.el
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(defmacro require-and-exec (feature &rest body)
"Require the feature and execute body if it was successfull loaded."
(declare (indent defun))
`(if (require ,feature nil 'noerror)
(progn ,@body)
(message (format "%s not loaded" ,feature))))
(defun add-to-hooks (fun hooks)
"Add function to hooks"
(dolist (hook hooks)
(add-hook hook fun)))
(defun add-all-to-hook (hook &rest funs)
"Add functions to hook."
(add-to-hook hook funs))
(defun add-to-hook (hook funs)
"Add list of functions to hook."
(dolist (fun funs)
(add-hook hook fun)))
(defun group (lst n)
"Group `LST' into portions of `N'."
(let (groups)
(while lst
(push (take n lst) groups)
(setq lst (nthcdr n lst)))
(nreverse groups)))
(defun take (n lst)
"Return atmost the first `N' items of `LST'."
(let (acc '())
(while (and lst (> n 0))
(cl-decf n)
(push (car lst) acc)
(setq lst (cdr lst)))
(nreverse acc)))
;;; keybinding
(defun cofi/set-key (map spec cmd)
"Set in `map' `spec' to `cmd'.
`Map' may be `'global' `'local' or a keymap.
A `spec' can be a `read-kbd-macro'-readable string or a vector."
(let ((setter-fun (cl-case map
(global #'global-set-key)
(local #'local-set-key)
(t (lambda (key def) (define-key map key def)))))
(key (cl-typecase spec
(vector spec)
(string (read-kbd-macro spec))
(t (error "wrong argument")))))
(funcall setter-fun key cmd)))
(defun pour-mappings-to (map mappings)
"Calls `cofi/set-key' with `map' on every key-fun pair in `MAPPINGS'.
`MAPPINGS' is a list of string-fun pairs, with a `READ-KBD-MACRO'-readable string and a interactive-fun."
(dolist (mapping (group mappings 2))
(cofi/set-key map (car mapping) (cadr mapping)))
map)
(defmacro defkeymap (symbol &rest mappings)
"Define keymap bound to `symbol'.
See `pour-mappings-to'"
`(progn (unless (boundp ',symbol)
(defvar ,symbol (make-sparse-keymap)))
(fill-keymap ,symbol ,@mappings)))
(defun fill-keymap (keymap &rest mappings)
"Fill `KEYMAP' with `MAPPINGS'.
See `pour-mappings-to'."
(declare (indent defun))
(pour-mappings-to keymap mappings))
(defun fill-keymaps (keymaps &rest mappings)
"Fill `KEYMAPS' with `MAPPINGS'.
See `pour-mappings-to'."
(declare (indent defun))
(dolist (keymap keymaps keymaps)
(let ((map (if (symbolp keymap)
(symbol-value keymap)
keymap)))
(pour-mappings-to map mappings))))
(defun kill-current-buffer ()
(interactive)
(kill-buffer (current-buffer)))
(provide 'yangchenyun-util)