Skip to content

Instantly share code, notes, and snippets.

@MagneFire
Last active October 13, 2024 19:20
Show Gist options
  • Save MagneFire/4fa65edc3e0387ac0b3d5199b3102307 to your computer and use it in GitHub Desktop.
Save MagneFire/4fa65edc3e0387ac0b3d5199b3102307 to your computer and use it in GitHub Desktop.
nemo open in code extension. Open vscode in the current directory using the Ctrl+Alt+T keyboard shortcut.
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
__author__ = "Darrel Griët"
__maintainer__ = "Darrel Griët <[email protected]>"
__version__ = "1.0"
__appname__ = "nemo-code"
__app_disp_name__ = "Nemo Code"
__website__ = "http://github.com/linuxmint/nemo-extensions"
import os
import sys
import subprocess
from signal import SIGTERM, SIGKILL
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
import codecs
import gettext
gettext.bindtextdomain("nemo-extensions")
gettext.textdomain("nemo-extensions")
_ = gettext.gettext
import gi
gi.require_version('Nemo', '3.0')
from gi.repository import GObject, Nemo, Gtk, Gdk, GLib, Gio
class Crowbar(object):
def __init__(self, uri, window):
"""The constructor."""
self._uri = uri
self._window = window
#Crowbar
self._crowbar = Gtk.EventBox()
self._crowbar.connect_after("parent-set", self._on_crowbar_parent_set)
#Lock
self._lock = False
def get_widget(self):
"""Returns the crowbar."""
return self._crowbar
def _uri_to_path(self, uri):
gfile = Gio.file_parse_name(uri)
path = gfile.get_path()
if path:
return path
else:
return ""
def open_code(self):
path = self._uri_to_path(self._uri)
subprocess.call(["code", path])
def _on_crowbar_parent_set(self, widget, old_parent):
#Check if the work has already started
if self._lock:
return
else:
self._lock = True
print("URI: %s" % self._uri)
window = self._window
#Register the callback for show/hide
if hasattr(window, "key_match_cb"):
window.key_match_cb.clear()
window.key_match_cb.append(self.open_code)
class NemoCodeProvider(GObject.GObject, Nemo.LocationWidgetProvider, Nemo.NameAndDescProvider):
def __init__(self):
print("[%s] I: Initializing the Nemo Keys extension"
% __app_disp_name__)
def get_widget(self, uri, window):
if not hasattr(window, "key_match_cb"):
window.key_match_cb = []
# don't add terminals to the desktop
if uri.startswith("x-nemo-desktop:///"):
return
#Event
window.connect_after("key-press-event", self._handle_keypress)
#Return the crowbar
return Crowbar(uri, window).get_widget()
def _handle_keypress(self, window, event):
# key = "<Primary><Shift>c"
key = "c"
print("KEY: %s" % event.keyval)
print("D: %s" % Gdk.keyval_from_name(key))
if event.keyval == Gdk.keyval_from_name(key):
print("c")
if event.state & Gdk.ModifierType.CONTROL_MASK:
print("ctrl")
if event.state & Gdk.ModifierType.MOD1_MASK:
print("alt")
if event.keyval == Gdk.keyval_from_name(key) \
and event.state & Gdk.ModifierType.CONTROL_MASK \
and event.state & Gdk.ModifierType.MOD1_MASK:
print("TRIGGERED!")
for callback in window.key_match_cb:
print("calling")
callback()
return Gdk.EVENT_STOP #Stop the event propagation
return Gdk.EVENT_PROPAGATE
def get_name_and_desc(self):
return [("Nemo Code:::code keyboard shortcut for Nemo:::nemo-code-prefs")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment