home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / JED / JED097-1.TAR / jed / lib / site.sl < prev    next >
Encoding:
Text File  |  1994-12-12  |  32.2 KB  |  1,396 lines

  1. %_traceback = 1;
  2. %               Site specific initialiation file.
  3. % This file must be present in the JED_LIBRARY.  JED loads it first--- even
  4. % before reading command line arguments.  The command line arguments are then
  5. % passed to a hook declared in this file for further processing.
  6. % In addition to some hooks, this file declares some autoloads for various
  7. % functions and defines utility functions.  Any user specific stuff should be
  8. % placed in the jed.rc (.jedrc) user startup file.  Only put here what you
  9. % believe EVERY user on your system should get!
  10. % The best way to make changes in this file is to put all your changes in a
  11. % separate file, defaults.sl.  defaults.sl is NOT distributed with JED.  Code
  12. % at the edn of this file checks for the existence of `defaults.sl' and loads
  13. % it if found. Functions occuring in this file (site.sl) may be overloaded in
  14. % defaults.sl. Making changes this way also makes it easier to upgrade to
  15. % future JED versions.
  16.  
  17.  
  18. %!% A function to contat a directory with a filename.  Basically checks
  19. %!% for the final slash on the dir and adds on if necessary
  20. define dircat(dir, file)
  21. {
  22.    variable n = strlen(dir);
  23.    
  24. #ifdef MSDOS OS2
  25.    variable slash = "\\";
  26.    if (n)
  27.      {
  28.     if (strcmp(substr(dir, n, 1), slash)) dir = strcat(dir, slash);
  29.      }
  30.    strcat(dir, file);
  31. #endif
  32. #ifdef UNIX
  33.    variable slash = "/";
  34.    if (n)
  35.      {
  36.     if (strcmp(substr(dir, n, 1), slash)) dir = strcat(dir, slash);
  37.      }
  38.    strcat(dir, file);
  39. #endif
  40. #ifdef VMS
  41.    % convert a.dir;1 to [.a] first
  42.    variable f1, d1;
  43.    dir = extract_element(dir, 0, ';');
  44.    f1 = extract_element(dir, 1, ']');
  45.    if (strlen(f1)) f1 = strcat(".", extract_element(f1, 0, '.'));
  46.    d1 = extract_element(dir, 0, ']');
  47.    strcat(d1, f1);
  48.    if (':' != int(substr(dir, strlen(dir), 1))) strcat((), "]");
  49.    strcat ((), file);
  50. #endif
  51.    expand_filename(());
  52. }
  53.  
  54. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  55. %     Global Variables
  56. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  57. variable Null_String = "";
  58.  
  59. %!% A Comma separated list of info directories to search.
  60. variable Info_Directory;
  61. variable Jed_Bin_Dir;
  62.  
  63.  
  64. %!% Prototype: Void strncat (String a, String b, ..., Integer n);
  65. %!% Returns concatenated string "abc..."
  66. define strncat (n)
  67. {
  68.    n--;
  69.    loop (n) strcat ();
  70. }
  71.  
  72.  
  73. %% Convert Unix- or OS/2- style path to comma-delimited list
  74. define path2list(path)
  75. {
  76. #ifdef VMS
  77.     path;
  78. #else
  79.     variable n, pathsep, a_path;
  80.  
  81. #ifdef UNIX
  82.     pathsep = ':';
  83. #else
  84.     pathsep = ';';
  85. #endif
  86.  
  87.     n = 0; Null_String; Null_String;
  88.     while (a_path = extract_element(path, n, pathsep), strlen(a_path))
  89.       {
  90.      strncat ((), (), a_path, 3);
  91.      % strcat((), strcat((), a_path));
  92.      ","; n++;
  93.       }
  94.     pop();
  95. #endif
  96. }
  97.  
  98. #ifdef VMS
  99.    Info_Directory = strcat(JED_ROOT, "[info]");
  100.    Jed_Bin_Dir = strcat(JED_ROOT, "[bin]");
  101. #else
  102.    Info_Directory = dircat(JED_ROOT, "info");
  103.    Jed_Bin_Dir = dircat(JED_ROOT, "bin");
  104. #endif
  105.  
  106. $1 = getenv("INFOPATH");
  107. if (strlen($1)) Info_Directory = path2list($1);
  108.   
  109. %!% Column to begin a C comment--- used by c_make_comment
  110. variable C_Comment_Column = 40;
  111.  
  112.  
  113. #ifdef UNIX
  114.  if (OUTPUT_RATE > 9600) OUTPUT_RATE = 0;   %% coming through a network?
  115. #endif
  116.  
  117. %%
  118. %% Some key definitions
  119. %%
  120. % These two are for compatability:
  121.   setkey("search_forward", "^Ff");
  122.   setkey("search_backward", "^Fb");
  123. #ifdef OS2
  124.    setkey("bob",  "^@\x84");
  125.    setkey("eob", "^@\x76");
  126.    setkey("skip_word", "^@\x74");  % C-left arrow
  127.    setkey("bskip_word", "^@\x73"); % C-right arrow
  128. #endif
  129.   setkey("skip_word", "^[^[[C");  %escape right arrow.
  130.   setkey("bskip_word", "^[^[[D");  %escape left arrow
  131.   setkey("upcase_word", "^[U");
  132.   setkey("downcase_word", "^[L");
  133.   setkey("capitalize_word", "^[C");
  134.   setkey("emacs_escape_x", "^[X");
  135.   setkey("undo", "^Xu");  %% Also ^_ but vtxxx have problems with it
  136.   setkey("transpose_lines", "^X^T");
  137.   setkey("help_prefix", "^H");
  138.   setkey("c_make_comment", "^[;");
  139.   setkey("indent_line_cmd", "^I");
  140.   setkey("insert_colon_cmd", ":");
  141.   setkey("newline_and_indent_cmd", "^M");
  142.   setkey("do_shell_cmd", "^[!");
  143.   setkey("find_tag", "^[.");
  144.   setkey("dabbrev", "\e/");
  145.   setkey("save_buffers", "^Xs");
  146.   setkey("whatpos", "^X?");
  147.   setkey("list_buffers", "^X^B");
  148.   setkey ("set_fill_column", "^Xf");
  149. #ifdef UNIX OS2
  150.   setkey("ispell", "^[$");
  151. #endif
  152. #ifndef MSDOS OS2
  153.   setkey("mail", "^Xm");
  154. #endif
  155.  
  156. %%
  157. %%  Autoloads
  158. %%
  159.  
  160.   autoload("find_binary_file",        "binary");
  161.   autoload("jed_easy_help",        "jedhelp");
  162.   autoload("query_replace_match",    "regexp");
  163.   autoload("re_search_forward",        "regexp");
  164.   autoload("re_search_backward",    "regexp");
  165.   autoload("c_make_comment",        "cmisc");
  166.   autoload("c_indent_line",        "indent");
  167.   autoload("c_colon_indent_line",    "indent");
  168.   autoload("dired",            "dired");
  169.   autoload("calendar",            "cal");
  170.   autoload("menu_main_cmds",        "menu");
  171.   autoload("trim_buffer",        "util");  %% trims excess lines and space
  172.   autoload("occur",            "util");  %% find all command
  173.   autoload("info_reader",        "info");
  174.   autoload("info_mode",            "info");
  175.   autoload("info_find_node",        "info");
  176.   autoload("list_buffers",        "buf");
  177.   autoload("append_region",        "buf");
  178.   autoload("write_region",        "buf");
  179.   autoload("recover_file",        "buf");
  180.   autoload("most_mode",            "most");
  181.   autoload("run_most",            "most");
  182.   autoload("compile",            "compile");
  183.   autoload("sort",            "sort");
  184.   autoload("untab",            "untab");
  185.   autoload("fortran_mode",        "fortran");
  186.   autoload("save_buffers",        "buf");
  187.   autoload("rot13",            "rot13");
  188.   autoload("search_forward",        "search");
  189.   autoload("search_backward",        "search");
  190.   autoload("replace_cmd",        "search");
  191.   autoload("replace_maybe_query",    "search");
  192.   autoload("replace_across_buffer_files","replace");
  193.   autoload("isearch_forward",        "isearch");
  194.   autoload("isearch_backward",        "isearch");
  195.   autoload("shell",            "shell");
  196.   autoload("mute_set_mute_keys",    "mutekeys");
  197. #ifndef UNIX OS2
  198.   autoload("shell_cmd",            "shell");
  199. #endif
  200.   autoload("do_shell_cmd",        "shell");
  201.   autoload("find_tag",            "ctags");
  202.   autoload("apropos",            "help");
  203.   autoload("expand_keystring",        "help");
  204.   autoload("describe_bindings",        "help");
  205.   autoload("describe_function",        "help");
  206.   autoload("describe_variable",        "help");
  207.   autoload("help_for_function",        "help");
  208.   autoload("where_is",            "help");
  209.   autoload("showkey",            "help");
  210.   autoload("describe_mode",        "help");
  211.   autoload("format_paragraph_hook",    "tmisc");
  212.   autoload("dabbrev",            "dabbrev");
  213.   autoload("tex_mode",            "tex");
  214.   autoload("bkmrk_goto_mark",           "bookmark");
  215.   autoload("bkmrk_set_mark",            "bookmark");
  216.  
  217. %%
  218. %% By default, tabs are every TAB columns (default 8).  Calling this function
  219. %% will allow the user to set the tabs arbitrarily and bind the TAB key
  220. %% appropriately.
  221.   autoload("edit_tab_stops",        "tabs");
  222.   autoload("tab_to_tab_stop",        "tabs");
  223.   autoload("append_string_to_file",    "misc");   
  224.   autoload("write_string_to_file",    "misc");   
  225.   autoload("make_tmp_buffer_name",    "misc");
  226.   autoload("abbrev_mode",        "abbrev");
  227.   autoload("set_abbrev_mode",        "abbrev");
  228.   autoload("save_abbrevs",        "abbrmisc");
  229.   autoload("define_abbreviation",    "abbrmisc");
  230.  
  231. #ifdef UNIX VMS
  232.   autoload("mail",            "mail");
  233.   autoload("mail_format_buffer",    "mail");
  234.   autoload("dcl_mode",            "dcl");
  235. #endif
  236.  
  237. #ifdef UNIX OS2
  238.   autoload("unix_man",            "man");
  239.   autoload("ispell",            "ispell");
  240. #endif
  241. #ifdef UNIX
  242.   autoload("rmail",            "rmail");
  243. #endif
  244.  
  245. #ifdef VMS
  246.   autoload("vms_help",            "vmshelp");
  247. #endif
  248.  
  249. % Utility functions
  250.  
  251. %!% Prototype: Void go_up (Integer n);
  252. %!% Move up 'n' lines.
  253. %!% See also: up, go_down
  254. define go_up() { () = up(); }
  255.  
  256. %!% Prototype: Void go_down (Integer n);
  257. %!% Move down 'n' lines.
  258. %!% See also: go_up, down
  259. define go_down() { () = down(); }
  260.  
  261. %!% Prototype: Void go_left (Integer n);
  262. %!% Move backward 'n' characters.
  263. %!% See also: left, go_right
  264. define go_left() { () = left();}
  265.  
  266. %!% Prototype: Void go_right (Integer n);
  267. %!% Move forward 'n' characters.
  268. %!% See also: right, go_left
  269. define go_right() { () = right();}
  270.  
  271. %% emacs-like escape-x function
  272. define emacs_escape_x()
  273. {
  274.    variable f = Null_String, i = 0;
  275.   
  276.    if (MINIBUFFER_ACTIVE)
  277.      {
  278.     call("evaluate_cmd");
  279.     return;
  280.      }
  281.    
  282.    forever
  283.      {
  284.     if (is_internal(f)) 
  285.       {
  286.          call(f);
  287.          return;
  288.       }
  289.     
  290.     if (is_defined(f))
  291.       {
  292.          eval(f);
  293.          return;
  294.       }
  295.     
  296.     if (i == 1) ungetkey(13);
  297.     ungetkey(' ');
  298.     ++i;
  299.     f = read_with_completion("M-x", Null_String, f, 'F')
  300.      } 
  301. }
  302.  
  303. define goto_line_cmd()
  304. {
  305.    read_mini("Goto line:", Null_String, Null_String);
  306.    goto_line(integer(()));
  307. }
  308.  
  309. define goto_column_cmd()
  310. {
  311.    read_mini("Goto Column:", Null_String, Null_String);
  312.    goto_column(integer(()));
  313. }
  314.  
  315. %!% Prototype: Void runhooks (String fun)
  316. %!% if S-Lang function 'fun' is defined, execute it.  It does nothing if 'fun'
  317. %!% does not exist.
  318. define runhooks(fun)
  319. {
  320.    if (is_defined (fun)) eval(fun);
  321. }
  322.  
  323. %!% Prototype: Void local_setkey (String fun, String key);
  324. %!% This function is like 'setkey' but unlike 'setkey' which operates on the
  325. %!% global keymap, 'local_setkey' operates on the current keymap which may or
  326. %!% may not be the global one.
  327. %!% See also: setkey, definekey, local_unsetkey
  328. define local_setkey(f, key)
  329. {
  330.    definekey(f, key, what_keymap());
  331. }
  332.  
  333. %!% Prototype: Void local_unsetkey (String key);
  334. %!% This function is like 'unsetkey' but unlike 'unsetkey' which unsets a key
  335. %!% from the global keymap, 'local_unsetkey' operates on the current keymap
  336. %!% which may or may not be the global one.
  337. %!% See also: unsetkey, undefinekey, local_setkey
  338. define local_unsetkey(key)
  339. {
  340.    undefinekey(key, what_keymap());
  341. }
  342.  
  343. %!% insert a character into a buffer.
  344. %!% This function should be called instead of 'insert' when it is desired to
  345. %!% insert a 1 character string.  Unlike 'insert', insert_char takes an integer
  346. %!% argument.  For example, 
  347. %!%    'x' insert_char
  348. %!% and 
  349. %!%    "x" insert
  350. %!% are functionally equivalent but insert_char is more memory efficient.
  351. define insert_char(ch) 
  352.    insert (char(ch));
  353. }
  354.  
  355.  
  356. %!% Prototype: Void newline (Void);
  357. %!% insert a newline in the buffer at point.
  358. %!% See also: insert, insert_char
  359. define newline () 
  360.    insert_char('\n');
  361. }
  362.  
  363.    
  364. %!% insert a single space into the buffer.
  365. define insert_single_space ()
  366. {
  367.    insert_char(' ');
  368. }
  369.  
  370. %!% Prototype: Integer looking_at_char (Integer ch);
  371. %!% This function returns non-zero if the character at the current editing
  372. %!% point is 'ch' otherwise it retuns zero.  This function performs a case 
  373. %!% sensitive comparison.
  374. define looking_at_char(ch)
  375. {
  376.    what_char() == ch;
  377. }
  378.  
  379.  
  380. %!% returns type of file.  e.g., /usr/a.b/file.c --> c
  381. define file_type(file)
  382. {
  383.    variable n;
  384.    file = extract_filename(file);
  385.    
  386.    n = is_substr(file, ".");
  387.    !if (n) return (Null_String);
  388.    
  389.    substr(file, n + 1, strlen(file));
  390. }
  391.  
  392.  
  393.  
  394. %;; scroll other window macros-- bind them yourself
  395. define next_wind_up()
  396. {
  397.    otherwindow();  call("page_up");
  398.    loop (nwindows() - 1) otherwindow();
  399. }
  400.  
  401. define next_wind_dn()
  402. {
  403.    otherwindow();  call("page_down");
  404.    loop (nwindows() - 1) otherwindow();
  405. }
  406.  
  407. %!% Mode dedicated to facilitate the editing of C language files.  Functions
  408. %!% that affect this mode include:
  409. %!%
  410. %!%   function:             default binding:
  411. %!%   brace_bra_cmd               {
  412. %!%   brace_ket_cmd               }
  413. %!%   newline_and_indent          RETURN
  414. %!%   indent_line_cmd             TAB
  415. %!%   goto_match                  ^\
  416. %!%   c_make_comment              ESC ;
  417. %!%
  418. %!%  Variables affecting indentation include:
  419. %!%
  420. %!%   C_INDENT
  421. %!%   C_BRACE
  422. %!%   C_Comment_Column  --- used by c_make_comment
  423. define c_mode()
  424. {
  425.    setmode("C", 2);
  426.    use_keymap("global");
  427.    runhooks("c_mode_hook");
  428. }
  429.  
  430. define slang_mode()
  431. {
  432.    setmode("SL", 2 | 8);
  433.    use_keymap("global");
  434.    runhooks("slang_mode_hook");
  435. }
  436.  
  437. %!%  Mode for indenting and wrapping text
  438. %!%  Functions that affect this mode include:
  439. %!%
  440. %!%    Function:                     Default Binding:
  441. %!%      indent_line_cmd                 TAB
  442. %!%      newline_and_indent_cmd          RETURN
  443. %!%      format_paragraph                ESC Q
  444. %!%      narrow_paragraph                ESC N
  445. %!%
  446. %!%  Variables include:
  447. %!%      WRAP_INDENTS, WRAP
  448. %!%      TAB, TAB_DEFAULT
  449. define text_mode()
  450. {
  451.    setmode("Text", 1);
  452.    use_keymap("global");
  453.    runhooks ("text_mode_hook");
  454. }
  455.  
  456. %!%  Generic mode not designed for anything in particular.
  457. %!%  See:  text_mode, c_mode
  458. define no_mode()
  459. {
  460.    setmode(Null_String, 0);
  461.    use_keymap("global");
  462. }
  463.  
  464. % Function prototypes
  465. % These 'functions' are only here to initialize function pointers.
  466. define _function_pop_0 (x) {0}
  467.  
  468. %!% called from mode_hook.  Returns 0 if it is desired that control return
  469. %!% to mode_hook or 1 if mode hook should exit after calling mode_hook_ptr
  470. variable mode_hook_pointer = &_function_pop_0;
  471.  
  472. variable Default_Mode = &text_mode;
  473.  
  474.  
  475.  
  476. % Emacs allows a mode definition on the first line of a file
  477. % -*- mode: MODENAME; VAR: VALUE; ... -*-
  478. % which can also include values of local variables 
  479.  
  480. %!% check first line for the simplest Emacs mode statement
  481. %!% -*- modename -*-
  482. define modeline_hook()
  483. {
  484.    variable mode = Null_String;
  485.    push_spot(); bob();
  486.    
  487.    if (looking_at("#!/")) mode = "sh";    % #!/bin/sh, #!/usr/local/bin/perl, ...
  488.        
  489.    push_mark (); narrow ();
  490.    if (re_fsearch ("-\\*- *\\([A-Za-z]+\\) *-\\*-"))
  491.      mode = strlow (regexp_nth_match (1));
  492.    
  493.    widen (); pop_spot ();        % restore place
  494.    
  495.    if ( strlen(mode) )
  496.      {
  497.     mode = strcat (mode, "_mode" );   
  498.     if ( is_defined(mode) )
  499.       {
  500.          eval (mode);
  501.          return 1;
  502.       }
  503.      }
  504.    0;
  505. }
  506.  
  507. variable Mode_List_Exts = "c,h,cc,cpp,sl,txt,doc,tex,f,for";
  508. variable Mode_List_Modes = "c,c,c,c,slang,text,text,tex,fortran,fortran";
  509.  
  510. #ifdef MSDOS OS2
  511. Mode_List_Exts = strcat (Mode_List_Exts, ",rc");     %  resource file
  512. Mode_List_Modes = strcat (Mode_List_Modes, ",c");
  513. #endif
  514.  
  515. #ifdef VMS UNIX
  516. Mode_List_Exts = strcat (Mode_List_Exts, ",com");     %  resource file
  517. Mode_List_Modes = strcat (Mode_List_Modes, ",dcl");
  518. #endif
  519.  
  520. #ifdef UNIX
  521. Mode_List_Exts = strcat (Mode_List_Exts, ",cshrc,tcshrc,login,profile");     %  resource file
  522. Mode_List_Modes = strcat (Mode_List_Modes, ",no,no,no,no");
  523. #endif
  524.  
  525.  
  526. %!% Prototype: Void add_mode_for_extension (String mode, String ext);
  527. %!% This function modifies Mode_List in such a way that when a file with 
  528. %!% filename extension `ext' is read in, function strcat (mode, "_mode")
  529. %!% will be called to set the mode.   That is, the first parameter 'mode' 
  530. %!% is the name of a mode without the '_mode' added to the end of it.
  531. define add_mode_for_extension (mode, ext)
  532. {
  533.    variable comma = ",";
  534.    
  535.    Mode_List_Modes = strncat (mode, comma, Mode_List_Modes, 3);
  536.    Mode_List_Exts = strncat (ext, comma, Mode_List_Exts, 3);
  537. }
  538.  
  539. %!% This is a hook called by find_file routines to set the mode
  540. %!% for the buffer. This function takes one parameter, the filename extension
  541. %!% and returns nothing.
  542. define mode_hook (ext)
  543. {
  544.    variable n, mode;
  545. #ifdef VMS
  546.    ext = extract_element(ext, 0, ';');
  547. #endif
  548.    
  549. #ifdef MSDOS OS2 VMS
  550.    ext = strlow (ext);
  551. #endif
  552.    
  553.    if (mode_hook_pointer(ext)) return;
  554.  
  555.    if (modeline_hook ()) return;
  556.    
  557.    n = is_list_element (Mode_List_Exts, ext, ',');
  558.    if (n)
  559.      {
  560.     n--;
  561.     mode = strcat (extract_element (Mode_List_Modes, n, ','), "_mode");
  562.     if (is_defined(mode)) 
  563.       {
  564.          eval (mode);
  565.          return;
  566.       }
  567.      }
  568.    Default_Mode ();
  569. }
  570.  
  571. define is_c_mode ()
  572. {
  573.    variable mode;
  574.    (, mode) = whatmode ();
  575.    mode & 2;
  576. }
  577.  
  578. define indent_line_cmd ()
  579. {
  580.    indent_line ();
  581.    if (is_c_mode) c_indent_line ();
  582. }
  583.  
  584. define insert_colon_cmd ()
  585. {
  586.    insert_char (':');
  587.    if (is_c_mode ())
  588.    {
  589.       () = c_colon_indent_line ();
  590.    }
  591. }
  592.  
  593.  
  594. define newline_and_indent_cmd ()
  595. {
  596.    % Note that the 'call' is necessary here so that exit_min is called
  597.    % if this is called from mini buffer at startup.
  598.    call ("newline");
  599.    indent_line_cmd ();
  600.    if (is_c_mode() and C_COMMENT_HINT) 
  601.      {
  602.     if (bolp ()) insert_single_space ();
  603.     insert ("* ");
  604.      }
  605. }
  606.  
  607.  
  608. %!% sets buf modified flag. If argument is 1, mark
  609. %!% buffer as modified.  If argument is 0, mark buffer as unchanged.
  610. define set_buffer_modified_flag(modif)
  611. {
  612.    getbuf_info();
  613.    if (modif) () | 1; else () & ~(1);
  614.    setbuf_info(());
  615. }
  616. %!%  returns non-zero if the buffer modified flag is set.  It returns zero
  617. %!%  if the buffer modified flag is not been set.  This works on the 
  618. %!%  current buffer.  See also 'set_buffer_modified_flag'.
  619. define buffer_modified ()
  620. {
  621.    variable flags;
  622.    (, , , flags) = getbuf_info ();
  623.    flags & 1;
  624. }
  625.  
  626. %!% set undo mode for buffer.  If argument is 1, undo is on.  0 turns it off
  627. define set_buffer_undo(modif)
  628. {
  629.    getbuf_info();
  630.    if (modif) () | 32; else () & ~(32);
  631.    setbuf_info(());
  632. }
  633.  
  634.  
  635. %!% Takes 1 parameter: 0 turn off readonly
  636. %!%                    1 turn on readonly
  637. define set_readonly(n)
  638. {
  639.    getbuf_info();
  640.    if (n) () | 8; else () & ~(8);
  641.    setbuf_info(());
  642. }
  643.  
  644. %!% Takes 1 parameter: 0 turn off overwrite
  645. %!%                    1 turn on overwrite
  646. define set_overwrite(n)
  647. {
  648.    getbuf_info();
  649.    if (n) () | 16; else () & ~(16);
  650.    setbuf_info(());
  651. }
  652.    
  653.  
  654. define toggle_crmode ()
  655. {
  656.    setbuf_info(getbuf_info() xor 0x400);
  657.    set_buffer_modified_flag (1);
  658. }
  659.  
  660. define toggle_readonly()
  661. {
  662.    setbuf_info(getbuf_info() xor 8);
  663. }
  664. define toggle_overwrite()
  665. {
  666.    setbuf_info(getbuf_info() xor 16);
  667. }
  668.  
  669. #ifdef MSDOS OS2
  670. setkey("toggle_overwrite", "^@R");     %/* insert key */
  671. #endif
  672.  
  673. define toggle_undo()
  674. {
  675.    setbuf_info(getbuf_info() xor 32);
  676. }
  677.  
  678. define double_line()
  679. {
  680.    POINT;
  681.    bol(); push_mark(); eol(); 
  682.    bufsubstr(); 
  683.    newline();
  684.    insert(());
  685.    =POINT;
  686. }
  687.  
  688. define transpose_lines()
  689. {
  690.    variable line;
  691.    bol(); push_mark(); push_mark(); eol(); 
  692.    line = bufsubstr();
  693.    go_right(1);
  694.    del_region();
  695.    go_up(1); bol();
  696.    insert(line);
  697.    newline();
  698.    go_down (1);                   %  goes to bol
  699. }
  700.  
  701.  
  702.  
  703.  
  704. %!% string to display at bottom of screen upon JED startup and when
  705. %!% user executes the help function.
  706. variable help_for_help_string;
  707.  
  708. help_for_help_string =
  709. #ifdef VMS
  710.  "^H -> Help:^H  Menu:?  Info:I  Apropos:A  Key:K  Where:W  Fnct:F  VMSHELP:M  Var:V";
  711. #endif
  712. #ifdef MSDOS
  713.  "^H -> Help:^H  Menu:?  Info:I  Apropos:A  Key:K  Where:W  Fnct:F  Var:V  Mem:M";
  714. #endif
  715. #ifdef UNIX OS2
  716.  "^H -> Help:^H  Menu:?  Info:I  Apropos:A  Key:K  Where:W  Fnct:F  Var:V  Man:M";
  717. #endif
  718.  
  719. % Load minibuffer routines now before any files are loaded.
  720. % This will reduce fragmentation on PC.
  721. !if (BATCH) () = evalfile("mini"); 
  722.  
  723.  
  724. %for compatability with older versions
  725. define read_file_from_mini(prompt)
  726. {
  727.    read_with_completion(prompt, Null_String, Null_String, 'f');
  728. }
  729.  
  730. %!% Search for FILE in directories specified by JED_LIBRARY returning
  731. %!% expanded pathname if found or the Null string otherwise.
  732. define expand_jedlib_file(f)
  733. {
  734.    variable n = 0, dir, file;
  735.    forever
  736.      {
  737.     dir = extract_element(JED_LIBRARY, n, ',');
  738.     !if (strlen(dir)) return (Null_String);
  739.     file = dircat(dir, f);
  740.     if (file_status(file) == 1) break;
  741.     ++n;
  742.    } 
  743.    file;
  744. }
  745.  
  746. %!% Prototype: String read_string_with_completion (prompt, dflt, list)
  747. %!% This function takes 3 String parameters and returns a String.  The
  748. %!% first parameter is used as the prompt, the second parameter is the 
  749. %!% default value to be returned and the third parameter is a list to be used
  750. %!% for completions.  This list is simply a comma separated list of strings.
  751. define read_string_with_completion (prompt, dflt, list)
  752. {
  753.    read_with_completion (list, prompt, dflt, Null_String, 's');
  754. }
  755.  
  756. %!% If non-zero, startup by asking user for a filename if one was
  757. %!% not specified on the command line.
  758. variable Startup_With_File = 0;
  759.  
  760. %% startup hook
  761. %!% Function that gets executed right before JED enters its main editing
  762. %!% loop.  This is for last minute modifications of data structures that
  763. %!% did not exist when startup files were loaded (e.g., minibuffer keymap)
  764. define jed_startup_hook()
  765. {
  766.    variable n, hlp, ok = 0, mini = "Mini_Map", file, do_message = 1;
  767.  
  768. #ifdef MSDOS OS2
  769.    definekey ("next_mini_command", "^@P", mini);
  770.    definekey ("prev_mini_command", "^@H", mini);
  771. #else
  772.    definekey ("next_mini_command", "^[[B", mini);
  773.    definekey ("prev_mini_command", "^[[A", mini);
  774. #endif
  775.  
  776.    definekey ("mini_exit_minibuffer", "^M", mini);
  777.    definekey ("exit_mini", "^[^M", mini);
  778.    
  779.    % turn on Abort character processing
  780.    IGNORE_USER_ABORT = 0;
  781.  
  782.    runhooks ("startup_hook");
  783.    
  784.    !if (strcmp(whatbuf(), "*scratch*") or buffer_modified())
  785.      {
  786.     ERROR_BLOCK 
  787.       {
  788.          erase_buffer ();
  789.          set_buffer_modified_flag (0);
  790.       }
  791.     
  792.     () = insert_file (expand_jedlib_file("cpright.hlp")); 
  793.     set_buffer_modified_flag (0);
  794.     bob();
  795.     file = Null_String;
  796.     if (Startup_With_File > 0)
  797.       {
  798.          forever 
  799.            {
  800.           file = read_file_from_mini ("Enter Filename:");
  801.           if (strlen(extract_filename(file))) break;
  802.            }
  803.       }
  804.     else !if (Startup_With_File)
  805.       {
  806.          message(help_for_help_string); do_message = 0;
  807.          update(1);
  808.          () = input_pending(600);
  809.       }
  810.     EXECUTE_ERROR_BLOCK;
  811.     if (strlen (file)) () = find_file(file);
  812.      }
  813.    
  814.    if (do_message) message(help_for_help_string);
  815. }
  816.  
  817.  
  818.  
  819. %!% display row and column information in minibuffer
  820. define whatpos ()
  821. {
  822.    variable max_lines;
  823.    
  824.    push_spot (); eob (); max_lines = whatline (); pop_spot ();
  825.    
  826.    message (Sprintf ("%s, Line %d of %d lines, Column %d",  
  827.              count_chars (), whatline(), max_lines, what_column (),
  828.              4));
  829. }
  830.  
  831.  
  832.  
  833. %!% find a file from JED_LIBRARY, returns number of lines read or 0 if not 
  834. %!% found.
  835. define find_jedlib_file(file)
  836. {
  837.    file = expand_jedlib_file(file);
  838.    !if (strlen(file)) return(0);
  839.    find_file(file);
  840. }
  841.  
  842.  
  843. %%
  844. %% help function
  845. %%
  846.  
  847. %!% name of the file to load when the help function is called.
  848. variable Help_File = "jed.hlp";   %% other modes will override this.
  849.  
  850. %!% Pop up a window containing a help file.  The help file that is read
  851. %!% in is given by the variable Help_File.
  852. define help()
  853. {
  854.    variable hlp = "*help*", buf, rows;
  855.     
  856.    !if (buffer_visible (hlp))
  857.      {
  858.     !if (strlen(Help_File)) Help_File = "generic.hlp";
  859.     Help_File = expand_jedlib_file(Help_File);
  860.     buf = whatbuf();
  861.     onewindow();
  862.     rows = window_info('r');
  863.     setbuf(hlp);
  864.     set_readonly(0);
  865.     erase_buffer();
  866.  
  867.     () = insert_file(Help_File);
  868.     pop2buf(hlp);
  869.     eob(); bskip_chars("\n");
  870.     rows = rows / 2 - (whatline() + 1);
  871.     bob();
  872.     set_buffer_modified_flag(0);
  873.     set_readonly(1);
  874.     pop2buf(buf);
  875.     loop (rows) enlargewin();
  876.      }
  877.    
  878.    update (1);
  879.    message(help_for_help_string);
  880. }
  881.  
  882. #ifdef VMS
  883. %% This resume hook is need for VMS when returning from spawn.
  884. %% In fact, it is NEEDED for certain JED functions on VMS so declare it.
  885. define resume_hook()
  886. {
  887.    variable file = getenv("JED_FILE_NAME");
  888.    !if (strlen(file)) return;
  889.    
  890.    !if (find_file(file)) error("File not found!");
  891. }
  892. #endif VMS
  893.  
  894.  
  895. %%file hooks
  896. %!% returns backup filename.  Arguments to function are dir and file.
  897. define make_backup_filename(dir, file)
  898. {
  899. #ifdef UNIX
  900.    strncat (dir, file, "~", 3);
  901. #endif
  902. #ifdef MSDOS OS2
  903.    variable type;
  904. #ifdef OS2
  905.    !if (IsHPFSFileSystem(dir)) {
  906. #endif
  907.  
  908.       % There are several things to worry about.  Here just break up the 
  909.       % filename and truncate type to 2 chars and paste it back.
  910.       % note that this takes a name like file.c and produces file.c~
  911.       % Also, note that if the type is empty as in 'file', it produces 
  912.       % 'file.~'
  913.  
  914.       type = file_type(file);
  915.       file = strncat (extract_element(file, 0, '.'), ".", substr(type, 1, 2), 3);
  916.       
  917.  
  918. #ifdef OS2
  919.    }
  920. #endif
  921.    strncat (dir, file, "~", 3);
  922. #endif  
  923. }
  924.  
  925.  
  926. %!% returns autosave filename.  Arguments to function are dir and file.
  927. define make_autosave_filename(dir, file)
  928. {
  929. #ifdef VMS
  930.    Sprintf ("%s_$%s;1", dir, file, 2);
  931. #endif
  932.    
  933. #ifdef UNIX
  934.    Sprintf ("%s#%s#", dir, file, 2);
  935. #endif
  936.    
  937. #ifdef MSDOS OS2
  938. #ifdef OS2
  939.    !if (IsHPFSFileSystem(dir)) 
  940.      {
  941. #endif
  942.     
  943.     variable type = file_type(file);
  944.     file = substr(extract_element(file, 0, '.'), 1, 7);
  945.     if (strlen(type)) file = strcat(file, ".");
  946.     file = strcat (file, type);
  947.  
  948. #ifdef OS2
  949.      }
  950. #endif
  951.    
  952.    strncat (dir, "#", file, 3);
  953. #endif
  954. }
  955.  
  956. %!% breaks a filespec into dir filename--- 
  957. %!% this routine returns dir and filename such that a simple strcat will
  958. %!% suffice to put them together again.  For example, on unix, /a/b/c
  959. %!% returns /a/b/ and c
  960. define parse_filename(fn)
  961. {
  962.    variable f, dir, n;
  963.       
  964.    f = extract_filename(fn);
  965.    n = strlen(fn) - strlen(f);
  966.    dir = substr(fn, 1, n);
  967.    dir; f;
  968. }
  969.  
  970.  
  971. define set_buffer_no_backup ()
  972. {
  973.    setbuf_info (getbuf_info() | 0x100);
  974. }
  975. define set_buffer_no_autosave ()
  976. {
  977.    setbuf_info (getbuf_info() & ~(0x2));
  978. }
  979.  
  980. variable No_Backups = 0;
  981.  
  982. %!% called AFTER a file is read in to a buffer.  FILENAME is on the stack.
  983. define find_file_hook(file)
  984. {
  985.    variable dir, a, f, m;
  986.    (dir, f) = parse_filename(file); 
  987.  
  988. #ifndef VMS
  989.    if (file_status(dir) != 2)
  990.      {
  991.     error (Sprintf("Directory %s is invalid!", dir, 1));
  992.      }
  993. #endif
  994.    
  995.    if (No_Backups) set_buffer_no_backup ();
  996.    a = make_autosave_filename(dir, f);
  997.    if (file_time_compare(a, file) > 0) 
  998.      {
  999.     m = "Autosave file is newer. Use ESC-X recover_file.";
  1000.     flush(m);
  1001.         () = input_pending(10); 
  1002.     message(m);
  1003.      }
  1004.    runhooks ("user_find_file_hook");
  1005. }
  1006.  
  1007. %
  1008. % completions  -- everything here must be predefined
  1009. % I just push the strings onto the stack and loop 'add_completion' over them
  1010. %
  1011.   $0 = _stkdepth();
  1012.  
  1013. . "toggle_undo" "calendar" "trim_buffer" "abbrev_mode"
  1014. . "define_abbreviation" "save_abbrevs"
  1015. . "occur" "append_region" "write_region" "replace_across_buffer_files"
  1016. . "recover_file" "compile" "sort" "untab" "fortran_mode" "save_buffers"
  1017. . "isearch_forward" "isearch_backward" "shell"
  1018. . "edit_tab_stops" "c_mode" "toggle_crmode"
  1019. . "text_mode" "no_mode" "goto_line_cmd" "goto_column_cmd" "describe_mode"
  1020. . "evalbuffer" "open_rect" "kill_rect" "insert_rect" "copy_rect" "blank_rect"
  1021. . "dired" "re_search_forward" "re_search_backward" "query_replace_match"
  1022. . "describe_bindings"  "search_backward" "search_forward" "replace_cmd"
  1023. .  "find_binary_file"
  1024. #ifndef MSDOS OS2
  1025. .  "mail" 
  1026. #endif
  1027. #ifdef UNIX OS2
  1028. . "ispell"
  1029. #endif
  1030.  
  1031. loop (_stkdepth - $0) add_completion(());
  1032.  
  1033.  
  1034. %!% Prototype: String buffer_filename ();
  1035. %!% Returns the name of the file associated with the current buffer.  If 
  1036. %!% there is none associated with it, the empty string is returned.
  1037. define buffer_filename ()
  1038. {
  1039.    variable file, dir;
  1040.    (file, dir, , ) = getbuf_info();
  1041.    !if (strlen (file)) dir = Null_String;
  1042.    strcat (dir, file);
  1043. }
  1044.  
  1045.  
  1046. % save buffer if necessary
  1047. define save_buffer()
  1048. {
  1049.    variable flags, dir, file;
  1050.    (file, , , flags) = getbuf_info();
  1051.  
  1052.    !if (flags & 1) return (message("Buffer not modified."));
  1053.    if (strlen(file))
  1054.      {
  1055.     () = write_buffer(buffer_filename ()); 
  1056.      }
  1057.    else call ("save_buffers");
  1058. } add_completion("save_buffer");
  1059.  
  1060. define insert_buffer()
  1061. {
  1062.    read_with_completion("Insert Buffer:", Null_String, Null_String, 'b');
  1063.    push_spot();
  1064.    ERROR_BLOCK {pop_spot()}
  1065.    insbuf(());
  1066.    EXECUTE_ERROR_BLOCK;
  1067. }  add_completion("insert_buffer");
  1068.  
  1069. variable Global_Top_Status_Line = " *** To activate menus, press `Ctrl-H ?'.  For help, press `Ctrl-H' twice. ***";
  1070. () = set_top_status_line (Global_Top_Status_Line); 
  1071. define help_prefix()
  1072. {
  1073.    variable c;
  1074.    
  1075.    !if (input_pending(20)) flush (help_for_help_string);
  1076.    c = int (strup(char(getkey())));
  1077.    switch (c)
  1078.      { case  8 : help }
  1079.      { case  'A' : apropos }
  1080.      { case  'I' : info_mode}
  1081.      { case  '?' : menu_main_cmds}
  1082.      { case  'F' : describe_function}
  1083.      { case  'V' : describe_variable}
  1084.      { case  'W' : where_is}
  1085.      { case  'C' or case ('K', c) : showkey}
  1086.      { case  'M' :
  1087. #ifdef UNIX OS2
  1088.     unix_man();
  1089. #else 
  1090. #ifdef VMS
  1091.     vms_help ();
  1092. #endif
  1093. #endif
  1094. #ifdef MSDOS
  1095.     call("coreleft");
  1096. #endif
  1097.      }
  1098.      { pop(); beep(); }
  1099. }
  1100.  
  1101. %%
  1102. %%  word movement definitions.  Since these vary according to editors,
  1103. %%  they are S-Lang routines.
  1104. %%
  1105.  
  1106. define skip_word ()
  1107. {
  1108.   while (skip_non_word_chars(), eolp()) 
  1109.     {
  1110.       if (1 != right(1)) break;
  1111.     }
  1112.    skip_word_chars();
  1113. }
  1114.  
  1115. define bskip_word()
  1116. {
  1117.    while (bskip_non_word_chars(), bolp())
  1118.      {
  1119.     !if (left(1)) break;
  1120.      }
  1121.    bskip_word_chars();
  1122. }
  1123.  
  1124. define delete_word()
  1125. {
  1126.    push_mark(); skip_word(); del_region();
  1127. }
  1128.  
  1129. define bdelete_word ()
  1130. {  
  1131.    push_mark(); bskip_word(); del_region();
  1132. }
  1133.  
  1134. define xform_word(x)
  1135. {
  1136.    skip_non_word_chars();
  1137.    push_mark(); skip_word(); 
  1138.    xform_region(x);
  1139. }
  1140.  
  1141. define capitalize_word()
  1142. {
  1143.    xform_word('c');
  1144. }
  1145.  
  1146. define upcase_word()
  1147. {
  1148.    xform_word('u');
  1149. }
  1150.  
  1151. define downcase_word()
  1152. {
  1153.    xform_word('d');
  1154. }
  1155.  
  1156. define smart_set_mark_cmd ()
  1157. {
  1158.    if (markp()) pop_mark(0); else call("set_mark_cmd");
  1159. }
  1160.  
  1161. define flush_input()
  1162. {
  1163.      while (input_pending(0)) () = getkey();
  1164. }
  1165.  
  1166. %!% Prototype Void buffer_format_in_columns();
  1167. %!% takes a buffer consisting of a sigle column of items and converts the
  1168. %!% buffer to a multi-column format.
  1169. define buffer_format_in_columns()
  1170. {
  1171.    push_spot();
  1172.    bob();
  1173.    forever 
  1174.      {
  1175.     _for (0,4,1) 
  1176.       {
  1177.          goto_column(() * 14 + 1);
  1178.          if (eolp())
  1179.            {
  1180.           if (eobp()) 
  1181.             {
  1182.                pop_spot();
  1183.                return;
  1184.             }
  1185.           insert_single_space;
  1186.           del();
  1187.            }
  1188.       } 
  1189.     !if (down(1)) break;
  1190.     % bol (); % this is a side effect of going down
  1191.      }
  1192.    pop_spot();
  1193. }
  1194.  
  1195. define delete_line()
  1196. {
  1197.    bol(); push_mark(); eol(); go_right(1); del_region();
  1198. }
  1199.  
  1200.  
  1201. define set_fill_column ()
  1202. {
  1203.    push_spot(); 
  1204.    eol();
  1205.    WRAP = what_column ();
  1206.    pop_spot();
  1207.    message (Sprintf("WRAP column at %d.", WRAP, 1));
  1208. }
  1209.  
  1210. define rename_buffer(name)
  1211. {
  1212.    variable flags = getbuf_info(); pop(); setbuf_info(name, flags);
  1213. }
  1214.  
  1215. define info ()
  1216. {
  1217.    info_mode ();
  1218.    definekey("exit_jed",        "Q",  "Infomap");
  1219.    definekey("exit_jed",        "q",  "Infomap");
  1220. }
  1221.  
  1222. define make_tmp_file(base)
  1223. {
  1224.    variable pid = getpid(), file, n = 1000;
  1225.    while (n)
  1226.      {
  1227.     file = strcat(base, string(pid));
  1228.     !if (file_status(file)) return (file);
  1229.     pid++;
  1230.      }
  1231.    error ("Unable to create a tmp file!");
  1232. }
  1233.  
  1234.  
  1235. define goto_top_of_window ()
  1236. {
  1237.    go_up (window_line () - 1);
  1238. }
  1239.  
  1240. define goto_bottom_of_window ()
  1241. {
  1242.    go_down (window_info ('r') - window_line ());
  1243. }
  1244.  
  1245.  
  1246. %!% called from main in JED executable.
  1247. define command_line_hook()
  1248. {
  1249.    variable n, i, file, depth, jedrc, home, next_file;
  1250.    variable script_file = Null_String;
  1251.    jedrc = "jed.rc";
  1252.   
  1253. #ifdef UNIX
  1254.    jedrc = ".jedrc";
  1255. #endif
  1256.    
  1257.    home = getenv("JED_HOME");
  1258. #ifdef VMS
  1259. %% allows JED_HOME: to be search list---
  1260. %% thanks to SYSTEM@VACMZB.chemie.Uni-Mainz.DE for suggestion
  1261.    if (strlen(home)) home = "JED_HOME:";
  1262. #endif
  1263.    !if (strlen(home))
  1264.      {
  1265.     home = getenv("HOME");
  1266. #ifdef VMS
  1267.     home = "SYS$LOGIN:";
  1268. #endif
  1269.      } 
  1270.    jedrc = dircat(home, jedrc);
  1271.    
  1272.    n = MAIN_ARGC;
  1273.    
  1274.    --n;  %% argv[0], here it is not used.
  1275. %
  1276. %  If batch then first argument is not used so start at second
  1277. %  Also, n is the number of effective command line parameters so reduce it.
  1278. %
  1279.    
  1280.    if (BATCH) {    --n; 2; } else 1;  =i;
  1281. %
  1282. % if first argument is -n then do NOT load init file
  1283. %
  1284.    % stuff left on stack for if 
  1285.    if (n) strcmp (command_line_arg(i), "-n"); else 1;
  1286.    if (())
  1287.      {
  1288.     depth = _stkdepth;
  1289.     if (file_status(jedrc) == 1) jedrc; else "jed.rc";
  1290.     () = evalfile(()); 
  1291.     if ( _stkdepth != depth)
  1292.       {
  1293.          flush(strcat ("Excess junk left on stack by ", jedrc));
  1294.          () = input_pending(10);
  1295.       }
  1296.  
  1297.     loop (_stkdepth - depth) pop();
  1298.      }
  1299.    else if (n) ++i;
  1300.    
  1301.    n = MAIN_ARGC - i;   
  1302.    
  1303.    !if (n) return;
  1304.    
  1305. %
  1306. % Is JED to emulate most?
  1307. %
  1308.    !if (strcmp(command_line_arg(i), "-most"))
  1309.      {
  1310.     run_most (i + 1);
  1311.     return;
  1312.      }
  1313.       
  1314.    while (n > 0)
  1315.      {
  1316.     file = command_line_arg(i);
  1317.     --n; ++i;
  1318.     if (n) next_file = command_line_arg(i);
  1319.     switch(file)
  1320.       {case "-f" and n : eval(next_file)}
  1321.       {case "-g" and n : goto_line(integer(next_file))}
  1322.       {case "-s" and n : 
  1323.          () = fsearch(next_file); 
  1324.          save_search_string(next_file);
  1325.       }
  1326.       {case "-l" and n : () = evalfile(next_file); }
  1327.       {case "-i" and n : () = insert_file(next_file);}
  1328.       {case "-2" : splitwindow(); ++n; --i;}
  1329.       {case "-tmp":
  1330.          set_buffer_no_backup ();
  1331.          set_buffer_no_autosave ();
  1332.          ++n; --i;
  1333.       }
  1334.       {() = find_file(());  ++n; --i;}
  1335.     
  1336.     --n; ++i;
  1337.      }
  1338.    
  1339.    if (strlen(script_file)) () = evalfile(script_file);
  1340. }
  1341. %!% Prototype: Void deln (Integer n);
  1342. %!% delete the next 'n' characters.
  1343. define deln (n)
  1344. {
  1345.    push_mark (); go_right(n); del_region ();
  1346. }
  1347.  
  1348. define insert_spaces (n) 
  1349. {
  1350.    loop (n) insert_single_space ();
  1351. }
  1352.  
  1353. define blooking_at (str)
  1354. {
  1355.    variable n = strlen (str);
  1356.    
  1357.    EXIT_BLOCK 
  1358.      {
  1359.     pop_spot ();
  1360.      }
  1361.    
  1362.    push_spot ();
  1363.  
  1364.    if (n != left(n)) return 0;
  1365.    return looking_at (str);
  1366. }
  1367.  
  1368. define exchange_point_and_mark ()
  1369. {
  1370.    call ("exchange");
  1371. }
  1372.  
  1373.     
  1374.    
  1375. % This fixes some bug in OS2 dealing with 'dir' issued non-interactively.
  1376. #ifdef OS2
  1377.    if (strlen(getenv("DIRCMD"))) putenv("DIRCMD=/ogn");
  1378. #endif
  1379.  
  1380. () = evalfile ("os.sl"); 
  1381.  
  1382. %
  1383. %  This code fragment looks for the existence of "defaults.sl" and loads
  1384. %  it.  This file IS NOT distributed with JED.
  1385. %
  1386.  
  1387. if (strlen(expand_jedlib_file("defaults.sl"))) () = evalfile("defaults");
  1388.  
  1389.