如何使用 minibuffer-choose.el 在 Emacs-buffer 中插入 *.tex-templates?

如何使用 minibuffer-choose.el 在 Emacs-buffer 中插入 *.tex-templates?

几周前我找到了这个答案https://tex.stackexchange.com/questions/2110/most-significant-reasons-that-led-%20us-to-latex/2144#2144在 tex.stackexchange.com 上。

“Leo2007” 描述了如何将 *.tex 模板插入 Emacs 的缓冲区。我找不到如何让它工作的方法,于是决定在 autex-mailinglist 上询问,但三周没有得到答复。所以也许你可以帮助我:

“Leo2007” 提供了文件minibuffer-choose.el,它允许快速将模板插入文档中。点击上面的 1 号链接,您将看到一张相当令人信服的图片。

不幸的是我不知道如何使用它。我 minibuffer-choose.el从这里获取了该文件:http://paste.lisp.org/display/113728 (我在下面添加了它)。

我进行了字节编译,将其放在 emacs 可以找到的地方,并在我的 .emacs 文件中添加了 3 行:

(require 'minibuffer-choose)
(let ((files (directory-files "~/Emacs/Vorlagen" t "[a-z]+")))
  (minibuffer-choose "Choose: " files 'file-name-nondirectory nil 'alpha))

But how do I use it?

Alexander

%%%%%%%%%%%%%%%%%%%%%%

;;; minibuffer-choose.el --- Choose from Minibuffer

;; Copyright (C) 2010  Leo

;; Author: Leo
;; Keywords: tools, extensions

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This is a programming facility allowing presenting a fixed set of
;; choices in the minibuffer with shortcut keys to select each.

;;; Code:
(eval-when-compile (require 'cl))

(defface minibuffer-choose-highlight
  '((((class color) (background light))
     :background "bisque2")
    (((class color) (background dark))
     :background "bisque4"))
  "Face for highlighting selection."
  :group 'minibuffer)

(defface minibuffer-choose-digit
  '((((class color))
     (:foreground "ForestGreen"))
    (t (:italic t)))
  "Face for highlighting numeric keys."
  :group 'minibuffer)

(defface minibuffer-choose-alpha
  '((((class color))
     (:foreground "OrangeRed4"))
    (t (:weight bold)))
  "Face for highlighting alpha keys."
  :group 'minibuffer)

(defvar minibuffer-choose-map (make-sparse-keymap)
  "Key map used in `minibuffer-choose'.")
(set-keymap-parent minibuffer-choose-map minibuffer-local-map)

