;;; $DOOMDIR/config.el -*- lexical-binding: t; -*-

;; Place your private configuration here! Remember, you do not need to run 'doom
;; sync' after modifying this file!


;; Some functionality uses this to identify you, e.g. GPG configuration, email
;; clients, file templates and snippets. It is optional.
;; (setq user-full-name "John Doe"
;;       user-mail-address "john@doe.com")

;; Doom exposes five (optional) variables for controlling fonts in Doom:
;;
;; - `doom-font' -- the primary font to use
;; - `doom-variable-pitch-font' -- a non-monospace font (where applicable)
;; - `doom-big-font' -- used for `doom-big-font-mode'; use this for
;;   presentations or streaming.
;; - `doom-symbol-font' -- for symbols
;; - `doom-serif-font' -- for the `fixed-pitch-serif' face
;;
;; See 'C-h v doom-font' for documentation and more examples of what they
;; accept. For example:
;;
;;(setq doom-font (font-spec :family "Fira Code" :size 12 :weight 'semi-light)
;;      doom-variable-pitch-font (font-spec :family "Fira Sans" :size 13))
;;
;; If you or Emacs can't find your font, use 'M-x describe-font' to look them
;; up, `M-x eval-region' to execute elisp code, and 'M-x doom/reload-font' to
;; refresh your font settings. If Emacs still can't find your font, it likely
;; wasn't installed correctly. Font issues are rarely Doom issues!

;; There are two ways to load a theme. Both assume the theme is installed and
;; available. You can either set `doom-theme' or manually load a theme with the
;; `load-theme' function. This is the default:
(setq doom-theme 'doom-one)

;; This determines the style of line numbers in effect. If set to `nil', line
;; numbers are disabled. For relative line numbers, set this to `relative'.
(setq display-line-numbers-type t)

;; If you use `org' and don't want your org files in the default location below,
;; change `org-directory'. It must be set before org loads!
(setq org-directory "~/usr/org/")


;; Whenever you reconfigure a package, make sure to wrap your config in an
;; `after!' block, otherwise Doom's defaults may override your settings. E.g.
;;
;;   (after! PACKAGE
;;     (setq x y))
;;
;; The exceptions to this rule:
;;
;;   - Setting file/directory variables (like `org-directory')
;;   - Setting variables which explicitly tell you to set them before their
;;     package is loaded (see 'C-h v VARIABLE' to look up their documentation).
;;   - Setting doom variables (which start with 'doom-' or '+').
;;
;; Here are some additional functions/macros that will help you configure Doom.
;;
;; - `load!' for loading external *.el files relative to this one
;; - `use-package!' for configuring packages
;; - `after!' for running code after a package has loaded
;; - `add-load-path!' for adding directories to the `load-path', relative to
;;   this file. Emacs searches the `load-path' when you load packages with
;;   `require' or `use-package'.
;; - `map!' for binding new keys
;;
;; To get information about any of these functions/macros, move the cursor over
;; the highlighted symbol at press 'K' (non-evil users must press 'C-c c k').
;; This will open documentation for it, including demos of how they are used.
;; Alternatively, use `C-h o' to look up a symbol (functions, variables, faces,
;; etc).
;;
;; You can also try 'gd' (or 'C-c c d') to jump to their definition and see how
;; they are implemented.

(setq doom-font "Victor Mono NerdFont")

(setq-default indent-tabs-mode t)

(map! :map c-mode-map :i "<tab>" #'indent-for-tab-command)

(defun user-intra-name () "kcolin")

(eval-after-load 'autoinsert
  '(define-auto-insert '("\\.\\(c\\|h\\)\\'" . "42 C header")
     '(
       "Short description: "
       "/* ************************************************************************** */" \n
       "/*                                                                            */" \n
       "/*                                                        :::      ::::::::   */" \n
       "/*   "
       (file-name-nondirectory (buffer-file-name))
       (substring (make-string 51 ? )
        (length (file-name-nondirectory (buffer-file-name))))
       ":+:      :+:    :+:   */" \n
       "/*                                                    +:+ +:+         +:+     */" \n
       "/*   By: " (user-intra-name) " <kcolin@42.fr>"
       (substring (make-string 28 ? ) (length (user-intra-name)))
       "+#+  +:+       +#+        */" \n
       "/*                                                +#+#+#+#+#+   +#+           */" \n
       "/*   Created: " (format-time-string "%Y/%m/%d %H:%M:%S") " by " (user-intra-name)
       (substring (make-string 18 ? ) (length (user-intra-name)))
       "#+#    #+#             */" \n
       "/*   Updated: " (format-time-string "%Y/%m/%d %H:%M:%S") " by " (user-intra-name)
       (substring (make-string 17 ? ) (length (user-intra-name)))
       "###   ########.fr       */" \n
       "/*                                                                            */" \n
       "/* ************************************************************************** */" \n
       )))

(defun my-42-header ()
  "check and update or replace 42 header in .c and .h files"
  (interactive)
  (if (my-42-header-check)
      (my-42-header-update)
    (my-42-header-replace)))

