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.
 
 
 
 
 
 

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