您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

2103 行
69 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Code:
  3. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  4. ;; SETUP_LOAD: (and (file-readable-p file)
  5. ;; SETUP_LOAD: (byte-recompile-file file nil 0 t)))
  6. (setq debug-on-error t)
  7. ;; make directories
  8. (unless (file-directory-p (expand-file-name user-emacs-directory))
  9. (make-directory (expand-file-name user-emacs-directory)))
  10. (require 'cl-lib)
  11. (require 'simple)
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. ;; Some macros for internals
  14. (defun call-after-init (func)
  15. "If `after-init-hook' has been run, call FUNC immediately.
  16. Otherwize hook it."
  17. (if after-init-time
  18. (funcall func)
  19. (add-hook 'after-init-hook
  20. func)))
  21. (defmacro safe-require-or-eval (feature)
  22. "Require FEATURE if available.
  23. At compile time the feature will be loaded immediately."
  24. `(eval-and-compile
  25. (message "safe-require-or-eval: Trying to require %s" ,feature)
  26. (require ,feature nil t)))
  27. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  28. "Define autoloading FEATURE that defines FUNCTIONS.
  29. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  30. the function same as FEATURE is defined as autoloaded function. BODY is passed
  31. to `eval-after-load'.
  32. After this macro is expanded, this returns the path to library if FEATURE
  33. found, otherwise returns nil."
  34. (declare (indent 2) (debug t))
  35. (let* ((libname (symbol-name (eval feature)))
  36. (libpath (locate-library libname)))
  37. `(progn
  38. (when (locate-library ,libname)
  39. ,@(mapcar (lambda (f)
  40. `(unless (fboundp ',f)
  41. (progn
  42. (message "Autoloaded function `%S' defined (%s)"
  43. (quote ,f)
  44. ,libpath)
  45. (autoload (quote ,f)
  46. ,libname
  47. ,(concat "Autoloaded function defined in \""
  48. libpath
  49. "\".")
  50. t))))
  51. (or (eval functions)
  52. `(,(eval feature)))))
  53. (eval-after-load ,feature
  54. (quote (progn
  55. ,@body)))
  56. (locate-library ,libname))))
  57. (when (autoload-eval-lazily 'tetris nil
  58. (message "Tetris loaded!"))
  59. (message "Tetris found!"))
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. ;; package
  62. (set (defvar 10sr-package-list)
  63. '(
  64. vimrc-mode
  65. markdown-mode
  66. yaml-mode
  67. gnuplot-mode
  68. php-mode
  69. erlang
  70. js2-mode
  71. js-doc
  72. git-commit
  73. gitignore-mode
  74. adoc-mode
  75. go-mode
  76. ;; It seems malabar has been merged into jdee and this package
  77. ;; already removed
  78. ;; malabar-mode
  79. gosh-mode
  80. scala-mode
  81. ;;ensime
  82. ;; ack
  83. color-moccur
  84. ggtags
  85. flycheck
  86. auto-highlight-symbol
  87. hl-todo
  88. ;; Currently not available
  89. ;; pp-c-l
  90. xclip
  91. foreign-regexp
  92. multi-term
  93. term-run
  94. editorconfig
  95. git-ps1-mode
  96. restart-emacs
  97. fill-column-indicator
  98. pkgbuild-mode
  99. minibuffer-line
  100. which-key
  101. ;; I think this works in place of my autosave lib
  102. super-save
  103. pipenv
  104. imenu-list
  105. page-break-lines
  106. ;; sync-recentf
  107. aggressive-indent
  108. ;; fancy-narrow
  109. dired-filter
  110. wgrep
  111. magit
  112. git-gutter
  113. end-mark
  114. sl
  115. ;; TODO: Configure pony-tpl-mode
  116. pony-mode
  117. editorconfig
  118. editorconfig-custom-majormode
  119. git-command
  120. prompt-text
  121. ;; 10sr repository
  122. ;; 10sr-extras
  123. terminal-title
  124. recentf-show
  125. dired-list-all-mode
  126. pack
  127. set-modeline-color
  128. read-only-only-mode
  129. smart-revert
  130. autosave
  131. ;;window-organizer
  132. ilookup
  133. pasteboard
  134. awk-preview
  135. recently
  136. ))
  137. (when (safe-require-or-eval 'package)
  138. (setq package-archives
  139. `(,@package-archives
  140. ("melpa" . "https://melpa.org/packages/")
  141. ;; Somehow fails to download via https
  142. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  143. (package-initialize)
  144. (defun my-auto-install-package ()
  145. "Install packages semi-automatically."
  146. (interactive)
  147. (package-refresh-contents)
  148. (mapc (lambda (pkg)
  149. (or (package-installed-p pkg)
  150. (package-install pkg)))
  151. 10sr-package-list))
  152. )
  153. ;; (lazy-load-eval 'sudoku)
  154. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  155. ;; my-idle-hook
  156. (defvar my-idle-hook nil
  157. "Hook run when idle for several secs.")
  158. (defvar my-idle-hook-sec 5
  159. "Second to run `my-idle-hook'.")
  160. (run-with-idle-timer my-idle-hook-sec
  161. t
  162. (lambda ()
  163. (run-hooks 'my-idle-hook)))
  164. ;; (add-hook 'my-idle-hook
  165. ;; (lambda ()
  166. ;; (message "idle hook message")))
  167. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  168. ;; start and quit
  169. (setq inhibit-startup-message t)
  170. (setq confirm-kill-emacs 'y-or-n-p)
  171. (setq gc-cons-threshold (* 1024 1024 16))
  172. (setq garbage-collection-messages nil)
  173. (when window-system
  174. (add-to-list 'default-frame-alist '(cursor-type . box))
  175. (add-to-list 'default-frame-alist '(background-color . "white"))
  176. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  177. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  178. ;; does not work?
  179. )
  180. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  181. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  182. (and (fboundp 'tool-bar-mode)
  183. (tool-bar-mode 0))
  184. (and (fboundp 'set-scroll-bar-mode)
  185. (set-scroll-bar-mode nil))
  186. (call-after-init
  187. (lambda ()
  188. (message "%s %s" invocation-name emacs-version)
  189. (message "Invocation directory: %s" default-directory)
  190. (message "%s was taken to initialize emacs." (emacs-init-time))
  191. (view-echo-area-messages)
  192. ;; (view-emacs-news)
  193. ))
  194. (cd ".") ; when using windows use / instead of \ in `default-directory'
  195. ;; locale
  196. (set-language-environment "Japanese")
  197. (set-default-coding-systems 'utf-8-unix)
  198. (prefer-coding-system 'utf-8-unix)
  199. (setq system-time-locale "C")
  200. ;; my prefix map
  201. (defvar my-prefix-map nil
  202. "My prefix map.")
  203. (define-prefix-command 'my-prefix-map)
  204. ;; (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  205. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  206. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  207. ;; (comint-show-maximum-output)
  208. ;; kill scratch
  209. (call-after-init (lambda ()
  210. (let ((buf (get-buffer "*scratch*")))
  211. (when buf
  212. (kill-buffer buf)))))
  213. ;; modifier keys
  214. ;; (setq mac-option-modifier 'control)
  215. ;; display
  216. (setq visible-bell t)
  217. (setq ring-bell-function 'ignore)
  218. (mouse-avoidance-mode 'banish)
  219. (setq echo-keystrokes 0.1)
  220. (defun reload-init-file ()
  221. "Reload Emacs init file."
  222. (interactive)
  223. (when (and user-init-file
  224. (file-readable-p user-init-file))
  225. (load-file user-init-file)))
  226. (safe-require-or-eval 'session)
  227. ;; server
  228. (set-variable 'server-name (concat "server"
  229. (number-to-string (emacs-pid))))
  230. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  231. ;; In Darwin environment, init fails with message like 'Service name too long'
  232. ;; when server-use-tcp is nil
  233. (when (or (eq system-type
  234. 'cygwin)
  235. (eq system-type
  236. 'darwin))
  237. (set-variable 'server-use-tcp t))
  238. ;; MSYS2 fix
  239. (when (eq system-type
  240. 'windows-nt)
  241. (setq shell-file-name
  242. (executable-find "bash"))
  243. '(setq function-key-map
  244. `(,@function-key-map ([pause] . [?\C-c])
  245. ))
  246. (define-key key-translation-map
  247. (kbd "<pause>")
  248. (kbd "C-c"))
  249. '(keyboard-translate [pause]
  250. (kbd "C-c")p)
  251. ;; TODO: move to other place later
  252. (when (not window-system)
  253. (setq interprogram-paste-function nil)
  254. (setq interprogram-cut-function nil)))
  255. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  256. ;; global keys
  257. (global-set-key (kbd "<up>") 'scroll-down-line)
  258. (global-set-key (kbd "<down>") 'scroll-up-line)
  259. (global-set-key (kbd "<left>") 'scroll-down)
  260. (global-set-key (kbd "<right>") 'scroll-up)
  261. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  262. (global-set-key (kbd "C-\\") help-map)
  263. (define-key ctl-x-map (kbd "DEL") help-map)
  264. (define-key ctl-x-map (kbd "C-h") help-map)
  265. (define-key help-map "a" 'apropos)
  266. ;; disable annoying keys
  267. (global-set-key [prior] 'ignore)
  268. (global-set-key (kbd "<next>") 'ignore)
  269. (global-set-key [menu] 'ignore)
  270. (global-set-key [down-mouse-1] 'ignore)
  271. (global-set-key [down-mouse-2] 'ignore)
  272. (global-set-key [down-mouse-3] 'ignore)
  273. (global-set-key [mouse-1] 'ignore)
  274. (global-set-key [mouse-2] 'ignore)
  275. (global-set-key [mouse-3] 'ignore)
  276. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  277. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  278. (when (safe-require-or-eval 'which-key)
  279. (which-key-mode))
  280. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  281. ;; editor
  282. (setq kill-whole-line t)
  283. (setq scroll-conservatively 35
  284. scroll-margin 2)
  285. (setq-default major-mode 'text-mode)
  286. (setq next-line-add-newlines nil)
  287. (setq kill-read-only-ok t)
  288. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  289. ;; (setq-default line-spacing 0.2)
  290. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  291. ;; (setq-default tab-width 4)
  292. (setq-default indent-tabs-mode nil)
  293. (setq-default indent-line-function 'indent-to-left-margin)
  294. ;; (setq-default indent-line-function nil)
  295. (setq-default truncate-lines nil)
  296. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  297. (delete-selection-mode 1)
  298. (cua-mode 0)
  299. (setq line-move-visual nil)
  300. (setq create-lockfiles nil)
  301. (add-hook 'before-save-hook
  302. 'time-stamp)
  303. ;; Add Time-stamp: <> to insert timestamp there
  304. (set-variable 'time-stamp-format
  305. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  306. ;; key bindings
  307. ;; moving around
  308. ;;(keyboard-translate ?\M-j ?\C-j)
  309. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  310. (define-key esc-map "p" 'backward-paragraph)
  311. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  312. (define-key esc-map "n" 'forward-paragraph)
  313. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  314. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  315. (global-set-key (kbd "C-<left>") 'scroll-down)
  316. (global-set-key (kbd "C-<right>") 'scroll-up)
  317. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  318. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  319. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  320. ;; C-h and DEL
  321. (global-set-key (kbd "C-h") (kbd "DEL"))
  322. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  323. (global-set-key (kbd "C-m") 'newline-and-indent)
  324. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  325. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  326. (define-key esc-map "u" 'undo)
  327. (define-key esc-map "i" (kbd "ESC TAB"))
  328. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  329. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  330. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  331. (require 'page-ext nil t)
  332. (when (safe-require-or-eval 'page-break-lines)
  333. (global-page-break-lines-mode 1))
  334. (when (safe-require-or-eval 'git-gutter)
  335. (custom-set-variables
  336. '(git-gutter:lighter " Gttr"))
  337. (custom-set-variables
  338. '(git-gutter:update-interval 2))
  339. (custom-set-variables
  340. '(git-gutter:unchanged-sign " "))
  341. (when (= (display-color-cells)
  342. 256)
  343. (let ((c "color-233"))
  344. (set-face-background 'git-gutter:modified c)
  345. (set-face-background 'git-gutter:added c)
  346. (set-face-background 'git-gutter:deleted c)
  347. (set-face-background 'git-gutter:unchanged c)))
  348. (global-git-gutter-mode 1)
  349. )
  350. ;; (when (safe-require-or-eval 'fancy-narrow)
  351. ;; (fancy-narrow-mode 1))
  352. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  353. ;; title and mode-line
  354. (when (safe-require-or-eval 'terminal-title)
  355. ;; if TERM is not screen use default value
  356. (if (getenv "TMUX")
  357. ;; if use tmux locally just basename of current dir
  358. (set-variable 'terminal-title-format
  359. '((file-name-nondirectory (directory-file-name
  360. default-directory))))
  361. (if (and (let ((tty-type (frame-parameter nil
  362. 'tty-type)))
  363. (and tty-type
  364. (equal (car (split-string tty-type
  365. "-"))
  366. "screen")))
  367. (not (getenv "SSH_CONNECTION")))
  368. (set-variable 'terminal-title-format
  369. '((file-name-nondirectory (directory-file-name
  370. default-directory))))
  371. ;; seems that TMUX is used locally and ssh to remote host
  372. (set-variable 'terminal-title-format
  373. `("em:"
  374. ,user-login-name
  375. "@"
  376. ,(car (split-string (system-name)
  377. "\\."))
  378. ":"
  379. default-directory))
  380. )
  381. )
  382. (terminal-title-mode))
  383. (setq eol-mnemonic-dos "\\r\\n")
  384. (setq eol-mnemonic-mac "\\r")
  385. (setq eol-mnemonic-unix "")
  386. (which-function-mode 1)
  387. (line-number-mode 0)
  388. (column-number-mode 0)
  389. (size-indication-mode 0)
  390. (setq mode-line-position
  391. '(:eval (format "L%%l/%d%s:C%%c"
  392. (count-lines (point-max)
  393. (point-min))
  394. (if (buffer-narrowed-p)
  395. "[N]"
  396. "")
  397. )))
  398. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  399. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  400. ;; minibuffer
  401. (setq insert-default-directory t)
  402. (setq completion-ignore-case t
  403. read-file-name-completion-ignore-case t
  404. read-buffer-completion-ignore-case t)
  405. (setq resize-mini-windows t)
  406. (temp-buffer-resize-mode 1)
  407. (savehist-mode 1)
  408. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  409. (set-variable 'help-at-pt-display-when-idle t)
  410. (fset 'yes-or-no-p 'y-or-n-p)
  411. ;; complete symbol when `eval'
  412. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  413. (define-key minibuffer-local-map (kbd "C-u")
  414. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  415. ;; I dont know these bindings are good
  416. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  417. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  418. (when (safe-require-or-eval 'minibuffer-line)
  419. (set-face-underline 'minibuffer-line nil)
  420. (set-variable 'minibuffer-line-refresh-interval
  421. 25)
  422. ;; Set idle timer
  423. (defvar my-minibuffer-line--idle-timer nil)
  424. (defvar minibuffer-line-mode)
  425. (add-hook 'minibuffer-line-mode-hook
  426. (lambda ()
  427. (when my-minibuffer-line--idle-timer
  428. (cancel-timer my-minibuffer-line--idle-timer)
  429. (setq my-minibuffer-line--idle-timer nil))
  430. (when minibuffer-line-mode
  431. (setq my-minibuffer-line--idle-timer
  432. (run-with-idle-timer 0.5
  433. t
  434. 'minibuffer-line--update)))))
  435. (set-variable 'minibuffer-line-format
  436. `(,(concat user-login-name
  437. "@"
  438. (car (split-string (system-name)
  439. "\\."))
  440. ":")
  441. (:eval (abbreviate-file-name (or buffer-file-name
  442. default-directory)))
  443. (:eval (and (fboundp 'git-ps1-mode-get-current)
  444. (git-ps1-mode-get-current " [GIT:%s]")))
  445. " "
  446. (:eval (format-time-string display-time-format))))
  447. (minibuffer-line-mode 1)
  448. )
  449. (when (safe-require-or-eval 'prompt-text)
  450. (set-variable 'prompt-text-format
  451. `(,(concat ""
  452. user-login-name
  453. "@"
  454. (car (split-string (system-name)
  455. "\\."))
  456. ":")
  457. (:eval (abbreviate-file-name (or buffer-file-name
  458. default-directory)))
  459. (:eval (and (fboundp 'git-ps1-mode-get-current)
  460. (git-ps1-mode-get-current " [GIT:%s]")))
  461. " "
  462. (:eval (format-time-string display-time-format))
  463. "\n"
  464. (:eval (symbol-name this-command))
  465. ": "))
  466. (prompt-text-mode 1))
  467. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  468. ;; letters, font-lock mode and fonts
  469. (setq text-quoting-style 'grave)
  470. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  471. ;; (set-window-margins (selected-window) 1 1)
  472. (unless window-system
  473. (setq frame-background-mode 'dark))
  474. (and (or (eq system-type 'Darwin)
  475. (eq system-type 'darwin))
  476. (fboundp 'mac-set-input-method-parameter)
  477. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  478. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  479. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  480. (boundp 'input-method-inactivate-hook))
  481. (add-hook 'input-method-activate-hook
  482. (lambda () (set-cursor-color "red")))
  483. (add-hook 'input-method-inactivate-hook
  484. (lambda () (set-cursor-color "black"))))
  485. (when (safe-require-or-eval 'paren)
  486. (show-paren-mode 1)
  487. (setq show-paren-delay 0.5
  488. show-paren-style 'parenthesis) ; mixed is hard to read
  489. ;; (set-face-background 'show-paren-match
  490. ;; "black")
  491. ;; ;; (face-foreground 'default))
  492. ;; (set-face-foreground 'show-paren-match
  493. ;; "white")
  494. ;; (set-face-inverse-video-p 'show-paren-match
  495. ;; t)
  496. )
  497. (transient-mark-mode 1)
  498. (global-font-lock-mode 1)
  499. (setq font-lock-global-modes
  500. '(not
  501. help-mode
  502. eshell-mode
  503. ;;term-mode
  504. Man-mode
  505. magit-diff-mode
  506. magit-revision-mode))
  507. ;; (standard-display-ascii ?\n "$\n")
  508. ;; (defvar my-eol-face
  509. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  510. ;; )
  511. ;; (defvar my-tab-face
  512. ;; '(("\t" . '(0 highlight t nil))))
  513. (defvar my-jspace-face
  514. '(("\u3000" . '(0 highlight t nil))))
  515. (add-hook 'font-lock-mode-hook
  516. (lambda ()
  517. ;; (font-lock-add-keywords nil my-eol-face)
  518. (font-lock-add-keywords nil my-jspace-face)
  519. ))
  520. (when (safe-require-or-eval 'whitespace)
  521. (add-to-list 'whitespace-display-mappings
  522. ;; We need t since last one takes precedence
  523. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  524. ;; (add-to-list 'whitespace-display-mappings
  525. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  526. (setq whitespace-style '(face
  527. trailing ; trailing blanks
  528. ;; tabs
  529. ;; spaces
  530. ;; lines
  531. lines-tail ; lines over 80
  532. newline ; newlines
  533. empty ; empty lines at beg or end of buffer
  534. ;; big-indent
  535. ;; space-mark
  536. tab-mark
  537. newline-mark ; use display table for newline
  538. ))
  539. ;; (setq whitespace-newline 'font-lock-comment-face)
  540. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  541. (defun my-whitesspace-mode-reload ()
  542. "Reload whitespace-mode config."
  543. (interactive)
  544. (when whitespace-mode
  545. (whitespace-mode 0)
  546. (whitespace-mode 1)))
  547. (set-variable 'whitespace-line-column nil)
  548. (global-whitespace-mode t)
  549. (add-hook 'dired-mode-hook
  550. (lambda ()
  551. (set (make-local-variable 'whitespace-style) nil)))
  552. (if (= (display-color-cells)
  553. 256)
  554. (set-face-foreground 'whitespace-newline "color-109")
  555. ;; (progn
  556. ;; (set-face-bold-p 'whitespace-newline
  557. ;; t))
  558. ))
  559. (and nil
  560. '(safe-require-or-eval 'fill-column-indicator)
  561. (setq fill-column-indicator))
  562. ;; highlight current line
  563. ;; http://wiki.riywo.com/index.php?Meadow
  564. (face-spec-set 'hl-line
  565. '((((min-colors 256)
  566. (background dark))
  567. (:background "color-234"))
  568. (((min-colors 256)
  569. (background light))
  570. (:background "color-234"))
  571. (t
  572. (:underline "black"))))
  573. (set-variable 'hl-line-global-modes
  574. '(not
  575. term-mode))
  576. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  577. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  578. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  579. ;;(safe-require-or-eval 'set-modeline-color)
  580. ;; (let ((fg (face-foreground 'default))
  581. ;; (bg (face-background 'default)))
  582. ;; (set-face-background 'mode-line-inactive
  583. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  584. ;; (set-face-foreground 'mode-line-inactive
  585. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  586. ;; (set-face-underline 'mode-line-inactive
  587. ;; t)
  588. ;; (set-face-underline 'vertical-border
  589. ;; nil)
  590. ;; (when (safe-require-or-eval 'end-mark)
  591. ;; (global-end-mark-mode))
  592. ;; M-x highlight-* to highlight things
  593. (global-hi-lock-mode 1)
  594. (unless (fboundp 'highlight-region-text)
  595. (defun highlight-region-text (beg end)
  596. "Highlight text between BEG and END."
  597. (interactive "r")
  598. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  599. end)))
  600. (setq deactivate-mark t)))
  601. (when (safe-require-or-eval 'auto-highlight-symbol)
  602. (set-variable 'ahs-idle-interval 0.6)
  603. (global-auto-highlight-symbol-mode 1))
  604. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  605. ;; file handling
  606. (when (safe-require-or-eval 'recently)
  607. (recently-mode 1))
  608. (when (safe-require-or-eval 'editorconfig)
  609. ;; (set-variable 'editorconfig-get-properties-function
  610. ;; 'editorconfig-core-get-properties-hash)
  611. (editorconfig-mode 1)
  612. (set-variable 'editorconfig-mode-lighter " EC")
  613. (with-eval-after-load 'org-src
  614. ;; [*.org\[\*Org Src*\[ c \]*\]]
  615. (add-hook 'org-src-mode-hook
  616. 'editorconfig-mode-apply t)))
  617. (when (fboundp 'editorconfig-custom-majormode)
  618. (add-hook 'editorconfig-after-apply-functions
  619. 'editorconfig-custom-majormode))
  620. ;; Add readonly=true to set read-only-mode
  621. (add-hook 'editorconfig-after-apply-functions
  622. (lambda (props)
  623. (let ((r (gethash 'readonly props)))
  624. (when (and (string= r "true")
  625. (not buffer-read-only))
  626. (read-only-mode 1)))))
  627. (add-hook 'editorconfig-hack-properties-functions
  628. '(lambda (props)
  629. (when (derived-mode-p 'makefile-mode)
  630. (puthash 'indent_style "tab" props))))
  631. ;; (when (fboundp 'editorconfig-charset-extras)
  632. ;; (add-hook 'editorconfig-custom-hooks
  633. ;; 'editorconfig-charset-extras))
  634. (setq revert-without-query '(".+"))
  635. ;; save cursor position
  636. (when (safe-require-or-eval 'saveplace)
  637. (setq-default save-place t)
  638. (setq save-place-file (concat user-emacs-directory
  639. "places")))
  640. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  641. (setq make-backup-files t)
  642. (setq vc-make-backup-files t)
  643. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  644. (setq backup-directory-alist
  645. (cons (cons "." (expand-file-name (concat user-emacs-directory
  646. "backup")))
  647. backup-directory-alist))
  648. (setq version-control 't)
  649. (setq delete-old-versions t)
  650. (setq kept-new-versions 20)
  651. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  652. "auto-save/")))
  653. ;; (setq delete-auto-save-files t)
  654. (setq auto-save-visited-interval 8)
  655. (auto-save-visited-mode 1)
  656. (add-to-list 'completion-ignored-extensions ".bak")
  657. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  658. (setq delete-by-moving-to-trash t)
  659. ;; trash-directory "~/.emacs.d/trash")
  660. (add-hook 'after-save-hook
  661. 'executable-make-buffer-file-executable-if-script-p)
  662. (set-variable 'bookmark-default-file
  663. (expand-file-name (concat user-emacs-directory
  664. "bmk")))
  665. (set-variable 'bookmark-save-flag
  666. 1)
  667. (with-eval-after-load 'recentf
  668. (defvar recentf-exclude)
  669. (defvar bookmark-default-file)
  670. (add-to-list 'recentf-exclude
  671. (regexp-quote bookmark-default-file)))
  672. (when (safe-require-or-eval 'smart-revert)
  673. (smart-revert-on))
  674. ;; autosave
  675. ;; auto-save-visited-mode can be used instead?
  676. ;; (when (safe-require-or-eval 'autosave)
  677. ;; (autosave-set 8))
  678. ;; bookmarks
  679. (define-key ctl-x-map "m" 'list-bookmarks)
  680. ;; vc
  681. (set-variable 'vc-handled-backends '(RCS))
  682. (set-variable 'vc-rcs-register-switches "-l")
  683. (set-variable 'vc-rcs-checkin-switches "-l")
  684. (set-variable 'vc-command-messages t)
  685. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  686. ;; share clipboard with x
  687. ;; this page describes this in details, but only these sexps seem to be needed
  688. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  689. (and nil
  690. (not window-system)
  691. (not (eq window-system 'mac))
  692. (getenv "DISPLAY")
  693. (not (equal (getenv "DISPLAY") ""))
  694. (executable-find "xclip")
  695. ;; (< emacs-major-version 24)
  696. '(safe-require-or-eval 'xclip)
  697. nil
  698. (turn-on-xclip))
  699. (and (eq system-type 'darwin)
  700. (safe-require-or-eval 'pasteboard)
  701. (turn-on-pasteboard))
  702. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  703. ;; some modes and hooks
  704. ;; Include some extra modes
  705. (require 'generic-x)
  706. (when (autoload-eval-lazily 'wgrep)
  707. (set-variable 'wgrep-auto-save-buffer t)
  708. (with-eval-after-load 'grep
  709. (defvar grep-mode-map)
  710. (define-key grep-mode-map
  711. "e"
  712. 'wgrep-change-to-wgrep-mode)))
  713. (with-eval-after-load 'remember
  714. (defvar remember-mode-map (make-sparse-keymap))
  715. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  716. (with-eval-after-load 'magit-files
  717. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  718. ;; existing keybindings.
  719. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  720. ;; to disable thie mode here, but do anyway.
  721. ;; See also https://github.com/magit/magit/issues/3517
  722. (global-magit-file-mode -1))
  723. (with-eval-after-load 'magit-section
  724. (set-face-background 'magit-section-highlight
  725. nil))
  726. (with-eval-after-load 'magit-diff
  727. (set-face-background 'magit-diff-added-highlight
  728. nil)
  729. (set-face-background 'magit-diff-removed-highlight
  730. nil)
  731. (set-face-background 'magit-diff-context-highlight
  732. nil)
  733. )
  734. (when (boundp 'git-rebase-filename-regexp)
  735. (add-to-list 'auto-mode-alist
  736. `(,git-rebase-filename-regexp . text-mode)))
  737. (when (safe-require-or-eval 'aggressive-indent)
  738. (defvar aggressive-indent-excluded-modes)
  739. (setq aggressive-indent-excluded-modes
  740. `(diff-mode
  741. toml-mode
  742. conf-mode
  743. dockerfile-mode
  744. groovy-mode
  745. ,@aggressive-indent-excluded-modes))
  746. (global-aggressive-indent-mode 1))
  747. (when (autoload-eval-lazily 'ggtags)
  748. (add-hook 'c-mode-common-hook
  749. (lambda ()
  750. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  751. (ggtags-mode 1))))
  752. (add-hook 'python-mode-hook
  753. (lambda ()
  754. (ggtags-mode 1))))
  755. (when (autoload-eval-lazily 'imenu-list)
  756. ;; (set-variable 'imenu-list-auto-resize t)
  757. (set-variable 'imenu-list-focus-after-activation t)
  758. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  759. (add-hook 'emacs-lisp-mode-hook
  760. (lambda ()
  761. (setq imenu-generic-expression
  762. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  763. ,@imenu-generic-expression))))
  764. (with-eval-after-load 'compile
  765. (defvar compilation-filter-start)
  766. (defvar compilation-error-regexp-alist)
  767. (require 'ansi-color)
  768. (add-hook 'compilation-filter-hook
  769. (lambda ()
  770. (let ((inhibit-read-only t))
  771. (ansi-color-apply-on-region compilation-filter-start
  772. (point)))))
  773. (add-to-list 'compilation-error-regexp-alist
  774. ;; ansible-lint
  775. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  776. ;; Workaround to avoid ensime error
  777. (defvar ensime-mode-key-prefix nil)
  778. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  779. (when (safe-require-or-eval 'company)
  780. (global-company-mode)
  781. (set-variable 'company-idle-delay 0.5)
  782. (set-variable 'company-minimum-prefix-length 2)
  783. (set-variable 'company-selection-wrap-around t))
  784. ;; https://github.com/lunaryorn/flycheck
  785. (when (safe-require-or-eval 'flycheck)
  786. (call-after-init 'global-flycheck-mode))
  787. (when (autoload-eval-lazily 'ilookup)
  788. (define-key ctl-x-map "d" 'ilookup-open-word))
  789. (set-variable 'ac-ignore-case nil)
  790. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  791. (define-key ctl-x-map "t" 'term-run-shell-command))
  792. (add-to-list 'safe-local-variable-values
  793. '(encoding utf-8))
  794. (setq enable-local-variables :safe)
  795. ;; Detect file type from shebang and set major-mode.
  796. (add-to-list 'interpreter-mode-alist
  797. '("python3" . python-mode))
  798. (add-to-list 'interpreter-mode-alist
  799. '("python2" . python-mode))
  800. (with-eval-after-load 'python
  801. (defvar python-mode-map (make-sparse-keymap))
  802. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  803. (when (autoload-eval-lazily 'pipenv)
  804. (add-hook 'python-mode-hook
  805. (lambda ()
  806. (pipenv-mode 1)
  807. (pipenv-projectile-after-switch-default)))
  808. )
  809. (set-variable 'flycheck-python-pycompile-executable "python3")
  810. (set-variable 'python-indent-guess-indent-offset nil)
  811. ;; http://fukuyama.co/foreign-regexp
  812. '(and (safe-require-or-eval 'foreign-regexp)
  813. (progn
  814. (setq foreign-regexp/regexp-type 'perl)
  815. '(setq reb-re-syntax 'foreign-regexp)
  816. ))
  817. (autoload-eval-lazily 'sql '(sql-mode)
  818. (require 'sql-indent nil t))
  819. (when (autoload-eval-lazily 'git-command)
  820. (define-key ctl-x-map "g" 'git-command))
  821. (when (safe-require-or-eval 'git-commit)
  822. (global-git-commit-mode 1))
  823. (with-eval-after-load 'git-commit
  824. (add-hook 'git-commit-setup-hook
  825. 'turn-off-auto-fill t))
  826. (autoload-eval-lazily 'sl)
  827. (with-eval-after-load 'rst
  828. (defvar rst-mode-map)
  829. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  830. (with-eval-after-load 'jdee
  831. (add-hook 'jdee-mode-hook
  832. (lambda ()
  833. (make-local-variable 'global-mode-string)
  834. (add-to-list 'global-mode-string
  835. mode-line-position))))
  836. ;; Cannot enable error thrown. Why???
  837. ;; https://github.com/m0smith/malabar-mode#Installation
  838. ;; (when (autoload-eval-lazily 'malabar-mode)
  839. ;; (add-to-list 'load-path
  840. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  841. ;; (safe-require-or-eval 'cedet-devel-load)
  842. ;; (call-after-init 'activate-malabar-mode))
  843. (with-eval-after-load 'make-mode
  844. (defvar makefile-mode-map (make-sparse-keymap))
  845. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  846. ;; this functions is set in write-file-functions, i cannot find any
  847. ;; good way to remove this.
  848. (fset 'makefile-warn-suspicious-lines 'ignore))
  849. (with-eval-after-load 'verilog-mode
  850. (defvar verilog-mode-map (make-sparse-keymap))
  851. (define-key verilog-mode-map ";" 'self-insert-command))
  852. (setq diff-switches "-u")
  853. (with-eval-after-load 'diff-mode
  854. ;; (when (and (eq major-mode
  855. ;; 'diff-mode)
  856. ;; (not buffer-file-name))
  857. ;; ;; do not pass when major-mode is derived mode of diff-mode
  858. ;; (view-mode 1))
  859. (set-face-attribute 'diff-header nil
  860. :foreground nil
  861. :background nil
  862. :weight 'bold)
  863. (set-face-attribute 'diff-file-header nil
  864. :foreground nil
  865. :background nil
  866. :weight 'bold)
  867. (set-face-foreground 'diff-index "blue")
  868. (set-face-attribute 'diff-hunk-header nil
  869. :foreground "cyan"
  870. :weight 'normal)
  871. (set-face-attribute 'diff-context nil
  872. ;; :foreground "white"
  873. :foreground nil
  874. :weight 'normal)
  875. (set-face-foreground 'diff-removed "red")
  876. (set-face-foreground 'diff-added "green")
  877. (set-face-background 'diff-removed nil)
  878. (set-face-background 'diff-added nil)
  879. (set-face-attribute 'diff-changed nil
  880. :foreground "magenta"
  881. :weight 'normal)
  882. (set-face-attribute 'diff-refine-changed nil
  883. :foreground nil
  884. :background nil
  885. :weight 'bold
  886. :inverse-video t)
  887. ;; Annoying !
  888. ;;(diff-auto-refine-mode)
  889. )
  890. ;; (ffap-bindings)
  891. (set-variable 'browse-url-browser-function
  892. 'eww-browse-url)
  893. (set-variable 'sh-here-document-word "__EOC__")
  894. (when (autoload-eval-lazily 'adoc-mode
  895. nil
  896. (defvar adoc-mode-map (make-sparse-keymap))
  897. (define-key adoc-mode-map (kbd "C-m") 'newline))
  898. (setq auto-mode-alist
  899. `(("\\.adoc\\'" . adoc-mode)
  900. ("\\.asciidoc\\'" . adoc-mode)
  901. ,@auto-mode-alist)))
  902. (with-eval-after-load 'markup-faces
  903. ;; Is this too match ?
  904. (set-face-foreground 'markup-meta-face
  905. "color-245")
  906. (set-face-foreground 'markup-meta-hide-face
  907. "color-245")
  908. )
  909. ;; TODO: check if this is required
  910. (when (autoload-eval-lazily 'groovy-mode nil
  911. (defvar groovy-mode-map (make-sparse-keymap))
  912. (define-key groovy-mode-map "(" 'self-insert-command)
  913. (define-key groovy-mode-map ")" 'self-insert-command)
  914. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  915. )
  916. (add-to-list 'auto-mode-alist
  917. '("build\\.gradle\\'" . groovy-mode)))
  918. (add-to-list 'auto-mode-alist
  919. '("\\.gawk\\'" . awk-mode))
  920. (with-eval-after-load 'yaml-mode
  921. (defvar yaml-mode-map (make-sparse-keymap))
  922. (define-key yaml-mode-map (kbd "C-m") 'newline))
  923. (with-eval-after-load 'html-mode
  924. (defvar html-mode-map (make-sparse-keymap))
  925. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  926. (with-eval-after-load 'text-mode
  927. (define-key text-mode-map (kbd "C-m") 'newline))
  928. (autoload-eval-lazily 'info nil
  929. (defvar Info-additional-directory-list)
  930. (dolist (dir (directory-files (concat user-emacs-directory
  931. "info")
  932. t
  933. "^[^.].*"))
  934. (when (file-directory-p dir)
  935. (add-to-list 'Info-additional-directory-list
  936. dir)))
  937. (let ((dir (expand-file-name "~/.brew/share/info")))
  938. (when (file-directory-p dir)
  939. (add-to-list 'Info-additional-directory-list
  940. dir))))
  941. (with-eval-after-load 'apropos
  942. (defvar apropos-mode-map (make-sparse-keymap))
  943. (define-key apropos-mode-map "n" 'next-line)
  944. (define-key apropos-mode-map "p" 'previous-line))
  945. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  946. ;; (define-key isearch-mode-map
  947. ;; (kbd "C-j") 'isearch-other-control-char)
  948. ;; (define-key isearch-mode-map
  949. ;; (kbd "C-k") 'isearch-other-control-char)
  950. ;; (define-key isearch-mode-map
  951. ;; (kbd "C-h") 'isearch-other-control-char)
  952. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  953. (define-key isearch-mode-map (kbd "M-r")
  954. 'isearch-query-replace-regexp)
  955. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  956. (setq lazy-highlight-cleanup nil)
  957. ;; face for isearch highlighing
  958. (set-face-attribute 'lazy-highlight
  959. nil
  960. :foreground `unspecified
  961. :background `unspecified
  962. :underline t
  963. ;; :weight `bold
  964. )
  965. (add-hook 'outline-mode-hook
  966. (lambda ()
  967. (when (string-match "\\.md\\'" buffer-file-name)
  968. (set (make-local-variable 'outline-regexp) "#+ "))))
  969. (add-hook 'outline-mode-hook
  970. 'outline-show-all)
  971. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  972. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  973. (when (autoload-eval-lazily 'markdown-mode
  974. '(markdown-mode gfm-mode)
  975. (defvar gfm-mode-map (make-sparse-keymap))
  976. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  977. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  978. (set-variable 'markdown-command (or (executable-find "markdown")
  979. (executable-find "markdown.pl")
  980. ""))
  981. (add-hook 'markdown-mode-hook
  982. (lambda ()
  983. (outline-minor-mode 1)
  984. (flyspell-mode)
  985. (set (make-local-variable 'comment-start) ";")))
  986. )
  987. ;; c-mode
  988. ;; http://www.emacswiki.org/emacs/IndentingC
  989. ;; http://en.wikipedia.org/wiki/Indent_style
  990. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  991. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  992. (with-eval-after-load 'cc-vars
  993. (defvar c-default-style nil)
  994. (add-to-list 'c-default-style
  995. '(c-mode . "k&r"))
  996. (add-to-list 'c-default-style
  997. '(c++-mode . "k&r")))
  998. (autoload-eval-lazily 'js2-mode nil
  999. ;; currently do not use js2-mode
  1000. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1001. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1002. ;; (defvar js2-mode-map (make-sparse-keymap))
  1003. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1004. ;; (interactive)
  1005. ;; (js2-enter-key)
  1006. ;; (indent-for-tab-command)))
  1007. ;; (add-hook (kill-local-variable 'before-save-hook)
  1008. ;; 'js2-before-save)
  1009. ;; (add-hook 'before-save-hook
  1010. ;; 'my-indent-buffer
  1011. ;; nil
  1012. ;; t)
  1013. )
  1014. (add-to-list 'interpreter-mode-alist
  1015. '("node" . js-mode))
  1016. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1017. (with-eval-after-load 'uniquify
  1018. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1019. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1020. (setq uniquify-min-dir-content 1))
  1021. (with-eval-after-load 'view
  1022. (defvar view-mode-map (make-sparse-keymap))
  1023. (define-key view-mode-map "j" 'scroll-up-line)
  1024. (define-key view-mode-map "k" 'scroll-down-line)
  1025. (define-key view-mode-map "v" 'toggle-read-only)
  1026. (define-key view-mode-map "q" 'bury-buffer)
  1027. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1028. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1029. ;; (define-key view-mode-map
  1030. ;; "n" 'nonincremental-repeat-search-forward)
  1031. ;; (define-key view-mode-map
  1032. ;; "N" 'nonincremental-repeat-search-backward)
  1033. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1034. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1035. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1036. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1037. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1038. (global-set-key "\M-r" 'view-mode)
  1039. ;; (setq view-read-only t)
  1040. (with-eval-after-load 'term
  1041. (defvar term-raw-map (make-sparse-keymap))
  1042. (define-key term-raw-map (kbd "C-x")
  1043. (lookup-key (current-global-map)
  1044. (kbd "C-x"))))
  1045. (add-hook 'term-mode-hook
  1046. (lambda ()
  1047. ;; Stop current line highlighting
  1048. (set (make-local-variable (defvar hl-line-range-function))
  1049. (lambda () '(0 . 0)))
  1050. (set (make-local-variable 'scroll-margin)
  1051. 0)))
  1052. (add-hook 'Man-mode-hook
  1053. (lambda ()
  1054. (view-mode 1)
  1055. (setq truncate-lines nil)))
  1056. (set-variable 'Man-notify-method (if window-system
  1057. 'newframe
  1058. 'aggressive))
  1059. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1060. "woman_cache.el")))
  1061. ;; not work because man.el will be loaded when man called
  1062. (defalias 'man 'woman)
  1063. (add-to-list 'auto-mode-alist
  1064. '("tox\\.ini\\'" . conf-unix-mode))
  1065. (when (autoload-eval-lazily 'toml-mode)
  1066. (add-to-list 'auto-mode-alist
  1067. '("/tox\\.ini\\'" . toml-mode))
  1068. (add-to-list 'auto-mode-alist
  1069. '("/Pipfile\\'" . toml-mode))
  1070. (add-to-list 'auto-mode-alist
  1071. '("/poetry\\.lock\\'" . toml-mode))
  1072. )
  1073. (when (autoload-eval-lazily 'json-mode)
  1074. (add-to-list 'auto-mode-alist
  1075. '("/Pipfile\\.lock\\'" . json-mode)))
  1076. (add-hook 'go-mode-hook
  1077. (lambda()
  1078. (defvar go-mode-map)
  1079. (add-hook 'before-save-hook' 'gofmt-before-save)
  1080. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1081. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1082. ;; buffers
  1083. (defvar bs-configurations)
  1084. (when (autoload-eval-lazily 'bs '(bs-show)
  1085. (add-to-list 'bs-configurations
  1086. '("specials" "^\\*" nil ".*" nil nil))
  1087. (add-to-list 'bs-configurations
  1088. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1089. (defvar bs-mode-map)
  1090. (defvar bs-current-configuration)
  1091. (define-key bs-mode-map (kbd "t")
  1092. ;; TODO: fix toggle feature
  1093. (lambda ()
  1094. (interactive)
  1095. (if (string= "specials"
  1096. bs-current-configuration)
  1097. (bs-set-configuration "files")
  1098. (bs-set-configuration "specials"))
  1099. (bs-refresh)
  1100. (bs-message-without-log "%s"
  1101. (bs--current-config-message))))
  1102. ;; (setq bs-configurations (list
  1103. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1104. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1105. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1106. )
  1107. (defalias 'list-buffers 'bs-show)
  1108. (set-variable 'bs-default-configuration "files-and-specials")
  1109. (set-variable 'bs-default-sort-name "by nothing")
  1110. (add-hook 'bs-mode-hook
  1111. (lambda ()
  1112. (set (make-local-variable 'scroll-margin) 0))))
  1113. ;;(iswitchb-mode 1)
  1114. (icomplete-mode)
  1115. (defun iswitchb-buffer-display-other-window ()
  1116. "Do iswitchb in other window."
  1117. (interactive)
  1118. (let ((iswitchb-default-method 'display))
  1119. (call-interactively 'iswitchb-buffer)))
  1120. ;; buffer killing
  1121. ;; (defun my-delete-window-killing-buffer () nil)
  1122. (defun my-query-kill-current-buffer ()
  1123. "Interactively kill current buffer."
  1124. (interactive)
  1125. (if (y-or-n-p (concat "kill current buffer? :"))
  1126. (kill-buffer (current-buffer))))
  1127. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1128. (substitute-key-definition 'kill-buffer
  1129. 'my-query-kill-current-buffer
  1130. global-map)
  1131. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1132. ;; recentf-mode
  1133. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1134. "recentf")))
  1135. (set-variable 'recentf-max-menu-items 20)
  1136. (set-variable 'recentf-max-saved-items 30)
  1137. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1138. (set-variable 'recentf-auto-cleanup 3)
  1139. ;; (safe-require-or-eval 'sync-recentf)
  1140. ;; (when (safe-require-or-eval 'recentf)
  1141. ;; (add-to-list 'recentf-exclude
  1142. ;; (regexp-quote recentf-save-file))
  1143. ;; (add-to-list 'recentf-exclude
  1144. ;; (regexp-quote (expand-file-name user-emacs-directory)))
  1145. ;; (add-to-list 'recentf-exclude
  1146. ;; "/sync-recentf-marker\\'")
  1147. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1148. ;; (remove-hook 'find-file-hook
  1149. ;; 'recentf-track-opened-file)
  1150. ;; (defun my-recentf-load-track-save-list ()
  1151. ;; "Load current recentf list from file, track current visiting file, then save
  1152. ;; the list."
  1153. ;; (recentf-load-list)
  1154. ;; (recentf-track-opened-file)
  1155. ;; (recentf-save-list))
  1156. ;; (add-hook 'find-file-hook
  1157. ;; 'my-recentf-load-track-save-list)
  1158. ;; (add-hook 'kill-emacs-hook
  1159. ;; 'recentf-load-list)
  1160. ;; ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1161. ;; ;; (add-hook 'find-file-hook
  1162. ;; ;; (lambda ()
  1163. ;; ;; (recentf-add-file default-directory)))
  1164. ;; (when (autoload-eval-lazily 'recentf-show)
  1165. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1166. ;; ;; (add-hook 'recentf-show-before-listing-hook
  1167. ;; ;; 'recentf-load-list)
  1168. ;; )
  1169. ;; (recentf-mode 1)
  1170. ;; (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1171. ;; (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1172. ;; (define-key recentf-dialog-mode-map "p" 'previous-line)
  1173. ;; (define-key recentf-dialog-mode-map "n" 'next-line)
  1174. ;; (add-hook 'recentf-dialog-mode-hook
  1175. ;; (lambda ()
  1176. ;; ;; (recentf-save-list)
  1177. ;; ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1178. ;; ;; 'my-recentf-cd-and-find-file)
  1179. ;; (cd "~/"))))
  1180. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1181. ;; dired
  1182. (defun my-file-head (filename &optional n)
  1183. "Return list of first N lines of file FILENAME."
  1184. ;; TODO: Fix for janapese text
  1185. ;; TODO: Fix for short text
  1186. (let ((num (or n 10))
  1187. (size 100)
  1188. (beg 0)
  1189. (end 0)
  1190. (result '())
  1191. (read -1))
  1192. (with-temp-buffer
  1193. (erase-buffer)
  1194. (while (or (<= (count-lines (point-min)
  1195. (point-max))
  1196. num)
  1197. (eq read 0))
  1198. (setq end (+ beg size))
  1199. (setq read (nth 1 (insert-file-contents-literally filename
  1200. nil
  1201. beg
  1202. end)))
  1203. (goto-char (point-max))
  1204. (setq beg (+ beg size)))
  1205. (goto-char (point-min))
  1206. (while (< (length result) num)
  1207. (let ((start (point)))
  1208. (forward-line 1)
  1209. (setq result
  1210. `(,@result ,(buffer-substring-no-properties start
  1211. (point))))))
  1212. result
  1213. ;; (buffer-substring-no-properties (point-min)
  1214. ;; (progn
  1215. ;; (forward-line num)
  1216. ;; (point)))
  1217. )))
  1218. ;; (apply 'concat (my-file-head "./shrc" 10)
  1219. (defun my-dired-echo-file-head (arg)
  1220. "Echo head of current file.
  1221. ARG is num to show, or defaults to 7."
  1222. (interactive "P")
  1223. (let ((f (dired-get-filename)))
  1224. (message "%s"
  1225. (apply 'concat
  1226. (my-file-head f
  1227. 7)))))
  1228. (defun my-dired-diff ()
  1229. "Show diff of marked file and file of current line."
  1230. (interactive)
  1231. (let ((files (dired-get-marked-files nil nil nil t)))
  1232. (if (eq (car files)
  1233. t)
  1234. (diff (cadr files) (dired-get-filename))
  1235. (message "One file must be marked!"))))
  1236. (defun dired-get-file-info ()
  1237. "Print information of current line file."
  1238. (interactive)
  1239. (let ((f (shell-quote-argument (dired-get-filename t))))
  1240. (if (file-directory-p f)
  1241. (progn
  1242. (message "Calculating disk usage...")
  1243. (shell-command (concat "du -hsD "
  1244. f)))
  1245. (shell-command (concat "file "
  1246. f)))))
  1247. (defun my-dired-scroll-up ()
  1248. "Scroll up."
  1249. (interactive)
  1250. (my-dired-previous-line (- (window-height) 1)))
  1251. (defun my-dired-scroll-down ()
  1252. "Scroll down."
  1253. (interactive)
  1254. (my-dired-next-line (- (window-height) 1)))
  1255. ;; (defun my-dired-forward-line (arg)
  1256. ;; ""
  1257. ;; (interactive "p"))
  1258. (defun my-dired-previous-line (arg)
  1259. "Move ARG lines up."
  1260. (interactive "p")
  1261. (if (> arg 0)
  1262. (progn
  1263. (if (eq (line-number-at-pos)
  1264. 1)
  1265. (goto-char (point-max))
  1266. (forward-line -1))
  1267. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1268. (dired-get-subdir))
  1269. (- arg 1)
  1270. arg)))
  1271. (dired-move-to-filename)))
  1272. (defun my-dired-next-line (arg)
  1273. "Move ARG lines down."
  1274. (interactive "p")
  1275. (if (> arg 0)
  1276. (progn
  1277. (if (eq (point)
  1278. (point-max))
  1279. (goto-char (point-min))
  1280. (forward-line 1))
  1281. (my-dired-next-line (if (or (dired-get-filename nil t)
  1282. (dired-get-subdir))
  1283. (- arg 1)
  1284. arg)))
  1285. (dired-move-to-filename)))
  1286. (defun my-tramp-remote-find-file (f)
  1287. "Open F."
  1288. (interactive (list (read-file-name "My Find File Tramp: "
  1289. "/scp:"
  1290. nil ;; "/scp:"
  1291. (confirm-nonexistent-file-or-buffer))))
  1292. (find-file f))
  1293. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1294. (if (eq window-system 'mac)
  1295. (setq dired-listing-switches "-lhF")
  1296. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1297. )
  1298. (setq dired-listing-switches "-lhF")
  1299. ;; when using dired-find-alternate-file
  1300. ;; reuse current dired buffer for the file to open
  1301. ;; (put 'dired-find-alternate-file 'disabled nil)
  1302. (set-variable 'dired-ls-F-marks-symlinks t)
  1303. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1304. (set-variable 'ls-lisp-dirs-first t)
  1305. (set-variable 'ls-lisp-use-localized-time-format t)
  1306. (set-variable 'ls-lisp-format-time-list
  1307. '("%Y-%m-%d %H:%M"
  1308. "%Y-%m-%d "))
  1309. (set-variable 'dired-dwim-target t)
  1310. (set-variable 'dired-isearch-filenames t)
  1311. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1312. (set-variable 'dired-hide-details-hide-information-lines nil)
  1313. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1314. (set-variable 'dired-recursive-deletes 'always)
  1315. ;; (add-hook 'dired-after-readin-hook
  1316. ;; 'my-replace-nasi-none)
  1317. (with-eval-after-load 'dired
  1318. (safe-require-or-eval 'ls-lisp)
  1319. (defvar dired-mode-map (make-sparse-keymap))
  1320. ;; dired-do-chgrp sometimes cause system hung
  1321. (define-key dired-mode-map "G" 'ignore)
  1322. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1323. (define-key dired-mode-map "i" 'dired-get-file-info)
  1324. (define-key dired-mode-map "f" 'find-file)
  1325. (define-key dired-mode-map "!" 'shell-command)
  1326. (define-key dired-mode-map "&" 'async-shell-command)
  1327. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1328. (define-key dired-mode-map "=" 'my-dired-diff)
  1329. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1330. (define-key dired-mode-map "b" 'gtkbm)
  1331. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1332. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1333. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1334. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1335. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1336. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1337. (substitute-key-definition 'dired-next-line
  1338. 'my-dired-next-line
  1339. dired-mode-map)
  1340. (substitute-key-definition 'dired-previous-line
  1341. 'my-dired-previous-line
  1342. dired-mode-map)
  1343. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1344. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1345. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1346. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1347. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1348. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1349. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1350. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1351. (add-hook 'dired-mode-hook
  1352. (lambda ()
  1353. (when (fboundp 'dired-hide-details-mode)
  1354. (dired-hide-details-mode t)
  1355. (local-set-key "l" 'dired-hide-details-mode))
  1356. (let ((file "._Icon\015"))
  1357. (when nil
  1358. '(file-readable-p file)
  1359. (delete-file file)))))
  1360. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1361. (with-eval-after-load 'dired
  1362. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1363. (when (autoload-eval-lazily 'dired-list-all-mode)
  1364. (setq dired-listing-switches "-lhF")
  1365. (with-eval-after-load 'dired
  1366. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1367. (when (autoload-eval-lazily 'dired-filter)
  1368. (add-hook 'dired-mode-hook
  1369. 'dired-filter-mode))
  1370. ;; Currently disabled in favor of dired-from-git-ls-files
  1371. ;; (define-key ctl-x-map "f" 'find-dired)
  1372. ;; It works!
  1373. ;; (pop-to-buffer (dired-noselect '("." "shrc" "emacs.el")))
  1374. (defun my-dired-git-ls-files (args)
  1375. "Dired from git ls-files."
  1376. (interactive "sgit ls-files args: ")
  1377. (pop-to-buffer-same-window
  1378. (dired-noselect `(,default-directory
  1379. ,@(split-string (shell-command-to-string (concat "git ls-files -z " args))
  1380. "\0" t))
  1381. ""))
  1382. )
  1383. (define-key ctl-x-map (kbd "f") 'my-dired-git-ls-files)
  1384. (with-eval-after-load 'dired
  1385. (defvar dired-mode-map (make-sparse-keymap))
  1386. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1387. ;; (define-minor-mode my-dired-glob-filter)
  1388. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1389. ;; misc funcs
  1390. (defalias 'qcalc 'quick-calc)
  1391. (defun memo (&optional dir)
  1392. "Open memo.txt in DIR."
  1393. (interactive)
  1394. (pop-to-buffer (find-file-noselect (concat (if dir
  1395. (file-name-as-directory dir)
  1396. "")
  1397. "memo.txt"))))
  1398. (set (defvar my-rgrep-alist nil
  1399. "Alist of rgrep command.
  1400. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1401. condition to choose COMMAND when evaluated.")
  1402. `(
  1403. ;; ripgrep
  1404. ("rg"
  1405. (executable-find "rg")
  1406. "rg --hidden --no-heading --smart-case ")
  1407. ;; git grep
  1408. ("gitgrep"
  1409. (eq 0
  1410. (shell-command "git rev-parse --git-dir"))
  1411. "git --no-pager -c color.grep=always grep -nH -e ")
  1412. ;; sift
  1413. ("sift"
  1414. (executable-find "sift")
  1415. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1416. ;; the silver searcher
  1417. ("ag"
  1418. (executable-find "ag")
  1419. "ag --nogroup --nopager --filename ")
  1420. ;; ack
  1421. ("ack"
  1422. (executable-find "ack")
  1423. "ack --nogroup --nopager --with-filename ")
  1424. ;; gnu global
  1425. ("global"
  1426. (and (require 'ggtags nil t)
  1427. (executable-find "global")
  1428. (ggtags-current-project-root))
  1429. "global --result grep ")
  1430. ;; grep
  1431. ("grep"
  1432. t
  1433. ,(concat "find . "
  1434. "-path '*/.git' -prune -o "
  1435. "-path '*/.svn' -prune -o "
  1436. "-type f -print0 | "
  1437. "xargs -0 grep -nH -e "))
  1438. )
  1439. )
  1440. (defvar my-rgrep-default nil
  1441. "Default command name for my-rgrep.")
  1442. (defun my-rgrep-grep-command (&optional name alist)
  1443. "Return recursive grep command for current directory or nil.
  1444. If NAME is given, use that without testing.
  1445. Commands are searched from ALIST."
  1446. (if alist
  1447. (if name
  1448. ;; if name is given search that from alist and return the command
  1449. (nth 2 (assoc name
  1450. alist))
  1451. ;; if name is not given try test in 1th elem
  1452. (let ((car (car alist))
  1453. (cdr (cdr alist)))
  1454. (if (eval (nth 1 car))
  1455. ;; if the condition is true return the command
  1456. (nth 2 car)
  1457. ;; try next one
  1458. (and cdr
  1459. (my-rgrep-grep-command name cdr)))))
  1460. ;; if alist is not given set default value
  1461. (my-rgrep-grep-command name my-rgrep-alist)))
  1462. (defun my-rgrep (command-args)
  1463. "My recursive grep. Run COMMAND-ARGS.
  1464. If prefix argument is given, use current symbol as default search target
  1465. and search from projectile root (if projectile is available)."
  1466. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1467. nil)))
  1468. (if cmd
  1469. (list (read-shell-command "grep command: "
  1470. (concat cmd
  1471. (if current-prefix-arg
  1472. (thing-at-point 'symbol t)
  1473. ""))
  1474. 'grep-find-history))
  1475. (error "My-Rgrep: Command for rgrep not found")
  1476. )))
  1477. (if (and current-prefix-arg
  1478. (safe-require-or-eval 'projectile)
  1479. (projectile-project-p))
  1480. (projectile-with-default-dir (projectile-project-root)
  1481. (compilation-start command-args
  1482. 'grep-mode))
  1483. (compilation-start command-args
  1484. 'grep-mode)))
  1485. (defun my-rgrep-thing-at-point-projectile-root ()
  1486. "My recursive grep to find thing at point from project root."
  1487. (interactive)
  1488. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1489. nil))
  1490. (command-args
  1491. (if cmd
  1492. (concat cmd
  1493. (or (thing-at-point 'symbol t)
  1494. (error "No symbol at point")))
  1495. (error "My-Rgrep: Command for rgrep not found"))))
  1496. (if (safe-require-or-eval 'projectile)
  1497. (projectile-with-default-dir (or (projectile-project-root)
  1498. default-directory)
  1499. (compilation-start command-args
  1500. 'grep-mode))
  1501. (compilation-start command-args
  1502. 'grep-mode))))
  1503. (defmacro define-my-rgrep (name)
  1504. "Define rgrep for NAME."
  1505. `(defun ,(intern (concat "my-rgrep-"
  1506. name)) ()
  1507. ,(format "My recursive grep by %s."
  1508. name)
  1509. (interactive)
  1510. (let ((my-rgrep-default ,name))
  1511. (if (called-interactively-p 'any)
  1512. (call-interactively 'my-rgrep)
  1513. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1514. )
  1515. (define-my-rgrep "ack")
  1516. (define-my-rgrep "ag")
  1517. (define-my-rgrep "rg")
  1518. (define-my-rgrep "sift")
  1519. (define-my-rgrep "gitgrep")
  1520. (define-my-rgrep "grep")
  1521. (define-my-rgrep "global")
  1522. (define-key ctl-x-map "s" 'my-rgrep)
  1523. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1524. (defun my-occur (regexp &optional region)
  1525. "My occur command to search REGEXP."
  1526. (interactive (list (read-string "List lines matching regexp: "
  1527. (thing-at-point 'symbol t))))
  1528. (occur regexp nil region))
  1529. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1530. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1531. (defalias 'make 'compile)
  1532. (define-key ctl-x-map "c" 'compile)
  1533. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1534. ;; editorconfig-auto-apply
  1535. (define-minor-mode editorconfig-auto-apply-mode
  1536. "When saving .editorconfig file update buffer configs."
  1537. :global t
  1538. :lighter ""
  1539. (if editorconfig-auto-apply-mode
  1540. (add-hook 'after-save-hook
  1541. 'editorconfig-auto-apply-mode--run)
  1542. (remove-hook 'after-save-hook
  1543. 'editorconfig-auto-apply-mode--run)))
  1544. (defun editorconfig-auto-apply-mode--run ()
  1545. "When saving .editorconfig file walk all buffers and update configs."
  1546. (when (eq major-mode
  1547. 'editorconfig-conf-mode)
  1548. (let ((dir (file-name-directory buffer-file-name)))
  1549. (cl-dolist (buf (buffer-list))
  1550. (when (and (buffer-file-name buf)
  1551. (file-in-directory-p (buffer-file-name buf)
  1552. dir))
  1553. (with-current-buffer buf
  1554. (editorconfig-mode-apply)))))))
  1555. (editorconfig-auto-apply-mode 1)
  1556. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  1557. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1558. ;; git-worktree
  1559. (defun git-worktree-get-current-trees ()
  1560. "Get current worktree list."
  1561. (with-temp-buffer
  1562. (git-worktree--call-process "worktree" "list" "--porcelain")
  1563. (goto-char (point-min))
  1564. (let ((trees nil))
  1565. (save-match-data
  1566. (while (not (eq (point) (point-max)))
  1567. (let ((worktree nil)
  1568. (head nil)
  1569. (branch nil))
  1570. (while (re-search-forward "^\\([^ ]+\\) \\(.*\\)$" (point-at-eol) t)
  1571. (pcase (match-string 1)
  1572. ("worktree" (setq worktree (match-string 2)))
  1573. ("HEAD" (setq head (match-string 2)))
  1574. ("branch" (setq branch (match-string 2))))
  1575. (forward-line 1)
  1576. (goto-char (point-at-bol)))
  1577. (setq trees `(,@trees
  1578. (:worktree ,worktree :head ,head :branch ,branch)))
  1579. (forward-line 1)
  1580. (goto-char (point-at-bol)))
  1581. ))
  1582. trees)))
  1583. (defun git-worktree--call-process (&rest args)
  1584. "Start git process synchronously with ARGS.
  1585. Raise error when git process ends with non-zero status.
  1586. Any output will be written to current buffer."
  1587. (let ((status (apply 'call-process
  1588. "git"
  1589. nil
  1590. t
  1591. nil
  1592. args)))
  1593. (cl-assert (eq status 0)
  1594. nil
  1595. (buffer-substring-no-properties (point-min) (point-max)))))
  1596. (defun git-worktree--get-repository-root (dir)
  1597. "Resolve repository root of DIR.
  1598. If DIR is not inside of any git repository, signal an error."
  1599. (cl-assert (file-directory-p dir))
  1600. (with-temp-buffer
  1601. (cd dir)
  1602. (git-worktree--call-process "rev-parse" "--show-toplevel")
  1603. (goto-char (point-min))
  1604. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  1605. ;;(git-worktree--get-repository-root default-directory)
  1606. (defun git-worktree-open-noselect (&optional directory)
  1607. "Open git worktree list buffer.
  1608. If optional arg DIRECTORY is given change current directory to there before
  1609. initializing."
  1610. (setq directory (expand-file-name (or directory
  1611. default-directory)))
  1612. (cl-assert (file-directory-p directory))
  1613. (let* ((root (git-worktree--get-repository-root directory))
  1614. (name (file-name-nondirectory root))
  1615. (bname (format "*GitWorktree<%s>*" name)))
  1616. (with-current-buffer (get-buffer-create bname)
  1617. (cd root)
  1618. (git-worktree--set-tabulated-list-mode-variables)
  1619. (git-worktree-mode)
  1620. (current-buffer))))
  1621. ;; ((:worktree "/Users/10sr/.dotfiles" :head "5e7457a8d49ef6a517cdf39d038ba5fdf98dc68e" :branch "refs/heads/master") (:worktree "/Users/10sr/.dotfiles/b1" :head "fa7d868076d807692e35f82ae23596c903fd1117" :branch "refs/heads/b1"))
  1622. (defun git-worktree--set-tabulated-list-mode-variables ()
  1623. "Set variables for `tabulated-list-mode'."
  1624. (let ((trees (git-worktree-get-current-trees)))
  1625. (setq tabulated-list-entries
  1626. (mapcar (lambda (e)
  1627. (list e
  1628. (vector
  1629. (concat (file-relative-name (plist-get e :worktree))
  1630. "/")
  1631. (or (plist-get e :branch) "")
  1632. (plist-get e :head)
  1633. )))
  1634. trees))
  1635. (let ((branch-max-size
  1636. (apply 'max
  1637. (cl-loop for e in tabulated-list-entries
  1638. collect (length (elt (cadr e) 1)))))
  1639. (worktree-max-size
  1640. (apply 'max
  1641. (length "Worktree")
  1642. (cl-loop for e in tabulated-list-entries
  1643. collect (length (elt (cadr e) 0))))))
  1644. (setq tabulated-list-format
  1645. `[
  1646. ("Worktree" ,worktree-max-size t)
  1647. ("Branch" ,branch-max-size t)
  1648. ("Head" -1 t)
  1649. ]))))
  1650. (defun git-worktree-open (&optional directory)
  1651. "Open git worktree list buffer.
  1652. If optional arg DIRECTORY is given change current directory to there before
  1653. initializing."
  1654. (interactive)
  1655. (let ((bf (git-worktree-open-noselect directory)))
  1656. (pop-to-buffer bf)))
  1657. (defalias 'git-worktree 'git-worktree-open)
  1658. (defun git-worktree-mode-go ()
  1659. "Go to worktree directory at point."
  1660. (interactive)
  1661. (let* ((id (tabulated-list-get-id))
  1662. (path (plist-get id :worktree)))
  1663. (cl-assert path nil "No worktree info at point")
  1664. (cl-assert (file-directory-p path) t "Directory not found")
  1665. (dired path)))
  1666. (defun git-worktree-mode-move ()
  1667. "Move worktree at point to a new location."
  1668. (interactive)
  1669. (let* ((id (tabulated-list-get-id))
  1670. (path (plist-get id :worktree)))
  1671. (cl-assert path nil "No worktree info at point")
  1672. (cl-assert (file-directory-p path) t "Directory not found")
  1673. (let ((new (read-file-name (format "New name for worktree \"%s\": "
  1674. path))))
  1675. (with-temp-buffer
  1676. (git-worktree--call-process "worktree"
  1677. "move"
  1678. path
  1679. (expand-file-name new)))
  1680. (revert-buffer))))
  1681. (defun git-worktree-mode-add ()
  1682. "Add new git worktree."
  1683. (interactive)
  1684. (let* ((path (read-file-name "Path of new worktree: "))
  1685. (commitish (read-string (format "Commitish to checkout to \"%s\" (Empty to omit): "
  1686. path)))
  1687. (args (append '("worktree" "add")
  1688. (if (string= "" commitish)
  1689. (list (expand-file-name path))
  1690. (list (expand-file-name path) commitish)))))
  1691. (with-temp-buffer
  1692. (apply 'git-worktree--call-process args))
  1693. (revert-buffer)))
  1694. (defun git-worktree-mode-remove ()
  1695. "Remove worktree at point."
  1696. (interactive)
  1697. (let* ((id (tabulated-list-get-id))
  1698. (path (plist-get id :worktree)))
  1699. (cl-assert path nil "No worktree info at point")
  1700. (cl-assert (file-directory-p path) t "Directory not found")
  1701. (when (yes-or-no-p (format "Remove workking directory \"%s\": "
  1702. path))
  1703. (with-temp-buffer
  1704. (git-worktree--call-process "worktree"
  1705. "remove"
  1706. path))
  1707. (revert-buffer))))
  1708. (defvar git-worktree-mode-map
  1709. (let ((map (make-sparse-keymap)))
  1710. (suppress-keymap map)
  1711. (define-key map "A" 'git-worktree-mode-add)
  1712. (define-key map (kbd "C-m") 'git-worktree-mode-go)
  1713. (define-key map "R" 'git-worktree-mode-move)
  1714. (define-key map "D" 'git-worktree-mode-remove)
  1715. ;; (define-key map (kbd "C-g") 'git-worktree-mode-close)
  1716. (define-key map "/" 'isearch-forward)
  1717. map))
  1718. (define-derived-mode git-worktree-mode tabulated-list-mode "Git-Worktrees"
  1719. "Major mode for browsing recently opened files and directories."
  1720. (setq tabulated-list-padding 2)
  1721. (add-hook 'tabulated-list-revert-hook
  1722. 'git-worktree--set-tabulated-list-mode-variables
  1723. nil
  1724. t)
  1725. (tabulated-list-init-header)
  1726. (tabulated-list-print nil nil))
  1727. ;;;;;;;;;;;;;;;;;;;;;;;
  1728. ;; with-venv
  1729. (defvar-local with-venv-venv-dir
  1730. nil
  1731. "Venv directory path.
  1732. This variable is intended to be explicitly set by user.
  1733. When nil, `with-venv' tries to find suitable venv dir.")
  1734. (defmacro with-venv-dir (dir &rest body)
  1735. "Set python environment to DIR and execute BODY."
  1736. `(let ((--with-venv-process-environment-orig (cl-copy-list process-environment))
  1737. (--with-venv-exec-path-orig (cl-copy-list exec-path)))
  1738. (unwind-protect
  1739. (progn
  1740. ;; Do the same thing that bin/activate does
  1741. (setq exec-path
  1742. (cons (concat ,dir "/bin")
  1743. exec-path))
  1744. (setenv "VIRTUAL_ENV" ,dir)
  1745. (setenv "PATH" (concat ,dir "/bin:" (or (getenv "PATH") "")))
  1746. (setenv "PYTHONHOME")
  1747. ,@body)
  1748. (setq process-environment
  1749. --with-venv-process-environment-orig)
  1750. (setq exec-path
  1751. --with-venv-exec-path-orig))))
  1752. (defmacro with-venv (&rest body)
  1753. "Execute BODY with venv enabled.
  1754. This function tries to find suitable venv dir, or raise error when none found."
  1755. `(with-venv-dir
  1756. ;; If set explicitly use it
  1757. ,(or with-venv-venv-dir
  1758. (with-venv-find-venv-dir))
  1759. ,@body))
  1760. (defun with-venv-find-venv-dir (&optional dir)
  1761. "Try to find venv dir for DIR or raise error when none found."
  1762. (with-temp-buffer
  1763. (when dir
  1764. (cd dir))
  1765. (or
  1766. ;; Check pipenv
  1767. (with-venv--find-venv-dir-pipenv)
  1768. ;; Check poetry
  1769. (with-venv--find-venv-dir-poetry)
  1770. ;; Search for .venv dir
  1771. (with-venv--find-venv-dir-by-name))))
  1772. (defun with-venv--find-venv-dir-pipenv ()
  1773. "Try to find venv dir via pipenv."
  1774. (with-temp-buffer
  1775. (let ((status (call-process "pipenv" nil t nil "--venv")))
  1776. (when (eq status 0)
  1777. (goto-char (point-min))
  1778. (buffer-substring-no-properties (point-at-bol)
  1779. (point-at-eol))))))
  1780. (defun with-venv--find-venv-dir-poetry ()
  1781. "Try to find venv dir via poetry."
  1782. (with-temp-buffer
  1783. ;; TODO: Use poetry env info --path
  1784. (let ((status (call-process "poetry" nil t nil "debug:info")))
  1785. (when (eq status 0)
  1786. (goto-char (point-min))
  1787. (save-match-data
  1788. (when (re-search-forward "^ \\* Path: *\\(.*\\)$")
  1789. (match-string 1)))))))
  1790. (defun with-venv--find-venv-dir-by-name ()
  1791. "Try to find venv dir by its name."
  1792. (let ((dir (locate-dominating-file default-directory
  1793. ".venv")))
  1794. (when dir
  1795. (expand-file-name ".venv"
  1796. dir))))
  1797. ;; (with-venv (:dir default-directory) (message "a"))
  1798. ;; (with-venv () (message "a"))
  1799. ;; Local Variables:
  1800. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1801. ;; flycheck-checker: emacs-lisp
  1802. ;; End:
  1803. ;;; emancs.el ends here