Gedit」タグアーカイブ

Open Terminal in Atom and Gedit

macOS 上の Atom で Terminal.app を一発で開きたい。
もちろん編集中ファイルの位置をカレントディレクトリにして。
Gedit, VScode には最初からその機能があったけど Atom には無いのね。

早速拡張をインス、、、、、なんてしないよ。
拡張のインストールネタをやっている輩は何もアプリが作れない人だけです(暴言)
プログラミングの実力は経験値だけなのに他人が作ったものに頼っている時点でねぇ。

それと Atom ウインドウ内部で開くターミナルを勧めている輩は何だよ。
debug 目的なんだから別プロセスにしなきゃ困るだろ、と思うんだが。
って、それは別の話ということで。

Atom は Node.js 製なので child_process.exec が使える。
Gjs, jjs, JXA, Node.js – L’Isola di Niente
後はカレントディレクトリを得る手段さえ解ればアッサリ作れるはず。

いや、coffeescript という Atom の拡張以外の使い道が思いつかない言語が。。。。。
世の中にはレールなんとか以外の使い道がゼロの言語を推すみたいな人もいるけどさ。
面倒だ、既存拡張のソースを見てしまえ!

GitHub – blueimp/atom-open-terminal-here: Open the Terminal (Mac OS X, Linux) or Command Prompt (Windows) in the given directory via context menu or keyboard shortcut in the Atom text editor.

index.coffee を見る。
child_process.exec は coffeescript ではこう書けばいいのか。
atom.workspace.getActivePaneItem() から辿って編集中パスが得られるようだ。
x-terminal-emulator って何かと調べたらサル専用ディストリだけじゃん。
サルブンツ以外の Linux 使いは結局自分で作らないといけなかったわwwwww

そういえばテンプレートリテラルは coffeescript ではどう描くの?
Template literals in Coffeescript (Example)
ダブルクォートとシャープって何よ、JS と同じバッククォートとドルでいいじゃん。
従うしかないけーが、ダブルクォートのエスケープめんどいな。

Atom を Gedit のように使う – L’Isola di Niente
これに追記する。

init.coffee

atom.commands.add 'atom-text-editor', 'editor:open-terminal', ->
    path = atom.workspace.getActivePaneItem()?.buffer?.file?.path
    dirname = require('path').dirname(path)
    require('child_process').exec "open -a Terminal \"#{dirname}\""

keymap.cson

'atom-workspace atom-text-editor:not([mini])':
    #etc...
    'alt-cmd-t': 'editor:open-terminal'

command+option+T で Terminal.app を開く機能追加おしまい。
ほとんどコピペだけどこの手段が解ってしまえば応用もできるし書き方も覚えた。
経験値はこんなことの積み重ねです。

ちなみに筆者は自前拡張呼び出しは command+option+* に統一している。
Gedit では Ctrl+Alt+* と同じように統一している。
Gedit 及び Eye of Gnome プラグイン – L’Isola di Niente
そうでもしないと自分で指定したはずのキーを結構忘れるんですよコレが。
拡張を沢山インストールって人は他人が決めたキーを全部覚えるのだろうか?

おまけ。

Gedit にこの機能は最初からあるけど 3.32 は起動すると stderr を吐く。
端末から gnome-terminal と打って起動すれば同じ stderr を吐くのが解る。
ということで標準の外部ツールを書き換えしましょう。

#!/bin/sh

gnome-terminal --working-directory=$GEDIT_CURRENT_DOCUMENT_DIR > /dev/null 2>&1 &

Ctrl+Alt+T にして入出力はすべて無しに。
これで stderr の赤い出力が出なくなるのでボトムパネルも開かなくなります。

GtkPopover motion-notify-event

前回書いた GtkPopover を motion-notify-event で処理する件。
フルスクリーンかつポップオーバー表示時はシグナルを無視する。
という超単純な手段でイケた、わっはっは。

それと前々回書いた F10 でポップオーバーが斜めに出る件。
ヘッダーを出した後 g_idle_add で一旦ハンドラを抜けてから表示。
という超単純な手段でイケた、g_idle_add はマジ便利。

困ったのがポップオーバーを出した時の Esc 処理。
ポップオーバーが割り込みで非表示になるけどシグナルも飛んでくる。
上手く逃がさないとフリーズする。

class ComipoliWindow(Gtk.ApplicationWindow):
    def __init__(self, app):
        Gtk.ApplicationWindow.__init__(self, application=app)
        self.connect('motion-notify-event', self.on_motion_notify_event)
        // etc...
    def on_motion_notify_event(self, widget, event):
        if self.is_fullscreen:
            if self.menuf.popover.props.visible:
                return
            // etc...
    def key_press(self, state, keyval):
        # CapsLock?
        keymap = Gdk.Keymap.get_default()
        _state = state ^ Gdk.ModifierType.LOCK_MASK if keymap.get_caps_lock_state() else state
        # UpperCase?
        val = Gdk.keyval_to_lower(keyval) if Gdk.keyval_is_upper(keyval) else keyval
        if val == Gdk.KEY_F10:
            if self.is_fullscreen:
                self.upperbar.show()
                GLib.idle_add(self.show_fullscreen_menu)
            else:
                self.menu.clicked()
        elif val == Gdk.KEY_F11:
            self.toggle_fullscreen()
        elif val == Gdk.KEY_Escape:
            if self.upperbar.props.visible:
                self.upperbar.hide()
            elif self.is_fullscreen:
                self.toggle_fullscreen()
    def show_fullscreen_menu(self):
        self.menuf.clicked()
        return False

みたいな。

0.3.4 までボタンしか並べていなかったからこんな罠があるとは思わなかった。
不具合をなんとかするの楽しいわい、ただしマゾではない。

ところで。
Fedora 30 にアップグレードしたら Gedit 外部ツールのデフォルトが消えていた。
いや筆者はバックアップを持っているからコピーすればいいんだけど。
同じように消えてしまった人用にデフォルトコマンドを書いておきます。

# open-terminal-here (端末を開く)

#!/bin/sh
gnome-terminal --working-directory=$GEDIT_CURRENT_DOCUMENT_DIR &

# remove-trailing-spaces (末尾のスペースを取り除く)
# 入力を編集中のドキュメント、出力を置き換えるにする

#!/bin/sh
sed 's/[[:blank:]]*$//'

build, run-command, send-to-fpaste はいらないよね。
筆者は使ったことがない。

Gedit Bottom Panel Close Plugin

Wayland 環境で Gedit を使うと Ctrl+F9 が動かない。
下方パネルの表示を切り替えするキーだが微妙に不便。

Gedit でスクリプトを debug – L’Isola di Niente
で主に使う、出すのは勝手に出るので問題ない
だけど非表示にするためだけにマウスに手をのばす必要がある。
キー一発で非表示にすること”だけ”できればいいんだけどなぁ。

だったら自分で非表示にするだけのプラグインを作ればよくね?
キーを何にするか、Esc が妥当だと思うけど単キーでは動かない。
gnome-terminal の終了同様な Ctrl+D は一行削除だ。
とりあえず Ctrl+Esc にしてみた、これはお好みで。

[Plugin]
Loader=python3
Module=bottom_panel_close
IAge=3
Name=Close a Bottom Panel
Name[ja]=ボトムパネルを閉じる
Description=Close a Bottom Panel
Description[ja]=ボトムパネルを閉じる
Authors=sasakima-nao <sasakimanao@gmail.com>
Copyright=Copyright © 2018 sasakima-nao <sasakimanao@gmail.com>
Website=http://palepoli.skr.jp/

bottom_panel_close.plugin

#!/usr/bin/env python3

#    Gedit bottom_panel_close plugin version 3.30.0
#    Copyright © 2011-2018 sasakima-nao <sasakimanao@gmail.com>

import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Gedit", "3.0")
gi.require_version("Peas", "1.0")

from gi.repository import GObject, Gedit, Gtk, Gio

