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.
 
 
 
 
 
 

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