OSDN Git Service

Update TranslationsStatus
[winmerge-jp/winmerge-jp.git] / Src / MergeDoc.h
1 /////////////////////////////////////////////////////////////////////////////
2 //    WinMerge:  an interactive diff/merge utility
3 //    Copyright (C) 1997  Dean P. Grimm
4 //    SPDX-License-Identifier: GPL-2.0-or-later
5 /////////////////////////////////////////////////////////////////////////////
6 /** 
7  * @file  MergeDoc.h
8  *
9  * @brief Declaration of CMergeDoc class
10  */
11 #pragma once
12
13 #include "DiffTextBuffer.h"
14 #include <vector>
15 #include <map>
16 #include <memory>
17 #include <optional>
18 #include "DiffWrapper.h"
19 #include "DiffList.h"
20 #include "TempFile.h"
21 #include "PathContext.h"
22 #include "IMergeDoc.h"
23
24 /**
25  * @brief Additional action codes for WinMerge.
26  * @note Reserve first 100 for CrystalEditor
27  */
28 enum
29 {
30         CE_ACTION_MERGE = 100, /**< Merging action */
31 };
32
33 /**
34  * @brief Return statuses of file rescan
35  */
36 enum
37 {
38         RESCAN_OK = 0, /**< Rescan succeeded */
39         RESCAN_SUPPRESSED, /**< Rescan not done - suppressed */
40         RESCAN_FILE_ERR, /**< Error reading file */
41         RESCAN_TEMP_ERR, /**< Error saving to temp file */
42 };
43
44 /**
45  * @brief File saving statuses
46  */
47 enum
48 {
49         SAVE_DONE = 0, /**< Saving succeeded */  
50         SAVE_FAILED, /**< Saving failed */  
51         SAVE_PACK_FAILED, /**< Plugin failed to pack file */  
52         SAVE_NO_FILENAME, /**< File has no filename */  
53         SAVE_CANCELLED, /**< Saving was cancelled */  
54 };
55
56 /**
57  * @brief Types for buffer. Buffer's type defines behavior
58  * of buffer when saving etc.
59  * 
60  * Difference between BUFFERTYPE::NORMAL and BUFFERTYPE::NORMAL_NAMED is
61  * that _NAMED has description text given and which is shown
62  * instead of filename.
63  *
64  * BUFFERTYPE::UNNAMED is created empty buffer (scratchpad), it has
65  * no filename, and default description is given for it. After
66  * this buffer is saved it becomes _SAVED. It is not equal to
67  * NORMAL_NAMED, since scratchpads don't have plugins etc.
68  */
69 enum class BUFFERTYPE
70 {
71         NORMAL = 0, /**< Normal, file loaded from disk */
72         NORMAL_NAMED, /**< Normal, description given */
73         UNNAMED, /**< Empty, created buffer */
74         UNNAMED_SAVED, /**< Empty buffer saved with filename */
75 };
76
77 struct WordDiff {
78         std::array<int, 3> begin; // 0-based, eg, begin[0] is from str1
79         std::array<int, 3> end; // 0-based, eg, end[1] is from str2
80         std::array<int, 3> beginline;
81         std::array<int, 3> endline;
82         int op;
83
84         WordDiff(int s1=0, int e1=0, int bl1=0, int el1=0, int s2=0, int e2=0, int bl2=0, int el2=0, int s3=0, int e3=0, int bl3=0, int el3=0, int op=0)
85                 : begin{s1, s2, s3}
86                 , beginline{bl1, bl2, bl3}
87                 , endline{el1, el2, el3}
88                 , op(op)
89         {
90                 if (s1>e1) e1=s1;
91                 if (s2>e2) e2=s2;
92                 if (s3>e3) e3=s3;
93                 end[0] = e1;
94                 end[1] = e2;
95                 end[2] = e3;
96         }
97 };
98
99 struct CurrentWordDiff
100 {
101         int nDiff;
102         size_t nWordDiff;
103         int nPane;
104         CPoint ptStart;
105         CPoint ptEnd;
106 };
107
108 struct DiffFileInfo;
109 class CMergeEditView;
110 class PackingInfo;
111 class PrediffingInfo;
112 class CMergeEditFrame;
113 class CDirDoc;
114 class CEncodingErrorBar;
115 class CLocationView;
116 class CMergeEditSplitterView;
117
118 /**
119  * @brief Document class for merging two files
120  */
121 class CMergeDoc : public CDocument, public IMergeDoc
122 {
123 public:
124         struct TableProps { bool istable; TCHAR delimiter; TCHAR quote; bool allowNewlinesInQuotes; };
125 // Attributes
126 public:
127         static int m_nBuffersTemp;
128
129 // Begin declaration of CMergeDoc
130
131         std::unique_ptr<CDiffTextBuffer> m_ptBuf[3]; /**< Left/Middle/Right side text buffer */
132         int m_nBuffers;
133         int m_nGroups;
134
135 protected: // create from serialization only
136         CMergeDoc();
137         DECLARE_DYNCREATE(CMergeDoc)
138
139         // Operations
140 public: 
141         std::unique_ptr<DiffFileInfo> m_pSaveFileInfo[3];
142         std::unique_ptr<DiffFileInfo> m_pRescanFileInfo[3];
143         DiffList m_diffList;
144         UINT m_nTrivialDiffs; /**< Amount of trivial (ignored) diffs */
145         PathContext m_filePaths; /**< Filepaths for this document */
146         /// String of concatenated filenames as text to apply plugins filter to
147         String m_strBothFilenames;
148
149         CMergeEditView * GetActiveMergeView();
150         CMergeEditView * GetActiveMergeGroupView(int nBuffer);
151         void UpdateHeaderPath(int pane);
152         void UpdateHeaderActivity(int pane, bool bActivate);
153         void RefreshOptions();
154         void UpdateResources();
155         bool OpenDocs(int nFiles, const FileLocation fileloc[],
156                 const bool bRO[], const String strDesc[]);
157         int LoadFile(CString sFileName, int nBuffer, bool & readOnly, const FileTextEncoding & encoding);
158         void MoveOnLoad(int nPane = -1, int nLinIndex = -1, bool bRealLine = false, int nCharIndex = -1);
159         void ChangeFile(int nBuffer, const String& path, int nLineIndex = -1);
160         void RescanIfNeeded(float timeOutInSecond);
161         int Rescan(bool &bBinary, IDENTLEVEL &identical, bool bForced = false);
162         void CheckFileChanged(void) override;
163         int ShowMessageBox(const String& sText, unsigned nType = MB_OK, unsigned nIDHelp = 0);
164         void ShowRescanError(int nRescanResult, IDENTLEVEL identical);
165         bool Undo();
166         void CopyAllList(int srcPane, int dstPane);
167         void CopyMultipleList(int srcPane, int dstPane, int firstDiff, int lastDiff, int firstWordDiff = -1, int lastWordDiff = -1);
168         void CopyMultiplePartialList(int srcPane, int dstPane, int firstDiff, int lastDiff, int firstLineDiff = -1, int lastLineDiff = -1);
169         void DoAutoMerge(int dstPane);
170         bool SanityCheckDiff(const DIFFRANGE& dr) const;
171         bool WordListCopy(int srcPane, int dstPane, int nDiff, int nFirstWordDiff, int nLastWordDiff, const std::vector<int> *pWordDiffIndice, bool bGroupWithPrevious = false, bool bUpdateView = true);
172         bool PartialListCopy(int srcPane, int dstPane, int nDiff, int firstLine, int lastLine = -1, bool bGroupWithPrevious = false, bool bUpdateView = true);
173         bool ListCopy(int srcPane, int dstPane, int nDiff = -1, bool bGroupWithPrevious = false, bool bUpdateView = true);
174         bool TrySaveAs(String& strPath, int &nLastErrorCode, String & sError,
175                 int nBuffer, PackingInfo& infoTempUnpacker);
176         bool DoSave(LPCTSTR szPath, bool &bSaveSuccess, int nBuffer);
177         bool DoSaveAs(LPCTSTR szPath, bool &bSaveSuccess, int nBuffer);
178         int RightLineInMovedBlock(int pane, int line);
179         int LeftLineInMovedBlock(int pane, int line);
180         void SetEditedAfterRescan(int nBuffer);
181         bool IsEditedAfterRescan(int nBuffer = -1) const;
182
183         const PackingInfo* GetUnpacker() const override { return &m_infoUnpacker; }
184         void SetUnpacker(const PackingInfo * infoUnpacker) override;
185         void SetPrediffer(const PrediffingInfo * infoPrediffer);
186         void GetPrediffer(PrediffingInfo * infoPrediffer) const;
187         const PrediffingInfo *GetPrediffer() const override;
188         void AddMergeViews(CMergeEditSplitterView* pMergeEditSplitterView, CMergeEditView* pView[3]);
189         void RemoveMergeViews(CMergeEditSplitterView* pMergeEditSplitterView);
190         void SetLocationView(CLocationView *pLocationView) { m_pLocationView = pLocationView; }
191
192         CDirDoc * GetDirDoc() const override { return m_pDirDoc; }
193         void SetDirDoc(CDirDoc * pDirDoc) override;
194         void DirDocClosing(CDirDoc * pDirDoc) override;
195         bool CloseNow() override;
196         int GetFileCount() const override { return m_filePaths.GetSize(); }
197         String GetPath(int pane) const override { return m_filePaths[pane]; } 
198         bool GetReadOnly(int pane) const override { return m_ptBuf[pane]->m_bReadOnly; }
199         CString GetTooltipString() const override;
200         void SwapFiles(int nFromIndex, int nToIndex);
201
202         CMergeEditView * GetView(int group, int buffer) const { return m_pView[group][buffer]; }
203         CLocationView * GetLocationView() { return m_pLocationView; }
204         std::vector<CMergeEditView *> GetViewList(int nGroup = -1, int nBuffer = -1) const {
205                 std::vector<CMergeEditView *> list;
206                 if (nGroup != -1)
207                         for (int nBuffer2 = 0; nBuffer2 < m_nBuffers; ++nBuffer2)
208                                 list.push_back(m_pView[nGroup][nBuffer2]);
209                 else if (nBuffer != -1)
210                         for (int nGroup2 = 0; nGroup2 < m_nGroups; ++nGroup2)
211                                 list.push_back(m_pView[nGroup2][nBuffer]);
212                 else
213                 {
214                         for (int nGroup3 = 0; nGroup3 < m_nGroups; nGroup3++)
215                                 for (int nBuffer3 = 0; nBuffer3 < m_nBuffers; ++nBuffer3)
216                                         list.push_back(m_pView[nGroup3][nBuffer3]);
217                 }
218                 return list;
219         }
220         template <typename Function>
221         void ForEachView(int nBuffer, Function func) {
222                 for (int nGroup = 0; nGroup < m_nGroups; nGroup++)
223                         func(m_pView[nGroup][nBuffer]);
224         }
225         template <typename Function>
226         void ForEachView(Function func) {
227                 for (int nBuffer = 0; nBuffer < m_nBuffers; ++nBuffer)
228                 {
229                         for (int nGroup = 0; nGroup < m_nGroups; nGroup++)
230                                 func(m_pView[nGroup][nBuffer]);
231                 }
232         }
233         template <typename Function>
234         void ForEachActiveGroupView(Function func) {
235                 int nGroup = GetActiveMergeView()->m_nThisGroup;
236                 for (int nBuffer = 0; nBuffer < m_nBuffers; ++nBuffer)
237                         func(m_pView[nGroup][nBuffer]);
238         }
239         CMergeEditFrame * GetParentFrame();
240
241         void AddSyncPoint();
242         bool DeleteSyncPoint(int pane, int nLine, bool bRescan = true);
243         void ClearSyncPoints();
244         bool HasSyncPoints();
245         std::vector<std::vector<int> > GetSyncPointList();
246         String GetDescription(int pane) const override { return m_strDesc[pane]; }
247         void SetDescription(int pane, const String& sText) { m_strDesc[pane] = sText; }
248
249         // Overrides
250         // ClassWizard generated virtual function overrides
251         //{{AFX_VIRTUAL(CMergeDoc)
252         public:
253         virtual BOOL OnNewDocument();
254         virtual void Serialize(CArchive& ar);
255         virtual BOOL SaveModified();
256         virtual void DeleteContents ();
257         virtual void SetTitle(LPCTSTR lpszTitle);
258         //}}AFX_VIRTUAL
259
260 // Implementation in MergeDocLineDiffs.cpp
261 public:
262         typedef enum { BYTEDIFF, WORDDIFF } DIFFLEVEL;
263         void Showlinediff(CMergeEditView *pView, bool bReversed = false);
264         void AddToSubstitutionFilters(CMergeEditView* pView, bool bReversed = false);
265         void AddToLineFilters(const String& text);
266         std::vector<WordDiff> GetWordDiffArrayInDiffBlock(int nDiff);
267         std::vector<WordDiff> GetWordDiffArray(int nLineIndex);
268         std::vector<WordDiff> GetWordDiffArrayInRange(const int begin[3], const int end[3], int pane1 = -1, int pane2 = -1);
269         void ClearWordDiffCache(int nDiff = -1);
270 private:
271         void Computelinediff(CMergeEditView *pView, CRect rc[], bool bReversed);
272         std::map<int, std::vector<WordDiff> > m_cacheWordDiffs;
273 // End MergeDocLineDiffs.cpp
274
275 // Implementation in MergeDocEncoding.cpp
276 public:
277         bool DoFileEncodingDialog(int pane = -1);
278 // End MergeDocEncoding.cpp
279
280 // Implementation
281 public:
282         FileChange IsFileChangedOnDisk(LPCTSTR szPath, DiffFileInfo &dfi,
283                 bool bSave, int nBuffer);
284         bool PromptAndSaveIfNeeded(bool bAllowCancel);
285         std::vector<int> undoTgt;
286         std::vector<int>::iterator curUndo;
287         void FlushAndRescan(bool bForced = false);
288         void SetCurrentDiff(int nDiff);
289         int GetCurrentDiff() const { return m_nCurDiff; }
290         const CurrentWordDiff& GetCurrentWordDiff() const { return m_CurWordDiff; }
291         bool EqualCurrentWordDiff(int nBuffer, const CPoint& ptStart, const CPoint& ptEnd) const
292         {
293                 return (m_CurWordDiff.nPane == nBuffer && m_CurWordDiff.ptStart == ptStart && m_CurWordDiff.ptEnd == ptEnd);
294         }
295         virtual ~CMergeDoc();
296         void SetDetectMovedBlocks(bool bDetectMovedBlocks);
297         bool IsMixedEOL(int nBuffer) const;
298         bool GenerateReport(const String& sFileName) const override;
299         void SetAutoMerged(bool bAutoMerged) { m_bAutoMerged = bAutoMerged; }
300         bool GetAutoMerged() const { return m_bAutoMerged; };
301         bool IsModified() const
302         {
303                 for (int nBuffer = 0; nBuffer < m_nBuffers; ++nBuffer)
304                         if (m_ptBuf[nBuffer]->IsModified())
305                                 return true;
306                 return false;
307         }
308         bool CanUndo() const
309         {
310                 for (int nBuffer = 0; nBuffer < m_nBuffers; ++nBuffer)
311                         if (m_ptBuf[nBuffer]->CanUndo())
312                                 return true;
313                 return false;
314         }
315
316         std::optional<bool> GetEnableTableEditing() const { return m_bEnableTableEditing; }
317         void SetEnableTableEditing(std::optional<bool> bEnableTableEditing) { m_bEnableTableEditing = bEnableTableEditing; }
318         static TableProps MakeTablePropertiesByFileName(const String& path, const std::optional<bool>& enableTableEditing, bool showDialog = true);
319         void SetPreparedTableProperties(const TableProps& props) { m_pTablePropsPrepared.reset(new TableProps(props)); }
320
321         void SetTextType(int textType);
322         void SetTextType(const String& ext);
323         bool GetChangedSchemeManually() const { return m_bChangedSchemeManually; }
324
325         bool GetAutomaticRescan() const { return m_bAutomaticRescan; }
326         // to customize the mergeview menu
327         HMENU createPrediffersSubmenu(HMENU hMenu);
328
329 // implementation methods
330 private:
331         bool GetBreakType() const;
332         bool GetByteColoringOption() const;
333         bool IsValidCodepageForMergeEditor(unsigned cp) const;
334         void SanityCheckCodepage(FileLocation & fileinfo);
335         DWORD LoadOneFile(int index, const String& filename, bool readOnly, const String& strDesc, const FileTextEncoding & encoding);
336         void SetTableProperties();
337
338 // Implementation data
339 protected:
340         int m_nCurDiff; /**< Selected diff, 0-based index, -1 if no diff selected */
341         CurrentWordDiff m_CurWordDiff;
342         CMergeEditView * m_pView[3][3]; /**< Pointer to left/middle/right view */
343         CMergeEditSplitterView * m_pMergeEditSplitterView[3];
344         CLocationView * m_pLocationView; /**< Pointer to locationview */
345         CDirDoc * m_pDirDoc;
346         bool m_bEnableRescan; /**< Automatic rescan enabled/disabled */
347         COleDateTime m_LastRescan; /**< Time of last rescan (for delaying) */ 
348         CDiffWrapper m_diffWrapper;
349         /// information about the file packer/unpacker
350         PackingInfo m_infoUnpacker;
351         String m_strDesc[3]; /**< Left/Middle/Right side description text */
352         BUFFERTYPE m_nBufferType[3];
353         bool m_bEditAfterRescan[3]; /**< Left/middle/right doc edited after rescanning */
354         TempFile m_tempFiles[3]; /**< Temp files for compared files */
355         int m_nDiffContext;
356         bool m_bInvertDiffContext;
357         bool m_bMixedEol; /**< Does this document have mixed EOL style? */
358         std::unique_ptr<CEncodingErrorBar> m_pEncodingErrorBar;
359         bool m_bHasSyncPoints;
360         bool m_bAutoMerged;
361         std::optional<bool> m_bEnableTableEditing;
362         std::unique_ptr<TableProps> m_pTablePropsPrepared;
363         /**
364          * Are automatic rescans enabled?
365          * If automatic rescans are enabled then we rescan files after edit
366          * events, unless timer suppresses rescan. We suppress rescans within
367          * certain time from previous rescan.
368          */
369         bool m_bAutomaticRescan;
370         /// active prediffer ID : helper to check the radio button
371         int m_CurrentPredifferID;
372         bool m_bChangedSchemeManually;  /**< `true` if the syntax highlighting scheme is changed manually */
373         String m_sCurrentHeaderTitle[3];
374
375 // friend access
376         friend class RescanSuppress;
377
378
379 // Generated message map functions
380 protected:
381         //{{AFX_MSG(CMergeDoc)
382         afx_msg void OnFileSave();
383         afx_msg void OnUpdateFileSave(CCmdUI* pCmdUI);
384         afx_msg void OnFileSaveLeft();
385         afx_msg void OnUpdateFileSaveLeft(CCmdUI* pCmdUI);
386         afx_msg void OnFileSaveMiddle();
387         afx_msg void OnUpdateFileSaveMiddle(CCmdUI* pCmdUI);
388         afx_msg void OnFileSaveRight();
389         afx_msg void OnUpdateFileSaveRight(CCmdUI* pCmdUI);
390         afx_msg void OnFileSaveAsLeft();
391         afx_msg void OnUpdateFileSaveAsMiddle(CCmdUI* pCmdUI);
392         afx_msg void OnFileSaveAsMiddle();
393         afx_msg void OnFileSaveAsRight();
394         afx_msg void OnUpdateStatusNum(CCmdUI* pCmdUI);
395         afx_msg void OnFileReload();
396         afx_msg void OnFileEncoding();
397         afx_msg void OnFileReadOnlyLeft();
398         afx_msg void OnUpdateFileReadOnlyLeft(CCmdUI* pCmdUI);
399         afx_msg void OnFileReadOnlyMiddle();
400         afx_msg void OnUpdateFileReadOnlyMiddle(CCmdUI* pCmdUI);
401         afx_msg void OnFileReadOnlyRight();
402         afx_msg void OnUpdateFileReadOnlyRight(CCmdUI* pCmdUI);
403         afx_msg void OnUpdateStatusRO(CCmdUI* pCmdUI);
404         afx_msg void OnDiffContext(UINT nID);
405         afx_msg void OnUpdateDiffContext(CCmdUI* pCmdUI);
406         afx_msg void OnToolsGenerateReport();
407         afx_msg void OnToolsGeneratePatch();
408         afx_msg void OnOpenWithUnpacker();
409         afx_msg void OnApplyPrediffer();
410         afx_msg void OnBnClickedFileEncoding();
411         afx_msg void OnBnClickedPlugin();
412         afx_msg void OnBnClickedHexView();
413         afx_msg void OnOK();
414         afx_msg void OnFileRecompareAsText();
415         afx_msg void OnFileRecompareAsTable();
416         afx_msg void OnUpdateFileRecompareAsText(CCmdUI* pCmdUI);
417         afx_msg void OnUpdateFileRecompareAsTable(CCmdUI* pCmdUI);
418         afx_msg void OnFileRecompareAs(UINT nID);
419         template<int srcPane, int dstPane>
420         afx_msg void OnViewSwapPanes();
421         afx_msg void OnUpdateSwapContext(CCmdUI* pCmdUI);
422         afx_msg void OnRefresh();
423         afx_msg void OnUpdatePrediffer(CCmdUI* pCmdUI);
424         afx_msg void OnPrediffer(UINT nID );
425         //}}AFX_MSG
426         DECLARE_MESSAGE_MAP()
427 private:
428         void PrimeTextBuffers();
429         void HideLines();
430         void AdjustDiffBlocks();
431         void AdjustDiffBlocks3way();
432         void AdjustDiffBlock(DiffMap & diffmap, const DIFFRANGE & diffrange,
433                 const std::vector<WordDiff>& worddiffs,
434                 int i0, int i1, int lo0, int hi0, int lo1, int hi1);
435         int GetMatchCost(const DIFFRANGE& dr, int i0, int i1, int line0, int line1, const std::vector<WordDiff>& worddiffs);
436         OP_TYPE ComputeOpType3way(const std::vector<std::array<int, 3>>& vlines, size_t index,
437                 const DIFFRANGE& diffrange, const DIFFOPTIONS& diffOptions);
438         void FlagTrivialLines();
439         void FlagMovedLines();
440         String GetFileExt(LPCTSTR sFileName, LPCTSTR sDescription) const;
441         void DoFileSave(int pane);
442         void SetPredifferByMenu(UINT nID);
443 };
444
445 /**
446  * @brief return true if there are synchronization points
447  */
448 inline bool CMergeDoc::HasSyncPoints()
449 {
450         return m_bHasSyncPoints;
451 }
452