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.
 
 
 
 
 
 

1742 lines
56 KiB

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