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.
 
 
 
 
 
 

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