/* 3.50.2 ###############################################################
   
			SMAC FILE USED BY XCORAL EDITOR
   
   File: xcoralrc.lf
   Path: /home/fournigault/c/X11/xcoral-2.33/SmacLib/xcoralrc.lf
   Description: 
   Created: Sun Aug  7 14:52:32 MET 1994
   Author: Thierry Emery
   Modified: Fri Feb 17 15:38:00 MET 1995
   Last maintained by: Lionel Fournigault
   
   RCS $Revision$ $State$
   
   
   ####################################################################
   
   Note: initialization file for Xcoral
   
   ####################################################################
   
   Copyright (c) : Thierry Emery
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   
   #################################################################### */


/* --------------------------------------------------------------------
   Load standard libraries
   -------------------------------------------------------------------- */
{
  /* general SMAC programmer and XCORAL user utilities */
  load_file("utilities.sc");
  
  /* C and C++ mode, auto indent, reindent, indent region etc */
  load_file("mode.sc" );

  /* provide class method and function profile */
  load_file("head.sc");
  
  /* extract logical pathname from automount pathname */
  load_file("hack-filename.sc");

  /* command shell utilities, grep make user interface */
  load_file("cmd.sc" );
  
  /* for us frenchies */
  load_file("french.sc");

  /* SMAC functions writer and/or user help */
  load_file("describe.sc");
  
  /* extensions of C and C++ modes */
  load_file("mode-ext.sc");
  
  /* mouse customization */
  load_file("mouse.sc");
  
  /* C C++ comments facilities */
  load_file("comments.sc");
  
  /* file title and custom organization title */
  load_file("title.sc");
  
  /* save file utilities */
  load_file("save.sc");
  
  /* global set key utilities */
  load_file("keydef-ext.sc");
  
  /* word completion */
  load_file("complete-word.sc");
  
  /* window-utilities */
  load_file("window-utilities.sc");
  
  /* compare two windows */
  load_file("compare-win.sc");

  /* latex indent */
  load_file("latex.sc");

  /* color region buffer */
  load_file("color.sc");

  /* edit directory */
  load_file("edir.sc");

  /* rcs interface */
  load_file("rcs.sc");
  
  /* Version control */
  load_file("version.sc");
  
  /* Latex utilities */
  load_file("latex-macros.sc");
  
  /* Misc utilities */
  load_file("misc-commands.sc");
 
 /* html utilities */
  load_file("html.sc");

  /* java mode */
  load_file("java.sc");

  /* hanoi demo */
  load_file("hanoi.sc");

  /* shell mode */
  load_file("shell-script.sc");
  
  /* javascript mode */
  load_file("javascript.sc");
  
  /* python */
  load_file("python.sc");

  /* ada mode */
  load_file("ada.sc");
  
  /* fortran90 mode */
  load_file("fortran.sc");

  /* perl mode */
  load_file("perl.sc");
}

/* --------------------------------------------------------------------
   Define font for each mode.
   
   Default fonts available:
   
     "DejaVu Sans Mono:style=book:size=10:antialias=true"
     "Liberation Mono:style=Regular:size=10:antialias=true"
     "Source Code Pro:style=Regular:size=10:antialias=true"
     "DejaVu Serif:style=book:size=10:antialias=true"
     
   else use shell command fc-list to choose TrueType font
   
   -------------------------------------------------------------------- */
{
  set_mode_font("default", "DejaVu Sans Mono:style=book:size=10:antialias=true");
  set_mode_font("C-mode", "DejaVu Sans Mono:style=book:size=10:antialias=true");
  set_mode_font("C++mode", "DejaVu Sans Mono:style=book:size=10:antialias=true");
  set_mode_font("Python", "DejaVu Sans Mono:style=book:size=10:antialias=true");
  set_mode_font("JavaScript", "DejaVu Sans Mono:style=book:size=10:antialias=true");
  set_mode_font("Java", "DejaVu Sans Mono:style=book:size=10:antialias=true");
  set_mode_font("Latex", "DejaVu Sans Mono:style=book:size=10:antialias=true");
  set_mode_font("Html", "DejaVu Sans Mono:style=book:size=10:antialias=true");
  set_mode_font("Ada", "DejaVu Sans Mono:style=book:size=10:antialias=true");
  set_mode_font("Perl", "DejaVu Sans Mono:style=book:size=10:antialias=true");
  set_mode_font("Fortran", "DejaVu Sans Mono:style=book:size=10:antialias=true");
  set_mode_font("shell", "DejaVu Sans Mono:style=book:size=10:antialias=true");
  set_mode_font("Shell", "DejaVu Sans Mono:style=book:size=10:antialias=true");
  set_mode_font("Edir", "DejaVu Sans Mono:style=book:size=10:antialias=true");
  set_mode_font ("french", "DejaVu Sans Mono:style=book:size=10:antialias=true");
}

/* ---------------------------------------------------------------------
   define colors
   Generics colors for all modes with background set to azure
   --------------------------------------------------------------------- */
{
  gen_comment_color = "limegreen";
  gen_include_color = "goldenrod";
  gen_define_color = "darkgreen";
  gen_keyword_color = "peru";
  gen_string_color = "steelblue";
  gen_varfunc_color = "tomato";
  gen_decl_color = "forestgreen";
  gen_class_color = "purple";
  gen_modifier_color = "tomato";
  gen_result_color = "lightsteelblue";
  gen_del_new_color = "red";
  gen_return_color = "dodgerblue";
  gen_link_color = "tan";
  gen_makefile_color = "plum";
  gen_html_color = "darkgreen";
  
  /* C,C++ colors */
  cpp_comment_color = gen_comment_color;
  cpp_keyword_color = gen_keyword_color;
  cpp_define_color = gen_define_color;
  cpp_string_color = gen_string_color;
 
  /* python colors */
  python_comment_color = gen_comment_color;
  python_keyword_color = gen_keyword_color;
  python_define_color = gen_define_color;
  python_string_color = gen_string_color;

  /* Java colors */
  java_comment_color = gen_comment_color;
  java_import_color = gen_define_color;
  java_category_color = gen_del_new_color;
  java_result_color = gen_return_color;
  java_class_color = gen_class_color;
  java_modifier_color = gen_modifier_color;
  java_keyword_color = gen_keyword_color;
  java_package_color = gen_define_color;
  java_string_color = gen_string_color;

  /* JavaScript colors */
  javascript_comment_color = gen_comment_color;
  javascript_result_color = gen_return_color;
  javascript_class_color = gen_class_color;
  javascript_modifier_color = gen_modifier_color;
  javascript_keyword_color = gen_keyword_color;
  javascript_html_color = gen_html_color;
  javascript_import_color = gen_define_color;
  javascript_string_color = gen_string_color;
  javascript_package_color = gen_define_color;
  
  /* latex colors */
  latex_comment_color = gen_comment_color;
  latex_keyword_color = gen_keyword_color;
  latex_defun_color = gen_decl_color;
  latex_define_color = gen_define_color;
  latex_decl_color = gen_decl_color;
  latex_label_color = gen_keyword_color;
  latex_include_color = gen_include_color;
  latex_italic_color = gen_decl_color;
  latex_bold_color = gen_varfunc_color;
  latex_ref_color = gen_comment_color;

  /* html colors */
  html_title_color = gen_comment_color;
  html_ibtt_color = gen_keyword_color;
  html_pre_color = gen_define_color;
  html_img_color = gen_decl_color;
  html_ref_color = gen_varfunc_color;
  html_list_color = gen_define_color;
  html_forms_color = gen_include_color;
  html_hds_color = gen_keyword_color;
  hltml_string_color = gen_string_color;
  
  /* Edir colors */
  edir_directory_color = gen_comment_color;
  edir_link_color = gen_link_color;
  edir_c_file_color = gen_decl_color;
  edir_tex_file_color = gen_decl_color;
  edir_sc_file_color = gen_decl_color;
  edir_h_file_color = gen_keyword_color;
  edir_makefile_color = gen_string_color;
  edir_readme_color = gen_del_new_color;

  /* Perl colors */
  perl_comment_color = gen_comment_color;
  perl_string_color = gen_string_color;
  perl_label_color = gen_define_color;
  perl_include_color = gen_include_color;
  perl_decl_color = gen_decl_color;
  perl_defun_color = gen_class_color;
  perl_keyword_color = gen_keyword_color;
  
  /* Fortran90 colors */
  f90_comment_color = gen_comment_color;
  f90_string_color  = gen_string_color;
  f90_unit_color    = gen_class_color;
  f90_include_color = gen_include_color;
  f90_type_color    = gen_decl_color;
  f90_decl_color    = gen_decl_color;
  f90_keyword_color = gen_keyword_color;
  
  /* Ada color */
  ada_comment_color = gen_comment_color;
  ada_glob_struct_color = gen_varfunc_color;
  ada_struct_color = gen_keyword_color;
  ada_decl_color = gen_decl_color;
  ada_include_color = gen_include_color;
  ada_string_color = gen_string_color;

  /* Shell colors */
  shell_comment_color = gen_comment_color;
  shell_string_color = gen_string_color;
  shell_include_color = gen_include_color;
  shell_define_color = gen_define_color;
  shell_var_color = gen_varfunc_color;
  shell_keyword_color = gen_keyword_color;
}

