Emacs functions for toggling between “.” and “->” in C code

The first function simply toggles the characters at the point between “.” and “->”. Not terribly useful.

The second function is a little more interesting. You can put the point by a struct-type variable which is or isn’t a pointer and this function will toggle both the “*” and all of the “.”s and “->”s in the function.

Send me any suggestions for improvements.

(defun c-toggle-dot-pointer ()                                                                                                                                 
  (interactive)                                                                                                                                                
  (cond                                                                                                                                                        
   ((looking-at "\\.")   (replace-string "." "->" nil (point) (+ (point) 1)))                                                                                  
   ((looking-at "\\->")  (replace-string "->" "." nil (point) (+ (point) 2)))))                                                                                
                                                                                                                                                               
(defun c-toggle-dot-pointer-in-defun ()                                                                                                                        
  (interactive)                                                                                                                                                
  (atomic-change-group                                                                                                                                         
    (save-excursion                                                                                                                                            
      (let ((is-ptr nil)                                                                                                                                       
            (var-under-cursor)                                                                                                                                 
            (end-of-func))                                                                                                                                     
        (when (eq (char-after) ?*)                                                                                                                             
          (delete-char 1)                                                                                                                                      
          (setf is-ptr t))                                                                                                                                     
        (if (looking-at "[a-zA-Z0-9_]+")                                                                                                                       
        ;then                                                                                                                                                  
          (setf var-under-cursor (match-string 0))                                                                                                             
        ;else                                                                                                                                                  
          (error "not variable"))                                                                                                                              
        (save-excursion (c-end-of-defun) (setf end-of-func (point)))                                                                                           
        (if is-ptr                                                                                                                                             
        ;then                                                                                                                                                  
          (replace-string (concat var-under-cursor "->")                                                                                                       
                          (concat var-under-cursor ".")                                                                                                        
                          nil                                                                                                                                  
                          (point)                                                                                                                              
                          end-of-func)                                                                                                                         
        ;else                                                                                                                                                  
          (insert "*")                                                                                                                                         
          (replace-string (concat var-under-cursor ".")                                                                                                        
                          (concat var-under-cursor "->")                                                                                                       
                          nil                                                                                                                                  
                          (point)                                                                                                                              
                          end-of-func))))))                            

DreamHost

One thought on “Emacs functions for toggling between “.” and “->” in C code

Leave a Reply

Your email address will not be published. Required fields are marked *