From 0293defdea75407b6e11b3ebef3bc0d49fac9a58 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Sun, 17 Sep 2023 01:19:17 +0200 Subject: Upgrade markdown-mode and rust-utils --- .emacs.d/lisp/rust-utils.el | 101 +++++++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 35 deletions(-) (limited to '.emacs.d/lisp/rust-utils.el') diff --git a/.emacs.d/lisp/rust-utils.el b/.emacs.d/lisp/rust-utils.el index 41f1ba8..cb55172 100644 --- a/.emacs.d/lisp/rust-utils.el +++ b/.emacs.d/lisp/rust-utils.el @@ -36,46 +36,77 @@ visit the new file." ;;; dbg! macro -(defun rust-insert-dbg () - "Insert the dbg! macro." - (cond ((region-active-p) - (when (< (mark) (point)) - (exchange-point-and-mark)) - (let ((old-point (point))) - (insert-parentheses) - (goto-char old-point))) - (t - (when (rust-in-str) - (up-list -1 t t)) - (insert "(") - (forward-sexp) - (insert ")") - (backward-sexp))) - (insert "dbg!")) +(defun rust-insert-dbg-sexp () + "Insert the dbg! macro around a sexp if possible, insert at current position +if not. Move cursor to the end of macro." + (when (rust-in-str) + (up-list -1 t t)) + (setq safe-to-forward t) + (save-excursion + (condition-case nil + (forward-sexp) + (error (setq safe-to-forward nil) + nil))) + (cond + ((not safe-to-forward) + (rust-insert-dbg-alone)) + (t + (insert "(") + (forward-sexp) + (insert ")") + (backward-sexp) + (insert "dbg!") + (forward-sexp)))) + +(defun rust-insert-dbg-region () + "Insert the dbg! macro around a region. Move cursor to the end of macro." + (when (< (mark) (point)) + (exchange-point-and-mark)) + (let ((old-point (point))) + (insert-parentheses) + (goto-char old-point)) + (insert "dbg!") + (forward-sexp)) + +(defun rust-insert-dbg-alone () + "Insert the dbg! macro alone. Move cursor in between the brackets." + (insert "dbg!()") + (backward-char)) ;;;###autoload (defun rust-dbg-wrap-or-unwrap () "Either remove or add the dbg! macro." (interactive) - (save-excursion - (if (region-active-p) - (rust-insert-dbg) - - (let ((beginning-of-symbol (ignore-errors (beginning-of-thing 'symbol)))) - (when beginning-of-symbol - (goto-char beginning-of-symbol))) - - (let ((dbg-point (save-excursion - (or (and (looking-at-p "dbg!") (+ 4 (point))) - (ignore-errors - (while (not (rust-looking-back-str "dbg!")) - (backward-up-list)) - (point)))))) - (cond (dbg-point - (goto-char dbg-point) - (delete-char -4) - (delete-pair)) - (t (rust-insert-dbg))))))) + + (cond + + ;; region + ((region-active-p) + (rust-insert-dbg-region)) + + ;; alone + ((or (looking-at-p " *$") (looking-at-p " *//.*")) + (rust-insert-dbg-alone)) + + ;; symbol + (t + (let ((beginning-of-symbol (ignore-errors (beginning-of-thing 'symbol)))) + (when beginning-of-symbol + (goto-char beginning-of-symbol))) + + (let ((dbg-point (save-excursion + (or (and (looking-at-p "dbg!") (+ 4 (point))) + (ignore-errors + (while (not (rust-looking-back-str "dbg!")) + (backward-up-list)) + (point)))))) + (cond (dbg-point + (goto-char dbg-point) + (delete-char -4) + (delete-pair)) + (t (rust-insert-dbg-sexp))))) + ) +) (defun rust-toggle-mutability () "Toggles the mutability of the variable defined on the current line" -- cgit v1.3