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.
 
 
 
 
 
 

1598 lines
53 KiB

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