Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some way to query the current buffers indent value #299

Open
mohkale opened this issue Apr 20, 2023 · 3 comments
Open

Add some way to query the current buffers indent value #299

mohkale opened this issue Apr 20, 2023 · 3 comments

Comments

@mohkale
Copy link
Contributor

mohkale commented Apr 20, 2023

This is probably somewhat out of scope but because this package is the best option for setting the indentation of a buffer and is already aware of all the indent variables associated with the various major-modes, I'm hoping you'd be receptive to supporting querying this value as well.

To give some context I'm one of the maintainers of apheleia. This is a package that lets you call formatter programs like black, clang-format, prettifier, etc. and update the text in the current Emacs buffer. A common need for a package like this is knowing what value to set the tab-width. Currently we maintain a partial list of common mode->variable associations but I think this is something better owned by editorconfig. I had thought we could just enumerate editorconfig-indentation-alist for this but the support for functional setters kind of breaks this :/.

If you're okay with it I'd like to add support for this to editorconfig and am open to suggestions on how to do this.

@jcs090218
Copy link
Contributor

I had thought we could just enumerate editorconfig-indentation-alist for this but the support for functional setters kind of breaks this :/.

Can you elaborate this part?

If you're okay with it I'd like to add support for this to editorconfig and am open to suggestions on how to do this.

It depends on in what way. If it's to make compatible with each another, then I second it. Other than that, I need more information to give an actual opinion.

cc @10sr

@mohkale
Copy link
Contributor Author

mohkale commented Apr 22, 2023

@jcs090218

Can you elaborate this part?

For reference this is what I had so far:

(seq-find
 (lambda (indent-var)
   (and (boundp indent-var)
        (symbol-value indent-var)))
 (ensure-list
  (alist-get major-mode editorconfig-indentation-alist)))

It returns the first variable in editorconfig-indentation-alist for the current mode that exists and is bound.

This has 2 faults. One it doesn't look for the parent mode of the current major mode (easy enough, editorconfig already has something like this when setting the indent level). It doesn't work when the indent configuration specifies a function like python-mode which uses editorconfig-set-indentation-python-mode.

It depends on in what way. If it's to make compatible with each another, then I second it.

One way I was considering was changing the contract of the indent variables so you can pass a an option to let it know what you wanted to do. If the option is a number then we retain the previous behaviour of setting the indent level. Otherwise we perform the action. For example:

(defun editorconfig-set-indentation-python-mode+ (mode)
  "Set `python-mode' indent size to SIZE."
  (cond
   ((numberp mode)
    (setq-local python-indent-offset mode)
    ;; For https://launchpad.net/python-mode
    (when (boundp 'py-indent-offset)
      (setq-local py-indent-offset mode)))
   ((eq mode :query)
    (bound-and-true-p python-indent-offset))))

(editorconfig-set-indentation-python-mode+ 2)
(editorconfig-set-indentation-python-mode+ :query)

Obvious downside is anyone with existing indent functions setup would set the indent to :query if they don't update it. I'd rather not create separte get-set functions but that's also an option. Alternatively it looks like all the functional ones just add a boundp check before setting the variable. It might be better to just remove this and set the variable regardless (I don't see much harm with that and the consistency benefit is nice).

@10sr
Copy link
Member

10sr commented Apr 28, 2023

Alternatively it looks like all the functional ones just add a boundp check before setting the variable. It might be better to just remove this and set the variable regardless (I don't see much harm with that and the consistency benefit is nice).

I think one reason that there are several "setter" functions is that there may be some cases where we want to compute offset value from indent_size rather than to use the value as is: editorconfig-set-indentation-latex-mode is an example for this: https://github.com/editorconfig/editorconfig-emacs/blob/master/editorconfig.el#L425 .
...Honestly, I don't know if this is important, because this function was already there when I first read the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants