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.
 
 
 
 
 
 

1629 lines
54 KiB

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