Skip to content

Latest commit

 

History

History
1231 lines (1085 loc) · 39.9 KB

configuration.org

File metadata and controls

1231 lines (1085 loc) · 39.9 KB

Eeemil’s Emacs Configuration

Emacs Configuration

By Emil Marklund, Initialized oct 2013. Converted into org-file May 2017

Remember: writing “<s” followed by <TAB> is using Org-mode easy template! C-x C-, is using org-insert-structure-template which was introduced in Org 9.2 as a new template expansion mechanism meant to provide the same functionality as easy templates. (Easy templates can still be used if org-tempo is loaded)

Work needed

Document everything

Clean old/unused packages

Investigate git tool

General settings

Initialization process.

Install use package

(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))

(require 'use-package)
(setq use-package-always-ensure t)

Delight needs to be loaded early: it configures mode line names of other modes

(use-package delight)

Investigate

I’m not quite sure what this is anymore, and why I cannot use-package it…

                                        ;iso-transl: Brings back dead keys to swedish keyboard layout (ex. tilde)
(require 'iso-transl)

A string manipulation library that a lot of other packages depend on

(use-package s)

Global editor configuration

Electric pair mode automatically pairs ( parentheses ).

(electric-pair-mode 1)

File settings

Backups should be saved in “~/.emacs.backup/”

;; Keep file backups in ~/.emacs.backup
(setq
 backup-by-copying t      ; don't clobber symlinks
 backup-directory-alist
 '(("." . "~/.emacs.backup"))    ; don't litter my files system
 delete-old-versions t
 kept-new-versions 6
 kept-old-versions 2
 version-control t)

teststr

Auto-save files in “~/.emacs.autosave/”

(unless (file-exists-p "~/.emacs.autosave/")
  (make-directory "~/.emacs.autosave/"))
(setq auto-save-file-name-transforms
      `((".*" "~/.emacs.autosave/" t)))

Define the local file variables that should be considered safe.

(setq safe-local-variable-values
      (quote ((eval add-hook 'before-save-hook 'org-table-recalculate-buffer-tables)
              (company-bibtex-bibliography . "base.bib")
              (company-bibtex-bibliography . "main.bib")
              ;;Flycheck should not complain about gnu99
              (flycheck-gcc-language-standard . gnu99)
              )))

Lock files are great and I want to use them when remote editing, but sadly they are a pain to use with certain build tools generating a new build on every new file created.

(setq create-lockfiles nil)

VC should automagically follow symlinks.

(setq vc-follow-symlinks t)

Set exec-patch from shell .zshenv

(use-package exec-path-from-shell
  :ensure t
  :config
  (setq exec-path-from-shell-arguments nil)
  (exec-path-from-shell-initialize))

Emacs cosmetics

Uniquifies/resolves buffer name collisions

;; Buffer colliding file names resolvement
(require 'uniquify)
(setq uniquify-buffer-name-style 'post-forward-angle-brackets)

Left fringe for marking errors etc

(fringe-mode '(8 . 0))

I don’t need a tool bar…

(tool-bar-mode 0)

It’s time to get rid of the menu bar

(menu-bar-mode 0)

I don’t like the scroll bar

(scroll-bar-mode 0)

Delight: reconfigure how major/minor modes are displayed in mode-line

(require 'delight)
(delight 'shell-script-mode "Sh" :major)

Frame title

More information in frame title

(setq frame-title-format
      '(:eval
        (format "[%s] %s@%s: %s %s"
                major-mode
                (or (file-remote-p default-directory 'user)
                    user-real-login-name)
                (or (file-remote-p default-directory 'host)
                    system-name)
                (buffer-name)
                (cond
                 (buffer-file-truename
                  (concat "(" buffer-file-truename ")"))
                 (dired-directory
                  (concat "{" dired-directory "}"))
                 (t
                  "[no file]")))))

Customize configuration

Setting custom-file to custom.el will make Custom system save variables to that file instead of clobbering my .emacs

Bear in mind that the file is never loaded, so if I need to update variables I will have to manually extract the new customized variables from custom.el into configuration.org. Read this for more information

(setq custom-file "~/.emacs.d/custom.el")

Variables

Shell-mode text colors

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(ansi-color-names-vector ["#212526" "#ff4b4b" "#b4fa70" "#fce94f" "#729fcf" "#ad7fa8" "#8cc4ff" "#eeeeec"])

Color theme: wheatgrass, some kind of dark theme.

'(custom-enabled-themes (quote (wheatgrass)))
'(keyboard-coding-system (quote utf-8-unix))
'(powerline-default-separator (quote contour))

Closing parenthesis (For making it easier to add more customization in the future: just add another lisp block)

)

Faces

(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.

Default editorlook

'(default ((t (:family "DejaVu Sans Mono"
                       :foundry "unknown"
                       :slant normal
                       :weight normal
                       :height 95
                       :width normal
                       :background "#211E1E"))))

Beautify org-mode settings

                                        ;Default settings makes BEGIN_SRC lines be highlighted, (and the code block not
                                        ;highlighted). I want the opposite...
'(org-block ((t (:inherit shadow :background "#211E1E" :foreground "wheat" :box nil))))
'(org-block-background ((t (:background "#242424"))))
'(org-block-begin-line ((t (:foreground unspecified :inherit org-meta-line :background "#211E1E"))) t)
'(org-block-end-line ((t (:foreground unspecified :inherit org-meta-line :background "#211E1E"))) t)

                                        ;org-beautify wants to put boxes around headings (to make more space around
                                        ;them?). Make the boxes have the correct background.
'(org-document-title ((t (:inherit org-level-1 :box (:line-width 5 :color "#211E1E") :underline nil :height 2.0))))
'(org-level-1 ((t (:inherit default :foreground "wheat" :box (:line-width 5 :color "#211E1E") :slant normal :weight normal :height 1.5 :width normal :foundry "microsoft" :family "Verdana"))))
'(org-level-2 ((t (:inherit default :foreground "wheat" :box (:line-width 5 :color "#211E1E") :slant normal :weight normal :height 1.25 :width normal :foundry "microsoft" :family "Verdana"))))
'(org-level-3 ((t (:inherit default :foreground "wheat" :box (:line-width 5 :color "#211E1E")))))

Mode line customization

                                        ;Mode line in a red (active) and blue (inactive) theme, with gold/grey fonts
'(mode-line ((t (:background "#150015" :foreground "#800000" :box (:line-width -1 :color "#100010")))))
'(powerline-active1 ((t :background "#990000" :foreground "#500000" )))
'(powerline-active2 ((t :background "#600000" :foreground "#300000")))
'(mode-line-inactive ((t (:background "#150015" :foreground "#404080" :box (:line-width -1 :color "#100010") :slant italic))))
'(powerline-inactive1 ((t :background "#000025" :foreground "#4040AA")))
'(powerline-inactive2 ((t :background "#050545" :foreground "#303080")))

Terminal cosmetics

                                        ;Ansi-term blue color is hard to read some times
'(term-color-blue ((t (:foreground "DodgerBlue3" :background "DodgerBlue3"))) t)

Ace-window character

'(aw-leading-char-face ((t (:foreground "red" :height 4.0))))

Tide

'(tide-hl-identifier-face ((t (:inherit highlight :background "#004000"))))

Custom-set… end

Closing parenthesis (For making it easier to add more customization in the future: just add another lisp block)

)

mode line configuration

(use-package spaceline)
(spaceline-emacs-theme)
(spaceline-helm-mode)
(use-package anzu
  :config
  (setq anzu-cons-mode-line-p nil)
  )

Mode line config

(spaceline-define-segment spaceline-time
  "Displays time"
  (format-time-string "%T")
  )

(defface spaceline-buffer-name
  '((t :foreground "gold3"
       :background "black"
       :weight bold
       ))
  "Face for buffer names."
  :group 'spaceline )

(spaceline-compile
                                        ; left side
  '(((line-column buffer-modified buffer-size remote-host
                  )
     :fallback ":)"
     :face highlight-face
     :priority 100
     )
    (anzu :priority 95)
    auto-compile
    ((buffer-id)
     :priority 99
     :face 'spaceline-buffer-name)
    (major-mode :priority 79)
    (process :when active)
    ((flycheck-error flycheck-warning flycheck-info)
     :when active
     :priority 89)
    (minor-modes :when active
                 :priority 9)
    (mu4e-alert-segment :when active)
    (erc-track :when active)
    (version-control :when active
                     :priority 78)
    (org-pomodoro :when active)
    (org-clock :when active)
    nyan-cat)
                                        ; right side
  '(which-function
    (python-pyvenv :fallback python-pyenv)
    (purpose :priority 94)
    (battery :when active)
    (selection-info :priority 95)
    input-method
    ((buffer-encoding-abbrev
      :priority 96)
     (hud :priority 95)))
  )

Calendar settings

;; Week begin on Monday
(setq calendar-week-start-day 1)
(setq org-agenda-start-on-weekday 1)
;; Use "european" dates (day/month)
(setq calendar-date-style 'european)

;; Date format
(setq calendar-date-display-form
      '((if dayname
            (concat dayname ", "))
        day " " monthname " " year))

;; 24-hour clock without timezone
(setq calendar-time-display-form
      '(24-hours ":" minutes))

Helm

Much of the configuration is inspired by this config

(use-package helm
  :bind
  (("M-x" . helm-M-x)
   ("M-y" . helm-show-kill-ring)
   ("C-x b" . helm-mini)
   ("C-x C-f" . helm-find-files)
   )
  :delight " He"
  :config
  ;; start helm-mode
  (helm-mode 1)
  ;; Find-file should auto-complete on tab
  (define-key helm-find-files-map "\t" 'helm-execute-persistent-action)
  ;; Fuzzy matching
  (setq helm-buffers-fuzzy-matching t
        helm-M-x-fuzzy-match t
        helm-mode-fuzzy-match t
        )
  ;; Disable find-file to try open whatever I'm pointing at
  ;; (it's not what I want to do in 99 of 100 times)
  (setq helm-find-files-ignore-thing-at-point t)

  )
(use-package helm-swoop
  :bind
  (("M-o" . helm-swoop)
   ("M-O" . helm-swoop-back-to-last-point)
   ("C-c M-o" . helm-multi-swoop)
   )
  :config ;By default swoop uses whatever is at point when swooping...
  (setq helm-swoop-pre-input-function
        (lambda() ""))
  )

Spellcheck

(setq ispell-dictionary "en")

Tree-sitter

Super fast parsing library https://tree-sitter.github.io/tree-sitter/

(use-package tree-sitter
  :ensure t
  :config
  ;; activate tree-sitter on any buffer containing code for which it has a parser available
  (global-tree-sitter-mode)
  ;; you can easily see the difference tree-sitter-hl-mode makes for python, ts or tsx
  ;; by switching on and off
  (add-hook 'tree-sitter-after-on-hook #'tree-sitter-hl-mode))

(use-package tree-sitter-langs
  :ensure t
  :after tree-sitter)

URLs

(url-handler-mode 1)

Remote editing (Tramp)

Set TERM variable so that terminals can handle tramp sessions

(setq tramp-terminal-type "tramp")

Editor configuration

Use editorconfig

(use-package editorconfig
  :ensure t
  :delight " EC"
  :config
  (editorconfig-mode 1))
(set-language-environment "UTF-8")

80 char width

(setq-default fill-column 80)

Indent with spaces

(setq-default indent-tabs-mode nil)

Show matching parenthesis when hovering over paren

(show-paren-mode 1)

I want manual pages to appear in current window

(setq Man-notify-method (quote pushy)) ;; Man pages appear in current window

Standard indent length: 4 spaces

(setq-default c-basic-offset 4) ; Standard indent: 4 spaces

Delete selection: almost never expects anything else than what is provided by delete-selection-mode.

(delete-selection-mode 1)
(use-package highlight-indent-guides
  :diminish
  :hook ((prog-mode . highlight-indent-guides-mode)
         ;; WORKAROUND: Fix the issue of not displaying plots
         (ein:notebook-multilang-mode . (lambda () (highlight-indent-guides-mode -1)))
         (yaml-mode . highlight-indent-guides-mode)
         )
  :config
  (setq highlight-indent-guides-method 'character)
  (setq highlight-indent-guides-responsive 'top)
  )

Auto completion

I may want to change auto complete system…

Switched to Company <2018-08-13 mån 21:07>
(use-package company
  :config
  (global-company-mode)
  (setq company-tooltip-limit 10)
  (setq company-dabbrev-downcase 0)
  (setq company-idle-delay 0.33)
  (setq company-echo-delay 0)
  (setq company-minimum-prefix-length 2)
  (setq company-selection-wrap-around t)
  (setq company-tooltip-align-annotations t)
  (setq company-transformers '(company-sort-by-backend-importance)) ; weight by frequency
  ;; (setq company-backends '((company-capf :with company-yasnippet)))
  ;; Add yasnippet support for all company backends
  ;; https://github.com/syl20bnr/spacemacs/pull/179
  ;;(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))
  (define-key company-active-map (kbd "<return>") nil)
  (define-key company-active-map (kbd "RET") nil)
  (define-key company-active-map (kbd "<tab>") #'company-complete-selection)
  (define-key company-active-map (kbd "C-j") #'company-complete-selection)
  :custom-face
  (company-preview ((t (:background "gray25"))))
  (company-scrollbar-bg ((t (:background "chocolate4"))))
  (company-scrollbar-fg ((t (:background "dark red"))))
  (company-tooltip ((t (:background "#000075" :foreground "dark goldenrod"))))
  (company-tooltip-selection ((t (:background "#000040"))))
  :delight " c-any"
  )
(use-package company-box
    :ensure t
    :hook (company-mode . company-box-mode)
    )
(use-package company-quickhelp
  :config
  (company-quickhelp-mode)
  :custom
  (company-quickhelp-color-background "#000075")
  (company-quickhelp-color-foreground "goldenrod")
  )

Cursor movement, navigation, marking

                                        ; Marks a word.
(global-set-key (kbd "M-\"") 'mark-word)

Window control, navigation

I might want to expand on ace-functionalities…

(use-package ace-window)
;; ace-window: give useful numbers to jump around to the correct window fast!
(global-set-key (kbd "C-x o") 'ace-window)
;; <prior>/<next> = up and down on my Microsoft Ergonomic 4000
(global-set-key (kbd "<prior>") 'scroll-down-line)
(global-set-key (kbd "<next>") 'scroll-up-line)


;; Make Emacs full screen
(global-set-key (kbd "<f11>") 'switch-full-screen)

TAGS: Use GNU Global (gtags)

(use-package ggtags)

YASnippet: I should learn it

(use-package yasnippet
  :config
  (yas-global-mode)
  :bind
  ("C-c y" . yas-insert-snippet)
  )
(use-package yasnippet-snippets)

Various special packages

Org mode configuration

Should perhaps exist in its own file. :)

Prettify org, see customization

Various org extras

(use-package org)

Force syntax highlighting within #+BEGIN_SRC blocks

                                        ; Syntax higlighting for code within org mode
(setq org-src-fontify-natively t)
(add-to-list 'org-latex-packages-alist '("" "minted"))
(setq org-latex-listings 'minted) 
(setq org-latex-pdf-process
      '("pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
        "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
        "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"))

(setq org-src-fontify-natively t)

Down arrow for indicating collapsed blocks instead of “…”

(setq org-ellipsis "")

Less stars

(setq org-hide-leading-stars t)

Show images by default

(setq org-startup-with-inline-images t)

By default, give todo-items a closing timestamp.

(setq org-log-done 'time)

Pretty bullets

(use-package org-bullets
  :ensure t
  :commands (org-bullets-mode)
  :init (add-hook 'org-mode-hook (lambda () (org-bullets-mode 1))))

Global keybindings

(global-set-key (kbd "C-c l") 'org-store-link)
(global-set-key (kbd "C-c a") 'org-agenda)
(global-set-key (kbd "C-c c") 'org-capture)

Where are org files located?

(setq org-agenda-files (list "~/org/"
                             "~/org/work"))
(setq org-archive-location '"~/org/archive/%s::")

Capture templates

(setq org-capture-templates
      '(("t" "Todo" entry (file+headline "~/org/tasks.org" "Tasks")
         "* TODO %?\n  %i\n (Task filed from %a)")
        ("j" "Journal" entry (file+datetree "~/org/notes.org")
         "* %?\nEntered on %U\n  %i\n  %a")))

Readable latex.

(setq org-format-latex-options (plist-put org-format-latex-options :scale 2.0))

Org TODO keywords faces:

(setq org-todo-keyword-faces
      '(("TODO" . "red")
        ("STARTED" . "yellow")
        ("WAIT" . "orange")
        ("WAITING" . "orange")
        ("CANCELED" . (:foreground "blue" :weight bold))
        ("DONE" . "green")
        ))

Org-babel languages

(org-babel-do-load-languages
 'org-babel-load-languages
 '(
   (shell . t)
   (python . t)
   ))

Erc: Emacs IRC Client

;; Erc Config - Emacs IRC-client
(add-hook 'erc-text-matched-hook 'erc-beep-on-match)
(setq erc-beep-match-types '(current-nick keyword))

Auctex: Latex wizardry

;; LaTeX/AucTEX
(use-package auctex
  :defer t
  :ensure t)
(require 'tex-site)
(setq TeX-command-extra-options "-shell-escape") ;For compile with minted
(add-hook 'LaTeX-mode-hook 'auto-fill-mode)
(add-hook 'LaTeX-mode-hook 'flyspell-mode)

Projectile: Project management

(use-package projectile
  :bind-keymap
  ("C-c p" . projectile-command-map)
  :config
  (projectile-mode +1)
  (setq projectile-mode-line-function
        '(lambda () (format " P[%s]" (projectile-project-name)))
        )
  )

(use-package helm-projectile
  :config
  (helm-projectile-on)
  )

Flycheck: syntax checking stuff

(use-package flycheck
  :ensure t
  :init (global-flycheck-mode)
  :delight " FC"
  )

;; Add proselint checker for prose
(flycheck-define-checker proselint
  "A linter for prose."
  :command ("proselint" source-inplace)
  :error-patterns
  ((warning line-start (file-name) ":" line ":" column ": "
            (id (one-or-more (not (any " "))))
            (message) line-end))
  :modes (text-mode markdown-mode gfm-mode latex-mode))

(add-to-list 'flycheck-checkers 'proselint)

Custom functions

base64-encode-no-line-breaks

(defun base64-encode-region-no-break ()
  (interactive)
  (base64-encode-region (mark) (point) t))

Toggle transparent emacs

(defun toggle-transparency ()
  (interactive)
  (let ((alpha (frame-parameter nil 'alpha)))
    (set-frame-parameter
     nil 'alpha
     (if (eql (cond ((numberp alpha) alpha)
                    ((numberp (cdr alpha)) (cdr alpha))
                    ;; Also handle undocumented (<active> <inactive>) form.
                    ((numberp (cadr alpha)) (cadr alpha)))
              90)
         '(68 . 33) '(100 . 90)))))
(global-set-key (kbd "C-c t") 'toggle-transparency)
; Set default alpha values
(call-interactively 'toggle-transparency)

sudo-edit: Reopen file as root [C-x C-r]

                                        ; Reopen file as emacs
(defun sudo-edit (&optional arg)
  "Edit currently visited file as root.

With a prefix ARG prompt for a file to visit.
Will also prompt for a file to visit if current
buffer is not visiting a file."
  (interactive "P")
  (if (or arg (not buffer-file-name))
      (find-file (concat "/sudo:root@localhost:"
                         (ido-read-file-name "Find file(as root): ")))
    (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))


(global-set-key (kbd "C-x C-r") 'sudo-edit)

fix-swedish-keys

(defun fix-swedish-keys ()
  (interactive)
  (set-keyboard-coding-system 'utf-8-unix)
  )

indent-region

;; Indents whole buffer
(defun indent-whole-buffer ()
  (interactive)
  (delete-trailing-whitespace)
  (indent-region (point-min) (point-max) nil)
  (untabify (point-min) (point-max)))

surround-region

(defun surround-region (begin end char)
  "Surrounds a region with a string"
  (interactive  "r\nsString: ")
  (save-excursion
    (goto-char end)
    (insert char)
    (goto-char begin)
    (insert char)))

load-emacs: Reload configuration

;; Reloads .emacs
(defun load-emacs ()
  (interactive)
  (load-file '"~/.emacs"))

Shell send line/region

Send line-or-region in shell-script-mode, as seen on Stack Overflow

(defun sh-send-line-or-region (&optional step)
  (interactive ())
  (let ((proc (get-process "*ansi-term*"))
        pbuf min max command)
    (unless proc
      (let ((currbuff (current-buffer)))
        (ansi-term "/usr/bin/zsh")
        (switch-to-buffer currbuff)
        (setq proc (get-process "*ansi-term*"))
        ))
    (setq pbuff (process-buffer proc))
    (if (use-region-p)
        (setq min (region-beginning)
              max (region-end))
      (setq min (point-at-bol)
            max (point-at-eol)))
    (setq command (concat (buffer-substring min max) "\n"))
    (with-current-buffer pbuff
      (goto-char (process-mark proc))
      (move-marker (process-mark proc) (point))
      ) ;;pop-to-buffer does not work with save-current-buffer -- bug?
    (comint-send-string  proc command)
    (display-buffer (process-buffer proc) t)
    (when step
      (goto-char max)
      (next-line))
    )
  )

(defun sh-send-line-or-region-and-step ()
  (interactive)
  (sh-send-line-or-region t))
(defun sh-switch-to-process-buffer ()
  (interactive)
  (pop-to-buffer (process-buffer (get-process "*ansi-term*")) t))

(add-hook 'sh-mode-hook (lambda()
                          (local-set-key (kbd "C-c C-c") 'sh-send-line-or-region)))

display-ansi-colors (+ auto-mode for .log-files)

Source

(require 'ansi-color)
(defun display-ansi-colors ()
  (interactive)
  (ansi-color-apply-on-region (point-min) (point-max)))

(add-to-list 'auto-mode-alist '("\\.log\\'" . display-ansi-colors))

Eldoc - documentation

     (use-package eldoc-box
     :ensure t
     :config
     (set-face-attribute 'eldoc-box-border nil :background "saddle brown")
     (set-face-attribute 'eldoc-box-body nil :background "#3b2d27")
     (set-variable 'eldoc-box-clear-with-C-g t)
     (setq x-gtk-resize-child-frames 'resize-mode)
)

Major mode configurations

lsp-mode

(setq lsp-keymap-prefix "C-M-l")
(use-package lsp-mode
     :ensure t
     :config
     ;;(setq lsp-completion-provider :none)
)
(use-package lsp-ui
     :ensure t
)

Term mode

(add-hook 'term-mode-hook (lambda ()
                            ;; Use nerdfonts
                            (setq buffer-face-mode-face '(:family "ShureTechMono Nerd Font"))
                            (buffer-face-mode)
                            )
          )

Matlab mode

;; Disabled: for some reason this wouldnt work with Emacs 25+
;;(use-package matlab-mode)

C mode

Hs-minor-mode: for hiding {blocks} etc.

(add-hook 'c-mode-hook 'hs-minor-mode) ;; Enables hide/show of code blocks.
;; Hide/show blocks of code
(global-set-key (kbd "C-c M-s") 'hs-show-all)
(global-set-key (kbd "C-c M-h") 'hs-hide-all)
(global-set-key (kbd "C-.") 'hs-toggle-hiding)

Show line & column number

(add-hook 'c-mode-hook 'column-number-mode) ;; Shows column.
(add-hook 'c-mode-hook 'linum-mode) ;; Shows line-numbering

Octave mode

;; Octave
(add-hook 'octave-mode-hook (lambda()
                              (local-set-key (kbd "C-c C-c") 'octave-send-region)
                              (local-set-key (kbd "C-c C-l") 'octave-send-line)
                              )
          )
;; *.m-files automatically aopen in octave-mode
(setq auto-mode-alist
      (cons '("\\.m$" . octave-mode) auto-mode-alist))

Java mode

;; Java programming hooks
(add-hook 'java-mode-hook (lambda()
                            (setq c-basic-offset 4)
                            ))

Markdown mode

(use-package markdown-mode)
(add-hook 'markdown-mode-hook (lambda()
                                (set-fill-column 80)
                                (auto-fill-mode)
                                (flyspell-mode)
                                ))

Php mode

(use-package php-mode)

Jsx mode

(use-package jsx-mode)
(add-to-list 'auto-mode-alist '("\\.jsx\\'" . jsx-mode))

Python mode

(use-package pyvenv)
(use-package elpy)
(elpy-enable)

Company-jedi as auto-completion framework

(use-package company-jedi
  :config
  (add-to-list 'company-backends 'company-jedi))

Dockerfile mode

(use-package dockerfile-mode)

Docker-compose-mode

(use-package docker-compose-mode)

k8s-mode

(use-package k8s-mode
  :ensure t
  :hook (k8s-mode . yas-minor-mode)
  :mode "\\.yaml\\'"
)

GO mode

(use-package go-mode
  :config
  (add-hook 'go-mode-hook #'lsp-deferred)

  ;; Set up before-save hooks to format buffer and add/delete imports.
  ;; Make sure you don't have other gofmt/goimports hooks enabled.
  (defun lsp-go-install-save-hooks ()
    (add-hook 'before-save-hook #'lsp-format-buffer t t)
    (add-hook 'before-save-hook #'lsp-organize-imports t t))
  (add-hook 'go-mode-hook #'lsp-go-install-save-hooks))

shell-script-mode

(setq comint-scroll-to-bottom-on-output t)
;; Editing command line in zsh creates a tmp file which should be opened in shell-script-mode
(add-to-list 'auto-mode-alist '("/tmp/zsh.*" . shell-script-mode))

web development (js|jsx|ts)

Prettier: makes thins prettier.

(use-package prettier)
(setenv "NODE_PATH" (concat  (expand-file-name default-directory) ".config/yarn/global/node_modules"))

Tide: Typescript magic

(use-package typescript-mode
  :mode "\\.ts\\'"
        "\\.mjs\\'"
  )
(use-package tide)
(defun setup-tide-mode ()
  (interactive)
  (tide-setup)
  (flycheck-mode +1)
  (setq flycheck-check-syntax-automatically '(save mode-enabled))
  (eldoc-mode +1)
  (eldoc-box-hover-mode +1)
  (tide-hl-identifier-mode +1)
  (prettier-mode +1)
  (company-mode +1))
;; aligns annotation to the right hand side
(setq company-tooltip-align-annotations t)
;; formats the buffer before saving
;;(add-hook 'before-save-hook 'tide-format-before-save)
(setq tide-format-options '(:indentSize 2 :tabSize 2))
(add-hook 'typescript-mode-hook #'setup-typescript-mode)

(flycheck-add-next-checker 'javascript-eslint 'javascript-tide 'append)

Deno stuff, won’t work with tide though.

(defun setup-lsp-ts ()
  (interactive)
  (lsp)
  (flycheck-mode +1)
  (setq flycheck-check-syntax-automatically '(save mode-enabled))
  (eldoc-mode +1)
  (eldoc-box-hover-mode +1)
  (prettier-mode +1)
  (company-mode +1))

Web-mode

(use-package eglot)
(defvar ts-use-tide nil "Load tide-mode when loading .ts")
(defvar ts-use-lsp t "Load lsp-mode when loading .ts")
(defvar ts-use-deno-lsp nil "Load lsp-mode with deno when loading .ts")
(defvar ts-use-eglot nil "Load eglot when loading .ts")
(add-to-list 'eglot-server-programs '((js-mode typescript-mode) . (eglot-deno "deno" "lsp")))

(defclass eglot-deno (eglot-lsp-server) ()
  :documentation "A custom class for deno lsp.")

(cl-defmethod eglot-initialization-options ((server eglot-deno))
  "Passes through required deno initialization options"
  (list :enable t
        :lint t))
(defun global-use-ts-tide ()
  (interactive)
  (setq ts-use-tide t)
  (setq ts-use-lsp nil)
  (setq ts-use-deno-lsp nil)
  (setq ts-use-eglot nil)
  )
(defun global-use-ts-lsp ()
  (interactive)
  (setq ts-use-tide nil)
  (setq ts-use-lsp t)
  (setq ts-use-eglot nil)
  (setq lsp-disabled-clients '())
  )
(defun global-use-deno-lsp ()
  (interactive)
  (setq ts-use-tide nil)
  (setq ts-use-lsp t)
  (setq ts-use-eglot nil)
  (setq lsp-disabled-clients '(ts-ls))
  )
(defun global-use-eglot-lsp ()
  (interactive)
  (setq ts-use-tide nil)
  (setq ts-use-lsp nil)
  (setq ts-use-deno-lsp nil)
  (setq ts-use-eglot t)
  )  
(defun setup-typescript-mode ()
  (interactive)
  (if ts-use-tide
      (setup-tide-mode)
    (if ts-use-eglot
        (eglot-ensure)
      (setup-lsp-ts)
      )
    )
  )
(use-package web-mode)
(add-to-list 'auto-mode-alist '("\\.[jt]sx\\'" . web-mode))
(add-hook 'web-mode-hook
          (lambda ()
            (setup-typescript-mode)
            ))
;; enable typescript-eslint checker
(flycheck-add-next-checker 'javascript-eslint 'javascript-tide 'append)

Javascript

(use-package js2-mode
  :ensure t
  :mode "\\.js\\'"
  :delight " JS2"
  :hook ('js2-mode . #'setup-tide-mode)
  )
;; JS
(flycheck-add-next-checker 'javascript-eslint 'javascript-tide 'append)
;; JSX
(flycheck-add-next-checker 'javascript-eslint 'jsx-tide 'append)

Various keybindings

Fixes for swedish/strange input devices etc

;; Get back some unusable keys for swedish keyboard
(global-set-key (kbd "<S-dead-circumflex>") "^")
(global-set-key (kbd "<S-dead-grave>") "`")
(define-key key-translation-map [dead-grave] "`")
(define-key key-translation-map [dead-acute] "'")
(define-key key-translation-map [dead-circumflex] "^")
(define-key key-translation-map [dead-diaeresis] "\"")
(define-key key-translation-map [dead-tilde] "~")
(put 'downcase-region 'disabled nil)

;; Original keybinding: M-$, however alt+shift+4=¤ on swedish keyboard
(global-set-key (kbd "M-¤") 'ispell-word)

Other

;; I dont remember why or how, but this fixed some problem some time...
(global-set-key (kbd "RET") 'newline-and-indent)

;; Display documentation of current major mode and minor modes.
(global-set-key (kbd "C-h M") 'describe-mode)

Rebinding of “impossible” en_us keys on sv_se keyboards

(global-set-key (kbd "C-ö") 'indent-region)
(global-set-key (kbd "C-å") 'comment-or-uncomment-region)

Notes

Make Emacs mirror ansi-term mirror

Putting the following in zshrc configuration will make Emacs understand local directory so that C-x C-f will open files accordingly, see https://www.emacswiki.org/emacs/AnsiTermHints#toc5

precmd() {
    if [ -z "$EMACS" ]; then
        return
    fi
    echo -e "\033AnSiTu" "$LOGNAME" # $LOGNAME is more portable than using whoami.
    echo -e "\033AnSiTc" "$(pwd)"
    if [ $(uname) = "SunOS" ]; then
        # The -f option does something else on SunOS and is not needed anyway.
        hostname_options="";
    else
        hostname_options="-f";
    fi
    echo -e "\033AnSiTh" "$(hostname $hostname_options)" # Using the -f option can
    # cause problems on some OSes.
}

Archived stuff

;; Legacy of an old experiment
;; (global-set-key (kbd "C-c m RET") 'music-player-play-pause)
;; (global-set-key (kbd "C-c m p") 'music-player-prev)
;; (global-set-key (kbd "C-c m n") 'music-player-next)
;; (global-set-key (kbd "C-c m <up>") 'music-player-volume-up)
;; (global-set-key (kbd "C-c m <down>") 'music-player-volume-down)

The End.

(print "Eeemil's configuration loaded!")