我有一个目标列表,我想写一个函数,你可以在其中选择当前的目标。我的代码如下所示。
问题是,当我执行"M-x my- 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))))发布于 2014-11-14 06:51:29
不确定你到底想要什么样的行为。但是,如果您只想让用户选择您的字符串之一,那么可以尝试使用completing-read
(defun my-test ()
(interactive)
(setq current-target (completing-read "Target: " target-list nil t)))或者,如果您想返回关联的目标,则查找在列表中选择的字符串:
(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)))你明白了吧。
发布于 2014-11-14 16:13:35
;; 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)))https://stackoverflow.com/questions/26919545
复制相似问题