diff options
Diffstat (limited to 'doc/misc/use-package.texi')
| -rw-r--r-- | doc/misc/use-package.texi | 946 |
1 files changed, 946 insertions, 0 deletions
diff --git a/doc/misc/use-package.texi b/doc/misc/use-package.texi new file mode 100644 index 00000000000..573baac89aa --- /dev/null +++ b/doc/misc/use-package.texi | |||
| @@ -0,0 +1,946 @@ | |||
| 1 | \input texinfo @c -*- texinfo -*- | ||
| 2 | @c %**start of header | ||
| 3 | @setfilename use-package.info | ||
| 4 | @settitle use-package User Manual | ||
| 5 | @documentencoding UTF-8 | ||
| 6 | @documentlanguage en | ||
| 7 | @c %**end of header | ||
| 8 | |||
| 9 | @copying | ||
| 10 | @quotation | ||
| 11 | Copyright (C) 2012-2022 Free Software Foundation, Inc. | ||
| 12 | |||
| 13 | You can redistribute this document and/or modify it under the terms | ||
| 14 | of the GNU General Public License as published by the Free Software | ||
| 15 | Foundation, either version 3 of the License, or (at your option) any | ||
| 16 | later version. | ||
| 17 | |||
| 18 | This document is distributed in the hope that it will be useful, | ||
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE@. See the GNU | ||
| 21 | General Public License for more details. | ||
| 22 | |||
| 23 | @end quotation | ||
| 24 | @end copying | ||
| 25 | |||
| 26 | @dircategory Emacs | ||
| 27 | @direntry | ||
| 28 | * use-package: (use-package). Declarative package configuration for Emacs. | ||
| 29 | @end direntry | ||
| 30 | |||
| 31 | @finalout | ||
| 32 | @titlepage | ||
| 33 | @title use-package User Manual | ||
| 34 | @subtitle for version 2.4.1-119-g0be480e+1 | ||
| 35 | @author John Wiegley | ||
| 36 | @page | ||
| 37 | @vskip 0pt plus 1filll | ||
| 38 | @insertcopying | ||
| 39 | @end titlepage | ||
| 40 | |||
| 41 | @contents | ||
| 42 | |||
| 43 | @ifnottex | ||
| 44 | @node Top | ||
| 45 | @top use-package User Manual | ||
| 46 | |||
| 47 | The @code{use-package} macro allows you to isolate package configuration in your | ||
| 48 | @code{.emacs} file in a way that is both performance-oriented and, well, tidy. I | ||
| 49 | created it because I have over 80 packages that I use in Emacs, and things | ||
| 50 | were getting difficult to manage. Yet with this utility my total load time is | ||
| 51 | around 2 seconds, with no loss of functionality! | ||
| 52 | |||
| 53 | @insertcopying | ||
| 54 | @end ifnottex | ||
| 55 | |||
| 56 | @menu | ||
| 57 | * Introduction:: | ||
| 58 | * Installation:: | ||
| 59 | * Getting Started:: | ||
| 60 | * Basic Concepts:: | ||
| 61 | * Issues/Requests:: | ||
| 62 | * Keywords:: | ||
| 63 | * Debugging Tools:: | ||
| 64 | |||
| 65 | @detailmenu | ||
| 66 | --- The Detailed Node Listing --- | ||
| 67 | |||
| 68 | Installation | ||
| 69 | |||
| 70 | * Installing from GNU ELPA:: | ||
| 71 | * Installing from the Git Repository:: | ||
| 72 | * Post-Installation Tasks:: | ||
| 73 | |||
| 74 | Keywords | ||
| 75 | |||
| 76 | * @code{after}:: @code{:after}. | ||
| 77 | * @code{bind-keymap} @code{bind-keymap*}:: @code{:bind-keymap}, @code{:bind-keymap*}. | ||
| 78 | * @code{bind} @code{bind*}:: @code{:bind}, @code{:bind*}. | ||
| 79 | * @code{commands}:: @code{:commands}. | ||
| 80 | * @code{preface} @code{init} @code{config}:: @code{:preface}, @code{:init}, @code{:config}. | ||
| 81 | * @code{custom}:: @code{:custom}. | ||
| 82 | * @code{custom-face}:: @code{:custom-face}. | ||
| 83 | * @code{defer} @code{demand}:: @code{:defer}, @code{:demand}. | ||
| 84 | * @code{defines} @code{functions}:: @code{:defines}, @code{:functions}. | ||
| 85 | * @code{diminish} @code{delight}:: @code{:diminish}, @code{:delight}. | ||
| 86 | * @code{disabled}:: @code{:disabled}. | ||
| 87 | * @code{ensure} @code{pin}:: @code{:ensure}, @code{:pin}. | ||
| 88 | * @code{hook}:: @code{:hook}. | ||
| 89 | * @code{if} @code{when} @code{unless}:: @code{:if}, @code{:when}, @code{:unless}. | ||
| 90 | * @code{load-path}:: @code{:load-path}. | ||
| 91 | * @code{mode} @code{interpreter}:: @code{:mode}, @code{:interpreter}. | ||
| 92 | * @code{magic} @code{magic-fallback}:: @code{:magic}, @code{:magic-fallback}. | ||
| 93 | * @code{no-require}:: @code{:no-require}. | ||
| 94 | * @code{requires}:: @code{:requires}. | ||
| 95 | |||
| 96 | @code{bind}, @code{bind*} | ||
| 97 | |||
| 98 | * Binding to local keymaps:: | ||
| 99 | |||
| 100 | |||
| 101 | @end detailmenu | ||
| 102 | @end menu | ||
| 103 | |||
| 104 | @node Introduction | ||
| 105 | @chapter Introduction | ||
| 106 | |||
| 107 | The @code{use-package} macro allows you to isolate package configuration in your | ||
| 108 | @code{.emacs} file in a way that is both performance-oriented and, well, tidy. I | ||
| 109 | created it because I have over 80 packages that I use in Emacs, and things | ||
| 110 | were getting difficult to manage. Yet with this utility my total load time is | ||
| 111 | around 2 seconds, with no loss of functionality! | ||
| 112 | |||
| 113 | More text to come@dots{} | ||
| 114 | |||
| 115 | @node Installation | ||
| 116 | @chapter Installation | ||
| 117 | |||
| 118 | use-package can be installed using Emacs' package manager or manually from | ||
| 119 | its development repository. | ||
| 120 | |||
| 121 | @menu | ||
| 122 | * Installing from GNU ELPA:: | ||
| 123 | * Installing from the Git Repository:: | ||
| 124 | * Post-Installation Tasks:: | ||
| 125 | @end menu | ||
| 126 | |||
| 127 | @node Installing from GNU ELPA | ||
| 128 | @section Installing from GNU ELPA | ||
| 129 | |||
| 130 | use-package is available from GNU ELPA. If you haven't used | ||
| 131 | Emacs' package manager before, then it is high time you familiarize yourself | ||
| 132 | with it by reading the documentation in the Emacs manual, see | ||
| 133 | @ref{Packages,,,emacs,}. Then add one of the archives to @code{package-archives}: | ||
| 134 | |||
| 135 | First, you need to update the local package list using: | ||
| 136 | |||
| 137 | @example | ||
| 138 | M-x package-refresh-contents RET | ||
| 139 | @end example | ||
| 140 | |||
| 141 | Once you have done that, you can install use-package and its | ||
| 142 | dependencies using: | ||
| 143 | |||
| 144 | @example | ||
| 145 | M-x package-install RET use-package RET | ||
| 146 | @end example | ||
| 147 | |||
| 148 | Now see @ref{Post-Installation Tasks}. | ||
| 149 | |||
| 150 | @node Installing from the Git Repository | ||
| 151 | @section Installing from the Git Repository | ||
| 152 | |||
| 153 | First, use Git to clone the use-package repository: | ||
| 154 | |||
| 155 | @example | ||
| 156 | $ git clone https://github.com/jwiegley/use-package.git ~/.emacs.d/site-lisp/use-package | ||
| 157 | $ cd ~/.emacs.d/site-lisp/use-package | ||
| 158 | @end example | ||
| 159 | |||
| 160 | Then compile the libraries and generate the info manuals: | ||
| 161 | |||
| 162 | @example | ||
| 163 | $ make | ||
| 164 | @end example | ||
| 165 | |||
| 166 | You may need to create @code{/path/to/use-package/config.mk} with the following | ||
| 167 | content before running @code{make}: | ||
| 168 | |||
| 169 | @example | ||
| 170 | LOAD_PATH = -L /path/to/use-package | ||
| 171 | @end example | ||
| 172 | |||
| 173 | Finally add this to your init file: | ||
| 174 | |||
| 175 | @lisp | ||
| 176 | (add-to-list 'load-path "~/.emacs.d/site-lisp/use-package") | ||
| 177 | (require 'use-package) | ||
| 178 | |||
| 179 | (with-eval-after-load 'info | ||
| 180 | (info-initialize) | ||
| 181 | (add-to-list 'Info-directory-list | ||
| 182 | "~/.emacs.d/site-lisp/use-package/")) | ||
| 183 | @end lisp | ||
| 184 | |||
| 185 | Note that elements of @code{load-path} should not end with a slash, while those of | ||
| 186 | @code{Info-directory-list} should. | ||
| 187 | |||
| 188 | Instead of running use-package directly from the repository by adding it to | ||
| 189 | the @code{load-path}, you might want to instead install it in some other directory | ||
| 190 | using @code{sudo make install} and setting @code{load-path} accordingly. | ||
| 191 | |||
| 192 | To update use-package use: | ||
| 193 | |||
| 194 | @example | ||
| 195 | $ git pull | ||
| 196 | $ make | ||
| 197 | @end example | ||
| 198 | |||
| 199 | At times it might be necessary to run @code{make clean all} instead. | ||
| 200 | |||
| 201 | To view all available targets use @code{make help}. | ||
| 202 | |||
| 203 | Now see @ref{Post-Installation Tasks}. | ||
| 204 | |||
| 205 | @node Post-Installation Tasks | ||
| 206 | @section Post-Installation Tasks | ||
| 207 | |||
| 208 | After installing use-package you should verify that you are indeed using the | ||
| 209 | use-package release you think you are using. It's best to restart Emacs before | ||
| 210 | doing so, to make sure you are not using an outdated value for @code{load-path}. | ||
| 211 | |||
| 212 | @example | ||
| 213 | C-h v use-package-version RET | ||
| 214 | @end example | ||
| 215 | |||
| 216 | should display something like | ||
| 217 | |||
| 218 | @example | ||
| 219 | use-package-version’s value is "2.4.3" | ||
| 220 | @end example | ||
| 221 | |||
| 222 | If you are completely new to use-package then see @ref{Getting Started}. | ||
| 223 | |||
| 224 | If you run into problems, then please see the @ref{Debugging Tools}. | ||
| 225 | |||
| 226 | @node Getting Started | ||
| 227 | @chapter Getting Started | ||
| 228 | |||
| 229 | TODO@. For now, see @code{README.md}. | ||
| 230 | |||
| 231 | @node Basic Concepts | ||
| 232 | @chapter Basic Concepts | ||
| 233 | |||
| 234 | @code{use-package} was created for few basic reasons, each of which drove the | ||
| 235 | design in various ways. Understanding these reasons may help make some of | ||
| 236 | those decisions clearer: | ||
| 237 | |||
| 238 | @itemize | ||
| 239 | @item | ||
| 240 | To gather all configuration details of a package into one place, | ||
| 241 | making it easier to copy, disable, or move it elsewhere in the init | ||
| 242 | file. | ||
| 243 | |||
| 244 | @item | ||
| 245 | To reduce duplication and boilerplate, capturing several common | ||
| 246 | practices as mere keywords both easy and intuitive to use. | ||
| 247 | |||
| 248 | @item | ||
| 249 | To make startup time of Emacs as quick as possible, without | ||
| 250 | sacrificing the quantity of add-on packages used. | ||
| 251 | |||
| 252 | @item | ||
| 253 | To make it so errors encountered during startup disable only the | ||
| 254 | package raising the error, and as little else as possible, leaving a | ||
| 255 | close to a functional Emacs as possible. | ||
| 256 | |||
| 257 | @item | ||
| 258 | To allow byte-compilation of one's init file so that any warnings or | ||
| 259 | errors seen are meaningful. In this way, even if byte-compilation is not | ||
| 260 | used for speed (reason 3), it can still be used as a sanity check. | ||
| 261 | @end itemize | ||
| 262 | |||
| 263 | @node Issues/Requests | ||
| 264 | @chapter Issues/Requests | ||
| 265 | |||
| 266 | @node Keywords | ||
| 267 | @chapter Keywords | ||
| 268 | |||
| 269 | @menu | ||
| 270 | * @code{after}:: @code{after}. | ||
| 271 | * @code{bind-keymap} @code{bind-keymap*}:: @code{:bind-keymap}, @code{:bind-keymap*}. | ||
| 272 | * @code{bind} @code{bind*}:: @code{bind} @code{:bind*}. | ||
| 273 | * @code{commands}:: @code{:commands}. | ||
| 274 | * @code{preface} @code{init} @code{config}:: @code{:preface}, @code{:init}, @code{:config}. | ||
| 275 | * @code{custom}:: @code{:custom}. | ||
| 276 | * @code{custom-face}:: @code{:custom-face}. | ||
| 277 | * @code{defer} @code{demand}:: @code{:defer}, @code{:demand}. | ||
| 278 | * @code{defines} @code{functions}:: @code{:defines}, @code{:functions}. | ||
| 279 | * @code{diminish} @code{delight}:: @code{:diminish}, @code{:delight}. | ||
| 280 | * @code{disabled}:: @code{:disabled}. | ||
| 281 | * @code{ensure} @code{pin}:: @code{:ensure}, @code{:pin}. | ||
| 282 | * @code{hook}:: @code{:hook}. | ||
| 283 | * @code{if} @code{when} @code{unless}:: @code{:if}, @code{:when}, @code{:unless}. | ||
| 284 | * @code{load-path}:: @code{:load-path}. | ||
| 285 | * @code{mode} @code{interpreter}:: @code{:mode}, @code{:interpreter}. | ||
| 286 | * @code{magic} @code{magic-fallback}:: @code{:magic}, @code{:magic-fallback}. | ||
| 287 | * @code{no-require}:: @code{:no-require}. | ||
| 288 | * @code{requires}:: @code{:requires}. | ||
| 289 | @end menu | ||
| 290 | |||
| 291 | @node @code{after} | ||
| 292 | @section @code{:after} | ||
| 293 | |||
| 294 | Sometimes it only makes sense to configure a package after another has been | ||
| 295 | loaded, because certain variables or functions are not in scope until that | ||
| 296 | time. This can achieved using an @code{:after} keyword that allows a fairly rich | ||
| 297 | description of the exact conditions when loading should occur. Here is an | ||
| 298 | example: | ||
| 299 | |||
| 300 | @lisp | ||
| 301 | (use-package hydra | ||
| 302 | :load-path "site-lisp/hydra") | ||
| 303 | |||
| 304 | (use-package ivy | ||
| 305 | :load-path "site-lisp/swiper") | ||
| 306 | |||
| 307 | (use-package ivy-hydra | ||
| 308 | :after (ivy hydra)) | ||
| 309 | @end lisp | ||
| 310 | |||
| 311 | In this case, because all of these packages are demand-loaded in the order | ||
| 312 | they occur, the use of @code{:after} is not strictly necessary. By using it, | ||
| 313 | however, the above code becomes order-independent, without an implicit | ||
| 314 | depedence on the nature of your init file. | ||
| 315 | |||
| 316 | By default, @code{:after (foo bar)} is the same as @code{:after (:all foo bar)}, meaning | ||
| 317 | that loading of the given package will not happen until both @code{foo} and @code{bar} | ||
| 318 | have been loaded. Here are some of the other possibilities: | ||
| 319 | |||
| 320 | @lisp | ||
| 321 | :after (foo bar) | ||
| 322 | :after (:all foo bar) | ||
| 323 | :after (:any foo bar) | ||
| 324 | :after (:all (:any foo bar) (:any baz quux)) | ||
| 325 | :after (:any (:all foo bar) (:all baz quux)) | ||
| 326 | @end lisp | ||
| 327 | |||
| 328 | When you nest selectors, such as @code{(:any (:all foo bar) (:all baz quux))}, it | ||
| 329 | means that the package will be loaded when either both @code{foo} and @code{bar} have | ||
| 330 | been loaded, or both @code{baz} and @code{quux} have been loaded. | ||
| 331 | |||
| 332 | @strong{NOTE}: Pay attention if you set @code{use-package-always-defer} to t, and also use | ||
| 333 | the @code{:after} keyword, as you will need to specify how the declared package is | ||
| 334 | to be loaded: e.g., by some @code{:bind}. If you're not using one of the mechanisms | ||
| 335 | that registers autoloads, such as @code{:bind} or @code{:hook}, and your package manager | ||
| 336 | does not provide autoloads, it's possible that without adding @code{:demand t} to | ||
| 337 | those declarations, your package will never be loaded. | ||
| 338 | |||
| 339 | @node @code{bind-keymap} @code{bind-keymap*} | ||
| 340 | @section @code{:bind-keymap}, @code{:bind-keymap*} | ||
| 341 | |||
| 342 | Normally @code{:bind} expects that commands are functions that will be autoloaded | ||
| 343 | from the given package. However, this does not work if one of those commands | ||
| 344 | is actually a keymap, since keymaps are not functions, and cannot be | ||
| 345 | autoloaded using Emacs' @code{autoload} mechanism. | ||
| 346 | |||
| 347 | To handle this case, @code{use-package} offers a special, limited variant of | ||
| 348 | @code{:bind} called @code{:bind-keymap}. The only difference is that the "commands" | ||
| 349 | bound to by @code{:bind-keymap} must be keymaps defined in the package, rather than | ||
| 350 | command functions. This is handled behind the scenes by generating custom code | ||
| 351 | that loads the package containing the keymap, and then re-executes your | ||
| 352 | keypress after the first load, to reinterpret that keypress as a prefix key. | ||
| 353 | |||
| 354 | For example: | ||
| 355 | |||
| 356 | @lisp | ||
| 357 | (use-package projectile | ||
| 358 | :bind-keymap | ||
| 359 | ("C-c p" . projectile-command-map) | ||
| 360 | @end lisp | ||
| 361 | |||
| 362 | @node @code{bind} @code{bind*} | ||
| 363 | @section @code{:bind}, @code{:bind*} | ||
| 364 | |||
| 365 | Another common thing to do when loading a module is to bind a key to primary | ||
| 366 | commands within that module: | ||
| 367 | |||
| 368 | @lisp | ||
| 369 | (use-package ace-jump-mode | ||
| 370 | :bind ("C-." . ace-jump-mode)) | ||
| 371 | @end lisp | ||
| 372 | |||
| 373 | This does two things: first, it creates an autoload for the @code{ace-jump-mode} | ||
| 374 | command and defers loading of @code{ace-jump-mode} until you actually use it. | ||
| 375 | Second, it binds the key @code{C-.} to that command. After loading, you can use | ||
| 376 | @code{M-x describe-personal-keybindings} to see all such keybindings you've set | ||
| 377 | throughout your @code{.emacs} file. | ||
| 378 | |||
| 379 | A more literal way to do the exact same thing is: | ||
| 380 | |||
| 381 | @lisp | ||
| 382 | (use-package ace-jump-mode | ||
| 383 | :commands ace-jump-mode | ||
| 384 | :init | ||
| 385 | (bind-key "C-." 'ace-jump-mode)) | ||
| 386 | @end lisp | ||
| 387 | |||
| 388 | When you use the @code{:commands} keyword, it creates autoloads for those commands | ||
| 389 | and defers loading of the module until they are used. Since the @code{:init} form | ||
| 390 | is always run---even if @code{ace-jump-mode} might not be on your system---remember | ||
| 391 | to restrict @code{:init} code to only what would succeed either way. | ||
| 392 | |||
| 393 | The @code{:bind} keyword takes either a cons or a list of conses: | ||
| 394 | |||
| 395 | @lisp | ||
| 396 | (use-package hi-lock | ||
| 397 | :bind (("M-o l" . highlight-lines-matching-regexp) | ||
| 398 | ("M-o r" . highlight-regexp) | ||
| 399 | ("M-o w" . highlight-phrase))) | ||
| 400 | @end lisp | ||
| 401 | |||
| 402 | The @code{:commands} keyword likewise takes either a symbol or a list of symbols. | ||
| 403 | |||
| 404 | NOTE: Special keys like @code{tab} or @code{F1}-@code{Fn} can be written in square brackets, | ||
| 405 | i.e. @code{[tab]} instead of @code{"tab"}. The syntax for the keybindings is similar to | ||
| 406 | the "kbd" syntax: see @uref{https://www.gnu.org/software/emacs/manual/html_node/emacs/Init-Rebinding.html, the Emacs Manual} for more information. | ||
| 407 | |||
| 408 | Examples: | ||
| 409 | |||
| 410 | @lisp | ||
| 411 | (use-package helm | ||
| 412 | :bind (("M-x" . helm-M-x) | ||
| 413 | ("M-<f5>" . helm-find-files) | ||
| 414 | ([f10] . helm-buffers-list) | ||
| 415 | ([S-f10] . helm-recentf))) | ||
| 416 | @end lisp | ||
| 417 | |||
| 418 | @menu | ||
| 419 | * Binding to local keymaps:: | ||
| 420 | @end menu | ||
| 421 | |||
| 422 | @node Binding to local keymaps | ||
| 423 | @subsection Binding to local keymaps | ||
| 424 | |||
| 425 | Slightly different from binding a key to a keymap, is binding a key @strong{within} a | ||
| 426 | local keymap that only exists after the package is loaded. @code{use-package} | ||
| 427 | supports this with a @code{:map} modifier, taking the local keymap to bind to: | ||
| 428 | |||
| 429 | @lisp | ||
| 430 | (use-package helm | ||
| 431 | :bind (:map helm-command-map | ||
| 432 | ("C-c h" . helm-execute-persistent-action))) | ||
| 433 | @end lisp | ||
| 434 | |||
| 435 | The effect of this statement is to wait until @code{helm} has loaded, and then to | ||
| 436 | bind the key @code{C-c h} to @code{helm-execute-persistent-action} within Helm's local | ||
| 437 | keymap, @code{helm-mode-map}. | ||
| 438 | |||
| 439 | Multiple uses of @code{:map} may be specified. Any binding occurring before the | ||
| 440 | first use of @code{:map} are applied to the global keymap: | ||
| 441 | |||
| 442 | @lisp | ||
| 443 | (use-package term | ||
| 444 | :bind (("C-c t" . term) | ||
| 445 | :map term-mode-map | ||
| 446 | ("M-p" . term-send-up) | ||
| 447 | ("M-n" . term-send-down) | ||
| 448 | :map term-raw-map | ||
| 449 | ("M-o" . other-window) | ||
| 450 | ("M-p" . term-send-up) | ||
| 451 | ("M-n" . term-send-down))) | ||
| 452 | @end lisp | ||
| 453 | |||
| 454 | @node @code{commands} | ||
| 455 | @section @code{:commands} | ||
| 456 | |||
| 457 | @node @code{preface} @code{init} @code{config} | ||
| 458 | @section @code{:preface}, @code{:init}, @code{:config} | ||
| 459 | |||
| 460 | Here is the simplest @code{use-package} declaration: | ||
| 461 | |||
| 462 | @lisp | ||
| 463 | ;; This is only needed once, near the top of the file | ||
| 464 | (eval-when-compile | ||
| 465 | ;; Following line is not needed if use-package.el is in ~/.emacs.d | ||
| 466 | (add-to-list 'load-path "<path where use-package is installed>") | ||
| 467 | (require 'use-package)) | ||
| 468 | |||
| 469 | (use-package foo) | ||
| 470 | @end lisp | ||
| 471 | |||
| 472 | This loads in the package @code{foo}, but only if @code{foo} is available on your | ||
| 473 | system. If not, a warning is logged to the @code{*Messages*} buffer. If it | ||
| 474 | succeeds, a message about @code{"Loading foo"} is logged, along with the time it | ||
| 475 | took to load, if it took over 0.1 seconds. | ||
| 476 | |||
| 477 | Use the @code{:init} keyword to execute code before a package is loaded. It | ||
| 478 | accepts one or more forms, up until the next keyword: | ||
| 479 | |||
| 480 | @lisp | ||
| 481 | (use-package foo | ||
| 482 | :init | ||
| 483 | (setq foo-variable t)) | ||
| 484 | @end lisp | ||
| 485 | |||
| 486 | Similarly, @code{:config} can be used to execute code after a package is loaded. | ||
| 487 | In cases where loading is done lazily (see more about autoloading below), this | ||
| 488 | execution is deferred until after the autoload occurs: | ||
| 489 | |||
| 490 | @lisp | ||
| 491 | (use-package foo | ||
| 492 | :init | ||
| 493 | (setq foo-variable t) | ||
| 494 | :config | ||
| 495 | (foo-mode 1)) | ||
| 496 | @end lisp | ||
| 497 | |||
| 498 | As you might expect, you can use @code{:init} and @code{:config} together: | ||
| 499 | |||
| 500 | @lisp | ||
| 501 | (use-package color-moccur | ||
| 502 | :commands (isearch-moccur isearch-all) | ||
| 503 | :bind (("M-s O" . moccur) | ||
| 504 | :map isearch-mode-map | ||
| 505 | ("M-o" . isearch-moccur) | ||
| 506 | ("M-O" . isearch-moccur-all)) | ||
| 507 | :init | ||
| 508 | (setq isearch-lazy-highlight t) | ||
| 509 | :config | ||
| 510 | (use-package moccur-edit)) | ||
| 511 | @end lisp | ||
| 512 | |||
| 513 | In this case, I want to autoload the commands @code{isearch-moccur} and | ||
| 514 | @code{isearch-all} from @code{color-moccur.el}, and bind keys both at the global level | ||
| 515 | and within the @code{isearch-mode-map} (see next section). When the package is | ||
| 516 | actually loaded (by using one of these commands), @code{moccur-edit} is also | ||
| 517 | loaded, to allow editing of the @code{moccur} buffer. | ||
| 518 | |||
| 519 | @node @code{custom} | ||
| 520 | @section @code{:custom} | ||
| 521 | |||
| 522 | The @code{:custom} keyword allows customization of package custom variables. | ||
| 523 | |||
| 524 | @lisp | ||
| 525 | (use-package comint | ||
| 526 | :custom | ||
| 527 | (comint-buffer-maximum-size 20000 "Increase comint buffer size.") | ||
| 528 | (comint-prompt-read-only t "Make the prompt read only.")) | ||
| 529 | @end lisp | ||
| 530 | |||
| 531 | The documentation string is not mandatory. | ||
| 532 | |||
| 533 | @node @code{custom-face} | ||
| 534 | @section @code{:custom-face} | ||
| 535 | |||
| 536 | The @code{:custom-face} keyword allows customization of package custom faces. | ||
| 537 | |||
| 538 | @lisp | ||
| 539 | (use-package eruby-mode | ||
| 540 | :custom-face | ||
| 541 | (eruby-standard-face ((t (:slant italic))))) | ||
| 542 | @end lisp | ||
| 543 | |||
| 544 | @node @code{defer} @code{demand} | ||
| 545 | @section @code{:defer}, @code{:demand} | ||
| 546 | |||
| 547 | In almost all cases you don't need to manually specify @code{:defer t}. This is | ||
| 548 | implied whenever @code{:bind} or @code{:mode} or @code{:interpreter} is used. Typically, you | ||
| 549 | only need to specify @code{:defer} if you know for a fact that some other package | ||
| 550 | will do something to cause your package to load at the appropriate time, and | ||
| 551 | thus you would like to defer loading even though use-package isn't creating | ||
| 552 | any autoloads for you. | ||
| 553 | |||
| 554 | You can override package deferral with the @code{:demand} keyword. Thus, even if | ||
| 555 | you use @code{:bind}, using @code{:demand} will force loading to occur immediately and | ||
| 556 | not establish an autoload for the bound key. | ||
| 557 | |||
| 558 | @node @code{defines} @code{functions} | ||
| 559 | @section @code{:defines}, @code{:functions} | ||
| 560 | |||
| 561 | Another feature of @code{use-package} is that it always loads every file that it | ||
| 562 | can when @code{.emacs} is being byte-compiled. This helps to silence spurious | ||
| 563 | warnings about unknown variables and functions. | ||
| 564 | |||
| 565 | However, there are times when this is just not enough. For those times, use | ||
| 566 | the @code{:defines} and @code{:functions} keywords to introduce dummy variable and | ||
| 567 | function declarations solely for the sake of the byte-compiler: | ||
| 568 | |||
| 569 | @lisp | ||
| 570 | (use-package texinfo | ||
| 571 | :defines texinfo-section-list | ||
| 572 | :commands texinfo-mode | ||
| 573 | :init | ||
| 574 | (add-to-list 'auto-mode-alist '("\\.texi$" . texinfo-mode))) | ||
| 575 | @end lisp | ||
| 576 | |||
| 577 | If you need to silence a missing function warning, you can use @code{:functions}: | ||
| 578 | |||
| 579 | @lisp | ||
| 580 | (use-package ruby-mode | ||
| 581 | :mode "\\.rb\\'" | ||
| 582 | :interpreter "ruby" | ||
| 583 | :functions inf-ruby-keys | ||
| 584 | :config | ||
| 585 | (defun my-ruby-mode-hook () | ||
| 586 | (require 'inf-ruby) | ||
| 587 | (inf-ruby-keys)) | ||
| 588 | |||
| 589 | (add-hook 'ruby-mode-hook 'my-ruby-mode-hook)) | ||
| 590 | @end lisp | ||
| 591 | |||
| 592 | @node @code{diminish} @code{delight} | ||
| 593 | @section @code{:diminish}, @code{:delight} | ||
| 594 | |||
| 595 | @code{use-package} also provides built-in support for the diminish and delight | ||
| 596 | utilities---if you have them installed. Their purpose is to remove or change | ||
| 597 | minor mode strings in your mode-line. | ||
| 598 | |||
| 599 | @uref{https://github.com/myrjola/diminish.el, diminish} is invoked with the @code{:diminish} keyword, which is passed either a | ||
| 600 | minor mode symbol, a cons of the symbol and its replacement string, or just a | ||
| 601 | replacement string, in which case the minor mode symbol is guessed to be the | ||
| 602 | package name with "-mode" appended at the end: | ||
| 603 | |||
| 604 | @lisp | ||
| 605 | (use-package abbrev | ||
| 606 | :diminish abbrev-mode | ||
| 607 | :config | ||
| 608 | (if (file-exists-p abbrev-file-name) | ||
| 609 | (quietly-read-abbrev-file))) | ||
| 610 | @end lisp | ||
| 611 | |||
| 612 | @uref{https://elpa.gnu.org/packages/delight.html, delight} is invoked with the @code{:delight} keyword, which is passed a minor mode | ||
| 613 | symbol, a replacement string or quoted @uref{https://www.gnu.org/software/emacs/manual/html_node/elisp/Mode-Line-Data.html, mode-line data} (in which case the minor | ||
| 614 | mode symbol is guessed to be the package name with "-mode" appended at the | ||
| 615 | end), both of these, or several lists of both. If no arguments are provided, | ||
| 616 | the default mode name is hidden completely. | ||
| 617 | |||
| 618 | @lisp | ||
| 619 | ;; Don't show anything for rainbow-mode. | ||
| 620 | (use-package rainbow-mode | ||
| 621 | :delight) | ||
| 622 | |||
| 623 | ;; Don't show anything for auto-revert-mode, which doesn't match | ||
| 624 | ;; its package name. | ||
| 625 | (use-package autorevert | ||
| 626 | :delight auto-revert-mode) | ||
| 627 | |||
| 628 | ;; Remove the mode name for projectile-mode, but show the project name. | ||
| 629 | (use-package projectile | ||
| 630 | :delight '(:eval (concat " " (projectile-project-name)))) | ||
| 631 | |||
| 632 | ;; Completely hide visual-line-mode and change auto-fill-mode to " AF". | ||
| 633 | (use-package emacs | ||
| 634 | :delight | ||
| 635 | (auto-fill-function " AF") | ||
| 636 | (visual-line-mode)) | ||
| 637 | @end lisp | ||
| 638 | |||
| 639 | @node @code{disabled} | ||
| 640 | @section @code{:disabled} | ||
| 641 | |||
| 642 | The @code{:disabled} keyword can turn off a module you're having difficulties with, | ||
| 643 | or stop loading something you're not using at the present time: | ||
| 644 | |||
| 645 | @lisp | ||
| 646 | (use-package ess-site | ||
| 647 | :disabled | ||
| 648 | :commands R) | ||
| 649 | @end lisp | ||
| 650 | |||
| 651 | When byte-compiling your @code{.emacs} file, disabled declarations are omitted | ||
| 652 | from the output entirely, to accelerate startup times. | ||
| 653 | |||
| 654 | @node @code{ensure} @code{pin} | ||
| 655 | @section @code{:ensure}, @code{:pin} | ||
| 656 | |||
| 657 | You can use @code{use-package} to load packages from ELPA with @code{package.el}. This | ||
| 658 | is particularly useful if you share your @code{.emacs} among several machines; the | ||
| 659 | relevant packages are downloaded automatically once declared in your @code{.emacs}. | ||
| 660 | The @code{:ensure} keyword causes the package(s) to be installed automatically if | ||
| 661 | not already present on your system (set @code{(setq use-package-always-ensure t)} | ||
| 662 | if you wish this behavior to be global for all packages): | ||
| 663 | |||
| 664 | @lisp | ||
| 665 | (use-package magit | ||
| 666 | :ensure t) | ||
| 667 | @end lisp | ||
| 668 | |||
| 669 | If you need to install a different package from the one named by | ||
| 670 | @code{use-package}, you can specify it like this: | ||
| 671 | |||
| 672 | @lisp | ||
| 673 | (use-package tex | ||
| 674 | :ensure auctex) | ||
| 675 | @end lisp | ||
| 676 | |||
| 677 | Lastly, when running on Emacs 24.4 or later, use-package can pin a package to | ||
| 678 | a specific archive, allowing you to mix and match packages from different | ||
| 679 | archives. The primary use-case for this is preferring packages from the | ||
| 680 | @code{melpa-stable} and @code{gnu} archives, but using specific packages from @code{melpa} | ||
| 681 | when you need to track newer versions than what is available in the @code{stable} | ||
| 682 | archives is also a valid use-case. | ||
| 683 | |||
| 684 | By default @code{package.el} prefers @code{melpa} over @code{melpa-stable} due to the | ||
| 685 | versioning @code{(> evil-20141208.623 evil-1.0.9)}, so even if you are tracking | ||
| 686 | only a single package from @code{melpa}, you will need to tag all the non-@code{melpa} | ||
| 687 | packages with the appropriate archive. If this really annoys you, then you can | ||
| 688 | set @code{use-package-always-pin} to set a default. | ||
| 689 | |||
| 690 | If you want to manually keep a package updated and ignore upstream updates, | ||
| 691 | you can pin it to @code{manual}, which as long as there is no repository by that | ||
| 692 | name, will Just Work(tm). | ||
| 693 | |||
| 694 | @code{use-package} throws an error if you try to pin a package to an archive that | ||
| 695 | has not been configured using @code{package-archives} (apart from the magic | ||
| 696 | @code{manual} archive mentioned above): | ||
| 697 | |||
| 698 | @example | ||
| 699 | Archive 'foo' requested for package 'bar' is not available. | ||
| 700 | @end example | ||
| 701 | |||
| 702 | Example: | ||
| 703 | |||
| 704 | @lisp | ||
| 705 | (use-package company | ||
| 706 | :ensure t | ||
| 707 | :pin melpa-stable) | ||
| 708 | |||
| 709 | (use-package evil | ||
| 710 | :ensure t) | ||
| 711 | ;; no :pin needed, as package.el will choose the version in melpa | ||
| 712 | |||
| 713 | (use-package adaptive-wrap | ||
| 714 | :ensure t | ||
| 715 | ;; as this package is available only in the gnu archive, this is | ||
| 716 | ;; technically not needed, but it helps to highlight where it | ||
| 717 | ;; comes from | ||
| 718 | :pin gnu) | ||
| 719 | |||
| 720 | (use-package org | ||
| 721 | :ensure t | ||
| 722 | ;; ignore org-mode from upstream and use a manually installed version | ||
| 723 | :pin manual) | ||
| 724 | @end lisp | ||
| 725 | |||
| 726 | @strong{NOTE}: the @code{:pin} argument has no effect on emacs versions < 24.4. | ||
| 727 | |||
| 728 | @node @code{hook} | ||
| 729 | @section @code{:hook} | ||
| 730 | |||
| 731 | The @code{:hook} keyword allows adding functions onto hooks, here only the basename | ||
| 732 | of the hook is required. Thus, all of the following are equivalent: | ||
| 733 | |||
| 734 | @lisp | ||
| 735 | (use-package ace-jump-mode | ||
| 736 | :hook prog-mode) | ||
| 737 | |||
| 738 | (use-package ace-jump-mode | ||
| 739 | :hook (prog-mode . ace-jump-mode)) | ||
| 740 | |||
| 741 | (use-package ace-jump-mode | ||
| 742 | :commands ace-jump-mode | ||
| 743 | :init | ||
| 744 | (add-hook 'prog-mode-hook #'ace-jump-mode)) | ||
| 745 | @end lisp | ||
| 746 | |||
| 747 | And likewise, when multiple hooks should be applied, the following are also | ||
| 748 | equivalent: | ||
| 749 | |||
| 750 | @lisp | ||
| 751 | (use-package ace-jump-mode | ||
| 752 | :hook (prog-mode text-mode)) | ||
| 753 | |||
| 754 | (use-package ace-jump-mode | ||
| 755 | :hook ((prog-mode text-mode) . ace-jump-mode)) | ||
| 756 | |||
| 757 | (use-package ace-jump-mode | ||
| 758 | :hook ((prog-mode . ace-jump-mode) | ||
| 759 | (text-mode . ace-jump-mode))) | ||
| 760 | |||
| 761 | (use-package ace-jump-mode | ||
| 762 | :commands ace-jump-mode | ||
| 763 | :init | ||
| 764 | (add-hook 'prog-mode-hook #'ace-jump-mode) | ||
| 765 | (add-hook 'text-mode-hook #'ace-jump-mode)) | ||
| 766 | @end lisp | ||
| 767 | |||
| 768 | The use of @code{:hook}, as with @code{:bind}, @code{:mode}, @code{:interpreter}, etc., causes the | ||
| 769 | functions being hooked to implicitly be read as @code{:commands} (meaning they will | ||
| 770 | establish interactive @code{autoload} definitions for that module, if not already | ||
| 771 | defined as functions), and so @code{:defer t} is also implied by @code{:hook}. | ||
| 772 | |||
| 773 | @node @code{if} @code{when} @code{unless} | ||
| 774 | @section @code{:if}, @code{:when}, @code{:unless} | ||
| 775 | |||
| 776 | You can use the @code{:if} keyword to predicate the loading and initialization of | ||
| 777 | modules. | ||
| 778 | |||
| 779 | For example, I only want @code{edit-server} running for my main, graphical Emacs, | ||
| 780 | not for other Emacsen I may start at the command line: | ||
| 781 | |||
| 782 | @lisp | ||
| 783 | (use-package edit-server | ||
| 784 | :if window-system | ||
| 785 | :init | ||
| 786 | (add-hook 'after-init-hook 'server-start t) | ||
| 787 | (add-hook 'after-init-hook 'edit-server-start t)) | ||
| 788 | @end lisp | ||
| 789 | |||
| 790 | In another example, we can load things conditional on the operating system: | ||
| 791 | |||
| 792 | @lisp | ||
| 793 | (use-package exec-path-from-shell | ||
| 794 | :if (memq window-system '(mac ns)) | ||
| 795 | :ensure t | ||
| 796 | :config | ||
| 797 | (exec-path-from-shell-initialize)) | ||
| 798 | @end lisp | ||
| 799 | |||
| 800 | Note that @code{:when} is provided as an alias for @code{:if}, and @code{:unless foo} means | ||
| 801 | the same thing as @code{:if (not foo)}. | ||
| 802 | |||
| 803 | @node @code{load-path} | ||
| 804 | @section @code{:load-path} | ||
| 805 | |||
| 806 | If your package needs a directory added to the @code{load-path} in order to load, | ||
| 807 | use @code{:load-path}. This takes a symbol, a function, a string or a list of | ||
| 808 | strings. If the path is relative, it is expanded within | ||
| 809 | @code{user-emacs-directory}: | ||
| 810 | |||
| 811 | @lisp | ||
| 812 | (use-package ess-site | ||
| 813 | :load-path "site-lisp/ess/lisp/" | ||
| 814 | :commands R) | ||
| 815 | @end lisp | ||
| 816 | |||
| 817 | Note that when using a symbol or a function to provide a dynamically generated | ||
| 818 | list of paths, you must inform the byte-compiler of this definition so the | ||
| 819 | value is available at byte-compilation time. This is done by using the special | ||
| 820 | form @code{eval-and-compile} (as opposed to @code{eval-when-compile}). Further, this | ||
| 821 | value is fixed at whatever was determined during compilation, to avoid looking | ||
| 822 | up the same information again on each startup: | ||
| 823 | |||
| 824 | @lisp | ||
| 825 | (eval-and-compile | ||
| 826 | (defun ess-site-load-path () | ||
| 827 | (shell-command "find ~ -path ess/lisp"))) | ||
| 828 | |||
| 829 | (use-package ess-site | ||
| 830 | :load-path (lambda () (list (ess-site-load-path))) | ||
| 831 | :commands R) | ||
| 832 | @end lisp | ||
| 833 | |||
| 834 | @node @code{mode} @code{interpreter} | ||
| 835 | @section @code{:mode}, @code{:interpreter} | ||
| 836 | |||
| 837 | Similar to @code{:bind}, you can use @code{:mode} and @code{:interpreter} to establish a | ||
| 838 | deferred binding within the @code{auto-mode-alist} and @code{interpreter-mode-alist} | ||
| 839 | variables. The specifier to either keyword can be a cons cell, a list of cons | ||
| 840 | cells, or a string or regexp: | ||
| 841 | |||
| 842 | @lisp | ||
| 843 | (use-package ruby-mode | ||
| 844 | :mode "\\.rb\\'" | ||
| 845 | :interpreter "ruby") | ||
| 846 | |||
| 847 | ;; The package is "python" but the mode is "python-mode": | ||
| 848 | (use-package python | ||
| 849 | :mode ("\\.py\\'" . python-mode) | ||
| 850 | :interpreter ("python" . python-mode)) | ||
| 851 | @end lisp | ||
| 852 | |||
| 853 | If you aren't using @code{:commands}, @code{:bind}, @code{:bind*}, @code{:bind-keymap}, | ||
| 854 | @code{:bind-keymap*}, @code{:mode}, or @code{:interpreter} (all of which imply @code{:defer}; see | ||
| 855 | the docstring for @code{use-package} for a brief description of each), you can | ||
| 856 | still defer loading with the @code{:defer} keyword: | ||
| 857 | |||
| 858 | @lisp | ||
| 859 | (use-package ace-jump-mode | ||
| 860 | :defer t | ||
| 861 | :init | ||
| 862 | (autoload 'ace-jump-mode "ace-jump-mode" nil t) | ||
| 863 | (bind-key "C-." 'ace-jump-mode)) | ||
| 864 | @end lisp | ||
| 865 | |||
| 866 | This does exactly the same thing as the following: | ||
| 867 | |||
| 868 | @lisp | ||
| 869 | (use-package ace-jump-mode | ||
| 870 | :bind ("C-." . ace-jump-mode)) | ||
| 871 | @end lisp | ||
| 872 | |||
| 873 | @node @code{magic} @code{magic-fallback} | ||
| 874 | @section @code{:magic}, @code{:magic-fallback} | ||
| 875 | |||
| 876 | Similar to @code{:mode} and @code{:interpreter}, you can also use @code{:magic} and | ||
| 877 | @code{:magic-fallback} to cause certain function to be run if the beginning of a | ||
| 878 | file matches a given regular expression. The difference between the two is | ||
| 879 | that @code{:magic-fallback} has a lower priority than @code{:mode}. For example: | ||
| 880 | |||
| 881 | @lisp | ||
| 882 | (use-package pdf-tools | ||
| 883 | :load-path "site-lisp/pdf-tools/lisp" | ||
| 884 | :magic ("%PDF" . pdf-view-mode) | ||
| 885 | :config | ||
| 886 | (pdf-tools-install)) | ||
| 887 | @end lisp | ||
| 888 | |||
| 889 | This registers an autoloaded command for @code{pdf-view-mode}, defers loading of | ||
| 890 | @code{pdf-tools}, and runs @code{pdf-view-mode} if the beginning of a buffer matches the | ||
| 891 | string @code{"%PDF"}. | ||
| 892 | |||
| 893 | @node @code{no-require} | ||
| 894 | @section @code{:no-require} | ||
| 895 | |||
| 896 | Normally, @code{use-package} will load each package at compile time before | ||
| 897 | compiling the configuration, to ensure that any necessary symbols are in scope | ||
| 898 | to satisfy the byte-compiler. At times this can cause problems, since a | ||
| 899 | package may have special loading requirements, and all that you want to use | ||
| 900 | @code{use-package} for is to add a configuration to the @code{eval-after-load} hook. In | ||
| 901 | such cases, use the @code{:no-require} keyword: | ||
| 902 | |||
| 903 | @lisp | ||
| 904 | (use-package foo | ||
| 905 | :no-require t | ||
| 906 | :config | ||
| 907 | (message "This is evaluated when `foo' is loaded")) | ||
| 908 | @end lisp | ||
| 909 | |||
| 910 | @node @code{requires} | ||
| 911 | @section @code{:requires} | ||
| 912 | |||
| 913 | While the @code{:after} keyword delays loading until the dependencies are loaded, | ||
| 914 | the somewhat simpler @code{:requires} keyword simply never loads the package if the | ||
| 915 | dependencies are not available at the time the @code{use-package} declaration is | ||
| 916 | encountered. By "available" in this context it means that @code{foo} is available | ||
| 917 | of @code{(featurep 'foo)} evaluates to a non-nil value. For example: | ||
| 918 | |||
| 919 | @lisp | ||
| 920 | (use-package abbrev | ||
| 921 | :requires foo) | ||
| 922 | @end lisp | ||
| 923 | |||
| 924 | This is the same as: | ||
| 925 | |||
| 926 | @lisp | ||
| 927 | (use-package abbrev | ||
| 928 | :if (featurep 'foo)) | ||
| 929 | @end lisp | ||
| 930 | |||
| 931 | As a convenience, a list of such packages may be specified: | ||
| 932 | |||
| 933 | @lisp | ||
| 934 | (use-package abbrev | ||
| 935 | :requires (foo bar baz)) | ||
| 936 | @end lisp | ||
| 937 | |||
| 938 | For more complex logic, such as that supported by @code{:after}, simply use @code{:if} | ||
| 939 | and the appropriate Lisp expression. | ||
| 940 | |||
| 941 | @node Debugging Tools | ||
| 942 | @chapter Debugging Tools | ||
| 943 | |||
| 944 | TODO | ||
| 945 | |||
| 946 | @bye | ||