ELISP:如何将显示完成列表捕获到变量

  • 本文关键字:列表 变量 显示 ELISP elisp
  • 更新时间 :
  • 英文 :


我有一个目标列表,我想编写一个函数,您可以在其中选择当前目标。我的代码如下所示。

问题是当我做"M-x my-test"时,current_target被设置为nil和选定的地址打印在当前缓冲区上。

如何将缓冲区输出捕获到current_target?还是我的整个方法都是错误的?

请指教?阅读哪个文档?

感谢

-悉

(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 targetnn")
    (setq current-target (display-completion-list target-list))))

不确定你想要什么行为。但是,如果您只想让用户选择您的字符串之一,请尝试使用 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)))

你明白了。

;; 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)))

最新更新