diff options
author | Case Duckworth | 2021-09-03 12:24:38 -0500 |
---|---|---|
committer | Case Duckworth | 2021-09-03 12:24:38 -0500 |
commit | 55962fcaf4f2b02a79f2644916af9bdaa8dca608 (patch) | |
tree | 9181fc7be1ea38b2bbbf47e7b0089f35661c3fb5 | |
parent | Change modeline (diff) | |
download | emacs-55962fcaf4f2b02a79f2644916af9bdaa8dca608.tar.gz emacs-55962fcaf4f2b02a79f2644916af9bdaa8dca608.zip |
Add `clone-buffer-write-file' and sort
-rw-r--r-- | lisp/acdw.el | 96 |
1 files changed, 57 insertions, 39 deletions
diff --git a/lisp/acdw.el b/lisp/acdw.el index ded65f8..8db7fea 100644 --- a/lisp/acdw.el +++ b/lisp/acdw.el | |||
@@ -50,6 +50,57 @@ ARG). When called with multiple arguments or a list, it returns | |||
50 | ;; I don't prefix these because ... reasons. Honestly I probably should prefix | 50 | ;; I don't prefix these because ... reasons. Honestly I probably should prefix |
51 | ;; them. | 51 | ;; them. |
52 | 52 | ||
53 | ;; Why isn't this a thing??? | ||
54 | (defmacro fbound-and-true-p (func) | ||
55 | "Return the value of function FUNC if it is bound, else nil." | ||
56 | `(and (fboundp ,func) ,func)) | ||
57 | |||
58 | (defmacro when-unfocused (name &rest forms) | ||
59 | "Define a function NAME, executing FORMS, that fires when Emacs | ||
60 | is unfocused." | ||
61 | (declare (indent 1)) | ||
62 | (let ((func-name (intern (concat "when-unfocused-" (symbol-name name))))) | ||
63 | `(progn | ||
64 | (defun ,func-name () "Defined by `when-unfocused'." | ||
65 | (when (seq-every-p #'null | ||
66 | (mapcar #'frame-focus-state (frame-list))) | ||
67 | ,@forms)) | ||
68 | (add-function :after after-focus-change-function #',func-name)))) | ||
69 | |||
70 | (defmacro with-eval-after-loads (files &rest body) | ||
71 | "Execute BODY after FILES are loaded. | ||
72 | This macro simplifies `with-eval-after-load' for multiple nested | ||
73 | features." | ||
74 | (declare (indent 1) (debug (form def-body))) | ||
75 | (waterfall-list 'with-eval-after-load files body)) | ||
76 | |||
77 | (defmacro with-message (message &rest body) | ||
78 | "Execute BODY, messaging 'MESSAGE...' before and 'MESSAGE... Done.' after." | ||
79 | (declare (indent 1)) | ||
80 | ;; Wrap a progn inside a prog1 to return the return value of the body. | ||
81 | `(prog1 | ||
82 | (progn (message "%s..." ,message) | ||
83 | ,@body) | ||
84 | (message "%s... Done." ,message))) | ||
85 | |||
86 | (defun clone-buffer-write-file (filename &optional confirm) | ||
87 | "Clone current buffer to a file named FILENAME and switch. | ||
88 | FILENAME and CONFIRM are passed directly to `write-file'." | ||
89 | (interactive ; stolen from `write-file' | ||
90 | (list (if buffer-file-name | ||
91 | (read-file-name "Write file: " | ||
92 | nil nil nil nil) | ||
93 | (read-file-name "Write file: " default-directory | ||
94 | (expand-file-name | ||
95 | (file-name-nondirectory (buffer-name)) | ||
96 | default-directory) | ||
97 | nil nil)) | ||
98 | (not current-prefix-arg))) | ||
99 | (let ((buf (clone-buffer nil nil))) | ||
100 | (with-current-buffer buf | ||
101 | (write-file filename confirm)) | ||
102 | (switch-to-buffer buf))) | ||
103 | |||
53 | (defun dos2unix (buffer) | 104 | (defun dos2unix (buffer) |
54 | "Replace \r\n with \n in BUFFER." | 105 | "Replace \r\n with \n in BUFFER." |
55 | (interactive "*b") | 106 | (interactive "*b") |
@@ -66,11 +117,6 @@ ARG). When called with multiple arguments or a list, it returns | |||
66 | file | 117 | file |
67 | nil))) | 118 | nil))) |
68 | 119 | ||
69 | ;; Why isn't this a thing??? | ||
70 | (defmacro fbound-and-true-p (func) | ||
71 | "Return the value of function FUNC if it is bound, else nil." | ||
72 | `(and (fboundp ,func) ,func)) | ||
73 | |||
74 | (defun kill-region-or-backward-word (arg) | 120 | (defun kill-region-or-backward-word (arg) |
75 | "Kill region if active, or backward word if not." | 121 | "Kill region if active, or backward word if not." |
76 | (interactive "p") | 122 | (interactive "p") |
@@ -80,6 +126,12 @@ ARG). When called with multiple arguments or a list, it returns | |||
80 | (paredit-backward-kill-word) | 126 | (paredit-backward-kill-word) |
81 | (backward-kill-word arg)))) | 127 | (backward-kill-word arg)))) |
82 | 128 | ||
129 | (defun unfill-buffer (&optional buffer-or-name) | ||
130 | (with-current-buffer (or buffer-or-name (current-buffer)) | ||
131 | (save-excursion | ||
132 | (save-restriction | ||
133 | (unfill-region (point-min) (point-max)))))) | ||
134 | |||
83 | ;; https://www.emacswiki.org/emacs/UnfillRegion | 135 | ;; https://www.emacswiki.org/emacs/UnfillRegion |
84 | (defun unfill-region (start end) | 136 | (defun unfill-region (start end) |
85 | "Unfill a region defined by START and END positions." | 137 | "Unfill a region defined by START and END positions." |
@@ -88,40 +140,6 @@ ARG). When called with multiple arguments or a list, it returns | |||
88 | (emacs-lisp-docstring-fill-column t)) | 140 | (emacs-lisp-docstring-fill-column t)) |
89 | (fill-region start end))) | 141 | (fill-region start end))) |
90 | 142 | ||
91 | (defun unfill-buffer (&optional buffer-or-name) | ||
92 | (with-current-buffer (or buffer-or-name (current-buffer)) | ||
93 | (save-excursion | ||
94 | (save-restriction | ||
95 | (unfill-region (point-min) (point-max)))))) | ||
96 | |||
97 | (defmacro when-unfocused (name &rest forms) | ||
98 | "Define a function NAME, executing FORMS, that fires when Emacs | ||
99 | is unfocused." | ||
100 | (declare (indent 1)) | ||
101 | (let ((func-name (intern (concat "when-unfocused-" (symbol-name name))))) | ||
102 | `(progn | ||
103 | (defun ,func-name () "Defined by `when-unfocused'." | ||
104 | (when (seq-every-p #'null | ||
105 | (mapcar #'frame-focus-state (frame-list))) | ||
106 | ,@forms)) | ||
107 | (add-function :after after-focus-change-function #',func-name)))) | ||
108 | |||
109 | (defmacro with-message (message &rest body) | ||
110 | "Execute BODY, messaging 'MESSAGE...' before and 'MESSAGE... Done.' after." | ||
111 | (declare (indent 1)) | ||
112 | ;; Wrap a progn inside a prog1 to return the return value of the body. | ||
113 | `(prog1 | ||
114 | (progn (message "%s..." ,message) | ||
115 | ,@body) | ||
116 | (message "%s... Done." ,message))) | ||
117 | |||
118 | (defmacro with-eval-after-loads (files &rest body) | ||
119 | "Execute BODY after FILES are loaded. | ||
120 | This macro simplifies `with-eval-after-load' for multiple nested | ||
121 | features." | ||
122 | (declare (indent 1) (debug (form def-body))) | ||
123 | (waterfall-list 'with-eval-after-load files body)) | ||
124 | |||
125 | (defun waterfall-list (car list rest) | 143 | (defun waterfall-list (car list rest) |
126 | "Cons CAR with each element in LIST in a waterfall fashion, end with REST. | 144 | "Cons CAR with each element in LIST in a waterfall fashion, end with REST. |
127 | For use with the `with-eval-after-loads' function." | 145 | For use with the `with-eval-after-loads' function." |