From: Eshel Yaron Date: Sun, 4 Sep 2022 17:04:30 +0000 (+0300) Subject: ADDED: sweep-indent-line, an indent-line-function for sweep-mode X-Git-Tag: v0.2.0~42 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=10516034be4c2db4a03754aa9de8a1c0f61808ec;p=dict.git ADDED: sweep-indent-line, an indent-line-function for sweep-mode --- diff --git a/sweep.el b/sweep.el index 48309a2..46ea566 100644 --- a/sweep.el +++ b/sweep.el @@ -820,6 +820,99 @@ Interactively, a prefix arg means to prompt for BUFFER." :doc "Keymap for `sweep-mode'." "C-c C-c" #'sweep-colourise-buffer) +(defun sweep-indent-line () + (interactive) + (when-let ((ppss (syntax-ppss)) + (pos (- (point-max) (point))) + (indent (sweep-indent-line-indentation ppss))) + (back-to-indentation) + (if (= indent (current-column)) + 'noindent + (beginning-of-line) + (combine-after-change-calls + (delete-horizontal-space) + (insert (make-string indent ? ))) + (if (> (- (point-max) pos) (point)) + (goto-char (- (point-max) pos)))))) + +(defun sweep-indent-line-indentation (ppss) + (save-match-data + (save-excursion + (beginning-of-line) + (re-search-backward (rx bol (zero-or-more (not "\n")) graph (zero-or-more (not "\n"))) nil t) + (cond + ((sweep-indent-line-ends-with-comment-or-string-p) 0) + ((sweep-indent-line-ends-with-fullstop-p) 0) + ((sweep-indent-line-ends-with-if)) + ((sweep-indent-line-ends-with-then ppss)) + ((sweep-indent-line-ends-with-else ppss)) + ((sweep-indent-line-ends-with-arg ppss)) + ((sweep-indent-line-ends-with-neck-p) 4) + (t (sweep-indent-line-fallback)))))) + +(defun sweep-indent-line-fallback () + (save-excursion + (back-to-indentation) + (current-column))) + +(defun sweep-indent-line-ends-with-if () + (save-excursion + (end-of-line) + (when-let ((start-of-ite (nth 1 (syntax-ppss)))) + (when (<= (pos-bol) start-of-ite) + (goto-char start-of-ite) + (let ((col (current-column))) + (when (looking-at-p (rx "( ")) + col)))))) + +(defun sweep-indent-line-ends-with-then (ppss) + (save-excursion + (when-let ((orig (nth 1 ppss)) + (start-of-ite (nth 1 (syntax-ppss)))) + (when (= start-of-ite orig) + (back-to-indentation) + (let ((col (current-column))) + (when (looking-at-p (rx "-> ")) + col)))))) + +(defun sweep-indent-line-ends-with-else (ppss) + (save-excursion + (when-let ((orig (nth 1 ppss)) + (start-of-ite (nth 1 (syntax-ppss)))) + (when (= start-of-ite orig) + (back-to-indentation) + (let ((col (current-column))) + (when (looking-at-p (rx "; ")) + col)))))) + +(defun sweep-indent-line-ends-with-arg (ppss) + (save-excursion + (end-of-line) + (when-let ((orig (nth 1 ppss)) + (start-of-ite (nth 1 (syntax-ppss)))) + (when (= start-of-ite orig) + (goto-char start-of-ite) + (forward-char 1) + (skip-syntax-forward " ") + (current-column))))) + +(defun sweep-indent-line-ends-with-neck-p () + (save-excursion + (looking-at-p (rx (zero-or-more (not "\n")) + (or ":-" "=>" "-->") + (zero-or-more blank) + eol)))) + +(defun sweep-indent-line-ends-with-comment-or-string-p () + (save-excursion + (end-of-line) + (nth 8 (syntax-ppss)))) + +(defun sweep-indent-line-ends-with-fullstop-p () + (save-excursion + (end-of-line) + (= ?. (preceding-char)))) + (defun sweep-syntax-propertize (start end) (goto-char start) (let ((case-fold-search nil)) @@ -840,6 +933,7 @@ Interactively, a prefix arg means to prompt for BUFFER." (setq-local beginning-of-defun-function #'sweep-beginning-of-top-term) (setq-local end-of-defun-function #'sweep-end-of-top-term) (setq-local syntax-propertize-function #'sweep-syntax-propertize) + (setq-local indent-line-function #'sweep-indent-line) (setq-local font-lock-defaults '(nil nil diff --git a/sweep.pl b/sweep.pl index 6901b59..0a475ef 100644 --- a/sweep.pl +++ b/sweep.pl @@ -159,6 +159,7 @@ sweep_module_path(ModuleName, Path) :- sweep_module_path_(Module, Path0), atom_string(Path0, Path). + sweep_module_path_(Module, Path) :- module_property(Module, file(Path)), !. sweep_module_path_(Module, Path) :- @@ -289,7 +290,7 @@ sweep_color_normalized(Offset, Col, Nom) :- Col =.. [Nom0|Rest], sweep_color_normalized_(Offset, Nom0, Rest, Nom). -sweep_color_normalized_(Offset, Goal0, [Kind0,Head|_], [Goal,Kind,F,N]) :- +sweep_color_normalized_(_, Goal0, [Kind0,Head|_], [Goal,Kind,F,N]) :- sweep_color_goal(Goal0), !, atom_string(Goal0, Goal),