-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinit-func.el
167 lines (150 loc) · 6.04 KB
/
init-func.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
;;; init-func.el --- -*- lexical-binding: t -*-
;;
;; Filename: init-func.el
;; Description: Initialize Functions
;; Author: Mingde (Matthew) Zeng
;; Copyright (C) 2019 Mingde (Matthew) Zeng
;; Created: Sun Jun 9 17:53:44 2019 (-0400)
;; Version: 2.0.0
;; Last-Updated: Tue Dec 24 14:22:38 2019 (-0500)
;; By: Mingde (Matthew) Zeng
;; URL: https://github.com/MatthewZMD/.emacs.d
;; Keywords: M-EMACS .emacs.d
;; Compatibility: emacs-version >= 26.1
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; This file initializes functions
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(eval-when-compile
(require '00-init-global-config))
;; ResizeWidthHeight
;; Resizes the window width based on the input
(defun resize-window-width (w)
"Resizes the window width based on W."
(interactive (list (if (> (count-windows) 1)
(read-number "Set the current window width in [1~9]x10%: ")
(error "You need more than 1 window to execute this function!"))))
(message "%s" w)
(window-resize nil (- (truncate (* (/ w 10.0) (frame-width))) (window-total-width)) t))
;; Resizes the window height based on the input
(defun resize-window-height (h)
"Resizes the window height based on H."
(interactive (list (if (> (count-windows) 1)
(read-number "Set the current window height in [1~9]x10%: ")
(error "You need more than 1 window to execute this function!"))))
(message "%s" h)
(window-resize nil (- (truncate (* (/ h 10.0) (frame-height))) (window-total-height)) nil))
;; Setup shorcuts for window resize width and height
(global-set-key (kbd "C-z w") #'resize-window-width)
(global-set-key (kbd "C-z h") #'resize-window-height)
;; -ResizeWidthheight
;; EditConfig
(defun edit-configs ()
"Opens the README.org file."
(interactive)
(find-file "~/.emacs.d/personal"))
(global-set-key (kbd "C-z e") #'edit-configs)
;; -EditConfig
;; OrgIncludeAuto
(defun save-and-update-includes ()
"Update the line numbers of #+INCLUDE:s in current buffer.
Only looks at INCLUDEs that have either :range-begin or :range-end.
This function does nothing if not in `org-mode', so you can safely
add it to `before-save-hook'."
(interactive)
(when (derived-mode-p 'org-mode)
(save-excursion
(goto-char (point-min))
(while (search-forward-regexp
"^\\s-*#\\+INCLUDE: *\"\\([^\"]+\\)\".*:range-\\(begin\\|end\\)"
nil 'noerror)
(let* ((file (expand-file-name (match-string-no-properties 1)))
lines begin end)
(forward-line 0)
(when (looking-at "^.*:range-begin *\"\\([^\"]+\\)\"")
(setq begin (match-string-no-properties 1)))
(when (looking-at "^.*:range-end *\"\\([^\"]+\\)\"")
(setq end (match-string-no-properties 1)))
(setq lines (decide-line-range file begin end))
(when lines
(if (looking-at ".*:lines *\"\\([-0-9]+\\)\"")
(replace-match lines :fixedcase :literal nil 1)
(goto-char (line-end-position))
(insert " :lines \"" lines "\""))))))))
(add-hook 'before-save-hook #'save-and-update-includes)
(defun decide-line-range (file begin end)
"Visit FILE and decide which lines to include.
BEGIN and END are regexps which define the line range to use."
(let (l r)
(save-match-data
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(if (null begin)
(setq l "")
(search-forward-regexp begin)
(setq l (line-number-at-pos (match-beginning 0))))
(if (null end)
(setq r "")
(search-forward-regexp end)
(setq r (1+ (line-number-at-pos (match-end 0)))))
(format "%s-%s" (+ l 1) (- r 1)))))) ;; Exclude wrapper
;; -OrgIncludeAuto
;; BetterMiniBuffer
(defun abort-minibuffer-using-mouse ()
"Abort the minibuffer when using the mouse."
(when (and (>= (recursion-depth) 1) (active-minibuffer-window))
(abort-recursive-edit)))
(add-hook 'mouse-leave-buffer-hook 'abort-minibuffer-using-mouse)
;; keep the point out of the minibuffer
(setq-default minibuffer-prompt-properties '(read-only t point-entered minibuffer-avoid-prompt face minibuffer-prompt))
;; -BetterMiniBuffer
;; DisplayLineOverlay
(defun display-line-overlay+ (pos str &optional face)
"Display line at POS as STR with FACE.
FACE defaults to inheriting from default and highlight."
(let ((ol (save-excursion
(goto-char pos)
(make-overlay (line-beginning-position)
(line-end-position)))))
(overlay-put ol 'display str)
(overlay-put ol 'face
(or face '(:background null :inherit highlight)))
ol))
;; -DisplayLineOverlay
;; ReadLines
(defun read-lines (filePath)
"Return a list of lines of a file at FILEPATH."
(with-temp-buffer (insert-file-contents filePath)
(split-string (buffer-string) "\n" t)))
;; -ReadLines
;; WhereAmI
(defun where-am-i ()
"An interactive function showing function `buffer-file-name' or `buffer-name'."
(interactive)
(message (kill-new (if (buffer-file-name) (buffer-file-name) (buffer-name)))))
;; -WhereAmI
(provide 'init-func)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; init-func.el ends here