.emacs.d/elisp/init-func.el

185 lines
7.0 KiB
EmacsLisp

;;; 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: Fri Dec 27 01:02:58 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 '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)
(defun resize-window (width delta)
"Resize the current window's size. If WIDTH is non-nil, resize width by some DELTA."
(if (> (count-windows) 1)
(window-resize nil delta width)
(error "You need more than 1 window to execute this function!")))
;; Setup shorcuts for window resize width and height
(global-set-key (kbd "M-W =") (lambda () (interactive) (resize-window t 5)))
(global-set-key (kbd "M-W M-+") (lambda () (interactive) (resize-window t 5)))
(global-set-key (kbd "M-W -") (lambda () (interactive) (resize-window t -5)))
(global-set-key (kbd "M-W M-_") (lambda () (interactive) (resize-window t -5)))
(global-set-key (kbd "M-H =") (lambda () (interactive) (resize-window nil 5)))
(global-set-key (kbd "M-H M-+") (lambda () (interactive) (resize-window nil 5)))
(global-set-key (kbd "M-H -") (lambda () (interactive) (resize-window nil -5)))
(global-set-key (kbd "M-H M-_") (lambda () (interactive) (resize-window nil -5)))
;; -ResizeWidthheight
;; EditConfig
(defun edit-configs ()
"Opens the README.org file."
(interactive)
(find-file "~/.emacs.d/init.org"))
(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