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.
 
 
 
 
 
 

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