Pin ELPA package versions
[~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(require 'cl-lib)
28
29(defmacro csetq (&rest args)
30 "Set the value of user option VAR to VALUE.
31
32More generally, you can use multiple variables and values, as in
33 (csetq VAR VALUE VAR VALUE...)
34This sets each user option VAR's value to the corresponding VALUE.
35
36\(fn [VAR VALUE]...)"
37 (declare (debug setq))
38 `(progn
39 ,@(cl-loop for (var value) on args by 'cddr
40 collect
41 `(funcall (or (get ',var 'custom-set) #'set-default)
42 ',var ,value))))
43
44(defun b/start-process (program &rest args)
45 "Same as `start-process', but doesn't bother about name and buffer."
46 (let ((process-name (concat program "_process"))
47 (buffer-name (generate-new-buffer-name
48 (concat program "_output"))))
49 (apply #'start-process
50 process-name buffer-name program args)))
51
52(defun b/dired-start-process (program &optional args)
53 "Open current file with a PROGRAM."
54 ;; Shell command looks like this: "program [ARGS]... FILE" (ARGS can
55 ;; be nil, so remove it).
56 (declare-function dired-get-file-for-visit "dired")
57 (apply #'b/start-process
58 program
59 (remove nil (list args (dired-get-file-for-visit)))))
60
61(defun b/add-elisp-section ()
62 (interactive)
63 (insert "\n")
64 (forward-line -1)
65 (insert "\n\f\n;;; "))
66
67;; (defvar b/fill-column 47
68;; "My custom `fill-column'.")
69
70(defconst b/asterism "* * *")
71
72(defun b/insert-asterism ()
73 "Insert a centred asterism."
74 (interactive)
75 (insert
76 (concat
77 "\n"
78 (make-string (floor (/ (- fill-column (length b/asterism)) 2))
79 ?\s)
80 b/asterism
81 "\n")))
82
83(defun b/no-mouse-autoselect-window ()
84 "Conveniently disable `focus-follows-mouse'.
85For disabling the behaviour for certain buffers and/or modes."
86 (make-local-variable 'mouse-autoselect-window)
87 (setq mouse-autoselect-window nil))
88
89(defun b/kill-current-buffer ()
90 "Kill the current buffer."
91 ;; also see https://redd.it/64xb3q
92 (interactive)
93 (kill-buffer (current-buffer)))
94
95(defun b/move-indentation-or-beginning-of-line (arg)
96 "Move to the indentation or to the beginning of line."
97 (interactive "^p")
98 ;; (if (bolp)
99 ;; (back-to-indentation)
100 ;; (move-beginning-of-line arg))
101 (if (= (point)
102 (progn (back-to-indentation)
103 (point)))
104 (move-beginning-of-line arg)))
105
106(defun b/join-line-top ()
107 "Like `join-line', but join next line to the current line."
108 (interactive)
109 (join-line 1))
110
111(defun b/duplicate-line-or-region (&optional n)
112 "Duplicate the current line, or region (if active).
113Make N (default: 1) copies of the current line or region."
114 (interactive "*p")
115 (let ((u-r-p (use-region-p)) ; if region is active
116 (n1 (or n 1)))
117 (save-excursion
118 (let ((text
119 (if u-r-p
120 (buffer-substring (region-beginning) (region-end))
121 (prog1 (thing-at-point 'line)
122 (end-of-line)
123 (if (eobp)
124 (newline)
125 (forward-line 1))))))
126 (dotimes (_ (abs n1))
127 (insert text))))))
128
129(provide 'bandali-utils)
130;;; bandali-utils.el ends here