Autodetect indentation mode

94 views
Skip to first unread message

Johan Sölve

unread,
Oct 12, 2016, 11:33:56 AM10/12/16
to BBEdit Talk
Since I work in the middle of an ongoing indentation war (you know, tabs vs. spaces) I have a need for auto-detecting the indentation mode when opening a file that BBEdit haven't saved an "auto-expand tabs" setting for. 
Has anyone created such a script? 

I'm not even sure if it's possible to hook a script into opening files, no matter how a file was opened. I'm only familiar with menu scripts. It wasn't back in 2009 according to this thread https://groups.google.com/forum/#!topic/bbedit/67q66_8HaJ4

Christopher Stone

unread,
Oct 13, 2016, 2:25:14 AM10/13/16
to BBEdit-Talk
On Oct 12, 2016, at 10:15, Johan Sölve <johan...@gmail.com> wrote:
Since I work in the middle of an ongoing indentation war (you know, tabs vs. spaces) I have a need for auto-detecting the indentation mode when opening …


I'm not even sure if it's possible to hook a script into opening files, no matter how a file was opened.


Hey Johan,

Look for “Document attachment points” in the BBEdit manual and “documentDidOpen”.

You'll want your script to take this general form:

--------------------------------------
using terms from application "BBEdit"

   

   on documentDidOpen()
      beep
   end documentDidOpen

   

end using terms from
--------------------------------------

With a file-name like this: Document.documentDidOpen.scpt

I may have discovered a bug with this and have a query in to support.

NOTE:

I've done stuff like this before and don't really recommend it as an automated process.  I've had too many instances where something fairly critical got changed unexpectedly.

It's easy enough to create a keyboard shortcut driven script to do this.

Generally I use tabs anyway and detab if needed.

But of course YMMV.

--
Best Regards,
Chris

Dave

unread,
Oct 13, 2016, 8:07:14 AM10/13/16
to BBEdit Talk
I work on different projects with their own conventions, so I face similar challeneges. One thing that has helped has been convincing people to use EditorConfig, which BBEdit and most IDEs and editors support. It allows you to configure indentation formats for different languages, so they can use two-space indenting for YAML and 4 spaces for JavaScript. So you can leave your global defaults set to your own preferences and still comply with the arbitrary and often hare-brained demands of the people you sometimes have to work with.

Another solution I've toyed with but never actually implemented is using git hooks to change the formatting of files when they're checked in and checked out. If it could be done right, we'd be able to use any style we prefer locally and still have the files formatted appropriately for the project when we check them in.

Johan Sölve

unread,
Oct 13, 2016, 12:30:41 PM10/13/16
to BBEdit Talk, listm...@suddenlink.net


On Thursday, 13 October 2016 08:25:14 UTC+2, Christopher Stone wrote:
On Oct 12, 2016, at 10:15, Johan Sölve <johan...@gmail.com> wrote:
Since I work in the middle of an ongoing indentation war (you know, tabs vs. spaces) I have a need for auto-detecting the indentation mode when opening …

I'm not even sure if it's possible to hook a script into opening files, no matter how a file was opened.


Hey Johan,

Look for “Document attachment points” in the BBEdit manual and “documentDidOpen”.

[...] 
I've done stuff like this before and don't really recommend it as an automated process.  I've had too many instances where something fairly critical got changed unexpectedly.

Thanks for the pointer. Chris!

I did not intend to have the script make any changes, just set the indentation setting to match what is detected in the opened file. 


On Thursday, 13 October 2016 14:07:14 UTC+2, Dave wrote:
I work on different projects with their own conventions, so I face similar challeneges. One thing that has helped has been convincing people to use EditorConfig, which BBEdit and most IDEs and editors support. It allows you to configure indentation formats for different languages, so they can use two-space indenting for YAML and 4 spaces for JavaScript. So you can leave your global defaults set to your own preferences and still comply with the arbitrary and often hare-brained demands of the people you sometimes have to work with.

Dave, this is excellent! I tried .editorconfig and it works exactly how I want, just by adding the file to projects where it is missing (that would be just about every project for now). 
I'll go for this solution. I'll just try to convince my PHPstorm colleagues to install the plugin needed for that IDE. 

Even better would be if all developers agreed that that spaces are for alignment and tabs are for indentation (win/win), but that is obviously not going to happen.

(better still would be if BBEdit supported elastic tab stops; then we could use tabs also for alignment... http://nickgravgaard.com/elastic-tabstops/ )

Christopher Stone

unread,
Jan 23, 2017, 2:15:08 AM1/23/17
to BBEdit Talk
Hey Johan,

I have expanded upon my script (posted 2016/10/10) here:


--
Best Regards,
Chris

Reply all
Reply to author
Forward
0 new messages