summaryrefslogtreecommitdiff
path: root/lisp/progmodes/prog-mode.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/progmodes/prog-mode.el')
-rw-r--r--lisp/progmodes/prog-mode.el38
1 files changed, 36 insertions, 2 deletions
diff --git a/lisp/progmodes/prog-mode.el b/lisp/progmodes/prog-mode.el
index 562a9c319d6..d4e0514a6c3 100644
--- a/lisp/progmodes/prog-mode.el
+++ b/lisp/progmodes/prog-mode.el
@@ -30,7 +30,14 @@
;;; Code:
(eval-when-compile (require 'cl-lib)
- (require 'subr-x))
+ (require 'subr-x)
+ (require 'treesit))
+
+(declare-function treesit-available-p "treesit.c")
+(declare-function treesit-parser-list "treesit.c")
+(declare-function treesit-node-type "treesit.c")
+(declare-function treesit-node-at "treesit.c")
+(declare-function treesit-node-match-p "treesit.c")
(defgroup prog-mode nil
"Generic programming mode, from which others derive."
@@ -102,7 +109,8 @@
(defvar-keymap prog-mode-map
:doc "Keymap used for programming modes."
- "C-M-q" #'prog-indent-sexp)
+ "C-M-q" #'prog-indent-sexp
+ "M-q" #'prog-fill-reindent-defun)
(defvar prog-indentation-context nil
"When non-nil, provides context for indenting embedded code chunks.
@@ -140,6 +148,30 @@ instead."
(end (progn (forward-sexp 1) (point))))
(indent-region start end nil))))
+(defun prog-fill-reindent-defun (&optional argument)
+ "Refill or reindent the paragraph or defun that contains point.
+
+If the point is in a string or a comment, fill the paragraph that
+contains point or follows point.
+
+Otherwise, reindent the function definition that contains point
+or follows point."
+ (interactive "P")
+ (save-excursion
+ (let ((treesit-text-node
+ (and (treesit-available-p)
+ (treesit-parser-list)
+ (treesit-node-match-p
+ (treesit-node-at (point)) 'text t))))
+ (if (or treesit-text-node
+ (nth 8 (syntax-ppss))
+ (re-search-forward "\\s-*\\s<" (line-end-position) t))
+ (fill-paragraph argument (region-active-p))
+ (beginning-of-defun)
+ (let ((start (point)))
+ (end-of-defun)
+ (indent-region start (point) nil))))))
+
(defun prog-first-column ()
"Return the indentation column normally used for top-level constructs."
(or (car prog-indentation-context) 0))
@@ -308,6 +340,8 @@ support it."
(setq-local require-final-newline mode-require-final-newline)
(setq-local parse-sexp-ignore-comments t)
(add-hook 'context-menu-functions 'prog-context-menu 10 t)
+ ;; Enable text conversion in this buffer.
+ (setq-local text-conversion-style t)
;; Any programming language is always written left to right.
(setq bidi-paragraph-direction 'left-to-right))