Skip to content

Files

Latest commit

e40c1ce · Apr 28, 2015

History

History
956 lines (844 loc) · 31.2 KB

emacstuff.org

File metadata and controls

956 lines (844 loc) · 31.2 KB

Secret

(when (file-exists-p (emacstuff-file "secret.el"))
  (load-file (emacstuff-file "secret.el")))

Basic configuration

Custom.el

(setq emacstuff-custom-file (emacstuff-file "custom.el"))

;; create file custom.el if it does not exist
(unless (file-exists-p emacstuff-custom-file)
  (with-temp-buffer (write-file (emacstuff-file "custom.el"))))

(setq custom-file emacstuff-custom-file)
(load custom-file)

Base directory

(when (eq 'windows-nt system-type)
  (cd "u:/sandbox"))

(setq sandbox-directory
      (case system-type
        (windows-nt "u:/sandbox/")
        (t "~/sandbox/")))

(defsubst jx--sandbox-file (file)
  (expand-file-name file sandbox-directory))

Server

(server-start)
(remove-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function)

Encoding

(prefer-coding-system 'utf-8)
(set-default-coding-systems 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)

;; Treat clipboard input as UTF-8 string first; compound text next, etc.
(setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING))

Start Maximized

(add-hook 'window-setup-hook 'toggle-frame-maximized t)

Basic modes

(setq inhibit-splash-screen t)
(show-paren-mode 1)
(tool-bar-mode -1)
(menu-bar-mode -1)
(setq column-number-mode t)
(tooltip-mode -1)
(setq tooltip-use-echo-area t)
(put 'narrow-to-region 'disabled nil)
(global-visual-line-mode 1)
(global-linum-mode 1)

Remove prompts

(fset 'yes-or-no-p 'y-or-n-p)
(setq confirm-nonexistent-file-or-buffer nil)
(setq ido-create-new-buffer 'always)
(setq inhibit-startup-message t
      inhibit-startup-echo-area-message t)
(setq kill-buffer-query-functions
  (remq 'process-kill-buffer-query-function
         kill-buffer-query-functions))

Recentf

(require 'recentf)

;; get rid of `find-file-read-only' and replace it with something
;; more useful.
(global-set-key (kbd "C-x C-r") 'ido-recentf-open)

;; enable recent files mode.
(recentf-mode t)

; 50 files ought to be enough.
(setq recentf-max-saved-items 50)

(defun ido-recentf-open ()
  "Use `ido-completing-read' to \\[find-file] a recent file"
  (interactive)
  (if (find-file (ido-completing-read "Find recent file: " recentf-list))
      (message "Opening file...")
    (message "Aborting")))

Backup and recycle bin

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

(setq delete-by-moving-to-trash t)

Utilities

(defmacro jx--save-excursion (&rest forms)
  (let ((oldp (gensym))
        (oldbuff (gensym))
        (retval (gensym)))
    `(let* ((,oldp (point))
            (,oldbuff (current-buffer))
            (,retval (progn ,@forms)))
       (unless (eq (current-buffer) ,oldbuff)
         (switch-to-buffer ,oldbuff))
       (goto-char ,oldp)
       ,retval)))

(defun unfill-region (beg end)
  "Unfill the region, joining text paragraphs into a single
    logical line.  This is useful, e.g., for use with
    `visual-line-mode'."
  (interactive "*r")
  (let ((fill-column (point-max)))
    (fill-region beg end)))

;; Handy key definition
(define-key global-map "\C-\M-Q" 'unfill-region)

(defun jx-uniquify-all-lines-region (start end arg)
  "Find duplicate lines in region START to END keeping first occurrence."
  (interactive "*rp")
  (cond
   ((= arg 4) (sort-lines nil start end))
   ((= arg 16) (sort-lines 'reverse  start end)))
  (save-excursion
    (let ((end (copy-marker end)))
      (while
          (progn
            (goto-char start)
            (re-search-forward "^\\(.*\\)\n\\(\\(.*\n\\)*\\)\\1\n" end t))
        (replace-match "\\1\n\\2")))))

(defun jx-uniquify-all-lines-buffer (arg)
  "Delete duplicate lines in buffer and keep first occurrence."
  (interactive "*p")
  (jx-uniquify-all-lines-region (point-min) (point-max) arg))

(defun jx-restart ()
  (interactive)
  (org-babel-load-file (expand-file-name "emacstuff.org" emacstuff-dir)))

Eldoc

(require 'eldoc)

(add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
(add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)
(add-hook 'ielm-mode-hook 'turn-on-eldoc-mode)

Dependencies and packages

Package setup

(require 'package)

(setq package-archives
      '(("gnu" . "http://elpa.gnu.org/packages/")
        ;("marmalade" . "http://marmalade-repo.org/packages/")
        ("melpa" . "http://melpa.milkbox.net/packages/")))

(package-initialize)

(defvar emacstuff-packages
  '(auto-complete
    boxquote
    deft
    expand-region
    flx-ido
    google-this
    highlight-parentheses
    ibuffer-vc
    ido-ubiquitous
    iedit
    imenu-anywhere
    magit
    markdown-mode
    multiple-cursors
    pandoc-mode
    paredit
    pretty-lambdada
    projectile
    racket-mode
    rainbow-delimiters
    smart-mode-line
    smex
    visible-mark
    writeroom-mode
    web-mode)
  "Libraries that should be installed by default.")

(unless package-archive-contents
  (package-refresh-contents))

(dolist (package emacstuff-packages)
  (unless (package-installed-p package)
    (package-install package)))

Smex

(global-set-key (kbd "M-x") 'smex)
(global-set-key (kbd "M-X") 'smex-major-mode-commands)

;; Old M-x.
(global-set-key (kbd "C-c C-c M-x") 'execute-extended-command)

Paredit

(autoload 'enable-paredit-mode "paredit" "Turn on pseudo-structural editing of Lisp code." t)
(add-hook 'emacs-lisp-mode-hook       #'enable-paredit-mode)
(add-hook 'eval-expression-minibuffer-setup-hook #'enable-paredit-mode)
(add-hook 'ielm-mode-hook             #'enable-paredit-mode)
(add-hook 'lisp-mode-hook             #'enable-paredit-mode)
(add-hook 'lisp-interaction-mode-hook #'enable-paredit-mode)
(add-hook 'scheme-mode-hook           #'enable-paredit-mode)
(add-hook 'racket-mode-hook           #'enable-paredit-mode)

; Eldoc
(eldoc-add-command
 'paredit-backward-delete
 'paredit-close-round)

Iedit

(require 'iedit)

(global-set-key (kbd "C-<f8>") 'iedit-mode)

Auto-complete

(require 'auto-complete-config)
(ac-config-default)

Markdown-mode

(autoload 'markdown-mode "markdown-mode"
   "Major mode for editing Markdown files" t)
(add-to-list 'auto-mode-alist '("\\.text\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.markdown\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))

(add-hook 'markdown-mode-hook 'pandoc-mode)

Multiple cursors

(global-set-key (kbd "<f8> m b") 'mc/edit-beginnings-of-lines)
(global-set-key (kbd "<f8> m e") 'mc/edit-ends-of-lines)
(global-set-key (kbd "<f8> m a") 'mc/mark-all-dwim)
(global-set-key (kbd "C->") 'mc/mark-next-like-this)
(global-set-key (kbd "C-<") 'mc/mark-previous-like-this)

Pretty lambda

(require 'pretty-lambdada)
(add-to-list 'pretty-lambda-auto-modes 'racket-mode)
(pretty-lambda-for-modes)

RainbowDelimiters

(require 'rainbow-delimiters)
(add-hook 'racket-mode-hook 'rainbow-delimiters-mode)

Smart mode line

(sml/setup)

Expand region

(require 'expand-region)
(global-set-key (kbd "C-=") 'er/expand-region)

Google this

(google-this-mode 1)

Writeroom-mode

(global-set-key (kbd "M-<f1>") 'writeroom-mode)

Vendor directory

(defvar emacstuff-vendor-dir (expand-file-name "vendor" emacstuff-dir))

(when (file-exists-p emacstuff-vendor-dir)
  (add-to-list 'load-path emacstuff-vendor-dir)
  (dolist (project (directory-files emacstuff-vendor-dir t "\\w+"))
    (when (file-directory-p project)
      (add-to-list 'load-path project))))

Occur

(eval-when-compile
  (require 'cl))

(defun get-buffers-matching-mode (mode)
  "Returns a list of buffers where their major-mode is equal to MODE"
  (let ((buffer-mode-matches '()))
   (dolist (buf (buffer-list))
     (with-current-buffer buf
       (if (eq mode major-mode)
           (add-to-list 'buffer-mode-matches buf))))
   buffer-mode-matches))

(defun multi-occur-in-this-mode ()
  "Show all lines matching REGEXP in buffers with this major mode."
  (interactive)
  (multi-occur
   (get-buffers-matching-mode major-mode)
   (car (occur-read-primary-args))))


(global-set-key (kbd "C-<f1>") 'occur)

;; global key for `multi-occur-in-this-mode' - you should change this.
(global-set-key (kbd "C-<f2>") 'multi-occur-in-this-mode)

Ido

(when (> emacs-major-version 21)
  (ido-mode t)
  (setq ido-enable-prefix nil
        ido-enable-flex-matching t
        ido-use-faces nil
        ido-create-new-buffer 'always
        ido-use-filename-at-point t
        ido-max-prospects 10))

(require 'flx-ido)

(setq ido-everywhere t)
(flx-ido-mode 1)
(setq ido-max-directory-size 100000)
(ido-mode 'both)
(setq ido-enable-flex-matching t
      ido-use-virtual-buffers t)
(setq ido-default-file-method 'selected-window)
(setq ido-default-buffer-method 'selected-window)

(defun jx--prepare-files-for-ido (files)
  (cl-labels ((make (file)
                    (cons file
                          (nreverse
                           (split-string (expand-file-name file) "/"))))
              (file (x) (car x))
              (head (x) (cadr x))
              (tail (x) (cddr x))
              (pop-head (x)
                        (let ((tail (tail x)))
                          (setf (cdr x) tail))
                        x))
    (let ((xs (mapcar #'make files))
          (table (make-hash-table :test #'equal))
          (final nil))
      (dolist (x xs)
        (let ((key (head x)))
          (push (pop-head x) (gethash key table))))
      (loop
       while (> (hash-table-count table) 0)
       do
       (maphash (lambda (key value)
                  (when (= (length value) 1)
                    (let ((x (first value)))
                      (push (cons key (file x)) final))
                    (remhash key table))) table)
       (maphash (lambda (key value)
                  (when (> (length value) 1)
                    (dolist (x value)
                      (let ((new-key (format "%s/%s" (head x) key)))
                        (push (pop-head x) (gethash new-key table))))
                    (remhash key table))) table))
      final)))

(defun jx--completing-read-file/short-path (prompt files)
  (let* ((options (jx--prepare-files-for-ido files))
         (option-list (mapcar #'first options))
         (selection (ido-completing-read prompt option-list nil t)))
    (when selection
      (cdr (assoc selection options)))))

(defun jx--completing-read-file (prompt files &optional full-path)
  (or (and full-path (ido-completing-read prompt files nil t))
      (jx--completing-read-file/short-path prompt files)))

Deft

(setq deft-auto-save-interval 0)

(defvar jx--deft-dir "~/Dropbox/Pessoal/deft/")

(defun jx--deft (dir extension mode)
  (setq deft-directory (expand-file-name dir jx--deft-dir))
  (setq deft-use-filename-as-title t)
  (setq deft-extension extension)
  (setq deft-text-mode mode)
  (deft)
  (deft-refresh))

(global-set-key (kbd "<f7>") (lambda () (interactive) (jx--deft "org" "gpg" 'org-mode)))
(global-set-key (kbd "M-<f7>") (lambda () (interactive) (jx--deft "md" "gpg" 'markdown-mode)))

Racket mode

(require 'racket-mode)

(defun racket-repl--bol ()
  (interactive)
  (when (= (point) (comint-bol)) (beginning-of-line)))

(defun racket-repl--last-prompt-end ()
  (cond ((and (boundp 'comint-last-prompt) (markerp (cdr comint-last-prompt)))
         (marker-position (cdr comint-last-prompt)))
        ((and (boundp 'comint-last-prompt-overlay) comint-last-prompt-overlay)
         (overlay-end comint-last-prompt-overlay))
        (t (save-excursion (racket-repl--bol) (point)))))

(defun racket-repl--last-prompt-start ()
  (cond ((and (boundp 'comint-last-prompt) (markerp (car comint-last-prompt)))
         (marker-position (car comint-last-prompt)))
        ((and (boundp 'comint-last-prompt-overlay) comint-last-prompt-overlay)
         (overlay-start comint-last-prompt-overlay))
        (t (save-excursion (racket-repl--bol) (point)))))

(defun racket-repl-clear-buffer ()
  "Delete the output generated by the scheme process."
  (interactive)
  (let ((inhibit-read-only t))
    (delete-region (point-min) (racket-repl--last-prompt-start))
    (when (< (point) (racket-repl--last-prompt-end))
      (goto-char (racket-repl--last-prompt-end)))
    (recenter t)))

(defun racket-clear-repl-buffer ()
  (interactive)
  (when (boundp 'racket--repl-buffer-name)
    (jx--save-excursion
     (switch-to-buffer racket--repl-buffer-name)
     (racket-repl-clear-buffer))))

(define-key racket-repl-mode-map "\C-c\M-o" 'racket-repl-clear-buffer)
(define-key racket-mode-map "\C-c\M-o" 'racket-clear-repl-buffer)

View mode

;; view docs
(fset 'doc-prev "\C-xo\C-x[\C-xo")
(fset 'doc-next "\C-xo\C-x]\C-xo")
(global-set-key (kbd "M-[") 'doc-prev)
(global-set-key (kbd "M-]") 'doc-next)

Spelling

(dolist (hook '(text-mode-hook))
  (add-hook hook (lambda () (flyspell-mode 1))))

(dolist (hook '(racket-mode-hook java-mode-hook lisp-mode-hook))
    (add-hook hook (lambda () (flyspell-prog-mode))))

(dolist (hook '(magit-auto-revert magit-mode change-log-mode-hook log-edit-mode-hook))
  (add-hook hook (lambda () (flyspell-mode -1))))

(let ((langs '("brasileiro" "american")))
  (setq lang-ring (make-ring (length langs)))
  (dolist (elem langs) (ring-insert lang-ring elem)))

(defun jx--cycle-ispell-languages ()
  (interactive)
  (let ((lang (ring-ref lang-ring -1)))
    (ring-insert lang-ring lang)
    (ispell-change-dictionary lang)))

(global-set-key [f6] 'jx--cycle-ispell-languages)

Sandbox and Scratch

(defun jx-sandbox-general (arg ext)
  (interactive "P\nsExtension: ")
  (find-file
   (jx--sandbox-file
    (cond
     ((equalp arg '()) (concat "sandbox." ext))
     ((equalp arg '(4))
      (concat (format-time-string "sandbox-%Y-%m-%d.") ext))
     ((equalp arg '(16))
      (concat "sandbox-" (org-read-date) "." ext))))))

(defun jx--sandbox-racket ()
  (interactive)
  (jx-sandbox-general nil "rkt"))

(defun jx--scratch ()
  (interactive)
  (switch-to-buffer "*scratch*"))

(global-set-key (kbd "<f9> <f9>") 'jx-sandbox-general)
(global-set-key (kbd "<f9> r") 'jx--sandbox-racket)
(global-set-key (kbd "<f9> s") 'jx--scratch)

Org-mode

Agenda

(setq org-agenda-archives-mode nil)
(setq org-agenda-skip-comment-trees nil)
(setq org-agenda-skip-function nil)

(global-set-key (kbd "<f9> a s") 'jx-ido-find-agenda-file)

(defun jx-ido-find-agenda-file (full-path)
  (interactive "P")
  (let ((file (jx--completing-read-file "Agenda file: " (org-agenda-files t 'ifmode) full-path)))
    (when file
      (find-file file))))

Archive

(setq org-archive-mark-done nil)
(setq org-archive-location "%s_archive::* Archived Tasks")

Loggin stuff

(setq org-log-done (quote time))
(setq org-log-into-drawer t)
(setq org-log-state-notes-insert-after-drawers nil)

Custom agendas

;; Do not dim blocked tasks
(setq org-agenda-dim-blocked-tasks nil)

;; Compact the block agenda view
(setq org-agenda-compact-blocks t)

;; Custom agenda command definitions
(setq org-agenda-custom-commands
      '(("b" "New Backlog Items" tags-todo "new"
         ((org-agenda-overriding-header "New Backlog Items")
          (org-tags-match-list-sublevels nil)))
        (" " "Agenda"
         (;(agenda "Cockpit" nil)
          (todo "NEXT"
                ((org-agenda-overriding-header "In Progress")
                 (org-agenda-skip-function '(jx--org-agenda-skip-tags "new" "REFILE"))))
          (todo "TODO"
                ((org-agenda-overriding-header "Backlog")
                 (org-agenda-skip-function '(jx--org-agenda-skip-tags "new" "REFILE"))))
          (tags "new"
                ((org-agenda-overriding-header "New Backlog Items")
                 (org-tags-match-list-sublevels nil)
                                        ;(org-agenda-skip-function '(jx--org-agenda-skip-tags "REFILE"))
                 (org-agenda-hide-tags-regexp "new")))
          (todo "WAITING"
                ((org-agenda-overriding-header "Waiting...")
                 (org-agenda-skip-function '(jx--org-agenda-skip-tags "new" "REFILE"))
                 (org-agenda-hide-tags-regexp "WAITING")))
          (tags "REFILE"
                ((org-agenda-overriding-header "Tasks to Refile")
                 (org-tags-match-list-sublevels nil)
                 (org-agenda-hide-tags-regexp "REFILE")))
          (tags "-REFILE/"
                ((org-agenda-overriding-header "Tasks to Archive")
                 (org-agenda-skip-function 'jx--skip-non-archivable-tasks)
                 (org-tags-match-list-sublevels nil)))
          (todo "HOLD"
                ((org-agenda-overriding-header "On hold")
                 (org-agenda-hide-tags-regexp "HOLD"))))
         nil)))

(defun jx--org-agenda-skip-tags (&rest tags)
  "Skip entry that contains at least one tag in tags"
  (let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
    (if (intersection tags (org-get-tags-at) :test #'string-equal)
        next-headline
      nil)))

(defun jx--skip-non-archivable-tasks ()
  "Skip trees that are not available for archiving"
  (save-restriction
    (widen)
    ;; Consider only tasks with done todo headings as archivable candidates
    (let ((next-headline (save-excursion (or (outline-next-heading) (point-max))))
          (subtree-end (save-excursion (org-end-of-subtree t))))
      (if (member (org-get-todo-state) org-todo-keywords-1)
          (if (member (org-get-todo-state) org-done-keywords)
              (let* ((daynr (string-to-int (format-time-string "%d" (current-time))))
                     (a-month-ago (* 60 60 24 (+ daynr 1)))
                     (last-month (format-time-string "%Y-%m-" (time-subtract (current-time) (seconds-to-time a-month-ago))))
                     (this-month (format-time-string "%Y-%m-" (current-time)))
                     (subtree-is-current (save-excursion
                                           (forward-line 1)
                                           (and (< (point) subtree-end)
                                                (re-search-forward (concat last-month "\\|" this-month) subtree-end t)))))
                (if subtree-is-current
                    subtree-end ; Has a date in this month or last month, skip it
                  nil))  ; available to archive
            (or subtree-end (point-max)))
        next-headline))))

(defun jx--org-auto-exclude-function (tag)
  "Automatic task exclusion in the agenda with / RET"
  (and (cond
        ((string= tag "hold") t))
       (concat "-" tag)))

(setq org-agenda-auto-exclude-function 'jx--org-auto-exclude-function)

Directories

(setq org-directory "~/.org")
(setq org-default-notes-file "~/.org/refile.org")

TODO Keywords

(setq org-todo-keywords
      '((sequence "TODO(t)" "NEXT(n)" "|" "DONE(d)")
        (sequence "WAITING(w@/!)" "HOLD(h@/!)" "|" "CANCELLED(c@/!)" "PHONE" "MEETING")))

(setq org-todo-keyword-faces
      (quote (("TODO" :foreground "red" :weight bold)
              ("NEXT" :foreground "blue" :weight bold)
              ("DONE" :foreground "forest green" :weight bold)
              ("WAITING" :foreground "orange" :weight bold)
              ("HOLD" :foreground "magenta" :weight bold)
              ("CANCELLED" :foreground "forest green" :weight bold)
              ("MEETING" :foreground "forest green" :weight bold)
              ("PHONE" :foreground "forest green" :weight bold))))

(setq org-use-fast-todo-selection t)
(setq org-treat-S-cursor-todo-selection-as-state-change nil)

TODO state triggers

(setq org-todo-state-tags-triggers
      '(("CANCELLED" ("CANCELLED" . t))
        ("WAITING" ("WAITING" . t))
        ("HOLD" ("WAITING") ("HOLD" . t))
        (done ("WAITING") ("HOLD"))
        ("TODO" ("WAITING") ("CANCELLED") ("HOLD"))
        ("NEXT" ("WAITING") ("CANCELLED") ("HOLD"))
        ("DONE" ("WAITING") ("CANCELLED") ("HOLD"))))

Capture

;; Capture templates for: TODO tasks, Notes, appointments, phone calls, meetings, and org-protocol
(setq org-capture-templates
      '(("t" "todo" entry (file "~/.org/refile.org")
         "* TODO %?\n  %U\n  %a\n")
        ("r" "respond" entry (file "~/.org/refile.org")
         "* NEXT Respond to %:from on %:subject\nSCHEDULED: %t\n%U\n%a\n")
        ("n" "note" entry (file "~/.org/refile.org")
         "* %? :NOTE:\n%U\n%a\n")
        ("j" "Journal" entry (file+datetree "~/.org/diary.org")
         "* %?\n%U\n")
        ("w" "org-protocol" entry (file "~/.org/refile.org")
         "* TODO Review %c\n%U\n")
        ("m" "Meeting" entry (file "~/.org/refile.org")
         "* MEETING with %? :MEETING:\n%U")
        ("p" "Phone call" entry (file "~/.org/refile.org")
         "* PHONE %? :PHONE:\n%U")
        ("h" "Habit" entry (file "~/.org/refile.org")
         "* NEXT %?\n%U\n%a\nSCHEDULED: %(format-time-string \"<%Y-%m-%d %a .+1d/3d>\")\n:PROPERTIES:\n:STYLE: habit\n:REPEAT_TO_STATE: NEXT\n:END:\n")))
;; Remove empty LOGBOOK drawers on clock out
(defun jx--remove-empty-drawer-on-clock-out ()
  (interactive)
  (save-excursion
    (beginning-of-line 0)
    (org-remove-empty-drawer-at (point))))

(add-hook 'org-clock-out-hook 'jx--remove-empty-drawer-on-clock-out 'append)

Refile

; Targets include this file and any file contributing to the agenda - up to 9 levels deep
(setq org-refile-targets (quote ((nil :maxlevel . 9)
                                 (org-agenda-files :maxlevel . 9))))

; Use full outline paths for refile targets - we file directly with IDO
(setq org-refile-use-outline-path t)

; Targets complete directly with IDO
(setq org-outline-path-complete-in-steps nil)

; Allow refile to create parent tasks with confirmation
(setq org-refile-allow-creating-parent-nodes (quote confirm))

; Use IDO for both buffer and file completion and ido-everywhere to t
(setq org-completion-use-ido t)
(setq ido-everywhere t)
(setq ido-max-directory-size 100000)
(ido-mode (quote both))
; Use the current window when visiting files and buffers with ido
(setq ido-default-file-method 'selected-window)
(setq ido-default-buffer-method 'selected-window)
; Use the current window for indirect buffer display
(setq org-indirect-buffer-display 'current-window)

;;;; Refile settings
; Exclude DONE state tasks from refile targets
(defun jx--verify-refile-target ()
  "Exclude todo keywords with a done state from refile targets"
  (not (member (nth 2 (org-heading-components)) org-done-keywords)))

(setq org-refile-target-verify-function 'jx--verify-refile-target)

Skeletons

;; Enable abbrev-mode
(add-hook 'org-mode-hook (lambda () (abbrev-mode 1)))

;; Skeletons
;;
;; sblk - Generic block #+begin_FOO .. #+end_FOO
(define-skeleton skel-org-block
  "Insert an org block, querying for type."
  "Type: "
  "#+begin_" str "\n"
  _ - \n
  "#+end_" str "\n")

(define-abbrev org-mode-abbrev-table "sblk" "" 'skel-org-block)

;; selisp - Emacs Lisp source block
(define-skeleton skel-org-block-elisp
  "Insert a org emacs-lisp block"
  ""
  "#+begin_src emacs-lisp\n"
  _ - \n
  "#+end_src\n")

(define-abbrev org-mode-abbrev-table "selisp" "" 'skel-org-block-elisp)

Structure

(setq org-structure-template-alist
      '(("s" "#+begin_src ?\n\n#+end_src")
        ("e" "#+begin_example\n?\n#+end_example")
        ("q" "#+begin_quote\n?\n#+end_quote")
        ("v" "#+begin_verse\n?\n#+end_verse")
        ("v" "#+begin_verbatim\n?\n#+end_verbatim")
        ("c" "#+begin_center\n?\n#+end_center")
        ("l" "#+begin_latex\n?\n#+end_latex")
        ("l" "#+latex: ")
        ("h" "#+begin_html\n?\n#+end_html")
        ("h" "#+html: ")
        ("a" "#+begin_ascii\n?\n#+end_ascii")
        ("a" "#+ascii: ")
        ("i" "#+index: ?")
        ("i" "#+include: %file ?")))

Key bindings

(global-set-key (kbd "<f12>") 'org-agenda)
(global-set-key (kbd "<f8> s") 'org-save-all-org-buffers)
(global-set-key (kbd "<f8> <f8>") 'org-narrow-to-subtree)

;; Org capture
(global-set-key (kbd "C-c c") 'org-capture)

Encryption

(require 'epa-file)

(defun jx-insert-encrypt-to-line ()
  (interactive)
  (insert
   (concat "-*- epa-file-encrypt-to: (\""
           user-mail-address
           "\") -*-")))

(global-set-key (kbd "C-x x") 'jx-insert-encrypt-to-line)

Misc

(setq next-line-add-newlines t)
(visible-mark-mode 1)
(require 'imenu)
(global-set-key (kbd "M-i") 'imenu-anywhere)
(setq echo-keystrokes 0.1
      use-dialog-box nil
      visible-bell t)

(mapcar
  (lambda (s) (put s 'racket-indent-function 'defun))
 '(arguments))

(mapcar
 (lambda (s) (put s 'racket-indent-function 2))
 '(parameterize-from-config struct))

Indentation and buffer cleanup

(defun jx-untabify-buffer ()
  (interactive)
  (untabify (point-min) (point-max)))

(defun jx-indent-buffer ()
  (interactive)
  (indent-region (point-min) (point-max)))

(defun jx-cleanup-buffer ()
  "Perform a bunch of operations on the whitespace content of a buffer."
  (interactive)
  (jx-indent-buffer)
  (jx-untabify-buffer)
  (delete-trailing-whitespace))

(defun jx-cleanup-region (beg end)
  "Remove tmux artifacts from region."
  (interactive "r")
  (dolist (re '("\\\\*\n" "\W*│*"))
    (replace-regexp re "" nil beg end)))

(global-set-key (kbd "C-x M-t") 'jx-cleanup-region)
(global-set-key (kbd "C-c n") 'jx-cleanup-buffer)

(setq-default show-trailing-whitespace t)

(dolist (mode '(racket-repl-mode))
  (add-hook (intern (format "%s-hook" mode))
            (lambda () (setq-local show-trailing-whitespace nil))))

Power Lisp

(setq lisp-modes '(lisp-mode
                   emacs-lisp-mode
                   common-lisp-mode
                   scheme-mode
                   racket-mode
                   clojure-mode))

(defvar lisp-power-map (make-keymap))
(define-minor-mode lisp-power-mode "Fix keybindings; add power."
  :lighter " (power)"
  :keymap lisp-power-map
  (paredit-mode t))
(define-key lisp-power-map [delete] 'paredit-forward-delete)
(define-key lisp-power-map [backspace] 'paredit-backward-delete)

(defun jx--engage-lisp-power ()
  (lisp-power-mode t))

(dolist (mode lisp-modes)
  (add-hook (intern (format "%s-hook" mode))
            #'jx--engage-lisp-power))

Web-mode

(require 'web-mode)

(add-to-list 'auto-mode-alist '("\\.phtml\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.tpl\\.php\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.[agj]sp\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.as[cp]x\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.erb\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.mustache\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.djhtml\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))

(setq web-mode-engines-alist
      '(("php"    . "\\.phtml\\'")
        ("blade"  . "\\.blade\\."))
      )

(defun jx--web-mode-hook ()
  "Hooks for Web mode."
  (setq web-mode-markup-indent-offset 2)
  (setq tab-width 4)
  (setq web-mode-css-indent-offset 2)
  (setq web-mode-code-indent-offset 2)
  (define-key web-mode-map (kbd "C-n") 'web-mode-tag-match))

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

Projectile

(projectile-global-mode)
(setq projectile-indexing-method
      (case system-type
        (windows-nt 'alien)
        (t 'native)))

(defun jx--projectile-project ()
  (projectile-completing-read
   "Projectile project: " (projectile-relevant-known-projects)))

(require 'magit)
(setq magit-last-seen-setup-instructions "1.4.0")

(defun jx-magit-status (dir)
  (interactive
   (list (if current-prefix-arg
             (if (= 4 (prefix-numeric-value current-prefix-arg))
                 (jx--projectile-project)
               (magit-read-top-dir nil))
           (or (magit-get-top-dir)
               (jx--projectile-project)
               (magit-read-top-dir nil)))))
  (magit-status dir))

General key bindings

(global-set-key (kbd "<f5>") (lambda () (interactive) (revert-buffer 'ignore-auto 'noconfirm)))
(global-set-key (kbd "C-<f5>") (lambda () (interactive) (revert-buffer-with-coding-system 'utf-8 'force)))

(global-set-key (kbd "<f1>") 'menu-bar-mode)

(global-set-key (kbd "<f8> x e")
  (lambda ()
    (interactive)
    (find-file (emacstuff-file "emacstuff.org"))))

(global-set-key (kbd "<f8> x x")
  (lambda ()
    (interactive)
    (kill-buffer "emacstuff.org")))

(global-set-key (kbd "C-+") 'text-scale-increase)
(global-set-key (kbd "C--") 'text-scale-decrease)

(global-set-key (kbd "C-x g") 'jx-magit-status)

(global-set-key (kbd "<f9> g") 'gnus)

(global-set-key (kbd "C-;") 'comment-dwim)

Aliases

(defalias 'list-buffers 'ibuffer)
(defalias 'jx-remove-duplicates 'jx-uniquify-all-lines-buffer)
(defalias 'yes-or-no-p 'y-or-n-p)

Specific settings

(defvar emacstuff--user-settings-file (emacstuff-file (concat (user-login-name) ".org")))
(when (file-exists-p emacstuff--user-settings-file)
  (org-babel-load-file emacstuff--user-settings-file))