org-mks.el 4.8 KB

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