summaryrefslogtreecommitdiffstats
path: root/ymh-emacs/ymh-tabs.el
blob: ea36f6e25aa7f653443f8ae219f820ae3ee419e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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