I've recently discovered that one can write the complete Emacs config in a .org file which at startup automatically gets converted into a .el file. The file gets only converted if something has changed otherwise the file stays the same. The reason why this function is great, is because it allows you to write the documentation and the code at the same time.

This post got updated on 2018-08-09.

Since I even forget the keybindings I configure myself I add here a table with all the keybindings one can find in this document.

keybinding mode function
F5   Start deft mode
F6   Start the general clock
F7   Start clock on the current heading
F8   Clock out
F9   Open the agenda
F10   Open magit
F12   Open treemacs
C-F5 php toggle between web and php mode
C-c a   Open org-agenda
C-c b   Switch to org-buffer
C-c c   Capture a new task
C-c d   Open ranger
C-c f auctex Allign table
C-c i org Insert an image from the Downloads folder
C-c l org Create link
C-c n org Link a PDFs to the current heading and start org-noter
C-c o   Start olivetti
C-c p   Open main org file
C-c ¨   Start flycheck (syntax check)
C-c - auctex Show next error
C-c C-x e org Set effort for current heading
C-h C-m   Show keybindings for major mode
C-x K   Kill frame
C-x N   Open new frame
C-x O   Switch to other frame
C-x 4   Toggle window split
C-x C-w org-agenda Export agenda to HTML file
C-S-c   Copy buffer to clipboard
M-m   Switch to minibuffer
M-SPC   Expand path at cursor
SPC b   Start general hydra

With these functions its possible to create configuration sections which only run on a specific system. The can be used like this:

