lh.el (2983B)
1 ;; -*- lexical-binding: t; -*- 2 3 (defvar lh/filepath-delims 4 "[\"']") 5 6 (defun lh/region-bounds-full-lines (start end) 7 (cons 8 (save-excursion 9 (goto-char start) 10 (line-beginning-position)) 11 (save-excursion 12 (goto-char end) 13 (line-end-position)))) 14 15 (defun lh/move-region-up () 16 (interactive) 17 (let* ((deactivate-mark nil) 18 (bounds (lh/region-bounds-full-lines (region-beginning) (region-end))) 19 (text (buffer-substring (car bounds) (cdr bounds)))) 20 (delete-region (car bounds) (cdr bounds)) 21 (transpose-lines 1) 22 (forward-line -2) 23 (set-mark (point)) 24 (insert text) 25 (activate-mark))) 26 27 (defun lh/move-line-up () 28 (interactive) 29 (if (region-active-p) 30 (lh/move-region-up) 31 (transpose-lines 1) 32 (forward-line -2))) 33 34 (defun lh/move-region-down () 35 (interactive) 36 (let* ((deactivate-mark nil) 37 (bounds (lh/region-bounds-full-lines (region-beginning) (region-end))) 38 (text (buffer-substring (car bounds) (cdr bounds)))) 39 (delete-region (car bounds) (cdr bounds)) 40 (forward-line) 41 (transpose-lines 1) 42 (forward-line -1) 43 (set-mark (point)) 44 (insert text) 45 (activate-mark))) 46 47 (defun lh/move-line-down () 48 (interactive) 49 (if (region-active-p) 50 (lh/move-region-down) 51 (forward-line) 52 (transpose-lines 1) 53 (forward-line -1))) 54 55 (defun lh/extern-open-file (file) 56 (shell-command (concat "xdg-open " (shell-quote-argument file)))) 57 58 (defun lh/extern-open-current-file () 59 (interactive) 60 (let ((files (if (eq major-mode 'dired-mode) 61 (dired-get-marked-files) 62 (list (buffer-file-name))))) 63 (mapc #'lh/extern-open-file files))) 64 65 (defun lh/file-name-from-cursor () 66 (save-excursion 67 (let ((start (progn 68 (search-backward-regexp lh/filepath-delims) 69 (forward-char) 70 (point))) 71 (end (progn 72 (search-forward-regexp lh/filepath-delims) 73 (backward-char) 74 (point)))) 75 (buffer-substring start end)))) 76 77 (defun lh/extern-open-file-at-cursor () 78 (interactive) 79 (lh/extern-open-file (lh/file-name-from-cursor))) 80 81 (defun lh/base64-encode-file (filename) 82 (with-temp-buffer 83 (insert-file-contents filename) 84 (base64-encode-region (point-min) (point-max)) 85 (buffer-string))) 86 87 (defun lh/root-current-buffer (sudo) 88 (let ((old-point (point)) 89 (old (current-buffer))) 90 (find-file (concat "/" sudo "::" (buffer-file-name))) 91 (goto-char old-point) 92 (kill-buffer old))) 93 94 (defun lh/doas-current-buffer () 95 (interactive) 96 (lh/root-current-buffer "doas")) 97 98 (defun lh/sudo-current-buffer () 99 (interactive) 100 (lh/root-current-buffer "sudo")) 101 102 (defun lh/buffer-create-new () 103 (interactive) 104 (let ((buf (generate-new-buffer "new"))) 105 (switch-to-buffer buf) 106 (setq-local buffer-offer-save t) 107 buf)) 108 109 (defun lh/pop-out-buffer () 110 (interactive) 111 (make-frame) 112 (switch-to-prev-buffer)) 113 114 (provide 'lh)