;************************************************************************
;**                  for   B R I E F   ver. 2.1                         *
;**                                                                     *
;**          CLR - Color Processing Main Macro Routines                 *
;**                                                                     *
;**         By Tom Bentley     November 1988     Ver. 1.1               *
;**                                                                     *
;**                 (C) Copyright - All rights reserved                 *
;**                                                                     *
;************************************************************************

#include "dialog.h"

(macro _init
   (
      (int     auto_color_flag clr_buf_ids clr_buf_mnu curr_buff next_color)
      (int     window_color_flag nxt_buff clr_button_line clr_flag)
      (int     clr_last_mnu)
      (string  env_help clr_button_text)
      (global  auto_color_flag clr_buf_ids clr_buf_mnu next_color)
      (global  window_color_flag clr_button_text clr_button_line clr_flag)
      (global  clr_last_mnu)


      (= curr_buff (inq_buffer))
      (= auto_color_flag 0)
      (= window_color_flag 0)
      (= env_help (inq_environment "BHELP"))
      (= clr_buf_mnu 0)
      (if (! (exist (+ env_help "\\color.mnu")))
         (
            (pause_on_error 1)
            (error "COLOR - file color.mnu not found in BHELP")
            (error "Do not proceed with COLOR installed!!!!")
            (pause_on_error 0)
            (return 0)
         )
      )
      (= clr_buf_mnu (create_buffer "clr.mnu" (+ env_help "\\color.mnu") 1))
      (set_buffer clr_buf_mnu)
      (= clr_last_mnu 1)
      (= clr_flag 0)
      (while (== 0 clr_flag)
         (
            (goto_line clr_last_mnu)
            (if (! (inq_position clr_last_mnu))
               (++ clr_last_mnu)
            ;else
               (= clr_flag 1)
            )
         )
      )
      (= clr_buf_ids (create_buffer "clr_buf_ids" NULL 1))
      (if (! (exist (+ env_help "\\clrauto.rst")))
         (
            (while (! (execute_macro "clrauto"))
               (
                  (continue)
               )
            )
         )
      )
      (set_buffer clr_buf_ids)
      (read_file (+ env_help "\\clrauto.rst"))
      (goto_line 1)
      (= auto_color_flag (atoi (read 1)))
      (move_abs 1 2)
      (= window_color_flag (atoi (read 1)))
      (= next_color 1)
      (set_buffer curr_buff)
      (while (!= curr_buff nxt_buff)
         (
            (set_new_color)
            (= nxt_buff (next_buffer))
            (set_buffer nxt_buff)
         )
      )
      (chk_color)
      (register_macro 1 "chk_color")
      (register_macro 6 "set_new_color")
   )
)

(macro chk_color
   (
      (string   cmd buf_strg)
      (int      curr_buff cmd_start cmd_length)

      (if (== clr_buf_mnu 0)
         (
            (return 0)
         )
      )
      (= curr_buff (inq_buffer))
      (set_buffer clr_buf_ids)
      (goto_line 1)
      (sprintf buf_strg "<%d" curr_buff)
      (if (! (> (search_fwd buf_strg) 0))
         (
            (set_buffer clr_buf_mnu)
            (goto_line 1)
         )
      )
      (= cmd (trim (read)))
      (set_buffer curr_buff)
      (if (inq_borders)
         (
            (= cmd_start (+ (index cmd ";") 1))
            (= cmd_length (- (rindex cmd ";") cmd_start))
            (execute_macro (substr cmd cmd_start cmd_length))
         )
      ;else
         (
            (switch window_color_flag
              1
               (
                  (inq_window_color)
                  (window_color (atoi (substr cmd (+ (rindex cmd ";") 1))))
               )
              2
               (
                  (inq_window_color)
                  (window_color)
               )
            )
         )
      )
   )
)

(macro set_new_color
   (
      (string   cmd buf_strg)
      (int      curr_buff)

      (if (|| (! auto_color_flag) (== clr_buf_mnu 0))
         (
            (return 0)
         )
      )
      (= curr_buff (inq_buffer))
      (set_buffer clr_buf_mnu)
      (goto_line next_color)
      (if (|| (inq_position next_color) (== next_color 1))
         (
            (= next_color 2)
            (set_buffer curr_buff)
            (return 0)
         )
      )
      (= cmd (trim (read)))
      (set_buffer clr_buf_ids)
      (goto_line 1)
      (execute_macro "open_line")
      (sprintf buf_strg "%d" curr_buff)
      (insert buf_strg)
      (insert (substr cmd (index cmd ";")))
      (set_buffer curr_buff)
      (++ next_color)
   )
)


(macro color_menu
   (
      (int     curr_buff tmp_buf_id nxt_buff)
      (string  env_help)

      (if (== clr_buf_mnu 0)
         (
            (= env_help (inq_environment "BHELP"))
            (= clr_buf_mnu (create_buffer "clr.mnu"
               (+ env_help "\\color.mnu") 1))
            (register_macro 1 "chk_color")
            (register_macro 6 "set_new_color")
         )
      )
      (= clr_button_line 0)
      (= curr_buff (inq_buffer))
      (= tmp_buf_id (create_buffer "COLOR" NULL 0))
      (set_buffer tmp_buf_id)
      (top_of_buffer)
      (insert "       Select Buffer Color        ;1\n")
      (insert "     Automatic Colors On/Off      ;2\n")
      (insert "       Default ALL Buffers        ;3\n")
      (insert "        Color ALL Buffers         ;4\n")
      (insert "          Edit Colors             ;5")
      (set_buffer curr_buff)
      (_process_menu 21 13 56 7 "Select Function" ""
          NULL  tmp_buf_id "clr_menu_action" 1)
      (set_buffer curr_buff)
      (delete_buffer tmp_buf_id)
      (switch clr_button_line
         1
            (
               (execute_macro "clrsel")
            )
         2
            (
               (execute_macro "clrauto")
            )
         3
            (
               (del_clr_bufs)
            )
         4
            (
               (del_clr_bufs)
               (= next_color 1)
               (while (!= curr_buff nxt_buff)
                  (
                     (set_new_color)
                     (= nxt_buff (next_buffer))
                     (set_buffer nxt_buff)
                  )
               )
            )
         5
            (
               (unregister_macro 1 "chk_color")
               (unregister_macro 6 "set_new_color")
               (delete_buffer clr_buf_mnu)
               (= clr_buf_mnu 0)
               (error "COLOR Dissabled")
               (beep)
            )
      )
      (chk_color)
   )
)

(macro clr_menu_action
   (
      (int      key)
      (string   text)

      (get_parm 0 key)
      (if (== key DIALOG_PICK_MENU)
         (
            (get_parm 1 clr_button_line)
            (get_parm 2 text)
            (message "Processing: %s" (substr text 1 (- (index text ";") 2)))
            (push_back (key_to_int "<esc>"))
         )
      )
      (returns TRUE)
   )
)

(macro del_clr_bufs
   (
      (int   curr_buff)

      (= curr_buff (inq_buffer))
      (set_buffer clr_buf_ids)
      (top_of_buffer)
      (drop_anchor 1)
      (end_of_buffer)
      (delete_block)
      (set_buffer curr_buff)
   )
)

(replacement borders
   (
      (int  border_parm)

      (if (== (get_parm 0 border_parm) 0)
            (borders)
      ;else
            (borders border_parm)
      )
      (chk_color)
   )
)
