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.
 
 
 
 
 
 

1710 lines
55 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. ;; is flymake installs are required?
  88. ;;flymake-jshint
  89. ;;flymake-python-pyflakes
  90. xclip
  91. foreign-regexp
  92. multi-term
  93. term-run
  94. editorconfig
  95. git-ps1-mode
  96. restart-emacs
  97. fill-column-indicator
  98. pkgbuild-mode
  99. minibuffer-line
  100. which-key
  101. ;; I think this works in place of my autosave lib
  102. super-save
  103. pipenv
  104. imenu-list
  105. page-break-lines
  106. scala-mode
  107. ;;ensime
  108. editorconfig
  109. editorconfig-custom-majormode
  110. git-command
  111. prompt-text
  112. ;; 10sr repository
  113. ;; 10sr-extras
  114. terminal-title
  115. recentf-show
  116. dired-list-all-mode
  117. pack
  118. set-modeline-color
  119. read-only-only-mode
  120. smart-revert
  121. autosave
  122. ;;window-organizer
  123. remember-major-modes-mode
  124. ilookup
  125. pasteboard
  126. end-mark
  127. sl
  128. gosh-mode
  129. ))
  130. (when (safe-require-or-eval 'package)
  131. (setq package-archives
  132. `(,@package-archives
  133. ("melpa" . "https://melpa.org/packages/")
  134. ;; Somehow fails to download via https
  135. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  136. (package-initialize)
  137. (defun my-auto-install-package ()
  138. "Install packages semi-automatically."
  139. (interactive)
  140. (package-refresh-contents)
  141. (mapc (lambda (pkg)
  142. (or (package-installed-p pkg)
  143. (package-install pkg)))
  144. 10sr-package-list))
  145. )
  146. ;; (lazy-load-eval 'sudoku)
  147. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  148. ;; my-idle-hook
  149. (defvar my-idle-hook nil
  150. "Hook run when idle for several secs.")
  151. (defvar my-idle-hook-sec 5
  152. "Second to run `my-idle-hook'.")
  153. (run-with-idle-timer my-idle-hook-sec
  154. t
  155. (lambda ()
  156. (run-hooks 'my-idle-hook)))
  157. ;; (add-hook 'my-idle-hook
  158. ;; (lambda ()
  159. ;; (message "idle hook message")))
  160. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  161. ;; start and quit
  162. (setq inhibit-startup-message t)
  163. (setq confirm-kill-emacs 'y-or-n-p)
  164. (setq gc-cons-threshold (* 1024 1024 4))
  165. (setq garbage-collection-messages t)
  166. (when window-system
  167. (add-to-list 'default-frame-alist '(cursor-type . box))
  168. (add-to-list 'default-frame-alist '(background-color . "white"))
  169. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  170. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  171. ;; does not work?
  172. )
  173. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  174. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  175. (and (fboundp 'tool-bar-mode)
  176. (tool-bar-mode 0))
  177. (and (fboundp 'set-scroll-bar-mode)
  178. (set-scroll-bar-mode nil))
  179. (call-after-init
  180. (lambda ()
  181. (message "%s %s" invocation-name emacs-version)
  182. (message "Invocation directory: %s" default-directory)
  183. (message "%s was taken to initialize emacs." (emacs-init-time))
  184. (view-echo-area-messages)
  185. ;; (view-emacs-news)
  186. ))
  187. (cd ".") ; when using windows use / instead of \ in `default-directory'
  188. ;; locale
  189. (set-language-environment "Japanese")
  190. (set-default-coding-systems 'utf-8-unix)
  191. (prefer-coding-system 'utf-8-unix)
  192. (setq system-time-locale "C")
  193. ;; my prefix map
  194. (defvar my-prefix-map nil
  195. "My prefix map.")
  196. (define-prefix-command 'my-prefix-map)
  197. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  198. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  199. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  200. ;; (comint-show-maximum-output)
  201. ;; kill scratch
  202. (call-after-init (lambda ()
  203. (let ((buf (get-buffer "*scratch*")))
  204. (when buf
  205. (kill-buffer buf)))))
  206. ;; modifier keys
  207. ;; (setq mac-option-modifier 'control)
  208. ;; display
  209. (setq visible-bell t)
  210. (setq ring-bell-function 'ignore)
  211. (mouse-avoidance-mode 'banish)
  212. (setq echo-keystrokes 0.1)
  213. (defun reload-init-file ()
  214. "Reload Emacs init file."
  215. (interactive)
  216. (when (and user-init-file
  217. (file-readable-p user-init-file))
  218. (load-file user-init-file)))
  219. (safe-require-or-eval 'session)
  220. ;; server
  221. (set-variable 'server-name (concat "server"
  222. (number-to-string (emacs-pid))))
  223. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  224. ;; In Darwin environment, init fails with message like 'Service name too long'
  225. ;; when server-use-tcp is nil
  226. (when (or (eq system-type
  227. 'cygwin)
  228. (eq system-type
  229. 'darwin))
  230. (set-variable 'server-use-tcp t))
  231. ;; MSYS2 fix
  232. (when (eq system-type
  233. 'windows-nt)
  234. (setq shell-file-name
  235. (executable-find "bash"))
  236. '(setq function-key-map
  237. `(,@function-key-map ([pause] . [?\C-c])
  238. ))
  239. (define-key key-translation-map
  240. (kbd "<pause>")
  241. (kbd "C-c"))
  242. '(keyboard-translate [pause]
  243. (kbd "C-c")p)
  244. ;; TODO: move to other place later
  245. (when (not window-system)
  246. (setq interprogram-paste-function nil)
  247. (setq interprogram-cut-function nil)))
  248. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  249. ;; global keys
  250. (global-set-key (kbd "<up>") 'scroll-down-line)
  251. (global-set-key (kbd "<down>") 'scroll-up-line)
  252. (global-set-key (kbd "<left>") 'scroll-down)
  253. (global-set-key (kbd "<right>") 'scroll-up)
  254. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  255. (global-set-key (kbd "C-\\") help-map)
  256. (define-key ctl-x-map (kbd "DEL") help-map)
  257. (define-key ctl-x-map (kbd "C-h") help-map)
  258. (define-key help-map "a" 'apropos)
  259. ;; disable annoying keys
  260. (global-set-key [prior] 'ignore)
  261. (global-set-key (kbd "<next>") 'ignore)
  262. (global-set-key [menu] 'ignore)
  263. (global-set-key [down-mouse-1] 'ignore)
  264. (global-set-key [down-mouse-2] 'ignore)
  265. (global-set-key [down-mouse-3] 'ignore)
  266. (global-set-key [mouse-1] 'ignore)
  267. (global-set-key [mouse-2] 'ignore)
  268. (global-set-key [mouse-3] 'ignore)
  269. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  270. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  271. (when (safe-require-or-eval 'which-key)
  272. (which-key-mode))
  273. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  274. ;; editting
  275. (setq kill-whole-line t)
  276. (setq scroll-conservatively 35
  277. scroll-margin 2
  278. scroll-step 0)
  279. (setq-default major-mode 'text-mode)
  280. (setq next-line-add-newlines nil)
  281. (setq kill-read-only-ok t)
  282. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  283. ;; (setq-default line-spacing 0.2)
  284. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  285. ;; (setq-default tab-width 4)
  286. (setq-default indent-tabs-mode nil)
  287. (setq-default indent-line-function 'indent-to-left-margin)
  288. ;; (setq-default indent-line-function nil)
  289. (setq-default truncate-lines nil)
  290. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  291. (delete-selection-mode 1)
  292. (cua-mode 0)
  293. (setq line-move-visual nil)
  294. ;; key bindings
  295. ;; moving around
  296. ;;(keyboard-translate ?\M-j ?\C-j)
  297. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  298. (define-key esc-map "p" 'backward-paragraph)
  299. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  300. (define-key esc-map "n" 'forward-paragraph)
  301. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  302. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  303. (global-set-key (kbd "C-<left>") 'scroll-down)
  304. (global-set-key (kbd "C-<right>") 'scroll-up)
  305. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  306. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  307. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  308. ;; C-h and DEL
  309. (global-set-key (kbd "C-h") (kbd "DEL"))
  310. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  311. (global-set-key (kbd "C-m") 'newline-and-indent)
  312. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  313. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  314. (define-key esc-map "u" 'undo)
  315. (define-key esc-map "i" (kbd "ESC TAB"))
  316. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  317. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  318. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  319. (define-key my-prefix-map (kbd "C-o") 'occur)
  320. (require 'page-ext nil t)
  321. (when (safe-require-or-eval 'page-break-lines)
  322. (global-page-break-lines-mode 1))
  323. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  324. ;; title and mode-line
  325. (when (safe-require-or-eval 'terminal-title)
  326. ;; if TERM is not screen use default value
  327. (if (getenv "TMUX")
  328. ;; if use tmux locally just basename of current dir
  329. (set-variable 'terminal-title-format
  330. '((file-name-nondirectory (directory-file-name
  331. default-directory))))
  332. (if (and (let ((tty-type (frame-parameter nil
  333. 'tty-type)))
  334. (and tty-type
  335. (equal (car (split-string tty-type
  336. "-"))
  337. "screen")))
  338. (not (getenv "SSH_CONNECTION")))
  339. (set-variable 'terminal-title-format
  340. '((file-name-nondirectory (directory-file-name
  341. default-directory))))
  342. ;; seems that TMUX is used locally and ssh to remote host
  343. (set-variable 'terminal-title-format
  344. `("em:"
  345. ,user-login-name
  346. "@"
  347. ,(car (split-string (system-name)
  348. "\\."))
  349. ":"
  350. default-directory))
  351. )
  352. )
  353. (terminal-title-mode))
  354. (setq eol-mnemonic-dos "\\r\\n")
  355. (setq eol-mnemonic-mac "\\r")
  356. (setq eol-mnemonic-unix "")
  357. (which-function-mode 0)
  358. (line-number-mode 0)
  359. (column-number-mode 0)
  360. (size-indication-mode 0)
  361. (setq mode-line-position
  362. '(:eval (format "L%%l/%d:C%%c"
  363. (count-lines (point-max)
  364. (point-min)))))
  365. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  366. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  367. ;; minibuffer
  368. (setq insert-default-directory t)
  369. (setq completion-ignore-case t
  370. read-file-name-completion-ignore-case t
  371. read-buffer-completion-ignore-case t)
  372. (setq resize-mini-windows t)
  373. (temp-buffer-resize-mode 1)
  374. (savehist-mode 1)
  375. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  376. (set-variable 'help-at-pt-display-when-idle t)
  377. (fset 'yes-or-no-p 'y-or-n-p)
  378. ;; complete symbol when `eval'
  379. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  380. (define-key minibuffer-local-map (kbd "C-u")
  381. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  382. ;; I dont know these bindings are good
  383. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  384. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  385. (when (safe-require-or-eval 'minibuffer-line)
  386. (set-face-underline 'minibuffer-line nil)
  387. (set-variable 'minibuffer-line-refresh-interval
  388. 25)
  389. ;; Set idle timer
  390. (defvar my-minibuffer-line--idle-timer nil)
  391. (defvar minibuffer-line-mode)
  392. (add-hook 'minibuffer-line-mode-hook
  393. (lambda ()
  394. (when my-minibuffer-line--idle-timer
  395. (cancel-timer my-minibuffer-line--idle-timer)
  396. (setq my-minibuffer-line--idle-timer nil))
  397. (when minibuffer-line-mode
  398. (setq my-minibuffer-line--idle-timer
  399. (run-with-idle-timer 0.5
  400. t
  401. 'minibuffer-line--update)))))
  402. (set-variable 'minibuffer-line-format
  403. `(,(concat user-login-name
  404. "@"
  405. (car (split-string (system-name)
  406. "\\."))
  407. ":")
  408. (:eval (abbreviate-file-name (or buffer-file-name
  409. default-directory)))
  410. (:eval (and (fboundp 'git-ps1-mode-get-current)
  411. (git-ps1-mode-get-current " [GIT:%s]")))
  412. " "
  413. (:eval (format-time-string display-time-format))))
  414. (minibuffer-line-mode 1)
  415. )
  416. (when (safe-require-or-eval 'prompt-text)
  417. (set-variable 'prompt-text-format
  418. `(,(concat ""
  419. user-login-name
  420. "@"
  421. (car (split-string (system-name)
  422. "\\."))
  423. ":")
  424. (:eval (abbreviate-file-name (or buffer-file-name
  425. default-directory)))
  426. (:eval (and (fboundp 'git-ps1-mode-get-current)
  427. (git-ps1-mode-get-current " [GIT:%s]")))
  428. " "
  429. (:eval (format-time-string display-time-format))
  430. "\n"
  431. (:eval (symbol-name this-command))
  432. ": "))
  433. (prompt-text-mode 1))
  434. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  435. ;; letters, font-lock mode and fonts
  436. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  437. ;; (set-window-margins (selected-window) 1 1)
  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 (autoload-eval-lazily 'ggtags)
  644. (add-hook 'c-mode-common-hook
  645. (lambda ()
  646. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  647. (ggtags-mode 1))))
  648. (add-hook 'python-mode-hook
  649. (lambda ()
  650. (ggtags-mode 1))))
  651. (when (autoload-eval-lazily 'imenu-list)
  652. ;; (set-variable 'imenu-list-auto-resize t)
  653. (set-variable 'imenu-list-focus-after-activation t)
  654. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  655. (with-eval-after-load 'compile
  656. (defvar compilation-filter-start)
  657. (defvar compilation-error-regexp-alist)
  658. (require 'ansi-color)
  659. (add-hook 'compilation-filter-hook
  660. (lambda ()
  661. (let ((inhibit-read-only t))
  662. (ansi-color-apply-on-region compilation-filter-start
  663. (point)))))
  664. (add-to-list 'compilation-error-regexp-alist
  665. ;; ansible-lint
  666. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  667. ;; Workaround to avoid ensime error
  668. (defvar ensime-mode-key-prefix nil)
  669. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  670. (when (safe-require-or-eval 'company)
  671. (global-company-mode)
  672. (set-variable 'company-idle-delay 0.5)
  673. (set-variable 'company-minimum-prefix-length 2)
  674. (set-variable 'company-selection-wrap-around t))
  675. ;; https://github.com/lunaryorn/flycheck
  676. (when (safe-require-or-eval 'flycheck)
  677. (call-after-init 'global-flycheck-mode))
  678. (when (autoload-eval-lazily 'ilookup)
  679. (define-key ctl-x-map "d" 'ilookup-open-word))
  680. (set-variable 'ac-ignore-case nil)
  681. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  682. (define-key ctl-x-map "t" 'term-run-shell-command))
  683. (add-to-list 'safe-local-variable-values
  684. '(encoding utf-8))
  685. (setq enable-local-variables :safe)
  686. ;; (when (safe-require-or-eval 'remember-major-modes-mode)
  687. ;; (remember-major-modes-mode 1))
  688. ;; Detect file type from shebang and set major-mode.
  689. (add-to-list 'interpreter-mode-alist
  690. '("python3" . python-mode))
  691. (add-to-list 'interpreter-mode-alist
  692. '("python2" . python-mode))
  693. (with-eval-after-load 'python
  694. (defvar python-mode-map (make-sparse-keymap))
  695. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  696. (when (autoload-eval-lazily 'pipenv)
  697. (add-hook 'python-mode-hook
  698. (lambda ()
  699. (pipenv-mode 1)
  700. (pipenv-projectile-after-switch-default)))
  701. )
  702. ;; http://fukuyama.co/foreign-regexp
  703. '(and (safe-require-or-eval 'foreign-regexp)
  704. (progn
  705. (setq foreign-regexp/regexp-type 'perl)
  706. '(setq reb-re-syntax 'foreign-regexp)
  707. ))
  708. (autoload-eval-lazily 'sql '(sql-mode)
  709. (require 'sql-indent nil t))
  710. (when (autoload-eval-lazily 'git-command)
  711. (define-key ctl-x-map "g" 'git-command))
  712. (when (safe-require-or-eval 'git-commit)
  713. (global-git-commit-mode 1))
  714. (with-eval-after-load 'git-commit
  715. (add-hook 'git-commit-setup-hook
  716. 'turn-off-auto-fill t))
  717. (autoload-eval-lazily 'sl)
  718. (with-eval-after-load 'rst
  719. (defvar rst-mode-map)
  720. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  721. (with-eval-after-load 'jdee
  722. (add-hook 'jdee-mode-hook
  723. (lambda ()
  724. (make-local-variable 'global-mode-string)
  725. (add-to-list 'global-mode-string
  726. mode-line-position))))
  727. ;; Cannot enable error thrown. Why???
  728. ;; https://github.com/m0smith/malabar-mode#Installation
  729. ;; (when (autoload-eval-lazily 'malabar-mode)
  730. ;; (add-to-list 'load-path
  731. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  732. ;; (safe-require-or-eval 'cedet-devel-load)
  733. ;; (call-after-init 'activate-malabar-mode))
  734. (with-eval-after-load 'make-mode
  735. (defvar makefile-mode-map (make-sparse-keymap))
  736. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  737. ;; this functions is set in write-file-functions, i cannot find any
  738. ;; good way to remove this.
  739. (fset 'makefile-warn-suspicious-lines 'ignore))
  740. (with-eval-after-load 'verilog-mode
  741. (defvar verilog-mode-map (make-sparse-keymap))
  742. (define-key verilog-mode-map ";" 'self-insert-command))
  743. (setq diff-switches "-u")
  744. (with-eval-after-load 'diff-mode
  745. ;; (when (and (eq major-mode
  746. ;; 'diff-mode)
  747. ;; (not buffer-file-name))
  748. ;; ;; do not pass when major-mode is derived mode of diff-mode
  749. ;; (view-mode 1))
  750. (set-face-attribute 'diff-header nil
  751. :foreground nil
  752. :background nil
  753. :weight 'bold)
  754. (set-face-attribute 'diff-file-header nil
  755. :foreground nil
  756. :background nil
  757. :weight 'bold)
  758. (set-face-foreground 'diff-index "blue")
  759. (set-face-attribute 'diff-hunk-header nil
  760. :foreground "cyan"
  761. :weight 'normal)
  762. (set-face-attribute 'diff-context nil
  763. ;; :foreground "white"
  764. :foreground nil
  765. :weight 'normal)
  766. (set-face-foreground 'diff-removed "red")
  767. (set-face-foreground 'diff-added "green")
  768. (set-face-background 'diff-removed nil)
  769. (set-face-background 'diff-added nil)
  770. (set-face-attribute 'diff-changed nil
  771. :foreground "magenta"
  772. :weight 'normal)
  773. (set-face-attribute 'diff-refine-changed nil
  774. :foreground nil
  775. :background nil
  776. :weight 'bold
  777. :inverse-video t)
  778. ;; Annoying !
  779. ;;(diff-auto-refine-mode)
  780. )
  781. ;; (ffap-bindings)
  782. (set-variable 'browse-url-browser-function
  783. 'eww-browse-url)
  784. (set-variable 'sh-here-document-word "__EOC__")
  785. (when (autoload-eval-lazily 'adoc-mode
  786. nil
  787. (defvar adoc-mode-map (make-sparse-keymap))
  788. (define-key adoc-mode-map (kbd "C-m") 'newline))
  789. (setq auto-mode-alist
  790. `(("\\.adoc\\'" . adoc-mode)
  791. ("\\.asciidoc\\'" . adoc-mode)
  792. ,@auto-mode-alist)))
  793. (with-eval-after-load 'markup-faces
  794. ;; Is this too match ?
  795. (set-face-foreground 'markup-meta-face
  796. "color-245")
  797. (set-face-foreground 'markup-meta-hide-face
  798. "color-245")
  799. )
  800. ;; TODO: check if this is required
  801. (when (autoload-eval-lazily 'groovy-mode nil
  802. (defvar groovy-mode-map (make-sparse-keymap))
  803. (define-key groovy-mode-map "(" 'self-insert-command)
  804. (define-key groovy-mode-map ")" 'self-insert-command)
  805. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  806. )
  807. (add-to-list 'auto-mode-alist
  808. '("build\\.gradle\\'" . groovy-mode)))
  809. (with-eval-after-load 'yaml-mode
  810. (defvar yaml-mode-map (make-sparse-keymap))
  811. (define-key yaml-mode-map (kbd "C-m") 'newline))
  812. (with-eval-after-load 'html-mode
  813. (defvar html-mode-map (make-sparse-keymap))
  814. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  815. (with-eval-after-load 'text-mode
  816. (define-key text-mode-map (kbd "C-m") 'newline))
  817. (autoload-eval-lazily 'info nil
  818. (defvar Info-additional-directory-list)
  819. (dolist (dir (directory-files (concat user-emacs-directory
  820. "info")
  821. t
  822. "^[^.].*"))
  823. (when (file-directory-p dir)
  824. (add-to-list 'Info-additional-directory-list
  825. dir)))
  826. (let ((dir (expand-file-name "~/.brew/share/info")))
  827. (when (file-directory-p dir)
  828. (add-to-list 'Info-additional-directory-list
  829. dir))))
  830. (with-eval-after-load 'apropos
  831. (defvar apropos-mode-map (make-sparse-keymap))
  832. (define-key apropos-mode-map "n" 'next-line)
  833. (define-key apropos-mode-map "p" 'previous-line))
  834. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  835. ;; (define-key isearch-mode-map
  836. ;; (kbd "C-j") 'isearch-other-control-char)
  837. ;; (define-key isearch-mode-map
  838. ;; (kbd "C-k") 'isearch-other-control-char)
  839. ;; (define-key isearch-mode-map
  840. ;; (kbd "C-h") 'isearch-other-control-char)
  841. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  842. (define-key isearch-mode-map (kbd "M-r")
  843. 'isearch-query-replace-regexp)
  844. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  845. (setq lazy-highlight-cleanup nil)
  846. ;; face for isearch highlighing
  847. (set-face-attribute 'lazy-highlight
  848. nil
  849. :foreground `unspecified
  850. :background `unspecified
  851. :underline t
  852. ;; :weight `bold
  853. )
  854. (add-hook 'outline-mode-hook
  855. (lambda ()
  856. (when (string-match "\\.md\\'" buffer-file-name)
  857. (set (make-local-variable 'outline-regexp) "#+ "))))
  858. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  859. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  860. (when (autoload-eval-lazily 'markdown-mode
  861. '(markdown-mode gfm-mode)
  862. (defvar gfm-mode-map (make-sparse-keymap))
  863. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  864. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  865. (set-variable 'markdown-command (or (executable-find "markdown")
  866. (executable-find "markdown.pl")
  867. ""))
  868. (add-hook 'markdown-mode-hook
  869. (lambda ()
  870. (outline-minor-mode 1)
  871. (flyspell-mode)
  872. (set (make-local-variable 'comment-start) ";")))
  873. )
  874. ;; c-mode
  875. ;; http://www.emacswiki.org/emacs/IndentingC
  876. ;; http://en.wikipedia.org/wiki/Indent_style
  877. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  878. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  879. (with-eval-after-load 'cc-vars
  880. (defvar c-default-style nil)
  881. (add-to-list 'c-default-style
  882. '(c-mode . "k&r"))
  883. (add-to-list 'c-default-style
  884. '(c++-mode . "k&r")))
  885. (autoload-eval-lazily 'js2-mode nil
  886. ;; currently do not use js2-mode
  887. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  888. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  889. ;; (defvar js2-mode-map (make-sparse-keymap))
  890. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  891. ;; (interactive)
  892. ;; (js2-enter-key)
  893. ;; (indent-for-tab-command)))
  894. ;; (add-hook (kill-local-variable 'before-save-hook)
  895. ;; 'js2-before-save)
  896. ;; (add-hook 'before-save-hook
  897. ;; 'my-indent-buffer
  898. ;; nil
  899. ;; t)
  900. )
  901. (add-to-list 'interpreter-mode-alist
  902. '("node" . js-mode))
  903. (when (autoload-eval-lazily 'flymake-jslint
  904. '(flymake-jslint-load))
  905. (autoload-eval-lazily 'js nil
  906. (add-hook 'js-mode-hook
  907. 'flymake-jslint-load)))
  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. (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. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1023. (remove-hook 'find-file-hook
  1024. 'recentf-track-opened-file)
  1025. (defun my-recentf-load-track-save-list ()
  1026. "Load current recentf list from file, track current visiting file, then save
  1027. the list."
  1028. (recentf-load-list)
  1029. (recentf-track-opened-file)
  1030. (recentf-save-list))
  1031. (add-hook 'find-file-hook
  1032. 'my-recentf-load-track-save-list)
  1033. (add-hook 'kill-emacs-hook
  1034. 'recentf-load-list)
  1035. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1036. ;; (add-hook 'find-file-hook
  1037. ;; (lambda ()
  1038. ;; (recentf-add-file default-directory)))
  1039. (and (autoload-eval-lazily 'recentf-show)
  1040. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1041. (add-hook 'recentf-show-before-listing-hook
  1042. 'recentf-load-list))
  1043. (recentf-mode 1)
  1044. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1045. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1046. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1047. (define-key recentf-dialog-mode-map "n" 'next-line)
  1048. (add-hook 'recentf-dialog-mode-hook
  1049. (lambda ()
  1050. ;; (recentf-save-list)
  1051. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1052. ;; 'my-recentf-cd-and-find-file)
  1053. (cd "~/"))))
  1054. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1055. ;; dired
  1056. (defun my-dired-echo-file-head (arg)
  1057. "Echo head of current file.
  1058. ARG is num to show, or defaults to 7."
  1059. (interactive "P")
  1060. (let ((f (dired-get-filename)))
  1061. (message "%s"
  1062. (apply 'concat
  1063. (my-file-head f
  1064. 7)))))
  1065. (defun my-dired-diff ()
  1066. "Show diff of marked file and file of current line."
  1067. (interactive)
  1068. (let ((files (dired-get-marked-files nil nil nil t)))
  1069. (if (eq (car files)
  1070. t)
  1071. (diff (cadr files) (dired-get-filename))
  1072. (message "One file must be marked!"))))
  1073. (defun dired-get-file-info ()
  1074. "Print information of current line file."
  1075. (interactive)
  1076. (let ((f (shell-quote-argument (dired-get-filename t))))
  1077. (if (file-directory-p f)
  1078. (progn
  1079. (message "Calculating disk usage...")
  1080. (shell-command (concat "du -hsD "
  1081. f)))
  1082. (shell-command (concat "file "
  1083. f)))))
  1084. (defun my-dired-scroll-up ()
  1085. "Scroll up."
  1086. (interactive)
  1087. (my-dired-previous-line (- (window-height) 1)))
  1088. (defun my-dired-scroll-down ()
  1089. "Scroll down."
  1090. (interactive)
  1091. (my-dired-next-line (- (window-height) 1)))
  1092. ;; (defun my-dired-forward-line (arg)
  1093. ;; ""
  1094. ;; (interactive "p"))
  1095. (defun my-dired-previous-line (arg)
  1096. "Move ARG lines up."
  1097. (interactive "p")
  1098. (if (> arg 0)
  1099. (progn
  1100. (if (eq (line-number-at-pos)
  1101. 1)
  1102. (goto-char (point-max))
  1103. (forward-line -1))
  1104. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1105. (dired-get-subdir))
  1106. (- arg 1)
  1107. arg)))
  1108. (dired-move-to-filename)))
  1109. (defun my-dired-next-line (arg)
  1110. "Move ARG lines down."
  1111. (interactive "p")
  1112. (if (> arg 0)
  1113. (progn
  1114. (if (eq (point)
  1115. (point-max))
  1116. (goto-char (point-min))
  1117. (forward-line 1))
  1118. (my-dired-next-line (if (or (dired-get-filename nil t)
  1119. (dired-get-subdir))
  1120. (- arg 1)
  1121. arg)))
  1122. (dired-move-to-filename)))
  1123. (defun my-dired-tramp-open (host)
  1124. "Open HOST home directory."
  1125. (interactive "sHostname: ")
  1126. (find-file (format "/scp:%s:" host)))
  1127. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1128. (if (eq window-system 'mac)
  1129. (setq dired-listing-switches "-lhF")
  1130. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1131. )
  1132. (setq dired-listing-switches "-lhF")
  1133. (put 'dired-find-alternate-file 'disabled nil)
  1134. ;; when using dired-find-alternate-file
  1135. ;; reuse current dired buffer for the file to open
  1136. (set-variable 'dired-ls-F-marks-symlinks t)
  1137. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1138. (set-variable 'ls-lisp-dirs-first t)
  1139. (set-variable 'ls-lisp-use-localized-time-format t)
  1140. (set-variable 'ls-lisp-format-time-list
  1141. '("%Y-%m-%d %H:%M"
  1142. "%Y-%m-%d "))
  1143. (set-variable 'dired-dwim-target t)
  1144. (set-variable 'dired-isearch-filenames t)
  1145. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1146. (set-variable 'dired-hide-details-hide-information-lines nil)
  1147. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1148. (set-variable 'dired-recursive-deletes 'always)
  1149. ;; (add-hook 'dired-after-readin-hook
  1150. ;; 'my-replace-nasi-none)
  1151. (with-eval-after-load 'dired
  1152. (safe-require-or-eval 'ls-lisp)
  1153. (defvar dired-mode-map (make-sparse-keymap))
  1154. (define-key dired-mode-map "i" 'dired-get-file-info)
  1155. (define-key dired-mode-map "f" 'find-file)
  1156. (define-key dired-mode-map "!" 'shell-command)
  1157. (define-key dired-mode-map "&" 'async-shell-command)
  1158. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1159. (define-key dired-mode-map "=" 'my-dired-diff)
  1160. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1161. (define-key dired-mode-map "b" 'gtkbm)
  1162. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1163. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1164. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1165. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1166. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1167. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1168. (substitute-key-definition 'dired-next-line
  1169. 'my-dired-next-line
  1170. dired-mode-map)
  1171. (substitute-key-definition 'dired-previous-line
  1172. 'my-dired-previous-line
  1173. dired-mode-map)
  1174. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1175. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1176. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1177. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1178. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1179. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1180. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1181. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1182. (add-hook 'dired-mode-hook
  1183. (lambda ()
  1184. (when (fboundp 'dired-hide-details-mode)
  1185. (dired-hide-details-mode t)
  1186. (local-set-key "l" 'dired-hide-details-mode))
  1187. (let ((file "._Icon\015"))
  1188. (when nil
  1189. '(file-readable-p file)
  1190. (delete-file file)))))
  1191. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1192. (with-eval-after-load 'dired
  1193. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1194. (when (autoload-eval-lazily 'dired-list-all-mode)
  1195. (setq dired-listing-switches "-lhF")
  1196. (with-eval-after-load 'dired
  1197. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1198. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1199. ;; misc funcs
  1200. (defalias 'qcalc 'quick-calc)
  1201. (defun memo (&optional dir)
  1202. "Open memo.txt in DIR."
  1203. (interactive)
  1204. (pop-to-buffer (find-file-noselect (concat (if dir
  1205. (file-name-as-directory dir)
  1206. "")
  1207. "memo.txt"))))
  1208. (set (defvar my-rgrep-alist nil
  1209. "Alist of rgrep command.
  1210. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1211. condition to choose COMMAND when evaluated.")
  1212. `(
  1213. ;; ripgrep
  1214. ("rg"
  1215. (executable-find "rg")
  1216. "rg --no-heading --smart-case ")
  1217. ;; git grep
  1218. ("gitgrep"
  1219. (eq 0
  1220. (shell-command "git rev-parse --git-dir"))
  1221. "git --no-pager -c color.grep=always grep -nH -e ")
  1222. ;; sift
  1223. ("sift"
  1224. (executable-find "sift")
  1225. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1226. ;; the silver searcher
  1227. ("ag"
  1228. (executable-find "ag")
  1229. "ag --nogroup --nopager --filename ")
  1230. ;; ack
  1231. ("ack"
  1232. (executable-find "ack")
  1233. "ack --nogroup --nopager --with-filename ")
  1234. ;; gnu global
  1235. ("global"
  1236. (and (require 'ggtags nil t)
  1237. (executable-find "global")
  1238. (ggtags-current-project-root))
  1239. "global --result grep ")
  1240. ;; grep
  1241. ("grep"
  1242. t
  1243. ,(concat "find . "
  1244. "-path '*/.git' -prune -o "
  1245. "-path '*/.svn' -prune -o "
  1246. "-type f -print0 | "
  1247. "xargs -0 grep -nH -e "))
  1248. )
  1249. )
  1250. (defvar my-rgrep-default nil
  1251. "Default command name for my-rgrep.")
  1252. (defun my-rgrep-grep-command (&optional name alist)
  1253. "Return recursive grep command for current directory or nil.
  1254. If NAME is given, use that without testing.
  1255. Commands are searched from ALIST."
  1256. (if alist
  1257. (if name
  1258. ;; if name is given search that from alist and return the command
  1259. (nth 2 (assoc name
  1260. alist))
  1261. ;; if name is not given try test in 1th elem
  1262. (let ((car (car alist))
  1263. (cdr (cdr alist)))
  1264. (if (eval (nth 1 car))
  1265. ;; if the condition is true return the command
  1266. (nth 2 car)
  1267. ;; try next one
  1268. (and cdr
  1269. (my-rgrep-grep-command name cdr)))))
  1270. ;; if alist is not given set default value
  1271. (my-rgrep-grep-command name my-rgrep-alist)))
  1272. (defun my-rgrep (command-args)
  1273. "My recursive grep. Run COMMAND-ARGS.
  1274. If prefix argument is given, use current symbol as default search target
  1275. and search from projectile root (if projectile is available)."
  1276. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1277. nil)))
  1278. (if cmd
  1279. (list (read-shell-command "grep command: "
  1280. (concat cmd
  1281. (if current-prefix-arg
  1282. (thing-at-point 'symbol t)
  1283. ""))
  1284. 'grep-find-history))
  1285. (error "My-Rgrep: Command for rgrep not found")
  1286. )))
  1287. (if (and current-prefix-arg
  1288. (safe-require-or-eval 'projectile)
  1289. (projectile-project-p))
  1290. (projectile-with-default-dir (projectile-project-root)
  1291. (compilation-start command-args
  1292. 'grep-mode))
  1293. (compilation-start command-args
  1294. 'grep-mode)))
  1295. (defun my-rgrep-thing-at-point-projectile-root ()
  1296. "My recursive grep to find thing at point from project root."
  1297. (interactive)
  1298. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1299. nil))
  1300. (command-args
  1301. (if cmd
  1302. (concat cmd
  1303. (or (thing-at-point 'symbol t)
  1304. (error "No symbol at point")))
  1305. (error "My-Rgrep: Command for rgrep not found"))))
  1306. (if (safe-require-or-eval 'projectile)
  1307. (projectile-with-default-dir (projectile-project-root)
  1308. (compilation-start command-args
  1309. 'grep-mode))
  1310. (compilation-start command-args
  1311. 'grep-mode))))
  1312. (defmacro define-my-rgrep (name)
  1313. "Define rgrep for NAME."
  1314. `(defun ,(intern (concat "my-rgrep-"
  1315. name)) ()
  1316. ,(format "My recursive grep by %s."
  1317. name)
  1318. (interactive)
  1319. (let ((my-rgrep-default ,name))
  1320. (if (called-interactively-p 'any)
  1321. (call-interactively 'my-rgrep)
  1322. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1323. )
  1324. (define-my-rgrep "ack")
  1325. (define-my-rgrep "ag")
  1326. (define-my-rgrep "rg")
  1327. (define-my-rgrep "sift")
  1328. (define-my-rgrep "gitgrep")
  1329. (define-my-rgrep "grep")
  1330. (define-my-rgrep "global")
  1331. (define-key ctl-x-map "s" 'my-rgrep)
  1332. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1333. (defalias 'make 'compile)
  1334. (define-key ctl-x-map "c" 'compile)
  1335. ;;;;;;;;;;;;;;;;;;;;;;;
  1336. ;; adoc-simple-mode
  1337. (when (safe-require-or-eval 'adoc-mode)
  1338. (defvar adoc-simple-font-lock-keywords
  1339. nil)
  1340. (define-derived-mode adoc-simple-mode adoc-mode
  1341. "Adoc-Simple"
  1342. "Major mode for editing AsciiDoc text files.
  1343. This mode is a simplified version of `adoc-mode'."
  1344. '(set (make-local-variable 'font-lock-defaults)
  1345. '(adoc-simple-font-lock-keywords
  1346. nil nil nil nil
  1347. (font-lock-multiline . t)
  1348. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1349. (add-to-list 'auto-mode-alist
  1350. '("\\.adoc\\'" . adoc-simple-mode)))
  1351. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1352. ;; awk-preview
  1353. (defgroup awk-preview nil
  1354. "Awk previewer."
  1355. :tag "Awk Preview"
  1356. :prefix "awk-preview-"
  1357. :group 'tools)
  1358. (defcustom awk-preview-program
  1359. (or (executable-find "gawk")
  1360. (executable-find "awk")
  1361. "awk")
  1362. "Awk program to execute."
  1363. :type 'string
  1364. :group 'awk-preview)
  1365. (defcustom awk-preview-switches
  1366. '("--sandbox" "--" "{print}")
  1367. "String of awk options appended when running awk preview."
  1368. :type '(repeat string)
  1369. :group 'awk-preview)
  1370. (defun awk-preview--invoke (start end output)
  1371. "Execute awk process and get result."
  1372. (let ((proc (apply 'start-process
  1373. "awk-preview"
  1374. output
  1375. awk-preview-program
  1376. awk-preview-switches)))
  1377. (message "%S" proc)
  1378. (process-send-region proc start end)
  1379. (process-send-eof proc)
  1380. (accept-process-output proc)
  1381. )
  1382. output)
  1383. (defun awk-preview (start end)
  1384. "Run awk and preview result."
  1385. (interactive "r")
  1386. (let ((output (with-current-buffer (get-buffer-create "*awk-preview output*")
  1387. (erase-buffer)
  1388. (current-buffer))))
  1389. (awk-preview--invoke start end output)
  1390. (message "awk-preview: %s" (with-current-buffer output
  1391. (buffer-substring-no-properties (point-min) (point-max))))
  1392. ))
  1393. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1394. ;; editorconfig-auto-apply
  1395. (define-minor-mode editorconfig-auto-apply-mode
  1396. "When saving .editorconfig file update buffer configs."
  1397. :global t
  1398. :lighter ""
  1399. (if editorconfig-auto-apply-mode
  1400. (add-hook 'after-save-hook
  1401. 'editorconfig-auto-apply-mode--when-save)
  1402. (remove-hook 'after-save-hook
  1403. 'editorconfig-auto-apply-mode--when-save)))
  1404. (defun editorconfig-auto-apply-mode--when-save ()
  1405. "When saving .editorconfig file walk all buffers and update configs."
  1406. (when (eq major-mode
  1407. 'editorconfig-conf-mode)
  1408. (let ((dir (file-name-directory buffer-file-name)))
  1409. (cl-dolist (buf (buffer-list))
  1410. (when (and (buffer-file-name buf)
  1411. (file-in-directory-p (buffer-file-name buf)
  1412. dir))
  1413. (with-current-buffer buf
  1414. (editorconfig-mode-apply)))))))
  1415. (editorconfig-auto-apply-mode 1)
  1416. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1417. (defun my-file-head (filename &optional n)
  1418. "Return list of first N lines of file FILENAME."
  1419. ;; Work with japanese text?
  1420. (let ((num (or n 10))
  1421. (size 100)
  1422. (beg 0)
  1423. (end 0)
  1424. (result '()))
  1425. (with-temp-buffer
  1426. (erase-buffer)
  1427. (while (<= (count-lines (point-min)
  1428. (point-max))
  1429. num)
  1430. (setq end (+ beg size))
  1431. (insert-file-contents-literally filename
  1432. nil
  1433. beg
  1434. end)
  1435. (goto-char (point-max))
  1436. (setq beg (+ beg size)))
  1437. (goto-char (point-min))
  1438. (while (< (length result) num)
  1439. (let ((start (point)))
  1440. (forward-line 1)
  1441. (setq result
  1442. `(,@result ,(buffer-substring-no-properties start
  1443. (point))))))
  1444. result
  1445. ;; (buffer-substring-no-properties (point-min)
  1446. ;; (progn
  1447. ;; (forward-line num)
  1448. ;; (point)))
  1449. )))
  1450. ;; (apply 'concat (my-file-head "./shrc" 10)
  1451. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1452. ;; Local Variables:
  1453. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1454. ;; flycheck-checker: emacs-lisp
  1455. ;; End:
  1456. ;;; emancs.el ends here