(defun my-42-header-update ()
  "Update the existing 42 header"
  (interactive)
  (save-excursion
    (goto-char 0)
    (re-search-forward "/\\*   .\\{51\\}:\\+:      :\\+:    :\\+:   \\*/" 891 t)
    (replace-match (concat "/*   "
                           (file-name-nondirectory (buffer-file-name))
                           (substring (make-string 51 ? )
                                      (length (file-name-nondirectory (buffer-file-name))))
                           ":+:      :+:    :+:   */"))
    (re-search-forward "/\\*   Updated: [0-9]\\{4\\}/[0-9]\\{2\\}/[0-9]\\{2\\} \
[0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\} by .\\{17\\}###   ########\\.fr       \\*/" 891 t)
    (replace-match (concat "/*   Updated: "
                           (format-time-string "%Y/%m/%d %H:%M:%S")
                           " by " (user-intra-name)
                           (substring (make-string 17 ? ) (length (user-intra-name)))
                           "###   ########.fr       */"))))

(defun my-42-header-replace ()
  "Replace the first 11 lines with a fresh 42 header, if there is probably a bad header"
  (interactive)
  (save-excursion
    (goto-char 0)
    (cond ((re-search-forward "^/\\*.*\\*/$" nil t)
           (re-search-forward "^$" nil t)
           (delete-region 1 (point))))
    (insert (my-42-header-generate))))

(defun my-42-header-generate ()
  "Generate 42 header string"
  (concat "/* ************************************************************************** */\n"
          "/*                                                                            */\n"
          "/*                                                        :::      ::::::::   */\n"
          "/*   "
          (file-name-nondirectory (buffer-file-name))
          (substring (make-string 51 ? )
                     (length (file-name-nondirectory (buffer-file-name))))
          ":+:      :+:    :+:   */\n"
          "/*                                                    +:+ +:+         +:+     */\n"
          "/*   By: " (user-intra-name) " <kcolin@42.fr>"
          (substring (make-string 28 ? ) (length (user-intra-name)))
          "+#+  +:+       +#+        */\n"
          "/*                                                +#+#+#+#+#+   +#+           */\n"
          "/*   Created: " (format-time-string "%Y/%m/%d %H:%M:%S") " by " (user-intra-name)
          (substring (make-string 18 ? ) (length (user-intra-name)))
          "#+#    #+#             */\n"
          "/*   Updated: " (format-time-string "%Y/%m/%d %H:%M:%S") " by " (user-intra-name)
          (substring (make-string 17 ? ) (length (user-intra-name)))
          "###   ########.fr       */\n"
          "/*                                                                            */\n"
          "/* ************************************************************************** */\n"))

(defun my-42-header-check ()
  "Check if there's a 42 header at the top of the current buffer"
  (widen)
  (let (header lines)
    (condition-case my-simple-error
	(setq header (buffer-substring-no-properties 1 891))
      (my-simple-handler
       (my-42-header-replace)))
    (setq lines (split-string header "\n"))
    (and (= 11 (length lines))
         (seq-every-p (lambda (e) (= 80 (length e))) lines)
         (string-match "/\\* \\*\\{74\\} \\*/" (nth 0 lines))
         (string-match "/\\* \\{76\\}\\*/" (nth 1 lines))
         (string-match "/\\* \\{56\\}:::      ::::::::   \\*/" (nth 2 lines))
         (string-match "/\\*   .\\{51\\}:\\+:      :\\+:    :\\+:   \\*/" (nth 3 lines))
         (string-match "/\\* \\{52\\}\\+:\\+ \\+:\\+         \\+:\\+     \\*/" (nth 4 lines))
         (string-match "/\\*   By: .\\{3,42\\}<kcolin@42.fr> *\\+#\\+  \\+:\\+       \\+#\\+        \\*/" (nth 5 lines))
         (string-match "/\\* \\{48\\}\\+#\\+#\\+#\\+#\\+#\\+   \\+#\\+           \\*/" (nth 6 lines))
         (string-match "/\\*   Created: [0-9]\\{4\\}/[0-9]\\{2\\}/[0-9]\\{2\\} \
[0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\} by .\\{18\\}#\\+#    #\\+#             \\*/" (nth 7 lines))
         (string-match "/\\*   Updated: [0-9]\\{4\\}/[0-9]\\{2\\}/[0-9]\\{2\\} \
[0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\} by .\\{17\\}###   ########\\.fr       \\*/" (nth 8 lines))
         (string-match "/\\* \\{76\\}\\*/" (nth 9 lines))
         (string-match "/\\* \\*\\{74\\} \\*/" (nth 10 lines)))))

(auto-insert-mode t)
(add-hook 'c-mode-hook 'my-42-header)

;; basic configuration for 42 .c and .h files
(defun my-c-mode-hook ()
  (setq-default tab-width 4)
  (setq-default indent-tabs-mode t)
  (setq-default c-default-style "linux")
  (c-set-offset 'substatement-open 0)
  (setq-default c-basic-offset 4)
  ;; buggy sometimes if you don't setq it as well
  (setq c-basic-offset 4))
(add-hook 'c-mode-hook 'my-c-mode-hook)

(defun my-42-header-before-save-hook ()
  (when (eq major-mode 'c-mode)
    (my-42-header)))

(add-hook 'before-save-hook #'my-42-header-before-save-hook)

(add-to-list 'auto-mode-alist '("\\.jjdescription\\'" . diff-mode))
