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.
 
 
 
 
 
 

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