【问题标题】:Writing "Hello World" in Emacs?在 Emacs 中编写“Hello World”?
【发布时间】:2011-01-11 08:21:41
【问题描述】:

我想用 Emacs Lisp 写一些 Unix 脚本。但是,似乎没有一种干净的方式可以写入 STDOUT,因此我可以将结果重定向到文件或将输出通过管道传输到另一个命令。 print 函数在输出字符串周围放置双引号,因此我得到 "Hello world!" 而不是 Hello world!

这是 emacs 脚本。

#!/usr/bin/emacs --script ;; ;;从 Unix shell 运行我:./hello.el > x.txt ;; (消息“Hello world!我正在写信给 STDERR。”) (打印“Hello world!我正在写信给 STDOUT,但我在引号中”) (插入“Hello world!我正在写入 Emacs 缓冲区”) (写入文件“y.txt”)

我想这样称呼它。

你好.el > x.txt 你好.el |厕所

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:

    您似乎想要princ 而不是print。所以,基本上:

    (princ "Hello world! I'm writing to STDOUT but I'm not in quotes!")

    但是,需要注意的一点是princ 不会自动以\n 终止输出。

    【讨论】:

    【解决方案2】:

    正如大卫·安塔拉米安所说,您可能想要princ

    此外,message 支持改编自 format 的格式控制字符串(类似于 C 中的 printf)。所以,你最终可能想要做类似的事情

    (princ (format "Hello, %s!\n" "World"))
    

    作为几个函数加演示:

    (defun fmt-stdout (&rest args)
      (princ (apply 'format args)))
    (defun fmtln-stdout (&rest args)
      (princ (apply 'format
                    (if (and args (stringp (car args)))
                        (cons (concat (car args) "\n") (cdr args))
                      args))))
    
    (defun test-fmt ()
      (message "Hello, %s!" "message to stderr")
      (fmt-stdout "Hello, %s!\n" "fmt-stdout, explict newline")
      (fmtln-stdout "Hello, %s!" "fmtln-stdout, implicit newline"))
    

    【讨论】: