ELISP:如何将display-completion-list捕获到变量中

时间:2014-11-13 22:20:30

标签: elisp

我有一个目标列表,我想写一个函数,你可以选择 目前的目标。我的代码如下所示。

问题在于,当我执行“M-x my-test”时,current_target设置为nil并且已选中 地址打印在当前缓冲区上。

如何将缓冲区输出捕获到current_target?或者我的整个方法都错了?

请指教?要阅读哪个文档?

感谢名单

-Siddhartha

(defvar target-list '( ("10.25.110.113" " -> target-1") 
    ("10.25.110.114" " -> target-2")) "List of Target boxes")

(defvar current-target "0.0.0.0" "Current target")

(defun my-test ()
  (interactive)
  (with-output-to-temp-buffer "*Target List*"
    (princ "\nPlease click on IP address to choose the target\n\n")
    (setq current-target (display-completion-list target-list))))

2 个答案:

答案 0 :(得分:2)

不确定您想要的确切行为。但是,如果您只是想让用户选择一个字符串,请尝试使用completing-read

(defun my-test ()
  (interactive)
  (setq current-target  (completing-read "Target: " target-list nil t)))

或者,如果您想要返回关联的目标,请查找您的alist中选择的字符串:

(defun my-test ()
  (interactive)
  (let (target)
    (setq current-target  (completing-read "Target: " target-list nil t)
          target          (cdr (assoc current-target target-list)))
    (message "Target: %s" target)))

你明白了。

答案 1 :(得分:0)

;; The code for the question after the reply from Drew is as follows
;; The idea is to present to the user names to choose from.
;; Thanx Drew for "giving the idea"

(defvar target-assoc-list '( ("Fire"  . "10.25.110.113")  ("Earth" . "10.25.110.114")
             ("Water" . "10.25.110.115")  ("Air"   . "10.25.110.116"))
 "The assoc list of (name . ip-addr) so that user chooses by name 
  and current-target is assigned the ip address")


(defvar current-target "0.0.0.0")

(defun my-select-target ()
   (interactive)
   (let (name)
       (setq name (completing-read "Enter Target (TAB for list): " 
                                    target-assoc-list nil t)
             current-target (cdr (assoc name  target-assoc-list)))
       (message "Chosen current-target IP address: %s name: %s" current-target name)))