[home] Add multi-digit numbers to the startup lists

Added support for jumping to two digit numbers.

How quickly two numbers have to be typed,
can be modified with the variable:
spacemacs-buffer-multi-digit-delay
This commit is contained in:
duianto 2021-03-20 18:24:19 +01:00 committed by Maximilian Wolff
parent 13a5d5c95f
commit 7a2eb1be70
1 changed files with 165 additions and 90 deletions

View File

@ -61,6 +61,17 @@ Internal use, do not set this variable.")
(defvar spacemacs-buffer-mode-map (defvar spacemacs-buffer-mode-map
(let ((map (make-sparse-keymap))) (let ((map (make-sparse-keymap)))
(define-key map (kbd "0") 'spacemacs-buffer/jump-to-number-startup-list-line)
(define-key map (kbd "1") 'spacemacs-buffer/jump-to-number-startup-list-line)
(define-key map (kbd "2") 'spacemacs-buffer/jump-to-number-startup-list-line)
(define-key map (kbd "3") 'spacemacs-buffer/jump-to-number-startup-list-line)
(define-key map (kbd "4") 'spacemacs-buffer/jump-to-number-startup-list-line)
(define-key map (kbd "5") 'spacemacs-buffer/jump-to-number-startup-list-line)
(define-key map (kbd "6") 'spacemacs-buffer/jump-to-number-startup-list-line)
(define-key map (kbd "7") 'spacemacs-buffer/jump-to-number-startup-list-line)
(define-key map (kbd "8") 'spacemacs-buffer/jump-to-number-startup-list-line)
(define-key map (kbd "9") 'spacemacs-buffer/jump-to-number-startup-list-line)
(define-key map [down-mouse-1] 'widget-button-click) (define-key map [down-mouse-1] 'widget-button-click)
(define-key map (kbd "RET") 'widget-button-press) (define-key map (kbd "RET") 'widget-button-press)
@ -756,28 +767,27 @@ The numbers indicate that the file can be opened,
by pressing its number key." by pressing its number key."
(when (car list) (when (car list)
(insert list-display-name) (insert list-display-name)
(let ((list-nr 1)) (mapc (lambda (el)
(mapc (lambda (el) (insert "\n ")
(insert "\n ") (let* ((button-text-prefix
(let ((button-text (format "%2s" (number-to-string
(concat spacemacs-buffer--startup-list-nr)))
(when (and (string= list-display-name "Recent Files:") (button-text
(< list-nr 10)) (concat button-text-prefix " " (abbreviate-file-name el))))
(concat (number-to-string list-nr) " ")) (widget-create 'push-button
(abbreviate-file-name el)))) :action `(lambda (&rest ignore)
(widget-create 'push-button (find-file-existing ,el))
:action `(lambda (&rest ignore) :mouse-face 'highlight
(find-file-existing ,el)) :follow-link "\C-m"
:mouse-face 'highlight :button-prefix ""
:follow-link "\C-m" :button-suffix ""
:button-prefix "" :format "%[%t%]" button-text))
:button-suffix "" (setq spacemacs-buffer--startup-list-nr
:format "%[%t%]" (1+ spacemacs-buffer--startup-list-nr)))
button-text)) list)))
(setq list-nr (1+ list-nr)))
list))))
(defun spacemacs-buffer//insert-files-by-dir-list (list-display-name grouped-list) (defun spacemacs-buffer//insert-files-by-dir-list
(list-display-name grouped-list)
"Insert an interactive grouped list of files in the home buffer. "Insert an interactive grouped list of files in the home buffer.
LIST-DISPLAY-NAME: the displayed title of the list. LIST-DISPLAY-NAME: the displayed title of the list.
GROUPED-LIST: a list of string pathnames made interactive in this function." GROUPED-LIST: a list of string pathnames made interactive in this function."
@ -785,27 +795,43 @@ GROUPED-LIST: a list of string pathnames made interactive in this function."
(insert list-display-name) (insert list-display-name)
(mapc (lambda (group) (mapc (lambda (group)
(insert "\n ") (insert "\n ")
(widget-create 'push-button (let* ((button-text-prefix
:action `(lambda (&rest ignore) (format "%2s" (number-to-string
(find-file-existing ,(car group))) spacemacs-buffer--startup-list-nr)))
:mouse-face 'highlight (button-text-project
:follow-link "\C-m" (concat button-text-prefix " "
:button-prefix "" (abbreviate-file-name (car group)))))
:button-suffix "" (widget-create 'push-button
:format "%[%t%]" :action `(lambda (&rest ignore)
(abbreviate-file-name (car group))) (find-file-existing ,(car group)))
(mapc (lambda (el) :mouse-face 'highlight
(insert "\n ") :follow-link "\C-m"
(widget-create 'push-button :button-prefix ""
:action `(lambda (&rest ignore) :button-suffix ""
(find-file-existing (concat ,(car group) ,el))) :format "%[%t%]" button-text-project)
:mouse-face 'highlight (setq spacemacs-buffer--startup-list-nr
:follow-link "\C-m" (1+ spacemacs-buffer--startup-list-nr))
:button-prefix "" (mapc (lambda (el)
:button-suffix "" (let* ((button-text-prefix
:format "%[%t%]" (format "%2s" (number-to-string
(abbreviate-file-name el))) spacemacs-buffer--startup-list-nr)))
(cdr group))) grouped-list))) (button-text-filename
(concat button-text-prefix " "
(abbreviate-file-name el))))
(insert "\n ")
(widget-create 'push-button
:action `(lambda (&rest ignore)
(find-file-existing
(concat ,(car group) ,el)))
:mouse-face 'highlight
:follow-link "\C-m"
:button-prefix ""
:button-suffix ""
:format "%[%t%]" button-text-filename))
(setq spacemacs-buffer--startup-list-nr
(1+ spacemacs-buffer--startup-list-nr)))
(cdr group))))
grouped-list)))
(defun spacemacs-buffer//insert-bookmark-list (list-display-name list) (defun spacemacs-buffer//insert-bookmark-list (list-display-name list)
"Insert an interactive list of bookmarks entries (if any) in the home buffer. "Insert an interactive list of bookmarks entries (if any) in the home buffer.
@ -815,18 +841,25 @@ LIST: a list of string bookmark names made interactive in this function."
(insert list-display-name) (insert list-display-name)
(mapc (lambda (el) (mapc (lambda (el)
(insert "\n ") (insert "\n ")
(let ((filename (bookmark-get-filename el))) (let* ((filename (bookmark-get-filename el))
(button-text-prefix
(format "%2s" (number-to-string
spacemacs-buffer--startup-list-nr)))
(button-text
(concat button-text-prefix " "
(if filename
(format "%s - %s"
el (abbreviate-file-name filename))
(format "%s" el)))))
(widget-create 'push-button (widget-create 'push-button
:action `(lambda (&rest ignore) (bookmark-jump ,el)) :action `(lambda (&rest ignore) (bookmark-jump ,el))
:mouse-face 'highlight :mouse-face 'highlight
:follow-link "\C-m" :follow-link "\C-m"
:button-prefix "" :button-prefix ""
:button-suffix "" :button-suffix ""
:format "%[%t%]" :format "%[%t%]" button-text))
(if filename (setq spacemacs-buffer--startup-list-nr
(format "%s - %s" (1+ spacemacs-buffer--startup-list-nr)))
el (abbreviate-file-name filename))
(format "%s" el)))))
list))) list)))
(defun spacemacs-buffer//get-org-items (types) (defun spacemacs-buffer//get-org-items (types)
@ -912,24 +945,31 @@ LIST: list of `org-agenda' entries in the todo list."
(cdr (assoc "time" b)))))))) (cdr (assoc "time" b))))))))
(mapc (lambda (el) (mapc (lambda (el)
(insert "\n ") (insert "\n ")
(widget-create 'push-button (let* ((button-text-prefix
:action `(lambda (&rest ignore) (format "%2s" (number-to-string
(spacemacs-buffer//org-jump ',el)) spacemacs-buffer--startup-list-nr)))
:mouse-face 'highlight (button-text
:follow-link "\C-m" (concat button-text-prefix " "
:button-prefix "" (format "%s %s %s"
:button-suffix "" (let ((filename (cdr (assoc "file" el))))
:format "%[%t%]" (if dotspacemacs-home-shorten-agenda-source
(format "%s %s %s" (file-name-nondirectory filename)
(let ((filename (cdr (assoc "file" el)))) (abbreviate-file-name filename)))
(if dotspacemacs-home-shorten-agenda-source (if (not (eq "" (cdr (assoc "time" el))))
(file-name-nondirectory filename) (format "- %s -"
(abbreviate-file-name filename))) (cdr (assoc "time" el)))
(if (not (eq "" (cdr (assoc "time" el)))) "-")
(format "- %s -" (cdr (assoc "text" el))))))
(cdr (assoc "time" el))) (widget-create 'push-button
"-") :action `(lambda (&rest ignore)
(cdr (assoc "text" el))))) (spacemacs-buffer//org-jump ',el))
:mouse-face 'highlight
:follow-link "\C-m"
:button-prefix ""
:button-suffix ""
:format "%[%t%]" button-text))
(setq spacemacs-buffer--startup-list-nr
(1+ spacemacs-buffer--startup-list-nr)))
list))) list)))
(defun spacemacs-buffer//associate-to-project (recent-file by-project) (defun spacemacs-buffer//associate-to-project (recent-file by-project)
@ -964,27 +1004,6 @@ SEQ, START and END are the same arguments as for `cl-subseq'"
(spacemacs-buffer||add-shortcut "w" "Warnings:") (spacemacs-buffer||add-shortcut "w" "Warnings:")
(insert spacemacs-buffer-list-separator))) (insert spacemacs-buffer-list-separator)))
(defvar spacemacs-buffer//recent-files-list nil
"Remember the recent files when the Spacemacs buffer was drawn.")
(defun spacemacs-buffer//recent-files-define-funcs-bind-keys (list-size)
"Define LIST-SIZE named functions:
spacemacs-buffer/open-recent-file-1, -2, etc.
and bind LIST-SIZE keys: 1, 2, etc.
to the previously defined functions.
Limit the functions and key bindings to 9."
(dotimes (i list-size)
(when (< (1+ i) 10)
(eval `(defun ,(intern (format "spacemacs-buffer/open-recent-file-%s"
(1+ i))) ()
(interactive)
(find-file (nth ,i spacemacs-buffer//recent-files-list))))
(define-key spacemacs-buffer-mode-map
(number-to-string (1+ i))
`,(intern (format "spacemacs-buffer/open-recent-file-%s" (1+ i)))))))
(defun spacemacs-buffer//insert-recent-files (list-size) (defun spacemacs-buffer//insert-recent-files (list-size)
(unless recentf-mode (recentf-mode)) (unless recentf-mode (recentf-mode))
(setq spacemacs-buffer//recent-files-list (setq spacemacs-buffer//recent-files-list
@ -992,9 +1011,7 @@ Limit the functions and key bindings to 9."
(when (spacemacs-buffer//insert-file-list (when (spacemacs-buffer//insert-file-list
"Recent Files:" "Recent Files:"
spacemacs-buffer//recent-files-list) spacemacs-buffer//recent-files-list)
(spacemacs-buffer||add-shortcut "r" "Recent Files:") (spacemacs-buffer||add-shortcut "r" "Recent Files:"))
(spacemacs-buffer//recent-files-define-funcs-bind-keys
(length spacemacs-buffer//recent-files-list)))
(insert spacemacs-buffer-list-separator)) (insert spacemacs-buffer-list-separator))
(defun spacemacs-buffer//insert-recent-files-by-project (list-size) (defun spacemacs-buffer//insert-recent-files-by-project (list-size)
@ -1049,8 +1066,11 @@ Limit the functions and key bindings to 9."
(spacemacs-buffer||add-shortcut "p" "Projects:") (spacemacs-buffer||add-shortcut "p" "Projects:")
(insert spacemacs-buffer-list-separator))) (insert spacemacs-buffer-list-separator)))
(defvar spacemacs-buffer--startup-list-nr 1)
(defun spacemacs-buffer//do-insert-startupify-lists () (defun spacemacs-buffer//do-insert-startupify-lists ()
"Insert the startup lists in the current buffer." "Insert the startup lists in the current buffer."
(setq spacemacs-buffer--startup-list-nr 1)
(dolist (els (append '(warnings) dotspacemacs-startup-lists)) (dolist (els (append '(warnings) dotspacemacs-startup-lists))
(let ((el (or (car-safe els) els)) (let ((el (or (car-safe els) els))
(list-size (or (cdr-safe els) (list-size (or (cdr-safe els)
@ -1120,6 +1140,61 @@ Limit the functions and key bindings to 9."
(search-forward "[") (search-forward "[")
(left-char 2)))) (left-char 2))))
(defvar spacemacs-buffer-multi-digit-delay 0.4
"This determines how quickly (in seconds)
the numbers have to be typed.")
(defvar spacemacs-buffer--idle-numbers-timer nil
"This stores the idle numbers timer.")
(defvar spacemacs-buffer--startup-list-number nil
"This accumulates the numbers that are typed in the home buffer.
It's cleared when the idle timer runs.")
(defun spacemacs-buffer/jump-to-number-startup-list-line ()
"Jump to the startup list line with the typed number.
The delay between the number key presses,
can be adjusted (in seconds) with the variable:
`spacemacs-buffer-multi-digit-delay'."
(interactive)
(when spacemacs-buffer--idle-numbers-timer
(cancel-timer spacemacs-buffer--idle-numbers-timer))
(let* ((key-pressed-string (string last-input-event)))
(setq spacemacs-buffer--startup-list-number
(concat spacemacs-buffer--startup-list-number key-pressed-string))
(let (message-log-max) ; only show in minibuffer
(message "Jump to startup list: %s" spacemacs-buffer--startup-list-number))
(setq spacemacs-buffer--idle-numbers-timer
(run-with-idle-timer
spacemacs-buffer-multi-digit-delay nil
'spacemacs-buffer/stop-waiting-for-additional-numbers))))
(defun spacemacs-buffer//re-line-starts-with-nr-space (nr-string)
(concat "^\s?*" nr-string " "))
(defun spacemacs-buffer/jump-to-line-starting-with-nr-space (nr-string)
"Jump to the line number that starts with NR."
(let ((prev-point (point)))
(goto-char (point-min))
(if (not (re-search-forward
(spacemacs-buffer//re-line-starts-with-nr-space nr-string)
nil 'noerror))
(progn (goto-char prev-point)
(let (message-log-max) ; only show in minibuffer
(message "Couldn't find startup list number: %s"
spacemacs-buffer--startup-list-number)))
(back-to-indentation)
(message "Opening file/dir: %s"
(replace-regexp-in-string "^\s*" ""
(widget-value (widget-at (point)))))
(widget-button-press (point)))))
(defun spacemacs-buffer/stop-waiting-for-additional-numbers ()
(spacemacs-buffer/jump-to-line-starting-with-nr-space
spacemacs-buffer--startup-list-number)
(setq spacemacs-buffer--startup-list-number nil))
(defun spacemacs-buffer//startup-hook () (defun spacemacs-buffer//startup-hook ()
"Code executed when Emacs has finished loading." "Code executed when Emacs has finished loading."
(with-current-buffer (get-buffer spacemacs-buffer-name) (with-current-buffer (get-buffer spacemacs-buffer-name)