(define-key minibuffer-choose-map [remap self-insert-command]
  'minibuffer-choose-match)
(define-key minibuffer-choose-map [left]          'minibuffer-choose-prev)
(define-key minibuffer-choose-map [right]         'minibuffer-choose-next)
(define-key minibuffer-choose-map [return]        'minibuffer-choose-current)
(define-key minibuffer-choose-map "\C-s"          'minibuffer-choose-next)
(define-key minibuffer-choose-map "\C-n"          'minibuffer-choose-next)
(define-key minibuffer-choose-map "\M-n"          'minibuffer-choose-next)
(define-key minibuffer-choose-map (kbd "<tab>")   'minibuffer-choose-next)
(define-key minibuffer-choose-map "\C-r"          'minibuffer-choose-prev)
(define-key minibuffer-choose-map "\M-p"          'minibuffer-choose-prev)
(define-key minibuffer-choose-map "\C-p"          'minibuffer-choose-prev)
(define-key minibuffer-choose-map (kbd "<S-tab>") 'minibuffer-choose-prev)
(define-key minibuffer-choose-map "\C-a"          'minibuffer-choose-first)
(define-key minibuffer-choose-map "\C-e"          'minibuffer-choose-last)

(defun minibuffer-choose-map-key (function)
  "Apply FUNCTION on each value of the text property
'minibuffer-choose-key and make a list of the results. FUNCTION
takes two arguments: the POSITION and VALUE."
  (let ((inhibit-point-motion-hooks t)
        (continue t) results)
    (save-excursion
      (goto-char (minibuffer-prompt-end))
      (while continue
        (when (get-text-property (point) 'minibuffer-choose-key)
          (push (funcall function (point)
                         (get-text-property (point) 'minibuffer-choose-key))
                results))
        (setq continue (next-single-property-change (point) 'minibuffer-
choose-key))
        (goto-char (or continue (point)))))
    (nreverse results)))

(defun minibuffer-choose-match ()
  "Set `minibuffer-choose-match' to the field that matches the
character you type."
  (interactive)
  (declare (special minibuffer-choose-match))
  (setq minibuffer-choose-match
        (car (delete nil (minibuffer-choose-map-key
                          (lambda (pos value)
                            (when (eq last-command-event value)
                              (get-text-property pos 'field)))))))
  (exit-minibuffer))

(defun minibuffer-choose-current ()
  "Set `minibuffer-choose-match' to the field at point."
  (interactive)
  (declare (special minibuffer-choose-match))
  (let ((field (get-text-property (point) 'field)))
    (when field
      (setq minibuffer-choose-match field)))
  (exit-minibuffer))

(defun minibuffer-choose-field-search (direction &optional position limit)
  "Search text property 'field after POSITION and not past LIMIT.
If found, the point is moved to the beginning of the field and
the new position is returned; else return nil."
  (let ((search-func (if (memq direction '(nil forward next))
                         'next-single-property-change
                       'previous-single-property-change))
        found)
    (unless position (setq position (point)))
    (when (setq found (funcall search-func position 'field nil limit))
      (goto-char found)
      (goto-char (field-beginning)))))

(defun minibuffer-choose-key-search ()
  "Search text property 'minibuffer-choose-key in current field
and place point on the first occurence."
  (let (found)
    (save-excursion
      (save-restriction
        (narrow-to-region (field-beginning) (field-end))
        (goto-char (point-min))
        (setq found (next-single-property-change (point)
                                                 'minibuffer-choose-key))))
    (when (and found (not (get-text-property (point) 'minibuffer-choose-key))
               (goto-char found)))))

(defun minibuffer-choose-first ()
  "Go to the first field."
  (interactive)
  (goto-char (minibuffer-prompt-end))
  (unless (get-text-property (point) 'field)
    (minibuffer-choose-field-search 'next (minibuffer-prompt-end)))
  (minibuffer-choose-key-search))

(defun minibuffer-choose-last ()
  "Go to the last field."
  (interactive)
  (minibuffer-choose-field-search 'prev (point-max))
  (minibuffer-choose-key-search))

(defun minibuffer-choose-next ()
  "Go to the next field."
  (interactive)
  (or (minibuffer-choose-field-search 'next (field-end))
      (minibuffer-choose-first))
  (minibuffer-choose-key-search))

(defun minibuffer-choose-prev ()
  "Go to the previous field."
  (interactive)
  (or (minibuffer-choose-field-search 'prev (field-beginning))
      (minibuffer-choose-last))
  (minibuffer-choose-key-search))

(defun minibuffer-choose-setup ()
  "This function sets up minibuffer for `minibuffer-choose'."
  (declare (special minibuffer-choose-choices minibuffer-choose-setup
                    minibuffer-choose-key-type minibuffer-choose-overlay
                    minibuffer-choose-format minibuffer-choose-default
                    separator))
  (when (and (boundp 'minibuffer-choose-setup)
             minibuffer-choose-setup)
    (setq minibuffer-choose-overlay (make-overlay (point) (point)))
    (overlay-put minibuffer-choose-overlay 'face 'minibuffer-choose-highlight)
    (loop for choice in minibuffer-choose-choices
          for tail = minibuffer-choose-choices then (cdr tail)
          for index from 1
          for display = (cond
                         ((functionp minibuffer-choose-format)
                          (funcall minibuffer-choose-format choice))
                         ((symbolp choice) (symbol-name choice))
                         ((stringp choice) choice)
                         (t (error "%s not recognised" choice)))
          with used-keys
          with default          ; to store the default choice position
          with beg and end and choice-beginning
          do
          (setq beg (point))
          (when (equal minibuffer-choose-default choice)
            (setq default (point)))
          (when (and (memq minibuffer-choose-key-type '(digit both))
                     (<= index 10))
            (when (= index 10) (setq index 0))
            (insert "[" (propertize (number-to-string index)
                                    'minibuffer-choose-key (+ 48 index)
                                    'face 'minibuffer-choose-digit) "]"))
          (setq choice-beginning (point))
          (insert display)
          (setq end (point))
          (when (memq minibuffer-choose-key-type '(alpha both))
            (save-excursion
              (goto-char (1- choice-beginning))
              (catch 'exit
                (while (re-search-forward "[[:alpha:]]" end t)
                  (unless (memq (char-before) used-keys)
                    (push (char-before) used-keys)
                    (add-text-properties (1- (point)) (point)
                                         (list 'minibuffer-choose-key (char-
before)
                                               'face 'minibuffer-choose-
alpha))
                    (throw 'exit nil))))))
          (add-text-properties
           beg end
           (list 'field choice
                 'front-sticky t
                 'point-entered (lambda (oldpt newpt)
                                  (move-overlay minibuffer-choose-overlay
                                                (field-beginning) (field-
end)))))
          (when (cdr tail) (insert (or separator " ")))
          ;; go to the default choice
          finally (if (null default)
                      (minibuffer-choose-first)
                    (goto-char default)
                    (minibuffer-choose-key-search)))
    (setq minibuffer-choose-setup nil)))

;;;; comments for non-trivial let-bound dynamic varialbes:
;;; 1. `minibuffer-choose-setup': indicate whether to perform
;;;     minibuffer setup; see also the function with the same name;
;;; 2. `minibuffer-choose-match': value to be returned by `minibuffer-choose';
;;; 3. `minibuffer-choose-overlay': overlay used in the minibuffer.
;;;; end

;;;###autoload
(defun minibuffer-choose (prompt choices &optional format default key-type 
separator)
  "Present CHOICES in minibuffer with keys to select each choice.

FORMAT is a function to format the display of choices. It will be
applied on each element of CHOICES and must return a string to be
inserted in the minibuffer.

DEFAULT designates the default choice. If it is a number the nth
choice in CHOICES will be used.

KEY-TYPE defaults to digit and takes three values:
    digit: use digit for keys
    alpha: use alpha for keys
     both: use both digit and alpha for keys

SEPARATOR is a string that is inserted between CHOICES."
  (let ((minibuffer-history nil) ; ignore history from `minibuffer-choose'
        (minibuffer-choose-setup t)
        (minibuffer-choose-choices choices)
        (minibuffer-choose-match nil)
        (minibuffer-choose-format format)
        (minibuffer-choose-default (if (numberp default)
                                       (nth default choices)
                                     default))
        (minibuffer-choose-key-type (or key-type 'digit))
        (minibuffer-choose-overlay)
        (minibuffer-setup-hook minibuffer-setup-hook))
    (add-hook 'minibuffer-setup-hook 'minibuffer-choose-setup)
    (read-from-minibuffer prompt nil minibuffer-choose-map)
    minibuffer-choose-match))

(provide 'minibuffer-choose)
;;; minibuffer-choose.el ends here

答案1

我不想给你提供另一种选择,但如果你正在寻找自动插入功能,你可以考虑一下yasnippet相反,它非常强大,并成为 emacs 插入的事实标准。他们已经有很多乳胶片段人们一直在努力,但对于你自己需要的任何特殊的东西,添加新的也相当容易。

相关内容