Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Weeks ago I found this answer to Most significant reasons that led us to (La)TeX on

"Leo2007" describes, how to insert *.tex-templates into a buffer of Emacs. I could not find out, how to make it work and decided to ask on the autex-mailinglist, but got no answer for three weeks. So maybe you can help me:

"Leo2007" presented the file minibuffer-choose.el, which should allow to insert a template into one's document quickly. Follow the link Nr. 1 above and you will see a quite convincing picture.

Unfortunately I do not understand how to use it. I got the the file minibuffer-choose.el from here: (I added it below).

I made a byte-compile, put it where emacs can find it and added 3 lines to my .emacs-file:

(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?



;;; 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
;; 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 <>.

;;; 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]
(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)
      (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))
        (setq continue (next-single-property-change (point) 'minibuffer-
        (goto-char (or continue (point)))))
    (nreverse results)))

(defun minibuffer-choose-match ()
  "Set `minibuffer-choose-match' to the field that matches the
character you type."
  (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)))))))

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

(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))
    (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)
        (narrow-to-region (field-beginning) (field-end))
        (goto-char (point-min))
        (setq found (next-single-property-change (point)
    (when (and found (not (get-text-property (point) 'minibuffer-choose-key))
               (goto-char found)))))

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

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

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

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

(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
  (when (and (boundp '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
          (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))
              (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-
                                               'face 'minibuffer-choose-
                    (throw 'exit nil))))))
           beg end
           (list 'field choice
                 'front-sticky t
                 'point-entered (lambda (oldpt newpt)
                                  (move-overlay minibuffer-choose-overlay
                                                (field-beginning) (field-
          (when (cdr tail) (insert (or separator " ")))
          ;; go to the default choice
          finally (if (null default)
                    (goto-char default)
    (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

(defun minibuffer-choose (prompt choices &optional format default key-type 
  "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)
        (minibuffer-choose-key-type (or key-type 'digit))
        (minibuffer-setup-hook minibuffer-setup-hook))
    (add-hook 'minibuffer-setup-hook 'minibuffer-choose-setup)
    (read-from-minibuffer prompt nil minibuffer-choose-map)

(provide 'minibuffer-choose)
;;; minibuffer-choose.el ends here
share|improve this question

migrated from May 24 '11 at 13:35

This question came from our site for users of TeX, LaTeX, ConTeXt, and related typesetting systems.

1 Answer 1

I hate to offer you another choice instead, but if you're looking for auto-insertion capabilities you might look into yasnippet instead which is wicked powerful and becoming the defacto-standard for emacs insertions. They already have a bunch of latex snippets that people have been working on, but adding new ones is quite easy for anything special you need for yourself.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.