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.
 
 
 
 
 
 

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