LoginSignup
4
1

More than 1 year has passed since last update.

テキストエディタ Micro を Delphi 互換のキーバインドで使う

Last updated at Posted at 2019-04-19

はじめに

ちょっと前までは Windows で MS-DOS 用のテキストエディタが動作したのですけれど、64bit Windows になってそれらが使えなくなりました。では、Win32 コンソールで動作するエディタはというと、そんなにないんですよねー。

Micro

Micro は Win32 コンソールで "も" 動作するテキストエディタで、次のプラットフォームに対応しています。

  • Windows
  • macOS
  • Linux
  • FreeBSD
  • NetBSD
  • OpenBSD

Windows で起動させるとこんな感じになります。デフォルトだと地味ですねー。
image.png

See also:

カスタマイズ

Windows 版の Micro は micro.exe 単体で動作しますので、適当なパスの通った場所に置いておくだけで使えます。環境設定ファイルは Windows ですと %USERPROFILE%\.config\micro にある settings.json です。

設定ファイルは一度 Micro を起動すると生成されます。micro.exe を実行して、〔Ctrl〕+〔Q〕で一旦終了させましょう。

基本的なカスタマイズ

まずは settings.json をいじって基本的な設定を行います。

    "fileformat": "dos",
    "tabsize": 2,
    "tabstospaces": true,
  • fileformat は改行コードです (デフォルト: unix)。dos を指定すると改行コードが CR+LF になります。
  • tabsize はタブ幅です (デフォルト: 4)。宗教論争の火種ですが、Delphi に合わせるので 2 にします。
  • tabstospaces はソフトウェアタブの指定です (デフォルト: false)。これまた宗教論争の火種ですが、Delphi に合わせるので true にしてソフトウェアタブにします。

この状態で Micro を起動して触ってみましょう。〔Shift〕+カーソル で文字列を選択できたり、〔Ctrl〕+〔X〕〔Ctrl〕+〔C〕〔Ctrl〕+〔V〕 でクリップボードのカット、コピー、ペースト操作ができたりしますよね?とてもクセのないテキストエディタです。
image.png
このまま〔Ctrl〕+〔S〕で Pascal ソースファイルとして保存してみます。そして〔Ctrl〕+〔E〕を押し、reload [Enter] と入力すると、強調構文表示されます。
image.png
Micro は 90 言語の強調構文表示に対応しています。

See also:

カラースキーム

Micro はカラースキームを変更できます。カラースキームは〔Ctrl〕+〔E〕を押してコマンドモード (プロンプトは >) に入り

> set colorscheme [カラースキーム]

で変更できます。コマンドモードでは〔Tab〕キーによる入力補完が可能です。

WIn32 コンソールで使う分には 16 色のカラースキームしか使えません。指定できるカラースキームは次の通りです。

  • simple
  • solarized
  • cmc-16
  • cmc-paper
  • geany

image.png
cmc-16 を指定してみました。

See also:

カスタムカラースキーム

カラースキームをカスタマイズするには、%USERPROFILE%\.config\microcolorschemes というサブフォルダを作成し、その中に xxxxx.micro という設定ファイルを置きます。

Turbo Pascal っぽいカラースキームファイルを作ってみました。

Turbo-16.micro
#16 colour version.
#Turbo Pascal color scheme.
color-link default ",navy"
color-link comment "bold black"
color-link constant "cyan"
color-link constant.bool "bold cyan"
color-link constant.bool.true "bold green"
color-link constant.bool.false "bold red"
color-link constant.string "yellow"
color-link constant.string.url "underline blue, white"
#color-link constant.number "constant"
color-link constant.specialChar "bold magenta"
color-link identifier "bold red"
color-link identifier.macro "bold red"
color-link identifier.var "bold blue"
#color-link identifier.class "bold green"
color-link identifier.class "bold white"
color-link statement "bold yellow"
color-link symbol "red"
color-link symbol.brackets "blue"
color-link symbol.tag "bold blue"
color-link symbol.tag.extended "bold green"
color-link preproc "bold cyan"
color-link type "green"
color-link type.keyword "bold green"
color-link special "magenta"
color-link ignore "default"
color-link error "bold ,brightred"
color-link todo "underline black,brightyellow"
color-link indent-char ",brightgreen"
color-link line-number "white"
color-link line-number.scrollbar "white"
color-link statusline "blue,white"
color-link tabbar "blue,white"
color-link current-line-number "bold yellow"
color-link current-line-number.scroller "bold yellow"
color-link gutter-error ",red"
color-link gutter-warning "red"
color-link color-column "cyan"
color-link underlined.url "underline blue, white"
color-link divider "white"

