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.
 
 
 
 
 
 

1692 line
55 KiB

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