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.
 
 
 
 
 
 

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