(when (is-mac-p)
    (set-face-attribute 'default nil :height 165))

Taken from this configuration:

  • https://github.com/mwfogleman/.emacs.d/blob/master/michael.org

    (defun is-mac-p ()
        (eq system-type 'darwin))
    
    (defun is-linux-p ()
        (eq system-type 'gnu/linux))
    
    (defun is-windows-p ()
        (or (eq system-type 'ms-dos)
            (eq system-type 'windows-nt)
            (eq system-type 'cygwin)))
    
    (defun is-bsd-p ()
        (eq system-type 'gnu/kfreebsd))

``` emacs-lisp
(require 'package)
(setq package-enable-at-startup nil)
```

I need to disable the signature check because it was failing all the time and I couldn’t find a solution to the problem.

``` emacs-lisp
;; disable gpg signature checks due to an error
(setq package-check-signature nil)
```

Melpa is an alternative repository for Emacs packages. Usually they are comming directly from a git repository. This can make them sometimes a bit unstable.

  • https://melpa.org/#/
    ;; MELPA
    (add-to-list 'package-archives
        '("melpa" . "https://melpa.org/packages/"))
    (when (< emacs-major-version 24)
        ;; For important compatibility libraries like cl-lib
        (add-to-list 'package-archives
            '("gnu" . "https://elpa.gnu.org/packages/")))

Even though org-mode comes with Emacs by default I would like to use the newever version from it’s repository.

``` emacs-lisp
;; org-mode
(add-to-list 'package-archives '("org" . "https://orgmode.org/elpa/") t)
```

Use-package is a very handy package which can automatically download specified packages. This helps with reinstalling Emacs. In addition it allows you to load the packages and their configuration only when they are needed. This helps with Emacs’ startup time.

``` emacs-lisp
(unless (package-installed-p 'use-package)
        (package-refresh-contents)
        (package-install 'use-package))
(require 'use-package)
```

Evil-mode is a package which emulates Vim in Emacs.

``` emacs-lisp
;; evil-mode allows to use vim keybindings
(use-package evil
    :ensure t
    :config
    ;; Start these models in emacs mode
    (add-to-list 'evil-emacs-state-modes 'deft-mode)
    (add-to-list 'evil-emacs-state-modes 'neotree-mode)

    ;; Add vim keybindings to the bookmark menu
    (evil-add-hjkl-bindings bookmark-bmenu-mode-map 'emacs
        (kbd "/")       'evil-search-forward
        (kbd "n")       'evil-search-next
        (kbd "N")       'evil-search-previous
        (kbd "C-d")     'evil-scroll-down
        (kbd "C-u")     'evil-scroll-up
        (kbd "C-w C-w") 'other-window)

    ;; Add vim keybindings to the ibuffer
    (evil-add-hjkl-bindings ibuffer-mode-map 'emacs
        (kbd "/")       'evil-search-forward
        (kbd "n")       'evil-search-next
        (kbd "N")       'evil-search-previous
        (kbd "C-d")     'evil-scroll-down
        (kbd "C-u")     'evil-scroll-up
        (kbd "C-w C-w") 'other-window)

;; Add vim keybindings to the package mode
    (evil-add-hjkl-bindings package-menu-mode-map 'emacs
        (kbd "/")       'evil-search-forward
        (kbd "n")       'evil-search-next
        (kbd "N")       'evil-search-previous
        (kbd "C-d")     'evil-scroll-down
        (kbd "C-u")     'evil-scroll-up
        (kbd "C-w C-w") 'other-window)

    (evil-mode 1))

(global-set-key (kbd "M-h") 'windmove-left)
(global-set-key (kbd "M-l") 'windmove-right)
(global-set-key (kbd "M-k") 'windmove-up)
(global-set-key (kbd "M-j") 'windmove-down)
```

Comming from Vim it’s a bit annoying to not be able to use ESC to quit basically everything. This code fixes this.

``` emacs-lisp
;;; esc quits
(defun minibuffer-keyboard-quit ()
    "Abort recursive edit.
    In Delete Selection mode, if the mark is active, just deactivate it;
    then it takes a second \\[keyboard-quit] to abort the minibuffer."
    (interactive)
    (if (and delete-selection-mode transient-mark-mode mark-active)
        (setq deactivate-mark  t)
        (when (get-buffer "*Completions*")
              (delete-windows-on "*Completions*"))
        (abort-recursive-edit)))
(define-key evil-normal-state-map [escape] 'keyboard-quit)
(define-key evil-visual-state-map [escape] 'keyboard-quit)
(define-key minibuffer-local-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-ns-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-completion-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-must-match-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-isearch-map [escape] 'minibuffer-keyboard-quit)
```

Enable vim keybindings in magit.

``` emacs-lisp
(use-package evil-magit
    :after evil
    :ensure t)
```

evil-surround emulates the vim-surround plugin for vim. Both plugins allow you to surround objects with an input of your choice. E.g. surround a word with ". Which would be: ysiw"

I can’t recall what this package does exactly, however it has to do with scrolling in comparison to Vim.

``` emacs-lisp
;; smooth scrolling
(use-package smooth-scrolling
    :ensure t
    :config
    (setq scroll-margin 1
          scroll-conservatively 9999
          scroll-step 1))
```

Deft is a mode which lets you quickly search and create notes. It works similarly to Notational Velocity. I configured it to use org files and open org-mode to edit. In addition it should search in all my org files. I search through all my org files and open deft with F5.

  • https://github.com/jrblevin/deft

    (use-package deft
        :ensure t
        :bind ("<f5>" . deft)
        :commands (deft)
        :config
        (setq deft-extensions '("org")
              deft-default-extension "org"
              deft-auto-save-interval 300.0
              deft-use-filename-as-title nil
              deft-use-filter-string-for-filename t
              deft-recursive t)
        (setq deft-file-naming-rules
              '((noslash . "-")
                (nospace . "_")
                (case-fn . downcase)))
        (when (is-linux-p)
            (setq deft-directory "~/git_repos/notes"))
        (when (is-windows-p)
            (setq deft-directory "~/git_repos/notes")))

Is required by many packages. I’ve added it specifically because it’s required by the org-insert-image function.

``` emacs-lisp
(use-package dash
    :ensure t)
```

Another dependency for the org-insert-image function.

``` emacs-lisp
(use-package swiper
    :ensure t)
```

The last depency for the org-insert-image function.

``` emacs-lisp
(use-package s
    :ensure t)
```

pdf-tools is an alternativ for doc-view which works very nicely with AUCTex. https://github.com/politza/pdf-tools/blob/master/README.org The config line is required so that a AUCTex compiled PDF gets updated after each compilation.

``` emacs-lisp
(use-package pdf-tools
    :ensure t
    :mode ("\\.pdf\\'" . pdf-view-mode)
    :config
    (pdf-tools-install)
    (setq-default pdf-view-display-size 'fit-page)
    ;; turn off cua so copy works
    (add-hook 'pdf-view-mode-hook (lambda () (cua-mode 0)))
    ;; keyboard shortcuts
    (define-key pdf-view-mode-map (kbd "C-w C-w") 'other-window)
    (define-key pdf-view-mode-map (kbd "j") 'pdf-view-next-page-command)
    (define-key pdf-view-mode-map (kbd "k") 'pdf-view-previous-page-command)
    (define-key pdf-view-mode-map (kbd "h") 'pdf-annot-add-highlight-markup-annotation)
    (define-key pdf-view-mode-map (kbd "t") 'pdf-annot-add-text-annotation)
    (define-key pdf-view-mode-map (kbd "D") 'pdf-annot-delete)
    (add-hook 'TeX-after-compilation-finished-functions #'TeX-revert-document-buffer))
```

Treemacs is a tool which display a file explorer in a little buffer on the side of your main buffer. This is very useful if you’re working on a project which consists of multiple files. It even understands some files and can display their sections (org-mode), or classes (python).

Ranger is a minor mode for dired. It allows for better navigation and overview of the files while browsing them. It as well supports previewing of files. The source can be found here: https://github.com/ralesi/ranger.el

``` emacs-lisp
;; enable yasnippet
(use-package ranger
    :ensure t
    :bind
    ("C-c d" . ranger))
```

``` emacs-lisp
(use-package projectile
    :ensure t
    :config
    (define-key projectile-mode-map (kbd "s-p") 'projectile-command-map)
    (define-key projectile-mode-map (kbd "C-c p") #'hydra-projectile/body)
    (projectile-mode t))
```

Yasnippet is a package with which you can insert code or text snippets based on templates. It already comes with a wide variety of snippets and you can easily define your own.

The default snippets got moved into their seperate package. I’m loading it after the main yasnippet package.

``` emacs-lisp
;; enable yasnippet
(use-package yasnippet-snippets
    :ensure t
    :after yasnippet
    :config
    (yas-global-mode 1))
```

key function
img\_ insert an html snippet for images
pic\_ inserts a snippet for latex images
orgtemp inserts a template for org files
orgrev template for org-reveal
clk template for a clock report
orgconf template for config files writen in org
\_project inserts a section for a new project

Enables fuzzy search in the suggestions buffers. In addition this code enables the IDO package which ships with Emacs.

  • https://github.com/lewang/flx

    ;; extend IDO with fussy search
    (use-package flx-ido
        :ensure t
        :init
        ;; enable IDO
        (ido-mode 1)
        (ido-everywhere t)
        :config
        (flx-ido-mode 1)
        (setq ido-enable-flex-matching t
              ido-use-faces nil))

Displays a small buffer which shows all the possible key combinations you can press at the current moment.

discover-my-major is a package which shows you all the keybindings related to the current major mode. While you can get this information with the C-h m keybindings it’s a bit easier to view in this mode.

``` emacs-lisp
(use-package langtool
    :ensure t
    :config
    (setq langtool-language-tool-jar "~/.emacs.d/languagetool/languagetool-commandline.jar"))
```

This code has been taken from this post: https://sam217pa.github.io/2016/09/23/keybindings-strategies-in-emacs/ I should read it again to get a better understanding of hydra and general.

``` emacs-lisp
(use-package hydra
    :ensure t)

(defhydra hydra-buffer (:color red :columns 3)
"
                Buffers :
"
("n" next-buffer "next")
("b" ido-switch-buffer "switch")
("B" ibuffer "ibuffer")
("p" previous-buffer "prev")
("C-b" buffer-menu "buffer menu")
("N" evil-buffer-new "new")
("d" kill-this-buffer "delete")
;; don't come back to previous buffer after delete
("D" (progn (kill-this-buffer) (next-buffer)) "Delete")
("s" save-buffer "save"))

(defhydra hydra-window-operations (:color red :columns 3)
"
                    Windows :
"
("h" windmove-left "left")
("l" windmove-right "right")
("j" windmove-down "down")
("k" windmove-up "up")
("w" (lambda () (interactive) (other-window 1)) "Unkown")
("h" split-window-vertically "horizontal split")
("v" split-window-horizontally "vertical split")
("o" delete-other-windows "Delete all other")
("X" delete-window "Delete this")
("x" (lambda ()
        (interactive)
        (ace-window 16)
        (add-hook 'ace-window-end-once-hook
                'hydra-window/body)) "Unknown")
("z" (lambda ()
        (interactive)
        (ace-window 4)
        (add-hook 'ace-window-end-once-hook
                'hydra-window/body)) "Swap")
("=" balance-windows "Balance")
("K" kill-this-buffer "Kill Buffer")
("D" kill-all-dired-buffers "Kill all dired")
("a" ace-window :exit t "Ace")
("S" toggle-window-split "Toggle Split"))

(defhydra hydra-restclient (:color red :columns 3)
"
                    Restclient :
"
("j" restclient-jump-next "next")
("k" restclient-jump-prev "previous")
("c" restclient-http-send-current-stay-in-window "run"))

(defhydra hydra-flycheck (:color red :columns 3)
"
                    Flycheck :
"
("f"  flycheck-error-list-set-filter                            "Filter")
("j"  flycheck-next-error                                       "Next")
("k"  flycheck-previous-error                                   "Previous")
("gg" flycheck-first-error                                      "First")
("G"  (progn (goto-char (point-max)) (flycheck-previous-error)) "Last"))

(defhydra hydra-ediff (:color blue :columns 3)
"
                    Ediff :
"
("b" ediff-buffers "buffers")
("B" ediff-buffers3 "buffers 3-way")
("f" ediff-files "files")
("F" ediff-files3 "files 3-way")
("c" ediff-current-file "current file")
("r" ediff-revision "revision")
("l" ediff-regions-linewise "linewise")
("w" ediff-regions-wordwise "wordwise"))

(defhydra hydra-yasnippet (:color blue :columns 3)
"
                    Yasnippet:
"
("d" yas-load-directory "directory")
("e" yas-activate-extra-mode "extra")
("i" yas-insert-snippet "insert")
("f" yas-visit-snippet-file "file")
("n" yas-new-snippet "new")
("t" yas-tryout-snippet "tryout")
("l" yas-describe-tables "list")
("a" yas-reload-all "reload"))

(defhydra hydra-apropos (:color blue :columns 3)
    "Apropos"
    ("a" apropos "apropos")
    ("c" apropos-command "cmd")
    ("d" apropos-documentation "doc")
    ("e" apropos-value "val")
    ("l" apropos-library "lib")
    ("o" apropos-user-option "option")
    ("v" apropos-variable "var")
    ("i" info-apropos "info")
    ("t" tags-apropos "tags")
    ("z" hydra-customize-apropos/body "customize"))

(defhydra hydra-customize-apropos (:color blue :columns 3)
    "Apropos (customize)"
    ("a" customize-apropos "apropos")
    ("f" customize-apropos-faces "faces")
    ("g" customize-apropos-groups "groups")
    ("o" customize-apropos-options "options"))

(define-key Info-mode-map (kbd "?") #'hydra-info/body)
(defhydra hydra-info (:color blue
            :hint nil)
    "
    Info-mode:

    ^^_]_ forward  (next logical node)       ^^_l_ast (←)        _u_p (↑)                             _f_ollow reference       _T_OC
    ^^_[_ backward (prev logical node)       ^^_r_eturn (→)      _m_enu (↓) (C-u for new window)      _i_ndex                  _d_irectory
    ^^_n_ext (same level only)               ^^_H_istory         _g_oto (C-u for new window)          _,_ next index item      _c_opy node name
    ^^_p_rev (same level only)               _<_/_t_op           _b_eginning of buffer                virtual _I_ndex          _C_lone buffer
    regex _s_earch (_S_ case sensitive)      ^^_>_ final         _e_nd of buffer                      ^^                       _a_propos

    _1_ .. _9_ Pick first .. ninth item in the node's menu.

    "
    ("]"   Info-forward-node)
    ("["   Info-backward-node)
    ("n"   Info-next)
    ("p"   Info-prev)
    ("s"   Info-search)
    ("S"   Info-search-case-sensitively)

    ("l"   Info-history-back)
    ("r"   Info-history-forward)
    ("H"   Info-history)
    ("t"   Info-top-node)
    ("<"   Info-top-node)
    (">"   Info-final-node)

    ("u"   Info-up)
    ("^"   Info-up)
    ("m"   Info-menu)
    ("g"   Info-goto-node)
    ("b"   beginning-of-buffer)
    ("e"   end-of-buffer)

    ("f"   Info-follow-reference)
    ("i"   Info-index)
    (","   Info-index-next)
    ("I"   Info-virtual-index)

    ("T"   Info-toc)
    ("d"   Info-directory)
    ("c"   Info-copy-current-node-name)
    ("C"   clone-buffer)
    ("a"   info-apropos)

    ("1"   Info-nth-menu-item)
    ("2"   Info-nth-menu-item)
    ("3"   Info-nth-menu-item)
    ("4"   Info-nth-menu-item)
    ("5"   Info-nth-menu-item)
    ("6"   Info-nth-menu-item)
    ("7"   Info-nth-menu-item)
    ("8"   Info-nth-menu-item)
    ("9"   Info-nth-menu-item)

    ("?"   Info-summary "Info summary")
    ("h"   Info-help "Info help"))

(defhydra hydra-projectile (:color teal :columns 3)
    "Projectile"
    ("f"   projectile-find-file                "Find File")
    ("r"   projectile-recentf                  "Recent Files")
    ("z"   projectile-cache-current-file       "Cache Current File")
    ("x"   projectile-remove-known-project     "Remove Known Project")

    ("d"   projectile-find-dir                 "Find Directory")
    ("b"   projectile-switch-to-buffer         "Switch to Buffer")
    ("c"   projectile-invalidate-cache         "Clear Cache")
    ("X"   projectile-cleanup-known-projects   "Cleanup Known Projects")

    ("o"   projectile-multi-occur              "Multi Occur")
    ("s"   projectile-switch-project           "Switch Project")
    ("k"   projectile-kill-buffers             "Kill Buffers")
    ("q"   nil "Cancel" :color blue))

(defhydra hydra-spellchecking (:color blue :columns 3)
    "
    Spell Checking
    "
    ("g" langtool-check "Check grammar")
    ("q" langtool-check-done "Finish grammar check")
    ("l" langtool-switch-default-language "Switch grammar language")
    ("m" langtool-show-message-at-point "unkown")
    ("b" langtool-correct-buffer "Correct grammar in buffer")
    ("s" ispell "Correct Spelling")
    ("d" ispell-change-dictionary "Change dictionary"))
```

``` emacs-lisp
(use-package general
    :ensure t
    :config
    (general-def
        :states '(normal visual insert emacs)
        :prefix "SPC"
        :non-normal-prefix "M-SPC"
        "b" '(hydra-buffer/body t :which-key "Buffer")
        "w" '(hydra-window-operations/body t :which-key "Windows")
        "r" '(hydra-restclient/body t :which-key "Restclient")
        "f" '(hydra-flycheck/body t :which-key "Flycheck")
        "s" '(hydra-spellchecking/body t :which-key "Spell Checking")
        "e" '(hydra-ediff/body t :which-key "Diffing")
        "y" '(hydra-yasnippet/body t :which-key "Yasnippets")
        "p" '(hydra-projectile/body t :which-key "Projectile")
        "a" '(hydra-apropos/body t :which-key "Apropos Commands")))
```

``` emacs-lisp
(use-package helpful
    :ensure t
    :bind (("C-h f" . helpful-function)
           ("C-h v" . helpful-variable)
           ("C-h s" . helpful-symbol)
           ("C-h k" . helpful-key)
           ("C-c h f" . helpful-function)
           ("C-c h v" . helpful-variable)
           ("C-c h c" . helpful-command)
           ("C-c h m" . helpful-macro)
           ("<C-tab>" . backward-button)
           :map helpful-mode-map
           ("M-?" . helpful-at-point)
           :map emacs-lisp-mode-map
           ("M-?" . helpful-at-point)
           :map lisp-interaction-mode-map  ; Scratch buffer
           ("M-?" . helpful-at-point)))
```

``` emacs-lisp
(use-package ibuffer-projectile
    :after ibuffer
    :preface
    (defun my/ibuffer-projectile ()
        (ibuffer-projectile-set-filter-groups)
        (unless (eq ibuffer-sorting-mode 'alphabetic)
                (ibuffer-do-sort-by-alphabetic)))
    :hook (ibuffer . my/ibuffer-projectile))
```

This packages allows you to automatically commit changes in a file after saving it. I use it to automatically commit changes in my org directory. https://github.com/ryuslash/git-auto-commit-mode

``` emacs-lisp
(use-package git-auto-commit-mode
    :ensure t)
```

The following package provide an improved support for their corresponding language.

Since PHP mode isn’t working very well it’s better to work in web-mode and extend it to work better with PHP.

``` emacs-lisp
;; enable php-mode for php syntax highlighting and more
(defun my/php-setup ()
    (web-mode)

    (make-local-variable 'web-mode-code-indent-offset)
    (make-local-variable 'web-mode-markup-indent-offset)
    (make-local-variable 'web-mode-css-indent-offset)

    (setq web-mode-css-indent-offset 2
          web-mode-code-indent-offset 4
          web-mode-markup-indent-offset 2)

    (flycheck-select-checker 'my-php)
    (ac-php-mode)
    (flycheck-mode t))
```

  • Note taken on [2018-02-10 Sa 14:50] The keybinding is broken because I moved deft to F5.

Web-mode helps greatly if your programming HTML code. To switch between web-mode (for HTML) and php-mode (for PHP) bound a toggle function to F5.

``` emacs-lisp
;; web-mode for general web development
(use-package web-mode
    :ensure t
    :mode
    (("\\.phtml\\'" . web-mode)
    ("\\.tpl\\'" . web-mode)
    ("\\.[agj]sp\\'" . web-mode)
    ("\\.as[cp]x\\'" . web-mode)
    ("\\.erb\\'" . web-mode)
    ("\\.mustache\\'" . web-mode)
    ("\\.djhtml\\'" . web-mode)
    ("\\.html?\\'" . web-mode))
    :config
    (add-to-list 'auto-mode-alist '("\\.php$" . my/php-setup)))
```

Provides syntax highlighting and other functions for markdown.

``` emacs-lisp
;; add markdown-mode to edit markdown files
(use-package markdown-mode
    :ensure t
    :commands (markdown-mode gfm-mode)
    :mode (("README\\.md\\'" . gfm-mode)
           ("\\.md\\'" . markdown-mode)
           ("\\.markdown\\'" . markdown-mode))
    :init (setq markdown-command "multimarkdown"))
```

Provides syntax highlighting and other functions for C#.

``` emacs-lisp
;;add csharp support
(use-package csharp-mode
    :ensure t
    :mode (("\\.cs\\'" . csharp-mode))
    :config
    (when (is-windows-p)
          (progn
              (use-package omnisharp
              :ensure t
              :after csharp-mode
              :config
              (define-key omnisharp-mode-map (kbd ".") 'omnisharp-add-dot-and-auto-complete)
              (define-key omnisharp-mode-map (kbd "<C-SPC>") 'omnisharp-auto-complete))

    (eval-after-load
        'company
        '(add-to-list 'company-backends #'company-omnisharp))

    (defun my-csharp-mode-setup ()
        (omnisharp-mode)
        (company-mode)
        (flycheck-mode)

        (setq indent-tabs-mode nil
              c-syntactic-indentation t
              c-basic-offset 4
              truncate-lines t)
        (c-set-style "ellemtel")

        (local-set-key (kbd "C-c r r") 'omnisharp-run-code-action-refactoring)
        (local-set-key (kbd "C-c C-c") 'recompile))

    (add-hook 'csharp-mode-hook 'my-csharp-mode-setup t)))
```

Provides syntax highlighting and other functions for powershell scripts.

``` emacs-lisp
;; enable powershell-mode
(use-package powershell
    :ensure t
    :mode
    (("\\.ps1\\'" . powershell-mode)
    ("\\.psm1\\'" . powershell-mode)))
```

Auctex is a great package which helps you a lot when you’re writing LaTeX documents.

``` emacs-lisp
;; auctex a greate plugin for latex writing
(use-package tex
    :ensure auctex
    :mode ("\\.tex\\'" . latex-mode)
    :bind
    ("C-c -" . TeX-next-error)
    :config
    (setq-default TeX-master nil)
    (setq TeX-auto-save t
          TeX-parse-self t
          TeX-electric-math (quote ("\\(" . "\\)"))
          LaTeX-electric-left-right-brace t
          TeX-view-program-selection)
    (quote
        (((output-dvi has-no-display-manager)
        "dvi2tty")
        ((output-dvi style-pstricks)
        "dvips and gv")
        (output-dvi "xdvi")
        (output-pdf "PDF Tools")
        (output-html "xdg-open"))))
```

Plantuml-mode lets you create diagrammes from plantuml code and provides the correct syntax highlighting.

``` emacs-lisp
;; add plantuml-mode to draw diagramms
(use-package plantuml-mode
    :load-path "~/.emacs.d/packages/plantuml-mode"
    :mode
    (("\\.puml\\'" . plantuml-mode)
    ("\\.plantuml\\'" . plantuml-mode))
    :config
    (setq plantuml-jar-path "~/.emacs.d/plantuml.jar")
    (setq plantuml-output-type "png"))
```

A major-mode to write and compile graphviz files. To compile a file you can use a C-c c and to preview an image you have to press C-c p.

A mode to work with yaml files.

``` emacs-lisp
(use-package yaml-mode
    :ensure t)
```

While Emacs comes by default with python-mode installed. I want to use the updated version from Melpa.

``` emacs-lisp
(use-package python-mode
    :ensure t
    :config
    (setq python-shell-interpreter "python3"))
```

elpy is a package which helps you greatly with writing Python code. It provides all the things you would which from an IDE.

  • https://github.com/jorgenschaefer/elpy

    (when (is-linux-p)
    (use-package elpy
        :ensure t
        :config
        (setq elpy-rpc-python-command "python3")
        (add-hook 'python-mode-hook (lambda () (highlight-indentation-mode -1)))
        (elpy-enable)))

``` emacs-lisp
(use-package css-mode
    :ensure t
    :config (setq css-indent-offset 2))
```

``` emacs-lisp
(use-package js2-mode
    :ensure t
    :hook (js2-mode . js2-imenu-extras-mode)
    :mode "\\.js\\'"
    :config (setq js-indent-level 2))

(use-package js2-refactor
    :after js2-mode
    :ensure t
    :bind (:map js2-mode-map
          ("C-k" . js2r-kill)
          ("M-." . nil))
    :hook ((js2-mode . js2-refactor-mode)
           (js2-mode . (lambda ()
                        (add-hook 'xref-backend-functions
                                  #'xref-js2-xref-backend nil t))))
    :config (js2r-add-keybindings-with-prefix "C-c C-r"))

(use-package xref-js2
    :ensure t)
```

``` emacs-lisp
(use-package yaml-mode
    :ensure t
    :mode "\\.yml\\'"
    :interpreter ("yml" . yml-mode))
```

company-mode is a package to provide auto-complete functionality.

  • https://company-mode.github.io/

    (use-package company
        :ensure t
        :bind
        ("C-<tab>" . company-complete)
        :config
        (define-key company-active-map (kbd "TAB") 'company-complete-common-or-cycle)
        (define-key company-active-map (kbd "<tab>") 'company-complete-common-or-cycle)
        (setq company-dabbrev-downcase nil)
        (add-hook 'after-init-hook 'global-company-mode)
    
        ;; Add yasnippet support for all company backends
        (defvar company-mode/enable-yas t
            "Enable yasnippet for all backends.")
        (defun company-mode/backend-with-yas (backend)
            (if (or (not company-mode/enable-yas) (and (listp backend) (member 'company-yasnippet backend)))
            backend
                (append (if (consp backend) backend (list backend))
                  '(:with company-yasnippet))))
        (setq company-backends (mapcar #'company-mode/backend-with-yas company-backends)))

Auctex support for company-mode.

https://github.com/dakra/dmacs/blob/master/init.org

``` emacs-lisp
(use-package company-web
    :ensure t
    :after web-mode
    :commands company-web-html
    :config
    (require 'company-web-html)

    ;; Tide completion support in web-mode with company-mode
    (defun my-web-mode-hook ()
        "Hook for `web-mode'."
        (set (make-local-variable 'company-backends)
            '(company-tide company-web-html company-yasnippet company-files)))

    (add-hook 'web-mode-hook 'my-web-mode-hook)

    ;; Enable JavaScript completion between <script>...</script> etc.
    (defadvice company-tide (before web-mode-set-up-ac-sources activate)
        "Set `tide-mode' based on current language before running company-tide."
        (if (equal major-mode 'web-mode)
            (let ((web-mode-cur-language
                (web-mode-language-at-pos)))
                (if (or (string= web-mode-cur-language "javascript")
                    (string= web-mode-cur-language "jsx"))
                (unless tide-mode (tide-mode))
                (if tide-mode (tide-mode -1)))))))
```

https://github.com/dakra/dmacs/blob/master/init.org

``` emacs-lisp
(use-package company-restclient
    :ensure t
    :after (restclient company)
    :config (add-to-list 'company-backends 'company-restclient))
```

``` emacs-lisp
(use-package ac-php
    :after (company web-mode)
    :custom (ac-sources '(ac-source-php))
    :config
    (auto-complete-mode t)
    (ac-php-core-eldoc-setup))
```

Magit is a package which allows you to control git from within Emacs. It’s really great and makes using git so much easier. Thanks to Magit alone I think I became a much better git user.

  • https://magit.vc/

    ;; enable magit a great git porcelain.
    (use-package magit
        :ensure t
        :commands magit-status
        :bind
        ("<f10>" . magit-status))

Each level of parantheses has it’s own colour to differentiate them more easily.

``` emacs-lisp
;; change the colours of parenthesis the further out they are
(use-package rainbow-delimiters
    :ensure t
    :config
    (add-hook 'prog-mode-hook #'rainbow-delimiters-mode))
```

restclient is as it says a rest client package. Very useful if you’re doing web developmenet.

``` emacs-lisp
(use-package restclient
    :ensure t)

(use-package restclient-helm
    :ensure t
    :after (restclient helm))
```

This package let’s you debug your Emacs config which makes it easier to find bugs before you restart Emacs.

https://github.com/Malabarba/elisp-bug-hunter/blob/master/README.org

``` emacs-lisp
(use-package bug-hunter
    :ensure t)
```

flycheck is a handy tool to provide syntax checking on the fly.

  • http://www.flycheck.org/en/latest/

    (use-package flycheck
        :ensure t
        :init (global-flycheck-mode)
        (define-key flycheck-mode-map flycheck-keymap-prefix nil)
        ;; the key is the ! key without pressing shift
        (setq flycheck-keymap-prefix (kbd "C-c `"))
        (define-key flycheck-mode-map flycheck-keymap-prefix
            flycheck-command-map))

This mode shows lines to display the current level of indentation. I’ve enabled it in text- and prog-modes. https://github.com/DarthFennec/highlight-indent-guides

``` emacs-lisp
(use-package highlight-indent-guides
    :ensure t
    :config
    (setq highlight-indent-guides-method 'character
          hightlight-indentation-mode nil
          highlight-indent-guides-auto-enabled nil)
    (set-face-background 'highlight-indent-guides-odd-face "darkgray")
    (set-face-background 'highlight-indent-guides-even-face "gray")
    (set-face-foreground 'highlight-indent-guides-character-face "gray")
    (add-hook 'text-mode-hook 'highlight-indent-guides-mode)
    (add-hook 'prog-mode-hook 'highlight-indent-guides-mode))
```

This settings provides the color-theme package and sets a color-theme which I store in my sourec directory as the current theme.

``` emacs-lisp
;; Color theme
(use-package color-theme
    :ensure t
    :config
    (color-theme-initialize))

;; load solarized color theme
(use-package color-theme-solarized
    :config
    (setq frame-background-mode (quote light))
    (load-theme 'solarized t))
```

I like to work in a distraction free editor that’s why I hide all the menues, toolbars and scrollbars.

``` emacs-lisp
;; disable menu and toolbar
(tool-bar-mode -1)
(menu-bar-mode -99)
(scroll-bar-mode -1)
```

By default Emacs doesn’t do line wrapping. Instead it shows you on the edge of the buffer a marker to indicate that the line continues further. I’m not really a fan of this behaviour, that’s why I disable it and enable proper line wrapping.

``` emacs-lisp
; Proper line wrapping
(global-visual-line-mode 1)
```

fringe-mode are the little markers on the side of the buffer which show when a line continues over the edge. Since I use visual-line-mode this settings isn’t required anymore.

``` emacs-lisp
; Disable fringe because I use visual-line-mode
(set-fringe-mode '(0 . 0))
```

Emacs usually displays a splash screen with some helpful information for beginners. I don’t really need that anymore.

``` emacs-lisp
; Disable splash screen
(setq inhibit-splash-screen t)
```

Can’t remember how they look like but I’ve disabled them probably for a reason :).

``` emacs-lisp
(tooltip-mode -1)
(setq tooltip-use-echo-area t)
```

I don’t want to type the whole word. The starting characters should be enough.

``` emacs-lisp
;; disable or reconfigure prompts
(fset 'yes-or-no-p 'y-or-n-p) ;; remap yes or no to y or n
```

“Yes I’m sure that I want to create a new buffer.”, options.

``` emacs-lisp
(setq confirm-nonexistent-file-or-buffer nil);; just create buffers don't ask
(setq ido-create-new-buffer 'always)
```

This options shows you where you accidentally left some bad whitespace.

``` emacs-lisp
;; highlight bad whitespace
(use-package whitespace
    :ensure t
    :config
    (setq whitespace-style '(face lines-tail tabs trailing))
    (global-whitespace-mode t))
```

Set the font depending on the operating system.

``` emacs-lisp
; for linux
(when (is-linux-p)
    (set-face-attribute 'default nil :font "Source Code Pro-14:weight=regular"))
; for windows
(when (is-windows-p)
    (set-face-attribute 'default nil :font "Source Code Pro-12:weight=regular"))
```

By default the doc-view looks a bit fuzzy. With a resolution of 200 it’s much more comfortable to read a PDF.

``` emacs-lisp
;; improve the resolution of doc-view
(setq doc-view-resolution 200)
```

I always want to run Emacs maximized. The easiest solution to this problem is the following code. The downside of this is, that if Emacs is already maximized it will switch to windowed mode.

``` emacs-lisp
(toggle-frame-maximized)
```

``` emacs-lisp
(setq-default mode-line-format
              '("%e"
                mode-line-front-space
                ;; mode-line-mule-info
                mode-line-client
                mode-line-modified
                mode-line-remote
                mode-line-frame-identification
                mode-line-buffer-identification
                "   "
                mode-line-position
                (vc-mode vc-mode)
                ;; "  "
                ;; mode-line-modes
                "   "
                mode-line-misc-info
                ;; battery-mode-line-string
                ;; mode-line-end-spaces
        ))
```

Please be aware that the whole org-mode configuration is one code block. While I’ve splitted it here for a better understanding it needs to be used in it’s entirety or it won’t work.

Provides prettier bullets for org-mode headings.

``` emacs-lisp
;; Enable pretty bullets in org mode
(use-package org-bullets
    :ensure t
    :config
    (add-hook 'org-mode-hook (lambda ()
                               (org-bullets-mode 1))))
```

This package is needed to export the current agenda view to a HTML file. The command to do so is C-x C-w.

``` emacs-lisp
;; add a package to convert the agenda view to HTML
(use-package htmlize
    :ensure t
    :after org)
```

org-edna allows you to set blockers and triggers on tasks. I’m using it currently to toggle projects in the project list to the NEXT state if all it’s tasks get toggled to DONE. This way I know that I have to look at the project either to close it or to add more tasks.

http://www.nongnu.org/org-edna-el/

``` emacs-lisp
;; add a package to convert the agenda view to HTML
(use-package org-edna
    :ensure t
    :after org
    :config
    (org-edna-load))
```

Allows to link to man pages in an org-mode file. This can be quite useful for taking notes about a Unix system. The code is taken from here: https://orgmode.org/manual/Adding-hyperlink-types.html

``` emacs-lisp
;;; org-man.el - Support for links to manpages in Org

(require 'org)

(org-add-link-type "man" 'org-man-open)
(add-hook 'org-store-link-functions 'org-man-store-link)

(defcustom org-man-command 'man
    "The Emacs command to be used to display a man page."
    :group 'org-link
    :type '(choice (const man) (const woman)))

(defun org-man-open (path)
    "Visit the manpage on PATH.
    PATH should be a topic that can be thrown at the man command."
    (funcall org-man-command path))

(defun org-man-store-link ()
    "Store a link to a manpage."
    (when (memq major-mode '(Man-mode woman-mode))
    ;; This is a man page, we do make this link
        (let* ((page (org-man-get-page-name))
            (link (concat "man:" page))
            (description (format "Manpage for %s" page)))
            (org-store-link-props
                :type "man"
                :link link
                :description description))))

(defun org-man-get-page-name ()
    "Extract the page name from the buffer name."
    ;; This works for both `Man-mode' and `woman-mode'.
    (if (string-match " \\(\\S-+\\)\\*" (buffer-name))
        (match-string 1 (buffer-name))
        (error "Cannot create link to this man page")))

(provide 'org-man)

;;; org-man.el ends here
(require 'org-man)
```

org-noter is a very useful package for note tacking on PDFs. It allows you to write notes in an .org file and keeps track on where you are in the PDF and extends your notes with that information so that you can easily find the related page to your notes. In addition it helps you with navigating directly to the related page. I like this very much because I’m not a big fan of taking notes directly in the PDF files.

  • https://github.com/weirdNox/org-noter

    ;; which key is a package to show which keys can be pressed
    (use-package org-noter
        :ensure t
        :after org
        :bind (:map org-mode-map
            ("C-c n" . org-noter))
        :config
        (setq org-noter-separate-notes-from-heading t))

``` emacs-lisp
(use-package org-ref
    :ensure t
    :after org
    :config
    (setq org-ref-default-citation-link "footcite"))
```

This settings make sure the org-mode is downloaded from the org repository.

``` emacs-lisp
(use-package org
    :ensure t
    :pin org
    :config
```

This defines org-modes main keybindings which are:

  • Open the agenda C-c a
  • Capture a task C-c c
  • Switch org buffer C-c b

        ;; enable org-mode keys
        (define-key global-map "\C-ca" 'org-agenda)
        (global-set-key "\C-cl" 'org-store-link)
        (global-set-key "\C-cc" 'org-capture)
        (global-set-key "\C-cb" 'org-iswitchb)
    
    ;; evil keybindings for the org-agenda
        (evil-add-hjkl-bindings org-agenda-mode-map 'emacs
        (kbd "/")       'evil-search-forward
        (kbd "n")       'evil-search-next
        (kbd "N")       'evil-search-previous
        (kbd "C-d")     'evil-scroll-down
        (kbd "C-u")     'evil-scroll-up
        (kbd "c")       'org-capture
        (kbd "C-w C-w") 'other-window)

By default the command M-RET enters a new heading which splits the current line at the cursor position. This can get very annoying at times that’s why I disable this behaviour.

``` emacs-lisp
;; disable line split with M-RET
(setq org-M-RET-may-split-line (quote ((default))))
```

Indentation in source code blocks should match the corresponding language in the code block.

``` emacs-lisp
;; enable the correct intdentation for source code blocks
(setq org-edit-src-content-indentation 0)
(setq org-src-tab-acts-natively t)
(setq org-src-preserve-indentation t)
```

This setting enables ido in org-mode.

``` emacs-lisp
;; enable ido for org-mode
(setq org-completion-use-ido t)
```

I’ve configured the archive function so that when I archive a tasks it gets archived into an archive folder and put into a monthly file.

``` emacs-lisp
;; archive files to a monthly file
(when (is-linux-p)
(setq org-archive-location
    (concat "~/git_repos/notes/personal/notes/archive/"
        (format-time-string "%Y-%m" (current-time)) "-%s::* "(format-time-string "%Y-%m-%d" (current-time)))))
(when (is-windows-p)
    (setq org-archive-location
        (concat "~/git_repos/notes/work/notes/archive/"
            (format-time-string "%Y-%m" (current-time)) "-%s::* "(format-time-string "%Y-%m-%d" (current-time)))))
```

This settings only lets you close a parent task if you closed all it’s parents.

``` emacs-lisp
;; enable todo and checkbox depencies
(setq org-enforce-todo-dependencies t)
(setq org-enforce-todo-checkbox-dependencies t)
```

This configuration defines the TODO and DONE keywords one can use for headings.

TODO keywords

  • TODO
  • NEXT
  • WAITING

DONE keywords

  • DONE
  • CANCELLED

If you want to change the TODO states of a heading you have to toggle through all of them which can be quite annoying if you want to use a state which is somewhere at the end of the list. With this setting Emacs shows you the complete list and you can select the corresponding key to apply your desired TODO state.

``` emacs-lisp
;; quick access for todo states
(setq org-todo-keywords
    '((sequence "TODO(t)" "NEXT(n)" "WAITING(w!)" "|" "DONE(d)")
        (sequence "|" "CANCELLED(c)")))
```

In order to keep track when I finished a tasks I would like to have a timestamp which records this information for me.

``` emacs-lisp
(setq org-log-done 'time)
```

In addition I would like to record when I set a WAITING state for a task. This way I know when I started waiting. This allows me for example to get back to a person if I haven’t back from them for two weeks.

``` emacs-lisp
(setq org-log-into-drawer t)
```

This option replaces the heading stars with indents. Which makes the outline more readable and gives a better overview.

``` emacs-lisp
;; enable org-indent
(setq org-startup-indented t)
```

To insert a snippet insert < followed by the matching character and hit TAB. Currently I have setup the following characters:

Character Function
p insert a latex picture
``` emacs-lisp
;; input templates
(eval-after-load 'org
    '(progn
        (add-to-list 'org-structure-template-alist
            '("p" "#+attr_latex: :center\n[[]]" "<src lang=\"?\">\n\n</src>"))))
```

Capture templates are templates with which you can predefine tasks/projects. Each template gets it’s own keybinding which you can call from C-c c.

Since I’m required to report my working hours at my job I’ve added some clocking functionality to my work related capture templates. The one I like the most ist the daily clock. This adds an entry with the current date as a heading and starts a clock on it. This makes it very easy to start the clock at the beginning of the day.

``` emacs-lisp
;; capture templates
(when (is-linux-p)
    (setq org-capture-templates
        (quote
            (("i" "IBZ Journal entry" plain
                (file+datetree "~/git_repos/notes/personal/notes/ibz/ibz_journal.org")
                "%?\n" :kill-buffer :empty-lines 1)
            ("j" "Journal entry" plain
                (file+datetree "~/git_repos/notes/personal/notes/journal.org")
                "%?\n" :kill-buffer :empty-lines 1)
            ("p" "Small Project" entry
                (file+headline "~/git_repos/notes/personal/agenda/personal.org" "Capture")
                (file "~/git_repos/notes/settings/templates/temp_personal_small_project.txt"))
            ("t" "Adds a Next entry" entry
                (file+headline "~/git_repos/notes/personal/agenda/personal.org" "Capture")
                (file "~/git_repos/notes/settings/templates/temp_personal_todo.txt")
                "%?\n" :empty-lines 1)))))

(when (is-windows-p)
    (setq org-capture-templates
        (quote
            (("j" "Journal Entry" entry
                (file+headline "~/git_repos/notes/work/agenda/work.org" "Clock")
                (file "~/git_repos/notes/settings/templates/temp_clock_note.txt")
                :empty-lines 1)
            ("t" "Adds a Next entry" entry
                (file+headline "~/git_repos/notes/work/agenda/work.org" "Capture")
                (file "~/git_repos/notes/settings/templates/temp_work_todo.txt")
                :clock-in t :clock-resume t :empty-lines 1)
            ("p" "Small Project" entry
                (file+headline "~/git_repos/notes/work/agenda/work.org" "Capture")
                (file "~/git_repos/notes/settings/templates/temp_work_small_project.txt"))
            ("m" "Meeting" entry (file+headline "~/git_repos/notes/work/agenda/work.org" "Capture")
                "* MEETING: with %?\n" :clock-in t :clock-resume t :empty-lines 1)))))
```

Adjust the format of the org-columns.

``` emacs-lisp
;; org-columns format
(setq org-columns-default-format
    "%40ITEM(Task) %8Effort(Estimated Effort){:} %8CLOCKSUM %10TAGS")
```

Configuration of the available effort times. You can set efforts with C-c C-x e.

``` emacs-lisp
;; available effort times
(setq org-global-properties
    (quote
        (("Effort_ALL" . "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00"))))
```

The refile option lets you move headings to other headings or into different files. I configured org-mode so that it allows to create parent headings and looks 6 levels deep for headings to file under. In addition I changed the way the refile menu works. It’s a bit slower this way but gives in my opinion a better overview.

``` emacs-lisp
;; org-refile options
(setq org-refile-allow-creating-parent-nodes (quote confirm))
(setq org-refile-targets (quote ((org-agenda-files :maxlevel . 6))))
(setq org-refile-use-outline-path 'file)
```

When displaying folded headings show the empty line between them.

``` emacs-lisp
;; set to line seperator to 1
(setq org-cycle-separator-lines 1)
```

By default syntax highlighting is not enabled for code blocks however it’s very nice to have that’s why I enabled it.

``` emacs-lisp
(setq org-src-fontify-natively t)
```

To make latex code in org-mode stand out a bit more we can enable syntax highlighting for it.

``` emacs-lisp
(setq org-highlight-latex-and-related '(latex))
```

I want to display images in org-mode.

``` emacs-lisp
(setq org-image-actual-width (quote (500)))
(setq org-startup-with-inline-images t)
```

When creating an internal org link this code creates a unique ID to the heading. This allows the links to continue working even when the file is in a new location.

``` emacs-lisp
(setq org-id-link-to-org-use-id 'create-if-interactive-and-no-custom-id
    org-clone-delete-id t)
```

Headings should be separated by their content by an empty line. This way the files are easier to read.

``` emacs-lisp
(setq org-blank-before-new-entry
    (quote ((heading . t)
        (plain-list-item . auto))))
```

This is currently not working correctly that’s why I customized it through org-customize. Configures the available export formats.

``` emacs-lisp
;; org-export formats
;;(setq org-export-backends (quote (beamer html latex md odt reveal)))
```

Since we live in the 21st century org-mode should use HTML5.

``` emacs-lisp
(setq org-html-html5-fancy t
    org-html-doctype "html5")
```

Usually I don’t want the TODO states being dispayed in the exported files. If i still want to have them displayed I can add the option:

```
#+options: todo:t
```

To the beginning of the file.

``` emacs-lisp
;; disable the Todo keywords in the export
(setq org-export-with-todo-keywords nil)
```

As with the TODO states I want to disable the tags as well. The per file option for this setting is:

```
#+options: tags:t
```

``` emacs-lisp
;; disable the tags in the export
(setq org-export-with-tags nil)
```

I want captions to appear below tables and not above.

``` emacs-lisp
(setq org-latex-caption-above nil)
```

The default settings export text like 10^2 or this_file as 10^{2} and this_{file}. Since I often use underline in filenames and variables it’s easiert for me to use 10^{2} and this_{file} to achieve this.

``` emacs-lisp
(setq org-export-with-sub-superscripts nil)
```

Different languages use different types of quotes. With this option org-mode tries to be smart about it and export the correct quotes.

``` emacs-lisp
(setq org-export-with-smart-quotes t)
```

By default org-mode exports headings down to level 3 as headings and the rest as lists. I want it to export headings down to level 5.

``` emacs-lisp
(setq org-export-headline-levels 5)
```

This specifies my options for Beamer exports. Level 1 headings generate title pages, the table of contents should be called “Inhalt” and the theme I’m using is metropolis.

``` emacs-lisp
;; options for beamer exports
(setq org-beamer-frame-level 2)
(setq org-beamer-outline-frame-options "")
(setq org-beamer-outline-frame-title "Inhalt")
(setq org-beamer-theme "metropolis")
```

Various LaTeX export settings. The most important one ist that I use my own class and customize the look of the title page.

``` emacs-lisp
;; options for latex exports
(setq org-latex-classes
    (quote
        (("beamer" "\\documentclass{az_beamer}"
            ("\\section{%s}" . "\\section*{%s}")
            ("\\subsection{%s}" . "\\subsection*{%s}")
            ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
         ("article" "\\documentclass{article}"
            ("\\section{%s}" . "\\section*{%s}")
            ("\\subsection{%s}" . "\\subsection*{%s}")
            ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
            ("\\paragraph{%s}" . "\\paragraph*{%s}")
            ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
         ("report" "\\documentclass[11pt]{report}"
            ("\\part{%s}" . "\\part*{%s}")
            ("\\chapter{%s}" . "\\chapter*{%s}")
            ("\\section{%s}" . "\\section*{%s}")
            ("\\subsection{%s}" . "\\subsection*{%s}")
            ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
         ("book" "\\documentclass[11pt]{book}"
            ("\\part{%s}" . "\\part*{%s}")
            ("\\chapter{%s}" . "\\chapter*{%s}")
            ("\\section{%s}" . "\\section*{%s}")
            ("\\subsection{%s}" . "\\subsection*{%s}")
            ("\\subsubsection{%s}" . "\\subsubsection*{%s}")))))
    (setq org-latex-default-packages-alist nil)
    (setq org-latex-listings 'listings)
    (setq org-latex-title-command "\\maketitle\\newpage")
    (setq org-latex-toc-command "\\tableofcontents
    \\newpage
    ")
```

To have easy access to my agenda view I’ve bound the F9 key to that function. Now I can access it with just one simple key press.

``` emacs-lisp
(global-set-key [f9] 'az/custom-agenda)
```

Tasks which are scheduled, have a deadline or have a timestamp but are marked DONE shouldn’t get displayed in the Agenda. I usually clean them out at the end of the week.

``` emacs-lisp
;; hide done tasks in the agenda
(setq org-agenda-skip-deadline-if-done t)
(setq org-agenda-skip-scheduled-if-done t)
(setq org-agenda-skip-timestamp-if-done t)
```

When you open the agenda command selection with C-c a you can choose these settings. They allow you to filter the current file for the TODO state corresponding to their keybinding. This is very useful for my weekly review.

Sorting Strategy By default org-mode sorts the agenda by the category. While this is great it mixes the NEXT actions with the WAITING actions which have a far lower priority than the NEXT actions. With this code the WAITING actions get sorted to the bottom.

``` emacs-lisp
;; Custom agenda command to list the stuck projects in the normal
;; agenda view.
(setq org-agenda-custom-commands
    (quote (("A" "Custom Agenda"
        ((agenda "" nil)
        (tags-todo "-CANCELLED/!"
            ((org-agenda-overriding-header "Stuck Projects")
                (org-agenda-skip-function 'bh/skip-non-stuck-projects)
                    (org-agenda-sorting-strategy (quote ((agenda
                        habit-down time-up priority-down todo-state-up)
                        (todo priority-down category-keep) (tags
                            priority-down category-keep) (search
                            category-keep))))))
        (tags-todo "PROJECT"
            ((org-agenda-overriding-header "Projects")
                (org-agenda-sorting-strategy
                    '(category-keep))))
                nil))
            ;; Show all headings with the corresponding TODO state
        ("N" occur-tree "NEXT")
        ("O" occur-tree "TODO")
        ("W" occur-tree "WAITING"))))
```

By default the agenda shows you prewarnings for tasks which have a deadline. However those don’t make much sense when I already have scheduled that item for this reason I disable the warnings for scheduled tasks.

``` emacs-lisp
;; don't show the warnings for deadlines if the item is scheduled
(setq org-agenda-skip-deadline-prewarning-if-scheduled t)
```

My default agenda view starts on the current day and shows the next 14 days. This gives me a good overview of whats comming and helps greatly with scheduling tasks. In addition I added an option to better display the tags in the agenda. With the new default settings they always get wrapped on a new line which is quite annoying.

;; start the agenda on the current day and show the next 13 days
``` emacs-lisp
(setq org-agenda-span 14
    org-agenda-start-on-weekday nil)
(setq org-agenda-tags-column -80)
(setq org-agenda-show-future-repeats (quote next))
```

Blocked tasks are apparently dimmed by default however I still have this setting enabled.

;; dimm open tasks
``` emacs-lisp
(setq org-agenda-dim-blocked-tasks t)
```

This function updates the agenda after you’ve captured a new task. Otherwise this would be a two step process.

;; automatically refresh the agenda after adding a task
``` emacs-lisp
(add-hook 'org-capture-after-finalize-hook 'nebucatnetzer:org-agenda-redo)

(defun nebucatnetzer:org-agenda-redo ()
(interactive)
(when (get-buffer "*Org Agenda*")
    (with-current-buffer "*Org Agenda*"
        (org-agenda-redo t)
        (message "[org agenda] refreshed!"))))
```

By default org-mode counts each heading without a TODO state as a stuck project. However I consider only headings with the TODO state a project and headings without a TODO state are mostly just notes on a project.

With this function org-mode will only show headings with a TODO state and no NEXT action as a stuck project.

Most of this code has been taken from here: http://doc.norang.ca/org-mode.html#Projects

;; Set the agenda separator to a space character.
``` emacs-lisp
(setq org-agenda-block-separator " ")

;; a function to call the custom agenda view.
(defun az/custom-agenda (&optional arg)
    (interactive "P")
    (org-agenda arg "A"))

(defun bh/is-project-p ()
    "Any task with a todo keyword subtask"
    (save-restriction
    (widen)
    (let ((has-subtask)
        (subtree-end (save-excursion (org-end-of-subtree t)))
        (is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
    (save-excursion
        (forward-line 1)
        (while (and (not has-subtask)
            (< (point) subtree-end)
            (re-search-forward "^\*+ " subtree-end t))
        (when (member (org-get-todo-state) org-todo-keywords-1)
        (setq has-subtask t))))
    (and is-a-task has-subtask))))

(defun bh/skip-non-stuck-projects ()
    "Skip trees that are not stuck projects"
    ;; (bh/list-sublevels-for-projects-indented)
    (save-restriction
    (widen)
    (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
        (if (bh/is-project-p)
            (let* ((subtree-end (save-excursion (org-end-of-subtree t)))
                (has-next ))
            (save-excursion
            (forward-line 1)
            (while (and (not has-next) (< (point) subtree-end) (re-search-forward "^\\*+ NEXT " subtree-end t))
                (unless (member "WAITING" (org-get-tags-at))
                (setq has-next t))))
            (if has-next
                next-headline
            nil)) ; a stuck project, has subtasks but no next task
            next-headline))))
```

I use the PROJECT tag to mark headings as projects. When searching for this headings I only want to see the top heading related to that tag. Therefore I have to exclude it from the inheritance feature.

``` emacs-lisp
(setq org-tags-exclude-from-inheritance (quote ("PROJECT")))
```

``` emacs-lisp
(setq org-clock-out-remove-zero-time-clocks t)
```

``` emacs-lisp
(defun start-heading-clock (id file)
    "Start clock programmatically for heading with ID in FILE."
    (require 'org-id)
    (if-let (marker (org-id-find-id-in-file id file t))
        (save-current-buffer
            (save-excursion
                (set-buffer (marker-buffer marker))
                (goto-char (marker-position marker))
                (org-clock-in)))
        (warn "Clock not started (Could not find ID '%s' in file '%s')" id file)))

(defun start-main-clock ()
    "This functions always clocks in to the * Clock heading"
    (interactive)
    (start-heading-clock "e9f71012-4370-4dd2-af8e-9ae14d86508a" "~/git_repos/notes/work/agenda/work.org"))

``` emacs-lisp
(global-set-key (kbd "<f6>") 'start-main-clock)
```

``` emacs-lisp
(org-clock-persistence-insinuate)
```

``` emacs-lisp
(setq org-clock-out-when-done t)
```

``` emacs-lisp
(setq org-clock-persist t)
;; Do not prompt to resume an active clock
(setq org-clock-persist-query-resume nil)
```

Clock in with C-x C-i and out with C-x C-o and C-x C-d to mark a task as the default task.

``` emacs-lisp
(global-set-key (kbd "<f7>") 'org-clock-in)
(global-set-key (kbd "<f8>") 'org-clock-out)
(global-set-key (kbd "C-x C-d") 'org-clock-mark-default-task)
```

At my job we have to report the times in decimal format. Therefore it’s a bit difficult when org-mode displays the time in the normal format. This code snippets let’s you display the time in decimal format in the clock table.

``` emacs-lisp
(setq org-duration-format (quote (("h") (special . 2))))
```

This function recursively tries to tangle all the files starting in the directory of the current buffer.

``` emacs-lisp
(defun directory-files-recursive (directory match maxdepth)
    "List files in DIRECTORY and in its sub-directories.
    Return files that match the regular expression MATCH. Recurse only
    to depth MAXDEPTH. If zero or negative, then do not recurse"
    (let* ((files-list '())
        (current-directory-list
            (directory-files directory t)))
        ;; while we are in the current directory
        (while current-directory-list
            (let ((f (car current-directory-list)))
                (cond
                ((and
                (file-regular-p f)
                (file-readable-p f)
                (string-match match f))
            (setq files-list (cons f files-list)))
                ((and
                (file-directory-p f)
                (file-readable-p f)
                (not (string-equal ".." (substring f -2)))
                (not (string-equal "." (substring f -1)))
                (> maxdepth 0))
            ;; recurse only if necessary
            (setq files-list (append files-list (directory-files-recursive f match (- maxdepth -1))))
            (setq files-list (cons f files-list)))
                (t)))
            (setq current-directory-list (cdr current-directory-list)))
                files-list))

    (defun tangle-all ()
        (interactive)
    "Tangle all the Org-mode files in the directory of the file of the current buffer
    recursively in child folders. Returns the list of tangled files"
    (mapcar (lambda (f)
        (when (not (file-directory-p f))
            (org-babel-tangle-file f)))
        (directory-files-recursive (file-name-directory (buffer-file-name)) "\\.org$" 20)))
```

To be able to evaluate shell code in org-mode I need to load the shell scripts language when org-mode gets started. Otherwise emacs returns this error:

```
no org-babel-execute function for sh!
```

The following code loads the language automatically. https://emacs.stackexchange.com/questions/20577/org-babel-load-all-languages-on-demand

``` emacs-lisp
    (defadvice org-babel-execute-src-block (around load-language nil activate)
        "Load language if needed"
        (let ((language (org-element-property :language (org-element-at-point))))
            (unless (cdr (assoc (intern language) org-babel-load-languages))
                (add-to-list 'org-babel-load-languages (cons (intern language) t))
                (org-babel-do-load-languages 'org-babel-load-languages org-babel-load-languages))
                ad-do-it))

    (setq org-plantuml-jar-path "~/.emacs.d/plantuml.jar")
;; end of the org-mode use-package block
```

after a graphiz image gets compiled the org-mode view needs to get updated. This function adds a hook to refresh the buffer.

``` emacs-lisp
(defun my/fix-inline-images ()
(when org-inline-image-overlays
    (org-redisplay-inline-images)))

(add-hook 'org-babel-after-execute-hook 'my/fix-inline-images)
```

This function allows to quickly insert a picture into the current org file. The file will get renamed and moved to the file location. I found this code on this blog post: http://pragmaticemacs.com/emacs/a-workflow-to-quickly-add-photos-to-org-mode-notes/

``` emacs-lisp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; add image from conference phone upload                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; use case is taking a photo of a slide in a conference and uploading
;; it to google drive or dropbox or whatever to get it on your
;; computer. You then want to embed it in an org-mode document by
;; moving it to the same folder and renaming according to the current
;; section of the org file, avoiding name clashes

;; required libraries
(require 'dash)
(require 'swiper)
(require 's)

(global-set-key (kbd "C-c i") 'org-insert-image)
;; start directory
(when (is-linux-p)
    (defvar bjm/conference-image-dir (expand-file-name "/home/andreas/Downloads/")))
(when (is-windows-p)
    (defvar bjm/conference-image-dir (expand-file-name "c:/Users/andreas/Downloads/")))

(defun org-insert-image ()
    "Insert image from conference directory, rename and add link in current file.
    The file is taken from a start directory set by
    `bjm/conference-image-dir' and moved to the current directory, renamed
    and embedded at the point as an org-mode link. The user is presented
    with a list of files in the start directory, from which to select the
    file to move, sorted by most recent first."
    (interactive)
        (let (file-list target-dir file-list-sorted start-file start-file-full file-ext end-file end-file-base end-file-full file-number)
            ;; clean directories from list but keep times
            (setq file-list
                (-remove (lambda (x) (nth 1 x))
                    (directory-files-and-attributes bjm/conference-image-dir)))

            ;; get target directory
            (setq target-dir (file-name-directory (buffer-file-name)))

            ;; sort list by most recent
            ;; http://stackoverflow.com/questions/26514437/emacs-sort-list-of-directories-files-by-modification-date
            (setq file-list-sorted
                (mapcar #'car
                    (sort file-list
                        #'(lambda (x y) (time-less-p (nth 6 y) (nth 6 x))))))

            ;; use ivy to select start-file
            (setq start-file (ivy-read
                (concat "Move selected file to " target-dir ":")
                    file-list-sorted
                    :re-builder #'ivy--regex
                    :sort nil
                    :initial-input nil))

            ;; add full path to start file and end-file
            (setq start-file-full
                (expand-file-name start-file bjm/conference-image-dir))
            ;; generate target file name from current org section
            (setq file-ext
                (file-name-extension start-file t))

            ;; my phone app doesn't add an extension to the image so I do it
            ;; here. If you want to keep the existing extension then use the
            ;; line above
            ;;(setq file-ext ".jpg")
            ;; get section heading and clean it up
            (setq end-file-base (s-downcase (s-dashed-words (nth 4 (org-heading-components)))))
            ;; shorten to first 40 chars to avoid long file names
            (setq end-file-base (s-left 40 end-file-base))
            ;; number to append to ensure unique name
            (setq file-number 1)
            (setq end-file (concat
                end-file-base
                    (format "-%s" file-number)
                    file-ext))

            ;; increment number at end of name if file exists
            (while (file-exists-p end-file)
                    ;; increment
                    (setq file-number (+ file-number 1))
                    (setq end-file (concat
                        end-file-base
                        (format "-%s" file-number)
                        file-ext)))

    ;; final file name including path
    (setq end-file-full
        (expand-file-name end-file target-dir))
    ;; rename file
    (rename-file start-file-full end-file-full)
    (message "moved %s to %s" start-file-full end-file)
    ;; insert link
    (insert (org-make-link-string (format "file:%s" end-file)))
    ;; display image
    (org-display-inline-images t t)))
```

This settings tells Emacs where to look for agenda files. All the files in this directories and its sub directories count as agenda files.

``` emacs-lisp
(load-library "find-lisp")
(when (is-linux-p)
    (setq org-agenda-files
    (find-lisp-find-files "~/git_repos/notes/personal/agenda" "\.org$")))
(when (is-windows-p)
    (setq org-agenda-files
    (find-lisp-find-files "~/git_repos/notes/work/agenda" "\.org$")))
```

It can happen that statistic cookies in an org-mode buffer don’t get updated. This function updates all the statistic cookies before the buffer gets saved.

``` emacs-lisp
(defun org-update-cookies-after-save()
(interactive)
(let ((current-prefix-arg '(4)))
(org-update-statistics-cookies "ALL")))

(add-hook 'org-mode-hook
    (lambda ()
        (add-hook 'before-save-hook 'org-update-cookies-after-save nil 'make-it-local)))
```

This function sets a parent to the DONE state when the last of it’s children gets changed to DONE. This is especially usefull in combination with org-edna.

  • https://orgmode.org/manual/Breaking-down-tasks.html

    (defun org-summary-todo (n-done n-not-done)
    "Switch entry to DONE when all subentries are done, to TODO otherwise."
    (let (org-log-done org-log-states)   ; turn off logging
        (org-todo (if (= n-not-done 0) "DONE" "TODO"))))
    
    (add-hook 'org-after-todo-statistics-hook 'org-summary-todo))

I defined C-c p to quickly jump to my personal org file.

``` emacs-lisp
;; keymap for my personal.org file
(when (is-linux-p)
    (global-set-key (kbd "C-c p")
        (lambda () (interactive) (find-file "~/git_repos/notes/personal/agenda/personal.org"))))
(when (is-windows-p)
    (global-set-key (kbd "C-c p")
        (lambda () (interactive) (find-file "~/git_repos/notes/work/agenda/work.org"))))
```

In org-mode most keywords were written all capitalized. IMO that’s not really nice looking nor conveniet to type. However org-mode actually understands lowercase keywords as well. Thanks to these post: https://scripter.co/converting-org-keywords-to-lower-case/ I have now a function with which I can convert all the keywords in the current buffer at once.

``` emacs-lisp
(defun lower-case-org-keywords ()
    "Lower case Org keywords and block identifiers.

Example: \"#+TITLE\" -> \"#+title\"
         \"#+BEGIN_EXAMPLE\" -> \"#+begin_example\"

Inspiration:
https://code.orgmode.org/bzg/org-mode/commit/13424336a6f30c50952d291e7a82906c1210daf0."
    (interactive)
    (save-excursion
        (goto-char (point-min))
        (let ((case-fold-search nil)
            (count 0))
        ;; Match examples: "#+foo bar", "#+foo:", "=#+foo=", "~#+foo~",
        ;;                 "‘#+foo’", "“#+foo”", ",#+foo bar",
        ;;                 "#+FOO_bar<eol>", "#+FOO<eol>".
            (while (re-search-forward "\\(?1:#\\+[A-Z_]+\\(?:_[[:alpha:]]+\\)*\\)\\(?:[ :=~’”]\\|$\\)" nil :noerror)
                (setq count (1+ count))
                (replace-match (downcase (match-string-no-properties 1)) :fixedcase nil nil 1))
            (message "Lower-cased %d matches" count))))
```

``` emacs-lisp
;; My details

(setq user-full-name "Andreas Zweili")
(setq user-mail-address "andreas@2li.ch")
```

Sometimes when emacs opens a buffer it’s in a wrong orientation. With the keybinding C-x 4 I can toggle between horizontal and vertical split.

``` emacs-lisp
;; a function to toggle the splits
(defun toggle-window-split ()
    (interactive)
    (if (= (count-windows) 2)
        (let* ((this-win-buffer (window-buffer))
            (next-win-buffer (window-buffer (next-window)))
            (this-win-edges (window-edges (selected-window)))
            (next-win-edges (window-edges (next-window)))
            (this-win-2nd (not (and (<= (car this-win-edges)
                (car next-win-edges))
                (<= (cadr this-win-edges)
                    (cadr next-win-edges)))))
            (splitter
                (if (= (car this-win-edges)
                    (car (window-edges (next-window))))
                    'split-window-horizontally
                    'split-window-vertically)))
            (delete-other-windows)
            (let ((first-win (selected-window)))
            (funcall splitter)
            (if this-win-2nd (other-window 1))
            (set-window-buffer (selected-window) this-win-buffer)
            (set-window-buffer (next-window) next-win-buffer)
            (select-window first-win)
            (if this-win-2nd (other-window 1))))))

(define-key ctl-x-map "4" 'toggle-window-split)
```

Usually when I want to split the window I want the cursor to be place into the new window because I need to manipulate that window. Emacs doesn’t do this by default but we can add that functionality.

``` emacs-lisp
(defun nebucatnetzer:split-window-below-and-move-cursor ()
    (interactive)
    (split-window-below)
    (other-window 1))

(defun nebucatnetzer:split-window-right-and-move-cursor ()
    (interactive)
    (split-window-right)
    (other-window 1))

(global-set-key (kbd "C-x 2") 'nebucatnetzer:split-window-below-and-move-cursor)
(global-set-key (kbd "C-x 3") 'nebucatnetzer:split-window-right-and-move-cursor)
```

``` emacs-lisp
;; Spaces instead of TABs
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq indent-line-function 'insert-tab)
```

ibuffer is a much better way to give an overview than the standard buffer view in emacs. Interestingly it’s already built in but not configured. You can open it with C-x C-b.

``` emacs-lisp
;; keymap for ibuffer
(global-set-key (kbd "C-x C-b") 'ibuffer)
```

Don’t ask which buffer to kill, kill the current buffer with C-x C-k.

``` emacs-lisp
;; kill THIS buffer
(global-set-key (kbd "C-x C-k") 'kill-this-buffer)
```

Sometimes it’s handy to be able the complete buffer into the clipboard. E.g. when you write an email in Emacs an want to copy it into Outlook. This function is bound to C-S-c.

``` emacs-lisp
;; copy the complete buffer to the clipboard
(defun copy-all ()
    "Copy entire buffer to clipboard"
    (interactive)
    (clipboard-kill-ring-save (point-min) (point-max)))

(global-set-key (kbd "C-S-c") 'copy-all)
```

This keybindings allow to create and navigate frames. Frames are in a desktop environment usually called windows. However because Emacs is so old it has it’s own terminology.

  • New frame: C-x N

  • Switch to the other frame: C-x O

  • Kill frame: C-x K

    ;; keybinding for new frame
    (global-set-key (kbd "C-x N") 'make-frame)
    
    ;; switch to frame
    (global-set-key (kbd "C-x O") 'other-frame)
    
    ;; kill frame
    (global-set-key (kbd "C-x K") 'delete-frame)

This option enables hippie expand which allows for path complition and more. You can execute it with M-Space.

``` emacs-lisp
;; enable hippie expand on M-Space
(global-set-key "\M- " 'hippie-expand)
```

``` emacs-lisp
(defun switch-to-minibuffer () "Switch to minibuffer window."
    (interactive) (if (active-minibuffer-window)
        (select-window
            (active-minibuffer-window)) (error "Minibuffer is not active")))

(bind-key "M-m" 'switch-to-minibuffer)
```

Dired opens a lot of buffers when you use it. This functions helps to clean up afterwards.

``` emacs-lisp
;;a function to kill all dired buffers
(defun kill-dired-buffers ()
    (interactive)
    (mapc (lambda (buffer)
        (when (eq 'dired-mode (buffer-local-value 'major-mode buffer))
            (kill-buffer buffer)))
        (buffer-list)))
```

``` emacs-lisp
;; file encodings
(prefer-coding-system 'utf-8-unix)
```

With this configuration Emacs saves the backup files into the temp directory. Otherwise they get created in the current directory which IMO is unneeded clutter.

``` emacs-lisp
(setq backup-directory-alist
    `((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
    `((".*" ,temporary-file-directory t)))
```

This function groups buffers depending on their mode which gives an even better overview. In addition it hides temporary buffers from the ibuffer view.

``` emacs-lisp
;; hide temporary buffers
(add-hook 'ibuffer-mode-hook
    (lambda ()
        (ibuffer-filter-by-name "^[^*]")))

(setq ibuffer-saved-filter-groups
    (quote (("default"
        ("Org" ;; all org-related buffers
            (mode . org-mode))
        ("Programming" ;; prog stuff not already in MyProjectX
            (or
                (mode . python-mode)
                (mode . web-mode)
                (mode . php-mode)
                (mode . csharp-mode)
                (mode . javascript-mode)
                (mode . sql-mode)
                (mode . powershell-mode)
                (mode . emacs-lisp-mode)))
                ;; etc
        ("Dired"
            (mode . dired-mode))))))

(add-hook 'ibuffer-mode-hook
    (lambda ()
        (ibuffer-switch-to-saved-filter-groups "default")))
```

When I create a new buffer it should start in text-mode. By default Emacs uses elisp-mode.

``` emacs-lisp
;; initial buffers should use text-mode
(setq-default major-mode 'text-mode)
```

In Switzerland weeks start on Monday not Sunday.

``` emacs-lisp
; Calender should start on Monday
(setq calendar-week-start-day 1)
```

Since I write both in English and in German I need two dictionaries. This code configures Emacs to use the Linux dictionaries en_US and de_CH. If you want to switch languages you need the function: ispell-change-dictionary

``` emacs-lisp
;; ispell settings
(setenv "DICTIONARY" "en_US")
(setq ispell-program-name "hunspell")
(setq ispell-local-dictionary "en_US")
(setq ispell-local-dictionary-alist
      '(("en_US" "[[:alpha:]]" "[^[:alpha:]]" "[']" nil ("-d" "en_US") nil utf-8)
        ("de_CH" "[[:alpha:]]" "[^[:alpha:]]" "[']" nil ("-d" "de_CH") nil utf-8)))
```

``` emacs-lisp
;; insert only one space after a period
(setq sentence-end-double-space nil)
```

This setting shows matching parantheses and hightlights the ones which don’t have a counter part.

``` emacs-lisp
; Matches parentheses and such in every mode
(show-paren-mode 1)
```

When you insert a parenthesis it’s counter pair gets inserted automatically.

``` emacs-lisp
;; pair parentheses
(electric-pair-mode 1)
```

This enables auto-fill which is basically line wrapping.

``` emacs-lisp
(add-hook 'text-mode-hook 'turn-on-auto-fill)
```

This settings surpresses the following warning which shows up when Emacs starts:

```
ad-handle-definition: `tramp-read-passwd' got redefined
```

``` emacs-lisp
(setq ad-redefinition-action 'accept)
```

``` emacs-lisp
(defun org-babel-tangle (&optional arg target-file lang)
  "Write code blocks to source-specific files.
Extract the bodies of all source code blocks from the current
file into their own source-specific files.
With one universal prefix argument, only tangle the block at point.
When two universal prefix arguments, only tangle blocks for the
tangle file of the block at point.
Optional argument TARGET-FILE can be used to specify a default
export file for all source blocks.  Optional argument LANG can be
used to limit the exported source code blocks by language."
  (interactive "P")
  (run-hooks 'org-babel-pre-tangle-hook)
  ;; Possibly Restrict the buffer to the current code block
  (save-restriction
    (save-excursion
      (when (equal arg '(4))
    (let ((head (org-babel-where-is-src-block-head)))
      (if head
          (goto-char head)
        (user-error "Point is not in a source code block"))))
      (let ((block-counter 0)
        (org-babel-default-header-args
         (if target-file
         (org-babel-merge-params org-babel-default-header-args
                     (list (cons :tangle target-file)))
           org-babel-default-header-args))
        (tangle-file
         (when (equal arg '(16))
           (or (cdr (assq :tangle (nth 2 (org-babel-get-src-block-info 'light))))
           (user-error "Point is not in a source code block"))))
        path-collector)
    (mapc ;; map over all languages
     (lambda (by-lang)
       (let* ((lang (car by-lang))
          (specs (cdr by-lang))
          (ext (or (cdr (assoc lang org-babel-tangle-lang-exts)) lang))
          (lang-f (intern
               (concat
                (or (and (cdr (assoc lang org-src-lang-modes))
                     (symbol-name
                      (cdr (assoc lang org-src-lang-modes))))
                lang)
                "-mode")))
          she-banged)
         (mapc
          (lambda (spec)
        (let ((get-spec (lambda (name) (cdr (assoc name (nth 4 spec))))))
          (let* ((tangle (funcall get-spec :tangle))
             (she-bang (let ((sheb (funcall get-spec :shebang)))
                                     (when (> (length sheb) 0) sheb)))
             (tangle-mode (funcall get-spec :tangle-mode))
             (base-name (cond
                     ((string= "yes" tangle)
                      (file-name-sans-extension
                       (nth 1 spec)))
                     ((string= "no" tangle) nil)
                     ((> (length tangle) 0) tangle)))
             (file-name (when base-name
                      ;; decide if we want to add ext to base-name
                      (if (and ext (string= "yes" tangle))
                      (concat base-name "." ext) base-name))))
            (when file-name
              ;; Possibly create the parent directories for file.
              (let ((m (funcall get-spec :mkdirp))
                (fnd (file-name-directory file-name)))
            (and m fnd (not (string= m "no"))
                 (make-directory fnd 'parents)))
              ;; delete any old versions of file
              (and (file-exists-p file-name)
               (not (member file-name (mapcar #'car path-collector)))
               (delete-file file-name))
              ;; drop source-block to file
              (with-temp-buffer
            (when (fboundp lang-f) (ignore-errors (funcall lang-f)))
            (when (and she-bang (not (member file-name she-banged)))
              (insert (concat she-bang "\n"))
              (setq she-banged (cons file-name she-banged)))
            (org-babel-spec-to-string spec)
            ;; We avoid append-to-file as it does not work with tramp.
            (let ((content (buffer-string)))
              (with-temp-buffer
                (when (file-exists-p file-name)
                  (insert-file-contents file-name))
                (goto-char (point-max))
        ;; Handle :padlines unless first line in file
        (let ((padlines (format "%s" (cdr (assq :padline (nth 4 spec))))))
        (cond
         ((and (string= "nil" padlines) (not (= (point) (point-min))))
          (insert "\n"))
         ((string= "no" padlines)
          nil)
         ((numberp (string-to-int padlines))
          (dotimes (i (string-to-int padlines)) (insert "\n")))
         (t
          (insert "\n"))))
                (insert content)
                (write-region nil nil file-name))))
              ;; if files contain she-bangs, then make the executable
              (when she-bang
            (unless tangle-mode (setq tangle-mode #o755)))
              ;; update counter
              (setq block-counter (+ 1 block-counter))
              (unless (assoc file-name path-collector)
            (push (cons file-name tangle-mode) path-collector))))))
          specs)))
     (if (equal arg '(4))
         (org-babel-tangle-single-block 1 t)
       (org-babel-tangle-collect-blocks lang tangle-file)))
    (message "Tangled %d code block%s from %s" block-counter
         (if (= block-counter 1) "" "s")
         (file-name-nondirectory
          (buffer-file-name
           (or (buffer-base-buffer) (current-buffer)))))
    ;; run `org-babel-post-tangle-hook' in all tangled files
    (when org-babel-post-tangle-hook
      (mapc
       (lambda (file)
         (org-babel-with-temp-filebuffer file
           (run-hooks 'org-babel-post-tangle-hook)))
       (mapcar #'car path-collector)))
    ;; set permissions on tangled files
    (mapc (lambda (pair)
        (when (cdr pair) (set-file-modes (car pair) (cdr pair))))
          path-collector)
    (mapcar #'car path-collector)))))
```

``` emacs-lisp
(add-hook 'before-save-hook 'whitespace-cleanup)
```

``` emacs-lisp
(global-auto-revert-mode t)
```

``` emacs-lisp
(setq-default fill-column 80)
```

``` emacs-lisp
(setq column-number-mode 1)
```

Make the tab completion behave like in Bash.

``` emacs-lisp
(add-hook 'eshell-mode-hook
    (lambda ()
        (setq pcomplete-cycle-completions nil)))
```

To improve the perfomance of tramp I use SSH instead of scp.

``` emacs-lisp
(setq tramp-default-method "ssh")
```

This is a nice function to work with LaTeX tables. I’ve got it from here: https://thenybble.de/projects/inhibit-auto-fill.html

``` emacs-lisp
(defun LaTeX-collapse-table ()
    (interactive)
    (save-excursion
        (LaTeX-mark-environment)
        (while (re-search-forward "[[:space:]]+\\(&\\|\\\\\\\\\\)" (region-end) t)
            (replace-match " \\1"))))

(defun LaTeX-align-environment (arg)
    (interactive "P")
    (if arg
        (LaTeX-collapse-table)
        (save-excursion
            (LaTeX-mark-environment)
            (align (region-beginning) (region-end)))))
```

“Auto-fill is nice. Filling to 80 columns (as god intended) makes code or any other text easier to read.

However, it’s not so nice for large latex tables that you want to align properly (perhaps using align). This is why I hacked together some elisp to inhibit auto-fill when the cursor is in a table (or any other environment that doesn’t need to be auto-filled, according to your whims.“

Taken from here: https://thenybble.de/projects/inhibit-auto-fill.html

``` emacs-lisp
(defcustom LaTeX-inhibited-auto-fill-environments
    '("tabular" "tikzpicture") "For which LaTeX environments not to run auto-fill.")

(defun LaTeX-limited-auto-fill ()
    (let ((environment (LaTeX-current-environment)))
        (when (not (member environment LaTeX-inhibited-auto-fill-environments))
            (do-auto-fill))))
```

``` emacs-lisp
(global-set-key (kbd "C-c f") 'LaTeX-align-environment)
(setq auto-fill-function 'LaTeX-limited-auto-fill)
```

Previous Post