Skip to content

Instantly share code, notes, and snippets.

@vermiculus
Created October 17, 2014 21:38
Show Gist options
  • Save vermiculus/45e0c08e35ac531a8491 to your computer and use it in GitHub Desktop.
Save vermiculus/45e0c08e35ac531a8491 to your computer and use it in GitHub Desktop.
(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.
'(ag-arguments (quote ("--skip-vcs-ignores" "--path-to-agignore=\"~/tcs/.agignore\"" "--line-number" "--smart-case" "--nogroup" "--column" "--")))
'(ag-highlight-search t)
'(c-basic-offset (quote set-from-style))
'(c-default-style (quote ((c-mode . "gd-ais") (c++-mode . "gd-ais") (java-mode . "java") (awk-mode . "awk") (other . "gnu"))))
'(c-mode-common-hook (quote (doxymacs-mode add-c-syntax-highlighting)))
'(c-mode-hook nil)
'(calendar-latitude 38.860131)
'(calendar-longitude -77.380241)
'(cc-other-file-alist (quote (("\\.cc\\'" (".hh" ".h")) ("\\.hh\\'" (".cpp" ".cc" ".C")) ("\\.c\\'" (".h")) ("\\.h\\'" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp")) ("\\.C\\'" (".H" ".hh" ".h")) ("\\.H\\'" (".C" ".CC")) ("\\.CC\\'" (".HH" ".H" ".hh" ".h")) ("\\.HH\\'" (".CC")) ("\\.c\\+\\+\\'" (".h++" ".hh" ".h")) ("\\.h\\+\\+\\'" (".c++")) ("\\.cpp\\'" (".hpp" ".hh" ".h")) ("\\.hpp\\'" (".cpp")) ("\\.cxx\\'" (".hxx" ".hh" ".h")) ("\\.hxx\\'" (".cxx")))))
'(cc-search-directories (quote ("../inc" "../src" "../common" "." "/usr/include" "/usr/local/include/*")))
'(column-number-mode t)
'(compile-command "source_all; make -k -j")
'(custom-enabled-themes (quote (sanityinc-tomorrow-night)))
'(custom-safe-themes (quote ("ad9fc392386f4859d28fe4ef3803585b51557838dbc072762117adad37e83585" "628278136f88aa1a151bb2d6c8a86bf2b7631fbea5f0f76cba2a0079cd910f7d" "06f0b439b62164c6f8f84fdda32b62fb50b6d00e8b01c2208e55543a6337433a" default)))
'(desktop-save-mode t)
'(dired-listing-switches "-alh")
'(display-time-format "%a, %b %-d (%-I:%M%P)")
'(display-time-interval 5)
'(display-time-mode t)
'(ff-always-in-other-window t)
'(find-file-hook (quote (#[nil "\302\301!\210\303\304!8\211\207" [buffer-file-name auto-revert-tail-pos make-local-variable 7 file-attributes] 3] yas-global-mode-check-buffers global-font-lock-mode-check-buffers epa-file-find-file-hook vc-find-file-hook)))
'(find-grep-options "-qI")
'(git-commit-mode-hook (quote (turn-on-auto-fill flyspell-mode magit-relate-timeclock-project)))
'(grep-command "grep -nHI -e ")
'(grep-files-aliases (quote (("all" . "* .*") ("el" . "*.el") ("ch" . "*.[ch]") ("c" . "*.c") ("cc" . "*.cc *.cxx *.cpp *.C *.CC *.c++") ("cchh" . "*.cc *.hh *.[ch]xx *.[ch]pp *.[CHh] *.CC *.HH *.[ch]++") ("hh" . "*.hxx *.hpp *.[Hh] *.HH *.h++") ("h" . "*.h") ("l" . "[Cc]hange[Ll]og*") ("m" . "[Mm]akefile*") ("tex" . "*.tex") ("texi" . "*.texi") ("asm" . "*.[sS]"))))
'(grep-find-command (quote ("find . -type f -exec grep -nHI -e {} +" . 34)))
'(grep-find-ignored-files (quote ("tags" "TAGS" ".#*" "*.o" "*~" "*.bin" "*.lbin" "*.so" "*.a" "*.ln" "*.blg" "*.bbl" "*.elc" "*.lof" "*.glo" "*.idx" "*.lot" "*.fmt" "*.tfm" "*.class" "*.fas" "*.lib" "*.mem" "*.x86f" "*.sparcf" "*.dfsl" "*.pfsl" "*.d64fsl" "*.p64fsl" "*.lx64fsl" "*.lx32fsl" "*.dx64fsl" "*.dx32fsl" "*.fx64fsl" "*.fx32fsl" "*.sx64fsl" "*.sx32fsl" "*.wx64fsl" "*.wx32fsl" "*.fasl" "*.ufsl" "*.fsl" "*.dxl" "*.lo" "*.la" "*.gmo" "*.mo" "*.toc" "*.aux" "*.cp" "*.fn" "*.ky" "*.pg" "*.tp" "*.vr" "*.cps" "*.fns" "*.kys" "*.pgs" "*.tps" "*.vrs" "*.pyc" "*.pyo")))
'(grep-find-template "find . <X> -type f <F> -exec grep <C> -nHIP -e <R> {} +")
'(ido-enable-flex-matching t)
'(ido-mode (quote both) nil (ido))
'(indent-tabs-mode nil)
'(magit-git-executable "~/bin/git")
'(menu-bar-mode nil)
'(org-agenda-files (quote ("~/org/appt" "~/org/bugs")))
'(org-agenda-property-list (quote ("DESCRIPTION")))
'(org-attach-method (quote mv))
'(org-capture-templates (quote (("t" "Task" checkitem (file "appt") "" :clock-keep t) ("s" "Question" checkitem (file+datetree "appt") "" :clock-keep t) ("n" "Note" entry (clock) "" :clock-keep t))))
'(org-default-notes-file (concat org-directory "~/org/notes.org"))
'(org-edit-src-content-indentation 0)
'(org-insert-mode-line-in-empty-file t)
'(package-archives (quote (("gnu" . "http://elpa.gnu.org/packages/") ("melpa" . "http://melpa.milkbox.net/packages/"))))
'(reftex-plug-into-AUCTeX t)
'(safe-local-variable-values (quote ((user-mail-address . "[email protected]") (org-log-into-drawer . t) (org-attach-method . ln) (org-edit-src-content-indentation . 0) (flyspell-mode . t) (auto-fill-mode . t) (require-final-newline))))
'(scroll-bar-mode nil)
'(sh-indentation 2)
'(show-paren-mode t)
'(show-trailing-whitespace (if (string-match "^\\*.*\\*$" (buffer-name)) nil t) nil nil "disabled if and only if the buffer file name begins and ends with '*'")
'(timeclock-first-in-hook (quote (org-agenda-to-appt (lambda nil (interactive) (org-agenda nil "n")))))
'(timeclock-in-hook nil)
'(timeclock-mode-line-display t nil (timeclock))
'(tool-bar-mode nil)
'(truncate-lines t)
'(yas-indent-line (quote fixed)))
(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.
'(gdais-code-section-face ((t (:inherit error :background "dark red" :weight ultra-bold :height 1.5)))))
; '(mode-line ((t (:background "gold" :foreground "black" :box (:line-width -1 :style released-button) :height 1.0))))
; '(mode-line-buffer-id ((t (:weight bold))))
; '(mode-line-inactive ((t (:inherit mode-line :background "gray90" :foreground "black" :box (:line-width -1 :color "grey75") :weight light))))
;

Emacs Customizations File

rpm -i http://download1.rpmfusion.org/free/el/updates/6/i386/rpmfusion-free-release-6-1.noarch.rpm
rpm -i http://download1.rpmfusion.org/nonfree/el/updates/6/i386/rpmfusion-nonfree-release-6-1.noarch.rpm
curl -sSL https://get.rvm.io | bash -s stable --ruby
# gconftool-2 -s -t string /desktop/gnome/interface/gtk_key_theme "Emacs"
;(add-hook 'after-init-hook (lambda () (setenv "SHELL" (setq shell-file-name "/bin/tcsh"))))
;(add-hook 'after-init-hook (lambda () (message "init file loaded")))
(let ((default-directory "~/.emacs.d/elpa/"))
  (normal-top-level-add-subdirs-to-load-path))

(let ((default-directory "~/.emacs.d/manually-installed-packages/"))
  (normal-top-level-add-subdirs-to-load-path))

Requirements

(mapc (lambda (feature) (require feature nil t))
      '(package
        cc-mode
        yasnippet
        ido
        org
        dired
        bf-mode
        fill-column-indicator
        doxymacs
        org-agenda-property
        god-mode
        reftex))

Package System

(add-to-list 'load-path
             "/home/sallred/.emacs.d/manually-installed-packages/doxygen/")

File Local Variables – Without the Files!

Since file-local variables are actually stored in the file, this is a bit weird when you’re working in a community environment.

This code will manage a special file in your emacs directory called .file-local-variables.alist. It will be read in and eval‘d at startup. When find-file visits a file that is in the association list (alist), it will apply all of the variable-value associations as appropriate. That way, the file can be kept clean.

 defvar file-local-variables-alist
; ("/some/absolute.path" (var1 . val1) (var2 . val2))

 defadvice find-file
   see if assoc find an entry
   if so, loop through cdr (the var-val pairs),
     applying (setf (car kv) (cdr kv))
   look for special 'mode key *first*

(defvar all-files-local-variables-alist nil)
(add-hook 'find-file-hook
          (lambda ()
            (print "hi")))

(defun set-all (variable-value-list)
  "Takes VARIABLE-VALUE-LIST, an association list of dotted
pairs (variable . value), and sets each variable = value."
  (if (assoc 'mode variable-value-list)
      (switch-to-mode (cdr (assoc 'mode variable-value-list))))
  (mapc (lambda (kv) (set (car kv) (cdr kv)))
        variable-value-list))

Better File Local Variables

http://www.emacswiki.org/emacs/FileLocalVariables#toc3

(defun my-file-locals-set-directory-class (file class &optional mtime)
  "Enable 'directory local' classes for individual files,
by allowing non-directories in `dir-locals-directory-cache'.
Adapted from `dir-locals-set-directory-class'."
  (setq file (expand-file-name file))
  (unless (assq class dir-locals-class-alist)
    (error "No such class `%s'" (symbol-name class)))
  (push (list file class mtime) dir-locals-directory-cache))

Then use with

(dir-locals-set-class-variables
 'my-javascript-class
 '((nil . ((js-indent-level . 2)
           (indent-tabs-mode . nil)))))

(my-file-locals-set-directory-class
 "path/to/the/file.js" 'my-javascript-class)

General Utility

(defun list-starts-with (starting-elements l)
  (if (null starting-elements) t
    (and (equal (car starting-elements) (car l))
         (list-starts-with (cdr starting-elements) (cdr l)))))

(defun get-random-element (list)
  "Returns a random element of LIST."
  (if (not (and (list) (listp list)))
      (nth (random (length list)) list)
    (error "Argument to get-random-element not a list or the list is empty")))

(defun chomp (str)
  "Chomp leading and tailing whitespace from STR."
  (while (string-match "\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'" str)
    (setq str (replace-match "" t t str))) str)

(defun shell-command-as-kill (command)
  (interactive "sShell command: ")
  (message (kill-new (chomp (shell-command-to-string command)))))

(defun file-string (file)
  (with-temp-buffer
    (insert-file-contents file)
    (buffer-string)))

(defun file-lines (file)
  (split-string (file-string file) "\n" t))

(defun write-string-to-file (string file)
  (interactive "sEnter the string: \nFFile to save to: ")
  (with-temp-buffer
    (insert string)
    (when (file-writable-p file)
      (write-file file nil))))

(defun write-lines (lines file)
  (write-string-to-file (mapconcat (function identity) lines "\n") file))

(defun toggle-fullscreen ()
  (interactive)
  (when (eq window-system 'x)
    (set-frame-parameter
     nil
     'fullscreen
     (when (not (frame-parameter nil 'fullscreen))
       'fullboth))))

(defun toggle-show-trailing-whitespace ()
  (interactive)
  (message "Trailing whitespace display: %s"
           (if
            (setq show-trailing-whitespace
                  (not show-trailing-whitespace))
           "on" "off")))

(defun yank-buffer-file-name (with-line)
  (interactive "P")
  (message (kill-new
            (concat
             (buffer-file-name)
             (if with-line
                 (concat ":" (number-to-string (line-number-at-pos))
                         (if (> (car with-line) 4)
                             (concat "c" (number-to-string (current-column))))))))))

Default Modes and Settings

(defun get-9/80-workday ()
  "See `timeclock-get-workday-function'."
  (* 60 60
     (if (string= (calendar-day-name
                   (calendar-current-date))
                  "Friday")
         8 9)))

(c-add-style "gd-ais" '("linux" (c-basic-offset . 2)))

Keybindings

(global-set-key             (kbd "s-!")        #'shell-command-as-kill)
(global-set-key             (kbd "M-n")        #'menu-bar-open)
(global-set-key             (kbd "C-c o")      #'other-frame)
(global-set-key             (kbd "C-M->")      #'mc/mark-next-like-this)
(global-set-key             (kbd "C->")        #'mc/mark-next-lines)
(global-set-key             (kbd "C-c l")      #'org-store-link)
(global-set-key             (kbd "C-c c")      #'org-capture)
(global-set-key             (kbd "M-<f12>")    (lambda nil (interactive) (org-agenda nil "n")))
(global-set-key             (kbd "s-SPC")      #'just-one-space)
(global-set-key             (kbd "C-x t")      #'toggle-truncate-lines)
(global-set-key             (kbd "M-?")        #'magit-status)
(global-set-key             (kbd "C-c q")      #'auto-fill-mode)
(global-set-key             (kbd "s-v")        #'scroll-up-line)
(global-set-key             (kbd "M-s-v")      #'scroll-down-line)
(global-set-key             (kbd "C-c a")      #'align-regexp)
(global-set-key             (kbd "C-<kp-6>")   #'forward-sexp)
(global-set-key             (kbd "C-<kp-4>")   #'backward-sexp)
(global-set-key             (kbd "C-<kp-2>")   #'down-list)
(global-set-key             (kbd "C-<kp-8>")   #'backward-up-list)
(global-set-key             (kbd "C-M-<kp-6>") #'forward-list)
(global-set-key             (kbd "C-M-<kp-4>") #'backward-list)
(global-set-key             (kbd "<f11>")      #'toggle-fullscreen)
(global-set-key             (kbd "<f12>")      #'yank-buffer-file-name)
(global-set-key             (kbd "C-c L")      #'custom-revert)
(global-set-key             (kbd "s-u")        #'revert-buffer)
(global-set-key             (kbd "C-c s")      #'toggle-show-trailing-whitespace)
(define-key c-mode-base-map (kbd "C-c RET")    #'ff-find-related-file)
(define-key dired-mode-map  (kbd " F")         #'dired-find-all)
(define-key c-mode-map      (kbd "C-c C-'")    #'source-and-compile)
(define-key c++-mode-map    (kbd "C-c C-'")    #'source-and-compile)
(define-key c++-mode-map    (kbd "C-c C-d")    #'doxymacs-insert-function-comment)
(define-key c++-mode-map    (kbd "C-c C-n")    #'insert-comment-guard)

Compiling

Needs list-starts-with General Utilities.

Use shell-file-name to alist commands

(defvar source-and-compile/shell
  "/bin/tcsh"
  "The shell to compile under.")

(defvar source-and-compile/source-directory-command
  "ls ./source_me >& /dev/null && source ./source_me"
  "The command to source a directory (whose relative name is
passed via `format').  This must not fail if the passed
directory exists.")

(defvar source-and-compile/home-directory
  "~/src/current"
  "The base directory for source code.")

(defvar source-and-compile/makefile-files
  '("makefile.make"))

(defvar source-and-compile/make-candidates
  '("mk"
    "make -f makefile.make"
    "make")
  "The make calls in the order they should be tried.")

(defvar source-and-compile/make-in-parallel
  t
  "If t, `source-and-compile/*command' will append the \"--jobs\"
option to each make call.")

(defvar source-and-compile/post-compile-command
  nil
  "a command (or set of commands with ;) to execute after")

(defun source-and-compile/*prepare-candidate (candidate)
  "Uses predefined variables MULTICORE and CLEAN to prepare a
call to make."
  (let ((command (if multicore
                     (concat candidate " --jobs")
                   candidate)))
    (if clean
        (concat command " clean")
      command)))

(defun source-and-compile/*command (absolute-file-path clean multicore)
  "Generates a command for `source-and-compile' that, after
sourcing every 'source_me', traverses upwards in the directory
tree until it finds a makefile.  This command is used as the
`compile-command' for `source-and-compile'."
  (let* ((normalized-command (concat source-and-compile/source-directory-command "; "))
         (path (cdr (butlast (split-string (file-name-directory absolute-file-path) "/"))))
         (root-path (cdr (butlast (split-string (expand-file-name source-and-compile/home-directory) "/")))))
    (if (not (list-starts-with root-path path))
        (error "Not in source directory")
      (let* ((relative-path (nthcdr (length root-path) path))
             (all-source-commands (mapcar (lambda (dir) (concat "cd ./" dir "; " normalized-command)) (cdr relative-path)))
             (single-source-command (apply #'concat all-source-commands))
             (find-make (mapconcat #'identity
                                   (make-list
                                    4;(1- (length relative-path))
                                    (mapconcat (lambda (file) (format "ls %s >&/dev/null" file))
                                               source-and-compile/makefile-files " || ")) " || cd .. && "))
             (make-command (mapconcat (lambda (candidate)
                                        (concat candidate
                                                (if multicore
                                                    " --jobs")
                                                (if clean
                                                    " clean")))
                                      source-and-compile/make-candidates " || ")))
        (concat "cd " source-and-compile/home-directory "; "
                (format normalized-command source-and-compile/home-directory)
                single-source-command
                find-make
                " && exit 1 ; "
                make-command
                " || exit 1")))))

(defun source-and-compile (&optional do-prompt)
  "This function sources every directory in the tree (starting
from 'current') and then uses `compile' to call the first
successful candidate in `source-make-candidates' on the deepest
makefile.make in the structure.

With a prefix argument, configure settings for cleaning and using
multiple cores."
  (interactive "P")
  (let ((file (buffer-file-name)))
    (if (not file)
        (error "Buffer is not visiting a file")
      (let* ((shell-file-name source-and-compile/shell)
             (clean (if do-prompt (y-or-n-p "Clean? ")))
             (multicore (if do-prompt (y-or-n-p "Multicore? ") t))
             (compile-command (concat (source-and-compile/*command file clean multicore)
                                      " ; " source-and-compile/post-compile-command)))
             (call-interactively #'compile)))))

(defun source-and-compile-maybe (arg)
  "Call `source-and-compile' after confirmation.  With a prefix
argument, configure settings for `source-and-compile'."
  (interactive "P")
  (if (y-or-n-p "Compile? ")
      (call-interactively #'source-and-compile)
    (message "Cancelled")))

Dired Extensions

Open all marked files

(defun dired-find-all () (interactive)
  (mapc #'find-file
        (dired-get-marked-files)))

Custom

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

(defun custom-revert ()
  "Loads `custom-file'"
  (interactive)
  (load custom-file))

(add-hook 'after-init-hook #'custom-revert)

Hooks

; (add-hook 'after-change-major-mode-hook 'fci-mode)

(require 'ox)
(eval-after-load 'ox
  '(progn (add-hook 'org-export-before-processing-hook
                    #'my-org-inline-css-hook)))

(defun delete-trailing-whitespace-if-showing ()
  (interactive)
  (if show-trailing-whitespace
      (delete-trailing-whitespace)))

(defun doxymacs-font-lock-for-modes ()
  (if (member major-mode '(c-mode c++-mode))
      (doxymacs-font-lock)))

(add-hook 'before-save-hook
          #'delete-trailing-whitespace-if-showing)

(add-hook 'font-lock-mode-hook
          #'doxymacs-font-lock-for-modes)

Doxymacs

(add-hook 'font-lock-mode-hook
          (lambda () (if (member major-mode '(c-mode c++-mode))
                         (doxymacs-font-lock))))
(add-hook 'c-mode-hook #'doxymacs-mode)
(add-hook 'c++-mode-hook #'doxymacs-mode)

;(font-lock-add-keywords 'c++-mode
;  '(("\\(?:/\\*\\*\\)\\s*\\(.*\\)" . 2)))
;
;(font-lock-add-keywords 'c++-mode
;  '(("///\\(.*\\)" . 1)))

(font-lock-add-keywords 'c++-mode
  '(("//-*" 0 'font-lock-warning-face-prepend)))

(defun insert-comment-guard ()
  (interactive)
  (insert "//" (make-string 78 ?-) "\n"))

(defun *my-doxy-func ()
  (interactive)
  (indent-for-tab-command)
  (insert "//")
  (while (< (current-column) 80) (insert "-"))
  (beginning-of-line)
  (let ((kill-whole-line t)) (kill-line))
  (yank)
  (newline-and-indent)
  (doxymacs-insert-function-comment)
  (save-excursion
    (search-forward "*/")
    (end-of-line)
    (newline)
    (yank)))

Org

(defun my-org-inline-css-hook (exporter)
  "Insert custom inline css.  Include style.css if it exists in
the current directory.  Otherwise, include
.emacs.d/org-style.css."
  (when (eq exporter 'html)
    (let* ((dir (ignore-errors (file-name-directory (buffer-file-name))))
           (path (concat dir "style.css"))
           (homestyle (or (null dir) (null (file-exists-p path))))
           (final (if homestyle "~/.emacs.d/org-style.css" path)))
      (when (file-exists-p final)
            (setq org-html-head-include-default-style nil)
            (setq org-html-head (concat
                                 "<style type=\"text/css\">\n"
                                 "<!--/*--><![CDATA[/*><!--*/\n"
                                 (with-temp-buffer
                                   (insert-file-contents final)
                                   (buffer-string))
                                 "/*]]>*/-->\n"
                                 "</style>\n"))))))

(org-agenda-to-appt)

Load Once

(appt-activate)
(yas-global-mode)
(org-agenda-to-appt)

Magit

(require 'magit)
(defun magit-relate-timeclock-project ()
  (interactive)
  (save-excursion
   (goto-char (point-max))
   (let* ((branch (or (magit-get-current-branch) "default"))
          (header (format "Branch: %s" branch)))
     (unless (string-match header (buffer-string))
       (insert header)))))

(defun magit-branch-to-timeclock-project ()
  (interactive)
  (magit-create-branch (caddr timeclock-last-event) "master"))
(global-set-key (kbd "M-m") #'magit-status)
;(add-hook 'git-commit-mode-hook #'magit-relate-timeclock-project)

m4

(require 'm4-mode)
(font-lock-add-keywords 'm4-mode '(("M_[a-zA-Z0-9_]+" . font-lock-keyword-face)))
(modify-syntax-entry ?# "@" m4-mode-syntax-table)

; (defface font-lock-warning-face
;   '((t :inherit error))
;   "Font Lock mode face used to highlight warnings."
;   :group 'font-lock-faces)
(defface my-code-section-face
  '((t :inherit error))
  "Face used for comment guards")

(defun add-c-syntax-highlighting ()
  (font-lock-add-keywords nil '(
    ("\\(//\\-+\\)" 1 (if t font-lock-warning-face
                        my-code-section-face) prepend))))

God

(eval-after-load 'god-mode
  '(progn
     (defun god-mode-update-cursor ()
       (setq cursor-type (if (or god-local-mode buffer-read-only)
                             'hbar
                           'box)))

     (add-hook 'god-mode-enabled-hook 'god-mode-update-cursor)
     (add-hook 'god-mode-disabled-hook 'god-mode-update-cursor)

     (global-set-key (kbd "<escape>") 'god-local-mode)
     (define-key god-local-mode-map (kbd ".") 'repeat)
     (define-key god-local-mode-map (kbd "i") 'god-local-mode)
     (global-set-key (kbd "C-x C-1") 'delete-other-windows)
     (global-set-key (kbd "C-x C-2") 'split-window-below)
     (global-set-key (kbd "C-x C-3") 'split-window-right)
     ;(global-set-key (kbd "C-x C-0") 'delete-window)
     (add-to-list 'god-exempt-major-modes 'dired-mode)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment