Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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