class BottomPanelCloseAppActivatable(GObject.Object, Gedit.AppActivatable):
    """
        Set GMenu and Accelerator
    """
    app = GObject.Property(type=Gedit.App)
 
    def __init__(self):
        GObject.Object.__init__(self)
 
    def do_activate(self):
        self.app.set_accels_for_action("win.bottomclose", ["<Control>Escape"])
        self.menu_ext = self.extend_menu("tools-section")
        item = Gio.MenuItem.new("Close Bottom Panel",  "win.bottomclose")
        self.menu_ext.append_menu_item(item)
 
    def do_deactivate(self):
        self.app.remove_accelerator("win.bottomclose", None)

class BottomPanelClosePlugin(GObject.Object, Gedit.WindowActivatable):
    __gtype_name__ = "BottomPanelClosePlugin"
    window = GObject.Property(type=Gedit.Window)
    def __init__(self):
        GObject.Object.__init__(self)

    def do_activate(self):
        self.action = Gio.SimpleAction.new("bottomclose", None)
        self.action.connect('activate', self.on_bottom_panel_close_activate)
        self.window.add_action(self.action)

    def do_deactivate(self):
        self.window.remove_action("bottomclose")

    def on_bottom_panel_close_activate(self, action, param):
        panel = self.window.get_bottom_panel()
        panel.props.visible = False

bottom_panel_close.py

Ctrl+Esc で閉じることができるようになった。
これでちょっぴりプログラミングが捗る。

@ 21:51
タイトルの bottom が buttom になっていたので書き換え。

Gedit 3.30 Auto Scroll is Bad!

Gedit 3.30 の仕様変更にまいった。

とりあえずカーソル位置からスクロールしてタブを切り換える。
そのタブをアクティブにするとカーソル位置まで勝手にスクロールしてしまう。
他のソースを参考にしながらコードを書いている時に超イライラする。

同じく Ctrl+Z で Undo するとその前の編集箇所に勝手にカーソル移動とスクロール。
これはありがたく思えるけど実際に使うと超迷惑。

それでいて gsettings のどこにも無効にする設定が見つからない。
ソースの参考は別ウインドウにする逃げ道があるけど Undo が困る。

タブを枠外にドラッグで別ウインドウにする時のアニメーションが無くなった。
相変わらず Wayland では Ctrl+F9 が使えない。

Xed にでも乗り換えようかな、と思ったらまったく同じ現象が。
どうやら GtkSourceView 自体の仕様変更のようだ、とっとと削除。
カーソル位置を見失なわないよう配慮したんだろうけど、困った。

、、、、、、、、、、

検索しまくったけど、海外すら誰もこれに気がついていないのか?
無効にする手段は見つからなかったけどこんな面白いのを見つけた。

GitHub – hardpixel/gedit-scroll-past: Gedit 3 plugin to allow scrolling past the end of file.

ファイルの終わりを過ぎてスクロールできるようにする Gedit プラグイン。
おぉ Visual Studio Code みたくできるんだ、作ってくれた人ありがとう!

、、、、、、、、、、

そんなこんなで自作アプリを更新、イライラしながら。

application.run([System.programInvocationName].concat(ARGV));

Y901x はこう書き替えただけで問題なく使えたのでとっとと出した。
gnome-mpv のサイズが完全固定できればこんなの作らなくてもいいのになぁ。

comipoli は何をやってもフルスクリーンがおかしい。
Y901x とまったく同じ処理に書き換えてさえおかしい、意味ワカラン。
早く動くようにしないとエロマンガが見られないジャン!

もう Clutter はあきらめて GtkDrawingArea に書き換えすることに。
macOS 版 comipoli のソースをコピペして cairo に書き換える。

このくらい同じなほうがお互いのメンテが楽でいいよね。
てゆーか滅茶苦茶軽くなったんだが、Clutter 遅かったのね。
作っていてよかった jxa 版。

Gedit RepeatLine Plugin

筆者は macOS で Visual Studio Code を使っている。
しかし Fedora では Gedit を使い続けている。
Fedora でも併用しようと考えたけど結局 Gedit しか使わない。

