Author Topic: Selecting extension for extensionless files  (Read 4034 times)

cb831

  • Community Member
  • Posts: 16
  • Hero Points: 0
Selecting extension for extensionless files
« on: May 16, 2010, 03:42:51 PM »
Hi I'm working with a number of files which need to be 5 chars long with no extension.

I figured I could change

Code: [Select]
int checkFirstLineForLanguage()
{
   save_pos(auto p);
:
:
   parse line with . '{{{' auto language '}}}' . ;
   if (language != '')
   {
       _SetEditorLanguage(language); /* CB 20100516.1457 */
   }
:
:
}
In that way I could add {{{myext}}} anywhere in the first line (inside relevant comment) and SE would act as if the extension was myext.

Problem is that this does only work for "well known SE languages" - If I define my own language _SetEditorLanguage(language) selects fundamental.

Is there any way to accomplish this ?

Thanks
Claus
« Last Edit: May 17, 2010, 10:35:13 PM by cb831 »

Sandra

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 754
  • Hero Points: 36
Re: Selecting extension for extensionless files
« Reply #1 on: May 17, 2010, 02:16:38 PM »
Are you on v15?  If so, have you checked out the Extensionless File Manager (Tools > Options > Languages > Extensionless File Manager)?  If your extensionless files are found in certain paths or have a pattern in their names, you can specify those paths or names in the file manager, and it will assign whatever language you choose to those files. 

cb831

  • Community Member
  • Posts: 16
  • Hero Points: 0
Re: Selecting extension for extensionless files
« Reply #2 on: May 17, 2010, 03:10:23 PM »
v15, yes
And yes I checked the feature because I thought it would do the trick for me.

However what is needed is a method to assign the "language" based on the content of the first few lines.
A simple regexp on first line would maybe be enough.
I my example I could use '\{\{\{(.*)\}\}\}" and match[1] / $1 would be the language.

Ideally you should have a table

\{\{\{(.*)\}\}\}--->$1
#!/.*/{bash|sh}--->bourneshell
etc

That would fix the hardcoded exceptions in the slick macros code also.

« Last Edit: May 17, 2010, 03:51:40 PM by cb831 »

Sandra

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 754
  • Hero Points: 36
Re: Selecting extension for extensionless files
« Reply #3 on: May 17, 2010, 07:45:40 PM »
I can't make any guarantees about a full feature to handle this sort of case.  However, for your particular example, I was able to get this to work using a user-defined language.  Is myext the mode name of the language (the string that shows up in the list under Tools > Options > Languages > Language Manager)?  Or is an extension found under Tools > Options > Languages > File Extension Manager that maps to a particular language?

You will need to send a language id to _SetEditorLanguage, rather than a mode name or an extension.  There are built-in functions to help you figure out what a language id for a language is, such as _Modename2LangId() and _Ext2LangId().

Let me know if that helps or if you're still hitting a wall.

cb831

  • Community Member
  • Posts: 16
  • Hero Points: 0
Re: Selecting extension for extensionless files
« Reply #4 on: May 17, 2010, 10:33:28 PM »

Code: [Select]
auto id = _Ext2LangId(language)           
_SetEditorLanguage(id); /* CB 20100516.1457 */

did the trick. The "Built in" languages I tried (like cs) must have had the same languageid as language.

Thanks a lot
Claus