dotemacs/contrapunctus/cp.el
2018-09-04 11:36:48 +05:30

145 lines
5.7 KiB
EmacsLisp

(defun cp-global-set-many-keys (bindings)
"A clean way to define many keys using `global-set-key'.
BINDINGS must be in the form `((,(kbd \"...\") FN) ...)"
(mapc (lambda (b)
(global-set-key (car b) (cadr b)))
bindings))
(defun cp-define-many-keys (keymap bindings)
"A clean way to define many keys using `define-key'.
KEYMAP must be a valid keymap.
BINDINGS must be in the form `((,(kbd \"...\") FN) ...)"
(mapc (lambda (b)
(define-key keymap (car b) (cadr b)))
bindings))
(defun cp-local-set-many-keys (bindings)
"A clean way to define many keys using `local-set-key'.
BINDINGS must be in the form `((,(kbd \"...\") FN) ...)"
(mapc (lambda (b)
(local-set-key (car b) (cadr b)))
bindings))
(require 'cl-lib)
(cl-defun cp-set-keys (&key keymap unset bindings)
"A clean way to set/unset many keybindings.
BINDINGS specifies the functions and the keys they are to be
bound to. It must be an alist in the form -
`((,(kbd \"...\") FN)
...)
If KEYMAP is a symbol `global' or not supplied, the binding is
created by `global-set-key'.
If it is a symbol `local', the binding is created by
`local-set-key'.
Otherwise, it is assumed to be a keymap and the binding is
created with `define-key'.
If UNSET is non-nil, unset keybinds as specified by KEYMAP
instead of setting them. If a keymap is specified, the key is unset
by using define-key to set it to nil in the given keymap -
otherwise, global-unset-key or local-unset-key are used as
applicable."
(let ((key-function (if (not unset)
(pcase keymap
((or `global `nil) #'global-set-key)
(`local #'local-set-key)
(x #'define-key))
(pcase keymap
((or `global `nil) #'global-unset-key)
(`local #'local-unset-key)
(x #'define-key)))))
(if (eq key-function 'define-key)
(if unset
(mapc (lambda (b)
(funcall key-function keymap (car b) nil))
bindings)
(mapc (lambda (b)
(funcall key-function keymap (car b) (cadr b)))
bindings))
(if unset
(mapc (lambda (b)
(funcall key-function (car b)))
bindings)
(mapc (lambda (b)
(funcall key-function (car b) (cadr b)))
bindings)))))
;; ;; tests
;; (progn (cp-set-keys :keymap 'global ; not actually needed, e.g. see next call
;; :bindings `((,(kbd "<f6>") erc) (,(kbd "<f7>") eww)))
;; (and (equal 'erc (lookup-key (current-global-map) (kbd "<f6>")))
;; (equal 'eww (lookup-key (current-global-map) (kbd "<f7>")))))
;; (progn (cp-set-keys :unset t
;; :bindings `((,(kbd "<f6>") erc) (,(kbd "<f7>") eww)))
;; (and (equal nil (lookup-key (current-global-map) (kbd "<f6>")))
;; (equal nil (lookup-key (current-global-map) (kbd "<f7>")))))
;; (progn (cp-set-keys :keymap emacs-lisp-mode-map
;; :bindings `((,(kbd "<f6>") erc) (,(kbd "<f7>") eww)))
;; (and (equal 'erc (lookup-key emacs-lisp-mode-map (kbd "<f6>")))
;; (equal 'eww (lookup-key emacs-lisp-mode-map (kbd "<f7>")))))
;; (progn (cp-set-keys :keymap emacs-lisp-mode-map
;; :unset t
;; :bindings `((,(kbd "<f6>") erc) (,(kbd "<f7>") eww)))
;; (and (equal nil (lookup-key emacs-lisp-mode-map (kbd "<f6>")))
;; (equal nil (lookup-key emacs-lisp-mode-map (kbd "<f7>")))))
;; (progn (cp-set-keys :keymap 'local
;; :bindings `((,(kbd "<f6>") erc) (,(kbd "<f7>") eww)))
;; (and (equal 'erc (lookup-key emacs-lisp-mode-map (kbd "<f6>")))
;; (equal 'eww (lookup-key emacs-lisp-mode-map (kbd "<f7>")))))
;; (progn (cp-set-keys :keymap 'local
;; :unset t
;; :bindings `((,(kbd "<f6>") erc) (,(kbd "<f7>") eww)))
;; (and (equal nil (lookup-key emacs-lisp-mode-map (kbd "<f6>")))
;; (equal nil (lookup-key emacs-lisp-mode-map (kbd "<f7>")))))
(defun cp-get-buffer-regexp (regexp &optional all)
"Returns the name of the first buffer in the buffer list which matches REGEXP.
If ALL is non-nil, multiple matches will be returned as a list."
(let ((results (cl-remove-if-not (lambda (a)
(string-match regexp a))
(mapcar #'buffer-name (buffer-list)))))
(if all results (car results))))
(cl-defun cp-buffer-name-match-p (regexp &optional (string (buffer-name)))
"Checks if STRING matches REGEXP.
If STRING is not provided, uses the current buffer name."
(if (string-match regexp string) t nil))
(defun cp/random-elt (list)
"Retun a random element from LIST."
(nth (random (length list)) list))
(defun cp/map-two (fn list)
"Apply FN to the first two elements of LIST, then the second two, and so on."
(let ((newlist '()))
(while (not (= 0 (- (length list) 1)))
(setq newlist
(append
newlist (list
(funcall fn (car list) (cadr list)))))
(pop list))
newlist))
(defun cp/average (numbers)
(/ (-reduce '+ numbers)
(length numbers)))
(defun cp/pastebinit ()
(interactive)
(-> (shell-command-on-region
(if (region-active-p) (region-beginning) (point-min))
(if (region-active-p) (region-end) (point-max))
"pastebinit")
(kill-new)))
(defun ^ (base pwr)
(while (not (zerop pwr))
(setq base (* base base)
pwr (- pwr 1)))
base)
(provide 'cp)