diff options
Diffstat (limited to 'lisp/pita.el')
-rw-r--r-- | lisp/pita.el | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/lisp/pita.el b/lisp/pita.el new file mode 100644 index 0000000..92ebf1b --- /dev/null +++ b/lisp/pita.el | |||
@@ -0,0 +1,64 @@ | |||
1 | ;;; pita.el --- wrappers making emacs less of a PITA -*- lexical-binding: t -*- | ||
2 | ;; 🥙 | ||
3 | |||
4 | (defmacro with-message (msg &rest body) | ||
5 | (declare (indent 1)) | ||
6 | (when (listp msg) | ||
7 | (setq msg (apply #'format (car msg) (cdr msg)))) | ||
8 | (when (string-match "[[:alnum:]]\\'" msg) | ||
9 | (setq msg (concat msg "..."))) | ||
10 | (let ((m (gensym)) | ||
11 | (r (gensym))) | ||
12 | `(let ((,m ,msg) | ||
13 | (,r nil)) | ||
14 | (condition-case e | ||
15 | (setq r (progn (message ,m) ,@body)) | ||
16 | (:success (message "%s done" ,m) r) | ||
17 | (t (signal (car e) (cdr e))))))) | ||
18 | |||
19 | (defun walk-tree-replace (tree find replace) | ||
20 | (let ((r nil)) | ||
21 | (dolist (form tree) | ||
22 | (push (cond ((eq find form) replace) | ||
23 | ((listp form) | ||
24 | (walk-tree-replace form find replace)) | ||
25 | (t form)) | ||
26 | r)) | ||
27 | (reverse r))) | ||
28 | |||
29 | (defmacro with-pr (msg &rest body) | ||
30 | (declare (indent 1)) | ||
31 | (when (listp msg) | ||
32 | (setq msg (apply #'format (car msg) (cdr msg)))) | ||
33 | (when (string-match "[[:alnum:]]\\'" msg) | ||
34 | (setq msg (concat msg "..."))) | ||
35 | (let ((pr (gensym)) | ||
36 | (m (gensym))) | ||
37 | `(let* ((,m ,msg) | ||
38 | (,pr (unless (minibufferp) | ||
39 | (make-progress-reporter ,m)))) | ||
40 | ,@(or (and pr (walk-tree-replace body '@ `(progress-reporter-update ,pr))) | ||
41 | body) | ||
42 | (and ,pr (progress-reporter-done ,pr))))) | ||
43 | |||
44 | |||
45 | ;;; crux advices | ||
46 | ;; these should all go :before the function they're advising. | ||
47 | |||
48 | (defun with-region-or-buffer (&rest _) | ||
49 | (interactive (if mark-active | ||
50 | (list (region-beginning) (region-end)) | ||
51 | (list (point-min) (point-max))))) | ||
52 | |||
53 | (defun with-region-or-line (&rest _) | ||
54 | (interactive (if mark-active | ||
55 | (list (region-beginning) (region-end)) | ||
56 | (list (line-beginning-position) (line-end-position))))) | ||
57 | |||
58 | (defun with-region-or-to-eol (&rest _) | ||
59 | (interactive (if mark-active | ||
60 | (list (region-beginning) (region-end)) | ||
61 | (list (point) (line-end-position))))) | ||
62 | |||
63 | (provide 'pita) | ||
64 | ;;; pita.el ends here | ||