]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-compat.el
emacs: Increase consistency of library headers
[notmuch] / emacs / notmuch-compat.el
1 ;;; notmuch-compat.el --- 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 ;; This file is part of Notmuch.
8 ;;
9 ;; Notmuch is free software: you can redistribute it and/or modify it
10 ;; under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13 ;;
14 ;; Notmuch is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18 ;;
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with Notmuch.  If not, see <https://www.gnu.org/licenses/>.
21
22 ;;; Code:
23
24 ;; emacs master has a bugfix for folding long headers when sending
25 ;; messages. Include the fix for earlier versions of emacs. To avoid
26 ;; interfering with gnus we only run the hook when called from
27 ;; notmuch-message-mode.
28
29 (declare-function mail-header-fold-field "mail-parse" nil)
30
31 (defun notmuch-message--fold-long-headers ()
32   (when (eq major-mode 'notmuch-message-mode)
33     (goto-char (point-min))
34     (while (not (eobp))
35       (when (and (looking-at "[^:]+:")
36                  (> (- (line-end-position) (point)) 998))
37         (mail-header-fold-field))
38       (forward-line 1))))
39
40 (unless (fboundp 'message--fold-long-headers)
41   (add-hook 'message-header-hook 'notmuch-message--fold-long-headers))
42
43 (if (fboundp 'setq-local)
44     (defalias 'notmuch-setq-local 'setq-local)
45   (defmacro notmuch-setq-local (var val)
46     "Set variable VAR to value VAL in current buffer.
47
48 Backport of setq-local for emacs without setq-local (pre 24.3)."
49     `(set (make-local-variable ',var) ,val)))
50
51 (if (fboundp 'read-char-choice)
52     (defalias 'notmuch-read-char-choice 'read-char-choice)
53   (defun notmuch-read-char-choice (prompt chars &optional inhibit-keyboard-quit)
54     "Read and return one of CHARS, prompting for PROMPT.
55 Any input that is not one of CHARS is ignored.
56
57 If optional argument INHIBIT-KEYBOARD-QUIT is non-nil, ignore
58 keyboard-quit events while waiting for a valid input.
59
60 This is an exact copy of this function from emacs 24 for use on
61 emacs 23, except with the one emacs 24 only function it calls
62 inlined."
63     (unless (consp chars)
64       (error "Called `read-char-choice' without valid char choices"))
65     (let (char done show-help (helpbuf " *Char Help*"))
66       (let ((cursor-in-echo-area t)
67             (executing-kbd-macro executing-kbd-macro)
68             (esc-flag nil))
69         (save-window-excursion        ; in case we call help-form-show
70           (while (not done)
71             (unless (get-text-property 0 'face prompt)
72               (setq prompt (propertize prompt 'face 'minibuffer-prompt)))
73             (setq char (let ((inhibit-quit inhibit-keyboard-quit))
74                          (read-key prompt)))
75             (and show-help (buffer-live-p (get-buffer helpbuf))
76                  (kill-buffer helpbuf))
77             (cond
78              ((not (numberp char)))
79              ;; If caller has set help-form, that's enough.
80              ;; They don't explicitly have to add help-char to chars.
81              ((and help-form
82                    (eq char help-char)
83                    (setq show-help t)
84                    ;; This is an inlined copy of help-form-show as that
85                    ;; was introduced in emacs 24 too.
86                    (let ((msg (eval help-form)))
87                      (when (stringp msg)
88                        (with-output-to-temp-buffer " *Char Help*"
89                          (princ msg))))))
90              ((memq char chars)
91               (setq done t))
92              ((and executing-kbd-macro (= char -1))
93               ;; read-event returns -1 if we are in a kbd macro and
94               ;; there are no more events in the macro.  Attempt to
95               ;; get an event interactively.
96               (setq executing-kbd-macro nil))
97              ((not inhibit-keyboard-quit)
98               (cond
99                ((and (null esc-flag) (eq char ?\e))
100                 (setq esc-flag t))
101                ((memq char '(?\C-g ?\e))
102                 (keyboard-quit))))))))
103       ;; Display the question with the answer.  But without cursor-in-echo-area.
104       (message "%s%s" prompt (char-to-string char))
105       char)))
106
107 ;; End of compatibility functions
108
109 (provide 'notmuch-compat)