AutoHotkey: Makedir and file create history

From TotalcmdWiki
Revision as of 10:40, 10 February 2006 by Jungle (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
/*

This script allows  use of directory creation  history in TC
dir creation dialog. History depth is defined by the "depth"
parameter in the ini-file created in the script dir.

Up/Down keys change current dir name to appropriate from the
history.

Alt+Down shows a drop-down history list

(c) Jungle, February 2006.
Send your comments and suggestions to: jungle-80@mail.ru

*/
;
;----------
;
$Up::
  ifWinActive, ahk_class TSTDTREEDLG, ,ftp
  {
    inc = -1
    GoSub take_hist_dir
    Return
  }

  Send {Up}
Return
;
;----------
;
$Down::
  ifWinActive, ahk_class TSTDTREEDLG, ,ftp
  {
    inc = 1
    GoSub take_hist_dir
    Return
  }

  Send {Down}
Return
;
;----------
;
~!Down::
  ifWinActive, ahk_class TSTDTREEDLG, ,ftp
    GoSub tc_show_history_list
Return
;
;----------
;
$Esc::
  ; if history was loaded, but dir creation cancelled
  ifWinActive, ahk_class TSTDTREEDLG, ,ftp
    GoSub array_free

  Send {Esc}
Return
;
;----------
;
$Enter::
  ; if Enter key pressed in dir history list
  if WinActive("ahk_class AutoHotkeyGUI")
  {
    GoSub GuiSubmit
    Return
  }
  ; if Enter key pressed in dir creation dialog
  else
  ifWinActive, ahk_class TSTDTREEDLG, ,ftp
    GoSub write_dir_history

  Send {Enter}
Return
;
;----------
;
~LButton::
  ; if Button "OK" clicked instead of Enter
  ifWinActive, ahk_class TSTDTREEDLG, ,ftp
  {
    MouseGetPos, , , ,m_ctrl
    if ( m_ctrl = "TButton2" )
      GoSub write_dir_history
  }
Return
;
;----------
;
write_dir_history:
  if ( dir_win_id <> space )
    Return

  ; get dir create dialog id
  WinGet, dir_win_id, ID, A
  ; and wait until dialog is closed
  SetTimer, wait_dlg_close, 100

  ; Get new dir name...
  ControlGetText, cur_dir, TEdit1
  ; if empty then exit

  if ( cur_dir = space )
  {
    ; free memory
    GoSub array_free
    Return ; ...and exit if it is blank
  }

  ; if history is not loaded, load it
  if ( item_cnt = space )
    GoSub read_hist_file

  ; if history contains new dir name, then return
  Loop, %item_cnt%
    if ( cur_dir = dir_hist%A_Index% )
      Return

  ; write history depth value into the ini file
  IniWrite, %hist_depth%, %A_ScriptDir%\mkdir_hist.ini, history, depth
  ; write first dir history entry into the ini file
  IniWrite, %cur_dir%, %A_ScriptDir%\mkdir_hist.ini, history, dir1
  
  ; if history count is equal to history depth,
  ; last history entry will be deleted and each
  ; entry will be replaced by the previous one
  if ( item_cnt >= hist_depth )
    cnt := hist_depth - 1
  else
    cnt := item_cnt

  pos = 2

  ; save the rest dir history back to the ini-file
  Loop, %cnt%
  {
    IniWrite, % dir_hist%A_Index%, %A_ScriptDir%\mkdir_hist.ini, history, dir%pos%
    pos++
  }

  GoSub array_free
  
Return
;
;----------
;
array_free:
  ; clear history array (free memory)
  Loop, %item_cnt%
    dir_hist%A_Index% =

  item_cnt =
  item_pos =
Return
;
;----------
;
take_hist_dir:
  ; if history was not loaded then try to load
  if ( item_cnt = space )
  {
    GoSub read_hist_file
    if ( item_cnt < 1 )
      Return
  }

  ; calculate history item position
  item_pos += inc

  if ( item_pos > item_cnt )
    item_pos = 1
  else if ( item_pos < 1 )
    item_pos := item_cnt

  ; place history item to the edit
  ControlSetText, TEdit1, % dir_hist%item_pos%
  ; set caret position to the end
  ControlSend, TEdit1, {End}
Return
;
;----------
;
tc_show_history_list:
  ; if Alt+Down pressed in the dir creation dialog
  ; build history
  if ( item_cnt = space )
    GoSub read_hist_file

  ; if history is not empty, build history listbox
  if item_cnt > 0
  {
    ; create GUI listbox... (menu using causes some problems)
    WinGetPos, w_x, w_y, , ,ahk_Class TSTDTREEDLG
    ControlGetPos, c_x, c_y, c_w, c_h, TEdit1
    c_x += w_x
    c_y += w_y + c_h
    Gui, Margin, 0, 0
    Gui, -0x0C00000 +Delimiter`n
    Gui, Add, ListBox, +AltSubmit w%c_w% h100 vDirChoice gHistBox, %dir_hist1%
    pos = 2
    Loop, % item_cnt - 1
    {
      GuiControl, ,ListBox1, % dir_hist%pos%
      pos++
    }
    ; ...and show it under the edit field
    Gui, Show, x%c_x% y%c_y%
    ; wait for GUI to become inactive
    SetTimer, WaitGuiInactive, 100
    GuiControl, Choose, ListBox1, 1
  }
Return
;
;----------
;
read_hist_file:
  ; read history depth value from the ini-file
  IniRead, hist_depth, %A_ScriptDir%\mkdir_hist.ini, history, depth, 20
  if (hist_depth = space)
    hist_depth = 20

  ; history items counter
  item_cnt = 0
  ; trying to build history array
  Loop, %hist_depth%
  {
    IniRead, dir, %A_ScriptDir%\mkdir_hist.ini, history, dir%A_Index%
    ; skip empty entries
    if ( dir <> "ERROR" AND dir <> space)
    {
      item_cnt++
      dir_hist%item_cnt% = %dir%
    }
  }

  if ( item_cnt > 0 )
  ; current history item position
    item_pos = 0

Return
;
;----------
;
HistBox:
  ; if not Left Mouse button pressed then return
  hk := A_ThisHotKey
  if (hk <> "~LButton")
    Return
;
;----------
;
GuiSubmit:
  ; get current selection into variable
  GuiControlGet, item_pos, ,ListBox1
  GuiControl, -AltSubmit, ListBox1
  Gui, Submit

  ; wait for dir creation dialog to become active
  WinWaitActive, ahk_class TSTDTREEDLG
  ; Set edit text to the history entry
  ControlSetText, TEdit1, %DirChoice%
  ; Set caret position to the end of text
  ControlSend, TEdit1, {End}
  ; free memory
  DirChoice =
GuiEscape:
GuiClose:
  Gui, Destroy
Return
;
;----------
;
WaitGuiInactive:
  ; if GUI is inactive then destroy it
  ifWinNotActive, ahk_class AutoHotkeyGUI
  {
    Gui, Destroy
    ; turn off timer
    SetTimer, WaitGuiInactive, Off
  }
Return
;
;----------
;
wait_dlg_close:
  ; if dir creation dialog doesn't exist
  ifWinNotExist, ahk_id %dir_win_id%
  {
    ; turn off timer
    SetTimer, wait_dlg_close, Off
    ; clear windows id variable
    dir_win_id =
  }
Return


Back to AutoHotkey