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.
 
 
 
 
 
 

1714 lines
56 KiB

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