general.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. (provide 'general)
  2. ;; ------
  3. ;; Require misc stuff
  4. ;; ------
  5. ;(require 'nxml-mode)
  6. ;(require 'python)
  7. ;(require 'ruby-mode)
  8. (require 'epa-file)
  9. (require 'ibuffer)
  10. (epa-file-enable)
  11. ;; ------
  12. ;; Place backups in ~/.backups/ directory, like a civilized program.
  13. ;; ------
  14. (defvar backup-dir (expand-file-name "~/.backup/"))
  15. ;(defvar backup-dir (concat "~/.backup/" "/"))
  16. ;; Create directory if no exist
  17. (when (and (not (file-directory-p backup-dir)))
  18. (make-directory backup-dir t))
  19. ;; Defined backup directory
  20. (setq backup-directory-alist (list (cons ".*" backup-dir)))
  21. (setq auto-save-list-file-prefix backup-dir)
  22. ;(setq auto-save-file-name-transforms '((".*" ,backup-dir t)))
  23. (setq backup-by-copying t ; Don't delink hardlinks
  24. delete-old-versions t ; Clean up the backups
  25. version-control t ; Use version numbers on backups,
  26. kept-new-versions 3 ; keep some new versions
  27. kept-old-versions 2) ; and some old ones, too
  28. ;; Make backups of files, even when they're in version control
  29. (setq vc-make-backup-files t)
  30. ;; Delete old backups
  31. (if (file-directory-p backup-dir)
  32. (message "Deleting old backup files...")
  33. (let ((week (* 60 60 24 7))
  34. (current (float-time (current-time))))
  35. (dolist (file (directory-files backup-dir t))
  36. (when (and (backup-file-name-p file)
  37. (> (- current (float-time (fifth (file-attributes file))))
  38. week))
  39. (message "%s" file)
  40. (delete-file file))))
  41. )
  42. ;; Save point position between sessions
  43. (require 'saveplace)
  44. (setq-default save-place t)
  45. (setq save-place-file (expand-file-name ".places" user-emacs-directory))
  46. ;; ---------
  47. ;; Make debian/ubuntu work nicely with cvs emacs
  48. ;; ---------
  49. ;; (let ((startup-file "/usr/share/emacs/site-lisp/debian-startup.el"))
  50. ;; (if (and (or (not (fboundp 'debian-startup))
  51. ;; (not (boundp 'debian-emacs-flavor)))
  52. ;; (file-readable-p startup-file))
  53. ;; (progn
  54. ;; (load-file startup-file)
  55. ;; (setq debian-emacs-flavor 'emacs22)
  56. ;; (debian-startup debian-emacs-flavor)
  57. ;; (mapcar '(lambda (f)
  58. ;; (and (not (string= (substring f -3) "/.."))
  59. ;; (file-directory-p f)
  60. ;; (add-to-list 'load-path f)))
  61. ;; (directory-files "/usr/share/emacs/site-lisp" t)))))
  62. ;; ------
  63. ;; General config BS
  64. ;; ------
  65. (setq fill-column 79)
  66. (recentf-mode 1)
  67. (setq pop-up-frames nil)
  68. (defalias 'yes-or-no-p 'y-or-n-p)
  69. (setq max-specpdl-size 9000)
  70. (setq font-lock-maximum-decoration t
  71. color-theme-is-global t
  72. truncate-partial-width-windows nil)
  73. ;;Delete trailing space
  74. (add-hook 'before-save-hook 'delete-trailing-whitespace)
  75. ;; Resaltado linea
  76. (global-hl-line-mode t)
  77. ;; Custom values CLI/GUI
  78. (defun custom-cli (frame)
  79. (select-frame frame)
  80. (if (window-system frame)
  81. (progn
  82. (set-face-background hl-line-face "gray13") )
  83. (progn
  84. (set-face-background hl-line-face "green")
  85. (set-cursor-color "cyan")
  86. (set-background-color "Black")
  87. (set-foreground-color "White")
  88. (set-border-color "dark orange")
  89. (set-mouse-color "Cyan")
  90. )))
  91. (add-hook 'after-make-frame-functions 'custom-cli)
  92. ;; Semantic | CEDET
  93. ;(semantic-mode 1)
  94. ;(global-semantic-idle-completions-mode t)
  95. ;(global-semantic-decoration-mode t)
  96. ;(global-semantic-highlight-func-mode t)
  97. ;(global-semantic-show-unmatched-syntax-mode t)
  98. ; winner mode
  99. (winner-mode 1)
  100. ;;(set-window-dedicated-p (selected-window) t)
  101. (when (not window-system)
  102. ;;allow you to see the region when in console mode
  103. (setq transient-mark-mode t))
  104. ;; Shell stuff
  105. (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
  106. (setq shell-file-name "/bin/bash")
  107. (setq explicit-shell-file-name "/bin/bash")
  108. (setq ispell-alternate-dictionary "/etc/dictionaries-common/words")
  109. (setq diary-file "~/org/diary")
  110. (setq tex-dvi-view-command
  111. (if (eq window-system 'x) "xdvi" "dvi2tty * | cat -s"))
  112. ; tab auto-completion cycling is evil.
  113. (setq pcomplete-cycle-completions nil)
  114. ;; Make sure that pressing middle mouse button pastes right at point,
  115. ;; not where the mouse cursor is.
  116. (setq mouse-yank-at-point t)
  117. ;; Don't show my password when I'm entering it, kthx.
  118. (add-hook 'comint-output-filter-functions
  119. 'comint-watch-for-password-prompt)
  120. (put 'downcase-region 'disabled nil)
  121. (put 'narrow-to-region 'disabled nil)
  122. (put 'upcase-region 'disabled nil)
  123. (setq inhibit-splash-screen t)
  124. (setq visual-line-fringe-indicators '(t t))
  125. ; Don't switch to another frame with iswitchb
  126. (setq iswitchb-default-method 'samewindow)
  127. ; Use diff -u
  128. (setq diff-switches "-u")
  129. ;; ------
  130. ;; Initialize some things
  131. ;; ------
  132. ; (display-time)
  133. (server-start)
  134. ;; Mouse scrolling
  135. (mwheel-install)
  136. ;; Don't minimize my emacs! Honestly wtf
  137. (when window-system
  138. (progn
  139. (setq scroll-bar-mode nil)
  140. (tool-bar-mode nil)
  141. (menu-bar-mode nil)))
  142. ;; ---------
  143. ;; Custom funcs
  144. ;; ---------
  145. (defun other-window-backward (&optional n)
  146. "Select Nth previous window."
  147. (interactive "P")
  148. (other-window (- (prefix-numeric-value n))))
  149. (defun warn-if-symlink ()
  150. (if (file-symlink-p buffer-file-name)
  151. ;progn here to execute both as part of else statement together
  152. (message "File is a symlink")))
  153. (add-hook 'find-file-hooks 'warn-if-symlink)
  154. ;; creating a scratch buffer command
  155. (defun create-scratch-buffer nil
  156. "create a scratch buffer"
  157. (interactive)
  158. (switch-to-buffer (get-buffer-create "*scratch*"))
  159. (lisp-interaction-mode)
  160. (insert ";; This buffer is for notes you don't want to save, and for Lisp evaluation.
  161. ;; If you want to create a file, visit that file with C-x C-f,
  162. ;; then enter the text in that file's own buffer.
  163. "))
  164. ;; If the *scratch* buffer is killed, recreate it automatically
  165. ;; FROM: Morten Welind
  166. ;;http://www.geocrawler.com/archives/3/338/1994/6/0/1877802/
  167. (save-excursion
  168. (set-buffer (get-buffer-create "*scratch*"))
  169. (lisp-interaction-mode)
  170. (make-local-variable 'kill-buffer-query-functions)
  171. (add-hook 'kill-buffer-query-functions 'kill-scratch-buffer))
  172. (defun kill-scratch-buffer ()
  173. ;; The next line is just in case someone calls this manually
  174. (set-buffer (get-buffer-create "*scratch*"))
  175. ;; Kill the current (*scratch*) buffer
  176. (remove-hook 'kill-buffer-query-functions 'kill-scratch-buffer)
  177. (kill-buffer (current-buffer))
  178. ;; Make a brand new *scratch* buffer
  179. (set-buffer (get-buffer-create "*scratch*"))
  180. (lisp-interaction-mode)
  181. (make-local-variable 'kill-buffer-query-functions)
  182. (add-hook 'kill-buffer-query-functions 'kill-scratch-buffer)
  183. ;; Since we killed it, don't let caller do that.
  184. nil)
  185. ;; Highlight previous (or current?) line
  186. (defun pg-uline (ulinechar)
  187. "Underline the current or the previous line with ULINECHAR"
  188. (interactive "cUnderline with:")
  189. (if (looking-at "^$")
  190. (next-line -1))
  191. (end-of-line)
  192. (let ((linelen (current-column)))
  193. (insert "\n")
  194. (while (> linelen 0)
  195. (setq linelen (1- linelen))
  196. (insert ulinechar)))
  197. (insert "\n"))
  198. (global-set-key (kbd "C-c u") 'pg-uline)
  199. ;; Swapping buffers! Can't live without this
  200. (setq cwebber/swapping-buffer nil)
  201. (setq cwebber/swapping-window nil)
  202. (defun cwebber/swap-buffers-in-windows ()
  203. "Swap buffers between two windows"
  204. (interactive)
  205. (if (and cwebber/swapping-window
  206. cwebber/swapping-buffer)
  207. (let ((this-buffer (current-buffer))
  208. (this-window (selected-window)))
  209. (if (and (window-live-p cwebber/swapping-window)
  210. (buffer-live-p cwebber/swapping-buffer))
  211. (progn (switch-to-buffer cwebber/swapping-buffer)
  212. (select-window cwebber/swapping-window)
  213. (switch-to-buffer this-buffer)
  214. (select-window this-window)
  215. (message "Swapped buffers."))
  216. (message "Old buffer/window killed. Aborting."))
  217. (setq cwebber/swapping-buffer nil)
  218. (setq cwebber/swapping-window nil))
  219. (progn
  220. (setq cwebber/swapping-buffer (current-buffer))
  221. (setq cwebber/swapping-window (selected-window))
  222. (message "Buffer and window marked for swapping."))))
  223. (global-set-key (kbd "C-c p") 'cwebber/swap-buffers-in-windows)
  224. ;; other stuff
  225. (defun add-spaces-to-region (beginning end numspaces)
  226. "Add spaces to a whole region of text"
  227. (interactive "r\nnNumber of spaces: ")
  228. (save-excursion
  229. (goto-char beginning)
  230. (beginning-of-line)
  231. (while (< (point) end)
  232. (let ((bol-point 0)
  233. (eol-point 0))
  234. (save-excursion
  235. (end-of-line)
  236. (setq eol-point (point))
  237. (beginning-of-line)
  238. (setq bol-point (point)))
  239. (if (not (equal bol-point eol-point))
  240. (progn
  241. (beginning-of-line)
  242. (dotimes (i numspaces)
  243. (insert " ")))))
  244. (forward-line)
  245. (beginning-of-line))))
  246. (defun rename-buffer-with-directory (&optional arg)
  247. "Useful for when you're dealing with multiple files with the
  248. same name in different directories. No more file.txt<2>!
  249. Running this will append the previous directory to the end of
  250. the filename. So for example, if you opened the emacs
  251. ChangeLog (living in the emacs/ directory), you'll get
  252. 'ChangeLog(emacs/)'. Using a prefix arg will give you number
  253. of subdirectories, if you need it.
  254. If you are accessing a file over Tramp, it will add 'host:' to
  255. the parenthesis.. so if you were accessing
  256. /ssh:example.org:/home/foobar/emacs/ChangeLog, you'd get:
  257. 'ChangeLog(example.org:emacs/)'"
  258. (interactive "^p")
  259. (let ((dir-name nil)
  260. (split-path (eshell-split-path
  261. (file-name-directory (buffer-file-name))))
  262. (tramp-data (condition-case nil
  263. (tramp-dissect-file-name (buffer-file-name))
  264. (error nil))))
  265. (setq dir-name
  266. (apply 'concat (nthcdr (- (length split-path) arg) split-path)))
  267. (rename-buffer
  268. (concat (file-name-nondirectory (buffer-file-name))
  269. (if tramp-data
  270. (concat
  271. "(" (aref tramp-data 2) ":" dir-name ")")
  272. (concat "(" dir-name ")"))))))
  273. (defun insert-virtualenv-load-line (virtualenv-dir)
  274. (interactive "DVirtualenv directory: ")
  275. (let ((activate-file (expand-file-name (concat virtualenv-dir "./bin/activate_this.py"))))
  276. (if (file-exists-p activate-file)
  277. (insert
  278. (concat "execfile('" activate-file
  279. "', dict(__file__='" activate-file "'))"))
  280. (error "No ./bin/activate_this.py in that virtualenv (maybe update virtualenv?)"))))
  281. ; (global-set-key (kbd "C-c r") 'rename-buffer-with-directory)
  282. (defun blender-style ()
  283. (interactive)
  284. (c-set-style "bsd")
  285. (setq indent-tabs-mode nil)
  286. (setq tab-width 4)
  287. (setq c-basic-offset 4))
  288. ;; En1arg3 y0ur w1|\|dow!!!
  289. (defun undo-or-shrink-horizontally ()
  290. "Either undo or shrink horizontally, depending on whether we're
  291. in X or in a terminal"
  292. (interactive)
  293. (if (window-system)
  294. (shrink-window-horizontally)
  295. (undo)))
  296. ;; Apparently I'm a crufty old timer who likes the way the old mouse
  297. ;; and x11 pasting worked. This sets it back
  298. (global-set-key [mouse-2] 'mouse-yank-at-click)
  299. (setq mouse-drag-copy-region nil
  300. select-active-regions nil
  301. x-select-enable-primary t
  302. x-select-enable-clipboard t)
  303. ;; ----------
  304. ;; Mail stuff
  305. ;; ----------
  306. (setq mail-source-movemail-program "/usr/bin/movemail")
  307. ; Randomly choose mail signature
  308. (setq cwebber-mail-sigs
  309. '("----------------------"
  310. "http://distopico.info/"
  311. "𝓒𝓱𝓻𝓲𝓼𝓽𝓸𝓹𝓱𝓮𝓻 𝓐𝓵𝓵𝓪𝓷 𝓦𝓮𝓫𝓫𝓮𝓻"))
  312. (defun cwebber-random-mail-sig ()
  313. (nth (random (length cwebber-mail-sigs))
  314. cwebber-mail-sigs))
  315. ;(setq mail-signature 'cwebber-random-mail-sig)
  316. ;(setq message-signature 'cwebber-random-mail-sig)
  317. (setq mail-signature nil)
  318. (setq message-signature nil)
  319. ;; ---------
  320. ;; Load some custom stuff
  321. ;; ---------
  322. (defun insert-ellipsis ()
  323. (interactive)
  324. (insert "…"))
  325. (global-set-key (kbd "C-c ;") 'insert-ellipsis)
  326. ; Always compose mail with gnus. *ALWAYS*.
  327. (setq read-mail-command 'gnus)
  328. (setq mail-user-agent 'gnus-user-agent)
  329. ; Turn off tool-bar-mode, which is slow
  330. (call-interactively 'tool-bar-mode)
  331. ;; UTF-8 please
  332. (setq locale-coding-system 'utf-8) ; pretty
  333. (set-terminal-coding-system 'utf-8) ; pretty
  334. (set-keyboard-coding-system 'utf-8) ; pretty
  335. (set-selection-coding-system 'utf-8) ; please
  336. (prefer-coding-system 'utf-8) ; with sugar on top
  337. (setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING))
  338. ; lock files borking git-annex-assistant autocommits. Disabling for now.
  339. ;; We can re-enable both of these once .gitignore support comes to git-annex
  340. (setq create-lockfiles nil)
  341. ; Similarly with auto-save files :(
  342. (setq auto-save-default nil)
  343. ;; Undo Redo
  344. ;;(load-file "~/.emacs.d/modes/undo-tree.el")
  345. (require 'undo-tree)
  346. (global-undo-tree-mode 1)
  347. ;; Cua Copy/Paste
  348. ;(cua-mode t)
  349. ;(setq cua-auto-tabify-rectangles nil)
  350. ;(transient-mark-mode 1)
  351. ;(setq cua-keep-region-after-copy t)
  352. ;(cua-selection-mode t)
  353. ;; Shutdown Server
  354. ;; define function to shutdown emacs server instance
  355. (defun shutdown ()
  356. "Save buffers, Quit, and Shutdown (kill) server"
  357. (interactive)
  358. (save-some-buffers)
  359. (kill-emacs)
  360. )
  361. ;; Allow recursive minibuffers
  362. (setq enable-recursive-minibuffers t)
  363. ;; Show me empty lines after buffer end
  364. (set-default 'indicate-empty-lines t)
  365. ;; Show active region
  366. (transient-mark-mode 1)
  367. (make-variable-buffer-local 'transient-mark-mode)
  368. (put 'transient-mark-mode 'permanent-local t)
  369. (setq-default transient-mark-mode t)
  370. ;; Remove text in active region if inserting text
  371. (delete-selection-mode 1)
  372. ;; Transparently open compressed files
  373. (auto-compression-mode t)
  374. ;; Move files to trash when deleting
  375. (setq delete-by-moving-to-trash t)
  376. ;; Show keystrokes in progress
  377. (setq echo-keystrokes 0.1)
  378. ;; Auto refresh buffers
  379. (global-auto-revert-mode t)
  380. ;; Also auto refresh dired, but be quiet about it
  381. (setq global-auto-revert-non-file-buffers t)
  382. (setq auto-revert-verbose nil)
  383. ;; Don't break lines for me, please
  384. (setq-default truncate-lines t)
  385. ;; Compiled lips files
  386. (defun byte-compile-init-dir ()
  387. "Byte-compile all your dotfiles."
  388. (interactive)
  389. (byte-recompile-directory user-emacs-directory 0))
  390. (defun remove-elc-on-save ()
  391. "If you're saving an elisp file, likely the .elc is no longer valid."
  392. (add-hook 'after-save-hook
  393. (lambda ()
  394. (if (file-exists-p (concat buffer-file-name "c"))
  395. (delete-file (concat buffer-file-name "c"))))
  396. nil
  397. t))
  398. (add-hook 'emacs-lisp-mode-hook 'remove-elc-on-save)