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.
 
 
 
 
 
 

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