You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2663 line
91 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Commentary:
  3. ;;; Code:
  4. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  5. ;; SETUP_LOAD: (and (file-readable-p file)
  6. ;; SETUP_LOAD: (load-file file)))
  7. ;; make directories
  8. (unless (file-directory-p (expand-file-name user-emacs-directory))
  9. (make-directory (expand-file-name user-emacs-directory)))
  10. (let ((d (expand-file-name (concat user-emacs-directory
  11. "lisp"))))
  12. (unless (file-directory-p d)
  13. (make-directory d))
  14. (add-to-list 'load-path d))
  15. (eval-when-compile
  16. ;; TODO: Use cl-lib intead
  17. (require 'cl nil t))
  18. ;; (add-hook 'after-change-major-mode-hook
  19. ;; (lambda ()
  20. ;; (message "cmm: %S %s"
  21. ;; major-mode
  22. ;; buffer-file-name)))
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;; Some macros for internals
  25. (defun call-after-init (func)
  26. "If `after-init-hook' has been run, call FUNC immediately.
  27. Otherwize hook it."
  28. (if after-init-time
  29. (funcall func)
  30. (add-hook 'after-init-hook
  31. func)))
  32. (defmacro defvar-set (symbol value &optional docstring)
  33. "Define SYMBOL as a variable and set to VALUE.
  34. Variable will be defined with DOCSTRING if given, otherwise do not set even
  35. VALUE when defining SYMBOL."
  36. `(set (if ,docstring
  37. (defvar ,symbol
  38. nil
  39. ,docstring)
  40. (defvar ,symbol))
  41. ,value))
  42. (defmacro safe-require-or-eval (feature)
  43. "Require FEATURE if available.
  44. At compile time the feature will be loaded immediately."
  45. `(eval-and-compile
  46. (require ,feature nil t)))
  47. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  48. "Define autoloading FEATURE that defines FUNCTIONS.
  49. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  50. the function same as FEATURE is defined as autoloaded function. BODY is passed
  51. to `eval-after-load'.
  52. After this macro is expanded, this returns the path to library if FEATURE
  53. found, otherwise returns nil."
  54. (let* ((libname (symbol-name (eval feature)))
  55. (libpath (locate-library libname)))
  56. (and libpath
  57. `(progn
  58. ,@(mapcar (lambda (f)
  59. (unless (fboundp f)
  60. `(progn
  61. (message "Autoloaded function `%S' defined (%s)"
  62. (quote ,f)
  63. ,libpath)
  64. (autoload (quote ,f)
  65. ,libname
  66. ,(concat "Autoloaded function defined in \""
  67. libpath
  68. "\".")
  69. t))))
  70. (or (eval functions)
  71. `(,(eval feature))))
  72. (eval-after-load ,feature
  73. (quote (progn
  74. ,@body)))
  75. (locate-library ,libname)))))
  76. (put 'autoload-eval-lazily 'lisp-indent-function 2)
  77. (when (autoload-eval-lazily 'tetris nil
  78. (message "Tetris loaded!"))
  79. (message "Tetris found!"))
  80. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  81. ;; download library from web
  82. (defvar fetch-library-enabled-p t
  83. "Set nil to skip downloading with `fetch-library'.")
  84. (defun fetch-library (url &optional byte-compile-p force-download-p)
  85. "Download a library from URL and locate it in \"~/emacs.d/lisp/\".
  86. Return nil if library unfound and failed to download,
  87. otherwise the path where the library installed.
  88. If BYTE-COMPILE-P is t byte compile the file after downloading.
  89. If FORCE-DOWNLOAD-P it t ignore exisiting library and always download.
  90. This function also checks the value of `fetch-library-enabled-p' and do not
  91. fetch libraries if this value is nil. In this case all arguments (including
  92. FORCE-DOWNLOAD-P) will be ignored."
  93. (let* ((dir (expand-file-name (concat user-emacs-directory "lisp/")))
  94. (lib (file-name-sans-extension (file-name-nondirectory url)))
  95. (lpath (concat dir lib ".el"))
  96. (locate-p (locate-library lib)))
  97. (if (and fetch-library-enabled-p
  98. (or force-download-p
  99. (not locate-p)))
  100. (if (progn (message "Downloading %s..."
  101. url)
  102. (download-file url
  103. lpath
  104. t))
  105. (progn (message "Downloading %s...done"
  106. url)
  107. (when (and byte-compile-p
  108. (require 'bytecomp nil t))
  109. (and (file-exists-p (byte-compile-dest-file lpath))
  110. (delete-file (byte-compile-dest-file lpath)))
  111. (message "Byte-compiling %s..."
  112. lpath)
  113. (byte-compile-file lpath)
  114. (message "Byte-compiling %s...done"
  115. lpath)))
  116. (progn (and (file-writable-p lpath)
  117. (delete-file lpath))
  118. (message "Downloading %s...failed"
  119. url))))
  120. (locate-library lib)))
  121. ;; If EMACS_EL_DRY_RUN is set and it is not an empty string, fetch-library
  122. ;; does not actually fetch library.
  123. (let ((dryrun (getenv "EMACS_EL_DRY_RUN")))
  124. (when (and dryrun
  125. (< 0
  126. (length dryrun)))
  127. (setq fetch-library-enabled-p
  128. nil)
  129. (message "EMACS_EL_DRY_RUN is set. Skip fetching libraries.")))
  130. (defun download-file (url path &optional ok-if-already-exists)
  131. "Download file from URL and output to PATH.
  132. IF OK-IF-ALREADY-EXISTS is true force download."
  133. (let ((curl (executable-find "curl"))
  134. (wget (executable-find "wget")))
  135. (cond (wget
  136. (if (and (not ok-if-already-exists)
  137. (file-exists-p path))
  138. nil
  139. (and (eq 0
  140. (call-process wget
  141. nil
  142. nil
  143. nil
  144. "-O"
  145. path
  146. url
  147. ))
  148. path)))
  149. (curl
  150. (if (and (not ok-if-already-exists)
  151. (file-exists-p path))
  152. nil
  153. (and (eq 0
  154. (call-process curl
  155. nil
  156. nil
  157. nil
  158. "--output"
  159. path
  160. "-L"
  161. url
  162. ))
  163. path)))
  164. (t
  165. (ignore-errors
  166. (require 'url)
  167. (url-copy-file url
  168. path
  169. ok-if-already-exists)
  170. path)))))
  171. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  172. ;; package
  173. (defvar-set my-package-list
  174. '(
  175. markdown-mode
  176. yaml-mode
  177. gnuplot-mode
  178. erlang
  179. js2-mode
  180. git-commit
  181. gitignore-mode
  182. ;; ack
  183. color-moccur
  184. ggtags
  185. flycheck
  186. ;; is flymake installs are required?
  187. ;;flymake-jshint
  188. ;;flymake-python-pyflakes
  189. xclip
  190. foreign-regexp
  191. multi-term
  192. dirtree
  193. term-run
  194. editorconfig
  195. git-ps1-mode
  196. restart-emacs
  197. fill-column-indicator
  198. scala-mode2
  199. ensime
  200. editorconfig
  201. git-command
  202. ;; 10sr repository
  203. terminal-title
  204. recentf-show
  205. dired-list-all-mode
  206. pack
  207. set-modeline-color
  208. read-only-only-mode
  209. smart-revert
  210. autosave
  211. ;;window-organizer
  212. remember-major-modes-mode
  213. ilookup
  214. pasteboard
  215. )
  216. "Package list just for me.")
  217. (when (safe-require-or-eval 'package)
  218. ;; (add-to-list 'package-archives
  219. ;; '("ELPA" . "http://tromey.com/elpa/"))
  220. (setq package-archives
  221. `(,@package-archives
  222. ("melpa" . "https://melpa.org/packages/")
  223. ("10sr-el" . "https://10sr.github.io/emacs-lisp/p/")))
  224. ;; (add-to-list 'package-archives
  225. ;; '("melpa" . "https://melpa.org/packages/")
  226. ;; t)
  227. ;; (add-to-list 'package-archives
  228. ;; '("marmalade" . "http://marmalade-repo.org/packages/"))
  229. (package-initialize)
  230. (defun my-auto-install-package ()
  231. "Install packages semi-automatically."
  232. (interactive)
  233. (package-refresh-contents)
  234. (mapc (lambda (pkg)
  235. (or (package-installed-p pkg)
  236. (locate-library (symbol-name pkg))
  237. (package-install pkg)))
  238. my-package-list))
  239. )
  240. ;; (lazy-load-eval 'sudoku)
  241. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  242. ;; my-idle-hook
  243. (defvar my-idle-hook nil
  244. "Hook run when idle for several secs.")
  245. (defvar my-idle-hook-sec 5
  246. "Second to run `my-idle-hook'.")
  247. (run-with-idle-timer my-idle-hook-sec
  248. t
  249. (lambda ()
  250. (run-hooks 'my-idle-hook)))
  251. ;; (add-hook 'my-idle-hook
  252. ;; (lambda ()
  253. ;; (message "idle hook message")))
  254. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  255. ;; start and quit
  256. (setq inhibit-startup-message t)
  257. (setq confirm-kill-emacs 'y-or-n-p)
  258. (setq gc-cons-threshold (* 1024 1024 4))
  259. (when window-system
  260. (add-to-list 'default-frame-alist '(cursor-type . box))
  261. (add-to-list 'default-frame-alist '(background-color . "white"))
  262. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  263. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  264. ;; does not work?
  265. )
  266. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  267. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  268. (and (fboundp 'tool-bar-mode)
  269. (tool-bar-mode 0))
  270. (and (fboundp 'set-scroll-bar-mode)
  271. (set-scroll-bar-mode nil))
  272. (add-hook 'kill-emacs-hook
  273. ;; load init file when terminating emacs to ensure file is not broken
  274. 'reload-init-file)
  275. (defun my-force-kill-emacs ()
  276. "My force kill Emacs."
  277. (interactive)
  278. (let ((kill-emacs-hook nil))
  279. (kill-emacs)))
  280. (call-after-init
  281. (lambda ()
  282. (message "%s %s" invocation-name emacs-version)
  283. (message "%s was taken to initialize emacs." (emacs-init-time))
  284. (switch-to-buffer "*Messages*")))
  285. (cd ".") ; when using windows use / instead of \ in `default-directory'
  286. ;; locale
  287. (set-language-environment "Japanese")
  288. (set-default-coding-systems 'utf-8-unix)
  289. (prefer-coding-system 'utf-8-unix)
  290. (setq system-time-locale "C")
  291. ;; my prefix map
  292. (defvar my-prefix-map nil
  293. "My prefix map.")
  294. (define-prefix-command 'my-prefix-map)
  295. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  296. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  297. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  298. ;; (comint-show-maximum-output)
  299. ;; kill scratch
  300. (call-after-init (lambda ()
  301. (let ((buf (get-buffer "*scratch*")))
  302. (when buf
  303. (kill-buffer buf)))))
  304. ;; modifier keys
  305. ;; (setq mac-option-modifier 'control)
  306. ;; display
  307. (setq visible-bell t)
  308. (setq ring-bell-function 'ignore)
  309. (mouse-avoidance-mode 'banish)
  310. (defun reload-init-file ()
  311. "Reload Emacs init file."
  312. (interactive)
  313. (when (and user-init-file
  314. (file-readable-p user-init-file))
  315. (load-file user-init-file)))
  316. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  317. ;; for windows
  318. (defun start-ckw-bash ()
  319. "Start ckw in windows."
  320. (interactive)
  321. (start-process
  322. "ckw_bash"
  323. nil
  324. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe"))
  325. ;; command seems to have to be in c drive
  326. (defun my-w32-add-export-path (&rest args)
  327. "Add pathes ARGS for windows."
  328. (mapc (lambda (path)
  329. (add-to-list 'exec-path (expand-file-name path)))
  330. (reverse args))
  331. (setenv "PATH"
  332. (mapconcat 'convert-standard-filename
  333. exec-path
  334. ";")))
  335. (when (eq system-type 'windows-nt)
  336. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  337. ;; (setq python-python-command "c:/Python26/python.exe")
  338. ;; (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  339. (my-w32-add-export-path "c:/Windows/system"
  340. "c:/Windows/System32"
  341. "c:/Program Files/Git/bin"
  342. "c:/MinGW/bin"
  343. "c:/MinGW/mingw32/bin"
  344. (expand-file-name "~/.local/bin")
  345. (expand-file-name "~/dbx/apps/bin"))
  346. (when window-system
  347. (defvar-set w32-enable-synthesized-fonts t))
  348. (defvar-set w32-apps-modifier 'meta)
  349. (setq file-name-coding-system 'sjis))
  350. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  351. ;; global keys
  352. (global-set-key (kbd "<up>") 'scroll-down-line)
  353. (global-set-key (kbd "<down>") 'scroll-up-line)
  354. (global-set-key (kbd "<left>") 'scroll-down)
  355. (global-set-key (kbd "<right>") 'scroll-up)
  356. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  357. (global-set-key (kbd "C-\\") help-map)
  358. (define-key ctl-x-map (kbd "DEL") help-map)
  359. (define-key ctl-x-map (kbd "C-h") help-map)
  360. (define-key help-map "a" 'apropos)
  361. ;; disable annoying keys
  362. (global-set-key [prior] 'ignore)
  363. (global-set-key (kbd "<next>") 'ignore)
  364. (global-set-key [menu] 'ignore)
  365. (global-set-key [down-mouse-1] 'ignore)
  366. (global-set-key [down-mouse-2] 'ignore)
  367. (global-set-key [down-mouse-3] 'ignore)
  368. (global-set-key [mouse-1] 'ignore)
  369. (global-set-key [mouse-2] 'ignore)
  370. (global-set-key [mouse-3] 'ignore)
  371. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  372. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  373. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  374. ;; title and mode-line
  375. (when (safe-require-or-eval 'terminal-title)
  376. ;; if TERM is not screen use default value
  377. (if (getenv "TMUX")
  378. ;; if use tmux locally just basename of current dir
  379. (setq terminal-title-format
  380. '((file-name-nondirectory (directory-file-name
  381. default-directory))))
  382. (if (and (let ((tty-type (frame-parameter nil
  383. 'tty-type)))
  384. (and tty-type
  385. (equal (car (split-string tty-type
  386. "-"))
  387. "screen")))
  388. (not (getenv "SSH_CONNECTION")))
  389. (setq terminal-title-format
  390. '((file-name-nondirectory (directory-file-name
  391. default-directory))))
  392. ;; seems that TMUX is used locally and ssh to remote host
  393. (setq terminal-title-format
  394. `("em:"
  395. ,user-login-name
  396. "@"
  397. ,(car (split-string system-name
  398. "\\."))
  399. ":"
  400. default-directory))
  401. )
  402. )
  403. (terminal-title-mode))
  404. (setq eol-mnemonic-dos "\\r\\n")
  405. (setq eol-mnemonic-mac "\\r")
  406. (setq eol-mnemonic-unix "\\n")
  407. (which-function-mode 0)
  408. (line-number-mode 0)
  409. (column-number-mode 0)
  410. (size-indication-mode 0)
  411. (setq mode-line-position
  412. '(:eval (format "L%%l/%d,C%%c"
  413. (count-lines (point-max)
  414. (point-min)))))
  415. (when (require 'git-ps1-mode nil t)
  416. (git-ps1-mode))
  417. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  418. ;; display date
  419. (call-after-init (lambda ()
  420. (when display-time-mode
  421. (display-time-update))))
  422. (when (safe-require-or-eval 'time)
  423. (setq display-time-interval 29)
  424. (setq display-time-day-and-date t)
  425. (setq display-time-format "%a, %d %b %Y %T")
  426. (if window-system
  427. (display-time-mode 0)
  428. (display-time-mode 1)))
  429. ;; ;; current directory
  430. ;; (let ((ls (member 'mode-line-buffer-identification
  431. ;; mode-line-format)))
  432. ;; (setcdr ls
  433. ;; (cons '(:eval (concat " ("
  434. ;; (abbreviate-file-name default-directory)
  435. ;; ")"))
  436. ;; (cdr ls))))
  437. ;; ;; display last modified time
  438. ;; (let ((ls (member 'mode-line-buffer-identification
  439. ;; mode-line-format)))
  440. ;; (setcdr ls
  441. ;; (cons '(:eval (concat " "
  442. ;; my-buffer-file-last-modified-time))
  443. ;; (cdr ls))))
  444. (defun buffer-list-not-start-with-space ()
  445. "Return a list of buffers that not start with whitespaces."
  446. (let ((bl (buffer-list))
  447. b nbl)
  448. (while bl
  449. (setq b (pop bl))
  450. (unless (string-equal " "
  451. (substring (buffer-name b)
  452. 0
  453. 1))
  454. (add-to-list 'nbl b)))
  455. nbl))
  456. ;; http://www.masteringemacs.org/articles/2012/09/10/hiding-replacing-modeline-strings/
  457. ;; (add-to-list 'minor-mode-alist
  458. ;; '(global-whitespace-mode ""))
  459. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  460. ;; system info
  461. (defun my-message-current-info ()
  462. "Echo current login name, hostname and directory."
  463. (interactive)
  464. (message "%s@%s:%s"
  465. user-login-name
  466. system-name
  467. (abbreviate-file-name default-directory)))
  468. ;; (run-with-idle-timer 3
  469. ;; t
  470. ;; 'my-message-current-info)
  471. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  472. ;; minibuffer
  473. (setq insert-default-directory t)
  474. (setq completion-ignore-case t
  475. read-file-name-completion-ignore-case t
  476. read-buffer-completion-ignore-case t)
  477. (setq resize-mini-windows t)
  478. (temp-buffer-resize-mode 1)
  479. (savehist-mode 1)
  480. (fset 'yes-or-no-p 'y-or-n-p)
  481. ;; complete symbol when `eval'
  482. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  483. (define-key minibuffer-local-map (kbd "C-u")
  484. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  485. ;; I dont know these bindings are good
  486. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  487. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  488. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  489. ;; letters, font-lock mode and fonts
  490. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  491. ;; (set-window-margins (selected-window) 1 1)
  492. (and (or (eq system-type 'Darwin)
  493. (eq system-type 'darwin))
  494. (fboundp 'mac-set-input-method-parameter)
  495. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  496. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  497. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  498. (boundp 'input-method-inactivate-hook))
  499. (add-hook 'input-method-activate-hook
  500. (lambda () (set-cursor-color "red")))
  501. (add-hook 'input-method-inactivate-hook
  502. (lambda () (set-cursor-color "black"))))
  503. (when (safe-require-or-eval 'paren)
  504. (show-paren-mode 1)
  505. (setq show-paren-delay 0.5
  506. show-paren-style 'parenthesis) ; mixed is hard to read
  507. ;; (set-face-background 'show-paren-match
  508. ;; "black")
  509. ;; ;; (face-foreground 'default))
  510. ;; (set-face-foreground 'show-paren-match
  511. ;; "white")
  512. ;; (set-face-inverse-video-p 'show-paren-match
  513. ;; t)
  514. )
  515. (transient-mark-mode 1)
  516. (global-font-lock-mode 1)
  517. (setq font-lock-global-modes
  518. '(not
  519. help-mode
  520. eshell-mode
  521. ;;term-mode
  522. Man-mode))
  523. ;; (standard-display-ascii ?\n "$\n")
  524. (defvar my-eol-face
  525. '(("\n" . (0 font-lock-comment-face t nil)))
  526. )
  527. (defvar my-tab-face
  528. '(("\t" . '(0 highlight t nil))))
  529. (defvar my-jspace-face
  530. '(("\u3000" . '(0 highlight t nil))))
  531. (add-hook 'font-lock-mode-hook
  532. (lambda ()
  533. ;; (font-lock-add-keywords nil my-eol-face)
  534. (font-lock-add-keywords nil my-jspace-face)
  535. ))
  536. (when (safe-require-or-eval 'whitespace)
  537. (add-to-list 'whitespace-display-mappings ; not work
  538. `(tab-mark ?\t ,(vconcat "^I\t")))
  539. (add-to-list 'whitespace-display-mappings
  540. `(newline-mark ?\n ,(vconcat "$\n")))
  541. (setq whitespace-style '(face
  542. trailing ; trailing blanks
  543. newline ; newlines
  544. newline-mark ; use display table for newline
  545. ;; tab-mark
  546. empty ; empty lines at beg or end of buffer
  547. lines-tail ; lines over 80
  548. ))
  549. ;; (setq whitespace-newline 'font-lock-comment-face)
  550. (global-whitespace-mode t)
  551. (if (eq (display-color-cells)
  552. 256)
  553. (set-face-foreground 'whitespace-newline "brightblack")
  554. ;; (progn
  555. ;; (set-face-bold-p 'whitespace-newline
  556. ;; t))
  557. ))
  558. (and nil
  559. (safe-require-or-eval 'fill-column-indicator)
  560. (setq fill-column-indicator))
  561. ;; highlight current line
  562. ;; http://wiki.riywo.com/index.php?Meadow
  563. (defface my-hl-line
  564. '((((min-colors 256)
  565. (background dark))
  566. (:background "color-234"))
  567. (((min-colors 256)
  568. (background light))
  569. (:background "color-234"))
  570. (t
  571. (:underline "black")))
  572. "*Face used by hl-line.")
  573. (defvar-set hl-line-face 'my-hl-line) ;; (setq hl-line-face nil)
  574. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  575. (defvar-set hl-line-global-modes
  576. '(not
  577. term-mode))
  578. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  579. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  580. ;; fonts
  581. (defun my-set-ascii-and-jp-font (list)
  582. "Set font configuration to LIST."
  583. (let ((fspec1 (if (> emacs-major-version 22)
  584. ;; font spec is available in emacs23 and later
  585. (font-spec :family (nth 2 list) :size (nth 3 list))
  586. (cons (nth 2 list) "jisx0208.*")))
  587. (fspec2 (if (> emacs-major-version 22)
  588. (font-spec :family (nth 2 list) :size (nth 3 list))
  589. (cons (nth 2 list) "jisx0201.*"))))
  590. (set-face-attribute 'default nil
  591. :family (nth 0 list)
  592. :height (nth 1 list))
  593. (set-fontset-font "fontset-default"
  594. 'japanese-jisx0208
  595. fspec1)
  596. (set-fontset-font "fontset-default"
  597. 'katakana-jisx0201
  598. fspec2)))
  599. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 90 "takaogothic" 13))
  600. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "takaogothic" 14))
  601. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "ms gothic" 14))
  602. ;; (my-set-ascii-and-jp-font '("monaco" 75 "takaogothic" 11))
  603. ;; (my-set-ascii-and-jp-font '("monaco" 90 "takaogothic" 13))
  604. ;; (my-set-ascii-and-jp-font '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  605. ;; あ a
  606. (safe-require-or-eval 'set-modeline-color)
  607. (let ((fg (face-foreground 'default))
  608. (bg (face-background 'default)))
  609. (set-face-background 'mode-line-inactive
  610. (if (face-inverse-video-p 'mode-line) fg bg))
  611. (set-face-foreground 'mode-line-inactive
  612. (if (face-inverse-video-p 'mode-line) bg fg)))
  613. (set-face-underline 'mode-line-inactive
  614. t)
  615. (set-face-underline 'vertical-border
  616. nil)
  617. ;; Not found in MELPA nor any other package repositories
  618. (and (fetch-library
  619. "https://raw.github.com/tarao/elisp/master/end-mark.el"
  620. t)
  621. (safe-require-or-eval 'end-mark)
  622. (global-end-mark-mode))
  623. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  624. ;; file handling
  625. (when (safe-require-or-eval 'editorconfig)
  626. (editorconfig-mode 1))
  627. (setq revert-without-query '(".+"))
  628. ;; save cursor position
  629. (when (safe-require-or-eval 'saveplace)
  630. (setq-default save-place t)
  631. (setq save-place-file (concat user-emacs-directory
  632. "places")))
  633. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  634. (setq make-backup-files t)
  635. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  636. (setq backup-directory-alist
  637. (cons (cons "\\.*$" (expand-file-name (concat user-emacs-directory
  638. "backup")))
  639. backup-directory-alist))
  640. (setq version-control 'never)
  641. (setq delete-old-versions t)
  642. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  643. "auto-save/")))
  644. (setq delete-auto-save-files t)
  645. (add-to-list 'completion-ignored-extensions ".bak")
  646. ;; (setq delete-by-moving-to-trash t
  647. ;; trash-directory "~/.emacs.d/trash")
  648. (add-hook 'after-save-hook
  649. 'executable-make-buffer-file-executable-if-script-p)
  650. (defvar-set bookmark-default-file (concat user-emacs-directory
  651. "bmk"))
  652. (add-hook 'recentf-load-hook
  653. (lambda ()
  654. (defvar recentf-exclude)
  655. (add-to-list 'recentf-exclude
  656. (regexp-quote bookmark-default-file))))
  657. (when (safe-require-or-eval 'smart-revert)
  658. (smart-revert-on))
  659. ;; autosave
  660. (when (safe-require-or-eval 'autosave)
  661. (autosave-set 2))
  662. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  663. ;; editting
  664. (defun my-copy-whole-line ()
  665. "Copy whole line."
  666. (interactive)
  667. (kill-new (concat (buffer-substring (point-at-bol)
  668. (point-at-eol))
  669. "\n")))
  670. (setq require-final-newline t)
  671. (setq kill-whole-line t)
  672. (setq scroll-conservatively 35
  673. scroll-margin 2
  674. scroll-step 0)
  675. (setq-default major-mode 'text-mode)
  676. (setq next-line-add-newlines nil)
  677. (setq kill-read-only-ok t)
  678. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  679. ;; (setq-default line-spacing 0.2)
  680. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  681. (setq-default tab-width 4)
  682. (setq-default indent-tabs-mode nil)
  683. (setq-default indent-line-function nil)
  684. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  685. (delete-selection-mode 1)
  686. (cua-mode 0)
  687. (setq line-move-visual nil)
  688. ;; key bindings
  689. ;; moving around
  690. ;; (global-set-key (kbd "M-j") 'next-line)
  691. ;; (global-set-key (kbd "M-k") 'previous-line)
  692. ;; (global-set-key (kbd "M-h") 'backward-char)
  693. ;; (global-set-key (kbd "M-l") 'forward-char)
  694. ;;(keyboard-translate ?\M-j ?\C-j)
  695. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  696. (define-key esc-map "p" 'backward-paragraph)
  697. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  698. (define-key esc-map "n" 'forward-paragraph)
  699. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  700. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  701. (global-set-key (kbd "C-<left>") 'scroll-down)
  702. (global-set-key (kbd "C-<right>") 'scroll-up)
  703. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  704. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  705. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  706. ;; C-h and DEL
  707. (global-set-key (kbd "C-h") (kbd "DEL"))
  708. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  709. (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  710. (define-key esc-map "k" 'my-copy-whole-line)
  711. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  712. (define-key esc-map "u" 'undo)
  713. (define-key esc-map "i" (kbd "ESC TAB"))
  714. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  715. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  716. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  717. (define-key my-prefix-map (kbd "C-o") 'occur)
  718. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  719. ;; japanese input method
  720. (defun my-load-scim ()
  721. "Use scim-bridge.el as japanese im."
  722. ;; Load scim-bridge.
  723. (when (safe-require-or-eval 'scim-bridge)
  724. ;; Turn on scim-mode automatically after loading .emacs
  725. (call-after-init 'scim-mode-on)
  726. (setq scim-cursor-color "red")
  727. (scim-define-preedit-key ?\^h t)
  728. (scim-define-common-key ?\* nil)
  729. (scim-define-common-key ?\^/ nil)))
  730. (defun my-load-anthy ()
  731. "Use anthy.el as japanese im."
  732. ;; anthy
  733. (when (safe-require-or-eval 'anthy)
  734. (global-set-key
  735. (kbd "<muhenkan>") (lambda () (interactive) (anthy-mode-off)))
  736. (global-set-key (kbd "<henkan>") (lambda () (interactive) (anthy-mode-on)))
  737. (when (>= emacs-major-version 23)
  738. (setq anthy-accept-timeout 1))))
  739. ;; quail
  740. ;; aproposs input-method for some information
  741. ;; (setq default-input-method "japanese")
  742. (defun my-load-mozc-el ()
  743. "Use mozc.el as japanese im."
  744. (when (safe-require-or-eval 'mozc)
  745. (setq defauit-input-method "japanese-mozc")
  746. (setq mozc-leim-title "[MZ]")
  747. ))
  748. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  749. ;; gmail
  750. (setq mail-interactive t
  751. send-mail-function 'smtpmail-send-it
  752. ;; message-send-mail-function 'smtpmail-send-it
  753. smtpmail-smtp-server "smtp.gmail.com"
  754. smtpmail-smtp-service 587
  755. smtpmail-starttls-credentials '(("smtp.gmail.com" 587
  756. "8.slashes@gmail.com" nil))
  757. smtpmail-auth-credentials '(("smtp.gmail.com" 587
  758. "8.slashes@gmail.com" nil))
  759. user-mail-address "8.slashes@gmail.com")
  760. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  761. ;; buffer killing
  762. ;; (defun my-delete-window-killing-buffer () nil)
  763. (defun my-query-kill-current-buffer ()
  764. "Interactively kill current buffer."
  765. (interactive)
  766. (if (y-or-n-p (concat "kill current buffer? :"))
  767. (kill-buffer (current-buffer))))
  768. (substitute-key-definition 'kill-buffer
  769. 'my-query-kill-current-buffer
  770. global-map)
  771. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  772. (defun my-kill-buffers ()
  773. "Kill buffers that visit files."
  774. (interactive)
  775. (mapcar (lambda (buf)
  776. (when (buffer-file-name buf)
  777. (kill-buffer buf)))
  778. (buffer-list)))
  779. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  780. ;; share clipboard with x
  781. ;; this page describes this in details, but only these sexps seem to be needed
  782. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  783. (and (not window-system)
  784. (not (eq window-system 'mac))
  785. (getenv "DISPLAY")
  786. (not (equal (getenv "DISPLAY") ""))
  787. (executable-find "xclip")
  788. ;; (< emacs-major-version 24)
  789. (safe-require-or-eval 'xclip)
  790. nil
  791. (turn-on-xclip))
  792. (and (eq system-type 'darwin)
  793. (safe-require-or-eval 'pasteboard)
  794. (turn-on-pasteboard)
  795. (getenv "TMUX")
  796. (pasteboard-enable-rtun))
  797. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  798. ;; https://github.com/lunaryorn/flycheck
  799. (when (safe-require-or-eval 'flycheck)
  800. (call-after-init 'global-flycheck-mode))
  801. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  802. ;; window
  803. ;; (when (autoload-eval-lazily 'window-organizer)
  804. ;; (define-key ctl-x-map (kbd "w") 'window-organizer))
  805. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  806. ;; server
  807. (when (safe-require-or-eval 'server)
  808. (setq server-name (concat "server"
  809. (number-to-string (emacs-pid))))
  810. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  811. ;; In Darwin environment, init fails with message like 'Service name too long'
  812. ;; when server-use-tcp is nil
  813. (when (or (eq system-type
  814. 'cygwin)
  815. (eq system-type
  816. 'darwin))
  817. (setq server-use-tcp t))
  818. (defun my-construct-emacsclient-editor-command ()
  819. "Construct and return command in a string to connect to current Emacs server."
  820. (if server-use-tcp
  821. (format "%s -f \"%s/%s\""
  822. "emacsclient"
  823. (expand-file-name server-auth-dir)
  824. server-name)
  825. (format "%s -s \"%s/%s\""
  826. "emacsclient"
  827. server-socket-dir
  828. server-name)))
  829. (setq process-environment
  830. `(,(concat "EDITOR="
  831. (my-construct-emacsclient-editor-command))
  832. ,(concat "GIT_EDITOR="
  833. (my-construct-emacsclient-editor-command))
  834. ,@process-environment))
  835. (server-start))
  836. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  837. ;; some modes and hooks
  838. (defvar-set ac-ignore-case nil)
  839. ;; (when (require 'ensime nil t)
  840. ;; (defvar-set ensime-ac-case-sensitive t)
  841. ;; (defvar-set ensime-company-case-sensitive t)
  842. ;; (add-hook 'scala-mode-hook
  843. ;; 'ensime-scala-mode-hook)
  844. ;; (add-hook 'ensime-scala-mode-hook
  845. ;; 'ac-stop))
  846. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  847. (define-key ctl-x-map "t" 'term-run-shell-command))
  848. (add-to-list 'safe-local-variable-values
  849. '(encoding utf-8))
  850. (setq enable-local-variables :safe)
  851. (when (autoload-eval-lazily 'dirtree nil
  852. (defun my-dirtree-current-line-directory-p ()
  853. "Return nil if element on current line is not a directory."
  854. (file-directory-p (widget-get (tree-mode-button-current-line)
  855. :file)))
  856. ;; This fix is actually a little strange. Strictly speaking
  857. ;; judging tree should be done by whether the widget is a tree one.
  858. (defun my-dirtree-next-node (arg)
  859. "Fix the problem that `tree-mode-next-node' moves cursor 2 lines."
  860. (interactive "p")
  861. (if (my-dirtree-current-line-directory-p)
  862. (widget-forward (* arg 2))
  863. (widget-forward arg)))
  864. (defun my-dirtree-previous-node (arg)
  865. "Fix the problem that `tree-mode-previous-node' moves cursor 2 lines."
  866. (interactive "p")
  867. (my-dirtree-next-node (- arg)))
  868. (define-key dirtree-mode-map "n" 'my-dirtree-next-node)
  869. (define-key dirtree-mode-map "p" 'my-dirtree-previous-node))
  870. (define-key ctl-x-map "d" 'dirtree))
  871. (when (safe-require-or-eval 'remember-major-modes-mode)
  872. (remember-major-modes-mode 1))
  873. ;; Detect file type from shebang and set major-mode.
  874. (add-to-list 'interpreter-mode-alist
  875. '("python3" . python-mode))
  876. (add-to-list 'interpreter-mode-alist
  877. '("python2" . python-mode))
  878. ;; http://fukuyama.co/foreign-regexp
  879. '(and (safe-require-or-eval 'foreign-regexp)
  880. (progn
  881. (setq foreign-regexp/regexp-type 'perl)
  882. '(setq reb-re-syntax 'foreign-regexp)
  883. ))
  884. (safe-require-or-eval 'session)
  885. (autoload-eval-lazily 'sql '(sql-mode)
  886. (safe-require-or-eval 'sql-indent))
  887. (when (autoload-eval-lazily 'git-command)
  888. (define-key ctl-x-map "g" 'git-command))
  889. (and (fetch-library
  890. "http://www.emacswiki.org/emacs/download/sl.el"
  891. t)
  892. (autoload-eval-lazily 'sl))
  893. (defalias 'qcalc 'quick-calc)
  894. (safe-require-or-eval 'simple)
  895. (add-hook 'makefile-mode-hook
  896. (lambda ()
  897. (local-set-key (kbd "C-m") 'newline-and-indent)
  898. ;; this functions is set in write-file-functions, i cannot find any
  899. ;; good way to remove this.
  900. (fset 'makefile-warn-suspicious-lines 'ignore)
  901. ))
  902. (add-hook 'verilog-mode-hook
  903. (lambda ()
  904. (local-set-key ";" 'self-insert-command)))
  905. (setq diff-switches "-u")
  906. (add-hook 'diff-mode-hook
  907. (lambda ()
  908. ;; (when (and (eq major-mode
  909. ;; 'diff-mode)
  910. ;; (not buffer-file-name))
  911. ;; ;; do not pass when major-mode is derived mode of diff-mode
  912. ;; (view-mode 1))
  913. (set-face-attribute 'diff-header nil
  914. :foreground nil
  915. :background nil
  916. :weight 'bold)
  917. (set-face-attribute 'diff-file-header nil
  918. :foreground nil
  919. :background nil
  920. :weight 'bold)
  921. (set-face-foreground 'diff-index-face "blue")
  922. (set-face-attribute 'diff-hunk-header nil
  923. :foreground "cyan"
  924. :weight 'normal)
  925. (set-face-attribute 'diff-context nil
  926. ;; :foreground "white"
  927. :foreground nil
  928. :weight 'normal)
  929. (set-face-foreground 'diff-removed-face "red")
  930. (set-face-foreground 'diff-added-face "green")
  931. (set-face-background 'diff-removed-face nil)
  932. (set-face-background 'diff-added-face nil)
  933. (set-face-attribute 'diff-changed nil
  934. :foreground "magenta"
  935. :weight 'normal)
  936. (set-face-attribute 'diff-refine-change nil
  937. :foreground nil
  938. :background nil
  939. :weight 'bold
  940. :inverse-video t)
  941. ;; Annoying !
  942. ;;(diff-auto-refine-mode)
  943. ))
  944. ;; (ffap-bindings)
  945. (add-hook 'sh-mode-hook
  946. (lambda ()
  947. (local-set-key
  948. (kbd "C-x C-e")
  949. 'my-execute-shell-command-current-line)))
  950. (defvar-set sh-here-document-word "__EOC__")
  951. (defun my-execute-shell-command-current-line ()
  952. "Run current line as shell command."
  953. (interactive)
  954. (shell-command (buffer-substring-no-properties (point-at-bol)
  955. (point))))
  956. (setq auto-mode-alist
  957. `(("autostart\\'" . sh-mode)
  958. ("xinitrc\\'" . sh-mode)
  959. ("xprograms\\'" . sh-mode)
  960. ("PKGBUILD\\'" . sh-mode)
  961. ,@auto-mode-alist))
  962. (and (autoload-eval-lazily 'pkgbuild-mode)
  963. (setq auto-mode-alist (append '(("PKGBUILD\\'" . pkgbuild-mode))
  964. auto-mode-alist)))
  965. (and (autoload-eval-lazily 'groovy-mode)
  966. (add-to-list 'auto-mode-alist
  967. '("build.gradle\\'" . groovy-mode)))
  968. (add-hook 'yaml-mode-hook
  969. (lambda ()
  970. (local-set-key(kbd "C-m") 'newline)))
  971. (add-hook 'html-mode-hook
  972. (lambda ()
  973. (local-set-key(kbd "C-m") 'reindent-then-newline-and-indent)))
  974. (add-hook 'text-mode-hook
  975. (lambda ()
  976. (local-set-key (kbd "C-m") 'newline)))
  977. (add-to-list 'Info-default-directory-list
  978. (expand-file-name "~/.info/emacs-ja"))
  979. (add-hook 'apropos-mode-hook
  980. (lambda ()
  981. (local-set-key "n" 'next-line)
  982. (local-set-key "p" 'previous-line)
  983. ))
  984. (add-hook 'isearch-mode-hook
  985. (lambda ()
  986. ;; (define-key isearch-mode-map
  987. ;; (kbd "C-j") 'isearch-other-control-char)
  988. ;; (define-key isearch-mode-map
  989. ;; (kbd "C-k") 'isearch-other-control-char)
  990. ;; (define-key isearch-mode-map
  991. ;; (kbd "C-h") 'isearch-other-control-char)
  992. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  993. (define-key isearch-mode-map (kbd "M-r")
  994. 'isearch-query-replace-regexp)))
  995. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  996. (setq lazy-highlight-cleanup nil)
  997. ;; face for isearch highlighing
  998. (set-face-attribute 'lazy-highlight
  999. nil
  1000. :foreground `unspecified
  1001. :background `unspecified
  1002. :underline t
  1003. ;; :weight `bold
  1004. )
  1005. (add-hook 'outline-mode-hook
  1006. (lambda ()
  1007. (if (string-match "\\.md\\'" buffer-file-name)
  1008. (set (make-local-variable 'outline-regexp) "#+ "))))
  1009. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1010. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1011. (when (autoload-eval-lazily 'markdown-mode
  1012. '(markdown-mode gfm-mode))
  1013. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1014. (setq markdown-command (or (executable-find "markdown")
  1015. (executable-find "markdown.pl")))
  1016. (add-hook 'markdown-mode-hook
  1017. (lambda ()
  1018. (outline-minor-mode 1)
  1019. (flyspell-mode)
  1020. (set (make-local-variable 'comment-start) ";")))
  1021. (add-hook 'gfm-mode-hook
  1022. (lambda ()
  1023. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1024. ;;(electric-indent-mode 0)
  1025. )))
  1026. ;; c-mode
  1027. ;; http://www.emacswiki.org/emacs/IndentingC
  1028. ;; http://en.wikipedia.org/wiki/Indent_style
  1029. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1030. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1031. (when (autoload-eval-lazily 'cc-vars
  1032. nil
  1033. (defvar c-default-style)
  1034. (add-to-list 'c-default-style
  1035. '(c-mode . "k&r"))
  1036. (add-to-list 'c-default-style
  1037. '(c++-mode . "k&r"))
  1038. (add-hook 'c-mode-common-hook
  1039. (lambda ()
  1040. ;; why c-basic-offset in k&r style defaults to 5 ???
  1041. (defvar-set c-basic-offset 4)
  1042. (defvar-set indent-tabs-mode nil)
  1043. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  1044. (c-toggle-hungry-state -1)
  1045. ;; (and (require 'gtags nil t)
  1046. ;; (gtags-mode 1))
  1047. ))))
  1048. (when (autoload-eval-lazily 'php-mode)
  1049. (add-hook 'php-mode-hook
  1050. (lambda ()
  1051. (setq c-basic-offset 2))))
  1052. (when (autoload-eval-lazily 'js2-mode)
  1053. ;; currently do not use js2-mode
  1054. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1055. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1056. (add-hook 'js2-mode-hook
  1057. (lambda ()
  1058. (define-key js2-mode-map (kbd "C-m") (lambda ()
  1059. (interactive)
  1060. (js2-enter-key)
  1061. (indent-for-tab-command)))
  1062. ;; (add-hook (kill-local-variable 'before-save-hook)
  1063. ;; 'js2-before-save)
  1064. ;; (add-hook 'before-save-hook
  1065. ;; 'my-indent-buffer
  1066. ;; nil
  1067. ;; t)
  1068. )))
  1069. (eval-after-load "js"
  1070. (defvar-set js-indent-level 2))
  1071. (add-to-list 'interpreter-mode-alist
  1072. '("node" . js-mode))
  1073. (when (autoload-eval-lazily 'flymake-jslint
  1074. '(flymake-jslint-load))
  1075. (autoload-eval-lazily 'js nil
  1076. (add-hook 'js-mode-hook
  1077. 'flymake-jslint-load)))
  1078. (safe-require-or-eval 'js-doc)
  1079. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1080. (when (safe-require-or-eval 'uniquify)
  1081. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1082. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1083. (setq uniquify-min-dir-content 1))
  1084. (add-hook 'view-mode-hook
  1085. (lambda()
  1086. (defvar view-mode-map)
  1087. (define-key view-mode-map "j" 'scroll-up-line)
  1088. (define-key view-mode-map "k" 'scroll-down-line)
  1089. (define-key view-mode-map "v" 'toggle-read-only)
  1090. (define-key view-mode-map "q" 'bury-buffer)
  1091. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1092. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1093. ;; (define-key view-mode-map
  1094. ;; "n" 'nonincremental-repeat-search-forward)
  1095. ;; (define-key view-mode-map
  1096. ;; "N" 'nonincremental-repeat-search-backward)
  1097. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1098. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1099. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1100. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1101. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point)
  1102. ))
  1103. (global-set-key "\M-r" 'view-mode)
  1104. ;; (setq view-read-only t)
  1105. ;; (defun my-view-mode-search-word (word)
  1106. ;; "Search for word current directory and subdirectories.
  1107. ;; If called intearctively, find word at point."
  1108. ;; (interactive (list (thing-at-point 'symbol)))
  1109. ;; (if word
  1110. ;; (if (and (require 'gtags nil t)
  1111. ;; (gtags-get-rootpath))
  1112. ;; (gtags-goto-tag word "s")
  1113. ;; (my-rgrep word))
  1114. ;; (message "No word at point.")
  1115. ;; nil))
  1116. (add-hook 'Man-mode-hook
  1117. (lambda ()
  1118. (view-mode 1)
  1119. (setq truncate-lines nil)))
  1120. (defvar-set Man-notify-method (if window-system
  1121. 'newframe
  1122. 'aggressive))
  1123. (defvar-set woman-cache-filename (expand-file-name (concat user-emacs-directory
  1124. "woman_cache.el")))
  1125. (defalias 'man 'woman)
  1126. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1127. ;; python
  1128. (when (autoload-eval-lazily 'python '(python-mode))
  1129. (defvar-set python-python-command (or (executable-find "python3")
  1130. (executable-find "python")))
  1131. ;; (defun my-python-run-as-command ()
  1132. ;; ""
  1133. ;; (interactive)
  1134. ;; (shell-command (concat python-python-command " " buffer-file-name)))
  1135. (defun my-python-display-python-buffer ()
  1136. ""
  1137. (interactive)
  1138. (defvar python-buffer)
  1139. (set-window-text-height (display-buffer python-buffer
  1140. t)
  1141. 7))
  1142. (add-hook 'python-mode-hook
  1143. (lambda ()
  1144. (local-set-key (kbd "C-c C-e") 'my-python-run-as-command)
  1145. (local-set-key (kbd "C-c C-b") 'my-python-display-python-buffer)
  1146. (local-set-key (kbd "C-m") 'newline-and-indent)))
  1147. (add-hook 'inferior-python-mode-hook
  1148. (lambda ()
  1149. (my-python-display-python-buffer)
  1150. (local-set-key (kbd "<up>") 'comint-previous-input)
  1151. (local-set-key (kbd "<down>") 'comint-next-input))))
  1152. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1153. ;; GNU GLOBAL(gtags)
  1154. ;; http://uguisu.skr.jp/Windows/gtags.html
  1155. ;; http://eigyr.dip.jp/gtags.html
  1156. ;; http://cha.la.coocan.jp/doc/gnu_global.html
  1157. (let ((d "/opt/local/share/gtags/"))
  1158. (and (file-directory-p d)
  1159. (add-to-list 'load-path
  1160. d)))
  1161. (when (autoload-eval-lazily 'gtags '(gtags-mode))
  1162. (add-hook 'gtags-mode-hook
  1163. (lambda ()
  1164. (view-mode gtags-mode)
  1165. (setq gtags-select-buffer-single t)
  1166. ;; (local-set-key "\M-t" 'gtags-find-tag)
  1167. ;; (local-set-key "\M-r" 'gtags-find-rtag)
  1168. ;; (local-set-key "\M-s" 'gtags-find-symbol)
  1169. ;; (local-set-key "\C-t" 'gtags-pop-stack)
  1170. (define-key gtags-mode-map (kbd "C-x t h")
  1171. 'gtags-find-tag-from-here)
  1172. (define-key gtags-mode-map (kbd "C-x t t") 'gtags-find-tag)
  1173. (define-key gtags-mode-map (kbd "C-x t r") 'gtags-find-rtag)
  1174. (define-key gtags-mode-map (kbd "C-x t s") 'gtags-find-symbol)
  1175. (define-key gtags-mode-map (kbd "C-x t p") 'gtags-find-pattern)
  1176. (define-key gtags-mode-map (kbd "C-x t f") 'gtags-find-file)
  1177. (define-key gtags-mode-map (kbd "C-x t b") 'gtags-pop-stack) ;back
  1178. ))
  1179. (add-hook 'gtags-select-mode-hook
  1180. (lambda ()
  1181. (define-key gtags-select-mode-map (kbd "C-m") 'gtags-select-tag)
  1182. ))
  1183. )
  1184. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1185. ;; term mode
  1186. ;; (setq multi-term-program shell-file-name)
  1187. (when (autoload-eval-lazily 'multi-term)
  1188. (setq multi-term-switch-after-close nil)
  1189. (setq multi-term-dedicated-select-after-open-p t)
  1190. (setq multi-term-dedicated-window-height 20))
  1191. (when (autoload-eval-lazily 'term '(term ansi-term))
  1192. (defun my-term-quit-or-send-raw ()
  1193. ""
  1194. (interactive)
  1195. (if (get-buffer-process (current-buffer))
  1196. (call-interactively 'term-send-raw)
  1197. (kill-buffer)))
  1198. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  1199. ;; (setq term-ansi-default-program shell-file-name)
  1200. (add-hook 'term-setup-hook
  1201. (lambda ()
  1202. (defvar-set term-display-table (make-display-table))))
  1203. (add-hook 'term-mode-hook
  1204. (lambda ()
  1205. (defvar term-raw-map)
  1206. (unless (memq (current-buffer)
  1207. (and (featurep 'multi-term)
  1208. (defvar multi-term-buffer-list)
  1209. ;; current buffer is not multi-term buffer
  1210. multi-term-buffer-list))
  1211. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1212. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1213. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1214. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1215. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1216. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1217. (define-key term-raw-map
  1218. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1219. (define-key term-raw-map
  1220. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1221. )
  1222. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  1223. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1224. (define-key term-raw-map (kbd "<up>") 'scroll-down-line)
  1225. (define-key term-raw-map (kbd "<down>") 'scroll-up-line)
  1226. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1227. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1228. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1229. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1230. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1231. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1232. (define-key term-raw-map [delete] 'term-send-raw)
  1233. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1234. (define-key term-raw-map "\C-y" 'term-paste)
  1235. (define-key term-raw-map
  1236. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1237. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1238. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1239. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1240. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1241. (set (make-local-variable 'scroll-margin) 0)
  1242. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1243. ;; (cua-mode 0)
  1244. ;; (and cua-mode
  1245. ;; (local-unset-key (kbd "C-c")))
  1246. ;; (define-key cua--prefix-override-keymap
  1247. ;;"\C-c" 'term-interrupt-subjob)
  1248. (set (make-local-variable (defvar hl-line-range-function))
  1249. (lambda ()
  1250. '(0 . 0)))
  1251. ))
  1252. ;; (add-hook 'term-exec-hook 'forward-char)
  1253. )
  1254. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1255. ;; buffer switching
  1256. (defvar bs-configurations)
  1257. (when (autoload-eval-lazily 'bs '(bs-show)
  1258. ;; (add-to-list 'bs-configurations
  1259. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1260. (add-to-list 'bs-configurations
  1261. '("files-and-terminals" nil nil nil
  1262. (lambda (buf)
  1263. (and (bs-visits-non-file buf)
  1264. (save-excursion
  1265. (set-buffer buf)
  1266. (not (memq major-mode
  1267. '(term-mode
  1268. eshell-mode))))))))
  1269. ;; (setq bs-configurations (list
  1270. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1271. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1272. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1273. )
  1274. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1275. (defalias 'list-buffers 'bs-show)
  1276. (defvar-set bs-default-configuration "files-and-terminals")
  1277. (defvar-set bs-default-sort-name "by nothing")
  1278. (add-hook 'bs-mode-hook
  1279. (lambda ()
  1280. ;; (setq bs-default-configuration "files")
  1281. ;; (and bs--show-all
  1282. ;; (call-interactively 'bs-toggle-show-all))
  1283. (set (make-local-variable 'scroll-margin) 0))))
  1284. ;;(iswitchb-mode 1)
  1285. (icomplete-mode)
  1286. (defun iswitchb-buffer-display-other-window ()
  1287. "Do iswitchb in other window."
  1288. (interactive)
  1289. (let ((iswitchb-default-method 'display))
  1290. (call-interactively 'iswitchb-buffer)))
  1291. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1292. ;; sdic
  1293. (when (autoload-eval-lazily 'sdic '(sdic-describe-word-at-point))
  1294. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  1295. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo)
  1296. (defun sdic-describe-word-at-point-echo ()
  1297. ""
  1298. (interactive)
  1299. (save-window-excursion
  1300. (sdic-describe-word-at-point))
  1301. (save-excursion
  1302. (set-buffer sdic-buffer-name)
  1303. (message (buffer-substring (point-min)
  1304. (progn (goto-char (point-min))
  1305. (or (and (re-search-forward "^\\w"
  1306. nil
  1307. t
  1308. 4)
  1309. (progn (previous-line) t)
  1310. (point-at-eol))
  1311. (point-max)))))))
  1312. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  1313. (setq sdic-waei-dictionary-list
  1314. '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  1315. (setq sdic-disable-select-window t)
  1316. (setq sdic-window-height 7))
  1317. ;;;;;;;;;;;;;;;;;;;;;;;;
  1318. ;; ilookup
  1319. (autoload-eval-lazily 'ilookup
  1320. '(ilookup-open)
  1321. (setq ilookup-dict-alist
  1322. '(
  1323. ("sdcv" . (lambda (word)
  1324. (shell-command-to-string
  1325. (format "sdcv -n '%s'"
  1326. word))))
  1327. ("en" . (lambda (word)
  1328. (shell-command-to-string
  1329. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  1330. word))))
  1331. ("ja" . (lambda (word)
  1332. (shell-command-to-string
  1333. (format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
  1334. word))))
  1335. ("jaj" . (lambda (word)
  1336. (shell-command-to-string
  1337. (format "sdcv -n -u jmdict-en-ja '%s'"
  1338. word))))
  1339. ("jag" .
  1340. (lambda (word)
  1341. (with-temp-buffer
  1342. (insert (shell-command-to-string
  1343. (format "sdcv -n -u 'Genius English-Japanese' '%s'"
  1344. word)))
  1345. (html2text)
  1346. (buffer-substring (point-min)
  1347. (point-max)))))
  1348. ("alc" . (lambda (word)
  1349. (shell-command-to-string
  1350. (format "alc '%s' | head -n 20"
  1351. word))))
  1352. ("app" . (lambda (word)
  1353. (shell-command-to-string
  1354. (format "dict_app '%s'"
  1355. word))))
  1356. ;; letters broken
  1357. ("ms" .
  1358. (lambda (word)
  1359. (let ((url (concat
  1360. "http://api.microsofttranslator.com/V2/Ajax.svc/"
  1361. "Translate?appId=%s&text=%s&to=%s"))
  1362. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  1363. (target "ja")
  1364. (eword (url-hexify-string word)))
  1365. (with-current-buffer (url-retrieve-synchronously
  1366. (format url
  1367. apikey
  1368. eword
  1369. target))
  1370. (message "")
  1371. (goto-char (point-min))
  1372. (search-forward-regexp "^$"
  1373. nil
  1374. t)
  1375. (url-unhex-string (buffer-substring-no-properties
  1376. (point)
  1377. (point-max)))))))
  1378. ))
  1379. ;; (funcall (cdr (assoc "ms"
  1380. ;; ilookup-alist))
  1381. ;; "dictionary")
  1382. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  1383. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  1384. (setq ilookup-default "ja")
  1385. (when (locate-library "google-translate")
  1386. (add-to-list 'ilookup-dict-alist
  1387. '("gt" .
  1388. (lambda (word)
  1389. (save-excursion
  1390. (google-translate-translate "auto"
  1391. "ja"
  1392. word))
  1393. (with-current-buffer "*Google Translate*"
  1394. (buffer-substring-no-properties (point-min)
  1395. (point-max)))))))
  1396. )
  1397. (when (autoload-eval-lazily 'google-translate '(google-translate-translate
  1398. google-translate-at-point))
  1399. (setq google-translate-default-source-language "auto")
  1400. (setq google-translate-default-target-language "ja"))
  1401. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1402. ;; vc
  1403. (require 'vc)
  1404. (setq vc-handled-backends '())
  1405. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1406. ;; gauche-mode
  1407. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  1408. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  1409. (when (and (fetch-library
  1410. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  1411. t)
  1412. (autoload-eval-lazily 'gauche-mode '(gauche-mode run-scheme)))
  1413. (let ((s (executable-find "gosh")))
  1414. (setq scheme-program-name s
  1415. gauche-program-name s))
  1416. (defun run-gauche-other-window ()
  1417. "Run gauche on other window"
  1418. (interactive)
  1419. (switch-to-buffer-other-window
  1420. (get-buffer-create "*scheme*"))
  1421. (run-gauche))
  1422. (defun run-gauche ()
  1423. "run gauche"
  1424. (interactive)
  1425. (run-scheme gauche-program-name)
  1426. )
  1427. (defun scheme-send-buffer ()
  1428. ""
  1429. (interactive)
  1430. (scheme-send-region (point-min) (point-max))
  1431. (my-scheme-display-scheme-buffer)
  1432. )
  1433. (defun my-scheme-display-scheme-buffer ()
  1434. ""
  1435. (interactive)
  1436. (set-window-text-height (display-buffer scheme-buffer
  1437. t)
  1438. 7))
  1439. (add-hook 'scheme-mode-hook
  1440. (lambda ()
  1441. nil))
  1442. (add-hook 'inferior-scheme-mode-hook
  1443. (lambda ()
  1444. ;; (my-scheme-display-scheme-buffer)
  1445. ))
  1446. (setq auto-mode-alist
  1447. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1448. (setq auto-mode-alist
  1449. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1450. (add-hook 'gauche-mode-hook
  1451. (lambda ()
  1452. (define-key gauche-mode-map
  1453. (kbd "C-c C-z") 'run-gauche-other-window)
  1454. (define-key scheme-mode-map
  1455. (kbd "C-c C-c") 'scheme-send-buffer)
  1456. (define-key scheme-mode-map
  1457. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer))))
  1458. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1459. ;; recentf-mode
  1460. (setq recentf-save-file (expand-file-name (concat user-emacs-directory
  1461. "recentf"))
  1462. recentf-max-menu-items 20
  1463. recentf-max-saved-items 30
  1464. recentf-show-file-shortcuts-flag nil)
  1465. (when (safe-require-or-eval 'recentf)
  1466. (add-to-list 'recentf-exclude
  1467. (regexp-quote recentf-save-file))
  1468. (add-to-list 'recentf-exclude
  1469. (regexp-quote (expand-file-name user-emacs-directory)))
  1470. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1471. (remove-hook 'find-file-hook
  1472. 'recentf-track-opened-file)
  1473. (defun my-recentf-load-track-save-list ()
  1474. "Load current recentf list from file, track current visiting file, then save
  1475. the list."
  1476. (recentf-load-list)
  1477. (recentf-track-opened-file)
  1478. (recentf-save-list))
  1479. (add-hook 'find-file-hook
  1480. 'my-recentf-load-track-save-list)
  1481. (add-hook 'kill-emacs-hook
  1482. 'recentf-load-list)
  1483. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1484. ;; (add-hook 'find-file-hook
  1485. ;; (lambda ()
  1486. ;; (recentf-add-file default-directory)))
  1487. (and (autoload-eval-lazily 'recentf-show)
  1488. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1489. (add-hook 'recentf-show-before-listing-hook
  1490. 'recentf-load-list))
  1491. (recentf-mode 1)
  1492. (add-hook 'recentf-dialog-mode-hook
  1493. (lambda ()
  1494. ;; (recentf-save-list)
  1495. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1496. ;; 'my-recentf-cd-and-find-file)
  1497. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1498. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1499. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1500. (define-key recentf-dialog-mode-map "n" 'next-line)
  1501. (cd "~/"))))
  1502. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1503. ;; dired
  1504. (when (autoload-eval-lazily 'dired nil)
  1505. (defun my-dired-echo-file-head (arg)
  1506. ""
  1507. (interactive "P")
  1508. (let ((f (dired-get-filename)))
  1509. (message "%s"
  1510. (with-temp-buffer
  1511. (insert-file-contents f)
  1512. (buffer-substring-no-properties
  1513. (point-min)
  1514. (progn (goto-line (if arg
  1515. (prefix-numeric-value arg)
  1516. 7))
  1517. (point-at-eol)))))))
  1518. (defun my-dired-diff ()
  1519. ""
  1520. (interactive)
  1521. (let ((files (dired-get-marked-files nil nil nil t)))
  1522. (if (eq (car files)
  1523. t)
  1524. (diff (cadr files) (dired-get-filename))
  1525. (message "One files must be marked!"))))
  1526. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1527. "pop up buffer using `display-buffer' and return that buffer."
  1528. (let ((bf (get-buffer-create buffer-or-name)))
  1529. (with-current-buffer bf
  1530. (cd ".")
  1531. (erase-buffer))
  1532. (display-buffer bf)
  1533. bf))
  1534. (defun my-replace-nasi-none ()
  1535. ""
  1536. (save-excursion
  1537. (let ((buffer-read-only nil))
  1538. (goto-char (point-min))
  1539. (while (search-forward "なし" nil t)
  1540. (replace-match "none")))))
  1541. (defun dired-get-file-info ()
  1542. "dired get file info"
  1543. (interactive)
  1544. (let ((f (shell-quote-argument (dired-get-filename t))))
  1545. (if (file-directory-p f)
  1546. (progn
  1547. (message "Calculating disk usage...")
  1548. (shell-command (concat "du -hsD "
  1549. f)))
  1550. (shell-command (concat "file "
  1551. f)))))
  1552. (defun my-dired-scroll-up ()
  1553. ""
  1554. (interactive)
  1555. (my-dired-previous-line (- (window-height) 1)))
  1556. (defun my-dired-scroll-down ()
  1557. ""
  1558. (interactive)
  1559. (my-dired-next-line (- (window-height) 1)))
  1560. ;; (defun my-dired-forward-line (arg)
  1561. ;; ""
  1562. ;; (interactive "p"))
  1563. (defun my-dired-previous-line (arg)
  1564. ""
  1565. (interactive "p")
  1566. (if (> arg 0)
  1567. (progn
  1568. (if (eq (line-number-at-pos)
  1569. 1)
  1570. (goto-char (point-max))
  1571. (forward-line -1))
  1572. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1573. (dired-get-subdir))
  1574. (- arg 1)
  1575. arg)))
  1576. (dired-move-to-filename)))
  1577. (defun my-dired-next-line (arg)
  1578. ""
  1579. (interactive "p")
  1580. (if (> arg 0)
  1581. (progn
  1582. (if (eq (point)
  1583. (point-max))
  1584. (goto-char (point-min))
  1585. (forward-line 1))
  1586. (my-dired-next-line (if (or (dired-get-filename nil t)
  1587. (dired-get-subdir))
  1588. (- arg 1)
  1589. arg)))
  1590. (dired-move-to-filename)))
  1591. (defun my-dired-print-current-dir-and-file ()
  1592. (message "%s %s"
  1593. default-directory
  1594. (buffer-substring-no-properties (point-at-bol)
  1595. (point-at-eol))))
  1596. (defun dired-do-execute-as-command ()
  1597. ""
  1598. (interactive)
  1599. (let ((file (dired-get-filename t)))
  1600. (if (file-executable-p file)
  1601. (start-process file nil file)
  1602. (when (y-or-n-p
  1603. "This file cant be executed. Mark as executable and go? ")
  1604. (set-file-modes file
  1605. (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1606. (start-process file nil file)))))
  1607. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1608. (defun my-dired-x-open ()
  1609. ""
  1610. (interactive)
  1611. (my-x-open (dired-get-filename t t)))
  1612. (if (eq window-system 'mac)
  1613. (setq dired-listing-switches "-lhF")
  1614. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1615. )
  1616. (setq dired-listing-switches "-lhF")
  1617. (put 'dired-find-alternate-file 'disabled nil)
  1618. ;; when using dired-find-alternate-file
  1619. ;; reuse current dired buffer for the file to open
  1620. (defvar-set dired-ls-F-marks-symlinks t)
  1621. (when (safe-require-or-eval 'ls-lisp)
  1622. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1623. (setq ls-lisp-dirs-first t)
  1624. (setq ls-lisp-use-localized-time-format t)
  1625. (setq ls-lisp-format-time-list
  1626. '("%Y-%m-%d %H:%M"
  1627. "%Y-%m-%d ")))
  1628. (defvar-set dired-dwim-target t)
  1629. (defvar-set dired-isearch-filenames t)
  1630. (defvar-set dired-hide-details-hide-symlink-targets nil)
  1631. (defvar-set dired-hide-details-hide-information-lines nil)
  1632. ;; (add-hook 'dired-after-readin-hook
  1633. ;; 'my-replace-nasi-none)
  1634. ;; (add-hook 'after-init-hook
  1635. ;; (lambda ()
  1636. ;; (dired ".")))
  1637. (add-hook 'dired-mode-hook
  1638. (lambda ()
  1639. (local-set-key "o" 'my-dired-x-open)
  1640. (local-set-key "i" 'dired-get-file-info)
  1641. (local-set-key "f" 'find-file)
  1642. (local-set-key "!" 'shell-command)
  1643. (local-set-key "&" 'async-shell-command)
  1644. (local-set-key "X" 'dired-do-async-shell-command)
  1645. (local-set-key "=" 'my-dired-diff)
  1646. (local-set-key "B" 'gtkbm-add-current-dir)
  1647. (local-set-key "b" 'gtkbm)
  1648. (local-set-key "h" 'my-dired-echo-file-head)
  1649. (local-set-key "@" (lambda ()
  1650. (interactive) (my-x-open ".")))
  1651. (local-set-key (kbd "TAB") 'other-window)
  1652. ;; (local-set-key "P" 'my-dired-do-pack-or-unpack)
  1653. (local-set-key "/" 'dired-isearch-filenames)
  1654. (local-set-key (kbd "DEL") 'dired-up-directory)
  1655. (local-set-key (kbd "C-h") 'dired-up-directory)
  1656. (substitute-key-definition 'dired-next-line
  1657. 'my-dired-next-line
  1658. (current-local-map))
  1659. (substitute-key-definition 'dired-previous-line
  1660. 'my-dired-previous-line
  1661. (current-local-map))
  1662. ;; (local-set-key (kbd "C-p") 'my-dired-previous-line)
  1663. ;; (local-set-key (kbd "p") 'my-dired-previous-line)
  1664. ;; (local-set-key (kbd "C-n") 'my-dired-next-line)
  1665. ;; (local-set-key (kbd "n") 'my-dired-next-line)
  1666. (local-set-key (kbd "<left>") 'my-dired-scroll-up)
  1667. (local-set-key (kbd "<right>") 'my-dired-scroll-down)
  1668. (local-set-key (kbd "ESC p") 'my-dired-scroll-up)
  1669. (local-set-key (kbd "ESC n") 'my-dired-scroll-down)
  1670. (when (fboundp 'dired-hide-details-mode)
  1671. (dired-hide-details-mode t)
  1672. (local-set-key "l" 'dired-hide-details-mode))
  1673. (let ((file "._Icon\015"))
  1674. (when nil (file-readable-p file)
  1675. (delete-file file)))))
  1676. (and (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack))
  1677. (add-hook 'dired-mode-hook
  1678. (lambda ()
  1679. (local-set-key "P" 'dired-do-pack-or-unpack))))
  1680. (and (autoload-eval-lazily 'dired-list-all-mode)
  1681. (setq dired-listing-switches "-lhF")
  1682. (add-hook 'dired-mode-hook
  1683. (lambda ()
  1684. (local-set-key "a" 'dired-list-all-mode)
  1685. )))
  1686. ) ; when dired locate
  1687. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1688. (defvar dired-marker-char)
  1689. (defun my-dired-toggle-mark()
  1690. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1691. (t dired-marker-char))))
  1692. (delete-char 1)
  1693. (insert cur)))
  1694. (defun my-dired-mark (arg)
  1695. "Toggle mark the current (or next ARG) files.
  1696. If on a subdir headerline, mark all its files except `.' and `..'.
  1697. Use \\[dired-unmark-all-files] to remove all marks
  1698. and \\[dired-unmark] on a subdir to remove the marks in
  1699. this subdir."
  1700. (interactive "P")
  1701. (if (dired-get-subdir)
  1702. (save-excursion (dired-mark-subdir-files))
  1703. (let ((inhibit-read-only t))
  1704. (dired-repeat-over-lines
  1705. (prefix-numeric-value arg)
  1706. 'my-dired-toggle-mark))))
  1707. (defun my-dired-mark-backward (arg)
  1708. "In Dired, move up lines and toggle mark there.
  1709. Optional prefix ARG says how many lines to unflag; default is one line."
  1710. (interactive "p")
  1711. (my-dired-mark (- arg)))
  1712. (add-hook 'dired-mode-hook
  1713. (lambda ()
  1714. (local-set-key (kbd "SPC") 'my-dired-mark)
  1715. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1716. )
  1717. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1718. ;; eshell
  1719. (autoload-eval-lazily 'eshell nil
  1720. (defvar-set eshell-banner-message (format "Welcome to the Emacs shell
  1721. %s
  1722. C-x t to toggling emacs-text-mode
  1723. "
  1724. (shell-command-to-string "uname -a")
  1725. ))
  1726. (defvar eshell-text-mode-map
  1727. (let ((map (make-sparse-keymap)))
  1728. (define-key map (kbd "C-x t") 'eshell-text-mode-toggle)
  1729. map))
  1730. (define-derived-mode eshell-text-mode text-mode
  1731. "Eshell-Text"
  1732. "Text-mode for Eshell."
  1733. nil)
  1734. (defun eshell-text-mode-toggle ()
  1735. "Toggle eshell-text-mode and eshell-mode."
  1736. (interactive)
  1737. (cond ((eq major-mode
  1738. 'eshell-text-mode)
  1739. (goto-char (point-max))
  1740. (message "Eshell text mode disabled")
  1741. (eshell-mode))
  1742. ((eq major-mode
  1743. 'eshell-mode)
  1744. (message "Eshell text mode enabled")
  1745. (eshell-write-history)
  1746. (eshell-text-mode))
  1747. (t
  1748. (message "Not in eshell buffer")
  1749. nil)))
  1750. (defun my-eshell-backward-delete-char ()
  1751. (interactive)
  1752. (when (< (save-excursion
  1753. (eshell-bol)
  1754. (point))
  1755. (point))
  1756. (backward-delete-char 1)))
  1757. (defun my-file-owner-p (file)
  1758. "t if FILE is owned by me."
  1759. (eq (user-uid) (nth 2 (file-attributes file))))
  1760. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1761. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1762. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1763. ;; (defun eshell/less (&rest args)
  1764. ;; "Invoke `view-file' on the file.
  1765. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1766. ;; (if args
  1767. ;; (while args
  1768. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1769. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1770. ;; (file (pop args)))
  1771. ;; (view-file file)
  1772. ;; (goto-line line))
  1773. ;; (view-file (pop args))))))
  1774. (defun eshell/o (&optional file)
  1775. (my-x-open (or file ".")))
  1776. ;; (defun eshell/vi (&rest args)
  1777. ;; "Invoke `find-file' on the file.
  1778. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1779. ;; (while args
  1780. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1781. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1782. ;; (file (pop args)))
  1783. ;; (find-file file)
  1784. ;; (goto-line line))
  1785. ;; (find-file (pop args)))))
  1786. (defun eshell/clear ()
  1787. "Clear the current buffer, leaving one prompt at the top."
  1788. (interactive)
  1789. (let ((inhibit-read-only t))
  1790. (erase-buffer)))
  1791. (defun eshell-clear ()
  1792. (interactive)
  1793. (let ((inhibit-read-only t))
  1794. (erase-buffer)
  1795. (insert (funcall eshell-prompt-function))))
  1796. (defun eshell/d (&optional dirname switches)
  1797. "if first arg is omitted open current directory."
  1798. (dired (or dirname ".") switches))
  1799. (defun eshell/v ()
  1800. (view-mode 1))
  1801. ;; (defun eshell/aaa (&rest args)
  1802. ;; (message "%S"
  1803. ;; args))
  1804. (defalias 'eshell/: 'ignore)
  1805. (defalias 'eshell/type 'eshell/which)
  1806. ;; (defalias 'eshell/vim 'eshell/vi)
  1807. (defalias 'eshell/ff 'find-file)
  1808. (defalias 'eshell/q 'eshell/exit)
  1809. (defun eshell-goto-prompt ()
  1810. ""
  1811. (interactive)
  1812. (goto-char (point-max)))
  1813. (defun eshell-delete-char-or-logout (n)
  1814. (interactive "p")
  1815. (if (equal (eshell-get-old-input)
  1816. "")
  1817. (progn
  1818. (insert "exit")
  1819. (eshell-send-input))
  1820. (delete-char n)))
  1821. (defun eshell-kill-input ()
  1822. (interactive)
  1823. (delete-region (point)
  1824. (progn (eshell-bol)
  1825. (point))))
  1826. (defalias 'eshell/logout 'eshell/exit)
  1827. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1828. "open eshell and change wd
  1829. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1830. (interactive)
  1831. (let ((dir (expand-file-name default-directory)))
  1832. (switch-to-buffer (or eshell-buffer-or-name
  1833. (eshell t)))
  1834. (unless (equal dir (expand-file-name default-directory))
  1835. ;; (cd dir)
  1836. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1837. ;; (eshell-emit-prompt)
  1838. (goto-char (point-max))
  1839. (eshell-kill-input)
  1840. (insert "cd " dir)
  1841. (eshell-send-input))))
  1842. (defadvice eshell-next-matching-input-from-input
  1843. ;; do not cycle history
  1844. (around eshell-history-do-not-cycle activate)
  1845. (if (= 0
  1846. (or eshell-history-index
  1847. 0))
  1848. (progn
  1849. (delete-region eshell-last-output-end (point))
  1850. (insert-and-inherit eshell-matching-input-from-input-string)
  1851. (setq eshell-history-index nil))
  1852. ad-do-it))
  1853. (defvar-set eshell-directory-name (concat user-emacs-directory
  1854. "eshell/"))
  1855. (defvar-set eshell-term-name "eterm-color")
  1856. (defvar-set eshell-scroll-to-bottom-on-input t)
  1857. (defvar-set eshell-cmpl-ignore-case t)
  1858. (defvar-set eshell-cmpl-cycle-completions nil)
  1859. (defvar-set eshell-highlight-prompt nil)
  1860. (if (eq system-type 'darwin)
  1861. (defvar-set eshell-ls-initial-args '("-hCFG")
  1862. (defvar-set eshell-ls-initial-args '("-hCFG"
  1863. "--color=auto"
  1864. "--time-style=long-iso")) ; "-hF")
  1865. ))
  1866. (defvar-set eshell-prompt-function
  1867. 'my-eshell-prompt-function)
  1868. (defvar eshell-last-command-status)
  1869. (defun my-eshell-prompt-function()
  1870. "Prompt function.
  1871. It looks like:
  1872. :: [10sr@darwin:~/][ESHELL]
  1873. :: $
  1874. "
  1875. (concat ":: ["
  1876. (let ((str (concat user-login-name
  1877. "@"
  1878. (car (split-string system-name
  1879. "\\."))
  1880. )))
  1881. (put-text-property 0
  1882. (length str)
  1883. 'face
  1884. 'underline
  1885. str)
  1886. str)
  1887. ":"
  1888. (let ((str (abbreviate-file-name default-directory)))
  1889. (put-text-property 0
  1890. (length str)
  1891. 'face
  1892. 'underline
  1893. str)
  1894. str)
  1895. "][ESHELL]\n:: "
  1896. (if (eq 0
  1897. eshell-last-command-status)
  1898. ""
  1899. (format "[STATUS:%d] "
  1900. eshell-last-command-status))
  1901. (if (= (user-uid)
  1902. 0)
  1903. "# "
  1904. "$ ")
  1905. ))
  1906. (add-hook 'eshell-mode-hook
  1907. (lambda ()
  1908. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1909. ;; (interactive)
  1910. ;; (switch-to-buffer (other-buffer))))
  1911. ;; (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1912. ;; (interactive)
  1913. ;; (eshell-goto-prompt)
  1914. ;; (keyboard-quit)))
  1915. (local-set-key (kbd "C-x t") 'eshell-text-mode-toggle)
  1916. (local-set-key (kbd "C-u") 'eshell-kill-input)
  1917. (local-set-key (kbd "C-d") 'eshell-delete-char-or-logout)
  1918. ;; (define-key eshell-mode-map (kbd "C-l")
  1919. ;; 'eshell-clear)
  1920. (local-set-key (kbd "DEL") 'my-eshell-backward-delete-char)
  1921. (local-set-key (kbd "<up>") 'scroll-down-line)
  1922. (local-set-key (kbd "<down>") 'scroll-up-line)
  1923. ;; (define-key eshell-mode-map
  1924. ;; (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1925. ;; (define-key eshell-mode-map
  1926. ;; (kbd "C-n") 'eshell-next-matching-input-from-input)
  1927. (apply 'eshell/addpath exec-path)
  1928. (set (make-local-variable 'scroll-margin) 0)
  1929. ;; (eshell/export "GIT_PAGER=")
  1930. ;; (eshell/export "GIT_EDITOR=")
  1931. (eshell/export "LC_MESSAGES=C")
  1932. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1933. (set (make-local-variable (defvar hl-line-range-function))
  1934. (lambda ()
  1935. '(0 . 0)))
  1936. (defvar eshell-virtual-targets)
  1937. (add-to-list 'eshell-virtual-targets
  1938. '("/dev/less"
  1939. (lambda (str)
  1940. (if str
  1941. (with-current-buffer nil)))
  1942. nil))
  1943. ))
  1944. (add-hook 'eshell-mode-hook
  1945. (lambda ()
  1946. (defvar eshell-visual-commands)
  1947. (defvar eshell-output-filter-functions)
  1948. (defvar eshell-command-aliases-list)
  1949. (add-to-list 'eshell-visual-commands "vim")
  1950. ;; (add-to-list 'eshell-visual-commands "git")
  1951. (add-to-list 'eshell-output-filter-functions
  1952. 'eshell-truncate-buffer)
  1953. (mapcar (lambda (alias)
  1954. (add-to-list 'eshell-command-aliases-list
  1955. alias))
  1956. '(
  1957. ;; ("ll" "ls -l $*")
  1958. ;; ("la" "ls -a $*")
  1959. ;; ("lla" "ls -al $*")
  1960. ("git" "git -c color.ui=always $*")
  1961. ("g" "git $*")
  1962. ("eless"
  1963. (concat "cat >>> (with-current-buffer "
  1964. "(get-buffer-create \"*eshell output\") "
  1965. "(erase-buffer) "
  1966. "(setq buffer-read-only nil) "
  1967. "(current-buffer)) "
  1968. "(view-buffer (get-buffer \"*eshell output*\"))"))
  1969. )
  1970. )))
  1971. ) ; eval after load eshell
  1972. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1973. ;; my-term
  1974. (defvar my-term nil
  1975. "My terminal buffer.")
  1976. (defvar my-term-function nil
  1977. "Function to create terminal buffer.
  1978. This function accept no argument and return newly created buffer of terminal.")
  1979. (defun my-term (&optional arg)
  1980. "Open terminal buffer and return that buffer.
  1981. If ARG is given or called with prefix argument, create new buffer."
  1982. (interactive "P")
  1983. (if (and (not arg)
  1984. my-term
  1985. (buffer-name my-term))
  1986. (pop-to-buffer my-term)
  1987. (setq my-term
  1988. (save-window-excursion
  1989. (funcall my-term-function)))
  1990. (and my-term
  1991. (my-term))))
  1992. ;; (setq my-term-function
  1993. ;; (lambda ()
  1994. ;; (if (eq system-type 'windows-nt)
  1995. ;; (eshell)
  1996. ;; (if (require 'multi-term nil t)
  1997. ;; (multi-term)
  1998. ;; (ansi-term shell-file-name)))))
  1999. (setq my-term-function (lambda () (eshell t)))
  2000. ;;(define-key my-prefix-map (kbd "C-s") 'my-term)
  2001. (define-key ctl-x-map "i" 'my-term)
  2002. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2003. ;; x open
  2004. (defvar my-filer nil)
  2005. (setq my-filer (or (executable-find "pcmanfm")
  2006. (executable-find "nautilus")))
  2007. (defun my-x-open (file)
  2008. "Open FILE."
  2009. (interactive "FOpen File: ")
  2010. (setq file (expand-file-name file))
  2011. (message "Opening %s..." file)
  2012. (cond ((eq system-type 'windows-nt)
  2013. (call-process "cmd.exe" nil 0 nil
  2014. "/c" "start" "" (convert-standard-filename file)))
  2015. ((eq system-type 'darwin)
  2016. (call-process "open" nil 0 nil file))
  2017. ((getenv "DISPLAY")
  2018. (call-process (or my-filer "xdg-open") nil 0 nil file))
  2019. (t
  2020. (find-file file))
  2021. )
  2022. ;; (recentf-add-file file)
  2023. (message "Opening %s...done" file))
  2024. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2025. ;; misc funcs
  2026. (defun my-git-apply-index-from-buffer (&optional buf)
  2027. "Git apply buffer. BUF is buffer to apply. nil to use current buffer."
  2028. (interactive)
  2029. (let ((buf (or buf
  2030. (current-buffer)))
  2031. (file (make-temp-file "git-apply-diff.emacs")))
  2032. (with-current-buffer buf
  2033. (write-region (point-min)
  2034. (point-max)
  2035. file)
  2036. (call-process "git"
  2037. nil
  2038. nil
  2039. nil
  2040. "apply"
  2041. "--cached"
  2042. file))))
  2043. (defun memo (&optional dir)
  2044. "Open memo.txt in DIR."
  2045. (interactive)
  2046. (pop-to-buffer (find-file-noselect (concat (if dir
  2047. (file-name-as-directory dir)
  2048. "")
  2049. "memo.txt"))))
  2050. (defvar my-rgrep-alist
  2051. `(
  2052. ;; the silver searcher
  2053. ("ag"
  2054. (executable-find "ag")
  2055. "ag --nocolor --nogroup --nopager --filename ")
  2056. ;; ack
  2057. ("ack"
  2058. (executable-find "ack")
  2059. "ack --nocolor --nogroup --nopager --with-filename ")
  2060. ;; gnu global
  2061. ("global"
  2062. (and (require 'gtags nil t)
  2063. (executable-find "global")
  2064. (gtags-get-rootpath))
  2065. "global --result grep ")
  2066. ;; git grep
  2067. ("gitgrep"
  2068. (eq 0
  2069. (shell-command "git rev-parse --git-dir"))
  2070. "git --no-pager -c color.grep=false grep -nH -e ")
  2071. ;; grep
  2072. ("grep"
  2073. t
  2074. ,(concat "find . "
  2075. "-path '*/.git' -prune -o "
  2076. "-path '*/.svn' -prune -o "
  2077. "-type f -print0 | "
  2078. "xargs -0 grep -nH -e "))
  2079. )
  2080. "Alist of rgrep command.
  2081. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  2082. condition to choose COMMAND when evaluated.")
  2083. (defvar my-rgrep-default nil
  2084. "Default command name for my-rgrep.")
  2085. (defun my-rgrep-grep-command (&optional name alist)
  2086. "Return recursive grep command for current directory or nil.
  2087. If NAME is given, use that without testing.
  2088. Commands are searched from ALIST."
  2089. (if alist
  2090. (if name
  2091. ;; if name is given search that from alist and return the command
  2092. (nth 2 (assoc name
  2093. alist))
  2094. ;; if name is not given try test in 1th elem
  2095. (let ((car (car alist))
  2096. (cdr (cdr alist)))
  2097. (if (eval (nth 1 car))
  2098. ;; if the condition is true return the command
  2099. (nth 2 car)
  2100. ;; try next one
  2101. (and cdr
  2102. (my-rgrep-grep-command name cdr)))))
  2103. ;; if alist is not given set default value
  2104. (my-rgrep-grep-command name my-rgrep-alist)))
  2105. (defun my-rgrep (command-args)
  2106. "My recursive grep. Run COMMAND-ARGS."
  2107. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  2108. nil)))
  2109. (if cmd
  2110. (list (read-shell-command "grep command: "
  2111. cmd
  2112. 'grep-find-history))
  2113. (error "My-Rgrep: Command for rgrep not found")
  2114. )))
  2115. (compilation-start command-args
  2116. 'grep-mode))
  2117. ;; (defun my-rgrep-symbol-at-point (command-args)
  2118. ;; "My recursive grep. Run COMMAND-ARGS."
  2119. ;; (interactive (list (read-shell-command "grep command: "
  2120. ;; (concat (my-rgrep-grep-command)
  2121. ;; " "
  2122. ;; (thing-at-point 'symbol))
  2123. ;; 'grep-find-history)))
  2124. ;; (compilation-start command-args
  2125. ;; 'grep-mode))
  2126. (defmacro define-my-rgrep (name)
  2127. "Define rgrep for NAME."
  2128. `(defun ,(intern (concat "my-rgrep-"
  2129. name)) ()
  2130. ,(format "My recursive grep by %s."
  2131. name)
  2132. (interactive)
  2133. (let ((my-rgrep-default ,name))
  2134. (if (called-interactively-p 'any)
  2135. (call-interactively 'my-rgrep)
  2136. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2137. )
  2138. (define-my-rgrep "ack")
  2139. (define-my-rgrep "ag")
  2140. (define-my-rgrep "gitgrep")
  2141. (define-my-rgrep "grep")
  2142. (define-my-rgrep "global")
  2143. (define-key ctl-x-map "s" 'my-rgrep)
  2144. ;; (defun make ()
  2145. ;; "Run \"make -k\" in current directory."
  2146. ;; (interactive)
  2147. ;; (compile "make -k"))
  2148. (defalias 'make 'compile)
  2149. (define-key ctl-x-map "c" 'compile)
  2150. (defvar sed-in-place-history nil
  2151. "History of `sed-in-place'.")
  2152. (defvar sed-in-place-command "sed --in-place=.bak -e")
  2153. (defun sed-in-place (command)
  2154. "Issue sed in place COMMAND."
  2155. (interactive (list (read-shell-command "sed in place: "
  2156. (concat sed-in-place-command " ")
  2157. 'sed-in-place-history)))
  2158. (shell-command command
  2159. "*sed in place*"))
  2160. (defun dired-do-sed-in-place (&optional arg)
  2161. "Issue sed in place dired. If ARG is given, use the next ARG files."
  2162. (interactive "p")
  2163. (require 'dired-aux)
  2164. (let* ((files (dired-get-marked-files t arg))
  2165. (expr (dired-mark-read-string "Run sed-in-place for %s: "
  2166. nil
  2167. 'sed-in-place
  2168. arg
  2169. files)))
  2170. (if (equal expr
  2171. "")
  2172. (error "No expression specified")
  2173. (shell-command (concat sed-in-place-command
  2174. " '"
  2175. expr
  2176. "' "
  2177. (mapconcat 'shell-quote-argument
  2178. files
  2179. " "))
  2180. "*sed in place*"))))
  2181. (defun dir-show (&optional dir)
  2182. "Show DIR list."
  2183. (interactive)
  2184. (let ((bf (get-buffer-create "*dir show*"))
  2185. (list-directory-brief-switches "-C"))
  2186. (with-current-buffer bf
  2187. (list-directory (or nil
  2188. default-directory)
  2189. nil))
  2190. ))
  2191. (defun my-convmv-sjis2utf8-test ()
  2192. "Run `convmv -r -f sjis -t utf8 *'.
  2193. this is test, does not rename files."
  2194. (interactive)
  2195. (shell-command "convmv -r -f sjis -t utf8 *"))
  2196. (defun my-convmv-sjis2utf8-notest ()
  2197. "Run `convmv -r -f sjis -t utf8 * --notest'."
  2198. (interactive)
  2199. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  2200. (defun kill-ring-save-buffer-file-name ()
  2201. "Get current filename."
  2202. (interactive)
  2203. (let ((file buffer-file-name))
  2204. (if file
  2205. (progn (kill-new file)
  2206. (message file))
  2207. (message "not visiting file."))))
  2208. (defvar kill-ring-buffer-name "*kill-ring*"
  2209. "Buffer name for `kill-ring-buffer'.")
  2210. (defun open-kill-ring-buffer ()
  2211. "Open kill- ring buffer."
  2212. (interactive)
  2213. (pop-to-buffer
  2214. (with-current-buffer (get-buffer-create kill-ring-buffer-name)
  2215. (erase-buffer)
  2216. (yank)
  2217. (text-mode)
  2218. (current-local-map)
  2219. (goto-char (point-min))
  2220. (yank)
  2221. (current-buffer))))
  2222. (defun set-terminal-header (string)
  2223. "Set terminal header STRING."
  2224. (let ((savepos "\033[s")
  2225. (restorepos "\033[u")
  2226. (movecursor "\033[0;%dH")
  2227. (inverse "\033[7m")
  2228. (restorecolor "\033[0m")
  2229. (cols (frame-parameter nil 'width))
  2230. (length (length string)))
  2231. ;; (redraw-frame (selected-frame))
  2232. (send-string-to-terminal (concat savepos
  2233. (format movecursor
  2234. (1+ (- cols length)))
  2235. inverse
  2236. string
  2237. restorecolor
  2238. restorepos))
  2239. ))
  2240. (defun my-set-terminal-header ()
  2241. "Set terminal header."
  2242. (set-terminal-header (concat " "
  2243. user-login-name
  2244. "@"
  2245. (car (split-string system-name
  2246. "\\."))
  2247. " "
  2248. (format-time-string "%Y/%m/%d %T %z")
  2249. " ")))
  2250. ;; (run-with-timer
  2251. ;; 0.1
  2252. ;; 1
  2253. ;; 'my-set-terminal-header)
  2254. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2255. ;; ;; savage emacs
  2256. ;; ;; when enabled emacs fails to complete
  2257. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  2258. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  2259. ;; (setq arg
  2260. ;; (concat arg
  2261. ;; (if (eq nil
  2262. ;; (string-match "\\. *$"
  2263. ;; arg))
  2264. ;; ".")
  2265. ;; " Stupid!")))
  2266. ;; TODO: make these a library
  2267. (defvar info-in-prompt
  2268. nil
  2269. "System info in the form of \"[user@host] \".")
  2270. (setq info-in-prompt
  2271. (concat "["
  2272. user-login-name
  2273. "@"
  2274. (car (split-string system-name
  2275. "\\."))
  2276. "]"))
  2277. (defun my-real-function-subr-p (function)
  2278. "Return t if FUNCTION is a built-in function even if it is advised."
  2279. (let* ((advised (and (symbolp function)
  2280. (featurep 'advice)
  2281. (ad-get-advice-info function)))
  2282. (real-function
  2283. (or (and advised (let ((origname (cdr (assq 'origname advised))))
  2284. (and (fboundp origname)
  2285. origname)))
  2286. function))
  2287. (def (if (symbolp real-function)
  2288. (symbol-function real-function)
  2289. function)))
  2290. (subrp def)))
  2291. ;; (my-real-function-subr-p 'my-real-function-subr-p)
  2292. ;; (defadvice read-from-minibuffer (before info-in-prompt activate)
  2293. ;; "Show system info when use `read-from-minibuffer'."
  2294. ;; (ad-set-arg 0
  2295. ;; (concat my-system-info
  2296. ;; (ad-get-arg 0))))
  2297. ;; (defadvice read-string (before info-in-prompt activate)
  2298. ;; "Show system info when use `read-string'."
  2299. ;; (ad-set-arg 0
  2300. ;; (concat my-system-info
  2301. ;; (ad-get-arg 0))))
  2302. ;; (when (< emacs-major-version 24)
  2303. ;; (defadvice completing-read (before info-in-prompt activate)
  2304. ;; "Show system info when use `completing-read'."
  2305. ;; (ad-set-arg 0
  2306. ;; (concat my-system-info
  2307. ;; (ad-get-arg 0)))))
  2308. (defmacro info-in-prompt-set (&rest functions)
  2309. "Set info-in-prompt advices for FUNCTIONS."
  2310. `(progn
  2311. ,@(mapcar (lambda (f)
  2312. `(defadvice ,f (before info-in-prompt activate)
  2313. "Show info in prompt."
  2314. (let ((orig (ad-get-arg 0)))
  2315. (unless (string-match-p (regexp-quote info-in-prompt)
  2316. orig)
  2317. (ad-set-arg 0
  2318. (concat info-in-prompt
  2319. " "
  2320. orig))))))
  2321. functions)))
  2322. (info-in-prompt-set read-from-minibuffer
  2323. read-string
  2324. completing-read)
  2325. ;;; emacs.el ends here