Here is an example file (/tmp/asdf.sh
), for which I want dtrt-indent-mode
to do the right thing.
#! /usr/bin/env bash
function asdf()
{
printf "asASDFASDF"
echo "1234"
}
printf "qwerqwerqwer\n"
result=$(asdf)
if [ df ] ; then
if [ er ] ; then
wefwefew
fi
fi
I have created a minimal Emacs init file (~/tmp/basic-init.el
) to test this:
(require 'package)
(setq package-enable-at-startup nil)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
(add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/"))
(add-to-list 'package-archives '("melpamilk" . "http://melpa.milkbox.net/packages/"))
(package-initialize)
(unless package-archive-contents
(package-refresh-contents))
(unless (package-installed-p 'use-package)
(package-install 'use-package))
(require 'use-package)
(defun my-prog-mode-hook ()
(dtrt-indent-mode))
(use-package dtrt-indent
:ensure t
:config (dtrt-indent-mode t))
(use-package blank-mode
:ensure t)
(global-set-key (kbd "C-c b") 'blank-mode)
Starting emacs with that init file, opening that shell file:
emacs -q -l ~/tmp/basic-init.el /tmp/asdf.sh
then showing blank characters with C-c b
, gives this:
(Don't mind the yellow highlight, blank-mode should be setup to leave that alone.)
Going to that yellow line, deleting a space char then pressing TAB
gives this:
According to me, the right thing to do is not to put a Tab character there.
What I have found trying to understand what is going on:
Luckily, dtrt-indent
has a diagnosis tool. This is the output of dtrt-indent-diagnosis
, on the original file (no Tab character):
Guessing offset for /tmp/asdf.sh
Elapsed time for analysis: 0.001 seconds
Total relevant lines: 5 out of 17 (limit: 5000)
Histogram:
4x 4 spaces
1x 8 spaces
Analysis:
offset 2 works for 100.00% of relevant lines, matching 2 distinct offsets - merged with offset 4 (0.00% deviation, limit 20.00%)
offset 4 works for 100.00% of relevant lines, matching 2 distinct offsets - CONSIDERED
offset 8 works for 20.00% of relevant lines, matching 1 distinct offsets - CONSIDERED
offset 3 works for 0.00% of relevant lines, matching 0 distinct offsets - rejected: too few distinct matching offsets (1 required)
offset 5 works for 0.00% of relevant lines, matching 0 distinct offsets - rejected: too few distinct matching offsets (1 required)
offset 6 works for 0.00% of relevant lines, matching 0 distinct offsets - rejected: too few distinct matching offsets (1 required)
offset 7 works for 0.00% of relevant lines, matching 0 distinct offsets - rejected: too few distinct matching offsets (1 required)
Summary:
Best guess is offset 4 with 100.00% matching lines (80.00% required)
Hard tab percentage: 0.00% (0 lines), -100.00% superior to soft tabs (threshold 300.00%)
Soft tab percentage: 100.00% (5 lines), inf% superior to hard tabs (threshold 300.00%)
Conclusion:
Guessed offset 4 with 100% confidence.
Change indent-tab-setting: yes, to nil
I did not manage to find help on indent-tab-setting
.
However, C-h k
on TAB
key press gives this:
TAB (translated from <tab>) runs the command indent-for-tab-command
(found in global-map), which is an interactive compiled Lisp function
in ‘indent.el’.
It is bound to TAB.
(indent-for-tab-command &optional ARG)
Indent the current line or region, or insert a tab, as appropriate.
This function either inserts a tab, or indents the current line,
or performs symbol completion, depending on ‘tab-always-indent’.
The function called to actually indent the line or insert a tab
is given by the variable ‘indent-line-function’.
and C-h v
on indent-line-function
:
indent-line-function is a variable defined in ‘indent.el’.
Its value is ‘smie-indent-line’
Local in buffer asdf.sh; global value is indent-relative
Apparently, smie-
is a default naive indentation function.
I was expecting to find a dtrt-
function here.
How can I make dtrt-indent-mode
do the right thing?
GNU Emacs 26.1.
dtrt-indent
is recommending that you change the value ofindent-tabs-mode
to nil for that file/buffer, to prevent the insertion of tabs.dtrt-indent-mode
is. It doesn't seem to be in vanilla Emacs.