Writing “Hello World” in Emacs?

后端 未结 2 615
谎友^
谎友^ 2021-02-01 05:27

I would like to write a few Unix scripts in Emacs Lisp. However, there doesn\'t seem to be a clean way to write to STDOUT so I can redirect the results to a file or pipe the ou

相关标签:
2条回答
  • 2021-02-01 06:04

    As David Antaramian says, you probably want princ.

    Also, message supports a format control string (akin to printf in C) that is adapted from format. So, you may eventually want to do something like

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

    As a couple of functions plus demonstration:

    (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"))
    
    0 讨论(0)
  • 2021-02-01 06:28

    Seems like you want princ instead of print. So, basically:

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

    However, one caveat is that princ does not automatically terminate the output with \n.

    0 讨论(0)
提交回复
热议问题