all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: eliz@gnu.org, 60983@debbugs.gnu.org, casouri@gmail.com
Subject: bug#60983: 29.0.60; Tree-sitter user-level control
Date: Thu, 26 Jan 2023 09:27:38 +0100	[thread overview]
Message-ID: <875ycthf9h.fsf@thornhill.no> (raw)
In-Reply-To: <jwvk01aiazc.fsf-monnier+emacs@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 3724 bytes --]

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> -(defcustom c-ts-mode-indent-style 'gnu
>> +(defcustom c-ts-mode-indent-style "gnu"
>>    "Style used for indentation.
>>  
>>  The selected style could be one of GNU, K&R, LINUX or BSD.  If
>> @@ -100,13 +100,33 @@ c-ts-mode-indent-style
>>  set instead.  This function is expected return a list that
>>  follows the form of `treesit-simple-indent-rules'."
>>    :version "29.1"
>> -  :type '(choice (symbol :tag "Gnu" 'gnu)
>> -                 (symbol :tag "K&R" 'k&r)
>> -                 (symbol :tag "Linux" 'linux)
>> -                 (symbol :tag "BSD" 'bsd)
>> +  :type '(choice (string :tag "Gnu" "gnu")
>> +                 (string :tag "K&R" "k&r")
>> +                 (string :tag "Linux" "linux")
>> +                 (string :tag "BSD" "bsd")
>>                   (function :tag "A function for user customized style" ignore))
>> +  :set #'c-ts-mode--indent-style
>>    :group 'c)
>
> Why change to strings?
> BTW the previous code seems wrong: instead of
>
>     (symbol :tag "FOO" 'foo)
>
> it should be
>
>     (symbol :tag "FOO" foo)
>
> since `'foo` is not a symbol but a list (of two symbols).
>

Thanks, fixed.


>> +(defun c-ts-mode--indent-style (sym val)
>> +  "Custom setter for `c-ts-mode-indent-style'."
>> +  (set-default sym val))
>
> Hmm... why bother use a `:set`ter if it doesn't do anything more than
> the default does?
> Shouldn't it call `treesit--indent-rules-optimize` to (re)set
> `treesit-simple-indent-rules`?
> [ Presumably in all relevant buffers, since the defcustom setting is
>   global.  ]
>

Thanks, done.

>> +(defun c-ts-mode-set-style ()
>> +  (interactive)
>> +  (or (eq major-mode 'c-ts-mode) (eq major-mode 'c++-ts-mode)
>> +      (error "Buffer %s is not a c-ts-mode (c-ts-mode-set-style)"
>> +             (buffer-name)))
>> +  (if-let ((mode (cond ((eq major-mode 'c-ts-mode) 'c)
>> +                       ((eq major-mode 'c++-ts-mode) 'cpp)
>> +                       (t nil)))
>> +           (choice (completing-read "Select style: " '("gnu" "k&r" "linux" "bsd"))))
>
> Here, we probably want to specify `must-match` to `completing-read`
> (which makes it unnecessary to check `if-let`, I think) and we should
> provide a default.
> Also we should probably use the (c-ts-mode--indent-styles mode) alist
> rather than hardcode the set of styles.
>
>> +      (c-ts-mode--indent-style 'c-ts-mode-indent-style choice)
>> +    (kill-local-variable 'treesit-simple-indent-rules)
>> +    (setq-local treesit-simple-indent-rules
>> +                (treesit--indent-rules-optimize
>> +                 (c-ts-mode--set-indent-style mode)))))
>
> Here we presumably want to do the (setq-local
> treesit-simple-indent-rules ...) every time (and set
> `c-ts-mode-indent-style` buffer locally rather than via
> `c-ts-mode--indent-style`, or otherwise provide an additional arg to
> `c-ts-mode--indent-style` to say whether it applies globally or only to
> the current buffer).
>
> BTW, the naming of `c-ts-mode--indent-style` and
> `c-ts-mode--set-indent-style` is confusing.
>
> Also: why `kill-local-variable` just before the `setq-local`?
>
>> -           (pcase c-ts-mode-indent-style
>> -             ('gnu   (alist-get 'gnu (c-ts-mode--indent-styles mode)))
>> -             ('k&r   (alist-get 'k&r (c-ts-mode--indent-styles mode)))
>> -             ('bsd   (alist-get 'bsd (c-ts-mode--indent-styles mode)))
>> -             ('linux (alist-get 'linux (c-ts-mode--indent-styles mode)))))))
>> +           (alist-get c-ts-mode-indent-style
>> +                      (c-ts-mode--indent-styles mode) nil nil #'equal))))
>
> Thanks :-)
>
>
>         Stefan


Does this patch look better?  It also works, now :)

Theo



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Initial-c-ts-mode-set-style-attempt.patch --]
[-- Type: text/x-patch, Size: 3344 bytes --]

From 49b135580ba3d60b2f77dada99a1adc4c64ef9b2 Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <theo@thornhill.no>
Date: Wed, 25 Jan 2023 21:04:00 +0100
Subject: [PATCH] Initial c-ts-mode-set-style attempt

---
 lisp/progmodes/c-ts-mode.el | 47 ++++++++++++++++++++++++++++++-------
 1 file changed, 38 insertions(+), 9 deletions(-)

diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 95f9001e0d..0585795f35 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -92,6 +92,25 @@ c-ts-mode-indent-offset
   :safe 'integerp
   :group 'c)
 
+(defun c-ts-mode--indent-style-setter (sym val)
+  "Custom setter for `c-ts-mode-set-style'."
+  (named-let loop ((res nil)
+                   (buffers (buffer-list)))
+    (if (null buffers)
+        (mapc (lambda (b)
+                (with-current-buffer b
+                  (set-default sym val)
+                  (setq-local treesit-simple-indent-rules
+                              (treesit--indent-rules-optimize
+                               (c-ts-mode--set-indent-style
+                                (if (eq major-mode 'c-ts-mode) 'c 'cpp))))))
+              res)
+      (let ((buffer (car buffers)))
+        (with-current-buffer buffer
+          (if (or (eq major-mode 'c-ts-mode) (eq major-mode 'c++-ts-mode))
+              (loop (append res (list buffer)) (cdr buffers))
+            (loop res (cdr buffers))))))))
+
 (defcustom c-ts-mode-indent-style 'gnu
   "Style used for indentation.
 
@@ -100,13 +119,27 @@ c-ts-mode-indent-style
 set instead.  This function is expected return a list that
 follows the form of `treesit-simple-indent-rules'."
   :version "29.1"
-  :type '(choice (symbol :tag "Gnu" 'gnu)
-                 (symbol :tag "K&R" 'k&r)
-                 (symbol :tag "Linux" 'linux)
-                 (symbol :tag "BSD" 'bsd)
+  :type '(choice (symbol :tag "Gnu" gnu)
+                 (symbol :tag "K&R" k&r)
+                 (symbol :tag "Linux" linux)
+                 (symbol :tag "BSD" bsd)
                  (function :tag "A function for user customized style" ignore))
+  :set #'c-ts-mode--indent-style-setter
   :group 'c)
 
+(defun c-ts-mode-set-style ()
+  (interactive)
+  (or (eq major-mode 'c-ts-mode) (eq major-mode 'c++-ts-mode)
+      (error "Buffer %s is not a c-ts-mode (c-ts-mode-set-style)"
+             (buffer-name)))
+  (c-ts-mode--indent-style-setter
+   'c-ts-mode-indent-style
+   (intern
+    (completing-read
+     "Select style: "
+     (mapcar #'car (c-ts-mode--indent-styles (if (eq major-mode 'c-ts-mode) 'c 'cpp)))
+     nil t nil nil "gnu"))))
+
 ;;; Syntax table
 
 (defvar c-ts-mode--syntax-table
@@ -255,11 +288,7 @@ c-ts-mode--set-indent-style
   (let ((style
          (if (functionp c-ts-mode-indent-style)
              (funcall c-ts-mode-indent-style)
-           (pcase c-ts-mode-indent-style
-             ('gnu   (alist-get 'gnu (c-ts-mode--indent-styles mode)))
-             ('k&r   (alist-get 'k&r (c-ts-mode--indent-styles mode)))
-             ('bsd   (alist-get 'bsd (c-ts-mode--indent-styles mode)))
-             ('linux (alist-get 'linux (c-ts-mode--indent-styles mode)))))))
+           (alist-get c-ts-mode-indent-style (c-ts-mode--indent-styles mode)))))
     `((,mode ,@style))))
 
 (defun c-ts-mode--top-level-label-matcher (node &rest _)
-- 
2.34.1


  reply	other threads:[~2023-01-26  8:27 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-21 11:11 bug#60983: 29.0.60; Tree-sitter user-level control Eli Zaretskii
2023-01-21 11:48 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-21 12:36   ` Eli Zaretskii
2023-01-21 12:40     ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-23 19:37       ` Yuan Fu
2023-01-23 19:59         ` Eli Zaretskii
2023-01-23 21:08           ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-23 23:55             ` Yuan Fu
2023-01-29 13:33               ` Eli Zaretskii
2023-01-29 19:12                 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-29 19:41                   ` Eli Zaretskii
2023-01-30  2:28                     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-30 13:45                       ` Eli Zaretskii
2023-01-24  3:26             ` Eli Zaretskii
2023-01-25 20:12               ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-25 21:16                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-26  8:27                   ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-01-26  6:08                 ` Eli Zaretskii
2023-01-26  6:25                   ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-23 16:52   ` Eli Zaretskii
2023-01-26  7:27     ` Eli Zaretskii
2023-01-26  7:37       ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-26  9:08         ` Eli Zaretskii
2023-01-28 13:12       ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-28 13:25         ` Eli Zaretskii
2023-01-28 18:41           ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-29 13:24             ` Eli Zaretskii
2023-01-26  7:56 ` Yuan Fu
2023-02-03  3:07 ` Yuan Fu
2023-02-03  7:47   ` Eli Zaretskii
2023-02-04 23:38     ` Yuan Fu
2023-02-05  6:01       ` Eli Zaretskii
2023-02-05  7:54         ` Yuan Fu
2023-02-05  9:23           ` Eli Zaretskii
2023-02-05  9:42             ` Yuan Fu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=875ycthf9h.fsf@thornhill.no \
    --to=bug-gnu-emacs@gnu.org \
    --cc=60983@debbugs.gnu.org \
    --cc=casouri@gmail.com \
    --cc=eliz@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=theo@thornhill.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.