Sunday, July 5, 2015

Emacs settings

;; loads libraries in ~/.emacs.d/site-lisp
(let ((default-directory (expand-file-name "~/.emacs.d/site-lisp")))
  (add-to-list 'load-path default-directory)
  (if (fboundp 'normal-top-level-add-subdirs-to-load-path)
      (normal-top-level-add-subdirs-to-load-path)))

;(require 'init-loader)
;(setq init-loader-show-log-after-init nil)
;(init-loader-load "~/.emacs.d/inits")

;(require 'package)
;; adding MELPA
;(add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/"))
;; Marmaladeを追加
;(add-to-list 'package-archives  '("marmalade" . "http://marmalade-repo.org/packages/"))
;; initialize
;(package-initialize)


;; helm ---------------------------------------------------
;(require 'helm-config)
;(helm-mode 1)

;(define-key helm-read-file-map (kbd "TAB") 'helm-execute-persistent-action)
;(define-key helm-find-files-map (kbd "TAB") 'helm-execute-persistent-action)
;(define-key helm-map (kbd "C-h") 'delete-backward-char)
;(define-key helm-map (kbd "TAB") 'helm-execute-persistent-action)
;(define-key helm-find-files-map (kbd "C-h") 'delete-backward-char)
;(define-key helm-M-x-map (kbd "TAB") 'helm-execute-persistent-action)

;(define-key global-map (kbd "M-x") 'helm-M-x)
;(define-key global-map (kbd "C-x C-f") 'helm-find-files)
;(define-key global-map (kbd "C-c i") 'helm-imenu)
;(define-key global-map (kbd "M-y") 'helm-show-kill-ring)

;; ---------------------------------------------------


;; auto-complete ---------------------------------------------------
(require 'auto-complete)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/auto-complete/dict")
(require 'auto-complete-config)
(ac-config-default)
; ---------------------------------------------------


;; undo-tree -------------------------------------
(require 'undo-tree)
(setq undo-limit 600000)
(setq undo-strong-limit 900000)

;;(global-set-key (kbd "M-/") 'undo-tree-redo)

(global-undo-tree-mode t)
;; ---------------------------------------------------

;; smooth-scroll -------------------------------------
(require 'smooth-scroll)
(smooth-scroll-mode t)
;; ---------------------------------------------------


;; git-gutter-fringe -------------------------------------
(require 'git-gutter-fringe)
(set-face-foreground 'git-gutter-fr:modified "yellow")
(set-face-foreground 'git-gutter-fr:added "green")
(set-face-foreground 'git-gutter-fr:deleted "white")
(global-set-key "\C-c\C-g" 'git-gutter:next-diff)
(global-set-key "\C-c\C-r" 'git-gutter:previous-diff)
(setq git-gutter-fr:side 'right-fringe)

;; ---------------------------------------------------

;; anzu ----------------------------------------------
(require 'anzu)
(anzu-mode +1)
(global-anzu-mode +1)
;; ---------------------------------------------------

;; magit ------------------------------
(require 'magit)
(global-set-key "\C-xvv" 'magit-status)
;; ------------------------------------

;(require 'flycheck)
;(require 'flycheck-pos-tip)

;(add-hook 'after-init-hook #'global-flycheck-mode)
;(eval-after-load 'flycheck
;  '(custom-set-variables
;    '(flycheck-display-errors-function "'flycheck-pos-tip-error-messages)))

;(flycheck-define-checker c/c++
;  "A C/C++ checker using g++."
;  :command ("g++" "-Wall" "-Wextra" source)
;  :error-patterns ((error line-start
;              (file-name) ":" line ":" column ":" "error:" (message);
;              line-end)
;           (warning line-start
;              (file-name) ":" line ":" column ":" "warning:" (message)
;              line-end))
;  :modes (c-mode c++-mode))

;; ---------------------------------------------------




;; default to unified diffs
(setq diff-switches "-u")
(mouse-wheel-mode t)

(defun Xmy-c-mode-hook()
  (c-set-style "cc-mode")
  (setq tab-width 4)
  (setq c-basic-offset tab-width)
  )

(add-hook 'c-mode-hook 'my-c-mode-hook)

(add-hook 'c-mode-common-hook
          '(lambda()
;             (c-toggle-auto-hungry-state 1)
         ))

;;  convert ident to space
(setq-default tab-width 4 indent-tabs-mode nil)

;; delete ths last spaces of the line
(add-hook 'before-save-hook 'delete-trailing-whitespace)

;;  ignore case in file name
(setq completion-ignore-case t)
(setq read--completion-ignore-case t)
(setq read-file-name-completion-ignore-case t)

;; auto revert the buffer
(global-auto-revert-mode t)

(global-hl-line-mode)
(setq hl-line-face 'underline)

(hl-line-mode 1)
(which-function-mode 1)
(column-number-mode 1)
(display-time-mode 1)
(setq inhibit-startup-message t)

;; my key bind setting -------------------------
(global-set-key "\C-x\C-b" 'buffer-menu)
(global-set-key "\C-x\C-g" 'goto-line)
(global-set-key "\C-xc" 'compile)
(global-set-key "\C-z" 'undo)
(global-set-key "\C-h" 'delete-backward-char)
(global-set-key "\C-xx" 'comment-region)
(global-set-key "\C-xw" 'uncomment-region)
(global-set-key [f1] 'clipboard-kill-ring-save)
(global-set-key [f2] 'clipboard-yank)
(global-set-key [f3] 'clipboard-kill-region)
(global-set-key [f4] 'delete-forward-char)
(global-set-key [f5] 'compile)
(global-set-key [f6] 'align)
(global-set-key [f7] 'flymake-mode)
;; ---------------------------------------------

;; whitespace ----------------------------------
(require 'whitespace)
;; (setq whitespace-style '(face
;;                          trailing
;;                          tabs
;;                          spaces
;;                          empty
;;                          space-mark
;;                          tab-mark
;;                          ))
(setq-default whitespace-style '(face
                                 tabs
                                 tab-mark
                                 spaces
                                 space-mark
                                 trailing
                                 ;; newline
                                 ;; newline-mark
                                 ))
(setq-default whitespace-space-regexp "\\(\x3000+\\)")
(setq-default whitespace-display-mappings
              '((space-mark   ?\x3000    [?□]    [?_]) ;□
                (newline-mark ?\n        [?$ ?\n])
                (tab-mark     ?\t        [?\u00BB ?\t] [?\\ ?\t]))
              )
(set-face-attribute 'whitespace-space     nil :foreground "purple4"       :background 'unspecified :weight'normal :slant 'italic)
(set-face-attribute 'whitespace-tab       nil :foreground "DodgerBlue4"   :background 'unspecified :weight'normal :slant 'italic)
(set-face-attribute 'whitespace-trailing  nil :foreground 'unspecified    :background "#35006b"    :weight'normal :slant 'italic)
(set-face-attribute 'whitespace-newline   nil :foreground "blue" :italic t :bold nil)

(global-whitespace-mode 1)
;; ---------------------------------------------


;; wdired ---------------------------------------------------
(require 'wdired)
(setq wdired-allow-to-change-permissions t)
(define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
;;-----------------------------------------------------------


;; gtags --------------------------------------------
(load-library "/usr/local/share/gtags/gtags.el")
(autoload 'gtags-mode "gtags" "" t)
(setq gtags-mode-hook
      '(lambda()
         (local-set-key "\M-." 'gtags-find-tag)
         (local-set-key "\M-r" 'gtags-find-rtag)
         (local-set-key "\M-s" 'gtags-find-symbol)
;         (local-set-key "\C-t" 'gtags-pop-stack)
         ))
(defadvice gtags-find-tag (before gtags-find-tag activate)
  (let ((default-directory (locate-dominating-file default-directory "makefile")))
    (shell-command "gtags")))


;; (shell-command (format "cd %s && gtags" default-directory))))


;;---------------------------------------------------

;; parenthesis highlight mode
(show-paren-mode t)
(setq-default blink-matching-paren t)
(face-spec-set 'show-paren-match            '((t (:foreground "magenta" :background nil :bold))))
(face-spec-set 'show-paren-mismatch         '((t (:foreground "black" :background "red" :bold))))

;; (lazy-loaded 'mic-paren nil
;;   (paren-activate)
;;   (setq paren-match-face '(bold))
;;   ;; (setq paren-match-face '(bold italic))
;;   (setq paren-sexp-mode t)
;;   (setq paren-display-message 'never)
;;   (setq paren-message-linefeed-display "^J"))
;; (lazy-loaded 'rainbow-delimiters nil)




;;-----------------------------------------------------------
(setq c-mode-hook '(lambda ()
                     (gtags-mode 1)
                     (git-gutter-mode 1)
                     ))
(setq c++-mode-hook '(lambda ()
                       (gtags-mode 1)
                       (git-gutter-mode 1)
                       ))
(setq java-mode-hook '(lambda ()
                        (gtags-mode 1)
                        (git-gutter-mode 1)
                        ))
;;-----------------------------------------------------------


(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(column-number-mode t)
 '(display-time-mode t))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(default ((t (:family "fixed" :foundry "misc" :slant normal :weight normal :height 125 :width semi-condensed)))))


(setq-default c-basic-offset 4)
(electric-pair-mode 1)

(defadvice compile (around in-make-file activate)
  (let ((default-directory (locate-dominating-file default-directory "makefile")))
    ad-do-it)
  ;; (shell-command "./l")
  (print "aaaaaa" (get-buffer "Messages"))
  (print default-directory (get-buffer "Messages"))
  )
(defadvice compile (after compile-finished activate)
  ;; (let ((default-directory (locate-dominating-file default-directory "makefile"))))
  (print "bbbbbbbbb" (get-buffer "Messages"))
  (print default-directory (get-buffer "Messages"))
  (shell-command "./l"))

;; (shell-command "/private/share/NativeUpdater/cds-updater_native/l"))

(put 'dired-find-alternate-file 'disabled nil)

Tuesday, July 26, 2011

A Common MySQL Query Error

 

Lot’s of php developers use MySQL as their database. And when we do it, have to use queries to interact with the database. So we use mysql_query(query) function. The argument for this function varies according to user inputs. We can’t keep fix query string to this function. As a example consider following query for insert a record for database.

INSERT INTO Persons (Name, Age, Address)
VALUES ('Peter', '35', ‘Park Street’);
 

In this query string there are three fields. Data for these fields solely depend on user input the web page. So we cant use fixed values for the VALUES part. To over come this issue we use following query string.

INSERT INTO Persons (Name, Age, Address)
VALUES ($Name, $Age, $Address);
 

I have replace the fixed values of the previous query with php variables. $Name for Name field, $Age for Age field and $Address for Address field. These three variables changes according to the user input. But the above query gives a error, Can you guess what it is ?. Look so closely, if you can’t identify the error you have the bug in your code. Now look at the corrected query.

INSERT INTO Persons (Name, Age, Address)
VALUES (‘$Name’, $Age, ‘$Address’);
 

Can you identify the difference between the two queries. Yes, it is the single quotation marks. Why we need these quotation marks for the query. This query contains three data types they are String and Integer. For Integer there are no problem but for String we have to say this is a string data. For that we use double quotation marks. So the full php code for this will be the following line.

mysql_query(INSERT INTO Persons (Name, Age, Address)
VALUES (‘$Name’, $Age, ‘$Address’);
);

Keep in mind that the arguments(inputs) for above function is a string and we have to give the string inside double quotation or single quotation. If we use double quotation for this task we have to use single quotation for the variable and vice versa.

Have fun with this and remember to write comments. eeesY....

Monday, June 14, 2010

Detecting Errors - javaScript

This is another way to detect error s in your php script. This also tells you that which is the place that code is currently running. This is a good but some complex calls of functions may not respond to this.

This is done by echoing a javascript.

    
echo
"<script>alert('Your message')</script>";

You can write any thing to your message.
Ex:
          1. A line number.
          2. A variable of php

Anything you want that helps to debug your code.This will alert you with a message and untill you click ok button of this alert message, the code will not continue to execute.

Have fun with this and remember to write comments. eeesY....
_______________

Detecting Errors - Log book

Here is the great way to detect bugs on your php script.This done by writing all outputs of the code to log file. It is very handy when you can't echo to the screen. Below shows how you can do it.

1.Create a file named log.txt in your root directory or other place you like.

2.Type the following code covering codes that you wish to debug.

        $fp = fopen("log.txt", "a+");
        ob_start();

        //put your code which is to be debugged here
       
        $content = ob_get_contents();
        ob_end_clean();
        fwrite($fp, $content);
        fclose($fp);


3.Next run the code and see the log.txt to check output and find where is the bug.

If you need any kind of help put a comment here.

Have fun with this.  eeesY