Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

1502 righe
49 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. (set-variable 'ac-ignore-case nil)
  648. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  649. (define-key ctl-x-map "t" 'term-run-shell-command))
  650. (add-to-list 'safe-local-variable-values
  651. '(encoding utf-8))
  652. (setq enable-local-variables :safe)
  653. ;; (when (safe-require-or-eval 'remember-major-modes-mode)
  654. ;; (remember-major-modes-mode 1))
  655. ;; Detect file type from shebang and set major-mode.
  656. (add-to-list 'interpreter-mode-alist
  657. '("python3" . python-mode))
  658. (add-to-list 'interpreter-mode-alist
  659. '("python2" . python-mode))
  660. (with-eval-after-load 'python
  661. (defvar python-mode-map (make-sparse-keymap))
  662. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  663. ;; http://fukuyama.co/foreign-regexp
  664. '(and (safe-require-or-eval 'foreign-regexp)
  665. (progn
  666. (setq foreign-regexp/regexp-type 'perl)
  667. '(setq reb-re-syntax 'foreign-regexp)
  668. ))
  669. (autoload-eval-lazily 'sql '(sql-mode)
  670. (require 'sql-indent nil t))
  671. (when (autoload-eval-lazily 'git-command)
  672. (define-key ctl-x-map "g" 'git-command))
  673. (when (safe-require-or-eval 'git-commit)
  674. (global-git-commit-mode 1))
  675. (with-eval-after-load 'git-commit
  676. (add-hook 'git-commit-setup-hook
  677. 'turn-off-auto-fill t))
  678. (autoload-eval-lazily 'sl)
  679. (with-eval-after-load 'rst
  680. (defvar rst-mode-map)
  681. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  682. (with-eval-after-load 'jdee
  683. (add-hook 'jdee-mode-hook
  684. (lambda ()
  685. (make-local-variable 'global-mode-string)
  686. (add-to-list 'global-mode-string
  687. mode-line-position))))
  688. ;; Cannot enable error thrown. Why???
  689. ;; https://github.com/m0smith/malabar-mode#Installation
  690. ;; (when (autoload-eval-lazily 'malabar-mode)
  691. ;; (add-to-list 'load-path
  692. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  693. ;; (safe-require-or-eval 'cedet-devel-load)
  694. ;; (call-after-init 'activate-malabar-mode))
  695. (with-eval-after-load 'make-mode
  696. (defvar makefile-mode-map (make-sparse-keymap))
  697. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  698. ;; this functions is set in write-file-functions, i cannot find any
  699. ;; good way to remove this.
  700. (fset 'makefile-warn-suspicious-lines 'ignore))
  701. (with-eval-after-load 'verilog-mode
  702. (defvar verilog-mode-map (make-sparse-keymap))
  703. (define-key verilog-mode-map ";" 'self-insert-command))
  704. (setq diff-switches "-u")
  705. (with-eval-after-load 'diff-mode
  706. ;; (when (and (eq major-mode
  707. ;; 'diff-mode)
  708. ;; (not buffer-file-name))
  709. ;; ;; do not pass when major-mode is derived mode of diff-mode
  710. ;; (view-mode 1))
  711. (set-face-attribute 'diff-header nil
  712. :foreground nil
  713. :background nil
  714. :weight 'bold)
  715. (set-face-attribute 'diff-file-header nil
  716. :foreground nil
  717. :background nil
  718. :weight 'bold)
  719. (set-face-foreground 'diff-index "blue")
  720. (set-face-attribute 'diff-hunk-header nil
  721. :foreground "cyan"
  722. :weight 'normal)
  723. (set-face-attribute 'diff-context nil
  724. ;; :foreground "white"
  725. :foreground nil
  726. :weight 'normal)
  727. (set-face-foreground 'diff-removed "red")
  728. (set-face-foreground 'diff-added "green")
  729. (set-face-background 'diff-removed nil)
  730. (set-face-background 'diff-added nil)
  731. (set-face-attribute 'diff-changed nil
  732. :foreground "magenta"
  733. :weight 'normal)
  734. (set-face-attribute 'diff-refine-changed nil
  735. :foreground nil
  736. :background nil
  737. :weight 'bold
  738. :inverse-video t)
  739. ;; Annoying !
  740. ;;(diff-auto-refine-mode)
  741. )
  742. ;; (ffap-bindings)
  743. (set-variable 'browse-url-browser-function
  744. 'eww-browse-url)
  745. (set-variable 'sh-here-document-word "__EOC__")
  746. (when (autoload-eval-lazily 'adoc-mode
  747. nil
  748. (defvar adoc-mode-map (make-sparse-keymap))
  749. (define-key adoc-mode-map (kbd "C-m") 'newline))
  750. (setq auto-mode-alist
  751. `(("\\.adoc\\'" . adoc-mode)
  752. ("\\.asciidoc\\'" . adoc-mode)
  753. ,@auto-mode-alist)))
  754. (with-eval-after-load 'markup-faces
  755. ;; Is this too match ?
  756. (set-face-foreground 'markup-meta-face
  757. "color-245")
  758. (set-face-foreground 'markup-meta-hide-face
  759. "color-245")
  760. )
  761. ;; TODO: check if this is required
  762. (when (autoload-eval-lazily 'groovy-mode nil
  763. (defvar groovy-mode-map (make-sparse-keymap))
  764. (define-key groovy-mode-map "(" 'self-insert-command)
  765. (define-key groovy-mode-map ")" 'self-insert-command)
  766. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  767. )
  768. (add-to-list 'auto-mode-alist
  769. '("build\\.gradle\\'" . groovy-mode)))
  770. (with-eval-after-load 'yaml-mode
  771. (defvar yaml-mode-map (make-sparse-keymap))
  772. (define-key yaml-mode-map (kbd "C-m") 'newline))
  773. (with-eval-after-load 'html-mode
  774. (defvar html-mode-map (make-sparse-keymap))
  775. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  776. (with-eval-after-load 'text-mode
  777. (define-key text-mode-map (kbd "C-m") 'newline))
  778. (add-to-list 'Info-default-directory-list
  779. (expand-file-name "~/.info/emacs-ja"))
  780. (with-eval-after-load 'apropos
  781. (defvar apropos-mode-map (make-sparse-keymap))
  782. (define-key apropos-mode-map "n" 'next-line)
  783. (define-key apropos-mode-map "p" 'previous-line))
  784. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  785. ;; (define-key isearch-mode-map
  786. ;; (kbd "C-j") 'isearch-other-control-char)
  787. ;; (define-key isearch-mode-map
  788. ;; (kbd "C-k") 'isearch-other-control-char)
  789. ;; (define-key isearch-mode-map
  790. ;; (kbd "C-h") 'isearch-other-control-char)
  791. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  792. (define-key isearch-mode-map (kbd "M-r")
  793. 'isearch-query-replace-regexp)
  794. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  795. (setq lazy-highlight-cleanup nil)
  796. ;; face for isearch highlighing
  797. (set-face-attribute 'lazy-highlight
  798. nil
  799. :foreground `unspecified
  800. :background `unspecified
  801. :underline t
  802. ;; :weight `bold
  803. )
  804. (add-hook 'outline-mode-hook
  805. (lambda ()
  806. (when (string-match "\\.md\\'" buffer-file-name)
  807. (set (make-local-variable 'outline-regexp) "#+ "))))
  808. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  809. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  810. (when (autoload-eval-lazily 'markdown-mode
  811. '(markdown-mode gfm-mode)
  812. (defvar gfm-mode-map (make-sparse-keymap))
  813. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  814. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  815. (set-variable 'markdown-command (or (executable-find "markdown")
  816. (executable-find "markdown.pl")
  817. ""))
  818. (add-hook 'markdown-mode-hook
  819. (lambda ()
  820. (outline-minor-mode 1)
  821. (flyspell-mode)
  822. (set (make-local-variable 'comment-start) ";")))
  823. )
  824. ;; c-mode
  825. ;; http://www.emacswiki.org/emacs/IndentingC
  826. ;; http://en.wikipedia.org/wiki/Indent_style
  827. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  828. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  829. (with-eval-after-load 'cc-vars
  830. (defvar c-default-style nil)
  831. (add-to-list 'c-default-style
  832. '(c-mode . "k&r"))
  833. (add-to-list 'c-default-style
  834. '(c++-mode . "k&r")))
  835. (autoload-eval-lazily 'js2-mode nil
  836. ;; currently do not use js2-mode
  837. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  838. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  839. ;; (defvar js2-mode-map (make-sparse-keymap))
  840. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  841. ;; (interactive)
  842. ;; (js2-enter-key)
  843. ;; (indent-for-tab-command)))
  844. ;; (add-hook (kill-local-variable 'before-save-hook)
  845. ;; 'js2-before-save)
  846. ;; (add-hook 'before-save-hook
  847. ;; 'my-indent-buffer
  848. ;; nil
  849. ;; t)
  850. )
  851. (add-to-list 'interpreter-mode-alist
  852. '("node" . js-mode))
  853. (when (autoload-eval-lazily 'flymake-jslint
  854. '(flymake-jslint-load))
  855. (autoload-eval-lazily 'js nil
  856. (add-hook 'js-mode-hook
  857. 'flymake-jslint-load)))
  858. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  859. (with-eval-after-load 'uniquify
  860. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  861. (setq uniquify-ignore-buffers-re "*[^*]+*")
  862. (setq uniquify-min-dir-content 1))
  863. (with-eval-after-load 'view
  864. (defvar view-mode-map (make-sparse-keymap))
  865. (define-key view-mode-map "j" 'scroll-up-line)
  866. (define-key view-mode-map "k" 'scroll-down-line)
  867. (define-key view-mode-map "v" 'toggle-read-only)
  868. (define-key view-mode-map "q" 'bury-buffer)
  869. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  870. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  871. ;; (define-key view-mode-map
  872. ;; "n" 'nonincremental-repeat-search-forward)
  873. ;; (define-key view-mode-map
  874. ;; "N" 'nonincremental-repeat-search-backward)
  875. (define-key view-mode-map "/" 'isearch-forward-regexp)
  876. (define-key view-mode-map "?" 'isearch-backward-regexp)
  877. (define-key view-mode-map "n" 'isearch-repeat-forward)
  878. (define-key view-mode-map "N" 'isearch-repeat-backward)
  879. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  880. (global-set-key "\M-r" 'view-mode)
  881. ;; (setq view-read-only t)
  882. (with-eval-after-load 'term
  883. (defvar term-raw-map (make-sparse-keymap))
  884. (define-key term-raw-map (kbd "C-x")
  885. (lookup-key (current-global-map)
  886. (kbd "C-x"))))
  887. (add-hook 'term-mode-hook
  888. (lambda ()
  889. ;; Stop current line highlighting
  890. (set (make-local-variable (defvar hl-line-range-function))
  891. (lambda () '(0 . 0)))
  892. (set (make-local-variable 'scroll-margin)
  893. 0)))
  894. (add-hook 'Man-mode-hook
  895. (lambda ()
  896. (view-mode 1)
  897. (setq truncate-lines nil)))
  898. (set-variable 'Man-notify-method (if window-system
  899. 'newframe
  900. 'aggressive))
  901. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  902. "woman_cache.el")))
  903. ;; not work because man.el will be loaded when man called
  904. (defalias 'man 'woman)
  905. (add-to-list 'auto-mode-alist
  906. '("tox\\.ini\\'" . conf-unix-mode))
  907. (when (autoload-eval-lazily 'toml-mode)
  908. (add-to-list 'auto-mode-alist
  909. '("/Pipfile\\'" . toml-mode)))
  910. (when (autoload-eval-lazily 'json-mode)
  911. (add-to-list 'auto-mode-alist
  912. '("/Pipfile\\.lock\\'" . json-mode)))
  913. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  914. ;; buffer switching
  915. (defvar bs-configurations)
  916. (when (autoload-eval-lazily 'bs '(bs-show)
  917. (add-to-list 'bs-configurations
  918. '("specials" "^\\*" nil ".*" nil nil))
  919. (add-to-list 'bs-configurations
  920. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  921. (defvar bs-mode-map)
  922. (defvar bs-current-configuration)
  923. (define-key bs-mode-map (kbd "t")
  924. ;; TODO: fix toggle feature
  925. (lambda ()
  926. (interactive)
  927. (if (string= "specials"
  928. bs-current-configuration)
  929. (bs-set-configuration "files")
  930. (bs-set-configuration "specials"))
  931. (bs-refresh)
  932. (bs-message-without-log "%s"
  933. (bs--current-config-message))))
  934. ;; (setq bs-configurations (list
  935. ;; '("processes" nil get-buffer-process ".*" nil nil)
  936. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  937. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  938. )
  939. (defalias 'list-buffers 'bs-show)
  940. (set-variable 'bs-default-configuration "files-and-specials")
  941. (set-variable 'bs-default-sort-name "by nothing")
  942. (add-hook 'bs-mode-hook
  943. (lambda ()
  944. (set (make-local-variable 'scroll-margin) 0))))
  945. ;;(iswitchb-mode 1)
  946. (icomplete-mode)
  947. (defun iswitchb-buffer-display-other-window ()
  948. "Do iswitchb in other window."
  949. (interactive)
  950. (let ((iswitchb-default-method 'display))
  951. (call-interactively 'iswitchb-buffer)))
  952. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  953. ;; vc
  954. (set-variable 'vc-handled-backends '())
  955. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  956. ;; recentf-mode
  957. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  958. "recentf")))
  959. (set-variable 'recentf-max-menu-items 20)
  960. (set-variable 'recentf-max-saved-items 30)
  961. (set-variable 'recentf-show-file-shortcuts-flag nil)
  962. (when (safe-require-or-eval 'recentf)
  963. (add-to-list 'recentf-exclude
  964. (regexp-quote recentf-save-file))
  965. (add-to-list 'recentf-exclude
  966. (regexp-quote (expand-file-name user-emacs-directory)))
  967. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  968. (remove-hook 'find-file-hook
  969. 'recentf-track-opened-file)
  970. (defun my-recentf-load-track-save-list ()
  971. "Load current recentf list from file, track current visiting file, then save
  972. the list."
  973. (recentf-load-list)
  974. (recentf-track-opened-file)
  975. (recentf-save-list))
  976. (add-hook 'find-file-hook
  977. 'my-recentf-load-track-save-list)
  978. (add-hook 'kill-emacs-hook
  979. 'recentf-load-list)
  980. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  981. ;; (add-hook 'find-file-hook
  982. ;; (lambda ()
  983. ;; (recentf-add-file default-directory)))
  984. (and (autoload-eval-lazily 'recentf-show)
  985. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  986. (add-hook 'recentf-show-before-listing-hook
  987. 'recentf-load-list))
  988. (recentf-mode 1)
  989. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  990. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  991. (define-key recentf-dialog-mode-map "p" 'previous-line)
  992. (define-key recentf-dialog-mode-map "n" 'next-line)
  993. (add-hook 'recentf-dialog-mode-hook
  994. (lambda ()
  995. ;; (recentf-save-list)
  996. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  997. ;; 'my-recentf-cd-and-find-file)
  998. (cd "~/"))))
  999. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1000. ;; dired
  1001. (defun my-dired-echo-file-head (arg)
  1002. "Echo head of current file.
  1003. ARG is num to show, or defaults to 7."
  1004. (interactive "P")
  1005. (let ((f (dired-get-filename)))
  1006. (message "%s"
  1007. (with-temp-buffer
  1008. (insert-file-contents f)
  1009. (buffer-substring-no-properties
  1010. (point-min)
  1011. (progn (goto-char (point-min))
  1012. (forward-line (1- (if arg
  1013. (prefix-numeric-value arg)
  1014. 7)))
  1015. (point-at-eol)))))))
  1016. (defun my-dired-diff ()
  1017. "Show diff of marked file and file of current line."
  1018. (interactive)
  1019. (let ((files (dired-get-marked-files nil nil nil t)))
  1020. (if (eq (car files)
  1021. t)
  1022. (diff (cadr files) (dired-get-filename))
  1023. (message "One file must be marked!"))))
  1024. (defun dired-get-file-info ()
  1025. "Print information of current line file."
  1026. (interactive)
  1027. (let ((f (shell-quote-argument (dired-get-filename t))))
  1028. (if (file-directory-p f)
  1029. (progn
  1030. (message "Calculating disk usage...")
  1031. (shell-command (concat "du -hsD "
  1032. f)))
  1033. (shell-command (concat "file "
  1034. f)))))
  1035. (defun my-dired-scroll-up ()
  1036. "Scroll up."
  1037. (interactive)
  1038. (my-dired-previous-line (- (window-height) 1)))
  1039. (defun my-dired-scroll-down ()
  1040. "Scroll down."
  1041. (interactive)
  1042. (my-dired-next-line (- (window-height) 1)))
  1043. ;; (defun my-dired-forward-line (arg)
  1044. ;; ""
  1045. ;; (interactive "p"))
  1046. (defun my-dired-previous-line (arg)
  1047. "Move ARG lines up."
  1048. (interactive "p")
  1049. (if (> arg 0)
  1050. (progn
  1051. (if (eq (line-number-at-pos)
  1052. 1)
  1053. (goto-char (point-max))
  1054. (forward-line -1))
  1055. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1056. (dired-get-subdir))
  1057. (- arg 1)
  1058. arg)))
  1059. (dired-move-to-filename)))
  1060. (defun my-dired-next-line (arg)
  1061. "Move ARG lines down."
  1062. (interactive "p")
  1063. (if (> arg 0)
  1064. (progn
  1065. (if (eq (point)
  1066. (point-max))
  1067. (goto-char (point-min))
  1068. (forward-line 1))
  1069. (my-dired-next-line (if (or (dired-get-filename nil t)
  1070. (dired-get-subdir))
  1071. (- arg 1)
  1072. arg)))
  1073. (dired-move-to-filename)))
  1074. (defun my-dired-tramp-open (host)
  1075. "Open HOST home directory."
  1076. (interactive "sHostname: ")
  1077. (find-file (format "/scp:%s:" host)))
  1078. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1079. (if (eq window-system 'mac)
  1080. (setq dired-listing-switches "-lhF")
  1081. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1082. )
  1083. (setq dired-listing-switches "-lhF")
  1084. (put 'dired-find-alternate-file 'disabled nil)
  1085. ;; when using dired-find-alternate-file
  1086. ;; reuse current dired buffer for the file to open
  1087. (set-variable 'dired-ls-F-marks-symlinks t)
  1088. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1089. (set-variable 'ls-lisp-dirs-first t)
  1090. (set-variable 'ls-lisp-use-localized-time-format t)
  1091. (set-variable 'ls-lisp-format-time-list
  1092. '("%Y-%m-%d %H:%M"
  1093. "%Y-%m-%d "))
  1094. (set-variable 'dired-dwim-target t)
  1095. (set-variable 'dired-isearch-filenames t)
  1096. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1097. (set-variable 'dired-hide-details-hide-information-lines nil)
  1098. ;; (add-hook 'dired-after-readin-hook
  1099. ;; 'my-replace-nasi-none)
  1100. (with-eval-after-load 'dired
  1101. (safe-require-or-eval 'ls-lisp)
  1102. (defvar dired-mode-map (make-sparse-keymap))
  1103. (define-key dired-mode-map "i" 'dired-get-file-info)
  1104. (define-key dired-mode-map "f" 'find-file)
  1105. (define-key dired-mode-map "!" 'shell-command)
  1106. (define-key dired-mode-map "&" 'async-shell-command)
  1107. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1108. (define-key dired-mode-map "=" 'my-dired-diff)
  1109. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1110. (define-key dired-mode-map "b" 'gtkbm)
  1111. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1112. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1113. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1114. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1115. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1116. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1117. (substitute-key-definition 'dired-next-line
  1118. 'my-dired-next-line
  1119. dired-mode-map)
  1120. (substitute-key-definition 'dired-previous-line
  1121. 'my-dired-previous-line
  1122. dired-mode-map)
  1123. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1124. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1125. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1126. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1127. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1128. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1129. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1130. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1131. (add-hook 'dired-mode-hook
  1132. (lambda ()
  1133. (when (fboundp 'dired-hide-details-mode)
  1134. (dired-hide-details-mode t)
  1135. (local-set-key "l" 'dired-hide-details-mode))
  1136. (let ((file "._Icon\015"))
  1137. (when nil
  1138. '(file-readable-p file)
  1139. (delete-file file)))))
  1140. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1141. (with-eval-after-load 'dired
  1142. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1143. (when (autoload-eval-lazily 'dired-list-all-mode)
  1144. (setq dired-listing-switches "-lhF")
  1145. (with-eval-after-load 'dired
  1146. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1147. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1148. ;; misc funcs
  1149. (defalias 'qcalc 'quick-calc)
  1150. (defun memo (&optional dir)
  1151. "Open memo.txt in DIR."
  1152. (interactive)
  1153. (pop-to-buffer (find-file-noselect (concat (if dir
  1154. (file-name-as-directory dir)
  1155. "")
  1156. "memo.txt"))))
  1157. (defvar my-rgrep-alist
  1158. `(
  1159. ;; git grep
  1160. ("gitgrep"
  1161. (eq 0
  1162. (shell-command "git rev-parse --git-dir"))
  1163. "git --no-pager -c color.grep=false grep -nH -e ")
  1164. ;; ripgrep
  1165. ("rg"
  1166. (executable-find "rg")
  1167. "rg --no-heading --color=never --smart-case ")
  1168. ;; sift
  1169. ("sift"
  1170. (executable-find "sift")
  1171. ("sift --no-color --binary-skip --filename --line-number --git --smart-case "))
  1172. ;; the silver searcher
  1173. ("ag"
  1174. (executable-find "ag")
  1175. "ag --nocolor --nogroup --nopager --filename ")
  1176. ;; ack
  1177. ("ack"
  1178. (executable-find "ack")
  1179. "ack --nocolor --nogroup --nopager --with-filename ")
  1180. ;; gnu global
  1181. ("global"
  1182. (and (require 'gtags nil t)
  1183. (executable-find "global")
  1184. (gtags-get-rootpath))
  1185. "global --result grep ")
  1186. ;; grep
  1187. ("grep"
  1188. t
  1189. ,(concat "find . "
  1190. "-path '*/.git' -prune -o "
  1191. "-path '*/.svn' -prune -o "
  1192. "-type f -print0 | "
  1193. "xargs -0 grep -nH -e "))
  1194. )
  1195. "Alist of rgrep command.
  1196. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1197. condition to choose COMMAND when evaluated.")
  1198. (defvar my-rgrep-default nil
  1199. "Default command name for my-rgrep.")
  1200. (defun my-rgrep-grep-command (&optional name alist)
  1201. "Return recursive grep command for current directory or nil.
  1202. If NAME is given, use that without testing.
  1203. Commands are searched from ALIST."
  1204. (if alist
  1205. (if name
  1206. ;; if name is given search that from alist and return the command
  1207. (nth 2 (assoc name
  1208. alist))
  1209. ;; if name is not given try test in 1th elem
  1210. (let ((car (car alist))
  1211. (cdr (cdr alist)))
  1212. (if (eval (nth 1 car))
  1213. ;; if the condition is true return the command
  1214. (nth 2 car)
  1215. ;; try next one
  1216. (and cdr
  1217. (my-rgrep-grep-command name cdr)))))
  1218. ;; if alist is not given set default value
  1219. (my-rgrep-grep-command name my-rgrep-alist)))
  1220. (defun my-rgrep (command-args)
  1221. "My recursive grep. Run COMMAND-ARGS."
  1222. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1223. nil)))
  1224. (if cmd
  1225. (list (read-shell-command "grep command: "
  1226. cmd
  1227. 'grep-find-history))
  1228. (error "My-Rgrep: Command for rgrep not found")
  1229. )))
  1230. (compilation-start command-args
  1231. 'grep-mode))
  1232. ;; (defun my-rgrep-symbol-at-point (command-args)
  1233. ;; "My recursive grep. Run COMMAND-ARGS."
  1234. ;; (interactive (list (read-shell-command "grep command: "
  1235. ;; (concat (my-rgrep-grep-command)
  1236. ;; " "
  1237. ;; (thing-at-point 'symbol))
  1238. ;; 'grep-find-history)))
  1239. ;; (compilation-start command-args
  1240. ;; 'grep-mode))
  1241. (defmacro define-my-rgrep (name)
  1242. "Define rgrep for NAME."
  1243. `(defun ,(intern (concat "my-rgrep-"
  1244. name)) ()
  1245. ,(format "My recursive grep by %s."
  1246. name)
  1247. (interactive)
  1248. (let ((my-rgrep-default ,name))
  1249. (if (called-interactively-p 'any)
  1250. (call-interactively 'my-rgrep)
  1251. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1252. )
  1253. (define-my-rgrep "ack")
  1254. (define-my-rgrep "ag")
  1255. (define-my-rgrep "rg")
  1256. (define-my-rgrep "sift")
  1257. (define-my-rgrep "gitgrep")
  1258. (define-my-rgrep "grep")
  1259. (define-my-rgrep "global")
  1260. (define-key ctl-x-map "s" 'my-rgrep)
  1261. (defalias 'make 'compile)
  1262. (define-key ctl-x-map "c" 'compile)
  1263. ;;;;;;;;;;;;;;;;;;;;;;;
  1264. ;; adoc-simple-mode
  1265. (when (safe-require-or-eval 'adoc-mode)
  1266. (defvar adoc-simple-font-lock-keywords
  1267. nil)
  1268. (define-derived-mode adoc-simple-mode adoc-mode
  1269. "Adoc-Simple"
  1270. "Major mode for editing AsciiDoc text files.
  1271. This mode is a simplified version of `adoc-mode'."
  1272. '(set (make-local-variable 'font-lock-defaults)
  1273. '(adoc-simple-font-lock-keywords
  1274. nil nil nil nil
  1275. (font-lock-multiline . t)
  1276. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1277. (add-to-list 'auto-mode-alist
  1278. '("\\.adoc\\'" . adoc-simple-mode)))
  1279. ;;; emacs.el ends here