25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1739 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. (global-aggressive-indent-mode 1))
  666. (when (autoload-eval-lazily 'ggtags)
  667. (add-hook 'c-mode-common-hook
  668. (lambda ()
  669. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  670. (ggtags-mode 1))))
  671. (add-hook 'python-mode-hook
  672. (lambda ()
  673. (ggtags-mode 1))))
  674. (when (autoload-eval-lazily 'imenu-list)
  675. ;; (set-variable 'imenu-list-auto-resize t)
  676. (set-variable 'imenu-list-focus-after-activation t)
  677. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  678. (with-eval-after-load 'compile
  679. (defvar compilation-filter-start)
  680. (defvar compilation-error-regexp-alist)
  681. (require 'ansi-color)
  682. (add-hook 'compilation-filter-hook
  683. (lambda ()
  684. (let ((inhibit-read-only t))
  685. (ansi-color-apply-on-region compilation-filter-start
  686. (point)))))
  687. (add-to-list 'compilation-error-regexp-alist
  688. ;; ansible-lint
  689. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  690. ;; Workaround to avoid ensime error
  691. (defvar ensime-mode-key-prefix nil)
  692. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  693. (when (safe-require-or-eval 'company)
  694. (global-company-mode)
  695. (set-variable 'company-idle-delay 0.5)
  696. (set-variable 'company-minimum-prefix-length 2)
  697. (set-variable 'company-selection-wrap-around t))
  698. ;; https://github.com/lunaryorn/flycheck
  699. (when (safe-require-or-eval 'flycheck)
  700. (call-after-init 'global-flycheck-mode))
  701. (when (autoload-eval-lazily 'ilookup)
  702. (define-key ctl-x-map "d" 'ilookup-open-word))
  703. (set-variable 'ac-ignore-case nil)
  704. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  705. (define-key ctl-x-map "t" 'term-run-shell-command))
  706. (add-to-list 'safe-local-variable-values
  707. '(encoding utf-8))
  708. (setq enable-local-variables :safe)
  709. ;; (when (safe-require-or-eval 'remember-major-modes-mode)
  710. ;; (remember-major-modes-mode 1))
  711. ;; Detect file type from shebang and set major-mode.
  712. (add-to-list 'interpreter-mode-alist
  713. '("python3" . python-mode))
  714. (add-to-list 'interpreter-mode-alist
  715. '("python2" . python-mode))
  716. (with-eval-after-load 'python
  717. (defvar python-mode-map (make-sparse-keymap))
  718. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  719. (when (autoload-eval-lazily 'pipenv)
  720. (add-hook 'python-mode-hook
  721. (lambda ()
  722. (pipenv-mode 1)
  723. (pipenv-projectile-after-switch-default)))
  724. )
  725. (set-variable 'flycheck-python-pycompile-executable "python3")
  726. ;; http://fukuyama.co/foreign-regexp
  727. '(and (safe-require-or-eval 'foreign-regexp)
  728. (progn
  729. (setq foreign-regexp/regexp-type 'perl)
  730. '(setq reb-re-syntax 'foreign-regexp)
  731. ))
  732. (autoload-eval-lazily 'sql '(sql-mode)
  733. (require 'sql-indent nil t))
  734. (when (autoload-eval-lazily 'git-command)
  735. (define-key ctl-x-map "g" 'git-command))
  736. (when (safe-require-or-eval 'git-commit)
  737. (global-git-commit-mode 1))
  738. (with-eval-after-load 'git-commit
  739. (add-hook 'git-commit-setup-hook
  740. 'turn-off-auto-fill t))
  741. (autoload-eval-lazily 'sl)
  742. (with-eval-after-load 'rst
  743. (defvar rst-mode-map)
  744. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  745. (with-eval-after-load 'jdee
  746. (add-hook 'jdee-mode-hook
  747. (lambda ()
  748. (make-local-variable 'global-mode-string)
  749. (add-to-list 'global-mode-string
  750. mode-line-position))))
  751. ;; Cannot enable error thrown. Why???
  752. ;; https://github.com/m0smith/malabar-mode#Installation
  753. ;; (when (autoload-eval-lazily 'malabar-mode)
  754. ;; (add-to-list 'load-path
  755. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  756. ;; (safe-require-or-eval 'cedet-devel-load)
  757. ;; (call-after-init 'activate-malabar-mode))
  758. (with-eval-after-load 'make-mode
  759. (defvar makefile-mode-map (make-sparse-keymap))
  760. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  761. ;; this functions is set in write-file-functions, i cannot find any
  762. ;; good way to remove this.
  763. (fset 'makefile-warn-suspicious-lines 'ignore))
  764. (with-eval-after-load 'verilog-mode
  765. (defvar verilog-mode-map (make-sparse-keymap))
  766. (define-key verilog-mode-map ";" 'self-insert-command))
  767. (setq diff-switches "-u")
  768. (with-eval-after-load 'diff-mode
  769. ;; (when (and (eq major-mode
  770. ;; 'diff-mode)
  771. ;; (not buffer-file-name))
  772. ;; ;; do not pass when major-mode is derived mode of diff-mode
  773. ;; (view-mode 1))
  774. (set-face-attribute 'diff-header nil
  775. :foreground nil
  776. :background nil
  777. :weight 'bold)
  778. (set-face-attribute 'diff-file-header nil
  779. :foreground nil
  780. :background nil
  781. :weight 'bold)
  782. (set-face-foreground 'diff-index "blue")
  783. (set-face-attribute 'diff-hunk-header nil
  784. :foreground "cyan"
  785. :weight 'normal)
  786. (set-face-attribute 'diff-context nil
  787. ;; :foreground "white"
  788. :foreground nil
  789. :weight 'normal)
  790. (set-face-foreground 'diff-removed "red")
  791. (set-face-foreground 'diff-added "green")
  792. (set-face-background 'diff-removed nil)
  793. (set-face-background 'diff-added nil)
  794. (set-face-attribute 'diff-changed nil
  795. :foreground "magenta"
  796. :weight 'normal)
  797. (set-face-attribute 'diff-refine-changed nil
  798. :foreground nil
  799. :background nil
  800. :weight 'bold
  801. :inverse-video t)
  802. ;; Annoying !
  803. ;;(diff-auto-refine-mode)
  804. )
  805. ;; (ffap-bindings)
  806. (set-variable 'browse-url-browser-function
  807. 'eww-browse-url)
  808. (set-variable 'sh-here-document-word "__EOC__")
  809. (when (autoload-eval-lazily 'adoc-mode
  810. nil
  811. (defvar adoc-mode-map (make-sparse-keymap))
  812. (define-key adoc-mode-map (kbd "C-m") 'newline))
  813. (setq auto-mode-alist
  814. `(("\\.adoc\\'" . adoc-mode)
  815. ("\\.asciidoc\\'" . adoc-mode)
  816. ,@auto-mode-alist)))
  817. (with-eval-after-load 'markup-faces
  818. ;; Is this too match ?
  819. (set-face-foreground 'markup-meta-face
  820. "color-245")
  821. (set-face-foreground 'markup-meta-hide-face
  822. "color-245")
  823. )
  824. ;; TODO: check if this is required
  825. (when (autoload-eval-lazily 'groovy-mode nil
  826. (defvar groovy-mode-map (make-sparse-keymap))
  827. (define-key groovy-mode-map "(" 'self-insert-command)
  828. (define-key groovy-mode-map ")" 'self-insert-command)
  829. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  830. )
  831. (add-to-list 'auto-mode-alist
  832. '("build\\.gradle\\'" . groovy-mode)))
  833. (with-eval-after-load 'yaml-mode
  834. (defvar yaml-mode-map (make-sparse-keymap))
  835. (define-key yaml-mode-map (kbd "C-m") 'newline))
  836. (with-eval-after-load 'html-mode
  837. (defvar html-mode-map (make-sparse-keymap))
  838. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  839. (with-eval-after-load 'text-mode
  840. (define-key text-mode-map (kbd "C-m") 'newline))
  841. (autoload-eval-lazily 'info nil
  842. (defvar Info-additional-directory-list)
  843. (dolist (dir (directory-files (concat user-emacs-directory
  844. "info")
  845. t
  846. "^[^.].*"))
  847. (when (file-directory-p dir)
  848. (add-to-list 'Info-additional-directory-list
  849. dir)))
  850. (let ((dir (expand-file-name "~/.brew/share/info")))
  851. (when (file-directory-p dir)
  852. (add-to-list 'Info-additional-directory-list
  853. dir))))
  854. (with-eval-after-load 'apropos
  855. (defvar apropos-mode-map (make-sparse-keymap))
  856. (define-key apropos-mode-map "n" 'next-line)
  857. (define-key apropos-mode-map "p" 'previous-line))
  858. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  859. ;; (define-key isearch-mode-map
  860. ;; (kbd "C-j") 'isearch-other-control-char)
  861. ;; (define-key isearch-mode-map
  862. ;; (kbd "C-k") 'isearch-other-control-char)
  863. ;; (define-key isearch-mode-map
  864. ;; (kbd "C-h") 'isearch-other-control-char)
  865. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  866. (define-key isearch-mode-map (kbd "M-r")
  867. 'isearch-query-replace-regexp)
  868. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  869. (setq lazy-highlight-cleanup nil)
  870. ;; face for isearch highlighing
  871. (set-face-attribute 'lazy-highlight
  872. nil
  873. :foreground `unspecified
  874. :background `unspecified
  875. :underline t
  876. ;; :weight `bold
  877. )
  878. (add-hook 'outline-mode-hook
  879. (lambda ()
  880. (when (string-match "\\.md\\'" buffer-file-name)
  881. (set (make-local-variable 'outline-regexp) "#+ "))))
  882. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  883. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  884. (when (autoload-eval-lazily 'markdown-mode
  885. '(markdown-mode gfm-mode)
  886. (defvar gfm-mode-map (make-sparse-keymap))
  887. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  888. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  889. (set-variable 'markdown-command (or (executable-find "markdown")
  890. (executable-find "markdown.pl")
  891. ""))
  892. (add-hook 'markdown-mode-hook
  893. (lambda ()
  894. (outline-minor-mode 1)
  895. (flyspell-mode)
  896. (set (make-local-variable 'comment-start) ";")))
  897. )
  898. ;; c-mode
  899. ;; http://www.emacswiki.org/emacs/IndentingC
  900. ;; http://en.wikipedia.org/wiki/Indent_style
  901. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  902. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  903. (with-eval-after-load 'cc-vars
  904. (defvar c-default-style nil)
  905. (add-to-list 'c-default-style
  906. '(c-mode . "k&r"))
  907. (add-to-list 'c-default-style
  908. '(c++-mode . "k&r")))
  909. (autoload-eval-lazily 'js2-mode nil
  910. ;; currently do not use js2-mode
  911. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  912. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  913. ;; (defvar js2-mode-map (make-sparse-keymap))
  914. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  915. ;; (interactive)
  916. ;; (js2-enter-key)
  917. ;; (indent-for-tab-command)))
  918. ;; (add-hook (kill-local-variable 'before-save-hook)
  919. ;; 'js2-before-save)
  920. ;; (add-hook 'before-save-hook
  921. ;; 'my-indent-buffer
  922. ;; nil
  923. ;; t)
  924. )
  925. (add-to-list 'interpreter-mode-alist
  926. '("node" . js-mode))
  927. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  928. (with-eval-after-load 'uniquify
  929. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  930. (setq uniquify-ignore-buffers-re "*[^*]+*")
  931. (setq uniquify-min-dir-content 1))
  932. (with-eval-after-load 'view
  933. (defvar view-mode-map (make-sparse-keymap))
  934. (define-key view-mode-map "j" 'scroll-up-line)
  935. (define-key view-mode-map "k" 'scroll-down-line)
  936. (define-key view-mode-map "v" 'toggle-read-only)
  937. (define-key view-mode-map "q" 'bury-buffer)
  938. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  939. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  940. ;; (define-key view-mode-map
  941. ;; "n" 'nonincremental-repeat-search-forward)
  942. ;; (define-key view-mode-map
  943. ;; "N" 'nonincremental-repeat-search-backward)
  944. (define-key view-mode-map "/" 'isearch-forward-regexp)
  945. (define-key view-mode-map "?" 'isearch-backward-regexp)
  946. (define-key view-mode-map "n" 'isearch-repeat-forward)
  947. (define-key view-mode-map "N" 'isearch-repeat-backward)
  948. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  949. (global-set-key "\M-r" 'view-mode)
  950. ;; (setq view-read-only t)
  951. (with-eval-after-load 'term
  952. (defvar term-raw-map (make-sparse-keymap))
  953. (define-key term-raw-map (kbd "C-x")
  954. (lookup-key (current-global-map)
  955. (kbd "C-x"))))
  956. (add-hook 'term-mode-hook
  957. (lambda ()
  958. ;; Stop current line highlighting
  959. (set (make-local-variable (defvar hl-line-range-function))
  960. (lambda () '(0 . 0)))
  961. (set (make-local-variable 'scroll-margin)
  962. 0)))
  963. (add-hook 'Man-mode-hook
  964. (lambda ()
  965. (view-mode 1)
  966. (setq truncate-lines nil)))
  967. (set-variable 'Man-notify-method (if window-system
  968. 'newframe
  969. 'aggressive))
  970. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  971. "woman_cache.el")))
  972. ;; not work because man.el will be loaded when man called
  973. (defalias 'man 'woman)
  974. (add-to-list 'auto-mode-alist
  975. '("tox\\.ini\\'" . conf-unix-mode))
  976. (when (autoload-eval-lazily 'toml-mode)
  977. (add-to-list 'auto-mode-alist
  978. '("/Pipfile\\'" . toml-mode)))
  979. (when (autoload-eval-lazily 'json-mode)
  980. (add-to-list 'auto-mode-alist
  981. '("/Pipfile\\.lock\\'" . json-mode)))
  982. (add-hook 'go-mode-hook
  983. (lambda()
  984. (defvar go-mode-map)
  985. (add-hook 'before-save-hook' 'gofmt-before-save)
  986. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  987. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  988. ;; buffer switching
  989. (defvar bs-configurations)
  990. (when (autoload-eval-lazily 'bs '(bs-show)
  991. (add-to-list 'bs-configurations
  992. '("specials" "^\\*" nil ".*" nil nil))
  993. (add-to-list 'bs-configurations
  994. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  995. (defvar bs-mode-map)
  996. (defvar bs-current-configuration)
  997. (define-key bs-mode-map (kbd "t")
  998. ;; TODO: fix toggle feature
  999. (lambda ()
  1000. (interactive)
  1001. (if (string= "specials"
  1002. bs-current-configuration)
  1003. (bs-set-configuration "files")
  1004. (bs-set-configuration "specials"))
  1005. (bs-refresh)
  1006. (bs-message-without-log "%s"
  1007. (bs--current-config-message))))
  1008. ;; (setq bs-configurations (list
  1009. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1010. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1011. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1012. )
  1013. (defalias 'list-buffers 'bs-show)
  1014. (set-variable 'bs-default-configuration "files-and-specials")
  1015. (set-variable 'bs-default-sort-name "by nothing")
  1016. (add-hook 'bs-mode-hook
  1017. (lambda ()
  1018. (set (make-local-variable 'scroll-margin) 0))))
  1019. ;;(iswitchb-mode 1)
  1020. (icomplete-mode)
  1021. (defun iswitchb-buffer-display-other-window ()
  1022. "Do iswitchb in other window."
  1023. (interactive)
  1024. (let ((iswitchb-default-method 'display))
  1025. (call-interactively 'iswitchb-buffer)))
  1026. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1027. ;; vc
  1028. (set-variable 'vc-handled-backends '())
  1029. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1030. ;; recentf-mode
  1031. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1032. "recentf")))
  1033. (set-variable 'recentf-max-menu-items 20)
  1034. (set-variable 'recentf-max-saved-items 30)
  1035. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1036. (set-variable 'recentf-auto-cleanup 3)
  1037. (safe-require-or-eval 'sync-recentf)
  1038. (when (safe-require-or-eval 'recentf)
  1039. (add-to-list 'recentf-exclude
  1040. (regexp-quote recentf-save-file))
  1041. (add-to-list 'recentf-exclude
  1042. (regexp-quote (expand-file-name user-emacs-directory)))
  1043. (add-to-list 'recentf-exclude
  1044. "/sync-recentf-marker\\'")
  1045. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1046. (remove-hook 'find-file-hook
  1047. 'recentf-track-opened-file)
  1048. (defun my-recentf-load-track-save-list ()
  1049. "Load current recentf list from file, track current visiting file, then save
  1050. the list."
  1051. (recentf-load-list)
  1052. (recentf-track-opened-file)
  1053. (recentf-save-list))
  1054. (add-hook 'find-file-hook
  1055. 'my-recentf-load-track-save-list)
  1056. (add-hook 'kill-emacs-hook
  1057. 'recentf-load-list)
  1058. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1059. ;; (add-hook 'find-file-hook
  1060. ;; (lambda ()
  1061. ;; (recentf-add-file default-directory)))
  1062. (when (autoload-eval-lazily 'recentf-show)
  1063. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1064. ;; (add-hook 'recentf-show-before-listing-hook
  1065. ;; 'recentf-load-list)
  1066. )
  1067. (recentf-mode 1)
  1068. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1069. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1070. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1071. (define-key recentf-dialog-mode-map "n" 'next-line)
  1072. (add-hook 'recentf-dialog-mode-hook
  1073. (lambda ()
  1074. ;; (recentf-save-list)
  1075. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1076. ;; 'my-recentf-cd-and-find-file)
  1077. (cd "~/"))))
  1078. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1079. ;; dired
  1080. (defun my-dired-echo-file-head (arg)
  1081. "Echo head of current file.
  1082. ARG is num to show, or defaults to 7."
  1083. (interactive "P")
  1084. (let ((f (dired-get-filename)))
  1085. (message "%s"
  1086. (apply 'concat
  1087. (my-file-head f
  1088. 7)))))
  1089. (defun my-dired-diff ()
  1090. "Show diff of marked file and file of current line."
  1091. (interactive)
  1092. (let ((files (dired-get-marked-files nil nil nil t)))
  1093. (if (eq (car files)
  1094. t)
  1095. (diff (cadr files) (dired-get-filename))
  1096. (message "One file must be marked!"))))
  1097. (defun dired-get-file-info ()
  1098. "Print information of current line file."
  1099. (interactive)
  1100. (let ((f (shell-quote-argument (dired-get-filename t))))
  1101. (if (file-directory-p f)
  1102. (progn
  1103. (message "Calculating disk usage...")
  1104. (shell-command (concat "du -hsD "
  1105. f)))
  1106. (shell-command (concat "file "
  1107. f)))))
  1108. (defun my-dired-scroll-up ()
  1109. "Scroll up."
  1110. (interactive)
  1111. (my-dired-previous-line (- (window-height) 1)))
  1112. (defun my-dired-scroll-down ()
  1113. "Scroll down."
  1114. (interactive)
  1115. (my-dired-next-line (- (window-height) 1)))
  1116. ;; (defun my-dired-forward-line (arg)
  1117. ;; ""
  1118. ;; (interactive "p"))
  1119. (defun my-dired-previous-line (arg)
  1120. "Move ARG lines up."
  1121. (interactive "p")
  1122. (if (> arg 0)
  1123. (progn
  1124. (if (eq (line-number-at-pos)
  1125. 1)
  1126. (goto-char (point-max))
  1127. (forward-line -1))
  1128. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1129. (dired-get-subdir))
  1130. (- arg 1)
  1131. arg)))
  1132. (dired-move-to-filename)))
  1133. (defun my-dired-next-line (arg)
  1134. "Move ARG lines down."
  1135. (interactive "p")
  1136. (if (> arg 0)
  1137. (progn
  1138. (if (eq (point)
  1139. (point-max))
  1140. (goto-char (point-min))
  1141. (forward-line 1))
  1142. (my-dired-next-line (if (or (dired-get-filename nil t)
  1143. (dired-get-subdir))
  1144. (- arg 1)
  1145. arg)))
  1146. (dired-move-to-filename)))
  1147. (defun my-dired-tramp-open (host)
  1148. "Open HOST home directory."
  1149. (interactive "sHostname: ")
  1150. (find-file (format "/scp:%s:" host)))
  1151. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1152. (if (eq window-system 'mac)
  1153. (setq dired-listing-switches "-lhF")
  1154. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1155. )
  1156. (setq dired-listing-switches "-lhF")
  1157. (put 'dired-find-alternate-file 'disabled nil)
  1158. ;; when using dired-find-alternate-file
  1159. ;; reuse current dired buffer for the file to open
  1160. (set-variable 'dired-ls-F-marks-symlinks t)
  1161. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1162. (set-variable 'ls-lisp-dirs-first t)
  1163. (set-variable 'ls-lisp-use-localized-time-format t)
  1164. (set-variable 'ls-lisp-format-time-list
  1165. '("%Y-%m-%d %H:%M"
  1166. "%Y-%m-%d "))
  1167. (set-variable 'dired-dwim-target t)
  1168. (set-variable 'dired-isearch-filenames t)
  1169. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1170. (set-variable 'dired-hide-details-hide-information-lines nil)
  1171. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1172. (set-variable 'dired-recursive-deletes 'always)
  1173. ;; (add-hook 'dired-after-readin-hook
  1174. ;; 'my-replace-nasi-none)
  1175. (with-eval-after-load 'dired
  1176. (safe-require-or-eval 'ls-lisp)
  1177. (defvar dired-mode-map (make-sparse-keymap))
  1178. (define-key dired-mode-map "i" 'dired-get-file-info)
  1179. (define-key dired-mode-map "f" 'find-file)
  1180. (define-key dired-mode-map "!" 'shell-command)
  1181. (define-key dired-mode-map "&" 'async-shell-command)
  1182. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1183. (define-key dired-mode-map "=" 'my-dired-diff)
  1184. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1185. (define-key dired-mode-map "b" 'gtkbm)
  1186. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1187. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1188. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1189. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1190. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1191. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1192. (substitute-key-definition 'dired-next-line
  1193. 'my-dired-next-line
  1194. dired-mode-map)
  1195. (substitute-key-definition 'dired-previous-line
  1196. 'my-dired-previous-line
  1197. dired-mode-map)
  1198. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1199. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1200. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1201. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1202. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1203. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1204. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1205. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1206. (add-hook 'dired-mode-hook
  1207. (lambda ()
  1208. (when (fboundp 'dired-hide-details-mode)
  1209. (dired-hide-details-mode t)
  1210. (local-set-key "l" 'dired-hide-details-mode))
  1211. (let ((file "._Icon\015"))
  1212. (when nil
  1213. '(file-readable-p file)
  1214. (delete-file file)))))
  1215. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1216. (with-eval-after-load 'dired
  1217. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1218. (when (autoload-eval-lazily 'dired-list-all-mode)
  1219. (setq dired-listing-switches "-lhF")
  1220. (with-eval-after-load 'dired
  1221. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1222. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1223. ;; misc funcs
  1224. (defalias 'qcalc 'quick-calc)
  1225. (defun memo (&optional dir)
  1226. "Open memo.txt in DIR."
  1227. (interactive)
  1228. (pop-to-buffer (find-file-noselect (concat (if dir
  1229. (file-name-as-directory dir)
  1230. "")
  1231. "memo.txt"))))
  1232. (set (defvar my-rgrep-alist nil
  1233. "Alist of rgrep command.
  1234. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1235. condition to choose COMMAND when evaluated.")
  1236. `(
  1237. ;; ripgrep
  1238. ("rg"
  1239. (executable-find "rg")
  1240. "rg --no-heading --smart-case ")
  1241. ;; git grep
  1242. ("gitgrep"
  1243. (eq 0
  1244. (shell-command "git rev-parse --git-dir"))
  1245. "git --no-pager -c color.grep=always grep -nH -e ")
  1246. ;; sift
  1247. ("sift"
  1248. (executable-find "sift")
  1249. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1250. ;; the silver searcher
  1251. ("ag"
  1252. (executable-find "ag")
  1253. "ag --nogroup --nopager --filename ")
  1254. ;; ack
  1255. ("ack"
  1256. (executable-find "ack")
  1257. "ack --nogroup --nopager --with-filename ")
  1258. ;; gnu global
  1259. ("global"
  1260. (and (require 'ggtags nil t)
  1261. (executable-find "global")
  1262. (ggtags-current-project-root))
  1263. "global --result grep ")
  1264. ;; grep
  1265. ("grep"
  1266. t
  1267. ,(concat "find . "
  1268. "-path '*/.git' -prune -o "
  1269. "-path '*/.svn' -prune -o "
  1270. "-type f -print0 | "
  1271. "xargs -0 grep -nH -e "))
  1272. )
  1273. )
  1274. (defvar my-rgrep-default nil
  1275. "Default command name for my-rgrep.")
  1276. (defun my-rgrep-grep-command (&optional name alist)
  1277. "Return recursive grep command for current directory or nil.
  1278. If NAME is given, use that without testing.
  1279. Commands are searched from ALIST."
  1280. (if alist
  1281. (if name
  1282. ;; if name is given search that from alist and return the command
  1283. (nth 2 (assoc name
  1284. alist))
  1285. ;; if name is not given try test in 1th elem
  1286. (let ((car (car alist))
  1287. (cdr (cdr alist)))
  1288. (if (eval (nth 1 car))
  1289. ;; if the condition is true return the command
  1290. (nth 2 car)
  1291. ;; try next one
  1292. (and cdr
  1293. (my-rgrep-grep-command name cdr)))))
  1294. ;; if alist is not given set default value
  1295. (my-rgrep-grep-command name my-rgrep-alist)))
  1296. (defun my-rgrep (command-args)
  1297. "My recursive grep. Run COMMAND-ARGS.
  1298. If prefix argument is given, use current symbol as default search target
  1299. and search from projectile root (if projectile is available)."
  1300. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1301. nil)))
  1302. (if cmd
  1303. (list (read-shell-command "grep command: "
  1304. (concat cmd
  1305. (if current-prefix-arg
  1306. (thing-at-point 'symbol t)
  1307. ""))
  1308. 'grep-find-history))
  1309. (error "My-Rgrep: Command for rgrep not found")
  1310. )))
  1311. (if (and current-prefix-arg
  1312. (safe-require-or-eval 'projectile)
  1313. (projectile-project-p))
  1314. (projectile-with-default-dir (projectile-project-root)
  1315. (compilation-start command-args
  1316. 'grep-mode))
  1317. (compilation-start command-args
  1318. 'grep-mode)))
  1319. (defun my-rgrep-thing-at-point-projectile-root ()
  1320. "My recursive grep to find thing at point from project root."
  1321. (interactive)
  1322. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1323. nil))
  1324. (command-args
  1325. (if cmd
  1326. (concat cmd
  1327. (or (thing-at-point 'symbol t)
  1328. (error "No symbol at point")))
  1329. (error "My-Rgrep: Command for rgrep not found"))))
  1330. (if (safe-require-or-eval 'projectile)
  1331. (projectile-with-default-dir (projectile-project-root)
  1332. (compilation-start command-args
  1333. 'grep-mode))
  1334. (compilation-start command-args
  1335. 'grep-mode))))
  1336. (defmacro define-my-rgrep (name)
  1337. "Define rgrep for NAME."
  1338. `(defun ,(intern (concat "my-rgrep-"
  1339. name)) ()
  1340. ,(format "My recursive grep by %s."
  1341. name)
  1342. (interactive)
  1343. (let ((my-rgrep-default ,name))
  1344. (if (called-interactively-p 'any)
  1345. (call-interactively 'my-rgrep)
  1346. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1347. )
  1348. (define-my-rgrep "ack")
  1349. (define-my-rgrep "ag")
  1350. (define-my-rgrep "rg")
  1351. (define-my-rgrep "sift")
  1352. (define-my-rgrep "gitgrep")
  1353. (define-my-rgrep "grep")
  1354. (define-my-rgrep "global")
  1355. (define-key ctl-x-map "s" 'my-rgrep)
  1356. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1357. (defalias 'make 'compile)
  1358. (define-key ctl-x-map "c" 'compile)
  1359. ;;;;;;;;;;;;;;;;;;;;;;;
  1360. ;; adoc-simple-mode
  1361. (when (safe-require-or-eval 'adoc-mode)
  1362. (defvar adoc-simple-font-lock-keywords
  1363. nil)
  1364. (define-derived-mode adoc-simple-mode adoc-mode
  1365. "Adoc-Simple"
  1366. "Major mode for editing AsciiDoc text files.
  1367. This mode is a simplified version of `adoc-mode'."
  1368. '(set (make-local-variable 'font-lock-defaults)
  1369. '(adoc-simple-font-lock-keywords
  1370. nil nil nil nil
  1371. (font-lock-multiline . t)
  1372. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1373. (add-to-list 'auto-mode-alist
  1374. '("\\.adoc\\'" . adoc-simple-mode)))
  1375. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1376. ;; awk-preview
  1377. (defgroup awk-preview nil
  1378. "Awk previewer."
  1379. :tag "Awk Preview"
  1380. :prefix "awk-preview-"
  1381. :group 'tools)
  1382. (defcustom awk-preview-program
  1383. (or (executable-find "gawk")
  1384. (executable-find "awk")
  1385. "awk")
  1386. "Awk program to execute."
  1387. :type 'string
  1388. :group 'awk-preview)
  1389. (defcustom awk-preview-switches
  1390. '("--sandbox" "--" "{print}")
  1391. "String of awk options appended when running awk preview."
  1392. :type '(repeat string)
  1393. :group 'awk-preview)
  1394. (defun awk-preview--invoke (start end output)
  1395. "Execute awk process and get result."
  1396. (let ((proc (apply 'start-process
  1397. "awk-preview"
  1398. output
  1399. awk-preview-program
  1400. awk-preview-switches)))
  1401. (message "%S" proc)
  1402. (process-send-region proc start end)
  1403. (process-send-eof proc)
  1404. (accept-process-output proc)
  1405. )
  1406. output)
  1407. (defun awk-preview (start end)
  1408. "Run awk and preview result."
  1409. (interactive "r")
  1410. (let ((output (with-current-buffer (get-buffer-create "*awk-preview output*")
  1411. (erase-buffer)
  1412. (current-buffer))))
  1413. (awk-preview--invoke start end output)
  1414. (message "awk-preview: %s" (with-current-buffer output
  1415. (buffer-substring-no-properties (point-min) (point-max))))
  1416. ))
  1417. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1418. ;; editorconfig-auto-apply
  1419. (define-minor-mode editorconfig-auto-apply-mode
  1420. "When saving .editorconfig file update buffer configs."
  1421. :global t
  1422. :lighter ""
  1423. (if editorconfig-auto-apply-mode
  1424. (add-hook 'after-save-hook
  1425. 'editorconfig-auto-apply-mode--when-save)
  1426. (remove-hook 'after-save-hook
  1427. 'editorconfig-auto-apply-mode--when-save)))
  1428. (defun editorconfig-auto-apply-mode--when-save ()
  1429. "When saving .editorconfig file walk all buffers and update configs."
  1430. (when (eq major-mode
  1431. 'editorconfig-conf-mode)
  1432. (let ((dir (file-name-directory buffer-file-name)))
  1433. (cl-dolist (buf (buffer-list))
  1434. (when (and (buffer-file-name buf)
  1435. (file-in-directory-p (buffer-file-name buf)
  1436. dir))
  1437. (with-current-buffer buf
  1438. (editorconfig-mode-apply)))))))
  1439. (editorconfig-auto-apply-mode 1)
  1440. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1441. (defun my-file-head (filename &optional n)
  1442. "Return list of first N lines of file FILENAME."
  1443. ;; Work with japanese text?
  1444. (let ((num (or n 10))
  1445. (size 100)
  1446. (beg 0)
  1447. (end 0)
  1448. (result '()))
  1449. (with-temp-buffer
  1450. (erase-buffer)
  1451. (while (<= (count-lines (point-min)
  1452. (point-max))
  1453. num)
  1454. (setq end (+ beg size))
  1455. (insert-file-contents-literally filename
  1456. nil
  1457. beg
  1458. end)
  1459. (goto-char (point-max))
  1460. (setq beg (+ beg size)))
  1461. (goto-char (point-min))
  1462. (while (< (length result) num)
  1463. (let ((start (point)))
  1464. (forward-line 1)
  1465. (setq result
  1466. `(,@result ,(buffer-substring-no-properties start
  1467. (point))))))
  1468. result
  1469. ;; (buffer-substring-no-properties (point-min)
  1470. ;; (progn
  1471. ;; (forward-line num)
  1472. ;; (point)))
  1473. )))
  1474. ;; (apply 'concat (my-file-head "./shrc" 10)
  1475. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1476. ;; Local Variables:
  1477. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1478. ;; flycheck-checker: emacs-lisp
  1479. ;; End:
  1480. ;;; emancs.el ends here