Reorganize doom core-lib & reformat autoload/ui.el

+ Move doom-files-in to autoload/files.el
+ Move doom*shut-up to autoload/ui.el
+ Reorganize autoload/ui.el
This commit is contained in:
Henrik Lissner 2018-09-07 19:38:16 -04:00
parent 7d3ffdff06
commit be29623f0d
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
4 changed files with 168 additions and 144 deletions

View file

@ -279,8 +279,7 @@ Respects `require-final-newline'."
executed from a commented line; handling special cases for certain languages
with weak native support."
(interactive)
(cond ((sp-point-in-string)
(newline))
(cond ((sp-point-in-string) (newline))
((and (sp-point-in-comment)
comment-line-break-function)
(funcall comment-line-break-function))

View file

@ -1,24 +1,86 @@
;;; core/autoload/files.el -*- lexical-binding: t; -*-
;;;###autoload
(defun doom/sudo-find-file (file)
"Open FILE as root."
(interactive
(list (read-file-name "Open as root: ")))
(when (file-writable-p file)
(user-error "File is user writeable, aborting sudo"))
(find-file (if (file-remote-p file)
(concat "/" (file-remote-p file 'method) ":" (file-remote-p file 'user) "@" (file-remote-p file 'host) "|sudo:root@" (file-remote-p file 'host) ":" (file-remote-p file 'localname))
(concat "/sudo:root@localhost:" file))))
;;
;; Public library
;;;###autoload
(defun doom/sudo-this-file ()
"Open the current file as root."
(interactive)
(doom/sudo-find-file (file-truename buffer-file-name)))
(cl-defun doom-files-in
(path-or-paths &rest rest
&key
filter
map
full
nosort
(follow-symlinks t)
(type 'files)
(relative-to (unless full default-directory))
(depth 99999)
(mindepth 0)
(match "/[^.]"))
"Returns a list of files/directories in PATH-OR-PATHS (one string path or a
list of them).
FILTER is a function or symbol that takes one argument (the path). If it returns
non-nil, the entry will be excluded.
MAP is a function or symbol which will be used to transform each entry in the
results.
TYPE determines what kind of path will be included in the results. This can be t
(files and folders), 'files or 'dirs.
By default, this function returns paths relative to PATH-OR-PATHS if it is a
single path. If it a list of paths, this function returns absolute paths.
Otherwise, by setting RELATIVE-TO to a path, the results will be transformed to
be relative to it.
The search recurses up to DEPTH and no further. DEPTH is an integer.
MATCH is a string regexp. Only entries that match it will be included."
(cond
((listp path-or-paths)
(cl-loop for path in path-or-paths
if (file-directory-p path)
nconc (apply #'doom-files-in path (plist-put rest :relative-to relative-to))))
((let ((path path-or-paths)
result)
(when (file-directory-p path)
(dolist (file (directory-files path nil "." nosort))
(unless (member file '("." ".."))
(let ((fullpath (expand-file-name file path)))
(cond ((file-directory-p fullpath)
(when (and (memq type '(t dirs))
(string-match-p match fullpath)
(not (and filter (funcall filter fullpath)))
(not (and (file-symlink-p fullpath)
(not follow-symlinks)))
(<= mindepth 0))
(setq result
(nconc result
(list (cond (map (funcall map fullpath))
(relative-to (file-relative-name fullpath relative-to))
(fullpath))))))
(unless (< depth 1)
(setq result
(nconc result (apply #'doom-files-in fullpath
(append `(:mindepth ,(1- mindepth)
:depth ,(1- depth)
:relative-to ,relative-to)
rest))))))
((and (memq type '(t files))
(string-match-p match fullpath)
(not (and filter (funcall filter fullpath)))
(<= mindepth 0))
(push (if relative-to
(file-relative-name fullpath relative-to)
fullpath)
result))))))
result)))))
;;
;; Helpers
(defun doom--forget-file (old-path &optional new-path)
"Ensure `recentf', `projectile' and `save-place' forget OLD-PATH."
(when (bound-and-true-p recentf-mode)
@ -130,3 +192,19 @@ file if it exists, without confirmation."
(`aborted (message "Aborted"))
(_ t)))
;;;###autoload
(defun doom/sudo-find-file (file)
"Open FILE as root."
(interactive
(list (read-file-name "Open as root: ")))
(when (file-writable-p file)
(user-error "File is user writeable, aborting sudo"))
(find-file (if (file-remote-p file)
(concat "/" (file-remote-p file 'method) ":" (file-remote-p file 'user) "@" (file-remote-p file 'host) "|sudo:root@" (file-remote-p file 'host) ":" (file-remote-p file 'localname))
(concat "/sudo:root@localhost:" file))))
;;;###autoload
(defun doom/sudo-this-file ()
"Open the current file as root."
(interactive)
(doom/sudo-find-file (file-truename buffer-file-name)))

View file

@ -1,5 +1,56 @@
;;; core/autoload/ui.el -*- lexical-binding: t; -*-
;;
;; Public library
;;;###autoload
(defun doom-resize-window (window new-size &optional horizontal force-p)
"Resize a window to NEW-SIZE. If HORIZONTAL, do it width-wise.
If FORCE-P is omitted when `window-size-fixed' is non-nil, resizing will fail."
(with-selected-window (or window (selected-window))
(let ((window-size-fixed (unless force-p window-size-fixed)))
(enlarge-window (- new-size (if horizontal (window-width) (window-height)))
horizontal))))
;;;###autoload
(defun doom-quit-p (&optional prompt)
"Prompt the user for confirmation when killing Emacs.
Returns t if it is safe to kill this session. Does not prompt if no real buffers
are open."
(or (not (ignore-errors (doom-real-buffer-list)))
(yes-or-no-p (format " %s" (or prompt "Quit Emacs?")))
(ignore (message "Aborted"))))
;;
;; Advice
;;;###autoload
(defun doom*recenter (&rest _)
"Generic advisor for recentering window (typically :after other functions)."
(recenter))
;;;###autoload
(defun doom*shut-up (orig-fn &rest args)
"Generic advisor for silencing noisy functions."
(quiet! (apply orig-fn args)))
;;
;; Hooks
;;;###autoload
(defun doom|apply-ansi-color-to-compilation-buffer ()
"Applies ansi codes to the compilation buffers. Meant for
`compilation-filter-hook'."
(with-silent-modifications
(ansi-color-apply-on-region compilation-filter-start (point))))
;;
;; Commands
;;;###autoload
(defun doom/toggle-line-numbers ()
"Toggle line numbers.
@ -32,13 +83,23 @@ See `display-line-numbers' for what these values mean."
(x (symbol-name next)))))))
;;;###autoload
(defun doom-resize-window (window new-size &optional horizontal force-p)
"Resize a window to NEW-SIZE. If HORIZONTAL, do it width-wise.
If FORCE-P is omitted when `window-size-fixed' is non-nil, resizing will fail."
(with-selected-window (or window (selected-window))
(let ((window-size-fixed (unless force-p window-size-fixed)))
(enlarge-window (- new-size (if horizontal (window-width) (window-height)))
horizontal))))
(defun doom/reload-theme ()
"Reset the current color theme and fonts."
(interactive)
(let ((theme (or (car-safe custom-enabled-themes) doom-theme)))
(when theme
(mapc #'disable-theme custom-enabled-themes))
(doom|init-theme)
(doom|init-fonts)))
;;;###autoload
(defun doom/delete-frame ()
"Delete the current frame, but ask for confirmation if it isn't empty."
(interactive)
(if (cdr (frame-list))
(when (doom-quit-p "Close frame?")
(delete-frame))
(save-buffers-kill-emacs)))
;;;###autoload
(defun doom/window-zoom ()
@ -81,14 +142,9 @@ windows (unlike `doom/window-zoom') Activate again to undo."
(maximize-window))
t)))
;;;###autoload
(defun doom/delete-frame ()
"Delete the current frame, but ask for confirmation if it isn't empty."
(interactive)
(if (cdr (frame-list))
(when (doom-quit-p "Close frame?")
(delete-frame))
(save-buffers-kill-emacs)))
;;
;; Modes
;;;###autoload
(define-minor-mode doom-big-font-mode
@ -102,34 +158,3 @@ presentations."
(if doom-big-font-mode
(set-frame-font doom-big-font t t)
(set-frame-font doom-font t t)))
;;;###autoload
(defun doom/reload-theme ()
"Reset the current color theme and fonts."
(interactive)
(let ((theme (or (car-safe custom-enabled-themes) doom-theme)))
(when theme
(mapc #'disable-theme custom-enabled-themes))
(doom|init-theme)
(doom|init-fonts)))
;;;###autoload
(defun doom*recenter (&rest _)
(recenter))
;;;###autoload
(defun doom-quit-p (&optional prompt)
"Prompt the user for confirmation when killing Emacs.
Returns t if it is safe to kill this session. Does not prompt if no real buffers
are open."
(or (not (ignore-errors (doom-real-buffer-list)))
(yes-or-no-p (format " %s" (or prompt "Quit Emacs?")))
(ignore (message "Aborted"))))
;;;###autoload
(defun doom|apply-ansi-color-to-compilation-buffer ()
"Applies ansi codes to the compilation buffers. Meant for
`compilation-filter-hook'."
(with-silent-modifications
(ansi-color-apply-on-region compilation-filter-start (point))))

View file

@ -118,88 +118,6 @@ This is used by `associate!', `file-exists-p!' and `project-file-exists-p!'."
(cl-check-type :test keyword)
(substring (symbol-name keyword) 1))
(cl-defun doom-files-in
(path-or-paths &rest rest
&key
filter
map
full
nosort
(follow-symlinks t)
(type 'files)
(relative-to (unless full default-directory))
(depth 99999)
(mindepth 0)
(match "/[^.]"))
"Returns a list of files/directories in PATH-OR-PATHS (one string path or a
list of them).
FILTER is a function or symbol that takes one argument (the path). If it returns
non-nil, the entry will be excluded.
MAP is a function or symbol which will be used to transform each entry in the
results.
TYPE determines what kind of path will be included in the results. This can be t
(files and folders), 'files or 'dirs.
By default, this function returns paths relative to PATH-OR-PATHS if it is a
single path. If it a list of paths, this function returns absolute paths.
Otherwise, by setting RELATIVE-TO to a path, the results will be transformed to
be relative to it.
The search recurses up to DEPTH and no further. DEPTH is an integer.
MATCH is a string regexp. Only entries that match it will be included."
(cond
((listp path-or-paths)
(cl-loop for path in path-or-paths
if (file-directory-p path)
nconc (apply #'doom-files-in path (plist-put rest :relative-to relative-to))))
((let ((path path-or-paths)
result)
(when (file-directory-p path)
(dolist (file (directory-files path nil "." nosort))
(unless (member file '("." ".."))
(let ((fullpath (expand-file-name file path)))
(cond ((file-directory-p fullpath)
(when (and (memq type '(t dirs))
(string-match-p match fullpath)
(not (and filter (funcall filter fullpath)))
(not (and (file-symlink-p fullpath)
(not follow-symlinks)))
(<= mindepth 0))
(setq result
(nconc result
(list (cond (map (funcall map fullpath))
(relative-to (file-relative-name fullpath relative-to))
(fullpath))))))
(unless (< depth 1)
(setq result
(nconc result (apply #'doom-files-in fullpath
(append `(:mindepth ,(1- mindepth)
:depth ,(1- depth)
:relative-to ,relative-to)
rest))))))
((and (memq type '(t files))
(string-match-p match fullpath)
(not (and filter (funcall filter fullpath)))
(<= mindepth 0))
(push (if relative-to
(file-relative-name fullpath relative-to)
fullpath)
result))))))
result)))))
(defun doom*shut-up (orig-fn &rest args)
"Generic advisor for silencing noisy functions."
(quiet! (apply orig-fn args)))
;;
;; Macros
;;
(defun FILE! ()
"Return the emacs lisp file this macro is called from."
(cond ((bound-and-true-p byte-compile-current-file))
@ -212,6 +130,10 @@ MATCH is a string regexp. Only entries that match it will be included."
(let ((file (FILE!)))
(and file (file-name-directory file))))
;;
;; Macros
(defmacro λ! (&rest body)
"A shortcut for inline interactive lambdas."
(declare (doc-string 1))