Moving to Doom Emacs!

This commit is contained in:
Derek Taylor
2019-12-16 20:21:19 -06:00
parent d9f2f456f1
commit d4b4c33550
683 changed files with 51877 additions and 100 deletions

View File

@@ -0,0 +1,370 @@
;;; core/autoload/buffers.el -*- lexical-binding: t; -*-
;;;###autoload
(defvar doom-real-buffer-functions
'(doom-dired-buffer-p)
"A list of predicate functions run to determine if a buffer is real, unlike
`doom-unreal-buffer-functions'. They are passed one argument: the buffer to be
tested.
Should any of its function returns non-nil, the rest of the functions are
ignored and the buffer is considered real.
See `doom-real-buffer-p' for more information.")
;;;###autoload
(defvar doom-unreal-buffer-functions
'(minibufferp doom-special-buffer-p doom-non-file-visiting-buffer-p)
"A list of predicate functions run to determine if a buffer is *not* real,
unlike `doom-real-buffer-functions'. They are passed one argument: the buffer to
be tested.
Should any of these functions return non-nil, the rest of the functions are
ignored and the buffer is considered unreal.
See `doom-real-buffer-p' for more information.")
;;;###autoload
(defvar-local doom-real-buffer-p nil
"If non-nil, this buffer should be considered real no matter what. See
`doom-real-buffer-p' for more information.")
;;;###autoload
(defvar doom-fallback-buffer-name "*scratch*"
"The name of the buffer to fall back to if no other buffers exist (will create
it if it doesn't exist).")
;;
;; Functions
;;;###autoload
(defun doom-buffer-frame-predicate (buf)
"To be used as the default frame buffer-predicate parameter. Returns nil if
BUF should be skipped over by functions like `next-buffer' and `other-buffer'."
(or (doom-real-buffer-p buf)
(eq buf (doom-fallback-buffer))))
;;;###autoload
(defun doom-fallback-buffer ()
"Returns the fallback buffer, creating it if necessary. By default this is the
scratch buffer. See `doom-fallback-buffer-name' to change this."
(let (buffer-list-update-hook)
(get-buffer-create doom-fallback-buffer-name)))
;;;###autoload
(defalias 'doom-buffer-list #'buffer-list)
;;;###autoload
(defun doom-project-buffer-list (&optional project)
"Return a list of buffers belonging to the specified PROJECT.
If PROJECT is nil, default to the current project.
If no project is active, return all buffers."
(let ((buffers (doom-buffer-list)))
(if-let* ((project-root
(if project (expand-file-name project)
(doom-project-root))))
(cl-loop for buf in buffers
if (projectile-project-buffer-p buf project-root)
collect buf)
buffers)))
;;;###autoload
(defun doom-open-projects ()
"Return a list of projects with open buffers."
(cl-loop with projects = (make-hash-table :test 'equal :size 8)
for buffer in (doom-buffer-list)
if (buffer-live-p buffer)
if (doom-real-buffer-p buffer)
if (with-current-buffer buffer (doom-project-root))
do (puthash (abbreviate-file-name it) t projects)
finally return (hash-table-keys projects)))
;;;###autoload
(defun doom-dired-buffer-p (buf)
"Returns non-nil if BUF is a dired buffer."
(with-current-buffer buf (derived-mode-p 'dired-mode)))
;;;###autoload
(defun doom-special-buffer-p (buf)
"Returns non-nil if BUF's name starts and ends with an *."
(equal (substring (buffer-name buf) 0 1) "*"))
;;;###autoload
(defun doom-temp-buffer-p (buf)
"Returns non-nil if BUF is temporary."
(equal (substring (buffer-name buf) 0 1) " "))
;;;###autoload
(defun doom-visible-buffer-p (buf)
"Return non-nil if BUF is visible."
(get-buffer-window buf))
;;;###autoload
(defun doom-buried-buffer-p (buf)
"Return non-nil if BUF is not visible."
(not (doom-visible-buffer-p buf)))
;;;###autoload
(defun doom-non-file-visiting-buffer-p (buf)
"Returns non-nil if BUF does not have a value for `buffer-file-name'."
(not (buffer-file-name buf)))
;;;###autoload
(defun doom-real-buffer-list (&optional buffer-list)
"Return a list of buffers that satify `doom-real-buffer-p'."
(cl-remove-if-not #'doom-real-buffer-p (or buffer-list (doom-buffer-list))))
;;;###autoload
(defun doom-real-buffer-p (buffer-or-name)
"Returns t if BUFFER-OR-NAME is a 'real' buffer.
A real buffer is a useful buffer; a first class citizen in Doom. Real ones
should get special treatment, because we will be spending most of our time in
them. Unreal ones should be low-profile and easy to cast aside, so we can focus
on real ones.
The exact criteria for a real buffer is:
1. A non-nil value for the buffer-local value of the `doom-real-buffer-p'
variable OR
2. Any function in `doom-real-buffer-functions' returns non-nil OR
3. None of the functions in `doom-unreal-buffer-functions' must return
non-nil.
If BUFFER-OR-NAME is omitted or nil, the current buffer is tested."
(or (bufferp buffer-or-name)
(stringp buffer-or-name)
(signal 'wrong-type-argument (list '(bufferp stringp) buffer-or-name)))
(when-let (buf (get-buffer buffer-or-name))
(and (buffer-live-p buf)
(not (doom-temp-buffer-p buf))
(or (buffer-local-value 'doom-real-buffer-p buf)
(run-hook-with-args-until-success 'doom-real-buffer-functions buf)
(not (run-hook-with-args-until-success 'doom-unreal-buffer-functions buf))))))
;;;###autoload
(defun doom-unreal-buffer-p (buffer-or-name)
"Return t if BUFFER-OR-NAME is an 'unreal' buffer.
See `doom-real-buffer-p' for details on what that means."
(not (doom-real-buffer-p buffer-or-name)))
;;;###autoload
(defun doom-buffers-in-mode (modes &optional buffer-list derived-p)
"Return a list of buffers whose `major-mode' is `eq' to MODE(S).
If DERIVED-P, test with `derived-mode-p', otherwise use `eq'."
(let ((modes (doom-enlist modes)))
(cl-remove-if-not (if derived-p
(lambda (buf)
(with-current-buffer buf
(apply #'derived-mode-p modes)))
(lambda (buf)
(memq (buffer-local-value 'major-mode buf) modes)))
(or buffer-list (doom-buffer-list)))))
;;;###autoload
(defun doom-visible-windows (&optional window-list)
"Return a list of the visible, non-popup (dedicated) windows."
(cl-loop for window in (or window-list (window-list))
when (or (window-parameter window 'visible)
(not (window-dedicated-p window)))
collect window))
;;;###autoload
(defun doom-visible-buffers (&optional buffer-list)
"Return a list of visible buffers (i.e. not buried)."
(if buffer-list
(cl-remove-if-not #'get-buffer-window buffer-list)
(delete-dups (mapcar #'window-buffer (window-list)))))
;;;###autoload
(defun doom-buried-buffers (&optional buffer-list)
"Get a list of buffers that are buried."
(cl-remove-if #'get-buffer-window (or buffer-list (doom-buffer-list))))
;;;###autoload
(defun doom-matching-buffers (pattern &optional buffer-list)
"Get a list of all buffers that match the regex PATTERN."
(cl-loop for buf in (or buffer-list (doom-buffer-list))
when (string-match-p pattern (buffer-name buf))
collect buf))
;;;###autoload
(defun doom-set-buffer-real (buffer flag)
"Forcibly mark BUFFER as FLAG (non-nil = real)."
(with-current-buffer buffer
(setq doom-real-buffer-p flag)))
;;;###autoload
(defun doom-kill-buffer-and-windows (buffer)
"Kill the buffer and delete all the windows it's displayed in."
(dolist (window (get-buffer-window-list buffer))
(unless (one-window-p t)
(delete-window window)))
(kill-buffer buffer))
;;;###autoload
(defun doom-fixup-windows (windows)
"Ensure that each of WINDOWS is showing a real buffer or the fallback buffer."
(dolist (window windows)
(with-selected-window window
(when (doom-unreal-buffer-p (window-buffer))
(previous-buffer)
(when (doom-unreal-buffer-p (window-buffer))
(switch-to-buffer (doom-fallback-buffer)))))))
;;;###autoload
(defun doom-kill-buffer-fixup-windows (buffer)
"Kill the BUFFER and ensure all the windows it was displayed in have switched
to a real buffer or the fallback buffer."
(let ((windows (get-buffer-window-list buffer)))
(kill-buffer buffer)
(doom-fixup-windows (cl-remove-if-not #'window-live-p windows))))
;;;###autoload
(defun doom-kill-buffers-fixup-windows (buffers)
"Kill the BUFFERS and ensure all the windows they were displayed in have
switched to a real buffer or the fallback buffer."
(let ((seen-windows (make-hash-table :test 'eq :size 8)))
(dolist (buffer buffers)
(let ((windows (get-buffer-window-list buffer)))
(kill-buffer buffer)
(dolist (window (cl-remove-if-not #'window-live-p windows))
(puthash window t seen-windows))))
(doom-fixup-windows (hash-table-keys seen-windows))))
;;;###autoload
(defun doom-kill-matching-buffers (pattern &optional buffer-list)
"Kill all buffers (in current workspace OR in BUFFER-LIST) that match the
regex PATTERN. Returns the number of killed buffers."
(let ((buffers (doom-matching-buffers pattern buffer-list)))
(dolist (buf buffers (length buffers))
(kill-buffer buf))))
;;
;; Hooks
;;;###autoload
(defun doom-mark-buffer-as-real-h ()
"Hook function that marks the current buffer as real."
(doom-set-buffer-real (current-buffer) t))
;;
;; Interactive commands
;;;###autoload
(defun doom/kill-this-buffer-in-all-windows (buffer &optional dont-save)
"Kill BUFFER globally and ensure all windows previously showing this buffer
have switched to a real buffer or the fallback buffer.
If DONT-SAVE, don't prompt to save modified buffers (discarding their changes)."
(interactive
(list (current-buffer) current-prefix-arg))
(cl-assert (bufferp buffer) t)
(when (and (buffer-modified-p buffer) dont-save)
(with-current-buffer buffer
(set-buffer-modified-p nil)))
(doom-kill-buffer-fixup-windows buffer))
;;;###autoload
(defun doom/kill-all-buffers (&optional buffer-list interactive)
"Kill all buffers and closes their windows.
If the prefix arg is passed, doesn't close windows and only kill buffers that
belong to the current project."
(interactive
(list (if current-prefix-arg
(doom-project-buffer-list)
(doom-buffer-list))
t))
(if (null buffer-list)
(message "No buffers to kill")
(save-some-buffers)
(when (memq (current-buffer) buffer-list)
(switch-to-buffer (doom-fallback-buffer)))
(mapc #'doom-kill-buffer-and-windows buffer-list)
(delete-other-windows)
(when interactive
(message "Killed %s buffers"
(- (length buffer-list)
(length (cl-remove-if-not #'buffer-live-p buffer-list)))))))
;;;###autoload
(defun doom/kill-other-buffers (&optional buffer-list interactive)
"Kill all other buffers (besides the current one).
If the prefix arg is passed, kill only buffers that belong to the current
project."
(interactive
(list (delq (current-buffer)
(if current-prefix-arg
(doom-project-buffer-list)
(doom-buffer-list)))
t))
(mapc #'doom-kill-buffer-and-windows buffer-list)
(when interactive
(message "Killed %s buffers"
(- (length buffer-list)
(length (cl-remove-if-not #'buffer-live-p buffer-list))))))
;;;###autoload
(defun doom/kill-matching-buffers (pattern &optional buffer-list interactive)
"Kill buffers that match PATTERN in BUFFER-LIST.
If the prefix arg is passed, only kill matching buffers in the current project."
(interactive
(list (read-regexp "Buffer pattern: ")
(if current-prefix-arg
(doom-project-buffer-list)
(doom-buffer-list))
t))
(doom-kill-matching-buffers pattern buffer-list)
(when interactive
(message "Killed %d buffer(s)"
(- (length buffer-list)
(length (cl-remove-if-not #'buffer-live-p buffer-list))))))
;;;###autoload
(defun doom/kill-buried-buffers (&optional buffer-list interactive)
"Kill buffers that are buried.
If PROJECT-P (universal argument), only kill buried buffers belonging to the
current project."
(interactive
(list (doom-buried-buffers
(if current-prefix-arg (doom-project-buffer-list)))
t))
(mapc #'kill-buffer buffer-list)
(when interactive
(message "Killed %s buried buffers"
(- (length buffer-list)
(length (cl-remove-if-not #'buffer-live-p buffer-list))))))
;;;###autoload
(defun doom/kill-project-buffers (project &optional interactive)
"Kill buffers for the specified PROJECT."
(interactive
(list (if-let (open-projects (doom-open-projects))
(completing-read
"Kill buffers for project: " open-projects
nil t nil nil
(if-let* ((project-root (doom-project-root))
(project-root (abbreviate-file-name project-root))
((member project-root open-projects)))
project-root))
(message "No projects are open!")
nil)
t))
(when project
(let ((buffers (doom-project-buffer-list project)))
(doom-kill-buffers-fixup-windows buffers)
(when interactive
(message "Killed %d buffer(s)"
(- (length buffers)
(length (cl-remove-if-not #'buffer-live-p buffers))))))))

View File

@@ -0,0 +1,95 @@
;;; core/autoload/cache.el -*- lexical-binding: t; -*-
;; This little library thinly wraps around persistent-soft (which is a pcache
;; wrapper, how about that). It has three purposes:
;;
;; + To encapsulate the cache backend (persistent-soft/pcache in this case), in
;; case it needs to change.
;; + To provide `doom-cache-persist': a mechanism for easily persisting
;; variables across Emacs sessions.
;; + To lazy-load persistent-soft until it is really needed.
;;
;; Like persistent-soft, caches assume a 2-tier structure, where all caches are
;; namespaced by location.
(defvar doom-cache-alists '(t)
"An alist of alists, containing lists of variables for the doom cache library
to persist across Emacs sessions.")
(defvar doom-cache-location 'doom
"The default location for cache files. This symbol is translated into a file
name under `pcache-directory' (by default a subdirectory under
`doom-cache-dir'). One file may contain multiple cache entries.")
(defun doom-save-persistent-cache-h ()
"Hook to run when an Emacs session is killed. Saves all persisted variables
listed in `doom-cache-alists' to files."
(dolist (alist (butlast doom-cache-alists 1))
(cl-loop with key = (car alist)
for var in (cdr alist)
if (symbol-value var)
do (doom-cache-set var it nil key))))
(add-hook 'kill-emacs-hook #'doom-save-persistent-cache-h)
;;
;; Library
;;;###autoload
(defmacro with-cache! (location &rest body)
"Runs BODY with a different default `doom-cache-location'."
(declare (indent defun))
`(let ((doom-cache-location ',location))
,@body))
;;;###autoload
(defun doom-cache-persist (location variables)
"Persist VARIABLES (list of symbols) in LOCATION (symbol).
This populates these variables with cached values, if one exists, and saves them
to file when Emacs quits.
Warning: this is incompatible with buffer-local variables."
(dolist (var variables)
(when (doom-cache-exists var location)
(set var (doom-cache-get var location))))
(setf (alist-get location doom-cache-alists)
(append variables (cdr (assq location doom-cache-alists)))))
;;;###autoload
(defun doom-cache-desist (location &optional variables)
"Unregisters VARIABLES (list of symbols) in LOCATION (symbol) from
`doom-cache-alists', thus preventing them from being saved between sessions.
Does not affect the actual variables themselves or their values."
(if variables
(setf (alist-get location doom-cache-alists)
(cl-set-difference (cdr (assq location doom-cache-alists))
variables))
(delq (assq location doom-cache-alists)
doom-cache-alists)))
;;;###autoload
(defun doom-cache-get (key &optional location)
"Retrieve KEY from LOCATION (defaults to `doom-cache-location'), if it exists
and hasn't expired."
(persistent-soft-fetch
key (symbol-name (or location doom-cache-location))))
;;;###autoload
(defun doom-cache-set (key value &optional ttl location)
"Set KEY to VALUE in the cache. TTL is the time (in seconds) until this cache
entry expires. LOCATION is the super-key to store this cache item under; the
default is `doom-cache-location'. "
(persistent-soft-store
key value
(symbol-name (or location doom-cache-location)) ttl))
;;;###autoload
(defun doom-cache-exists (key &optional location)
"Returns t if KEY exists at LOCATION (defaults to `doom-cache-location')."
(persistent-soft-exists-p key (or location doom-cache-location)))
;;;###autoload
(defun doom-cache-clear (&optional location)
"Clear a cache LOCATION (defaults to `doom-cache-location')."
(persistent-soft-flush (or location doom-cache-location)))

View File

@@ -0,0 +1,45 @@
;;; core/autoload/cli.el -*- lexical-binding: t; -*-
;;
;;; Library
;;;###autoload
(defun doom-call-process (command &rest args)
"Execute COMMAND with ARGS synchronously.
Returns (STATUS . OUTPUT) when it is done, where STATUS is the returned error
code of the process and OUTPUT is its stdout output."
(with-temp-buffer
(cons (or (apply #'call-process command nil t nil args)
-1)
(string-trim (buffer-string)))))
;;;###autoload
(defun doom-exec-process (command &rest args)
"Execute COMMAND with ARGS synchronously.
Unlike `doom-call-process', this pipes output to `standard-output' on the fly to
simulate 'exec' in the shell, so batch scripts could run external programs
synchronously without sacrificing their output.
Warning: freezes indefinitely on any stdin prompt."
;; FIXME Is there any way to handle prompts?
(with-temp-buffer
(cons (let ((process
(make-process :name "doom-sh"
:buffer (current-buffer)
:command (cons command args)
:connection-type 'pipe))
done-p)
(set-process-filter
process (lambda (_process output)
(princ output (current-buffer))
(princ output)))
(set-process-sentinel
process (lambda (process _event)
(when (memq (process-status process) '(exit stop))
(setq done-p t))))
(while (not done-p)
(sit-for 0.1))
(process-exit-status process))
(string-trim (buffer-string)))))

View File

@@ -0,0 +1,142 @@
;;; core/autoload/config.el -*- lexical-binding: t; -*-
(defvar doom-bin-dir (concat doom-emacs-dir "bin/"))
(defvar doom-bin (concat doom-bin-dir "doom"))
;;;###autoload
(defvar doom-reload-hook nil
"A list of hooks to run when `doom/reload' is called.")
;;;###autoload
(defvar doom-reloading-p nil
"TODO")
;;;###autoload
(defun doom/open-private-config ()
"Browse your `doom-private-dir'."
(interactive)
(unless (file-directory-p doom-private-dir)
(make-directory doom-private-dir t))
(doom-project-browse doom-private-dir))
;;;###autoload
(defun doom/find-file-in-private-config ()
"Search for a file in `doom-private-dir'."
(interactive)
(doom-project-find-file doom-private-dir))
;;;###autoload
(defun doom/goto-doomblock ()
"Open your private init.el and go to your `doom!' block."
(interactive)
(find-file (expand-file-name "init.el" doom-private-dir))
(goto-char
(or (save-excursion
(goto-char (point-min))
(search-forward "(doom!" nil t))
(point))))
;;;###autoload
(defun doom/goto-config-file ()
"Open your private config.el file."
(interactive)
(find-file (expand-file-name "config.el" doom-private-dir)))
;;;###autoload
(defun doom/goto-packages-file ()
"Open your private packages.el file."
(interactive)
(find-file (expand-file-name "packages.el" doom-private-dir)))
;;
;;; Managements
(cl-defmacro doom--compile (command &key on-success on-failure)
(declare (indent defun))
`(with-current-buffer (compile ,command)
(add-hook
'compilation-finish-functions
(lambda (_buf status)
(if (equal status "finished\n")
,on-success
,on-failure))
nil 'local)))
;;;###autoload
(defun doom/reload ()
"Reloads your private config.
This is experimental! It will try to do as `bin/doom refresh' does, but from
within this Emacs session. i.e. it reload autoloads files (if necessary),
reloads your package list, and lastly, reloads your private config.el.
Runs `doom-reload-hook' afterwards."
(interactive)
(require 'core-cli)
(when (and IS-WINDOWS (file-exists-p doom-env-file))
(warn "Can't regenerate envvar file from within Emacs. Run 'doom env' from the console"))
(doom--compile (format "%s refresh -e" doom-bin)
:on-success
(let ((doom-reloading-p t))
(doom-initialize 'force)
(with-demoted-errors "PRIVATE CONFIG ERROR: %s"
(general-auto-unbind-keys)
(unwind-protect
(doom-initialize-modules 'force)
(general-auto-unbind-keys t)))
(run-hook-wrapped 'doom-reload-hook #'doom-try-run-hook)
(print! (success "Config successfully reloaded!")))
:on-failure
(user-error "Failed to reload your config")))
;;;###autoload
(defun doom/reload-autoloads ()
"Reload only `doom-autoload-file' and `doom-package-autoload-file'.
This is much faster and safer than `doom/reload', but not as comprehensive. This
reloads your package and module visibility, but does not install new packages or
remove orphaned ones. It also doesn't reload your private config.
It is useful to only pull in changes performed by 'doom refresh' on the command
line."
(interactive)
(require 'core-cli)
(require 'core-packages)
(doom-initialize-packages)
(doom-cli-reload-autoloads nil 'force))
;;;###autoload
(defun doom/reload-env (&optional arg)
"Regenerates and/or reloads your envvar file.
If passed the prefix ARG, clear the envvar file. Uses the same mechanism as
'bin/doom env'.
An envvar file contains a snapshot of your shell environment, which can be
imported into Emacs."
(interactive "P")
(when IS-WINDOWS
(user-error "Cannot reload envvar file from within Emacs on Windows, run it from cmd.exe"))
(doom--compile
(format "%s -ic '%s env%s'"
(string-trim
(shell-command-to-string
(format "getent passwd %S | cut -d: -f7"
(user-login-name))))
doom-bin (if arg " -c" ""))
:on-success
(let ((doom-reloading-p t))
(unless arg
(doom-load-envvars-file doom-env-file)))
:on-failure
(error "Failed to generate env file")))
;;;###autoload
(defun doom/upgrade ()
"Run 'doom upgrade' then prompt to restart Emacs."
(interactive)
(doom--compile (format "%s upgrade" doom-bin)
:on-success
(when (y-or-n-p "You must restart Emacs for the upgrade to take effect.\n\nRestart Emacs?")
(doom/restart-and-restore))))

View File

@@ -0,0 +1,344 @@
;;; core/autoload/debug.el -*- lexical-binding: t; -*-
;;;###autoload
(defun doom-run-all-startup-hooks-h ()
"Run all startup Emacs hooks. Meant to be executed after starting Emacs with
-q or -Q, for example:
emacs -Q -l init.el -f doom-run-all-startup-hooks-h"
(run-hook-wrapped 'after-init-hook #'doom-try-run-hook)
(setq after-init-time (current-time))
(mapc (doom-rpartial #'run-hook-wrapped #'doom-try-run-hook)
(list 'delayed-warnings-hook
'emacs-startup-hook 'tty-setup-hook
'window-setup-hook)))
;;
;;; Helpers
(defun doom-template-insert (template)
"TODO"
(let ((file (expand-file-name (format "templates/%s" template) doom-core-dir)))
(when (file-exists-p file)
(insert-file-contents file))))
;;;###autoload
(defun doom-info ()
"Returns diagnostic information about the current Emacs session in markdown,
ready to be pasted in a bug report on github."
(require 'vc-git)
(let ((default-directory doom-emacs-dir)
(doom-modules (doom-modules)))
(cl-letf
(((symbol-function 'sh)
(lambda (&rest args)
(cdr (apply #'doom-call-process args)))))
`((emacs
(version . ,emacs-version)
(features ,@system-configuration-features)
(build . ,(format-time-string "%b %d, %Y" emacs-build-time))
(buildopts ,system-configuration-options)
(windowsys . ,(if noninteractive 'batch window-system))
(daemonp . ,(cond ((daemonp) 'daemon)
((and (require 'server)
(server-running-p))
'server-running))))
(doom
(version . ,doom-version)
(build . ,(sh "git" "log" "-1" "--format=%D %h %ci")))
(system
(type . ,system-type)
(config . ,system-configuration)
(shell . ,shell-file-name)
(uname . ,(if IS-WINDOWS
"n/a"
(sh "uname" "-msrv")))
(path . ,(mapcar #'abbreviate-file-name exec-path)))
(config
(envfile
. ,(cond ((file-exists-p doom-env-file) 'envvar-file)
((featurep 'exec-path-from-shell) 'exec-path-from-shell)))
(elc-files
. ,(length (doom-files-in `(,@doom-modules-dirs
,doom-core-dir
,doom-private-dir)
:type 'files :match "\\.elc$")))
(modules
,@(or (cl-loop with cat = nil
for key being the hash-keys of doom-modules
if (or (not cat)
(not (eq cat (car key))))
do (setq cat (car key))
and collect cat
collect
(let ((flags (doom-module-get cat (cdr key) :flags)))
(if flags
`(,(cdr key) ,@flags)
(cdr key))))
'("n/a")))
(packages
,@(or (condition-case e
(cl-loop for (name . plist) in (doom-package-list)
if (cl-find :private (plist-get plist :modules)
:key #'car)
collect
(if-let (splist (doom-plist-delete (copy-sequence plist)
:modules))
(prin1-to-string (cons name splist))
name))
(error (format "<%S>" e)))
'("n/a")))
(elpa
,@(or (condition-case e
(progn
(package-initialize)
(cl-loop for (name . _) in package-alist
collect (format "%s" name)))
(error (format "<%S>" e)))
'("n/a"))))))))
;;
;;; Commands
;;;###autoload
(defun doom/version ()
"Display the current version of Doom & Emacs, including the current Doom
branch and commit."
(interactive)
(require 'vc-git)
(let ((default-directory doom-core-dir))
(print! "Doom v%s (Emacs v%s)\nBranch: %s\nCommit: %s\nBuild date: %s"
doom-version
emacs-version
(or (vc-git--symbolic-ref doom-core-dir)
"n/a")
(or (vc-git-working-revision doom-core-dir)
"n/a")
(or (cdr (doom-call-process "git" "log" "-1" "--format=%ci"))
"n/a"))))
;;;###autoload
(defun doom/info (&optional raw)
"Collects some debug information about your Emacs session, formats it into
markdown and copies it to your clipboard, ready to be pasted into bug reports!"
(interactive "P")
(let ((buffer (get-buffer-create "*doom-info*"))
(info (doom-info)))
(with-current-buffer buffer
(unless (or noninteractive
(eq major-mode 'markdown-mode)
(not (fboundp 'markdown-mode)))
(markdown-mode))
(erase-buffer)
(if raw
(progn
(save-excursion
(pp info (current-buffer)))
(when (search-forward "(modules " nil t)
(goto-char (match-beginning 0))
(cl-destructuring-bind (beg . end)
(bounds-of-thing-at-point 'sexp)
(let ((sexp (prin1-to-string (sexp-at-point))))
(delete-region beg end)
(insert sexp)))))
(insert "<details>\n\n```\n")
(dolist (group info)
(insert! "%-8s%-10s %s\n"
((car group)
(caadr group)
(cdadr group)))
(dolist (spec (cddr group))
(insert! (indent 8 "%-10s %s\n")
((car spec) (cdr spec)))))
(insert "```\n</details>"))
(if noninteractive
(print! (buffer-string))
(switch-to-buffer buffer)
(kill-new (buffer-string))
(print! (green "Copied markdown to clipboard"))))))
;;;###autoload
(defun doom/am-i-secure ()
"Test to see if your root certificates are securely configured in emacs."
(declare (interactive-only t))
(interactive)
(unless (string-match-p "\\_<GNUTLS\\_>" system-configuration-features)
(warn "gnutls support isn't built into Emacs, there may be problems"))
(if-let* ((bad-hosts
(cl-loop for bad
in '("https://wrong.host.badssl.com/"
"https://self-signed.badssl.com/")
if (condition-case _e
(url-retrieve-synchronously bad)
(error nil))
collect bad)))
(error "tls seems to be misconfigured (it got %s)."
bad-hosts)
(url-retrieve "https://badssl.com"
(lambda (status)
(if (or (not status) (plist-member status :error))
(warn "Something went wrong.\n\n%s" (pp-to-string status))
(message "Your trust roots are set up properly.\n\n%s" (pp-to-string status))
t)))))
;;
;;; Vanilla sandbox
(defun doom--run-sandbox (&optional mode)
(interactive)
(let ((contents (buffer-string))
(file (make-temp-file "doom-sandbox-")))
(require 'package)
(with-temp-file file
(insert
(prin1-to-string
(macroexp-progn
(append `((setq noninteractive nil
doom-debug-mode t
load-path ',load-path
package--init-file-ensured t
package-user-dir ,package-user-dir
package-archives ',package-archives
user-emacs-directory ,doom-emacs-dir)
(with-eval-after-load 'undo-tree
;; undo-tree throws errors because `buffer-undo-tree' isn't
;; corrrectly initialized
(setq-default buffer-undo-tree (make-undo-tree))))
(pcase mode
(`vanilla-doom+ ; Doom core + modules - private config
`((load-file ,(expand-file-name "core.el" doom-core-dir))
(doom-initialize)
(doom-initialize-core)
(add-hook 'window-setup-hook #'doom-display-benchmark-h)
(setq doom-modules ',doom-modules)
(maphash (lambda (key plist)
(let ((doom--current-module key)
(doom--current-flags (plist-get plist :flags)))
(load! "init" (doom-module-locate-path (car key) (cdr key)) t)))
doom-modules)
(maphash (lambda (key plist)
(let ((doom--current-module key)
(doom--current-flags (plist-get plist :flags)))
(load! "config" (doom-module-locate-path (car key) (cdr key)) t)))
doom-modules)
(run-hook-wrapped 'doom-init-modules-hook #'doom-try-run-hook)
(doom-run-all-startup-hooks-h)))
(`vanilla-doom ; only Doom core
`((load-file ,(expand-file-name "core.el" doom-core-dir))
(doom-initialize)
(doom-initialize-core)
(doom-run-all-startup-hooks-h)))
(`vanilla ; nothing loaded
`((package-initialize)))))))
"\n(unwind-protect (progn\n" contents "\n)\n"
(format "(delete-file %S))" file)))
(let ((args (if (eq mode 'doom)
(list "-l" file)
(list "-Q" "-l" file))))
(require 'restart-emacs)
(condition-case e
(cond ((display-graphic-p)
(if (memq system-type '(windows-nt ms-dos))
(restart-emacs--start-gui-on-windows args)
(restart-emacs--start-gui-using-sh args)))
((memq system-type '(windows-nt ms-dos))
(user-error "Cannot start another Emacs from Windows shell."))
((suspend-emacs
(format "%s %s -nw; fg"
(shell-quote-argument (restart-emacs--get-emacs-binary))
(mapconcat #'shell-quote-argument args " ")))))
(error
(delete-file file)
(signal (car e) (cdr e)))))))
(fset 'doom--run-vanilla-emacs (lambda! (doom--run-sandbox 'vanilla)))
(fset 'doom--run-vanilla-doom (lambda! (doom--run-sandbox 'vanilla-doom)))
(fset 'doom--run-vanilla-doom+ (lambda! (doom--run-sandbox 'vanilla-doom+)))
(fset 'doom--run-full-doom (lambda! (doom--run-sandbox 'doom)))
(defvar doom-sandbox-emacs-lisp-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") #'doom--run-vanilla-emacs)
(define-key map (kbd "C-c C-d") #'doom--run-vanilla-doom)
(define-key map (kbd "C-c C-p") #'doom--run-vanilla-doom+)
(define-key map (kbd "C-c C-f") #'doom--run-full-doom)
(define-key map (kbd "C-c C-k") #'kill-current-buffer)
map))
(define-derived-mode doom-sandbox-emacs-lisp-mode emacs-lisp-mode "Sandbox Elisp"
"TODO")
;;;###autoload
(defun doom/sandbox ()
"Open the Emacs Lisp sandbox.
This is a test bed for running Emacs Lisp in another instance of Emacs with
varying amounts of Doom loaded, including:
a) vanilla Emacs (nothing loaded),
b) vanilla Doom (only Doom core),
c) Doom + modules - your private config or
c) Doom + modules + your private config (a complete Doom session)
This is done without sacrificing access to installed packages. Use the sandbox
to reproduce bugs and determine if Doom is to blame."
(interactive)
(let* ((buffer-name "*doom:sandbox*")
(exists (get-buffer buffer-name))
(buf (get-buffer-create buffer-name)))
(with-current-buffer buf
(doom-sandbox-emacs-lisp-mode)
(setq-local default-directory doom-emacs-dir)
(unless (buffer-live-p exists)
(doom-template-insert "VANILLA_SANDBOX")
(let ((contents (substitute-command-keys (buffer-string))))
(erase-buffer)
(insert contents "\n")))
(goto-char (point-max)))
(pop-to-buffer buf)))
;;
;;; Reporting bugs
;;;###autoload
(defun doom/report-bug ()
"Open a markdown buffer destinated to populate the New Issue page on Doom
Emacs' issue tracker.
If called when a backtrace buffer is present, it and the output of `doom-info'
will be automatically appended to the result."
(interactive)
(browse-url "https://github.com/hlissner/doom-emacs/issues/new/choose"))
;;
;;; Profiling
(defvar doom--profiler nil)
;;;###autoload
(defun doom/toggle-profiler ()
"Toggle the Emacs profiler. Run it again to see the profiling report."
(interactive)
(if (not doom--profiler)
(profiler-start 'cpu+mem)
(profiler-report)
(profiler-stop))
(setq doom--profiler (not doom--profiler)))
;;;###autoload
(defun doom/toggle-debug-mode (&optional arg)
"Toggle `debug-on-error' and `doom-debug-mode' for verbose logging."
(interactive (list (or current-prefix-arg 'toggle)))
(let ((value
(cond ((eq arg 'toggle) (not doom-debug-mode))
((> (prefix-numeric-value arg) 0)))))
(setq doom-debug-mode value
debug-on-error value
jka-compr-verbose value
lsp-log-io value
gcmh-verbose value)
(message "Debug mode %s" (if value "on" "off"))))

View File

@@ -0,0 +1,343 @@
;;; core/autoload/files.el -*- lexical-binding: t; -*-
(defun doom--resolve-path-forms (spec &optional directory)
"Converts a simple nested series of or/and forms into a series of
`file-exists-p' checks.
For example
(doom--resolve-path-forms
'(or A (and B C))
\"~\")
Returns (approximately):
'(let* ((_directory \"~\")
(A (expand-file-name A _directory))
(B (expand-file-name B _directory))
(C (expand-file-name C _directory)))
(or (and (file-exists-p A) A)
(and (if (file-exists-p B) B)
(if (file-exists-p C) C))))
This is used by `file-exists-p!' and `project-file-exists-p!'."
(declare (pure t) (side-effect-free t))
(let ((exists-fn (if (fboundp 'projectile-file-exists-p)
#'projectile-file-exists-p
#'file-exists-p)))
(if (and (listp spec)
(memq (car spec) '(or and)))
(cons (car spec)
(mapcar (doom-rpartial #'doom--resolve-path-forms directory)
(cdr spec)))
(let ((filevar (make-symbol "file")))
`(let* ((file-name-handler-alist nil)
(,filevar ,spec))
(and (stringp ,filevar)
,(if directory
`(let ((default-directory ,directory))
(,exists-fn ,filevar))
(list exists-fn filevar))
,filevar))))))
(defun doom--path (&rest segments)
(let (file-name-handler-alist)
(let ((dir (pop segments)))
(unless segments
(setq dir (expand-file-name dir)))
(while segments
(setq dir (expand-file-name (car segments) dir)
segments (cdr segments)))
dir)))
;;;###autoload
(defun doom-glob (&rest segments)
"Construct a path from SEGMENTS and expand glob patterns.
Returns nil if the path doesn't exist."
(let* (case-fold-search
file-name-handler-alist
(dir (apply #'doom--path segments)))
(if (string-match-p "[[*?]" dir)
(file-expand-wildcards dir t)
(if (file-exists-p dir)
dir))))
;;;###autoload
(defun doom-path (&rest segments)
"Constructs a file path from SEGMENTS."
(if segments
(apply #'doom--path segments)
(file!)))
;;;###autoload
(defun doom-dir (&rest segments)
"Constructs a path from SEGMENTS.
See `doom-path'."
(when-let (path (apply #'doom-path segments))
(directory-file-name (file-name-directory path))))
;;;###autoload
(cl-defun doom-files-in
(paths &rest rest
&key
filter
map
(full t)
(follow-symlinks t)
(type 'files)
(relative-to (unless full default-directory))
(depth 99999)
(mindepth 0)
(match "/[^._][^/]+"))
"Return a list of files/directories in PATHS (one string 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."
(let (file-name-handler-alist
result)
(dolist (file (mapcan (doom-rpartial #'doom-glob "*") (doom-enlist paths)))
(cond ((file-directory-p file)
(nconcq! result
(and (memq type '(t dirs))
(string-match-p match file)
(not (and filter (funcall filter file)))
(not (and (file-symlink-p file)
(not follow-symlinks)))
(<= mindepth 0)
(list (cond (map (funcall map file))
(relative-to (file-relative-name file relative-to))
(file))))
(and (>= depth 1)
(apply #'doom-files-in file
(append (list :mindepth (1- mindepth)
:depth (1- depth)
:relative-to relative-to)
rest)))))
((and (memq type '(t files))
(string-match-p match file)
(not (and filter (funcall filter file)))
(<= mindepth 0))
(push (if relative-to
(file-relative-name file relative-to)
file)
result))))
result))
;;;###autoload
(defun doom-file-cookie-p (file &optional cookie null-value)
"Returns the evaluated result of FORM in a ;;;###COOKIE FORM at the top of
FILE.
If COOKIE doesn't exist, return NULL-VALUE."
(unless (file-exists-p file)
(signal 'file-missing file))
(unless (file-readable-p file)
(error "%S is unreadable" file))
(with-temp-buffer
(insert-file-contents file nil 0 256)
(if (re-search-forward (format "^;;;###%s " (regexp-quote (or cookie "if")))
nil t)
(let ((load-file-name file))
(eval (sexp-at-point) t))
null-value)))
;;;###autoload
(defmacro file-exists-p! (files &optional directory)
"Returns non-nil if the FILES in DIRECTORY all exist.
DIRECTORY is a path; defaults to `default-directory'.
Returns the last file found to meet the rules set by FILES, which can be a
single file or nested compound statement of `and' and `or' statements."
`(let ((p ,(doom--resolve-path-forms files directory)))
(and p (expand-file-name p ,directory))))
;;;###autoload
(defun doom-file-size (file &optional dir)
"Returns the size of FILE (in DIR) in bytes."
(let ((file (expand-file-name file dir)))
(unless (file-exists-p file)
(error "Couldn't find file %S" file))
(unless (file-readable-p file)
(error "File %S is unreadable; can't acquire its filesize"
file))
(nth 7 (file-attributes file))))
(defvar w32-get-true-file-attributes)
;;;###autoload
(defun doom-directory-size (dir)
"Returns the size of FILE (in DIR) in kilobytes."
(unless (file-directory-p dir)
(error "Directory %S does not exist" dir))
(if (executable-find "du")
(/ (string-to-number (cdr (doom-call-process "du" "-sb" dir)))
1024.0)
;; REVIEW This is slow and terribly inaccurate, but it's something
(let ((w32-get-true-file-attributes t)
(file-name-handler-alist dir)
(max-lisp-eval-depth 5000)
(sum 0.0))
(dolist (attrs (directory-files-and-attributes dir nil nil t) sum)
(unless (member (car attrs) '("." ".."))
(cl-incf
sum (if (eq (nth 1 attrs) t) ; is directory
(doom-directory-size (expand-file-name (car attrs) dir))
(/ (nth 8 attrs) 1024.0))))))))
;;
;;; 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)
(when new-path
(recentf-add-file new-path))
(recentf-remove-if-non-kept old-path))
(when (and (bound-and-true-p projectile-mode)
(doom-project-p)
(projectile-file-cached-p old-path (doom-project-root)))
(projectile-purge-file-from-cache old-path))
(when (bound-and-true-p save-place-mode)
(save-place-forget-unreadable-files)))
(defun doom--update-file (path)
(when (featurep 'vc)
(vc-file-clearprops path)
(vc-resynch-buffer path nil t))
(when (featurep 'magit)
(magit-refresh)))
(defun doom--copy-file (old-path new-path &optional force-p)
(let* ((new-path (expand-file-name new-path))
(old-path (file-truename old-path))
(new-path (apply #'expand-file-name
(if (or (directory-name-p new-path)
(file-directory-p new-path))
(list (file-name-nondirectory old-path) new-path)
(list new-path))))
(new-path-dir (file-name-directory new-path))
(project-root (doom-project-root))
(short-new-name (if (and project-root (file-in-directory-p new-path project-root))
(file-relative-name new-path project-root)
(abbreviate-file-name new-path))))
(unless (file-directory-p new-path-dir)
(make-directory new-path-dir t))
(when (buffer-modified-p)
(save-buffer))
(cond ((file-equal-p old-path new-path)
(throw 'status 'overwrite-self))
((and (file-exists-p new-path)
(not force-p)
(not (y-or-n-p (format "File already exists at %s, overwrite?" short-new-name))))
(throw 'status 'aborted))
((file-exists-p old-path)
(copy-file old-path new-path t)
short-new-name)
(short-new-name))))
;;
;;; Commands
;;;###autoload
(defun doom/delete-this-file (&optional path force-p)
"Delete FILENAME (defaults to the file associated with current buffer) and
kills the buffer. If FORCE-P, force the deletion (don't ask for confirmation)."
(interactive
(list (file-truename (buffer-file-name))
current-prefix-arg))
(let* ((fbase (file-name-sans-extension (file-name-nondirectory path)))
(buf (current-buffer)))
(cond ((not (file-exists-p path))
(error "File doesn't exist: %s" path))
((not (or force-p (y-or-n-p (format "Really delete %s?" fbase))))
(message "Aborted")
nil)
((unwind-protect
(progn (delete-file path) t)
(let ((short-path (file-relative-name path (doom-project-root))))
(if (file-exists-p path)
(error "Failed to delete %s" short-path)
;; Ensures that windows displaying this buffer will be switched
;; to real buffers (`doom-real-buffer-p')
(doom/kill-this-buffer-in-all-windows buf t)
(doom--forget-file path)
(doom--update-file path)
(message "Successfully deleted %s" short-path))))))))
;;;###autoload
(defun doom/copy-this-file (new-path &optional force-p)
"Copy current buffer's file to NEW-PATH. If FORCE-P, overwrite the destination
file if it exists, without confirmation."
(interactive
(list (read-file-name "Copy file to: ")
current-prefix-arg))
(pcase (catch 'status
(when-let (dest (doom--copy-file (buffer-file-name) new-path force-p))
(doom--update-file new-path)
(message "File successfully copied to %s" dest)))
(`overwrite-self (error "Cannot overwrite self"))
(`aborted (message "Aborted"))
(_ t)))
;;;###autoload
(defun doom/move-this-file (new-path &optional force-p)
"Move current buffer's file to NEW-PATH. If FORCE-P, overwrite the destination
file if it exists, without confirmation."
(interactive
(list (read-file-name "Move file to: ")
current-prefix-arg))
(pcase (catch 'status
(let ((old-path (buffer-file-name))
(new-path (expand-file-name new-path)))
(when-let (dest (doom--copy-file old-path new-path force-p))
(when (file-exists-p old-path)
(delete-file old-path))
(kill-current-buffer)
(doom--forget-file old-path new-path)
(doom--update-file new-path)
(find-file new-path)
(message "File successfully moved to %s" dest))))
(`overwrite-self (error "Cannot overwrite self"))
(`aborted (message "Aborted"))
(_ t)))
(defun doom--sudo-file (file)
(let ((host (or (file-remote-p file 'host) "localhost")))
(concat "/" (when (file-remote-p file)
(concat (file-remote-p file 'method) ":"
(if-let (user (file-remote-p file 'user))
(concat user "@" host)
host)
"|"))
"sudo:root@" host
":" (or (file-remote-p file 'localname)
file))))
;;;###autoload
(defun doom/sudo-find-file (file)
"Open FILE as root."
(interactive "FOpen file as root: ")
(find-file (doom--sudo-file file)))
;;;###autoload
(defun doom/sudo-this-file ()
"Open the current file as root."
(interactive)
(find-alternate-file (doom--sudo-file buffer-file-name)))

View File

@@ -0,0 +1,136 @@
;;; core/autoload/fonts.el -*- lexical-binding: t; -*-
;;;###autoload
(defvar doom-font-increment 2
"How many steps to increase the font size each time `doom/increase-font-size'
or `doom/decrease-font-size' are invoked.")
;;;###autoload
(defvar doom-big-font nil
"The font to use for `doom-big-font-mode'. If nil, `doom-font' will be used,
scaled up by `doom-big-font-increment'. See `doom-font' for details on
acceptable values for this variable.")
;;;###autoload
(defvar doom-big-font-increment 8
"How many steps to increase the font size (with `doom-font' as the base) when
`doom-big-font-mode' is enabled and `doom-big-font' is nil.")
;;
;;; Library
(defun doom--font-name (fontname frame)
(when (query-fontset fontname)
(when-let (ascii (assq 'ascii (aref (fontset-info fontname frame) 2)))
(setq fontname (nth 2 ascii))))
(or (x-decompose-font-name fontname)
(error "Cannot decompose font name")))
(defun doom--frame-list (&optional frame)
"Return a list consisting of FRAME and all of FRAME's child frames."
(let ((frame (or frame (selected-frame))))
(cons (selected-frame)
(cl-loop for fr in (frame-list)
if (eq (frame-parameter fr 'parent-frame) frame)
collect fr))))
;;;###autoload
(defun doom-adjust-font-size (increment &optional frame)
"Increase size of font in FRAME by INCREMENT.
FRAME parameter defaults to current frame."
(if (null increment)
(let ((frames (doom--frame-list frame)))
(dolist (frame frames)
(when (frame-parameter frame 'font-scale)
(set-frame-parameter frame 'font-scale nil)))
(set-frame-font doom-font 'keep-size frames)
(and frames t))
(let (success)
(dolist (frame (doom--frame-list frame))
(let* ((font (frame-parameter frame 'font))
(font (doom--font-name font frame))
(increment (* increment doom-font-increment))
(zoom-factor (or (frame-parameter frame 'font-scale) 0)))
(let ((new-size (+ (string-to-number (aref font xlfd-regexp-pixelsize-subnum))
increment)))
(unless (> new-size 0)
(error "Font is too small at %d" new-size))
(aset font xlfd-regexp-pixelsize-subnum (number-to-string new-size)))
;; Set point size & width to "*", so frame width will adjust to new font size
(aset font xlfd-regexp-pointsize-subnum "*")
(aset font xlfd-regexp-avgwidth-subnum "*")
(setq font (x-compose-font-name font))
(unless (x-list-fonts font)
(error "Cannot change font size"))
(set-frame-parameter frame 'font font)
(set-frame-parameter frame 'font-scale (+ zoom-factor increment))
(setq success t)))
(when success
;; Unlike `set-frame-font', `set-frame-parameter' won't trigger this
(run-hooks 'after-setting-font-hook)
t))))
;;
;;; Commands
;;;###autoload
(defun doom/reload-font ()
"Reload your fonts, if they're set.
See `doom-init-fonts-h'."
(interactive)
(when doom-font
(set-frame-font doom-font t))
(doom-init-fonts-h)
(mapc #'doom-init-extra-fonts-h (frame-list)))
;;;###autoload
(defun doom/increase-font-size (count)
"Enlargens the font size across the current and child frames."
(interactive "p")
(doom-adjust-font-size count))
;;;###autoload
(defun doom/decrease-font-size (count)
"Shrinks the font size across the current and child frames."
(interactive "p")
(doom-adjust-font-size (- count)))
;;;###autoload
(defun doom/reset-font-size ()
"Reset font size and `text-scale'.
Assuming it has been adjusted via `doom/increase-font-size' and
`doom/decrease-font-size', or `text-scale-*' commands."
(interactive)
(let (success)
(when (and (boundp 'text-scale-mode-amount)
(/= text-scale-mode-amount 0))
(text-scale-set 0)
(setq success t))
(when (doom-adjust-font-size nil)
(setq success t))
(unless success
(user-error "The font hasn't been resized"))))
;;;###autoload
(define-minor-mode doom-big-font-mode
"A global mode that resizes the font, for streams, screen-sharing and
presentations.
This uses `doom/increase-font-size' under the hood, and enlargens the font by
`doom-big-font-increment'."
:init-value nil
:lighter " BIG"
:global t
(unless doom-font
(user-error "`doom-font' must be set to a valid font"))
(if doom-big-font
(set-frame-font (if doom-big-font-mode doom-big-font doom-font)
'keep-size (doom--frame-list))
(doom-adjust-font-size
(and doom-big-font-mode
(integerp doom-big-font-increment)
(/= doom-big-font-increment 0)
doom-big-font-increment))))

View File

@@ -0,0 +1,243 @@
;;; core/autoload/format.el -*- lexical-binding: t; -*-
(defvar doom-format-ansi-alist
'(;; fx
(bold 1 :weight bold)
(dark 2)
(italic 3 :slant italic)
(underscore 4 :underline t)
(blink 5)
(rapid 6)
(contrary 7)
(concealed 8)
(strike 9 :strike-through t)
;; fg
(black 30 term-color-black)
(red 31 term-color-red)
(green 32 term-color-green)
(yellow 33 term-color-yellow)
(blue 34 term-color-blue)
(magenta 35 term-color-magenta)
(cyan 36 term-color-cyan)
(white 37 term-color-white)
;; bg
(on-black 40 term-color-black)
(on-red 41 term-color-red)
(on-green 42 term-color-green)
(on-yellow 43 term-color-yellow)
(on-blue 44 term-color-blue)
(on-magenta 45 term-color-magenta)
(on-cyan 46 term-color-cyan)
(on-white 47 term-color-white))
"An alist of fg/bg/fx names mapped to ansi codes and term-color-* variables.
This serves as the cipher for converting (COLOR ...) function calls in `print!'
and `format!' into colored output, where COLOR is any car of this list.")
(defvar doom-format-class-alist
`((color . doom--format-color)
(class . doom--format-class)
(indent . doom--format-indent)
(autofill . doom--format-autofill)
(success . (lambda (str &rest args)
(apply #'doom--format-color 'green (format "✓ %s" str) args)))
(warn . (lambda (str &rest args)
(apply #'doom--format-color 'yellow (format "! %s" str) args)))
(error . (lambda (str &rest args)
(apply #'doom--format-color 'red (format "x %s" str) args)))
(info . (lambda (str &rest args)
(concat "- " (if args (apply #'format str args) str))))
(start . (lambda (str &rest args)
(concat "> " (if args (apply #'format str args) str))))
(debug . (lambda (str &rest args)
(if doom-debug-mode
(if args
(apply #'format str args)
(format "%s" str))
"")))
(path . abbreviate-file-name)
(symbol . symbol-name)
(relpath . (lambda (str &optional dir)
(if (or (not str)
(not (stringp str))
(string-empty-p str))
str
(let ((dir (or dir (file-truename default-directory)))
(str (file-truename str)))
(if (file-in-directory-p str dir)
(file-relative-name str dir)
(abbreviate-file-name str))))))
(filename . file-name-nondirectory)
(dirname . (lambda (path)
(unless (file-directory-p path)
(setq path (file-name-directory path)))
(directory-file-name path))))
"An alist of text classes that map to transformation functions.
Any of these classes can be called like functions from within `format!' and
`print!' calls, which will transform their input.")
(defvar doom-format-indent 0
"Level to rigidly indent text returned by `format!' and `print!'.")
(defvar doom-format-indent-increment 2
"Steps in which to increment `doom-format-indent' for consecutive levels.")
(defvar doom-format-backend
(if noninteractive 'ansi 'text-properties)
"Determines whether to print colors with ANSI codes or with text properties.
Accepts 'ansi and 'text-properties. nil means don't render colors.")
;;
;;; Library
;;;###autoload
(defun doom--format (output)
(if (string-empty-p (string-trim output))
""
(concat (make-string doom-format-indent 32)
(replace-regexp-in-string
"\n" (concat "\n" (make-string doom-format-indent 32))
output t t))))
;;;###autoload
(defun doom--format-print (output)
(unless (string-empty-p output)
(princ output)
(when (or noninteractive (not (eq standard-output t)))
(terpri)) ; newline
t))
;;;###autoload
(defun doom--format-indent (width text &optional prefix)
"Indent TEXT by WIDTH spaces. If ARGS, format TEXT with them."
(with-temp-buffer
(setq text (format "%s" text))
(insert text)
(indent-rigidly (point-min) (point-max) width)
(when (stringp prefix)
(when (> width 2)
(goto-char (point-min))
(beginning-of-line-text)
(delete-char (- (length prefix)))
(insert prefix)))
(buffer-string)))
;;;###autoload
(defun doom--format-autofill (&rest msgs)
"Ensure MSG is split into lines no longer than `fill-column'."
(with-temp-buffer
(let ((fill-column 76))
(dolist (line msgs)
(when line
(insert (format "%s" line))))
(fill-region (point-min) (point-max))
(buffer-string))))
;;;###autoload
(defun doom--format-color (style format &rest args)
"Apply STYLE to formatted MESSAGE with ARGS.
STYLE is a symbol that correlates to `doom-format-ansi-alist'.
In a noninteractive session, this wraps the result in ansi color codes.
Otherwise, it maps colors to a term-color-* face."
(let* ((code (cadr (assq style doom-format-ansi-alist)))
(format (format "%s" format))
(message (if args (apply #'format format args) format)))
(unless code
(error "%S is an invalid color" style))
(pcase doom-format-backend
(`ansi
(format "\e[%dm%s\e[%dm" code message 0))
(`text-properties
(require 'term) ; piggyback on term's color faces
(propertize
message
'face
(append (get-text-property 0 'face format)
(cond ((>= code 40)
`(:background ,(caddr (assq style doom-format-ansi-alist))))
((>= code 30)
`(:foreground ,(face-foreground (caddr (assq style doom-format-ansi-alist)))))
((cddr (assq style doom-format-ansi-alist)))))))
(_ message))))
;;;###autoload
(defun doom--format-class (class format &rest args)
"Apply CLASS to formatted format with ARGS.
CLASS is derived from `doom-format-class-alist', and can contain any arbitrary,
transformative logic."
(let (fn)
(cond ((setq fn (cdr (assq class doom-format-class-alist)))
(if (functionp fn)
(apply fn format args)
(error "%s does not have a function" class)))
(args (apply #'format format args))
(format))))
;;;###autoload
(defun doom--format-apply (forms &optional sub)
"Replace color-name functions with calls to `doom--format-color'."
(cond ((null forms) nil)
((listp forms)
(append (cond ((not (symbolp (car forms)))
(list (doom--format-apply (car forms))))
(sub
(list (car forms)))
((assq (car forms) doom-format-ansi-alist)
`(doom--format-color ',(car forms)))
((assq (car forms) doom-format-class-alist)
`(doom--format-class ',(car forms)))
((list (car forms))))
(doom--format-apply (cdr forms) t)
nil))
(forms)))
;;;###autoload
(defmacro format! (message &rest args)
"An alternative to `format' that understands (color ...) and converts them
into faces or ANSI codes depending on the type of sesssion we're in."
`(doom--format (format ,@(doom--format-apply `(,message ,@args)))))
;;;###autoload
(defmacro print-group! (&rest body)
"Indents any `print!' or `format!' output within BODY."
`(let ((doom-format-indent (+ doom-format-indent-increment doom-format-indent)))
,@body))
;;;###autoload
(defmacro print! (message &rest args)
"Uses `message' in interactive sessions and `princ' otherwise (prints to
standard out).
Can be colored using (color ...) blocks:
(print! \"Hello %s\" (bold (blue \"How are you?\")))
(print! \"Hello %s\" (red \"World\"))
(print! (green \"Great %s!\") \"success\")
Uses faces in interactive sessions and ANSI codes otherwise."
`(doom--format-print (format! ,message ,@args)))
;;;###autoload
(defmacro insert! (message &rest args)
"Like `insert'; the last argument must be format arguments for MESSAGE.
\(fn MESSAGE... ARGS)"
`(insert (format! (concat ,message ,@(butlast args))
,@(car (last args)))))
;;;###autoload
(defmacro error! (message &rest args)
"Like `error', but with the power of `format!'."
`(error (format! ,message ,@args)))
;;;###autoload
(defmacro user-error! (message &rest args)
"Like `user-error', but with the power of `format!'."
`(user-error (format! ,message ,@args)))

View File

@@ -0,0 +1,646 @@
;;; core/autoload/help.el -*- lexical-binding: t; -*-
(defvar doom--help-major-mode-module-alist
'((dockerfile-mode :tools docker)
(agda2-mode :lang agda)
(haxor-mode :lang assembly)
(mips-mode :lang assembly)
(nasm-mode :lang assembly)
(c-mode :lang cc)
(c++-mode :lang cc)
(objc++-mode :lang cc)
(crystal-mode :lang crystal)
(lisp-mode :lang common-lisp)
(csharp-mode :lang csharp)
(clojure-mode :lang clojure)
(clojurescript-mode :lang clojure)
(graphql-mode :lang data)
(toml-mode :lang data)
(json-mode :lang data)
(yaml-mode :lang data)
(csv-mode :lang data)
(dhall-mode :lang data)
(erlang-mode :lang erlang)
(elixir-mode :lang elixir)
(elm-mode :lang elm)
(emacs-lisp-mode :lang emacs-lisp)
(ess-r-mode :lang ess)
(ess-julia-mode :lang ess)
(go-mode :lang go)
(haskell-mode :lang haskell)
(hy-mode :lang hy)
(idris-mode :lang idris)
(java-mode :lang java)
(js2-mode :lang javascript)
(rjsx-mode :lang javascript)
(typescript-mode :lang javascript)
(coffee-mode :lang javascript)
(julia-mode :lang julia)
(kotlin-mode :lang kotlin)
(latex-mode :lang latex)
(LaTeX-mode :lang latex)
(ledger-mode :lang ledger)
(lua-mode :lang lua)
(markdown-mode :lang markdown)
(gfm-mode :lang markdown)
(nim-mode :lang nim)
(nix-mode :lang nix)
(taureg-mode :lang ocaml)
(org-mode :lang org)
(perl-mode :lang perl)
(php-mode :lang php)
(hack-mode :lang php)
(plantuml-mode :lang plantuml)
(purescript-mode :lang purescript)
(python-mode :lang python)
(restclient-mode :lang rest)
(ruby-mode :lang ruby)
(enh-ruby-mode :lang ruby)
(rust-mode :lang rust)
(scala-mode :lang scala)
(sh-mode :lang sh)
(swift-mode :lang swift)
(web-mode :lang web)
(css-mode :lang web)
(scss-mode :lang web)
(sass-mode :lang web)
(less-css-mode :lang web)
(stylus-mode :lang web)
(terra-mode :lang terra))
"An alist mapping major modes to Doom modules.
This is used by `doom/help-modules' to auto-select the module corresponding to
the current major-modea.")
;;
;;; Helpers
;;;###autoload
(defun doom-active-minor-modes ()
"Return a list of active minor-mode symbols."
(cl-loop for mode in minor-mode-list
if (and (boundp mode) (symbol-value mode))
collect mode))
;;
;;; Custom describe commands
;;;###autoload (defalias 'doom/describe-autodefs #'doom/help-autodefs)
;;;###autoload (defalias 'doom/describe-module #'doom/help-modules)
;;;###autoload (defalias 'doom/describe-package #'doom/help-packages)
;;;###autoload
(defun doom/describe-active-minor-mode (mode)
"Get information on an active minor mode. Use `describe-minor-mode' for a
selection of all minor-modes, active or not."
(interactive
(list (completing-read "Minor mode: " (doom-active-minor-modes))))
(let ((symbol
(cond ((stringp mode) (intern mode))
((symbolp mode) mode)
((error "Expected a symbol/string, got a %s" (type-of mode))))))
(if (fboundp symbol)
(helpful-function symbol)
(helpful-variable symbol))))
;;;###autoload
(defun doom/describe-symbol (symbol)
"Show help for SYMBOL, a variable, function or macro."
(interactive
(list (helpful--read-symbol "Symbol: " #'helpful--bound-p)))
(let* ((sym (intern-soft symbol))
(bound (boundp sym))
(fbound (fboundp sym)))
(cond ((and sym bound (not fbound))
(helpful-variable sym))
((and sym fbound (not bound))
(helpful-callable sym))
((apropos (format "^%s\$" symbol)))
((apropos (format "%s" symbol))))))
;;
;;; Documentation commands
(defvar org-agenda-files)
(defun doom--org-headings (files &optional depth include-files)
"TODO"
(require 'org)
(let* ((default-directory doom-docs-dir)
(org-agenda-files (mapcar #'expand-file-name (doom-enlist files)))
(depth (if (integerp depth) depth)))
(message "Loading search results...")
(unwind-protect
(delq
nil
(org-map-entries
(lambda ()
(cl-destructuring-bind (level _reduced-level _todo _priority text tags)
(org-heading-components)
(when (and (or (null depth)
(<= level depth))
(or (null tags)
(not (string-match-p ":TOC" tags))))
(let ((path (org-get-outline-path)))
(list (string-join
(list (string-join
(append (when include-files
(list (or (+org-get-global-property "TITLE")
(file-relative-name (buffer-file-name)))))
path
(list (replace-regexp-in-string org-link-any-re "\\4" text)))
" > ")
tags)
" ")
(buffer-file-name)
(point))))))
t 'agenda))
(mapc #'kill-buffer org-agenda-new-buffers)
(setq org-agenda-new-buffers nil))))
(defvar ivy-sort-functions-alist)
;;;###autoload
(defun doom-completing-read-org-headings (prompt files &optional depth include-files initial-input extra-candidates)
"TODO"
(let ((alist
(append (doom--org-headings files depth include-files)
extra-candidates))
ivy-sort-functions-alist)
(if-let (result (completing-read prompt alist nil nil initial-input))
(cl-destructuring-bind (file &optional location)
(cdr (assoc result alist))
(find-file file)
(cond ((functionp location)
(funcall location))
(location
(goto-char location)))
(ignore-errors
(when (outline-invisible-p)
(save-excursion
(outline-previous-visible-heading 1)
(org-show-subtree)))))
(user-error "Aborted"))))
;;;###autoload
(defun doom/homepage ()
"Open the doom emacs homepage in the browser."
(interactive)
(browse-url "https://doomemacs.org"))
;;;###autoload
(defun doom/help ()
"Open Doom's user manual."
(interactive)
(find-file (expand-file-name "index.org" doom-docs-dir)))
;;;###autoload
(defun doom/help-search (&optional initial-input)
"Search Doom's documentation and jump to a headline."
(interactive)
(doom-completing-read-org-headings
"Find in Doom help: "
(list "getting_started.org"
"contributing.org"
"troubleshooting.org"
"tutorials.org"
"faq.org")
2 t initial-input
(mapcar (lambda (x)
(setcar x (concat "Doom Modules > " (car x)))
x)
(doom--help-modules-list))))
;;;###autoload
(defun doom/help-news-search (&optional initial-input)
"Search headlines in Doom's newsletters."
(interactive)
(doom-completing-read-org-headings
"Find in News: "
(nreverse (doom-files-in (expand-file-name "news" doom-docs-dir)
:match "/[0-9]"
:relative-to doom-docs-dir))
nil t initial-input))
;;;###autoload
(defun doom/help-faq (&optional initial-input)
"Search Doom's FAQ and jump to a question."
(interactive)
(doom-completing-read-org-headings
"Find in FAQ: " (list "faq.org")
2 nil initial-input))
;;;###autoload
(defun doom/help-news ()
"Open a Doom newsletter.
The latest newsletter will be selected by default."
(interactive)
(let* ((default-directory (expand-file-name "news/" doom-docs-dir))
(news-files (doom-files-in default-directory)))
(find-file
(read-file-name (format "Open Doom newsletter (current: v%s): "
doom-version)
default-directory
(if (member doom-version news-files)
doom-version
(concat (mapconcat #'number-to-string
(nbutlast (version-to-list doom-version) 1)
".")
".x"))
t doom-version))))
;;;###autoload
(defun doom/help-autodefs (autodef)
"Open documentation for an autodef.
An autodef is a Doom concept. It is a function or macro that is always defined,
whether or not its containing module is disabled (in which case it will safely
no-op without evaluating its arguments). This syntactic sugar lets you use them
without needing to check if they are available."
(interactive
(let* ((settings
(cl-loop with case-fold-search = nil
for sym being the symbols of obarray
for sym-name = (symbol-name sym)
if (and (or (functionp sym)
(macrop sym))
(string-match-p "[a-z]!$" sym-name))
collect sym))
(sym (symbol-at-point))
(autodef
(completing-read
"Describe setter: "
;; TODO Could be cleaner (refactor me!)
(cl-loop with maxwidth = (apply #'max (mapcar #'length (mapcar #'symbol-name settings)))
for def in (sort settings #'string-lessp)
if (get def 'doom-module)
collect
(format (format "%%-%ds%%s" (+ maxwidth 4))
def (propertize (format "%s %s" (car it) (cdr it))
'face 'font-lock-comment-face))
else if (and (string-match-p "^set-.+!$" (symbol-name def))
(symbol-file def)
(file-in-directory-p (symbol-file def) doom-core-dir))
collect
(format (format "%%-%ds%%s" (+ maxwidth 4))
def (propertize (format "core/%s.el" (file-name-sans-extension (file-relative-name (symbol-file def) doom-core-dir)))
'face 'font-lock-comment-face)))
nil t
(when (and (symbolp sym)
(string-match-p "!$" (symbol-name sym)))
(symbol-name sym)))))
(list (and autodef (car (split-string autodef " "))))))
(or (stringp autodef)
(functionp autodef)
(signal 'wrong-type-argument (list '(stringp functionp) autodef)))
(let ((fn (if (functionp autodef)
autodef
(intern-soft autodef))))
(or (fboundp fn)
(error "'%s' is not a valid DOOM autodef" autodef))
(if (fboundp 'helpful-callable)
(helpful-callable fn)
(describe-function fn))))
(defun doom--help-modules-list ()
(cl-loop for path in (cdr (doom-module-load-path 'all))
for (cat . mod) = (doom-module-from-path path)
for readme-path = (or (doom-module-locate-path cat mod "README.org")
(doom-module-locate-path cat mod))
for format = (format "%s %s" cat mod)
if (doom-module-p cat mod)
collect (list format readme-path)
else if (and cat mod)
collect (list (propertize format 'face 'font-lock-comment-face)
readme-path)))
(defun doom--help-current-module-str ()
(cond ((and buffer-file-name
(eq major-mode 'emacs-lisp-mode)
(file-in-directory-p buffer-file-name doom-private-dir)
(save-excursion (goto-char (point-min))
(re-search-forward "^\\s-*(doom! " nil t))
(thing-at-point 'sexp t)))
((save-excursion
(require 'smartparens)
(ignore-errors
(sp-beginning-of-sexp)
(unless (eq (char-after) ?\()
(backward-char))
(let ((sexp (sexp-at-point)))
(when (memq (car-safe sexp) '(featurep! require!))
(format "%s %s" (nth 1 sexp) (nth 2 sexp)))))))
((when buffer-file-name
(when-let (mod (doom-module-from-path buffer-file-name))
(unless (memq (car mod) '(:core :private))
(format "%s %s" (car mod) (cdr mod))))))
((when-let (mod (cdr (assq major-mode doom--help-major-mode-module-alist)))
(format "%s %s"
(symbol-name (car mod))
(symbol-name (cadr mod)))))))
;;;###autoload
(defun doom/help-modules (category module)
"Open the documentation for a Doom module.
CATEGORY is a keyword and MODULE is a symbol. e.g. :editor and 'evil.
Automatically selects a) the module at point (in private init files), b) the
module derived from a `featurep!' or `require!' call, c) the module that the
current file is in, or d) the module associated with the current major mode (see
`doom--help-major-mode-module-alist')."
(interactive
(mapcar #'intern
(split-string
(completing-read "Describe module: "
(doom--help-modules-list) nil t nil nil
(doom--help-current-module-str))
" " t)))
(cl-check-type category symbol)
(cl-check-type module symbol)
(cl-destructuring-bind (module-string path)
(or (assoc (format "%s %s" category module) (doom--help-modules-list))
(user-error "'%s %s' is not a valid module" category module))
(unless (file-readable-p path)
(error "Can't find or read %S module at %S" module-string path))
(if (not (file-directory-p path))
(find-file path)
(if (y-or-n-p (format "The %S module has no README file. Explore its directory?"
module-string))
(doom-project-browse path)
(user-error "Aborted module lookup")))))
;;
;;; `doom/help-packages'
(defun doom--help-package-insert-button (label path &optional regexp)
(declare (indent defun))
(insert-text-button
(string-trim label)
'face 'link
'follow-link t
'action
`(lambda (_)
(unless (file-exists-p ,path)
(user-error "Module doesn't exist"))
(when (window-dedicated-p)
(other-window 1))
(let ((buffer (find-file ,path)))
(when ,(stringp regexp)
(with-current-buffer buffer
(goto-char (point-min))
(if (re-search-forward ,regexp nil t)
(recenter)
(message "Couldn't find the config block"))))))))
(defun doom--help-package-configs (package)
(let ((default-directory doom-emacs-dir))
;; TODO Use ripgrep instead
(split-string
(cdr (doom-call-process
"git" "grep" "--no-break" "--no-heading" "--line-number"
(format "%s %s\\($\\| \\)"
"\\(^;;;###package\\|(after!\\|(use-package!\\)"
package)
":(exclude)*.org"))
"\n" t)))
;;;###autoload
(defun doom/help-packages (package)
"Like `describe-package', but for packages installed by Doom modules.
Only shows installed packages. Includes information about where packages are
defined and configured.
If prefix arg is present, refresh the cache."
(interactive
(let ((guess (or (function-called-at-point)
(symbol-at-point))))
(require 'finder-inf nil t)
(require 'core-packages)
(doom-initialize-packages)
(let ((packages (delete-dups
(append (mapcar #'car package-alist)
(mapcar #'car package--builtins)
(mapcar #'intern (hash-table-keys straight--build-cache))
(mapcar #'car (doom-package-list 'all))
nil))))
(unless (memq guess packages)
(setq guess nil))
(list
(intern
(completing-read (if guess
(format "Select package to search for (default %s): "
guess)
"Describe package: ")
packages nil t nil nil
(if guess (symbol-name guess))))))))
(require 'core-packages)
(doom-initialize-packages)
(if (or (package-desc-p package)
(and (symbolp package)
(or (assq package package-alist)
(assq package package--builtins))))
(describe-package package)
(help-setup-xref (list #'doom/help-packages package)
(called-interactively-p 'interactive))
(with-help-window (help-buffer)))
(save-excursion
(with-current-buffer (help-buffer)
(let ((inhibit-read-only t)
(indent (make-string 13 ? )))
(goto-char (point-max))
(if (re-search-forward "^ *Status: " nil t)
(progn
(end-of-line)
(insert "\n"))
(search-forward "\n\n" nil t))
(package--print-help-section "Package")
(insert (symbol-name package) "\n")
(package--print-help-section "Source")
(insert (or (pcase (doom-package-backend package)
(`straight
(format! "Straight (%s)\n%s"
(let ((default-directory (straight--build-dir (symbol-name package))))
(cdr
(doom-call-process "git" "log" "-1" "--format=%D %h %ci")))
(indent
13 (string-trim
(pp-to-string
(doom-package-build-recipe package))))))
(`elpa
(format "[M]ELPA %s" (doom--package-url package)))
(`builtin "Built-in")
(_ (abbreviate-file-name (symbol-file package))))
"unknown")
"\n")
(when (gethash (symbol-name package) straight--build-cache)
(package--print-help-section "Modules")
(insert "Declared by the following Doom modules:\n")
(dolist (m (doom-package-get package :modules))
(insert indent)
(doom--help-package-insert-button
(format "%s %s" (car m) (or (cdr m) ""))
(pcase (car m)
(:core doom-core-dir)
(:private doom-private-dir)
(category (doom-module-path category (cdr m)))))
(insert "\n")))
(package--print-help-section "Configs")
(insert "This package is configured in the following locations:")
(dolist (location (doom--help-package-configs package))
(insert "\n" indent)
(insert-text-button
location
'face 'link
'follow-link t
'action
`(lambda (_)
(cl-destructuring-bind (file line _match)
',(split-string location ":")
(find-file (expand-file-name file doom-emacs-dir))
(goto-char (point-min))
(forward-line (1- (string-to-number line)))
(recenter)))))
(insert "\n\n")))))
(defvar doom--package-cache nil)
(defun doom--package-list ()
(let* ((guess (or (function-called-at-point)
(symbol-at-point))))
(require 'finder-inf nil t)
(unless package--initialized
(package-initialize t))
(let ((packages (or doom--package-cache
(progn
(message "Reading packages...")
(cl-delete-duplicates
(append (mapcar 'car package-alist)
(mapcar 'car package--builtins)
(mapcar 'car package-archive-contents)))))))
(setq doom--package-cache packages)
(unless (memq guess packages)
(setq guess nil))
(intern (completing-read (if guess
(format "Select package to search for (default %s): "
guess)
"Describe package: ")
packages nil t nil nil
(if guess (symbol-name guess)))))))
(defun doom--package-url (package)
(cond ((assq package package--builtins)
(user-error "Package is built into Emacs and cannot be looked up"))
((when-let (location (locate-library (symbol-name package)))
(with-temp-buffer
(insert-file-contents (concat (file-name-sans-extension location) ".el")
nil 0 4096)
(let ((case-fold-search t))
(when (re-search-forward " \\(?:URL\\|homepage\\|Website\\): \\(http[^\n]+\\)\n" nil t)
(match-string-no-properties 1))))))
((and (ignore-errors (eq (doom-package-backend package) 'quelpa))
(let* ((plist (cdr (doom-package-prop package :recipe)))
(fetcher (plist-get plist :fetcher)))
(pcase fetcher
(`git (plist-get plist :url))
(`github (format "https://github.com/%s.git" (plist-get plist :repo)))
(`gitlab (format "https://gitlab.com/%s.git" (plist-get plist :repo)))
(`bitbucket (format "https://bitbucket.com/%s" (plist-get plist :repo)))
(`wiki (format "https://www.emacswiki.org/emacs/download/%s"
(or (car-safe (doom-enlist (plist-get plist :files)))
(format "%s.el" package))))
(_ (plist-get plist :url))))))
((and (require 'package nil t)
(or package-archive-contents (doom-refresh-packages-maybe))
(pcase (package-desc-archive (cadr (assq package package-archive-contents)))
("org" "https://orgmode.org")
((or "melpa" "melpa-mirror")
(format "https://melpa.org/#/%s" package))
("gnu"
(format "https://elpa.gnu.org/packages/%s.html" package))
(archive
(if-let (src (cdr (assoc package package-archives)))
(format "%s" src)
(user-error "%S isn't installed through any known source (%s)"
package archive))))))
((user-error "Cannot find the homepage for %S" package))))
;;;###autoload
(defun doom/help-package-config (package)
"Jump to any `use-package!', `after!' or ;;;###package block for PACKAGE.
This only searches `doom-emacs-dir' (typically ~/.emacs.d) and does not include
config blocks in your private config."
(interactive (list (doom--package-list)))
(cl-destructuring-bind (file line _match)
(split-string
(completing-read
"Jump to config: "
(or (doom--help-package-configs package)
(user-error "This package isn't configured by you or Doom")))
":")
(find-file (expand-file-name file doom-emacs-dir))
(goto-char (point-min))
(forward-line (1- line))
(recenter)))
;;;###autoload
(defun doom/help-package-homepage (package)
"Open PACKAGE's repo or homepage in your browser."
(interactive (list (doom--package-list)))
(browse-url (doom--package-url package)))
;;;###autoload
(defun doom/help-search-load-path (query)
"Perform a text search on your `load-path'.
Uses the symbol at point or the current selection, if available."
(interactive
(let ((query
;; TODO Generalize this later; into something the lookup module and
;; project search commands could as well
(if (use-region-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(or (symbol-name (symbol-at-point)) ""))))
(list (read-string
(format "Search load-path (default: %s): " query)
nil 'git-grep query))))
;; REVIEW Replace with deadgrep
(grep-find
(mapconcat
#'shell-quote-argument
(append (list "rg" "-L" "--search-zip" "--no-heading" "--color=never" query)
(cl-remove-if-not #'file-directory-p load-path))
" ")))
;; TODO factor our the duplicate code between this and the above
;;;###autoload
(defun doom/help-search-loaded-files (query)
"Perform a text search on your `load-path'.
Uses the symbol at point or the current selection, if available."
(interactive
(let ((query
;; TODO Generalize this later; into something the lookup module and
;; project search commands could as well.
(if (use-region-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(or (symbol-name (symbol-at-point)) ""))))
(list (read-string
(format "Search load-path (default: %s): " query)
nil 'git-grep query))))
(unless (executable-find "rg")
(user-error "Can't find ripgrep on your system"))
(require 'elisp-refs)
;; REVIEW Replace with deadgrep
(grep-find
(mapconcat
#'shell-quote-argument
(append (list "rg" "-L" "--search-zip" "--no-heading" "--color=never" query)
(cl-remove-if-not #'file-directory-p (elisp-refs--loaded-paths)))
" ")))

View File

@@ -0,0 +1,203 @@
;;; core/autoload/packages.el -*- lexical-binding: t; -*-
;;
;;; Package metadata
;;;###autoload
(defun doom-package-get (package &optional prop nil-value)
"Returns PACKAGE's `package!' recipe from `doom-packages'."
(let ((plist (cdr (assq package doom-packages))))
(if prop
(if (plist-member plist prop)
(plist-get plist prop)
nil-value)
plist)))
;;;###autoload
(defun doom-package-recipe (package &optional prop nil-value)
"Returns the `straight' recipe PACKAGE was registered with."
(let ((plist (gethash (symbol-name package) straight--recipe-cache)))
(if prop
(if (plist-member plist prop)
(plist-get plist prop)
nil-value)
plist)))
;;;###autoload
(defun doom-package-build-recipe (package &optional prop nil-value)
"Returns the `straight' recipe PACKAGE was installed with."
(let ((plist (nth 2 (gethash (symbol-name package) straight--build-cache))))
(if prop
(if (plist-member plist prop)
(plist-get plist prop)
nil-value)
plist)))
;;;###autoload
(defun doom-package-build-time (package)
"TODO"
(car (gethash (symbol-name package) straight--build-cache)))
;;;###autoload
(defun doom-package-dependencies (package &optional recursive noerror)
"Return a list of dependencies for a package."
(let ((deps (nth 1 (gethash (symbol-name package) straight--build-cache))))
(if recursive
(nconc deps (mapcan (lambda (dep) (doom-package-dependencies dep t t))
deps))
deps)))
(defun doom-package-depending-on (package &optional noerror)
"Return a list of packages that depend on the package named NAME."
(cl-check-type name symbol)
;; can't get dependencies for built-in packages
(unless (or (doom-package-build-recipe name)
noerror)
(error "Couldn't find %s, is it installed?" name))
(cl-loop for pkg in (hash-table-keys straight--build-cache)
for deps = (doom-package-dependencies pkg)
if (memq package deps)
collect pkg
and append (doom-package-depending-on pkg t)))
;;
;;; Predicate functions
;;;###autoload
(defun doom-package-built-in-p (package)
"Return non-nil if PACKAGE (a symbol) is built-in."
(eq (doom-package-build-recipe package :type)
'built-in))
;;;###autoload
(defun doom-package-installed-p (package)
"Return non-nil if PACKAGE (a symbol) is installed."
(file-directory-p (straight--build-dir (symbol-name package))))
;;;###autoload
(defun doom-package-registered-p (package)
"Return non-nil if PACKAGE (a symbol) has been registered with `package!'.
Excludes packages that have a non-nil :built-in property."
(when-let (plist (doom-package-get package))
(not (plist-get plist :ignore))))
;;;###autoload
(defun doom-package-private-p (package)
"Return non-nil if PACKAGE was installed by the user's private config."
(assq :private (doom-package-get package :modules)))
;;;###autoload
(defun doom-package-protected-p (package)
"Return non-nil if PACKAGE is protected.
A protected package cannot be deleted and will be auto-installed if missing."
(memq package doom-core-packages))
;;;###autoload
(defun doom-package-core-p (package)
"Return non-nil if PACKAGE is a core Doom package."
(or (doom-package-protected-p package)
(assq :core (doom-package-get package :modules))))
;;;###autoload
(defun doom-package-backend (package)
"Return 'straight, 'builtin, 'elpa or 'other, depending on how PACKAGE is
installed."
(cond ((gethash (symbol-name package) straight--build-cache)
'straight)
((or (doom-package-built-in-p package)
(assq package package--builtins))
'builtin)
((assq package package-alist)
'elpa)
('other)))
;;;###autoload
(defun doom-package-different-recipe-p (name)
"Return t if a package named NAME (a symbol) has a different recipe than it
was installed with."
(cl-check-type name symbol)
;; TODO
;; (when (doom-package-installed-p name)
;; (when-let* ((doom-recipe (assq name doom-packages))
;; (install-recipe (doom-package-recipe)))
;; (not (equal (cdr quelpa-recipe)
;; (cdr (plist-get (cdr doom-recipe) :recipe))))))
)
;;
;;; Package list getters
(defun doom--read-module-packages-file (file &optional noeval noerror)
(with-temp-buffer ; prevent buffer-local settings from propagating
(condition-case e
(if (not noeval)
(load file noerror t t)
(when (file-readable-p file)
(insert-file-contents file)
(delay-mode-hooks (emacs-lisp-mode))
(while (search-forward "(package! " nil t)
(save-excursion
(goto-char (match-beginning 0))
(unless (let ((ppss (syntax-ppss)))
(or (nth 3 ppss)
(nth 4 ppss)))
(cl-destructuring-bind (name . plist)
(cdr (sexp-at-point))
(push (cons
name (plist-put
plist :modules
(list (doom-module-from-path file))))
doom-packages)))))))
((debug error)
(signal 'doom-package-error
(list (doom-module-from-path file)
e))))))
;;;###autoload
(defun doom-package-list (&optional all-p)
"Retrieve a list of explicitly declared packages from enabled modules.
This excludes core packages listed in `doom-core-packages'.
If ALL-P, gather packages unconditionally across all modules, including disabled
ones."
(let ((doom-interactive-mode t)
(doom-modules (doom-modules))
doom-packages
doom-disabled-packages)
(doom--read-module-packages-file
(doom-path doom-core-dir "packages.el") all-p t)
(let ((private-packages (doom-path doom-private-dir "packages.el")))
(unless all-p
;; We load the private packages file twice to ensure disabled packages
;; are seen ASAP, and a second time to ensure privately overridden
;; packages are properly overwritten.
(doom--read-module-packages-file private-packages nil t))
(if all-p
(mapc #'doom--read-module-packages-file
(doom-files-in doom-modules-dir
:depth 2
:match "/packages\\.el$"))
(cl-loop for key being the hash-keys of doom-modules
for path = (doom-module-path (car key) (cdr key) "packages.el")
for doom--current-module = key
do (doom--read-module-packages-file path nil t)))
(doom--read-module-packages-file private-packages all-p t))
(nreverse doom-packages)))
;;
;;; Main functions
;;;###autoload
(defun doom/reload-packages ()
"Reload `doom-packages', `package' and `quelpa'."
(interactive)
;; HACK straight.el must be loaded for this to work
(message "Reloading packages")
(doom-initialize-packages t)
(message "Reloading packages...DONE"))

View File

@@ -0,0 +1,93 @@
;;; core/autoload/plist.el -*- lexical-binding: t; -*-
;;
;;; Macros
;;;###autoload
(cl-defmacro doplist! ((arglist plist &optional retval) &rest body)
"Loop over a PLIST's (property value) pairs then return RETVAL.
Evaluate BODY with either ARGLIST bound to (cons PROP VAL) or, if ARGLIST is a
list, the pair is destructured into (CAR . CDR)."
(declare (indent 1))
(let ((plist-var (make-symbol "plist")))
`(let ((,plist-var (copy-sequence ,plist)))
(while ,plist-var
(let ,(if (listp arglist)
`((,(pop arglist) (pop ,plist-var))
(,(pop arglist) (pop ,plist-var)))
`((,arglist (cons (pop ,plist-var)
(pop ,plist-var)))))
,@body))
,retval)))
;;;###autoload
(defmacro plist-put! (plist &rest rest)
"Set each PROP VALUE pair in REST to PLIST in-place."
`(cl-loop for (prop value)
on (list ,@rest) by #'cddr
do ,(if (symbolp plist)
`(setq ,plist (plist-put ,plist prop value))
`(plist-put ,plist prop value))))
;;;###autoload
(defmacro plist-delete! (plist prop)
"Delete PROP from PLIST in-place."
`(setq ,plist (doom-plist-delete ,plist ,prop)))
;;;###autoload
(defmacro with-plist! (plist props &rest body)
"With props bound from PLIST to PROPS, evaluate BODY.
PROPS is a list of symbols. Each one is converted to a keyword and then its
value is looked up in the PLIST and bound to the symbol for the duration of
BODY."
(declare (indent 2))
(let ((plist-sym (make-symbol "plist")))
`(let* ((,plist-sym ,plist)
,@(cl-loop for prop in props
collect
`(,prop
(plist-get
,plist-sym
,(doom-keyword-intern (symbol-name prop))))))
,@body)))
;;
;;; Library
;;;###autoload
(defun doom-plist-get (plist prop &optional nil-value)
"Return PROP in PLIST, if it exists. Otherwise NIL-VALUE."
(if-let (val (plist-member plist prop))
(cadr val)
nil-value))
;;;###autoload
(defun doom-plist-merge (from-plist to-plist)
"Destructively merge FROM-PLIST onto TO-PLIST"
(let ((plist (copy-sequence from-plist)))
(while plist
(plist-put! to-plist (pop plist) (pop plist)))
to-plist))
;;;###autoload
(defun doom-plist-delete-nil (plist)
"Delete `nil' properties from a copy of PLIST."
(let (p)
(while plist
(if (car plist)
(plist-put! p (car plist) (nth 1 plist)))
(setq plist (cddr plist)))
p))
;;;###autoload
(defun doom-plist-delete (plist prop)
"Delete PROP from a copy of PLIST."
(let (p)
(while plist
(if (not (eq prop (car plist)))
(plist-put! p (car plist) (nth 1 plist)))
(setq plist (cddr plist)))
p))

View File

@@ -0,0 +1,135 @@
;;; core/autoload/projects.el -*- lexical-binding: t; -*-
(defvar projectile-project-root nil)
;;;###autoload (autoload 'projectile-relevant-known-projects "projectile")
;;;###autodef
(cl-defun set-project-type! (name &key predicate compile run test configure dir)
"Add a project type to `projectile-project-type'."
(declare (indent 1))
(after! projectile
(add-to-list 'projectile-project-types
(list name
'marker-files predicate
'compilation-dir dir
'configure-command configure
'compile-command compile
'test-command test
'run-command run))))
;;
;;; Macros
;;;###autoload
(defmacro project-file-exists-p! (files)
"Checks if the project has the specified FILES.
Paths are relative to the project root, unless they start with ./ or ../ (in
which case they're relative to `default-directory'). If they start with a slash,
they are absolute."
`(file-exists-p! ,files (doom-project-root)))
;;
;;; Commands
;;;###autoload
(defun doom/find-file-in-other-project (project-root)
"Preforms `projectile-find-file' in a known project of your choosing."
(interactive
(list
(completing-read "Find file in project: " (projectile-relevant-known-projects)
nil nil nil nil (doom-project-root))))
(unless (file-directory-p project-root)
(error "Project directory '%s' doesn't exist" project-root))
(doom-project-find-file project-root))
;;;###autoload
(defun doom/browse-in-other-project (project-root)
"Preforms `find-file' in a known project of your choosing."
(interactive
(list
(completing-read "Browse in project: " (projectile-relevant-known-projects)
nil nil nil nil (doom-project-root))))
(unless (file-directory-p project-root)
(error "Project directory '%s' doesn't exist" project-root))
(doom-project-browse project-root))
;;
;;; Library
;;;###autoload
(defun doom-project-p (&optional dir)
"Return t if DIR (defaults to `default-directory') is a valid project."
(and (doom-project-root dir)
t))
;;;###autoload
(defun doom-project-root (&optional dir)
"Return the project root of DIR (defaults to `default-directory').
Returns nil if not in a project."
(let ((projectile-project-root (unless dir projectile-project-root))
projectile-require-project-root)
(projectile-project-root dir)))
;;;###autoload
(defun doom-project-name (&optional dir)
"Return the name of the current project.
Returns '-' if not in a valid project."
(if-let (project-root (or (doom-project-root dir)
(if dir (expand-file-name dir))))
(funcall projectile-project-name-function project-root)
"-"))
;;;###autoload
(defun doom-project-expand (name &optional dir)
"Expand NAME to project root."
(expand-file-name name (doom-project-root dir)))
;;;###autoload
(defun doom-project-find-file (dir)
"Jump to a file in DIR (searched recursively).
If DIR is not a project, it will be indexed (but not cached)."
(unless (file-directory-p dir)
(error "Directory %S does not exist" dir))
(unless (file-readable-p dir)
(error "Directory %S isn't readable" dir))
(let* ((default-directory (file-truename (expand-file-name dir)))
(project-root (doom-project-root default-directory))
(projectile-project-root default-directory)
(projectile-enable-caching projectile-enable-caching))
(cond ((and project-root (file-equal-p project-root projectile-project-root))
(unless (doom-project-p projectile-project-root)
;; Disable caching if this is not a real project; caching
;; non-projects easily has the potential to inflate the projectile
;; cache beyond reason.
(setq projectile-enable-caching nil))
(call-interactively
;; Intentionally avoid `helm-projectile-find-file', because it runs
;; asynchronously, and thus doesn't see the lexical
;; `default-directory'
(if (doom-module-p :completion 'ivy)
#'counsel-projectile-find-file
#'projectile-find-file)))
((fboundp 'counsel-file-jump) ; ivy only
(call-interactively #'counsel-file-jump))
((project-current)
(project-find-file-in nil (list default-directory) nil))
((fboundp 'helm-find-files)
(call-interactively #'helm-find-files))
((call-interactively #'find-file)))))
;;;###autoload
(defun doom-project-browse (dir)
"Traverse a file structure starting linearly from DIR."
(let ((default-directory (file-truename (expand-file-name dir))))
(call-interactively
(cond ((doom-module-p :completion 'ivy)
#'counsel-find-file)
((doom-module-p :completion 'helm)
#'helm-find-files)
(#'find-file)))))

View File

@@ -0,0 +1,173 @@
;;; core/autoload/scratch.el -*- lexical-binding: t; -*-
(defvar doom-scratch-default-file "__default"
"The default file name for a project-less scratch buffer.
Will be saved in `doom-scratch-dir'.")
(defvar doom-scratch-dir (concat doom-etc-dir "scratch")
"Where to save persistent scratch buffers.")
(defvar doom-scratch-buffer-major-mode nil
"What major mode to use in scratch buffers. This can be one of the
following:
t Inherits the major mode of the last buffer you had selected.
nil Uses `fundamental-mode'
MAJOR-MODE Any major mode symbol")
(defvar doom-scratch-buffers nil
"A list of active scratch buffers.")
(defvar doom-scratch-current-project nil
"The name of the project associated with the current scratch buffer.")
(defvar doom-scratch-buffer-hook ()
"The hooks to run after a scratch buffer is created.")
(defun doom--load-persistent-scratch-buffer (name)
(setq-local doom-scratch-current-project
(or name
doom-scratch-default-file))
(let ((scratch-file
(expand-file-name doom-scratch-current-project
doom-scratch-dir)))
(make-directory doom-scratch-dir t)
(when (file-readable-p scratch-file)
(erase-buffer)
(insert-file-contents scratch-file)
(set-auto-mode)
t)))
;;;###autoload
(defun doom-scratch-buffer (&optional mode directory project-name)
"Return a scratchpad buffer in major MODE."
(with-current-buffer
(get-buffer-create (if project-name
(format "*doom:scratch (%s)*" project-name)
"*doom:scratch*"))
(setq default-directory directory)
(unless doom-scratch-current-project
(doom--load-persistent-scratch-buffer project-name)
(when (and (eq major-mode 'fundamental-mode)
(functionp mode))
(funcall mode)))
(cl-pushnew (current-buffer) doom-scratch-buffers)
(add-hook 'kill-buffer-hook #'doom-persist-scratch-buffer-h nil 'local)
(add-hook 'doom-switch-buffer-hook #'doom-persist-scratch-buffers-after-switch-h)
(run-hooks 'doom-scratch-buffer-created-hook)
(current-buffer)))
;;
;;; Persistent scratch buffer
;;;###autoload
(defun doom-persist-scratch-buffer-h ()
"Save the current buffer to `doom-scratch-dir'."
(write-region
(point-min) (point-max)
(expand-file-name (or doom-scratch-current-project
doom-scratch-default-file)
doom-scratch-dir)))
;;;###autoload
(defun doom-persist-scratch-buffers-h ()
"Save all scratch buffers to `doom-scratch-dir'."
(setq doom-scratch-buffers
(cl-delete-if-not #'buffer-live-p doom-scratch-buffers))
(dolist (buffer doom-scratch-buffers)
(with-current-buffer buffer
(doom-persist-scratch-buffer-h))))
;;;###autoload
(defun doom-persist-scratch-buffers-after-switch-h ()
"Kill scratch buffers when they are no longer visible, saving them to disk."
(unless (cl-some #'get-buffer-window doom-scratch-buffers)
(mapc #'kill-buffer doom-scratch-buffers)
(remove-hook 'doom-switch-buffer-hook #'doom-persist-scratch-buffers-after-switch-h)))
;;;###autoload
(unless noninteractive
(add-hook 'kill-emacs-hook #'doom-persist-scratch-buffers-h))
;;
;;; Commands
;;;###autoload
(defun doom/open-scratch-buffer (&optional arg project-p)
"Opens the (persistent) scratch buffer in a popup.
If passed the prefix ARG, switch to it in the current window.
If PROJECT-P is non-nil, open a persistent scratch buffer associated with the
current project."
(interactive "P")
(let (projectile-enable-caching)
(funcall
(if arg
#'switch-to-buffer
#'pop-to-buffer)
(doom-scratch-buffer
(cond ((eq doom-scratch-buffer-major-mode t)
(unless (or buffer-read-only
(derived-mode-p 'special-mode)
(string-match-p "^ ?\\*" (buffer-name)))
major-mode))
((null doom-scratch-buffer-major-mode)
nil)
((symbolp doom-scratch-buffer-major-mode)
doom-scratch-buffer-major-mode))
default-directory
(when project-p
(doom-project-name))))))
;;;###autoload
(defun doom/switch-to-scratch-buffer (&optional project-p)
"Like `doom/open-scratch-buffer', but switches to it in the current window.
If passed the prefix arg, open project scratch buffer."
(interactive "P")
(doom/open-scratch-buffer t project-p))
;;;###autoload
(defun doom/open-project-scratch-buffer (&optional current-window)
"Opens the (persistent) project scratch buffer in a popup.
If passed the prefix arg, switch to it in the current window."
(interactive "P")
(doom/open-scratch-buffer current-window 'project))
;;;###autoload
(defun doom/switch-to-project-scratch-buffer ()
"Like `doom/open-project-scratch-buffer', but switches to it in the current
window."
(interactive)
(doom/open-project-scratch-buffer t))
;;;###autoload
(defun doom/revert-scratch-buffer ()
"Revert scratch buffer to last persistent state."
(interactive)
(unless (string-match-p "^\\*doom:scratch" (buffer-name))
(user-error "Not in a scratch buffer"))
(when (doom--load-persistent-scratch-buffer doom-scratch-current-project)
(message "Reloaded scratch buffer")))
;;;###autoload
(defun doom/delete-persistent-scratch-file (&optional arg)
"Deletes a scratch buffer file in `doom-scratch-dir'.
If prefix ARG, delete all persistent scratches."
(interactive)
(if arg
(progn
(delete-directory doom-scratch-dir t)
(message "Cleared %S" (abbreviate-file-name doom-scratch-dir)))
(make-directory doom-scratch-dir t)
(let ((file (read-file-name "Delete scratch file > " doom-scratch-dir "scratch")))
(if (not (file-exists-p file))
(message "%S does not exist" (abbreviate-file-name file))
(delete-file file)
(message "Successfully deleted %S" (abbreviate-file-name file))))))

View File

@@ -0,0 +1,128 @@
;;; core/autoload/sessions.el -*- lexical-binding: t; -*-
(defvar desktop-base-file-name)
(defvar desktop-dirname)
(defvar desktop-restore-eager)
(defvar desktop-file-modtime)
;;
;;; Helpers
;;;###autoload
(defun doom-session-file (&optional name)
"TODO"
(cond ((require 'persp-mode nil t)
(expand-file-name (or name persp-auto-save-fname) persp-save-dir))
((require 'desktop nil t)
(if name
(expand-file-name name (file-name-directory (desktop-full-file-name)))
(desktop-full-file-name)))
((error "No session backend available"))))
;;;###autoload
(defun doom-save-session (&optional file)
"TODO"
(setq file (expand-file-name (or file (doom-session-file))))
(cond ((require 'persp-mode nil t)
(unless persp-mode (persp-mode +1))
(setq persp-auto-save-opt 0)
(persp-save-state-to-file file))
((and (require 'frameset nil t)
(require 'restart-emacs nil t))
(let ((frameset-filter-alist (append '((client . restart-emacs--record-tty-file))
frameset-filter-alist))
(desktop-base-file-name (file-name-nondirectory file))
(desktop-dirname (file-name-directory file))
(desktop-restore-eager t)
desktop-file-modtime)
(make-directory desktop-dirname t)
;; Prevents confirmation prompts
(let ((desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
(desktop-save desktop-dirname t))))
((error "No session backend to save session with"))))
;;;###autoload
(defun doom-load-session (&optional file)
"TODO"
(setq file (expand-file-name (or file (doom-session-file))))
(message "Attempting to load %s" file)
(cond ((require 'persp-mode nil t)
(unless persp-mode
(persp-mode +1))
(persp-load-state-from-file file))
((and (require 'frameset nil t)
(require 'restart-emacs nil t))
(restart-emacs--restore-frames-using-desktop file))
((error "No session backend to load session with"))))
;;
;;; Command line switch
;;;###autoload
(defun doom-restore-session-handler (&rest _)
"TODO"
(add-hook 'window-setup-hook #'doom-load-session 'append))
;;
;;; Commands
;;;###autoload
(defun doom/quickload-session ()
"TODO"
(interactive)
(message "Restoring session...")
(doom-load-session)
(message "Session restored. Welcome back."))
;;;###autoload
(defun doom/quicksave-session ()
"TODO"
(interactive)
(message "Saving session")
(doom-save-session)
(message "Saving session...DONE"))
;;;###autoload
(defun doom/load-session (file)
"TODO"
(interactive
(let ((session-file (doom-session-file)))
(list (or (read-file-name "Session to restore: "
(file-name-directory session-file)
(file-name-nondirectory session-file)
t)
(user-error "No session selected. Aborting")))))
(unless file
(error "No session file selected"))
(message "Loading '%s' session" file)
(doom-load-session file)
(message "Session restored. Welcome back."))
;;;###autoload
(defun doom/save-session (file)
"TODO"
(interactive
(let ((session-file (doom-session-file)))
(list (or (read-file-name "Save session to: "
(file-name-directory session-file)
(file-name-nondirectory session-file))
(user-error "No session selected. Aborting")))))
(unless file
(error "No session file selected"))
(message "Saving '%s' session" file)
(doom-save-session file))
;;;###autoload
(defalias 'doom/restart #'restart-emacs)
;;;###autoload
(defun doom/restart-and-restore (&optional debug)
"TODO"
(interactive "P")
(setq doom-autosave-session nil)
(doom/quicksave-session)
(restart-emacs
(delq nil (list (if debug "--debug-init") "--restore"))))

View File

@@ -0,0 +1,274 @@
;;; core/autoload/text.el -*- lexical-binding: t; -*-
;;;###autoload
(defun doom-surrounded-p (pair &optional inline balanced)
"Returns t if point is surrounded by a brace delimiter: {[(
If INLINE is non-nil, only returns t if braces are on the same line, and
whitespace is balanced on either side of the cursor.
If INLINE is nil, returns t if the opening and closing braces are on adjacent
lines, above and below, with only whitespace in between."
(when pair
(let ((beg (plist-get pair :beg))
(end (plist-get pair :end))
(pt (point)))
(when (and (> pt beg) (< pt end))
(when-let* ((cl (plist-get pair :cl))
(op (plist-get pair :op)))
(and (not (string= op ""))
(not (string= cl ""))
(let ((nbeg (+ (length op) beg))
(nend (- end (length cl))))
(let ((content (buffer-substring-no-properties nbeg nend)))
(and (string-match-p (format "[ %s]*" (if inline "" "\n")) content)
(or (not balanced)
(= (- pt nbeg) (- nend pt))))))))))))
;;;###autoload
(defun doom-point-in-comment-p (&optional pos)
"Return non-nil if POS is in a comment.
POS defaults to the current position."
;; REVIEW Should we cache `syntax-ppss'?
(let* ((pos (or pos (point)))
(ppss (syntax-ppss pos)))
(or (nth 4 ppss)
(nth 8 ppss)
(and (< pos (point-max))
(memq (char-syntax (char-after pos)) '(?< ?>))
(not (eq (char-after pos) ?\n)))
(when-let (s (car (syntax-after pos)))
(or (and (/= 0 (logand (lsh 1 16) s))
(nth 4 (doom-syntax-ppss (+ pos 2))))
(and (/= 0 (logand (lsh 1 17) s))
(nth 4 (doom-syntax-ppss (+ pos 1))))
(and (/= 0 (logand (lsh 1 18) s))
(nth 4 (doom-syntax-ppss (- pos 1))))
(and (/= 0 (logand (lsh 1 19) s))
(nth 4 (doom-syntax-ppss (- pos 2)))))))))
;;;###autoload
(defun doom-point-in-string-p (&optional pos)
"Return non-nil if POS is in a string."
;; REVIEW Should we cache `syntax-ppss'?
(nth 3 (syntax-ppss pos)))
;;;###autoload
(defun doom-point-in-string-or-comment-p (&optional pos)
"Return non-nil if POS is in a string or comment."
(or (doom-point-in-string-p pos)
(doom-point-in-comment-p pos)))
;;
;;; Commands
(defvar doom--last-backward-pt most-positive-fixnum)
;;;###autoload
(defun doom/backward-to-bol-or-indent ()
"Jump between the indentation column (first non-whitespace character) and the
beginning of the line. The opposite of
`doom/forward-to-last-non-comment-or-eol'."
(interactive)
(let ((pt (point)))
(cl-destructuring-bind (bol . bot)
(save-excursion
(beginning-of-visual-line)
(cons (point)
(progn (skip-chars-forward " \t\r")
(point))))
(cond ((> pt bot)
(goto-char bot))
((= pt bol)
(goto-char (min doom--last-backward-pt bot))
(setq doom--last-backward-pt most-positive-fixnum))
((<= pt bot)
(setq doom--last-backward-pt pt)
(goto-char bol))))))
(defvar doom--last-forward-pt -1)
;;;###autoload
(defun doom/forward-to-last-non-comment-or-eol ()
"Jumps between the last non-blank, non-comment character in the line and the
true end of the line. The opposite of `doom/backward-to-bol-or-indent'."
(interactive)
(let ((eol (if (not visual-line-mode)
(line-end-position)
(save-excursion (end-of-visual-line) (point)))))
(if (or (and (< (point) eol)
(sp-point-in-comment))
(not (sp-point-in-comment eol)))
(if (= (point) eol)
(progn
(goto-char doom--last-forward-pt)
(setq doom--last-forward-pt -1))
(setq doom--last-forward-pt (point))
(goto-char eol))
(let* ((bol (save-excursion (beginning-of-visual-line) (point)))
(boc (or (save-excursion
(if (not comment-use-syntax)
(progn
(goto-char bol)
(when (re-search-forward comment-start-skip eol t)
(or (match-end 1) (match-beginning 0))))
(goto-char eol)
(while (and (sp-point-in-comment)
(> (point) bol))
(backward-char))
(skip-chars-backward " " bol)
(point)))
eol)))
(when (> doom--last-forward-pt boc)
(setq boc doom--last-forward-pt))
(if (or (= eol (point))
(> boc (point)))
(progn
(goto-char boc)
(setq doom--last-forward-pt -1))
(setq doom--last-forward-pt (point))
(goto-char eol))))))
;;;###autoload
(defun doom/dumb-indent ()
"Inserts a tab character (or spaces x tab-width)."
(interactive)
(if indent-tabs-mode
(insert "\t")
(let* ((movement (% (current-column) tab-width))
(spaces (if (= 0 movement) tab-width (- tab-width movement))))
(insert (make-string spaces ? )))))
;;;###autoload
(defun doom/dumb-dedent ()
"Dedents the current line."
(interactive)
(if indent-tabs-mode
(call-interactively #'backward-delete-char)
(unless (bolp)
(save-excursion
(when (> (current-column) (current-indentation))
(back-to-indentation))
(let ((movement (% (current-column) tab-width)))
(delete-char
(- (if (= 0 movement)
tab-width
(- tab-width movement)))))))))
;;;###autoload
(defun doom/backward-kill-to-bol-and-indent ()
"Kill line to the first non-blank character. If invoked again
afterwards, kill line to beginning of line."
(interactive)
(let ((empty-line-p (save-excursion (beginning-of-line)
(looking-at-p "[ \t]*$"))))
(funcall (if (fboundp 'evil-delete)
#'evil-delete
#'delete-region)
(point-at-bol) (point))
(unless empty-line-p
(indent-according-to-mode))))
;;;###autoload
(defun doom/retab (arg &optional beg end)
"Converts tabs-to-spaces or spaces-to-tabs within BEG and END (defaults to
buffer start and end, to make indentation consistent. Which it does depends on
the value of `indent-tab-mode'.
If ARG (universal argument) is non-nil, retab the current buffer using the
opposite indentation style."
(interactive "Pr")
(unless (and beg end)
(setq beg (point-min)
end (point-max)))
(let ((indent-tabs-mode (if arg (not indent-tabs-mode) indent-tabs-mode)))
(if indent-tabs-mode
(tabify beg end)
(untabify beg end))))
;;;###autoload
(defun doom/delete-trailing-newlines ()
"Trim trailing newlines.
Respects `require-final-newline'."
(interactive)
(goto-char (point-max))
(skip-chars-backward " \t\n\v")
(when (looking-at "\n\\(\n\\|\\'\\)")
(forward-char 1))
(when require-final-newline
(unless (bolp)
(insert "\n")))
(when (looking-at "\n+")
(replace-match "")))
;;;###autoload
(defun doom/dos2unix ()
"Convert the current buffer to a Unix file encoding."
(interactive)
(set-buffer-file-coding-system 'undecided-unix nil))
;;;###autoload
(defun doom/unix2dos ()
"Convert the current buffer to a DOS file encoding."
(interactive)
(set-buffer-file-coding-system 'undecided-dos nil))
;;;###autoload
(defun doom/toggle-indent-style ()
"Switch between tabs and spaces indentation style in the current buffer."
(interactive)
(setq indent-tabs-mode (not indent-tabs-mode))
(message "Indent style changed to %s" (if indent-tabs-mode "tabs" "spaces")))
(defvar editorconfig-lisp-use-default-indent)
;;;###autoload
(defun doom/set-indent-width (width)
"Change the indentation size to WIDTH of the current buffer.
The effectiveness of this command is significantly improved if you have
editorconfig or dtrt-indent installed."
(interactive
(list (if (integerp current-prefix-arg)
current-prefix-arg
(read-number "New indent size: "))))
(setq tab-width width)
(setq-local standard-indent width)
(when (boundp 'evil-shift-width)
(setq evil-shift-width width))
(cond ((require 'editorconfig nil t)
(let (editorconfig-lisp-use-default-indent)
(editorconfig-set-indentation nil width)))
((require 'dtrt-indent nil t)
(when-let (var (nth 2 (assq major-mode dtrt-indent-hook-mapping-list)))
(doom-log "Updated %s = %d" var width)
(set var width))))
(message "Changed indentation to %d" width))
;;
;;; Hooks
;;;###autoload
(defun doom-enable-delete-trailing-whitespace-h ()
"Enables the automatic deletion of trailing whitespaces upon file save.
i.e. enables `ws-butler-mode' in the current buffer."
(ws-butler-mode +1))
;;;###autoload
(defun doom-disable-delete-trailing-whitespace-h ()
"Disables the automatic deletion of trailing whitespaces upon file save.
i.e. disables `ws-butler-mode' in the current buffer."
(ws-butler-mode -1))
;;;###autoload
(defun doom-enable-show-trailing-whitespace-h ()
"Enable `show-trailing-whitespace' in the current buffer."
(setq-local show-trailing-whitespace t))
;;;###autoload
(defun doom-disable-show-trailing-whitespace-h ()
"Disable `show-trailing-whitespace' in the current buffer."
(setq-local show-trailing-whitespace nil))

View File

@@ -0,0 +1,51 @@
;;; core/autoload/themes.el -*- lexical-binding: t; -*-
(defun doom--custom-theme-set-face (spec)
(cond ((listp (car spec))
(cl-loop for face in (car spec)
collect
(car (doom--custom-theme-set-face (cons face (cdr spec))))))
((keywordp (cadr spec))
`((,(car spec) ((t ,(cdr spec))))))
(`((,(car spec) ,(cdr spec))))))
;;;###autoload
(defmacro custom-theme-set-faces! (theme &rest specs)
"Apply a list of face SPECS as user customizations for THEME.
THEME can be a single symbol or list thereof. If nil, apply these settings to
all themes. It will apply to all themes once they are loaded."
(declare (indent defun))
`(let ((fn (gensym "doom--customize-themes-h-")))
(fset
fn (lambda ()
(let (custom--inhibit-theme-enable)
(dolist (theme (doom-enlist (or ,theme 'user)))
(when (or (eq theme 'user)
(custom-theme-enabled-p theme))
(apply #'custom-theme-set-faces theme
(mapcan #'doom--custom-theme-set-face
(list ,@specs))))))))
(when (or doom-init-theme-p (null doom-theme))
(funcall fn))
(add-hook 'doom-load-theme-hook fn 'append)))
;;;###autoload
(defmacro custom-set-faces! (&rest specs)
"Apply a list of face SPECS as user customizations.
This is a drop-in replacement for `custom-set-face' that allows for a simplified
face format."
(declare (indent defun))
`(custom-theme-set-faces! 'user ,@specs))
(defvar doom--prefer-theme-elc)
;;;###autoload
(defun doom/reload-theme ()
"Reload the current color theme."
(interactive)
(let ((theme (or (car-safe custom-enabled-themes) doom-theme)))
(when theme
(mapc #'disable-theme custom-enabled-themes))
(load-theme doom-theme 'noconfirm)
(doom/reload-font)))

View File

@@ -0,0 +1,232 @@
;;; 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-a (&rest _)
"Generic advisor for recentering window (typically :after other functions)."
(recenter))
;;;###autoload
(defun doom-shut-up-a (orig-fn &rest args)
"Generic advisor for silencing noisy functions.
In interactive Emacs, this just inhibits messages from appearing in the
minibuffer. They are still logged to *Messages*.
In tty Emacs, messages suppressed completely."
(quiet! (apply orig-fn args)))
;;
;; Hooks
;;;###autoload
(defun doom-apply-ansi-color-to-compilation-buffer-h ()
"Applies ansi codes to the compilation buffers. Meant for
`compilation-filter-hook'."
(with-silent-modifications
(ansi-color-apply-on-region compilation-filter-start (point))))
;;;###autoload
(defun doom-disable-show-paren-mode-h ()
"Turn off `show-paren-mode' buffer-locally."
(setq-local show-paren-mode nil))
;;
;; Commands
;;;###autoload
(defun doom/toggle-line-numbers ()
"Toggle line numbers.
Cycles through regular, relative and no line numbers. The order depends on what
`display-line-numbers-type' is set to. If you're using Emacs 26+, and
visual-line-mode is on, this skips relative and uses visual instead.
See `display-line-numbers' for what these values mean."
(interactive)
(defvar doom--line-number-style display-line-numbers-type)
(let* ((styles `(t ,(if visual-line-mode 'visual 'relative) nil))
(order (cons display-line-numbers-type (remq display-line-numbers-type styles)))
(queue (memq doom--line-number-style order))
(next (if (= (length queue) 1)
(car order)
(car (cdr queue)))))
(setq doom--line-number-style next)
(setq display-line-numbers next)
(message "Switched to %s line numbers"
(pcase next
(`t "normal")
(`nil "disabled")
(_ (symbol-name next))))))
;;;###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-maximize-buffer ()
"Close other windows to focus on this one. Activate again to undo this. If the
window changes before then, the undo expires.
Alternatively, use `doom/window-enlargen'."
(interactive)
(if (and (one-window-p)
(assq ?_ register-alist))
(jump-to-register ?_)
(when (and (bound-and-true-p +popup-mode)
(+popup-window-p))
(user-error "Cannot maximize a popup, use `+popup/raise' first or use `doom/window-enlargen' instead"))
(window-configuration-to-register ?_)
(delete-other-windows)))
(defvar doom--window-enlargened nil)
;;;###autoload
(defun doom/window-enlargen ()
"Enlargen the current window to focus on this one. Does not close other
windows (unlike `doom/window-maximize-buffer') Activate again to undo."
(interactive)
(setq doom--window-enlargened
(if (and doom--window-enlargened
(assq ?_ register-alist))
(ignore (ignore-errors (jump-to-register ?_)))
(window-configuration-to-register ?_)
(let* ((window (selected-window))
(dedicated-p (window-dedicated-p window))
(preserved-p (window-parameter window 'window-preserved-size))
(ignore-window-parameters t))
(unwind-protect
(progn
(when dedicated-p
(set-window-dedicated-p window nil))
(when preserved-p
(set-window-parameter window 'window-preserved-size nil))
(maximize-window window))
(set-window-dedicated-p window dedicated-p)
(when preserved-p
(set-window-parameter window 'window-preserved-size preserved-p)))
t))))
;;;###autoload
(defun doom/window-maximize-horizontally ()
"Delete all windows to the left and right of the current window."
(interactive)
(require 'windmove)
(save-excursion
(while (ignore-errors (windmove-left)) (delete-window))
(while (ignore-errors (windmove-right)) (delete-window))))
;;;###autoload
(defun doom/window-maximize-vertically ()
"Delete all windows above and below the current window."
(interactive)
(require 'windmove)
(save-excursion
(while (ignore-errors (windmove-up)) (delete-window))
(while (ignore-errors (windmove-down)) (delete-window))))
;;;###autoload
(defun doom/set-frame-opacity (opacity)
"Interactively change the current frame's opacity.
OPACITY is an integer between 0 to 100, inclusive."
(interactive
(list (read-number "Opacity (0-100): "
(or (frame-parameter nil 'alpha)
100))))
(set-frame-parameter nil 'alpha opacity))
(defvar doom--narrowed-base-buffer nil)
;;;###autoload
(defun doom/narrow-buffer-indirectly (beg end)
"Restrict editing in this buffer to the current region, indirectly.
This recursively creates indirect clones of the current buffer so that the
narrowing doesn't affect other windows displaying the same buffer. Call
`doom/widen-indirectly-narrowed-buffer' to undo it (incrementally).
Inspired from http://demonastery.org/2013/04/emacs-evil-narrow-region/"
(interactive
(list (or (bound-and-true-p evil-visual-beginning) (region-beginning))
(or (bound-and-true-p evil-visual-end) (region-end))))
(unless (region-active-p)
(setq beg (line-beginning-position)
end (line-end-position)))
(deactivate-mark)
(let ((orig-buffer (current-buffer)))
(with-current-buffer (switch-to-buffer (clone-indirect-buffer nil nil))
(narrow-to-region beg end)
(setq-local doom--narrowed-base-buffer orig-buffer))))
;;;###autoload
(defun doom/widen-indirectly-narrowed-buffer (&optional arg)
"Widens narrowed buffers.
This command will incrementally kill indirect buffers (under the assumption they
were created by `doom/narrow-buffer-indirectly') and switch to their base
buffer.
If ARG, then kill all indirect buffers, return the base buffer and widen it.
If the current buffer is not an indirect buffer, it is `widen'ed."
(interactive "P")
(unless (buffer-narrowed-p)
(user-error "Buffer isn't narrowed"))
(let ((orig-buffer (current-buffer))
(base-buffer doom--narrowed-base-buffer))
(cond ((or (not base-buffer)
(not (buffer-live-p base-buffer)))
(widen))
(arg
(let ((buffer orig-buffer)
(buffers-to-kill (list orig-buffer)))
(while (setq buffer (buffer-local-value 'doom--narrowed-base-buffer buffer))
(push buffer buffers-to-kill))
(switch-to-buffer (buffer-base-buffer))
(mapc #'kill-buffer (remove (current-buffer) buffers-to-kill))))
((switch-to-buffer base-buffer)
(kill-buffer orig-buffer)))))
;;;###autoload
(defun doom/toggle-narrow-buffer (beg end)
"Narrow the buffer to BEG END. If narrowed, widen it."
(interactive
(list (or (bound-and-true-p evil-visual-beginning) (region-beginning))
(or (bound-and-true-p evil-visual-end) (region-end))))
(if (buffer-narrowed-p)
(widen)
(unless (region-active-p)
(setq beg (line-beginning-position)
end (line-end-position)))
(narrow-to-region beg end)))