From f34c82f7654ad2c751524095b389c39343ab828b Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 15 Dec 2019 22:53:58 +0000 Subject: refactor(emacs-pkgs): Extract term-switcher into separate emacs pkg --- tools/emacs-pkgs/term-switcher/default.nix | 14 +++++ tools/emacs-pkgs/term-switcher/term-switcher.el | 72 +++++++++++++++++++++++++ tools/emacs/config/term-setup.el | 36 ------------- 3 files changed, 86 insertions(+), 36 deletions(-) create mode 100644 tools/emacs-pkgs/term-switcher/default.nix create mode 100644 tools/emacs-pkgs/term-switcher/term-switcher.el delete mode 100644 tools/emacs/config/term-setup.el (limited to 'tools') diff --git a/tools/emacs-pkgs/term-switcher/default.nix b/tools/emacs-pkgs/term-switcher/default.nix new file mode 100644 index 0000000000..ad96638a05 --- /dev/null +++ b/tools/emacs-pkgs/term-switcher/default.nix @@ -0,0 +1,14 @@ +{ pkgs, ... }: + +with pkgs.third_party.emacsPackagesNg; + +melpaBuild rec { + pname = "term-switcher"; + version = "1.0"; + src = ./term-switcher.el; + packageRequires = [ dash ivy s ]; + + recipe = builtins.toFile "recipe" '' + (term-switcher :fetcher github :repo "tazjin/depot") + ''; +} diff --git a/tools/emacs-pkgs/term-switcher/term-switcher.el b/tools/emacs-pkgs/term-switcher/term-switcher.el new file mode 100644 index 0000000000..66247d6824 --- /dev/null +++ b/tools/emacs-pkgs/term-switcher/term-switcher.el @@ -0,0 +1,72 @@ +;;; term-switcher.el --- Easily switch between open X11 terminals +;; +;; Copyright (C) 2019 Google Inc. +;; +;; Author: Vincent Ambo +;; Version: 1.0 +;; Package-Requires: (dash ivy s) +;; +;;; Commentary: +;; +;; This package adds a function that lets users quickly switch between +;; different open X11 terminals using ivy. +;; +;; It is primarily intended to be used by EXWM users who use graphical +;; terminals inside of Emacs. +;; +;; Users MUST configure the group `term-switcher' and can then bind +;; `ts/switch-to-terminal' to an appropriate key. + +(require 'dash) +(require 'ivy) +(require 's) + +(defgroup term-switcher nil + "Customization options for configuring `term-switcher' with the + user's terminal emulator of choice.") + +(defcustom term-switcher-program "gnome-terminal" + "X11 terminal application to use." + :type '(string) + :group 'term-switcher) + +(defcustom term-switcher-buffer-prefix "Term" + "String prefix for X11 terminal buffers. For example, if your + EXWM configuration renames X11 terminal buffers to + `Term' you might want to use `Term' as the matching + prefix." + :type '(string) + :group 'term-switcher) + +(defun ts/run-terminal-program () + (message "Starting %s..." term-switcher-program) + (start-process-shell-command term-switcher-program nil term-switcher-program)) + +(defun ts/open-or-create-terminal-buffer (buffer-name) + "Switch to the buffer with BUFFER-NAME or create a new buffer + running the configured X11 terminal." + (let ((buffer (get-buffer buffer-name))) + (if (not buffer) + (ts/run-terminal-program) + (switch-to-buffer buffer)))) + +(defun ts/is-terminal-buffer (buffer) + "Determine whether BUFFER runs an X11 terminal." + (and (equal 'exwm-mode (buffer-local-value 'major-mode buffer)) + (s-starts-with? term-switcher-buffer-prefix (buffer-name buffer)))) + +(defun ts/switch-to-terminal () + "Switch to an X11 terminal buffer, or create a new one." + (interactive) + (let ((terms (-map #'buffer-name + (-filter #'ts/is-terminal-buffer (buffer-list))))) + (if terms + (ivy-read "Switch to terminal buffer: " + (cons "New terminal" terms) + :caller 'ts/switch-to-terminal + :preselect (s-concat "^" term-switcher-buffer-prefix) + :require-match t + :action #'ts/open-or-create-terminal-buffer) + (ts/run-terminal-program)))) + +(provide 'term-switcher) diff --git a/tools/emacs/config/term-setup.el b/tools/emacs/config/term-setup.el deleted file mode 100644 index cd4f9c25ef..0000000000 --- a/tools/emacs/config/term-setup.el +++ /dev/null @@ -1,36 +0,0 @@ -;; Utilities for X11 terminal buffers. - -(defvar x11-terminal-program "gnome-terminal" - "Which X11 terminal application to use.") - -(defvar x11-terminal-buffer-prefix "Term" - "String prefix for X11 terminal buffer names.") - -(defun open-or-create-terminal-buffer (buffer-name) - "Switch to the buffer with BUFFER-NAME or create a new buffer - running the configured X11 terminal." - (let ((buffer (get-buffer buffer-name))) - (if (not buffer) - (run-external-command x11-terminal-program) - (switch-to-buffer buffer)))) - -(defun is-terminal-buffer (buffer) - "Determine whether BUFFER runs an X11 terminal." - (and (equal 'exwm-mode (buffer-local-value 'major-mode buffer)) - (s-starts-with? x11-terminal-buffer-prefix (buffer-name buffer)))) - -(defun counsel-switch-to-terminal () - "Switch to an X11 terminal buffer, or create a new one." - (interactive) - (let ((terms (-map #'buffer-name - (-filter #'is-terminal-buffer (buffer-list))))) - (if terms - (ivy-read "Switch to terminal buffer: " - (cons "New terminal" terms) - :caller 'counsel-switch-to-terminal - :preselect (s-concat "^" x11-terminal-buffer-prefix) - :require-match t - :action #'open-or-create-terminal-buffer) - (run-external-command x11-terminal-program)))) - -(provide 'term-setup) -- cgit 1.4.1