Skip to content

Instantly share code, notes, and snippets.

@afeinberg
Created November 4, 2010 21:05
Show Gist options
  • Select an option

  • Save afeinberg/663190 to your computer and use it in GitHub Desktop.

Select an option

Save afeinberg/663190 to your computer and use it in GitHub Desktop.
(require 'cl)
(defun indent-if-blank ()
(interactive "*")
(progn
(flet ((error (&REST) nil))
(indent-according-to-mode))
(delete-trailing-whitespace-this-line)))
(defun indent-newline-and-indent ()
(interactive "*")
(when (not buffer-read-only)
(indent-if-blank))
(newline-and-indent))
(defun start-of-current-line-or-region ()
"Return the point at the start of the current line or region."
(save-excursion
(if mark-active
(progn
(goto-char (region-beginning))
(point-at-bol))
(point-at-bol))))
(defun end-of-current-line-or-region ()
"Return the point at the end of the current line or region."
(save-excursion
(if mark-active
(progn
(goto-char (region-end))
(point-at-eol))
(point-at-eol))))
(defun delete-trailing-whitespace-this-line ()
"Delete trailing whitespace on the current line."
(interactive "*")
(save-excursion
(goto-char (point-at-eol))
(if (re-search-backward "\\S-\\s-+$" (point-at-bol) t)
(delete-region (+ (match-beginning 0) 1)(point-at-eol)))))
(defun delete-trailing-whitespace-this-region ()
"Delete trailing whitespace on the current line or region."
(interactive "*")
(save-excursion
(goto-char (end-of-current-line-or-region))
(let ((sor (start-of-current-line-or-region)))
(while (and (> (point) sor)
(> (point) (point-min)))
(delete-trailing-whitespace-this-line)
(if (> (line-beginning-position) (point-min))
(previous-line 1)
(goto-char (point-min)))))))
(defun comment-current-line-or-region () (interactive)
"Comment current line or region."
(progn
(comment-region (start-of-current-line-or-region)
(end-of-current-line-or-region))
(end-of-current-line-or-region)
(indent-region (start-of-current-line-or-region)
(end-of-current-line-or-region)
nil)))
(defun uncomment-current-line-or-region () (interactive)
"Uncomment current line or region."
(progn
(comment-region (start-of-current-line-or-region)
(end-of-current-line-or-region) -2)
(end-of-current-line-or-region)
(indent-region (start-of-current-line-or-region)
(end-of-current-line-or-region)
nil)
(delete-trailing-whitespace-this-region)))
(defun toggle-comment-current-line-or-region (arg)
"Toggle comment on current line."
(interactive "*P")
(let ((beg (start-of-current-line-or-region))
(end (end-of-current-line-or-region)))
(if (save-excursion
(goto-char beg)
(Forward-comment 1)
(<= end (point)))
(uncomment-current-line-or-region)
(comment-current-line-or-region))))
(defconst af-c-style
'((c-tab-always-indent .t)
(c-auto-newline .t)
(c-offsets-alist . ((case-label . +))))
"af's C Programming Style")
(defun af-c-mode-common-hook ()
(c-add-style "af-c-style" af-c-style t)
(setq c-default-style "af-c-style"
c-basic-offset 4
tab-width 4
indent-tabs-mode nil)
(local-set-key "\r" 'newline-and-indent))
(add-hook 'c-mode-common-hook 'af-c-mode-common-hook)
(global-set-key [(control c)(\#)] 'toggle-comment-current-line-or-region)
(global-set-key [(meta z)] 'repeat)
(global-set-key [(meta g)] 'goto-lne)
(global-set-key [(control c)(g)] 'goto-line)
(global-set-key [(control c)(control g)] 'goto-line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment