Skip to content

Commit

Permalink
Implement "Insert Script Shebang Line".
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Nov 24, 2018
1 parent b6404db commit 0ef8c9b
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ void EditAutoCloseXMLTag(HWND hwnd);
void EditAutoIndent(HWND hwnd);
void EditToggleCommentLine(HWND hwnd);
void EditToggleCommentBlock(HWND hwnd);
void EditInsertScriptShebangLine(HWND hwnd);
void EditShowCallTips(HWND hwnd, Sci_Position position);

#define NCP_DEFAULT 1
Expand Down
86 changes: 86 additions & 0 deletions src/EditAutoC.c
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,92 @@ void EditToggleCommentBlock(HWND hwnd) {
}
}

// see Style_SniffShebang() in Styles.c
void EditInsertScriptShebangLine(HWND hwnd) {
char line[128];
strcpy(line, "#!/usr/bin/env ");

switch (pLexCurrent->iLexer) {
case SCLEX_BASH:
switch (np2LexLangIndex) {
case IDM_LANG_CSHELL:
strcpy(line, "#!/bin/csh");
break;

case IDM_LANG_M4:
strcat(line, "m4");
break;

default:
strcpy(line, "#!/bin/bash");
break;
}
break;

case SCLEX_CPP:
switch (pLexCurrent->rid) {
case NP2LEX_AWK:
strcat(line, "awk");
break;

case NP2LEX_GO:
strcat(line, "go");
break;

case NP2LEX_GROOVY:
strcat(line, "groovy");
break;

case NP2LEX_JS:
strcat(line, "node");
break;

case NP2LEX_PHP:
strcat(line, "php");
break;

case NP2LEX_SCALA:
strcat(line, "scala");
break;

default:
return;
}
break;

case SCLEX_LUA:
strcat(line, "lua");
break;

case SCLEX_PERL:
strcat(line, "perl");
break;

case SCLEX_PYTHON:
strcat(line, "python");
break;

case SCLEX_RUBY:
strcat(line, "ruby");
break;

case SCLEX_TCL:
strcat(line, "wish");
break;

default:
return;
}

const Sci_Position iCurrentPos = SciCall_GetCurrentPos();
if (iCurrentPos == 0) {
const int iEOLMode = (int)SendMessage(hwnd, SCI_GETEOLMODE, 0, 0);
LPCSTR lineEnd = (iEOLMode == SC_EOL_LF) ? "\n" : ((iEOLMode == SC_EOL_CR) ? "\r" : "\r\n");
strcat(line, lineEnd);
}
SendMessage(hwnd, SCI_REPLACESEL, 0, (LPARAM)line);
}

void EditShowCallTips(HWND hwnd, Sci_Position position) {
const int iLine = (int)SendMessage(hwnd, SCI_LINEFROMPOSITION, position, 0);
const int iDocLen = SciCall_GetLine(iLine, NULL); // get length
Expand Down
5 changes: 5 additions & 0 deletions src/Notepad2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2281,6 +2281,7 @@ void MsgInitMenu(HWND hwnd, WPARAM wParam, LPARAM lParam) {
EnableCmd(hmenu, IDM_EDIT_STREAMCOMMENT, bCurrentLexerHasBlockComment);

EnableCmd(hmenu, IDM_EDIT_INSERT_ENCODING, *mEncoding[iEncoding].pszParseNames);
EnableCmd(hmenu, IDM_EDIT_INSERT_SHEBANG, bCurrentLexerHasShebangLine);

//EnableCmd(hmenu, IDM_EDIT_INSERT_SHORTDATE, !bReadOnly);
//EnableCmd(hmenu, IDM_EDIT_INSERT_LONGDATE, !bReadOnly);
Expand Down Expand Up @@ -3284,6 +3285,10 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) {
}
break;

case IDM_EDIT_INSERT_SHEBANG:
EditInsertScriptShebangLine(hwndEdit);
break;

case IDM_EDIT_INSERT_SHORTDATE:
case IDM_EDIT_INSERT_LONGDATE: {
WCHAR tchDateTime[256];
Expand Down
2 changes: 1 addition & 1 deletion src/Notepad2.rc
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ BEGIN
MENUITEM "Time/Date (&Long Form)\tCtrl+Shift+F5", IDM_EDIT_INSERT_LONGDATE
MENUITEM SEPARATOR
MENUITEM "&Encoding Identifier\tCtrl+F8", IDM_EDIT_INSERT_ENCODING
//MENUITEM "Script She&bang Line", IDM_EDIT_INSERT_SHEBANG
MENUITEM "Script She&bang Line", IDM_EDIT_INSERT_SHEBANG
END
POPUP "Spec&ial"
BEGIN
Expand Down
18 changes: 18 additions & 0 deletions src/Styles.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ static int iLexerLoadedCount = 0;
BOOL bUse2ndDefaultStyle;
BOOL bCurrentLexerHasLineComment;
BOOL bCurrentLexerHasBlockComment;
BOOL bCurrentLexerHasShebangLine;
static UINT fStylesModified = STYLESMODIFIED_NONE;
static BOOL fWarnedNoIniFile = FALSE;
static int iBaseFontSize = 11*SC_FONT_SIZE_MULTIPLIER; // 11 pt in lexDefault
Expand Down Expand Up @@ -836,6 +837,22 @@ static inline BOOL DidLexerHasBlockComment(int iLexer, int rid) {
);
}

static inline BOOL DidLexerHasShebangLine(int iLexer, int rid) {
return iLexer == SCLEX_BASH
|| iLexer == SCLEX_LUA
|| iLexer == SCLEX_PERL
|| iLexer == SCLEX_PYTHON
|| iLexer == SCLEX_RUBY
|| iLexer == SCLEX_TCL
|| rid == NP2LEX_AWK
|| rid == NP2LEX_GO
|| rid == NP2LEX_GROOVY
|| rid == NP2LEX_JS
|| rid == NP2LEX_PHP
|| rid == NP2LEX_SCALA
;
}

static void Style_Parse(struct DetailStyle *style, LPCWSTR lpszStyle);
static void Style_SetParsed(HWND hwnd, const struct DetailStyle *style, int iStyle);
static inline void Style_SetDefaultStyle(HWND hwnd, int index) {
Expand Down Expand Up @@ -1117,6 +1134,7 @@ void Style_SetLexer(HWND hwnd, PEDITLEXER pLexNew) {
pLexCurrent = pLexNew;
bCurrentLexerHasLineComment = DidLexerHasLineComment(iLexer);
bCurrentLexerHasBlockComment = DidLexerHasBlockComment(iLexer, rid);
bCurrentLexerHasShebangLine = DidLexerHasShebangLine(iLexer, rid);
UpdateStatusBarCache(STATUS_LEXER);
UpdateLineNumberWidth();
UpdateFoldMarginWidth();
Expand Down
1 change: 1 addition & 0 deletions src/Styles.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extern int np2LexLangIndex;
extern BOOL bUse2ndDefaultStyle;
extern BOOL bCurrentLexerHasLineComment;
extern BOOL bCurrentLexerHasBlockComment;
extern BOOL bCurrentLexerHasShebangLine;
extern UINT8 currentLexKeywordAttr[NUMKEYWORD];

BOOL IsDocWordChar(int ch);
Expand Down
1 change: 1 addition & 0 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@
#define IDM_EDIT_INSERT_TIMESTAMP_MS 40385 // milli
#define IDM_EDIT_INSERT_TIMESTAMP_US 40386 // micro
#define IDM_EDIT_INSERT_TIMESTAMP_NS 40387 // nano
#define IDM_EDIT_INSERT_SHEBANG 40388

#define IDM_EDIT_SELTOEND 40389
#define IDM_EDIT_SELTOBEGIN 40390
Expand Down

0 comments on commit 0ef8c9b

Please sign in to comment.