]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-compat.el
emacs: Remove notmuch-setq-local
[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 'read-char-choice)
44     (defalias 'notmuch-read-char-choice 'read-char-choice)
45   (defun notmuch-read-char-choice (prompt chars &optional inhibit-keyboard-quit)
46     "Read and return one of CHARS, prompting for PROMPT.
47 Any input that is not one of CHARS is ignored.
48
49 If optional argument INHIBIT-KEYBOARD-QUIT is non-nil, ignore
50 keyboard-quit events while waiting for a valid input.
51
52 This is an exact copy of this function from emacs 24 for use on
53 emacs 23, except with the one emacs 24 only function it calls
54 inlined."
55     (unless (consp chars)
56       (error "Called `read-char-choice' without valid char choices"))
57     (let (char done show-help (helpbuf " *Char Help*"))
58       (let ((cursor-in-echo-area t)
59             (executing-kbd-macro executing-kbd-macro)
60             (esc-flag nil))
61         (save-window-excursion        ; in case we call help-form-show
62           (while (not done)
63             (unless (get-text-property 0 'face prompt)
64               (setq prompt (propertize prompt 'face 'minibuffer-prompt)))
65             (setq char (let ((inhibit-quit inhibit-keyboard-quit))
66                          (read-key prompt)))
67             (and show-help (buffer-live-p (get-buffer helpbuf))
68                  (kill-buffer helpbuf))
69             (cond
70              ((not (numberp char)))
71              ;; If caller has set help-form, that's enough.
72              ;; They don't explicitly have to add help-char to chars.
73              ((and help-form
74                    (eq char help-char)
75                    (setq show-help t)
76                    ;; This is an inlined copy of help-form-show as that
77                    ;; was introduced in emacs 24 too.
78                    (let ((msg (eval help-form)))
79                      (when (stringp msg)
80                        (with-output-to-temp-buffer " *Char Help*"
81                          (princ msg))))))
82              ((memq char chars)
83               (setq done t))
84              ((and executing-kbd-macro (= char -1))
85               ;; read-event returns -1 if we are in a kbd macro and
86               ;; there are no more events in the macro.  Attempt to
87               ;; get an event interactively.
88               (setq executing-kbd-macro nil))
89              ((not inhibit-keyboard-quit)
90               (cond
91                ((and (null esc-flag) (eq char ?\e))
92                 (setq esc-flag t))
93                ((memq char '(?\C-g ?\e))
94                 (keyboard-quit))))))))
95       ;; Display the question with the answer.  But without cursor-in-echo-area.
96       (message "%s%s" prompt (char-to-string char))
97       char)))
98
99 ;; End of compatibility functions
100
101 (provide 'notmuch-compat)
102
103 ;;; notmuch-compat.el ends here