これを%USERPROFILE%\.config\micro\colorschemes にコピーし、Micro を起動していたのならコマンドモードで reload [Enter] してから、コマンドモードで

> set colorscheme Turbo-16 [Enter]

とします。あら、Turbo Pascal っぽい。
image.png

キーバインド

キーバインドを変更するには %USERPROFILE%\.config\microbindings.json という名前の設定ファイルを置きます。micro を起動していたのならコマンドモードで reload [Enter] すると設定が反映されます。

Delphi のキーバインドにほぼ近い設定ファイルを作ってみました。

bindings.json
{
    "Backspace":      "Backspace",
    "Backtab":        "OutdentSelection,OutdentLine",
    "CtrlA":          "SelectAll",
    "CtrlC":          "Copy",
    "CtrlF":          "Find",
    "CtrlO":          "OpenFile",
    "CtrlS":          "Save",
    "CtrlShiftZ":     "Redo",
    "CtrlV":          "Paste",
    "CtrlX":          "Cut",
    "CtrlZ":          "Undo",
    "Delete":         "Delete",
    "Down":           "CursorDown",
    "End":            "EndOfLine",
    "Enter":          "InsertNewline",
    "Esc":            "Escape",
    "Home":           "StartOfLine",
    "Left":           "CursorLeft",
    "PageDown":       "CursorPageDown",
    "PageUp":         "CursorPageUp",
    "Right":          "CursorRight",
    "Tab":            "IndentSelection,InsertTab",
    "Up":             "CursorUp",

    "F1":             "ToggleHelp",
    "F3":             "FindNext",

    "Shift-F3":       "FindPrevious",
    "AltPageUp":      "PreviousTab",
    "AltPageDown":    "NextTab",
    "AltG":           "JumpLine",
    "CtrlBackspace":  "DeleteWordLeft",
    "CtrlEnd":        "CursorEnd",
    "CtrlHome":       "CursorStart",
    "CtrlPageDown":   "CursorEnd",
    "CtrlPageUp":     "CursorStart",
    "CtrlUp":         "ScrollUp",
    "CtrlDown":       "ScrollDown",
    "CtrlLeft":       "WordLeft",
    "CtrlRight":      "WordRight",
    "CtrlI":          "InsertTab",
    "CtrlM":          "InsertNewline",
    "CtrlT":          "AddTab",
    "CtrlY":          "DeleteLine",
    "Backspace":      "Backspace",
    "ShiftBackspace": "Backspace",
    "ShiftEnd":       "SelectToEndOfLine",
    "ShiftEnter":     "InsertNewline",
    "ShiftHome":      "SelectToStartOfLine",
    "ShiftUp":        "SelectUp",
    "ShiftDown":      "SelectDown",
    "ShiftLeft":      "SelectLeft",
    "ShiftRight":     "SelectRight",
    "CtrlShiftLeft":  "SelectWordLeft",
    "CtrlShiftRight": "SelectWordRight",
    "CtrlShiftPageUp":    "SelectToStart",
    "CtrlShiftPageDown":  "SelectToEnd",
    "CtrlShiftI":     "IndentSelection",
    "CtrlShiftP":     "PlayMacro",
    "CtrlShiftR":     "ToggleMacro",
    "CtrlShiftS":     "SaveAs",
    "CtrlShiftU":     "OutdentSelection",

    "CtrlR":          "ToggleRuler",
    "CtrlB":          "ShellMode",
    "CtrlQ":          "Quit",
    "CtrlE":          "CommandMode",
    "CtrlW":          "NextSplit",

    // Emacs-style keybindings
    "Alt-f": "WordRight",
    "Alt-b": "WordLeft",
    "Alt-a": "StartOfLine",
    "Alt-e": "EndOfLine",

    // Mouse bindings
    "MouseWheelUp":   "ScrollUp",
    "MouseWheelDown": "ScrollDown",
    "MouseLeft":      "MousePress",
    "MouseMiddle":    "PastePrimary",
    "Ctrl-MouseLeft": "MouseMultiCursor",

    // Multiple cursors bindings
    "Alt-n": "SpawnMultiCursor",
    "Alt-m": "SpawnMultiCursorSelect",
    "Alt-p": "RemoveMultiCursor",
    "Alt-c": "RemoveAllMultiCursors",
    "Alt-x": "SkipMultiCursor",
}

