Commit 134c75f9 authored by Pierre Neidhardt's avatar Pierre Neidhardt

emacs: Use user-emacs-directory as cache folder

parent d5b8b811
......@@ -27,13 +27,15 @@ emacsclient -c -a "" "[email protected]"
The =-a ""= option lets Emacs start a daemon if none was already started. The
command will spawn a new Emacs frame and return immediately, which is usually
what we want; this has some shortcomings however. While forking Emacs in the
background is generally a good idea, there are times when it should wait
(writing e-mails with Mutt, git commit message, web browser fields), others when
it should fork. We can create links to it and parameterize the waiting behaviour
depending on the name of the script. Which leads us to the following script:
=em= forks and does not return while the =emw= and =emc= links (windowed and
console version respectively) return to their caller.
what we want.
This has some shortcomings however. While forking Emacs in the background is
generally a good idea, there are times when it should wait (writing git commit
message, web browser fields), others when it should fork. We can create links
to it and parameterize the waiting behaviour depending on the name of the
script. Which leads us to the following script: =em= forks and does not return
while the =emw= and =emc= links (windowed and console version respectively)
return to their caller.
#+BEGIN_SRC sh
if [ "${0##*/}" = "emc" ]; then
......@@ -121,9 +123,9 @@ variables, function definitions, skeletons, etc.
(setq semanticdb-default-save-directory (concat emacs-cache-folder "semanticdb"))
(semantic-mode 1)
(local-set-key (kbd "<f6>") (recompile))
;;
; Need to end with `provide' so that `require' does not load the file twice.
;; Need to end with `provide' so that `require' does not load the file twice.
(provide 'init-cc)
#+END_SRC
......@@ -142,7 +144,7 @@ hook.
(setq indent-tabs-mode t)
(set (make-local-variable 'compile-command) (concat "go run " (shell-quote-argument buffer-file-name)))
(add-hook 'before-save-hook #'gofmt-before-save nil t))
(add-hook go-mode-hook go-setup)
(add-hook 'go-mode-hook #'go-setup)
#+END_SRC
This last example shows three types of relevant hook use:
......@@ -165,20 +167,20 @@ This last example shows three types of relevant hook use:
Last but not least, refrain from using lambdas in hooks: it makes the
documentation and the intention harder to understand, while making it much
trickier to use the =remove-hook= function, should you need to alter the hook at
runtime.
trickier to use the =remove-hook= function, should you need to alter the hook
interactively.
** Package management
Third party packages, major modes or not, can be loaded similarly depending on
their availability: if the package is not installed, there is no need to parse
it's configuration. The procedure is the same:
its configuration. The procedure is the same:
#+BEGIN_SRC elisp
(with-eval-after-load 'lua-mode (require 'init-lua))
#+END_SRC
If you want to make a mode immediately available on start-up:
If you want to make a mode immediately available on startup:
#+BEGIN_SRC elisp
(when (require 'helm-config nil t) (require 'init-helm))
......@@ -200,10 +202,11 @@ presentation.
The one killer feature is the ability to search text in your whole project or
file tree. Helm comes with a few /greppers/: grep itself, but it also supports
the current version control grepper and other tools such as [[http://geoff.greer.fm/ag/][ag]] and [[https://github.com/monochromegane/the_platinum_searcher][pt]].
the current version control grepper (e.g. =git grep=) and other tools such as [[http://geoff.greer.fm/ag/][ag]]
and [[https://github.com/monochromegane/the_platinum_searcher][pt]].
The VCS grepper is usually faster than =grep=. I have set the bindings to use
the VCS grepper first and to fallback to =pt= when no file in the current folder
the VCS grepper first and to fallback to =ag= when no file in the current folder
is versioned:
#+BEGIN_SRC elisp
......@@ -223,7 +226,7 @@ is versioned:
(helm-grep-do-git-grep arg)
(helm-do-grep-ag arg)))
(define-key mickey-minor-mode-map (kbd "C-x G") 'helm-grep-git-or-ag)
(global-set-key (kbd "C-x G") #'helm-grep-git-or-ag)
#+END_SRC
Other features of Helm:
......@@ -236,9 +239,9 @@ Other features of Helm:
- Lookup files in the Git project with third-party =helm-ls-git=.
- Use =yank= to lookup last region.
- Call =yank= to lookup last region.
- Use the universal argument to include more to your lookup (e.g. subfolders).
- Use the universal argument to include more to your lookup (e.g. subfolders).
- Use =C-c C-f= to activate follow mode and navigate through the results to
display a complete context.
......@@ -253,6 +256,8 @@ Other features of Helm:
- Lookup completion suggestions with =helm-company=.
- Browse Man page sections with =helm-imenu=.
* Update to latest Emacs version
You might like Emacs enough you want it everywhere. And yet sometimes you are
......@@ -273,44 +278,26 @@ Many modes store their cache files in =~/.emacs.d=. I prefer to keep those
ephemeral files in =~/.cache/emacs=.
#+BEGIN_SRC elisp
(defvar emacs-cache-folder "~/.cache/emacs"
"Cache folder is everything we do not want to track along with
the configuration files.")
(if (not (file-directory-p emacs-cache-folder))
(make-directory emacs-cache-folder t))
#+END_SRC
Some common settings:
#+BEGIN_SRC elisp
(setq save-place-file (expand-file-name "saveplace" emacs-cache-folder))
(setq url-cookie-file (expand-file-name "url.cookies" emacs-cache-folder))
(setq bookmark-default-file (expand-file-name "emacs.bmk" emacs-cache-folder))
(setq recentf-save-file (expand-file-name "recentf" emacs-cache-folder))
(setq backup-directory-alist `((".*" . ,(expand-file-name "backups/" emacs-cache-folder))))
(setq eshell-directory-name (expand-file-name "eshell" emacs-cache-folder))
#+END_SRC
(setq user-emacs-directory "~/.cache/emacs/")
(if (not (file-directory-p user-cache-directory))
(make-directory user-cache-directory t))
The cache folder must exist for =desktop-mode=:
;; Some files need to be forced to the cache folder.
(setq geiser-repl-history-filename (expand-file-name "geiser_history" user-emacs-directory))
(setq elfeed-db-directory (expand-file-name "elfeed" user-emacs-directory))
#+BEGIN_SRC elisp
(setq desktop-dirname (expand-file-name "desktop" emacs-cache-folder))
(unless (file-directory-p desktop-dirname)
(make-directory desktop-dirname t))
(setq desktop-path `(,desktop-dirname))
;; Place backup files in specific directory.
(setq backup-directory-alist
`(("." . ,(expand-file-name "backups" user-emacs-directory))))
#+END_SRC
Semantic must be started /after/ setting the db folder:
#+BEGIN_SRC elisp
(setq semanticdb-default-save-directory (expand-file-name "semanticdb" emacs-cache-folder))
(semantic-mode 1)
#+END_SRC
If you use Semantic, make sure it is started /after/ changing the cache folder
since its database is stored there.
* Streamline indentation
I think Emacs has too many options for indentation. Since I have a strong
[[../indentation/index.html][opinion on always using tabs to indent]] (except for Lisp), I "redirect" with
[[../indentation/index.org][opinion on always using tabs to indent]] (except for Lisp), I "redirect" with
=defvaralias= the mode-specific indentation levels to only one variable, namely
=tab-width=.
......@@ -323,8 +310,7 @@ I think Emacs has too many options for indentation. Since I have a strong
(lambda (hook)
(add-hook
hook
(lambda ()
(setq indent-tabs-mode nil))))
(lambda () (setq indent-tabs-mode nil))))
'(lisp-mode-hook emacs-lisp-mode-hook))
;; This needs to be set globally since they are defined as local variables and
......@@ -339,8 +325,8 @@ Add the following to =sh-mode-hook=:
(defvaralias 'sh-indentation 'sh-basic-offset)
#+END_SRC
The case of /C/ and /sh/ are special for historical reasons. Other modes can be
corrected as follows:
The cases of /C/ and /sh/ are special for historical reasons. Other modes
indentation can be corrected as follows:
#+BEGIN_SRC elisp
(defvaralias 'js-indent-level 'tab-width)
......@@ -355,15 +341,15 @@ functions, yet it does not have a proper =go to definition= command. Not for
long:
#+BEGIN_SRC elisp
(define-key lisp-mode-shared-map "\M-." 'find-symbol-at-point)
(defun find-symbol-at-point ()
"Find directly the symbol at point, i.e. go to definition."
"Find the symbol at point, i.e. go to definition."
(interactive)
(let ((sym (symbol-at-point)))
(if (boundp sym)
(find-variable sym)
(find-function sym))))
(define-key lisp-mode-shared-map (kbd "M-.") 'find-symbol-at-point)
#+END_SRC
* Smart compilation
......@@ -394,7 +380,7 @@ the modes:
The compile command can be modified per buffer upon request. If you use the
=desktop= mode to save your session, each buffer's command can be restored as
well.
well:
#+BEGIN_SRC elisp
(add-to-list 'desktop-locals-to-save 'compile-command)
......@@ -425,13 +411,13 @@ Let's add some bindings for convenience:
#+BEGIN_SRC elisp
(defun compile-last-command () (interactive) (compile compile-command))
(global-set-key (kbd "C-<f6>") 'compile)
(global-set-key (kbd "<f6>") 'compile-last-command)
(global-set-key (kbd "C-<f6>") #'compile)
(global-set-key (kbd "<f6>") #'compile-last-command)
#+END_SRC
Here follows a complete example for C: it will look for the closest =Makefile=
in the parent folders and set the command to =make -C /path/to/makefile= or else
fallback on some dynamically set values depending on the language (C or C++) and
fallback to some dynamically set values depending on the language (C or C++) and
the environment (GCC, Clang, etc.). The linker flags are configurable on a
per-buffer basis thanks to the buffer-local =cc-ldlibs= and =cc-ldflags=
variables.
......@@ -484,15 +470,15 @@ restored."
(compile (format "make -k -C %s clean" (shell-quote-argument makefile-dir))))))
(dolist (map (list c-mode-map c++-mode-map))
(define-key map "<f5>" 'cc-clean))
(define-key map "<f5>" #'cc-clean))
(dolist (hook '(c-mode-hook c++-mode-hook))
(add-hook hook 'cc-set-compiler))
(add-hook hook #'cc-set-compiler))
#+END_SRC
* C pretty format
I use [[http://uncrustify.sourceforge.net/][uncrustify]] to format my C code automatically. See my [[../indentation/index.html][indentation
I use [[http://uncrustify.sourceforge.net/][uncrustify]] to format my C code automatically. See my [[../indentation/index.org][indentation
rationale]].
I can call it from Emacs with the following function:
......@@ -501,14 +487,14 @@ I can call it from Emacs with the following function:
(defun cc-fmt ()
"Run uncrustify(1) on current buffer or region."
(interactive)
(let (status
start end
(formatbuf (get-buffer-create "*C format buffer*")))
(let ((formatbuf (get-buffer-create "*C format buffer*"))
status start end)
(if (use-region-p)
(setq start (region-beginning) end (region-end))
(setq start (point-min) end (point-max)))
(setq status
(call-process-region start end "uncrustify" nil formatbuf nil "-lc" "-q" "-c" (concat (getenv "HOME") "/.uncrustify.cfg")))
(call-process-region start end "uncrustify" nil formatbuf nil "-lc" "-q" "-c"
(concat (getenv "HOME") "/.uncrustify.cfg")))
(if (/= status 0)
(error "error running uncrustify")
(delete-region start end)
......@@ -516,13 +502,13 @@ I can call it from Emacs with the following function:
(kill-buffer formatbuf))))
#+END_SRC
We could add this to the save hook to auto-format my code at all times, but that
would be bad practice when working with source code using different formatting
rules.
We could add this to =before-save-hook= to auto-format my code at all times, but
that would be bad practice when working with source code using different
formatting rules.
* Magit
[[https://magit.vc/][Magit]] makes Git management a bliss. The most evident feature would be the easy
[[https://magit.vc/][Magit]] makes Git management a bliss. The most evident feature would be the easy
hunk selection when staging code. This simple feature together with a few others
will make a drastic change to your workflow.
......@@ -539,11 +525,11 @@ As of Septermber 2016, multiple cursors does not support searching, so I use
;; Load the file at the new location.
(load mc/list-file t)
(global-unset-key (kbd "C-<down-mouse-1>"))
(define-key mickey-minor-mode-map (kbd "C-<mouse-1>") 'mc/add-cursor-on-click)
(define-key mickey-minor-mode-map (kbd "C-x M-r") 'mc/edit-lines)
(define-key mickey-minor-mode-map (kbd "C-x M-m") 'mc/mark-more-like-this-extended)
(define-key mickey-minor-mode-map (kbd "C-x M-l") 'mc/mark-all-like-this-dwim)
;; Search compatible with mc.
(global-set-key (kbd "C-<mouse-1>") #'mc/add-cursor-on-click)
(global-set-key (kbd "C-x M-r") #'mc/edit-lines)
(global-set-key (kbd "C-x M-m") #'mc/mark-more-like-this-extended)
(global-set-key (kbd "C-x M-l") #'mc/mark-all-like-this-dwim)
;; mc-compatible with search.
(require 'phi-search nil t))
#+END_SRC
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment