[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/sm-c-mode 3ef465d 11/12: * packages/sm-c-mode/sm-c-mode
From: |
Stefan Monnier |
Subject: |
[elpa] externals/sm-c-mode 3ef465d 11/12: * packages/sm-c-mode/sm-c-mode.el: Improve \ alignment with long lines |
Date: |
Sat, 28 Nov 2020 18:11:08 -0500 (EST) |
branch: externals/sm-c-mode
commit 3ef465d3ebed319fdaa3bd650bc19eb11c786fe5
Author: Stefan Monnier <monnier@iro.umontreal.ca>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>
* packages/sm-c-mode/sm-c-mode.el: Improve \ alignment with long lines
(syntax-ppss-cache, syntax-ppss-last): Declare before let-binding.
(sm-c-syntax-propertize): Add support for multiline // comments.
(sm-c-backslash-max-align-column): New defcustom.
(sm-c--bs-current-column): New function.
(sm-c--bs-realign-1): Use them.
---
sm-c-mode.el | 49 +++++++++++++++++++++++++++++++++++++------------
1 file changed, 37 insertions(+), 12 deletions(-)
diff --git a/sm-c-mode.el b/sm-c-mode.el
index 2090d99..7422a66 100644
--- a/sm-c-mode.el
+++ b/sm-c-mode.el
@@ -3,7 +3,7 @@
;; Copyright (C) 2015-2020 Free Software Foundation, Inc.
;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
-;; Version: 1.0
+;; Version: 1.1
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
@@ -140,6 +140,9 @@ no difference."
(nth 4 (parse-partial-sexp (1+ (nth 8 ppss)) (point)))))))
found))
+(defvar syntax-ppss-cache)
+(defvar syntax-ppss-last)
+
(defun sm-c--cpp-fontify-syntactically (ppss)
;; FIXME: ¡¡BIG UGLY HACK!!
;; Copied from font-lock.el's font-lock-fontify-syntactically-region.
@@ -312,7 +315,16 @@ E.g. a #define nested within 2 #ifs will be turned into
\"# define\"."
'syntax-table (string-to-syntax "|"))
(put-text-property (match-beginning 2) (match-end 2)
'syntax-table (string-to-syntax "|")))
- (sm-c--cpp-syntax-propertize end)))))
+ (sm-c--cpp-syntax-propertize end))))
+ ;; Handle // comments that span multiple lines via \\\n!
+ ("\\\\\\(\n\\)"
+ (1 (let ((ppss (save-excursion (syntax-ppss (match-beginning 0)))))
+ (when (and (nth 4 ppss) ;Within a comment
+ (null (nth 7 ppss)) ;Within a // comment
+ (save-excursion ;The \ is not itself escaped
+ (goto-char (match-beginning 0))
+ (zerop (mod (skip-chars-backward "\\\\") 2))))
+ (string-to-syntax "."))))))
(point) end))
(defun sm-c-syntactic-face-function (ppss)
@@ -795,6 +807,16 @@ if INNER is non-nil, it stops at the innermost one."
(sm-c--bs-realign-1 (car sm-c--bs-changed) (cdr sm-c--bs-changed))
(setq sm-c--bs-changed nil)))
+(defcustom sm-c-backslash-max-align-column 78
+ "Maximum column to align backslashes.
+Past this column, we do not try to align the backslashes."
+ :type 'integer)
+
+(defun sm-c--bs-current-column ()
+ (let ((col (current-column)))
+ (if (> col sm-c-backslash-max-align-column)
+ 0 col)))
+
(defun sm-c--bs-realign-1 (from to)
(save-excursion
(goto-char from)
@@ -802,27 +824,29 @@ if INNER is non-nil, it stops at the innermost one."
(unless (zerop (mod (skip-chars-backward "\\\\") 2))
(skip-chars-backward " \t")
(setq from (point))
- (let ((col (current-column))
+ (let ((col (sm-c--bs-current-column))
start end)
(while
(progn (setq start (point))
(end-of-line 0)
(and (< (point) start)
(not (zerop (mod (skip-chars-backward "\\\\") 2)))))
- (skip-chars-backward " \t")
- (setq col (max (current-column) col)))
+ (unless (>= col sm-c-backslash-max-align-column)
+ (skip-chars-backward " \t")
+ (setq col (max (sm-c--bs-current-column) col))))
(goto-char from)
(while
(progn (setq end (point))
(end-of-line 2)
(and (> (line-beginning-position) end)
(not (zerop (mod (skip-chars-backward "\\\\") 2)))))
- (skip-chars-backward " \t")
- (setq col (max (current-column) col)))
+ (unless (>= col sm-c-backslash-max-align-column)
+ (skip-chars-backward " \t")
+ (setq col (max (sm-c--bs-current-column) col))))
(goto-char to)
(beginning-of-line)
- (unless (or (> (point) end) ;Don't realign if we changed outside!
- (<= end start)) ;A lone \
+ (unless (or (> (point) end) ;Don't realign if we changed outside!
+ (<= end start)) ;A lone \
(setq col (1+ col)) ;Add a space before the backslashes.
(goto-char end)
@@ -834,9 +858,10 @@ if INNER is non-nil, it stops at the innermost one."
(cond
((> col curcol) (indent-to col))
((< col curcol)
- (move-to-column col t)
- (delete-region (point)
- (progn (skip-chars-forward " \t") (point))))))
+ (skip-chars-backward " \t")
+ (unless (> (current-column) col)
+ (move-to-column col t)
+ (delete-region (point) (1- (line-end-position)))))))
(end-of-line 0)))))))
;;; Font-lock support
- [elpa] branch externals/sm-c-mode created (now a207ad8), Stefan Monnier, 2020/11/28
- [elpa] externals/sm-c-mode 1f2c7ec 04/12: * sm-c-mode: Improve indentation of struct; plus bug fixes, Stefan Monnier, 2020/11/28
- [elpa] externals/sm-c-mode 9d2cff5 06/12: * packages/sm-c-mode: Add imenu/which-func/add-log support, Stefan Monnier, 2020/11/28
- [elpa] externals/sm-c-mode 97dea83 02/12: * sm-c-mode.el (sm-c-font-lock-keywords): Match function defs., Stefan Monnier, 2020/11/28
- [elpa] externals/sm-c-mode 4c63cb1 03/12: * sm-c-mode.el: Auto-align backslashes. Improve indent rules, Stefan Monnier, 2020/11/28
- [elpa] externals/sm-c-mode 5e67afc 07/12: * sm-c-mode/sm-c-mode.el (sm-c-smie-rules): Improve indent of enum's {...}, Stefan Monnier, 2020/11/28
- [elpa] externals/sm-c-mode 3ef465d 11/12: * packages/sm-c-mode/sm-c-mode.el: Improve \ alignment with long lines,
Stefan Monnier <=
- [elpa] externals/sm-c-mode a207ad8 12/12: * .gitignore: New file, Stefan Monnier, 2020/11/28
- [elpa] externals/sm-c-mode a3efebd 09/12: * sm-c-mode.el: Fix some problems reported by Andrés Ramírez, Stefan Monnier, 2020/11/28
- [elpa] externals/sm-c-mode 5646e71 05/12: * sm-c-mode.el: Improve "Commentary:" and docstrings, Stefan Monnier, 2020/11/28
- [elpa] externals/sm-c-mode 3c3b1f7 08/12: * sm-c-mode.el (sm-c--comment-regexp): Fix excessive backtracking, Stefan Monnier, 2020/11/28
- [elpa] externals/sm-c-mode 3b5d1e0 01/12: * sm-c-mode: New experimental package, Stefan Monnier, 2020/11/28
- [elpa] externals/sm-c-mode 0fde734 10/12: * packages/sm-c-mode/sm-c-mode.el: Release as version 1.0, Stefan Monnier, 2020/11/28