blob: 36ac2e5f228073000ef299d25633fff65e5ffdcb (
about) (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
;;; anywhere-mode.el --- Anywhere mode
;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
;; This file is part of Emacs-Anywhere.
;; Emacs-Anywhere is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; Emacs-Anywhere is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Emacs-Anywhere.
;; If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This file provides `anywhere-create', the command to open
;; `anywhere-buffer'.
;;
;; Inspired by <https://github.com/zachcurry/emacs-anywhere>.
;;; Code:
(require 'subr-x)
(defcustom anywhere-buffer "*Anywhere*"
"Buffer name for anywhere-* commands."
:type 'string
:group 'anywhere-mode)
(defcustom anywhere-kill-buffer t
"Kill buffer in `anywhere-exit' function."
:type 'boolean
:group 'anywhere-mode)
(defcustom anywhere-kill-frame t
"Delete frame in `anywhere-exit' function."
:type 'boolean
:group 'anywhere-mode)
(defcustom anywhere-major-mode 'markdown-mode
"Major mode in `anywhere-buffer' buffer."
:type 'function
:group 'anywhere-mode)
(defvar anywhere-command
(mapconcat 'identity
(list "@XCLIP_BIN@"
"-selection" "clipboard" "&>" "/dev/null")
" "))
(defvar anywhere-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c . q") 'anywhere-exit)
(define-key map (kbd "C-c . l") 'anywhere-erase-buffer)
map)
"Key map used by `anywhere-mode'.")
(defun anywhere-mode-p (buffer)
"Return non-nil if BUFFER contains `anywhere-mode'."
(buffer-local-value 'anywhere-mode (get-buffer buffer)))
(defun anywhere-buffer-p (buffer)
"Return non-nil if BUFFER name has a `anywhere-buffer' prefix."
(string-prefix-p anywhere-buffer (buffer-name buffer)))
(defmacro anywhere-with-buffer (buffer &rest body)
"Return an error if BUFFER not an `anywhere-buffer' buffer
and `anywhere-mode' is not enabled for a BUFFER."
`(if (and (anywhere-buffer-p ,buffer) (anywhere-mode-p ,buffer))
(progn ,@body)
(error "Currently not in `anywhere-buffer' buffer\
or `anywhere-mode' is not enabled.")))
(defun anywhere-exit ()
"Copy `anywhere-buffer' to GUI's clipboard.
If `anywhere-kill-buffer' is non-nil kill current buffer.
If `anywhere-kill-frame' is non-nil delete current frame."
(interactive)
(let ((buffer (current-buffer)))
(anywhere-with-buffer buffer
(with-current-buffer buffer
(shell-command-on-region (point-min) (point-max)
anywhere-command))
(when anywhere-kill-buffer (kill-buffer buffer))
(when anywhere-kill-frame (delete-frame)))))
;;;###autoload
(defun anywhere-erase-buffer ()
"Erase current buffer."
(interactive)
(unless (string-blank-p
(filter-buffer-substring (point-min) (point-max)))
(erase-buffer)))
;;;###autoload
(define-minor-mode anywhere-mode
"Auxiliary minor mode used when editing `anywhere-buffer'."
nil " Anywhere" anywhere-mode-map
(setq-local header-line-format
(substitute-command-keys
"Edit, then exit with `\\[anywhere-exit]'.")))
;;;###autoload
(defun anywhere-create ()
"Create a `anywhere-buffer' buffer with `anywhere-mode' and
switch to it."
(let ((buffer (get-buffer-create anywhere-buffer)))
(switch-to-buffer buffer)
(anywhere-erase-buffer)
(funcall anywhere-major-mode)
(unless (anywhere-mode-p (current-buffer))
(anywhere-mode))))
(provide 'anywhere-mode)
;;; anywhere-mode.el ends here
|