24.2.7 表格列表模式

表格列表模式是一种用于展示表格化数据的主模式,即由 条目(entries) 构成的数据,每个条目占据一行文本,内容按列划分。表格列表模式提供了行列美观排版、按各列数值对行排序的功能。它派生自特殊模式(see 基础主模式)。

表格列表模式面向使用等宽字体、单一字体与字号展示文本的场景。若需要使用变宽字体或图片展示表格,可改用 make-vtable。虚拟表格(vtable)还支持在一个缓冲区中放置多个表格,或缓冲区同时包含表格与额外文本。更多信息,See (vtable)Introduction

表格列表模式适合作为更专用主模式的父模式。例如进程菜单模式(see Process Information)与包菜单模式(see Package Menu in The GNU Emacs Manual)。

这类派生模式应照常使用 define-derived-mode,并将 tabulated-list-mode 指定为第二个参数(see 定义派生模式)。define-derived-mode 表达式的主体部分需为下方文档所述变量赋值,以指定表格数据格式;可选地,随后可调用 tabulated-list-init-header 函数,用列名生成表头。

派生模式还应定义一个列表命令(listing command)。用户实际调用的是该命令(如 M-x list-processes),而非模式命令本身。列表命令需要创建或切换至缓冲区、启用派生模式、指定表格数据,最终调用 tabulated-list-print 填充缓冲区内容。

User Option: tabulated-list-gui-sort-indicator-asc

该变量指定在图形框架中用于标记列按升序排序的字符。

在表格列表缓冲区中切换排序方向时,该标记会在升序(“asc”)与降序(“desc”)之间切换。

User Option: tabulated-list-gui-sort-indicator-desc

tabulated-list-gui-sort-indicator-asc 类似,用于标记列按降序排序。

User Option: tabulated-list-tty-sort-indicator-asc

tabulated-list-gui-sort-indicator-asc 类似,用于文本模式框架。

User Option: tabulated-list-tty-sort-indicator-desc

tabulated-list-tty-sort-indicator-asc 类似,用于标记列按降序排序。

Variable: tabulated-list-format

该缓冲区局部变量指定表格列表数据的格式,取值为一个向量。向量中每个元素代表一列数据,格式为列表 (name width sort . props),其中:

  • name 为列名(字符串)。
  • width 为该列预留宽度(整数)。对最后一列无效,其会自动延伸至行尾。
  • sort 指定该列的排序方式。为 nil 时该列不可排序;为 t 时按字符串比较排序;否则应为适用于 sort 的谓词函数(see 重排列表的函数),接收两个与 tabulated-list-entries 元素格式相同的参数(见下文)。
  • props 为额外列属性的属性列表(see 属性列表)。若 :right-align 属性非 nil,该列右对齐;:pad-right 属性指定列右侧额外填充空格数(省略时默认为 1)。
Variable: tabulated-list-entries

该缓冲区局部变量指定表格列表缓冲区中展示的条目,取值为列表或函数。

若为列表,每个元素对应一个条目,格式为 (id contents),其中:

  • idnil 或用于标识条目的 Lisp 对象。若为后者,重新排序时点会保持在同一条目上,比较使用 equal
  • contents 为与 tabulated-list-format 长度相同的向量。 每个向量元素可以是直接插入缓冲区的字符串、用于插入图片的图片描述符(see Image Descriptors), 或列表 (label . properties)—后者会通过 insert-text-buttonlabelproperties 为参数插入文本按钮(see Making Buttons)。

    所有字符串中不应包含换行符。

若为函数,则该函数无参调用时需返回上述格式的列表。

Variable: tabulated-list-groups

该缓冲区局部变量指定表格列表缓冲区中展示的条目分组,取值为列表或函数。

若为列表,每个元素对应一个分组,格式为 (group-name entries),其中 group-name 为分组前显示的字符串,entries 格式与 tabulated-list-entries 相同(见上文)。

若为函数,则该函数无参调用时需返回上述格式的列表。

