Skip to content

Latest commit

 

History

History
209 lines (192 loc) · 7.08 KB

custom_functions.org

File metadata and controls

209 lines (192 loc) · 7.08 KB

Custom functions

Stable

  • Pluralize the word
    (defun pluralize (word count &optional plural)
      "Pluralize the word."
      (if (= count 1)
          word
        (if (null plural)
            (concat word "s")
          plural)))
        
  • Delete indentation
    ;;; http://emacsredux.com/blog/2013/05/30/joining-lines/
    (defun top-join-line()
      "Join the cunrrent line with the line beneath it"
      (interactive)
      (delete-indentation 1)
      )
    (global-set-key (kbd "M-^") 'tjoin-line)
        
  • Search Google
    ;;; Search region in Google
    (defun google ()
      "Google the selected region if any, display a query prompt otherwise."
      (interactive)
      (browse-url
       (concat
        "http://www.google.com/search?ie=utf-8&oe=utf-8&q="
        (url-hexify-string (if mark-active
             (buffer-substring (region-beginning) (region-end))
             (read-string "Search Google: "))))))
    (global-set-key (kbd "C-x g") 'google)
        
  • Search Youtube
    ;;; Search youtube[emacsredux.com]
    (defun youtube ()
      "Search YouTube with a query or region if any."
      (interactive)
      (browse-url
       (concat
        "http://www.youtube.com/results?search_query="
        (url-hexify-string (if mark-active
                               (buffer-substring (region-beginning) (region-end))
                             (read-string "Search YouTube: "))))))
    (global-set-key (kbd "C-x y") 'youtube)
        
  • Terminal
    ;;; Terminal at your fingerprint
    ;;; http://emacsredux.com/blog/page/2/
    (defun visit-term-buffer ()
      "Create or visit a terminal buffer."
      (interactive)
      (if (not (get-buffer "*ansi-term*"))
          (progn
            (split-window-sensibly (selected-window))
            (other-window 1)
            (ansi-term (getenv "SHELL")))
        (switch-to-buffer-other-window "*ansi-term*")))
    (global-set-key (kbd "C-c t") 'visit-term-buffer)
        
  • Count total number of words in current buffer
    ;;; Count total number of words in current buffer
    (defun count-words-buffer ()
      "Count total number of words in current buffer."
      (interactive)
      (let ((count 0))
        (save-excursion
          (goto-char (point-min))
          (while (< (point) (point-max))
            (forward-word 1)
            (setq count (1+ count)))
          (if (zerop count)
              (message "buffer has no words.")
            (message "buffer approximately has %d %s." count
                     (pluralize "word" count))))))
    (global-set-key (kbd "C-x c") 'count-words-buffer)
        
  • Visit place in buffer expressed in percentage
    ;;; Go to place in a buffer expressed in percentage
    (defun goto-percent (pct)
      "Go to place in a buffer expressed in percentage."
      (interactive "nPercent: ")
      (goto-char (/ (* (point-max) pct) 100)))
    (global-set-key (kbd "C-x p") 'goto-percent)
        
  • Highlight comment annotations
    ;; Highlight Comment Annotations
    (defun font-lock-comment-annotations ()
      "Highlight a bunch of well known comment annotations.
    This functions should be added to the hooks of major modes for
    programming."
      (font-lock-add-keywords
       nil '(("\\<\\(FIX\\(ME\\)?\\|TODO\\|OPTIMIZE\\|HACK\\|REFACTOR\\):"
              1 font-lock-warning-face t))))
    (add-hook 'prog-mode-hook 'font-lock-comment-annotations)
        
  • Open current shell configuration file
    ;; Instant access to shell init file
    (defun find-shell-init-file ()
      "Edit the shell init file in another window."
      (interactive)
      (let* ((shell (car (reverse (split-string (getenv "SHELL") "/"))))
             (shell-init-file (cond
                               ((string-equal "zsh" shell) ".zshrc")
                               ((string-equal "bash" shell) ".bashrc")
                               (t (error "Unknown shell")))))
        (find-file-other-window (expand-file-name shell-init-file (getenv "HOME")))))
    (global-set-key (kbd "C-c S") 'find-shell-init-file)
        
  • Sort buffers
    ;;; Sort buffer list
    (defun sort-buffers ()
      "Put the buffer list in alphabetical order."
      (called-interactively-p 'interactive)
      (dolist (buff (buffer-list-sorted)) (bury-buffer buff))
      (when (called-interactively-p 'any) (list-buffers)))
    ;;(global-set-key "\M-b"    'sort-buffers)
    
    (defun buffer-list-sorted ()
      (sort (buffer-list)
            (function
             (lambda
               (a b) (string<
                      (downcase (buffer-name a))
                      (downcase (buffer-name b)))))))
        
  • Screenshot frame
    (defun screenshot-frame ()
      "Take screenshot.
    Default image ~/screenshots/TIMESTAMP.png
    Usage:
    M-x screenshot-frame
    Enter custom-name or RET to save image with timestamp"
      (interactive)
      (let* ((insert-default-directory t)
             (screenshots-dir "~/screenshots/")
             (sframe-name (concat (format-time-string "%d-%b-%Y-%T") ".png"))
             (sframe-full-path
              (read-file-name "Screenshot name: " screenshots-dir
                              (concat screenshots-dir sframe-name))))
    
        (if (not (file-accessible-directory-p screenshots-dir))
            (make-directory-internal screenshots-dir))
    
        (shell-command-to-string
         (concat "import " sframe-full-path))
        (message "Screenshot saved as %s" sframe-full-path)))
        
  • Use IDO to select ERC buffers. https://www.emacswiki.org/emacs/InteractivelyDoThings#toc4
         (defun psachin/ido-erc-buffer()
    	 (interactive)
    	 (switch-to-buffer
    	  (ido-completing-read "Channel:" 
    			       (save-excursion
    				 (delq
    				  nil
    				  (mapcar (lambda (buf)
    					    (when (buffer-live-p buf)
    					      (with-current-buffer buf
    						(and (eq major-mode 'erc-mode)
    						     (buffer-name buf)))))
    					  (buffer-list)))))))
        
  • Other buffer functions
         (defun switch_buffer ()
    	 "switch to next window."
    	 (interactive)
    	 (other-window 1))
        
         (defun kill_buffer ()
    	 "kill THIS buffer."
    	 (interactive)
    	 (kill-buffer (buffer-name)))
        
  • Turn off line number
         (defun psachin/turn-off-linum ()
    	 "Turn off linum for ERC buffers."
    	 (interactive)
    	 (linum-mode 0))