Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

2903 lignes
98 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. ;; ack
  80. color-moccur
  81. ggtags
  82. flycheck
  83. auto-highlight-symbol
  84. hl-todo
  85. ;; Currently not available
  86. ;; pp-c-l
  87. xclip
  88. foreign-regexp
  89. multi-term
  90. term-run
  91. editorconfig
  92. git-ps1-mode
  93. restart-emacs
  94. fill-column-indicator
  95. pkgbuild-mode
  96. minibuffer-line
  97. which-key
  98. ;; I think this works in place of my autosave lib
  99. super-save
  100. pipenv
  101. imenu-list
  102. page-break-lines
  103. ;; sync-recentf
  104. aggressive-indent
  105. fancy-narrow
  106. dired-filter
  107. wgrep
  108. magit
  109. git-gutter
  110. scala-mode
  111. ;;ensime
  112. editorconfig
  113. editorconfig-custom-majormode
  114. git-command
  115. prompt-text
  116. ;; 10sr repository
  117. ;; 10sr-extras
  118. terminal-title
  119. recentf-show
  120. dired-list-all-mode
  121. pack
  122. set-modeline-color
  123. read-only-only-mode
  124. smart-revert
  125. autosave
  126. ;;window-organizer
  127. ilookup
  128. pasteboard
  129. end-mark
  130. sl
  131. gosh-mode
  132. ))
  133. (when (safe-require-or-eval 'package)
  134. (setq package-archives
  135. `(,@package-archives
  136. ("melpa" . "https://melpa.org/packages/")
  137. ;; Somehow fails to download via https
  138. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  139. (package-initialize)
  140. (defun my-auto-install-package ()
  141. "Install packages semi-automatically."
  142. (interactive)
  143. (package-refresh-contents)
  144. (mapc (lambda (pkg)
  145. (or (package-installed-p pkg)
  146. (package-install pkg)))
  147. 10sr-package-list))
  148. )
  149. ;; (lazy-load-eval 'sudoku)
  150. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  151. ;; my-idle-hook
  152. (defvar my-idle-hook nil
  153. "Hook run when idle for several secs.")
  154. (defvar my-idle-hook-sec 5
  155. "Second to run `my-idle-hook'.")
  156. (run-with-idle-timer my-idle-hook-sec
  157. t
  158. (lambda ()
  159. (run-hooks 'my-idle-hook)))
  160. ;; (add-hook 'my-idle-hook
  161. ;; (lambda ()
  162. ;; (message "idle hook message")))
  163. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  164. ;; start and quit
  165. (setq inhibit-startup-message t)
  166. (setq confirm-kill-emacs 'y-or-n-p)
  167. (setq gc-cons-threshold (* 1024 1024 16))
  168. (setq garbage-collection-messages t)
  169. (when window-system
  170. (add-to-list 'default-frame-alist '(cursor-type . box))
  171. (add-to-list 'default-frame-alist '(background-color . "white"))
  172. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  173. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  174. ;; does not work?
  175. )
  176. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  177. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  178. (and (fboundp 'tool-bar-mode)
  179. (tool-bar-mode 0))
  180. (and (fboundp 'set-scroll-bar-mode)
  181. (set-scroll-bar-mode nil))
  182. (call-after-init
  183. (lambda ()
  184. (message "%s %s" invocation-name emacs-version)
  185. (message "Invocation directory: %s" default-directory)
  186. (message "%s was taken to initialize emacs." (emacs-init-time))
  187. (view-echo-area-messages)
  188. ;; (view-emacs-news)
  189. ))
  190. (cd ".") ; when using windows use / instead of \ in `default-directory'
  191. ;; locale
  192. (set-language-environment "Japanese")
  193. (set-default-coding-systems 'utf-8-unix)
  194. (prefer-coding-system 'utf-8-unix)
  195. (setq system-time-locale "C")
  196. ;; my prefix map
  197. (defvar my-prefix-map nil
  198. "My prefix map.")
  199. (define-prefix-command 'my-prefix-map)
  200. ;; (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  201. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  202. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  203. ;; (comint-show-maximum-output)
  204. ;; kill scratch
  205. (call-after-init (lambda ()
  206. (let ((buf (get-buffer "*scratch*")))
  207. (when buf
  208. (kill-buffer buf)))))
  209. ;; modifier keys
  210. ;; (setq mac-option-modifier 'control)
  211. ;; display
  212. (setq visible-bell t)
  213. (setq ring-bell-function 'ignore)
  214. (mouse-avoidance-mode 'banish)
  215. (setq echo-keystrokes 0.1)
  216. (defun reload-init-file ()
  217. "Reload Emacs init file."
  218. (interactive)
  219. (when (and user-init-file
  220. (file-readable-p user-init-file))
  221. (load-file user-init-file)))
  222. (safe-require-or-eval 'session)
  223. ;; server
  224. (set-variable 'server-name (concat "server"
  225. (number-to-string (emacs-pid))))
  226. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  227. ;; In Darwin environment, init fails with message like 'Service name too long'
  228. ;; when server-use-tcp is nil
  229. (when (or (eq system-type
  230. 'cygwin)
  231. (eq system-type
  232. 'darwin))
  233. (set-variable 'server-use-tcp t))
  234. ;; MSYS2 fix
  235. (when (eq system-type
  236. 'windows-nt)
  237. (setq shell-file-name
  238. (executable-find "bash"))
  239. '(setq function-key-map
  240. `(,@function-key-map ([pause] . [?\C-c])
  241. ))
  242. (define-key key-translation-map
  243. (kbd "<pause>")
  244. (kbd "C-c"))
  245. '(keyboard-translate [pause]
  246. (kbd "C-c")p)
  247. ;; TODO: move to other place later
  248. (when (not window-system)
  249. (setq interprogram-paste-function nil)
  250. (setq interprogram-cut-function nil)))
  251. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  252. ;; global keys
  253. (global-set-key (kbd "<up>") 'scroll-down-line)
  254. (global-set-key (kbd "<down>") 'scroll-up-line)
  255. (global-set-key (kbd "<left>") 'scroll-down)
  256. (global-set-key (kbd "<right>") 'scroll-up)
  257. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  258. (global-set-key (kbd "C-\\") help-map)
  259. (define-key ctl-x-map (kbd "DEL") help-map)
  260. (define-key ctl-x-map (kbd "C-h") help-map)
  261. (define-key help-map "a" 'apropos)
  262. ;; disable annoying keys
  263. (global-set-key [prior] 'ignore)
  264. (global-set-key (kbd "<next>") 'ignore)
  265. (global-set-key [menu] 'ignore)
  266. (global-set-key [down-mouse-1] 'ignore)
  267. (global-set-key [down-mouse-2] 'ignore)
  268. (global-set-key [down-mouse-3] 'ignore)
  269. (global-set-key [mouse-1] 'ignore)
  270. (global-set-key [mouse-2] 'ignore)
  271. (global-set-key [mouse-3] 'ignore)
  272. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  273. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  274. (when (safe-require-or-eval 'which-key)
  275. (which-key-mode))
  276. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  277. ;; editor
  278. (setq kill-whole-line t)
  279. (setq scroll-conservatively 35
  280. scroll-margin 2)
  281. (setq-default major-mode 'text-mode)
  282. (setq next-line-add-newlines nil)
  283. (setq kill-read-only-ok t)
  284. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  285. ;; (setq-default line-spacing 0.2)
  286. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  287. ;; (setq-default tab-width 4)
  288. (setq-default indent-tabs-mode nil)
  289. (setq-default indent-line-function 'indent-to-left-margin)
  290. ;; (setq-default indent-line-function nil)
  291. (setq-default truncate-lines nil)
  292. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  293. (delete-selection-mode 1)
  294. (cua-mode 0)
  295. (setq line-move-visual nil)
  296. (setq create-lockfiles nil)
  297. (add-hook 'before-save-hook
  298. 'time-stamp)
  299. ;; Add Time-stamp: <> to insert timestamp there
  300. (set-variable 'time-stamp-format
  301. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  302. ;; key bindings
  303. ;; moving around
  304. ;;(keyboard-translate ?\M-j ?\C-j)
  305. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  306. (define-key esc-map "p" 'backward-paragraph)
  307. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  308. (define-key esc-map "n" 'forward-paragraph)
  309. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  310. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  311. (global-set-key (kbd "C-<left>") 'scroll-down)
  312. (global-set-key (kbd "C-<right>") 'scroll-up)
  313. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  314. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  315. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  316. ;; C-h and DEL
  317. (global-set-key (kbd "C-h") (kbd "DEL"))
  318. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  319. (global-set-key (kbd "C-m") 'newline-and-indent)
  320. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  321. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  322. (define-key esc-map "u" 'undo)
  323. (define-key esc-map "i" (kbd "ESC TAB"))
  324. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  325. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  326. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  327. (require 'page-ext nil t)
  328. (when (safe-require-or-eval 'page-break-lines)
  329. (global-page-break-lines-mode 1))
  330. (when (safe-require-or-eval 'git-gutter)
  331. (custom-set-variables
  332. '(git-gutter:update-interval 2))
  333. (custom-set-variables
  334. '(git-gutter:unchanged-sign " "))
  335. (when (= (display-color-cells)
  336. 256)
  337. (let ((c "color-233"))
  338. (set-face-background 'git-gutter:modified c)
  339. (set-face-background 'git-gutter:added c)
  340. (set-face-background 'git-gutter:deleted c)
  341. (set-face-background 'git-gutter:unchanged c)))
  342. (global-git-gutter-mode 1)
  343. )
  344. ;; (when (safe-require-or-eval 'fancy-narrow)
  345. ;; (fancy-narrow-mode 1))
  346. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  347. ;; title and mode-line
  348. (when (safe-require-or-eval 'terminal-title)
  349. ;; if TERM is not screen use default value
  350. (if (getenv "TMUX")
  351. ;; if use tmux locally just basename of current dir
  352. (set-variable 'terminal-title-format
  353. '((file-name-nondirectory (directory-file-name
  354. default-directory))))
  355. (if (and (let ((tty-type (frame-parameter nil
  356. 'tty-type)))
  357. (and tty-type
  358. (equal (car (split-string tty-type
  359. "-"))
  360. "screen")))
  361. (not (getenv "SSH_CONNECTION")))
  362. (set-variable 'terminal-title-format
  363. '((file-name-nondirectory (directory-file-name
  364. default-directory))))
  365. ;; seems that TMUX is used locally and ssh to remote host
  366. (set-variable 'terminal-title-format
  367. `("em:"
  368. ,user-login-name
  369. "@"
  370. ,(car (split-string (system-name)
  371. "\\."))
  372. ":"
  373. default-directory))
  374. )
  375. )
  376. (terminal-title-mode))
  377. (setq eol-mnemonic-dos "\\r\\n")
  378. (setq eol-mnemonic-mac "\\r")
  379. (setq eol-mnemonic-unix "")
  380. (which-function-mode 1)
  381. (line-number-mode 0)
  382. (column-number-mode 0)
  383. (size-indication-mode 0)
  384. (setq mode-line-position
  385. '(:eval (format "L%%l/%d%s:C%%c"
  386. (count-lines (point-max)
  387. (point-min))
  388. (if (buffer-narrowed-p)
  389. "[N]"
  390. "")
  391. )))
  392. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  393. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  394. ;; minibuffer
  395. (setq insert-default-directory t)
  396. (setq completion-ignore-case t
  397. read-file-name-completion-ignore-case t
  398. read-buffer-completion-ignore-case t)
  399. (setq resize-mini-windows t)
  400. (temp-buffer-resize-mode 1)
  401. (savehist-mode 1)
  402. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  403. (set-variable 'help-at-pt-display-when-idle t)
  404. (fset 'yes-or-no-p 'y-or-n-p)
  405. ;; complete symbol when `eval'
  406. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  407. (define-key minibuffer-local-map (kbd "C-u")
  408. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  409. ;; I dont know these bindings are good
  410. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  411. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  412. (when (safe-require-or-eval 'minibuffer-line)
  413. (set-face-underline 'minibuffer-line nil)
  414. (set-variable 'minibuffer-line-refresh-interval
  415. 25)
  416. ;; Set idle timer
  417. (defvar my-minibuffer-line--idle-timer nil)
  418. (defvar minibuffer-line-mode)
  419. (add-hook 'minibuffer-line-mode-hook
  420. (lambda ()
  421. (when my-minibuffer-line--idle-timer
  422. (cancel-timer my-minibuffer-line--idle-timer)
  423. (setq my-minibuffer-line--idle-timer nil))
  424. (when minibuffer-line-mode
  425. (setq my-minibuffer-line--idle-timer
  426. (run-with-idle-timer 0.5
  427. t
  428. 'minibuffer-line--update)))))
  429. (set-variable 'minibuffer-line-format
  430. `(,(concat user-login-name
  431. "@"
  432. (car (split-string (system-name)
  433. "\\."))
  434. ":")
  435. (:eval (abbreviate-file-name (or buffer-file-name
  436. default-directory)))
  437. (:eval (and (fboundp 'git-ps1-mode-get-current)
  438. (git-ps1-mode-get-current " [GIT:%s]")))
  439. " "
  440. (:eval (format-time-string display-time-format))))
  441. (minibuffer-line-mode 1)
  442. )
  443. (when (safe-require-or-eval 'prompt-text)
  444. (set-variable 'prompt-text-format
  445. `(,(concat ""
  446. user-login-name
  447. "@"
  448. (car (split-string (system-name)
  449. "\\."))
  450. ":")
  451. (:eval (abbreviate-file-name (or buffer-file-name
  452. default-directory)))
  453. (:eval (and (fboundp 'git-ps1-mode-get-current)
  454. (git-ps1-mode-get-current " [GIT:%s]")))
  455. " "
  456. (:eval (format-time-string display-time-format))
  457. "\n"
  458. (:eval (symbol-name this-command))
  459. ": "))
  460. (prompt-text-mode 1))
  461. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  462. ;; letters, font-lock mode and fonts
  463. (setq text-quoting-style 'grave)
  464. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  465. ;; (set-window-margins (selected-window) 1 1)
  466. (unless window-system
  467. (setq frame-background-mode 'dark))
  468. (and (or (eq system-type 'Darwin)
  469. (eq system-type 'darwin))
  470. (fboundp 'mac-set-input-method-parameter)
  471. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  472. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  473. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  474. (boundp 'input-method-inactivate-hook))
  475. (add-hook 'input-method-activate-hook
  476. (lambda () (set-cursor-color "red")))
  477. (add-hook 'input-method-inactivate-hook
  478. (lambda () (set-cursor-color "black"))))
  479. (when (safe-require-or-eval 'paren)
  480. (show-paren-mode 1)
  481. (setq show-paren-delay 0.5
  482. show-paren-style 'parenthesis) ; mixed is hard to read
  483. ;; (set-face-background 'show-paren-match
  484. ;; "black")
  485. ;; ;; (face-foreground 'default))
  486. ;; (set-face-foreground 'show-paren-match
  487. ;; "white")
  488. ;; (set-face-inverse-video-p 'show-paren-match
  489. ;; t)
  490. )
  491. (transient-mark-mode 1)
  492. (global-font-lock-mode 1)
  493. (setq font-lock-global-modes
  494. '(not
  495. help-mode
  496. eshell-mode
  497. ;;term-mode
  498. Man-mode
  499. magit-diff-mode
  500. magit-revision-mode))
  501. ;; (standard-display-ascii ?\n "$\n")
  502. ;; (defvar my-eol-face
  503. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  504. ;; )
  505. ;; (defvar my-tab-face
  506. ;; '(("\t" . '(0 highlight t nil))))
  507. (defvar my-jspace-face
  508. '(("\u3000" . '(0 highlight t nil))))
  509. (add-hook 'font-lock-mode-hook
  510. (lambda ()
  511. ;; (font-lock-add-keywords nil my-eol-face)
  512. (font-lock-add-keywords nil my-jspace-face)
  513. ))
  514. (when (safe-require-or-eval 'whitespace)
  515. (add-to-list 'whitespace-display-mappings
  516. ;; We need t since last one takes precedence
  517. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  518. ;; (add-to-list 'whitespace-display-mappings
  519. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  520. (setq whitespace-style '(face
  521. trailing ; trailing blanks
  522. ;; tabs
  523. ;; spaces
  524. ;; lines
  525. lines-tail ; lines over 80
  526. newline ; newlines
  527. empty ; empty lines at beg or end of buffer
  528. ;; big-indent
  529. ;; space-mark
  530. tab-mark
  531. newline-mark ; use display table for newline
  532. ))
  533. ;; (setq whitespace-newline 'font-lock-comment-face)
  534. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  535. (defun my-whitesspace-mode-reload ()
  536. "Reload whitespace-mode config."
  537. (interactive)
  538. (when whitespace-mode
  539. (whitespace-mode 0)
  540. (whitespace-mode 1)))
  541. (set-variable 'whitespace-line-column nil)
  542. (global-whitespace-mode t)
  543. (add-hook 'dired-mode-hook
  544. (lambda ()
  545. (set (make-local-variable 'whitespace-style) nil)))
  546. (if (= (display-color-cells)
  547. 256)
  548. (set-face-foreground 'whitespace-newline "color-109")
  549. ;; (progn
  550. ;; (set-face-bold-p 'whitespace-newline
  551. ;; t))
  552. ))
  553. (and nil
  554. '(safe-require-or-eval 'fill-column-indicator)
  555. (setq fill-column-indicator))
  556. ;; highlight current line
  557. ;; http://wiki.riywo.com/index.php?Meadow
  558. (face-spec-set 'hl-line
  559. '((((min-colors 256)
  560. (background dark))
  561. (:background "color-234"))
  562. (((min-colors 256)
  563. (background light))
  564. (:background "color-234"))
  565. (t
  566. (:underline "black"))))
  567. (set-variable 'hl-line-global-modes
  568. '(not
  569. term-mode))
  570. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  571. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  572. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  573. ;;(safe-require-or-eval 'set-modeline-color)
  574. ;; (let ((fg (face-foreground 'default))
  575. ;; (bg (face-background 'default)))
  576. ;; (set-face-background 'mode-line-inactive
  577. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  578. ;; (set-face-foreground 'mode-line-inactive
  579. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  580. ;; (set-face-underline 'mode-line-inactive
  581. ;; t)
  582. ;; (set-face-underline 'vertical-border
  583. ;; nil)
  584. (when (safe-require-or-eval 'end-mark)
  585. (global-end-mark-mode))
  586. ;; M-x highlight-* to highlight things
  587. (global-hi-lock-mode 1)
  588. (unless (fboundp 'highlight-region-text)
  589. (defun highlight-region-text (beg end)
  590. "Highlight text between BEG and END."
  591. (interactive "r")
  592. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  593. end)))
  594. (setq deactivate-mark t)))
  595. (when (safe-require-or-eval 'auto-highlight-symbol)
  596. (set-variable 'ahs-idle-interval 0.6)
  597. (global-auto-highlight-symbol-mode 1))
  598. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  599. ;; file handling
  600. (when (safe-require-or-eval 'editorconfig)
  601. ;; (set-variable 'editorconfig-get-properties-function
  602. ;; 'editorconfig-core-get-properties-hash)
  603. (editorconfig-mode 1)
  604. (set-variable 'editorconfig-mode-lighter " EC")
  605. (with-eval-after-load 'org-src
  606. ;; [*.org\[\*Org Src*\[ c \]*\]]
  607. (add-hook 'org-src-mode-hook
  608. 'editorconfig-mode-apply t)))
  609. (when (fboundp 'editorconfig-custom-majormode)
  610. (add-hook 'editorconfig-custom-hooks
  611. 'editorconfig-custom-majormode))
  612. ;; Add readonly=true to set read-only-mode
  613. (add-hook 'editorconfig-custom-hooks
  614. (lambda (props)
  615. (let ((r (gethash 'readonly props)))
  616. (when (and (string= r "true")
  617. (not buffer-read-only))
  618. (read-only-mode 1)))))
  619. ;; (when (fboundp 'editorconfig-charset-extras)
  620. ;; (add-hook 'editorconfig-custom-hooks
  621. ;; 'editorconfig-charset-extras))
  622. (setq revert-without-query '(".+"))
  623. ;; save cursor position
  624. (when (safe-require-or-eval 'saveplace)
  625. (setq-default save-place t)
  626. (setq save-place-file (concat user-emacs-directory
  627. "places")))
  628. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  629. (setq make-backup-files t)
  630. (setq vc-make-backup-files t)
  631. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  632. (setq backup-directory-alist
  633. (cons (cons "." (expand-file-name (concat user-emacs-directory
  634. "backup")))
  635. backup-directory-alist))
  636. (setq version-control 't)
  637. (setq delete-old-versions t)
  638. (setq kept-new-versions 20)
  639. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  640. "auto-save/")))
  641. ;; (setq delete-auto-save-files t)
  642. (setq auto-save-visited-interval 8)
  643. (auto-save-visited-mode 1)
  644. (add-to-list 'completion-ignored-extensions ".bak")
  645. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  646. (setq delete-by-moving-to-trash t)
  647. ;; trash-directory "~/.emacs.d/trash")
  648. (add-hook 'after-save-hook
  649. 'executable-make-buffer-file-executable-if-script-p)
  650. (set-variable 'bookmark-default-file
  651. (expand-file-name (concat user-emacs-directory
  652. "bmk")))
  653. (set-variable 'bookmark-save-flag
  654. 1)
  655. (with-eval-after-load 'recentf
  656. (defvar recentf-exclude)
  657. (defvar bookmark-default-file)
  658. (add-to-list 'recentf-exclude
  659. (regexp-quote bookmark-default-file)))
  660. (when (safe-require-or-eval 'smart-revert)
  661. (smart-revert-on))
  662. ;; autosave
  663. ;; auto-save-visited-mode can be used instead?
  664. ;; (when (safe-require-or-eval 'autosave)
  665. ;; (autosave-set 8))
  666. ;; bookmarks
  667. (define-key ctl-x-map "m" 'list-bookmarks)
  668. ;; vc
  669. (set-variable 'vc-handled-backends '(RCS))
  670. (set-variable 'vc-rcs-register-switches "-l")
  671. (set-variable 'vc-rcs-checkin-switches "-l")
  672. (set-variable 'vc-command-messages t)
  673. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  674. ;; share clipboard with x
  675. ;; this page describes this in details, but only these sexps seem to be needed
  676. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  677. (and nil
  678. (not window-system)
  679. (not (eq window-system 'mac))
  680. (getenv "DISPLAY")
  681. (not (equal (getenv "DISPLAY") ""))
  682. (executable-find "xclip")
  683. ;; (< emacs-major-version 24)
  684. '(safe-require-or-eval 'xclip)
  685. nil
  686. (turn-on-xclip))
  687. (and (eq system-type 'darwin)
  688. (safe-require-or-eval 'pasteboard)
  689. (turn-on-pasteboard))
  690. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  691. ;; some modes and hooks
  692. ;; Include some extra modes
  693. (require 'generic-x)
  694. (when (safe-require-or-eval 'wgrep)
  695. (defvar grep-mode-map)
  696. (define-key grep-mode-map
  697. "e"
  698. 'wgrep-change-to-wgrep-mode))
  699. (with-eval-after-load 'remember
  700. (defvar remember-mode-map (make-sparse-keymap))
  701. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  702. (with-eval-after-load 'magit-files
  703. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  704. ;; existing keybindings.
  705. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  706. ;; to disable thie mode here, but do anyway.
  707. ;; See also https://github.com/magit/magit/issues/3517
  708. (global-magit-file-mode -1))
  709. (when (boundp 'git-rebase-filename-regexp)
  710. (add-to-list 'auto-mode-alist
  711. `(,git-rebase-filename-regexp . text-mode)))
  712. (when (safe-require-or-eval 'aggressive-indent)
  713. (defvar aggressive-indent-excluded-modes)
  714. (setq aggressive-indent-excluded-modes
  715. `(diff-mode
  716. toml-mode
  717. ,@aggressive-indent-excluded-modes))
  718. (global-aggressive-indent-mode 1))
  719. (when (autoload-eval-lazily 'ggtags)
  720. (add-hook 'c-mode-common-hook
  721. (lambda ()
  722. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  723. (ggtags-mode 1))))
  724. (add-hook 'python-mode-hook
  725. (lambda ()
  726. (ggtags-mode 1))))
  727. (when (autoload-eval-lazily 'imenu-list)
  728. ;; (set-variable 'imenu-list-auto-resize t)
  729. (set-variable 'imenu-list-focus-after-activation t)
  730. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  731. (add-hook 'emacs-lisp-mode-hook
  732. (lambda ()
  733. (setq imenu-generic-expression
  734. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  735. ,@imenu-generic-expression))))
  736. (with-eval-after-load 'compile
  737. (defvar compilation-filter-start)
  738. (defvar compilation-error-regexp-alist)
  739. (require 'ansi-color)
  740. (add-hook 'compilation-filter-hook
  741. (lambda ()
  742. (let ((inhibit-read-only t))
  743. (ansi-color-apply-on-region compilation-filter-start
  744. (point)))))
  745. (add-to-list 'compilation-error-regexp-alist
  746. ;; ansible-lint
  747. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  748. ;; Workaround to avoid ensime error
  749. (defvar ensime-mode-key-prefix nil)
  750. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  751. (when (safe-require-or-eval 'company)
  752. (global-company-mode)
  753. (set-variable 'company-idle-delay 0.5)
  754. (set-variable 'company-minimum-prefix-length 2)
  755. (set-variable 'company-selection-wrap-around t))
  756. ;; https://github.com/lunaryorn/flycheck
  757. (when (safe-require-or-eval 'flycheck)
  758. (call-after-init 'global-flycheck-mode))
  759. (when (autoload-eval-lazily 'ilookup)
  760. (define-key ctl-x-map "d" 'ilookup-open-word))
  761. (set-variable 'ac-ignore-case nil)
  762. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  763. (define-key ctl-x-map "t" 'term-run-shell-command))
  764. (add-to-list 'safe-local-variable-values
  765. '(encoding utf-8))
  766. (setq enable-local-variables :safe)
  767. ;; Detect file type from shebang and set major-mode.
  768. (add-to-list 'interpreter-mode-alist
  769. '("python3" . python-mode))
  770. (add-to-list 'interpreter-mode-alist
  771. '("python2" . python-mode))
  772. (with-eval-after-load 'python
  773. (defvar python-mode-map (make-sparse-keymap))
  774. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  775. (when (autoload-eval-lazily 'pipenv)
  776. (add-hook 'python-mode-hook
  777. (lambda ()
  778. (pipenv-mode 1)
  779. (pipenv-projectile-after-switch-default)))
  780. )
  781. (set-variable 'flycheck-python-pycompile-executable "python3")
  782. ;; http://fukuyama.co/foreign-regexp
  783. '(and (safe-require-or-eval 'foreign-regexp)
  784. (progn
  785. (setq foreign-regexp/regexp-type 'perl)
  786. '(setq reb-re-syntax 'foreign-regexp)
  787. ))
  788. (autoload-eval-lazily 'sql '(sql-mode)
  789. (require 'sql-indent nil t))
  790. (when (autoload-eval-lazily 'git-command)
  791. (define-key ctl-x-map "g" 'git-command))
  792. (when (safe-require-or-eval 'git-commit)
  793. (global-git-commit-mode 1))
  794. (with-eval-after-load 'git-commit
  795. (add-hook 'git-commit-setup-hook
  796. 'turn-off-auto-fill t))
  797. (autoload-eval-lazily 'sl)
  798. (with-eval-after-load 'rst
  799. (defvar rst-mode-map)
  800. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  801. (with-eval-after-load 'jdee
  802. (add-hook 'jdee-mode-hook
  803. (lambda ()
  804. (make-local-variable 'global-mode-string)
  805. (add-to-list 'global-mode-string
  806. mode-line-position))))
  807. ;; Cannot enable error thrown. Why???
  808. ;; https://github.com/m0smith/malabar-mode#Installation
  809. ;; (when (autoload-eval-lazily 'malabar-mode)
  810. ;; (add-to-list 'load-path
  811. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  812. ;; (safe-require-or-eval 'cedet-devel-load)
  813. ;; (call-after-init 'activate-malabar-mode))
  814. (with-eval-after-load 'make-mode
  815. (defvar makefile-mode-map (make-sparse-keymap))
  816. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  817. ;; this functions is set in write-file-functions, i cannot find any
  818. ;; good way to remove this.
  819. (fset 'makefile-warn-suspicious-lines 'ignore))
  820. (with-eval-after-load 'verilog-mode
  821. (defvar verilog-mode-map (make-sparse-keymap))
  822. (define-key verilog-mode-map ";" 'self-insert-command))
  823. (setq diff-switches "-u")
  824. (with-eval-after-load 'diff-mode
  825. ;; (when (and (eq major-mode
  826. ;; 'diff-mode)
  827. ;; (not buffer-file-name))
  828. ;; ;; do not pass when major-mode is derived mode of diff-mode
  829. ;; (view-mode 1))
  830. (set-face-attribute 'diff-header nil
  831. :foreground nil
  832. :background nil
  833. :weight 'bold)
  834. (set-face-attribute 'diff-file-header nil
  835. :foreground nil
  836. :background nil
  837. :weight 'bold)
  838. (set-face-foreground 'diff-index "blue")
  839. (set-face-attribute 'diff-hunk-header nil
  840. :foreground "cyan"
  841. :weight 'normal)
  842. (set-face-attribute 'diff-context nil
  843. ;; :foreground "white"
  844. :foreground nil
  845. :weight 'normal)
  846. (set-face-foreground 'diff-removed "red")
  847. (set-face-foreground 'diff-added "green")
  848. (set-face-background 'diff-removed nil)
  849. (set-face-background 'diff-added nil)
  850. (set-face-attribute 'diff-changed nil
  851. :foreground "magenta"
  852. :weight 'normal)
  853. (set-face-attribute 'diff-refine-changed nil
  854. :foreground nil
  855. :background nil
  856. :weight 'bold
  857. :inverse-video t)
  858. ;; Annoying !
  859. ;;(diff-auto-refine-mode)
  860. )
  861. ;; (ffap-bindings)
  862. (set-variable 'browse-url-browser-function
  863. 'eww-browse-url)
  864. (set-variable 'sh-here-document-word "__EOC__")
  865. (when (autoload-eval-lazily 'adoc-mode
  866. nil
  867. (defvar adoc-mode-map (make-sparse-keymap))
  868. (define-key adoc-mode-map (kbd "C-m") 'newline))
  869. (setq auto-mode-alist
  870. `(("\\.adoc\\'" . adoc-mode)
  871. ("\\.asciidoc\\'" . adoc-mode)
  872. ,@auto-mode-alist)))
  873. (with-eval-after-load 'markup-faces
  874. ;; Is this too match ?
  875. (set-face-foreground 'markup-meta-face
  876. "color-245")
  877. (set-face-foreground 'markup-meta-hide-face
  878. "color-245")
  879. )
  880. ;; TODO: check if this is required
  881. (when (autoload-eval-lazily 'groovy-mode nil
  882. (defvar groovy-mode-map (make-sparse-keymap))
  883. (define-key groovy-mode-map "(" 'self-insert-command)
  884. (define-key groovy-mode-map ")" 'self-insert-command)
  885. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  886. )
  887. (add-to-list 'auto-mode-alist
  888. '("build\\.gradle\\'" . groovy-mode)))
  889. (with-eval-after-load 'yaml-mode
  890. (defvar yaml-mode-map (make-sparse-keymap))
  891. (define-key yaml-mode-map (kbd "C-m") 'newline))
  892. (with-eval-after-load 'html-mode
  893. (defvar html-mode-map (make-sparse-keymap))
  894. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  895. (with-eval-after-load 'text-mode
  896. (define-key text-mode-map (kbd "C-m") 'newline))
  897. (autoload-eval-lazily 'info nil
  898. (defvar Info-additional-directory-list)
  899. (dolist (dir (directory-files (concat user-emacs-directory
  900. "info")
  901. t
  902. "^[^.].*"))
  903. (when (file-directory-p dir)
  904. (add-to-list 'Info-additional-directory-list
  905. dir)))
  906. (let ((dir (expand-file-name "~/.brew/share/info")))
  907. (when (file-directory-p dir)
  908. (add-to-list 'Info-additional-directory-list
  909. dir))))
  910. (with-eval-after-load 'apropos
  911. (defvar apropos-mode-map (make-sparse-keymap))
  912. (define-key apropos-mode-map "n" 'next-line)
  913. (define-key apropos-mode-map "p" 'previous-line))
  914. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  915. ;; (define-key isearch-mode-map
  916. ;; (kbd "C-j") 'isearch-other-control-char)
  917. ;; (define-key isearch-mode-map
  918. ;; (kbd "C-k") 'isearch-other-control-char)
  919. ;; (define-key isearch-mode-map
  920. ;; (kbd "C-h") 'isearch-other-control-char)
  921. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  922. (define-key isearch-mode-map (kbd "M-r")
  923. 'isearch-query-replace-regexp)
  924. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  925. (setq lazy-highlight-cleanup nil)
  926. ;; face for isearch highlighing
  927. (set-face-attribute 'lazy-highlight
  928. nil
  929. :foreground `unspecified
  930. :background `unspecified
  931. :underline t
  932. ;; :weight `bold
  933. )
  934. (add-hook 'outline-mode-hook
  935. (lambda ()
  936. (when (string-match "\\.md\\'" buffer-file-name)
  937. (set (make-local-variable 'outline-regexp) "#+ "))))
  938. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  939. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  940. (when (autoload-eval-lazily 'markdown-mode
  941. '(markdown-mode gfm-mode)
  942. (defvar gfm-mode-map (make-sparse-keymap))
  943. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  944. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  945. (set-variable 'markdown-command (or (executable-find "markdown")
  946. (executable-find "markdown.pl")
  947. ""))
  948. (add-hook 'markdown-mode-hook
  949. (lambda ()
  950. (outline-minor-mode 1)
  951. (flyspell-mode)
  952. (set (make-local-variable 'comment-start) ";")))
  953. )
  954. ;; c-mode
  955. ;; http://www.emacswiki.org/emacs/IndentingC
  956. ;; http://en.wikipedia.org/wiki/Indent_style
  957. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  958. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  959. (with-eval-after-load 'cc-vars
  960. (defvar c-default-style nil)
  961. (add-to-list 'c-default-style
  962. '(c-mode . "k&r"))
  963. (add-to-list 'c-default-style
  964. '(c++-mode . "k&r")))
  965. (autoload-eval-lazily 'js2-mode nil
  966. ;; currently do not use js2-mode
  967. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  968. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  969. ;; (defvar js2-mode-map (make-sparse-keymap))
  970. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  971. ;; (interactive)
  972. ;; (js2-enter-key)
  973. ;; (indent-for-tab-command)))
  974. ;; (add-hook (kill-local-variable 'before-save-hook)
  975. ;; 'js2-before-save)
  976. ;; (add-hook 'before-save-hook
  977. ;; 'my-indent-buffer
  978. ;; nil
  979. ;; t)
  980. )
  981. (add-to-list 'interpreter-mode-alist
  982. '("node" . js-mode))
  983. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  984. (with-eval-after-load 'uniquify
  985. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  986. (setq uniquify-ignore-buffers-re "*[^*]+*")
  987. (setq uniquify-min-dir-content 1))
  988. (with-eval-after-load 'view
  989. (defvar view-mode-map (make-sparse-keymap))
  990. (define-key view-mode-map "j" 'scroll-up-line)
  991. (define-key view-mode-map "k" 'scroll-down-line)
  992. (define-key view-mode-map "v" 'toggle-read-only)
  993. (define-key view-mode-map "q" 'bury-buffer)
  994. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  995. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  996. ;; (define-key view-mode-map
  997. ;; "n" 'nonincremental-repeat-search-forward)
  998. ;; (define-key view-mode-map
  999. ;; "N" 'nonincremental-repeat-search-backward)
  1000. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1001. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1002. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1003. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1004. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1005. (global-set-key "\M-r" 'view-mode)
  1006. ;; (setq view-read-only t)
  1007. (with-eval-after-load 'term
  1008. (defvar term-raw-map (make-sparse-keymap))
  1009. (define-key term-raw-map (kbd "C-x")
  1010. (lookup-key (current-global-map)
  1011. (kbd "C-x"))))
  1012. (add-hook 'term-mode-hook
  1013. (lambda ()
  1014. ;; Stop current line highlighting
  1015. (set (make-local-variable (defvar hl-line-range-function))
  1016. (lambda () '(0 . 0)))
  1017. (set (make-local-variable 'scroll-margin)
  1018. 0)))
  1019. (add-hook 'Man-mode-hook
  1020. (lambda ()
  1021. (view-mode 1)
  1022. (setq truncate-lines nil)))
  1023. (set-variable 'Man-notify-method (if window-system
  1024. 'newframe
  1025. 'aggressive))
  1026. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1027. "woman_cache.el")))
  1028. ;; not work because man.el will be loaded when man called
  1029. (defalias 'man 'woman)
  1030. (add-to-list 'auto-mode-alist
  1031. '("tox\\.ini\\'" . conf-unix-mode))
  1032. (when (autoload-eval-lazily 'toml-mode)
  1033. (add-to-list 'auto-mode-alist
  1034. '("/tox\\.ini\\'" . toml-mode))
  1035. (add-to-list 'auto-mode-alist
  1036. '("/Pipfile\\'" . toml-mode)))
  1037. (when (autoload-eval-lazily 'json-mode)
  1038. (add-to-list 'auto-mode-alist
  1039. '("/Pipfile\\.lock\\'" . json-mode)))
  1040. (add-hook 'go-mode-hook
  1041. (lambda()
  1042. (defvar go-mode-map)
  1043. (add-hook 'before-save-hook' 'gofmt-before-save)
  1044. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1045. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1046. ;; buffers
  1047. (defvar bs-configurations)
  1048. (when (autoload-eval-lazily 'bs '(bs-show)
  1049. (add-to-list 'bs-configurations
  1050. '("specials" "^\\*" nil ".*" nil nil))
  1051. (add-to-list 'bs-configurations
  1052. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1053. (defvar bs-mode-map)
  1054. (defvar bs-current-configuration)
  1055. (define-key bs-mode-map (kbd "t")
  1056. ;; TODO: fix toggle feature
  1057. (lambda ()
  1058. (interactive)
  1059. (if (string= "specials"
  1060. bs-current-configuration)
  1061. (bs-set-configuration "files")
  1062. (bs-set-configuration "specials"))
  1063. (bs-refresh)
  1064. (bs-message-without-log "%s"
  1065. (bs--current-config-message))))
  1066. ;; (setq bs-configurations (list
  1067. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1068. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1069. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1070. )
  1071. (defalias 'list-buffers 'bs-show)
  1072. (set-variable 'bs-default-configuration "files-and-specials")
  1073. (set-variable 'bs-default-sort-name "by nothing")
  1074. (add-hook 'bs-mode-hook
  1075. (lambda ()
  1076. (set (make-local-variable 'scroll-margin) 0))))
  1077. ;;(iswitchb-mode 1)
  1078. (icomplete-mode)
  1079. (defun iswitchb-buffer-display-other-window ()
  1080. "Do iswitchb in other window."
  1081. (interactive)
  1082. (let ((iswitchb-default-method 'display))
  1083. (call-interactively 'iswitchb-buffer)))
  1084. ;; buffer killing
  1085. ;; (defun my-delete-window-killing-buffer () nil)
  1086. (defun my-query-kill-current-buffer ()
  1087. "Interactively kill current buffer."
  1088. (interactive)
  1089. (if (y-or-n-p (concat "kill current buffer? :"))
  1090. (kill-buffer (current-buffer))))
  1091. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1092. (substitute-key-definition 'kill-buffer
  1093. 'my-query-kill-current-buffer
  1094. global-map)
  1095. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1096. ;; recentf-mode
  1097. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1098. "recentf")))
  1099. (set-variable 'recentf-max-menu-items 20)
  1100. (set-variable 'recentf-max-saved-items 30)
  1101. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1102. (set-variable 'recentf-auto-cleanup 3)
  1103. ;; (safe-require-or-eval 'sync-recentf)
  1104. ;; (when (safe-require-or-eval 'recentf)
  1105. ;; (add-to-list 'recentf-exclude
  1106. ;; (regexp-quote recentf-save-file))
  1107. ;; (add-to-list 'recentf-exclude
  1108. ;; (regexp-quote (expand-file-name user-emacs-directory)))
  1109. ;; (add-to-list 'recentf-exclude
  1110. ;; "/sync-recentf-marker\\'")
  1111. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1112. ;; (remove-hook 'find-file-hook
  1113. ;; 'recentf-track-opened-file)
  1114. ;; (defun my-recentf-load-track-save-list ()
  1115. ;; "Load current recentf list from file, track current visiting file, then save
  1116. ;; the list."
  1117. ;; (recentf-load-list)
  1118. ;; (recentf-track-opened-file)
  1119. ;; (recentf-save-list))
  1120. ;; (add-hook 'find-file-hook
  1121. ;; 'my-recentf-load-track-save-list)
  1122. ;; (add-hook 'kill-emacs-hook
  1123. ;; 'recentf-load-list)
  1124. ;; ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1125. ;; ;; (add-hook 'find-file-hook
  1126. ;; ;; (lambda ()
  1127. ;; ;; (recentf-add-file default-directory)))
  1128. ;; (when (autoload-eval-lazily 'recentf-show)
  1129. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1130. ;; ;; (add-hook 'recentf-show-before-listing-hook
  1131. ;; ;; 'recentf-load-list)
  1132. ;; )
  1133. ;; (recentf-mode 1)
  1134. ;; (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1135. ;; (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1136. ;; (define-key recentf-dialog-mode-map "p" 'previous-line)
  1137. ;; (define-key recentf-dialog-mode-map "n" 'next-line)
  1138. ;; (add-hook 'recentf-dialog-mode-hook
  1139. ;; (lambda ()
  1140. ;; ;; (recentf-save-list)
  1141. ;; ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1142. ;; ;; 'my-recentf-cd-and-find-file)
  1143. ;; (cd "~/"))))
  1144. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1145. ;; dired
  1146. (defun my-file-head (filename &optional n)
  1147. "Return list of first N lines of file FILENAME."
  1148. ;; TODO: Fix for janapese text
  1149. ;; TODO: Fix for short text
  1150. (let ((num (or n 10))
  1151. (size 100)
  1152. (beg 0)
  1153. (end 0)
  1154. (result '())
  1155. (read -1))
  1156. (with-temp-buffer
  1157. (erase-buffer)
  1158. (while (or (<= (count-lines (point-min)
  1159. (point-max))
  1160. num)
  1161. (eq read 0))
  1162. (setq end (+ beg size))
  1163. (setq read (nth 1 (insert-file-contents-literally filename
  1164. nil
  1165. beg
  1166. end)))
  1167. (goto-char (point-max))
  1168. (setq beg (+ beg size)))
  1169. (goto-char (point-min))
  1170. (while (< (length result) num)
  1171. (let ((start (point)))
  1172. (forward-line 1)
  1173. (setq result
  1174. `(,@result ,(buffer-substring-no-properties start
  1175. (point))))))
  1176. result
  1177. ;; (buffer-substring-no-properties (point-min)
  1178. ;; (progn
  1179. ;; (forward-line num)
  1180. ;; (point)))
  1181. )))
  1182. ;; (apply 'concat (my-file-head "./shrc" 10)
  1183. (defun my-dired-echo-file-head (arg)
  1184. "Echo head of current file.
  1185. ARG is num to show, or defaults to 7."
  1186. (interactive "P")
  1187. (let ((f (dired-get-filename)))
  1188. (message "%s"
  1189. (apply 'concat
  1190. (my-file-head f
  1191. 7)))))
  1192. (defun my-dired-diff ()
  1193. "Show diff of marked file and file of current line."
  1194. (interactive)
  1195. (let ((files (dired-get-marked-files nil nil nil t)))
  1196. (if (eq (car files)
  1197. t)
  1198. (diff (cadr files) (dired-get-filename))
  1199. (message "One file must be marked!"))))
  1200. (defun dired-get-file-info ()
  1201. "Print information of current line file."
  1202. (interactive)
  1203. (let ((f (shell-quote-argument (dired-get-filename t))))
  1204. (if (file-directory-p f)
  1205. (progn
  1206. (message "Calculating disk usage...")
  1207. (shell-command (concat "du -hsD "
  1208. f)))
  1209. (shell-command (concat "file "
  1210. f)))))
  1211. (defun my-dired-scroll-up ()
  1212. "Scroll up."
  1213. (interactive)
  1214. (my-dired-previous-line (- (window-height) 1)))
  1215. (defun my-dired-scroll-down ()
  1216. "Scroll down."
  1217. (interactive)
  1218. (my-dired-next-line (- (window-height) 1)))
  1219. ;; (defun my-dired-forward-line (arg)
  1220. ;; ""
  1221. ;; (interactive "p"))
  1222. (defun my-dired-previous-line (arg)
  1223. "Move ARG lines up."
  1224. (interactive "p")
  1225. (if (> arg 0)
  1226. (progn
  1227. (if (eq (line-number-at-pos)
  1228. 1)
  1229. (goto-char (point-max))
  1230. (forward-line -1))
  1231. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1232. (dired-get-subdir))
  1233. (- arg 1)
  1234. arg)))
  1235. (dired-move-to-filename)))
  1236. (defun my-dired-next-line (arg)
  1237. "Move ARG lines down."
  1238. (interactive "p")
  1239. (if (> arg 0)
  1240. (progn
  1241. (if (eq (point)
  1242. (point-max))
  1243. (goto-char (point-min))
  1244. (forward-line 1))
  1245. (my-dired-next-line (if (or (dired-get-filename nil t)
  1246. (dired-get-subdir))
  1247. (- arg 1)
  1248. arg)))
  1249. (dired-move-to-filename)))
  1250. (defun my-tramp-remote-find-file (f)
  1251. "Open F."
  1252. (interactive (list (read-file-name "My Find File Tramp: "
  1253. "/scp:"
  1254. nil ;; "/scp:"
  1255. (confirm-nonexistent-file-or-buffer))))
  1256. (find-file f))
  1257. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1258. (if (eq window-system 'mac)
  1259. (setq dired-listing-switches "-lhF")
  1260. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1261. )
  1262. (setq dired-listing-switches "-lhF")
  1263. ;; when using dired-find-alternate-file
  1264. ;; reuse current dired buffer for the file to open
  1265. ;; (put 'dired-find-alternate-file 'disabled nil)
  1266. (set-variable 'dired-ls-F-marks-symlinks t)
  1267. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1268. (set-variable 'ls-lisp-dirs-first t)
  1269. (set-variable 'ls-lisp-use-localized-time-format t)
  1270. (set-variable 'ls-lisp-format-time-list
  1271. '("%Y-%m-%d %H:%M"
  1272. "%Y-%m-%d "))
  1273. (set-variable 'dired-dwim-target t)
  1274. (set-variable 'dired-isearch-filenames t)
  1275. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1276. (set-variable 'dired-hide-details-hide-information-lines nil)
  1277. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1278. (set-variable 'dired-recursive-deletes 'always)
  1279. ;; (add-hook 'dired-after-readin-hook
  1280. ;; 'my-replace-nasi-none)
  1281. (with-eval-after-load 'dired
  1282. (safe-require-or-eval 'ls-lisp)
  1283. (defvar dired-mode-map (make-sparse-keymap))
  1284. ;; dired-do-chgrp sometimes cause system hung
  1285. (define-key dired-mode-map "G" 'ignore)
  1286. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1287. (define-key dired-mode-map "i" 'dired-get-file-info)
  1288. (define-key dired-mode-map "f" 'find-file)
  1289. (define-key dired-mode-map "!" 'shell-command)
  1290. (define-key dired-mode-map "&" 'async-shell-command)
  1291. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1292. (define-key dired-mode-map "=" 'my-dired-diff)
  1293. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1294. (define-key dired-mode-map "b" 'gtkbm)
  1295. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1296. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1297. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1298. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1299. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1300. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1301. (substitute-key-definition 'dired-next-line
  1302. 'my-dired-next-line
  1303. dired-mode-map)
  1304. (substitute-key-definition 'dired-previous-line
  1305. 'my-dired-previous-line
  1306. dired-mode-map)
  1307. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1308. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1309. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1310. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1311. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1312. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1313. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1314. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1315. (add-hook 'dired-mode-hook
  1316. (lambda ()
  1317. (when (fboundp 'dired-hide-details-mode)
  1318. (dired-hide-details-mode t)
  1319. (local-set-key "l" 'dired-hide-details-mode))
  1320. (let ((file "._Icon\015"))
  1321. (when nil
  1322. '(file-readable-p file)
  1323. (delete-file file)))))
  1324. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1325. (with-eval-after-load 'dired
  1326. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1327. (when (autoload-eval-lazily 'dired-list-all-mode)
  1328. (setq dired-listing-switches "-lhF")
  1329. (with-eval-after-load 'dired
  1330. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1331. (when (autoload-eval-lazily 'dired-filter)
  1332. (add-hook 'dired-mode-hook
  1333. 'dired-filter-mode))
  1334. ;; Currently disabled in favor of dired-from-git-ls-files
  1335. ;; (define-key ctl-x-map "f" 'find-dired)
  1336. ;; It works!
  1337. ;; (pop-to-buffer (dired-noselect '("." "shrc" "emacs.el")))
  1338. (defun my-dired-git-ls-files (args)
  1339. "Dired from git ls-files."
  1340. (interactive "sgit ls-files args: ")
  1341. (pop-to-buffer-same-window
  1342. (dired-noselect `(,default-directory
  1343. ,@(split-string (shell-command-to-string (concat "git ls-files -z " args))
  1344. "\0" t))
  1345. ""))
  1346. )
  1347. (define-key ctl-x-map (kbd "f") 'my-dired-git-ls-files)
  1348. (with-eval-after-load 'dired
  1349. (defvar dired-mode-map (make-sparse-keymap))
  1350. (define-key dired-mode-map "f" 'my-dired-git-ls-files))
  1351. ;; (define-minor-mode my-dired-glob-filter)
  1352. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1353. ;; misc funcs
  1354. (defalias 'qcalc 'quick-calc)
  1355. (defun memo (&optional dir)
  1356. "Open memo.txt in DIR."
  1357. (interactive)
  1358. (pop-to-buffer (find-file-noselect (concat (if dir
  1359. (file-name-as-directory dir)
  1360. "")
  1361. "memo.txt"))))
  1362. (set (defvar my-rgrep-alist nil
  1363. "Alist of rgrep command.
  1364. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1365. condition to choose COMMAND when evaluated.")
  1366. `(
  1367. ;; ripgrep
  1368. ("rg"
  1369. (executable-find "rg")
  1370. "rg --hidden --no-heading --smart-case ")
  1371. ;; git grep
  1372. ("gitgrep"
  1373. (eq 0
  1374. (shell-command "git rev-parse --git-dir"))
  1375. "git --no-pager -c color.grep=always grep -nH -e ")
  1376. ;; sift
  1377. ("sift"
  1378. (executable-find "sift")
  1379. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1380. ;; the silver searcher
  1381. ("ag"
  1382. (executable-find "ag")
  1383. "ag --nogroup --nopager --filename ")
  1384. ;; ack
  1385. ("ack"
  1386. (executable-find "ack")
  1387. "ack --nogroup --nopager --with-filename ")
  1388. ;; gnu global
  1389. ("global"
  1390. (and (require 'ggtags nil t)
  1391. (executable-find "global")
  1392. (ggtags-current-project-root))
  1393. "global --result grep ")
  1394. ;; grep
  1395. ("grep"
  1396. t
  1397. ,(concat "find . "
  1398. "-path '*/.git' -prune -o "
  1399. "-path '*/.svn' -prune -o "
  1400. "-type f -print0 | "
  1401. "xargs -0 grep -nH -e "))
  1402. )
  1403. )
  1404. (defvar my-rgrep-default nil
  1405. "Default command name for my-rgrep.")
  1406. (defun my-rgrep-grep-command (&optional name alist)
  1407. "Return recursive grep command for current directory or nil.
  1408. If NAME is given, use that without testing.
  1409. Commands are searched from ALIST."
  1410. (if alist
  1411. (if name
  1412. ;; if name is given search that from alist and return the command
  1413. (nth 2 (assoc name
  1414. alist))
  1415. ;; if name is not given try test in 1th elem
  1416. (let ((car (car alist))
  1417. (cdr (cdr alist)))
  1418. (if (eval (nth 1 car))
  1419. ;; if the condition is true return the command
  1420. (nth 2 car)
  1421. ;; try next one
  1422. (and cdr
  1423. (my-rgrep-grep-command name cdr)))))
  1424. ;; if alist is not given set default value
  1425. (my-rgrep-grep-command name my-rgrep-alist)))
  1426. (defun my-rgrep (command-args)
  1427. "My recursive grep. Run COMMAND-ARGS.
  1428. If prefix argument is given, use current symbol as default search target
  1429. and search from projectile root (if projectile is available)."
  1430. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1431. nil)))
  1432. (if cmd
  1433. (list (read-shell-command "grep command: "
  1434. (concat cmd
  1435. (if current-prefix-arg
  1436. (thing-at-point 'symbol t)
  1437. ""))
  1438. 'grep-find-history))
  1439. (error "My-Rgrep: Command for rgrep not found")
  1440. )))
  1441. (if (and current-prefix-arg
  1442. (safe-require-or-eval 'projectile)
  1443. (projectile-project-p))
  1444. (projectile-with-default-dir (projectile-project-root)
  1445. (compilation-start command-args
  1446. 'grep-mode))
  1447. (compilation-start command-args
  1448. 'grep-mode)))
  1449. (defun my-rgrep-thing-at-point-projectile-root ()
  1450. "My recursive grep to find thing at point from project root."
  1451. (interactive)
  1452. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1453. nil))
  1454. (command-args
  1455. (if cmd
  1456. (concat cmd
  1457. (or (thing-at-point 'symbol t)
  1458. (error "No symbol at point")))
  1459. (error "My-Rgrep: Command for rgrep not found"))))
  1460. (if (safe-require-or-eval 'projectile)
  1461. (projectile-with-default-dir (or (projectile-project-root)
  1462. default-directory)
  1463. (compilation-start command-args
  1464. 'grep-mode))
  1465. (compilation-start command-args
  1466. 'grep-mode))))
  1467. (defmacro define-my-rgrep (name)
  1468. "Define rgrep for NAME."
  1469. `(defun ,(intern (concat "my-rgrep-"
  1470. name)) ()
  1471. ,(format "My recursive grep by %s."
  1472. name)
  1473. (interactive)
  1474. (let ((my-rgrep-default ,name))
  1475. (if (called-interactively-p 'any)
  1476. (call-interactively 'my-rgrep)
  1477. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1478. )
  1479. (define-my-rgrep "ack")
  1480. (define-my-rgrep "ag")
  1481. (define-my-rgrep "rg")
  1482. (define-my-rgrep "sift")
  1483. (define-my-rgrep "gitgrep")
  1484. (define-my-rgrep "grep")
  1485. (define-my-rgrep "global")
  1486. (define-key ctl-x-map "s" 'my-rgrep)
  1487. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1488. (defun my-occur (regexp &optional region)
  1489. "My occur command to search REGEXP."
  1490. (interactive (list (read-string "List lines matching regexp: "
  1491. (thing-at-point 'symbol t))))
  1492. (occur regexp nil region))
  1493. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1494. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1495. (defalias 'make 'compile)
  1496. (define-key ctl-x-map "c" 'compile)
  1497. ;;;;;;;;;;;;;;;;;;;;;;;
  1498. ;; adoc-simple-mode
  1499. (when (safe-require-or-eval 'adoc-mode)
  1500. (defvar adoc-simple-font-lock-keywords
  1501. nil)
  1502. (define-derived-mode adoc-simple-mode adoc-mode
  1503. "Adoc-Simple"
  1504. "Major mode for editing AsciiDoc text files.
  1505. This mode is a simplified version of `adoc-mode'."
  1506. '(set (make-local-variable 'font-lock-defaults)
  1507. '(adoc-simple-font-lock-keywords
  1508. nil nil nil nil
  1509. (font-lock-multiline . t)
  1510. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1511. (add-to-list 'auto-mode-alist
  1512. '("\\.adoc\\'" . adoc-simple-mode)))
  1513. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1514. ;; awk-preview
  1515. (defgroup awk-preview nil
  1516. "Awk previewer."
  1517. :tag "Awk Preview"
  1518. :prefix "awk-preview-"
  1519. :group 'tools)
  1520. (defcustom awk-preview-program
  1521. (or (executable-find "gawk")
  1522. (executable-find "awk")
  1523. "awk")
  1524. "Awk program to execute."
  1525. :type 'string
  1526. :group 'awk-preview)
  1527. (defcustom awk-preview-switches
  1528. ;; '("--sandbox")
  1529. nil
  1530. "String of awk options appended when running awk preview."
  1531. :type '(repeat string)
  1532. :group 'awk-preview)
  1533. (defcustom awk-preview-default-program
  1534. "# C-c C-l to update preview, C-c C-c to commit, C-c C-k to abort.
  1535. {
  1536. print NR, $0
  1537. }
  1538. "
  1539. "Default awk command."
  1540. :type 'string
  1541. :group 'awk-preview)
  1542. (defvar awk-preview--point-beg
  1543. nil
  1544. "Point of beginning.")
  1545. (make-variable-buffer-local 'awk-preview--point-beg)
  1546. (defvar awk-preview--point-end
  1547. nil
  1548. "Point of end.")
  1549. (make-variable-buffer-local 'awk-preview--point-end)
  1550. (defvar awk-preview--preview-point-beg
  1551. nil
  1552. "Point of beginning.
  1553. Used by preview buffer and always same as awk-preview--point-beg.")
  1554. (make-variable-buffer-local 'awk-preview--preview-point-beg)
  1555. (defvar awk-preview--preview-point-end
  1556. nil
  1557. "Point of beginning.
  1558. Used by preview buffer and may defferent from awk-preview--point-end.")
  1559. (make-variable-buffer-local 'awk-preview--preview-point-end)
  1560. (defvar awk-preview--program-filename
  1561. nil
  1562. "Awk preview program file name.")
  1563. (make-variable-buffer-local 'awk-preview--program-filename)
  1564. (defvar awk-preview--source-buffer
  1565. nil
  1566. "Awk source buffer.")
  1567. (make-variable-buffer-local 'awk-preview--source-buffer)
  1568. (defvar awk-preview--preview-buffer
  1569. nil
  1570. "Awk preview buffer.")
  1571. (make-variable-buffer-local 'awk-preview--preview-buffer)
  1572. (defvar awk-preview--program-buffer
  1573. nil
  1574. "Awk program buffer.")
  1575. (make-variable-buffer-local 'awk-preview--program-buffer)
  1576. (defvar awk-preview--window-configuration
  1577. nil
  1578. "Awk preview window configuration.")
  1579. (defun awk-preview--invoke-awk (buf beg end progfile output &optional delete)
  1580. "Execute PROGFILE awk process with BEG and END input and output to OUTPUT buffer.
  1581. When OUTPUT is t the result will be output to BUF.
  1582. Delete the text between BEG and END when DELETE is non-nil."
  1583. (with-current-buffer buf
  1584. (let ((status (apply 'call-process-region
  1585. beg
  1586. end
  1587. awk-preview-program
  1588. delete
  1589. output
  1590. nil
  1591. `(,@awk-preview-switches "-f" ,progfile))))
  1592. (unless (eq status
  1593. 0)
  1594. (error "awk-preview: Awk program exited abnormally."))
  1595. output)))
  1596. (defvar awk-preview-program-buffer-name
  1597. "*AWK Preview Program<%s>*"
  1598. "Buffer name of awk preview program.")
  1599. (defvar awk-preview-preview-buffer-name
  1600. "*AWK Preview<%s>*"
  1601. "Buffer name of awk preview.")
  1602. (defun awk-preview--create-program-buffer (source preview)
  1603. "Create and show awk program buffer for SOURCE and PREVIEW buffer.
  1604. Return that buffer."
  1605. (let ((source-name (buffer-name source)))
  1606. (with-current-buffer (generate-new-buffer (format awk-preview-program-buffer-name
  1607. source-name))
  1608. (erase-buffer)
  1609. (insert awk-preview-default-program)
  1610. (awk-mode)
  1611. (awk-preview-program-mode 1)
  1612. (setq awk-preview--source-buffer source)
  1613. (setq awk-preview--preview-buffer preview)
  1614. (setq awk-preview--program-buffer (current-buffer))
  1615. (setq awk-preview--program-filename (make-temp-file "awk-preview-"
  1616. nil
  1617. ".awk"))
  1618. (current-buffer))))
  1619. (defun awk-preview-program-buffer-kill-hook ()
  1620. "Cleanup for awk-preview program buffer."
  1621. (when awk-preview--program-filename
  1622. (delete-file awk-preview--program-filename)))
  1623. (add-hook 'kill-buffer-hook
  1624. 'awk-preview-program-buffer-kill-hook)
  1625. (defun awk-preview--create-preview-buffer (source)
  1626. "Create preview buffer of SOURCE buffer and return it."
  1627. (with-current-buffer source
  1628. (let ((buffer-file-name nil)
  1629. (beg awk-preview--point-beg)
  1630. (end awk-preview--point-end))
  1631. (with-current-buffer (clone-buffer (format awk-preview-preview-buffer-name
  1632. (buffer-name)))
  1633. (awk-preview-program-mode 1)
  1634. (setq awk-preview--preview-point-beg beg)
  1635. (setq awk-preview--preview-point-end end)
  1636. (setq awk-preview--source-buffer source)
  1637. (setq awk-preview--preview-buffer (current-buffer))
  1638. (goto-char end)
  1639. (setq buffer-read-only t)
  1640. (current-buffer)))))
  1641. (defun awk-preview (beg end)
  1642. "Run awk and preview result."
  1643. (interactive "r")
  1644. (setq awk-preview--point-beg beg)
  1645. (setq awk-preview--point-end end)
  1646. (setq awk-preview--source-buffer (current-buffer))
  1647. (setq awk-preview--preview-buffer
  1648. (awk-preview--create-preview-buffer (current-buffer)))
  1649. (setq awk-preview--program-buffer
  1650. (awk-preview--create-program-buffer (current-buffer)
  1651. awk-preview--preview-buffer))
  1652. ;; At creation of preview buffer, program buffer is not available so set this
  1653. ;; here
  1654. (let ((b awk-preview--program-buffer))
  1655. (with-current-buffer awk-preview--preview-buffer
  1656. (setq awk-preview--program-buffer b)))
  1657. (setq awk-preview--window-configuration
  1658. (current-window-configuration))
  1659. (set-window-buffer (get-buffer-window awk-preview--source-buffer)
  1660. awk-preview--preview-buffer)
  1661. (pop-to-buffer awk-preview--program-buffer)
  1662. (switch-to-buffer awk-preview--program-buffer)
  1663. (awk-preview-update-preview)
  1664. )
  1665. (defun awk-preview-update-preview ()
  1666. "Update awk-preview."
  1667. (interactive)
  1668. (with-current-buffer awk-preview--program-buffer
  1669. (write-region (point-min)
  1670. (point-max)
  1671. awk-preview--program-filename))
  1672. (let ((output (with-current-buffer (get-buffer-create " *awk-preview output*")
  1673. (erase-buffer)
  1674. (current-buffer)))
  1675. (progfile (with-current-buffer awk-preview--program-buffer
  1676. awk-preview--program-filename)))
  1677. (with-current-buffer awk-preview--source-buffer
  1678. (awk-preview--invoke-awk (current-buffer)
  1679. awk-preview--point-beg
  1680. awk-preview--point-end
  1681. progfile
  1682. output)
  1683. (with-current-buffer awk-preview--preview-buffer
  1684. (let ((inhibit-read-only t))
  1685. (goto-char awk-preview--preview-point-end)
  1686. (delete-region awk-preview--preview-point-beg (point))
  1687. (insert-buffer-substring output)
  1688. (setq awk-preview--preview-point-end (point))))
  1689. )))
  1690. (defun awk-preview-commit ()
  1691. "Exit awk-preview and update buffer."
  1692. (interactive))
  1693. (defun awk-preview-abort ()
  1694. "Discard result and exit awk-preview."
  1695. (interactive))
  1696. (defun awk-preview--cleanup()
  1697. "Cleanup awk preview buffers and variables.")
  1698. (defvar awk-preview-program-mode-map
  1699. (let ((map (make-sparse-keymap)))
  1700. (define-key map (kbd "C-c C-l") 'awk-preview-update-preview)
  1701. (define-key map (kbd "C-c C-k") 'awk-preview-abort)
  1702. (define-key map (kbd "C-c C-c") 'awk-preview-commit)
  1703. map)
  1704. "Keymap for `awk-preview-program-mode'.")
  1705. (define-minor-mode awk-preview-program-mode
  1706. "Minor mode for awk-preview program buffer."
  1707. :lighter " AWKPrev"
  1708. :keymap awk-preview-program-mode-map)
  1709. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1710. ;; editorconfig-auto-apply
  1711. (define-minor-mode editorconfig-auto-apply-mode
  1712. "When saving .editorconfig file update buffer configs."
  1713. :global t
  1714. :lighter ""
  1715. (if editorconfig-auto-apply-mode
  1716. (add-hook 'after-save-hook
  1717. 'editorconfig-auto-apply-mode--run)
  1718. (remove-hook 'after-save-hook
  1719. 'editorconfig-auto-apply-mode--run)))
  1720. (defun editorconfig-auto-apply-mode--run ()
  1721. "When saving .editorconfig file walk all buffers and update configs."
  1722. (when (eq major-mode
  1723. 'editorconfig-conf-mode)
  1724. (let ((dir (file-name-directory buffer-file-name)))
  1725. (cl-dolist (buf (buffer-list))
  1726. (when (and (buffer-file-name buf)
  1727. (file-in-directory-p (buffer-file-name buf)
  1728. dir))
  1729. (with-current-buffer buf
  1730. (editorconfig-mode-apply)))))))
  1731. (editorconfig-auto-apply-mode 1)
  1732. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1733. ;; recently
  1734. (defgroup recently nil
  1735. "Recently visited files"
  1736. :tag "Recently"
  1737. :prefix "recently-"
  1738. :group 'tools)
  1739. (defcustom recently-file
  1740. (concat user-emacs-directory
  1741. "recently.el")
  1742. "Recently file."
  1743. :type 'string
  1744. :group 'recently)
  1745. (defcustom recently-max
  1746. 100
  1747. "Recently list max length."
  1748. :type 'int
  1749. :group 'recently)
  1750. (defcustom recently-excludes
  1751. '()
  1752. "List of regexps for filenames excluded from the recent list."
  1753. :type '(repeat string)
  1754. :group 'recently)
  1755. (add-to-list 'recently-excludes
  1756. (eval-when-compile (rx "/COMMIT_EDITMSG" eot)))
  1757. (defvar recently-list
  1758. '()
  1759. "Recently list.")
  1760. (defvar recently-file-mtime
  1761. nil
  1762. "Modified time of file when last read file.")
  1763. (defun recently-write ()
  1764. "Write to file."
  1765. (with-temp-buffer
  1766. (prin1 recently-list
  1767. (current-buffer))
  1768. (write-region (point-min)
  1769. (point-max)
  1770. recently-file)))
  1771. (defun recently-read ()
  1772. "Read file."
  1773. (when (file-readable-p recently-file)
  1774. (with-temp-buffer
  1775. (insert-file-contents recently-file)
  1776. (goto-char (point-min))
  1777. (setq recently-list
  1778. (read (current-buffer))))
  1779. (setq recently-file-mtime
  1780. (nth 5
  1781. (file-attributes recently-file)))))
  1782. (defun recently-reload ()
  1783. "Reload file."
  1784. (when (and (file-readable-p recently-file)
  1785. (not (equal recently-file-mtime
  1786. (nth 5
  1787. (file-attributes recently-file)))))
  1788. (recently-read)
  1789. (cl-assert (equal recently-file-mtime
  1790. (nth 5
  1791. (file-attributes recently-file))))))
  1792. (defun recently-add (path)
  1793. "Add PATH to list."
  1794. (cl-assert (string= path
  1795. (expand-file-name path)))
  1796. (when (cl-loop for re in recently-excludes
  1797. if (string-match re path) return nil
  1798. finally return t)
  1799. (recently-reload)
  1800. (let* ((l (copy-list recently-list))
  1801. (l (delete path
  1802. l))
  1803. (l (cl-loop for e in l
  1804. unless (file-in-directory-p path e)
  1805. collect e))
  1806. (l (cons path
  1807. l))
  1808. (l (recently--truncate l
  1809. recently-max)))
  1810. (unless (equal recently-list
  1811. l)
  1812. (setq recently-list l)
  1813. (recently-write)))))
  1814. (defun recently--truncate (list len)
  1815. "Truncate LIST to LEN."
  1816. (if (> (length list)
  1817. len)
  1818. (cl-subseq list
  1819. 0
  1820. len)
  1821. list))
  1822. (defun recently-find-file-hook ()
  1823. "Add current file."
  1824. (when buffer-file-name
  1825. (recently-add buffer-file-name)))
  1826. (defun recently-dired-mode-hook ()
  1827. "Add current directory."
  1828. (recently-add (expand-file-name default-directory)))
  1829. (add-hook 'find-file-hook
  1830. 'recently-find-file-hook)
  1831. (add-hook 'dired-mode-hook
  1832. 'recently-dired-mode-hook)
  1833. ;; recently-show
  1834. (defvar recently-show-window-height 10
  1835. "Max height of window of `recently-show'")
  1836. (defvar recently-show-mode-map
  1837. (let ((map (make-sparse-keymap)))
  1838. (suppress-keymap map)
  1839. (define-key map "n" 'next-line)
  1840. (define-key map "p" 'previous-line)
  1841. (define-key map (kbd "C-m") 'recently-show-find-file)
  1842. (define-key map (kbd "SPC") 'recently-show-find-file)
  1843. (define-key map "v" 'recently-show-view-file)
  1844. (define-key map "@" 'recently-show-dired)
  1845. (define-key map "q" 'recently-show-close)
  1846. (define-key map (kbd "C-g") 'recently-show-close)
  1847. (define-key map "?" 'describe-mode)
  1848. (define-key map "/" 'isearch-forward)
  1849. map))
  1850. (defvar recently-show-before-listing-hook nil
  1851. "Hook run before creating buffer of `recently-show'.")
  1852. (defvar recently-show-window-configuration nil
  1853. "Used for internal")
  1854. (defvar recently-show-abbreviate t
  1855. "Non-nil means use `abbreviate-file-name' when listing recently opened files.")
  1856. (define-derived-mode recently-show-mode special-mode "recently-show"
  1857. "Major mode for `recently-show'."
  1858. ;; (set (make-local-variable 'scroll-margin)
  1859. ;; 0)
  1860. )
  1861. ;;;###autoload
  1862. (defun recently-show (&optional files buffer-name)
  1863. "Show simplified list of recently opened files.
  1864. If optional argument FILES is non-nil, it is a list of recently-opened
  1865. files to choose from. It defaults to the whole recent list.
  1866. If optional argument BUFFER-NAME is non-nil, it is a buffer name to
  1867. use for the buffer. It defaults to \"*recetf-show*\"."
  1868. (interactive)
  1869. (let ((bf (recently-show-create-buffer files buffer-name)))
  1870. (if bf
  1871. (progn
  1872. ;; (recently-save-list)
  1873. (setq recently-show-window-configuration (current-window-configuration))
  1874. ;;(pop-to-buffer bf t t)
  1875. (display-buffer bf)
  1876. ;; (set-window-text-height (selected-window)
  1877. ;; recently-show-window-height)
  1878. (shrink-window-if-larger-than-buffer (selected-window)))
  1879. (message "No recent file!"))))
  1880. (defun recently-show-create-buffer (&optional files buffer-name)
  1881. "Create buffer listing recently files."
  1882. (let ((bname (or buffer-name
  1883. "*recently-show*"))
  1884. (list (or files
  1885. (progn
  1886. (recently-reload)
  1887. recently-list))))
  1888. (when list
  1889. (and (get-buffer bname)
  1890. (kill-buffer bname))
  1891. (let ((bf (get-buffer-create bname)))
  1892. (with-current-buffer bf
  1893. (recently-show-mode)
  1894. (let ((inhibit-read-only t))
  1895. (mapc (lambda (f)
  1896. (insert (if recently-show-abbreviate
  1897. (abbreviate-file-name f)
  1898. f)
  1899. "\n"))
  1900. list))
  1901. (goto-char (point-min))
  1902. ;; (setq buffer-read-only t)
  1903. )
  1904. bf))))
  1905. (defun recently-show-close ()
  1906. "Close recently-show window."
  1907. (interactive)
  1908. (kill-buffer (current-buffer))
  1909. (set-window-configuration recently-show-window-configuration))
  1910. (defun recently-show-find-file ()
  1911. "Fine file of current line."
  1912. (interactive)
  1913. (let ((f (recently-show-get-filename)))
  1914. (recently-show-close)
  1915. (find-file f)))
  1916. (defun recently-show-view-file ()
  1917. "view file of current line."
  1918. (interactive)
  1919. (let ((f (recently-show-get-filename)))
  1920. (recently-show-close)
  1921. (view-file f)))
  1922. (defun recently-show-get-filename ()
  1923. "Get filename of current line."
  1924. (buffer-substring-no-properties (point-at-bol)
  1925. (point-at-eol)))
  1926. (defun recently-show-dired()
  1927. "Open dired buffer of directory containing file of current line."
  1928. (interactive)
  1929. (let ((f (recently-show-get-filename)))
  1930. (recently-show-close)
  1931. (dired (if (file-directory-p f)
  1932. f
  1933. (or (file-name-directory f)
  1934. ".")))))
  1935. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  1936. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1937. ;; git walktree
  1938. (defgroup git-walktree nil
  1939. "Git Walktree."
  1940. :tag "GitWalktree"
  1941. :prefix "git-walktree-"
  1942. :group 'tools)
  1943. (defvar git-walktree-current-committish nil
  1944. "Committish name of currently browsing.")
  1945. (make-variable-buffer-local 'git-walktree-current-committish)
  1946. (defvar git-walktree-current-path nil
  1947. "Path name currently visiting without leading and trailing slash.
  1948. This path is always relative to repository root.")
  1949. (make-variable-buffer-local 'git-walktree-current-path)
  1950. (defvar git-walktree-buffer-file-name nil
  1951. "Psudo filename of current buffer.")
  1952. (make-variable-buffer-local 'git-walktree-buffer-file-name)
  1953. (defvar git-walktree-object-full-sha1 nil
  1954. "Object name in full sha1 format of current buffer.")
  1955. (make-variable-buffer-local 'git-walktree-object-full-sha1)
  1956. (defvar git-walktree-repository-root nil
  1957. "Repository root path of current buffer.")
  1958. (make-variable-buffer-local 'git-walktree-repository-root)
  1959. (put 'git-walktree-repository-root
  1960. 'permanent-local
  1961. t)
  1962. (defun git-walktree--committish-fordisplay (committish)
  1963. "Convert COMMITTISH and return is a suitable format for displaying."
  1964. (if (and committish
  1965. (string-match-p "\\`[0-9a-f]+\\'"
  1966. committish)
  1967. (>= (length committish) 32))
  1968. (git-walktree--git-plumbing "rev-parse"
  1969. "--short"
  1970. committish)
  1971. committish))
  1972. (defun git-walktree--create-buffer (committish name type)
  1973. "Create and return buffer for COMMITTISH:NAME.
  1974. TYPE is target object type."
  1975. (let* ((root (git-walktree--git-plumbing "rev-parse"
  1976. "--show-toplevel"))
  1977. (committish-display (git-walktree--committish-fordisplay committish))
  1978. (name (format "%s:%s"
  1979. (or committish-display "")
  1980. name)))
  1981. (if (and git-walktree-reuse-tree-buffer
  1982. (string= type "tree"))
  1983. (with-current-buffer (or git-walktree-tree-buffer-for-reuse
  1984. (setq git-walktree-tree-buffer-for-reuse
  1985. (generate-new-buffer "gitwalktreebuf")))
  1986. (setq git-walktree-repository-root root)
  1987. (rename-buffer name t)
  1988. (current-buffer))
  1989. (with-current-buffer (get-buffer-create name)
  1990. (if git-walktree-repository-root
  1991. (if (string= root
  1992. git-walktree-repository-root)
  1993. (current-buffer)
  1994. ;; If the buffer is for another repository, create new buffer
  1995. (with-current-buffer (generate-new-buffer name)
  1996. (setq git-walktree-repository-root root)
  1997. (current-buffer)))
  1998. ;; New buffer
  1999. (setq git-walktree-repository-root root)
  2000. (current-buffer))))))
  2001. (defun git-walktree--replace-into-buffer (target)
  2002. "Replace TARGET buffer contents with that of current buffer.
  2003. It also copy text overlays."
  2004. (let ((src (current-buffer)))
  2005. (with-current-buffer target
  2006. (replace-buffer-contents src)))
  2007. ;; Copy color overlays
  2008. (let ((overlays (overlays-in (point-min) (point-max))))
  2009. (dolist (o overlays)
  2010. (let ((beg (overlay-start o))
  2011. (end (overlay-end o)))
  2012. (move-overlay (copy-overlay o)
  2013. beg
  2014. end
  2015. target)))))
  2016. (require 'ansi-color)
  2017. (defun git-walktree--open-treeish (committish path treeish)
  2018. "Open git tree buffer of COMMITISH:PATH.
  2019. TREEISH should be a tree-ish object full-sha1 of COMMITISH:PATH."
  2020. (cl-assert path)
  2021. (cl-assert treeish)
  2022. (let* (point-tree-start
  2023. (type (git-walktree--git-plumbing "cat-file"
  2024. "-t"
  2025. treeish))
  2026. (buf (git-walktree--create-buffer committish path type))
  2027. )
  2028. (cl-assert (member type
  2029. '("commit" "tree")))
  2030. (with-current-buffer buf
  2031. (unless (and (string= treeish
  2032. git-walktree-object-full-sha1)
  2033. (or (eq committish
  2034. git-walktree-current-committish)
  2035. (string= committish
  2036. git-walktree-current-committish)))
  2037. (buffer-disable-undo)
  2038. ;; For running git command go back to repository root
  2039. (cd git-walktree-repository-root)
  2040. (save-excursion
  2041. (let ((inhibit-read-only t))
  2042. ;; Remove existing overlays generated by ansi-color-apply-on-region
  2043. (remove-overlays)
  2044. (with-temp-buffer
  2045. (if committish
  2046. (progn (git-walktree--call-process nil
  2047. "show"
  2048. ;; TODO: Make this args configurable
  2049. ;; "--no-patch"
  2050. "--color=always"
  2051. "--pretty=short"
  2052. "--decorate"
  2053. "--stat"
  2054. committish)
  2055. (ansi-color-apply-on-region (point-min)
  2056. (point))
  2057. (insert "\n")
  2058. (insert (format "Contents of '%s:%s':\n"
  2059. (git-walktree--committish-fordisplay committish)
  2060. path)))
  2061. (insert (format "Contents of treeish object '%s:\n"
  2062. treeish)))
  2063. (setq point-tree-start (point))
  2064. (git-walktree--call-process nil
  2065. "ls-tree"
  2066. ;; "-r"
  2067. "--abbrev"
  2068. treeish)
  2069. (git-walktree--replace-into-buffer buf))
  2070. ))
  2071. (git-walktree-mode)
  2072. (set-buffer-modified-p nil)
  2073. (setq git-walktree-current-committish committish)
  2074. (setq git-walktree-current-path path)
  2075. (setq git-walktree-object-full-sha1 treeish)
  2076. (let ((dir (expand-file-name path git-walktree-repository-root)))
  2077. (when (and git-walktree-try-cd
  2078. (file-directory-p dir))
  2079. (cd dir)))
  2080. (when (= (point) (point-min))
  2081. (goto-char point-tree-start)
  2082. (git-walktree-mode--move-to-file)
  2083. )
  2084. ))
  2085. buf))
  2086. (defun git-walktree--call-process (&optional infile &rest args)
  2087. "Call git command with input from INFILE and args ARGS.
  2088. Result will be inserted into current buffer."
  2089. (let ((status (apply 'call-process
  2090. git-walktree-git-executable
  2091. infile
  2092. t
  2093. nil
  2094. args)))
  2095. (unless (eq 0
  2096. status)
  2097. (error "Failed to call git process %S %S"
  2098. infile
  2099. args))))
  2100. ?w
  2101. (defun git-walktree--open-blob (committish path blob)
  2102. "Open blob object of COMMITISH:PATH.
  2103. BLOB should be a object full sha1 of COMMITISH:PATH."
  2104. (cl-assert committish)
  2105. (cl-assert path)
  2106. (cl-assert blob)
  2107. (let* ((type (git-walktree--git-plumbing "cat-file"
  2108. "-t"
  2109. blob))
  2110. (buf (git-walktree--create-buffer committish path type)))
  2111. (cl-assert (string= type "blob"))
  2112. (with-current-buffer buf
  2113. (unless (string= blob
  2114. git-walktree-object-full-sha1)
  2115. ;; For running git command go back to repository root
  2116. (cd git-walktree-repository-root)
  2117. (let ((inhibit-read-only t))
  2118. (with-temp-buffer
  2119. (git-walktree--call-process nil
  2120. "cat-file"
  2121. "-p"
  2122. blob)
  2123. (git-walktree--replace-into-buffer buf)))
  2124. (setq git-walktree-buffer-file-name
  2125. (concat git-walktree-repository-root "/git@" committish ":" path))
  2126. (setq buffer-file-name
  2127. (concat git-walktree-repository-root "/" path))
  2128. (normal-mode t)
  2129. ;; For asking filename when C-xC-s
  2130. (setq buffer-file-name nil)
  2131. (set-buffer-modified-p t)
  2132. (setq git-walktree-current-committish committish)
  2133. (setq git-walktree-current-path path)
  2134. (setq git-walktree-object-full-sha1 blob)
  2135. (let ((dir (expand-file-name (or (file-name-directory path)
  2136. ".")
  2137. git-walktree-repository-root)))
  2138. (when (and git-walktree-try-cd
  2139. (file-directory-p dir))
  2140. (cd dir)))
  2141. (view-mode 1)
  2142. ))
  2143. buf))
  2144. (defun git-walktree--open-noselect-safe-path (committish &optional path)
  2145. "Open git object of COMMITTISH:PATH.
  2146. If PATH not found in COMMITTISH tree, go up path and try again until found.
  2147. When PATH is omitted or nil, it is calculated from current file or directory."
  2148. (cl-assert committish)
  2149. (let ((type (git-walktree--git-plumbing "cat-file"
  2150. "-t"
  2151. committish)))
  2152. (cl-assert (string= type "commit")))
  2153. (setq path
  2154. (or path
  2155. (git-walktree--path-in-repository (or buffer-file-name
  2156. default-directory))))
  2157. ;; PATH must not start with and end with slashes
  2158. (cl-assert (not (string-match-p "\\`/" path)))
  2159. (cl-assert (not (string-match-p "/\\'" path)))
  2160. (let ((obj (git-walktree--resolve-object committish path)))
  2161. (while (not obj)
  2162. (setq path
  2163. (git-walktree--parent-directory path))
  2164. (setq obj
  2165. (git-walktree--resolve-object committish path)))
  2166. (git-walktree--open-noselect committish
  2167. path
  2168. obj)))
  2169. ;; TODO: Store view history
  2170. (defun git-walktree--open-noselect (committish path object)
  2171. "Open buffer to view git object of COMMITTISH:PATH.
  2172. When PATH was given and non-nil open that, otherwise open root tree.
  2173. When OBJECT was given and non-nil, assume that is the object full sha1 of
  2174. COMMITTISH:PATH without checking it."
  2175. (cl-assert committish)
  2176. (let ((type (git-walktree--git-plumbing "cat-file"
  2177. "-t"
  2178. committish)))
  2179. (cl-assert (string= type "commit")))
  2180. (setq path (or path
  2181. "."))
  2182. ;; PATH must not start with and end with slashes
  2183. (cl-assert (not (string-match-p "\\`/" path)))
  2184. (cl-assert (not (string-match-p "/\\'" path)))
  2185. (setq object (or object
  2186. (git-walktree--resolve-object committish path)))
  2187. (setq object (git-walktree--git-plumbing "rev-parse"
  2188. object))
  2189. (cl-assert object)
  2190. (let ((type (git-walktree--git-plumbing "cat-file"
  2191. "-t"
  2192. object)))
  2193. (pcase type
  2194. ((or "commit" "tree")
  2195. (git-walktree--open-treeish committish path object))
  2196. ("blob"
  2197. (git-walktree--open-blob committish path object))
  2198. (_
  2199. (error "Type cannot handle: %s" type)))))
  2200. (defun git-walktree--resolve-object (committish path)
  2201. "Return object full sha1 name of COMMITISIH:PATH.
  2202. If path is equal to \".\" return COMMITTISH's root tree object.
  2203. PATH will be always treated as relative to repository root."
  2204. (cl-assert committish)
  2205. (cl-assert path)
  2206. (cl-assert (not (string-match-p "\\`/" path)))
  2207. (cl-assert (not (string-match-p "/\\'" path)))
  2208. (if (string= path ".")
  2209. (git-walktree--git-plumbing "show"
  2210. "--no-patch"
  2211. "--pretty=format:%T"
  2212. committish)
  2213. (let ((info (git-walktree--parse-lstree-line (git-walktree--git-plumbing "ls-tree"
  2214. "--full-tree"
  2215. committish
  2216. path))))
  2217. (plist-get info :object))))
  2218. (defun git-walktree-open (committish &optional path)
  2219. "Open git tree buffer of COMMITTISH.
  2220. When PATH was given and non-nil open that, otherwise try to open current path.
  2221. If target path is not found in COMMITISH tree, go up path and try again until found."
  2222. (interactive (list (magit-read-branch-or-commit "Revision: ")))
  2223. (switch-to-buffer (git-walktree--open-noselect-safe-path committish path)))
  2224. (defalias 'git-walktree 'git-walktree-open)
  2225. (defun git-walktree--path-in-repository (path)
  2226. "Convert PATH into relative path to repository root.
  2227. Result will not have leading and trailing slashes."
  2228. (with-temp-buffer
  2229. (cd (if (file-directory-p path)
  2230. path
  2231. (file-name-directory path)))
  2232. (let ((root (git-walktree--git-plumbing "rev-parse"
  2233. "--show-toplevel")))
  2234. (file-relative-name (directory-file-name path)
  2235. root))))
  2236. (defcustom git-walktree-git-executable "git"
  2237. "Git executable."
  2238. :type 'string
  2239. :group 'git-walktree)
  2240. (defcustom git-walktree-try-cd t
  2241. "Try to cd if directory exists in current working directory if non-nil.
  2242. Otherwise use repository root for gitwalktree buffer's `default-directory'."
  2243. :type 'boolean
  2244. :group 'git-walktree)
  2245. (defcustom git-walktree-reuse-tree-buffer t
  2246. "Non-nil to reuse buffer for treeish object."
  2247. :type 'boolean
  2248. :group 'git-walktree)
  2249. (defvar git-walktree-tree-buffer-for-reuse nil
  2250. "Buffer to use when `git-walktree-reuse-tree-buffer' is non-nil.")
  2251. (defun git-walktree--git-plumbing (&rest args)
  2252. "Run git plubming command with ARGS.
  2253. Returns first line of output without newline."
  2254. (with-temp-buffer
  2255. (let ((status (apply 'call-process
  2256. git-walktree-git-executable
  2257. nil
  2258. t
  2259. nil
  2260. args)))
  2261. (unless (eq 0
  2262. status)
  2263. (error "Faild to run git %S:\n%s"
  2264. args
  2265. (buffer-substring-no-properties (point-min)
  2266. (point-max))))
  2267. (buffer-substring-no-properties (point-min)
  2268. (progn
  2269. (goto-char (point-min))
  2270. (point-at-eol))))))
  2271. (defconst git-walktree-ls-tree-line-regexp
  2272. "^\\([0-9]\\{6\\}\\) \\(\\w+\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2273. "Regexp for one line of output of git ls-tree.")
  2274. (defconst git-walktree-ls-tree-line-tree-regexp
  2275. "^\\([0-9]\\{6\\}\\) \\(tree\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2276. "Regexp for tree line of output of git ls-tree.")
  2277. (defconst git-walktree-ls-tree-line-commit-regexp
  2278. "^\\([0-9]\\{6\\}\\) \\(commit\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2279. "Regexp for commit line of output of git ls-tree.")
  2280. (defun git-walktree--parse-lstree-line (str)
  2281. "Extract object info from STR.
  2282. STR should be a string like following without newline.:
  2283. 100644 blob 6fd4d58202d0b46547c6fe43de0f8c878456f966 .editorconfig
  2284. Returns property list like (:mode MODE :type TYPE :object OBJECT :file FILE)."
  2285. (let (result mode type object file)
  2286. (save-match-data
  2287. (with-temp-buffer
  2288. (insert str)
  2289. (goto-char (point-min))
  2290. (and (re-search-forward git-walktree-ls-tree-line-regexp
  2291. nil
  2292. t)
  2293. (list :mode (match-string 1)
  2294. :type (match-string 2)
  2295. :object (match-string 3)
  2296. :file (match-string 4)))))))
  2297. (defun git-walktree-mode-open-this ()
  2298. "Open git object of current line."
  2299. (interactive)
  2300. (let ((info (git-walktree--parse-lstree-line (buffer-substring-no-properties (point-at-bol)
  2301. (point-at-eol)))))
  2302. (if info
  2303. (switch-to-buffer
  2304. (if (string= (plist-get info
  2305. :type)
  2306. "commit")
  2307. ;; For submodule cd to that directory and intialize
  2308. ;; TODO: Provide way to go back to known "parent" repository
  2309. (with-temp-buffer
  2310. (cd (plist-get info :file))
  2311. (git-walktree--open-noselect (plist-get info
  2312. :object)
  2313. nil
  2314. (plist-get info
  2315. :object)))
  2316. (git-walktree--open-noselect git-walktree-current-committish
  2317. (git-walktree--join-path (plist-get info
  2318. :file))
  2319. (plist-get info
  2320. :object))))
  2321. (message "No object on current line."))))
  2322. (defun git-walktree--join-path (name &optional base)
  2323. "Make path from NAME and BASE.
  2324. If base is omitted or nil use value of `git-walktree-current-path'."
  2325. (setq base (or base
  2326. git-walktree-current-path))
  2327. (cl-assert base)
  2328. (if (string= base ".")
  2329. name
  2330. (concat base "/" name)))
  2331. (defun git-walktree--parent-directory (path)
  2332. "Return parent directory of PATH without trailing slash.
  2333. For root directory return \".\".
  2334. If PATH is equal to \".\", return nil."
  2335. (if (string-match-p "/" path)
  2336. (directory-file-name (file-name-directory path))
  2337. (if (string= "." path)
  2338. nil
  2339. ".")))
  2340. (defun git-walktree-up (&optional committish path)
  2341. "Open parent directory of COMMITTISH and PATH.
  2342. If not given, value of current buffer will be used."
  2343. (interactive)
  2344. (setq committish
  2345. (or committish git-walktree-current-committish))
  2346. (setq path
  2347. (or path git-walktree-current-path))
  2348. (let ((parent (git-walktree--parent-directory path)))
  2349. (if parent
  2350. (switch-to-buffer (git-walktree--open-noselect committish
  2351. parent
  2352. nil))
  2353. (message "Cannot find parent directory for current tree."))))
  2354. (defun git-walktree-mode--move-to-file ()
  2355. "Move point to file field of ls-tree output in current line.
  2356. This function do nothing when current line is not ls-tree output."
  2357. (interactive)
  2358. (save-match-data
  2359. (when (save-excursion
  2360. (goto-char (point-at-bol))
  2361. (re-search-forward git-walktree-ls-tree-line-regexp
  2362. (point-at-eol) t))
  2363. (goto-char (match-beginning 4)))))
  2364. (defun git-walktree-mode-next-line (&optional arg try-vscroll)
  2365. "Move cursor vertically down ARG lines and move to file field if found."
  2366. (interactive "^p\np")
  2367. (or arg (setq arg 1))
  2368. (line-move arg nil nil try-vscroll)
  2369. (git-walktree-mode--move-to-file)
  2370. )
  2371. (defun git-walktree-mode-previous-line (&optional arg try-vscroll)
  2372. "Move cursor vertically up ARG lines and move to file field if found."
  2373. (interactive "^p\np")
  2374. (or arg (setq arg 1))
  2375. (line-move (- arg) nil nil try-vscroll)
  2376. (git-walktree-mode--move-to-file)
  2377. )
  2378. (defgroup git-walktree-faces nil
  2379. "Faces used by git-walktree."
  2380. :group 'git-walktree
  2381. :group 'faces)
  2382. (defface git-walktree-tree-face
  2383. ;; Same as dired-directory
  2384. '((t (:inherit font-lock-function-name-face)))
  2385. "Face used for tree objects."
  2386. :group 'git-walktree-faces)
  2387. (defface git-walktree-commit-face
  2388. ;; Same as dired-symlink face
  2389. '((t (:inherit font-lock-keyword-face)))
  2390. "Face used for commit objects."
  2391. :group 'git-walktree-faces)
  2392. (defvar git-walktree-known-child-revisions (make-hash-table :test 'equal)
  2393. "Hash of already known pair of commitid -> list of child commitid.
  2394. Both values should be object full sha1 names.")
  2395. (defun git-walktree--put-child (parent child)
  2396. "Register PARENT and CHILD relationship.
  2397. PARENT should be a full sha1 object name."
  2398. ;; Any way to check if PARENT is a full SHA-1 object name?
  2399. (let ((current (gethash parent git-walktree-known-child-revisions)))
  2400. (unless (member child current)
  2401. (puthash parent
  2402. (cons child
  2403. current)
  2404. git-walktree-known-child-revisions))))
  2405. ;; TODO: Add aggressive search mode
  2406. ;; https://stackoverflow.com/a/9870218
  2407. ;; git log --reverse --pretty=format:%H -n 1 --ancestry-path <PARENT>..HEAD
  2408. (defun git-walktree--get-children (parent)
  2409. "Get known children list of PARENT commit.
  2410. PARENT should be a full sha1 object name."
  2411. (gethash parent git-walktree-known-child-revisions))
  2412. (defun git-walktree--choose-committish (prompt-format collection)
  2413. "Emit PROMPT-FORMAT and ask user to which committish of COLLECTION to use.
  2414. When collection has just one element, return the first element without asking."
  2415. (cl-assert collection)
  2416. (if (< (length collection) 2)
  2417. (car collection)
  2418. (completing-read (format prompt-format
  2419. (mapconcat 'git-walktree--committish-fordisplay
  2420. collection
  2421. " "))
  2422. collection
  2423. nil
  2424. t)))
  2425. (defun git-walktree-parent-revision ()
  2426. "Open parent revision of current path.
  2427. If current path was not found in the parent revision try to go up path."
  2428. (interactive)
  2429. (cl-assert git-walktree-current-committish)
  2430. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2431. git-walktree-current-committish))
  2432. (parents (git-walktree--parent-full-sha1 commit-full-sha1)))
  2433. (dolist (parent parents)
  2434. (git-walktree--put-child parent
  2435. commit-full-sha1))
  2436. (if (< (length parents)
  2437. 1)
  2438. (message "This revision has no parent revision")
  2439. (let* ((parent (git-walktree--choose-committish "This revision has multiple parents. Which to open? (%s) "
  2440. parents))
  2441. (path git-walktree-current-path))
  2442. (cl-assert path)
  2443. (switch-to-buffer (git-walktree--open-noselect-safe-path parent
  2444. path))))))
  2445. (defun git-walktree--parent-full-sha1 (committish)
  2446. "Return list of parent commits of COMMITTISH in sha1 string."
  2447. (let ((type (git-walktree--git-plumbing "cat-file"
  2448. "-t"
  2449. committish)))
  2450. (cl-assert (string= type "commit")))
  2451. (let ((parents (git-walktree--git-plumbing "show"
  2452. "--no-patch"
  2453. "--pretty=format:%P"
  2454. committish)))
  2455. (split-string parents)))
  2456. (defun git-walktree-known-child-revision ()
  2457. "Open known revision of current path."
  2458. (interactive)
  2459. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2460. git-walktree-current-committish))
  2461. (children (git-walktree--get-children commit-full-sha1)))
  2462. (if (< (length children)
  2463. 1)
  2464. (message "There are no known child revision")
  2465. (let* ((child (git-walktree--choose-committish "There are multiple known childrens. Which to open? (%s)"
  2466. children))
  2467. (path git-walktree-current-path))
  2468. (cl-assert path)
  2469. (switch-to-buffer (git-walktree--open-noselect-safe-path child
  2470. path))))))
  2471. (defvar git-walktree-mode-map
  2472. (let ((map (make-sparse-keymap)))
  2473. (define-key map "n" 'git-walktree-mode-next-line)
  2474. (define-key map "p" 'git-walktree-mode-previous-line)
  2475. (define-key map (kbd "C-n") 'git-walktree-mode-next-line)
  2476. (define-key map (kbd "C-p") 'git-walktree-mode-previous-line)
  2477. ;; TODO: Review keybind
  2478. (define-key map "P" 'git-walktree-parent-revision)
  2479. (define-key map "N" 'git-walktree-known-child-revision)
  2480. (define-key map "^" 'git-walktree-up)
  2481. ;; TODO: implement
  2482. (define-key map (kbd "DEL") 'git-walktree-back)
  2483. (define-key map (kbd "C-m") 'git-walktree-mode-open-this)
  2484. map))
  2485. (defvar git-walktree-mode-font-lock-keywords
  2486. `(
  2487. (,git-walktree-ls-tree-line-regexp . (
  2488. (1 'shadow)
  2489. (3 'shadow)
  2490. ))
  2491. (,git-walktree-ls-tree-line-tree-regexp . (
  2492. (2 'git-walktree-tree-face)
  2493. (4 'git-walktree-tree-face)
  2494. ))
  2495. (,git-walktree-ls-tree-line-commit-regexp . (
  2496. (2 'git-walktree-commit-face)
  2497. (4 'git-walktree-commit-face)
  2498. ))
  2499. )
  2500. "Syntax highlighting for git-walktree mode.")
  2501. (define-derived-mode git-walktree-mode special-mode "GitWalktree"
  2502. "Major-mode for `git-walktree-open'."
  2503. (set (make-local-variable 'font-lock-defaults)
  2504. '(git-walktree-mode-font-lock-keywords
  2505. nil nil nil nil
  2506. ))
  2507. )
  2508. (require 'magit nil t)
  2509. ;; (git-revision--git-plumbing "cat-file" "-t" "HEAD")
  2510. ;; Local Variables:
  2511. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2512. ;; flycheck-checker: emacs-lisp
  2513. ;; End:
  2514. ;;; emancs.el ends here