/* ---------------------------------------------------------------------
   define indentation variables (mode C, C++,  Java and other mode)
   --------------------------------------------------------------------- */
{
  c_indent_in_comment = 3;
  c_indent_in_string = 0;
  c_indent_in_parenthesis = 1;
  c_indent_in_bracket = 1;
  c_indent_in_brace = 2;
  c_indent_in_statement = 2;
  c_arg_decl_indent = 4;

  java_indent_step = 2;
  javascript_indent_step = 2;
  ada_indent_step = 2;
  fortran_indent_step = 2;
  perl_indent_step = 2;
  python_indent_step = 4;
}

/* ---------------------------------------------------------------------
   define suffixes
   --------------------------------------------------------------------- */
{
  set_mode_suffixes("C++mode",".c .cc .cpp .h .hh .sc .xcoralrc");
  set_mode_suffixes("Python",".py");
  set_mode_suffixes("Java",".java");
  set_mode_suffixes("C-mode",".c .h .sc .xcoralrc");
  set_mode_suffixes("Latex",".tex .latex .sty");
  set_mode_suffixes("Html",".html");
  set_mode_suffixes("Perl",".pl");
  set_mode_suffixes("Fortran",".f .f90");
  set_mode_suffixes("Ada",".a");
  set_mode_suffixes("Python",".py");
  set_mode_suffixes("JavaScript",".js");
  
  set_mode_suffixes("french",".txt .texte");
}

/* ---------------------------------------------------------------------
   define modes on which global_key_def operates
   --------------------------------------------------------------------- */
{
  globalize_mode("default");
  globalize_mode("C-mode");
  globalize_mode("C++mode");
  globalize_mode("Python");
  globalize_mode("Java");
  globalize_mode("JavaScript");
  globalize_mode("Latex");
  globalize_mode("Html");
  globalize_mode("Perl");
  globalize_mode("Ada");
  globalize_mode("Fortran");
  globalize_mode("shell");
  globalize_mode("french");
  globalize_mode("Shell");
}

