From f7e155dec47fcdc1e8fc8941970509991af5fdbb Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 21 Dec 2020 16:00:15 +0800 Subject: [PATCH 01/15] Shorten project and recent files. --- dashboard-widgets.el | 55 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/dashboard-widgets.el b/dashboard-widgets.el index 11b3beb1..40b76c6d 100644 --- a/dashboard-widgets.el +++ b/dashboard-widgets.el @@ -273,6 +273,16 @@ If nil it is disabled. Possible values for list-type are: :type '(repeat (alist :key-type symbol :value-type string)) :group 'dashboard) +(defcustom dashboard-path-max-length 70 + "Maximum length for path to display." + :type 'string + :group 'dashboard) + +(defcustom dashboard-path-shorten-string "..." + "String the that displays in the center of the path." + :type 'string + :group 'dashboard) + (defvar recentf-list nil) (defvar dashboard-buffer-name) @@ -633,18 +643,50 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." (insert (propertize footer 'face 'dashboard-footer)) (insert "\n")))) +;; +;; Truncate +;; +(defun dashboard-shorten-path (path) + "Shorten the PATH." + (setq path (abbreviate-file-name path)) + (let* ((len-path (length path)) (len-rep (length dashboard-path-shorten-string)) + (len-total (- dashboard-path-max-length len-rep)) + (center (/ len-total 2)) + (end-back center) + (start-front (- len-path center)) + back front ) + (if (<= len-path dashboard-path-max-length) path + (setq back (substring path 0 end-back) + front (substring path start-front len-path)) + (concat back dashboard-path-shorten-string front)))) + +(defun dashboard-shorten-paths (paths alist) + "Shorten all path from PATHS." + (let (lst-display abbrev) + (setf (symbol-value alist) nil) ; reset + (dolist (item paths) + (setq abbrev (dashboard-shorten-path item)) + ;; store `abbrev' as id; and `item' with value + (push (cons abbrev item) (symbol-value alist)) + (push abbrev lst-display)) + (reverse lst-display))) + ;; ;; Recentf ;; +(defvar dashboard-recentf-alist nil + "Alist records shorten's recent files and it's full paths.") + (defun dashboard-insert-recents (list-size) "Add the list of LIST-SIZE items from recently edited files." (recentf-mode) (dashboard-insert-section "Recent Files:" - recentf-list + (dashboard-shorten-paths recentf-list 'dashboard-recentf-alist) list-size (dashboard-get-shortcut 'recents) - `(lambda (&rest ignore) (find-file-existing ,el)) + `(lambda (&rest ignore) + (find-file-existing (cdr (assoc ,el dashboard-recentf-alist)))) (abbreviate-file-name el))) ;; @@ -676,15 +718,20 @@ switch to." :type '(choice (const :tag "Default" nil) function) :group 'dashboard) +(defvar dashboard-projects-alist nil + "Alist records the shorten's project paths and it's full paths.") + (defun dashboard-insert-projects (list-size) "Add the list of LIST-SIZE items of projects." (dashboard-insert-section "Projects:" - (dashboard-subseq (dashboard-projects-backend-load-projects) 0 list-size) + (dashboard-shorten-paths (dashboard-subseq (dashboard-projects-backend-load-projects) 0 list-size) + 'dashboard-projects-alist) list-size (dashboard-get-shortcut 'projects) `(lambda (&rest ignore) - (funcall (dashboard-projects-backend-switch-function) ,el)) + (funcall (dashboard-projects-backend-switch-function) + (cdr (assoc ,el dashboard-projects-alist)))) (abbreviate-file-name el))) (defun dashboard-projects-backend-load-projects () From 5f1422792392acf29ced4ac5121074efc6a3f75c Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 21 Dec 2020 16:13:07 +0800 Subject: [PATCH 02/15] Add path style option. --- dashboard-widgets.el | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/dashboard-widgets.el b/dashboard-widgets.el index 40b76c6d..02b47fec 100644 --- a/dashboard-widgets.el +++ b/dashboard-widgets.el @@ -273,9 +273,15 @@ If nil it is disabled. Possible values for list-type are: :type '(repeat (alist :key-type symbol :value-type string)) :group 'dashboard) +(defcustom dashboard-path-style nil + "Style to display path." + :type '(choice (const :tag "No specify" nil) + (const :tag "Truncate by length" truncate-length)) + :group 'dashboard) + (defcustom dashboard-path-max-length 70 "Maximum length for path to display." - :type 'string + :type 'integer :group 'dashboard) (defcustom dashboard-path-shorten-string "..." @@ -646,9 +652,9 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." ;; ;; Truncate ;; -(defun dashboard-shorten-path (path) - "Shorten the PATH." - (setq path (abbreviate-file-name path)) + +(defun dashboard-shorten-path-by-length (path) + "Shorten the PATH by length." (let* ((len-path (length path)) (len-rep (length dashboard-path-shorten-string)) (len-total (- dashboard-path-max-length len-rep)) (center (/ len-total 2)) @@ -660,6 +666,13 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." front (substring path start-front len-path)) (concat back dashboard-path-shorten-string front)))) +(defun dashboard-shorten-path (path) + "Shorten the PATH." + (setq path (abbreviate-file-name path)) + (cl-case dashboard-path-style + (truncate-length (dashboard-shorten-path-by-length path)) + (t path))) + (defun dashboard-shorten-paths (paths alist) "Shorten all path from PATHS." (let (lst-display abbrev) From 5afe89cd44fd4666b54c3ea5a206cf9f248e097c Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 21 Dec 2020 16:14:33 +0800 Subject: [PATCH 03/15] Fix doc string. --- dashboard-widgets.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dashboard-widgets.el b/dashboard-widgets.el index 02b47fec..bc68ce1e 100644 --- a/dashboard-widgets.el +++ b/dashboard-widgets.el @@ -674,7 +674,7 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." (t path))) (defun dashboard-shorten-paths (paths alist) - "Shorten all path from PATHS." + "Shorten all path from PATHS and store it to ALIST." (let (lst-display abbrev) (setf (symbol-value alist) nil) ; reset (dolist (item paths) From 798fe27676eda4e72e5b1d4da1e310e2c5f05580 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 21 Dec 2020 16:15:18 +0800 Subject: [PATCH 04/15] Remove newline for consistent style. --- dashboard-widgets.el | 1 - 1 file changed, 1 deletion(-) diff --git a/dashboard-widgets.el b/dashboard-widgets.el index bc68ce1e..e1cd81c2 100644 --- a/dashboard-widgets.el +++ b/dashboard-widgets.el @@ -652,7 +652,6 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." ;; ;; Truncate ;; - (defun dashboard-shorten-path-by-length (path) "Shorten the PATH by length." (let* ((len-path (length path)) (len-rep (length dashboard-path-shorten-string)) From 0346d3247fc28b729a60101484a88e374d829542 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 21 Dec 2020 16:24:37 +0800 Subject: [PATCH 05/15] Fixed linter. --- dashboard-widgets.el | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dashboard-widgets.el b/dashboard-widgets.el index e1cd81c2..964c9205 100644 --- a/dashboard-widgets.el +++ b/dashboard-widgets.el @@ -737,8 +737,9 @@ switch to." "Add the list of LIST-SIZE items of projects." (dashboard-insert-section "Projects:" - (dashboard-shorten-paths (dashboard-subseq (dashboard-projects-backend-load-projects) 0 list-size) - 'dashboard-projects-alist) + (dashboard-shorten-paths + (dashboard-subseq (dashboard-projects-backend-load-projects) 0 list-size) + 'dashboard-projects-alist) list-size (dashboard-get-shortcut 'projects) `(lambda (&rest ignore) From cb2c78f71e57c06b4f7be2e94ac4db283d3eea85 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 21 Dec 2020 16:38:33 +0800 Subject: [PATCH 06/15] Revert to use widget-params. --- dashboard-widgets.el | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/dashboard-widgets.el b/dashboard-widgets.el index 964c9205..a39c268b 100644 --- a/dashboard-widgets.el +++ b/dashboard-widgets.el @@ -694,16 +694,19 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." (recentf-mode) (dashboard-insert-section "Recent Files:" - (dashboard-shorten-paths recentf-list 'dashboard-recentf-alist) + recentf-list list-size (dashboard-get-shortcut 'recents) `(lambda (&rest ignore) (find-file-existing (cdr (assoc ,el dashboard-recentf-alist)))) - (abbreviate-file-name el))) + (dashboard-shorten-path el))) ;; ;; Bookmarks ;; +(defvar dashboard-bookmark-alist nil + "Alist records shorten's recent files and it's full paths.") + (defun dashboard-insert-bookmarks (list-size) "Add the list of LIST-SIZE items of bookmarks." (require 'bookmark) @@ -715,7 +718,7 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." `(lambda (&rest ignore) (bookmark-jump ,el)) (let ((file (bookmark-get-filename el))) (if file - (format "%s - %s" el (abbreviate-file-name file)) + (format "%s - %s" el (dashboard-shorten-path file)) el)))) ;; @@ -737,15 +740,13 @@ switch to." "Add the list of LIST-SIZE items of projects." (dashboard-insert-section "Projects:" - (dashboard-shorten-paths - (dashboard-subseq (dashboard-projects-backend-load-projects) 0 list-size) - 'dashboard-projects-alist) + (dashboard-subseq (dashboard-projects-backend-load-projects) 0 list-size) list-size (dashboard-get-shortcut 'projects) `(lambda (&rest ignore) (funcall (dashboard-projects-backend-switch-function) (cdr (assoc ,el dashboard-projects-alist)))) - (abbreviate-file-name el))) + (dashboard-shorten-path el))) (defun dashboard-projects-backend-load-projects () "Depending on `dashboard-projects-backend' load corresponding backend. From af3ec07788ebd069ea5f3ad2e7b067795e3a8d12 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Date: Mon, 21 Dec 2020 16:41:54 +0800 Subject: [PATCH 07/15] Revert action from widget-params. --- dashboard-widgets.el | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dashboard-widgets.el b/dashboard-widgets.el index a39c268b..3402a855 100644 --- a/dashboard-widgets.el +++ b/dashboard-widgets.el @@ -694,12 +694,12 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." (recentf-mode) (dashboard-insert-section "Recent Files:" - recentf-list + (dashboard-shorten-paths recentf-list 'dashboard-recentf-alist) list-size (dashboard-get-shortcut 'recents) `(lambda (&rest ignore) (find-file-existing (cdr (assoc ,el dashboard-recentf-alist)))) - (dashboard-shorten-path el))) + (abbreviate-file-name el))) ;; ;; Bookmarks @@ -740,13 +740,15 @@ switch to." "Add the list of LIST-SIZE items of projects." (dashboard-insert-section "Projects:" - (dashboard-subseq (dashboard-projects-backend-load-projects) 0 list-size) + (dashboard-shorten-paths + (dashboard-subseq (dashboard-projects-backend-load-projects) 0 list-size) + 'dashboard-projects-alist) list-size (dashboard-get-shortcut 'projects) `(lambda (&rest ignore) (funcall (dashboard-projects-backend-switch-function) (cdr (assoc ,el dashboard-projects-alist)))) - (dashboard-shorten-path el))) + (abbreviate-file-name el))) (defun dashboard-projects-backend-load-projects () "Depending on `dashboard-projects-backend' load corresponding backend. From bbf48cd4ca1d4bc2eb05fd77104489fdf2b5ddb0 Mon Sep 17 00:00:00 2001 From: JenChieh Date: Mon, 21 Dec 2020 22:41:04 +0800 Subject: [PATCH 08/15] Add format and fix logic from getting path. --- dashboard-widgets.el | 90 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 13 deletions(-) diff --git a/dashboard-widgets.el b/dashboard-widgets.el index 3402a855..a261ffa8 100644 --- a/dashboard-widgets.el +++ b/dashboard-widgets.el @@ -275,8 +275,11 @@ If nil it is disabled. Possible values for list-type are: (defcustom dashboard-path-style nil "Style to display path." - :type '(choice (const :tag "No specify" nil) - (const :tag "Truncate by length" truncate-length)) + :type '(choice + (const :tag "No specify" nil) + (const :tag "Truncate the beginning part of the path" truncate-beginning) + (const :tag "Truncate the middle part of the path" truncate-middle) + (const :tag "Truncate the end part of the path" truncate-end)) :group 'dashboard) (defcustom dashboard-path-max-length 70 @@ -652,40 +655,83 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." ;; ;; Truncate ;; -(defun dashboard-shorten-path-by-length (path) - "Shorten the PATH by length." +(defun dashboard-shorten-path-beginning (path) + "Shorten PATH from beginning if exceeding maximum length." + (let* ((len-path (length path)) (len-rep (length dashboard-path-shorten-string)) + (len-total (- dashboard-path-max-length len-rep)) + front) + (if (<= len-path dashboard-path-max-length) path + (setq front (substring path (- len-path len-total) len-path)) + (concat dashboard-path-shorten-string front)))) + +(defun dashboard-shorten-path-middle (path) + "Shorten PATH from middle if exceeding maximum length." (let* ((len-path (length path)) (len-rep (length dashboard-path-shorten-string)) (len-total (- dashboard-path-max-length len-rep)) (center (/ len-total 2)) (end-back center) (start-front (- len-path center)) - back front ) + back front) (if (<= len-path dashboard-path-max-length) path (setq back (substring path 0 end-back) front (substring path start-front len-path)) (concat back dashboard-path-shorten-string front)))) +(defun dashboard-shorten-path-end (path) + "Shorten PATH from end if exceeding maximum length." + (let* ((len-path (length path)) (len-rep (length dashboard-path-shorten-string)) + (len-total (- dashboard-path-max-length len-rep)) + back) + (if (<= len-path dashboard-path-max-length) path + (setq back (substring path 0 len-total)) + (concat back dashboard-path-shorten-string)))) + (defun dashboard-shorten-path (path) "Shorten the PATH." (setq path (abbreviate-file-name path)) (cl-case dashboard-path-style - (truncate-length (dashboard-shorten-path-by-length path)) + (truncate-beginning (dashboard-shorten-path-beginning path)) + (truncate-middle (dashboard-shorten-path-middle path)) + (truncate-end (dashboard-shorten-path-end path)) (t path))) (defun dashboard-shorten-paths (paths alist) "Shorten all path from PATHS and store it to ALIST." - (let (lst-display abbrev) + (let (lst-display abbrev (index 0)) (setf (symbol-value alist) nil) ; reset (dolist (item paths) - (setq abbrev (dashboard-shorten-path item)) + (setq abbrev (dashboard-shorten-path item) + ;; Add salt here, and use for extraction. + ;; See function `dashboard-extract-key-path-alist'. + abbrev (format "%s|%s" index abbrev)) ;; store `abbrev' as id; and `item' with value (push (cons abbrev item) (symbol-value alist)) - (push abbrev lst-display)) + (push abbrev lst-display) + (cl-incf index)) (reverse lst-display))) +(defun dashboard-extract-key-path-alist (key alist) + "Remove salt from KEY, and return true shorten path from ALIST." + (let* ((key (car (assoc key alist))) (split (split-string key "|"))) + (nth 1 split))) + +(defun dashboard-expand-path-alist (key alist) + "Get the full path (un-shorten) using KEY from ALIST." + (cdr (assoc key alist))) + ;; ;; Recentf ;; +(defcustom dasbhoard-recentf-show-base nil + "Show the base file name infront of it's path." + :type 'boolean + :group 'dashboard) + +(defcustom dasbhoard-recentf-item-format "[%s] %s" + "Format to use when showing the base of the file name." + :type 'string + :group 'dashboard) + (defvar dashboard-recentf-alist nil "Alist records shorten's recent files and it's full paths.") @@ -698,8 +744,12 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." list-size (dashboard-get-shortcut 'recents) `(lambda (&rest ignore) - (find-file-existing (cdr (assoc ,el dashboard-recentf-alist)))) - (abbreviate-file-name el))) + (find-file-existing (dashboard-expand-path-alist ,el dashboard-recentf-alist))) + (let ((file (dashboard-expand-path-alist el dashboard-recentf-alist)) + (path (dashboard-extract-key-path-alist el dashboard-recentf-alist))) + (if dasbhoard-recentf-show-base + (format dasbhoard-recentf-item-format (f-filename file) path) + path)))) ;; ;; Bookmarks @@ -733,6 +783,16 @@ switch to." :type '(choice (const :tag "Default" nil) function) :group 'dashboard) +(defcustom dasbhoard-projects-show-base nil + "Show the project name infront of it's path." + :type 'boolean + :group 'dashboard) + +(defcustom dasbhoard-projects-item-format "[%s] %s" + "Format to use when showing the base of the project name." + :type 'string + :group 'dashboard) + (defvar dashboard-projects-alist nil "Alist records the shorten's project paths and it's full paths.") @@ -747,8 +807,12 @@ switch to." (dashboard-get-shortcut 'projects) `(lambda (&rest ignore) (funcall (dashboard-projects-backend-switch-function) - (cdr (assoc ,el dashboard-projects-alist)))) - (abbreviate-file-name el))) + (dashboard-expand-path-alist ,el dashboard-projects-alist))) + (let ((file (dashboard-expand-path-alist el dashboard-projects-alist)) + (path (dashboard-extract-key-path-alist el dashboard-projects-alist))) + (if dasbhoard-projects-show-base + (format dasbhoard-projects-item-format ((f-base file)) path) + path)))) (defun dashboard-projects-backend-load-projects () "Depending on `dashboard-projects-backend' load corresponding backend. From 5a6cb3a00ab1413b81079eef7ab73f53462a95b3 Mon Sep 17 00:00:00 2001 From: JenChieh Date: Mon, 21 Dec 2020 22:48:18 +0800 Subject: [PATCH 09/15] Fix lint. --- dashboard-widgets.el | 1 + 1 file changed, 1 insertion(+) diff --git a/dashboard-widgets.el b/dashboard-widgets.el index a261ffa8..e08cb925 100644 --- a/dashboard-widgets.el +++ b/dashboard-widgets.el @@ -23,6 +23,7 @@ ;;; Code: (require 'cl-lib) +(require 'f) ;; Compiler pacifier (declare-function all-the-icons-icon-for-dir "ext:all-the-icons.el") From 1108468f5015c7389b76a93bfe7ee6e9ac5a9e89 Mon Sep 17 00:00:00 2001 From: JenChieh Date: Mon, 21 Dec 2020 22:48:26 +0800 Subject: [PATCH 10/15] Add f as dep. --- dashboard.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dashboard.el b/dashboard.el index 7151199e..626f1db9 100644 --- a/dashboard.el +++ b/dashboard.el @@ -14,7 +14,7 @@ ;; Created: October 05, 2016 ;; Package-Version: 1.8.0-SNAPSHOT ;; Keywords: startup, screen, tools, dashboard -;; Package-Requires: ((emacs "25.3") (page-break-lines "0.11")) +;; Package-Requires: ((emacs "25.3") (page-break-lines "0.11") (f "0.20.0")) ;;; Commentary: ;; An extensible Emacs dashboard, with sections for From 44ac67e69017a8ef21b6380906878777444babd0 Mon Sep 17 00:00:00 2001 From: JenChieh Date: Mon, 21 Dec 2020 22:51:35 +0800 Subject: [PATCH 11/15] Update cask for dependency f. --- Cask | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cask b/Cask index fdbc8fa8..e71d43d4 100644 --- a/Cask +++ b/Cask @@ -5,4 +5,5 @@ (files "*.el" "*.org" "banners") (development - (depends-on "page-break-lines")) \ No newline at end of file + (depends-on "page-break-lines") + (depends-on "f")) From 1c821649395e9bcea9a9aed9d2d49c6ab5b5ccdc Mon Sep 17 00:00:00 2001 From: JenChieh Date: Mon, 21 Dec 2020 22:53:28 +0800 Subject: [PATCH 12/15] Revert dep f. --- Cask | 3 +-- dashboard-widgets.el | 1 - dashboard.el | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Cask b/Cask index e71d43d4..0f45d508 100644 --- a/Cask +++ b/Cask @@ -5,5 +5,4 @@ (files "*.el" "*.org" "banners") (development - (depends-on "page-break-lines") - (depends-on "f")) + (depends-on "page-break-lines")) diff --git a/dashboard-widgets.el b/dashboard-widgets.el index e08cb925..a261ffa8 100644 --- a/dashboard-widgets.el +++ b/dashboard-widgets.el @@ -23,7 +23,6 @@ ;;; Code: (require 'cl-lib) -(require 'f) ;; Compiler pacifier (declare-function all-the-icons-icon-for-dir "ext:all-the-icons.el") diff --git a/dashboard.el b/dashboard.el index 626f1db9..7151199e 100644 --- a/dashboard.el +++ b/dashboard.el @@ -14,7 +14,7 @@ ;; Created: October 05, 2016 ;; Package-Version: 1.8.0-SNAPSHOT ;; Keywords: startup, screen, tools, dashboard -;; Package-Requires: ((emacs "25.3") (page-break-lines "0.11") (f "0.20.0")) +;; Package-Requires: ((emacs "25.3") (page-break-lines "0.11")) ;;; Commentary: ;; An extensible Emacs dashboard, with sections for From e99d8f9fa46fa66c47bd190950689747dbf6899d Mon Sep 17 00:00:00 2001 From: JenChieh Date: Mon, 21 Dec 2020 22:58:37 +0800 Subject: [PATCH 13/15] Use self built-in f functions. --- dashboard-widgets.el | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/dashboard-widgets.el b/dashboard-widgets.el index a261ffa8..f09dfa03 100644 --- a/dashboard-widgets.el +++ b/dashboard-widgets.el @@ -655,6 +655,14 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." ;; ;; Truncate ;; +(defun dashboard-f-filename (path) + "Return file name from PATH." + (file-name-nondirectory path)) + +(defun dashboard-f-base (path) + "Return directory name from PATH." + (file-name-nondirectory (directory-file-name (file-name-directory path)))) + (defun dashboard-shorten-path-beginning (path) "Shorten PATH from beginning if exceeding maximum length." (let* ((len-path (length path)) (len-rep (length dashboard-path-shorten-string)) @@ -748,7 +756,7 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." (let ((file (dashboard-expand-path-alist el dashboard-recentf-alist)) (path (dashboard-extract-key-path-alist el dashboard-recentf-alist))) (if dasbhoard-recentf-show-base - (format dasbhoard-recentf-item-format (f-filename file) path) + (format dasbhoard-recentf-item-format (dashboard-f-filename file) path) path)))) ;; @@ -811,7 +819,7 @@ switch to." (let ((file (dashboard-expand-path-alist el dashboard-projects-alist)) (path (dashboard-extract-key-path-alist el dashboard-projects-alist))) (if dasbhoard-projects-show-base - (format dasbhoard-projects-item-format ((f-base file)) path) + (format dasbhoard-projects-item-format (dashboard-f-base file) path) path)))) (defun dashboard-projects-backend-load-projects () From 9d516a2f119d9aba96153c9e18e3685d5f74fbae Mon Sep 17 00:00:00 2001 From: JenChieh Date: Tue, 22 Dec 2020 01:00:42 +0800 Subject: [PATCH 14/15] Add option for show option. --- dashboard-widgets.el | 81 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 16 deletions(-) diff --git a/dashboard-widgets.el b/dashboard-widgets.el index f09dfa03..9ba82d48 100644 --- a/dashboard-widgets.el +++ b/dashboard-widgets.el @@ -727,15 +727,37 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." "Get the full path (un-shorten) using KEY from ALIST." (cdr (assoc key alist))) +(defun dashboard--generate-align-format (fmt len) + "Return inserted align LEN to FMT." + (let ((pos (1+ (string-match-p "%s" fmt)))) + (concat (substring fmt 0 pos) + (concat "-" (number-to-string len)) + (substring fmt pos (length fmt))))) + +(defun dashboard--get-align-length (alist &optional dir) + "Return maximum align length from ALIST. + +If optional argument DIR is non-nil; align with directory name instead." + (let ((align-length -1) path len-path) + (dolist (item alist) + (setq path (cdr item) + path (if dir (dashboard-f-base path) (dashboard-f-filename path)) + len-path (length path) + align-length (max len-path align-length))) + align-length)) + ;; ;; Recentf ;; -(defcustom dasbhoard-recentf-show-base nil +(defcustom dashboard-recentf-show-base nil "Show the base file name infront of it's path." - :type 'boolean + :type '(choice + (const :tag "Don't show the base infront" nil) + (const :tag "Respect format" t) + (const :tag "Align the from base" align)) :group 'dashboard) -(defcustom dasbhoard-recentf-item-format "[%s] %s" +(defcustom dashboard-recentf-item-format "%s %s" "Format to use when showing the base of the file name." :type 'string :group 'dashboard) @@ -743,8 +765,12 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." (defvar dashboard-recentf-alist nil "Alist records shorten's recent files and it's full paths.") +(defvar dashboard--recentf-cache-item-format nil + "Cache to record the new generated align format.") + (defun dashboard-insert-recents (list-size) "Add the list of LIST-SIZE items from recently edited files." + (setq dashboard--recentf-cache-item-format nil) (recentf-mode) (dashboard-insert-section "Recent Files:" @@ -753,11 +779,19 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." (dashboard-get-shortcut 'recents) `(lambda (&rest ignore) (find-file-existing (dashboard-expand-path-alist ,el dashboard-recentf-alist))) - (let ((file (dashboard-expand-path-alist el dashboard-recentf-alist)) - (path (dashboard-extract-key-path-alist el dashboard-recentf-alist))) - (if dasbhoard-recentf-show-base - (format dasbhoard-recentf-item-format (dashboard-f-filename file) path) - path)))) + (let* ((file (dashboard-expand-path-alist el dashboard-recentf-alist)) + (filename (dashboard-f-filename file)) + (path (dashboard-extract-key-path-alist el dashboard-recentf-alist))) + (cl-case dashboard-recentf-show-base + (align + (unless dashboard--recentf-cache-item-format + (let* ((len-align (dashboard--get-align-length dashboard-recentf-alist)) + (new-fmt (dashboard--generate-align-format + dashboard-recentf-item-format len-align))) + (setq dashboard--recentf-cache-item-format new-fmt))) + (format dashboard--recentf-cache-item-format filename path)) + (nil (format dashboard-recentf-item-format filename path)) + (t path))))) ;; ;; Bookmarks @@ -791,12 +825,15 @@ switch to." :type '(choice (const :tag "Default" nil) function) :group 'dashboard) -(defcustom dasbhoard-projects-show-base nil +(defcustom dashboard-projects-show-base nil "Show the project name infront of it's path." - :type 'boolean + :type '(choice + (const :tag "Don't show the base infront" nil) + (const :tag "Respect format" t) + (const :tag "Align the from base" align)) :group 'dashboard) -(defcustom dasbhoard-projects-item-format "[%s] %s" +(defcustom dashboard-projects-item-format "%s %s" "Format to use when showing the base of the project name." :type 'string :group 'dashboard) @@ -804,8 +841,12 @@ switch to." (defvar dashboard-projects-alist nil "Alist records the shorten's project paths and it's full paths.") +(defvar dashboard--projects-cache-item-format nil + "Cache to record the new generated align format.") + (defun dashboard-insert-projects (list-size) "Add the list of LIST-SIZE items of projects." + (setq dashboard--projects-cache-item-format nil) (dashboard-insert-section "Projects:" (dashboard-shorten-paths @@ -816,11 +857,19 @@ switch to." `(lambda (&rest ignore) (funcall (dashboard-projects-backend-switch-function) (dashboard-expand-path-alist ,el dashboard-projects-alist))) - (let ((file (dashboard-expand-path-alist el dashboard-projects-alist)) - (path (dashboard-extract-key-path-alist el dashboard-projects-alist))) - (if dasbhoard-projects-show-base - (format dasbhoard-projects-item-format (dashboard-f-base file) path) - path)))) + (let* ((file (dashboard-expand-path-alist el dashboard-projects-alist)) + (filename (dashboard-f-base file)) + (path (dashboard-extract-key-path-alist el dashboard-projects-alist))) + (cl-case dashboard-projects-show-base + (align + (unless dashboard--projects-cache-item-format + (let* ((len-align (dashboard--get-align-length dashboard-projects-alist t)) + (new-fmt (dashboard--generate-align-format + dashboard-projects-item-format len-align))) + (setq dashboard--projects-cache-item-format new-fmt))) + (format dashboard--projects-cache-item-format filename path)) + (nil (format dashboard-projects-item-format filename path)) + (t path))))) (defun dashboard-projects-backend-load-projects () "Depending on `dashboard-projects-backend' load corresponding backend. From 837201cb5606aea0fa11360fa4de61843de36f7a Mon Sep 17 00:00:00 2001 From: JenChieh Date: Tue, 22 Dec 2020 01:03:27 +0800 Subject: [PATCH 15/15] Fixed lint. --- dashboard-widgets.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dashboard-widgets.el b/dashboard-widgets.el index 9ba82d48..90fab8fc 100644 --- a/dashboard-widgets.el +++ b/dashboard-widgets.el @@ -728,7 +728,7 @@ WIDGET-PARAMS are passed to the \"widget-create\" function." (cdr (assoc key alist))) (defun dashboard--generate-align-format (fmt len) - "Return inserted align LEN to FMT." + "Return FMT after inserting align LEN." (let ((pos (1+ (string-match-p "%s" fmt)))) (concat (substring fmt 0 pos) (concat "-" (number-to-string len))