aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/init/ycp-buffer.el
blob: 944a45e7ea5f54e242fdfe45ff59d032e4a9d0c7 (plain) (blame)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
;;; ycp-buffer.el -- My config for buffers, windows, frames etc. -*- lexical-binding: t -*-

;; Copyright (C) 2023 Free Software Foundation.

;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "28.2"))

;; This file is part of dotfiles.

;; dotfiles is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; dotfiles 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 Affero General
;; Public License for more details.

;; You should have received a copy of the GNU Affero General Public
;; License along with dotfiles.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; My config for buffers, windows, frames etc..

;;; Code:


(my-package my-buffer
  (:delay 15)
  (my-keybind global-map
    "\M-c" #'my-copy-buffer-file-name
    "<C-f2>" #'my-rename-file-and-buffer
    "C-x k" #'my-kill-buffer
    "M-s b" #'my-switch-to-buffer-matching-major-mode
    "M-s v" #'my-buffers-vc-root
    ;; F7: Cycle or create buffers with the same base buffer as the
    ;; current buffer
    "<f10>" #'my-create-or-switch-indirect-buffers
    ;; F8: Cycle or create buffers with the same major mode as the
    ;; current buffer
    "<f8>" #'my-buffer-create-or-cycle-same-mode
    ;; F9: Prompt for a mode, then switch to or create a buffer of
    ;; that mode
    "<f9>" #'my-buffer-switch-or-create-major-mode
    )
  )

