Top / AppleScript

Jedit X Scripts

Jeditのスクリプトと同じものを作ってみました。でも適当に作っているので、たまたま動いているだけで不適切な部分があるかも (どのスクリプトもそうですが^^;)。アートマン21で公開されているサンプルスクリプト、特にぴよまるソフトウェア (MARo) さんのスクリプトを参考にしました。でも以下のスクリプトを使わなくても、付属ソフトウェアとか便利なものがあるので、他のやり方がいろいろ考えられると思います。

●TeXのコメントに色を付ける1

開いている前面の文書を検索して、\の直後でない%があったら行末までをコメントと見なして色を付けます。Jedit Xにはカラーリング機能があってLaTeX文書 (拡張子.texなどのファイル) は自動的にコメントも含めて色付けされるのですが。まあ普通の文書でもTeXと同様に%でコメントとすることにしていたら便利かも。

tell front document of application "Jedit X"
    set str_list to findAll string "^%.*$|[^\\\\]%.*$" with grep and select all
    repeat with aStr in str_list
        set startIndex to (loc of aStr)
        set endIndex to startIndex + (len of aStr) - 1
        if character startIndex is not "%" then
            set startIndex to startIndex + 1
        end if
        tell characters from startIndex to endIndex of text of it
            -- set font to "HeiseiMincho-W3"
            set color of it to {65535, 0, 65535}
        end tell
    end repeat
end tell

●TeXのコメントに色を付ける2

上のスクリプトは文書全体を処理しますが、これは選択したパラグラフ (いわゆる行で、改行を区切りとして分けたもの) を対象にします。

tell front document of application "Jedit X"
    set selected_para to selected paragraph range
    set first_paraid to loc of selected_para
    set last_paraid to first_paraid + (len of selected_para) - 1
    set selected paragraph range to {loc:first_paraid, len:(last_paraid - first_paraid + 1)}
    set str_list to findAll string "^%.*$|[^\\\\]%.*$" with grep
    repeat with aStr in str_list
        set startIndex to (loc of aStr)
        set endIndex to startIndex + (len of aStr) - 1
        if character startIndex is not "%" then
            set startIndex to startIndex + 1
        end if
        tell characters from startIndex to endIndex of text of it
            -- set font to "HeiseiMincho-W3"
            set color of it to {65535, 0, 65535}
        end tell
    end repeat
    set selected paragraph range to {loc:first_paraid, len:(last_paraid - first_paraid + 1)}
end tell

コメントの削除 (あるいはアンコメント) はちょっと変更して、次のようにしたらできました。

tell front document of application "Jedit X"
    set selected_para to selected paragraph range
    set first_paraid to loc of selected_para
    set last_paraid to first_paraid + (len of selected_para) - 1
    set selected paragraph range to {loc:first_paraid, len:(last_paraid - first_paraid + 1)}
    set str_list to findAll string "^%.*$|[^\\\\]%.*$" with grep
    set num_str to number of every item of str_list
    repeat with str_id from num_str to 1 by -1
        set aStr to item str_id of str_list
        set startIndex to (loc of aStr)
        set endIndex to startIndex + (len of aStr) - 1
        if character startIndex is not "%" then
            set startIndex to startIndex + 1
        end if
        tell characters from endIndex to startIndex of text of it
            -- set font to "HeiseiMincho-W3"
            -- set color of it to {65535, 0, 65535}
            set character of it to ""
        end tell
        -- set character startIndex to "" -- アンコメントする
    end repeat
    set selected paragraph range to {loc:first_paraid, len:(last_paraid - first_paraid + 1)}
end tell

●選択部分の行頭に%を付ける (TeXのコメント)

正規表現を使って行頭を処理します。

tell front document of application "Jedit X"
    set selected_para to selected paragraph range
    set first_paraid to loc of selected_para
    set last_paraid to first_paraid + (len of selected_para) - 1
    repeat with paraid from first_paraid to last_paraid
        set selected paragraph range to {loc:paraid, len:1}
        replace string "^" to "%" with grep
        -- 代わりに以下のようにすれば行頭の%を取り除ける
        -- replace string "^%" to "" with grep
        -- set color of paragraph paraid to {65535, 0, 65535}
    end repeat
    set selected paragraph range to {loc:first_paraid, len:(last_paraid - first_paraid + 1)}
end tell

微妙に実行結果が違うけど (選択部分の最初の行の扱いの違い)、次のようにしても同様に処理できます。

tell front document of application "Jedit X"
    -- 本質的には不要だけども、以下のコメント部分のような選択部分なしに実行された場合のエラー処理をした方が無難だろう
    -- set selected_area to selected character range
    -- if (len of selected_area) = 0 then return -- 選択部分なしのときには何もしないで停止
    -- if (len of selected_area) = 0 then -- 選択部分なしのときには文書全体を処理
    --     set docLen to textlen
    --     set selected character range to {loc:1, len:docLen}
    -- end if
    replaceAll string "^" to "%" with grep
end tell

●色付きHTML文書作成支援

アンダーラインなどのスタイルをどう処理すればいいかわからないのですが (できない?)、このスクリプトでは取り敢えず色だけ処理します。前のJeditのでもですが、スクリプトの最初の方にあるbasicColorsの色のコードを増やして問題ないです (RGBcolorやhexRGB256が違ってnameは重複しているとかでもいいですし、そうするとHTMLではそのRGBcolorやhexRGB256の色をnameの色で表現することになります)。

set basicColors to {¬
    {name:"black", RGBcolor:{0, 0, 0}, hexRGB256:"000000"}, ¬
    {name:"gray", RGBcolor:{32768, 32768, 32768}, hexRGB256:"808080"}, ¬
    {name:"silver", RGBcolor:{49152, 49152, 49152}, hexRGB256:"C0C0C0"}, ¬
    {name:"white", RGBcolor:{65535, 65535, 65535}, hexRGB256:"FFFFFF"}, ¬
    {name:"maroon", RGBcolor:{32768, 0, 0}, hexRGB256:"800000"}, ¬
    {name:"red", RGBcolor:{65535, 0, 0}, hexRGB256:"FF0000"}, ¬
    {name:"purple", RGBcolor:{32768, 0, 32768}, hexRGB256:"800080"}, ¬
    {name:"fuchsia", RGBcolor:{65535, 0, 65535}, hexRGB256:"FF00FF"}, ¬
    {name:"green", RGBcolor:{0, 32768, 0}, hexRGB256:"008000"}, ¬
    {name:"lime", RGBcolor:{0, 65535, 0}, hexRGB256:"00FF00"}, ¬
    {name:"olive", RGBcolor:{32768, 32768, 0}, hexRGB256:"808000"}, ¬
    {name:"yellow", RGBcolor:{65535, 65535, 0}, hexRGB256:"FFFF00"}, ¬
    {name:"navy", RGBcolor:{0, 0, 32768}, hexRGB256:"000080"}, ¬
    {name:"blue", RGBcolor:{0, 0, 65535}, hexRGB256:"0000FF"}, ¬
    {name:"teal", RGBcolor:{0, 32768, 32768}, hexRGB256:"008080"}, ¬
    {name:"aqua", RGBcolor:{0, 65535, 65535}, hexRGB256:"00FFFF"}}
tell front document of application "Jedit X"
    replaceAll string tab to "    " with select all -- タブを4つのスペースにしてみました
    replaceAll string "&" to "&" with case sensitive and select all
    replaceAll string "\"" to """ with case sensitive and select all
    replaceAll string "<" to "&lt;" with case sensitive and select all
    replaceAll string ">" to "&gt;" with case sensitive and select all
    replaceAll string "≤" to "&le;" with case sensitive and select all
    replaceAll string "≥" to "&ge;" with case sensitive and select all
    replaceAll string "«" to "&laquo;" with case sensitive and select all
    replaceAll string "»" to "&raquo;" with case sensitive and select all
    replaceAll string "≠" to "&ne;" with case sensitive and select all
    replaceAll string "¬" to "&not;" with case sensitive and select all
    -- replaceAll string "\\\\" to "&#92;" with case sensitive and select all -- 円記号がバックスラッシュで表示される?
    -- replaceAll string "'" to "&#39;" with case sensitive and select all -- シングルクォート、これもやるべきなのかしら?&apos;と書いてもいい?
    -- 必要ならば上のような文を、もっと追加するといいでしょう
    set allText to every attribute run
    set allColor to color of every attribute run
    set allText_ref to a reference to allText
    set allColor_ref to a reference to allColor
    repeat with idx from (number of every item of allText_ref) to 1 by -1
        set selected attribute range to {loc:idx, len:1}
        set digitHex to "0123456789ABCDEF"
        set codeStyle to ""
        set theColor to item idx of allColor_ref
        if theColor is not {0, 0, 0} then -- 黒のところはそのままにする
            set theRGB256 to {(item 1 of theColor) div 256, (item 2 of theColor) div 256, (item 3 of theColor) div 256}
            repeat with idxRGB from 1 to 3
                set decNum to item idxRGB of theRGB256
                set hexNum to ((character (decNum div 16 + 1) of digitHex) & (character (decNum mod 16 + 1) of digitHex)) as string
                set item idxRGB of theRGB256 to hexNum
            end repeat
            set theRGB256 to ((item 1 of theRGB256) & (item 2 of theRGB256) & (item 3 of theRGB256)) as string
            repeat with basicColor in basicColors
                if (theColor is RGBcolor of basicColor) or (theRGB256 is hexRGB256 of basicColor) then
                    set codeStyle to "color:" & (name of basicColor) & ";"
                    exit repeat
                end if
            end repeat
            if codeStyle is "" then set codeStyle to "color:#" & theRGB256 & ";"
        end if
        if codeStyle is not "" then
            if first character of codeStyle is space then set codeStyle to text 2 thru -1 in codeStyle
            if last character of codeStyle is ";" then set codeStyle to text 1 thru -2 in codeStyle
            replace string (item idx of allText_ref) to ("<span style=\"" & codeStyle & "\">" & item idx of allText_ref & "</span>")
        end if
    end repeat
end tell

Top / AppleScript