elisp消息框:我可以在文本中包含换行符,如果是,如何?

时间:2012-03-30 18:52:56

标签: emacs elisp

使用message-box fn,我可以显示模态对话框。

我知道这很烦人,并不总是很好的用户体验。 Flymake使用message-box来警告当飞行检查失败时,似乎是一个很好的例子。 但是为了这个问题的目的,将用户体验问题放在一边。假设我足够明智地负责任地使用消息框。

如何格式化消息框显示的文本?最简单的情况是,如何告诉消息框显示多行文本。如果我有一个很长的消息,它会产生一个非常宽的消息框。 (Flymake使用中出现的另一个UI问题)。

点击此处查看示例。这段代码:

(message-box (concat "You need to get an \"api key\".<NL>"
             "Then, set it in your .emacs with the appropriate statement."))

导致此UI:

enter image description here

我想用换行代替<NL>。我尝试使用\n\r以及\r\n,但这些都没有奏效。我还尝试了\x000D\x000A

比简单的换行符更好,我希望能够格式化文本。斜体,大胆或其他什么。有选择吗?文档中没有提到任何内容。

我查看了源代码以试图解决这个问题,但是找不到message2,这是由消息框调用的,而且我不确定无论如何只要查看源代码我就会学到任何东西。

2 个答案:

答案 0 :(得分:3)

使用\n。这就是诀窍:

(message-box (concat "You need to get an \"api key\".\n"
                     "Then, set it in your .emacs with the appropriate statement."))

答案 1 :(得分:0)

在Windows上针对错误#11138进行黑客攻击。

(defun multiline-message-box (msg)
  "display a multiline message box on Windows.

According to bug #11138, when passing a message with newlines to
`message-box' on Windows, the rendered message-box appears all on
one line.

This function can work around that problem.
"
  (flet ((ok (&optional p1 &rest args) t))

    (let ((parts (split-string msg "\n"))
          (menu-1 (make-sparse-keymap "Attention"))
          c)

      (define-key menu-1 [menu-1-ok-event]
        `(menu-item ,(purecopy "OK")
                    ok
                    :keys ""))
      (define-key menu-1 [separator-1] menu-bar-separator)

      ;; add lines in reverse order
      (setq c (length parts))
      (while (> c 0)
        (setq c (1- c))
        (define-key menu-1 (vector (intern (format "menu-1-fake-event-%d" c)))
          `(menu-item ,(purecopy (nth c parts))
                      nil
                      :keys ""
                      :enable t)))
      (x-popup-menu t menu-1))))


(multiline-message-box "Hello!\nI must be going!\nThis is line 3.")