commit aaf6cf560c6ca7f85bb2960fa6f1a3a122786379
parent b3f8f12795f8f37eb90b9accec404cffa7420959
Author: Lukas Henkel <lh@entf.net>
Date: Sat, 9 Apr 2022 12:41:44 +0200
Add some useful functions
Diffstat:
M | init.el | | | 19 | ++++++++++++------- |
A | lisp/lh.el | | | 98 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
D | lisp/utils.el | | | 24 | ------------------------ |
3 files changed, 110 insertions(+), 31 deletions(-)
diff --git a/init.el b/init.el
@@ -11,7 +11,7 @@
lisp-path)
load-path)
-(require 'utils)
+(require 'lh)
(require 'consult-eglot)
(require 'paredit-menu)
(require 'iso-transl)
@@ -120,16 +120,21 @@
("C-c n l" . org-roam-buffer-toggle)
("C-c n f" . org-roam-node-find)
- ("C-c n i" . org-roam-node-insert)))
-(lh/global-set-keys
- (("C-h f" . helpful-callable)
+ ("C-c n i" . org-roam-node-insert)
+
+ ("C-h f" . helpful-callable)
("C-h F" . helpful-function)
("C-h C" . helpful-command)
("C-h v" . helpful-variable)
("C-h k" . helpful-key)
- ("C-c C-d" . helpful-at-point)))
-(lh/global-set-keys
- (("M-s d" . deadgrep)))
+ ("C-c C-d" . helpful-at-point)
+
+ ("C-S-p" . lh/move-line-up)
+ ("M-<up>" . lh/move-line-up)
+ ("C-S-n" . lh/move-line-down)
+ ("M-<down>" . lh/move-line-down)
+
+ ("M-s d" . deadgrep)))
(lh/define-keys icomplete-fido-mode-map
(("TAB" . icomplete-force-complete)
diff --git a/lisp/lh.el b/lisp/lh.el
@@ -0,0 +1,98 @@
+;; -*- lexical-binding: t; -*-
+
+(defvar lh/filepath-delims
+ "[\"']")
+
+(defun lh/region-bounds-full-lines (start end)
+ (cons
+ (save-excursion
+ (goto-char start)
+ (line-beginning-position))
+ (save-excursion
+ (goto-char end)
+ (line-end-position))))
+
+(defun lh/move-region-up ()
+ (interactive)
+ (let* ((deactivate-mark nil)
+ (bounds (lh/region-bounds-full-lines (region-beginning) (region-end)))
+ (text (buffer-substring (car bounds) (cdr bounds))))
+ (delete-region (car bounds) (cdr bounds))
+ (transpose-lines 1)
+ (forward-line -2)
+ (set-mark (point))
+ (insert text)
+ (activate-mark)))
+
+(defun lh/move-line-up ()
+ (interactive)
+ (if (region-active-p)
+ (lh/move-region-up)
+ (transpose-lines 1)
+ (forward-line -2)))
+
+(defun lh/move-region-down ()
+ (interactive)
+ (let* ((deactivate-mark nil)
+ (bounds (lh/region-bounds-full-lines (region-beginning) (region-end)))
+ (text (buffer-substring (car bounds) (cdr bounds))))
+ (delete-region (car bounds) (cdr bounds))
+ (forward-line)
+ (transpose-lines 1)
+ (forward-line -1)
+ (set-mark (point))
+ (insert text)
+ (activate-mark)))
+
+(defun lh/move-line-down ()
+ (interactive)
+ (if (region-active-p)
+ (lh/move-region-down)
+ (forward-line)
+ (transpose-lines 1)
+ (forward-line -1)))
+
+(defun lh/extern-open-file (file)
+ (shell-command (concat "xdg-open " (shell-quote-argument file))))
+
+(defun lh/extern-open-current-file ()
+ (interactive)
+ (let ((files (if (eq major-mode 'dired-mode)
+ (dired-get-marked-files)
+ (list (buffer-file-name)))))
+ (mapc #'lh/extern-open-file files)))
+
+(defun lh/file-name-from-cursor ()
+ (save-excursion
+ (let ((start (progn
+ (search-backward-regexp lh/filepath-delims)
+ (forward-char)
+ (point)))
+ (end (progn
+ (search-forward-regexp lh/filepath-delims)
+ (backward-char)
+ (point))))
+ (buffer-substring start end))))
+
+(defun lh/extern-open-file-at-cursor ()
+ (interactive)
+ (lh/extern-open-file (lh/file-name-from-cursor)))
+
+(defun lh/base64-encode-file (filename)
+ (with-temp-buffer
+ (insert-file-contents filename)
+ (base64-encode-region (point-min) (point-max))
+ (buffer-string)))
+
+(defun lh/insert-random-sha1 ()
+ "Insert a random sha1 sum at point"
+ (interactive)
+ (insert (sha1 (number-to-string (random)))))
+
+(defun lh/insert-number-from-register-format (register format-string)
+ (interactive (list (register-read-with-preview "Insert register: ")
+ (read-string "Format String: ")))
+ (let ((val (get-register register)))
+ (insert (format format-string val))))
+
+(provide 'lh)
diff --git a/lisp/utils.el b/lisp/utils.el
@@ -1,24 +0,0 @@
-;;; utils.el -*- lexical-binding: t; -*-
-;; This file contains a bunch of utilities
-;; both for use in scripts, but also interactive functions
-
-(require 'uuidgen)
-
-(defun util-base64-encode-file (filename)
- (with-temp-buffer
- (insert-file-contents filename)
- (base64-encode-region (point-min) (point-max))
- (buffer-string)))
-
-(defun util-insert-random-sha1 ()
- "Insert a random sha1 sum at point"
- (interactive)
- (insert (sha1 (number-to-string (random)))))
-
-(defun util-insert-number-from-register-format (register format-string)
- (interactive (list (register-read-with-preview "Insert register: ")
- (read-string "Format String: ")))
- (let ((val (get-register register)))
- (insert (format format-string val))))
-
-(provide 'utils)