我正在使用Elisp编写自己的模式。它基本上是一个简单的crud应用程序,显示可以通过minibuffer操作的数据行。我想为这些行创建一个视图,看起来像emacs包管理器:数据列整齐地对齐。实现这样的视图的最佳方式是什么?
phils 的回答让我找到了方向。虽然没有任何教程或简单的例子,但我创建了一个。这是一个带有静态数据并可以打印当前列 ID 的 tabulated-list-mode 派生示例:
(define-derived-mode mymode tabulated-list-mode "mymode" "Major mode My Mode, just a test"
(setq tabulated-list-format [("Col1" 18 t)
("Col2" 12 nil)
("Col3" 10 t)
("Col4" 0 nil)])
(setq tabulated-list-padding 2)
(setq tabulated-list-sort-key (cons "Col3" nil))
(tabulated-list-init-header))
(defun print-current-line-id ()
(interactive)
(message (concat "current line ID is: " (tabulated-list-get-id))))
(defun my-listing-command ()
(interactive)
(pop-to-buffer "*MY MODE*" nil)
(mymode)
(setq tabulated-list-entries (list
(list "1" ["1" "2" "3" "4"])
(list "2" ["a" "b" "c" "d"])))
(tabulated-list-print t))
如果您查看您提到的包列表功能的代码,您会发现它使用了package-menu-mode
,该模式派生自tabulated-list-mode
。
find-function
RET package-menu-mode
RETtabulated-list-mode
RET我经常使用org-mode来完成这种任务。
因为你已经有了漂亮的表格,所以这应该是你开发的起点。
tblui-define
,可以快速创建一个派生自tabulated-list-mode
的主模式。(tblui-define ocodo/gh-run-list-tblui
"GitHub Workflow Runlist"
"Display workflow runs in a tabulated list."
ocodo/gh-run-list-entries-provider
[("startedAt" 15 nil)
("url" 1 nil)
("status" 10 nil)
("event" 10 nil)
("workflowName" 15 nil)]
((:key "w" ;; tiny popup
:name browse-row-url
:funcs ((?W "Browse URL for current run" ocodo/gh-run-list-browse-row-url)))))
(ocodo/gh-run-list-entries-provider)
tabulated-list
(下面会详细介绍)。(defun ocodo/gh-run-list-browse-row-url ()
"Open the url for the current row."
(interactive)
(shell-command-to-string
(format "open \"%s\""
(elt (tabulated-list-get-entry) 1))))
;;(id [vector of col values])
(list 0 ["10m ago" "https://github.com/org/repo/actions/runs/60324234205" "completed" "push" "MyFlow"])
(list
(list 0 [... column values ...])
(list 1 [... column values ...])
(list 2 [... column values ...])
(list 3 [... column values ...])
;; ... etc ...
)
tblui
创建的表格列表视图的所有可用功能。