しかし Visual Studio Code には便利すぎる機能がある。
opthon(alt)+shift+down で行の複製ができる、これが超スバラシイ。

fn+left
shift+fn+right
command+c
fn+right
return
command+v

とやっていたことを一発だ、よく使うんだな行の複製って。
ちなみに fn の所は TextEdit.app 同様に command でもいい。
筆者は US 配列なので fn のほうが楽だということで。

てか mac の日本語キーボードは何故 fn が右なのか、マジで糞。
US 配列を店頭でも普通に買えるようにしてくれないかなぁ。
それは今は関係なくて。

Gedit でも同じことがやりたいぞ。
ということで Plugin を探し、、、じゃなくて作る!
何年ぶりの新規プラグイン作りだろう、ワクワク。

#-*- coding:utf-8 -*-

#    Gedit repeat plugin version 3.22.0
#    Copyright © 2018 sasakima-nao <sasakimanao@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; _endher version 2 of the License, or
#    (at your option) any later version.

import gi, os
gi.require_version("Gtk", "3.0")
gi.require_version("Gedit", "3.0")
gi.require_version("Peas", "1.0")

from gi.repository import GObject, Gedit, Gtk, Gio, GLib

class RepeatLineAppActivatable(GObject.Object, Gedit.AppActivatable):
    """
        Set GMenu and Accelerator
    """
    app = GObject.Property(type=Gedit.App)
 
    def __init__(self):
        GObject.Object.__init__(self)
 
    def do_activate(self):
        # "<Alt><Shift>Down" Not Work
        self.app.add_accelerator("<Alt><Shift>d", "win.repeatline", None)
        self.menu_ext = self.extend_menu("tools-section")
        item = Gio.MenuItem.new("Repeat Line",  "win.repeatline")
        self.menu_ext.append_menu_item(item)
 
    def do_deactivate(self):
        self.app.remove_accelerator("win.repeatline", None)

class RepeatLinePlugin(GObject.Object, Gedit.WindowActivatable):
    __gtype_name__ = "RepeatLinePlugin"
    window = GObject.Property(type=Gedit.Window)
    def __init__(self):
        GObject.Object.__init__(self)

    def do_activate(self):
        self.action = Gio.SimpleAction.new("repeatline", None)
        self.action.connect('activate', self.on_repeatline_activate)
        self.window.add_action(self.action)

    def do_deactivate(self):
        self.window.remove_action("repeatline")

    def do_update_state(self):
        self.action.set_enabled(self.window.get_active_document() != None)

    def on_repeatline_activate(self, action, param):
        view = self.window.get_active_view()
        buf = view.get_buffer()
        _start_ = buf.get_start_iter()
        _end_ = buf.get_end_iter()
        spos = buf.props.cursor_position
        epos = spos
        it = buf.get_iter_at_offset(spos - 1)
        line = None
        while 1:
            # search line start position
            if it.equal(_start_) or it.get_char() == "\n":
                line = it.copy()
                it = buf.get_iter_at_offset(epos)
                break
            spos -= 1
            it = buf.get_iter_at_offset(spos)
        while 1:
            # search line end position
            if it.equal(_end_) or it.get_char() == "\n":
                s = line.get_text(it)
                if line.equal(_start_):
                    s = "\n" + s
                buf.insert(it, s, -1)
                break;
            epos += 1
            it = buf.get_iter_at_offset(epos)

作ってみた。

残念ながら Alt+Shift+Down は無視された。
Alt+Shift+D でいいやもう。

os.getenv(GEDIT_CURRENT_LINE)
が使えると思ったけどこれはプラグインからは参照できないのね。
しかたがないので GtkTextIter で地味に \n 位置を探すことに。

先頭と最後は \n が無いけどこんな処理でイケた。
何をやっているかは GtkTextBuffer のドキュメントで。

とりあえずこれで Gedit でも同様なことができるぞい。
探せば既にあるかもだけど、プログラミングは経験値だよと一言。