branch: elpa/scala-mode commit c154f1623f4696d26e1c88d19170e67bf6825837 Author: Heikki Vesalainen <heikkivesalai...@yahoo.com> Commit: Heikki Vesalainen <heikkivesalai...@yahoo.com>
better join-line and fixup-whitespace --- README.md | 20 +++++++++-------- scala-mode2-indent.el | 61 ++++++++++++++++++++++++++++++--------------------- scala-mode2-map.el | 2 +- scala-mode2.el | 4 ++++ 4 files changed, 52 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 1482bbd..9bd3b29 100644 --- a/README.md +++ b/README.md @@ -299,17 +299,19 @@ To re-fill a paragraph, use the *fill-paragraph* command ( **M-q** command. To set the default, you use the *customize-variable* command or a mode-hook. -## Joinin lines (delete indentation) +## Joinin lines (delete indentation) and removing horizontal whitespace -Scala-mode defines its own *scala-indent:join-line' function. Besides -doing what the normal *join-line* (aka *delete-indentation*) function -does, it also removes comment marks (asterisks and slashes) when -comment lines are joined and space when code lines are joined and the -uppper line ended with a dot. +Scala-mode defines its own *scala-indent:join-line* and +*scala-indent:fixup-whitespace* functions. -In scala-mode2 buffers *scala-indent:join-line* replaces -*delete-indentation* in your key bindings. The default binding is -**M-^**. +Unlike the normal *join-line* (aka *delete-indentation*), +*scala-indent:join-line* detects the comment fill-prefix and removes +it. + +The *scala-indent:fixup-whitespace* first removes all horizontal +whitespace, then adds one space the context requires none to be +present (before semicolon, around dot, after '(' or '[', before ')' or +']', etc). ## Motion diff --git a/scala-mode2-indent.el b/scala-mode2-indent.el index 3475f51..bb059f7 100644 --- a/scala-mode2-indent.el +++ b/scala-mode2-indent.el @@ -801,7 +801,7 @@ strings" (defun scala-indent:indent-line (&optional strategy) "Indents the current line." - (interactive) + (interactive "*") (let ((state (save-excursion (syntax-ppss (line-beginning-position))))) (if (not (nth 8 state)) ;; 8 = start pos of comment or string, nil if none (scala-indent:indent-code-line strategy) @@ -820,7 +820,7 @@ strings" (t (current-indentation))))))) (defun scala-indent:indent-with-reluctant-strategy () - (interactive) + (interactive "*") (scala-indent:indent-line scala-indent:reluctant-strategy)) (defun scala-indent:scaladoc-indent (comment-start-pos) @@ -917,28 +917,39 @@ of a line inside a multi-line comment " (defun scala-mode:indent-scaladoc-asterisk (&optional insert-space-p) (message "scala-mode:indent-scaladoc-asterisk has been deprecated")) -(defun scala-indent:join-line () - (interactive) - (join-line) - (let ((state (syntax-ppss))) - (cond - ((and (integerp (nth 4 state)) ; nestable comment (i.e. with *) - (looking-at " \\*") - (save-excursion (goto-char (max (nth 8 state) (line-beginning-position))) - (looking-at "\\s */?\\*"))) - (delete-forward-char 2) - (delete-horizontal-space) - (insert " ")) - ((and (nth 4 state) ; row comment (i.e. with //) - (looking-at " //")) - (delete-forward-char 3) - (delete-horizontal-space) - (insert " ")) - ((and (not (nth 8 (syntax-ppss))) ; not in comment or string - (or (= (char-before) ?.) - (= (char-after (1+ (point))) ?.) - (= (char-after (1+ (point))) ?:))) - (delete-horizontal-space) - )))) + +(defun scala-indent:fixup-whitespace () + "scala-mode2 version of `fixup-whitespace'" + (interactive "*") + (save-excursion + (delete-horizontal-space) + (if (or (looking-at "^\\|[]):.]") + (save-excursion (forward-char -1) + (if (nth 4 (syntax-ppss)) + (looking-at "$\\|\\s(") + (looking-at "$\\|[[(.]"))) + (and (= (char-before) ?{) (= (char-after) ?}))) + nil + (insert ?\s)))) + +(defun scala-indent:join-line (&optional arg) + "scala-mode2 version of `join-line', i.e. `delete-indentation'" + (interactive "*P") + (beginning-of-line) + (if arg (forward-line 1)) + (when (= (preceding-char) ?\n) + (delete-region (point) (1- (point))) + (delete-horizontal-space) + (let ((state (syntax-ppss))) + (cond + ((and (integerp (nth 4 state)) ; nestable comment (i.e. with *) + (looking-at " *\\*\\($\\|[^/]\\)") + (save-excursion (goto-char (max (nth 8 state) (line-beginning-position))) + (looking-at "\\s */?\\*"))) + (delete-forward-char 2)) + ((and (nth 4 state) ; row comment (i.e. with //) + (looking-at " //")) + (delete-forward-char 3)))) + (scala-indent:fixup-whitespace))) (provide 'scala-mode2-indent) diff --git a/scala-mode2-map.el b/scala-mode2-map.el index 78a847b..70e3ae4 100644 --- a/scala-mode2-map.el +++ b/scala-mode2-map.el @@ -7,7 +7,7 @@ (defvar scala-mode-map (let ((map (make-sparse-keymap))) (set-keymap-parent map prog-mode-map) - (substitute-key-definition 'delete-indentation 'scala-indent:join-line map global-map) + ;(substitute-key-definition 'delete-indentation 'scala-indent:join-line map global-map) map) "Local key map used for scala mode") diff --git a/scala-mode2.el b/scala-mode2.el index ff25e5e..efe2a68 100644 --- a/scala-mode2.el +++ b/scala-mode2.el @@ -106,6 +106,8 @@ When started, runs `scala-mode-hook'. 'forward-sexp-function 'find-tag-default-function 'indent-line-function + 'fixup-whitespace + 'delete-indentation 'indent-tabs-mode) (add-hook 'syntax-propertize-extend-region-functions @@ -137,6 +139,8 @@ When started, runs `scala-mode-hook'. forward-sexp-function 'scala-mode:forward-sexp-function find-tag-default-function 'scala-mode:find-tag indent-line-function 'scala-indent:indent-line + fixup-whitespace 'scala-indent:fixup-whitespace + delete-indentation 'scala-indent:join-line indent-tabs-mode nil ) (use-local-map scala-mode-map)