]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-compat.el
emacs: Use 'when' instead of 'if' when there is no ELSE part
[notmuch] / emacs / notmuch-compat.el
1 ;; Compatibility functions for earlier versions of emacs
2
3 ;; The functions in this file are copied from more modern versions of
4 ;; emacs and are Copyright (C) 1985-1986, 1992, 1994-1995, 1999-2017
5 ;; Free Software Foundation, Inc.
6
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;; emacs master has a bugfix for folding long headers when sending
9 ;; messages. Include the fix for earlier versions of emacs. To avoid
10 ;; interfering with gnus we only run the hook when called from
11 ;; notmuch-message-mode.
12
13 (declare-function mail-header-fold-field "mail-parse" nil)
14
15 (defun notmuch-message--fold-long-headers ()
16   (when (eq major-mode 'notmuch-message-mode)
17     (goto-char (point-min))
18     (while (not (eobp))
19       (when (and (looking-at "[^:]+:")
20                  (> (- (line-end-position) (point)) 998))
21         (mail-header-fold-field))
22       (forward-line 1))))
23
24 (unless (fboundp 'message--fold-long-headers)
25   (add-hook 'message-header-hook 'notmuch-message--fold-long-headers))
26
27 (if (fboundp 'setq-local)
28     (defalias 'notmuch-setq-local 'setq-local)
29   (defmacro notmuch-setq-local (var val)
30     "Set variable VAR to value VAL in current buffer.
31
32 Backport of setq-local for emacs without setq-local (pre 24.3)."
33     `(set (make-local-variable ',var) ,val)))
34
35 (if (fboundp 'read-char-choice)
36     (defalias 'notmuch-read-char-choice 'read-char-choice)
37   (defun notmuch-read-char-choice (prompt chars &optional inhibit-keyboard-quit)
38     "Read and return one of CHARS, prompting for PROMPT.
39 Any input that is not one of CHARS is ignored.
40
41 If optional argument INHIBIT-KEYBOARD-QUIT is non-nil, ignore
42 keyboard-quit events while waiting for a valid input.
43
44 This is an exact copy of this function from emacs 24 for use on
45 emacs 23, except with the one emacs 24 only function it calls
46 inlined."
47     (unless (consp chars)
48       (error "Called `read-char-choice' without valid char choices"))
49     (let (char done show-help (helpbuf " *Char Help*"))
50       (let ((cursor-in-echo-area t)
51             (executing-kbd-macro executing-kbd-macro)
52             (esc-flag nil))
53         (save-window-excursion        ; in case we call help-form-show
54           (while (not done)
55             (unless (get-text-property 0 'face prompt)
56               (setq prompt (propertize prompt 'face 'minibuffer-prompt)))
57             (setq char (let ((inhibit-quit inhibit-keyboard-quit))
58                          (read-key prompt)))
59             (and show-help (buffer-live-p (get-buffer helpbuf))
60                  (kill-buffer helpbuf))
61             (cond
62              ((not (numberp char)))
63              ;; If caller has set help-form, that's enough.
64              ;; They don't explicitly have to add help-char to chars.
65              ((and help-form
66                    (eq char help-char)
67                    (setq show-help t)
68                    ;; This is an inlined copy of help-form-show as that
69                    ;; was introduced in emacs 24 too.
70                    (let ((msg (eval help-form)))
71                      (when (stringp msg)
72                        (with-output-to-temp-buffer " *Char Help*"
73                          (princ msg))))))
74              ((memq char chars)
75               (setq done t))
76              ((and executing-kbd-macro (= char -1))
77               ;; read-event returns -1 if we are in a kbd macro and
78               ;; there are no more events in the macro.  Attempt to
79               ;; get an event interactively.
80               (setq executing-kbd-macro nil))
81              ((not inhibit-keyboard-quit)
82               (cond
83                ((and (null esc-flag) (eq char ?\e))
84                 (setq esc-flag t))
85                ((memq char '(?\C-g ?\e))
86                 (keyboard-quit))))))))
87       ;; Display the question with the answer.  But without cursor-in-echo-area.
88       (message "%s%s" prompt (char-to-string char))
89       char)))
90
91 ;; End of compatibility functions
92
93 (provide 'notmuch-compat)