Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. (defun my-multi-toggle ()
  2. (interactive)
  3. (set-buffer-multibyte (not enable-multibyte-characters)))
  4. (global-set-key (kbd "C-~") 'my-multi-toggle)
  5.  
  6. (defun poff-zap ()
  7. "Get the byte offset of point - A prototye, tested minimally only with UTF-16LE"
  8. (interactive)
  9.  
  10. (let ((linect (- (line-number-at-pos) 1)) ;; line count to point
  11. (choncl (- (point) (point-at-bol))) ;; characters to point on current line
  12. (chrpnl 0) ;; chars per newline
  13. (bytpch 0) ;; bytes per char
  14. (bytpnl 0) ;; bytes per newline
  15. (offset 0) ;; the byte offset
  16. (coding (car (split-string (symbol-name buffer-file-coding-system) "-")))
  17. (format (cadr (split-string (symbol-name buffer-file-coding-system) "-"))))
  18.  
  19. (case (coding-system-eol-type buffer-file-coding-system)
  20. ('0 (setq chrpnl 1)) ;; unix
  21. ('1 (setq chrpnl 2)) ;; dos
  22. ('2 (setq chrpnl 1)) ;; mac
  23. (t))
  24.  
  25. (if (> chrpnl 0)
  26. (cond
  27. ((string= "utf" coding)
  28. (cond
  29. ((string= "8" format)
  30. (progn
  31. (setq bytpch -1)
  32. ;; need to do an actual byte count
  33. ;; using a UTF-8 parser
  34. ;; ...plus a BOM check(?)
  35. ))
  36. ((or
  37. (string= "16" format)
  38. (string= "16le" format))
  39. (progn
  40. (setq bytpch 2)
  41. (if (= 2 chrpnl) (setq offset linect))
  42. (setq offset (+ offset (point)))
  43. (setq offset (* offset bytpch))
  44. ))
  45. (t)))
  46. (t)))
  47.  
  48. (message (concat
  49. "poff-zap: " (number-to-string bytpch) " bytes-per-charn"
  50. " " (number-to-string chrpnl) " chars-per-newlinern"
  51. " " (number-to-string bytpnl) " bytes-per-newlinern"
  52. " " (number-to-string (point)) " point-emacsn"
  53. " " (number-to-string offset) " offset poff-zapn"
  54. " " (symbol-name buffer-file-coding-system) ))
  55. ))
  56. (global-set-key (kbd "C-#") 'poff-zap)
  57.  
  58. M-: (1- (position-bytes (point)))
  59.  
  60. (defun wh/byte-offset-at-point ()
  61. "Report the byte offset (0-indexed) in the file
  62. corresponding to the position of point."
  63. (interactive)
  64. (message "byte offset: %d" (1- (position-bytes (point)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement