Python Tab

English main discussion
Post Reply
  • Author
  • Message
Offline
Posts: 165
Joined: Fri Aug 15, 2008 8:58 am

Python Tab

Post by Diamen »

I normally use \t for tab.
Do it is possible to automatically switch to 4 space when edit a .py file?

DV
Offline
Posts: 1294
Joined: Thu Nov 16, 2006 11:53 am
Location: Kyiv, Ukraine

Re: Python Tab

Post by DV »

I think the Tab/Spaces setting is global for all languages and there is no built-in ability to make it different for different languages.

Probably a resident script can help with this. The idea is to analyze the file extension on AKDN_OPENDOCUMENT_FINISH and AKDN_SAVEDOCUMENT_FINISH and set the desired Tab/Spaces setting basing on the file extension. The resident script should subclass AkelPad's main proc similarly to this:
https://github.com/d0vgan/AkelPad-Scrip ... toFocus.js
Then, in the MainCallback, we should process AKDN_OPENDOCUMENT_FINISH (0x436) and AKDN_SAVEDOCUMENT_FINISH (0x438).
To get the file extension, one should call AkelPad.GetEditFile(0) and then extract the extension similarly to getFileExt in https://github.com/d0vgan/AkelPad-Scrip ... s/RunMe.js
Finally, to deal with Tabs/Spaces, probably the field bTabStopAsSpaces of FRAMEDATA should be used. Probably a combination of AKD_GETFRAMEINFO + AKD_SETFRAMEINFO should be used for this.
(By the way, the approach with bTabStopAsSpaces is used in InsertTabOrSpaces.js that can be loaded using the AkelUpdater).

The approach with the resident script looks somewhat complicated, so maybe instead you could create two simple scripts for Tab and Shift+Tab (e.g. Tab.js and ShiftTab.js and assign Tab and Shift+Tab keys to call these scripts). In each of these scripts, you may analyze the file extension and insert/remove either Tab or Space characters.

DV
Offline
Posts: 1294
Joined: Thu Nov 16, 2006 11:53 am
Location: Kyiv, Ukraine

Re: Python Tab

Post by DV »

I think something similar to this should work:

Code: Select all

var hWndMain = AkelPad.GetMainWnd();
var oSys = AkelPad.SystemFunction();

var AKD_JS_SCRIPT_STOP   = 0x7CDE; // unique message (must not be equal to any existing AKD_* message)
var UNIQUE_SCRIPT_ID     = 0x1A2BC82F; // unique wParam id to identify _this_ script file

AkelPad.ScriptNoMutex();

var mutexName = "SettingsByFileExt_js_" + hWndMain;
var hMutex;
if (hMutex = oSys.Call("kernel32::CreateMutex" + _TCHAR, 0, 1, mutexName))
{
  if (oSys.GetLastError() == 183 /*ERROR_ALREADY_EXISTS*/)
  {
    oSys.Call("kernel32::CloseHandle", hMutex);
    oSys.Call("user32::PostMessage" + _TCHAR, hWndMain, AKD_JS_SCRIPT_STOP, UNIQUE_SCRIPT_ID, 0);
    WScript.Quit();
  }
}

applySettingsForFile();

var hMainSubClass;
if (hMainSubClass = AkelPad.WindowSubClass(1 /*WSC_MAINPROC*/, MainCallback))
{
  AkelPad.WindowGetMessage(); //Message loop

  AkelPad.WindowUnsubClass(1 /*WSC_MAINPROC*/);
}

function MainCallback(hWnd, uMsg, wParam, lParam)
{
  if (uMsg == 0x416 /*AKDN_FRAME_ACTIVATE*/
   || uMsg == 0x436 /*AKDN_OPENDOCUMENT_FINISH*/
   || uMsg == 0x438 /*AKDN_SAVEDOCUMENT_FINISH*/)
  {
    applySettingsForFile();
  }
  else if ((uMsg == 0x406 /*AKDN_MAIN_ONFINISH*/)
        || (uMsg == AKD_JS_SCRIPT_STOP && wParam == UNIQUE_SCRIPT_ID))
  {
    oSys.Call("user32::PostQuitMessage", 0); // exit the message loop
  }

  return 0;
}

function applySettingsForFile()
{
  var filePath = AkelPad.GetEditFile(0);
  var fileExt = getFileExt(filePath).toLowerCase();

  if (fileExt == "py")
  {
    // Always replacing Tabs with Spaces:
    var bTabStopAsSpaces = AkelPad.SendMessage(hWndMain, 1223 /*AKD_GETFRAMEINFO*/, 53 /*FI_TABSTOPASSPACES*/, 0);
    if (!bTabStopAsSpaces)
    {
      AkelPad.SetFrameInfo(0, 2 /*FIS_TABSTOPASSPACES*/, 1);
    }
  }
}

function getFileExt(filePathName) // file extension w/o leading '.'
{
  var n = filePathName.lastIndexOf(".");
  return (n >= 0) ? filePathName.substr(n + 1) : "";
}
Post Reply