x-menu.el 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ;; Copyright (C) 1986 Free Software Foundation, Inc.
  2. ;; This file is part of GNU Emacs.
  3. ;; GNU Emacs is distributed in the hope that it will be useful,
  4. ;; but WITHOUT ANY WARRANTY. No author or distributor
  5. ;; accepts responsibility to anyone for the consequences of using it
  6. ;; or for whether it serves any particular purpose or works at all,
  7. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  8. ;; License for full details.
  9. ;; Everyone is granted permission to copy, modify and redistribute
  10. ;; GNU Emacs, but only under the conditions described in the
  11. ;; GNU Emacs General Public License. A copy of this license is
  12. ;; supposed to have been given to you along with GNU Emacs so you
  13. ;; can know your rights and responsibilities. It should be in a
  14. ;; file named COPYING. Among other things, the copyright notice
  15. ;; and this notice must be preserved on all copies.
  16. (defmacro caar (conscell)
  17. (list 'car (list 'car conscell)))
  18. (defmacro cdar (conscell)
  19. (list 'cdr (list 'car conscell)))
  20. (defun x-menu-mode ()
  21. "Major mode for creating permanent menus for use with X.
  22. These menus are iimplemented entirely in Lisp; popup menus, implemented
  23. with x-popup-menu, are implemented using XMenu primitives."
  24. (make-local-variable 'x-menu-items-per-line)
  25. (make-local-variable 'x-menu-item-width)
  26. (make-local-variable 'x-menu-items-alist)
  27. (make-local-variable 'x-process-mouse-hook)
  28. (make-local-variable 'x-menu-assoc-buffer)
  29. (setq buffer-read-only t)
  30. (setq truncate-lines t)
  31. (setq x-process-mouse-hook 'x-menu-pick-entry)
  32. (setq mode-line-buffer-identification '("MENU: %32b")))
  33. (defvar x-menu-max-width 0)
  34. (defvar x-menu-items-per-line 0)
  35. (defvar x-menu-item-width 0)
  36. (defvar x-menu-items-alist nil)
  37. (defvar x-menu-assoc-buffer nil)
  38. (defvar x-menu-item-spacing 1
  39. "*Minimum horizontal spacing between objects in a permanent X menu.")
  40. (defun x-menu-create-menu (name)
  41. "Create a permanent X menu. Returns an item which should be used as a
  42. menu object whenever referring to the menu."
  43. (let ((old (current-buffer))
  44. (buf (get-buffer-create name)))
  45. (set-buffer buf)
  46. (x-menu-mode)
  47. (setq x-menu-assoc-buffer old)
  48. (set-buffer old)
  49. buf))
  50. (defun x-menu-change-associated-buffer (menu buffer)
  51. "Change associated buffer of MENU to BUFFER. BUFFER should be a buffer
  52. object."
  53. (let ((old (current-buffer)))
  54. (set-buffer menu)
  55. (setq x-menu-assoc-buffer buffer)
  56. (set-buffer old)))
  57. (defun x-menu-add-item (menu item binding)
  58. "Adds to MENU an item with name ITEM, associated with BINDING.
  59. Following a sequence of calls to x-menu-add-item, a call to x-menu-compute
  60. should be performed before the menu will be made available to the user.
  61. BINDING should be a function of one argument, which is the numerical
  62. button/key code as defined in x-menu.el."
  63. (let ((old (current-buffer))
  64. elt)
  65. (set-buffer menu)
  66. (if (setq elt (assoc item x-menu-items-alist))
  67. (rplacd elt binding)
  68. (setq x-menu-items-alist (append x-menu-items-alist
  69. (list (cons item binding)))))
  70. (set-buffer old)
  71. item))
  72. (defun x-menu-delete-item (menu item)
  73. "Deletes from MENU the item named ITEM. x-menu-compute should be called
  74. before the menu is made available to the user."
  75. (let ((old (current-buffer))
  76. elt)
  77. (set-buffer menu)
  78. (if (setq elt (assoc item x-menu-items-alist))
  79. (rplaca elt nil))
  80. (set-buffer old)
  81. item))
  82. (defun x-menu-activate (menu)
  83. "Computes all necessary parameters for MENU. This must be called whenever
  84. a menu is modified before it is made available to the user.
  85. This also creates the menu itself."
  86. (let ((buf (current-buffer)))
  87. (pop-to-buffer menu)
  88. (let (buffer-read-only)
  89. (setq x-menu-max-width (1- (screen-width)))
  90. (setq x-menu-item-width 0)
  91. (let (items-head
  92. (items-tail x-menu-items-alist))
  93. (while items-tail
  94. (if (caar items-tail)
  95. (progn (setq items-head (cons (car items-tail) items-head))
  96. (setq x-menu-item-width
  97. (max x-menu-item-width
  98. (length (caar items-tail))))))
  99. (setq items-tail (cdr items-tail)))
  100. (setq x-menu-items-alist (reverse items-head)))
  101. (setq x-menu-item-width (+ x-menu-item-spacing x-menu-item-width))
  102. (setq x-menu-items-per-line
  103. (max 1 (/ x-menu-max-width x-menu-item-width)))
  104. (erase-buffer)
  105. (let ((items-head x-menu-items-alist))
  106. (while items-head
  107. (let ((items 0))
  108. (while (and items-head
  109. (<= (setq items (1+ items)) x-menu-items-per-line))
  110. (insert (format (concat "%"
  111. (int-to-string x-menu-item-width) "s")
  112. (caar items-head)))
  113. (setq items-head (cdr items-head))))
  114. (insert ?\n)))
  115. (shrink-window (max 0
  116. (- (window-height)
  117. (1+ (count-lines (point-min) (point-max))))))
  118. (goto-char (point-min)))
  119. (pop-to-buffer buf)))
  120. (defun x-menu-pick-entry (position event)
  121. "Internal function for dispatching on mouse/menu events"
  122. (let* ((x (min (1- x-menu-items-per-line)
  123. (/ (current-column) x-menu-item-width)))
  124. (y (- (count-lines (point-min) (point))
  125. (if (zerop (current-column)) 0 1)))
  126. (item (+ x (* y x-menu-items-per-line)))
  127. (litem (cdr (nth item x-menu-items-alist))))
  128. (and litem (funcall litem event)))
  129. (pop-to-buffer x-menu-assoc-buffer))