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.

emacs.el 72 KiB

12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040
  1. ;; (and (file-readable-p "~/.dotfiles/emacs.el")
  2. ;; (load-file "~/.dotfiles/emacs.el"))
  3. ;; make directories
  4. (unless (file-directory-p (expand-file-name user-emacs-directory))
  5. (make-directory (expand-file-name user-emacs-directory)))
  6. (let ((d (expand-file-name (concat user-emacs-directory
  7. "lisp"))))
  8. (unless (file-directory-p d)
  9. (make-directory d))
  10. (add-to-list 'load-path d))
  11. (require 'cl nil t)
  12. (progn
  13. (defvar buffer-file-changed-functions nil "Hook run when buffer file changed.
  14. Each function is called with two args, the filename before changing and after
  15. changing.")
  16. (declare-function run-buffer-file-changed-functions "emacs.el")
  17. (add-hook 'post-command-hook
  18. 'run-buffer-file-changed-functions)
  19. (lexical-let (previous-file)
  20. (defun run-buffer-file-changed-functions ()
  21. ""
  22. (unless (and previous-file
  23. (equal previous-file
  24. (expand-file-name (or buffer-file-name
  25. default-directory))))
  26. (let ((pfile previous-file)
  27. (cfile (expand-file-name (or buffer-file-name
  28. default-directory))))
  29. (setq previous-file cfile)
  30. (run-hook-with-args 'buffer-file-changed-functions pfile cfile)))))
  31. ;; (add-hook 'buffer-file-changed-function
  32. ;; (lambda (pdir cdir)
  33. ;; (message "dir changed %s to %s !" pdir cdir)))
  34. )
  35. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36. ;; download library from web
  37. (defun fetch-library (url &optional byte-compile-p force-download-p)
  38. "If library does not exist, download it from URL and locate it in
  39. \"~/emacs.d/lisp/\". Return nil if library unfound and failed to download,
  40. otherwise the path where the library installed."
  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. (byte-compile-file lpath)))
  58. (progn (and (file-writable-p lpath)
  59. (delete-file lpath))
  60. (message "Downloading %s...failed"
  61. url))))
  62. (locate-library lib)))
  63. (defun download-file (url path &optional ok-if-already-exists)
  64. "Download file from URL and output to PATH."
  65. (or (ignore-errors
  66. (require 'url)
  67. (url-copy-file url
  68. path
  69. ok-if-already-exists)
  70. path)
  71. (let ((curl (executable-find "curl")))
  72. (when curl
  73. (if (and (not ok-if-already-exists)
  74. (file-exists-p path))
  75. nil
  76. (and (eq 0
  77. (call-process curl
  78. nil
  79. nil
  80. nil
  81. "--output"
  82. path
  83. url
  84. ))
  85. path))))))
  86. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  87. ;; autoload
  88. (defmacro lazy-load-eval (feature &optional functions &rest body)
  89. "Define each FUNCTIONS to autoload from FEATURE.
  90. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  91. the function same as FEATURE is defined as autoloaded function. BODY is passed
  92. to `eval-after-load'.
  93. When this macro is evaluated, this returns the path to library if FEATURE
  94. found, otherwise returns nil."
  95. (let* ((libname (symbol-name (eval feature)))
  96. (libpath (locate-library libname)))
  97. (and libpath
  98. `(progn
  99. ,@(mapcar (lambda (f)
  100. (unless (fboundp f)
  101. (progn
  102. (message "Autoloaded function `%S' defined (%s)"
  103. f
  104. libpath)
  105. `(autoload (quote ,f)
  106. ,libname
  107. ,(concat "Autoloaded function defined in \""
  108. libpath
  109. "\".")
  110. t))))
  111. (or (eval functions)
  112. `(,(eval feature))))
  113. (eval-after-load ,feature
  114. (quote (progn
  115. ,@body)))
  116. ,libpath))))
  117. (put 'lazy-load-eval 'lisp-indent-function 2)
  118. (when (lazy-load-eval 'tetris nil
  119. (message "Tetris loaded!"))
  120. (message "Tetris found!"))
  121. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  122. ;; start and quit
  123. (setq inhibit-startup-message t)
  124. (setq confirm-kill-emacs 'y-or-n-p)
  125. (setq gc-cons-threshold (* 1024 1024 4))
  126. (when window-system
  127. (add-to-list 'default-frame-alist '(cursor-type . box))
  128. (add-to-list 'default-frame-alist '(background-color . "white"))
  129. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  130. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  131. ;; does not work?
  132. )
  133. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  134. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  135. (and (fboundp 'tool-bar-mode)
  136. (tool-bar-mode 0))
  137. (and (fboundp 'set-scroll-bar-mode)
  138. (set-scroll-bar-mode nil))
  139. (add-hook 'kill-emacs-hook
  140. ;; load init file when terminating emacs to ensure file is not broken
  141. 'reload-init-file)
  142. (add-hook 'after-init-hook
  143. (lambda ()
  144. (message "%s was taken to initialize emacs." (emacs-init-time))
  145. (switch-to-buffer "*Messages*")
  146. ))
  147. (cd ".") ; when using windows use / instead of \ in `default-directory'
  148. ;; locale
  149. (set-language-environment "Japanese")
  150. (set-default-coding-systems 'utf-8-unix)
  151. (prefer-coding-system 'utf-8-unix)
  152. (setq system-time-locale "C")
  153. ;; my prefix map
  154. (define-prefix-command 'my-prefix-map)
  155. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  156. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  157. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  158. ;; (comint-show-maximum-output)
  159. ;; kill scratch
  160. (add-hook 'after-init-hook
  161. (lambda ()
  162. (kill-buffer "*scratch*")))
  163. ;; modifier keys
  164. ;; (setq mac-option-modifier 'control)
  165. (setq w32-apps-modifier 'meta)
  166. ;; display
  167. (setq redisplay-dont-pause t)
  168. (setq visible-bell t)
  169. (setq ring-bell-function 'ignore)
  170. (mouse-avoidance-mode 'banish)
  171. (and window-system
  172. (fetch-library
  173. "https://raw.github.com/10sr/emacs-lisp/master/save-window-size.el"
  174. t)
  175. (require 'save-window-size nil t))
  176. (defun reload-init-file ()
  177. "Reload emacs init file."
  178. (interactive)
  179. (when (file-readable-p user-init-file)
  180. (load-file user-init-file)))
  181. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  182. ;; global keys
  183. (global-set-key (kbd "<up>") (lambda() (interactive) (scroll-down 1)))
  184. (global-set-key (kbd "<down>") (lambda() (interactive) (scroll-up 1)))
  185. (global-set-key (kbd "<left>") 'scroll-down)
  186. (global-set-key (kbd "<right>") 'scroll-up)
  187. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  188. (global-set-key (kbd "C-\\") help-map)
  189. (define-key ctl-x-map (kbd "DEL") help-map)
  190. (define-key ctl-x-map (kbd "C-h") help-map)
  191. (define-key help-map "a" 'apropos)
  192. ;; disable annoying keys
  193. (global-set-key [prior] 'ignore)
  194. (global-set-key (kbd "<next>") 'ignore)
  195. (global-set-key [menu] 'ignore)
  196. (global-set-key [down-mouse-1] 'ignore)
  197. (global-set-key [down-mouse-2] 'ignore)
  198. (global-set-key [down-mouse-3] 'ignore)
  199. (global-set-key [mouse-1] 'ignore)
  200. (global-set-key [mouse-2] 'ignore)
  201. (global-set-key [mouse-3] 'ignore)
  202. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  203. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  204. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  205. ;; title and mode-line
  206. (setq eol-mnemonic-dos "crlf")
  207. (setq eol-mnemonic-mac "cr")
  208. (setq eol-mnemonic-unix "lf")
  209. (which-function-mode 0)
  210. (line-number-mode 0)
  211. (column-number-mode 0)
  212. (size-indication-mode 0)
  213. (setq mode-line-position
  214. '(:eval (format "L%%l/%d,C%%c"
  215. (count-lines (point-max)
  216. (point-min)))))
  217. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  218. ;; display date
  219. (add-hook 'after-init-hook
  220. (lambda ()
  221. (when display-time-mode
  222. (display-time-update))
  223. ))
  224. (setq display-time-interval 29)
  225. (setq display-time-day-and-date t)
  226. (setq display-time-format "%a, %d %b %Y %T")
  227. (if window-system
  228. (display-time-mode 0)
  229. (display-time-mode 1))
  230. ;; ;; current directory
  231. ;; (let ((ls (member 'mode-line-buffer-identification
  232. ;; mode-line-format)))
  233. ;; (setcdr ls
  234. ;; (cons '(:eval (concat " ("
  235. ;; (abbreviate-file-name default-directory)
  236. ;; ")"))
  237. ;; (cdr ls))))
  238. ;; ;; display last modified time
  239. ;; (let ((ls (member 'mode-line-buffer-identification
  240. ;; mode-line-format)))
  241. ;; (setcdr ls
  242. ;; (cons '(:eval (concat " "
  243. ;; my-buffer-file-last-modified-time))
  244. ;; (cdr ls))))
  245. '(setq-default header-line-format (list " "
  246. 'display-time-string))
  247. (defvar set-terminal-title-term-regexp ""
  248. "Rexexp for `set-terminal-title'.")
  249. (setq set-terminal-title-term-regexp "^\\(rxvt\\|xterm\\|aterm$\\|screen\\)")
  250. (defun set-terminal-title (&rest args)
  251. ""
  252. (interactive "sString to set as title: ")
  253. (let ((tty (frame-parameter nil
  254. 'tty-type)))
  255. (when (and tty
  256. (string-match set-terminal-title-term-regexp
  257. tty))
  258. (send-string-to-terminal (apply 'concat
  259. "\033]0;"
  260. `(,@args "\007"))))))
  261. (defun my-set-terminal-title ()
  262. ""
  263. (set-terminal-title "["
  264. user-login-name
  265. "@"
  266. system-name
  267. ":"
  268. (abbreviate-file-name (or buffer-file-name
  269. default-directory))
  270. "]["
  271. invocation-name
  272. " "
  273. emacs-version
  274. " "
  275. (symbol-name system-type)
  276. "]["
  277. "FRAME:"
  278. (frame-parameter nil 'name)
  279. ":"
  280. (number-to-string (length
  281. (buffer-list-not-start-with-space)))
  282. "]"
  283. ))
  284. (add-hook 'buffer-file-changed-functions
  285. (lambda (p c)
  286. (my-set-terminal-title)))
  287. (add-hook 'suspend-resume-hook
  288. 'my-set-terminal-title)
  289. (defun buffer-list-not-start-with-space ()
  290. (let ((bl (buffer-list))
  291. b nbl)
  292. (while bl
  293. (setq b (pop bl))
  294. (unless (string-equal " "
  295. (substring (buffer-name b)
  296. 0
  297. 1))
  298. (add-to-list 'nbl b)))
  299. nbl))
  300. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  301. ;; minibuffer
  302. (setq insert-default-directory t)
  303. (setq completion-ignore-case t
  304. read-file-name-completion-ignore-case t
  305. read-buffer-completion-ignore-case t)
  306. (setq resize-mini-windows t)
  307. (temp-buffer-resize-mode 1)
  308. (savehist-mode 1)
  309. (fset 'yes-or-no-p 'y-or-n-p)
  310. ;; complete symbol when `eval'
  311. (define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol)
  312. (define-key minibuffer-local-map (kbd "C-u")
  313. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  314. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  315. ;; letters, font-lock mode and fonts
  316. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  317. (and (or (eq system-type 'Darwin)
  318. (eq system-type 'darwin))
  319. (fboundp 'mac-set-input-method-parameter)
  320. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  321. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  322. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  323. (boundp 'input-method-inactivate-hook))
  324. (add-hook 'input-method-activate-hook
  325. (lambda () (set-cursor-color "red")))
  326. (add-hook 'input-method-inactivate-hook
  327. (lambda () (set-cursor-color "black"))))
  328. (show-paren-mode 1)
  329. (setq show-paren-delay 0.5
  330. show-paren-style 'parenthesis) ; mixed is hard to read
  331. (set-face-background 'show-paren-match
  332. (face-foreground 'default))
  333. (set-face-inverse-video-p 'show-paren-match
  334. t)
  335. (transient-mark-mode 1)
  336. (global-font-lock-mode 1)
  337. (setq font-lock-global-modes
  338. '(not
  339. help-mode
  340. eshell-mode
  341. term-mode
  342. Man-mode))
  343. ;; (standard-display-ascii ?\n "$\n")
  344. (defvar my-eol-face
  345. '(("\n" . (0 font-lock-comment-face t nil)))
  346. )
  347. (defvar my-tab-face
  348. '(("\t" . '(0 highlight t nil))))
  349. (defvar my-jspace-face
  350. '(("\u3000" . '(0 highlight t nil))))
  351. (add-hook 'font-lock-mode-hook
  352. (lambda ()
  353. ;; (font-lock-add-keywords nil my-eol-face)
  354. (font-lock-add-keywords nil my-jspace-face)
  355. ))
  356. (when (require 'whitespace nil t)
  357. (setq whitespace-style '(face
  358. trailing ; trailing blanks
  359. newline ; newlines
  360. newline-mark ; use display table for newline
  361. empty ; empty lines at beg or end of buffer
  362. lines-tail ; lines over 80
  363. ))
  364. ;; (setq whitespace-newline 'font-lock-comment-face)
  365. (add-to-list 'whitespace-display-mappings
  366. `(newline-mark ?\n ,(vconcat "$\n"))
  367. )
  368. (global-whitespace-mode t))
  369. (and nil
  370. (fetch-library
  371. "http://www.emacswiki.org/emacs/download/fill-column-indicator.el"
  372. t)
  373. (require 'fill-column-indicator nil t)
  374. (setq fill-column-indicator))
  375. ;; highlight current line
  376. ;; http://wiki.riywo.com/index.php?Meadow
  377. (defface hlline-face
  378. '((((type x w32)
  379. (class color)
  380. (background dark))
  381. (:background "midnightblue"))
  382. (((type x w32)
  383. (class color)
  384. (background light))
  385. (:background "gainsboro"))
  386. (t
  387. (:underline "black")))
  388. "*Face used by hl-line.")
  389. ;; (defface hlline-ul-face
  390. ;; '((t (:underline "yellow")))
  391. ;; "underline yellow")
  392. (setq hl-line-face 'hlline-face) ;; (setq hl-line-face nil)
  393. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  394. (setq hl-line-global-modes
  395. '(not
  396. term-mode))
  397. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  398. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  399. ;; fonts
  400. (defun my-set-ascii-and-jp-font (list)
  401. ""
  402. (if (> emacs-major-version 22) ;; font spec is available in emacs23 and later
  403. (progn ; 23 or later
  404. (set-face-attribute 'default nil
  405. :family (nth 0 list)
  406. :height (nth 1 list))
  407. (set-fontset-font "fontset-default"
  408. 'japanese-jisx0208
  409. (font-spec :family (nth 2 list) :size (nth 3 list)))
  410. (set-fontset-font "fontset-default"
  411. 'katakana-jisx0201
  412. (font-spec :family (nth 2 list) :size (nth 3 list))))
  413. (progn ; 22
  414. (set-face-attribute 'default nil
  415. :family (nth 0 list)
  416. :height (nth 1 list))
  417. (set-fontset-font "fontset-default"
  418. 'japanese-jisx0208
  419. (cons (nth 2 list) "jisx0208.*"))
  420. (set-fontset-font "fontset-default"
  421. 'katakana-jisx0201
  422. (cons (nth 2 list) "jisx0201.*"))
  423. )))
  424. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 90 "takaogothic" 13))
  425. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "takaogothic" 14))
  426. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "ms gothic" 14))
  427. ;; (my-set-ascii-and-jp-font '("monaco" 75 "takaogothic" 11))
  428. ;; (my-set-ascii-and-jp-font '("monaco" 90 "takaogothic" 13))
  429. ;; (my-set-ascii-and-jp-font '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  430. ;; あ a
  431. (and (fetch-library
  432. "https://raw.github.com/10sr/emacs-lisp/master/set-modeline-color.el"
  433. t)
  434. (progn
  435. (require 'set-modeline-color nil t)))
  436. (let ((fg (face-foreground 'default))
  437. (bg (face-background 'default)))
  438. (set-face-background 'mode-line-inactive
  439. (if (face-inverse-video-p 'mode-line) fg bg))
  440. (set-face-foreground 'mode-line-inactive
  441. (if (face-inverse-video-p 'mode-line) bg fg)))
  442. (set-face-underline-p 'mode-line-inactive
  443. t)
  444. (set-face-underline-p 'vertical-border
  445. nil)
  446. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  447. ;; file handling
  448. (setq revert-without-query '(".+"))
  449. ;; save cursor position
  450. (setq save-place-file (concat user-emacs-directory
  451. "places"))
  452. (when (require 'saveplace nil t)
  453. (setq-default save-place t))
  454. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  455. (setq make-backup-files t)
  456. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  457. (setq backup-directory-alist
  458. (cons (cons "\\.*$" (expand-file-name "~/.emacs.d/backup"))
  459. backup-directory-alist))
  460. (setq version-control 'never)
  461. (setq delete-old-versions t)
  462. (setq auto-save-list-file-prefix (expand-file-name "~/.emacs.d/auto-save/"))
  463. (setq delete-auto-save-files t)
  464. (add-to-list 'completion-ignored-extensions ".bak")
  465. ;; (setq delete-by-moving-to-trash t
  466. ;; trash-directory "~/.emacs.d/trash")
  467. (add-hook 'after-save-hook
  468. 'executable-make-buffer-file-executable-if-script-p)
  469. (setq bookmark-default-file "~/.emacs.d/bmk")
  470. (and (fetch-library
  471. "https://github.com/10sr/emacs-lisp/raw/master/read-only-only-mode.el"
  472. t)
  473. (lazy-load-eval 'read-only-only-mode))
  474. (and (fetch-library
  475. "https://raw.github.com/10sr/emacs-lisp/master/smart-revert.el"
  476. t)
  477. (require 'smart-revert nil t)
  478. (smart-revert-on)
  479. )
  480. ;; autosave
  481. (and (fetch-library
  482. "https://raw.github.com/10sr/emacs-lisp/master/autosave.el"
  483. t)
  484. (require 'autosave nil t)
  485. (autosave-set 2))
  486. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  487. ;; editting
  488. (defun my-copy-whole-line ()
  489. ""
  490. (interactive)
  491. (kill-new (concat (buffer-substring (point-at-bol)
  492. (point-at-eol))
  493. "\n")))
  494. (setq require-final-newline t)
  495. (setq kill-whole-line t)
  496. (setq scroll-conservatively 35
  497. scroll-margin 2
  498. scroll-step 0)
  499. (setq default-major-mode 'text-mode)
  500. (setq next-line-add-newlines nil)
  501. (setq kill-read-only-ok t)
  502. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  503. ;; (setq-default line-spacing 0.2)
  504. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  505. (setq-default tab-width 4)
  506. (setq-default indent-tabs-mode nil)
  507. (setq-default indent-line-function nil)
  508. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  509. (delete-selection-mode 1)
  510. (cua-mode 0)
  511. (setq line-move-visual nil)
  512. ;; key bindings
  513. ;; moving around
  514. ;; (global-set-key (kbd "M-j") 'next-line)
  515. ;; (global-set-key (kbd "M-k") 'previous-line)
  516. ;; (global-set-key (kbd "M-h") 'backward-char)
  517. ;; (global-set-key (kbd "M-l") 'forward-char)
  518. ;;(keyboard-translate ?\M-j ?\C-j)
  519. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  520. (define-key esc-map "p" 'backward-paragraph)
  521. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  522. (define-key esc-map "n" 'forward-paragraph)
  523. (global-set-key (kbd "C-<up>") (lambda () (interactive)(scroll-down 1)))
  524. (global-set-key (kbd "C-<down>") (lambda () (interactive)(scroll-up 1)))
  525. (global-set-key (kbd "C-<left>") 'scroll-down)
  526. (global-set-key (kbd "C-<right>") 'scroll-up)
  527. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  528. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  529. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  530. ;; C-h and DEL
  531. (global-set-key (kbd "C-h") (kbd "DEL"))
  532. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  533. (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  534. (define-key esc-map "k" 'my-copy-whole-line)
  535. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  536. (define-key esc-map "u" 'undo)
  537. (define-key esc-map "i" (kbd "ESC TAB"))
  538. ;(global-set-key (kbd "C-r") 'query-replace-regexp)
  539. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  540. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  541. (define-key my-prefix-map (kbd "C-o") 'occur)
  542. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  543. ;; gmail
  544. (setq mail-interactive t
  545. send-mail-function 'smtpmail-send-it
  546. ;; message-send-mail-function 'smtpmail-send-it
  547. smtpmail-smtp-server "smtp.gmail.com"
  548. smtpmail-smtp-service 587
  549. smtpmail-starttls-credentials '(("smtp.gmail.com" 587
  550. "8.slashes@gmail.com" nil))
  551. smtpmail-auth-credentials '(("smtp.gmail.com" 587
  552. "8.slashes@gmail.com" nil))
  553. user-mail-address "8.slashes@gmail.com")
  554. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  555. ;; buffer killing
  556. (defun my-delete-window-killing-buffer () nil)
  557. (defun my-query-kill-current-buffer ()
  558. ""
  559. (interactive)
  560. (if (y-or-n-p (concat "kill current buffer? :"))
  561. (kill-buffer (current-buffer))))
  562. (substitute-key-definition 'kill-buffer 'my-query-kill-current-buffer global-map)
  563. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  564. (defun my-kill-buffers ()
  565. ""
  566. (interactive)
  567. (mapcar (lambda (buf)
  568. (when (buffer-file-name buf)
  569. (kill-buffer buf)))
  570. (buffer-list)))
  571. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  572. ;; share clipboard with x
  573. ;; this page describes this in details, but only these sexps seem to be needed
  574. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  575. (and (not window-system)
  576. (not (eq window-system 'mac))
  577. (getenv "DISPLAY")
  578. (not (equal (getenv "DISPLAY") ""))
  579. (executable-find "xclip")
  580. ;; (< emacs-major-version 24)
  581. (fetch-library "http://www.emacswiki.org/emacs/download/xclip.el" t)
  582. (require 'xclip nil t)
  583. (turn-on-xclip))
  584. (and (eq system-type 'darwin)
  585. (fetch-library
  586. "https://raw.github.com/10sr/emacs-lisp/master/pasteboard.el"
  587. t)
  588. (require 'pasteboard nil t)
  589. (turn-on-pasteboard)
  590. (getenv "TMUX")
  591. (pasteboard-enable-rtun))
  592. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  593. ;; package
  594. (when (require 'package nil t)
  595. (add-to-list 'package-archives
  596. '("melpa" . "http://melpa.milkbox.net/packages/")
  597. t)
  598. (add-to-list 'package-archives
  599. '("marmalade" . "http://marmalade-repo.org/packages/"))
  600. (add-to-list 'package-archives
  601. '("ELPA" . "http://tromey.com/elpa/"))
  602. (package-initialize))
  603. (lazy-load-eval 'sudoku)
  604. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  605. ;; window
  606. ;; forked from http://d.hatena.ne.jp/khiker/20100119/window_resize
  607. (define-key my-prefix-map (kbd "C-w") 'my-window-organizer)
  608. (defun my-window-organizer ()
  609. "Control window size and position."
  610. (interactive)
  611. (save-selected-window
  612. (select-window (window-at 0 0))
  613. (let ( ;; (window-obj (selected-window))
  614. ;; (current-width (window-width))
  615. ;; (current-height (window-height))
  616. action
  617. c)
  618. (catch 'end-flag
  619. (while t
  620. (setq action
  621. (read-key-sequence-vector
  622. (format "size[%dx%d] 1: maximize; 2, 3: split; 0: \
  623. delete; o: select other; j, l: enlarge; h, k: shrink; q: quit."
  624. (window-width)
  625. (window-height))))
  626. (setq c (aref action 0))
  627. (cond ((= c ?l)
  628. (unless (eq (window-width) (frame-width))
  629. (enlarge-window-horizontally 1)))
  630. ((= c ?h)
  631. (unless (eq (window-width) (frame-width))
  632. (shrink-window-horizontally 1)))
  633. ((= c ?j)
  634. (enlarge-window 1))
  635. ((= c ?k)
  636. (shrink-window 1))
  637. ((= c ?o)
  638. (other-window 1))
  639. ((memq c '(?d ?0))
  640. (unless (eq (selected-window)
  641. (next-window (selected-window) 0 1))
  642. (delete-window (selected-window))))
  643. ((= c ?1)
  644. (delete-other-windows))
  645. ((= c ?2)
  646. (split-window-vertically))
  647. ((= c ?3)
  648. (split-window-horizontally))
  649. ((memq c '(?q ?\C-g))
  650. (message "Quit")
  651. (throw 'end-flag t))
  652. (t
  653. (beep))))))))
  654. ;; (aref (read-key-sequence-vector "aa") 0)
  655. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  656. ;; some modes and hooks
  657. ;; http://fukuyama.co/foreign-regexp
  658. '(and (fetch-library
  659. "https://raw.github.com/k-talo/foreign-regexp.el/master/foreign-regexp.el"
  660. t)
  661. (require 'foreign-regexp nil t)
  662. (progn
  663. (setq foreign-regexp/regexp-type 'perl)
  664. '(setq reb-re-syntax 'foreign-regexp)
  665. ))
  666. (require 'session nil t)
  667. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/gtkbm.el"
  668. t)
  669. (lazy-load-eval 'gtkbm)
  670. (global-set-key (kbd "C-x C-d") 'gtkbm))
  671. (and (fetch-library
  672. "https://raw.github.com/10sr/emacs-lisp/master/git-command.el"
  673. t)
  674. (lazy-load-eval 'git-command)
  675. (define-key ctl-x-map "g" 'git-command))
  676. (and (fetch-library
  677. "http://www.emacswiki.org/emacs/download/sl.el"
  678. t)
  679. (lazy-load-eval 'sl))
  680. (defalias 'qcalc 'quick-calc)
  681. (require 'simple nil t)
  682. (add-hook 'makefile-mode-hook
  683. (lambda ()
  684. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)))
  685. (defun make ()
  686. "Run \"make -k\" in current directory."
  687. (interactive)
  688. (compile "make -k"))
  689. (add-hook 'verilog-mode-hook
  690. (lambda ()
  691. (define-key verilog-mode-map ";" 'self-insert-command)))
  692. (setq diff-switches "-u")
  693. (add-hook 'diff-mode-hook
  694. (lambda ()
  695. (view-mode 1)
  696. (set-face-attribute 'diff-header nil
  697. :foreground nil
  698. :background nil
  699. :weight 'bold)
  700. (set-face-attribute 'diff-file-header nil
  701. :foreground nil
  702. :background nil
  703. :weight 'bold)
  704. (set-face-foreground 'diff-index-face "blue")
  705. (set-face-attribute 'diff-hunk-header nil
  706. :foreground "cyan"
  707. :weight 'normal)
  708. (set-face-attribute 'diff-context nil
  709. ;; :foreground "white"
  710. :foreground nil
  711. :weight 'normal)
  712. (set-face-foreground 'diff-removed-face "red")
  713. (set-face-foreground 'diff-added-face "green")
  714. (set-face-attribute 'diff-changed nil
  715. :foreground "magenta"
  716. :weight 'normal)
  717. ))
  718. ;; (ffap-bindings)
  719. (add-hook 'sh-mode-hook
  720. (lambda ()
  721. (define-key sh-mode-map
  722. (kbd "C-x C-e")
  723. 'my-execute-shell-command-current-line)))
  724. (defun my-execute-shell-command-current-line ()
  725. ""
  726. (interactive)
  727. (shell-command (buffer-substring-no-properties (point-at-bol)
  728. (point))))
  729. (setq auto-mode-alist
  730. `(("autostart\\'" . sh-mode)
  731. ("xinitrc\\'" . sh-mode)
  732. ("xprograms\\'" . sh-mode)
  733. ("PKGBUILD\\'" . sh-mode)
  734. ,@auto-mode-alist))
  735. (and (lazy-load-eval 'pkgbuild-mode)
  736. (setq auto-mode-alist (append '(("PKGBUILD\\'" . pkgbuild-mode))
  737. auto-mode-alist)))
  738. (add-hook 'text-mode-hook
  739. (lambda ()
  740. (define-key text-mode-map (kbd "C-m") 'newline)))
  741. (add-to-list 'Info-default-directory-list (expand-file-name "~/.info/emacs-ja"))
  742. (add-hook 'apropos-mode-hook
  743. (lambda ()
  744. (define-key apropos-mode-map "n" 'next-line)
  745. (define-key apropos-mode-map "p" 'previous-line)
  746. ))
  747. (add-hook 'isearch-mode-hook
  748. (lambda ()
  749. ;; (define-key isearch-mode-map
  750. ;; (kbd "C-j") 'isearch-other-control-char)
  751. ;; (define-key isearch-mode-map
  752. ;; (kbd "C-k") 'isearch-other-control-char)
  753. ;; (define-key isearch-mode-map
  754. ;; (kbd "C-h") 'isearch-other-control-char)
  755. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  756. (define-key isearch-mode-map (kbd "M-r")
  757. 'isearch-query-replace-regexp)))
  758. (add-hook 'outline-mode-hook
  759. (lambda ()
  760. (if (string-match "\\.md\\'" buffer-file-name)
  761. (set (make-local-variable 'outline-regexp) "#+ "))))
  762. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  763. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  764. (when (fetch-library
  765. "http://jblevins.org/projects/markdown-mode/markdown-mode.el"
  766. t)
  767. (lazy-load-eval 'markdown-mode)
  768. (setq markdown-command (or (executable-find "markdown")
  769. (executable-find "markdown.pl")))
  770. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'markdown-mode))
  771. (add-hook 'markdown-mode-hook
  772. (lambda ()
  773. (outline-minor-mode 1)
  774. (set (make-local-variable 'comment-start) ";"))))
  775. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  776. ;; c-mode
  777. ;; (setq c-default-style "bsd")
  778. (add-hook 'c-mode-common-hook
  779. (lambda ()
  780. (setq c-basic-offset 2
  781. indent-tabs-mode nil)
  782. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  783. (c-toggle-hungry-state -1)
  784. (and (require 'gtags nil t)
  785. (gtags-mode 1))
  786. ))
  787. (when (fetch-library
  788. "https://raw.github.com/mooz/js2-mode/master/js2-mode.el"
  789. t)
  790. (lazy-load-eval 'js2-mode)
  791. (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  792. (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  793. ;; (add-hook 'js2-mode-hook
  794. ;; (lambda ()
  795. (add-hook 'js2-mode-hook
  796. (lambda ()
  797. (define-key js2-mode-map (kbd "C-m") (lambda ()
  798. (interactive)
  799. (js2-enter-key)
  800. (indent-for-tab-command)))
  801. (add-hook (kill-local-variable 'before-save-hook)
  802. 'js2-before-save)
  803. ;; (add-hook 'before-save-hook
  804. ;; 'my-indent-buffer
  805. ;; nil
  806. ;; t)
  807. )))
  808. (when (require 'uniquify nil t)
  809. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  810. (setq uniquify-ignore-buffers-re "*[^*]+*")
  811. (setq uniquify-min-dir-content 1))
  812. (add-hook 'view-mode-hook
  813. (lambda()
  814. (define-key view-mode-map "j"
  815. (lambda() (interactive) (scroll-up 1)))
  816. (define-key view-mode-map "k"
  817. (lambda() (interactive) (scroll-down 1)))
  818. (define-key view-mode-map "v" 'toggle-read-only)
  819. (define-key view-mode-map "q" 'bury-buffer)
  820. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  821. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  822. ;; (define-key view-mode-map
  823. ;; "n" 'nonincremental-repeat-search-forward)
  824. ;; (define-key view-mode-map
  825. ;; "N" 'nonincremental-repeat-search-backward)
  826. (define-key view-mode-map "/" 'isearch-forward-regexp)
  827. (define-key view-mode-map "?" 'isearch-backward-regexp)
  828. (define-key view-mode-map "n" 'isearch-repeat-forward)
  829. (define-key view-mode-map "N" 'isearch-repeat-backward)
  830. ))
  831. (global-set-key "\M-r" 'view-mode)
  832. (setq view-read-only t)
  833. (add-hook 'Man-mode-hook
  834. (lambda ()
  835. (view-mode 1)
  836. (setq truncate-lines nil)))
  837. (setq Man-notify-method (if window-system
  838. 'newframe
  839. 'pushy))
  840. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  841. ;; python
  842. (setq python-python-command (or (executable-find "python3")
  843. (executable-find "python")))
  844. (defun my-python-run-as-command ()
  845. ""
  846. (interactive)
  847. (shell-command (concat python-python-command " " buffer-file-name)))
  848. (defun my-python-display-python-buffer ()
  849. ""
  850. (interactive)
  851. (set-window-text-height (display-buffer python-buffer
  852. t)
  853. 7))
  854. (add-hook 'python-mode-hook
  855. (lambda ()
  856. (define-key python-mode-map
  857. (kbd "C-c C-e") 'my-python-run-as-command)
  858. (define-key python-mode-map
  859. (kbd "C-c C-b") 'my-python-display-python-buffer)
  860. (define-key python-mode-map (kbd "C-m") 'newline-and-indent)))
  861. (add-hook 'inferior-python-mode-hook
  862. (lambda ()
  863. (my-python-display-python-buffer)
  864. (define-key inferior-python-mode-map
  865. (kbd "<up>") 'comint-previous-input)
  866. (define-key inferior-python-mode-map
  867. (kbd "<down>") 'comint-next-input)))
  868. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  869. ;; GNU GLOBAL(gtags)
  870. ;; http://uguisu.skr.jp/Windows/gtags.html
  871. ;; http://eigyr.dip.jp/gtags.html
  872. ;; http://cha.la.coocan.jp/doc/gnu_global.html
  873. (when (lazy-load-eval 'gtags '(gtags-mode))
  874. (setq gtags-mode-hook
  875. '(lambda ()
  876. (setq gtags-select-buffer-single t)
  877. ;; (local-set-key "\M-t" 'gtags-find-tag)
  878. ;; (local-set-key "\M-r" 'gtags-find-rtag)
  879. ;; (local-set-key "\M-s" 'gtags-find-symbol)
  880. ;; (local-set-key "\C-t" 'gtags-pop-stack)
  881. (define-key gtags-mode-map (kbd "C-x t h") 'gtags-find-tag-from-here)
  882. (define-key gtags-mode-map (kbd "C-x t t") 'gtags-find-tag)
  883. (define-key gtags-mode-map (kbd "C-x t r") 'gtags-find-rtag)
  884. (define-key gtags-mode-map (kbd "C-x t s") 'gtags-find-symbol)
  885. (define-key gtags-mode-map (kbd "C-x t p") 'gtags-find-pattern)
  886. (define-key gtags-mdoe-map (kbd "C-x t f") 'gtags-find-file)
  887. (define-key gtags-mode-map (kbd "C-x t b") 'gtags-pop-stack) ;back
  888. )))
  889. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  890. ;; term mode
  891. ;; (setq multi-term-program shell-file-name)
  892. (and (fetch-library "http://www.emacswiki.org/emacs/download/multi-term.el"
  893. t)
  894. (lazy-load-eval 'multi-term)
  895. (progn
  896. (setq multi-term-switch-after-close nil)
  897. (setq multi-term-dedicated-select-after-open-p t)
  898. (setq multi-term-dedicated-window-height 20)))
  899. (when (lazy-load-eval 'term '(term ansi-term))
  900. (defun my-term-quit-or-send-raw ()
  901. ""
  902. (interactive)
  903. (if (get-buffer-process (current-buffer))
  904. (call-interactively 'term-send-raw)
  905. (kill-buffer)))
  906. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  907. ;; (setq term-ansi-default-program shell-file-name)
  908. (add-hook 'term-setup-hook
  909. (lambda ()
  910. (setq term-display-table (make-display-table))))
  911. (add-hook 'term-mode-hook
  912. (lambda ()
  913. (unless (memq (current-buffer)
  914. (and (featurep 'multi-term)
  915. ;; current buffer is not multi-term buffer
  916. (multi-term-list)))
  917. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  918. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  919. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  920. ;; (define-key term-raw-map "\C-f" 'forward-char)
  921. ;; (define-key term-raw-map "\C-b" 'backward-char)
  922. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  923. (define-key term-raw-map
  924. "\C-x" (lookup-key (current-global-map) "\C-x"))
  925. (define-key term-raw-map
  926. "\C-z" (lookup-key (current-global-map) "\C-z"))
  927. )
  928. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  929. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  930. (define-key term-raw-map (kbd "<up>")
  931. (lambda () (interactive) (scroll-down 1)))
  932. (define-key term-raw-map (kbd "<down>")
  933. (lambda () (interactive) (scroll-up 1)))
  934. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  935. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  936. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  937. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  938. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  939. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  940. (define-key term-raw-map [delete] 'term-send-raw)
  941. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  942. (define-key term-raw-map "\C-y" 'term-paste)
  943. (define-key term-raw-map
  944. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  945. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  946. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  947. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  948. ;; (define-key term-raw-map "\C-d" 'delete-char)
  949. (set (make-local-variable 'scroll-margin) 0)
  950. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  951. ;; (cua-mode 0)
  952. ;; (and cua-mode
  953. ;; (local-unset-key (kbd "C-c")))
  954. ;; (define-key cua--prefix-override-keymap
  955. ;;"\C-c" 'term-interrupt-subjob)
  956. (set (make-local-variable 'hl-line-range-function)
  957. (lambda ()
  958. '(0 . 0)))
  959. ))
  960. ;; (add-hook 'term-exec-hook 'forward-char)
  961. )
  962. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  963. ;; buffer switching
  964. (when (lazy-load-eval 'bs '(bs-show)
  965. ;; (add-to-list 'bs-configurations
  966. ;; '("processes" nil get-buffer-process ".*" nil nil))
  967. (add-to-list 'bs-configurations
  968. '("same-dir" nil buffer-same-dir-p ".*" nil nil))
  969. (add-to-list 'bs-configurations
  970. '("this-frame" nil (lambda (buf)
  971. (memq buf (my-frame-buffer-get)))
  972. ".*" nil nil))
  973. ;; (setq bs-configurations (list
  974. ;; '("processes" nil get-buffer-process ".*" nil nil)
  975. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  976. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  977. )
  978. ;; (global-set-key "\C-x\C-b" 'bs-show)
  979. (defalias 'list-buffers 'bs-show)
  980. (setq bs-default-configuration "files")
  981. (setq bs-default-sort-name "by name")
  982. (add-hook 'bs-mode-hook
  983. (lambda ()
  984. (setq bs-default-configuration "files")
  985. ;; (and bs--show-all
  986. ;; (call-interactively 'bs-toggle-show-all))
  987. (set (make-local-variable 'scroll-margin) 0)))
  988. (defun buffer-same-dir-p (bf)
  989. "return t if BF's dir is same as current dir, otherwise nil."
  990. (let ((cdir (expand-file-name default-directory)))
  991. (with-current-buffer bf
  992. (equal (expand-file-name default-directory) cdir)))))
  993. (iswitchb-mode 1)
  994. (defun iswitchb-buffer-display-other-window ()
  995. ""
  996. (interactive)
  997. (let ((iswitchb-default-method 'display))
  998. (call-interactively 'iswitchb-buffer)))
  999. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1000. ;; sdic
  1001. (when (lazy-load-eval 'sdic '(sdic-describe-word-at-point))
  1002. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  1003. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo)
  1004. (defun sdic-describe-word-at-point-echo ()
  1005. ""
  1006. (interactive)
  1007. (save-window-excursion
  1008. (sdic-describe-word-at-point))
  1009. (save-excursion
  1010. (set-buffer sdic-buffer-name)
  1011. (message (buffer-substring (point-min)
  1012. (progn (goto-char (point-min))
  1013. (or (and (re-search-forward "^\\w"
  1014. nil
  1015. t
  1016. 4)
  1017. (progn (previous-line) t)
  1018. (point-at-eol))
  1019. (point-max)))))))
  1020. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  1021. (setq sdic-waei-dictionary-list
  1022. '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  1023. (setq sdic-disable-select-window t)
  1024. (setq sdic-window-height 7))
  1025. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1026. ;; vc
  1027. ;; (require 'vc)
  1028. (setq vc-handled-backends '())
  1029. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1030. ;; gauche-mode
  1031. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  1032. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  1033. (when (and (fetch-library
  1034. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  1035. t)
  1036. (lazy-load-eval 'gauche-mode '(gauche-mode run-scheme)))
  1037. (let ((s (executable-find "gosh")))
  1038. (setq scheme-program-name s
  1039. gauche-program-name s))
  1040. (defun run-gauche-other-window ()
  1041. "Run gauche on other window"
  1042. (interactive)
  1043. (switch-to-buffer-other-window
  1044. (get-buffer-create "*scheme*"))
  1045. (run-gauche))
  1046. (defun run-gauche ()
  1047. "run gauche"
  1048. (run-scheme gauche-program-name)
  1049. )
  1050. (defun scheme-send-buffer ()
  1051. ""
  1052. (interactive)
  1053. (scheme-send-region (point-min) (point-max))
  1054. (my-scheme-display-scheme-buffer)
  1055. )
  1056. (defun my-scheme-display-scheme-buffer ()
  1057. ""
  1058. (interactive)
  1059. (set-window-text-height (display-buffer scheme-buffer
  1060. t)
  1061. 7))
  1062. (add-hook 'scheme-mode-hook
  1063. (lambda ()
  1064. nil))
  1065. (add-hook 'inferior-scheme-mode-hook
  1066. (lambda ()
  1067. ;; (my-scheme-display-scheme-buffer)
  1068. ))
  1069. (setq auto-mode-alist
  1070. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1071. (setq auto-mode-alist
  1072. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1073. (add-hook 'gauche-mode-hook
  1074. (lambda ()
  1075. (define-key gauche-mode-map
  1076. (kbd "C-c C-z") 'run-gauche-other-window)
  1077. (define-key scheme-mode-map
  1078. (kbd "C-c C-c") 'scheme-send-buffer)
  1079. (define-key scheme-mode-map
  1080. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer))))
  1081. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1082. ;; recentf-mode
  1083. (setq recentf-save-file (expand-file-name "~/.emacs.d/recentf")
  1084. recentf-max-menu-items 20
  1085. recentf-max-saved-items 30
  1086. recentf-show-file-shortcuts-flag nil)
  1087. (when (require 'recentf nil t)
  1088. (add-to-list 'recentf-exclude (regexp-quote recentf-save-file))
  1089. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1090. (add-hook 'find-file-hook
  1091. 'recentf-save-list
  1092. t) ; save to file immediately after adding file to recentf list
  1093. (add-hook 'kill-emacs-hook
  1094. 'recentf-load-list)
  1095. (add-hook 'recentf-mode-hook
  1096. 'recentf-save-list)
  1097. ;; (add-hook 'find-file-hook
  1098. ;; (lambda ()
  1099. ;; (recentf-add-file default-directory)))
  1100. (and (fetch-library
  1101. "https://raw.github.com/10sr/emacs-lisp/master/recentf-show.el"
  1102. t)
  1103. (lazy-load-eval 'recentf-show)
  1104. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1105. (add-hook 'recentf-show-before-listing-hook
  1106. 'recentf-load-list))
  1107. (recentf-mode 1)
  1108. (add-hook 'recentf-dialog-mode-hook
  1109. (lambda ()
  1110. ;; (recentf-save-list)
  1111. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1112. ;; 'my-recentf-cd-and-find-file)
  1113. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1114. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1115. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1116. (define-key recentf-dialog-mode-map "n" 'next-line)
  1117. (cd "~/"))))
  1118. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1119. ;; dired
  1120. (when (lazy-load-eval 'dired nil
  1121. (defun my-dired-echo-file-head (arg)
  1122. ""
  1123. (interactive "P")
  1124. (let ((f (dired-get-filename)))
  1125. (message "%s"
  1126. (with-temp-buffer
  1127. (insert-file-contents f)
  1128. (buffer-substring-no-properties
  1129. (point-min)
  1130. (progn (goto-line (if arg
  1131. (prefix-numeric-value arg)
  1132. 10))
  1133. (point-at-eol)))))))
  1134. (defun my-dired-diff ()
  1135. ""
  1136. (interactive)
  1137. (let ((files (dired-get-marked-files nil nil nil t)))
  1138. (if (eq (car files)
  1139. t)
  1140. (diff (cadr files) (dired-get-filename))
  1141. (message "One files must be marked!"))))
  1142. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1143. "pop up buffer using `display-buffer' and return that buffer."
  1144. (let ((bf (get-buffer-create buffer-or-name)))
  1145. (with-current-buffer bf
  1146. (cd ".")
  1147. (erase-buffer))
  1148. (display-buffer bf)
  1149. bf))
  1150. (defun my-replace-nasi-none ()
  1151. ""
  1152. (save-excursion
  1153. (let ((buffer-read-only nil))
  1154. (goto-char (point-min))
  1155. (while (search-forward "なし" nil t)
  1156. (replace-match "none")))))
  1157. (defun dired-get-file-info ()
  1158. "dired get file info"
  1159. (interactive)
  1160. (let ((f (shell-quote-argument (dired-get-filename t))))
  1161. (if (file-directory-p f)
  1162. (progn
  1163. (message "Calculating disk usage...")
  1164. (shell-command (concat "du -hsD "
  1165. f)))
  1166. (shell-command (concat "file "
  1167. f)))))
  1168. (defun my-dired-scroll-up ()
  1169. ""
  1170. (interactive)
  1171. (my-dired-previous-line (- (window-height) 1)))
  1172. (defun my-dired-scroll-down ()
  1173. ""
  1174. (interactive)
  1175. (my-dired-next-line (- (window-height) 1)))
  1176. ;; (defun my-dired-forward-line (arg)
  1177. ;; ""
  1178. ;; (interactive "p"))
  1179. (defun my-dired-previous-line (arg)
  1180. ""
  1181. (interactive "p")
  1182. (if (> arg 0)
  1183. (progn
  1184. (if (eq (line-number-at-pos)
  1185. 1)
  1186. (goto-char (point-max))
  1187. (forward-line -1))
  1188. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1189. (dired-get-subdir))
  1190. (- arg 1)
  1191. arg)))
  1192. (dired-move-to-filename)))
  1193. (defun my-dired-next-line (arg)
  1194. ""
  1195. (interactive "p")
  1196. (if (> arg 0)
  1197. (progn
  1198. (if (eq (point)
  1199. (point-max))
  1200. (goto-char (point-min))
  1201. (forward-line 1))
  1202. (my-dired-next-line (if (or (dired-get-filename nil t)
  1203. (dired-get-subdir))
  1204. (- arg 1)
  1205. arg)))
  1206. (dired-move-to-filename)))
  1207. (defun my-dired-print-current-dir-and-file ()
  1208. (message "%s %s"
  1209. default-directory
  1210. (buffer-substring-no-properties (point-at-bol)
  1211. (point-at-eol))))
  1212. (defun dired-do-execute-as-command ()
  1213. ""
  1214. (interactive)
  1215. (let ((file (dired-get-filename t)))
  1216. (if (file-executable-p file)
  1217. (start-process file nil file)
  1218. (when (y-or-n-p
  1219. "this file cant be executed. mark as executable and go? : ")
  1220. (set-file-modes file
  1221. (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1222. (start-process file nil file)))))
  1223. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1224. (defun my-dired-x-open ()
  1225. ""
  1226. (interactive)
  1227. (my-x-open (dired-get-filename t t)))
  1228. (if (eq window-system 'mac)
  1229. (setq dired-listing-switches "-lhFG")
  1230. (setq dired-listing-switches "-lhFG --time-style=long-iso")
  1231. )
  1232. (setq dired-listing-switches "-lhFG")
  1233. (put 'dired-find-alternate-file 'disabled nil)
  1234. ;; when using dired-find-alternate-file
  1235. ;; reuse current dired buffer for the file to open
  1236. (setq dired-ls-F-marks-symlinks t)
  1237. (require 'ls-lisp)
  1238. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1239. (setq ls-lisp-dirs-first t)
  1240. (setq ls-lisp-use-localized-time-format t)
  1241. (setq ls-lisp-format-time-list
  1242. '("%Y-%m-%d %H:%M"
  1243. "%Y-%m-%d "))
  1244. (setq dired-dwim-target t)
  1245. ;; (add-hook 'dired-after-readin-hook
  1246. ;; 'my-replace-nasi-none)
  1247. ;; (add-hook 'after-init-hook
  1248. ;; (lambda ()
  1249. ;; (dired ".")))
  1250. (add-hook 'dired-mode-hook
  1251. (lambda ()
  1252. (define-key dired-mode-map "o" 'my-dired-x-open)
  1253. (define-key dired-mode-map "i" 'dired-get-file-info)
  1254. (define-key dired-mode-map "f" 'find-file)
  1255. (define-key dired-mode-map "!" 'shell-command)
  1256. (define-key dired-mode-map "&" 'async-shell-command)
  1257. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1258. (define-key dired-mode-map "=" 'my-dired-diff)
  1259. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1260. (define-key dired-mode-map "b" 'gtkbm)
  1261. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1262. (define-key dired-mode-map "@" (lambda ()
  1263. (interactive) (my-x-open ".")))
  1264. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1265. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1266. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1267. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1268. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1269. (substitute-key-definition 'dired-next-line
  1270. 'my-dired-next-line dired-mode-map)
  1271. (substitute-key-definition 'dired-previous-line
  1272. 'my-dired-previous-line dired-mode-map)
  1273. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1274. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1275. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1276. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1277. (let ((file "._Icon\015"))
  1278. (when nil (file-readable-p file)
  1279. (delete-file file)))))
  1280. ) ; eval after load dired
  1281. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  1282. t)
  1283. (lazy-load-eval 'pack '(dired-do-pack-or-unpack pack))
  1284. (add-hook 'dired-mode-hook
  1285. (lambda ()
  1286. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack))))
  1287. (and (fetch-library
  1288. "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  1289. t)
  1290. (lazy-load-eval 'dired-list-all-mode)
  1291. (setq dired-listing-switches "-lhFG")
  1292. (add-hook 'dired-mode-hook
  1293. (lambda ()
  1294. (define-key dired-mode-map "a" 'dired-list-all-mode)
  1295. )))
  1296. ) ; when dired locate
  1297. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1298. (defun my-dired-toggle-mark()
  1299. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1300. (t dired-marker-char))))
  1301. (delete-char 1)
  1302. (insert cur)))
  1303. (defun my-dired-mark (arg)
  1304. "toggle mark the current (or next ARG) files.
  1305. If on a subdir headerline, mark all its files except `.' and `..'.
  1306. Use \\[dired-unmark-all-files] to remove all marks
  1307. and \\[dired-unmark] on a subdir to remove the marks in
  1308. this subdir."
  1309. (interactive "P")
  1310. (if (dired-get-subdir)
  1311. (save-excursion (dired-mark-subdir-files))
  1312. (let ((inhibit-read-only t))
  1313. (dired-repeat-over-lines
  1314. (prefix-numeric-value arg)
  1315. 'my-dired-toggle-mark))))
  1316. (defun my-dired-mark-backward (arg)
  1317. "In Dired, move up lines and toggle mark there.
  1318. Optional prefix ARG says how many lines to unflag; default is one line."
  1319. (interactive "p")
  1320. (my-dired-mark (- arg)))
  1321. (add-hook 'dired-mode-hook
  1322. (lambda ()
  1323. (local-set-key (kbd "SPC") 'my-dired-mark)
  1324. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1325. )
  1326. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1327. ;; eshell
  1328. (lazy-load-eval 'eshell nil
  1329. (defun my-eshell-backward-delete-char ()
  1330. (interactive)
  1331. (when (< (save-excursion
  1332. (eshell-bol)
  1333. (point))
  1334. (point))
  1335. (backward-delete-char 1)))
  1336. (defun my-file-owner-p (file)
  1337. "t if FILE is owned by me."
  1338. (eq (user-uid) (nth 2 (file-attributes file))))
  1339. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1340. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1341. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1342. ;; (defun eshell/less (&rest args)
  1343. ;; "Invoke `view-file' on the file.
  1344. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1345. ;; (if args
  1346. ;; (while args
  1347. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1348. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1349. ;; (file (pop args)))
  1350. ;; (view-file file)
  1351. ;; (goto-line line))
  1352. ;; (view-file (pop args))))))
  1353. (defun eshell/o (&optional file)
  1354. (my-x-open (or file ".")))
  1355. ;; (defun eshell/vi (&rest args)
  1356. ;; "Invoke `find-file' on the file.
  1357. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1358. ;; (while args
  1359. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1360. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1361. ;; (file (pop args)))
  1362. ;; (find-file file)
  1363. ;; (goto-line line))
  1364. ;; (find-file (pop args)))))
  1365. (defun eshell/clear ()
  1366. "Clear the current buffer, leaving one prompt at the top."
  1367. (let ((inhibit-read-only t))
  1368. (erase-buffer)))
  1369. (defun eshell/d (&optional dirname switches)
  1370. "if first arg is omitted open current directory."
  1371. (dired (or dirname ".") switches))
  1372. (defun eshell/v ()
  1373. (view-mode 1))
  1374. (defun eshell/git (&rest args)
  1375. ""
  1376. (if (member (car args)
  1377. '("di" "diff" "log" "show"))
  1378. (apply 'eshell-exec-visual "git" args)
  1379. (shell-command (mapconcat 'shell-quote-argument
  1380. `("git" ,@args)
  1381. " ")
  1382. t)
  1383. ;; (eshell-external-command "git" args)
  1384. ))
  1385. (defalias 'eshell/: 'ignore)
  1386. (defalias 'eshell/type 'eshell/which)
  1387. ;; (defalias 'eshell/vim 'eshell/vi)
  1388. (defalias 'eshell/ff 'find-file)
  1389. (defalias 'eshell/q 'eshell/exit)
  1390. (defun eshell-goto-prompt ()
  1391. ""
  1392. (interactive)
  1393. (goto-char (point-max)))
  1394. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1395. "open eshell and change wd
  1396. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1397. (interactive)
  1398. (let ((dir (expand-file-name default-directory)))
  1399. (switch-to-buffer (or eshell-buffer-or-name
  1400. (eshell t)))
  1401. (unless (equal dir (expand-file-name default-directory))
  1402. ;; (cd dir)
  1403. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1404. ;; (eshell-emit-prompt)
  1405. (goto-char (point-max))
  1406. (eshell-kill-input)
  1407. (insert "cd " dir)
  1408. (eshell-send-input))))
  1409. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1410. (setq eshell-term-name "eterm-color")
  1411. (setq eshell-scroll-to-bottom-on-input t)
  1412. (setq eshell-cmpl-ignore-case t)
  1413. (setq eshell-cmpl-cycle-completions nil)
  1414. (setq eshell-highlight-prompt nil)
  1415. (setq eshell-ls-initial-args '("-hCFG"
  1416. "--color=auto"
  1417. "--time-style=long-iso")) ; "-hF")
  1418. (setq eshell-prompt-function
  1419. (lambda ()
  1420. (with-temp-buffer
  1421. (let (p1 p2 p3 p4)
  1422. (insert " [")
  1423. (setq p1 (point))
  1424. (insert (abbreviate-file-name default-directory))
  1425. (setq p2 (point))
  1426. (insert "]"
  1427. "\n")
  1428. (setq p3 (point))
  1429. (insert user-login-name
  1430. "@"
  1431. (or (getenv "HOSTNAME")
  1432. (substring (shell-command-to-string
  1433. (or (executable-find "hostname")
  1434. "echo ''"))
  1435. 0
  1436. -1)))
  1437. (setq p4 (point))
  1438. (insert " "
  1439. (format-time-string "%a, %d %b %Y %T %z")
  1440. " eshell\n"
  1441. "last:"
  1442. (number-to-string eshell-last-command-status)
  1443. (if (= (user-uid)
  1444. 0)
  1445. " # "
  1446. " $ "))
  1447. (add-text-properties p1
  1448. p2
  1449. '(face ((foreground-color . "yellow"))))
  1450. (add-text-properties p3
  1451. p4
  1452. '(face ((foreground-color . "cyan"))))
  1453. (buffer-substring (point-min)
  1454. (point-max))))))
  1455. (add-hook 'eshell-mode-hook
  1456. (lambda ()
  1457. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1458. ;; (interactive)
  1459. ;; (switch-to-buffer (other-buffer))))
  1460. (define-key eshell-mode-map (kbd "C-u") (lambda ()
  1461. (interactive)
  1462. (eshell-goto-prompt)
  1463. (eshell-kill-input)))
  1464. (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1465. (interactive)
  1466. (eshell-goto-prompt)
  1467. (my-keyboard-quit)))
  1468. (define-key eshell-mode-map
  1469. (kbd "DEL") 'my-eshell-backward-delete-char)
  1470. (define-key eshell-mode-map
  1471. (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1472. (define-key eshell-mode-map
  1473. (kbd "C-n") 'eshell-next-matching-input-from-input)
  1474. (apply 'eshell/addpath exec-path)
  1475. (set (make-local-variable 'scroll-margin) 0)
  1476. ;; (eshell/export "GIT_PAGER=")
  1477. ;; (eshell/export "GIT_EDITOR=")
  1478. (eshell/export "LC_MESSAGES=C")
  1479. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1480. (set (make-local-variable 'hl-line-range-function)
  1481. (lambda ()
  1482. '(0 . 0)))
  1483. (add-to-list 'eshell-virtual-targets
  1484. '("/dev/less"
  1485. (lambda (str)
  1486. (if str
  1487. (with-current-buffer nil)))
  1488. nil))
  1489. ))
  1490. (add-hook 'eshell-mode-hook
  1491. (lambda ()
  1492. (add-to-list 'eshell-visual-commands "vim")
  1493. ;; (add-to-list 'eshell-visual-commands "git")
  1494. (add-to-list 'eshell-output-filter-functions
  1495. 'eshell-truncate-buffer)
  1496. (mapcar (lambda (alias)
  1497. (add-to-list 'eshell-command-aliases-list
  1498. alias))
  1499. '(
  1500. ; ("ll" "ls -l $*")
  1501. ; ("la" "ls -a $*")
  1502. ; ("lla" "ls -al $*")
  1503. ("aptin" "apt-get install $*")
  1504. ("eless"
  1505. (concat "cat >>> (with-current-buffer "
  1506. "(get-buffer-create \"*eshell output\") "
  1507. "(erase-buffer) "
  1508. "(setq buffer-read-only nil) "
  1509. "(current-buffer)) "
  1510. "(view-buffer (get-buffer \"*eshell output*\"))")
  1511. ("g" "git $*")
  1512. ))
  1513. )))
  1514. ) ; eval after load eshell
  1515. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1516. ;; get last modified date
  1517. (defvar my-buffer-file-last-modified-time nil "")
  1518. (make-variable-buffer-local 'my-buffer-file-last-modified-time)
  1519. (defun my-get-file-last-modified-time (file)
  1520. ""
  1521. (nth 5
  1522. (file-attributes file)))
  1523. (defun my-set-buffer-file-last-modified-time ()
  1524. ""
  1525. (make-local-variable 'my-buffer-file-last-modified-time)
  1526. (setq my-buffer-file-last-modified-time
  1527. (format-time-string "%Y/%m/%d %H:%M"
  1528. (my-get-file-last-modified-time buffer-file-name))))
  1529. (add-hook 'find-file-hook
  1530. 'my-set-buffer-file-last-modified-time)
  1531. (add-hook 'after-save-hook
  1532. 'my-set-buffer-file-last-modified-time)
  1533. (add-hook 'after-revert-hook
  1534. 'my-set-buffer-file-last-modified-time)
  1535. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1536. ;; frame buffer
  1537. ;; todo:
  1538. ;; work well when opening the file that was already opened on another window
  1539. (add-hook 'after-make-frame-functions
  1540. (lambda (f)
  1541. (set-window-buffer (frame-selected-window f)
  1542. "*Messages*")))
  1543. (defun make-frame-command-with-name (name)
  1544. "Make frame with name specified."
  1545. (interactive "sName for new frame: ")
  1546. (set-frame-parameter (make-frame-command)
  1547. 'name
  1548. name))
  1549. (defvar my-frame-buffer-plist nil)
  1550. (defun my-frame-buffer-add (&optional buf frame)
  1551. ""
  1552. (setq my-frame-buffer-plist
  1553. (plist-put my-frame-buffer-plist
  1554. (or frame
  1555. (selected-frame))
  1556. (let ((lst (my-frame-buffer-get frame)))
  1557. (if lst
  1558. (add-to-list 'lst
  1559. (or buf
  1560. (current-buffer)))
  1561. (list (or buf
  1562. (current-buffer))))))))
  1563. (defun my-frame-buffer-remove (&optional buf frame)
  1564. ""
  1565. (setq my-frame-buffer-plist
  1566. (plist-put my-frame-buffer-plist
  1567. (or frame
  1568. (selected-frame))
  1569. (delq (or buf
  1570. (current-buffer))
  1571. (my-frame-buffer-get frame)))))
  1572. (defun my-frame-buffer-get (&optional frame)
  1573. ""
  1574. (plist-get my-frame-buffer-plist
  1575. (or frame
  1576. (selected-frame))))
  1577. (defun my-frame-buffer-kill-all-buffer (&optional frame)
  1578. ""
  1579. (mapcar 'kill-buffer
  1580. (my-frame-buffer-get frame)))
  1581. (add-hook 'find-file-hook
  1582. 'my-frame-buffer-add)
  1583. ;; (add-hook 'term-mode-hook
  1584. ;; 'my-frame-buffer-add)
  1585. (add-hook 'eshell-mode-hook
  1586. 'my-frame-buffer-add)
  1587. (add-hook 'Man-mode-hook
  1588. 'my-frame-buffer-add)
  1589. (add-hook 'kill-buffer-hook
  1590. 'my-frame-buffer-remove)
  1591. (add-hook 'delete-frame-functions
  1592. 'my-frame-buffer-kill-all-buffer)
  1593. (defvar my-desktop-terminal "roxterm")
  1594. (defun my-execute-terminal ()
  1595. ""
  1596. (interactive)
  1597. (if (and (or (eq system-type 'windows-nt)
  1598. window-system)
  1599. my-desktop-terminal
  1600. )
  1601. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1602. (start-process "terminal"
  1603. nil
  1604. my-desktop-terminal))
  1605. (my-term)))
  1606. (defvar my-term nil "my terminal buffer")
  1607. (defun my-term ()
  1608. "open terminal buffer and return that buffer."
  1609. (interactive)
  1610. (if (and my-term
  1611. (buffer-name my-term))
  1612. (pop-to-buffer my-term)
  1613. (setq my-term
  1614. (if (eq system-type 'windows-nt)
  1615. (eshell)
  1616. (if (require 'multi-term nil t)
  1617. (multi-term)
  1618. (ansi-term shell-file-name))))))
  1619. (defun my-delete-frame-or-kill-emacs ()
  1620. "delete frame when opening multiple frame, kill emacs when only one."
  1621. (interactive)
  1622. (if (eq 1
  1623. (length (frame-list)))
  1624. (save-buffers-kill-emacs)
  1625. (delete-frame)))
  1626. (define-key my-prefix-map (kbd "C-s") 'my-execute-terminal)
  1627. (define-key my-prefix-map (kbd "C-f") 'make-frame-command-with-name)
  1628. (global-set-key (kbd "C-x C-c") 'my-delete-frame-or-kill-emacs)
  1629. (define-key my-prefix-map (kbd "C-x C-c") 'save-buffers-kill-emacs)
  1630. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1631. ;; x open
  1632. (defvar my-filer nil)
  1633. (setq my-filer (or (executable-find "pcmanfm")
  1634. (executable-find "nautilus")))
  1635. (defun my-x-open (file)
  1636. "open file."
  1637. (interactive "FOpen File: ")
  1638. (setq file (expand-file-name file))
  1639. (message "Opening %s..." file)
  1640. (cond ((eq system-type 'windows-nt)
  1641. (call-process "cmd.exe" nil 0 nil
  1642. "/c" "start" "" (convert-standard-filename file)))
  1643. ((eq system-type 'darwin)
  1644. (call-process "open" nil 0 nil file))
  1645. ((getenv "DISPLAY")
  1646. (call-process (or my-filer "xdg-open") nil 0 nil file))
  1647. (t
  1648. (find-file file))
  1649. )
  1650. ;; (recentf-add-file file)
  1651. (message "Opening %s...done" file))
  1652. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1653. ;; misc funcs
  1654. (defvar sed-in-place-history nil
  1655. "History of `sed-in-place'")
  1656. (defvar sed-in-place-command "sed --in-place=.bak -e")
  1657. (defun sed-in-place (command)
  1658. "sed in place"
  1659. (interactive (list (read-shell-command "sed in place: "
  1660. (concat sed-in-place-command " ")
  1661. 'sed-in-place-history)))
  1662. (shell-command command
  1663. "*sed in place*"))
  1664. (defun dired-do-sed-in-place (&optional arg)
  1665. "sed in place dired"
  1666. (interactive "P")
  1667. (require 'dired-aux)
  1668. (let* ((files (dired-get-marked-files t arg))
  1669. (expr (dired-mark-read-string "Run sed-in-place for %s: "
  1670. nil
  1671. 'sed-in-place
  1672. arg
  1673. files)))
  1674. (if (equal expr
  1675. "")
  1676. (error "No expression specified")
  1677. (shell-command (concat sed-in-place-command
  1678. " '"
  1679. expr
  1680. "' "
  1681. (mapconcat 'shell-quote-argument
  1682. files
  1683. " "))
  1684. "*sed in place*"))))
  1685. (defun dir-show (&optional dir)
  1686. (interactive)
  1687. (let ((bf (get-buffer-create "*dir show*"))
  1688. (list-directory-brief-switches "-C"))
  1689. (with-current-buffer bf
  1690. (list-directory (or nil
  1691. default-directory)
  1692. nil))
  1693. ))
  1694. (defun my-keyboard-quit ()
  1695. ""
  1696. (interactive)
  1697. (run-hooks 'before-keyboard-quit-hook)
  1698. ;; (redisplay t)
  1699. (redraw-display)
  1700. ;; (run-hooks 'window-configuration-change-hook)
  1701. (keyboard-quit)
  1702. (insert "insert me")
  1703. (run-hooks 'after-keyboard-quit-hook))
  1704. (substitute-key-definition 'keyboard-quit 'my-keyboard-quit global-map)
  1705. ;; (global-set-key (kbd "C-g") 'my-keyboard-quit)
  1706. (defun my-convmv-sjis2utf8-test ()
  1707. "run `convmv -r -f sjis -t utf8 *'
  1708. this is test, does not rename files"
  1709. (interactive)
  1710. (shell-command "convmv -r -f sjis -t utf8 *"))
  1711. (defun my-convmv-sjis2utf8-notest ()
  1712. "run `convmv -r -f sjis -t utf8 * --notest'"
  1713. (interactive)
  1714. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  1715. (defun kill-ring-save-buffer-file-name ()
  1716. "get current filename"
  1717. (interactive)
  1718. (let ((file buffer-file-name))
  1719. (if file
  1720. (progn (kill-new file)
  1721. (message file))
  1722. (message "not visiting file."))))
  1723. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1724. ;; ;; savage emacs
  1725. ;; ;; when enabled emacs fails to complete
  1726. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  1727. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  1728. ;; (setq arg
  1729. ;; (concat arg
  1730. ;; (if (eq nil (string-match "\\. *$" arg)) ".")
  1731. ;; " Stupid!")))
  1732. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1733. ;; japanese input method
  1734. (defun my-load-scim ()
  1735. "use scim-bridge.el as japanese im."
  1736. ;; Load scim-bridge.
  1737. (when (require 'scim-bridge nil t)
  1738. ;; Turn on scim-mode automatically after loading .emacs
  1739. (add-hook 'after-init-hook 'scim-mode-on)
  1740. (setq scim-cursor-color "red")
  1741. (scim-define-preedit-key ?\^h t)
  1742. (scim-define-common-key ?\* nil)
  1743. (scim-define-common-key ?\^/ nil)))
  1744. (defun my-load-anthy ()
  1745. "use anthy.el as japanese im."
  1746. ;; anthy
  1747. (when (require 'anthy nil t)
  1748. (global-set-key
  1749. (kbd "<muhenkan>") (lambda () (interactive) (anthy-mode-off)))
  1750. (global-set-key (kbd "<henkan>") (lambda () (interactive) (anthy-mode-on)))
  1751. (when (>= emacs-major-version 23)
  1752. (setq anthy-accept-timeout 1))))
  1753. ;; quail
  1754. ;; aproposs input-method for some information
  1755. ;; (setq default-input-method "japanese")
  1756. (defun my-load-mozc-el ()
  1757. ""
  1758. (setq mozc-leim-title "[MZ]")
  1759. (when (require 'mozc nil t)
  1760. (setq defauit-input-method "japanese-mozc")
  1761. ))
  1762. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1763. ;; for windows
  1764. ;; (add-to-list 'exec-path "c:/Program Files/Gauche/bin/")
  1765. (defun start-ckw-bash ()
  1766. ""
  1767. (interactive)
  1768. (start-process
  1769. "ckw_bash"
  1770. nil
  1771. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe"))
  1772. ;; command seems to have to be in c drive
  1773. (defun my-w32-add-export-path (&rest args)
  1774. ""
  1775. (mapcar (lambda (path)
  1776. (add-to-list 'exec-path (expand-file-name path)))
  1777. (reverse args))
  1778. (setenv "PATH"
  1779. (mapconcat 'convert-standard-filename
  1780. exec-path
  1781. ";")))
  1782. (when (eq system-type 'windows-nt)
  1783. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  1784. ;; (setq python-python-command "c:/Python26/python.exe")
  1785. (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  1786. (my-w32-add-export-path "c:/Windows/system"
  1787. "c:/Windows/System32"
  1788. "c:/Program Files/Git/bin"
  1789. "c:/MinGW/bin"
  1790. "c:/MinGW/mingw32/bin"
  1791. (expand-file-name "~/.local/bin")
  1792. (expand-file-name "~/dbx/apps/bin"))
  1793. (when window-system
  1794. (setq w32-enable-synthesized-fonts t))
  1795. (setq file-name-coding-system 'sjis))