home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / templates / part05 / menu.el < prev    next >
Encoding:
Text File  |  1987-10-04  |  6.1 KB  |  211 lines

  1. ;;; menu.el
  2. ;;; Copyright (C) 1987 Mark A. Ardis.
  3.  
  4. (provide 'menu)
  5.  
  6. ;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  7.  
  8. (defvar menu-choice nil
  9.   "Item selected from menu."
  10. ) ; menu-choice
  11. (make-variable-buffer-local 'menu-choice)
  12.  
  13. (defvar menu-last-input ""
  14.   "String used for searching through menu."
  15. ) ; menu-last-input
  16. (make-variable-buffer-local 'menu-last-input)
  17. (setq-default menu-last-input "")
  18.  
  19. (defvar menu-mode-map nil "Key-map for Menu Mode.")
  20.  
  21. ;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  22.  
  23. (defun menu-mode ()
  24.   "Major mode for selecting an item from a menu.
  25.      Like View Mode, but with only the following commands:
  26.      Space, C-n   menu-next-item
  27.      Del, C-p     menu-previous-item
  28.      a,b,...,z    menu-next-match
  29.      Return, Linefeed, ESC C-c   exit.
  30.    Returns the line selected."
  31.   (interactive)
  32.                     ; Local Variables
  33.   (let ()
  34.                     ; Body
  35.     (or menu-mode-map
  36.     (menu-set-local-keys))
  37.     (use-local-map menu-mode-map)
  38.     (setq major-mode 'menu-mode)
  39.     (setq mode-name "Menu")
  40.     (setq case-fold-search t)
  41.     (setq menu-last-input "")
  42.                     ; Prompt for selection
  43.     (message "Position on selection and exit with Return (or ESC-^c).")
  44.                     ; Wait for user's selction
  45.     (setq menu-choice nil)
  46.     (unwind-protect
  47.     (recursive-edit)
  48.     ) ; unwind-protect
  49.                     ; Return selection
  50.     menu-choice
  51.   ) ; let
  52. ) ; defun menu-mode
  53.  
  54. ;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  55.  
  56. (defun menu-abort-recursive-edit ()
  57.   "Abort recursive edit in menu-mode gracefully."
  58.   (interactive)
  59.                     ; Local Variables
  60.   (let ()
  61.                     ; Body
  62.     (setq menu-choice nil)
  63.     (exit-recursive-edit)
  64.   ) ; let
  65. ) ; defun menu-abort-recursive-edit
  66.  
  67. ;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  68.  
  69. (defun menu-exit-recursive-edit ()
  70.   "Pick up selection and exit Menu-mode."
  71.   (interactive)
  72.                     ; Local Variables
  73.   (let (start stop)
  74.                     ; Body
  75.     (beginning-of-line)
  76.     (setq start (point))
  77.     (end-of-line)
  78.     (setq stop (point))
  79.     (setq menu-choice (buffer-substring start stop))
  80.     (exit-recursive-edit)
  81.   ) ; let
  82. ) ; defun menu-exit-recursive-edit
  83.  
  84. ;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  85.  
  86. (defun menu-next-item ()
  87.   "Go to the next item in the menu (wrap around at end)."
  88.   (interactive)
  89.                     ; Local Variables
  90.   (let ()
  91.                     ; Body
  92.     (end-of-line)
  93.     (if (eobp)
  94.       (beginning-of-buffer)
  95.       (beginning-of-line 2)
  96.     ) ; if
  97.     (setq menu-last-input "")
  98.   ) ; let
  99. ) ; defun menu-next-item
  100.  
  101. ;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  102.  
  103. (defun menu-next-match ()
  104.   "Find the next item with last-input-char leading character."
  105.   (interactive)
  106.                     ; Local Variables
  107.   (let (stop)
  108.                     ; Body
  109.     (setq menu-last-input
  110.       (concat menu-last-input (char-to-string last-input-char)))
  111.     (beginning-of-line)
  112.     (if (not (re-search-forward (concat "^" menu-last-input) nil t))
  113.       (progn
  114.     (setq stop (point))
  115.     (beginning-of-buffer)
  116.     (if (not (re-search-forward (concat "^" menu-last-input) stop stop))
  117.       (progn
  118.         (message "No match for that character!")
  119.         (ding)
  120.         (setq menu-last-input "")
  121.       ) ; progn
  122.     ) ; if
  123.       ) ; progn
  124.     ) ; if
  125.   ) ; let
  126. ) ; defun menu-next-match
  127.  
  128. ;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  129.  
  130. (defun menu-previous-item ()
  131.   "Go to the previous item in the menu (wrap around at beginning)."
  132.   (interactive)
  133.                     ; Local Variables
  134.   (let ()
  135.                     ; Body
  136.     (beginning-of-line)
  137.     (if (bobp)
  138.       (progn
  139.     (end-of-buffer)
  140.     (beginning-of-line)
  141.       ) ; progn
  142.       (beginning-of-line 0)
  143.     ) ; if
  144.     (setq menu-last-input "")
  145.   ) ; let
  146. ) ; defun menu-previous-item
  147.  
  148. ;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  149.  
  150. (defun menu-set-local-keys ()
  151.   "Create key-map for Menu Mode."
  152.                     ; Local Variables
  153.   (let ()
  154.                     ; Body
  155.     (setq menu-mode-map (make-keymap))
  156.     (suppress-keymap menu-mode-map)
  157.     (define-key menu-mode-map "\C-g" 'menu-abort-recursive-edit)
  158.     (define-key menu-mode-map "\C-j" 'menu-exit-recursive-edit) ; LFD
  159.     (define-key menu-mode-map "\C-m" 'menu-exit-recursive-edit) ; CR
  160.     (define-key menu-mode-map "\e\C-c" 'menu-exit-recursive-edit)
  161.     (define-key menu-mode-map " " 'menu-next-item)
  162.     (define-key menu-mode-map "\C-n" 'menu-next-item)
  163.     (define-key menu-mode-map "\C-p" 'menu-previous-item)
  164.     (define-key menu-mode-map "\177" 'menu-previous-item) ; DEL
  165.     (define-key menu-mode-map "a" 'menu-next-match)
  166.     (define-key menu-mode-map "b" 'menu-next-match)
  167.     (define-key menu-mode-map "c" 'menu-next-match)
  168.     (define-key menu-mode-map "d" 'menu-next-match)
  169.     (define-key menu-mode-map "e" 'menu-next-match)
  170.     (define-key menu-mode-map "f" 'menu-next-match)
  171.     (define-key menu-mode-map "g" 'menu-next-match)
  172.     (define-key menu-mode-map "h" 'menu-next-match)
  173.     (define-key menu-mode-map "i" 'menu-next-match)
  174.     (define-key menu-mode-map "j" 'menu-next-match)
  175.     (define-key menu-mode-map "k" 'menu-next-match)
  176.     (define-key menu-mode-map "l" 'menu-next-match)
  177.     (define-key menu-mode-map "m" 'menu-next-match)
  178.     (define-key menu-mode-map "n" 'menu-next-match)
  179.     (define-key menu-mode-map "o" 'menu-next-match)
  180.     (define-key menu-mode-map "p" 'menu-next-match)
  181.     (define-key menu-mode-map "q" 'menu-next-match)
  182.     (define-key menu-mode-map "r" 'menu-next-match)
  183.     (define-key menu-mode-map "s" 'menu-next-match)
  184.     (define-key menu-mode-map "t" 'menu-next-match)
  185.     (define-key menu-mode-map "u" 'menu-next-match)
  186.     (define-key menu-mode-map "v" 'menu-next-match)
  187.     (define-key menu-mode-map "w" 'menu-next-match)
  188.     (define-key menu-mode-map "x" 'menu-next-match)
  189.     (define-key menu-mode-map "y" 'menu-next-match)
  190.     (define-key menu-mode-map "z" 'menu-next-match)
  191.   ) ; let
  192. ) ; defun menu-set-local-keys
  193.  
  194. ;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  195.  
  196. (defun menu-undefined ()
  197.   "Catch all undefined keys for Menu Mode."
  198.   (interactive)
  199.                     ; Local Variables
  200.   (let ()
  201.                     ; Body
  202.     (ding)
  203.     (message "C-n for next, C-p for previous, Return (or ESC-^c) to quit.")
  204.     (setq menu-last-input "")
  205.   ) ; let
  206. ) ; defun menu-undefined
  207.  
  208. ;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  209.  
  210. ;;; end of menu.el
  211.