Files
creature/lisp/init-func.el
T

171 lines
6.4 KiB
EmacsLisp

;; -*- coding: utf-8; lexical-binding: t; -*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; open file ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun creature/open-init-file ()
"Open init file."
(interactive)
(find-file (expand-file-name "init.el" creature/config-dir)))
(defun creature/open-in-external-app (file-path)
(if (eq system-type 'windows-nt)
(w32-shell-execute "open" (replace-regexp-in-string "/" "\\\\" file-path))
(call-process "xdg-open" nil 0 nil file-path)))
(defun creature/open-file-or-directory-in-external-app (arg)
(interactive "P")
(if arg
(creature/open-in-external-app (expand-file-name default-directory))
(let ((file-path (if (derived-mode-p 'dired-mode)
(dired-get-file-for-visit)
buffer-file-name)))
(if file-path
(creature/open-in-external-app file-path)
(message "No file associated to this buffer")))))
(defun creature/sudo-edit (&optional arg)
(interactive "P")
(let ((fname (if (or arg (not buffer-file-name))
(read-file-name "File: ")
buffer-file-name)))
(find-file
(cond ((string-match-p "^/ssh:" fname)
(with-temp-buffer
(insert fname)
(search-backward ":")
(let ((last-match-end nil)
(last-ssh-hostname nil))
(while (string-match "@\\\([^:|]+\\\)" fname last-match-end)
(setq last-ssh-hostname (or (match-string 1 fname)
last-ssh-hostname))
(setq last-match-end (match-end 0)))
(insert (format "|sudo:%s" (or last-ssh-hostname "localhost"))))
(buffer-string)))
(t (concat "/sudo:root@localhost:" fname))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; mpv ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun creature/is-bd-dir (dir)
(let ((index-file (file-name-concat dir "BDMV" "index.bdmv"))
(stream-dir (file-name-concat dir "BDMV" "STREAM"))
(playlist-dir (file-name-concat dir "BDMV" "PLAYLIST")))
(and (file-exists-p index-file)
(not (directory-empty-p stream-dir))
(not (directory-empty-p playlist-dir)))))
(defun creature/is-iso-file (filename)
(and (string-equal (downcase (or (file-name-extension filename) "")) "iso")
(file-exists-p filename)))
(defun creature/get-file-mime-type (file &optional deref-symlinks)
"Get the mime type of FILE, according to the `file' command.
If you give a prefix argument \\[universal-argument] to this command, and
FILE is a symbolic link, then the command will print the type
of the target of the link instead."
(interactive (list (dired-get-filename t t) current-prefix-arg))
(let (process-file-side-effects)
(with-temp-buffer
(if deref-symlinks
(process-file "file" nil t t "-L" "--brief" "--mime-type" "--" file)
(process-file "file" nil t t "--brief" "--mime-type" "--" file))
(when (bolp)
(delete-char -1))
(buffer-string))))
(defun creature/open-by-mpv (filename)
(cond
((creature/is-bd-dir filename)
(call-process "mpv" nil 0 nil filename))
((creature/is-iso-file filename)
(call-process "mpv" nil 0 nil
"bd://"
(concat "--bluray-device=" filename)))
(t
(call-process "mpv" nil 0 nil filename))))
(defun creature/mpv ()
(interactive)
(let ((filename (if (derived-mode-p 'dired-mode)
(dired-get-file-for-visit)
(buffer-file-name))))
(if (not filename)
(user-error "mpv: no file to play")
(if (tramp-tramp-file-p filename)
(user-error "mpv: cannot play remote file: %s" filename)
(let ((mime-type (creature/get-file-mime-type filename t)))
(if (not (and (stringp mime-type)
(or (string-prefix-p "audio/" mime-type t)
(string-prefix-p "video/" mime-type t))))
(user-error "mpv: cannot play '%s' file: %s" mime-type filename)
(creature/open-by-mpv filename)))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; indentation ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar creature/indent-sensitive-modes
'(asm-mode
coffee-mode
elm-mode
haml-mode
haskell-mode
slim-mode
makefile-mode
makefile-bsdmake-mode
makefile-gmake-mode
makefile-imake-mode
python-mode
yaml-mode)
"Modes which disable auto-indenting.")
(defun creature/fallback-indent-func ()
(unless (member major-mode creature/indent-sensitive-modes)
(save-excursion
(if (region-active-p)
(progn
(indent-region (region-beginning) (region-end))
(message "Indented selected region."))
(progn
(indent-region (point-min) (point-max))
(message "Indented buffer.")))
(whitespace-cleanup))))
(defun creature/indent-region-or-buffer ()
"Indent a region if selected, otherwise the whole buffer."
(interactive)
(if (and (bound-and-true-p prettier-mode)
(bound-and-true-p prettier-version)
(not (region-active-p)))
(progn
(save-excursion
(prettier-prettify))
(message "Indented prettier buffer."))
(creature/fallback-indent-func)))
(defvar creature/drink-timer nil
"Timer for drink water notifition.")
(defun creature/start-drink-timer ()
"Start timer for notice drink water."
(interactive)
(when (timerp creature/drink-timer)
(cancel-timer creature/drink-timer)
(setq creature/drink-timer nil))
(setq creature/drink-timer
(run-with-timer 1200 1200
(lambda ()
(require 'notifications)
(let ((msg (format-time-string "drink time: %Y-%m-%d %H:%M:%S")))
(notifications-notify :title msg :timeout 0 :app-icon nil))
))))
(defun creature/kill-this-buffer ()
"Kill the current buffer quietly."
(interactive)
(kill-buffer (current-buffer)))
(provide 'init-func)