You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1576 lines
52 KiB

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