Closed c02y closed 5 years ago
As a workaround, you could use (add-hook 'before-save-hook 'whitespace-cleanup)
to convert tabs to spaces on save.
@dolorsitatem I'm sorry, it doesn't work at all, the tab/space is still the same when I tried for the snippet I posted.
Before I, or anyone else, can effectively help you, please provide the following:
M-;
?Things work as expected, more or less, from where I'm sitting. However, I believe you when you say you're experiencing a bug. My questions are aimed at closing the gap between what I experience and what you experience. Until someone else can reproduce the behavior you're experiencing, I don't think anyone will be able to provide much insight.
What function are you calling when you press M-;?
M-;
is bound to comment-diwm-2
function
Please provide the actual text you're testing this on instead of a screenshot (or in addition to the screenshot)
int ABC = 0000; /* var1 */ int ABCASDASDS = 111111; /* var2 */ int ASDASDASDASDASDS = 22222; /* var3 */
The space/tab cannot be copied/pasted precisely in here like those in the screenshot, you may have to change it maually according to the screenshot if you want to try it.
Regarding the workaround, what did you do, precisely?
Turn on whitespace-mode
, select the code snippet and do M-;
, and check how the space/tab changes, that's all.(You may need to enable (add-hook 'before-save-hook 'whitespace-cleanup)
like you suggested.)
BTW, I'm not sure whether this is a bug, but it definitely doesn't handle the situation very well.
hey @c02y, sorry for the late reply
comment-dwim-2
is based on Emacs' own comment commands (in this particular case, it uses comment-indent
), so it should behave properly according to your own space & tabs configuration. It seems your problem is that your emacs is configured to use tabs (whitespace-mode
is only there for visualisation purposes).
Here is how you can disable tabs, comment-dwim-2
should behave as expected with this :
(setq indent-tabs-mode nil)
Alternatively, you could use an advice to convert tabs to spaces after the command has been applied.
(defadvice comment-indent (after comment-indent-with-spaces activate)
(untabify (line-beginning-position) (line-end-position)))
Does it solve your problem ?
@dolorsitatem thanks for helping out !
@remyferre
Since I write C/C++ code, the the indent-tabs-mode
is t
, I shouldn't use (setq indent-tabs-mode nil)
.
I use the alternative way, it works fine for situation when there are no indentation in the beginning of the multiple lines I want to add comments using M-;
, but if the multiple lines are inside a function or if/for, or any kind of situation that there are indentation before the lines, M-;
will also replace the indentation(tab) with spaces.
So it doesn't works as expected.
hmm I see, indent-tabs-mode
is indeed all-or-nothing so I would suggest you do something like this :
(defadvice comment-indent (around comment-indent-with-spaces activate)
(setq indent-tabs-mode nil)
ad-do-it
(setq indent-tabs-mode t))
This disables tabs only for the time of comment-indent
, your indentation should not be affected this way.
I tweaked it a little, so it works for both buffers like C/C++(indent-tabs-mode
= t
) and python(indent-tabs-mode
= nil
)
(defadvice comment-indent (around comment-indent-with-spaces activate)
(let ((orig-indent-tabs-mode indent-tabs-mode))
(when orig-indent-tabs-mode
(setq indent-tabs-mode nil))
ad-do-it
(when orig-indent-tabs-mode
(setq indent-tabs-mode t))))
If you want to add this into your package, please let me know, I'll remove this part from my init.el then.
By default, when I use
M-;
twice to append a comment at the end of line, it will separate the code and the comment with tabs instead of spaces, this will cause a problem, when I view the code in another code editor such as vim or tool like less, the comments of multiple lines will not aligned, even when I turn onwhitespace-mode
in Emacs, those comment lines will not be aligned.Is is possible to insert spaces instead of tabs between code and comment?
When I'm using
M-;
twice:When I turn on
whitespace-mode
:What I want: