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 lines
55 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Code:
  3. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  4. ;; SETUP_LOAD: (and (file-readable-p file)
  5. ;; SETUP_LOAD: (byte-recompile-file file nil 0 t)))
  6. (setq debug-on-error t)
  7. ;; make directories
  8. (unless (file-directory-p (expand-file-name user-emacs-directory))
  9. (make-directory (expand-file-name user-emacs-directory)))
  10. (require 'cl-lib)
  11. (require 'simple)
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. ;; Some macros for internals
  14. (defun call-after-init (func)
  15. "If `after-init-hook' has been run, call FUNC immediately.
  16. Otherwize hook it."
  17. (if after-init-time
  18. (funcall func)
  19. (add-hook 'after-init-hook
  20. func)))
  21. (defmacro safe-require-or-eval (feature)
  22. "Require FEATURE if available.
  23. At compile time the feature will be loaded immediately."
  24. `(eval-and-compile
  25. (message "safe-require-or-eval: Trying to require %s" ,feature)
  26. (require ,feature nil t)))
  27. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  28. "Define autoloading FEATURE that defines FUNCTIONS.
  29. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  30. the function same as FEATURE is defined as autoloaded function. BODY is passed
  31. to `eval-after-load'.
  32. After this macro is expanded, this returns the path to library if FEATURE
  33. found, otherwise returns nil."
  34. (declare (indent 2) (debug t))
  35. (let* ((libname (symbol-name (eval feature)))
  36. (libpath (locate-library libname)))
  37. `(progn
  38. (when (locate-library ,libname)
  39. ,@(mapcar (lambda (f)
  40. `(unless (fboundp ',f)
  41. (progn
  42. (message "Autoloaded function `%S' defined (%s)"
  43. (quote ,f)
  44. ,libpath)
  45. (autoload (quote ,f)
  46. ,libname
  47. ,(concat "Autoloaded function defined in \""
  48. libpath
  49. "\".")
  50. t))))
  51. (or (eval functions)
  52. `(,(eval feature)))))
  53. (eval-after-load ,feature
  54. (quote (progn
  55. ,@body)))
  56. (locate-library ,libname))))
  57. (when (autoload-eval-lazily 'tetris nil
  58. (message "Tetris loaded!"))
  59. (message "Tetris found!"))
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. ;; package
  62. (set (defvar 10sr-package-list)
  63. '(
  64. vimrc-mode
  65. markdown-mode
  66. yaml-mode
  67. gnuplot-mode
  68. php-mode
  69. erlang
  70. js2-mode
  71. js-doc
  72. git-commit
  73. gitignore-mode
  74. adoc-mode
  75. go-mode
  76. ;; It seems malabar has been merged into jdee and this package
  77. ;; already removed
  78. ;; malabar-mode
  79. ;; ack
  80. color-moccur
  81. ggtags
  82. flycheck
  83. auto-highlight-symbol
  84. hl-todo
  85. ;; Currently not available
  86. ;; pp-c-l
  87. xclip
  88. foreign-regexp
  89. multi-term
  90. term-run
  91. editorconfig
  92. git-ps1-mode
  93. restart-emacs
  94. fill-column-indicator
  95. pkgbuild-mode
  96. minibuffer-line
  97. which-key
  98. ;; I think this works in place of my autosave lib
  99. super-save
  100. pipenv
  101. imenu-list
  102. page-break-lines
  103. sync-recentf
  104. scala-mode
  105. ;;ensime
  106. editorconfig
  107. editorconfig-custom-majormode
  108. git-command
  109. prompt-text
  110. ;; 10sr repository
  111. ;; 10sr-extras
  112. terminal-title
  113. recentf-show
  114. dired-list-all-mode
  115. pack
  116. set-modeline-color
  117. read-only-only-mode
  118. smart-revert
  119. autosave
  120. ;;window-organizer
  121. remember-major-modes-mode
  122. ilookup
  123. pasteboard
  124. end-mark
  125. sl
  126. gosh-mode
  127. ))
  128. (when (safe-require-or-eval 'package)
  129. (setq package-archives
  130. `(,@package-archives
  131. ("melpa" . "https://melpa.org/packages/")
  132. ;; Somehow fails to download via https
  133. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  134. (package-initialize)
  135. (defun my-auto-install-package ()
  136. "Install packages semi-automatically."
  137. (interactive)
  138. (package-refresh-contents)
  139. (mapc (lambda (pkg)
  140. (or (package-installed-p pkg)
  141. (package-install pkg)))
  142. 10sr-package-list))
  143. )
  144. ;; (lazy-load-eval 'sudoku)
  145. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  146. ;; my-idle-hook
  147. (defvar my-idle-hook nil
  148. "Hook run when idle for several secs.")
  149. (defvar my-idle-hook-sec 5
  150. "Second to run `my-idle-hook'.")
  151. (run-with-idle-timer my-idle-hook-sec
  152. t
  153. (lambda ()
  154. (run-hooks 'my-idle-hook)))
  155. ;; (add-hook 'my-idle-hook
  156. ;; (lambda ()
  157. ;; (message "idle hook message")))
  158. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  159. ;; start and quit
  160. (setq inhibit-startup-message t)
  161. (setq confirm-kill-emacs 'y-or-n-p)
  162. (setq gc-cons-threshold (* 1024 1024 4))
  163. (setq garbage-collection-messages t)
  164. (when window-system
  165. (add-to-list 'default-frame-alist '(cursor-type . box))
  166. (add-to-list 'default-frame-alist '(background-color . "white"))
  167. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  168. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  169. ;; does not work?
  170. )
  171. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  172. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  173. (and (fboundp 'tool-bar-mode)
  174. (tool-bar-mode 0))
  175. (and (fboundp 'set-scroll-bar-mode)
  176. (set-scroll-bar-mode nil))
  177. (call-after-init
  178. (lambda ()
  179. (message "%s %s" invocation-name emacs-version)
  180. (message "Invocation directory: %s" default-directory)
  181. (message "%s was taken to initialize emacs." (emacs-init-time))
  182. (view-echo-area-messages)
  183. ;; (view-emacs-news)
  184. ))
  185. (cd ".") ; when using windows use / instead of \ in `default-directory'
  186. ;; locale
  187. (set-language-environment "Japanese")
  188. (set-default-coding-systems 'utf-8-unix)
  189. (prefer-coding-system 'utf-8-unix)
  190. (setq system-time-locale "C")
  191. ;; my prefix map
  192. (defvar my-prefix-map nil
  193. "My prefix map.")
  194. (define-prefix-command 'my-prefix-map)
  195. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  196. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  197. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  198. ;; (comint-show-maximum-output)
  199. ;; kill scratch
  200. (call-after-init (lambda ()
  201. (let ((buf (get-buffer "*scratch*")))
  202. (when buf
  203. (kill-buffer buf)))))
  204. ;; modifier keys
  205. ;; (setq mac-option-modifier 'control)
  206. ;; display
  207. (setq visible-bell t)
  208. (setq ring-bell-function 'ignore)
  209. (mouse-avoidance-mode 'banish)
  210. (setq echo-keystrokes 0.1)
  211. (defun reload-init-file ()
  212. "Reload Emacs init file."
  213. (interactive)
  214. (when (and user-init-file
  215. (file-readable-p user-init-file))
  216. (load-file user-init-file)))
  217. (safe-require-or-eval 'session)
  218. ;; server
  219. (set-variable 'server-name (concat "server"
  220. (number-to-string (emacs-pid))))
  221. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  222. ;; In Darwin environment, init fails with message like 'Service name too long'
  223. ;; when server-use-tcp is nil
  224. (when (or (eq system-type
  225. 'cygwin)
  226. (eq system-type
  227. 'darwin))
  228. (set-variable 'server-use-tcp t))
  229. ;; MSYS2 fix
  230. (when (eq system-type
  231. 'windows-nt)
  232. (setq shell-file-name
  233. (executable-find "bash"))
  234. '(setq function-key-map
  235. `(,@function-key-map ([pause] . [?\C-c])
  236. ))
  237. (define-key key-translation-map
  238. (kbd "<pause>")
  239. (kbd "C-c"))
  240. '(keyboard-translate [pause]
  241. (kbd "C-c")p)
  242. ;; TODO: move to other place later
  243. (when (not window-system)
  244. (setq interprogram-paste-function nil)
  245. (setq interprogram-cut-function nil)))
  246. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  247. ;; global keys
  248. (global-set-key (kbd "<up>") 'scroll-down-line)
  249. (global-set-key (kbd "<down>") 'scroll-up-line)
  250. (global-set-key (kbd "<left>") 'scroll-down)
  251. (global-set-key (kbd "<right>") 'scroll-up)
  252. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  253. (global-set-key (kbd "C-\\") help-map)
  254. (define-key ctl-x-map (kbd "DEL") help-map)
  255. (define-key ctl-x-map (kbd "C-h") help-map)
  256. (define-key help-map "a" 'apropos)
  257. ;; disable annoying keys
  258. (global-set-key [prior] 'ignore)
  259. (global-set-key (kbd "<next>") 'ignore)
  260. (global-set-key [menu] 'ignore)
  261. (global-set-key [down-mouse-1] 'ignore)
  262. (global-set-key [down-mouse-2] 'ignore)
  263. (global-set-key [down-mouse-3] 'ignore)
  264. (global-set-key [mouse-1] 'ignore)
  265. (global-set-key [mouse-2] 'ignore)
  266. (global-set-key [mouse-3] 'ignore)
  267. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  268. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  269. (when (safe-require-or-eval 'which-key)
  270. (which-key-mode))
  271. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  272. ;; editting
  273. (setq kill-whole-line t)
  274. (setq scroll-conservatively 35
  275. scroll-margin 2
  276. scroll-step 0)
  277. (setq-default major-mode 'text-mode)
  278. (setq next-line-add-newlines nil)
  279. (setq kill-read-only-ok t)
  280. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  281. ;; (setq-default line-spacing 0.2)
  282. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  283. ;; (setq-default tab-width 4)
  284. (setq-default indent-tabs-mode nil)
  285. (setq-default indent-line-function 'indent-to-left-margin)
  286. ;; (setq-default indent-line-function nil)
  287. (setq-default truncate-lines nil)
  288. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  289. (delete-selection-mode 1)
  290. (cua-mode 0)
  291. (setq line-move-visual nil)
  292. ;; key bindings
  293. ;; moving around
  294. ;;(keyboard-translate ?\M-j ?\C-j)
  295. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  296. (define-key esc-map "p" 'backward-paragraph)
  297. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  298. (define-key esc-map "n" 'forward-paragraph)
  299. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  300. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  301. (global-set-key (kbd "C-<left>") 'scroll-down)
  302. (global-set-key (kbd "C-<right>") 'scroll-up)
  303. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  304. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  305. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  306. ;; C-h and DEL
  307. (global-set-key (kbd "C-h") (kbd "DEL"))
  308. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  309. (global-set-key (kbd "C-m") 'newline-and-indent)
  310. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  311. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  312. (define-key esc-map "u" 'undo)
  313. (define-key esc-map "i" (kbd "ESC TAB"))
  314. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  315. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  316. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  317. (define-key my-prefix-map (kbd "C-o") 'occur)
  318. (require 'page-ext nil t)
  319. (when (safe-require-or-eval 'page-break-lines)
  320. (global-page-break-lines-mode 1))
  321. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  322. ;; title and mode-line
  323. (when (safe-require-or-eval 'terminal-title)
  324. ;; if TERM is not screen use default value
  325. (if (getenv "TMUX")
  326. ;; if use tmux locally just basename of current dir
  327. (set-variable 'terminal-title-format
  328. '((file-name-nondirectory (directory-file-name
  329. default-directory))))
  330. (if (and (let ((tty-type (frame-parameter nil
  331. 'tty-type)))
  332. (and tty-type
  333. (equal (car (split-string tty-type
  334. "-"))
  335. "screen")))
  336. (not (getenv "SSH_CONNECTION")))
  337. (set-variable 'terminal-title-format
  338. '((file-name-nondirectory (directory-file-name
  339. default-directory))))
  340. ;; seems that TMUX is used locally and ssh to remote host
  341. (set-variable 'terminal-title-format
  342. `("em:"
  343. ,user-login-name
  344. "@"
  345. ,(car (split-string (system-name)
  346. "\\."))
  347. ":"
  348. default-directory))
  349. )
  350. )
  351. (terminal-title-mode))
  352. (setq eol-mnemonic-dos "\\r\\n")
  353. (setq eol-mnemonic-mac "\\r")
  354. (setq eol-mnemonic-unix "")
  355. (which-function-mode 0)
  356. (line-number-mode 0)
  357. (column-number-mode 0)
  358. (size-indication-mode 0)
  359. (setq mode-line-position
  360. '(:eval (format "L%%l/%d:C%%c"
  361. (count-lines (point-max)
  362. (point-min)))))
  363. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  364. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  365. ;; minibuffer
  366. (setq insert-default-directory t)
  367. (setq completion-ignore-case t
  368. read-file-name-completion-ignore-case t
  369. read-buffer-completion-ignore-case t)
  370. (setq resize-mini-windows t)
  371. (temp-buffer-resize-mode 1)
  372. (savehist-mode 1)
  373. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  374. (set-variable 'help-at-pt-display-when-idle t)
  375. (fset 'yes-or-no-p 'y-or-n-p)
  376. ;; complete symbol when `eval'
  377. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  378. (define-key minibuffer-local-map (kbd "C-u")
  379. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  380. ;; I dont know these bindings are good
  381. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  382. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  383. (when (safe-require-or-eval 'minibuffer-line)
  384. (set-face-underline 'minibuffer-line nil)
  385. (set-variable 'minibuffer-line-refresh-interval
  386. 25)
  387. ;; Set idle timer
  388. (defvar my-minibuffer-line--idle-timer nil)
  389. (defvar minibuffer-line-mode)
  390. (add-hook 'minibuffer-line-mode-hook
  391. (lambda ()
  392. (when my-minibuffer-line--idle-timer
  393. (cancel-timer my-minibuffer-line--idle-timer)
  394. (setq my-minibuffer-line--idle-timer nil))
  395. (when minibuffer-line-mode
  396. (setq my-minibuffer-line--idle-timer
  397. (run-with-idle-timer 0.5
  398. t
  399. 'minibuffer-line--update)))))
  400. (set-variable 'minibuffer-line-format
  401. `(,(concat user-login-name
  402. "@"
  403. (car (split-string (system-name)
  404. "\\."))
  405. ":")
  406. (:eval (abbreviate-file-name (or buffer-file-name
  407. default-directory)))
  408. (:eval (and (fboundp 'git-ps1-mode-get-current)
  409. (git-ps1-mode-get-current " [GIT:%s]")))
  410. " "
  411. (:eval (format-time-string display-time-format))))
  412. (minibuffer-line-mode 1)
  413. )
  414. (when (safe-require-or-eval 'prompt-text)
  415. (set-variable 'prompt-text-format
  416. `(,(concat ""
  417. user-login-name
  418. "@"
  419. (car (split-string (system-name)
  420. "\\."))
  421. ":")
  422. (:eval (abbreviate-file-name (or buffer-file-name
  423. default-directory)))
  424. (:eval (and (fboundp 'git-ps1-mode-get-current)
  425. (git-ps1-mode-get-current " [GIT:%s]")))
  426. " "
  427. (:eval (format-time-string display-time-format))
  428. "\n"
  429. (:eval (symbol-name this-command))
  430. ": "))
  431. (prompt-text-mode 1))
  432. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  433. ;; letters, font-lock mode and fonts
  434. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  435. ;; (set-window-margins (selected-window) 1 1)
  436. (and (or (eq system-type 'Darwin)
  437. (eq system-type 'darwin))
  438. (fboundp 'mac-set-input-method-parameter)
  439. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  440. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  441. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  442. (boundp 'input-method-inactivate-hook))
  443. (add-hook 'input-method-activate-hook
  444. (lambda () (set-cursor-color "red")))
  445. (add-hook 'input-method-inactivate-hook
  446. (lambda () (set-cursor-color "black"))))
  447. (when (safe-require-or-eval 'paren)
  448. (show-paren-mode 1)
  449. (setq show-paren-delay 0.5
  450. show-paren-style 'parenthesis) ; mixed is hard to read
  451. ;; (set-face-background 'show-paren-match
  452. ;; "black")
  453. ;; ;; (face-foreground 'default))
  454. ;; (set-face-foreground 'show-paren-match
  455. ;; "white")
  456. ;; (set-face-inverse-video-p 'show-paren-match
  457. ;; t)
  458. )
  459. (transient-mark-mode 1)
  460. (global-font-lock-mode 1)
  461. (setq font-lock-global-modes
  462. '(not
  463. help-mode
  464. eshell-mode
  465. ;;term-mode
  466. Man-mode))
  467. ;; (standard-display-ascii ?\n "$\n")
  468. ;; (defvar my-eol-face
  469. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  470. ;; )
  471. ;; (defvar my-tab-face
  472. ;; '(("\t" . '(0 highlight t nil))))
  473. (defvar my-jspace-face
  474. '(("\u3000" . '(0 highlight t nil))))
  475. (add-hook 'font-lock-mode-hook
  476. (lambda ()
  477. ;; (font-lock-add-keywords nil my-eol-face)
  478. (font-lock-add-keywords nil my-jspace-face)
  479. ))
  480. (when (safe-require-or-eval 'whitespace)
  481. (add-to-list 'whitespace-display-mappings
  482. ;; We need t since last one takes precedence
  483. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  484. ;; (add-to-list 'whitespace-display-mappings
  485. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  486. (setq whitespace-style '(face
  487. trailing ; trailing blanks
  488. newline ; newlines
  489. newline-mark ; use display table for newline
  490. tab-mark
  491. empty ; empty lines at beg or end of buffer
  492. lines-tail ; lines over 80
  493. ))
  494. ;; (setq whitespace-newline 'font-lock-comment-face)
  495. (set-variable 'whitespace-line-column nil)
  496. (global-whitespace-mode t)
  497. (add-hook 'dired-mode-hook
  498. (lambda ()
  499. (set (make-local-variable 'whitespace-style) nil)))
  500. (if (eq (display-color-cells)
  501. 256)
  502. (set-face-foreground 'whitespace-newline "color-109")
  503. ;; (progn
  504. ;; (set-face-bold-p 'whitespace-newline
  505. ;; t))
  506. ))
  507. (and nil
  508. '(safe-require-or-eval 'fill-column-indicator)
  509. (setq fill-column-indicator))
  510. ;; highlight current line
  511. ;; http://wiki.riywo.com/index.php?Meadow
  512. (face-spec-set 'hl-line
  513. '((((min-colors 256)
  514. (background dark))
  515. (:background "color-234"))
  516. (((min-colors 256)
  517. (background light))
  518. (:background "color-234"))
  519. (t
  520. (:underline "black"))))
  521. (set-variable 'hl-line-global-modes
  522. '(not
  523. term-mode))
  524. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  525. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  526. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  527. ;;(safe-require-or-eval 'set-modeline-color)
  528. ;; (let ((fg (face-foreground 'default))
  529. ;; (bg (face-background 'default)))
  530. ;; (set-face-background 'mode-line-inactive
  531. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  532. ;; (set-face-foreground 'mode-line-inactive
  533. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  534. ;; (set-face-underline 'mode-line-inactive
  535. ;; t)
  536. ;; (set-face-underline 'vertical-border
  537. ;; nil)
  538. (when (safe-require-or-eval 'end-mark)
  539. (global-end-mark-mode))
  540. (when (safe-require-or-eval 'auto-highlight-symbol)
  541. (set-variable 'ahs-idle-interval 0.6)
  542. (global-auto-highlight-symbol-mode 1))
  543. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  544. ;; file handling
  545. (when (safe-require-or-eval 'editorconfig)
  546. ;; (set-variable 'editorconfig-get-properties-function
  547. ;; 'editorconfig-core-get-properties-hash)
  548. (editorconfig-mode 1)
  549. (set-variable 'editorconfig-mode-lighter " EC")
  550. (with-eval-after-load 'org-src
  551. ;; [*.org\[\*Org Src*\[ c \]*\]]
  552. (add-hook 'org-src-mode-hook
  553. 'editorconfig-mode-apply t)))
  554. (when (fboundp 'editorconfig-custom-majormode)
  555. (add-hook 'editorconfig-custom-hooks
  556. 'editorconfig-custom-majormode))
  557. ;; Add readonly=true to set read-only-mode
  558. (add-hook 'editorconfig-custom-hooks
  559. (lambda (props)
  560. (let ((r (gethash 'readonly props)))
  561. (when (and (string= r "true")
  562. (not buffer-read-only))
  563. (read-only-mode 1)))))
  564. ;; (when (fboundp 'editorconfig-charset-extras)
  565. ;; (add-hook 'editorconfig-custom-hooks
  566. ;; 'editorconfig-charset-extras))
  567. (setq revert-without-query '(".+"))
  568. ;; save cursor position
  569. (when (safe-require-or-eval 'saveplace)
  570. (setq-default save-place t)
  571. (setq save-place-file (concat user-emacs-directory
  572. "places")))
  573. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  574. (setq make-backup-files t)
  575. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  576. (setq backup-directory-alist
  577. (cons (cons "\\.*$" (expand-file-name (concat user-emacs-directory
  578. "backup")))
  579. backup-directory-alist))
  580. (setq version-control 'never)
  581. (setq delete-old-versions t)
  582. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  583. "auto-save/")))
  584. (setq delete-auto-save-files t)
  585. (add-to-list 'completion-ignored-extensions ".bak")
  586. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  587. (setq delete-by-moving-to-trash t)
  588. ;; trash-directory "~/.emacs.d/trash")
  589. (add-hook 'after-save-hook
  590. 'executable-make-buffer-file-executable-if-script-p)
  591. (set-variable 'bookmark-default-file
  592. (expand-file-name (concat user-emacs-directory
  593. "bmk")))
  594. (set-variable 'bookmark-save-flag
  595. 1)
  596. (with-eval-after-load 'recentf
  597. (defvar recentf-exclude)
  598. (defvar bookmark-default-file)
  599. (add-to-list 'recentf-exclude
  600. (regexp-quote bookmark-default-file)))
  601. (when (safe-require-or-eval 'smart-revert)
  602. (smart-revert-on))
  603. ;; autosave
  604. (when (safe-require-or-eval 'autosave)
  605. (autosave-set 8))
  606. ;; bookmarks
  607. (define-key ctl-x-map "m" 'list-bookmarks)
  608. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  609. ;; buffer killing
  610. ;; (defun my-delete-window-killing-buffer () nil)
  611. (defun my-query-kill-current-buffer ()
  612. "Interactively kill current buffer."
  613. (interactive)
  614. (if (y-or-n-p (concat "kill current buffer? :"))
  615. (kill-buffer (current-buffer))))
  616. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  617. (substitute-key-definition 'kill-buffer
  618. 'my-query-kill-current-buffer
  619. global-map)
  620. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  621. ;; share clipboard with x
  622. ;; this page describes this in details, but only these sexps seem to be needed
  623. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  624. (and nil
  625. (not window-system)
  626. (not (eq window-system 'mac))
  627. (getenv "DISPLAY")
  628. (not (equal (getenv "DISPLAY") ""))
  629. (executable-find "xclip")
  630. ;; (< emacs-major-version 24)
  631. '(safe-require-or-eval 'xclip)
  632. nil
  633. (turn-on-xclip))
  634. (and (eq system-type 'darwin)
  635. (safe-require-or-eval 'pasteboard)
  636. (turn-on-pasteboard))
  637. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  638. ;; some modes and hooks
  639. ;; Include some extra modes
  640. (require 'generic-x)
  641. (when (autoload-eval-lazily 'ggtags)
  642. (add-hook 'c-mode-common-hook
  643. (lambda ()
  644. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  645. (ggtags-mode 1))))
  646. (add-hook 'python-mode-hook
  647. (lambda ()
  648. (ggtags-mode 1))))
  649. (when (autoload-eval-lazily 'imenu-list)
  650. ;; (set-variable 'imenu-list-auto-resize t)
  651. (set-variable 'imenu-list-focus-after-activation t)
  652. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  653. (with-eval-after-load 'compile
  654. (defvar compilation-filter-start)
  655. (defvar compilation-error-regexp-alist)
  656. (require 'ansi-color)
  657. (add-hook 'compilation-filter-hook
  658. (lambda ()
  659. (let ((inhibit-read-only t))
  660. (ansi-color-apply-on-region compilation-filter-start
  661. (point)))))
  662. (add-to-list 'compilation-error-regexp-alist
  663. ;; ansible-lint
  664. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  665. ;; Workaround to avoid ensime error
  666. (defvar ensime-mode-key-prefix nil)
  667. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  668. (when (safe-require-or-eval 'company)
  669. (global-company-mode)
  670. (set-variable 'company-idle-delay 0.5)
  671. (set-variable 'company-minimum-prefix-length 2)
  672. (set-variable 'company-selection-wrap-around t))
  673. ;; https://github.com/lunaryorn/flycheck
  674. (when (safe-require-or-eval 'flycheck)
  675. (call-after-init 'global-flycheck-mode))
  676. (when (autoload-eval-lazily 'ilookup)
  677. (define-key ctl-x-map "d" 'ilookup-open-word))
  678. (set-variable 'ac-ignore-case nil)
  679. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  680. (define-key ctl-x-map "t" 'term-run-shell-command))
  681. (add-to-list 'safe-local-variable-values
  682. '(encoding utf-8))
  683. (setq enable-local-variables :safe)
  684. ;; (when (safe-require-or-eval 'remember-major-modes-mode)
  685. ;; (remember-major-modes-mode 1))
  686. ;; Detect file type from shebang and set major-mode.
  687. (add-to-list 'interpreter-mode-alist
  688. '("python3" . python-mode))
  689. (add-to-list 'interpreter-mode-alist
  690. '("python2" . python-mode))
  691. (with-eval-after-load 'python
  692. (defvar python-mode-map (make-sparse-keymap))
  693. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  694. (when (autoload-eval-lazily 'pipenv)
  695. (add-hook 'python-mode-hook
  696. (lambda ()
  697. (pipenv-mode 1)
  698. (pipenv-projectile-after-switch-default)))
  699. )
  700. (set-variable 'flycheck-python-pycompile-executable "python3")
  701. ;; http://fukuyama.co/foreign-regexp
  702. '(and (safe-require-or-eval 'foreign-regexp)
  703. (progn
  704. (setq foreign-regexp/regexp-type 'perl)
  705. '(setq reb-re-syntax 'foreign-regexp)
  706. ))
  707. (autoload-eval-lazily 'sql '(sql-mode)
  708. (require 'sql-indent nil t))
  709. (when (autoload-eval-lazily 'git-command)
  710. (define-key ctl-x-map "g" 'git-command))
  711. (when (safe-require-or-eval 'git-commit)
  712. (global-git-commit-mode 1))
  713. (with-eval-after-load 'git-commit
  714. (add-hook 'git-commit-setup-hook
  715. 'turn-off-auto-fill t))
  716. (autoload-eval-lazily 'sl)
  717. (with-eval-after-load 'rst
  718. (defvar rst-mode-map)
  719. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  720. (with-eval-after-load 'jdee
  721. (add-hook 'jdee-mode-hook
  722. (lambda ()
  723. (make-local-variable 'global-mode-string)
  724. (add-to-list 'global-mode-string
  725. mode-line-position))))
  726. ;; Cannot enable error thrown. Why???
  727. ;; https://github.com/m0smith/malabar-mode#Installation
  728. ;; (when (autoload-eval-lazily 'malabar-mode)
  729. ;; (add-to-list 'load-path
  730. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  731. ;; (safe-require-or-eval 'cedet-devel-load)
  732. ;; (call-after-init 'activate-malabar-mode))
  733. (with-eval-after-load 'make-mode
  734. (defvar makefile-mode-map (make-sparse-keymap))
  735. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  736. ;; this functions is set in write-file-functions, i cannot find any
  737. ;; good way to remove this.
  738. (fset 'makefile-warn-suspicious-lines 'ignore))
  739. (with-eval-after-load 'verilog-mode
  740. (defvar verilog-mode-map (make-sparse-keymap))
  741. (define-key verilog-mode-map ";" 'self-insert-command))
  742. (setq diff-switches "-u")
  743. (with-eval-after-load 'diff-mode
  744. ;; (when (and (eq major-mode
  745. ;; 'diff-mode)
  746. ;; (not buffer-file-name))
  747. ;; ;; do not pass when major-mode is derived mode of diff-mode
  748. ;; (view-mode 1))
  749. (set-face-attribute 'diff-header nil
  750. :foreground nil
  751. :background nil
  752. :weight 'bold)
  753. (set-face-attribute 'diff-file-header nil
  754. :foreground nil
  755. :background nil
  756. :weight 'bold)
  757. (set-face-foreground 'diff-index "blue")
  758. (set-face-attribute 'diff-hunk-header nil
  759. :foreground "cyan"
  760. :weight 'normal)
  761. (set-face-attribute 'diff-context nil
  762. ;; :foreground "white"
  763. :foreground nil
  764. :weight 'normal)
  765. (set-face-foreground 'diff-removed "red")
  766. (set-face-foreground 'diff-added "green")
  767. (set-face-background 'diff-removed nil)
  768. (set-face-background 'diff-added nil)
  769. (set-face-attribute 'diff-changed nil
  770. :foreground "magenta"
  771. :weight 'normal)
  772. (set-face-attribute 'diff-refine-changed nil
  773. :foreground nil
  774. :background nil
  775. :weight 'bold
  776. :inverse-video t)
  777. ;; Annoying !
  778. ;;(diff-auto-refine-mode)
  779. )
  780. ;; (ffap-bindings)
  781. (set-variable 'browse-url-browser-function
  782. 'eww-browse-url)
  783. (set-variable 'sh-here-document-word "__EOC__")
  784. (when (autoload-eval-lazily 'adoc-mode
  785. nil
  786. (defvar adoc-mode-map (make-sparse-keymap))
  787. (define-key adoc-mode-map (kbd "C-m") 'newline))
  788. (setq auto-mode-alist
  789. `(("\\.adoc\\'" . adoc-mode)
  790. ("\\.asciidoc\\'" . adoc-mode)
  791. ,@auto-mode-alist)))
  792. (with-eval-after-load 'markup-faces
  793. ;; Is this too match ?
  794. (set-face-foreground 'markup-meta-face
  795. "color-245")
  796. (set-face-foreground 'markup-meta-hide-face
  797. "color-245")
  798. )
  799. ;; TODO: check if this is required
  800. (when (autoload-eval-lazily 'groovy-mode nil
  801. (defvar groovy-mode-map (make-sparse-keymap))
  802. (define-key groovy-mode-map "(" 'self-insert-command)
  803. (define-key groovy-mode-map ")" 'self-insert-command)
  804. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  805. )
  806. (add-to-list 'auto-mode-alist
  807. '("build\\.gradle\\'" . groovy-mode)))
  808. (with-eval-after-load 'yaml-mode
  809. (defvar yaml-mode-map (make-sparse-keymap))
  810. (define-key yaml-mode-map (kbd "C-m") 'newline))
  811. (with-eval-after-load 'html-mode
  812. (defvar html-mode-map (make-sparse-keymap))
  813. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  814. (with-eval-after-load 'text-mode
  815. (define-key text-mode-map (kbd "C-m") 'newline))
  816. (autoload-eval-lazily 'info nil
  817. (defvar Info-additional-directory-list)
  818. (dolist (dir (directory-files (concat user-emacs-directory
  819. "info")
  820. t
  821. "^[^.].*"))
  822. (when (file-directory-p dir)
  823. (add-to-list 'Info-additional-directory-list
  824. dir)))
  825. (let ((dir (expand-file-name "~/.brew/share/info")))
  826. (when (file-directory-p dir)
  827. (add-to-list 'Info-additional-directory-list
  828. dir))))
  829. (with-eval-after-load 'apropos
  830. (defvar apropos-mode-map (make-sparse-keymap))
  831. (define-key apropos-mode-map "n" 'next-line)
  832. (define-key apropos-mode-map "p" 'previous-line))
  833. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  834. ;; (define-key isearch-mode-map
  835. ;; (kbd "C-j") 'isearch-other-control-char)
  836. ;; (define-key isearch-mode-map
  837. ;; (kbd "C-k") 'isearch-other-control-char)
  838. ;; (define-key isearch-mode-map
  839. ;; (kbd "C-h") 'isearch-other-control-char)
  840. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  841. (define-key isearch-mode-map (kbd "M-r")
  842. 'isearch-query-replace-regexp)
  843. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  844. (setq lazy-highlight-cleanup nil)
  845. ;; face for isearch highlighing
  846. (set-face-attribute 'lazy-highlight
  847. nil
  848. :foreground `unspecified
  849. :background `unspecified
  850. :underline t
  851. ;; :weight `bold
  852. )
  853. (add-hook 'outline-mode-hook
  854. (lambda ()
  855. (when (string-match "\\.md\\'" buffer-file-name)
  856. (set (make-local-variable 'outline-regexp) "#+ "))))
  857. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  858. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  859. (when (autoload-eval-lazily 'markdown-mode
  860. '(markdown-mode gfm-mode)
  861. (defvar gfm-mode-map (make-sparse-keymap))
  862. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  863. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  864. (set-variable 'markdown-command (or (executable-find "markdown")
  865. (executable-find "markdown.pl")
  866. ""))
  867. (add-hook 'markdown-mode-hook
  868. (lambda ()
  869. (outline-minor-mode 1)
  870. (flyspell-mode)
  871. (set (make-local-variable 'comment-start) ";")))
  872. )
  873. ;; c-mode
  874. ;; http://www.emacswiki.org/emacs/IndentingC
  875. ;; http://en.wikipedia.org/wiki/Indent_style
  876. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  877. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  878. (with-eval-after-load 'cc-vars
  879. (defvar c-default-style nil)
  880. (add-to-list 'c-default-style
  881. '(c-mode . "k&r"))
  882. (add-to-list 'c-default-style
  883. '(c++-mode . "k&r")))
  884. (autoload-eval-lazily 'js2-mode nil
  885. ;; currently do not use js2-mode
  886. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  887. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  888. ;; (defvar js2-mode-map (make-sparse-keymap))
  889. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  890. ;; (interactive)
  891. ;; (js2-enter-key)
  892. ;; (indent-for-tab-command)))
  893. ;; (add-hook (kill-local-variable 'before-save-hook)
  894. ;; 'js2-before-save)
  895. ;; (add-hook 'before-save-hook
  896. ;; 'my-indent-buffer
  897. ;; nil
  898. ;; t)
  899. )
  900. (add-to-list 'interpreter-mode-alist
  901. '("node" . js-mode))
  902. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  903. (with-eval-after-load 'uniquify
  904. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  905. (setq uniquify-ignore-buffers-re "*[^*]+*")
  906. (setq uniquify-min-dir-content 1))
  907. (with-eval-after-load 'view
  908. (defvar view-mode-map (make-sparse-keymap))
  909. (define-key view-mode-map "j" 'scroll-up-line)
  910. (define-key view-mode-map "k" 'scroll-down-line)
  911. (define-key view-mode-map "v" 'toggle-read-only)
  912. (define-key view-mode-map "q" 'bury-buffer)
  913. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  914. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  915. ;; (define-key view-mode-map
  916. ;; "n" 'nonincremental-repeat-search-forward)
  917. ;; (define-key view-mode-map
  918. ;; "N" 'nonincremental-repeat-search-backward)
  919. (define-key view-mode-map "/" 'isearch-forward-regexp)
  920. (define-key view-mode-map "?" 'isearch-backward-regexp)
  921. (define-key view-mode-map "n" 'isearch-repeat-forward)
  922. (define-key view-mode-map "N" 'isearch-repeat-backward)
  923. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  924. (global-set-key "\M-r" 'view-mode)
  925. ;; (setq view-read-only t)
  926. (with-eval-after-load 'term
  927. (defvar term-raw-map (make-sparse-keymap))
  928. (define-key term-raw-map (kbd "C-x")
  929. (lookup-key (current-global-map)
  930. (kbd "C-x"))))
  931. (add-hook 'term-mode-hook
  932. (lambda ()
  933. ;; Stop current line highlighting
  934. (set (make-local-variable (defvar hl-line-range-function))
  935. (lambda () '(0 . 0)))
  936. (set (make-local-variable 'scroll-margin)
  937. 0)))
  938. (add-hook 'Man-mode-hook
  939. (lambda ()
  940. (view-mode 1)
  941. (setq truncate-lines nil)))
  942. (set-variable 'Man-notify-method (if window-system
  943. 'newframe
  944. 'aggressive))
  945. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  946. "woman_cache.el")))
  947. ;; not work because man.el will be loaded when man called
  948. (defalias 'man 'woman)
  949. (add-to-list 'auto-mode-alist
  950. '("tox\\.ini\\'" . conf-unix-mode))
  951. (when (autoload-eval-lazily 'toml-mode)
  952. (add-to-list 'auto-mode-alist
  953. '("/Pipfile\\'" . toml-mode)))
  954. (when (autoload-eval-lazily 'json-mode)
  955. (add-to-list 'auto-mode-alist
  956. '("/Pipfile\\.lock\\'" . json-mode)))
  957. (add-hook 'go-mode-hook
  958. (lambda()
  959. (defvar go-mode-map)
  960. (add-hook 'before-save-hook' 'gofmt-before-save)
  961. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  962. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  963. ;; buffer switching
  964. (defvar bs-configurations)
  965. (when (autoload-eval-lazily 'bs '(bs-show)
  966. (add-to-list 'bs-configurations
  967. '("specials" "^\\*" nil ".*" nil nil))
  968. (add-to-list 'bs-configurations
  969. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  970. (defvar bs-mode-map)
  971. (defvar bs-current-configuration)
  972. (define-key bs-mode-map (kbd "t")
  973. ;; TODO: fix toggle feature
  974. (lambda ()
  975. (interactive)
  976. (if (string= "specials"
  977. bs-current-configuration)
  978. (bs-set-configuration "files")
  979. (bs-set-configuration "specials"))
  980. (bs-refresh)
  981. (bs-message-without-log "%s"
  982. (bs--current-config-message))))
  983. ;; (setq bs-configurations (list
  984. ;; '("processes" nil get-buffer-process ".*" nil nil)
  985. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  986. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  987. )
  988. (defalias 'list-buffers 'bs-show)
  989. (set-variable 'bs-default-configuration "files-and-specials")
  990. (set-variable 'bs-default-sort-name "by nothing")
  991. (add-hook 'bs-mode-hook
  992. (lambda ()
  993. (set (make-local-variable 'scroll-margin) 0))))
  994. ;;(iswitchb-mode 1)
  995. (icomplete-mode)
  996. (defun iswitchb-buffer-display-other-window ()
  997. "Do iswitchb in other window."
  998. (interactive)
  999. (let ((iswitchb-default-method 'display))
  1000. (call-interactively 'iswitchb-buffer)))
  1001. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1002. ;; vc
  1003. (set-variable 'vc-handled-backends '())
  1004. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1005. ;; recentf-mode
  1006. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1007. "recentf")))
  1008. (set-variable 'recentf-max-menu-items 20)
  1009. (set-variable 'recentf-max-saved-items 30)
  1010. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1011. (set-variable 'recentf-auto-cleanup 3)
  1012. (safe-require-or-eval 'sync-recentf)
  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