-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcfrs.el
179 lines (152 loc) · 6.36 KB
/
cfrs.el
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
;;; cfrs.el --- Child-frame based read-string -*- lexical-binding: t -*-
;; Copyright (C) 2021 Alexander Miller
;; Author: Alexander Miller <[email protected]>
;; Package-Requires: ((emacs "26.1") (dash "2.11.0") (s "1.10.0") (posframe "0.6.0"))
;; Package-Version: 1.6.0
;; Homepage: https://github.com/Alexander-Miller/cfrs
;; This program 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.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Simple implementation of reading a string with child-frames.
;;; Synchronous control is maintained by using `recursive-edit'. When finished
;;; the entered text is read from the input buffer and the child-frame is
;;; hidden.
;;; Code:
(require 's)
(require 'dash)
(require 'posframe)
(defgroup cfrs nil
"Cfrs configuration options."
:group 'cfrs
:prefix "cfrs-")
(defcustom cfrs-frame-parameters nil
"Alist of parameters for cfrs' child frames.
Can be used to override useful parameters like `internal-border-width' or
`background-color' for better frame visibility."
:type '(alist :key-type symbol)
:group 'cfrs)
(defcustom cfrs-max-width 80
"The maximum width of the cfrs input field.
cfrs will try to extend its initial width to fit both the prompt and the initial
input, up to a maximum of `cfrs-max-width' characters. For any combination
longer than this horizontal scrolling will be necessary.
See also `cfrs-min-width'"
:type 'integer
:group 'cfrs)
(defcustom cfrs-min-width 40
"The minimum width of the cfrs input field.
cfrs will never be smaller than `cfrs-min-width' characters regardless of the
length of the prompt and initial input.
See also `cfrs-max-width'"
:type 'integer
:group 'cfrs)
(defface cfrs-border-color
`((t :inherit internal-border))
"The face for the border of the cfrs popup frame.
Only the `:background' part is used."
:group 'cfrs)
;;;###autoload
(defun cfrs-read (prompt &optional initial-input)
"Read a string using a pos-frame with given PROMPT and INITIAL-INPUT."
(if (not (or (display-graphic-p)
(not (fboundp #'display-buffer-in-side-window))))
(read-string prompt initial-input)
(let* ((buffer (get-buffer-create " *Pos-Frame-Read*"))
(border-color (face-attribute 'cfrs-border-color :background nil t))
(cursor (cfrs--determine-cursor-type))
(width (+ 2 ;; extra space for margin and cursor
(min cfrs-max-width
(max cfrs-min-width
(+ (length prompt)
(if initial-input (length initial-input) 0))))))
(frame (posframe-show
buffer
:min-height 1
:min-width width
:internal-border-width 2
:internal-border-color border-color
:string ""
:accept-focus t
:override-parameters `(,@cfrs-frame-parameters
(cursor-type . ,cursor)))))
(with-selected-frame frame
(select-frame frame)
(x-focus-frame frame)
(add-hook 'delete-frame-functions #'cfrs--on-frame-kill nil :local)
(with-current-buffer buffer
(cfrs-input-mode)
(-each (overlays-in (point-min) (point-max)) #'delete-overlay)
(erase-buffer)
(-doto (make-overlay 1 2)
(overlay-put
'before-string
(propertize (concat " " prompt) 'face 'minibuffer-prompt))
(overlay-put 'rear-nonsticky t)
(overlay-put 'read-only t))
(when initial-input
(insert initial-input))
(when (and (bound-and-true-p evil-mode)
(fboundp 'evil-insert-state))
(evil-insert-state nil))
(end-of-line)
(recursive-edit)
(cfrs--hide)
(s-trim (buffer-string)))))))
(defun cfrs--determine-cursor-type ()
"Determine the cursor type for the popup frame.
Prevents showing an invisible cursor with a height or width of 0."
(let ((ct (if (memq cursor-type '(t nil))
(frame-parameter (selected-frame) 'cursor-type)
cursor-type)))
(pcase ct
(`(,_ . 0) ct)
(`nil 'hbar)
(_ ct))))
(defun cfrs--hide ()
"Hide the current cfrs frame."
(when (eq major-mode 'cfrs-input-mode)
(posframe-hide (current-buffer))
(x-focus-frame (frame-parent (selected-frame)))))
(defun cfrs--adjust-height ()
"Adjust input frame's height to the number of lines in the buffer."
(set-frame-height (selected-frame) (count-lines (point-min) (point-max))))
(defun cfrs--on-frame-kill (frame)
"Redirect focus after FRAME is killed."
(-let [parent (or (frame-parent frame) (selected-frame))]
(x-focus-frame parent)))
(defun cfrs-finish ()
"Finish the cfrs read, returning the entered string."
(interactive)
;; XXX: workaround for persp believing we are in a different frame
;; and need a new perspective when the recursive edit ends
(set-frame-parameter (selected-frame) 'persp--recursive nil)
(exit-recursive-edit))
(defun cfrs-cancel ()
"Cancel the `cfrs-read' call and the function that called it."
(interactive)
(cfrs--hide)
(abort-recursive-edit))
(defvar cfrs-input-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") #'cfrs-finish)
(define-key map [return] #'cfrs-finish)
(define-key map [remap keyboard-quit] #'cfrs-cancel)
map))
(define-derived-mode cfrs-input-mode fundamental-mode "Child Frame Read String"
"Simple mode for buffers displayed in cfrs's input frames."
(add-hook 'post-command-hook #'cfrs--adjust-height nil :local)
(display-line-numbers-mode -1))
;; https://github.com/Alexander-Miller/treemacs/issues/775
(with-eval-after-load 'beacon
(with-no-warnings
(add-to-list 'beacon-dont-blink-major-modes 'cfrs-input-mode)))
(provide 'cfrs)
;;; cfrs.el ends here