No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

3126 líneas
107 KiB

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