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.
 
 
 
 
 
 

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