Author Topic: How to create Alias files?  (Read 7415 times)

daviescj

  • Community Member
  • Posts: 8
  • Hero Points: -11
How to create Alias files?
« on: November 21, 2006, 11:23:02 pm »
I was trying to figure out how to have SlickEdit insert a date and time automatically when I open a new, empty text buffer, so I thought of using an alias. However, I don't have an alias file for fundamental mode. Even if I create one, SlickEdit doesn't see it. 

What I really want is to be able to open a new, empty buffer and have SlickEdit automatically insert a comment like this:

/* 11/21/2006 3:31:21 PM */

Any suggestions?

« Last Edit: November 21, 2006, 11:31:58 pm by daviescj »

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: How to create Alias files?
« Reply #1 on: November 22, 2006, 10:54:32 am »
There should be a alias.slk file in your config dir.
It contains your 'fundamental' aliases and is used when no specific alias file is specified for the current file extension (@see File ext. setup).
You can edit alias.slk as usual using the 'alias' command (alias editor) and select it from the list.
(If it's lost somehow try to create a new one or re-install Slick.)

For automagic insertion of the date - comment you could try this example using Slicks callback mechanism.
Jusrt copy'n paste the code into your vusrmacs.e file in your config dir in case you don't already use your own macro collection module and load it with 'Macro->Load module'.
Code: [Select]
#include 'slick.sh'

void _buffer_add_insert_datetime_cmt (...)
{
   _str cmt_begin = '//';
   _str cmt_end   = '';

   if ( _bufname2ext( p_buf_name ) == 'c' )
   {
      // all file ext.s referring to 'c' (e.g. h, cpp, ...)
      cmt_begin = '/*';
      cmt_end   = '*/';
   }
   else if (_bufname2ext( p_buf_name ) == 'bat')
   {
      // all file ext.s referring to 'bat' (e.g. cmd, ...)
      cmt_begin = '@REM'
      cmt_end   = '';
   }

   top();
   _insert_text ( cmt_begin " " _date ('U') " " _time( 'T' ) " " cmt_end );
   split_insert_line();
}

Have fun,

HS2

Graeme

  • Senior Community Member
  • Posts: 2298
  • Hero Points: 301
Re: How to create Alias files?
« Reply #2 on: November 22, 2006, 11:36:26 am »

hs2 - hope you won't mind if I point out a slight problem here - I think _buffer_add functions get called whenever a "new buffer" is created, including for existing non-empty files, so I think your macro would insert the time and date every time the file was opened in slickedit, not just when it's empty  :)

Maybe add a check on p_Noflines at the start
if (p_Noflines > 1)
    return;

Graeme

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: How to create Alias files?
« Reply #3 on: November 22, 2006, 11:47:09 am »
@Graeme:
Hmm ... tried it and it's only invoked for a newly created file here.
But I agree that for robustness reasons the p_Noflines check could be added.

@daviescj:
You may also use an (ext. specific) alias instead of inline macro code inserting your datetime header.
Example:
Code: [Select]
void _buffer_add_insert_datetime_cmt (...)
{
   expand_alias ( 'ins-datetime-header' );
}
If you created a c-file and you specified 'c.als' as alias file for this extension the 'ins-datetime-header' alias from c.als is used.
If you do the same for an e-file the 'ins-datetime-header' alias from e.als is used (if specified).

Maybe a better approach.

HS2

Graeme

  • Senior Community Member
  • Posts: 2298
  • Hero Points: 301
Re: How to create Alias files?
« Reply #4 on: November 22, 2006, 11:56:21 am »

Quote
Hmm ... tried it and it's only invoked for a newly created file here.
But I agree that for robustness reasons the p_Noflines check could be added.

mm, strange.  Are you sure?  On my machine, it's definitely called when any existing non empty file is opened.

Graeme

hs2

  • Senior Community Member
  • Posts: 2737
  • Hero Points: 288
Re: How to create Alias files?
« Reply #5 on: November 22, 2006, 02:04:21 pm »
@Graeme: You're right... Dunno, what I tried before ???

Final example:
Code: [Select]
void _buffer_add_insert_datetime_cmt (...)
{
   // ensure that its a brand new empty buffer
   if ( p_Noflines > 1 ) return;

   // alternatively make use of (ext.specific) alias facility
   // add a 'insert-datetime-cmt' alias to the alias file specified for the file ext.s where this header should be added.
   
   // expand_alias ( 'insert-datetime-cmt' );

   // or use this macro code...
   
   _str cmt_begin = '//';
   _str cmt_end   = '';

   if ( _bufname2ext( p_buf_name ) == 'c' )
   {
      // all file ext.s referring to 'c' (e.g. h, cpp, ...)
      cmt_begin   = '/*';
      cmt_end     = '*/';
   }
   else if (_bufname2ext( p_buf_name ) == 'bat')
   {
      // all file ext.s referring to 'bat' (e.g. cmd, ...)
      cmt_begin   = '@REM'
      cmt_end     = '';
   }

   top();
   _insert_text ( cmt_begin " " _date ('U') " " _time( 'T' ) " " cmt_end );
   split_insert_line();
}

HS2
« Last Edit: November 22, 2006, 02:07:25 pm by hs2 »

Lee

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1247
  • Hero Points: 125
Re: How to create Alias files?
« Reply #6 on: November 22, 2006, 02:24:52 pm »
You might also try using code templates available in version 11, it allows you to create exactly what you want.