Guile: setting *current-warning-prefix* during evaluation
[geiser.git] / elisp / geiser-menu.el
blob5d522e724d880c81ba82279c2a1b903856e566a1
1 ;;; geiser-menu.el -- menu and keymaps definition
3 ;; Copyright (c) 2010 Jose Antonio Ortega Ruiz
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the Modified BSD License. You should
7 ;; have received a copy of the license along with this program. If
8 ;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.
10 ;; Start date: Sat Jun 12, 2010 03:01
13 (require 'geiser-custom)
14 (require 'geiser-base)
17 ;;; Customization:
19 (geiser-custom--defcustom geiser-global-menu-always-on-p nil
20 "Whether the Geiser menu is always visible."
21 :type 'boolean
22 :group 'geiser)
25 ;;; Top-level menu
27 (defmacro geiser-menu--add-item (keymap map kd)
28 (cond ((or (eq '-- kd) (eq 'line kd)) `(geiser-menu--add-line ,map))
29 ((stringp (car kd)) `(geiser-menu--add-basic-item ,keymap ,map ,kd))
30 ((eq 'menu (car kd)) `(geiser-menu--add-submenu ,(cadr kd)
31 ,keymap ,map ,(cddr kd)))
32 ((eq 'custom (car kd)) `(geiser-menu--add-custom ,(nth 1 kd)
33 ,(nth 2 kd)
34 ,keymap
35 ,map))
36 ((eq 'mode (car kd)) `(geiser-menu--mode-toggle ,(nth 1 kd)
37 ,(nth 2 kd)
38 ,(nth 3 kd)
39 ,keymap
40 ,map))
41 (t (error "Bad item form: %s" kd))))
43 (defmacro geiser-menu--add-basic-item (keymap map kd)
44 (let* ((title (nth 0 kd))
45 (binding (nth 1 kd))
46 (cmd (nth 2 kd))
47 (hlp (nth 3 kd))
48 (item (make-symbol title))
49 (hlp (and (stringp hlp) (list :help hlp)))
50 (rest (or (and hlp (nthcdr 4 kd))
51 (nthcdr 3 kd)))
52 (binding (if (listp binding)
53 binding
54 (list binding))))
55 `(progn (define-key ,map [,item]
56 '(menu-item ,title ,cmd ,@hlp ,@rest))
57 ,@(and (car binding)
58 `((put ',cmd
59 :advertised-binding
60 ,(car binding))))
61 ,@(mapcar (lambda (b)
62 `(define-key ,keymap ,b ',cmd))
63 binding))))
65 (defmacro geiser-menu--add-items (keymap map keys)
66 `(progn ,@(mapcar (lambda (k) (list 'geiser-menu--add-item keymap map k))
67 (reverse keys))))
69 (defmacro geiser-menu--add-submenu (name keymap map keys)
70 (let ((ev (make-symbol name))
71 (map2 (make-symbol "map2")))
72 `(progn
73 (let ((,map2 (make-sparse-keymap ,name)))
74 (define-key ,map [,ev] (cons ,name ,map2))
75 (geiser-menu--add-items ,keymap ,map2 ,keys)))))
77 (defvar geiser-menu--line-counter 0)
79 (defun geiser-menu--add-line (&optional map)
80 (let ((line (make-symbol (format "line%s"
81 (setq geiser-menu--line-counter
82 (1+ geiser-menu--line-counter))))))
83 (define-key (or map global-map) `[,line]
84 `(menu-item "--single-line"))))
86 (defmacro geiser-menu--add-custom (title group keymap map)
87 `(geiser-menu--add-item ,keymap ,map
88 (,title nil (lambda () (interactive) (customize-group ',group)))))
90 (defmacro geiser-menu--mode-toggle (title bindings mode keymap map)
91 `(geiser-menu--add-item ,keymap ,map
92 (,title ,bindings ,mode
93 :button (:toggle . (and (boundp ',mode) ,mode)))))
95 (defmacro geiser-menu--defmenu (name keymap &rest keys)
96 (let ((mmap (make-symbol "mmap")))
97 `(progn
98 (let ((,mmap (make-sparse-keymap "Geiser")))
99 (define-key ,keymap [menu-bar ,name] (cons "Geiser" ,mmap))
100 (define-key ,mmap [customize]
101 (cons "Customize" geiser-menu--custom-customize))
102 (define-key ,mmap [switch]
103 (cons "Switch to" geiser-menu--custom-switch))
104 (define-key ,mmap [Run] (cons "Run" geiser-menu--custom-run))
105 (geiser-menu--add-line ,mmap)
106 (geiser-menu--add-items ,keymap ,mmap ,keys)
107 ,mmap))))
109 (put 'geiser-menu--defmenu 'lisp-indent-function 2)
112 ;;; Shared entries
114 (defvar geiser-menu--custom-map (make-sparse-keymap "Geiser"))
115 (defvar geiser-menu--custom-run (make-sparse-keymap "Run"))
116 (defvar geiser-menu--custom-switch (make-sparse-keymap "Switch"))
117 (defvar geiser-menu--custom-customize (make-sparse-keymap "Customize"))
119 (define-key geiser-menu--custom-map [customize]
120 (cons "Customize" geiser-menu--custom-customize))
121 (define-key geiser-menu--custom-map [switch]
122 (cons "Switch to" geiser-menu--custom-switch))
123 (define-key geiser-menu--custom-map [run]
124 (cons "Run" geiser-menu--custom-run))
126 (defun geiser-menu--add-global-custom (title group)
127 (define-key geiser-menu--custom-customize `[,(make-symbol title)]
128 (cons title `(lambda () (interactive) (customize-group ',group)))))
130 (defun geiser-menu--add-impl (name runner switcher)
131 (let ((title (capitalize (format "%s" name)))
132 (group (intern (format "geiser-%s" name))))
133 (define-key geiser-menu--custom-run `[,name]
134 `(menu-item ,title ,runner :enable (geiser-impl--active-p ',name)))
135 (define-key geiser-menu--custom-switch `[,name]
136 `(menu-item ,title ,switcher :enable (geiser-repl--repl/impl ',name)))
137 (geiser-menu--add-global-custom title group)))
139 (geiser-menu--add-global-custom "Geiser" 'geiser)
143 (provide 'geiser-menu)
144 ;;; geiser-menu.el ends here