1 ;;; bandali-utils.el --- useful utilities -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2020 Amin Bandali
5 ;; Author: Amin Bandali <bandali@gnu.org>
6 ;; Keywords: lisp, tools
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.
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.
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/>.
23 ;; A small collection of useful utilities used through my init files.
29 (defmacro csetq
(&rest args
)
30 "Set the value of user option VAR to VALUE.
32 More generally, you can use multiple variables and values, as in
33 (csetq VAR VALUE VAR VALUE...)
34 This sets each user option VAR's value to the corresponding VALUE.
37 (declare (debug setq
))
39 ,@(cl-loop for
(var value
) on args by
'cddr
41 `(funcall (or (get ',var
'custom-set
) #'set-default
)
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
)))
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
59 (remove nil
(list args
(dired-get-file-for-visit)))))
61 (defun b/add-elisp-section
()
65 (insert "\n\f\n;;; "))
67 ;; (defvar b/fill-column 47
68 ;; "My custom `fill-column'.")
70 (defconst b
/asterism
"* * *")
72 (defun b/insert-asterism
()
73 "Insert a centred asterism."
78 (make-string (floor (/ (- fill-column
(length b
/asterism
)) 2))
83 (defun b/no-mouse-autoselect-window
()
84 "Conveniently disable `focus-follows-mouse'.
85 For disabling the behaviour for certain buffers and/or modes."
86 (make-local-variable 'mouse-autoselect-window
)
87 (setq mouse-autoselect-window nil
))
89 (defun b/kill-current-buffer
()
90 "Kill the current buffer."
91 ;; also see https://redd.it/64xb3q
93 (kill-buffer (current-buffer)))
95 (defun b/move-indentation-or-beginning-of-line
(arg)
96 "Move to the indentation or to the beginning of line."
99 ;; (back-to-indentation)
100 ;; (move-beginning-of-line arg))
102 (progn (back-to-indentation)
104 (move-beginning-of-line arg
)))
106 (defun b/join-line-top
()
107 "Like `join-line', but join next line to the current line."
111 (defun b/duplicate-line-or-region
(&optional n
)
112 "Duplicate the current line, or region (if active).
113 Make N (default: 1) copies of the current line or region."
115 (let ((u-r-p (use-region-p)) ; if region is active
120 (buffer-substring (region-beginning) (region-end))
121 (prog1 (thing-at-point 'line
)
125 (forward-line 1))))))
126 (dotimes (_ (abs n1
))
129 (provide 'bandali-utils
)
130 ;;; bandali-utils.el ends here