Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Different indents for different documents

Help
2005-01-17
2013-04-26
  • Peter Mayne
    Peter Mayne
    2005-01-17

    I'd like to indent my Python scripts with four spaces, and my HTML/XML documents with two spaces. Is there a way of specifying per-document-type indents?

    Thanks.

     
    • There should be.  I knew I forgot something.  Franz suggested this.

      As long as I am at it, are there any other per document type preferences that should be set?

      (I can't think of any at the moment.)

       
    • For me applies:
      Eol Endings
      Use Tab/Spaces
      Tab/Indentation Width

      these two variables could be get from the settings
      self.addchar = "\t".expandtabs(2)
      self.tabwidth = 2

      self.addchar = "\t".expandtabs(GetTabWidth())
      self.tabwidth = GetTabWidth()
      or simply replace self.tabwidth with GetTabWidth()

       
    • Peter Mayne
      Peter Mayne
      2005-01-18

      Most of the things in the Prefences Document page could be set as per-type. For instance, word wrap might make sense for HTML, but not Python; the Python comment string doesn't make much sense in HTML or C++; etc.

       
    • Word wrap as well works.

      Comment string too.

      (This will be fun ;P).

      Ok.  So here is what I have so far:

      Extensions
      Folding
      Tab Width
      Indentation Type
      Line Ending Type
      Context Sensitive AutoIndent (At the moment, only for python)
      Comment String.

      At the moment, file types will be:
      Python, C/C++, HTML, Plain Text.

      Although I should probably add pyrex support at some point.

      I am thinking of implementing this as a dictionary,
      so that you'd have
      DrFrame.prefs.tabwidth[DrFrame.GetCurrentFileType()]

      The only thing I am a bit muddled about is letting people set these globally.

       
    • Peter Mayne
      Peter Mayne
      2005-01-20

      > The only thing I am a bit muddled about is letting people set these globally.

      Hmm, tricky. One option is not to have a global setting at all, and add a "Apply these preferences to all languages" button, similar to the existing "Apply Text Property To All Styles" button. After all, I only need to set up my document preferences once, so any pain will hopefully be soon forgotten.

      If someone wants to be really clever, they can edit the preferences file directly. :-)

      > I am thinking of implementing this as a dictionary,

      Why not make prefs aware of the current file type, so you don't have to pass it in every time? For per-type preferences, you could use a dictionary of file types, each of which stores the per-type dictionary. Then you could do something like

          def __getattr__(self, pref):
              if pref in self.__dict__:
                  return self.__dict__[pref]
              else:
                  return self.prefs[self.currentType][pref]

      and call it like

          prefs = drPreferences(type=...)
          tabwidth = prefs.tabwidth

      Less typing, looks nicer.

       
    • The problem is prefs are a member of DrFrame, and there can be multiple documents, each with their own file type.

      So the filetype has to be part of the Document.

      I could add functions like:

      DrFrame.GetTabWidth()
      or DrDocument.GetTabWidth()

       
    • Peter Mayne
      Peter Mayne
      2005-01-21

      Ah, so it is.

      If you added methods like GetTabWidth(), you need one for every preference, which is getting a little unwieldy, and clutters up the source browser.

      What about in DrFrame, something like:

      def getPreference(self, preference, language=None):
          if language:
              return self.prefs.perLanguagePrefs[language][preference]
          else:
              return self.prefs.__dict__[preference]

      where perLanguagePrefs is a dictionary keyed on language containing dictionaries keyed on preference names. (Or use some other data structure: you know the internals better than I do.)

      Now you can use

      DrFrame.getPreference('tabwidth', currentLanguage)

      for language-specific preferences, and

      DrFrame.getPreference('saveonrun')

      for global preferences.

       
    • Easy enough.

      DrFrame.GetPreference('doctabwidth', 0)

      Although you can also access prefs as a dictionary,
      so:

      DrFrame.prefs['doctabwidth[0]']
      and
      DrFrame.prefs['doctabwidth'][0]
      and
      DrFrame.prefs.doctabwidth[0]

      All work.