From 4e945fa7bc9e86fc0616930195eb3fbea74c4f7e Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Fri, 21 May 2021 11:08:41 -0500 Subject: Add `comment-or-uncomment-sexp' from https://endlessparentheses.com/a-comment-or-uncomment-sexp-command.html --- init.el | 4 ++- lisp/acdw.el | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/init.el b/init.el index 91f8baa..7caa90e 100644 --- a/init.el +++ b/init.el @@ -987,7 +987,9 @@ if ripgrep is installed, otherwise `consult-grep'." (defun setup-paredit-mode () "Correct weirdnesses and set up paredit mode." (paredit-mode +1) - (define-key lisp-mode-shared-map (kbd "DEL") #'paredit-backward-delete)) + (let ((map lisp-mode-shared-map)) + (define-key map (kbd "DEL") #'paredit-backward-delete) + (define-key map (kbd "C-M-;") #'comment-or-uncomment-sexp))) (dolist (mode lispy-modes) (add-hook (intern (concat (symbol-name mode) "-hook")) diff --git a/lisp/acdw.el b/lisp/acdw.el index c48c4e3..8b87dd5 100644 --- a/lisp/acdw.el +++ b/lisp/acdw.el @@ -105,6 +105,96 @@ is unfocused." ,@body) (message "%s... Done." ,message))) + +;;; Comment-or-uncomment-sexp +;; from https://endlessparentheses.com/a-comment-or-uncomment-sexp-command.html + +(defun uncomment-sexp (&optional n) + "Uncomment a sexp around point." + (interactive "P") + (let* ((initial-point (point-marker)) + (inhibit-field-text-motion t) + (p) + (end (save-excursion + (when (elt (syntax-ppss) 4) + (re-search-backward comment-start-skip + (line-beginning-position) + t)) + (setq p (point-marker)) + (comment-forward (point-max)) + (point-marker))) + (beg (save-excursion + (forward-line 0) + (while (and (not (bobp)) + (= end (save-excursion + (comment-forward (point-max)) + (point)))) + (forward-line -1)) + (goto-char (line-end-position)) + (re-search-backward comment-start-skip + (line-beginning-position) + t) + (ignore-errors + (while (looking-at-p comment-start-skip) + (forward-char -1))) + (point-marker)))) + (unless (= beg end) + (uncomment-region beg end) + (goto-char p) + ;; Indentify the "top-level" sexp inside the comment. + (while (and (ignore-errors (backward-up-list) t) + (>= (point) beg)) + (skip-chars-backward (rx (syntax expression-prefix))) + (setq p (point-marker))) + ;; Re-comment everything before it. + (ignore-errors + (comment-region beg p)) + ;; And everything after it. + (goto-char p) + (forward-sexp (or n 1)) + (skip-chars-forward "\r\n[:blank:]") + (if (< (point) end) + (ignore-errors + (comment-region (point) end)) + ;; If this is a closing delimiter, pull it up. + (goto-char end) + (skip-chars-forward "\r\n[:blank:]") + (when (eq 5 (car (syntax-after (point)))) + (delete-indentation)))) + ;; Without a prefix, it's more useful to leave point where + ;; it was. + (unless n + (goto-char initial-point)))) + +(defun comment-sexp--raw () + "Comment the sexp at point or ahead of point." + (pcase (or (bounds-of-thing-at-point 'sexp) + (save-excursion + (skip-chars-forward "\r\n[:blank:]") + (bounds-of-thing-at-point 'sexp))) + (`(,l . ,r) + (goto-char r) + (skip-chars-forward "\r\n[:blank:]") + (save-excursion + (comment-region l r)) + (skip-chars-forward "\r\n[:blank:]")))) + +(defun comment-or-uncomment-sexp (&optional n) + "Comment the sexp at point and move past it. +If already inside (or before) a comment, uncomment instead. +With a prefix argument N, (un)comment that many sexps." + (interactive "P") + (if (or (elt (syntax-ppss) 4) + (< (save-excursion + (skip-chars-forward "\r\n[:blank:]") + (point)) + (save-excursion + (comment-forward 1) + (point)))) + (uncomment-sexp n) + (dotimes (_ (or n 1)) + (comment-sexp--raw)))) + ;;; Emacs configuration functions -- cgit 1.4.1-21-gabe81