diff options
Diffstat (limited to 'lisp/+util.el')
-rw-r--r-- | lisp/+util.el | 94 |
1 files changed, 0 insertions, 94 deletions
diff --git a/lisp/+util.el b/lisp/+util.el deleted file mode 100644 index a87eae9..0000000 --- a/lisp/+util.el +++ /dev/null | |||
@@ -1,94 +0,0 @@ | |||
1 | ;;; +util.el --- utility whatevers -*- lexical-binding: t -*- | ||
2 | |||
3 | ;;; Commentary: | ||
4 | |||
5 | ;; This file is going to be my version of like, subr.el -- lots of | ||
6 | ;; random shit that all goes in here. | ||
7 | |||
8 | ;;; Code: | ||
9 | |||
10 | (require 'cl-lib) | ||
11 | |||
12 | (defgroup +util nil | ||
13 | "Utility whatevers." | ||
14 | :group 'convenience) | ||
15 | |||
16 | ;;; STRINGS | ||
17 | |||
18 | (defcustom +string-default-alignment 'left | ||
19 | "Default alignment." | ||
20 | :type '(choice (const :tag "Left" 'left) | ||
21 | (const :tag "Right" 'right))) | ||
22 | |||
23 | ;; stolen from s.el | ||
24 | (defun +string-repeat (n s) | ||
25 | "Make a string of S repeated N times." | ||
26 | (declare (pure t) | ||
27 | (side-effect-free t)) | ||
28 | (let (ss) | ||
29 | (while (> n 0) | ||
30 | (setq ss (cons s ss) | ||
31 | n (1- n))) | ||
32 | (apply 'concat ss))) | ||
33 | |||
34 | (defun +string-truncate (s length &optional ellipsis alignment) | ||
35 | "Return S, shortened to LENGTH including ELLIPSIS and aligned to ALIGNMENT. | ||
36 | |||
37 | ELLIPSIS defaults to `truncate-string-ellipsis', or \"...\". | ||
38 | |||
39 | ALIGNMENT defaults to `+string-default-alignment'." | ||
40 | (declare (pure t) | ||
41 | (side-effect-free t)) | ||
42 | (let ((ellipsis (or ellipsis truncate-string-ellipsis "...")) | ||
43 | (alignment (or alignment +string-default-alignment))) | ||
44 | (if (> (length s) length) | ||
45 | (format "%s%s" | ||
46 | (substring s 0 (- length (length ellipsis))) | ||
47 | ellipsis) | ||
48 | s))) | ||
49 | |||
50 | (cl-defun +string-align (s len | ||
51 | &key | ||
52 | (before "") (after "") (fill " ") | ||
53 | (ellipsis (or truncate-string-ellipsis "...")) | ||
54 | (alignment +string-default-alignment)) | ||
55 | "Print S to fit in LEN characters. | ||
56 | Optional arguments BEFORE and AFTER specify strings to go on | ||
57 | either side of S. | ||
58 | |||
59 | FILL is the string to fill extra space with (default \" \"). | ||
60 | |||
61 | ELLIPSIS is the string to show when S is too long to fit (default | ||
62 | `truncate-string-ellipsis' or \"...\"). If nil, don't truncate | ||
63 | the string. | ||
64 | |||
65 | ALIGNMENT can be one of these: | ||
66 | - nil: align to `+string-default-alignment' | ||
67 | - `left': align left | ||
68 | - `right': align right" | ||
69 | (let* ((s-length (length s)) | ||
70 | (before-length (length before)) | ||
71 | (after-length (length after)) | ||
72 | (max-length (- len (+ before-length after-length))) | ||
73 | (left-over (max 0 (- max-length s-length))) | ||
74 | (filler (+string-repeat left-over fill))) | ||
75 | (format "%s%s%s%s%s" | ||
76 | before | ||
77 | (if (eq alignment 'left) "" filler) | ||
78 | (if ellipsis (+string-truncate s max-length ellipsis alignment) s) | ||
79 | (if (eq alignment 'right) "" filler) | ||
80 | after))) | ||
81 | |||
82 | ;;; COMMANDS | ||
83 | |||
84 | (defun +dos2unix (buffer) | ||
85 | "Replace \r\n with \n in BUFFER." | ||
86 | (interactive "*b") | ||
87 | (save-excursion | ||
88 | (with-current-buffer buffer | ||
89 | (goto-char (point-min)) | ||
90 | (while (search-forward (string ?\C-m ?\C-j) nil t) | ||
91 | (replace-match (string ?\C-j) nil t))))) | ||
92 | |||
93 | (provide '+util) | ||
94 | ;;; +util.el ends here | ||