You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1606 lines
54 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Commentary:
  3. ;;; Code:
  4. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  5. ;; SETUP_LOAD: (and (file-readable-p file)
  6. ;; SETUP_LOAD: (load-file file)))
  7. (setq debug-on-error t)
  8. ;; make directories
  9. (unless (file-directory-p (expand-file-name user-emacs-directory))
  10. (make-directory (expand-file-name user-emacs-directory)))
  11. (require 'cl-lib)
  12. (require 'simple)
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14. ;; Some macros for internals
  15. ;; `emacs --load emacs.el` with Emacs 24.3 requires with-eval-after-load to be
  16. ;; defined at the toplevel (means that it should not be defined inside of some
  17. ;; special forms like `when'. I do not now how to do with about this...)
  18. (unless (fboundp 'with-eval-after-load)
  19. ;; polyfill for Emacs < 24.4
  20. (defmacro with-eval-after-load (file &rest body)
  21. "After FILE is loaded execute BODY."
  22. (declare (indent 1) (debug t))
  23. `(eval-after-load ,file (quote (progn ,@body)))))
  24. (defun call-after-init (func)
  25. "If `after-init-hook' has been run, call FUNC immediately.
  26. Otherwize hook it."
  27. (if after-init-time
  28. (funcall func)
  29. (add-hook 'after-init-hook
  30. func)))
  31. (defmacro safe-require-or-eval (feature)
  32. "Require FEATURE if available.
  33. At compile time the feature will be loaded immediately."
  34. `(eval-and-compile
  35. (message "safe-require-or-eval: Trying to require %s" ,feature)
  36. (require ,feature nil t)))
  37. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  38. "Define autoloading FEATURE that defines FUNCTIONS.
  39. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  40. the function same as FEATURE is defined as autoloaded function. BODY is passed
  41. to `eval-after-load'.
  42. After this macro is expanded, this returns the path to library if FEATURE
  43. found, otherwise returns nil."
  44. (declare (indent 2) (debug t))
  45. (let* ((libname (symbol-name (eval feature)))
  46. (libpath (locate-library libname)))
  47. `(progn
  48. (when (locate-library ,libname)
  49. ,@(mapcar (lambda (f)
  50. `(unless (fboundp ',f)
  51. (progn
  52. (message "Autoloaded function `%S' defined (%s)"
  53. (quote ,f)
  54. ,libpath)
  55. (autoload (quote ,f)
  56. ,libname
  57. ,(concat "Autoloaded function defined in \""
  58. libpath
  59. "\".")
  60. t))))
  61. (or (eval functions)
  62. `(,(eval feature)))))
  63. (eval-after-load ,feature
  64. (quote (progn
  65. ,@body)))
  66. (locate-library ,libname))))
  67. (when (autoload-eval-lazily 'tetris nil
  68. (message "Tetris loaded!"))
  69. (message "Tetris found!"))
  70. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  71. ;; package
  72. (set (defvar 10sr-package-list)
  73. '(
  74. vimrc-mode
  75. markdown-mode
  76. yaml-mode
  77. gnuplot-mode
  78. php-mode
  79. erlang
  80. js2-mode
  81. js-doc
  82. git-commit
  83. gitignore-mode
  84. adoc-mode
  85. malabar-mode
  86. ;; ack
  87. color-moccur
  88. ggtags
  89. flycheck
  90. auto-highlight-symbol
  91. hl-todo
  92. pp-c-l
  93. ;; is flymake installs are required?
  94. ;;flymake-jshint
  95. ;;flymake-python-pyflakes
  96. xclip
  97. foreign-regexp
  98. multi-term
  99. term-run
  100. editorconfig
  101. git-ps1-mode
  102. restart-emacs
  103. fill-column-indicator
  104. pkgbuild-mode
  105. minibuffer-line
  106. which-key
  107. ;; I think this works in place of my autosave lib
  108. super-save
  109. scala-mode
  110. ;;ensime
  111. editorconfig
  112. editorconfig-custom-majormode
  113. cyberpunk-theme
  114. grandshell-theme
  115. afternoon-theme
  116. git-command
  117. prompt-text
  118. ;; 10sr repository
  119. ;; 10sr-extras
  120. terminal-title
  121. recentf-show
  122. dired-list-all-mode
  123. pack
  124. set-modeline-color
  125. read-only-only-mode
  126. smart-revert
  127. autosave
  128. ;;window-organizer
  129. remember-major-modes-mode
  130. ilookup
  131. pasteboard
  132. end-mark
  133. sl
  134. gosh-mode
  135. ))
  136. (when (safe-require-or-eval 'package)
  137. (setq package-archives
  138. `(,@package-archives
  139. ("melpa" . "https://melpa.org/packages/")
  140. ;; Somehow fails to download via https
  141. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  142. (package-initialize)
  143. (defun my-auto-install-package ()
  144. "Install packages semi-automatically."
  145. (interactive)
  146. (package-refresh-contents)
  147. (mapc (lambda (pkg)
  148. (or (package-installed-p pkg)
  149. (package-install pkg)))
  150. 10sr-package-list))
  151. )
  152. ;; (lazy-load-eval 'sudoku)
  153. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  154. ;; my-idle-hook
  155. (defvar my-idle-hook nil
  156. "Hook run when idle for several secs.")
  157. (defvar my-idle-hook-sec 5
  158. "Second to run `my-idle-hook'.")
  159. (run-with-idle-timer my-idle-hook-sec
  160. t
  161. (lambda ()
  162. (run-hooks 'my-idle-hook)))
  163. ;; (add-hook 'my-idle-hook
  164. ;; (lambda ()
  165. ;; (message "idle hook message")))
  166. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  167. ;; start and quit
  168. (setq inhibit-startup-message t)
  169. (setq confirm-kill-emacs 'y-or-n-p)
  170. (setq gc-cons-threshold (* 1024 1024 4))
  171. (when window-system
  172. (add-to-list 'default-frame-alist '(cursor-type . box))
  173. (add-to-list 'default-frame-alist '(background-color . "white"))
  174. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  175. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  176. ;; does not work?
  177. )
  178. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  179. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  180. (and (fboundp 'tool-bar-mode)
  181. (tool-bar-mode 0))
  182. (and (fboundp 'set-scroll-bar-mode)
  183. (set-scroll-bar-mode nil))
  184. (add-hook 'kill-emacs-hook
  185. ;; load init file when terminating emacs to ensure file is not broken
  186. 'reload-init-file)
  187. (defun my-force-kill-emacs ()
  188. "My force kill Emacs."
  189. (interactive)
  190. (let ((kill-emacs-hook nil))
  191. (kill-emacs)))
  192. (call-after-init
  193. (lambda ()
  194. (message "%s %s" invocation-name emacs-version)
  195. (message "Invocation directory: %s" default-directory)
  196. (message "%s was taken to initialize emacs." (emacs-init-time))
  197. (switch-to-buffer "*Messages*")))
  198. (cd ".") ; when using windows use / instead of \ in `default-directory'
  199. ;; locale
  200. (set-language-environment "Japanese")
  201. (set-default-coding-systems 'utf-8-unix)
  202. (prefer-coding-system 'utf-8-unix)
  203. (setq system-time-locale "C")
  204. ;; my prefix map
  205. (defvar my-prefix-map nil
  206. "My prefix map.")
  207. (define-prefix-command 'my-prefix-map)
  208. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  209. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  210. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  211. ;; (comint-show-maximum-output)
  212. ;; kill scratch
  213. (call-after-init (lambda ()
  214. (let ((buf (get-buffer "*scratch*")))
  215. (when buf
  216. (kill-buffer buf)))))
  217. ;; modifier keys
  218. ;; (setq mac-option-modifier 'control)
  219. ;; display
  220. (setq visible-bell t)
  221. (setq ring-bell-function 'ignore)
  222. (mouse-avoidance-mode 'banish)
  223. (setq echo-keystrokes 0.1)
  224. (defun reload-init-file ()
  225. "Reload Emacs init file."
  226. (interactive)
  227. (when (and user-init-file
  228. (file-readable-p user-init-file))
  229. (load-file user-init-file)))
  230. (safe-require-or-eval 'session)
  231. ;; server
  232. (set-variable 'server-name (concat "server"
  233. (number-to-string (emacs-pid))))
  234. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  235. ;; In Darwin environment, init fails with message like 'Service name too long'
  236. ;; when server-use-tcp is nil
  237. (when (or (eq system-type
  238. 'cygwin)
  239. (eq system-type
  240. 'darwin))
  241. (set-variable 'server-use-tcp t))
  242. ;; MSYS2 fix
  243. (when (eq system-type
  244. 'windows-nt)
  245. (setq shell-file-name
  246. (executable-find "bash"))
  247. '(setq function-key-map
  248. `(,@function-key-map ([pause] . [?\C-c])
  249. ))
  250. (define-key key-translation-map
  251. (kbd "<pause>")
  252. (kbd "C-c"))
  253. '(keyboard-translate [pause]
  254. (kbd "C-c")p)
  255. ;; TODO: move to other place later
  256. (when (not window-system)
  257. (setq interprogram-paste-function nil)
  258. (setq interprogram-cut-function nil)))
  259. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  260. ;; global keys
  261. (global-set-key (kbd "<up>") 'scroll-down-line)
  262. (global-set-key (kbd "<down>") 'scroll-up-line)
  263. (global-set-key (kbd "<left>") 'scroll-down)
  264. (global-set-key (kbd "<right>") 'scroll-up)
  265. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  266. (global-set-key (kbd "C-\\") help-map)
  267. (define-key ctl-x-map (kbd "DEL") help-map)
  268. (define-key ctl-x-map (kbd "C-h") help-map)
  269. (define-key help-map "a" 'apropos)
  270. ;; disable annoying keys
  271. (global-set-key [prior] 'ignore)
  272. (global-set-key (kbd "<next>") 'ignore)
  273. (global-set-key [menu] 'ignore)
  274. (global-set-key [down-mouse-1] 'ignore)
  275. (global-set-key [down-mouse-2] 'ignore)
  276. (global-set-key [down-mouse-3] 'ignore)
  277. (global-set-key [mouse-1] 'ignore)
  278. (global-set-key [mouse-2] 'ignore)
  279. (global-set-key [mouse-3] 'ignore)
  280. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  281. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  282. (when (safe-require-or-eval 'which-key)
  283. (which-key-mode))
  284. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  285. ;; editting
  286. (defun my-copy-whole-line ()
  287. "Copy whole line."
  288. (interactive)
  289. (kill-new (concat (buffer-substring (point-at-bol)
  290. (point-at-eol))
  291. "\n")))
  292. (setq require-final-newline t)
  293. (setq kill-whole-line t)
  294. (setq scroll-conservatively 35
  295. scroll-margin 2
  296. scroll-step 0)
  297. (setq-default major-mode 'text-mode)
  298. (setq next-line-add-newlines nil)
  299. (setq kill-read-only-ok t)
  300. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  301. ;; (setq-default line-spacing 0.2)
  302. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  303. (setq-default tab-width 4)
  304. (setq-default indent-tabs-mode nil)
  305. (setq-default indent-line-function 'indent-to-left-margin)
  306. ;; (setq-default indent-line-function nil)
  307. (setq-default truncate-lines nil)
  308. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  309. (delete-selection-mode 1)
  310. (cua-mode 0)
  311. (setq line-move-visual nil)
  312. ;; key bindings
  313. ;; moving around
  314. ;; (global-set-key (kbd "M-j") 'next-line)
  315. ;; (global-set-key (kbd "M-k") 'previous-line)
  316. ;; (global-set-key (kbd "M-h") 'backward-char)
  317. ;; (global-set-key (kbd "M-l") 'forward-char)
  318. ;;(keyboard-translate ?\M-j ?\C-j)
  319. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  320. (define-key esc-map "p" 'backward-paragraph)
  321. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  322. (define-key esc-map "n" 'forward-paragraph)
  323. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  324. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  325. (global-set-key (kbd "C-<left>") 'scroll-down)
  326. (global-set-key (kbd "C-<right>") 'scroll-up)
  327. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  328. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  329. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  330. ;; C-h and DEL
  331. (global-set-key (kbd "C-h") (kbd "DEL"))
  332. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  333. (global-set-key (kbd "C-m") 'newline-and-indent)
  334. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  335. (define-key esc-map "k" 'my-copy-whole-line)
  336. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  337. (define-key esc-map "u" 'undo)
  338. (define-key esc-map "i" (kbd "ESC TAB"))
  339. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  340. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  341. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  342. (define-key my-prefix-map (kbd "C-o") 'occur)
  343. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  344. ;; title and mode-line
  345. (when (safe-require-or-eval 'terminal-title)
  346. ;; if TERM is not screen use default value
  347. (if (getenv "TMUX")
  348. ;; if use tmux locally just basename of current dir
  349. (set-variable 'terminal-title-format
  350. '((file-name-nondirectory (directory-file-name
  351. default-directory))))
  352. (if (and (let ((tty-type (frame-parameter nil
  353. 'tty-type)))
  354. (and tty-type
  355. (equal (car (split-string tty-type
  356. "-"))
  357. "screen")))
  358. (not (getenv "SSH_CONNECTION")))
  359. (set-variable 'terminal-title-format
  360. '((file-name-nondirectory (directory-file-name
  361. default-directory))))
  362. ;; seems that TMUX is used locally and ssh to remote host
  363. (set-variable 'terminal-title-format
  364. `("em:"
  365. ,user-login-name
  366. "@"
  367. ,(car (split-string (system-name)
  368. "\\."))
  369. ":"
  370. default-directory))
  371. )
  372. )
  373. (terminal-title-mode))
  374. (setq eol-mnemonic-dos "\\r\\n")
  375. (setq eol-mnemonic-mac "\\r")
  376. (setq eol-mnemonic-unix "\\n")
  377. (which-function-mode 0)
  378. (line-number-mode 0)
  379. (column-number-mode 0)
  380. (size-indication-mode 0)
  381. (setq mode-line-position
  382. '(:eval (format "L%%l/%d,C%%c"
  383. (count-lines (point-max)
  384. (point-min)))))
  385. (when (safe-require-or-eval 'git-ps1-mode)
  386. (git-ps1-mode))
  387. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  388. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  389. ;; minibuffer
  390. (setq insert-default-directory t)
  391. (setq completion-ignore-case t
  392. read-file-name-completion-ignore-case t
  393. read-buffer-completion-ignore-case t)
  394. (setq resize-mini-windows t)
  395. (temp-buffer-resize-mode 1)
  396. (savehist-mode 1)
  397. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  398. (fset 'yes-or-no-p 'y-or-n-p)
  399. ;; complete symbol when `eval'
  400. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  401. (define-key minibuffer-local-map (kbd "C-u")
  402. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  403. ;; I dont know these bindings are good
  404. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  405. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  406. (when (safe-require-or-eval 'minibuffer-line)
  407. (set-face-underline 'minibuffer-line nil)
  408. (set-variable 'minibuffer-line-refresh-interval
  409. 25)
  410. (set-variable 'minibuffer-line-format
  411. `(,(concat user-login-name
  412. "@"
  413. (car (split-string (system-name)
  414. "\\."))
  415. ":")
  416. (:eval (abbreviate-file-name (or buffer-file-name
  417. default-directory)))
  418. (:eval (and (fboundp 'git-ps1-mode-get-current)
  419. (git-ps1-mode-get-current " [GIT:%s]")))
  420. " "
  421. (:eval (format-time-string display-time-format))))
  422. (minibuffer-line-mode 1)
  423. )
  424. (when (safe-require-or-eval 'prompt-text)
  425. (set-variable 'prompt-text-format
  426. `(,(concat ""
  427. user-login-name
  428. "@"
  429. (car (split-string (system-name)
  430. "\\."))
  431. ":")
  432. (:eval (abbreviate-file-name (or buffer-file-name
  433. default-directory)))
  434. (:eval (and (fboundp 'git-ps1-mode-get-current)
  435. (git-ps1-mode-get-current " [GIT:%s]")))
  436. " "
  437. (:eval (format-time-string display-time-format))
  438. "\n"
  439. (:eval (symbol-name this-command))
  440. ": "))
  441. (prompt-text-mode 1))
  442. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  443. ;; letters, font-lock mode and fonts
  444. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  445. ;; (set-window-margins (selected-window) 1 1)
  446. (and (or (eq system-type 'Darwin)
  447. (eq system-type 'darwin))
  448. (fboundp 'mac-set-input-method-parameter)
  449. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  450. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  451. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  452. (boundp 'input-method-inactivate-hook))
  453. (add-hook 'input-method-activate-hook
  454. (lambda () (set-cursor-color "red")))
  455. (add-hook 'input-method-inactivate-hook
  456. (lambda () (set-cursor-color "black"))))
  457. (when (safe-require-or-eval 'paren)
  458. (show-paren-mode 1)
  459. (setq show-paren-delay 0.5
  460. show-paren-style 'parenthesis) ; mixed is hard to read
  461. ;; (set-face-background 'show-paren-match
  462. ;; "black")
  463. ;; ;; (face-foreground 'default))
  464. ;; (set-face-foreground 'show-paren-match
  465. ;; "white")
  466. ;; (set-face-inverse-video-p 'show-paren-match
  467. ;; t)
  468. )
  469. (transient-mark-mode 1)
  470. (global-font-lock-mode 1)
  471. (setq font-lock-global-modes
  472. '(not
  473. help-mode
  474. eshell-mode
  475. ;;term-mode
  476. Man-mode))
  477. ;; (standard-display-ascii ?\n "$\n")
  478. ;; (defvar my-eol-face
  479. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  480. ;; )
  481. ;; (defvar my-tab-face
  482. ;; '(("\t" . '(0 highlight t nil))))
  483. (defvar my-jspace-face
  484. '(("\u3000" . '(0 highlight t nil))))
  485. (add-hook 'font-lock-mode-hook
  486. (lambda ()
  487. ;; (font-lock-add-keywords nil my-eol-face)
  488. (font-lock-add-keywords nil my-jspace-face)
  489. ))
  490. (when (safe-require-or-eval 'whitespace)
  491. (add-to-list 'whitespace-display-mappings
  492. ;; We need t since last one takes precedence
  493. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  494. ;; (add-to-list 'whitespace-display-mappings
  495. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  496. (setq whitespace-style '(face
  497. trailing ; trailing blanks
  498. newline ; newlines
  499. newline-mark ; use display table for newline
  500. tab-mark
  501. empty ; empty lines at beg or end of buffer
  502. lines-tail ; lines over 80
  503. ))
  504. ;; (setq whitespace-newline 'font-lock-comment-face)
  505. (set-variable 'whitespace-line-column nil)
  506. (global-whitespace-mode t)
  507. (add-hook 'dired-mode-hook
  508. (lambda ()
  509. (set (make-local-variable 'whitespace-style) nil)))
  510. (if (eq (display-color-cells)
  511. 256)
  512. (set-face-foreground 'whitespace-newline "color-109")
  513. ;; (progn
  514. ;; (set-face-bold-p 'whitespace-newline
  515. ;; t))
  516. ))
  517. (and nil
  518. (safe-require-or-eval 'fill-column-indicator)
  519. (setq fill-column-indicator))
  520. ;; highlight current line
  521. ;; http://wiki.riywo.com/index.php?Meadow
  522. (face-spec-set 'hl-line
  523. '((((min-colors 256)
  524. (background dark))
  525. (:background "color-234"))
  526. (((min-colors 256)
  527. (background light))
  528. (:background "color-234"))
  529. (t
  530. (:underline "black"))))
  531. (set-variable 'hl-line-global-modes
  532. '(not
  533. term-mode))
  534. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  535. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  536. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  537. ;;(safe-require-or-eval 'set-modeline-color)
  538. ;; (let ((fg (face-foreground 'default))
  539. ;; (bg (face-background 'default)))
  540. ;; (set-face-background 'mode-line-inactive
  541. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  542. ;; (set-face-foreground 'mode-line-inactive
  543. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  544. ;; (set-face-underline 'mode-line-inactive
  545. ;; t)
  546. ;; (set-face-underline 'vertical-border
  547. ;; nil)
  548. (when (safe-require-or-eval 'end-mark)
  549. (global-end-mark-mode))
  550. (when (safe-require-or-eval 'auto-highlight-symbol)
  551. (set-variable 'ahs-idle-interval 0.6)
  552. (global-auto-highlight-symbol-mode 1))
  553. ;; (when (safe-require-or-eval 'cyberpunk-theme)
  554. ;; (load-theme 'cyberpunk t)
  555. ;; (set-face-attribute 'button
  556. ;; nil
  557. ;; :inherit 'highlight)
  558. ;; (set-face-foreground 'mode-line-inactive
  559. ;; "white")
  560. ;; (face-spec-set 'term-color-black (face-default-spec 'term-color-black))
  561. ;; (face-spec-set 'term-color-red (face-default-spec 'term-color-red))
  562. ;; (face-spec-set 'term-color-green (face-default-spec 'term-color-green))
  563. ;; (face-spec-set 'term-color-yellow (face-default-spec 'term-color-yellow))
  564. ;; (face-spec-set 'term-color-blue (face-default-spec 'term-color-blue))
  565. ;; (face-spec-set 'term-color-magenta (face-default-spec 'term-color-magenta))
  566. ;; (face-spec-set 'term-color-cyan (face-default-spec 'term-color-cyan))
  567. ;; (face-spec-set 'term-color-white (face-default-spec 'term-color-white))
  568. ;; )
  569. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  570. ;; file handling
  571. (when (safe-require-or-eval 'editorconfig)
  572. ;; (set-variable 'editorconfig-get-properties-function
  573. ;; 'editorconfig-core-get-properties-hash)
  574. (editorconfig-mode 1)
  575. (with-eval-after-load 'org-src
  576. ;; [*.org\[\*Org Src*\[ c \]*\]]
  577. (add-hook 'org-src-mode-hook
  578. 'editorconfig-mode-apply t)))
  579. (when (fboundp 'editorconfig-custom-majormode)
  580. (add-hook 'editorconfig-custom-hooks
  581. 'editorconfig-custom-majormode))
  582. ;; (when (fboundp 'editorconfig-charset-extras)
  583. ;; (add-hook 'editorconfig-custom-hooks
  584. ;; 'editorconfig-charset-extras))
  585. (setq revert-without-query '(".+"))
  586. ;; save cursor position
  587. (when (safe-require-or-eval 'saveplace)
  588. (setq-default save-place t)
  589. (setq save-place-file (concat user-emacs-directory
  590. "places")))
  591. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  592. (setq make-backup-files t)
  593. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  594. (setq backup-directory-alist
  595. (cons (cons "\\.*$" (expand-file-name (concat user-emacs-directory
  596. "backup")))
  597. backup-directory-alist))
  598. (setq version-control 'never)
  599. (setq delete-old-versions t)
  600. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  601. "auto-save/")))
  602. (setq delete-auto-save-files t)
  603. (add-to-list 'completion-ignored-extensions ".bak")
  604. ;; (setq delete-by-moving-to-trash t
  605. ;; trash-directory "~/.emacs.d/trash")
  606. (add-hook 'after-save-hook
  607. 'executable-make-buffer-file-executable-if-script-p)
  608. (set (defvar bookmark-default-file)
  609. (expand-file-name (concat user-emacs-directory
  610. "bmk")))
  611. (with-eval-after-load 'recentf
  612. (defvar recentf-exclude nil)
  613. (add-to-list 'recentf-exclude
  614. (regexp-quote bookmark-default-file)))
  615. (when (safe-require-or-eval 'smart-revert)
  616. (smart-revert-on))
  617. ;; autosave
  618. (when (safe-require-or-eval 'autosave)
  619. (autosave-set 8))
  620. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  621. ;; buffer killing
  622. ;; (defun my-delete-window-killing-buffer () nil)
  623. (defun my-query-kill-current-buffer ()
  624. "Interactively kill current buffer."
  625. (interactive)
  626. (if (y-or-n-p (concat "kill current buffer? :"))
  627. (kill-buffer (current-buffer))))
  628. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  629. (substitute-key-definition 'kill-buffer
  630. 'my-query-kill-current-buffer
  631. global-map)
  632. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  633. ;; share clipboard with x
  634. ;; this page describes this in details, but only these sexps seem to be needed
  635. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  636. (and (not window-system)
  637. (not (eq window-system 'mac))
  638. (getenv "DISPLAY")
  639. (not (equal (getenv "DISPLAY") ""))
  640. (executable-find "xclip")
  641. ;; (< emacs-major-version 24)
  642. (safe-require-or-eval 'xclip)
  643. nil
  644. (turn-on-xclip))
  645. (and (eq system-type 'darwin)
  646. (safe-require-or-eval 'pasteboard)
  647. (turn-on-pasteboard))
  648. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  649. ;; some modes and hooks
  650. ;; Workaround to avoid ensime error
  651. (defvar ensime-mode-key-prefix nil)
  652. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  653. (when (safe-require-or-eval 'company)
  654. (global-company-mode)
  655. (set-variable 'company-idle-delay 0.5)
  656. (set-variable 'company-minimum-prefix-length 2)
  657. (set-variable 'company-selection-wrap-around t))
  658. ;; https://github.com/lunaryorn/flycheck
  659. (when (safe-require-or-eval 'flycheck)
  660. (call-after-init 'global-flycheck-mode))
  661. (set-variable 'ac-ignore-case nil)
  662. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  663. (define-key ctl-x-map "t" 'term-run-shell-command))
  664. (add-to-list 'safe-local-variable-values
  665. '(encoding utf-8))
  666. (setq enable-local-variables :safe)
  667. (when (safe-require-or-eval 'remember-major-modes-mode)
  668. (remember-major-modes-mode 1))
  669. ;; Detect file type from shebang and set major-mode.
  670. (add-to-list 'interpreter-mode-alist
  671. '("python3" . python-mode))
  672. (add-to-list 'interpreter-mode-alist
  673. '("python2" . python-mode))
  674. (with-eval-after-load 'python
  675. (defvar python-mode-map (make-sparse-keymap))
  676. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  677. ;; http://fukuyama.co/foreign-regexp
  678. '(and (safe-require-or-eval 'foreign-regexp)
  679. (progn
  680. (setq foreign-regexp/regexp-type 'perl)
  681. '(setq reb-re-syntax 'foreign-regexp)
  682. ))
  683. (autoload-eval-lazily 'sql '(sql-mode)
  684. (safe-require-or-eval 'sql-indent))
  685. (when (autoload-eval-lazily 'git-command)
  686. (define-key ctl-x-map "g" 'git-command))
  687. (when (safe-require-or-eval 'git-commit)
  688. (global-git-commit-mode 1))
  689. (with-eval-after-load 'git-commit
  690. (add-hook 'git-commit-setup-hook
  691. 'turn-off-auto-fill t))
  692. (autoload-eval-lazily 'sl)
  693. (with-eval-after-load 'rst
  694. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  695. ;; jdee is too old! use malabar instead
  696. (with-eval-after-load 'jdee
  697. (add-hook 'jdee-mode-hook
  698. (lambda ()
  699. (make-local-variable 'global-mode-string)
  700. (add-to-list 'global-mode-string
  701. mode-line-position))))
  702. ;; Cannot enable error thrown. Why???
  703. ;; https://github.com/m0smith/malabar-mode#Installation
  704. ;; (when (autoload-eval-lazily 'malabar-mode)
  705. ;; (add-to-list 'load-path
  706. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  707. ;; (safe-require-or-eval 'cedet-devel-load)
  708. ;; (call-after-init 'activate-malabar-mode))
  709. (with-eval-after-load 'make-mode
  710. (defvar makefile-mode-map (make-sparse-keymap))
  711. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  712. ;; this functions is set in write-file-functions, i cannot find any
  713. ;; good way to remove this.
  714. (fset 'makefile-warn-suspicious-lines 'ignore))
  715. (with-eval-after-load 'verilog-mode
  716. (defvar verilog-mode-map (make-sparse-keymap))
  717. (define-key verilog-mode-map ";" 'self-insert-command))
  718. (setq diff-switches "-u")
  719. (with-eval-after-load 'diff-mode
  720. ;; (when (and (eq major-mode
  721. ;; 'diff-mode)
  722. ;; (not buffer-file-name))
  723. ;; ;; do not pass when major-mode is derived mode of diff-mode
  724. ;; (view-mode 1))
  725. (set-face-attribute 'diff-header nil
  726. :foreground nil
  727. :background nil
  728. :weight 'bold)
  729. (set-face-attribute 'diff-file-header nil
  730. :foreground nil
  731. :background nil
  732. :weight 'bold)
  733. (set-face-foreground 'diff-index-face "blue")
  734. (set-face-attribute 'diff-hunk-header nil
  735. :foreground "cyan"
  736. :weight 'normal)
  737. (set-face-attribute 'diff-context nil
  738. ;; :foreground "white"
  739. :foreground nil
  740. :weight 'normal)
  741. (set-face-foreground 'diff-removed-face "red")
  742. (set-face-foreground 'diff-added-face "green")
  743. (set-face-background 'diff-removed-face nil)
  744. (set-face-background 'diff-added-face nil)
  745. (set-face-attribute 'diff-changed nil
  746. :foreground "magenta"
  747. :weight 'normal)
  748. (set-face-attribute 'diff-refine-change nil
  749. :foreground nil
  750. :background nil
  751. :weight 'bold
  752. :inverse-video t)
  753. ;; Annoying !
  754. ;;(diff-auto-refine-mode)
  755. )
  756. ;; (ffap-bindings)
  757. (set-variable 'browse-url-browser-function
  758. 'eww-browse-url)
  759. (set-variable 'sh-here-document-word "__EOC__")
  760. (when (autoload-eval-lazily 'adoc-mode
  761. nil
  762. (defvar adoc-mode-map (make-sparse-keymap))
  763. (define-key adoc-mode-map (kbd "C-m") 'newline))
  764. (setq auto-mode-alist
  765. `(("\\.adoc\\'" . adoc-mode)
  766. ("\\.asciidoc\\'" . adoc-mode)
  767. ,@auto-mode-alist)))
  768. (with-eval-after-load 'markup-faces
  769. ;; Is this too match ?
  770. (set-face-foreground 'markup-meta-face
  771. "color-245")
  772. (set-face-foreground 'markup-meta-hide-face
  773. "color-245")
  774. )
  775. ;; TODO: check if this is required
  776. (when (autoload-eval-lazily 'groovy-mode nil
  777. (defvar groovy-mode-map (make-sparse-keymap))
  778. (define-key groovy-mode-map "(" 'self-insert-command)
  779. (define-key groovy-mode-map ")" 'self-insert-command)
  780. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  781. )
  782. (add-to-list 'auto-mode-alist
  783. '("build\\.gradle\\'" . groovy-mode)))
  784. (with-eval-after-load 'yaml-mode
  785. (defvar yaml-mode-map (make-sparse-keymap))
  786. (define-key yaml-mode-map (kbd "C-m") 'newline))
  787. (with-eval-after-load 'html-mode
  788. (defvar html-mode-map (make-sparse-keymap))
  789. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  790. (with-eval-after-load 'text-mode
  791. (define-key text-mode-map (kbd "C-m") 'newline))
  792. (add-to-list 'Info-default-directory-list
  793. (expand-file-name "~/.info/emacs-ja"))
  794. (with-eval-after-load 'apropos
  795. (defvar apropos-mode-map (make-sparse-keymap))
  796. (define-key apropos-mode-map "n" 'next-line)
  797. (define-key apropos-mode-map "p" 'previous-line))
  798. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  799. ;; (define-key isearch-mode-map
  800. ;; (kbd "C-j") 'isearch-other-control-char)
  801. ;; (define-key isearch-mode-map
  802. ;; (kbd "C-k") 'isearch-other-control-char)
  803. ;; (define-key isearch-mode-map
  804. ;; (kbd "C-h") 'isearch-other-control-char)
  805. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  806. (define-key isearch-mode-map (kbd "M-r")
  807. 'isearch-query-replace-regexp)
  808. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  809. (setq lazy-highlight-cleanup nil)
  810. ;; face for isearch highlighing
  811. (set-face-attribute 'lazy-highlight
  812. nil
  813. :foreground `unspecified
  814. :background `unspecified
  815. :underline t
  816. ;; :weight `bold
  817. )
  818. (add-hook 'outline-mode-hook
  819. (lambda ()
  820. (when (string-match "\\.md\\'" buffer-file-name)
  821. (set (make-local-variable 'outline-regexp) "#+ "))))
  822. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  823. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  824. (when (autoload-eval-lazily 'markdown-mode
  825. '(markdown-mode gfm-mode)
  826. (defvar gfm-mode-map (make-sparse-keymap))
  827. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  828. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  829. (set-variable 'markdown-command (or (executable-find "markdown")
  830. (executable-find "markdown.pl")
  831. ""))
  832. (add-hook 'markdown-mode-hook
  833. (lambda ()
  834. (outline-minor-mode 1)
  835. (flyspell-mode)
  836. (set (make-local-variable 'comment-start) ";")))
  837. )
  838. ;; c-mode
  839. ;; http://www.emacswiki.org/emacs/IndentingC
  840. ;; http://en.wikipedia.org/wiki/Indent_style
  841. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  842. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  843. (with-eval-after-load 'cc-vars
  844. (defvar c-default-style nil)
  845. (add-to-list 'c-default-style
  846. '(c-mode . "k&r"))
  847. (add-to-list 'c-default-style
  848. '(c++-mode . "k&r")))
  849. (autoload-eval-lazily 'js2-mode nil
  850. ;; currently do not use js2-mode
  851. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  852. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  853. ;; (defvar js2-mode-map (make-sparse-keymap))
  854. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  855. ;; (interactive)
  856. ;; (js2-enter-key)
  857. ;; (indent-for-tab-command)))
  858. ;; (add-hook (kill-local-variable 'before-save-hook)
  859. ;; 'js2-before-save)
  860. ;; (add-hook 'before-save-hook
  861. ;; 'my-indent-buffer
  862. ;; nil
  863. ;; t)
  864. )
  865. (add-to-list 'interpreter-mode-alist
  866. '("node" . js-mode))
  867. (when (autoload-eval-lazily 'flymake-jslint
  868. '(flymake-jslint-load))
  869. (autoload-eval-lazily 'js nil
  870. (add-hook 'js-mode-hook
  871. 'flymake-jslint-load)))
  872. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  873. (with-eval-after-load 'uniquify
  874. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  875. (setq uniquify-ignore-buffers-re "*[^*]+*")
  876. (setq uniquify-min-dir-content 1))
  877. (with-eval-after-load 'view
  878. (defvar view-mode-map (make-sparse-keymap))
  879. (define-key view-mode-map "j" 'scroll-up-line)
  880. (define-key view-mode-map "k" 'scroll-down-line)
  881. (define-key view-mode-map "v" 'toggle-read-only)
  882. (define-key view-mode-map "q" 'bury-buffer)
  883. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  884. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  885. ;; (define-key view-mode-map
  886. ;; "n" 'nonincremental-repeat-search-forward)
  887. ;; (define-key view-mode-map
  888. ;; "N" 'nonincremental-repeat-search-backward)
  889. (define-key view-mode-map "/" 'isearch-forward-regexp)
  890. (define-key view-mode-map "?" 'isearch-backward-regexp)
  891. (define-key view-mode-map "n" 'isearch-repeat-forward)
  892. (define-key view-mode-map "N" 'isearch-repeat-backward)
  893. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  894. (global-set-key "\M-r" 'view-mode)
  895. ;; (setq view-read-only t)
  896. (with-eval-after-load 'term
  897. (defvar term-raw-map (make-sparse-keymap))
  898. (define-key term-raw-map (kbd "C-x")
  899. (lookup-key (current-global-map)
  900. (kbd "C-x"))))
  901. (add-hook 'term-mode-hook
  902. (lambda ()
  903. ;; Stop current line highlighting
  904. (set (make-local-variable (defvar hl-line-range-function))
  905. (lambda () '(0 . 0)))
  906. (set (make-local-variable 'scroll-margin)
  907. 0)))
  908. (add-hook 'Man-mode-hook
  909. (lambda ()
  910. (view-mode 1)
  911. (setq truncate-lines nil)))
  912. (set-variable 'Man-notify-method (if window-system
  913. 'newframe
  914. 'aggressive))
  915. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  916. "woman_cache.el")))
  917. ;; not work because man.el will be loaded when man called
  918. (defalias 'man 'woman)
  919. (add-to-list 'auto-mode-alist
  920. '("tox\\.ini\\'" . conf-unix-mode))
  921. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  922. ;; buffer switching
  923. (defvar bs-configurations)
  924. (when (autoload-eval-lazily 'bs '(bs-show)
  925. (add-to-list 'bs-configurations
  926. '("specials" "^\\*" nil ".*" nil nil))
  927. (add-to-list 'bs-configurations
  928. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  929. (defvar bs-mode-map)
  930. (defvar bs-current-configuration)
  931. (define-key bs-mode-map (kbd "t")
  932. ;; TODO: fix toggle feature
  933. (lambda ()
  934. (interactive)
  935. (if (string= "specials"
  936. bs-current-configuration)
  937. (bs-set-configuration "files")
  938. (bs-set-configuration "specials"))
  939. (bs-refresh)
  940. (bs-message-without-log "%s"
  941. (bs--current-config-message))))
  942. ;; (setq bs-configurations (list
  943. ;; '("processes" nil get-buffer-process ".*" nil nil)
  944. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  945. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  946. )
  947. (defalias 'list-buffers 'bs-show)
  948. (set-variable 'bs-default-configuration "files-and-specials")
  949. (set-variable 'bs-default-sort-name "by nothing")
  950. (add-hook 'bs-mode-hook
  951. (lambda ()
  952. (set (make-local-variable 'scroll-margin) 0))))
  953. ;;(iswitchb-mode 1)
  954. (icomplete-mode)
  955. (defun iswitchb-buffer-display-other-window ()
  956. "Do iswitchb in other window."
  957. (interactive)
  958. (let ((iswitchb-default-method 'display))
  959. (call-interactively 'iswitchb-buffer)))
  960. ;;;;;;;;;;;;;;;;;;;;;;;;
  961. ;; ilookup
  962. (with-eval-after-load 'ilookup
  963. (set-variable 'ilookup-dict-alist
  964. '(
  965. ("sdcv" . (lambda (word)
  966. (shell-command-to-string
  967. (format "sdcv -n '%s'"
  968. word))))
  969. ("en" . (lambda (word)
  970. (shell-command-to-string
  971. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  972. word))))
  973. ("ja" . (lambda (word)
  974. (shell-command-to-string
  975. (format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
  976. word))))
  977. ("jaj" . (lambda (word)
  978. (shell-command-to-string
  979. (format "sdcv -n -u jmdict-en-ja '%s'"
  980. word))))
  981. ("jag" .
  982. (lambda (word)
  983. (with-temp-buffer
  984. (insert (shell-command-to-string
  985. (format "sdcv -n -u 'Genius English-Japanese' '%s'"
  986. word)))
  987. (html2text)
  988. (buffer-substring (point-min)
  989. (point-max)))))
  990. ("alc" . (lambda (word)
  991. (shell-command-to-string
  992. (format "alc '%s' | head -n 20"
  993. word))))
  994. ("app" . (lambda (word)
  995. (shell-command-to-string
  996. (format "dict_app '%s'"
  997. word))))
  998. ;; letters broken
  999. ("ms" .
  1000. (lambda (word)
  1001. (let ((url (concat
  1002. "http://api.microsofttranslator.com/V2/Ajax.svc/"
  1003. "Translate?appId=%s&text=%s&to=%s"))
  1004. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  1005. (target "ja")
  1006. (eword (url-hexify-string word)))
  1007. (with-current-buffer (url-retrieve-synchronously
  1008. (format url
  1009. apikey
  1010. eword
  1011. target))
  1012. (message "")
  1013. (goto-char (point-min))
  1014. (search-forward-regexp "^$"
  1015. nil
  1016. t)
  1017. (url-unhex-string (buffer-substring-no-properties
  1018. (point)
  1019. (point-max)))))))
  1020. ))
  1021. ;; (funcall (cdr (assoc "ms"
  1022. ;; ilookup-alist))
  1023. ;; "dictionary")
  1024. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  1025. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  1026. (set-variable 'ilookup-default "ja")
  1027. (when (locate-library "google-translate")
  1028. (defvar ilookup-dict-alist nil)
  1029. (add-to-list 'ilookup-dict-alist
  1030. '("gt" .
  1031. (lambda (word)
  1032. (save-excursion
  1033. (google-translate-translate "auto"
  1034. "ja"
  1035. word))
  1036. (with-current-buffer "*Google Translate*"
  1037. (buffer-substring-no-properties (point-min)
  1038. (point-max)))))))
  1039. )
  1040. (when (autoload-eval-lazily 'google-translate '(google-translate-translate
  1041. google-translate-at-point))
  1042. (set-variable 'google-translate-default-source-language "auto")
  1043. (set-variable 'google-translate-default-target-language "ja"))
  1044. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1045. ;; vc
  1046. (set-variable 'vc-handled-backends '())
  1047. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1048. ;; recentf-mode
  1049. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1050. "recentf")))
  1051. (set-variable 'recentf-max-menu-items 20)
  1052. (set-variable 'recentf-max-saved-items 30)
  1053. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1054. (when (safe-require-or-eval 'recentf)
  1055. (add-to-list 'recentf-exclude
  1056. (regexp-quote recentf-save-file))
  1057. (add-to-list 'recentf-exclude
  1058. (regexp-quote (expand-file-name user-emacs-directory)))
  1059. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1060. (remove-hook 'find-file-hook
  1061. 'recentf-track-opened-file)
  1062. (defun my-recentf-load-track-save-list ()
  1063. "Load current recentf list from file, track current visiting file, then save
  1064. the list."
  1065. (recentf-load-list)
  1066. (recentf-track-opened-file)
  1067. (recentf-save-list))
  1068. (add-hook 'find-file-hook
  1069. 'my-recentf-load-track-save-list)
  1070. (add-hook 'kill-emacs-hook
  1071. 'recentf-load-list)
  1072. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1073. ;; (add-hook 'find-file-hook
  1074. ;; (lambda ()
  1075. ;; (recentf-add-file default-directory)))
  1076. (and (autoload-eval-lazily 'recentf-show)
  1077. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1078. (add-hook 'recentf-show-before-listing-hook
  1079. 'recentf-load-list))
  1080. (recentf-mode 1)
  1081. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1082. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1083. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1084. (define-key recentf-dialog-mode-map "n" 'next-line)
  1085. (add-hook 'recentf-dialog-mode-hook
  1086. (lambda ()
  1087. ;; (recentf-save-list)
  1088. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1089. ;; 'my-recentf-cd-and-find-file)
  1090. (cd "~/"))))
  1091. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1092. ;; dired
  1093. (defun my-dired-echo-file-head (arg)
  1094. ""
  1095. (interactive "P")
  1096. (let ((f (dired-get-filename)))
  1097. (message "%s"
  1098. (with-temp-buffer
  1099. (insert-file-contents f)
  1100. (buffer-substring-no-properties
  1101. (point-min)
  1102. (progn (goto-char (point-min))
  1103. (forward-line (1- (if arg
  1104. (prefix-numeric-value arg)
  1105. 7)))
  1106. (point-at-eol)))))))
  1107. (defun my-dired-diff ()
  1108. ""
  1109. (interactive)
  1110. (let ((files (dired-get-marked-files nil nil nil t)))
  1111. (if (eq (car files)
  1112. t)
  1113. (diff (cadr files) (dired-get-filename))
  1114. (message "One file must be marked!"))))
  1115. (defun dired-get-file-info ()
  1116. "dired get file info"
  1117. (interactive)
  1118. (let ((f (shell-quote-argument (dired-get-filename t))))
  1119. (if (file-directory-p f)
  1120. (progn
  1121. (message "Calculating disk usage...")
  1122. (shell-command (concat "du -hsD "
  1123. f)))
  1124. (shell-command (concat "file "
  1125. f)))))
  1126. (defun my-dired-scroll-up ()
  1127. ""
  1128. (interactive)
  1129. (my-dired-previous-line (- (window-height) 1)))
  1130. (defun my-dired-scroll-down ()
  1131. ""
  1132. (interactive)
  1133. (my-dired-next-line (- (window-height) 1)))
  1134. ;; (defun my-dired-forward-line (arg)
  1135. ;; ""
  1136. ;; (interactive "p"))
  1137. (defun my-dired-previous-line (arg)
  1138. ""
  1139. (interactive "p")
  1140. (if (> arg 0)
  1141. (progn
  1142. (if (eq (line-number-at-pos)
  1143. 1)
  1144. (goto-char (point-max))
  1145. (forward-line -1))
  1146. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1147. (dired-get-subdir))
  1148. (- arg 1)
  1149. arg)))
  1150. (dired-move-to-filename)))
  1151. (defun my-dired-next-line (arg)
  1152. ""
  1153. (interactive "p")
  1154. (if (> arg 0)
  1155. (progn
  1156. (if (eq (point)
  1157. (point-max))
  1158. (goto-char (point-min))
  1159. (forward-line 1))
  1160. (my-dired-next-line (if (or (dired-get-filename nil t)
  1161. (dired-get-subdir))
  1162. (- arg 1)
  1163. arg)))
  1164. (dired-move-to-filename)))
  1165. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1166. (if (eq window-system 'mac)
  1167. (setq dired-listing-switches "-lhF")
  1168. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1169. )
  1170. (setq dired-listing-switches "-lhF")
  1171. (put 'dired-find-alternate-file 'disabled nil)
  1172. ;; when using dired-find-alternate-file
  1173. ;; reuse current dired buffer for the file to open
  1174. (set-variable 'dired-ls-F-marks-symlinks t)
  1175. (with-eval-after-load 'ls-lisp
  1176. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1177. (setq ls-lisp-dirs-first t)
  1178. (setq ls-lisp-use-localized-time-format t)
  1179. (setq ls-lisp-format-time-list
  1180. '("%Y-%m-%d %H:%M"
  1181. "%Y-%m-%d ")))
  1182. (set-variable 'dired-dwim-target t)
  1183. (set-variable 'dired-isearch-filenames t)
  1184. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1185. (set-variable 'dired-hide-details-hide-information-lines nil)
  1186. ;; (add-hook 'dired-after-readin-hook
  1187. ;; 'my-replace-nasi-none)
  1188. ;; (add-hook 'after-init-hook
  1189. ;; (lambda ()
  1190. ;; (dired ".")))
  1191. (with-eval-after-load 'dired
  1192. (safe-require-or-eval 'ls-lisp)
  1193. (defvar dired-mode-map (make-sparse-keymap))
  1194. (define-key dired-mode-map "i" 'dired-get-file-info)
  1195. (define-key dired-mode-map "f" 'find-file)
  1196. (define-key dired-mode-map "!" 'shell-command)
  1197. (define-key dired-mode-map "&" 'async-shell-command)
  1198. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1199. (define-key dired-mode-map "=" 'my-dired-diff)
  1200. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1201. (define-key dired-mode-map "b" 'gtkbm)
  1202. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1203. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1204. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1205. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1206. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1207. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1208. (substitute-key-definition 'dired-next-line
  1209. 'my-dired-next-line
  1210. dired-mode-map)
  1211. (substitute-key-definition 'dired-previous-line
  1212. 'my-dired-previous-line
  1213. dired-mode-map)
  1214. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1215. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1216. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1217. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1218. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1219. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1220. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1221. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1222. (add-hook 'dired-mode-hook
  1223. (lambda ()
  1224. (when (fboundp 'dired-hide-details-mode)
  1225. (dired-hide-details-mode t)
  1226. (local-set-key "l" 'dired-hide-details-mode))
  1227. (let ((file "._Icon\015"))
  1228. (when nil
  1229. '(file-readable-p file)
  1230. (delete-file file)))))
  1231. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1232. (with-eval-after-load 'dired
  1233. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1234. (when (autoload-eval-lazily 'dired-list-all-mode)
  1235. (setq dired-listing-switches "-lhF")
  1236. (with-eval-after-load 'dired
  1237. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1238. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1239. ;; misc funcs
  1240. (defalias 'qcalc 'quick-calc)
  1241. (defun memo (&optional dir)
  1242. "Open memo.txt in DIR."
  1243. (interactive)
  1244. (pop-to-buffer (find-file-noselect (concat (if dir
  1245. (file-name-as-directory dir)
  1246. "")
  1247. "memo.txt"))))
  1248. (defvar my-rgrep-alist
  1249. `(
  1250. ;; the silver searcher
  1251. ("ag"
  1252. (executable-find "ag")
  1253. "ag --nocolor --nogroup --nopager --filename ")
  1254. ;; ack
  1255. ("ack"
  1256. (executable-find "ack")
  1257. "ack --nocolor --nogroup --nopager --with-filename ")
  1258. ;; gnu global
  1259. ("global"
  1260. (and (require 'gtags nil t)
  1261. (executable-find "global")
  1262. (gtags-get-rootpath))
  1263. "global --result grep ")
  1264. ;; git grep
  1265. ("gitgrep"
  1266. (eq 0
  1267. (shell-command "git rev-parse --git-dir"))
  1268. "git --no-pager -c color.grep=false grep -nH -e ")
  1269. ;; grep
  1270. ("grep"
  1271. t
  1272. ,(concat "find . "
  1273. "-path '*/.git' -prune -o "
  1274. "-path '*/.svn' -prune -o "
  1275. "-type f -print0 | "
  1276. "xargs -0 grep -nH -e "))
  1277. )
  1278. "Alist of rgrep command.
  1279. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1280. condition to choose COMMAND when evaluated.")
  1281. (defvar my-rgrep-default nil
  1282. "Default command name for my-rgrep.")
  1283. (defun my-rgrep-grep-command (&optional name alist)
  1284. "Return recursive grep command for current directory or nil.
  1285. If NAME is given, use that without testing.
  1286. Commands are searched from ALIST."
  1287. (if alist
  1288. (if name
  1289. ;; if name is given search that from alist and return the command
  1290. (nth 2 (assoc name
  1291. alist))
  1292. ;; if name is not given try test in 1th elem
  1293. (let ((car (car alist))
  1294. (cdr (cdr alist)))
  1295. (if (eval (nth 1 car))
  1296. ;; if the condition is true return the command
  1297. (nth 2 car)
  1298. ;; try next one
  1299. (and cdr
  1300. (my-rgrep-grep-command name cdr)))))
  1301. ;; if alist is not given set default value
  1302. (my-rgrep-grep-command name my-rgrep-alist)))
  1303. (defun my-rgrep (command-args)
  1304. "My recursive grep. Run COMMAND-ARGS."
  1305. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1306. nil)))
  1307. (if cmd
  1308. (list (read-shell-command "grep command: "
  1309. cmd
  1310. 'grep-find-history))
  1311. (error "My-Rgrep: Command for rgrep not found")
  1312. )))
  1313. (compilation-start command-args
  1314. 'grep-mode))
  1315. ;; (defun my-rgrep-symbol-at-point (command-args)
  1316. ;; "My recursive grep. Run COMMAND-ARGS."
  1317. ;; (interactive (list (read-shell-command "grep command: "
  1318. ;; (concat (my-rgrep-grep-command)
  1319. ;; " "
  1320. ;; (thing-at-point 'symbol))
  1321. ;; 'grep-find-history)))
  1322. ;; (compilation-start command-args
  1323. ;; 'grep-mode))
  1324. (defmacro define-my-rgrep (name)
  1325. "Define rgrep for NAME."
  1326. `(defun ,(intern (concat "my-rgrep-"
  1327. name)) ()
  1328. ,(format "My recursive grep by %s."
  1329. name)
  1330. (interactive)
  1331. (let ((my-rgrep-default ,name))
  1332. (if (called-interactively-p 'any)
  1333. (call-interactively 'my-rgrep)
  1334. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1335. )
  1336. (define-my-rgrep "ack")
  1337. (define-my-rgrep "ag")
  1338. (define-my-rgrep "gitgrep")
  1339. (define-my-rgrep "grep")
  1340. (define-my-rgrep "global")
  1341. (define-key ctl-x-map "s" 'my-rgrep)
  1342. ;; (defun make ()
  1343. ;; "Run \"make -k\" in current directory."
  1344. ;; (interactive)
  1345. ;; (compile "make -k"))
  1346. (defalias 'make 'compile)
  1347. (define-key ctl-x-map "c" 'compile)
  1348. ;;;;;;;;;;;;;;;;;;;;;;;
  1349. ;; adoc-simple-mode
  1350. (when (safe-require-or-eval 'adoc-mode)
  1351. (defvar adoc-simple-font-lock-keywords
  1352. nil)
  1353. (define-derived-mode adoc-simple-mode adoc-mode
  1354. "Adoc-Simple"
  1355. "Major mode for editing AsciiDoc text files.
  1356. This mode is a simplified version of `adoc-mode'."
  1357. '(set (make-local-variable 'font-lock-defaults)
  1358. '(adoc-simple-font-lock-keywords
  1359. nil nil nil nil
  1360. (font-lock-multiline . t)
  1361. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1362. (add-to-list 'auto-mode-alist
  1363. '("\\.adoc\\'" . adoc-simple-mode)))
  1364. (when (and (safe-require-or-eval 'google-translate)
  1365. (safe-require-or-eval 'google-translate-smooth-ui))
  1366. (add-to-list 'google-translate-translation-directions-alist
  1367. '("en" . "ja"))
  1368. (defun translate-echo-at-point ()
  1369. "Translate popup at point."
  1370. (interactive)
  1371. (let ((google-translate-output-destination 'echo-area))
  1372. (google-translate-translate "auto" "ja" (current-word t t))))
  1373. (define-minor-mode auto-translate-mode
  1374. "Translate word at point automatically."
  1375. :global nil
  1376. :lighter "ATranslate"))
  1377. ;;; emacs.el ends here