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.
 
 
 
 
 
 

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