org-mks.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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: 6.36trans
  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. (defun org-mks (table title &optional prompt specials)
  22. "Select a member of an alist with multiple keys.
  23. TABLE is the alist which should contain entries where the car is a string.
  24. There should be two types of entries.
  25. 1. prefix descriptions like (\"a\" \"Description\")
  26. This indicates that `a' is a prefix key for multi-letter selection, and
  27. that there are entries following with keys like \"ab\", \"ax\"...
  28. 2. Selectable members must have more than two elements, with the first
  29. being the string of keys that lead to selecting it, and the second a
  30. short description string of the item.
  31. The command will then make a temporary buffer listing all entries
  32. that can be selected with a single key, and all the sinke key
  33. prefixes. When you press the key for a single-letter enty, it is selected.
  34. When you press a prefix key, the commands (and maybe further prefixes)
  35. under this key will be shown and offered for selection.
  36. TITLE will be placed over the selection in the temporary buffer,
  37. PROMPT will be used when prompting for a key. SPECIAL is an alist with
  38. also (\"key\" \"description\") entries. When they are selected,
  39. "
  40. (setq prompt (or prompt "Select: "))
  41. (let (tbl orig-table dkey ddesc des-keys allowed-keys current prefix rtn)
  42. (save-window-excursion
  43. (org-switch-to-buffer-other-window "*Org Select*")
  44. (setq orig-table table)
  45. (catch 'exit
  46. (while t
  47. (erase-buffer)
  48. (insert title "\n\n")
  49. (setq tbl table
  50. des-keys nil
  51. allowed-keys nil)
  52. (setq prefix (if current (concat current " ") ""))
  53. (while tbl
  54. (cond
  55. ((and (= 2 (length (car tbl))) (= (length (caar tbl)) 1))
  56. ;; This is a description on this level
  57. (setq dkey (caar tbl) ddesc (cadar tbl))
  58. (pop tbl)
  59. (push dkey des-keys)
  60. (push dkey allowed-keys)
  61. (insert prefix "[" dkey "]" "..." " " ddesc "..." "\n")
  62. ;; Skip keys which are below this prefix
  63. (setq re (concat "\\`" (regexp-quote dkey)))
  64. (while (and tbl (string-match re (caar tbl))) (pop tbl)))
  65. ((= 2 (length (car tbl)))
  66. ;; Not yet a usable description, skip it
  67. )
  68. (t
  69. ;; usable entry on this level
  70. (insert prefix "[" (caar tbl) "]" " " (nth 1 (car tbl)) "\n")
  71. (push (caar tbl) allowed-keys)
  72. (pop tbl))))
  73. (when specials
  74. (insert "-------------------------------------------------------------------------------\n")
  75. (let ((sp specials))
  76. (while sp
  77. (insert (format "[%s] %s\n"
  78. (caar sp) (nth 1 (car sp))))
  79. (push (caar sp) allowed-keys)
  80. (pop sp))))
  81. (push "\C-g" allowed-keys)
  82. (goto-char (point-min))
  83. (if (not (pos-visible-in-window-p (point-max)))
  84. (org-fit-window-to-buffer))
  85. (message prompt)
  86. (setq pressed (char-to-string (read-char-exclusive)))
  87. (while (not (member pressed allowed-keys))
  88. (message "Invalid key `%s'" pressed) (sit-for 1)
  89. (message prompt)
  90. (setq pressed (char-to-string (read-char-exclusive))))
  91. (if (equal pressed "\C-g") (error "Abort"))
  92. (if (assoc pressed specials) (throw 'exit (setq rtn pressed)))
  93. (unless (member pressed des-keys)
  94. (throw 'exit (setq rtn (rassoc (cdr (assoc pressed table))
  95. orig-table))))
  96. (setq current (concat current pressed))
  97. (setq table (mapcar
  98. (lambda (x)
  99. (if (and (> (length (car x)) 1)
  100. (equal (substring (car x) 0 1) pressed))
  101. (cons (substring (car x) 1) (cdr x))
  102. nil))
  103. table))
  104. (setq table (remove nil table)))))
  105. (kill-buffer "*Org Select*")
  106. rtn))
  107. (provide 'org-mks)
  108. ;; arch-tag: 4ea90d0e-c6e4-4684-bd61-baf878712f9f
  109. ;;; org-mks.el ends here