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.
 
 
 
 
 
 

1734 lines
56 KiB

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