Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

1609 Zeilen
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. (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. (set-variable 'editorconfig-mode-lighter " EC")
  558. (with-eval-after-load 'org-src
  559. ;; [*.org\[\*Org Src*\[ c \]*\]]
  560. (add-hook 'org-src-mode-hook
  561. 'editorconfig-mode-apply t)))
  562. (when (fboundp 'editorconfig-custom-majormode)
  563. (add-hook 'editorconfig-custom-hooks
  564. 'editorconfig-custom-majormode))
  565. ;; (when (fboundp 'editorconfig-charset-extras)
  566. ;; (add-hook 'editorconfig-custom-hooks
  567. ;; 'editorconfig-charset-extras))
  568. (setq revert-without-query '(".+"))
  569. ;; save cursor position
  570. (when (safe-require-or-eval 'saveplace)
  571. (setq-default save-place t)
  572. (setq save-place-file (concat user-emacs-directory
  573. "places")))
  574. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  575. (setq make-backup-files t)
  576. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  577. (setq backup-directory-alist
  578. (cons (cons "\\.*$" (expand-file-name (concat user-emacs-directory
  579. "backup")))
  580. backup-directory-alist))
  581. (setq version-control 'never)
  582. (setq delete-old-versions t)
  583. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  584. "auto-save/")))
  585. (setq delete-auto-save-files t)
  586. (add-to-list 'completion-ignored-extensions ".bak")
  587. ;; (setq delete-by-moving-to-trash t
  588. ;; trash-directory "~/.emacs.d/trash")
  589. (add-hook 'after-save-hook
  590. 'executable-make-buffer-file-executable-if-script-p)
  591. (set (defvar bookmark-default-file)
  592. (expand-file-name (concat user-emacs-directory
  593. "bmk")))
  594. (with-eval-after-load 'recentf
  595. (defvar recentf-exclude nil)
  596. (add-to-list 'recentf-exclude
  597. (regexp-quote bookmark-default-file)))
  598. (when (safe-require-or-eval 'smart-revert)
  599. (smart-revert-on))
  600. ;; autosave
  601. (when (safe-require-or-eval 'autosave)
  602. (autosave-set 8))
  603. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  604. ;; buffer killing
  605. ;; (defun my-delete-window-killing-buffer () nil)
  606. (defun my-query-kill-current-buffer ()
  607. "Interactively kill current buffer."
  608. (interactive)
  609. (if (y-or-n-p (concat "kill current buffer? :"))
  610. (kill-buffer (current-buffer))))
  611. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  612. (substitute-key-definition 'kill-buffer
  613. 'my-query-kill-current-buffer
  614. global-map)
  615. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  616. ;; share clipboard with x
  617. ;; this page describes this in details, but only these sexps seem to be needed
  618. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  619. (and nil
  620. (not window-system)
  621. (not (eq window-system 'mac))
  622. (getenv "DISPLAY")
  623. (not (equal (getenv "DISPLAY") ""))
  624. (executable-find "xclip")
  625. ;; (< emacs-major-version 24)
  626. '(safe-require-or-eval 'xclip)
  627. nil
  628. (turn-on-xclip))
  629. (and (eq system-type 'darwin)
  630. (safe-require-or-eval 'pasteboard)
  631. (turn-on-pasteboard))
  632. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  633. ;; some modes and hooks
  634. ;; Include some extra modes
  635. (require 'generic-x)
  636. ;; Workaround to avoid ensime error
  637. (defvar ensime-mode-key-prefix nil)
  638. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  639. (when (safe-require-or-eval 'company)
  640. (global-company-mode)
  641. (set-variable 'company-idle-delay 0.5)
  642. (set-variable 'company-minimum-prefix-length 2)
  643. (set-variable 'company-selection-wrap-around t))
  644. ;; https://github.com/lunaryorn/flycheck
  645. (when (safe-require-or-eval 'flycheck)
  646. (call-after-init 'global-flycheck-mode))
  647. (set-variable 'ac-ignore-case nil)
  648. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  649. (define-key ctl-x-map "t" 'term-run-shell-command))
  650. (add-to-list 'safe-local-variable-values
  651. '(encoding utf-8))
  652. (setq enable-local-variables :safe)
  653. ;; (when (safe-require-or-eval 'remember-major-modes-mode)
  654. ;; (remember-major-modes-mode 1))
  655. ;; Detect file type from shebang and set major-mode.
  656. (add-to-list 'interpreter-mode-alist
  657. '("python3" . python-mode))
  658. (add-to-list 'interpreter-mode-alist
  659. '("python2" . python-mode))
  660. (with-eval-after-load 'python
  661. (defvar python-mode-map (make-sparse-keymap))
  662. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  663. ;; http://fukuyama.co/foreign-regexp
  664. '(and (safe-require-or-eval 'foreign-regexp)
  665. (progn
  666. (setq foreign-regexp/regexp-type 'perl)
  667. '(setq reb-re-syntax 'foreign-regexp)
  668. ))
  669. (autoload-eval-lazily 'sql '(sql-mode)
  670. (require 'sql-indent nil t))
  671. (when (autoload-eval-lazily 'git-command)
  672. (define-key ctl-x-map "g" 'git-command))
  673. (when (safe-require-or-eval 'git-commit)
  674. (global-git-commit-mode 1))
  675. (with-eval-after-load 'git-commit
  676. (add-hook 'git-commit-setup-hook
  677. 'turn-off-auto-fill t))
  678. (autoload-eval-lazily 'sl)
  679. (with-eval-after-load 'rst
  680. (defvar rst-mode-map)
  681. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  682. (with-eval-after-load 'jdee
  683. (add-hook 'jdee-mode-hook
  684. (lambda ()
  685. (make-local-variable 'global-mode-string)
  686. (add-to-list 'global-mode-string
  687. mode-line-position))))
  688. ;; Cannot enable error thrown. Why???
  689. ;; https://github.com/m0smith/malabar-mode#Installation
  690. ;; (when (autoload-eval-lazily 'malabar-mode)
  691. ;; (add-to-list 'load-path
  692. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  693. ;; (safe-require-or-eval 'cedet-devel-load)
  694. ;; (call-after-init 'activate-malabar-mode))
  695. (with-eval-after-load 'make-mode
  696. (defvar makefile-mode-map (make-sparse-keymap))
  697. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  698. ;; this functions is set in write-file-functions, i cannot find any
  699. ;; good way to remove this.
  700. (fset 'makefile-warn-suspicious-lines 'ignore))
  701. (with-eval-after-load 'verilog-mode
  702. (defvar verilog-mode-map (make-sparse-keymap))
  703. (define-key verilog-mode-map ";" 'self-insert-command))
  704. (setq diff-switches "-u")
  705. (with-eval-after-load 'diff-mode
  706. ;; (when (and (eq major-mode
  707. ;; 'diff-mode)
  708. ;; (not buffer-file-name))
  709. ;; ;; do not pass when major-mode is derived mode of diff-mode
  710. ;; (view-mode 1))
  711. (set-face-attribute 'diff-header nil
  712. :foreground nil
  713. :background nil
  714. :weight 'bold)
  715. (set-face-attribute 'diff-file-header nil
  716. :foreground nil
  717. :background nil
  718. :weight 'bold)
  719. (set-face-foreground 'diff-index "blue")
  720. (set-face-attribute 'diff-hunk-header nil
  721. :foreground "cyan"
  722. :weight 'normal)
  723. (set-face-attribute 'diff-context nil
  724. ;; :foreground "white"
  725. :foreground nil
  726. :weight 'normal)
  727. (set-face-foreground 'diff-removed "red")
  728. (set-face-foreground 'diff-added "green")
  729. (set-face-background 'diff-removed nil)
  730. (set-face-background 'diff-added nil)
  731. (set-face-attribute 'diff-changed nil
  732. :foreground "magenta"
  733. :weight 'normal)
  734. (set-face-attribute 'diff-refine-changed nil
  735. :foreground nil
  736. :background nil
  737. :weight 'bold
  738. :inverse-video t)
  739. ;; Annoying !
  740. ;;(diff-auto-refine-mode)
  741. )
  742. ;; (ffap-bindings)
  743. (set-variable 'browse-url-browser-function
  744. 'eww-browse-url)
  745. (set-variable 'sh-here-document-word "__EOC__")
  746. (when (autoload-eval-lazily 'adoc-mode
  747. nil
  748. (defvar adoc-mode-map (make-sparse-keymap))
  749. (define-key adoc-mode-map (kbd "C-m") 'newline))
  750. (setq auto-mode-alist
  751. `(("\\.adoc\\'" . adoc-mode)
  752. ("\\.asciidoc\\'" . adoc-mode)
  753. ,@auto-mode-alist)))
  754. (with-eval-after-load 'markup-faces
  755. ;; Is this too match ?
  756. (set-face-foreground 'markup-meta-face
  757. "color-245")
  758. (set-face-foreground 'markup-meta-hide-face
  759. "color-245")
  760. )
  761. ;; TODO: check if this is required
  762. (when (autoload-eval-lazily 'groovy-mode nil
  763. (defvar groovy-mode-map (make-sparse-keymap))
  764. (define-key groovy-mode-map "(" 'self-insert-command)
  765. (define-key groovy-mode-map ")" 'self-insert-command)
  766. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  767. )
  768. (add-to-list 'auto-mode-alist
  769. '("build\\.gradle\\'" . groovy-mode)))
  770. (with-eval-after-load 'yaml-mode
  771. (defvar yaml-mode-map (make-sparse-keymap))
  772. (define-key yaml-mode-map (kbd "C-m") 'newline))
  773. (with-eval-after-load 'html-mode
  774. (defvar html-mode-map (make-sparse-keymap))
  775. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  776. (with-eval-after-load 'text-mode
  777. (define-key text-mode-map (kbd "C-m") 'newline))
  778. (add-to-list 'Info-default-directory-list
  779. (expand-file-name "~/.info/emacs-ja"))
  780. (with-eval-after-load 'apropos
  781. (defvar apropos-mode-map (make-sparse-keymap))
  782. (define-key apropos-mode-map "n" 'next-line)
  783. (define-key apropos-mode-map "p" 'previous-line))
  784. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  785. ;; (define-key isearch-mode-map
  786. ;; (kbd "C-j") 'isearch-other-control-char)
  787. ;; (define-key isearch-mode-map
  788. ;; (kbd "C-k") 'isearch-other-control-char)
  789. ;; (define-key isearch-mode-map
  790. ;; (kbd "C-h") 'isearch-other-control-char)
  791. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  792. (define-key isearch-mode-map (kbd "M-r")
  793. 'isearch-query-replace-regexp)
  794. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  795. (setq lazy-highlight-cleanup nil)
  796. ;; face for isearch highlighing
  797. (set-face-attribute 'lazy-highlight
  798. nil
  799. :foreground `unspecified
  800. :background `unspecified
  801. :underline t
  802. ;; :weight `bold
  803. )
  804. (add-hook 'outline-mode-hook
  805. (lambda ()
  806. (when (string-match "\\.md\\'" buffer-file-name)
  807. (set (make-local-variable 'outline-regexp) "#+ "))))
  808. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  809. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  810. (when (autoload-eval-lazily 'markdown-mode
  811. '(markdown-mode gfm-mode)
  812. (defvar gfm-mode-map (make-sparse-keymap))
  813. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  814. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  815. (set-variable 'markdown-command (or (executable-find "markdown")
  816. (executable-find "markdown.pl")
  817. ""))
  818. (add-hook 'markdown-mode-hook
  819. (lambda ()
  820. (outline-minor-mode 1)
  821. (flyspell-mode)
  822. (set (make-local-variable 'comment-start) ";")))
  823. )
  824. ;; c-mode
  825. ;; http://www.emacswiki.org/emacs/IndentingC
  826. ;; http://en.wikipedia.org/wiki/Indent_style
  827. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  828. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  829. (with-eval-after-load 'cc-vars
  830. (defvar c-default-style nil)
  831. (add-to-list 'c-default-style
  832. '(c-mode . "k&r"))
  833. (add-to-list 'c-default-style
  834. '(c++-mode . "k&r")))
  835. (autoload-eval-lazily 'js2-mode nil
  836. ;; currently do not use js2-mode
  837. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  838. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  839. ;; (defvar js2-mode-map (make-sparse-keymap))
  840. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  841. ;; (interactive)
  842. ;; (js2-enter-key)
  843. ;; (indent-for-tab-command)))
  844. ;; (add-hook (kill-local-variable 'before-save-hook)
  845. ;; 'js2-before-save)
  846. ;; (add-hook 'before-save-hook
  847. ;; 'my-indent-buffer
  848. ;; nil
  849. ;; t)
  850. )
  851. (add-to-list 'interpreter-mode-alist
  852. '("node" . js-mode))
  853. (when (autoload-eval-lazily 'flymake-jslint
  854. '(flymake-jslint-load))
  855. (autoload-eval-lazily 'js nil
  856. (add-hook 'js-mode-hook
  857. 'flymake-jslint-load)))
  858. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  859. (with-eval-after-load 'uniquify
  860. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  861. (setq uniquify-ignore-buffers-re "*[^*]+*")
  862. (setq uniquify-min-dir-content 1))
  863. (with-eval-after-load 'view
  864. (defvar view-mode-map (make-sparse-keymap))
  865. (define-key view-mode-map "j" 'scroll-up-line)
  866. (define-key view-mode-map "k" 'scroll-down-line)
  867. (define-key view-mode-map "v" 'toggle-read-only)
  868. (define-key view-mode-map "q" 'bury-buffer)
  869. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  870. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  871. ;; (define-key view-mode-map
  872. ;; "n" 'nonincremental-repeat-search-forward)
  873. ;; (define-key view-mode-map
  874. ;; "N" 'nonincremental-repeat-search-backward)
  875. (define-key view-mode-map "/" 'isearch-forward-regexp)
  876. (define-key view-mode-map "?" 'isearch-backward-regexp)
  877. (define-key view-mode-map "n" 'isearch-repeat-forward)
  878. (define-key view-mode-map "N" 'isearch-repeat-backward)
  879. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  880. (global-set-key "\M-r" 'view-mode)
  881. ;; (setq view-read-only t)
  882. (with-eval-after-load 'term
  883. (defvar term-raw-map (make-sparse-keymap))
  884. (define-key term-raw-map (kbd "C-x")
  885. (lookup-key (current-global-map)
  886. (kbd "C-x"))))
  887. (add-hook 'term-mode-hook
  888. (lambda ()
  889. ;; Stop current line highlighting
  890. (set (make-local-variable (defvar hl-line-range-function))
  891. (lambda () '(0 . 0)))
  892. (set (make-local-variable 'scroll-margin)
  893. 0)))
  894. (add-hook 'Man-mode-hook
  895. (lambda ()
  896. (view-mode 1)
  897. (setq truncate-lines nil)))
  898. (set-variable 'Man-notify-method (if window-system
  899. 'newframe
  900. 'aggressive))
  901. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  902. "woman_cache.el")))
  903. ;; not work because man.el will be loaded when man called
  904. (defalias 'man 'woman)
  905. (add-to-list 'auto-mode-alist
  906. '("tox\\.ini\\'" . conf-unix-mode))
  907. (when (autoload-eval-lazily 'toml-mode)
  908. (add-to-list 'auto-mode-alist
  909. '("/Pipfile\\'" . toml-mode)))
  910. (when (autoload-eval-lazily 'json-mode)
  911. (add-to-list 'auto-mode-alist
  912. '("/Pipfile\\.lock\\'" . json-mode)))
  913. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  914. ;; buffer switching
  915. (defvar bs-configurations)
  916. (when (autoload-eval-lazily 'bs '(bs-show)
  917. (add-to-list 'bs-configurations
  918. '("specials" "^\\*" nil ".*" nil nil))
  919. (add-to-list 'bs-configurations
  920. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  921. (defvar bs-mode-map)
  922. (defvar bs-current-configuration)
  923. (define-key bs-mode-map (kbd "t")
  924. ;; TODO: fix toggle feature
  925. (lambda ()
  926. (interactive)
  927. (if (string= "specials"
  928. bs-current-configuration)
  929. (bs-set-configuration "files")
  930. (bs-set-configuration "specials"))
  931. (bs-refresh)
  932. (bs-message-without-log "%s"
  933. (bs--current-config-message))))
  934. ;; (setq bs-configurations (list
  935. ;; '("processes" nil get-buffer-process ".*" nil nil)
  936. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  937. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  938. )
  939. (defalias 'list-buffers 'bs-show)
  940. (set-variable 'bs-default-configuration "files-and-specials")
  941. (set-variable 'bs-default-sort-name "by nothing")
  942. (add-hook 'bs-mode-hook
  943. (lambda ()
  944. (set (make-local-variable 'scroll-margin) 0))))
  945. ;;(iswitchb-mode 1)
  946. (icomplete-mode)
  947. (defun iswitchb-buffer-display-other-window ()
  948. "Do iswitchb in other window."
  949. (interactive)
  950. (let ((iswitchb-default-method 'display))
  951. (call-interactively 'iswitchb-buffer)))
  952. ;;;;;;;;;;;;;;;;;;;;;;;;
  953. ;; ilookup
  954. (with-eval-after-load 'ilookup
  955. (set-variable 'ilookup-dict-alist
  956. '(
  957. ("sdcv" . (lambda (word)
  958. (shell-command-to-string
  959. (format "sdcv -n '%s'"
  960. word))))
  961. ("en" . (lambda (word)
  962. (shell-command-to-string
  963. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  964. word))))
  965. ("ja" . (lambda (word)
  966. (shell-command-to-string
  967. (format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
  968. word))))
  969. ("jaj" . (lambda (word)
  970. (shell-command-to-string
  971. (format "sdcv -n -u jmdict-en-ja '%s'"
  972. word))))
  973. ("jag" .
  974. (lambda (word)
  975. (with-temp-buffer
  976. (insert (shell-command-to-string
  977. (format "sdcv -n -u 'Genius English-Japanese' '%s'"
  978. word)))
  979. (html2text)
  980. (buffer-substring (point-min)
  981. (point-max)))))
  982. ("alc" . (lambda (word)
  983. (shell-command-to-string
  984. (format "alc '%s' | head -n 20"
  985. word))))
  986. ("app" . (lambda (word)
  987. (shell-command-to-string
  988. (format "dict_app '%s'"
  989. word))))
  990. ;; letters broken
  991. ("ms" .
  992. (lambda (word)
  993. (let ((url (concat
  994. "http://api.microsofttranslator.com/V2/Ajax.svc/"
  995. "Translate?appId=%s&text=%s&to=%s"))
  996. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  997. (target "ja")
  998. (eword (url-hexify-string word)))
  999. (with-current-buffer (url-retrieve-synchronously
  1000. (format url
  1001. apikey
  1002. eword
  1003. target))
  1004. (message "")
  1005. (goto-char (point-min))
  1006. (search-forward-regexp "^$"
  1007. nil
  1008. t)
  1009. (url-unhex-string (buffer-substring-no-properties
  1010. (point)
  1011. (point-max)))))))
  1012. ))
  1013. ;; (funcall (cdr (assoc "ms"
  1014. ;; ilookup-alist))
  1015. ;; "dictionary")
  1016. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  1017. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  1018. (set-variable 'ilookup-default "ja")
  1019. (when (locate-library "google-translate")
  1020. (defvar ilookup-dict-alist nil)
  1021. (add-to-list 'ilookup-dict-alist
  1022. '("gt" .
  1023. (lambda (word)
  1024. (save-excursion
  1025. (google-translate-translate "auto"
  1026. "ja"
  1027. word))
  1028. (with-current-buffer "*Google Translate*"
  1029. (buffer-substring-no-properties (point-min)
  1030. (point-max)))))))
  1031. )
  1032. (when (autoload-eval-lazily 'google-translate '(google-translate-translate
  1033. google-translate-at-point))
  1034. (set-variable 'google-translate-default-source-language "auto")
  1035. (set-variable 'google-translate-default-target-language "ja"))
  1036. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1037. ;; vc
  1038. (set-variable 'vc-handled-backends '())
  1039. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1040. ;; recentf-mode
  1041. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1042. "recentf")))
  1043. (set-variable 'recentf-max-menu-items 20)
  1044. (set-variable 'recentf-max-saved-items 30)
  1045. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1046. (when (safe-require-or-eval 'recentf)
  1047. (add-to-list 'recentf-exclude
  1048. (regexp-quote recentf-save-file))
  1049. (add-to-list 'recentf-exclude
  1050. (regexp-quote (expand-file-name user-emacs-directory)))
  1051. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1052. (remove-hook 'find-file-hook
  1053. 'recentf-track-opened-file)
  1054. (defun my-recentf-load-track-save-list ()
  1055. "Load current recentf list from file, track current visiting file, then save
  1056. the list."
  1057. (recentf-load-list)
  1058. (recentf-track-opened-file)
  1059. (recentf-save-list))
  1060. (add-hook 'find-file-hook
  1061. 'my-recentf-load-track-save-list)
  1062. (add-hook 'kill-emacs-hook
  1063. 'recentf-load-list)
  1064. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1065. ;; (add-hook 'find-file-hook
  1066. ;; (lambda ()
  1067. ;; (recentf-add-file default-directory)))
  1068. (and (autoload-eval-lazily 'recentf-show)
  1069. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1070. (add-hook 'recentf-show-before-listing-hook
  1071. 'recentf-load-list))
  1072. (recentf-mode 1)
  1073. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1074. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1075. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1076. (define-key recentf-dialog-mode-map "n" 'next-line)
  1077. (add-hook 'recentf-dialog-mode-hook
  1078. (lambda ()
  1079. ;; (recentf-save-list)
  1080. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1081. ;; 'my-recentf-cd-and-find-file)
  1082. (cd "~/"))))
  1083. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1084. ;; dired
  1085. (defun my-dired-echo-file-head (arg)
  1086. "Echo head of current file.
  1087. ARG is num to show, or defaults to 7."
  1088. (interactive "P")
  1089. (let ((f (dired-get-filename)))
  1090. (message "%s"
  1091. (with-temp-buffer
  1092. (insert-file-contents f)
  1093. (buffer-substring-no-properties
  1094. (point-min)
  1095. (progn (goto-char (point-min))
  1096. (forward-line (1- (if arg
  1097. (prefix-numeric-value arg)
  1098. 7)))
  1099. (point-at-eol)))))))
  1100. (defun my-dired-diff ()
  1101. "Show diff of marked file and file of current line."
  1102. (interactive)
  1103. (let ((files (dired-get-marked-files nil nil nil t)))
  1104. (if (eq (car files)
  1105. t)
  1106. (diff (cadr files) (dired-get-filename))
  1107. (message "One file must be marked!"))))
  1108. (defun dired-get-file-info ()
  1109. "Print information of current line file."
  1110. (interactive)
  1111. (let ((f (shell-quote-argument (dired-get-filename t))))
  1112. (if (file-directory-p f)
  1113. (progn
  1114. (message "Calculating disk usage...")
  1115. (shell-command (concat "du -hsD "
  1116. f)))
  1117. (shell-command (concat "file "
  1118. f)))))
  1119. (defun my-dired-scroll-up ()
  1120. "Scroll up."
  1121. (interactive)
  1122. (my-dired-previous-line (- (window-height) 1)))
  1123. (defun my-dired-scroll-down ()
  1124. "Scroll down."
  1125. (interactive)
  1126. (my-dired-next-line (- (window-height) 1)))
  1127. ;; (defun my-dired-forward-line (arg)
  1128. ;; ""
  1129. ;; (interactive "p"))
  1130. (defun my-dired-previous-line (arg)
  1131. "Move ARG lines up."
  1132. (interactive "p")
  1133. (if (> arg 0)
  1134. (progn
  1135. (if (eq (line-number-at-pos)
  1136. 1)
  1137. (goto-char (point-max))
  1138. (forward-line -1))
  1139. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1140. (dired-get-subdir))
  1141. (- arg 1)
  1142. arg)))
  1143. (dired-move-to-filename)))
  1144. (defun my-dired-next-line (arg)
  1145. "Move ARG lines down."
  1146. (interactive "p")
  1147. (if (> arg 0)
  1148. (progn
  1149. (if (eq (point)
  1150. (point-max))
  1151. (goto-char (point-min))
  1152. (forward-line 1))
  1153. (my-dired-next-line (if (or (dired-get-filename nil t)
  1154. (dired-get-subdir))
  1155. (- arg 1)
  1156. arg)))
  1157. (dired-move-to-filename)))
  1158. (defun my-dired-tramp-open (host)
  1159. "Open HOST home directory."
  1160. (interactive "sHostname: ")
  1161. (find-file (format "/scp:%s:" host)))
  1162. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1163. (if (eq window-system 'mac)
  1164. (setq dired-listing-switches "-lhF")
  1165. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1166. )
  1167. (setq dired-listing-switches "-lhF")
  1168. (put 'dired-find-alternate-file 'disabled nil)
  1169. ;; when using dired-find-alternate-file
  1170. ;; reuse current dired buffer for the file to open
  1171. (set-variable 'dired-ls-F-marks-symlinks t)
  1172. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1173. (set-variable 'ls-lisp-dirs-first t)
  1174. (set-variable 'ls-lisp-use-localized-time-format t)
  1175. (set-variable 'ls-lisp-format-time-list
  1176. '("%Y-%m-%d %H:%M"
  1177. "%Y-%m-%d "))
  1178. (set-variable 'dired-dwim-target t)
  1179. (set-variable 'dired-isearch-filenames t)
  1180. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1181. (set-variable 'dired-hide-details-hide-information-lines nil)
  1182. ;; (add-hook 'dired-after-readin-hook
  1183. ;; 'my-replace-nasi-none)
  1184. (with-eval-after-load 'dired
  1185. (safe-require-or-eval 'ls-lisp)
  1186. (defvar dired-mode-map (make-sparse-keymap))
  1187. (define-key dired-mode-map "i" 'dired-get-file-info)
  1188. (define-key dired-mode-map "f" 'find-file)
  1189. (define-key dired-mode-map "!" 'shell-command)
  1190. (define-key dired-mode-map "&" 'async-shell-command)
  1191. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1192. (define-key dired-mode-map "=" 'my-dired-diff)
  1193. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1194. (define-key dired-mode-map "b" 'gtkbm)
  1195. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1196. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1197. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1198. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1199. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1200. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1201. (substitute-key-definition 'dired-next-line
  1202. 'my-dired-next-line
  1203. dired-mode-map)
  1204. (substitute-key-definition 'dired-previous-line
  1205. 'my-dired-previous-line
  1206. dired-mode-map)
  1207. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1208. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1209. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1210. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1211. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1212. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1213. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1214. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1215. (add-hook 'dired-mode-hook
  1216. (lambda ()
  1217. (when (fboundp 'dired-hide-details-mode)
  1218. (dired-hide-details-mode t)
  1219. (local-set-key "l" 'dired-hide-details-mode))
  1220. (let ((file "._Icon\015"))
  1221. (when nil
  1222. '(file-readable-p file)
  1223. (delete-file file)))))
  1224. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1225. (with-eval-after-load 'dired
  1226. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1227. (when (autoload-eval-lazily 'dired-list-all-mode)
  1228. (setq dired-listing-switches "-lhF")
  1229. (with-eval-after-load 'dired
  1230. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1231. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1232. ;; misc funcs
  1233. (defalias 'qcalc 'quick-calc)
  1234. (defun memo (&optional dir)
  1235. "Open memo.txt in DIR."
  1236. (interactive)
  1237. (pop-to-buffer (find-file-noselect (concat (if dir
  1238. (file-name-as-directory dir)
  1239. "")
  1240. "memo.txt"))))
  1241. (defvar my-rgrep-alist
  1242. `(
  1243. ;; git grep
  1244. ("gitgrep"
  1245. (eq 0
  1246. (shell-command "git rev-parse --git-dir"))
  1247. "git --no-pager -c color.grep=false grep -nH -e ")
  1248. ;; ripgrep
  1249. ("rg"
  1250. (executable-find "rg")
  1251. "rg --no-heading --color=never --smart-case ")
  1252. ;; sift
  1253. ("sift"
  1254. (executable-find "sift")
  1255. ("sift --no-color --binary-skip --filename --line-number --git --smart-case "))
  1256. ;; the silver searcher
  1257. ("ag"
  1258. (executable-find "ag")
  1259. "ag --nocolor --nogroup --nopager --filename ")
  1260. ;; ack
  1261. ("ack"
  1262. (executable-find "ack")
  1263. "ack --nocolor --nogroup --nopager --with-filename ")
  1264. ;; gnu global
  1265. ("global"
  1266. (and (require 'gtags nil t)
  1267. (executable-find "global")
  1268. (gtags-get-rootpath))
  1269. "global --result grep ")
  1270. ;; grep
  1271. ("grep"
  1272. t
  1273. ,(concat "find . "
  1274. "-path '*/.git' -prune -o "
  1275. "-path '*/.svn' -prune -o "
  1276. "-type f -print0 | "
  1277. "xargs -0 grep -nH -e "))
  1278. )
  1279. "Alist of rgrep command.
  1280. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1281. condition to choose COMMAND when evaluated.")
  1282. (defvar my-rgrep-default nil
  1283. "Default command name for my-rgrep.")
  1284. (defun my-rgrep-grep-command (&optional name alist)
  1285. "Return recursive grep command for current directory or nil.
  1286. If NAME is given, use that without testing.
  1287. Commands are searched from ALIST."
  1288. (if alist
  1289. (if name
  1290. ;; if name is given search that from alist and return the command
  1291. (nth 2 (assoc name
  1292. alist))
  1293. ;; if name is not given try test in 1th elem
  1294. (let ((car (car alist))
  1295. (cdr (cdr alist)))
  1296. (if (eval (nth 1 car))
  1297. ;; if the condition is true return the command
  1298. (nth 2 car)
  1299. ;; try next one
  1300. (and cdr
  1301. (my-rgrep-grep-command name cdr)))))
  1302. ;; if alist is not given set default value
  1303. (my-rgrep-grep-command name my-rgrep-alist)))
  1304. (defun my-rgrep (command-args)
  1305. "My recursive grep. Run COMMAND-ARGS."
  1306. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1307. nil)))
  1308. (if cmd
  1309. (list (read-shell-command "grep command: "
  1310. cmd
  1311. 'grep-find-history))
  1312. (error "My-Rgrep: Command for rgrep not found")
  1313. )))
  1314. (compilation-start command-args
  1315. 'grep-mode))
  1316. ;; (defun my-rgrep-symbol-at-point (command-args)
  1317. ;; "My recursive grep. Run COMMAND-ARGS."
  1318. ;; (interactive (list (read-shell-command "grep command: "
  1319. ;; (concat (my-rgrep-grep-command)
  1320. ;; " "
  1321. ;; (thing-at-point 'symbol))
  1322. ;; 'grep-find-history)))
  1323. ;; (compilation-start command-args
  1324. ;; 'grep-mode))
  1325. (defmacro define-my-rgrep (name)
  1326. "Define rgrep for NAME."
  1327. `(defun ,(intern (concat "my-rgrep-"
  1328. name)) ()
  1329. ,(format "My recursive grep by %s."
  1330. name)
  1331. (interactive)
  1332. (let ((my-rgrep-default ,name))
  1333. (if (called-interactively-p 'any)
  1334. (call-interactively 'my-rgrep)
  1335. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1336. )
  1337. (define-my-rgrep "ack")
  1338. (define-my-rgrep "ag")
  1339. (define-my-rgrep "rg")
  1340. (define-my-rgrep "sift")
  1341. (define-my-rgrep "gitgrep")
  1342. (define-my-rgrep "grep")
  1343. (define-my-rgrep "global")
  1344. (define-key ctl-x-map "s" 'my-rgrep)
  1345. (defalias 'make 'compile)
  1346. (define-key ctl-x-map "c" 'compile)
  1347. ;;;;;;;;;;;;;;;;;;;;;;;
  1348. ;; adoc-simple-mode
  1349. (when (safe-require-or-eval 'adoc-mode)
  1350. (defvar adoc-simple-font-lock-keywords
  1351. nil)
  1352. (define-derived-mode adoc-simple-mode adoc-mode
  1353. "Adoc-Simple"
  1354. "Major mode for editing AsciiDoc text files.
  1355. This mode is a simplified version of `adoc-mode'."
  1356. '(set (make-local-variable 'font-lock-defaults)
  1357. '(adoc-simple-font-lock-keywords
  1358. nil nil nil nil
  1359. (font-lock-multiline . t)
  1360. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1361. (add-to-list 'auto-mode-alist
  1362. '("\\.adoc\\'" . adoc-simple-mode)))
  1363. (when (and (safe-require-or-eval 'google-translate)
  1364. (safe-require-or-eval 'google-translate-smooth-ui))
  1365. (add-to-list 'google-translate-translation-directions-alist
  1366. '("en" . "ja"))
  1367. (defun translate-echo-at-point ()
  1368. "Translate popup at point."
  1369. (interactive)
  1370. (let ((google-translate-output-destination 'echo-area))
  1371. (google-translate-translate "auto" "ja" (current-word t t))))
  1372. (define-minor-mode auto-translate-mode
  1373. "Translate word at point automatically."
  1374. :global nil
  1375. :lighter "ATranslate"))
  1376. ;;; emacs.el ends here