summaryrefslogtreecommitdiffstats
path: root/lisp/bandali-utils.el
blob: 95c287810c34b32e67291cedeff1336e6a018ea8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
;;; bandali-utils.el --- useful utilities            -*- lexical-binding: t; -*-

;; Copyright (C) 2020  Amin Bandali

;; Author: Amin Bandali <bandali@gnu.org>
;; Keywords: lisp, tools

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; A small collection of useful utilities used through my init files.

;;; Code:

(defmacro b/setq-every (value &rest vars)
  "Set all the variables from VARS to value VALUE."
  (declare (indent defun) (debug t))
  `(progn ,@(mapcar (lambda (x) (list 'setq x value)) vars)))

(defun b/start-process (program &rest args)
  "Same as `start-process', but doesn't bother about name and buffer."
  (let ((process-name (concat program "_process"))
        (buffer-name  (generate-new-buffer-name
                       (concat program "_output"))))
    (apply #'start-process
           process-name buffer-name program args)))

(defun b/dired-start-process (program &optional args)
  "Open current file with a PROGRAM."
  ;; Shell command looks like this: "program [ARGS]... FILE" (ARGS can
  ;; be nil, so remove it).
  (declare-function dired-get-file-for-visit "dired")
  (apply #'b/start-process
         program
         (remove nil (list args (dired-get-file-for-visit)))))

(defun b/add-elisp-section ()
  (interactive)
  (insert "\n")
  (forward-line -1)
  (insert "\n\n;;; "))

;; (defvar b/fill-column 47
;;   "My custom `fill-column'.")

(defconst b/asterism "* * *")

(defun b/insert-asterism ()
  "Insert a centred asterism."
  (interactive)
  (insert
   (concat
    "\n"
    (make-string (floor (/ (- fill-column (length b/asterism)) 2))
                 ?\s)
    b/asterism
    "\n")))

(defun b/no-mouse-autoselect-window ()
  "Conveniently disable `focus-follows-mouse'.
For disabling the behaviour for certain buffers and/or modes."
  (make-local-variable 'mouse-autoselect-window)
  (setq mouse-autoselect-window nil))

(defun b/kill-current-buffer ()
  "Kill the current buffer."
  ;; also see https://redd.it/64xb3q
  (interactive)
  (kill-buffer (current-buffer)))

(defun b/move-indentation-or-beginning-of-line (arg)
  "Move to the indentation or to the beginning of line."
  (interactive "^p")
  ;; (if (bolp)
  ;;     (back-to-indentation)
  ;;   (move-beginning-of-line arg))
  (if (= (point)
         (progn (back-to-indentation)
                (point)))
      (move-beginning-of-line arg)))

(defun b/join-line-top ()
  "Like `join-line', but join next line to the current line."
  (interactive)
  (join-line 1))

(defun b/duplicate-line-or-region (&optional n)
  "Duplicate the current line, or region (if active).
Make N (default: 1) copies of the current line or region."
  (interactive "*p")
  (let ((u-r-p (use-region-p))          ; if region is active
        (n1 (or n 1)))
    (save-excursion
      (let ((text
             (if u-r-p
                 (buffer-substring (region-beginning) (region-end))
               (prog1 (thing-at-point 'line)
                 (end-of-line)
                 (if (eobp)
                     (newline)
                   (forward-line 1))))))
        (dotimes (_ (abs n1))
          (insert text))))))

(provide 'bandali-utils)
;;; bandali-utils.el ends here