org-mks.el 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ;;; org-mks.el --- Multi-key-selection for Org-mode
  2. ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;; Commentary:
  21. ;;
  22. ;;; Code:
  23. (require 'org)
  24. (eval-when-compile
  25. (require 'cl))
  26. (defun org-mks (table title &optional prompt specials)
  27. "Select a member of an alist with multiple keys.
  28. TABLE is the alist which should contain entries where the car is a string.
  29. There should be two types of entries.
  30. 1. prefix descriptions like (\"a\" \"Description\")
  31. This indicates that `a' is a prefix key for multi-letter selection, and
  32. that there are entries following with keys like \"ab\", \"ax\"...
  33. 2. Selectable members must have more than two elements, with the first
  34. being the string of keys that lead to selecting it, and the second a
  35. short description string of the item.
  36. The command will then make a temporary buffer listing all entries
  37. that can be selected with a single key, and all the single key
  38. prefixes. When you press the key for a single-letter entry, it is selected.
  39. When you press a prefix key, the commands (and maybe further prefixes)
  40. under this key will be shown and offered for selection.
  41. TITLE will be placed over the selection in the temporary buffer,
  42. PROMPT will be used when prompting for a key. SPECIAL is an alist with
  43. also (\"key\" \"description\") entries. When one of these is selection,
  44. only the bare key is returned."
  45. (setq prompt (or prompt "Select: "))
  46. (let (tbl orig-table dkey ddesc des-keys allowed-keys
  47. current prefix rtn re pressed buffer (inhibit-quit t))
  48. (save-window-excursion
  49. (setq buffer (org-switch-to-buffer-other-window "*Org Select*"))
  50. (setq orig-table table)
  51. (catch 'exit
  52. (while t
  53. (erase-buffer)
  54. (insert title "\n\n")
  55. (setq tbl table
  56. des-keys nil
  57. allowed-keys nil)
  58. (setq prefix (if current (concat current " ") ""))
  59. (while tbl
  60. (cond
  61. ((and (= 2 (length (car tbl))) (= (length (caar tbl)) 1))
  62. ;; This is a description on this level
  63. (setq dkey (caar tbl) ddesc (cadar tbl))
  64. (pop tbl)
  65. (push dkey des-keys)
  66. (push dkey allowed-keys)
  67. (insert prefix "[" dkey "]" "..." " " ddesc "..." "\n")
  68. ;; Skip keys which are below this prefix
  69. (setq re (concat "\\`" (regexp-quote dkey)))
  70. (while (and tbl (string-match re (caar tbl))) (pop tbl)))
  71. ((= 2 (length (car tbl)))
  72. ;; Not yet a usable description, skip it
  73. )
  74. (t
  75. ;; usable entry on this level
  76. (insert prefix "[" (caar tbl) "]" " " (nth 1 (car tbl)) "\n")
  77. (push (caar tbl) allowed-keys)
  78. (pop tbl))))
  79. (when specials
  80. (insert "-------------------------------------------------------------------------------\n")
  81. (let ((sp specials))
  82. (while sp
  83. (insert (format "[%s] %s\n"
  84. (caar sp) (nth 1 (car sp))))
  85. (push (caar sp) allowed-keys)
  86. (pop sp))))
  87. (push "\C-g" allowed-keys)
  88. (goto-char (point-min))
  89. (if (not (pos-visible-in-window-p (point-max)))
  90. (org-fit-window-to-buffer))
  91. (message prompt)
  92. (setq pressed (char-to-string (read-char-exclusive)))
  93. (while (not (member pressed allowed-keys))
  94. (message "Invalid key `%s'" pressed) (sit-for 1)
  95. (message prompt)
  96. (setq pressed (char-to-string (read-char-exclusive))))
  97. (when (equal pressed "\C-g")
  98. (kill-buffer buffer)
  99. (error "Abort"))
  100. (when (and (not (assoc pressed table))
  101. (not (member pressed des-keys))
  102. (assoc pressed specials))
  103. (throw 'exit (setq rtn pressed)))
  104. (unless (member pressed des-keys)
  105. (throw 'exit (setq rtn (rassoc (cdr (assoc pressed table))
  106. orig-table))))
  107. (setq current (concat current pressed))
  108. (setq table (mapcar
  109. (lambda (x)
  110. (if (and (> (length (car x)) 1)
  111. (equal (substring (car x) 0 1) pressed))
  112. (cons (substring (car x) 1) (cdr x))
  113. nil))
  114. table))
  115. (setq table (remove nil table)))))
  116. (when buffer (kill-buffer buffer))
  117. rtn))
  118. (provide 'org-mks)
  119. ;;; org-mks.el ends here