Skip to content

Commit cfb649c

Browse files
committed
refactor: extract unique naming utilities to an external package
1 parent f1a79dc commit cfb649c

File tree

2 files changed

+4
-76
lines changed

2 files changed

+4
-76
lines changed

core/me-lib.el

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,81 +1177,6 @@ scaling factor for the font in Emacs' `face-font-rescale-alist'. See the
11771177

11781178

11791179

1180-
;;; Unique name from directory
1181-
1182-
(defvar +unique-name-map-default (make-hash-table :test 'equal))
1183-
(defvar +unique-name-format "%s[%s]")
1184-
1185-
(defun +unique-name--get-dir-elements (dir)
1186-
(butlast (reverse (file-name-split (directory-file-name (expand-file-name dir))))))
1187-
1188-
(defun +unique-name--unique-dir-elements (dir1 dir2)
1189-
"Return unique elements of DIR1 and DIR2."
1190-
(let* ((els1 (+unique-name--get-dir-elements dir1))
1191-
(els2 (+unique-name--get-dir-elements dir2)))
1192-
(while-let ((el1 (car els1))
1193-
(el2 (car els2))
1194-
(_ (string= el1 el2)))
1195-
(pop els1) (pop els2))
1196-
(cons els1 els2)))
1197-
1198-
(cl-defun +unique-name-create-or-update (dir &key ((:map map-sym) '+unique-name-map-default) ((:rename-fn rename-func) nil))
1199-
"See `+unique-name-register'."
1200-
(let* ((dir (expand-file-name dir))
1201-
(name (file-name-nondirectory (directory-file-name (expand-file-name dir))))
1202-
(unique-map (eval map-sym))
1203-
(unique-name
1204-
(cl-loop for other-path in (hash-table-keys unique-map)
1205-
with len-min = most-positive-fixnum
1206-
with len-max = most-negative-fixnum
1207-
with max-path = nil
1208-
collect
1209-
(let ((curr-element (gethash other-path unique-map)))
1210-
(when (and (not (string= dir other-path)) ; not the same dir
1211-
(string= name (alist-get 'dir-name curr-element))) ; have the same name
1212-
(cl-destructuring-bind (els1 . els2) (+unique-name--unique-dir-elements dir other-path)
1213-
(let ((len (length els1)))
1214-
(setq len-min (min len-min len))
1215-
(when (> len len-max)
1216-
(setq len-max len
1217-
max-path els1))))))
1218-
finally return
1219-
(let ((s (string-join
1220-
(reverse (butlast max-path
1221-
(- (length max-path)
1222-
(1+ (- len-max len-min)))))
1223-
"/")))
1224-
(if (string-empty-p s)
1225-
name
1226-
(format +unique-name-format name s))))))
1227-
(let* ((old (gethash dir unique-map))
1228-
(old-name (assoc 'unique-name old)))
1229-
(when (and (functionp rename-func) (not (equal (cdr old-name) unique-name)))
1230-
(ignore-errors (funcall rename-func (cdr old-name) unique-name)))
1231-
(if old
1232-
(setcdr old-name unique-name)
1233-
(puthash dir `((dir-name . ,name) (unique-name . ,unique-name)) unique-map)))))
1234-
1235-
(cl-defun +unique-name-register (dir &key ((:map map-sym) '+unique-name-map-default) ((:rename-fn rename-func) nil))
1236-
"Make a unique name derived from DIR.
1237-
The :MAP is a symbol for the hash-table used to register the names, all
1238-
names will be renamed accordingly when needed.
1239-
The :RENAME-FN is a function of signature (OLD NEW), called before renaming
1240-
the hash-table elements."
1241-
(append
1242-
`((path . ,dir))
1243-
(if-let* ((dir (expand-file-name dir))
1244-
(name (file-name-nondirectory (directory-file-name (expand-file-name dir))))
1245-
(unique-map (eval map-sym))
1246-
(element (gethash dir unique-map)))
1247-
element
1248-
(puthash dir `((dir-name . ,name) (unique-name . ,name)) unique-map)
1249-
(dolist (path (hash-table-keys unique-map)) ; Update all the names
1250-
(+unique-name-create-or-update path :map map-sym :rename-fn rename-func))
1251-
(gethash dir unique-map))))
1252-
1253-
1254-
12551180
(provide 'me-lib)
12561181

12571182
;;; me-lib.el ends here

modules/me-workspaces.el

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
;;; Code:
1515

16+
(use-package unique-dir-name
17+
:straight (:host github :repo "abougouffa/unique-dir-name"))
18+
1619
(use-package project-tab-groups
1720
:straight t
1821
:after project
@@ -35,7 +38,7 @@
3538
#'project-tab-groups--select-or-create-tab-group :after-while
3639
(satch-defun +project-tab-groups--name-tab-by-group:after-while-a (&rest _)
3740
(when-let ((group-path (alist-get 'group (tab-bar--current-tab))))
38-
(+unique-name-register group-path :map '+project-tab-groups-unique-map)
41+
(unique-dir-name-register group-path :map '+project-tab-groups-unique-map)
3942
;; Rename all tabs accordingly
4043
(dolist (frame (frame-list))
4144
(dolist (tab (frame-parameter frame 'tabs))

0 commit comments

Comments
 (0)