Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

1615 рядки
52 KiB

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