首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ELISP:如何将显示完成列表捕获到变量

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

Stack Overflow用户
提问于 2014-11-14 06:20:30
回答 2查看 57关注 0票数 1

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

问题是,当我执行"M-x my- current_target“时,测试被设置为nil,并且所选的地址被打印到当前缓冲区。

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

有什么建议吗?阅读哪个文档?

谢谢

-Siddhartha

代码语言:javascript
复制
(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))))
EN

回答 2

Stack Overflow用户

发布于 2014-11-14 06:51:29

不确定你到底想要什么样的行为。但是,如果您只想让用户选择您的字符串之一,那么可以尝试使用completing-read

代码语言:javascript
复制
(defun my-test ()
  (interactive)
  (setq current-target  (completing-read "Target: " target-list nil t)))

或者,如果您想返回关联的目标,则查找在列表中选择的字符串:

代码语言:javascript
复制
(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)))

你明白了吧。

票数 2
EN

Stack Overflow用户

发布于 2014-11-14 16:13:35

代码语言:javascript
复制
;; 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)))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26919545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档