/* ---------------------------------------------------------------------
   Util Smac functions.
   --------------------------------------------------------------------- */
{
  global_key_def("^[>", "goto_end_of_file");

  global_key_def("^xt", "transpose_chars");
  global_key_def("^xy", "transpose_forms");
  global_key_def("^[\\", "delete_line_blanks");
  global_key_def("^[ ",  "just_one_blank");
  global_key_def("^[k",  "delete_to_beginning_of_line");
  global_key_def("^xc",  "center_line");
  global_key_def("^[m", "recenter");
  
  global_key_def("^x#", "sharp_comment");
  global_key_def("^x+", "plus_comment");
  global_key_def("^x=", "equal_comment");
  global_key_def("^x-", "minus_comment");
  global_key_def("^x%", "percent_comment");
  global_key_def("^xz", "update_title_and_save_file");
  global_key_def("^xs", "update_title_backup_and_save_file");
  global_key_def("^[/", "complete_word");
  global_key_def("^xC", "CompareAgain");
  global_key_def("^xg", "go_next");
  global_key_def("^xa", "color_buffer");
  
  global_key_def("^xe", "edir");
  key_def("Edir", "^xe", "edir");

  key_def("Shell", "^[b", "backward_c_form");
  key_def("Shell", "^[f", "forward_c_form");
  key_def("Shell", "^[d", "delete_next_c_form");
  key_def("Shell", "^[\b", "delete_previous_c_form");
  key_def("Shell", "^[\177", "delete_previous_c_form"); /* esc delete */
  key_def("Shell", "^x^c", "quit_shell");

  key_def("Latex","^[x", "latex_back_indent");
}

/* ----------------------------------------------------------------------------
   French accents in Latex mode.
   ---------------------------------------------------------------------------- */
{
    key_def("Latex", "'", "french_accent");
    key_def("Latex", "`", "french_accent");
    key_def("Latex", "^^", "french_accent");
    key_def("Latex", "\"", "french_accent");
}

/* --------------------------------------------------------------------------------
   Comment region with //
   -------------------------------------------------------------------------------- */
comment_region () {
  int currentline, markline, count=0;

  if (mark_position() == -1) /* no mark */
    return;
  
  currentline = current_line();
  goto_mark();
  markline = current_line();
  goto_line(currentline);

  if (currentline == markline)
    return;
   
  while (currentline != markline) {
    goto_beginning_of_line();
    insert_string("//");
    if (currentline > markline)
      goto_previous_line();
    else
      goto_next_line();
    currentline = current_line();
    count += 2;
  }
  goto_beginning_of_line();
  insert_string("//");

  goto_line(markline);
  goto_beginning_of_line();
  color_buffer();
  redisplay();
}

/* --------------------------------------------------------------------------------
   Remove comment // in region
   -------------------------------------------------------------------------------- */
uncomment_region () {
  int currentline, markline;

  if (mark_position() == -1) /* no mark */
    return;
  
  currentline = current_line();
  goto_mark();
  markline = current_line();
  goto_line(currentline);

  if (currentline == markline)
    return;
   
  while (currentline != markline) {
    goto_beginning_of_line();

    if ((current_char() == '/') && (next_char()=='/')){
      delete_char();
      delete_char();
    }
    else {
      goto_line(markline);
      goto_beginning_of_line();
      color_buffer();
      redisplay();
      return;
    }
    
    if (currentline > markline)
      goto_previous_line();
    else
      goto_next_line();
    currentline = current_line();
  }
  goto_beginning_of_line();
  if ((current_char() == '/') && (next_char()=='/')){
    delete_char();
    delete_char();
  }

  goto_line(markline);
  goto_beginning_of_line();
  color_buffer();
  redisplay();
}

/* --------------------------------------------------------------------------------
   Edit .xcoralrc file
   -------------------------------------------------------------------------------- */
edit_xcoralrc () {
  int w, result;
  char *path, *home, *file;
  
  home = cmd_shell_to_string("echo $HOME");
  path = (char*) malloc(strlen(home) + 11);
  sprintf ( path, "%s/.xcoralrc", home);
  
  w = new_window();
  select_window(w);
  
  result = read_file(path);
  if (result == -1)
    display_message("\n>>>\nFile already load");
  else if (result == 0)
    display_message("\n>>>\nFile not found");
  else
    color_buffer();
}

/* --------------------------------------------------------------------------------
   Edit .bashrc file
   -------------------------------------------------------------------------------- */
