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.
 
 
 
 
 
 

2556 lines
88 KiB

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