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.
 
 
 
 
 
 

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