Update 5 drones
[~bandali/configs] / lisp / bandali-utils.el
... / ...
CommitLineData
1;;; bandali-utils.el --- useful utilities -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2020 Amin Bandali
4
5;; Author: Amin Bandali <bandali@gnu.org>
6;; Keywords: lisp, tools
7
8;; This program is free software; you can redistribute it and/or modify
9;; it under the terms of the GNU General Public License as published by
10;; the Free Software Foundation, either version 3 of the License, or
11;; (at your option) any later version.
12
13;; This program is distributed in the hope that it will be useful,
14;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;; GNU General Public License for more details.
17
18;; You should have received a copy of the GNU General Public License
19;; along with this program. If not, see <https://www.gnu.org/licenses/>.
20
21;;; Commentary:
22
23;; A small collection of useful utilities used through my init files.
24
25;;; Code:
26
27(defmacro b/setq-every (value &rest vars)
28 "Set all the variables from VARS to value VALUE."
29 (declare (indent defun) (debug t))
30 `(progn ,@(mapcar (lambda (x) (list 'setq x value)) vars)))
31
32(defun b/start-process (program &rest args)
33 "Same as `start-process', but doesn't bother about name and buffer."
34 (let ((process-name (concat program "_process"))
35 (buffer-name (generate-new-buffer-name
36 (concat program "_output"))))
37 (apply #'start-process
38 process-name buffer-name program args)))
39
40(defun b/dired-start-process (program &optional args)
41 "Open current file with a PROGRAM."
42 ;; Shell command looks like this: "program [ARGS]... FILE" (ARGS can
43 ;; be nil, so remove it).
44 (declare-function dired-get-file-for-visit "dired")
45 (apply #'b/start-process
46 program
47 (remove nil (list args (dired-get-file-for-visit)))))
48
49(defun b/add-elisp-section ()
50 (interactive)
51 (insert "\n")
52 (forward-line -1)
53 (insert "\n\f\n;;; "))
54
55;; (defvar b/fill-column 47
56;; "My custom `fill-column'.")
57
58(defconst b/asterism "* * *")
59
60(defun b/insert-asterism ()
61 "Insert a centred asterism."
62 (interactive)
63 (insert
64 (concat
65 "\n"
66 (make-string (floor (/ (- fill-column (length b/asterism)) 2))
67 ?\s)
68 b/asterism
69 "\n")))
70
71(defun b/no-mouse-autoselect-window ()
72 "Conveniently disable `focus-follows-mouse'.
73For disabling the behaviour for certain buffers and/or modes."
74 (make-local-variable 'mouse-autoselect-window)
75 (setq mouse-autoselect-window nil))
76
77(defun b/kill-current-buffer ()
78 "Kill the current buffer."
79 ;; also see https://redd.it/64xb3q
80 (interactive)
81 (kill-buffer (current-buffer)))
82
83(defun b/move-indentation-or-beginning-of-line (arg)
84 "Move to the indentation or to the beginning of line."
85 (interactive "^p")
86 ;; (if (bolp)
87 ;; (back-to-indentation)
88 ;; (move-beginning-of-line arg))
89 (if (= (point)
90 (progn (back-to-indentation)
91 (point)))
92 (move-beginning-of-line arg)))
93
94(defun b/join-line-top ()
95 "Like `join-line', but join next line to the current line."
96 (interactive)
97 (join-line 1))
98
99(defun b/duplicate-line-or-region (&optional n)
100 "Duplicate the current line, or region (if active).
101Make N (default: 1) copies of the current line or region."
102 (interactive "*p")
103 (let ((u-r-p (use-region-p)) ; if region is active
104 (n1 (or n 1)))
105 (save-excursion
106 (let ((text
107 (if u-r-p
108 (buffer-substring (region-beginning) (region-end))
109 (prog1 (thing-at-point 'line)
110 (end-of-line)
111 (if (eobp)
112 (newline)
113 (forward-line 1))))))
114 (dotimes (_ (abs n1))
115 (insert text))))))
116
117(provide 'bandali-utils)
118;;; bandali-utils.el ends here