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