edit_bashrc () {
  int w, result;
  char *path, *home, *file;
  
  home = cmd_shell_to_string("echo $HOME");
  path = (char*) malloc(strlen(home) + 11);
  sprintf ( path, "%s/.bashrc", home);
  
  w = new_window();
  select_window(w);
  
  result = read_file(path);
  if (result == 0 ){
      sprintf ( path, "%s/.bash_profile", home);
      result = read_file(path);
      if ( result == -1)
	display_message("\n>>>\nFile already load");
      else if (result == 0)
	display_message("\n>>>\nFile not found");
      else
	;
  }
  else if (result == -1)
    display_message("File already load");
  else
    color_buffer();
}

/* --------------------------------------------------------------------------------
   Edit SmacLib file
   -------------------------------------------------------------------------------- */
edit_smaclib_file() {
    char *str, *item, *smacdir, *cmd, *file;
    int win = current_window();
    int i, len, window;
    
    smacdir = cmd_shell_to_string("echo $XCORAL_SMACLIB");
    if (strcmp(smacdir,"")==0){
      smacdir = (char*) malloc(strlen("/usr/local/lib/xcoral") + 1);
      smacdir = sprintf (smacdir, "/usr/local/lib/xcoral");
    }
    cmd = (char*) malloc(strlen(smacdir) + 5); 
    sprintf (cmd, "ls %s", smacdir);

    str = cmd_shell_to_string(cmd);
    item = (char*) malloc(64);
    clear_list();

    /* parcours de la chaine pour extraire les noms de fichiers */
    i = 0;
    while (*str != 0) {
      if (*str == 10){
	item[i] = 0;
	if (strcmp(item, "man.dtex")!=0)
	  add_list_item ( item );
	item = (char*) malloc(64);
	i = 0;
	str++;
      }
      item[i] = *str;
      i++;
      str++;
    }
    item[i] = *str;
    add_list_item (item );/* le dernier de la liste */

    file = select_from_list("Edit SmacLib file");
    if (file == 0)
      return;
    window = new_window();
    sprintf (str, "%s/%s", smacdir, file);
    select_window(window);
    if (read_file(str) == -1)
      display_message("File already load");
    else
      color_buffer();
}

/* --------------------------------------------------------------------------------
   Display special chars (160 to 256)
   -------------------------------------------------------------------------------- */
display_special_chars(){
  int i=160, w;
  
  w = new_window();
  select_window(w);

  while (i<256){
    insert_char(i);
    if (i==240)
      insert_char('\n');
    i++;
  }

  while (i<256){
    insert_char(i);
    if (i==240)
      insert_char('\n');
    i++;
  }
}

/* ----------------------------------------------------------------------------
   User commands: call by Users commands item in Misc Menu or toolbar
   ---------------------------------------------------------------------------- */
user_commands()
{
    char *str;
    int win = current_window();

    clear_list();
    add_list_item("Comment region");
    add_list_item("Uncomment region");
    add_list_item("Edit .xcoralrc");
    add_list_item("Edit .bashrc");
    add_list_item("Edit Smaclib files");
    add_list_item("Display special chars");

    str = select_from_list("User commands");
    redisplay();
    select_window(win);
    
    if(str==0 || strlen(str) < 2) {
	return;
    }
    
    if (strcmp(str,"Comment region")==0) {
	comment_region();
	return;
    }
    if (strcmp(str,"Uncomment region")==0) {
	uncomment_region();
	return;
    }
    if (strcmp(str,"Uncomment region")==0) {
	uncomment_region();
	return;
    }
    if (strcmp(str,"Edit .xcoralrc")==0) {
	edit_xcoralrc();
	return;
    }
    if (strcmp(str,"Edit .bashrc")==0) {
	edit_bashrc();
	return;
    }
    if (strcmp(str,"Edit Smaclib files")==0) {
	edit_smaclib_file();
	return;
    }
    if (strcmp(str,"Display special chars")==0) {
	display_special_chars();
	return;
    }
}
