fix-pymode-tab-space.dpatch

G. Milde g.milde at web.de
Wed Jan 10 12:36:40 CET 2007


Dear Jörg, dear Jed packagers,

thanks for the new version of the tab-space patch. I still have some issues:

+private define py_whitespace(cnt)
+{
+   if ( get_blocal_var("py_use_tab") )
+      loop (cnt / TAB) insert_char('\t');

   This assumes every use of py_whitespace uses a multiple of TAB.
   However there are cases (in continuation lines or multi-line literal
   strings) where this is not the case, e.g.

   +	 py_whitespace(level * Py_Indent_Level + (col - current_indent));

+   else
+      loop (cnt) insert_char(' ');
+}

My suggestion would be:

+private define py_whitespace(n)
+{
+   if ( get_blocal_var("py_use_tab") )
+      whitespace(n);
+   else
+      insert_spaces(n);
+}
+

The choice of py_use_tab is non-intuitive and undocumented. 

The choice of hard-tabs or spaces for indentation is widely disputed. The
python documentation recommends spaces over tabs but there are good
arguments for both cases. The Python interpreter always uses 8 spaces to
expand a "\t" before calculating the indent of a line.

+   set_blocal_var(Py_Indent_Level == TAB, "py_use_tab");

With this logic, 

if I want to use tabs, I need to:

  add a python_mode_hook to set TAB to Py_Indent_Level or make sure
  Py_Indent_Level == TAB_DEFAULT (affecting all other editing as well).
  
  Always remember to change both, Py_Indent_Level and the mode-specific
  TAB if I ever want to use a different indent level
  
if I do not want to use tabs, I need to:

  make sure, that TAB_DEFAULT != Py_Indent_Level or add a
  python_mode_hook to set TAB to something different than Py_Indent_Level.
  
  Remember to make sure this holds for any change of Py_Indent_Level
  

My suggestion is to use the Py_Indent_Level variable for this distinction:

%!%+
%\variable{Py_Indent_Level}
%\synopsis{Number of spaces to indent a code block}
%\usage{variable Py_Indent_Level = 4}
%\description
%  \sfun{python->get_indent_level} determines the buffer-local indentation
%  level from the first indented code line. \var{Py_Indent_Level} is the
%  default, used for the buffer-local indent-level, for a buffer
%  without indented code lines (e.g. a new one).
%  
%  The pre-set value of 4 spaces corresponds to the "Style Guide for Python
%  Code" (http://www.python.org/dev/peps/pep-0008/)
%  
%  The special value 0 means use hard tabs ("\\t") for indentation.
%\example
%  To have tab-indentation by default with a tab-width of 3, write in jed.rc
%  something like
%#v+
%    Py_Indent_Level = 0;
%    TAB_DEFAULT = 3;
%#v-
%  If the global \var{TAB_DEFAULT} should not be touched, set the buffer local
%  TAB in a \sfun{python_mode_hook}.
%!%-
custom_variable("Py_Indent_Level", 4);


High on the wishlist are also automatic determination of the indent level
and a mode menu. This and some bugfixes as well as improved indentation
logic is in my pymode.sl version at jedmodes. I'll suggest my changes to
J. E. D. and will wait for his comments/decision until further advancing
this case.

Günter




More information about the Pkg-jed-devel mailing list