Loading ...
Sorry, an error occurred while loading the content.

Re: Mixed tabs and spaces in comments

Expand Messages
  • Harry Whitfield
    The problem of dealing with the many issues surrounding the tab character arose in the early days of computers. I remember first having to deal with it in the
    Message 1 of 5 , Oct 24 8:01 AM
    • 0 Attachment
      The problem of dealing with the many issues surrounding the tab character arose
      in the early days of computers. I remember first having to deal with it in the
      mid sixties. Kernighan and Plauger discussed various solutions to the problem in
      their Software Tools book, which was first published in 1976.

      I do not wish to engage in the "religious war" which surrounds this issue, and
      suspect that I may be cast into outer darkness for the following comments.

      I program in JavaScript and make frequent use of JSLint. Most of the time, I
      edit code files using the BBEdit text editor on a Macintosh. This can be
      configured to have tabstops at fixed character intervals (by default 4) and
      facilitates use of files which contain both tab and space characters.

      In editing files, my main concern is to make the layout/indentation look correct
      on the screen, no matter whether that has been achieved with tabs or spaces or a
      mixture of both. I can't easily tell whether the desired layout has been
      achieved with tabs or spaces and I really don't want to have to care.

      BBEdit has commands to detab and entab text files, so I could easily produce a
      file without tabs to use as input to JSLint or to make available to other users.

      However, most of the time I do not need to detab files as a very simple wrapper
      to JSLint does all that is needed.

      In my Widget Tester Widget, I make use of an unmodified version of JSLint
      maintained by Douglas at http://www.JSLint.com/jslint.js which is a minified
      version of fulljslint.js.

      In addition to preferences to set the standard JSLint options, there is an
      additional preference to set a tabstops option:
      <preference name="tabstops" group="Operation" defaultValue="4"
      title="Tab-stop Interval:" type="popup"
      description="Specify the tab-stop interval (default is 4). Select 0 for standard JSLint behavior."

      This is similar to the standard indent option:
      <preference name="indent" group="Operation" defaultValue="4"
      title="Indentation:" type="popup"
      description="Number of spaces in a properly formatted block indentation."

      The code used in the wrapper to call JSLint on a text file takes the following form:

      function detab(data, tabspace) {
      var i, c, col = 0, out = "";
      for (i = 0; i < data.length; i += 1) {
      c = data[i];
      if (c === "\t") {
      do {
      out += " ";
      col += 1;
      } while ((col % tabspace) !== 0);
      } else if (c === "\n") {
      out += "\n";
      col = 0;
      } else {
      out += c;
      col += 1;
      return out;

      function filesystem_readFile(path, asLines) {
      var tabstops = Number(preferences.tabstops.value), data;
      if (tabstops === 0) {
      return filesystem.readFile(path, asLines);
      data = filesystem.readFile(path).replace(/\r\n?|\n/g, '\n');
      if (asLines) {
      return detab(data, tabstops).split('\n');
      return detab(data, tabstops);

      var theText = filesystem_readFile(theFilePath, true);
      var ok = JSLINT(theText, optionObject);

      filesystem.readFile(path, asLines) is the standard call used in Yahoo! Widgets
      to read a text file. If asLines is true the function returns an array of
      strings, one string per line, otherwise a single string including end-of-line

      The detab function converts tabs to the appropriate number of spaces.

      Setting the tabstops option to zero, passes the text unchanged to JSLint for the
      benefit of those who wish to have the standard JSLint behavior.

      The filesystem_readFile function also handles the various types of end-of-line
      character that can be used in BBEdit on the Macintosh, viz CR, CRLF and LF.

      The latest version of Widget Tester can be downloaded from http://tinyurl.com/5unocx .
      It can be unzipped to reveal all of the code.

    Your message has been successfully submitted and would be delivered to recipients shortly.