about summary refs log tree commit diff
path: root/users/glittershark/emacs.d/utils.el
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2020-06-16T00·06+0100
committerVincent Ambo <mail@tazj.in>2020-06-16T00·06+0100
commit4fe4e3d9a297ff9c8aa77e1a196a96b65ecd99ae (patch)
tree54455b0cd36e58fd745ce0b6d38101b7a902ce4b /users/glittershark/emacs.d/utils.el
parent2edb963b97867b27f68efac8d05bf966077b0b01 (diff)
parent69ee53bffaf145eb86dd39a6cdc0ae4d9d3de45c (diff)
Add 'users/glittershark/emacs.d/' from commit '69ee53bffaf145eb86dd39a6cdc0ae4d9d3de45c' r/979
git-subtree-dir: users/glittershark/emacs.d
git-subtree-mainline: 2edb963b97867b27f68efac8d05bf966077b0b01
git-subtree-split: 69ee53bffaf145eb86dd39a6cdc0ae4d9d3de45c
Diffstat (limited to 'users/glittershark/emacs.d/utils.el')
-rw-r--r--users/glittershark/emacs.d/utils.el92
1 files changed, 92 insertions, 0 deletions
diff --git a/users/glittershark/emacs.d/utils.el b/users/glittershark/emacs.d/utils.el
new file mode 100644
index 0000000000..d6d1d5722b
--- /dev/null
+++ b/users/glittershark/emacs.d/utils.el
@@ -0,0 +1,92 @@
+;;; private/grfn/utils.el -*- lexical-binding: t; -*-
+
+
+;; Elisp Extras
+
+(defmacro comment (&rest _body)
+  "Comment out one or more s-expressions"
+  nil)
+
+(defun inc (x) "Returns x + 1" (+ 1 x))
+(defun dec (x) "Returns x - 1" (- x 1))
+
+
+;;
+;; Text editing utils
+;;
+
+;; Reading strings
+
+(defun get-char (&optional point)
+  "Get the character at the given `point' (defaulting to the current point),
+without properties"
+  (let ((point (or point (point))))
+    (buffer-substring-no-properties point (+ 1 point))))
+
+(defun get-line (&optional lineno)
+  "Read the line number `lineno', or the current line if `lineno' is nil, and
+return it as a string stripped of all text properties"
+  (let ((current-line (line-number-at-pos)))
+    (if (or (not lineno)
+            (= current-line lineno))
+        (thing-at-point 'line t)
+      (save-mark-and-excursion
+       (line-move (- lineno (line-number-at-pos)))
+       (thing-at-point 'line t)))))
+
+(defun get-line-point ()
+  "Get the position in the current line of the point"
+  (- (point) (line-beginning-position)))
+
+;; Moving in the file
+
+(defun goto-line-char (pt)
+  "Moves the point to the given position expressed as an offset from the start
+of the line"
+  (goto-char (+ (line-beginning-position) pt)))
+
+(defun goto-eol ()
+  "Moves to the end of the current line"
+  (goto-char (line-end-position)))
+
+(defun goto-regex-on-line (regex)
+  "Moves the point to the first occurrence of `regex' on the current line.
+Returns nil if the regex did not match, non-nil otherwise"
+  (when-let ((current-line (get-line))
+             (line-char (string-match regex current-line)))
+    (goto-line-char line-char)))
+
+(defun goto-regex-on-line-r (regex)
+  "Moves the point to the *last* occurrence of `regex' on the current line.
+Returns nil if the regex did not match, non-nil otherwise"
+  (when-let ((current-line (get-line))
+             (modified-regex (concat ".*\\(" regex "\\)"))
+             (_ (string-match modified-regex current-line))
+             (match-start (match-beginning 1)))
+    (goto-line-char match-start)))
+
+(comment
+ (progn
+   (string-match (rx (and (zero-or-more anything)
+                          (group "foo" "foo")))
+                 "foofoofoo")
+   (match-beginning 1)))
+
+;; Changing file contents
+
+(defun delete-line ()
+  "Remove the line at the current point"
+  (delete-region (line-beginning-position)
+                 (inc (line-end-position))))
+
+(defmacro modify-then-indent (&rest body)
+  "Modify text in the buffer according to body, then re-indent from where the
+  cursor started to where the cursor ended up, then return the cursor to where
+  it started."
+  `(let ((beg (line-beginning-position))
+         (orig-line-char (- (point) (line-beginning-position))))
+     (atomic-change-group
+       (save-mark-and-excursion
+        ,@body
+        (evil-indent beg (+ (line-end-position) 1))))
+     (goto-line-char orig-line-char)))