Skip to content

Instantly share code, notes, and snippets.

@ixmatus ixmatus/gist:5043190
Created Feb 26, 2013

Embed
What would you like to do?
(setq-default tab-width 4)
(setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60))
;; Force 'other' modes to use the bsd style tabs (important!!!)
(setq c-default-style
'(((c-mode . "bsd") (c++-mode . "bsd") (other . "bsd"))))
;; Fix the worse part about emacs: indentation craziness
;; 1. When I hit the TAB key, I always want a TAB character inserted
;; 2. Don't automatically indent the line I am editing.
;; 3. When I hit C-j, I always want a newline, plus enough tabs to put me on
;; the same column I was at before.
;; 4. When I hit the BACKSPACE key to the right of a TAB character, I want the
;; TAB character deleted-- not replaced with tabwidth-1 spaces.
(defun newline-and-indent-relative ()
"Insert a newline, then indent relative to the previous line."
(interactive "*") (newline) (indent-relative))
(defun indent-according-to-mode () ())
(defalias 'newline-and-indent 'newline-and-indent-relative)
(defun my-c-hook ()
(defalias 'c-electric-backspace 'delete-backward-char))
; ;(defun c-indent-command () (interactive "*") (self-insert-command 1)))
(add-hook 'c-mode-common-hook 'my-c-hook)
(defun indent-region-with-tab ()
(interactive)
(save-excursion
(if (< (point) (mark)) (exchange-point-and-mark))
(let ((save-mark (mark)))
(if (= (point) (line-beginning-position)) (previous-line 1))
(goto-char (line-beginning-position))
(while (>= (point) save-mark)
(goto-char (line-beginning-position))
(insert "\t")
(previous-line 1)))))
;;(global-set-key [?\C-x tab] 'indent-region-with-tab)
(defun unindent-region-with-tab ()
(interactive)
(save-excursion
(if (< (point) (mark)) (exchange-point-and-mark))
(let ((save-mark (mark)))
(if (= (point) (line-beginning-position)) (previous-line 1))
(goto-char (line-beginning-position))
(while (>= (point) save-mark)
(goto-char (line-beginning-position))
(if (= (string-to-char "\t") (char-after (point))) (delete-char 1))
(previous-line 1)))))
;; Define the tab-stop list - important for proper indentation
(defun my-build-tab-stop-list (width)
(let ((num-tab-stops (/ 80 width))
(counter 1)
(ls nil))
(while (<= counter num-tab-stops)
(setq ls (cons (* width counter) ls))
(setq counter (1+ counter)))
(set (make-local-variable 'tab-stop-list) (nreverse ls))))
;; Here's the programming indentation magic - define all the special indenting rules
(defun my-c-mode-common-hook ()
(setq abbrev-mode -1)
(setq tab-width 4)
(setq c-basic-offset 4)
(c-set-offset 'topmost-intro-cont 4)
(c-set-offset 'class-open 0)
(c-set-offset 'inline-open 0)
(c-set-offset 'substatement-open 0)
(c-set-offset 'arglist-intro 0)
(c-set-offset 'arglist-close 0)
(c-set-offset 'defun-block-intro tab-width)
(c-set-offset 'defun-close 0)
(my-build-tab-stop-list tab-width))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.