From eacccc307d263bd0d1eb2b387d47fe6efa6c9c76 Mon Sep 17 00:00:00 2001 From: Jackson Ray Hamilton Date: Sat, 21 Mar 2015 12:52:42 -0700 Subject: [PATCH 1/3] Improve single- and multi-line comment support Use single-line comments for `comment-region' and respect them for `fill-paragraph'. Make `fill-paragraph' behave like `js2-mode', which is pretty good at handling "//" and "/**/" comments. --- scss-mode.el | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/scss-mode.el b/scss-mode.el index 375f4b9..21cbfff 100644 --- a/scss-mode.el +++ b/scss-mode.el @@ -31,6 +31,11 @@ (require 'derived) (require 'compile) (require 'flymake) +(require 'css-mode) ; For `css-mode-syntax-table'. + +(eval-and-compile + (require 'cc-styles) ; For `c-setup-paragraph-variables'. + (require 'cc-engine)) ; For `c-paragraph-start' et. al. (defgroup scss nil "Scss mode" @@ -69,6 +74,32 @@ HYPERLINK HIGHLIGHT)" ;; Variables '(("$[a-z_-][a-z-_0-9]*" . font-lock-constant-face))) +(defconst scss-comment-start-skip + "\\(//+\\|/\\*+\\)\\s *") + +(defconst scss-comment-prefix-regexp + "//+\\|\\**") + +(defconst scss-comment-start-regexp + "/[*/]\\|\\s|") + +(defconst scss-paragraph-start + "\\(@[[:alpha:]]+\\>\\|$\\)") + +(defconst scss-syntactic-ws-start + "\\s \\|/[*/]\\|[\n\r]\\|\\\\[\n\r]\\|\\s!\\|") + +(defconst scss-syntactic-ws-end + "\\s \\|[\n\r/]\\|\\s!") + +(defconst scss-syntactic-eol + (concat "\\s *\\(/\\*[^*\n\r]*" + "\\(\\*+[^*\n\r/][^*\n\r]*\\)*" + "\\*+/\\s *\\)*" + "\\(//\\|/\\*[^*\n\r]*" + "\\(\\*+[^*\n\r/][^*\n\r]*\\)*$" + "\\|\\\\$\\|$\\)")) + (defun scss-compile-maybe() "Runs `scss-compile' on if `scss-compile-at-save' is t" (if scss-compile-at-save @@ -94,6 +125,35 @@ Special commands: (modify-syntax-entry ?/ ". 124" css-mode-syntax-table) (modify-syntax-entry ?* ". 23b" css-mode-syntax-table) (modify-syntax-entry ?\n ">" css-mode-syntax-table) + + ;; Prefer single-line comments for `comment-region'. These variables are also + ;; used by `fill-paragraph' for single-line comments. + (setq-local comment-start "//") + (setq-local comment-end "") + (setq-local comment-start-skip scss-comment-start-skip) + (setq-local comment-end-skip nil) ; Override `css-mode'. + + ;; Some variables needed by `cc-engine' for `fill-paragraph', etc. Copied + ;; from `js2-mode'. + (setq-local fill-paragraph-function 'c-fill-paragraph) + (setq-local c-comment-prefix-regexp scss-comment-prefix-regexp) + (setq c-comment-start-regexp scss-comment-start-regexp) + (setq c-line-comment-starter "//") + (setq c-paragraph-start scss-paragraph-start) + (setq c-paragraph-separate "$") + (setq c-syntactic-ws-start scss-syntactic-ws-start) + (setq c-syntactic-ws-end scss-syntactic-ws-end) + (setq c-syntactic-eol scss-syntactic-eol) + + (let ((c-buffer-is-cc-mode t)) + ;; Copied from `js-mode'. Also see Bug#6071. + (make-local-variable 'paragraph-start) + (make-local-variable 'paragraph-separate) + (make-local-variable 'paragraph-ignore-fill-prefix) + (make-local-variable 'adaptive-fill-mode) + (make-local-variable 'adaptive-fill-regexp) + (c-setup-paragraph-variables)) + (add-to-list 'compilation-error-regexp-alist scss-compile-error-regex) (add-hook 'after-save-hook 'scss-compile-maybe nil t)) From f151d0e24bcd2339052d2234762e7c6ab53f1736 Mon Sep 17 00:00:00 2001 From: Jackson Ray Hamilton Date: Mon, 6 Apr 2015 11:22:55 -0700 Subject: [PATCH 2/3] Add require for `c-fill-paragraph'. --- scss-mode.el | 1 + 1 file changed, 1 insertion(+) diff --git a/scss-mode.el b/scss-mode.el index 21cbfff..65212a4 100644 --- a/scss-mode.el +++ b/scss-mode.el @@ -32,6 +32,7 @@ (require 'compile) (require 'flymake) (require 'css-mode) ; For `css-mode-syntax-table'. +(require 'cc-cmds) ; For `c-fill-paragraph'. (eval-and-compile (require 'cc-styles) ; For `c-setup-paragraph-variables'. From bd30b175ae6a31a9587bc97210ffeb0d806d0463 Mon Sep 17 00:00:00 2001 From: Jackson Ray Hamilton Date: Mon, 6 Apr 2015 12:21:49 -0700 Subject: [PATCH 3/3] Break comments correctly with M-j. --- scss-mode.el | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scss-mode.el b/scss-mode.el index 65212a4..c90fe43 100644 --- a/scss-mode.el +++ b/scss-mode.el @@ -32,7 +32,7 @@ (require 'compile) (require 'flymake) (require 'css-mode) ; For `css-mode-syntax-table'. -(require 'cc-cmds) ; For `c-fill-paragraph'. +(require 'cc-cmds) ; For `c-fill-paragraph', `c-indent-new-comment-line'. (eval-and-compile (require 'cc-styles) ; For `c-setup-paragraph-variables'. @@ -137,6 +137,7 @@ Special commands: ;; Some variables needed by `cc-engine' for `fill-paragraph', etc. Copied ;; from `js2-mode'. (setq-local fill-paragraph-function 'c-fill-paragraph) + (setq-local comment-line-break-function 'c-indent-new-comment-line) (setq-local c-comment-prefix-regexp scss-comment-prefix-regexp) (setq c-comment-start-regexp scss-comment-start-regexp) (setq c-line-comment-starter "//") @@ -160,6 +161,9 @@ Special commands: (define-key scss-mode-map "\C-c\C-c" 'scss-compile) +;; Weirdly this is still needed when `comment-line-break-function' is bound. +(define-key scss-mode-map (kbd "M-j") 'c-indent-new-comment-line) + (defun flymake-scss-init () "Flymake support for SCSS files" (let* ((temp-file (flymake-init-create-temp-buffer-copy