〔Ctrl〕+〔F〕で文字列検索して〔F3〕で次を検索して〔Shift〕+〔F3〕で前を検索できたりします。

See also:

拡張子による関連付け

*.pas は強調構文表示されますが、*.dpr*.dpk は強調構文表示されません。pascal.yaml に拡張子を追加したものを %USERPROFILE%\.config\micro\syntax に置くと、これらの拡張子も強調構文表示されるようになります。

pascal.yaml
filetype: pascal

detect:
    filename: "(?i:\\.(pas|dpr|dpk|inc|pl0|p)$)"

rules:
    - type: "\\b(?i:(string|ansistring|widestring|shortstring|char|ansichar|widechar|boolean|byte|shortint|word|smallint|longword|cardinal|longint|integer|int64|single|currency|double|extended))\\b"
    - statement: "\\b(?i:(and|asm|array|begin|break|case|const|constructor|continue|destructor|div|do|downto|else|end|file|for|function|goto|if|implementation|in|inline|interface|label|mod|not|object|of|on|operator|or|packed|procedure|program|record|repeat|resourcestring|set|shl|shr|then|to|type|unit|until|uses|var|while|with|xor))\\b"
    - statement: "\\b(?i:(as|class|dispose|except|exit|exports|finalization|finally|inherited|initialization|is|library|new|on|out|property|raise|self|threadvar|try))\\b"
    - statement: "\\b(?i:(absolute|abstract|alias|assembler|cdecl|cppdecl|default|export|external|forward|generic|index|local|name|nostackframe|oldfpccall|override|pascal|private|protected|public|published|read|register|reintroduce|safecall|softfloat|specialize|stdcall|virtual|write))\\b"
    - constant: "\\b(?i:(false|true|nil))\\b"
    - special:
        start: "asm"
        end: "end"
        rules: []
    - constant.number: "\\$[0-9A-Fa-f]+"
    - constant.number: "\\b[+-]?[0-9]+([.]?[0-9]+)?(?i:e[+-]?[0-9]+)?"
    - constant.string:
        start: "#[0-9]{1,}"
        end: "$"
        rules: 
            - constant.specialChar: "\\\\."
    - constant.string:
        start: "#\\$[0-9A-Fa-f]{1,}"
        end: "$"
        rules: 
            - constant.specialChar: "\\\\."
    - constant.string:
        start: "'"
        end: "'"
        skip: "\\\\."
        rules:
            - constant.specialChar: "\\\\."
    - preproc:
        start: "{\\$"
        end: "}"
        rules: []
    - comment:
        start: "//"
        end: "$"
        rules: []
    - comment:
        start: "\\(\\*"
        end: "\\*\\)"
        rules: []
    - comment:
        start: "({)(?:[^$])"
        end: "}"
        rules: []

See also:

Pascal を使う

私の PC のこのフォルダには PASCAL-P5 が置いてあるので、〔Ctrl〕+〔B〕してシェルモード (プロンプトは $) に入り、

$ compile test

ってやると Pascal のソースコードがコンパイルされ、
image.png
エラーがなければ続けてシェルモードから

$ run test

ってやると実行されます。
image.png
この PC は Delphi 使いのものですから、当然 Delphi がインストールされているため、シェルモードから

$ dcc32 -CC test.pas

ってやれば Win32 用コマンドラインコンパイラ DCC32 を使ってコンパイルでき、
image.png
TEST.EXE が生成されているので、シェルモードから

$ test

ってやれば EXE が実行できます。
image.png
See also:

おわりに

コマンドラインツールを使う場合には普通に便利なテキストエディタ Micro でした。
image.png
Qiita にはテキストエディタ Micro に関する秀逸な記事があるのでご一読をお勧めします。

See also:

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1