summaryrefslogtreecommitdiffstats
path: root/ymh-emacs/ymh-tabs.el
diff options
context:
space:
mode:
Diffstat (limited to 'ymh-emacs/ymh-tabs.el')
-rw-r--r--ymh-emacs/ymh-tabs.el40
1 files changed, 40 insertions, 0 deletions
diff --git a/ymh-emacs/ymh-tabs.el b/ymh-emacs/ymh-tabs.el
new file mode 100644
index 0000000..ea36f6e
--- /dev/null
+++ b/ymh-emacs/ymh-tabs.el
@@ -0,0 +1,40 @@
+;;; ymh-tabs.el --- Extensions to organising tabs -*- lexical-binding: t; -*-
+
+;;; Commentary:
+
+;; Initially inspired by
+;; https://mihaiolteanu.me/emacs-workspace-management.html.
+
+;;; Code:
+
+(defun ymh-tabs--create (name)
+ "Create the NAME tab if it doesn't exist already."
+ (condition-case nil
+ (unless (equal (alist-get 'name (tab-bar--current-tab))
+ name)
+ (tab-bar-rename-tab-by-name name name)
+ nil)
+ (error (tab-new)
+ (tab-bar-rename-tab name)
+ t)))
+
+(defun ymh-tabs--get-name-from-path (dir)
+ "Get a tab name from a project path DIR."
+ (file-name-base (directory-file-name dir)))
+
+(defun ymh-tabs-switch-project (dir)
+ "\"Switch\" to another project by running an Emacs command.
+The available commands are presented as a dispatch menu
+made from `project-switch-commands'.
+
+When called in a program, it will use the project corresponding
+to directory DIR."
+ (interactive (list (project-prompt-project-dir)))
+ (let ((proj-name (ymh-tabs--get-name-from-path dir)))
+ (if (ymh-tabs--create proj-name)
+ (project-switch-project dir)
+ (tab-bar-switch-to-tab proj-name))))
+
+(provide 'ymh-tabs)
+
+;;; ymh-tabs.el ends here