用 command-switch-alist 添加 Emacs 命令行选项

比如给 Emacs 添加 --search,打开文件 FILE 并搜索 TEXT,类似 less -p PATTERN

$ emacs FILE --search TEXT

在配置中写入:

(add-to-list 'command-switch-alist '("--search"  . command-line-search))

(defun command-line-search (_switch)
  (my-search-for (pop command-line-args-left)))

(defun my-search-for (pattern)
  "Search for pattern with Isearch."
  (interactive "sSearch for: ")
  (isearch-forward nil t)
  (isearch-yank-string pattern))
3 个赞