(my-configure
  (:delay 15)
  (my-keybind ctl-x-x-map
    "f" #'follow-mode                   ; override `font-lock-update'
    "r" #'rename-uniquely
    "l" #'visual-line-mode)

  ;; make ibuffer default
  (advice-add 'list-buffers :override 'ibuffer-list-buffers)

  ;; In Emacs 27+, use Control + mouse wheel to scale text.
  (setq mouse-wheel-scroll-amount
        '(1
          ((shift) . 5)
          ((meta) . 0.5)
          ((control) . text-scale))
        mouse-drag-copy-region nil
        make-pointer-invisible t
        mouse-wheel-progressive-speed t
        mouse-wheel-follow-mouse t)

  ;; Scrolling behaviour
  (setq-default scroll-conservatively 1 ; affects `scroll-step'
                scroll-margin 0
                next-screen-context-lines 2)

  (mouse-wheel-mode 1)
  (define-key global-map (kbd "C-M-<mouse-3>") #'tear-off-window))

;;; Repeatable key chords (repeat-mode)
(my-package repeat
  (:delay 30)
  (setq repeat-on-final-keystroke t
        repeat-exit-timeout 5
        repeat-exit-key "<escape>"
        repeat-keep-prefix nil
        repeat-check-key t
        repeat-echo-function 'ignore
        ;; Technically, this is not in repeal.el, though it is the
        ;; same idea.
        set-mark-command-repeat-pop t)
  (repeat-mode 1))

;;;; Built-in bookmarking framework (bookmark.el)
(my-package bookmark
  (setq bookmark-use-annotations nil)
  (setq bookmark-automatically-show-annotations t)
  (add-hook 'bookmark-bmenu-mode-hook #'hl-line-mode))

(my-package follow
  (:delay 15)
  ;; TODO: update this to adapt to number of windows
  (my-keybind follow-mode-map
    "C-v" #'follow-scroll-up
    "M-v" #'follow-scroll-down
    "<next>" #'follow-scroll-up
    "<prior>" #'follow-scroll-down)
  )

(my-package view
  (:delay 10)
  (my-keybind view-mode-map
    "n" #'next-line
    "p" #'previous-line
    "f" #'forward-char
    "b" #'backward-char
    "e" #'move-end-of-line
    "a" #'move-beginning-of-line
    "v" #'scroll-up-command
    "V" #'scroll-down-command
    "l" #'previous-buffer
    "r" #'next-buffer
    "d" nil
    "u" nil
    "w" #'kill-ring-save
    "i" #'view-mode)
  (my-keybind global-map "C-`" #'view-mode))

;; move windows
(my-package windmove
  (:delay 5)
  (windmove-default-keybindings 'control)
  ;; swap windows
  (windmove-swap-states-default-keybindings '(control shift))
  (setq windmove-wrap-around t))

(my-package winner
  (:delay 5)
  (winner-mode t))

;;;; `window', `display-buffer-alist', and related
(my-package window
  (require 'time)
  (require 'my-buffer)
  (add-hook 'window-configuration-change-hook 'my-dedicate-windows-by-modes)
  (setq display-buffer-alist
        `(;; no window
          ("\\`\\*Async Shell Command\\*\\'"
           (display-buffer-no-window)
           (dedicated . t))
          ;; bottom side window
          ("\\*Org Select\\*"        ; the `org-capture' key selection
           (display-buffer-in-side-window)
           (dedicated . t)
           (side . bottom)
           (slot . 0)
           (window-parameters . ((mode-line-format . none))))
          ;; bottom buffer (NOT side window)
          ((or . (,(my-buffer-make-display-matcher
                    '(flymake-diagnostics-buffer-mode
                      flymake-project-diagnostics-mode
                      backtrace-mode))
                  "\\*\\(Warnings\\|Compile-Log\\|Org Links\\)\\*"
                  ,world-clock-buffer-name))
           (display-buffer-reuse-mode-window display-buffer-at-bottom)
           (window-height . 0.3)
           (dedicated . t)
           (preserve-size . (t . t)))
          ("\\*\\(Output\\|Register Preview\\).*"
           (display-buffer-reuse-mode-window display-buffer-at-bottom))
          ;; below current window
          ((derived-mode . help-mode) ; See the hooks for `visual-line-mode'
           (display-buffer-reuse-mode-window display-buffer-below-selected))
          ("\\*\\vc-\\(incoming\\|outgoing\\|git : \\).*"
           (display-buffer-reuse-mode-window display-buffer-below-selected)
           (window-height . 0.1)
           (dedicated . t)
           (preserve-size . (t . t)))
          ((derived-mode . log-view-mode)
           (display-buffer-reuse-mode-window display-buffer-below-selected)
           (window-height . 0.3)
           (dedicated . t)
           (preserve-size . (t . t)))
          ((derived-mode . reb-mode)    ; M-x re-builder
           (display-buffer-reuse-mode-window display-buffer-below-selected)
           (window-height . 4) ; note this is literal lines, not relative
           (dedicated . t)
           (preserve-size . (t . t)))
          ("\\*\\(Calendar\\|Bookmark Annotation\\).*"
           (display-buffer-reuse-mode-window display-buffer-below-selected)
           (dedicated . t)
           (window-height . fit-window-to-buffer))
          ("\\*ispell-top-choices\\*.*"
           (display-buffer-reuse-mode-window display-buffer-below-selected)
           (window-height . fit-window-to-buffer))
          ;; same window

          ;; NOTE 2023-02-17: `man' does not fully obey the
          ;; `display-buffer-alist'.  It works for new frames and for
          ;; `display-buffer-below-selected', but otherwise is
          ;; unpredictable.  See `Man-notify-method'.
          ((or . ((derived-mode . Man-mode)
                  (derived-mode . woman-mode)
                  "\\*\\(Man\\|woman\\).*"
                  "\\*shell\\*.*"))
           (display-buffer-same-window))
          ))

  (setq switch-to-buffer-in-dedicated-window 'pop)
  (setq window-combination-resize nil)
  (my-keybind resize-window-repeat-map
    ">" #'enlarge-window-horizontally
    "<" #'shrink-window-horizontally)
  )

(my-keybind global-map
  "C-x C-n" #'next-buffer     ; override `set-goal-column'
  "C-x C-p" #'previous-buffer ; override `mark-page'
  "C-x !" #'delete-other-windows-vertically
  "C-x -" #'fit-window-to-buffer ; hyphen
  "C-x }" #'enlarge-window
  "C-x {" #'shrink-window
  "C-x >" #'enlarge-window-horizontally ; override `scroll-right'
  "C-x <" #'shrink-window-horizontally) ; override `scroll-left'

(my-package my-buffer
  (:delay 10)
  (my-keybind global-map
    "<f1>" #'my-toggle-focus-write
    "<insert>" #'my-cycle-windows
    "C-M-<mouse-4>" #'my-increase-default-face-height
    "C-M-<mouse-5>" #'my-decrease-default-face-height)
  )

(setq large-file-warning-threshold 15000000)

(provide 'ycp-buffer)