Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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