Emacs 中文档的本地缩写

问题描述 投票:0回答:2

我想在 Emacs 中定义文档本地的缩写,即:

  • 它不会被写入缩写文件
  • 它会否决缩写文件中具有相同键的任何缩写,但仅限于当前文档。

到目前为止我所拥有的是:

%%% eval: (define-abbrev-table 'latex-mode-abbrev-table '(("tc" "triangular clique" nil 0)))

但这并不能满足我的要求...,

emacs abbreviation
2个回答
0
投票

这个怎么样:

(defun set-local-abbrevs (abbrevs)
    "Add ABBREVS to `local-abbrev-table' and make it buffer local.
     ABBREVS should be a list of abbrevs as passed to `define-abbrev-table'.
     The `local-abbrev-table' will be replaced by a copy with the new 
     abbrevs added, so that it is not the same as the abbrev table used
     in other buffers with the same `major-mode'."
    (let* ((bufname (buffer-name))
           (prefix (substring (md5 bufname) 0 (length bufname)))
           (tblsym (intern (concat prefix "-abbrev-table"))))
      (set tblsym (copy-abbrev-table local-abbrev-table))
      (dolist (abbrev abbrevs)
          (define-abbrev (eval tblsym)
            (car abbrev)
            (cadr abbrev)
            (caddr abbrev)))
    (setq-local local-abbrev-table (eval tblsym))))

然后:

(set-local-abbrevs '(("tc" "triangular clique" nil)))

0
投票

接受的答案有效。但是,通过

list-abbrevs
无法看到缩写。

(defun my-define-local-abbrev-table (definitions)
  "Define abbrev table with DEFINITIONS local to the current buffer.

Abbrev DEFINITIONS is a list of elements of the form (ABBREVNAME
EXPANSION ...) that are passed to `define-abbrev'.

The local abbrev table has name of the current buffer appended
 with \"-abbrev-table\".

Use `my-clear-local-abbrev-table' to remove local abbrev
definitions."
  (let* ((table-symbol (intern-soft (concat (buffer-name) "-abbrev-table"))))
    (define-abbrev-table table-symbol definitions)
    (setq-local local-abbrev-table (symbol-value table-symbol))
    (message "Created local abbrev table '%s'" table-symbol)))

(defun my-clear-local-abbrev-table ()
  "Clear buffer-local abbrevs.

See `my-define-local-abbrev-table'."
  (let* ((buffer-table-string (concat (buffer-name) "-abbrev-table"))
         (buffer-table-symbol (intern-soft buffer-table-string))
         (buffer-table (symbol-value buffer-table-symbol)))
    (cond ((abbrev-table-p buffer-table)
           (clear-abbrev-table buffer-table)
           (message "Cleared local abbrev table '%s'" buffer-table-string)))))

(my-define-local-abbrev-table
   '(("TC" "triangular clique" nil :case-fixed t)
    ("banana" "rama")
    ))

;; Type TC<SPC> in the desired buffer and see that it expands into
;; 'triangular clique'
;; Type TC<SPC> in a different buffer and see that it doesn't expand
;; Notice that tc doesn't expand in the desired buffer
;; Notice that both banana and BaNaNA expand desired buffer
;; Notice that abbrevs are visible through M-x list-abbrevs

(my-clear-local-abbrev-table)
© www.soinside.com 2019 - 2024. All rights reserved.