1 aaf6cf56 2022-04-09 lh ;; -*- lexical-binding: t; -*-
3 aaf6cf56 2022-04-09 lh (defvar lh/filepath-delims
6 aaf6cf56 2022-04-09 lh (defun lh/region-bounds-full-lines (start end)
8 aaf6cf56 2022-04-09 lh (save-excursion
9 aaf6cf56 2022-04-09 lh (goto-char start)
10 aaf6cf56 2022-04-09 lh (line-beginning-position))
11 aaf6cf56 2022-04-09 lh (save-excursion
12 aaf6cf56 2022-04-09 lh (goto-char end)
13 aaf6cf56 2022-04-09 lh (line-end-position))))
15 aaf6cf56 2022-04-09 lh (defun lh/move-region-up ()
17 aaf6cf56 2022-04-09 lh (let* ((deactivate-mark nil)
18 aaf6cf56 2022-04-09 lh (bounds (lh/region-bounds-full-lines (region-beginning) (region-end)))
19 aaf6cf56 2022-04-09 lh (text (buffer-substring (car bounds) (cdr bounds))))
20 aaf6cf56 2022-04-09 lh (delete-region (car bounds) (cdr bounds))
21 aaf6cf56 2022-04-09 lh (transpose-lines 1)
22 aaf6cf56 2022-04-09 lh (forward-line -2)
23 aaf6cf56 2022-04-09 lh (set-mark (point))
25 aaf6cf56 2022-04-09 lh (activate-mark)))
27 aaf6cf56 2022-04-09 lh (defun lh/move-line-up ()
29 aaf6cf56 2022-04-09 lh (if (region-active-p)
30 aaf6cf56 2022-04-09 lh (lh/move-region-up)
31 aaf6cf56 2022-04-09 lh (transpose-lines 1)
32 aaf6cf56 2022-04-09 lh (forward-line -2)))
34 aaf6cf56 2022-04-09 lh (defun lh/move-region-down ()
36 aaf6cf56 2022-04-09 lh (let* ((deactivate-mark nil)
37 aaf6cf56 2022-04-09 lh (bounds (lh/region-bounds-full-lines (region-beginning) (region-end)))
38 aaf6cf56 2022-04-09 lh (text (buffer-substring (car bounds) (cdr bounds))))
39 aaf6cf56 2022-04-09 lh (delete-region (car bounds) (cdr bounds))
40 aaf6cf56 2022-04-09 lh (forward-line)
41 aaf6cf56 2022-04-09 lh (transpose-lines 1)
42 aaf6cf56 2022-04-09 lh (forward-line -1)
43 aaf6cf56 2022-04-09 lh (set-mark (point))
45 aaf6cf56 2022-04-09 lh (activate-mark)))
47 aaf6cf56 2022-04-09 lh (defun lh/move-line-down ()
49 aaf6cf56 2022-04-09 lh (if (region-active-p)
50 aaf6cf56 2022-04-09 lh (lh/move-region-down)
51 aaf6cf56 2022-04-09 lh (forward-line)
52 aaf6cf56 2022-04-09 lh (transpose-lines 1)
53 aaf6cf56 2022-04-09 lh (forward-line -1)))
55 aaf6cf56 2022-04-09 lh (defun lh/extern-open-file (file)
56 aaf6cf56 2022-04-09 lh (shell-command (concat "xdg-open " (shell-quote-argument file))))
58 aaf6cf56 2022-04-09 lh (defun lh/extern-open-current-file ()
60 aaf6cf56 2022-04-09 lh (let ((files (if (eq major-mode 'dired-mode)
61 aaf6cf56 2022-04-09 lh (dired-get-marked-files)
62 aaf6cf56 2022-04-09 lh (list (buffer-file-name)))))
63 aaf6cf56 2022-04-09 lh (mapc #'lh/extern-open-file files)))
65 aaf6cf56 2022-04-09 lh (defun lh/file-name-from-cursor ()
66 aaf6cf56 2022-04-09 lh (save-excursion
67 aaf6cf56 2022-04-09 lh (let ((start (progn
68 aaf6cf56 2022-04-09 lh (search-backward-regexp lh/filepath-delims)
69 aaf6cf56 2022-04-09 lh (forward-char)
72 aaf6cf56 2022-04-09 lh (search-forward-regexp lh/filepath-delims)
73 aaf6cf56 2022-04-09 lh (backward-char)
75 aaf6cf56 2022-04-09 lh (buffer-substring start end))))
77 aaf6cf56 2022-04-09 lh (defun lh/extern-open-file-at-cursor ()
79 aaf6cf56 2022-04-09 lh (lh/extern-open-file (lh/file-name-from-cursor)))
81 aaf6cf56 2022-04-09 lh (defun lh/base64-encode-file (filename)
82 aaf6cf56 2022-04-09 lh (with-temp-buffer
83 aaf6cf56 2022-04-09 lh (insert-file-contents filename)
84 aaf6cf56 2022-04-09 lh (base64-encode-region (point-min) (point-max))
85 aaf6cf56 2022-04-09 lh (buffer-string)))
87 40efebe3 2023-10-12 lh (defun lh/root-current-buffer (sudo)
88 85a5dd36 2022-06-02 lh (let ((old-point (point))
89 85a5dd36 2022-06-02 lh (old (current-buffer)))
90 40efebe3 2023-10-12 lh (find-file (concat "/" sudo "::" (buffer-file-name)))
91 85a5dd36 2022-06-02 lh (goto-char old-point)
92 bde020d7 2022-04-13 lh (kill-buffer old)))
94 40efebe3 2023-10-12 lh (defun lh/doas-current-buffer ()
96 40efebe3 2023-10-12 lh (lh/root-current-buffer "doas"))
98 40efebe3 2023-10-12 lh (defun lh/sudo-current-buffer ()
100 40efebe3 2023-10-12 lh (lh/root-current-buffer "sudo"))
102 5b3a91cd 2022-05-16 lh (defun lh/buffer-create-new ()
103 5b3a91cd 2022-05-16 lh (interactive)
104 5b3a91cd 2022-05-16 lh (let ((buf (generate-new-buffer "new")))
105 5b3a91cd 2022-05-16 lh (switch-to-buffer buf)
106 5b3a91cd 2022-05-16 lh (setq-local buffer-offer-save t)
109 dfb9342c 2022-05-16 lh (defun lh/pop-out-buffer ()
110 dfb9342c 2022-05-16 lh (interactive)
112 dfb9342c 2022-05-16 lh (switch-to-prev-buffer))
114 aaf6cf56 2022-04-09 lh (provide 'lh)