Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

1755 строки
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. 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 16))
  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. (when (safe-require-or-eval 'fancy-narrow)
  322. (fancy-narrow-mode 1))
  323. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  324. ;; title and mode-line
  325. (when (safe-require-or-eval 'terminal-title)
  326. ;; if TERM is not screen use default value
  327. (if (getenv "TMUX")
  328. ;; if use tmux locally just basename of current dir
  329. (set-variable 'terminal-title-format
  330. '((file-name-nondirectory (directory-file-name
  331. default-directory))))
  332. (if (and (let ((tty-type (frame-parameter nil
  333. 'tty-type)))
  334. (and tty-type
  335. (equal (car (split-string tty-type
  336. "-"))
  337. "screen")))
  338. (not (getenv "SSH_CONNECTION")))
  339. (set-variable 'terminal-title-format
  340. '((file-name-nondirectory (directory-file-name
  341. default-directory))))
  342. ;; seems that TMUX is used locally and ssh to remote host
  343. (set-variable 'terminal-title-format
  344. `("em:"
  345. ,user-login-name
  346. "@"
  347. ,(car (split-string (system-name)
  348. "\\."))
  349. ":"
  350. default-directory))
  351. )
  352. )
  353. (terminal-title-mode))
  354. (setq eol-mnemonic-dos "\\r\\n")
  355. (setq eol-mnemonic-mac "\\r")
  356. (setq eol-mnemonic-unix "")
  357. (which-function-mode 0)
  358. (line-number-mode 0)
  359. (column-number-mode 0)
  360. (size-indication-mode 0)
  361. (setq mode-line-position
  362. '(:eval (format "L%%l/%d:C%%c"
  363. (count-lines (point-max)
  364. (point-min)))))
  365. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  366. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  367. ;; minibuffer
  368. (setq insert-default-directory t)
  369. (setq completion-ignore-case t
  370. read-file-name-completion-ignore-case t
  371. read-buffer-completion-ignore-case t)
  372. (setq resize-mini-windows t)
  373. (temp-buffer-resize-mode 1)
  374. (savehist-mode 1)
  375. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  376. (set-variable 'help-at-pt-display-when-idle t)
  377. (fset 'yes-or-no-p 'y-or-n-p)
  378. ;; complete symbol when `eval'
  379. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  380. (define-key minibuffer-local-map (kbd "C-u")
  381. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  382. ;; I dont know these bindings are good
  383. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  384. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  385. (when (safe-require-or-eval 'minibuffer-line)
  386. (set-face-underline 'minibuffer-line nil)
  387. (set-variable 'minibuffer-line-refresh-interval
  388. 25)
  389. ;; Set idle timer
  390. (defvar my-minibuffer-line--idle-timer nil)
  391. (defvar minibuffer-line-mode)
  392. (add-hook 'minibuffer-line-mode-hook
  393. (lambda ()
  394. (when my-minibuffer-line--idle-timer
  395. (cancel-timer my-minibuffer-line--idle-timer)
  396. (setq my-minibuffer-line--idle-timer nil))
  397. (when minibuffer-line-mode
  398. (setq my-minibuffer-line--idle-timer
  399. (run-with-idle-timer 0.5
  400. t
  401. 'minibuffer-line--update)))))
  402. (set-variable 'minibuffer-line-format
  403. `(,(concat user-login-name
  404. "@"
  405. (car (split-string (system-name)
  406. "\\."))
  407. ":")
  408. (:eval (abbreviate-file-name (or buffer-file-name
  409. default-directory)))
  410. (:eval (and (fboundp 'git-ps1-mode-get-current)
  411. (git-ps1-mode-get-current " [GIT:%s]")))
  412. " "
  413. (:eval (format-time-string display-time-format))))
  414. (minibuffer-line-mode 1)
  415. )
  416. (when (safe-require-or-eval 'prompt-text)
  417. (set-variable 'prompt-text-format
  418. `(,(concat ""
  419. user-login-name
  420. "@"
  421. (car (split-string (system-name)
  422. "\\."))
  423. ":")
  424. (:eval (abbreviate-file-name (or buffer-file-name
  425. default-directory)))
  426. (:eval (and (fboundp 'git-ps1-mode-get-current)
  427. (git-ps1-mode-get-current " [GIT:%s]")))
  428. " "
  429. (:eval (format-time-string display-time-format))
  430. "\n"
  431. (:eval (symbol-name this-command))
  432. ": "))
  433. (prompt-text-mode 1))
  434. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  435. ;; letters, font-lock mode and fonts
  436. (setq text-quoting-style 'grave)
  437. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  438. ;; (set-window-margins (selected-window) 1 1)
  439. (unless window-system
  440. (setq frame-background-mode 'dark))
  441. (and (or (eq system-type 'Darwin)
  442. (eq system-type 'darwin))
  443. (fboundp 'mac-set-input-method-parameter)
  444. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  445. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  446. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  447. (boundp 'input-method-inactivate-hook))
  448. (add-hook 'input-method-activate-hook
  449. (lambda () (set-cursor-color "red")))
  450. (add-hook 'input-method-inactivate-hook
  451. (lambda () (set-cursor-color "black"))))
  452. (when (safe-require-or-eval 'paren)
  453. (show-paren-mode 1)
  454. (setq show-paren-delay 0.5
  455. show-paren-style 'parenthesis) ; mixed is hard to read
  456. ;; (set-face-background 'show-paren-match
  457. ;; "black")
  458. ;; ;; (face-foreground 'default))
  459. ;; (set-face-foreground 'show-paren-match
  460. ;; "white")
  461. ;; (set-face-inverse-video-p 'show-paren-match
  462. ;; t)
  463. )
  464. (transient-mark-mode 1)
  465. (global-font-lock-mode 1)
  466. (setq font-lock-global-modes
  467. '(not
  468. help-mode
  469. eshell-mode
  470. ;;term-mode
  471. Man-mode))
  472. ;; (standard-display-ascii ?\n "$\n")
  473. ;; (defvar my-eol-face
  474. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  475. ;; )
  476. ;; (defvar my-tab-face
  477. ;; '(("\t" . '(0 highlight t nil))))
  478. (defvar my-jspace-face
  479. '(("\u3000" . '(0 highlight t nil))))
  480. (add-hook 'font-lock-mode-hook
  481. (lambda ()
  482. ;; (font-lock-add-keywords nil my-eol-face)
  483. (font-lock-add-keywords nil my-jspace-face)
  484. ))
  485. (when (safe-require-or-eval 'whitespace)
  486. (add-to-list 'whitespace-display-mappings
  487. ;; We need t since last one takes precedence
  488. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  489. ;; (add-to-list 'whitespace-display-mappings
  490. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  491. (setq whitespace-style '(face
  492. trailing ; trailing blanks
  493. ;; tabs
  494. ;; spaces
  495. ;; lines
  496. lines-tail ; lines over 80
  497. newline ; newlines
  498. empty ; empty lines at beg or end of buffer
  499. ;; big-indent
  500. ;; space-mark
  501. tab-mark
  502. newline-mark ; use display table for newline
  503. ))
  504. ;; (setq whitespace-newline 'font-lock-comment-face)
  505. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  506. (defun my-whitesspace-mode-reload ()
  507. "Reload whitespace-mode config."
  508. (interactive)
  509. (when whitespace-mode
  510. (whitespace-mode 0)
  511. (whitespace-mode 1)))
  512. (set-variable 'whitespace-line-column nil)
  513. (global-whitespace-mode t)
  514. (add-hook 'dired-mode-hook
  515. (lambda ()
  516. (set (make-local-variable 'whitespace-style) nil)))
  517. (if (eq (display-color-cells)
  518. 256)
  519. (set-face-foreground 'whitespace-newline "color-109")
  520. ;; (progn
  521. ;; (set-face-bold-p 'whitespace-newline
  522. ;; t))
  523. ))
  524. (and nil
  525. '(safe-require-or-eval 'fill-column-indicator)
  526. (setq fill-column-indicator))
  527. ;; highlight current line
  528. ;; http://wiki.riywo.com/index.php?Meadow
  529. (face-spec-set 'hl-line
  530. '((((min-colors 256)
  531. (background dark))
  532. (:background "color-234"))
  533. (((min-colors 256)
  534. (background light))
  535. (:background "color-234"))
  536. (t
  537. (:underline "black"))))
  538. (set-variable 'hl-line-global-modes
  539. '(not
  540. term-mode))
  541. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  542. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  543. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  544. ;;(safe-require-or-eval 'set-modeline-color)
  545. ;; (let ((fg (face-foreground 'default))
  546. ;; (bg (face-background 'default)))
  547. ;; (set-face-background 'mode-line-inactive
  548. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  549. ;; (set-face-foreground 'mode-line-inactive
  550. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  551. ;; (set-face-underline 'mode-line-inactive
  552. ;; t)
  553. ;; (set-face-underline 'vertical-border
  554. ;; nil)
  555. (when (safe-require-or-eval 'end-mark)
  556. (global-end-mark-mode))
  557. ;; M-x highlight-* to highlight things
  558. (global-hi-lock-mode 1)
  559. (unless (fboundp 'highlight-region-text)
  560. (defun highlight-region-text (beg end)
  561. "Highlight text between BEG and END."
  562. (interactive "r")
  563. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  564. end)))
  565. (setq deactivate-mark t)))
  566. (when (safe-require-or-eval 'auto-highlight-symbol)
  567. (set-variable 'ahs-idle-interval 0.6)
  568. (global-auto-highlight-symbol-mode 1))
  569. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  570. ;; file handling
  571. (when (safe-require-or-eval 'editorconfig)
  572. ;; (set-variable 'editorconfig-get-properties-function
  573. ;; 'editorconfig-core-get-properties-hash)
  574. (editorconfig-mode 1)
  575. (set-variable 'editorconfig-mode-lighter " EC")
  576. (with-eval-after-load 'org-src
  577. ;; [*.org\[\*Org Src*\[ c \]*\]]
  578. (add-hook 'org-src-mode-hook
  579. 'editorconfig-mode-apply t)))
  580. (when (fboundp 'editorconfig-custom-majormode)
  581. (add-hook 'editorconfig-custom-hooks
  582. 'editorconfig-custom-majormode))
  583. ;; Add readonly=true to set read-only-mode
  584. (add-hook 'editorconfig-custom-hooks
  585. (lambda (props)
  586. (let ((r (gethash 'readonly props)))
  587. (when (and (string= r "true")
  588. (not buffer-read-only))
  589. (read-only-mode 1)))))
  590. ;; (when (fboundp 'editorconfig-charset-extras)
  591. ;; (add-hook 'editorconfig-custom-hooks
  592. ;; 'editorconfig-charset-extras))
  593. (setq revert-without-query '(".+"))
  594. ;; save cursor position
  595. (when (safe-require-or-eval 'saveplace)
  596. (setq-default save-place t)
  597. (setq save-place-file (concat user-emacs-directory
  598. "places")))
  599. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  600. (setq make-backup-files t)
  601. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  602. (setq backup-directory-alist
  603. (cons (cons "\\.*$" (expand-file-name (concat user-emacs-directory
  604. "backup")))
  605. backup-directory-alist))
  606. (setq version-control 'never)
  607. (setq delete-old-versions t)
  608. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  609. "auto-save/")))
  610. (setq delete-auto-save-files t)
  611. (add-to-list 'completion-ignored-extensions ".bak")
  612. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  613. (setq delete-by-moving-to-trash t)
  614. ;; trash-directory "~/.emacs.d/trash")
  615. (add-hook 'after-save-hook
  616. 'executable-make-buffer-file-executable-if-script-p)
  617. (set-variable 'bookmark-default-file
  618. (expand-file-name (concat user-emacs-directory
  619. "bmk")))
  620. (set-variable 'bookmark-save-flag
  621. 1)
  622. (with-eval-after-load 'recentf
  623. (defvar recentf-exclude)
  624. (defvar bookmark-default-file)
  625. (add-to-list 'recentf-exclude
  626. (regexp-quote bookmark-default-file)))
  627. (when (safe-require-or-eval 'smart-revert)
  628. (smart-revert-on))
  629. ;; autosave
  630. (when (safe-require-or-eval 'autosave)
  631. (autosave-set 8))
  632. ;; bookmarks
  633. (define-key ctl-x-map "m" 'list-bookmarks)
  634. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  635. ;; buffer killing
  636. ;; (defun my-delete-window-killing-buffer () nil)
  637. (defun my-query-kill-current-buffer ()
  638. "Interactively kill current buffer."
  639. (interactive)
  640. (if (y-or-n-p (concat "kill current buffer? :"))
  641. (kill-buffer (current-buffer))))
  642. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  643. (substitute-key-definition 'kill-buffer
  644. 'my-query-kill-current-buffer
  645. global-map)
  646. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  647. ;; share clipboard with x
  648. ;; this page describes this in details, but only these sexps seem to be needed
  649. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  650. (and nil
  651. (not window-system)
  652. (not (eq window-system 'mac))
  653. (getenv "DISPLAY")
  654. (not (equal (getenv "DISPLAY") ""))
  655. (executable-find "xclip")
  656. ;; (< emacs-major-version 24)
  657. '(safe-require-or-eval 'xclip)
  658. nil
  659. (turn-on-xclip))
  660. (and (eq system-type 'darwin)
  661. (safe-require-or-eval 'pasteboard)
  662. (turn-on-pasteboard))
  663. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  664. ;; some modes and hooks
  665. ;; Include some extra modes
  666. (require 'generic-x)
  667. (with-eval-after-load 'remember
  668. (defvar remember-mode-map (make-sparse-keymap))
  669. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  670. (with-eval-after-load 'magit-files
  671. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  672. ;; existing keybindings.
  673. ;; Apparently it is a HARMFUL behavior and should be fixed, but for now
  674. ;; disable this mode here.
  675. (global-magit-file-mode -1))
  676. (when (safe-require-or-eval 'aggressive-indent)
  677. (defvar aggressive-indent-excluded-modes)
  678. (add-to-list 'aggressive-indent-excluded-modes
  679. 'diff-mode)
  680. (global-aggressive-indent-mode 1))
  681. (when (autoload-eval-lazily 'ggtags)
  682. (add-hook 'c-mode-common-hook
  683. (lambda ()
  684. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  685. (ggtags-mode 1))))
  686. (add-hook 'python-mode-hook
  687. (lambda ()
  688. (ggtags-mode 1))))
  689. (when (autoload-eval-lazily 'imenu-list)
  690. ;; (set-variable 'imenu-list-auto-resize t)
  691. (set-variable 'imenu-list-focus-after-activation t)
  692. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  693. (with-eval-after-load 'compile
  694. (defvar compilation-filter-start)
  695. (defvar compilation-error-regexp-alist)
  696. (require 'ansi-color)
  697. (add-hook 'compilation-filter-hook
  698. (lambda ()
  699. (let ((inhibit-read-only t))
  700. (ansi-color-apply-on-region compilation-filter-start
  701. (point)))))
  702. (add-to-list 'compilation-error-regexp-alist
  703. ;; ansible-lint
  704. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  705. ;; Workaround to avoid ensime error
  706. (defvar ensime-mode-key-prefix nil)
  707. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  708. (when (safe-require-or-eval 'company)
  709. (global-company-mode)
  710. (set-variable 'company-idle-delay 0.5)
  711. (set-variable 'company-minimum-prefix-length 2)
  712. (set-variable 'company-selection-wrap-around t))
  713. ;; https://github.com/lunaryorn/flycheck
  714. (when (safe-require-or-eval 'flycheck)
  715. (call-after-init 'global-flycheck-mode))
  716. (when (autoload-eval-lazily 'ilookup)
  717. (define-key ctl-x-map "d" 'ilookup-open-word))
  718. (set-variable 'ac-ignore-case nil)
  719. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  720. (define-key ctl-x-map "t" 'term-run-shell-command))
  721. (add-to-list 'safe-local-variable-values
  722. '(encoding utf-8))
  723. (setq enable-local-variables :safe)
  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