可使用 seq-group-bytabulated-list-entries 生成 tabulated-list-groups,示例:

(setq tabulated-list-groups
      (seq-group-by 'Buffer-menu-group-by-mode
                    tabulated-list-entries))

其中 Buffer-menu-group-by-mode 可如下定义:

(defun Buffer-menu-group-by-mode (entry)
  (concat "* " (aref (cadr entry) 5)))
Variable: tabulated-list-revert-hook

该通用钩子在表格列表缓冲区恢复前运行。派生模式可向该钩子添加函数以重新计算 tabulated-list-entries

Variable: tabulated-list-printer

该变量的值为在当前位置插入条目(含结尾换行符)的函数。函数接收两个参数 idcontents,含义与 tabulated-list-entries 中一致。默认值为以常规方式插入条目的函数;更复杂使用表格列表模式的模式可指定其他函数。

Variable: tabulated-list-sort-key

该变量指定表格列表缓冲区当前的排序键。为 nil 时不排序。否则格式为 (name . flip),其中 name 为与 tabulated-list-format 中列名匹配的字符串,flipnil 时表示反转排序顺序。

Function: tabulated-list-init-header

该函数计算并设置表格列表缓冲区的 header-line-format(see 窗口标题栏),并为表头行绑定按键映射,支持点击列标题排序条目。

从表格列表模式派生的模式应在设置上述变量后调用该函数(尤其需先设置 tabulated-list-format)。

Function: tabulated-list-print &optional remember-pos update

该函数用条目填充当前缓冲区,应由列表命令调用。它清空缓冲区,按 tabulated-list-sort-keytabulated-list-entries 指定的条目排序,随后调用 tabulated-list-printer 指定的函数插入每条目。

若可选参数 remember-posnil,函数会查找当前行的 id(若有),并在所有条目重新插入后尝试定位到该条目。

若可选参数 updatenil,函数仅删除或添加自上次打印后发生变化的条目。在大多数条目未改变时,速度可快数倍。唯一结果差异是:通过 tabulated-list-put-tag 添加的标记不会从未变化的条目上移除(常规情况下所有标记都会被清除)。

Function: tabulated-list-delete-entry

该函数删除当前位置的条目。

返回列表 (id cols),其中 id 为被删除条目标识,cols 为其列描述向量。点会移至当前行开头。当前位置无条目时返回 nil

注意:该函数仅修改缓冲区内容,不改变 tabulated-list-entries

Function: tabulated-list-get-id &optional pos

defsubsttabulated-list-entries(列表时)或其函数返回的列表中获取标识对象。pos 省略或为 nil 时默认为点。

Function: tabulated-list-get-entry &optional pos

defsubsttabulated-list-entries(列表时)或其函数返回的列表中获取位置 pos 处标识对应的条目向量。该位置无条目时返回 nil

Function: tabulated-list-header-overlay-p &optional POS

defsubstpos 处存在伪表头时返回非 nil。当 tabulated-list-use-header-linenil 时,会使用伪表头在缓冲区开头显示列名。pos 省略或为 nil 时默认为 point-min

Function: tabulated-list-put-tag tag &optional advance

该函数在当前行的填充区域放置标记 tag。填充区域为行首空白,宽度由 tabulated-list-padding 控制。tag 为字符串,长度不超过 tabulated-list-padding。若 advancenil,点向下移动一行。

Function: tabulated-list-clear-all-tags

该函数清除当前缓冲区中所有填充区域的标记。

Function: tabulated-list-set-col col desc &optional change-entry-data

该函数修改当前位置的表格列表条目,将第 col 列设为 desccol 为列号或列名,desc 为新列描述,通过 tabulated-list-print-col 插入。

change-entry-datanil,函数会修改底层数据(通常为 tabulated-list-entries 列表中的列描述),将对应向量位置设为 desc


emacs

Emacs

org-mode

Orgmode

Donations

打赏

Copyright

© Jasper Hsu

Creative Commons

Creative Commons

Attribute

Attribute

Noncommercial

Noncommercial

Share Alike

Share Alike